From 9a2c78c4059d2be37d10adda397fb6e64f38ac02 Mon Sep 17 00:00:00 2001 From: Dispersia Date: Sun, 11 Apr 2021 18:55:57 -0700 Subject: Upgrade wgpu --- wgpu/src/shader/blit.frag | 12 ---- wgpu/src/shader/blit.frag.spv | Bin 684 -> 0 bytes wgpu/src/shader/blit.vert | 26 -------- wgpu/src/shader/blit.vert.spv | Bin 1384 -> 0 bytes wgpu/src/shader/blit.wgsl | 43 +++++++++++++ wgpu/src/shader/image.frag | 12 ---- wgpu/src/shader/image.frag.spv | Bin 684 -> 0 bytes wgpu/src/shader/image.vert | 27 --------- wgpu/src/shader/image.vert.spv | Bin 2504 -> 0 bytes wgpu/src/shader/image.wgsl | 45 ++++++++++++++ wgpu/src/shader/quad.frag | 66 -------------------- wgpu/src/shader/quad.frag.spv | Bin 4212 -> 0 bytes wgpu/src/shader/quad.vert | 47 --------------- wgpu/src/shader/quad.vert.spv | Bin 3604 -> 0 bytes wgpu/src/shader/quad.wgsl | 123 ++++++++++++++++++++++++++++++++++++++ wgpu/src/shader/triangle.frag | 8 --- wgpu/src/shader/triangle.frag.spv | Bin 372 -> 0 bytes wgpu/src/shader/triangle.vert | 15 ----- wgpu/src/shader/triangle.vert.spv | Bin 1256 -> 0 bytes wgpu/src/shader/triangle.wgsl | 31 ++++++++++ 20 files changed, 242 insertions(+), 213 deletions(-) delete mode 100644 wgpu/src/shader/blit.frag delete mode 100644 wgpu/src/shader/blit.frag.spv delete mode 100644 wgpu/src/shader/blit.vert delete mode 100644 wgpu/src/shader/blit.vert.spv create mode 100644 wgpu/src/shader/blit.wgsl delete mode 100644 wgpu/src/shader/image.frag delete mode 100644 wgpu/src/shader/image.frag.spv delete mode 100644 wgpu/src/shader/image.vert delete mode 100644 wgpu/src/shader/image.vert.spv create mode 100644 wgpu/src/shader/image.wgsl delete mode 100644 wgpu/src/shader/quad.frag delete mode 100644 wgpu/src/shader/quad.frag.spv delete mode 100644 wgpu/src/shader/quad.vert delete mode 100644 wgpu/src/shader/quad.vert.spv create mode 100644 wgpu/src/shader/quad.wgsl delete mode 100644 wgpu/src/shader/triangle.frag delete mode 100644 wgpu/src/shader/triangle.frag.spv delete mode 100644 wgpu/src/shader/triangle.vert delete mode 100644 wgpu/src/shader/triangle.vert.spv create mode 100644 wgpu/src/shader/triangle.wgsl (limited to 'wgpu/src/shader') diff --git a/wgpu/src/shader/blit.frag b/wgpu/src/shader/blit.frag deleted file mode 100644 index dfed960f..00000000 --- a/wgpu/src/shader/blit.frag +++ /dev/null @@ -1,12 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 v_Uv; - -layout(set = 0, binding = 0) uniform sampler u_Sampler; -layout(set = 1, binding = 0) uniform texture2D u_Texture; - -layout(location = 0) out vec4 o_Color; - -void main() { - o_Color = texture(sampler2D(u_Texture, u_Sampler), v_Uv); -} diff --git a/wgpu/src/shader/blit.frag.spv b/wgpu/src/shader/blit.frag.spv deleted file mode 100644 index 2c5638b5..00000000 Binary files a/wgpu/src/shader/blit.frag.spv and /dev/null differ diff --git a/wgpu/src/shader/blit.vert b/wgpu/src/shader/blit.vert deleted file mode 100644 index 899cd39d..00000000 --- a/wgpu/src/shader/blit.vert +++ /dev/null @@ -1,26 +0,0 @@ -#version 450 - -layout(location = 0) out vec2 o_Uv; - -const vec2 positions[6] = vec2[6]( - vec2(-1.0, 1.0), - vec2(-1.0, -1.0), - vec2(1.0, -1.0), - vec2(-1.0, 1.0), - vec2(1.0, 1.0), - vec2(1.0, -1.0) -); - -const vec2 uvs[6] = vec2[6]( - vec2(0.0, 0.0), - vec2(0.0, 1.0), - vec2(1.0, 1.0), - vec2(0.0, 0.0), - vec2(1.0, 0.0), - vec2(1.0, 1.0) -); - -void main() { - o_Uv = uvs[gl_VertexIndex]; - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); -} diff --git a/wgpu/src/shader/blit.vert.spv b/wgpu/src/shader/blit.vert.spv deleted file mode 100644 index e0b436ce..00000000 Binary files a/wgpu/src/shader/blit.vert.spv and /dev/null differ diff --git a/wgpu/src/shader/blit.wgsl b/wgpu/src/shader/blit.wgsl new file mode 100644 index 00000000..7a987c2c --- /dev/null +++ b/wgpu/src/shader/blit.wgsl @@ -0,0 +1,43 @@ +var positions: array, 6> = array, 6>( + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2(1.0, -1.0), + vec2(-1.0, 1.0), + vec2(1.0, 1.0), + vec2(1.0, -1.0) +); + +var uvs: array, 6> = array, 6>( + vec2(0.0, 0.0), + vec2(0.0, 1.0), + vec2(1.0, 1.0), + vec2(0.0, 0.0), + vec2(1.0, 0.0), + vec2(1.0, 1.0) +); + +[[group(0), binding(0)]] var u_sampler: sampler; +[[group(1), binding(0)]] var u_texture: texture_2d; + +struct VertexInput { + [[builtin(vertex_index)]] vertex_index: u32; +}; + +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] uv: vec2; +}; + +[[stage(vertex)]] +fn vs_main(input: VertexInput) -> VertexOutput { + var out: VertexOutput; + out.uv = uvs[input.vertex_index]; + out.position = vec4(positions[input.vertex_index], 0.0, 1.0); + + return out; +} + +[[stage(fragment)]] +fn fs_main(input: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(u_texture, u_sampler, input.uv); +} \ No newline at end of file diff --git a/wgpu/src/shader/image.frag b/wgpu/src/shader/image.frag deleted file mode 100644 index 2809e9e6..00000000 --- a/wgpu/src/shader/image.frag +++ /dev/null @@ -1,12 +0,0 @@ -#version 450 - -layout(location = 0) in vec3 v_Uv; - -layout(set = 0, binding = 1) uniform sampler u_Sampler; -layout(set = 1, binding = 0) uniform texture2DArray u_Texture; - -layout(location = 0) out vec4 o_Color; - -void main() { - o_Color = texture(sampler2DArray(u_Texture, u_Sampler), v_Uv); -} diff --git a/wgpu/src/shader/image.frag.spv b/wgpu/src/shader/image.frag.spv deleted file mode 100644 index 65b08aa3..00000000 Binary files a/wgpu/src/shader/image.frag.spv and /dev/null differ diff --git a/wgpu/src/shader/image.vert b/wgpu/src/shader/image.vert deleted file mode 100644 index dab53cfe..00000000 --- a/wgpu/src/shader/image.vert +++ /dev/null @@ -1,27 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 v_Pos; -layout(location = 1) in vec2 i_Pos; -layout(location = 2) in vec2 i_Scale; -layout(location = 3) in vec2 i_Atlas_Pos; -layout(location = 4) in vec2 i_Atlas_Scale; -layout(location = 5) in uint i_Layer; - -layout (set = 0, binding = 0) uniform Globals { - mat4 u_Transform; -}; - -layout(location = 0) out vec3 o_Uv; - -void main() { - o_Uv = vec3(v_Pos * i_Atlas_Scale + i_Atlas_Pos, i_Layer); - - mat4 i_Transform = mat4( - vec4(i_Scale.x, 0.0, 0.0, 0.0), - vec4(0.0, i_Scale.y, 0.0, 0.0), - vec4(0.0, 0.0, 1.0, 0.0), - vec4(i_Pos, 0.0, 1.0) - ); - - gl_Position = u_Transform * i_Transform * vec4(v_Pos, 0.0, 1.0); -} diff --git a/wgpu/src/shader/image.vert.spv b/wgpu/src/shader/image.vert.spv deleted file mode 100644 index 21f5db2d..00000000 Binary files a/wgpu/src/shader/image.vert.spv and /dev/null differ diff --git a/wgpu/src/shader/image.wgsl b/wgpu/src/shader/image.wgsl new file mode 100644 index 00000000..c623247e --- /dev/null +++ b/wgpu/src/shader/image.wgsl @@ -0,0 +1,45 @@ +[[block]] +struct Globals { + transform: mat4x4; +}; + +[[group(0), binding(0)]] var globals: Globals; +[[group(0), binding(1)]] var u_sampler: sampler; +[[group(0), binding(2)]] var u_texture: texture2d; + +struct VertexInput { + [[location(0)]] v_pos: vec2; + [[location(1)]] pos: vec2; + [[location(2)]] scale: vec2; + [[location(3)]] atlas_pos: vec2; + [[location(4)]] atlas_scale: vec2; + [[location(5)]] layer: u32; +}; + +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] uv: vec3; +}; + +[[stage(vertex)]] +fn vs_main(input: VertexInput) -> VertexOutput { + var out: VertexOutput; + + out.uv = vec3(input.v_pos * input.atlas_scale + input.atlas_pos, input.layer); + + var transform: mat4x4 = mat4x4( + vec4(input.scale.x, 0.0, 0.0, 0.0), + vec4(0.0, scale.y, 0.0, 0.0), + vec4(0.0, 0.0, 1.0, 0.0), + vec4(input.pos, 0.0, 1.0) + ); + + out.position = globals.transform * transform * vec4(input.v_pos, 0.0, 1.0); + + return out; +} + +[[stage(fragment)]] +fn fs_main(input: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(u_texture, u_sampler, input.uv); +} diff --git a/wgpu/src/shader/quad.frag b/wgpu/src/shader/quad.frag deleted file mode 100644 index ad1af1ad..00000000 --- a/wgpu/src/shader/quad.frag +++ /dev/null @@ -1,66 +0,0 @@ -#version 450 - -layout(location = 0) in vec4 v_Color; -layout(location = 1) in vec4 v_BorderColor; -layout(location = 2) in vec2 v_Pos; -layout(location = 3) in vec2 v_Scale; -layout(location = 4) in float v_BorderRadius; -layout(location = 5) in float v_BorderWidth; - -layout(location = 0) out vec4 o_Color; - -float distance(in vec2 frag_coord, in vec2 position, in vec2 size, float radius) -{ - // TODO: Try SDF approach: https://www.shadertoy.com/view/wd3XRN - vec2 inner_size = size - vec2(radius, radius) * 2.0; - vec2 top_left = position + vec2(radius, radius); - vec2 bottom_right = top_left + inner_size; - - vec2 top_left_distance = top_left - frag_coord; - vec2 bottom_right_distance = frag_coord - bottom_right; - - vec2 distance = vec2( - max(max(top_left_distance.x, bottom_right_distance.x), 0), - max(max(top_left_distance.y, bottom_right_distance.y), 0) - ); - - return sqrt(distance.x * distance.x + distance.y * distance.y); -} - -void main() { - vec4 mixed_color; - - // TODO: Remove branching (?) - if(v_BorderWidth > 0) { - float internal_border = max(v_BorderRadius - v_BorderWidth, 0); - - float internal_distance = distance( - gl_FragCoord.xy, - v_Pos + vec2(v_BorderWidth), - v_Scale - vec2(v_BorderWidth * 2.0), - internal_border - ); - - float border_mix = smoothstep( - max(internal_border - 0.5, 0.0), - internal_border + 0.5, - internal_distance - ); - - mixed_color = mix(v_Color, v_BorderColor, border_mix); - } else { - mixed_color = v_Color; - } - - float d = distance( - gl_FragCoord.xy, - v_Pos, - v_Scale, - v_BorderRadius - ); - - float radius_alpha = - 1.0 - smoothstep(max(v_BorderRadius - 0.5, 0), v_BorderRadius + 0.5, d); - - o_Color = vec4(mixed_color.xyz, mixed_color.w * radius_alpha); -} diff --git a/wgpu/src/shader/quad.frag.spv b/wgpu/src/shader/quad.frag.spv deleted file mode 100644 index 519f5f01..00000000 Binary files a/wgpu/src/shader/quad.frag.spv and /dev/null differ diff --git a/wgpu/src/shader/quad.vert b/wgpu/src/shader/quad.vert deleted file mode 100644 index 09a278b1..00000000 --- a/wgpu/src/shader/quad.vert +++ /dev/null @@ -1,47 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 v_Pos; -layout(location = 1) in vec2 i_Pos; -layout(location = 2) in vec2 i_Scale; -layout(location = 3) in vec4 i_Color; -layout(location = 4) in vec4 i_BorderColor; -layout(location = 5) in float i_BorderRadius; -layout(location = 6) in float i_BorderWidth; - -layout (set = 0, binding = 0) uniform Globals { - mat4 u_Transform; - float u_Scale; -}; - -layout(location = 0) out vec4 o_Color; -layout(location = 1) out vec4 o_BorderColor; -layout(location = 2) out vec2 o_Pos; -layout(location = 3) out vec2 o_Scale; -layout(location = 4) out float o_BorderRadius; -layout(location = 5) out float o_BorderWidth; - -void main() { - vec2 p_Pos = i_Pos * u_Scale; - vec2 p_Scale = i_Scale * u_Scale; - - float i_BorderRadius = min( - i_BorderRadius, - min(i_Scale.x, i_Scale.y) / 2.0 - ); - - mat4 i_Transform = mat4( - vec4(p_Scale.x + 1.0, 0.0, 0.0, 0.0), - vec4(0.0, p_Scale.y + 1.0, 0.0, 0.0), - vec4(0.0, 0.0, 1.0, 0.0), - vec4(p_Pos - vec2(0.5, 0.5), 0.0, 1.0) - ); - - o_Color = i_Color; - o_BorderColor = i_BorderColor; - o_Pos = p_Pos; - o_Scale = p_Scale; - o_BorderRadius = i_BorderRadius * u_Scale; - o_BorderWidth = i_BorderWidth * u_Scale; - - gl_Position = u_Transform * i_Transform * vec4(v_Pos, 0.0, 1.0); -} diff --git a/wgpu/src/shader/quad.vert.spv b/wgpu/src/shader/quad.vert.spv deleted file mode 100644 index fa71ba1e..00000000 Binary files a/wgpu/src/shader/quad.vert.spv and /dev/null differ diff --git a/wgpu/src/shader/quad.wgsl b/wgpu/src/shader/quad.wgsl new file mode 100644 index 00000000..c67b45e3 --- /dev/null +++ b/wgpu/src/shader/quad.wgsl @@ -0,0 +1,123 @@ +[[block]] +struct Globals { + transform: mat4x4; + scale: f32; +}; + +[[group(0), binding(0)]] var globals: Globals; + +struct VertexInput { + [[location(0)]] v_pos: vec2; + [[location(1)]] pos: vec2; + [[location(2)]] scale: vec2; + [[location(3)]] color: vec4; + [[location(4)]] border_color: vec4; + [[location(5)]] border_radius: f32; + [[location(6)]] border_width: f32; +}; + +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] color: vec4; + [[location(1)]] border_color: vec4; + [[location(2)]] pos: vec2; + [[location(3)]] scale: vec2; + [[location(4)]] border_radius: f32; + [[location(5)]] border_width: f32; +}; + +[[stage(vertex)]] +fn vs_main(input: VertexInput) -> VertexOutput { + var out: VertexOutput; + + var pos: vec2 = input.pos * globals.scale; + var scale: vec2 = input.scale * globals.scale; + + var border_radius: f32 = min( + input.border_radius, + min(input.scale.x, input.scale.y) / 2.0 + ); + + var transform: mat4x4 = mat4x4( + vec4(scale.x + 1.0, 0.0, 0.0, 0.0), + vec4(0.0, scale.y + 1.0, 0.0, 0.0), + vec4(0.0, 0.0, 1.0, 0.0), + vec4(pos - vec2(0.5, 0.5), 0.0, 1.0) + ); + + out.color = input.color; + out.border_color = input.border_color; + out.pos = pos; + out.scale = scale; + out.border_radius = border_radius * globals.scale; + out.border_width = input.border_width * globals.scale; + out.position = globals.transform * transform * vec4(input.v_pos, 0.0, 1.0); + + return out; +} + +fn distance_alg( + frag_coord: vec2, + position: vec2, + size: vec2, + radius: f32 +) -> f32 { + var inner_size: vec2 = size - vec2(radius, radius) * 2.0; + var top_left: vec2 = position + vec2(radius, radius); + var bottom_right: vec2 = top_left + inner_size; + + var top_left_distance: vec2 = top_left - frag_coord; + var bottom_right_distance: vec2 = frag_coord - bottom_right; + + var dist: vec2 = vec2( + max(max(top_left_distance.x, bottom_right_distance.x), 0.0), + max(max(top_left_distance.y, bottom_right_distance.y), 0.0) + ); + + return sqrt(dist.x * dist.x + dist.y * dist.y); +} + + +[[stage(fragment)]] +fn fs_main( + input: VertexOutput, + [[builtin(position)]] coord: vec4 +) -> [[location(0)]] vec4 { + var mixed_color: vec4 = input.color; + + if (input.border_width > 0.0) { + var internal_border: f32 = max( + input.border_radius - input.border_width, + 0.0 + ); + + var internal_distance: f32 = distance_alg( + vec2(coord.x, coord.y), + input.pos + vec2(input.border_width, input.border_width), + input.scale - vec2(input.border_width * 2.0, input.border_width * 2.0), + internal_border + ); + + var border_mix: f32 = smoothStep( + max(internal_border - 0.5, 0.0), + internal_border + 0.5, + internal_distance + ); + + mixed_color = mix(input.color, input.border_color, vec4(border_mix, border_mix, border_mix, border_mix)); + } + + var dist: f32 = distance_alg( + vec2(coord.x, coord.y), + input.pos, + input.scale, + input.border_radius + ); + + var radius_alpha: f32 = 1.0 - smoothStep( + max(input.border_radius - 0.5, 0.0), + input.border_radius + 0.5, + dist); + + return vec4(radius_alpha, radius_alpha, radius_alpha, radius_alpha); +} \ No newline at end of file diff --git a/wgpu/src/shader/triangle.frag b/wgpu/src/shader/triangle.frag deleted file mode 100644 index e39c45e7..00000000 --- a/wgpu/src/shader/triangle.frag +++ /dev/null @@ -1,8 +0,0 @@ -#version 450 - -layout(location = 0) in vec4 i_Color; -layout(location = 0) out vec4 o_Color; - -void main() { - o_Color = i_Color; -} diff --git a/wgpu/src/shader/triangle.frag.spv b/wgpu/src/shader/triangle.frag.spv deleted file mode 100644 index 11201872..00000000 Binary files a/wgpu/src/shader/triangle.frag.spv and /dev/null differ diff --git a/wgpu/src/shader/triangle.vert b/wgpu/src/shader/triangle.vert deleted file mode 100644 index 1f2c009b..00000000 --- a/wgpu/src/shader/triangle.vert +++ /dev/null @@ -1,15 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 i_Position; -layout(location = 1) in vec4 i_Color; - -layout(location = 0) out vec4 o_Color; - -layout (set = 0, binding = 0) uniform Globals { - mat4 u_Transform; -}; - -void main() { - gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0); - o_Color = i_Color; -} diff --git a/wgpu/src/shader/triangle.vert.spv b/wgpu/src/shader/triangle.vert.spv deleted file mode 100644 index 871f4f55..00000000 Binary files a/wgpu/src/shader/triangle.vert.spv and /dev/null differ diff --git a/wgpu/src/shader/triangle.wgsl b/wgpu/src/shader/triangle.wgsl new file mode 100644 index 00000000..96eaabcc --- /dev/null +++ b/wgpu/src/shader/triangle.wgsl @@ -0,0 +1,31 @@ +[[block]] +struct Globals { + transform: mat4x4; +}; + +[[group(0), binding(0)]] var globals: Globals; + +struct VertexInput { + [[location(0)]] position: vec2; + [[location(1)]] color: vec4; +}; + +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] color: vec4; +}; + +[[stage(vertex)]] +fn vs_main(input: VertexInput) -> VertexOutput { + var out: VertexOutput; + + out.color = input.color; + out.position = globals.transform * vec4(input.position, 0.0, 1.0); + + return out; +} + +[[stage(fragment)]] +fn fs_main(input: VertexOutput) -> [[location(0)]] vec4 { + return input.color; +} -- cgit