-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
📀 Make wgpu
an optional dependency
#359
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,28 +21,31 @@ use std::{ | |
sync::atomic::{AtomicU64, Ordering}, | ||
}; | ||
|
||
#[cfg(feature = "wgpu")] | ||
use wgpu::{ | ||
BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoderDescriptor, ComputePipeline, | ||
Device, Queue, Texture, TextureAspect, TextureUsages, TextureView, TextureViewDimension, | ||
}; | ||
|
||
pub type Error = Box<dyn std::error::Error>; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct ShaderId(usize); | ||
#[derive(Clone, Copy, PartialEq, Eq, Hash)] | ||
pub struct ShaderId(pub usize); | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, Hash)] | ||
pub struct Id(NonZeroU64); | ||
pub struct Id(pub NonZeroU64); | ||
|
||
static ID_COUNTER: AtomicU64 = AtomicU64::new(0); | ||
|
||
#[cfg(feature = "wgpu")] | ||
pub struct Engine { | ||
Comment on lines
-35
to
41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These sections together are a bit strange This suggests the engine module is too entangled with wgpu/that these IDs shouldn't be in this module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like at least there was an intention behind splitting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fwiw, anybody implementing another |
||
shaders: Vec<Shader>, | ||
pool: ResourcePool, | ||
bind_map: BindMap, | ||
downloads: HashMap<Id, Buffer>, | ||
} | ||
|
||
#[cfg(feature = "wgpu")] | ||
struct Shader { | ||
pipeline: ComputePipeline, | ||
bind_group_layout: BindGroupLayout, | ||
|
@@ -56,9 +59,9 @@ pub struct Recording { | |
|
||
#[derive(Clone, Copy)] | ||
pub struct BufProxy { | ||
size: u64, | ||
id: Id, | ||
name: &'static str, | ||
pub size: u64, | ||
pub id: Id, | ||
pub name: &'static str, | ||
} | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq)] | ||
|
@@ -70,10 +73,10 @@ pub enum ImageFormat { | |
|
||
#[derive(Clone, Copy)] | ||
pub struct ImageProxy { | ||
width: u32, | ||
height: u32, | ||
format: ImageFormat, | ||
id: Id, | ||
pub width: u32, | ||
pub height: u32, | ||
pub format: ImageFormat, | ||
pub id: Id, | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
|
@@ -82,6 +85,7 @@ pub enum ResourceProxy { | |
Image(ImageProxy), | ||
} | ||
|
||
#[cfg(feature = "wgpu")] | ||
pub enum ExternalResource<'a> { | ||
#[allow(unused)] | ||
Buf(BufProxy, &'a Buffer), | ||
|
@@ -119,19 +123,22 @@ pub enum BindType { | |
// TODO: Uniform, Sampler, maybe others | ||
} | ||
|
||
#[cfg(feature = "wgpu")] | ||
struct BindMapBuffer { | ||
buffer: Buffer, | ||
#[cfg_attr(not(feature = "buffer_labels"), allow(unused))] | ||
label: &'static str, | ||
} | ||
|
||
#[derive(Default)] | ||
#[cfg(feature = "wgpu")] | ||
struct BindMap { | ||
buf_map: HashMap<Id, BindMapBuffer>, | ||
image_map: HashMap<Id, (Texture, TextureView)>, | ||
} | ||
|
||
#[derive(Hash, PartialEq, Eq)] | ||
#[cfg(feature = "wgpu")] | ||
struct BufferProperties { | ||
size: u64, | ||
usages: BufferUsages, | ||
|
@@ -140,10 +147,12 @@ struct BufferProperties { | |
} | ||
|
||
#[derive(Default)] | ||
#[cfg(feature = "wgpu")] | ||
struct ResourcePool { | ||
bufs: HashMap<BufferProperties, Vec<Buffer>>, | ||
} | ||
|
||
#[cfg(feature = "wgpu")] | ||
impl Engine { | ||
pub fn new() -> Engine { | ||
Engine { | ||
|
@@ -534,6 +543,10 @@ impl Recording { | |
ResourceProxy::Image(image) => self.free_image(image), | ||
} | ||
} | ||
|
||
pub fn into_commands(self) -> Vec<Command> { | ||
self.commands | ||
} | ||
} | ||
|
||
impl BufProxy { | ||
|
@@ -548,6 +561,7 @@ impl BufProxy { | |
} | ||
|
||
impl ImageFormat { | ||
#[cfg(feature = "wgpu")] | ||
pub fn to_wgpu(self) -> wgpu::TextureFormat { | ||
match self { | ||
Self::Rgba8 => wgpu::TextureFormat::Rgba8Unorm, | ||
|
@@ -612,6 +626,7 @@ impl Id { | |
} | ||
} | ||
|
||
#[cfg(feature = "wgpu")] | ||
impl BindMap { | ||
fn insert_buf(&mut self, proxy: &BufProxy, buffer: Buffer) { | ||
self.buf_map.insert( | ||
|
@@ -810,6 +825,7 @@ impl BindMap { | |
|
||
const SIZE_CLASS_BITS: u32 = 1; | ||
|
||
#[cfg(feature = "wgpu")] | ||
impl ResourcePool { | ||
/// Get a buffer from the pool or create one. | ||
fn get_buf( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How long does this check take?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI run for that step (on this PR) was ~3 seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough! Useful to have it documented here though. I've got no objection to adding this check on that metric then.