IrradianceReduceSH.bsl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  2. #include "$ENGINE$\SHCommon.bslinc"
  3. shader IrradianceReduceSH
  4. {
  5. mixin ReflectionCubemapCommon;
  6. mixin SHCommon;
  7. featureset = HighEnd;
  8. variations
  9. {
  10. SH_ORDER = { 3, 5 };
  11. };
  12. code
  13. {
  14. #define PI 3.1415926
  15. struct SHCoeffsAndWeight
  16. {
  17. SHVectorRGB coeffs;
  18. float weight;
  19. };
  20. StructuredBuffer<SHCoeffsAndWeight> gInput;
  21. RWTexture2D<float4> gOutput;
  22. [internal]
  23. cbuffer Params
  24. {
  25. uint2 gOutputIdx;
  26. uint gNumEntries;
  27. }
  28. [numthreads(1, 1, 1)]
  29. void csmain(
  30. uint groupIdx : SV_GroupIndex,
  31. uint groupId : SV_GroupID,
  32. uint3 dispatchThreadId : SV_DispatchThreadID)
  33. {
  34. SHVectorRGB coeffs;
  35. float weight = 0;
  36. SHZero(coeffs.R);
  37. SHZero(coeffs.G);
  38. SHZero(coeffs.B);
  39. // Note: There shouldn't be many entries, so we add them all in one thread. Otherwise we should do parallel reduction.
  40. for(uint i = 0; i < gNumEntries; i++)
  41. {
  42. SHCoeffsAndWeight current = gInput[i];
  43. SHAdd(coeffs.R, current.coeffs.R);
  44. SHAdd(coeffs.G, current.coeffs.G);
  45. SHAdd(coeffs.B, current.coeffs.B);
  46. weight += current.weight;
  47. }
  48. // Normalize
  49. float normFactor = (4 * PI) / weight;
  50. SHMultiply(coeffs.R, normFactor);
  51. SHMultiply(coeffs.G, normFactor);
  52. SHMultiply(coeffs.B, normFactor);
  53. uint2 writeIdx = gOutputIdx;
  54. [unroll]
  55. for(int i = 0; i < SH_NUM_COEFFS; ++i)
  56. {
  57. gOutput[writeIdx] = float4(coeffs.R.v[i], coeffs.G.v[i], coeffs.B.v[i], 0.0f);
  58. writeIdx.x += 1;
  59. }
  60. }
  61. };
  62. };