aboutsummaryrefslogtreecommitdiff
path: root/demo/d3d/shaders/blurDepthPS.hlsl
diff options
context:
space:
mode:
authorMiles Macklin <[email protected]>2017-06-09 13:41:15 +1200
committerMiles Macklin <[email protected]>2017-06-09 13:41:15 +1200
commit688b5f42e9bfe498d7af7075d4d8f4429867f3a3 (patch)
tree7e0d0e7c95298f0418723abd92f61ac6e16b055e /demo/d3d/shaders/blurDepthPS.hlsl
parentUpdate README.md (diff)
downloadflex-688b5f42e9bfe498d7af7075d4d8f4429867f3a3.tar.xz
flex-688b5f42e9bfe498d7af7075d4d8f4429867f3a3.zip
1.2.0.beta.11.2.0.beta.1
Diffstat (limited to 'demo/d3d/shaders/blurDepthPS.hlsl')
-rw-r--r--demo/d3d/shaders/blurDepthPS.hlsl84
1 files changed, 84 insertions, 0 deletions
diff --git a/demo/d3d/shaders/blurDepthPS.hlsl b/demo/d3d/shaders/blurDepthPS.hlsl
new file mode 100644
index 0000000..c08d7f5
--- /dev/null
+++ b/demo/d3d/shaders/blurDepthPS.hlsl
@@ -0,0 +1,84 @@
+#include "shaderCommon.h"
+
+cbuffer constBuf : register(b0)
+{
+ FluidShaderConst gParams;
+};
+
+Texture2D<float> depthTex : register(t0);
+
+float sqr(float x) { return x*x; }
+
+float blurDepthPS(PassthroughVertexOut input) : SV_TARGET
+{
+ float4 inPosition = input.position;
+
+ // debug: return the center depth sample
+ //return depthTex.Load(int3(inPosition.xy, 0)).x;
+
+ const float blurRadiusWorld = gParams.blurRadiusWorld;
+ const float blurScale = gParams.blurScale;
+ const float blurFalloff = gParams.blurFalloff;
+
+ // eye-space depth of center sample
+ float depth = depthTex.Load(int3(inPosition.xy, 0)).x;
+ float thickness = 0.0f; //texture2D(thicknessTex, gl_TexCoord[0].xy).x;
+
+ /*
+ // threshold on thickness to create nice smooth silhouettes
+ if (depth == 0.0)
+ {
+ return 0.0f;
+ }
+ */
+
+ float blurDepthFalloff = 5.5;
+ float maxBlurRadius = 5.0;
+
+ //discontinuities between different tap counts are visible. to avoid this we
+ //use fractional contributions between #taps = ceil(radius) and floor(radius)
+ float radius = min(maxBlurRadius, blurScale * (blurRadiusWorld / -depth));
+ float radiusInv = 1.0 / radius;
+ float taps = ceil(radius);
+ float frac = taps - radius;
+
+ float sum = 0.0;
+ float wsum = 0.0;
+ float count = 0.0;
+
+ for (float y = -taps; y <= taps; y += 1.0)
+ {
+ for (float x = -taps; x <= taps; x += 1.0)
+ {
+ float2 offset = float2(x, y);
+ float sample = depthTex.Load(int3(inPosition.xy + offset, 0)).x;
+
+ //if (sample < -10000.0 * 0.5)
+ //continue;
+
+ // spatial domain
+ float r1 = length(float2(x, y))*radiusInv;
+ float w = exp(-(r1*r1));
+
+ // range domain (based on depth difference)
+ float r2 = (sample - depth) * blurDepthFalloff;
+ float g = exp(-(r2*r2));
+
+ //fractional radius contributions
+ float wBoundary = step(radius, max(abs(x), abs(y)));
+ float wFrac = 1.0 - wBoundary*frac;
+
+ sum += sample * w * g * wFrac;
+ wsum += w * g * wFrac;
+ count += g * wFrac;
+ }
+ }
+
+ if (wsum > 0.0)
+ {
+ sum /= wsum;
+ }
+
+ float blend = count / sqr(2.0 * radius + 1.0);
+ return lerp(depth, sum, blend);
+}