Compare commits
3 Commits
247607a4a6
...
ab76a63256
Author | SHA1 | Date |
---|---|---|
Jarrod Doyle | ab76a63256 | |
Jarrod Doyle | 20e4ff829c | |
Jarrod Doyle | d7f48d4627 |
|
@ -2,7 +2,11 @@ use std::sync::Arc;
|
|||
|
||||
use thiserror::Error;
|
||||
use winit::{
|
||||
dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window,
|
||||
dpi::PhysicalSize,
|
||||
error::{EventLoopError, OsError},
|
||||
event::WindowEvent,
|
||||
event_loop::{EventLoop, EventLoopWindowTarget},
|
||||
window::{Window, WindowBuilder},
|
||||
};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -15,6 +19,10 @@ pub enum ContextError {
|
|||
Adapter,
|
||||
#[error("Device request failed: {0}")]
|
||||
Device(#[from] wgpu::RequestDeviceError),
|
||||
#[error("Event loop creation failed: {0}")]
|
||||
EventLoop(#[from] EventLoopError),
|
||||
#[error("Window creation failed: {0}")]
|
||||
Os(#[from] OsError),
|
||||
}
|
||||
|
||||
pub struct Context<'window> {
|
||||
|
@ -29,10 +37,15 @@ pub struct Context<'window> {
|
|||
}
|
||||
|
||||
impl<'window> Context<'window> {
|
||||
pub async fn new(window: Arc<Window>, limits: wgpu::Limits) -> Result<Self, ContextError> {
|
||||
pub async fn new(
|
||||
window: Arc<Window>,
|
||||
backends: wgpu::Backends,
|
||||
required_features: wgpu::Features,
|
||||
required_limits: wgpu::Limits,
|
||||
) -> Result<Self, ContextError> {
|
||||
log::info!("Initialising WGPU context...");
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
backends: wgpu::Backends::VULKAN,
|
||||
backends,
|
||||
dx12_shader_compiler: Default::default(),
|
||||
..Default::default()
|
||||
});
|
||||
|
@ -63,8 +76,8 @@ impl<'window> Context<'window> {
|
|||
.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
required_features: wgpu::Features::empty(),
|
||||
required_limits: limits,
|
||||
required_features,
|
||||
required_limits,
|
||||
},
|
||||
None,
|
||||
)
|
||||
|
@ -126,3 +139,75 @@ impl<'window> Context<'window> {
|
|||
handled
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContextBuilder {
|
||||
title: String,
|
||||
vsync: bool,
|
||||
resolution: PhysicalSize<u32>,
|
||||
backends: wgpu::Backends,
|
||||
features: wgpu::Features,
|
||||
limits: wgpu::Limits,
|
||||
}
|
||||
|
||||
impl Default for ContextBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: "Crawl".to_string(),
|
||||
vsync: true,
|
||||
resolution: PhysicalSize::new(1280, 720),
|
||||
backends: Default::default(),
|
||||
features: Default::default(),
|
||||
limits: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn with_title(mut self, title: &str) -> Self {
|
||||
self.title = title.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_vsync(mut self, vsync: bool) -> Self {
|
||||
self.vsync = vsync;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_resolution(mut self, width: u32, height: u32) -> Self {
|
||||
self.resolution = PhysicalSize::new(width, height);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_backends(mut self, backends: wgpu::Backends) -> Self {
|
||||
self.backends = backends;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_features(mut self, features: wgpu::Features) -> Self {
|
||||
self.features = features;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_limits(mut self, limits: wgpu::Limits) -> Self {
|
||||
self.limits = limits;
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn build(self) -> Result<(Context<'static>, EventLoop<()>), ContextError> {
|
||||
let event_loop = EventLoop::new()?;
|
||||
let window = Arc::new(
|
||||
WindowBuilder::new()
|
||||
.with_title(self.title)
|
||||
.with_inner_size(self.resolution)
|
||||
.build(&event_loop)?,
|
||||
);
|
||||
|
||||
let context = Context::new(window, self.backends, self.features, self.limits).await?;
|
||||
|
||||
Ok((context, event_loop))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ mod texture;
|
|||
pub use self::{
|
||||
bind_group::{BindGroupBuilder, BindGroupLayoutBuilder},
|
||||
buffer::{BufferExt, BulkBufferBuilder},
|
||||
context::Context,
|
||||
context::{Context, ContextBuilder},
|
||||
texture::{Texture, TextureBuilder},
|
||||
};
|
||||
|
||||
pub use {wgpu, winit};
|
||||
|
|
Loading…
Reference in New Issue