doing blur shader without alpha blending

This commit is contained in:
Martins Mozeiko
2024-01-23 11:45:42 -08:00
committed by Ryan Fleury
parent 82f86d654f
commit bae91cd40c
4 changed files with 35 additions and 31 deletions
+12 -15
View File
@@ -226,6 +226,7 @@ struct Vertex2Pixel
float4 position : SV_POSITION;
float2 texcoord : TEX;
float2 sdf_sample_pos : SDF;
nointerpolation float2 rect_half_size : RHS;
float corner_radius : RAD;
};
@@ -270,6 +271,7 @@ vs_main(CPU2Vertex c2v)
v2p.position = float4(vertex_position__scr.x, -vertex_position__scr.y, 0.f, 1.f);
v2p.texcoord = vertex_position__pct;
v2p.sdf_sample_pos = (2.f * cornercoords__pct - 1.f) * rect_half_size;
v2p.rect_half_size = rect_half_size - 2.f;
v2p.corner_radius = corner_radii__px[c2v.vertex_id];
}
return v2p;
@@ -281,33 +283,28 @@ float4
ps_main(Vertex2Pixel v2p) : SV_TARGET
{
// rjf: blend weighted texture samples into color
float4 color = kernel[0].x * stage_t2d.Sample(stage_sampler, v2p.texcoord);
color.a = kernel[0].x;
float3 color = kernel[0].x * stage_t2d.Sample(stage_sampler, v2p.texcoord).rgb;
for(uint i = 1; i < blur_count; i += 1)
{
float weight = kernel[i].x;
float offset = kernel[i].y;
float4 min_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord - offset * direction);
float4 max_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord + offset * direction);
min_sample.a = 1.f;
max_sample.a = 1.f;
color += min_sample * weight;
color += max_sample * weight;
color += weight * stage_t2d.Sample(stage_sampler, v2p.texcoord - offset * direction).rgb;
color += weight * stage_t2d.Sample(stage_sampler, v2p.texcoord + offset * direction).rgb;
}
// rjf: determine SDF sample position
float2 rect_half_size = float2((rect.z-rect.x)/2, (rect.w-rect.y)/2);
float2 sdf_sample_pos = v2p.sdf_sample_pos;
// rjf: sample for corners
float corner_sdf_s = rect_sdf(sdf_sample_pos, rect_half_size - 2.f, v2p.corner_radius);
float corner_sdf_s = rect_sdf(v2p.sdf_sample_pos, v2p.rect_half_size, v2p.corner_radius);
float corner_sdf_t = 1-smoothstep(0, 2, corner_sdf_s);
// rjf: weight output color by sdf
color.a *= corner_sdf_t;
// this is doing alpha testing, leave blurring only where mostly opaque pixels are
if (corner_sdf_t < 0.9f)
{
discard;
}
return color;
return float4(color, 1.f);
}
"""