From 19d193584aed70108762eca2f29389c7644a1ad5 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Mon, 8 Aug 2022 19:25:33 +0100 Subject: [PATCH] Fix shader warning and tidy up code. --- src/common.wgsl | 8 ++++---- src/outline.wgsl | 13 ++++--------- src/stencil.wgsl | 11 +++-------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/common.wgsl b/src/common.wgsl index ab20469..d7fa949 100644 --- a/src/common.wgsl +++ b/src/common.wgsl @@ -5,11 +5,11 @@ @group(1) @binding(0) var mesh: Mesh; -fn model_origin_z() -> f32 { - var origin = mesh.model[3]; +fn model_origin_z(model: mat4x4, view_proj: mat4x4) -> f32 { + var origin = model[3]; var proj_zw = mat4x2( - view.view_proj[0].zw, view.view_proj[1].zw, - view.view_proj[2].zw, view.view_proj[3].zw); + view_proj[0].zw, view_proj[1].zw, + view_proj[2].zw, view_proj[3].zw); var zw = proj_zw * origin; return zw.x / zw.y; } \ No newline at end of file diff --git a/src/outline.wgsl b/src/outline.wgsl index a307a72..86510da 100644 --- a/src/outline.wgsl +++ b/src/outline.wgsl @@ -5,10 +5,6 @@ struct VertexInput { @location(1) normal: vec3, }; -struct VertexOutput { - @builtin(position) clip_position: vec4, -}; - struct OutlineViewUniform { scale: vec2, }; @@ -37,13 +33,12 @@ fn mat4to3(m: mat4x4) -> mat3x3 { } @vertex -fn vertex(vertex: VertexInput) -> VertexOutput { - var out: VertexOutput; +fn vertex(vertex: VertexInput) -> @builtin(position) vec4 { var clip_pos = view.view_proj * (mesh.model * vec4(vertex.position, 1.0)); var clip_norm = mat4to3(view.view_proj) * (mat4to3(mesh.model) * vertex.normal); - var clip_delta = vec2(vstage.width * normalize(clip_norm.xy) * clip_pos.w * view_uniform.scale); - out.clip_position = vec4((clip_pos.xy + clip_delta) / clip_pos.w, model_origin_z(), 1.0); - return out; + var ndc_pos = clip_pos.xy / clip_pos.w; + var ndc_delta = vstage.width * normalize(clip_norm.xy) * view_uniform.scale; + return vec4(ndc_pos + ndc_delta, model_origin_z(mesh.model, view.view_proj), 1.0); } @fragment diff --git a/src/stencil.wgsl b/src/stencil.wgsl index e58660a..501efb6 100644 --- a/src/stencil.wgsl +++ b/src/stencil.wgsl @@ -4,16 +4,11 @@ struct VertexInput { @location(0) position: vec3, }; -struct VertexOutput { - @builtin(position) clip_position: vec4, -}; - @vertex -fn vertex(vertex: VertexInput) -> VertexOutput { - var out: VertexOutput; +fn vertex(vertex: VertexInput) -> @builtin(position) vec4 { var clip_pos = view.view_proj * (mesh.model * vec4(vertex.position, 1.0)); - out.clip_position = vec4(clip_pos.xy / clip_pos.w, model_origin_z(), 1.0); - return out; + var ndc_pos = clip_pos.xy / clip_pos.w; + return vec4(ndc_pos, model_origin_z(mesh.model, view.view_proj), 1.0); } @fragment