Skip to content
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

Fix wgsl out struct layout #2450

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ impl<W: Write> Writer<W> {

// Write all structs
for (handle, ty) in module.types.iter() {
if let TypeInner::Struct {
ref members,
span: _,
} = ty.inner
{
self.write_struct(module, handle, members)?;
if let TypeInner::Struct { ref members, span } = ty.inner {
self.write_struct(module, handle, members, span)?;
writeln!(self.out)?;
}
}
Expand Down Expand Up @@ -377,17 +373,28 @@ impl<W: Write> Writer<W> {
module: &Module,
handle: Handle<crate::Type>,
members: &[crate::StructMember],
struct_span: u32,
) -> BackendResult {
write!(self.out, "struct ")?;
self.write_struct_name(module, handle)?;
write!(self.out, " {{")?;
writeln!(self.out)?;

for (index, member) in members.iter().enumerate() {
// The indentation is only for readability
write!(self.out, "{}", back::INDENT)?;
if let Some(ref binding) = member.binding {
self.write_attributes(&map_binding_to_attribute(binding))?;
}

let current_field_size = if index != members.len() - 1 {
let next_member = &members[index + 1];
next_member.offset - member.offset
} else {
struct_span - member.offset
};
write!(self.out, "@size({current_field_size}) ")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid writing the size explicitly if it's the same as the one inferred. See example below on how to get the size:

naga/src/back/msl/writer.rs

Lines 3178 to 3179 in 0491d39

let ty_inner = &module.types[member.ty].inner;
last_offset = member.offset + ty_inner.size(module.to_ctx());


// Write struct member name and type
let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];
write!(self.out, "{member_name}: ")?;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/210-bevy-2d-shader-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
struct ColorMaterial_color {
Color: vec4<f32>,
@size(16) Color: vec4<f32>,
}

struct FragmentOutput {
@location(0) o_Target: vec4<f32>,
@location(0) @size(16) o_Target: vec4<f32>,
}

var<private> v_Uv_1: vec2<f32>;
Expand Down
10 changes: 5 additions & 5 deletions tests/out/wgsl/210-bevy-2d-shader-vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
struct Camera {
ViewProj: mat4x4<f32>,
@size(64) ViewProj: mat4x4<f32>,
}

struct Transform {
Model: mat4x4<f32>,
@size(64) Model: mat4x4<f32>,
}

struct Sprite_size {
size: vec2<f32>,
@size(8) size: vec2<f32>,
}

struct VertexOutput {
@location(0) v_Uv: vec2<f32>,
@builtin(position) member: vec4<f32>,
@location(0) @size(8) v_Uv: vec2<f32>,
@builtin(position) @size(16) member: vec4<f32>,
}

var<private> Vertex_Position_1: vec3<f32>;
Expand Down
12 changes: 6 additions & 6 deletions tests/out/wgsl/210-bevy-shader-vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
struct Camera {
ViewProj: mat4x4<f32>,
@size(64) ViewProj: mat4x4<f32>,
}

struct Transform {
Model: mat4x4<f32>,
@size(64) Model: mat4x4<f32>,
}

struct VertexOutput {
@location(0) v_Position: vec3<f32>,
@location(1) v_Normal: vec3<f32>,
@location(2) v_Uv: vec2<f32>,
@builtin(position) member: vec4<f32>,
@location(0) @size(12) v_Position: vec3<f32>,
@location(1) @size(12) v_Normal: vec3<f32>,
@location(2) @size(8) v_Uv: vec2<f32>,
@builtin(position) @size(16) member: vec4<f32>,
}

var<private> Vertex_Position_1: vec3<f32>;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/246-collatz-comp.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct PrimeIndices {
indices: array<u32>,
@size(4) indices: array<u32>,
}

@group(0) @binding(0)
Expand Down
8 changes: 4 additions & 4 deletions tests/out/wgsl/800-out-of-bounds-panic-vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
struct Globals {
view_matrix: mat4x4<f32>,
@size(64) view_matrix: mat4x4<f32>,
}

struct VertexPushConstants {
world_matrix: mat4x4<f32>,
@size(64) world_matrix: mat4x4<f32>,
}

struct VertexOutput {
@location(0) frag_color: vec4<f32>,
@builtin(position) member: vec4<f32>,
@location(0) @size(16) frag_color: vec4<f32>,
@builtin(position) @size(16) member: vec4<f32>,
}

@group(0) @binding(0)
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/896-push-constant-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct PushConstants {
example: f32,
@size(4) example: f32,
}

var<push_constant> c: PushConstants;
Expand Down
24 changes: 12 additions & 12 deletions tests/out/wgsl/access.wgsl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
struct GlobalConst {
a: u32,
b: vec3<u32>,
c: i32,
@size(16) a: u32,
@size(12) b: vec3<u32>,
@size(4) c: i32,
}

struct AlignedWrapper {
value: i32,
@size(8) value: i32,
}

struct Bar {
_matrix: mat4x3<f32>,
matrix_array: array<mat2x2<f32>, 2>,
atom: atomic<i32>,
atom_arr: array<atomic<i32>, 10>,
arr: array<vec2<u32>, 2>,
data: array<AlignedWrapper>,
@size(64) _matrix: mat4x3<f32>,
@size(32) matrix_array: array<mat2x2<f32>, 2>,
@size(4) atom: atomic<i32>,
@size(44) atom_arr: array<atomic<i32>, 10>,
@size(16) arr: array<vec2<u32>, 2>,
@size(16) data: array<AlignedWrapper>,
}

struct Baz {
m: mat3x2<f32>,
@size(24) m: mat3x2<f32>,
}

struct MatCx2InArray {
am: array<mat4x2<f32>, 2>,
@size(64) am: array<mat4x2<f32>, 2>,
}

var<private> global_const: GlobalConst = GlobalConst(0u, vec3<u32>(0u, 0u, 0u), 0);
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/array-in-ctor.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct Ah {
inner: array<f32, 2>,
@size(8) inner: array<f32, 2>,
}

@group(0) @binding(0)
Expand Down
8 changes: 4 additions & 4 deletions tests/out/wgsl/atomicCompareExchange.wgsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
struct gen___atomic_compare_exchange_resultSint4_ {
old_value: i32,
exchanged: bool,
@size(4) old_value: i32,
@size(4) exchanged: bool,
}

struct gen___atomic_compare_exchange_resultUint4_ {
old_value: u32,
exchanged: bool,
@size(4) old_value: u32,
@size(4) exchanged: bool,
}

const SIZE: u32 = 128u;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/atomicOps.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct Struct {
atomic_scalar: atomic<u32>,
atomic_arr: array<atomic<i32>, 2>,
@size(4) atomic_scalar: atomic<u32>,
@size(8) atomic_arr: array<atomic<i32>, 2>,
}

@group(0) @binding(0)
Expand Down
34 changes: 17 additions & 17 deletions tests/out/wgsl/bevy-pbr-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
struct PointLight {
pos: vec4<f32>,
color: vec4<f32>,
lightParams: vec4<f32>,
@size(16) pos: vec4<f32>,
@size(16) color: vec4<f32>,
@size(16) lightParams: vec4<f32>,
}

struct DirectionalLight {
direction: vec4<f32>,
color: vec4<f32>,
@size(16) direction: vec4<f32>,
@size(16) color: vec4<f32>,
}

struct CameraViewProj {
ViewProj: mat4x4<f32>,
@size(64) ViewProj: mat4x4<f32>,
}

struct CameraPosition {
CameraPos: vec4<f32>,
@size(16) CameraPos: vec4<f32>,
}

struct Lights {
AmbientColor: vec4<f32>,
NumLights: vec4<u32>,
PointLights: array<PointLight, 10>,
DirectionalLights: array<DirectionalLight, 1>,
@size(16) AmbientColor: vec4<f32>,
@size(16) NumLights: vec4<u32>,
@size(480) PointLights: array<PointLight, 10>,
@size(32) DirectionalLights: array<DirectionalLight, 1>,
}

struct StandardMaterial_base_color {
base_color: vec4<f32>,
@size(16) base_color: vec4<f32>,
}

struct StandardMaterial_roughness {
perceptual_roughness: f32,
@size(4) perceptual_roughness: f32,
}

struct StandardMaterial_metallic {
metallic: f32,
@size(4) metallic: f32,
}

struct StandardMaterial_reflectance {
reflectance: f32,
@size(4) reflectance: f32,
}

struct StandardMaterial_emissive {
emissive: vec4<f32>,
@size(16) emissive: vec4<f32>,
}

struct FragmentOutput {
@location(0) o_Target: vec4<f32>,
@location(0) @size(16) o_Target: vec4<f32>,
}

const MAX_POINT_LIGHTS: i32 = 10;
Expand Down
14 changes: 7 additions & 7 deletions tests/out/wgsl/bevy-pbr-vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
struct CameraViewProj {
ViewProj: mat4x4<f32>,
@size(64) ViewProj: mat4x4<f32>,
}

struct Transform {
Model: mat4x4<f32>,
@size(64) Model: mat4x4<f32>,
}

struct VertexOutput {
@location(0) v_WorldPosition: vec3<f32>,
@location(1) v_WorldNormal: vec3<f32>,
@location(2) v_Uv: vec2<f32>,
@location(3) v_WorldTangent: vec4<f32>,
@builtin(position) member: vec4<f32>,
@location(0) @size(12) v_WorldPosition: vec3<f32>,
@location(1) @size(12) v_WorldNormal: vec3<f32>,
@location(2) @size(8) v_Uv: vec2<f32>,
@location(3) @size(16) v_WorldTangent: vec4<f32>,
@builtin(position) @size(16) member: vec4<f32>,
}

var<private> Vertex_Position_1: vec3<f32>;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/binding-arrays.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
struct UniformIndex {
index: u32,
@size(4) index: u32,
}

struct FragmentIn {
@location(0) @interpolate(flat) index: u32,
@location(0) @interpolate(flat) @size(4) index: u32,
}

@group(0) @binding(0)
Expand Down
6 changes: 3 additions & 3 deletions tests/out/wgsl/binding-buffer-arrays.wgsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
struct UniformIndex {
index: u32,
@size(4) index: u32,
}

struct Foo {
x: u32,
@size(4) x: u32,
}

struct FragmentIn {
@location(0) @interpolate(flat) index: u32,
@location(0) @interpolate(flat) @size(4) index: u32,
}

@group(0) @binding(0)
Expand Down
20 changes: 10 additions & 10 deletions tests/out/wgsl/boids.wgsl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
struct Particle {
pos: vec2<f32>,
vel: vec2<f32>,
@size(8) pos: vec2<f32>,
@size(8) vel: vec2<f32>,
}

struct SimParams {
deltaT: f32,
rule1Distance: f32,
rule2Distance: f32,
rule3Distance: f32,
rule1Scale: f32,
rule2Scale: f32,
rule3Scale: f32,
@size(4) deltaT: f32,
@size(4) rule1Distance: f32,
@size(4) rule2Distance: f32,
@size(4) rule3Distance: f32,
@size(4) rule1Scale: f32,
@size(4) rule2Scale: f32,
@size(4) rule3Scale: f32,
}

struct Particles {
particles: array<Particle>,
@size(16) particles: array<Particle>,
}

const NUM_PARTICLES: u32 = 1500u;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/bool-select-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct FragmentOutput {
@location(0) o_color: vec4<f32>,
@location(0) @size(16) o_color: vec4<f32>,
}

var<private> o_color: vec4<f32>;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/buffer-frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
struct testBufferBlock {
data: array<u32>,
@size(4) data: array<u32>,
}

struct testBufferReadOnlyBlock {
data: array<u32>,
@size(4) data: array<u32>,
}

@group(0) @binding(0)
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/clamp-splat-vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct VertexOutput {
@builtin(position) member: vec4<f32>,
@builtin(position) @size(16) member: vec4<f32>,
}

var<private> a_pos_1: vec2<f32>;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/collatz.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct PrimeIndices {
data: array<u32>,
@size(4) data: array<u32>,
}

@group(0) @binding(0)
Expand Down
Loading
Loading