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

129 lines
3.6 KiB
Text
Raw Normal View History

// OBS Default
uniform float4x4 ViewProj;
// Settings (Shared)
uniform texture2d u_image;
uniform float2 u_imageSize;
uniform float2 u_imageTexelDelta;
uniform int u_radius;
uniform int u_diameter;
uniform float2 u_texelDelta;
// Settings (Private)
//uniform float registerkernel[25];
uniform texture2d kernel;
uniform float2 kernelTexel;
sampler_state pointClampSampler {
Filter = Point;
AddressU = Clamp;
AddressV = Clamp;
MinLOD = 0;
MaxLOD = 0;
};
sampler_state bilinearClampSampler {
Filter = Bilinear;
AddressU = Clamp;
AddressV = Clamp;
MinLOD = 0;
MaxLOD = 0;
};
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;
}
// Gaussian Blur
float Gaussian(float x, float o) {
const float pivalue = 3.1415926535897932384626433832795;
return (1.0 / (o * sqrt(2.0 * pivalue))) * exp((-(x * x)) / (2 * (o * o)));
}
float4 InternalGaussian(
float2 p_uv, float2 p_uvStep, int p_size,
texture2d p_source) {
float l_gauss = Gaussian(0, p_size);
float4 l_value = p_source.Sample(pointClampSampler, p_uv) * gauss;
float2 l_uvoffset = p_uvStep;
for (int k = 1; k <= p_size; k++) {
float l_g = Gaussian(k, p_size);
float4 l_p = p_source.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g;
float4 l_n = p_source.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g;
l_value += l_p + l_n;
l_uvoffset += p_uvStep;
l_gauss += l_g;
}
l_value = l_value * (1.0 / l_gauss);
return l_value;
}
float4 InternalGaussianPrecalculated(
float2 p_uv, float2 p_uvStep, int p_size,
texture2d p_source, float2 p_sourceStep,
texture2d p_kernel, float2 p_kernelStep) {
float4 l_value = p_source.Sample(pointClampSampler, p_uv + imageTexel)
* kernel.Sample(pointClampSampler, float2(0, 0)).r;
float2 l_uvoffset = p_uvStep;
float2 l_koffset = p_kerneltexel;
for (int k = 1; k <= p_size; k++) {
float l_g = p_kernel.Sample(pointClampSampler, l_koffset).r;
float4 l_p = p_source.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g;
float4 l_n = p_source.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g;
l_value += l_p + l_n;
l_uvoffset += p_uvStep;
l_koffset += p_kerneltexel;
}
return l_value;
}
float4 InternalGaussianPrecalculatedNVOptimized(float2 p_uv, texture2d p_source, float2 p_texel,
int p_size, texture2d p_kernel, texture2d p_kerneltexel) {
if (p_size % 2 == 0) {
float4 l_value = p_source.Sample(pointClampSampler, p_uv)
* kernel.Sample(pointClampSampler, float2(0, 0)).r;
float2 l_uvoffset = p_texel;
float2 l_koffset = p_kerneltexel;
for (int k = 1; k <= p_size; k++) {
float l_g = p_kernel.Sample(pointClampSampler, l_koffset).r;
float4 l_p = p_source.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g;
float4 l_n = p_source.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g;
l_value += l_p + l_n;
l_uvoffset += p_texel;
l_koffset += p_kerneltexel;
}
return l_value;
} else {
return InternalGaussianPrecalculated(p_uv, p_source, p_texel, p_size, p_kernel, p_kerneltexel);)
}
}
float4 PSGaussian(VertDataOut v_in) : TARGET {
//return InternalGaussian(v_in.uv, u_image, u_imageTexelDelta, u_radius);
//return InternalGaussianPrecalculated(v_in.uv, image, texel, radius, kernel, kernelTexel);
return InternalGaussianPrecalculatedNVOptimize(v_in.uv, u_
image, texel, radius, kernel, kernelTexel);
}
technique Draw
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSGaussian(v_in);
}
}