IrradianceReduceSH.bsl 1.3 KB

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