-
Notifications
You must be signed in to change notification settings - Fork 969
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
Switch Binding Arrays on Metal to Argument Buffers #6751
Changes from all commits
fbf5395
00cb1d8
eff15e7
4ce012c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,15 @@ impl BindGroupState { | |
fn run_bench(ctx: &mut Criterion) { | ||
let state = Lazy::new(BindGroupState::new); | ||
|
||
if !state | ||
.device_state | ||
.device | ||
.features() | ||
.contains(wgpu::Features::TEXTURE_BINDING_ARRAY) | ||
{ | ||
return; | ||
} | ||
Comment on lines
+70
to
+72
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. Missed a feature check on this benchmark |
||
|
||
let mut group = ctx.benchmark_group("Bind Group Creation"); | ||
|
||
for count in [5, 50, 500, 5_000, 50_000] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,14 @@ const RAY_QUERY_FUN_MAP_INTERSECTION: &str = "_map_intersection_type"; | |
pub(crate) const ATOMIC_COMP_EXCH_FUNCTION: &str = "naga_atomic_compare_exchange_weak_explicit"; | ||
pub(crate) const MODF_FUNCTION: &str = "naga_modf"; | ||
pub(crate) const FREXP_FUNCTION: &str = "naga_frexp"; | ||
/// For some reason, Metal does not let you have `metal::texture<..>*` as a buffer argument. | ||
/// However, if you put that texture inside a struct, everything is totally fine. This | ||
/// baffles me to no end. | ||
/// | ||
/// As such, we wrap all argument buffers in a struct that has a single generic `<T>` field. | ||
/// This allows `NagaArgumentBufferWrapper<metal::texture<..>>*` to work. The astute among | ||
/// you have noticed that this should be exactly the same to the compiler, and you're correct. | ||
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. 😅 |
||
pub(crate) const ARGUMENT_BUFFER_WRAPPER_STRUCT: &str = "NagaArgumentBufferWrapper"; | ||
|
||
/// Write the Metal name for a Naga numeric type: scalar, vector, or matrix. | ||
/// | ||
|
@@ -275,24 +283,17 @@ impl Display for TypeContext<'_> { | |
crate::TypeInner::RayQuery => { | ||
write!(out, "{RAY_QUERY_TYPE}") | ||
} | ||
crate::TypeInner::BindingArray { base, size } => { | ||
crate::TypeInner::BindingArray { base, .. } => { | ||
let base_tyname = Self { | ||
handle: base, | ||
first_time: false, | ||
..*self | ||
}; | ||
|
||
if let Some(&super::ResolvedBinding::Resource(super::BindTarget { | ||
binding_array_size: Some(override_size), | ||
.. | ||
})) = self.binding | ||
{ | ||
write!(out, "{NAMESPACE}::array<{base_tyname}, {override_size}>") | ||
} else if let crate::ArraySize::Constant(size) = size { | ||
write!(out, "{NAMESPACE}::array<{base_tyname}, {size}>") | ||
} else { | ||
unreachable!("metal requires all arrays be constant sized"); | ||
} | ||
write!( | ||
out, | ||
"constant {ARGUMENT_BUFFER_WRAPPER_STRUCT}<{base_tyname}>*" | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -2551,6 +2552,8 @@ impl<W: Write> Writer<W> { | |
} => true, | ||
_ => false, | ||
}; | ||
let accessing_wrapped_binding_array = | ||
matches!(*base_ty, crate::TypeInner::BindingArray { .. }); | ||
|
||
self.put_access_chain(base, policy, context)?; | ||
if accessing_wrapped_array { | ||
|
@@ -2587,6 +2590,10 @@ impl<W: Write> Writer<W> { | |
|
||
write!(self.out, "]")?; | ||
|
||
if accessing_wrapped_binding_array { | ||
write!(self.out, ".{WRAPPED_ARRAY_FIELD}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -3700,7 +3707,18 @@ impl<W: Write> Writer<W> { | |
} | ||
|
||
fn write_type_defs(&mut self, module: &crate::Module) -> BackendResult { | ||
let mut generated_argument_buffer_wrapper = false; | ||
for (handle, ty) in module.types.iter() { | ||
if let crate::TypeInner::BindingArray { .. } = ty.inner { | ||
if !generated_argument_buffer_wrapper { | ||
writeln!(self.out, "template <typename T>")?; | ||
writeln!(self.out, "struct {ARGUMENT_BUFFER_WRAPPER_STRUCT} {{")?; | ||
writeln!(self.out, "{}T {WRAPPED_ARRAY_FIELD};", back::INDENT)?; | ||
writeln!(self.out, "}};")?; | ||
generated_argument_buffer_wrapper = true; | ||
} | ||
} | ||
|
||
if !ty.needs_alias() { | ||
continue; | ||
} | ||
|
@@ -5131,13 +5149,10 @@ template <typename A> | |
let target = options.get_resource_binding_target(ep, br); | ||
let good = match target { | ||
Some(target) => { | ||
let binding_ty = match module.types[var.ty].inner { | ||
crate::TypeInner::BindingArray { base, .. } => { | ||
&module.types[base].inner | ||
} | ||
ref ty => ty, | ||
}; | ||
match *binding_ty { | ||
// We intentionally don't dereference binding_arrays here, | ||
// so that binding arrays fall to the buffer location. | ||
|
||
match module.types[var.ty].inner { | ||
crate::TypeInner::Image { .. } => target.texture.is_some(), | ||
crate::TypeInner::Sampler { .. } => { | ||
target.sampler.is_some() | ||
|
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.
I think this is a better setting anyway