wgpu: Fix some invalid WGSL

naga is currently lenient about some things which are invalid by
the WGSL spec. The Tint WGSL compiler disagreed, so these shaders
failed to compile in Chrome.
This commit is contained in:
Mike Welsh 2021-10-25 21:36:19 -07:00
parent 5ac8e75239
commit 26e0432ccc
4 changed files with 13 additions and 13 deletions

View File

@ -10,10 +10,10 @@ var<uniform> textureTransforms: TextureTransforms;
[[group(2), binding(1)]] [[group(2), binding(1)]]
var texture: texture_2d<f32>; var texture: texture_2d<f32>;
[[group(3), binding(0)]] [[group(3), binding(0)]]
var sampler: sampler; var texture_sampler: sampler;
[[stage(vertex)]] [[stage(vertex)]]
fn main(in: VertexInput) -> VertexOutput { fn main_vertex(in: VertexInput) -> VertexOutput {
let matrix = textureTransforms.matrix; let matrix = textureTransforms.matrix;
let uv = (mat3x3<f32>(matrix[0].xyz, matrix[1].xyz, matrix[2].xyz) * vec3<f32>(in.position, 1.0)).xy; let uv = (mat3x3<f32>(matrix[0].xyz, matrix[1].xyz, matrix[2].xyz) * vec3<f32>(in.position, 1.0)).xy;
let pos = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0); let pos = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0);
@ -21,8 +21,8 @@ fn main(in: VertexInput) -> VertexOutput {
} }
[[stage(fragment)]] [[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> { fn main_fragment(in: VertexOutput) -> [[location(0)]] vec4<f32> {
var color: vec4<f32> = textureSample(texture, sampler, in.uv); var color: vec4<f32> = textureSample(texture, texture_sampler, in.uv);
// Texture is premultiplied by alpha. // Texture is premultiplied by alpha.
// Unmultiply alpha, apply color transform, remultiply alpha. // Unmultiply alpha, apply color transform, remultiply alpha.
if( color.a > 0.0 ) { if( color.a > 0.0 ) {

View File

@ -6,13 +6,13 @@ struct VertexOutput {
}; };
[[stage(vertex)]] [[stage(vertex)]]
fn main(in: VertexInput) -> VertexOutput { fn main_vertex(in: VertexInput) -> VertexOutput {
let pos: vec4<f32> = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0); let pos: vec4<f32> = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0);
return VertexOutput(pos, in.color); return VertexOutput(pos, in.color);
} }
[[stage(fragment)]] [[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> { fn main_fragment(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let out = in.color * transforms.mult_color + transforms.add_color; let out = in.color * transforms.mult_color + transforms.add_color;
return output(out); return output(out);
} }

View File

@ -22,7 +22,7 @@ var<uniform> textureTransforms: TextureTransforms;
var<storage> gradient: Gradient; var<storage> gradient: Gradient;
[[stage(vertex)]] [[stage(vertex)]]
fn main(in: VertexInput) -> VertexOutput { fn main_vertex(in: VertexInput) -> VertexOutput {
let matrix = textureTransforms.matrix; let matrix = textureTransforms.matrix;
let uv = (mat3x3<f32>(matrix[0].xyz, matrix[1].xyz, matrix[2].xyz) * vec3<f32>(in.position, 1.0)).xy; let uv = (mat3x3<f32>(matrix[0].xyz, matrix[1].xyz, matrix[2].xyz) * vec3<f32>(in.position, 1.0)).xy;
let pos = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0); let pos = globals.view_matrix * transforms.world_matrix * vec4<f32>(in.position.x, in.position.y, 0.0, 1.0);
@ -30,7 +30,7 @@ fn main(in: VertexInput) -> VertexOutput {
} }
[[stage(fragment)]] [[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> { fn main_fragment(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let last = gradient.num_colors - 1u; let last = gradient.num_colors - 1u;
// Calculate normalized `t` position in gradient, [0.0, 1.0] being the bounds of the ratios. // Calculate normalized `t` position in gradient, [0.0, 1.0] being the bounds of the ratios.
@ -90,11 +90,11 @@ fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
t = clamp(t, gradient.ratios[0], gradient.ratios[last]); t = clamp(t, gradient.ratios[0], gradient.ratios[last]);
// Find the two gradient colors bordering our position. // Find the two gradient colors bordering our position.
var j: u32 = 1u; var j: u32;
for( var j: u32 = 1u; t > gradient.ratios[j]; j = j + 1u) { for( j = 1u; t > gradient.ratios[j]; j = j + 1u) {
// Noop // Noop
} }
var i: u32 = j - 1u; let i = j - 1u;
// Lerp between the two colors. // Lerp between the two colors.
let a = (t - gradient.ratios[i]) / (gradient.ratios[j] - gradient.ratios[i]); let a = (t - gradient.ratios[i]) / (gradient.ratios[j] - gradient.ratios[i]);

View File

@ -212,12 +212,12 @@ fn create_pipeline_descriptor<'a>(
layout: Some(pipeline_layout), layout: Some(pipeline_layout),
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
module: vertex_shader, module: vertex_shader,
entry_point: "main", entry_point: "main_vertex",
buffers: vertex_buffer_layout, buffers: vertex_buffer_layout,
}, },
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
module: fragment_shader, module: fragment_shader,
entry_point: "main", entry_point: "main_fragment",
targets: color_target_state, targets: color_target_state,
}), }),
primitive: wgpu::PrimitiveState { primitive: wgpu::PrimitiveState {