summaryrefslogtreecommitdiff
path: root/rust/crates/gpu/src/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'rust/crates/gpu/src/shaders')
-rw-r--r--rust/crates/gpu/src/shaders/blit.wgsl12
-rw-r--r--rust/crates/gpu/src/shaders/fullscreen.wgsl12
-rw-r--r--rust/crates/gpu/src/shaders/gaussian_blur.wgsl34
-rw-r--r--rust/crates/gpu/src/shaders/jfa_distance.wgsl50
-rw-r--r--rust/crates/gpu/src/shaders/jfa_init.wgsl35
-rw-r--r--rust/crates/gpu/src/shaders/jfa_step.wgsl75
6 files changed, 218 insertions, 0 deletions
diff --git a/rust/crates/gpu/src/shaders/blit.wgsl b/rust/crates/gpu/src/shaders/blit.wgsl
new file mode 100644
index 00000000..2be67288
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/blit.wgsl
@@ -0,0 +1,12 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+@group(0) @binding(0) var input_texture: texture_2d<f32>;
+@group(0) @binding(1) var input_sampler: sampler;
+
+@fragment
+fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
+ return textureSample(input_texture, input_sampler, input.tex_coord);
+}
diff --git a/rust/crates/gpu/src/shaders/fullscreen.wgsl b/rust/crates/gpu/src/shaders/fullscreen.wgsl
new file mode 100644
index 00000000..1ef26e48
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/fullscreen.wgsl
@@ -0,0 +1,12 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+@vertex
+fn vertex_main(@location(0) position: vec2f) -> VertexOutput {
+ var output: VertexOutput;
+ output.position = vec4f(position, 0.0, 1.0);
+ output.tex_coord = (position * 0.5) + vec2f(0.5, 0.5);
+ return output;
+}
diff --git a/rust/crates/gpu/src/shaders/gaussian_blur.wgsl b/rust/crates/gpu/src/shaders/gaussian_blur.wgsl
new file mode 100644
index 00000000..6c8d311d
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/gaussian_blur.wgsl
@@ -0,0 +1,34 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+struct EffectUniforms {
+ resolution: vec2f,
+ direction: vec2f,
+ scalars: vec4f,
+}
+
+@group(0) @binding(0) var input_texture: texture_2d<f32>;
+@group(0) @binding(1) var input_sampler: sampler;
+@group(1) @binding(0) var<uniform> uniforms: EffectUniforms;
+
+@fragment
+fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
+ let texel_size = vec2f(1.0, 1.0) / uniforms.resolution;
+ let sigma = uniforms.scalars.x;
+ let step_size = uniforms.scalars.y;
+
+ var color = vec4f(0.0, 0.0, 0.0, 0.0);
+ var total_weight = 0.0;
+
+ for (var index = -30; index <= 30; index = index + 1) {
+ let position = f32(index) * step_size;
+ let weight = exp(-(position * position) / (2.0 * sigma * sigma));
+ let sample_uv = input.tex_coord + (texel_size * uniforms.direction * position);
+ color = color + textureSample(input_texture, input_sampler, sample_uv) * weight;
+ total_weight = total_weight + weight;
+ }
+
+ return color / total_weight;
+}
diff --git a/rust/crates/gpu/src/shaders/jfa_distance.wgsl b/rust/crates/gpu/src/shaders/jfa_distance.wgsl
new file mode 100644
index 00000000..e65a7023
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/jfa_distance.wgsl
@@ -0,0 +1,50 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+struct DistanceUniforms {
+ resolution: vec2f,
+ feather_half: f32,
+ _padding: f32,
+}
+
+@group(0) @binding(0) var inside_texture: texture_2d<f32>;
+@group(0) @binding(1) var inside_sampler: sampler;
+@group(1) @binding(0) var outside_texture: texture_2d<f32>;
+@group(1) @binding(1) var outside_sampler: sampler;
+@group(2) @binding(0) var<uniform> uniforms: DistanceUniforms;
+
+fn decode_seed(encoded: vec4f) -> vec2f {
+ let x = floor(encoded.r * 255.0 + 0.5) * 256.0 + floor(encoded.g * 255.0 + 0.5);
+ let y = floor(encoded.b * 255.0 + 0.5) * 256.0 + floor(encoded.a * 255.0 + 0.5);
+ return vec2f(x, y);
+}
+
+fn is_no_seed(encoded: vec4f) -> bool {
+ return encoded.r > 0.99 && encoded.g > 0.99 && encoded.b > 0.99 && encoded.a > 0.99;
+}
+
+@fragment
+fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
+ let pixel_coord = floor(input.tex_coord * uniforms.resolution);
+ let inside_encoded = textureSample(inside_texture, inside_sampler, input.tex_coord);
+ let outside_encoded = textureSample(outside_texture, outside_sampler, input.tex_coord);
+
+ let has_inside = !is_no_seed(inside_encoded);
+ let has_outside = !is_no_seed(outside_encoded);
+ let distance_to_inside = select(
+ 100000.0,
+ distance(pixel_coord, decode_seed(inside_encoded)),
+ has_inside,
+ );
+ let distance_to_outside = select(
+ 100000.0,
+ distance(pixel_coord, decode_seed(outside_encoded)),
+ has_outside,
+ );
+ let signed_distance = distance_to_outside - distance_to_inside;
+ let alpha = smoothstep(-uniforms.feather_half, uniforms.feather_half, signed_distance);
+
+ return vec4f(alpha, alpha, alpha, alpha);
+}
diff --git a/rust/crates/gpu/src/shaders/jfa_init.wgsl b/rust/crates/gpu/src/shaders/jfa_init.wgsl
new file mode 100644
index 00000000..29aa38c7
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/jfa_init.wgsl
@@ -0,0 +1,35 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+struct JfaInitUniforms {
+ resolution: vec2f,
+ invert: f32,
+ _padding: f32,
+}
+
+@group(0) @binding(0) var input_texture: texture_2d<f32>;
+@group(0) @binding(1) var input_sampler: sampler;
+@group(1) @binding(0) var<uniform> uniforms: JfaInitUniforms;
+
+fn encode_seed(seed: vec2f) -> vec4f {
+ let x_hi = floor(seed.x / 256.0);
+ let x_lo = seed.x - (x_hi * 256.0);
+ let y_hi = floor(seed.y / 256.0);
+ let y_lo = seed.y - (y_hi * 256.0);
+ return vec4f(x_hi / 255.0, x_lo / 255.0, y_hi / 255.0, y_lo / 255.0);
+}
+
+@fragment
+fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
+ let mask = textureSample(input_texture, input_sampler, input.tex_coord).r;
+ let is_seed = select(mask > 0.5, mask < 0.5, uniforms.invert > 0.5);
+
+ if (is_seed) {
+ let pixel_coord = floor(input.tex_coord * uniforms.resolution);
+ return encode_seed(pixel_coord);
+ }
+
+ return vec4f(1.0, 1.0, 1.0, 1.0);
+}
diff --git a/rust/crates/gpu/src/shaders/jfa_step.wgsl b/rust/crates/gpu/src/shaders/jfa_step.wgsl
new file mode 100644
index 00000000..e20d7fb0
--- /dev/null
+++ b/rust/crates/gpu/src/shaders/jfa_step.wgsl
@@ -0,0 +1,75 @@
+struct VertexOutput {
+ @builtin(position) position: vec4f,
+ @location(0) tex_coord: vec2f,
+}
+
+struct JfaStepUniforms {
+ resolution: vec2f,
+ step_size: f32,
+ _padding: f32,
+}
+
+@group(0) @binding(0) var input_texture: texture_2d<f32>;
+@group(0) @binding(1) var input_sampler: sampler;
+@group(1) @binding(0) var<uniform> uniforms: JfaStepUniforms;
+
+fn decode_seed(encoded: vec4f) -> vec2f {
+ let x = floor(encoded.r * 255.0 + 0.5) * 256.0 + floor(encoded.g * 255.0 + 0.5);
+ let y = floor(encoded.b * 255.0 + 0.5) * 256.0 + floor(encoded.a * 255.0 + 0.5);
+ return vec2f(x, y);
+}
+
+fn encode_seed(seed: vec2f) -> vec4f {
+ let x_hi = floor(seed.x / 256.0);
+ let x_lo = seed.x - (x_hi * 256.0);
+ let y_hi = floor(seed.y / 256.0);
+ let y_lo = seed.y - (y_hi * 256.0);
+ return vec4f(x_hi / 255.0, x_lo / 255.0, y_hi / 255.0, y_lo / 255.0);
+}
+
+fn is_no_seed(encoded: vec4f) -> bool {
+ return encoded.r > 0.99 && encoded.g > 0.99 && encoded.b > 0.99 && encoded.a > 0.99;
+}
+
+@fragment
+fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
+ let pixel_coord = floor(input.tex_coord * uniforms.resolution);
+ let texel_size = vec2f(1.0, 1.0) / uniforms.resolution;
+
+ var best_distance = 10000000000.0;
+ var best_seed = vec2f(65535.0, 65535.0);
+
+ for (var y = -1; y <= 1; y = y + 1) {
+ for (var x = -1; x <= 1; x = x + 1) {
+ let offset = vec2f(f32(x), f32(y)) * uniforms.step_size;
+ let sample_uv = input.tex_coord + (offset * texel_size);
+
+ if (
+ sample_uv.x < 0.0 ||
+ sample_uv.x > 1.0 ||
+ sample_uv.y < 0.0 ||
+ sample_uv.y > 1.0
+ ) {
+ continue;
+ }
+
+ let encoded = textureSample(input_texture, input_sampler, sample_uv);
+ if (is_no_seed(encoded)) {
+ continue;
+ }
+
+ let seed = decode_seed(encoded);
+ let distance_to_seed = distance(pixel_coord, seed);
+ if (distance_to_seed < best_distance) {
+ best_distance = distance_to_seed;
+ best_seed = seed;
+ }
+ }
+ }
+
+ if (best_distance < 1000000000.0) {
+ return encode_seed(best_seed);
+ }
+
+ return vec4f(1.0, 1.0, 1.0, 1.0);
+}