Skip to content

Commit

Permalink
[wgsl-out] Simplify map_binding_to_attribute to always return an inte…
Browse files Browse the repository at this point in the history
…rpolate attribute (gfx-rs#2318)
  • Loading branch information
expenses authored May 2, 2023
1 parent 9befaed commit da3f433
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 37 deletions.
31 changes: 8 additions & 23 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ impl<W: Write> Writer<W> {
for (index, arg) in func.arguments.iter().enumerate() {
// Write argument attribute if a binding is present
if let Some(ref binding) = arg.binding {
self.write_attributes(&map_binding_to_attribute(
binding,
module.types[arg.ty].inner.scalar_kind(),
))?;
self.write_attributes(&map_binding_to_attribute(binding))?;
}
// Write argument name
let argument_name = match func_ctx.ty {
Expand All @@ -274,10 +271,7 @@ impl<W: Write> Writer<W> {
if let Some(ref result) = func.result {
write!(self.out, " -> ")?;
if let Some(ref binding) = result.binding {
self.write_attributes(&map_binding_to_attribute(
binding,
module.types[result.ty].inner.scalar_kind(),
))?;
self.write_attributes(&map_binding_to_attribute(binding))?;
}
self.write_type(module, result.ty)?;
}
Expand Down Expand Up @@ -401,10 +395,7 @@ impl<W: Write> Writer<W> {
// 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,
module.types[member.ty].inner.scalar_kind(),
))?;
self.write_attributes(&map_binding_to_attribute(binding))?;
}
// Write struct member name and type
let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];
Expand Down Expand Up @@ -1941,10 +1932,7 @@ const fn address_space_str(
)
}

fn map_binding_to_attribute(
binding: &crate::Binding,
scalar_kind: Option<crate::ScalarKind>,
) -> Vec<Attribute> {
fn map_binding_to_attribute(binding: &crate::Binding) -> Vec<Attribute> {
match *binding {
crate::Binding::BuiltIn(built_in) => {
if let crate::BuiltIn::Position { invariant: true } = built_in {
Expand All @@ -1957,12 +1945,9 @@ fn map_binding_to_attribute(
location,
interpolation,
sampling,
} => match scalar_kind {
Some(crate::ScalarKind::Float) => vec![
Attribute::Location(location),
Attribute::Interpolate(interpolation, sampling),
],
_ => vec![Attribute::Location(location)],
},
} => vec![
Attribute::Location(location),
Attribute::Interpolate(interpolation, sampling),
],
}
}
2 changes: 1 addition & 1 deletion tests/out/wgsl/binding-arrays.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct UniformIndex {
}

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

@group(0) @binding(0)
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/binding-buffer-arrays.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Foo {
}

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

@group(0) @binding(0)
Expand All @@ -16,7 +16,7 @@ var<storage> storage_array: binding_array<Foo,1>;
var<uniform> uni: UniformIndex;

@fragment
fn main(fragment_in: FragmentIn) -> @location(0) u32 {
fn main(fragment_in: FragmentIn) -> @location(0) @interpolate(flat) u32 {
var u1_: u32;

let uniform_index = uni.index;
Expand Down
16 changes: 8 additions & 8 deletions tests/out/wgsl/fragment-output.wgsl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
struct FragmentOutputVec4Vec3_ {
@location(0) vec4f: vec4<f32>,
@location(1) vec4i: vec4<i32>,
@location(2) vec4u: vec4<u32>,
@location(1) @interpolate(flat) vec4i: vec4<i32>,
@location(2) @interpolate(flat) vec4u: vec4<u32>,
@location(3) vec3f: vec3<f32>,
@location(4) vec3i: vec3<i32>,
@location(5) vec3u: vec3<u32>,
@location(4) @interpolate(flat) vec3i: vec3<i32>,
@location(5) @interpolate(flat) vec3u: vec3<u32>,
}

struct FragmentOutputVec2Scalar {
@location(0) vec2f: vec2<f32>,
@location(1) vec2i: vec2<i32>,
@location(2) vec2u: vec2<u32>,
@location(1) @interpolate(flat) vec2i: vec2<i32>,
@location(2) @interpolate(flat) vec2u: vec2<u32>,
@location(3) scalarf: f32,
@location(4) scalari: i32,
@location(5) scalaru: u32,
@location(4) @interpolate(flat) scalari: i32,
@location(5) @interpolate(flat) scalaru: u32,
}

@fragment
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/interface.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Input2_ {
var<workgroup> output: array<u32,1>;

@vertex
fn vertex(@builtin(vertex_index) vertex_index: u32, @builtin(instance_index) instance_index: u32, @location(10) color: u32) -> VertexOutput {
fn vertex(@builtin(vertex_index) vertex_index: u32, @builtin(instance_index) instance_index: u32, @location(10) @interpolate(flat) color: u32) -> VertexOutput {
let tmp: u32 = ((vertex_index + instance_index) + color);
return VertexOutput(vec4<f32>(1.0), f32(tmp));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/interpolate.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct FragmentInput {
@builtin(position) position: vec4<f32>,
@location(0) _flat: u32,
@location(0) @interpolate(flat) _flat: u32,
@location(1) @interpolate(linear) _linear: f32,
@location(2) @interpolate(linear, centroid) linear_centroid: vec2<f32>,
@location(3) @interpolate(linear, sample) linear_sample: vec3<f32>,
Expand Down
2 changes: 1 addition & 1 deletion tests/out/wgsl/shadow.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
}

@vertex
fn vs_main(@location(0) position: vec4<i32>, @location(1) normal: vec4<i32>) -> VertexOutput {
fn vs_main(@location(0) @interpolate(flat) position: vec4<i32>, @location(1) @interpolate(flat) normal: vec4<i32>) -> VertexOutput {
var out: VertexOutput;

let w = u_entity.world;
Expand Down

0 comments on commit da3f433

Please sign in to comment.