MultiScaleVODownsample1.compute 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  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.
  4. //
  5. // The original source code of MiniEngine is available on GitHub.
  6. // https://github.com/Microsoft/DirectX-Graphics-Samples
  7. //
  8. //
  9. // Copyright (c) Microsoft. All rights reserved.
  10. // This code is licensed under the MIT License (MIT).
  11. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  12. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  13. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  14. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  15. //
  16. // Developed by Minigraph
  17. //
  18. // Author: James Stanard
  19. //
  20. #pragma warning(disable : 3568)
  21. #pragma exclude_renderers gles gles3 d3d11_9x
  22. #pragma kernel MultiScaleVODownsample1 main=MultiScaleVODownsample1
  23. #pragma kernel MultiScaleVODownsample1_MSAA main=MultiScaleVODownsample1_MSAA MSAA
  24. #include "../StdLib.hlsl"
  25. #ifdef MSAA
  26. // Output textures
  27. RWTexture2D<float2> LinearZ;
  28. RWTexture2D<float2> DS2x;
  29. RWTexture2DArray<float2> DS2xAtlas;
  30. RWTexture2D<float2> DS4x;
  31. RWTexture2DArray<float2> DS4xAtlas;
  32. // Input textures
  33. Texture2D<float4> Depth;
  34. // Shared memory
  35. groupshared float2 g_CacheW[256];
  36. #else
  37. // Output textures
  38. RWTexture2D<float> LinearZ;
  39. RWTexture2D<float> DS2x;
  40. RWTexture2DArray<float> DS2xAtlas;
  41. RWTexture2D<float> DS4x;
  42. RWTexture2DArray<float> DS4xAtlas;
  43. // Input textures
  44. Texture2D<float> Depth;
  45. // Shared memory
  46. groupshared float g_CacheW[256];
  47. #endif
  48. CBUFFER_START(CB0)
  49. float4 ZBufferParams;
  50. CBUFFER_END
  51. #ifdef MSAA
  52. float2 Linearize(uint2 st)
  53. {
  54. float depthMin = Depth[st].y;
  55. float depthMax = Depth[st].x;
  56. float2 depth = float2(depthMin, depthMax);
  57. float2 dist = 1.0 / (ZBufferParams.x * depth + ZBufferParams.y);
  58. #ifdef UNITY_REVERSED_Z
  59. if (depth.x == 0) dist.x = 1e5;
  60. if (depth.y == 0) dist.y = 1e5;
  61. #else
  62. if (depth.x == 1) dist.x = 1e5;
  63. if (depth.y == 1) dist.y = 1e5;
  64. #endif
  65. LinearZ[st] = dist;
  66. return dist;
  67. }
  68. #else
  69. float Linearize(uint2 st)
  70. {
  71. float depth = Depth[st];
  72. float dist = 1.0 / (ZBufferParams.x * depth + ZBufferParams.y);
  73. #ifdef UNITY_REVERSED_Z
  74. if (depth == 0) dist = 1e5;
  75. #else
  76. if (depth == 1) dist = 1e5;
  77. #endif
  78. LinearZ[st] = dist;
  79. return dist;
  80. }
  81. #endif
  82. #ifdef DISABLE_COMPUTE_SHADERS
  83. TRIVIAL_COMPUTE_KERNEL(main)
  84. #else
  85. [numthreads(8, 8, 1)]
  86. void main(uint3 Gid : SV_GroupID, uint GI : SV_GroupIndex, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID)
  87. {
  88. uint2 startST = Gid.xy << 4 | GTid.xy;
  89. uint destIdx = GTid.y << 4 | GTid.x;
  90. g_CacheW[destIdx + 0 ] = Linearize(startST | uint2(0, 0));
  91. g_CacheW[destIdx + 8 ] = Linearize(startST | uint2(8, 0));
  92. g_CacheW[destIdx + 128] = Linearize(startST | uint2(0, 8));
  93. g_CacheW[destIdx + 136] = Linearize(startST | uint2(8, 8));
  94. GroupMemoryBarrierWithGroupSync();
  95. uint ldsIndex = (GTid.x << 1) | (GTid.y << 5);
  96. #ifdef MSAA
  97. float2 w1 = g_CacheW[ldsIndex];
  98. #else
  99. float w1 = g_CacheW[ldsIndex];
  100. #endif
  101. uint2 st = DTid.xy;
  102. uint slice = ((st.x & 3) | (st.y << 2)) & 15;
  103. DS2x[st] = w1;
  104. DS2xAtlas[uint3(st >> 2, slice)] = w1;
  105. if ((GI & 011) == 0)
  106. {
  107. st = DTid.xy >> 1;
  108. slice = ((st.x & 3) | (st.y << 2)) & 15;
  109. DS4x[st] = w1;
  110. DS4xAtlas[uint3(st >> 2, slice)] = w1;
  111. }
  112. }
  113. #endif