From daab437d10adfd48385df34062126735ad8f7d21 Mon Sep 17 00:00:00 2001 From: Millennium Cyborg Date: Wed, 29 May 2024 13:32:42 +0100 Subject: [PATCH] Modify the opengl version of the blur shader to match the d3d11 version --- data/shaders/gaussian_1d_texture.effect | 87 +++++++++++++++++++++---- 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/data/shaders/gaussian_1d_texture.effect b/data/shaders/gaussian_1d_texture.effect index dc562be..b07f8f2 100644 --- a/data/shaders/gaussian_1d_texture.effect +++ b/data/shaders/gaussian_1d_texture.effect @@ -31,21 +31,33 @@ struct VertData { float2 uv : TEXCOORD0; }; +float4 PremulToStraight(float4 col) +{ + col.rgb = saturate(col.rgb / max(col.a, 0.0001f)); + return col; +} + +float4 StraightToPremul(float4 col) +{ + col.rgb *= col.a; + return col; +} + VertData mainTransform(VertData v_in) { v_in.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj); return v_in; } -float4 mainImage(VertData v_in) : TARGET +float4 SampleBlur(float2 uv) { // DO THE BLUR // 1. Sample incoming pixel, multiply by weight[0] - // float4 test = kernel_texture.Sample(tableSampler, float2(v_in.uv[0], 0.0)); + // float4 test = kernel_texture.Sample(tableSampler, float2(uv[0], 0.0)); // float max_radius = kernel_texture.Sample(tableSampler, float2(1.0, 0.0))[1]; // return float4(test.g/max_radius, test.g/max_radius, test.g/max_radius, 1.0); float weight = kernel_texture.Sample(tableSampler, float2(0.0f, 0.0f))[0]; - float4 col = image.Sample(textureSampler, v_in.uv) * weight; + float4 col = image.Sample(textureSampler, uv) * weight; float total_weight = weight; // 2. March out from incoming pixel, multiply by corresponding weight. @@ -55,18 +67,71 @@ float4 mainImage(VertData v_in) : TARGET weight = kernel_values[0]; float offset = kernel_values[1]; total_weight += 2.0f*weight; - col += image.Sample(textureSampler, v_in.uv + (offset * texel_step)) * weight; - col += image.Sample(textureSampler, v_in.uv - (offset * texel_step)) * weight; + col += image.Sample(textureSampler, uv + (offset * texel_step)) * weight; + col += image.Sample(textureSampler, uv - (offset * texel_step)) * weight; } col /= total_weight; return col; } -technique Draw +float4 mainStraightToPremul(VertData v_in) : TARGET { - pass - { - vertex_shader = mainTransform(v_in); - pixel_shader = mainImage(v_in); - } + return StraightToPremul(image.Sample(textureSampler, v_in.uv)); +} + +float4 mainPremulToStraight(VertData v_in) : TARGET +{ + return PremulToStraight(image.Sample(textureSampler, v_in.uv)); +} + +float4 mainBlur(VertData v_in) : TARGET +{ + // Input image is premultiplied + float4 col = SampleBlur(v_in.uv); + // Output is still premultiplied + return col; +} + +float4 mainBlurPremulToStraight(VertData v_in) : TARGET +{ + // Input image is premultiplied + float4 col = SampleBlur(v_in.uv); + // Output is straight alpha + return PremulToStraight(col); +} + +technique DrawStraightToPremul +{ + pass + { + vertex_shader = mainTransform(v_in); + pixel_shader = mainStraightToPremul(v_in); + } +} + +technique DrawPremulToStraight +{ + pass + { + vertex_shader = mainTransform(v_in); + pixel_shader = mainPremulToStraight(v_in); + } +} + +technique DrawBlur +{ + pass + { + vertex_shader = mainTransform(v_in); + pixel_shader = mainBlur(v_in); + } +} + +technique DrawBlurPremulToStraight +{ + pass + { + vertex_shader = mainTransform(v_in); + pixel_shader = mainBlurPremulToStraight(v_in); + } }