Skip to content

Commit

Permalink
Treat built-in fields as padding #34
Browse files Browse the repository at this point in the history
  • Loading branch information
sytherax committed Jul 27, 2024
1 parent 6ddf9e6 commit fb8b58b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 47 deletions.
1 change: 1 addition & 0 deletions example/shaders/utils/types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ struct Scalars {
a: u32,
b: i32,
c: f32,
@builtin(vertex_index) d: u32,
};

struct VectorsU32 {
Expand Down
12 changes: 3 additions & 9 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,9 @@ impl State {
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("vertex buffer"),
contents: bytemuck::cast_slice(&[
shader_bindings::triangle::VertexInput {
position: vec3a(-1.0, -1.0, 0.0),
},
shader_bindings::triangle::VertexInput {
position: vec3a(3.0, -1.0, 0.0),
},
shader_bindings::triangle::VertexInput {
position: vec3a(-1.0, 3.0, 0.0),
},
shader_bindings::triangle::VertexInput(vec3a(-1.0, -1.0, 0.0)),
shader_bindings::triangle::VertexInput(vec3a(3.0, -1.0, 0.0)),
shader_bindings::triangle::VertexInput(vec3a(-1.0, 3.0, 0.0)),
]),
usage: wgpu::BufferUsages::VERTEX,
});
Expand Down
38 changes: 6 additions & 32 deletions example/src/shader_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// File automatically generated by wgsl_bindgen^
//
// ^ wgsl_bindgen version 0.14.0
// ^ wgsl_bindgen version 0.14.1
// Changes made to this file will not be saved.
// SourceHash: 46afc31c9d0b833873f2da321d23a4499bd85e13644967cf1e6260128dcfb0fc
// SourceHash: 6017586cd84fa513a8e92bda218ca1f7b6cf9de5fa8c4fd92c2206e287dc1dfa

#![allow(unused, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -486,38 +486,11 @@ pub mod testbed {
pub struct Uniforms {
/// size: 16, offset: 0x0, type: `vec4<f32>`
pub color_rgb: glam::Vec4,
/// size: 12, offset: 0x10, type: `struct`
/// size: 16, offset: 0x10, type: `struct`
pub scalars: crate::MyScalars,
pub _pad_scalars: [u8; 0x10 - core::mem::size_of::<crate::MyScalars>()],
}
impl Uniforms {
pub const fn new(color_rgb: glam::Vec4, scalars: crate::MyScalars) -> Self {
Self {
color_rgb,
scalars,
_pad_scalars: [0; 0x10 - core::mem::size_of::<crate::MyScalars>()],
}
}
}
#[repr(C)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct UniformsInit {
pub color_rgb: glam::Vec4,
pub scalars: crate::MyScalars,
}
impl UniformsInit {
pub const fn build(&self) -> Uniforms {
Uniforms {
color_rgb: self.color_rgb,
scalars: self.scalars,
_pad_scalars: [0; 0x10 - core::mem::size_of::<crate::MyScalars>()],
}
}
}
impl From<UniformsInit> for Uniforms {
fn from(data: UniformsInit) -> Self {
data.build()
}
pub const fn Uniforms(color_rgb: glam::Vec4, scalars: crate::MyScalars) -> Uniforms {
Uniforms { color_rgb, scalars }
}
#[derive(Debug)]
pub struct WgpuBindGroup0EntriesParams<'a> {
Expand Down Expand Up @@ -983,6 +956,7 @@ struct ScalarsX_naga_oil_mod_XOV2GS3DTHI5HI6LQMVZQX {
a: u32,
b: i32,
c: f32,
@builtin(vertex_index) d: u32,
}
struct VectorsU32X_naga_oil_mod_XOV2GS3DTHI5HI6LQMVZQX {
Expand Down
6 changes: 5 additions & 1 deletion wgsl_bindgen/src/quote_gen/rust_struct_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ impl<'a> NagaToRustStructState<'a> {
.iter()
.any(|pad_expr| pad_expr.is_match(&member_name));

let entry = if is_current_field_padding {
// both padding field and built-in fields are handled in the same way
// skip builtins like @builtin(vertex_index)
let entry = if is_current_field_padding
|| matches!(naga_member.binding, Some(naga::Binding::BuiltIn(_)))
{
let size = naga_type.inner.size(gctx);
let size = format!("0x{:X}", size);
let pad_size_tokens = syn::parse_str::<TokenStream>(&size).unwrap();
Expand Down
13 changes: 8 additions & 5 deletions wgsl_bindgen/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,8 @@ mod tests {
@size(8)
a: u32,
b: i32,
@align(32)
c: f32,
@align(32) c: f32,
@builtin(vertex_index) d: u32,
};
var<storage, read_write> test: Input0;
Expand Down Expand Up @@ -1070,7 +1070,8 @@ mod tests {
pub _pad_b: [u8; 0x18 - core::mem::size_of::<i32>()],
/// size: 4, offset: 0x20, type: `f32`
pub c: f32,
pub _pad_c: [u8; 0x20 - core::mem::size_of::<f32>()],
pub d: [u8; 0x4],
pub _pad_d: [u8; 0x1C - core::mem::size_of::<u32>()],
}
impl Input0 {
pub const fn new(a: u32, b: i32, c: f32) -> Self {
Expand All @@ -1080,7 +1081,8 @@ mod tests {
b,
_pad_b: [0; 0x18 - core::mem::size_of::<i32>()],
c,
_pad_c: [0; 0x20 - core::mem::size_of::<f32>()],
d: [0; 0x4],
_pad_d: [0; 0x1C - core::mem::size_of::<u32>()],
}
}
}
Expand All @@ -1100,7 +1102,8 @@ mod tests {
b: self.b,
_pad_b: [0; 0x18 - core::mem::size_of::<i32>()],
c: self.c,
_pad_c: [0; 0x20 - core::mem::size_of::<f32>()],
d: [0; 0x4],
_pad_d: [0; 0x1C - core::mem::size_of::<u32>()],
}
}
}
Expand Down

0 comments on commit fb8b58b

Please sign in to comment.