2 // This is a modified version of the SSAO renderer from Microsoft's MiniEngine
3 // library. The copyright notice from the original version is included below.
5 // The original source code of MiniEngine is available on GitHub.
6 // https://github.com/Microsoft/DirectX-Graphics-Samples
10 // Copyright (c) Microsoft. All rights reserved.
11 // This code is licensed under the MIT License (MIT).
12 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
13 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
14 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
15 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
17 // Developed by Minigraph
19 // Author: James Stanard
22 #pragma warning(disable : 3568)
23 #pragma exclude_renderers gles gles3 d3d11_9x
25 #pragma kernel MultiScaleVODownsample1 main=MultiScaleVODownsample1
26 #pragma kernel MultiScaleVODownsample1_MSAA main=MultiScaleVODownsample1_MSAA MSAA
28 #include "../StdLib.hlsl"
32 RWTexture2D<float2> LinearZ;
33 RWTexture2D<float2> DS2x;
34 RWTexture2DArray<float2> DS2xAtlas;
35 RWTexture2D<float2> DS4x;
36 RWTexture2DArray<float2> DS4xAtlas;
39 Texture2D<float4> Depth;
42 groupshared float2 g_CacheW[256];
45 RWTexture2D<float> LinearZ;
46 RWTexture2D<float> DS2x;
47 RWTexture2DArray<float> DS2xAtlas;
48 RWTexture2D<float> DS4x;
49 RWTexture2DArray<float> DS4xAtlas;
52 Texture2D<float> Depth;
55 groupshared float g_CacheW[256];
63 float2 Linearize(uint2 st)
65 float depthMin = Depth[st].y;
66 float depthMax = Depth[st].x;
68 float2 depth = float2(depthMin, depthMax);
69 float2 dist = 1.0 / (ZBufferParams.x * depth + ZBufferParams.y);
70 #ifdef UNITY_REVERSED_Z
71 if (depth.x == 0) dist.x = 1e5;
72 if (depth.y == 0) dist.y = 1e5;
74 if (depth.x == 1) dist.x = 1e5;
75 if (depth.y == 1) dist.y = 1e5;
81 float Linearize(uint2 st)
83 float depth = Depth[st];
84 float dist = 1.0 / (ZBufferParams.x * depth + ZBufferParams.y);
85 #ifdef UNITY_REVERSED_Z
86 if (depth == 0) dist = 1e5;
88 if (depth == 1) dist = 1e5;
96 #ifdef DISABLE_COMPUTE_SHADERS
98 TRIVIAL_COMPUTE_KERNEL(main)
102 [numthreads(8, 8, 1)]
103 void main(uint3 Gid : SV_GroupID, uint GI : SV_GroupIndex, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID)
105 uint2 startST = Gid.xy << 4 | GTid.xy;
106 uint destIdx = GTid.y << 4 | GTid.x;
107 g_CacheW[destIdx + 0 ] = Linearize(startST | uint2(0, 0));
108 g_CacheW[destIdx + 8 ] = Linearize(startST | uint2(8, 0));
109 g_CacheW[destIdx + 128] = Linearize(startST | uint2(0, 8));
110 g_CacheW[destIdx + 136] = Linearize(startST | uint2(8, 8));
112 GroupMemoryBarrierWithGroupSync();
114 uint ldsIndex = (GTid.x << 1) | (GTid.y << 5);
117 float2 w1 = g_CacheW[ldsIndex];
119 float w1 = g_CacheW[ldsIndex];
122 uint slice = ((st.x & 3) | (st.y << 2)) & 15;
124 DS2xAtlas[uint3(st >> 2, slice)] = w1;
129 slice = ((st.x & 3) | (st.y << 2)) & 15;
131 DS4xAtlas[uint3(st >> 2, slice)] = w1;