From 2f55841f887d884a1fbd26f003c604f682568a81 Mon Sep 17 00:00:00 2001 From: Xaymar Date: Sun, 22 Oct 2017 19:05:29 +0200 Subject: [PATCH] effect: Code update, clean up and optimization --- data/effects/bilateral-blur.effect | 40 +++++++++------ data/effects/box-blur.effect | 2 +- data/effects/gaussian-blur.effect | 78 ++++++++++++++++-------------- 3 files changed, 69 insertions(+), 51 deletions(-) diff --git a/data/effects/bilateral-blur.effect b/data/effects/bilateral-blur.effect index e31912fb..7d94c8f3 100644 --- a/data/effects/bilateral-blur.effect +++ b/data/effects/bilateral-blur.effect @@ -4,7 +4,7 @@ uniform float4x4 ViewProj; // Settings (Shared) uniform texture2d u_image; uniform float2 u_imageSize; -uniform float2 u_imageTexelDelta; +uniform float2 u_imageTexel; uniform int u_radius; uniform int u_diameter; uniform float2 u_texelDelta; @@ -45,32 +45,44 @@ float Bilateral(float x, float 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 { - float4 source = image.Sample(textureSampler, v_in.uv); + float2 l_uv = float2(0, 0); - float3 color = float3(0, 0, 0); - float2 rootuv = v_in.uv - (texel * widthHalf); float Z = 0.0; float bZ = 1.0 / Bilateral(0.0, bilateralSharpness); - for (int k = -widthHalf; k <= widthHalf; k++) { - // Sample Color - float3 sample = image.Sample(textureSampler, rootuv); + 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 Stuff + // Bilateral Kernel float bKernel = Bilateral(abs(k), bilateralSmoothing); bKernel *= bKernel; - float factor = Bilateral3(sample - source.rgb, bilateralSharpness) * bZ * bKernel; - Z += factor; + 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 += factor * sample; - - // Advance UV - rootuv += texel; + color += l_p * l_factor_p; + color += l_n * l_factor_n; } return float4(color.rgb / Z, source.a); diff --git a/data/effects/box-blur.effect b/data/effects/box-blur.effect index 1b6210c6..b4a2f361 100644 --- a/data/effects/box-blur.effect +++ b/data/effects/box-blur.effect @@ -4,7 +4,7 @@ uniform float4x4 ViewProj; // Settings (Shared) uniform texture2d u_image; uniform float2 u_imageSize; -uniform float2 u_imageTexelDelta; +uniform float2 u_imageTexel; uniform int u_radius; uniform int u_diameter; uniform float2 u_texelDelta; diff --git a/data/effects/gaussian-blur.effect b/data/effects/gaussian-blur.effect index eace1247..a27c9aeb 100644 --- a/data/effects/gaussian-blur.effect +++ b/data/effects/gaussian-blur.effect @@ -4,7 +4,7 @@ uniform float4x4 ViewProj; // Settings (Shared) uniform texture2d u_image; uniform float2 u_imageSize; -uniform float2 u_imageTexelDelta; +uniform float2 u_imageTexel; uniform int u_radius; uniform int u_diameter; uniform float2 u_texelDelta; @@ -53,69 +53,75 @@ float Gaussian(float x, float o) { 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) { +float4 InternalGaussian(float2 p_uv, float2 p_uvStep, int p_radius, + texture2d p_image, float2 p_imageTexel) { float l_gauss = Gaussian(0, p_size); - float4 l_value = p_source.Sample(pointClampSampler, p_uv) * gauss; - float2 l_uvoffset = p_uvStep; + float4 l_value = p_image.Sample(pointClampSampler, p_uv) * gauss; + float2 l_uvoffset = float2(0, 0); 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; + float l_g = Gaussian(k, p_size); + float4 l_p = p_image.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g; + float4 l_n = p_image.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g; + l_value += l_p + l_n; 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) +float4 InternalGaussianPrecalculated(float2 p_uv, float2 p_uvStep, int p_radius, + texture2d p_image, float2 p_imageTexel, + texture2d p_kernel, float2 p_kernelTexel) { + float4 l_value = p_image.Sample(pointClampSampler, p_uv) * 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; + float2 l_uvoffset = float2(0, 0); + for (int k = 1; k <= p_radius; k++) { l_uvoffset += p_uvStep; - l_koffset += p_kerneltexel; + float l_g = p_kernel.Sample(pointClampSampler, p_kernelTexel * k).r; + float4 l_p = p_image.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g; + float4 l_n = p_image.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g; + l_value += l_p + l_n; } return l_value; } -float4 InternalGaussianPrecalculatedNVOptimized(float2 p_uv, texture2d p_source, float2 p_texel, - int p_size, texture2d p_kernel, texture2d p_kerneltexel) { +/*float4 InternalGaussianPrecalculatedNVOptimized(float2 p_uv, int p_size, + texture2d p_image, float2 p_imageTexel, + texture2d p_kernel, float2 p_kernelTexel) { if (p_size % 2 == 0) { - float4 l_value = p_source.Sample(pointClampSampler, p_uv) + float4 l_value = p_image.Sample(pointClampSampler, p_uv) * kernel.Sample(pointClampSampler, float2(0, 0)).r; float2 l_uvoffset = p_texel; - float2 l_koffset = p_kerneltexel; + 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; + float4 l_p = p_image.Sample(pointClampSampler, p_uv + l_uvoffset) * l_g; + float4 l_n = p_image.Sample(pointClampSampler, p_uv - l_uvoffset) * l_g; l_value += l_p + l_n; l_uvoffset += p_texel; - l_koffset += p_kerneltexel; + l_koffset += p_kernelTexel; } return l_value; } else { - return InternalGaussianPrecalculated(p_uv, p_source, p_texel, p_size, p_kernel, p_kerneltexel);) + return InternalGaussianPrecalculated(p_uv, p_image, 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); + //return InternalGaussian(v_in.uv, u_texelDelta, u_image, u_imageTexel, u_radius); + + ///* + return InternalGaussianPrecalculated( + v_in.uv, u_texelDelta, u_radius, + u_image, u_imageTexel, + kernel, kernelTexel);//*/ + + /* + return InternalGaussianPrecalculatedNVOptimize( + v_in.uv, u_texelDelta, u_radius, + u_image, u_imageTexel, + kernel, kernelTexel);//*/ } technique Draw