Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

DSL cleanup 1 (#1041)

uuuvn092ed986cb73
diff --git a/crates/uzu-engine/build/common/kernel.rs b/crates/uzu-engine/build/common/kernel.rs
index 961a8c60f..96e1c5924 100644
--- a/crates/uzu-engine/build/common/kernel.rs
+++ b/crates/uzu-engine/build/common/kernel.rs
@@ -11,7 +11,7 @@ pub enum KernelBufferAccess {
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum KernelArgumentType {
Buffer(KernelBufferAccess),
- Constant(Box<str>),
+ Value(Box<str>),
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
diff --git a/crates/uzu-engine/build/common/traitgen.rs b/crates/uzu-engine/build/common/traitgen.rs
index a9c53d57c..3bf992db6 100644
--- a/crates/uzu-engine/build/common/traitgen.rs
+++ b/crates/uzu-engine/build/common/traitgen.rs
@@ -53,7 +53,7 @@ pub fn traitgen(
quote! { impl crate::backends::common::BufferMut<Backend = Self::Backend> }
},
},
- KernelArgumentType::Constant(ty) => {
+ KernelArgumentType::Value(ty) => {
let ty = gpu_type_paths
.resolve_type(ty)
.with_context(|| format!("invalid type for argument `{}`", a.name))?;
diff --git a/crates/uzu-engine/build/cpu/compiler.rs b/crates/uzu-engine/build/cpu/compiler.rs
index ba6082651..9dbfece66 100644
--- a/crates/uzu-engine/build/cpu/compiler.rs
+++ b/crates/uzu-engine/build/cpu/compiler.rs
@@ -22,9 +22,8 @@ use crate::common::{
#[derive(PartialEq, Debug)]
pub enum FunctionArgumentType {
- Buffer(KernelBufferAccess),
- Constant(Type, Option<Expr>),
- Scalar(Type),
+ Pointer(KernelBufferAccess),
+ Value(Type),
Specialization(Type),
}
@@ -44,16 +43,21 @@ impl FunctionArgument {
name: ArgumentName::from(self.name.to_string()),
conditional: self.conditional.is_some(),
ty: match &self.ty {
- FunctionArgumentType::Buffer(access) => KernelArgumentType::Buffer(*access),
- FunctionArgumentType::Constant(ty, None) => KernelArgumentType::Constant(
- format!("&[{}]", canonicalize_type_text(ty, gpu_type_paths)).into_boxed_str(),
- ),
- FunctionArgumentType::Constant(ty, Some(size)) => KernelArgumentType::Constant(
- format!("&[{}; {}]", canonicalize_type_text(ty, gpu_type_paths), size.to_token_stream(),)
- .into_boxed_str(),
- ),
- FunctionArgumentType::Scalar(ty) => {
- KernelArgumentType::Constant(canonicalize_type_text(ty, gpu_type_paths).into_boxed_str())
+ FunctionArgumentType::Pointer(access) => KernelArgumentType::Buffer(*access),
+ FunctionArgumentType::Value(ty) => {
+ let ty = match ty {
+ Type::Reference(reference) => match &*reference.elem {
+ Type::Slice(slice) => format!("&[{}]", canonicalize_type_text(&slice.elem, gpu_type_paths)),
+ Type::Array(array) => format!(
+ "&[{}; {}]",
+ canonicalize_type_text(&array.elem, gpu_type_paths),
+ array.len.to_token_stream()
+ ),
+ _ => unreachable!(),
+ },
+ ty => canonicalize_type_text(ty, gpu_type_paths),
+ };
+ KernelArgumentType::Value(ty.into_boxed_str())
},
FunctionArgumentType::Specialization(_) => {
return None;
@@ -395,7 +399,7 @@ impl CpuCompiler {
quote! { impl crate::backends::common::BufferMut<Backend = crate::backends::cpu::Cpu> }
},
},
- KernelArgumentType::Constant(ty) => {
+ KernelArgumentType::Value(ty) => {
let ty = gpu_type_paths.resolve_type(ty)?;
quote! { #ty }
},
@@ -414,7 +418,7 @@ impl CpuCompiler {
.flat_map(|argument| {
let argument_ident = &argument.name;
match &argument.ty {
- FunctionArgumentType::Buffer(access) => {
+ FunctionArgumentType::Pointer(access) => {
let (buffer_ptr, buffer_ptr_wrapper) = match access {
KernelBufferAccess::Read => (
quote! { crate::backends::cpu::buffer::CpuBufferExt::cpu_address(__dsl_buffer).as_ptr().cast::<u8>().cast_const() },
@@ -444,13 +448,12 @@ impl CpuCompiler {
})
}
},
- FunctionArgumentType::Constant(_, None) => {
- Some(quote! { let #argument_ident = #argument_ident.to_vec().into_boxed_slice(); })
- },
- FunctionArgumentType::Constant(_, Some(_)) => {
- Some(quote! { let #argument_ident = Box::new(*#argument_ident); })
+ FunctionArgumentType::Value(Type::Reference(reference)) => match &*reference.elem {
+ Type::Slice(_) => Some(quote! { let #argument_ident = #argument_ident.to_vec().into_boxed_slice(); }),
+ Type::Array(_) => Some(quote! { let #argument_ident = Box::new(*#argument_ident); }),
+ _ => unreachable!(),
},
- FunctionArgumentType::Scalar(_) => None,
+ FunctionArgumentType::Value(_) => None,
FunctionArgumentType::Specialization(_) => {
Some(quote! { let #argument_ident = self.#argument_ident; })
},
@@ -469,15 +472,15 @@ impl CpuCompiler {
let argument_ident = &argument.name;
match &argument.ty {
- FunctionArgumentType::Buffer(_) => {
+ FunctionArgumentType::Pointer(_) => {
if argument.conditional.is_some() {
quote! { #argument_ident.map(|p| p.as_ptr() as _) }
} else {
quote! { #argument_ident.as_ptr() as _ }
}
},
- FunctionArgumentType::Constant(_, _) => quote! { &*#argument_ident },
- FunctionArgumentType::Scalar(_) | FunctionArgumentType::Specialization(_) => {
+ FunctionArgumentType::Value(Type::Reference(_)) => quote! { &*#argument_ident },
+ FunctionArgumentType::Value(_) | FunctionArgumentType::Specialization(_) => {
quote! { #argument_ident }
},
}
@@ -640,17 +643,20 @@ impl CpuCompiler {
fn parse_type(ty: Type) -> anyhow::Result<FunctionArgumentType> {
Ok(match ty {
- Type::Ptr(ty) => FunctionArgumentType::Buffer(if matches!(ty.mutability, syn::PointerMutability::Mut(_)) {
- KernelBufferAccess::ReadWrite
- } else {
- KernelBufferAccess::Read
- }),
- Type::Reference(ty) => match *ty.elem {
- Type::Slice(ty) => FunctionArgumentType::Constant(*ty.elem, None),
- Type::Array(ty) => FunctionArgumentType::Constant(*ty.elem, Some(ty.len)),
- ty => bail!("unsupported reference type: {} ({:?})", ty.to_token_stream(), ty),
+ Type::Ptr(ty) => {
+ FunctionArgumentType::Pointer(if matches!(ty.mutability, syn::PointerMutability::Mut(_)) {
+ KernelBufferAccess::ReadWrite
+ } else {
+ KernelBufferAccess::Read
+ })
+ },
+ Type::Reference(ty) => {
+ if !matches!(*ty.elem, Type::Slice(_) | Type::Array(_)) {
+ bail!("unsupported reference type: {} ({:?})", ty.elem.to_token_stream(), ty.elem);
+ }
+ FunctionArgumentType::Value(Type::Reference(ty))
},
- Type::Path(ty) => FunctionArgumentType::Scalar(Type::Path(ty)),
+ Type::Path(ty) => FunctionArgumentType::Value(Type::Path(ty)),
ty => bail!("unsupported type: {} ({:?})", ty.to_token_stream(), ty),
})
}
diff --git a/crates/uzu-engine/build/metal/ast.rs b/crates/uzu-engine/build/metal/ast.rs
index eaf8b92ed..e7dc1f33e 100644
--- a/crates/uzu-engine/build/metal/ast.rs
+++ b/crates/uzu-engine/build/metal/ast.rs
@@ -121,18 +121,6 @@ fn annotation_from_ast_node(annotation_node: MetalAstNode) -> anyhow::Result<Box
.collect()
}
-#[derive(Debug, Clone, Copy)]
-pub enum MetalBufferAccess {
- Read,
- ReadWrite,
-}
-
-#[derive(Debug, Clone)]
-pub enum MetalConstantType {
- Scalar,
- Array(Option<Box<str>>),
-}
-
#[derive(Debug, Clone)]
pub enum MetalGroupsType {
Direct(Box<str>),
@@ -141,8 +129,8 @@ pub enum MetalGroupsType {
#[derive(Debug, Clone)]
pub enum MetalArgumentType {
- Buffer(MetalBufferAccess),
- Constant((Box<str>, MetalConstantType)),
+ Device,
+ Constant,
Shared(Option<Box<str>>),
Specialize(Box<str>),
Axis(Box<str>, Box<str>),
@@ -156,6 +144,7 @@ pub struct MetalArgument {
pub name: ArgumentName,
pub c_type: Box<str>,
pub argument_type: MetalArgumentType,
+ pub binding_type: Option<KernelArgumentType>,
pub condition: Option<Box<str>>,
}
@@ -246,11 +235,35 @@ impl MetalArgument {
let (argument_type, condition) =
parse_argument_annotation(&c_type, &source, annotation.as_deref(), integer_defines)?;
+ let binding_type = match argument_type {
+ MetalArgumentType::Device => Some(KernelArgumentType::Buffer(if c_type.contains("const") {
+ KernelBufferAccess::Read
+ } else {
+ KernelBufferAccess::ReadWrite
+ })),
+ MetalArgumentType::Constant => {
+ let ty = Self::scalar_type_to_rust(c_type.split_whitespace().nth(2).unwrap())?;
+ let ty = if c_type.ends_with('*') {
+ if source.contains('[') && source.contains(']') {
+ let lbracket = source.rfind('[').context("sized constant missing size bracket")? + 1;
+ let rbracket = source.rfind(']').context("sized constant missing size bracket")?;
+ format!("&[{ty}; {}]", &source[lbracket..rbracket]).into_boxed_str()
+ } else {
+ format!("&[{ty}]").into_boxed_str()
+ }
+ } else {
+ ty
+ };
+ Some(KernelArgumentType::Value(ty))
+ },
+ _ => None,
+ };
Ok(Self {
name: ArgumentName::from(name),
c_type,
argument_type,
+ binding_type,
condition,
})
}
@@ -268,6 +281,29 @@ impl MetalArgument {
pub fn is_optional_shared(&self) -> bool {
matches!(self.argument_type, MetalArgumentType::Shared(_)) && self.condition.is_some()
}
+
+ pub fn to_common_argument(
+ &self,
+ indirect_used: &mut bool,
+ ) -> Option<KernelArgument> {
+ if let Some(ty) = &self.binding_type {
+ Some(KernelArgument {
+ name: self.name.clone(),
+ conditional: self.condition.is_some(),
+ ty: ty.clone(),
+ })
+ } else if matches!(self.argument_type, MetalArgumentType::Groups(MetalGroupsType::Indirect)) && !*indirect_used
+ {
+ *indirect_used = true;
+ Some(KernelArgument {
+ name: "__dsl_indirect_dispatch_buffer".into(),
+ conditional: false,
+ ty: KernelArgumentType::Buffer(KernelBufferAccess::Read),
+ })
+ } else {
+ None
+ }
+ }
}
fn parse_argument_annotation(
@@ -295,7 +331,7 @@ fn parse_argument_annotation(
let argument_type = parse_argument_type(c_type, source, None, integer_defines)?;
if !matches!(
argument_type,
- MetalArgumentType::Buffer(_) | MetalArgumentType::Constant(_) | MetalArgumentType::Shared(_)
+ MetalArgumentType::Device | MetalArgumentType::Constant | MetalArgumentType::Shared(_)
) {
bail!("Only a buffer, a constant or a threadgroup argument can be optional");
}
@@ -358,33 +394,11 @@ fn parse_argument_type(
}
if c_type.contains("device") && c_type.contains('*') && !c_type.contains('&') {
- return Ok(MetalArgumentType::Buffer(if c_type.contains("const") {
- MetalBufferAccess::Read
- } else {
- MetalBufferAccess::ReadWrite
- }));
+ return Ok(MetalArgumentType::Device);
}
- if let ["const", "constant", c_type_scalar, "&"] = c_type.split_whitespace().collect::<Vec<_>>().as_slice() {
- return Ok(MetalArgumentType::Constant((
- MetalArgument::scalar_type_to_rust(c_type_scalar)?,
- MetalConstantType::Scalar,
- )));
- }
-
- if let ["const", "constant", c_type_scalar, "*"] = c_type.split_whitespace().collect::<Vec<_>>().as_slice() {
- let size = if source.contains('[') && source.contains(']') {
- let lbracket = source.rfind('[').context("sized constant missing size bracket")? + 1;
- let rbracket = source.rfind(']').context("sized constant missing size bracket")?;
- Some(source[lbracket..rbracket].into())
- } else {
- None
- };
-
- return Ok(MetalArgumentType::Constant((
- MetalArgument::scalar_type_to_rust(c_type_scalar)?,
- MetalConstantType::Array(size),
- )));
+ if let ["const", "constant", _, "&" | "*"] = c_type.split_whitespace().collect::<Vec<_>>().as_slice() {
+ return Ok(MetalArgumentType::Constant);
}
if c_type.contains("threadgroup") && c_type.contains('*') {
@@ -479,44 +493,7 @@ impl MetalKernelInfo {
.into_iter()
.chain(self.arguments.iter().filter_map(|a| a.to_parameter()))
.collect(),
- arguments: self
- .arguments
- .iter()
- .filter_map(|a| match &a.argument_type {
- MetalArgumentType::Buffer(access) => Some(KernelArgument {
- name: a.name.clone(),
- conditional: a.condition.is_some(),
- ty: KernelArgumentType::Buffer(match access {
- MetalBufferAccess::Read => KernelBufferAccess::Read,
- MetalBufferAccess::ReadWrite => KernelBufferAccess::ReadWrite,
- }),
- }),
- MetalArgumentType::Groups(MetalGroupsType::Indirect) if !indirect_flag => {
- indirect_flag = true;
- Some(KernelArgument {
- name: "__dsl_indirect_dispatch_buffer".into(),
- conditional: false,
- ty: KernelArgumentType::Buffer(KernelBufferAccess::Read),
- })
- },
- MetalArgumentType::Constant((ty, MetalConstantType::Scalar)) => Some(KernelArgument {
- name: a.name.clone(),
- conditional: a.condition.is_some(),
- ty: KernelArgumentType::Constant(ty.clone()),
- }),
- MetalArgumentType::Constant((ty, MetalConstantType::Array(None))) => Some(KernelArgument {
- name: a.name.clone(),
- conditional: a.condition.is_some(),
- ty: KernelArgumentType::Constant(format!("&[{ty}]").into_boxed_str()),
- }),
- MetalArgumentType::Constant((ty, MetalConstantType::Array(Some(size)))) => Some(KernelArgument {
- name: a.name.clone(),
- conditional: a.condition.is_some(),
- ty: KernelArgumentType::Constant(format!("&[{ty}; {size}]").into_boxed_str()),
- }),
- _ => None,
- })
- .collect(),
+ arguments: self.arguments.iter().filter_map(|a| a.to_common_argument(&mut indirect_flag)).collect(),
})
}
}
diff --git a/crates/uzu-engine/build/metal/bindgen/arguments.rs b/crates/uzu-engine/build/metal/bindgen/arguments.rs
index ca3877f4e..402d6a9c1 100644
--- a/crates/uzu-engine/build/metal/bindgen/arguments.rs
+++ b/crates/uzu-engine/build/metal/bindgen/arguments.rs
@@ -4,12 +4,13 @@ use quote::{format_ident, quote};
use syn::{Expr, Ident, Type};
use crate::{
- common::{expr_rewrite::rewrite_paths_with, gpu_type_paths::GpuTypePaths},
+ common::{
+ expr_rewrite::rewrite_paths_with,
+ gpu_type_paths::GpuTypePaths,
+ kernel::{KernelArgumentType, KernelBufferAccess},
+ },
metal::{
- ast::{
- MetalArgument, MetalArgumentType, MetalBufferAccess, MetalConstantType, MetalGroupsType, MetalKernelInfo,
- shared_element_byte_size,
- },
+ ast::{MetalArgument, MetalArgumentType, MetalGroupsType, MetalKernelInfo, shared_element_byte_size},
bindgen::host_expression_rewriter::HostExpressionRewriter,
enum_path_rewrite::rewrite_for_rust,
},
@@ -17,7 +18,7 @@ use crate::{
pub enum ArgumentEmission {
Buffer(BufferArgument),
- Constant(ConstantArgument),
+ Value(ValueArgument),
Shared(SharedArgument),
IndirectDispatch(IndirectDispatchArgument),
}
@@ -25,18 +26,18 @@ pub enum ArgumentEmission {
pub struct BufferArgument {
name: Ident,
buffer_index: usize,
- access: MetalBufferAccess,
+ access: KernelBufferAccess,
condition: Option<ArgumentCondition>,
}
-pub struct ConstantArgument {
+pub struct ValueArgument {
name: Ident,
buffer_index: usize,
- shape: ConstantShape,
+ shape: ValueShape,
condition: Option<ArgumentCondition>,
}
-enum ConstantShape {
+enum ValueShape {
Scalar(Type),
UnsizedSlice(Type),
SizedArray {
@@ -69,23 +70,20 @@ pub fn parse(
let mut indirect_dispatch_emitted = false;
for argument in kernel.arguments.iter() {
- match &argument.argument_type {
- MetalArgumentType::Buffer(access) => {
+ match &argument.binding_type {
+ Some(KernelArgumentType::Buffer(access)) => {
let buffer = parse_buffer_argument(argument, *access, next_buffer_index, gpu_type_paths)?;
emissions.push(ArgumentEmission::Buffer(buffer));
next_buffer_index += 1;
},
- MetalArgumentType::Constant((rust_type_text, constant_type)) => {
- let constant = parse_constant_argument(
- argument,
- rust_type_text,
- constant_type,
- next_buffer_index,
- gpu_type_paths,
- )?;
- emissions.push(ArgumentEmission::Constant(constant));
+ Some(KernelArgumentType::Value(rust_type_text)) => {
+ let value = parse_value_argument(argument, rust_type_text, next_buffer_index, gpu_type_paths)?;
+ emissions.push(ArgumentEmission::Value(value));
next_buffer_index += 1;
},
+ None => {},
+ }
+ match &argument.argument_type {
MetalArgumentType::Shared(dimensions) if argument.condition.is_some() => {
let shared = parse_shared_argument(
argument,
@@ -139,7 +137,7 @@ fn parse_shared_argument(
fn parse_buffer_argument(
argument: &MetalArgument,
- access: MetalBufferAccess,
+ access: KernelBufferAccess,
buffer_index: usize,
gpu_type_paths: &GpuTypePaths,
) -> Result<BufferArgument> {
@@ -153,31 +151,29 @@ fn parse_buffer_argument(
})
}
-fn parse_constant_argument(
+fn parse_value_argument(
argument: &MetalArgument,
rust_type_text: &str,
- constant_type: &MetalConstantType,
buffer_index: usize,
gpu_type_paths: &GpuTypePaths,
-) -> Result<ConstantArgument> {
+) -> Result<ValueArgument> {
let name = format_ident!("{}", argument.name.as_ref());
- let element_type = gpu_type_paths
+ let ty = gpu_type_paths
.resolve_type(rust_type_text)
- .with_context(|| format!("constant rust type `{}` cannot be parsed", rust_type_text))?;
- let shape = match constant_type {
- MetalConstantType::Scalar => ConstantShape::Scalar(element_type),
- MetalConstantType::Array(None) => ConstantShape::UnsizedSlice(element_type),
- MetalConstantType::Array(Some(size_text)) => {
- let size: Expr = syn::parse_str(size_text)
- .with_context(|| format!("constant array size `{}` cannot be parsed", size_text))?;
- ConstantShape::SizedArray {
- element_type,
- size,
- }
+ .with_context(|| format!("value rust type `{}` cannot be parsed", rust_type_text))?;
+ let shape = match ty {
+ Type::Reference(reference) => match *reference.elem {
+ Type::Slice(slice) => ValueShape::UnsizedSlice(*slice.elem),
+ Type::Array(array) => ValueShape::SizedArray {
+ element_type: *array.elem,
+ size: array.len,
+ },
+ _ => anyhow::bail!("unsupported value reference type `{rust_type_text}`"),
},
+ ty => ValueShape::Scalar(ty),
};
let condition = parse_argument_condition(argument, gpu_type_paths)?;
- Ok(ConstantArgument {
+ Ok(ValueArgument {
name,
buffer_index,
shape,
@@ -236,7 +232,7 @@ impl ArgumentEmission {
pub fn encode_argument_definition(&self) -> Option<TokenStream> {
match self {
ArgumentEmission::Buffer(buffer) => Some(emit_buffer_argument_definition(buffer)),
- ArgumentEmission::Constant(constant) => Some(emit_constant_argument_definition(constant)),
+ ArgumentEmission::Value(value) => Some(emit_value_argument_definition(value)),
ArgumentEmission::Shared(_) => None,
ArgumentEmission::IndirectDispatch(_) => Some(quote! {
__dsl_indirect_dispatch_buffer: impl crate::backends::common::BufferRef<Backend = crate::backends::metal::Metal>
@@ -257,32 +253,32 @@ impl ArgumentEmission {
ArgumentEmission::IndirectDispatch(_) => Some(quote! {
let __dsl_indirect_dispatch_buffer = __dsl_indirect_dispatch_buffer.parts();
}),
- ArgumentEmission::Constant(constant) => {
- let constant_name = &constant.name;
- let buffer_name = format_ident!("__dsl_argument_buffer_{}", constant_name);
- let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", constant_name);
- let (buffer_size, buffer_copy) = match &constant.shape {
- ConstantShape::Scalar(ty) => (
+ ArgumentEmission::Value(value) => {
+ let value_name = &value.name;
+ let buffer_name = format_ident!("__dsl_argument_buffer_{}", value_name);
+ let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", value_name);
+ let (buffer_size, buffer_copy) = match &value.shape {
+ ValueShape::Scalar(ty) => (
quote! { std::mem::size_of::<#ty>() },
- quote! { #buffer_name.copyin(unsafe { std::slice::from_raw_parts((&raw const #constant_name) as *const u8, std::mem::size_of::<#ty>()) }); },
+ quote! { #buffer_name.copyin(unsafe { std::slice::from_raw_parts((&raw const #value_name) as *const u8, std::mem::size_of::<#ty>()) }); },
),
- ConstantShape::UnsizedSlice(ty) => (
- quote! { std::mem::size_of_val::<[#ty]>(#constant_name).max(std::mem::size_of::<#ty>()) },
+ ValueShape::UnsizedSlice(ty) => (
+ quote! { std::mem::size_of_val::<[#ty]>(#value_name).max(std::mem::size_of::<#ty>()) },
quote! {
- if !#constant_name.is_empty() {
- #buffer_name.copyin(unsafe { std::slice::from_raw_parts(#constant_name.as_ptr() as *const u8, std::mem::size_of_val::<[#ty]>(#constant_name)) });
+ if !#value_name.is_empty() {
+ #buffer_name.copyin(unsafe { std::slice::from_raw_parts(#value_name.as_ptr() as *const u8, std::mem::size_of_val::<[#ty]>(#value_name)) });
}
},
),
- ConstantShape::SizedArray {
+ ValueShape::SizedArray {
element_type,
size,
} => (
quote! { std::mem::size_of::<[#element_type; #size]>() },
- quote! { #buffer_name.copyin(unsafe { std::slice::from_raw_parts(#constant_name.as_ptr() as *const u8, std::mem::size_of::<[#element_type; #size]>()) }); },
+ quote! { #buffer_name.copyin(unsafe { std::slice::from_raw_parts(#value_name.as_ptr() as *const u8, std::mem::size_of::<[#element_type; #size]>()) }); },
),
};
- Some(if constant.condition.is_none() {
+ Some(if value.condition.is_none() {
quote! {
let mut #buffer_name = command_buffer.allocate_constant(#buffer_size).unwrap();
#buffer_copy
@@ -291,7 +287,7 @@ impl ArgumentEmission {
} else {
quote! {
let mut #buffer_name;
- let #parts_name = if let Some(#constant_name) = #constant_name {
+ let #parts_name = if let Some(#value_name) = #value_name {
#buffer_name = command_buffer.allocate_constant(#buffer_size).unwrap();
#buffer_copy
Some(#buffer_name.parts())
@@ -316,7 +312,7 @@ impl ArgumentEmission {
write: false,
})
}),
- ArgumentEmission::Constant(constant) => Some(emit_constant_access(constant)),
+ ArgumentEmission::Value(value) => Some(emit_value_access(value)),
ArgumentEmission::Shared(_) => None,
}
}
@@ -324,7 +320,7 @@ impl ArgumentEmission {
pub fn encode_set(&self) -> Option<TokenStream> {
match self {
ArgumentEmission::Buffer(buffer) => Some(emit_buffer_set(buffer)),
- ArgumentEmission::Constant(constant) => Some(emit_constant_set(constant)),
+ ArgumentEmission::Value(value) => Some(emit_value_set(value)),
ArgumentEmission::Shared(shared) => Some(emit_shared_set(shared)),
ArgumentEmission::IndirectDispatch(_) => None,
}
@@ -333,7 +329,7 @@ impl ArgumentEmission {
fn condition(&self) -> Option<&ArgumentCondition> {
match self {
ArgumentEmission::Buffer(buffer) => buffer.condition.as_ref(),
- ArgumentEmission::Constant(constant) => constant.condition.as_ref(),
+ ArgumentEmission::Value(value) => value.condition.as_ref(),
ArgumentEmission::Shared(shared) => Some(&shared.condition),
ArgumentEmission::IndirectDispatch(_) => None,
}
@@ -343,10 +339,10 @@ impl ArgumentEmission {
fn emit_buffer_argument_definition(buffer: &BufferArgument) -> TokenStream {
let name = &buffer.name;
let buffer_argument_type = match buffer.access {
- MetalBufferAccess::Read => quote! {
+ KernelBufferAccess::Read => quote! {
impl crate::backends::common::BufferRef<Backend = crate::backends::metal::Metal>
},
- MetalBufferAccess::ReadWrite => quote! {
+ KernelBufferAccess::ReadWrite => quote! {
impl crate::backends::common::BufferMut<Backend = crate::backends::metal::Metal>
},
};
@@ -357,17 +353,17 @@ fn emit_buffer_argument_definition(buffer: &BufferArgument) -> TokenStream {
}
}
-fn emit_constant_argument_definition(constant: &ConstantArgument) -> TokenStream {
- let name = &constant.name;
- let base_type = match &constant.shape {
- ConstantShape::Scalar(element_type) => quote! { #element_type },
- ConstantShape::UnsizedSlice(element_type) => quote! { &[#element_type] },
- ConstantShape::SizedArray {
+fn emit_value_argument_definition(value: &ValueArgument) -> TokenStream {
+ let name = &value.name;
+ let base_type = match &value.shape {
+ ValueShape::Scalar(element_type) => quote! { #element_type },
+ ValueShape::UnsizedSlice(element_type) => quote! { &[#element_type] },
+ ValueShape::SizedArray {
element_type,
size,
} => quote! { &[#element_type; #size] },
};
- if constant.condition.is_some() {
+ if value.condition.is_some() {
quote! { #name: Option<#base_type> }
} else {
quote! { #name: #base_type }
@@ -376,7 +372,7 @@ fn emit_constant_argument_definition(constant: &ConstantArgument) -> TokenStream
fn emit_buffer_access(buffer: &BufferArgument) -> TokenStream {
let name = &buffer.name;
- let write = matches!(buffer.access, MetalBufferAccess::ReadWrite);
+ let write = matches!(buffer.access, KernelBufferAccess::ReadWrite);
let access_expression = quote! {
crate::backends::metal::command_buffer::Access {
range: #name.0.gpu_address_subrange(#name.1),
@@ -390,15 +386,15 @@ fn emit_buffer_access(buffer: &BufferArgument) -> TokenStream {
}
}
-fn emit_constant_access(constant: &ConstantArgument) -> TokenStream {
- let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", constant.name);
+fn emit_value_access(value: &ValueArgument) -> TokenStream {
+ let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", value.name);
let access_expression = quote! {
crate::backends::metal::command_buffer::Access {
range: #parts_name.0.gpu_address_subrange(#parts_name.1),
write: false,
}
};
- if constant.condition.is_some() {
+ if value.condition.is_some() {
quote! { #parts_name.as_ref().map(|#parts_name| #access_expression) }
} else {
quote! { Some(#access_expression) }
@@ -425,13 +421,13 @@ fn emit_buffer_set(buffer: &BufferArgument) -> TokenStream {
}
}
-fn emit_constant_set(constant: &ConstantArgument) -> TokenStream {
- let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", constant.name);
- let buffer_index = constant.buffer_index;
+fn emit_value_set(value: &ValueArgument) -> TokenStream {
+ let parts_name = format_ident!("__dsl_argument_buffer_parts_{}", value.name);
+ let buffer_index = value.buffer_index;
let unconditional_set = quote! {
command_buffer.argument_table.set_address_at_index(#parts_name.0.gpu_address() + #parts_name.1.start as u64, #buffer_index);
};
- match &constant.condition {
+ match &value.condition {
Some(condition) => {
let field_name = &condition.field_name;
quote! {
diff --git a/crates/uzu-engine/build/metal/wrapper.rs b/crates/uzu-engine/build/metal/wrapper.rs
index 63a31b878..174e61c46 100644
--- a/crates/uzu-engine/build/metal/wrapper.rs
+++ b/crates/uzu-engine/build/metal/wrapper.rs
@@ -211,7 +211,7 @@ fn kernel_wrappers(
let (mut wrapper_arguments, condition_definitions): (Vec<_>, Vec<_>) = kernel
.arguments
.iter()
- .filter(|a| matches!(&a.argument_type, MetalArgumentType::Buffer(_) | MetalArgumentType::Constant(_)))
+ .filter(|a| matches!(&a.argument_type, MetalArgumentType::Device | MetalArgumentType::Constant))
.enumerate()
.map(|(i, a)| {
let condition_field = format!("__dsl_buffer_condition_{}_{}", wrapper_name, a.name);
@@ -275,8 +275,8 @@ fn kernel_wrappers(
MetalArgumentType::Shared(_) if a.condition.is_some() => {
format!("({}){}", a.c_type, a.name)
},
- MetalArgumentType::Buffer(_)
- | MetalArgumentType::Constant(_)
+ MetalArgumentType::Device
+ | MetalArgumentType::Constant
| MetalArgumentType::Shared(_)
| MetalArgumentType::Specialize(_) => a.name.to_string(),
MetalArgumentType::Axis(..) => {
diff --git a/crates/uzu-engine/build/slang/compiler.rs b/crates/uzu-engine/build/slang/compiler.rs
index 91c7814ae..3840cf773 100644
--- a/crates/uzu-engine/build/slang/compiler.rs
+++ b/crates/uzu-engine/build/slang/compiler.rs
@@ -62,7 +62,7 @@ impl<T: SlangTarget> Compiler for SlangCompiler<T> {
fn build(
&self,
gpu_types: &GpuTypes,
- _gpu_type_paths: &GpuTypePaths,
+ gpu_type_paths: &GpuTypePaths,
) -> anyhow::Result<HashMap<KernelPath, Box<[Kernel]>>> {
let gpu_type_map = Arc::new(
gpu_type_gen(&self.fields.source_directory.join("generated"), gpu_types)
@@ -83,8 +83,10 @@ impl<T: SlangTarget> Compiler for SlangCompiler<T> {
let kernels = slang_sources
.into_par_iter()
.map(|path| {
- self.with_blocking_compiler(|compiler| compiler.compile(path.clone(), gpu_type_map.clone()))
- .with_context(|| format!("cannot compile {}", path.display()))
+ self.with_blocking_compiler(|compiler| {
+ compiler.compile(path.clone(), gpu_type_map.clone(), gpu_type_paths)
+ })
+ .with_context(|| format!("cannot compile {}", path.display()))
})
.collect::<anyhow::Result<HashMap<Box<[Box<str>]>, Box<[Kernel]>>>>()?;
diff --git a/crates/uzu-engine/build/slang/compiler_internal.rs b/crates/uzu-engine/build/slang/compiler_internal.rs
index 163b0464b..0f03ee17f 100644
--- a/crates/uzu-engine/build/slang/compiler_internal.rs
+++ b/crates/uzu-engine/build/slang/compiler_internal.rs
@@ -18,7 +18,7 @@ use shader_slang::{
};
use crate::{
- common::{caching, codegen::write_tokens, kernel::Kernel},
+ common::{caching, codegen::write_tokens, gpu_type_paths::GpuTypePaths, kernel::Kernel},
debug_log,
slang::{reflection::SlangKernel, slang_sys_ext::SessionLoadModuleWithDiagnosticsExt, wrapper},
};
@@ -35,6 +35,7 @@ pub trait SlangTarget: Send + Sync + Clone + 'static {
linked_component: &ComponentType,
kernels: &[SlangKernel],
gpu_type_map: &HashMap<String, String>,
+ gpu_type_paths: &GpuTypePaths,
constants: &HashMap<String, String>,
object_file: &Path,
) -> anyhow::Result<TokenStream>;
@@ -121,6 +122,7 @@ impl<T: SlangTarget> BlockingSlangCompiler<T> {
&self,
source_path: PathBuf,
gpu_type_map: Arc<HashMap<String, String>>,
+ gpu_type_paths: &GpuTypePaths,
) -> anyhow::Result<(Box<[Box<str>]>, Box<[Kernel]>)> {
let source_path_relative =
source_path.strip_prefix(&self.fields.source_directory).context("source is not in src_dir")?;
@@ -174,10 +176,11 @@ impl<T: SlangTarget> BlockingSlangCompiler<T> {
.collect::<anyhow::Result<HashMap<String, [u8; blake3::OUT_LEN]>>>()
.context("cannot hash dependencies")?;
+ let common_gpu_type_map = gpu_type_map.keys().map(|name| (name.clone(), name.clone())).collect();
let kernels = source_module
.module_reflection()
.children()
- .filter_map(|decl| SlangKernel::from_reflection(decl).transpose())
+ .filter_map(|decl| SlangKernel::from_reflection(decl, &common_gpu_type_map).transpose())
.collect::<anyhow::Result<Vec<SlangKernel>>>()
.context("cannot collect kernels")?;
@@ -232,13 +235,13 @@ impl<T: SlangTarget> BlockingSlangCompiler<T> {
T::validate_output(&output_file).with_context(|| format!("cannot validate {}", output_file.display()))?;
- let bindgen_tokens = T::bindgen_file(&linked_component, &kernels, &gpu_type_map, &constants, &output_file)
- .context("cannot generate bindings")?;
+ let bindgen_tokens =
+ T::bindgen_file(&linked_component, &kernels, &gpu_type_map, gpu_type_paths, &constants, &output_file)
+ .context("cannot generate bindings")?;
write_tokens(bindgen_tokens, &bindgen_file).context("cannot write bindgen file")?;
}
- let common_gpu_type_map = gpu_type_map.keys().map(|name| (name.clone(), name.clone())).collect();
let public_kernels = kernels
.iter()
.filter_map(|slang_kernel| {
diff --git a/crates/uzu-engine/build/slang/reflection/argument.rs b/crates/uzu-engine/build/slang/reflection/argument.rs
index c8072313a..b826e5f03 100644
--- a/crates/uzu-engine/build/slang/reflection/argument.rs
+++ b/crates/uzu-engine/build/slang/reflection/argument.rs
@@ -1,7 +1,4 @@
-use std::{
- borrow::Cow,
- collections::{HashMap, hash_map::Entry},
-};
+use std::collections::{HashMap, hash_map::Entry};
use anyhow::{Context, anyhow, bail};
use shader_slang::reflection::{UserAttribute, Variable};
@@ -16,9 +13,7 @@ use crate::{
#[derive(Debug)]
pub enum SlangBufferAccess {
- Read {
- is_constant: bool,
- },
+ Read,
ReadWrite,
}
@@ -28,7 +23,7 @@ pub enum SlangArgumentType {
access_type: SlangBufferAccess,
condition: Option<String>,
},
- Constant {
+ Uniform {
condition: Option<String>,
},
Specialize {
@@ -52,10 +47,14 @@ pub struct SlangArgument {
pub name: String,
pub slang_type: String,
pub argument_type: SlangArgumentType,
+ pub binding_type: Option<KernelArgumentType>,
}
impl SlangArgument {
- pub fn from_reflection(variable: &Variable) -> anyhow::Result<Self> {
+ pub fn from_reflection(
+ variable: &Variable,
+ gpu_type_map: &HashMap<String, String>,
+ ) -> anyhow::Result<Self> {
let name = variable.name().unwrap().to_string();
let ty = variable.ty().unwrap();
@@ -93,6 +92,7 @@ impl SlangArgument {
Ok(())
};
+ let mut binding_type = None;
let argument_type = if let Some(specialize_attribute) = user_attributes.remove("Specialize") {
if specialize_attribute.argument_count() != 0 {
bail!("specialize expected no argument, found {}", specialize_attribute.argument_count());
@@ -148,18 +148,30 @@ impl SlangArgument {
SlangArgumentType::ThreadContext
} else if type_name == "StructuredBuffer" {
let condition = get_condition(&mut user_attributes)?;
- let is_constant = user_attributes.remove("Constant").is_some();
+ binding_type = Some(if user_attributes.remove("Constant").is_some() {
+ let element_type = type_full_name
+ .strip_prefix("StructuredBuffer<")
+ .and_then(|ty| ty.strip_suffix(", DefaultDataLayout>"))
+ .with_context(|| format!("value buffer argument doesn't match StructuredBuffer<T, DefaultDataLayout>: {type_full_name}"))?;
+ KernelArgumentType::Value(
+ slang2rust(&format!("{element_type}[]"), gpu_type_map)
+ .with_context(|| format!("cannot convert slang {type_full_name} to rust"))?
+ .into_owned()
+ .into_boxed_str(),
+ )
+ } else {
+ KernelArgumentType::Buffer(KernelBufferAccess::Read)
+ });
no_attributes(&user_attributes, "buffer (read-only)")?;
SlangArgumentType::Buffer {
- access_type: SlangBufferAccess::Read {
- is_constant,
- },
+ access_type: SlangBufferAccess::Read,
condition,
}
} else if type_name == "RWStructuredBuffer" {
let condition = get_condition(&mut user_attributes)?;
+ binding_type = Some(KernelArgumentType::Buffer(KernelBufferAccess::ReadWrite));
no_attributes(&user_attributes, "buffer (read-write)")?;
@@ -170,9 +182,15 @@ impl SlangArgument {
} else {
let condition = get_condition(&mut user_attributes)?;
- no_attributes(&user_attributes, "constant")?;
+ no_attributes(&user_attributes, "uniform")?;
+ binding_type = Some(KernelArgumentType::Value(
+ slang2rust(&type_full_name, gpu_type_map)
+ .with_context(|| format!("cannot convert slang {type_full_name} to rust"))?
+ .into_owned()
+ .into_boxed_str(),
+ ));
- SlangArgumentType::Constant {
+ SlangArgumentType::Uniform {
condition,
}
};
@@ -181,6 +199,7 @@ impl SlangArgument {
name,
slang_type: type_full_name,
argument_type,
+ binding_type,
})
}
@@ -188,25 +207,7 @@ impl SlangArgument {
&self,
gpu_type_map: &HashMap<String, String>,
) -> anyhow::Result<Box<str>> {
- let slang_type = if matches!(
- self.argument_type,
- SlangArgumentType::Buffer {
- access_type: SlangBufferAccess::Read {
- is_constant: true,
- },
- condition: _,
- }
- ) {
- let element_type = self.slang_type
- .strip_prefix("StructuredBuffer<")
- .and_then(|slang_type| slang_type.strip_suffix(", DefaultDataLayout>")).with_context(|| format!("buffer-constant argument doesn't match the StructuredBuffer<T, DefaultDataLayout> template: {}", self.slang_type))?;
-
- Cow::Owned(format!("{element_type}[]"))
- } else {
- Cow::Borrowed(&self.slang_type)
- };
-
- Ok(slang2rust(&slang_type, gpu_type_map)
+ Ok(slang2rust(&self.slang_type, gpu_type_map)
.with_context(|| format!("cannot convert slang {} to rust", self.slang_type))?
.to_string()
.into_boxed_str())
@@ -235,52 +236,38 @@ impl SlangArgument {
pub fn to_common_argument(
&self,
- gpu_type_map: &HashMap<String, String>,
indirect_used: &mut bool,
- ) -> anyhow::Result<Option<KernelArgument>> {
- match &self.argument_type {
- SlangArgumentType::Buffer {
- access_type:
- access_type @ (SlangBufferAccess::Read {
- is_constant: false,
- }
- | SlangBufferAccess::ReadWrite),
- condition,
- } => Ok(Some(KernelArgument {
- name: ArgumentName::from(self.name.clone()),
- conditional: condition.is_some(),
- ty: KernelArgumentType::Buffer(match *access_type {
- SlangBufferAccess::Read {
- is_constant: _,
- } => KernelBufferAccess::Read,
- SlangBufferAccess::ReadWrite => KernelBufferAccess::ReadWrite,
- }),
- })),
- SlangArgumentType::Constant {
- condition,
- }
- | SlangArgumentType::Buffer {
- access_type: SlangBufferAccess::Read {
- is_constant: true,
- },
- condition,
- } => Ok(Some(KernelArgument {
+ ) -> Option<KernelArgument> {
+ if let Some(ty) = &self.binding_type {
+ let condition = match &self.argument_type {
+ SlangArgumentType::Buffer {
+ condition,
+ ..
+ }
+ | SlangArgumentType::Uniform {
+ condition,
+ } => condition,
+ _ => unreachable!(),
+ };
+ Some(KernelArgument {
name: ArgumentName::from(self.name.clone()),
conditional: condition.is_some(),
- ty: KernelArgumentType::Constant(self.rust_type(gpu_type_map)?),
- })),
- SlangArgumentType::Groups {
- groups,
- } if !*indirect_used && groups == "INDIRECT" => {
- *indirect_used = true;
-
- Ok(Some(KernelArgument {
- name: ArgumentName::from("__dsl_indirect_dispatch_buffer".to_string()),
- conditional: false,
- ty: KernelArgumentType::Buffer(KernelBufferAccess::Read),
- }))
- },
- _ => Ok(None),
+ ty: ty.clone(),
+ })
+ } else if let SlangArgumentType::Groups {
+ groups,
+ } = &self.argument_type
+ && groups == "INDIRECT"
+ && !*indirect_used
+ {
+ *indirect_used = true;
+ Some(KernelArgument {
+ name: ArgumentName::from("__dsl_indirect_dispatch_buffer".to_string()),
+ conditional: false,
+ ty: KernelArgumentType::Buffer(KernelBufferAccess::Read),
+ })
+ } else {
+ None
}
}
}
diff --git a/crates/uzu-engine/build/slang/reflection/kernel.rs b/crates/uzu-engine/build/slang/reflection/kernel.rs
index 5cb768ad4..62aff65d5 100644
--- a/crates/uzu-engine/build/slang/reflection/kernel.rs
+++ b/crates/uzu-engine/build/slang/reflection/kernel.rs
@@ -27,7 +27,10 @@ pub struct SlangKernel {
}
impl SlangKernel {
- pub fn from_reflection(toplevel_decl: &Decl) -> anyhow::Result<Option<Self>> {
+ pub fn from_reflection(
+ toplevel_decl: &Decl,
+ gpu_type_map: &HashMap<String, String>,
+ ) -> anyhow::Result<Option<Self>> {
let (generic_decl, function_decl) = if toplevel_decl.kind() == DeclKind::Generic {
(Some(toplevel_decl), toplevel_decl.as_generic().unwrap().inner_decl().unwrap())
} else {
@@ -77,7 +80,7 @@ impl SlangKernel {
let arguments = function
.parameters()
- .map(SlangArgument::from_reflection)
+ .map(|variable| SlangArgument::from_reflection(variable, gpu_type_map))
.collect::<anyhow::Result<Vec<SlangArgument>>>()
.context("cannot collect arguments")?;
@@ -158,12 +161,8 @@ impl SlangKernel {
self.arguments
.iter()
- .filter_map(|argument| {
- SlangArgument::to_common_argument(argument, gpu_type_map, &mut indirect_used)
- .with_context(|| format!("cannot convert argument {} to common argument", argument.name))
- .transpose()
- })
- .collect::<anyhow::Result<Box<[KernelArgument]>>>()?
+ .filter_map(|argument| argument.to_common_argument(&mut indirect_used))
+ .collect::<Box<[KernelArgument]>>()
},
}))
}
diff --git a/crates/uzu-engine/build/slang/wrapper.rs b/crates/uzu-engine/build/slang/wrapper.rs
index 9b60c9ba6..2e5bb7402 100644
--- a/crates/uzu-engine/build/slang/wrapper.rs
+++ b/crates/uzu-engine/build/slang/wrapper.rs
@@ -111,7 +111,7 @@ pub fn kernel_wrappers(kernel: &SlangKernel) -> anyhow::Result<Vec<String>> {
} => {
wrapper.push(format!(" {} {},", specializer.specialize(&argument.slang_type), argument.name));
},
- SlangArgumentType::Constant {
+ SlangArgumentType::Uniform {
..
} => {
wrapper.push(format!(
@@ -189,7 +189,7 @@ pub fn kernel_wrappers(kernel: &SlangKernel) -> anyhow::Result<Vec<String>> {
SlangArgumentType::Buffer {
..
}
- | SlangArgumentType::Constant {
+ | SlangArgumentType::Uniform {
..
} => argument.name.to_string(),
SlangArgumentType::Specialize {
@@ -241,7 +241,7 @@ fn slang_axis_expr(
return None;
}
match &argument.argument_type {
- SlangArgumentType::Constant {
+ SlangArgumentType::Uniform {
condition,
} => condition.as_deref(),
_ => None,
diff --git a/crates/uzu-engine/build/webgpu/bindgen_file.rs b/crates/uzu-engine/build/webgpu/bindgen_file.rs
index 9ce77eba9..2f3f06e8e 100644
--- a/crates/uzu-engine/build/webgpu/bindgen_file.rs
+++ b/crates/uzu-engine/build/webgpu/bindgen_file.rs
@@ -13,7 +13,11 @@ use shader_slang::{
use syn::{Expr, Ident, Type};
use crate::{
- common::{kernel::KernelParameterType, mangling::static_mangle},
+ common::{
+ gpu_type_paths::GpuTypePaths,
+ kernel::{KernelArgumentType, KernelBufferAccess, KernelParameterType},
+ mangling::static_mangle,
+ },
slang::{SlangArgumentType, SlangBufferAccess, SlangKernel, SlangParameterType, Specializer, slang2rust},
};
@@ -21,6 +25,7 @@ pub fn bindgen_file(
linked_component: &ComponentType,
kernels: &[SlangKernel],
gpu_type_map: &HashMap<String, String>,
+ gpu_type_paths: &GpuTypePaths,
constants: &HashMap<String, String>,
object_file: &Path,
) -> anyhow::Result<TokenStream> {
@@ -30,7 +35,9 @@ pub fn bindgen_file(
let generated_kernels = kernels
.iter()
- .map(|kernel| bindgen_kernel(linked_component, shader, kernel, gpu_type_map, constants, &object_constant))
+ .map(|kernel| {
+ bindgen_kernel(linked_component, shader, kernel, gpu_type_map, gpu_type_paths, constants, &object_constant)
+ })
.collect::<anyhow::Result<Vec<TokenStream>>>()?;
Ok(quote! {
@@ -46,6 +53,7 @@ fn bindgen_kernel(
shader: &Shader,
kernel: &SlangKernel,
gpu_type_map: &HashMap<String, String>,
+ gpu_type_paths: &GpuTypePaths,
constants: &HashMap<String, String>,
object_constant: &Ident,
) -> anyhow::Result<TokenStream> {
@@ -164,89 +172,46 @@ fn bindgen_kernel(
let specialization_constants = specialization_constants.into_iter().flatten().collect::<Vec<_>>();
let mut indirect_used = false;
-
- let (kernel_encode_arguments_definitions, wrapper_encode_arguments_definitions, kernel_wrapper_call_arguments) = kernel
+ let binding_arguments = kernel
.arguments
.iter()
- .map(|argument| {
- match &argument.argument_type {
- SlangArgumentType::Buffer {
- access_type: _,
- condition,
- }
- | SlangArgumentType::Constant {
- condition,
- } => {
- let argument_name = format_ident!("{}", argument.name);
-
- let (mut kernel_argument_type, mut wrapper_argument_type, mut kernel_wrapper_call_argument) = match &argument.argument_type {
- SlangArgumentType::Buffer {
- access_type:
- access_type @ (SlangBufferAccess::Read {
- is_constant: false,
- }
- | SlangBufferAccess::ReadWrite),
- condition: _,
- } => {
- let buffer_arg_type = match access_type {
- SlangBufferAccess::Read { .. } => quote! { BufferRef },
- SlangBufferAccess::ReadWrite => quote! { BufferMut },
- };
+ .filter_map(|argument| argument.to_common_argument(&mut indirect_used))
+ .collect::<Vec<_>>();
- (
- quote! { impl crate::backends::common::#buffer_arg_type<Backend = crate::backends::webgpu::WebGPU> },
- quote! { (&wgpu::Buffer, usize, usize) },
- Some(quote! { { let (buffer, range) = #argument_name.parts(); binding_parts(buffer, range) } }),
- )
- },
- SlangArgumentType::Constant {
- condition: _,
- }
- | SlangArgumentType::Buffer {
- access_type:
- SlangBufferAccess::Read {
- is_constant: true,
- },
- condition: _,
- } => {
- let ty: Type = argument.rust_type(gpu_type_map).and_then(|ty| {
- syn::parse_str(&ty).with_context(|| format!("cannot parse the type of argument {}", argument.name))
- })?;
-
- (quote! { #ty }, quote! { #ty }, None)
- },
- _ => unreachable!(),
+ let (kernel_encode_arguments_definitions, wrapper_encode_arguments_definitions, kernel_wrapper_call_arguments) = binding_arguments
+ .iter()
+ .map(|argument| {
+ let argument_name = format_ident!("{}", argument.name.as_ref());
+ let (mut kernel_argument_type, mut wrapper_argument_type, mut conversion) = match &argument.ty {
+ KernelArgumentType::Buffer(access) => {
+ let buffer_arg_type = match access {
+ KernelBufferAccess::Read => quote! { BufferRef },
+ KernelBufferAccess::ReadWrite => quote! { BufferMut },
};
-
- if let Some(_condition) = condition {
- kernel_argument_type = quote! { Option<#kernel_argument_type> };
- wrapper_argument_type = quote! { Option<#wrapper_argument_type> };
- kernel_wrapper_call_argument = kernel_wrapper_call_argument.map(|conversion| {
- quote! { #argument_name.map(|#argument_name| #conversion) }
- });
- }
-
- let kernel_wrapper_call_argument = kernel_wrapper_call_argument.unwrap_or_else(|| quote! { #argument_name });
-
- Ok(Some((
- quote! { #argument_name: #kernel_argument_type },
- quote! { #[allow(unused)] #argument_name: #wrapper_argument_type },
- quote! { #kernel_wrapper_call_argument },
- )))
+ (
+ quote! { impl crate::backends::common::#buffer_arg_type<Backend = crate::backends::webgpu::WebGPU> },
+ quote! { (&wgpu::Buffer, usize, usize) },
+ Some(quote! { { let (buffer, range) = #argument_name.parts(); binding_parts(buffer, range) } }),
+ )
},
- SlangArgumentType::Groups { groups } if !indirect_used && groups == "INDIRECT" => {
- indirect_used = true;
-
- Ok(Some((
- quote! { __dsl_indirect_dispatch_buffer: impl crate::backends::common::BufferRef<Backend = crate::backends::webgpu::WebGPU> },
- quote! { #[allow(unused)] __dsl_indirect_dispatch_buffer: (&wgpu::Buffer, usize, usize) },
- quote! { { let (buffer, range) = __dsl_indirect_dispatch_buffer.parts(); binding_parts(buffer, range) } },
- )))
+ KernelArgumentType::Value(ty) => {
+ let ty = gpu_type_paths.resolve_type(ty)
+ .with_context(|| format!("cannot parse the type of argument {}", argument.name))?;
+ (quote! { #ty }, quote! { #ty }, None)
},
- _ => Ok(None)
+ };
+ if argument.conditional {
+ kernel_argument_type = quote! { Option<#kernel_argument_type> };
+ wrapper_argument_type = quote! { Option<#wrapper_argument_type> };
+ conversion = conversion.map(|conversion| quote! { #argument_name.map(|#argument_name| #conversion) });
}
+ let call_argument = conversion.unwrap_or_else(|| quote! { #argument_name });
+ Ok((
+ quote! { #argument_name: #kernel_argument_type },
+ quote! { #[allow(unused)] #argument_name: #wrapper_argument_type },
+ call_argument,
+ ))
})
- .filter_map(|x| x.transpose())
.collect::<anyhow::Result<(Vec<TokenStream>, Vec<TokenStream>, Vec<TokenStream>)>>()?;
let wrapper_trait_name = format_ident!("{}KernelTrait", kernel.name.as_str());
@@ -424,7 +389,7 @@ fn bindgen_kernel(
Buffer {
conditional: bool,
},
- Constant {
+ Values {
buffer_size: usize,
fields: Vec<(usize, String, bool, SourceType)>,
},
@@ -466,25 +431,20 @@ fn bindgen_kernel(
let buffer_name = parameter_name.to_string();
- let read_only = matches!(access_type, SlangBufferAccess::Read { .. });
-
- let source_type = if matches!(
- access_type,
- SlangBufferAccess::Read {
- is_constant: true
- }
- ) {
- let element_type = parameter.type_layout().unwrap().resource_result_type().unwrap();
- let element_type_layout =
- shader.type_layout(element_type, LayoutRules::DefaultStructuredBuffer).unwrap();
+ let read_only = matches!(access_type, SlangBufferAccess::Read);
- BindGroupEntrySourceType::Slice {
- element_type: SourceType::from_type_layout(element_type_layout)?,
- }
- } else {
- BindGroupEntrySourceType::Buffer {
+ let source_type = match kernel_argument.unwrap().binding_type.as_ref().unwrap() {
+ KernelArgumentType::Value(_) => {
+ let element_type = parameter.type_layout().unwrap().resource_result_type().unwrap();
+ let element_type_layout =
+ shader.type_layout(element_type, LayoutRules::DefaultStructuredBuffer).unwrap();
+ BindGroupEntrySourceType::Slice {
+ element_type: SourceType::from_type_layout(element_type_layout)?,
+ }
+ },
+ KernelArgumentType::Buffer(_) => BindGroupEntrySourceType::Buffer {
conditional: condition.is_some(),
- }
+ },
};
vacant.insert(BindGroupEntry {
@@ -496,13 +456,18 @@ fn bindgen_kernel(
});
},
ParameterCategory::Uniform => {
- let SlangArgumentType::Constant {
+ let SlangArgumentType::Uniform {
condition,
} = &kernel_argument.unwrap().argument_type
else {
unreachable!();
};
+ ensure!(
+ matches!(kernel_argument.unwrap().binding_type, Some(KernelArgumentType::Value(_))),
+ "uniform argument {parameter_name} requires a value binding",
+ );
+
let binding = binding_base + parameter.offset(ParameterCategory::DescriptorTableSlot);
let offset = parameter.offset(ParameterCategory::Uniform);
@@ -514,12 +479,12 @@ fn bindgen_kernel(
if !matches!(occupied.get().binding_type, BindGroupEntryBindingType::UniformBuffer) {
bail!("binding {binding} repeated as uniform and non-uniform")
}
- let BindGroupEntrySourceType::Constant {
+ let BindGroupEntrySourceType::Values {
buffer_size,
fields,
} = &mut occupied.get_mut().source_type
else {
- bail!("binding {binding} repeated as buffer and constant")
+ bail!("binding {binding} repeated as buffer and values")
};
*buffer_size = usize::max(*buffer_size, field_buffer_size);
fields.push((offset, parameter_name.to_string(), condition.is_some(), source_type))
@@ -531,7 +496,7 @@ fn bindgen_kernel(
vacant.insert(BindGroupEntry {
buffer_name,
binding_type: BindGroupEntryBindingType::UniformBuffer,
- source_type: BindGroupEntrySourceType::Constant {
+ source_type: BindGroupEntrySourceType::Values {
buffer_size: field_buffer_size,
fields: vec![(
offset,
@@ -596,7 +561,7 @@ fn bindgen_kernel(
});
})
}
- BindGroupEntrySourceType::Constant { buffer_size, fields } => {
+ BindGroupEntrySourceType::Values { buffer_size, fields } => {
let field_writes = fields.iter().map(|(offset, name, conditional, source_type)| {
let name = format_ident!("{}", name);
let write = source_type.generate_write(quote! { buffer_slice[#offset..] }, quote! { #name });
diff --git a/crates/uzu-engine/build/webgpu/mod.rs b/crates/uzu-engine/build/webgpu/mod.rs
index 4ff34c34b..2c4b5684f 100644
--- a/crates/uzu-engine/build/webgpu/mod.rs
+++ b/crates/uzu-engine/build/webgpu/mod.rs
@@ -8,7 +8,10 @@ use naga::{
use proc_macro2::TokenStream;
use shader_slang::{CompileTarget, ComponentType, GlobalSession, TargetDesc};
-use crate::slang::{SlangKernel, SlangTarget};
+use crate::{
+ common::gpu_type_paths::GpuTypePaths,
+ slang::{SlangKernel, SlangTarget},
+};
mod bindgen_file;
mod bindgen_global;
@@ -46,10 +49,11 @@ impl SlangTarget for WebGPU {
linked_component: &ComponentType,
kernels: &[SlangKernel],
gpu_type_map: &HashMap<String, String>,
+ gpu_type_paths: &GpuTypePaths,
constants: &HashMap<String, String>,
object_file: &Path,
) -> anyhow::Result<TokenStream> {
- bindgen_file::bindgen_file(linked_component, kernels, gpu_type_map, constants, object_file)
+ bindgen_file::bindgen_file(linked_component, kernels, gpu_type_map, gpu_type_paths, constants, object_file)
}
fn bindgen_global(kernels: &[(impl AsRef<Path>, &[crate::common::kernel::Kernel])]) -> anyhow::Result<TokenStream> {