BlurCS.hlsl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: groupId
  3. // CHECK: threadIdInGroup
  4. // CHECK: threadId
  5. // CHECK: barrier
  6. // CHECK: addrspace(3)
  7. //
  8. // Copyright (c) Microsoft. All rights reserved.
  9. // This code is licensed under the MIT License (MIT).
  10. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  11. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  12. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  13. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  14. //
  15. // Developed by Minigraph
  16. //
  17. // Author: James Stanard
  18. //
  19. // The CS for guassian blurring a single RGB buffer.
  20. //
  21. // For the intended bloom blurring algorithm, this shader is expected to be used only on
  22. // the lowest resolution bloom buffer before starting the series of upsample-and-blur
  23. // passes.
  24. #include "PostEffectsRS.hlsli"
  25. Texture2D<float3> InputBuf : register( t0 );
  26. RWTexture2D<float3> Result : register( u0 );
  27. cbuffer cb0 : register(b0)
  28. {
  29. float2 g_inverseDimensions;
  30. }
  31. // The guassian blur weights (derived from Pascal's triangle)
  32. static const float Weights[5] = { 70.0f / 256.0f, 56.0f / 256.0f, 28.0f / 256.0f, 8.0f / 256.0f, 1.0f / 256.0f };
  33. float3 BlurPixels( float3 a, float3 b, float3 c, float3 d, float3 e, float3 f, float3 g, float3 h, float3 i )
  34. {
  35. return Weights[0]*e + Weights[1]*(d+f) + Weights[2]*(c+g) + Weights[3]*(b+h) + Weights[4]*(a+i);
  36. }
  37. // 16x16 pixels with an 8x8 center that we will be blurring writing out. Each uint is two color channels packed together
  38. groupshared uint CacheR[128];
  39. groupshared uint CacheG[128];
  40. groupshared uint CacheB[128];
  41. void Store2Pixels( uint index, float3 pixel1, float3 pixel2 )
  42. {
  43. CacheR[index] = f32tof16(pixel1.r) | f32tof16(pixel2.r) << 16;
  44. CacheG[index] = f32tof16(pixel1.g) | f32tof16(pixel2.g) << 16;
  45. CacheB[index] = f32tof16(pixel1.b) | f32tof16(pixel2.b) << 16;
  46. }
  47. void Load2Pixels( uint index, out float3 pixel1, out float3 pixel2 )
  48. {
  49. uint rr = CacheR[index];
  50. uint gg = CacheG[index];
  51. uint bb = CacheB[index];
  52. pixel1 = float3( f16tof32(rr ), f16tof32(gg ), f16tof32(bb ) );
  53. pixel2 = float3( f16tof32(rr >> 16), f16tof32(gg >> 16), f16tof32(bb >> 16) );
  54. }
  55. void Store1Pixel( uint index, float3 pixel )
  56. {
  57. CacheR[index] = asuint(pixel.r);
  58. CacheG[index] = asuint(pixel.g);
  59. CacheB[index] = asuint(pixel.b);
  60. }
  61. void Load1Pixel( uint index, out float3 pixel )
  62. {
  63. pixel = asfloat( uint3(CacheR[index], CacheG[index], CacheB[index]) );
  64. }
  65. // Blur two pixels horizontally. This reduces LDS reads and pixel unpacking.
  66. void BlurHorizontally( uint outIndex, uint leftMostIndex )
  67. {
  68. float3 s0, s1, s2, s3, s4, s5, s6, s7, s8, s9;
  69. Load2Pixels( leftMostIndex + 0, s0, s1 );
  70. Load2Pixels( leftMostIndex + 1, s2, s3 );
  71. Load2Pixels( leftMostIndex + 2, s4, s5 );
  72. Load2Pixels( leftMostIndex + 3, s6, s7 );
  73. Load2Pixels( leftMostIndex + 4, s8, s9 );
  74. Store1Pixel(outIndex , BlurPixels(s0, s1, s2, s3, s4, s5, s6, s7, s8));
  75. Store1Pixel(outIndex+1, BlurPixels(s1, s2, s3, s4, s5, s6, s7, s8, s9));
  76. }
  77. void BlurVertically( uint2 pixelCoord, uint topMostIndex )
  78. {
  79. float3 s0, s1, s2, s3, s4, s5, s6, s7, s8;
  80. Load1Pixel( topMostIndex , s0 );
  81. Load1Pixel( topMostIndex+ 8, s1 );
  82. Load1Pixel( topMostIndex+16, s2 );
  83. Load1Pixel( topMostIndex+24, s3 );
  84. Load1Pixel( topMostIndex+32, s4 );
  85. Load1Pixel( topMostIndex+40, s5 );
  86. Load1Pixel( topMostIndex+48, s6 );
  87. Load1Pixel( topMostIndex+56, s7 );
  88. Load1Pixel( topMostIndex+64, s8 );
  89. Result[pixelCoord] = BlurPixels(s0, s1, s2, s3, s4, s5, s6, s7, s8);
  90. }
  91. [RootSignature(PostEffects_RootSig)]
  92. [numthreads( 8, 8, 1 )]
  93. void main( uint3 Gid : SV_GroupID, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID )
  94. {
  95. //
  96. // Load 4 pixels per thread into LDS
  97. //
  98. int2 GroupUL = (Gid.xy << 3) - 4; // Upper-left pixel coordinate of group read location
  99. int2 ThreadUL = (GTid.xy << 1) + GroupUL; // Upper-left pixel coordinate of quad that this thread will read
  100. //
  101. // Store 4 unblurred pixels in LDS
  102. //
  103. int destIdx = GTid.x + (GTid.y << 4);
  104. Store2Pixels(destIdx+0, InputBuf[ThreadUL + uint2(0, 0)], InputBuf[ThreadUL + uint2(1, 0)]);
  105. Store2Pixels(destIdx+8, InputBuf[ThreadUL + uint2(0, 1)], InputBuf[ThreadUL + uint2(1, 1)]);
  106. GroupMemoryBarrierWithGroupSync();
  107. //
  108. // Horizontally blur the pixels in Cache
  109. //
  110. uint row = GTid.y << 4;
  111. BlurHorizontally(row + (GTid.x << 1), row + GTid.x + (GTid.x & 4));
  112. GroupMemoryBarrierWithGroupSync();
  113. //
  114. // Vertically blur the pixels and write the result to memory
  115. //
  116. BlurVertically(DTid.xy, (GTid.y << 3) + GTid.x);
  117. }