|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Text; |
| 5 | +using Silk.NET.WebGPU; |
| 6 | + |
| 7 | +namespace SixLabors.ImageSharp.Drawing.Processing.Backends; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// GPU compute shader that composites a source layer texture onto a destination texture |
| 11 | +/// using configurable blend mode, alpha composition mode, and opacity. |
| 12 | +/// </summary> |
| 13 | +internal static class ComposeLayerComputeShader |
| 14 | +{ |
| 15 | + private static readonly object CacheSync = new(); |
| 16 | + private static readonly Dictionary<TextureFormat, byte[]> ShaderCache = []; |
| 17 | + |
| 18 | + private static readonly string ShaderTemplate = |
| 19 | + """ |
| 20 | + struct LayerConfig { |
| 21 | + source_width: u32, |
| 22 | + source_height: u32, |
| 23 | + dest_offset_x: i32, |
| 24 | + dest_offset_y: i32, |
| 25 | + color_blend_mode: u32, |
| 26 | + alpha_composition_mode: u32, |
| 27 | + blend_percentage: u32, |
| 28 | + _padding: u32, |
| 29 | + }; |
| 30 | +
|
| 31 | + @group(0) @binding(0) var source_texture: texture_2d<__TEXEL_TYPE__>; |
| 32 | + @group(0) @binding(1) var backdrop_texture: texture_2d<__TEXEL_TYPE__>; |
| 33 | + @group(0) @binding(2) var output_texture: texture_storage_2d<__OUTPUT_FORMAT__, write>; |
| 34 | + @group(0) @binding(3) var<uniform> config: LayerConfig; |
| 35 | +
|
| 36 | + __DECODE_TEXEL_FUNCTION__ |
| 37 | +
|
| 38 | + __ENCODE_OUTPUT_FUNCTION__ |
| 39 | +
|
| 40 | + __BLEND_AND_COMPOSE__ |
| 41 | +
|
| 42 | + @compute @workgroup_size(16, 16, 1) |
| 43 | + fn cs_main(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 44 | + // Output coordinates are in local output-texture space. |
| 45 | + let out_x = i32(gid.x); |
| 46 | + let out_y = i32(gid.y); |
| 47 | +
|
| 48 | + // Destination coordinates map into the full backdrop texture. |
| 49 | + let dest_x = out_x + config.dest_offset_x; |
| 50 | + let dest_y = out_y + config.dest_offset_y; |
| 51 | +
|
| 52 | + let dest_dims = textureDimensions(backdrop_texture); |
| 53 | + if (dest_x < 0 || dest_y < 0 || u32(dest_x) >= dest_dims.x || u32(dest_y) >= dest_dims.y) { |
| 54 | + return; |
| 55 | + } |
| 56 | +
|
| 57 | + let src_x = out_x; |
| 58 | + let src_y = out_y; |
| 59 | + if (u32(src_x) >= config.source_width || u32(src_y) >= config.source_height) { |
| 60 | + // Outside layer bounds — pass through the backdrop. |
| 61 | + let backdrop = decode_texel(__LOAD_BACKDROP__); |
| 62 | + let alpha = backdrop.a; |
| 63 | + let rgb = unpremultiply(backdrop.rgb, alpha); |
| 64 | + __STORE_OUTPUT__ |
| 65 | + return; |
| 66 | + } |
| 67 | +
|
| 68 | + let backdrop = decode_texel(__LOAD_BACKDROP__); |
| 69 | + let source_raw = decode_texel(__LOAD_SOURCE__); |
| 70 | +
|
| 71 | + // Apply layer opacity. |
| 72 | + let opacity = bitcast<f32>(config.blend_percentage); |
| 73 | + let source = vec4<f32>(source_raw.rgb, source_raw.a * opacity); |
| 74 | +
|
| 75 | + let result = compose_pixel(backdrop, source, config.color_blend_mode, config.alpha_composition_mode); |
| 76 | + let alpha = result.a; |
| 77 | + let rgb = unpremultiply(result.rgb, alpha); |
| 78 | + __STORE_OUTPUT__ |
| 79 | + } |
| 80 | + """; |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Gets the null-terminated WGSL source for the layer composite shader variant. |
| 84 | + /// </summary> |
| 85 | + public static bool TryGetCode(TextureFormat textureFormat, out byte[] code, out string? error) |
| 86 | + { |
| 87 | + if (!CompositeComputeShader.TryGetInputSampleType(textureFormat, out _)) |
| 88 | + { |
| 89 | + code = []; |
| 90 | + error = $"Layer composite shader does not support texture format '{textureFormat}'."; |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + lock (CacheSync) |
| 95 | + { |
| 96 | + if (ShaderCache.TryGetValue(textureFormat, out byte[]? cachedCode)) |
| 97 | + { |
| 98 | + code = cachedCode; |
| 99 | + error = null; |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + LayerShaderTraits traits = GetTraits(textureFormat); |
| 104 | + string source = ShaderTemplate |
| 105 | + .Replace("__TEXEL_TYPE__", traits.TexelType, StringComparison.Ordinal) |
| 106 | + .Replace("__OUTPUT_FORMAT__", traits.OutputFormat, StringComparison.Ordinal) |
| 107 | + .Replace("__DECODE_TEXEL_FUNCTION__", traits.DecodeTexelFunction, StringComparison.Ordinal) |
| 108 | + .Replace("__ENCODE_OUTPUT_FUNCTION__", traits.EncodeOutputFunction, StringComparison.Ordinal) |
| 109 | + .Replace("__BLEND_AND_COMPOSE__", CompositionShaderSnippets.BlendAndCompose, StringComparison.Ordinal) |
| 110 | + .Replace("__LOAD_BACKDROP__", traits.LoadBackdropExpression, StringComparison.Ordinal) |
| 111 | + .Replace("__LOAD_SOURCE__", traits.LoadSourceExpression, StringComparison.Ordinal) |
| 112 | + .Replace("__STORE_OUTPUT__", traits.StoreOutputStatement, StringComparison.Ordinal); |
| 113 | + |
| 114 | + byte[] sourceBytes = Encoding.UTF8.GetBytes(source); |
| 115 | + code = new byte[sourceBytes.Length + 1]; |
| 116 | + sourceBytes.CopyTo(code, 0); |
| 117 | + code[^1] = 0; |
| 118 | + ShaderCache[textureFormat] = code; |
| 119 | + } |
| 120 | + |
| 121 | + error = null; |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + private static LayerShaderTraits GetTraits(TextureFormat textureFormat) |
| 126 | + { |
| 127 | + return textureFormat switch |
| 128 | + { |
| 129 | + TextureFormat.R8Unorm => CreateFloatTraits("r8unorm"), |
| 130 | + TextureFormat.RG8Unorm => CreateFloatTraits("rg8unorm"), |
| 131 | + TextureFormat.Rgba8Unorm => CreateFloatTraits("rgba8unorm"), |
| 132 | + TextureFormat.Bgra8Unorm => CreateFloatTraits("bgra8unorm"), |
| 133 | + TextureFormat.Rgb10A2Unorm => CreateFloatTraits("rgb10a2unorm"), |
| 134 | + TextureFormat.R16float => CreateFloatTraits("r16float"), |
| 135 | + TextureFormat.RG16float => CreateFloatTraits("rg16float"), |
| 136 | + TextureFormat.Rgba16float => CreateFloatTraits("rgba16float"), |
| 137 | + TextureFormat.Rgba32float => CreateFloatTraits("rgba32float"), |
| 138 | + TextureFormat.RG8Snorm => CreateSnormTraits("rg8snorm"), |
| 139 | + TextureFormat.Rgba8Snorm => CreateSnormTraits("rgba8snorm"), |
| 140 | + _ => CreateFloatTraits("rgba8unorm"), |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + private static LayerShaderTraits CreateFloatTraits(string outputFormat) |
| 145 | + { |
| 146 | + const string decodeTexel = |
| 147 | + """ |
| 148 | + fn decode_texel(texel: vec4<f32>) -> vec4<f32> { |
| 149 | + return texel; |
| 150 | + } |
| 151 | + """; |
| 152 | + |
| 153 | + const string encodeOutput = |
| 154 | + """ |
| 155 | + fn encode_output(color: vec4<f32>) -> vec4<f32> { |
| 156 | + return color; |
| 157 | + } |
| 158 | + """; |
| 159 | + |
| 160 | + return new LayerShaderTraits( |
| 161 | + outputFormat, |
| 162 | + "f32", |
| 163 | + decodeTexel, |
| 164 | + encodeOutput, |
| 165 | + "textureLoad(backdrop_texture, vec2<i32>(dest_x, dest_y), 0)", |
| 166 | + "textureLoad(source_texture, vec2<i32>(src_x, src_y), 0)", |
| 167 | + "textureStore(output_texture, vec2<i32>(out_x, out_y), encode_output(vec4<f32>(rgb, alpha)));"); |
| 168 | + } |
| 169 | + |
| 170 | + private static LayerShaderTraits CreateSnormTraits(string outputFormat) |
| 171 | + { |
| 172 | + const string decodeTexel = |
| 173 | + """ |
| 174 | + fn decode_texel(texel: vec4<f32>) -> vec4<f32> { |
| 175 | + return (texel * 0.5) + vec4<f32>(0.5); |
| 176 | + } |
| 177 | + """; |
| 178 | + |
| 179 | + const string encodeOutput = |
| 180 | + """ |
| 181 | + fn encode_output(color: vec4<f32>) -> vec4<f32> { |
| 182 | + let clamped = clamp(color, vec4<f32>(0.0), vec4<f32>(1.0)); |
| 183 | + return (clamped * 2.0) - vec4<f32>(1.0); |
| 184 | + } |
| 185 | + """; |
| 186 | + |
| 187 | + return new LayerShaderTraits( |
| 188 | + outputFormat, |
| 189 | + "f32", |
| 190 | + decodeTexel, |
| 191 | + encodeOutput, |
| 192 | + "textureLoad(backdrop_texture, vec2<i32>(dest_x, dest_y), 0)", |
| 193 | + "textureLoad(source_texture, vec2<i32>(src_x, src_y), 0)", |
| 194 | + "textureStore(output_texture, vec2<i32>(out_x, out_y), encode_output(vec4<f32>(rgb, alpha)));"); |
| 195 | + } |
| 196 | + |
| 197 | + private readonly struct LayerShaderTraits( |
| 198 | + string outputFormat, |
| 199 | + string texelType, |
| 200 | + string decodeTexelFunction, |
| 201 | + string encodeOutputFunction, |
| 202 | + string loadBackdropExpression, |
| 203 | + string loadSourceExpression, |
| 204 | + string storeOutputStatement) |
| 205 | + { |
| 206 | + public string OutputFormat { get; } = outputFormat; |
| 207 | + |
| 208 | + public string TexelType { get; } = texelType; |
| 209 | + |
| 210 | + public string DecodeTexelFunction { get; } = decodeTexelFunction; |
| 211 | + |
| 212 | + public string EncodeOutputFunction { get; } = encodeOutputFunction; |
| 213 | + |
| 214 | + public string LoadBackdropExpression { get; } = loadBackdropExpression; |
| 215 | + |
| 216 | + public string LoadSourceExpression { get; } = loadSourceExpression; |
| 217 | + |
| 218 | + public string StoreOutputStatement { get; } = storeOutputStatement; |
| 219 | + } |
| 220 | +} |
0 commit comments