Skip to content

Commit

Permalink
Blur gaussian in premul space
Browse files Browse the repository at this point in the history
  • Loading branch information
y2kcyborg committed May 27, 2024
1 parent 7e499fd commit 84d2fc1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions data/shaders/gaussian_1d.effect
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ float4 mainImage(VertData v_in) : TARGET
{
// DO THE BLUR
// 1. Sample incoming pixel, multiply by weight[0]
float4 col = image.Sample(textureSampler, v_in.uv) * weightLookup(0);
float4 sample0 = image.Sample(textureSampler, v_in.uv);
// Convert to premul alpha
sample0.rgb *= sample0.a;
// Now it's cool to multiply and add col
float4 col = sample0 * weightLookup(0);
float total_weight = weightLookup(0);

// 2. March out from incoming pixel, multiply by corresponding weight.
for(uint i=1; i<kernel_size; i++) {
float weight = weightLookup(i);
float offset = offsetLookup(i);
total_weight += 2.0*weight;
col += image.Sample(textureSampler, v_in.uv + (offset * texel_step)) * weight;
col += image.Sample(textureSampler, v_in.uv - (offset * texel_step)) * weight;
float4 sampleL = image.Sample(textureSampler, v_in.uv + (offset * texel_step));
sampleL.rgb *= sampleL.a;
float4 sampleR = image.Sample(textureSampler, v_in.uv - (offset * texel_step));
sampleR.rgb *= sampleR.a;
col += sampleL * weight;
col += sampleR * weight;
}
col /= total_weight;
// Safely convert back to straight alpha
col.rgb = saturate(col.rgb / max(col.a, 0.0001f));
return col;
}

Expand Down

0 comments on commit 84d2fc1

Please sign in to comment.