obs-StreamFX/data/effects/bilateral-blur.effect

98 lines
2.2 KiB
Text

// OBS Default
uniform float4x4 ViewProj;
// Settings (Shared)
uniform texture2d u_image;
uniform float2 u_imageSize;
uniform float2 u_imageTexel;
uniform int u_radius;
uniform int u_diameter;
uniform float2 u_texelDelta;
// Settings (Private)
uniform float bilateralSmoothing;
uniform float bilateralSharpness;
sampler_state textureSampler {
Filter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
struct VertDataIn {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
struct VertDataOut {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
VertDataOut VSDefault(VertDataIn v_in)
{
VertDataOut vert_out;
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
vert_out.uv = v_in.uv;
return vert_out;
}
// Bilateral Blur
float Bilateral(float x, float sigma)
{
return 0.39894 * exp(-0.5 * (x*x) / (sigma*sigma)) / sigma;
}
float Bilateral3(float3 v, float sigma)
{
// First part is Gaussian function (1.0 / (o * sqrt(2.0 * pivalue))) with o = 1
return 0.39894 * exp(-0.5 * dot(v,v) / (sigma*sigma)) / sigma;
}
float4 BilateralBlur(float2 p_uv, float2 p_radius,
texture2d p_image, float2 p_imageTexel) {
float2 l_uvoffset = float2(0, 0);
}
float4 PSBilateral(VertDataOut v_in) : TARGET
{
float2 l_uv = float2(0, 0);
float Z = 0.0;
float bZ = 1.0 / Bilateral(0.0, bilateralSharpness);
float4 source = u_image.Sample(textureSampler, v_in.uv);
float3 color = float3(0, 0, 0);
for (int k = 1; k <= u_radius; k++) {
// Advance UV by one texel.
l_uv += u_texelDelta;
// Bilateral Kernel
float bKernel = Bilateral(abs(k), bilateralSmoothing);
bKernel *= bKernel;
float bZKernel = bZ * bKernel;
// Sample Color
float3 l_p = u_image.Sample(textureSampler, v_in.uv + l_uv).rgb;
float3 l_n = u_image.Sample(textureSampler, v_in.uv - l_uv).rgb;
// Bilateral Stuff
float l_factor_p = Bilateral3(l_p - source.rgb, bilateralSharpness) * bZKernel;
float l_factor_n = Bilateral3(l_n - source.rgb, bilateralSharpness) * bZKernel;
Z = Z + l_factor_p + l_factor_n;
// Store Color
color += l_p * l_factor_p;
color += l_n * l_factor_n;
}
return float4(color.rgb / Z, source.a);
}
technique Draw
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSBilateral(v_in);
}
}