| 1 | // Temporal MotionBlur GPU accumulator compute shaders (WebGPU-safe). |
| 2 | // |
| 3 | // Accumulation uses Rgba32Float ping-pong textures accessed strictly via |
| 4 | // `textureLoad` and write-only `textureStore` (never `textureSample` on |
| 5 | // Rgba32Float and never `read_write` storage textures). |
| 6 | |
| 7 | struct MotionBlurUniform { |
| 8 | // [dst_origin.x, dst_origin.y, sample_origin.x, sample_origin.y] |
| 9 | origins: vec4<i32>, |
| 10 | // [weight, 0.0, 0.0, 0.0] |
| 11 | params: vec4<f32>, |
| 12 | }; |
| 13 | |
| 14 | @group(0) @binding(0) var accum_src: texture_2d<f32>; |
| 15 | @group(0) @binding(1) var sample_tex: texture_2d<f32>; |
| 16 | @group(0) @binding(2) var accum_dst: texture_storage_2d<rgba32float, write>; |
| 17 | @group(0) @binding(3) var final_dst: texture_storage_2d<rgba8unorm, write>; |
| 18 | @group(0) @binding(4) var<uniform> u: MotionBlurUniform; |
| 19 | |
| 20 | // Overridden to `false` when building Rgba16Float / Rgba32Float finalize pipelines. |
| 21 | const CLAMP_UNORM: bool = true; |
| 22 | |
| 23 | @compute @workgroup_size(8, 8) |
| 24 | fn accum_clear(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 25 | let dims = textureDimensions(accum_dst); |
| 26 | if (gid.x >= dims.x || gid.y >= dims.y) { |
| 27 | return; |
| 28 | } |
| 29 | textureStore(accum_dst, vec2<i32>(gid.xy), vec4<f32>(0.0)); |
| 30 | } |
| 31 | |
| 32 | @compute @workgroup_size(8, 8) |
| 33 | fn accum_add(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 34 | let dst_dims = textureDimensions(accum_dst); |
| 35 | if (gid.x >= dst_dims.x || gid.y >= dst_dims.y) { |
| 36 | return; |
| 37 | } |
| 38 | let px = vec2<i32>(gid.xy); |
| 39 | let prev = textureLoad(accum_src, px, 0); |
| 40 | let sample_px = px + u.origins.xy - u.origins.zw; |
| 41 | let sample_dims = vec2<i32>(textureDimensions(sample_tex)); |
| 42 | var col = vec4<f32>(0.0); |
| 43 | if (sample_px.x >= 0 && sample_px.y >= 0 && sample_px.x < sample_dims.x && sample_px.y < sample_dims.y) { |
| 44 | col = textureLoad(sample_tex, sample_px, 0); |
| 45 | } |
| 46 | textureStore(accum_dst, px, prev + u.params.x * col); |
| 47 | } |
| 48 | |
| 49 | @compute @workgroup_size(8, 8) |
| 50 | fn accum_finalize(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 51 | let dims = textureDimensions(final_dst); |
| 52 | if (gid.x >= dims.x || gid.y >= dims.y) { |
| 53 | return; |
| 54 | } |
| 55 | let px = vec2<i32>(gid.xy); |
| 56 | let col = textureLoad(accum_src, px, 0); |
| 57 | if (CLAMP_UNORM) { |
| 58 | textureStore(final_dst, px, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0))); |
| 59 | } else { |
| 60 | textureStore(final_dst, px, col); |
| 61 | } |
| 62 | } |