2022-11-27 06:27:19 +11:00
|
|
|
// Copyright 2022 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Also licensed under MIT license, at your choice.
|
|
|
|
|
|
|
|
//! Simple helpers for managing wgpu state and surfaces.
|
|
|
|
|
2022-11-27 06:56:43 +11:00
|
|
|
use super::Result;
|
|
|
|
|
2022-11-27 06:27:19 +11:00
|
|
|
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
|
2022-12-14 10:35:56 +11:00
|
|
|
use wgpu::{
|
|
|
|
Adapter, Device, Instance, Limits, Queue, RequestAdapterOptions, Surface, SurfaceConfiguration,
|
|
|
|
};
|
2022-11-27 06:27:19 +11:00
|
|
|
|
|
|
|
/// Simple render context that maintains wgpu state for rendering the pipeline.
|
|
|
|
pub struct RenderContext {
|
|
|
|
pub instance: Instance,
|
2022-12-14 10:38:50 +11:00
|
|
|
pub devices: Vec<DeviceHandle>,
|
2022-12-14 10:35:56 +11:00
|
|
|
}
|
|
|
|
|
2022-12-14 10:38:50 +11:00
|
|
|
pub struct DeviceHandle {
|
2022-12-14 10:35:56 +11:00
|
|
|
adapter: Adapter,
|
2022-12-14 10:38:50 +11:00
|
|
|
pub device: Device,
|
|
|
|
pub queue: Queue,
|
2022-11-27 06:27:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderContext {
|
2022-12-14 10:35:56 +11:00
|
|
|
pub fn new() -> Result<Self> {
|
2022-11-27 06:27:19 +11:00
|
|
|
let instance = Instance::new(wgpu::Backends::PRIMARY);
|
|
|
|
Ok(Self {
|
|
|
|
instance,
|
2022-12-14 10:35:56 +11:00
|
|
|
devices: Vec::new(),
|
2022-11-27 06:27:19 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new surface for the specified window and dimensions.
|
2022-12-14 10:35:56 +11:00
|
|
|
pub async fn create_surface<W>(&mut self, window: &W, width: u32, height: u32) -> RenderSurface
|
2022-11-27 06:27:19 +11:00
|
|
|
where
|
|
|
|
W: HasRawWindowHandle + HasRawDisplayHandle,
|
|
|
|
{
|
|
|
|
let surface = unsafe { self.instance.create_surface(window) };
|
|
|
|
let format = wgpu::TextureFormat::Bgra8Unorm;
|
|
|
|
let config = wgpu::SurfaceConfiguration {
|
|
|
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
|
|
format,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
present_mode: wgpu::PresentMode::Fifo,
|
|
|
|
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
|
|
|
};
|
2022-12-14 10:35:56 +11:00
|
|
|
let dev_id = self.device(Some(&surface)).await.unwrap();
|
|
|
|
surface.configure(&self.devices[dev_id].device, &config);
|
|
|
|
RenderSurface {
|
|
|
|
surface,
|
|
|
|
config,
|
|
|
|
dev_id,
|
|
|
|
}
|
2022-11-27 06:27:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resizes the surface to the new dimensions.
|
|
|
|
pub fn resize_surface(&self, surface: &mut RenderSurface, width: u32, height: u32) {
|
|
|
|
surface.config.width = width;
|
|
|
|
surface.config.height = height;
|
2022-12-14 10:35:56 +11:00
|
|
|
surface
|
|
|
|
.surface
|
|
|
|
.configure(&self.devices[surface.dev_id].device, &surface.config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds or creates a compatible device handle id.
|
|
|
|
async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
|
|
|
let compatible = match compatible_surface {
|
|
|
|
Some(s) => self
|
|
|
|
.devices
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, d)| d.adapter.is_surface_supported(s))
|
|
|
|
.map(|(i, _)| i),
|
|
|
|
None => (!self.devices.is_empty()).then_some(0),
|
|
|
|
};
|
|
|
|
if compatible.is_none() {
|
|
|
|
return self.new_device(compatible_surface).await;
|
|
|
|
}
|
2023-01-15 03:07:07 +11:00
|
|
|
compatible
|
2022-12-14 10:35:56 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a compatible device handle id.
|
|
|
|
async fn new_device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
2023-01-22 07:29:23 +11:00
|
|
|
let adapter = wgpu::util::initialize_adapter_from_env_or_default(
|
|
|
|
&self.instance,
|
|
|
|
wgpu::Backends::PRIMARY,
|
|
|
|
compatible_surface,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-12-14 10:35:56 +11:00
|
|
|
let features = adapter.features();
|
|
|
|
let limits = Limits::default();
|
|
|
|
let (device, queue) = adapter
|
|
|
|
.request_device(
|
|
|
|
&wgpu::DeviceDescriptor {
|
|
|
|
label: None,
|
|
|
|
features: features
|
|
|
|
& (wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::CLEAR_TEXTURE),
|
|
|
|
limits,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.ok()?;
|
|
|
|
let device_handle = DeviceHandle {
|
|
|
|
adapter,
|
|
|
|
device,
|
|
|
|
queue,
|
|
|
|
};
|
|
|
|
self.devices.push(device_handle);
|
|
|
|
Some(self.devices.len() - 1)
|
2022-11-27 06:27:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combination of surface and its configuration.
|
|
|
|
pub struct RenderSurface {
|
|
|
|
pub surface: Surface,
|
|
|
|
pub config: SurfaceConfiguration,
|
2022-12-14 10:38:50 +11:00
|
|
|
pub dev_id: usize,
|
2022-11-27 06:27:19 +11:00
|
|
|
}
|