70 lines
2.1 KiB
Rust
70 lines
2.1 KiB
Rust
use winit::{dpi::PhysicalSize, window::Window};
|
|
|
|
pub struct Context {
|
|
pub instance: wgpu::Instance,
|
|
pub size: PhysicalSize<u32>,
|
|
pub surface: wgpu::Surface,
|
|
pub surface_config: wgpu::SurfaceConfiguration,
|
|
pub adapter: wgpu::Adapter,
|
|
pub device: wgpu::Device,
|
|
pub queue: wgpu::Queue,
|
|
}
|
|
|
|
impl Context {
|
|
pub async fn new(window: &Window, limits: wgpu::Limits) -> Self {
|
|
log::info!("Initialising WGPU context...");
|
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
|
backends: wgpu::Backends::VULKAN,
|
|
dx12_shader_compiler: Default::default(),
|
|
});
|
|
|
|
// To be able to start drawing we need a few things:
|
|
// - A surface
|
|
// - A GPU device to draw to the surface
|
|
// - A draw command queue
|
|
log::info!("Initialising window surface...");
|
|
let surface = unsafe { instance.create_surface(&window) }.unwrap();
|
|
|
|
log::info!("Requesting GPU adapter...");
|
|
let adapter = instance
|
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
|
force_fallback_adapter: false,
|
|
compatible_surface: Some(&surface),
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
log::info!("Checking GPU adapter meets requirements");
|
|
log::info!("Requesting GPU device...");
|
|
let (device, queue) = adapter
|
|
.request_device(
|
|
&wgpu::DeviceDescriptor {
|
|
label: None,
|
|
features: wgpu::Features::empty(),
|
|
limits,
|
|
},
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
log::info!("Configuring window surface...");
|
|
let size = window.inner_size();
|
|
let surface_config = surface
|
|
.get_default_config(&adapter, size.width, size.height)
|
|
.unwrap();
|
|
surface.configure(&device, &surface_config);
|
|
|
|
Self {
|
|
instance,
|
|
size,
|
|
surface,
|
|
surface_config,
|
|
adapter,
|
|
device,
|
|
queue,
|
|
}
|
|
}
|
|
}
|