LightGridCommon.bslinc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Technique : base("LightGridCommon") =
  2. {
  3. Language = "HLSL11";
  4. Pass =
  5. {
  6. Common =
  7. {
  8. cbuffer GridParams : register(b4)
  9. {
  10. // Offsets at which specific light types begin in gLights buffer
  11. // Assumed directional lights start at 0
  12. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  13. uint3 gLightOffsets;
  14. uint gNumReflProbes;
  15. uint gNumCells;
  16. uint3 gGridSize;
  17. uint gMaxNumLightsPerCell;
  18. uint2 gGridPixelSize;
  19. }
  20. float calcViewZFromCellZ(uint cellZ)
  21. {
  22. // We don't want to subdivide depth uniformly because XY sizes will be much
  23. // smaller closer to the near plane, and larger towards far plane. We want
  24. // our cells to be as close to cube shape as possible, so that width/height/depth
  25. // are all similar. Ideally we would use either width or height as calculated for
  26. // purposes of the projection matrix, for the depth. But since we'll be splitting
  27. // the depth range into multiple slices, in practice this ends up with many tiny
  28. // cells close to the near plane. Instead we use a square function, which is
  29. // somewhere between the two extremes:
  30. // view = slice^2
  31. // We need it in range [near, far] so we normalize and scale
  32. // view = slice^2 / maxSlices^2 * (far - near) + near
  33. // Note: Some of these calculations could be moved to CPU
  34. float viewZ = (pow(cellZ, 2) / pow(gGridSize.z, 2)) * (gNearFar.y - gNearFar.x) + gNearFar.x;
  35. return -viewZ;
  36. }
  37. uint calcCellZFromViewZ(float viewZ)
  38. {
  39. // Inverse of calculation in calcViewZFromCellZ
  40. uint cellZ = min((uint)floor(sqrt(((-viewZ - gNearFar.x)*pow(gGridSize.z, 2))/(gNearFar.y - gNearFar.x))), gGridSize.z);
  41. return cellZ;
  42. }
  43. uint calcCellIdx(uint2 pixelPos, float deviceZ)
  44. {
  45. // Note: Use bitshift to divide since gGridPixelSize will be a power of 2
  46. uint2 cellXY = pixelPos / gGridPixelSize;
  47. uint cellZ = calcCellZFromViewZ(convertFromDeviceZ(deviceZ));
  48. uint cellIdx = (cellZ * gGridSize.y + cellXY.y) * gGridSize.x + cellXY.x;
  49. return cellIdx;
  50. }
  51. };
  52. };
  53. };
  54. Technique : base("LightGridCommon") =
  55. {
  56. Language = "GLSL";
  57. Pass =
  58. {
  59. Common =
  60. {
  61. layout(binding = 4, std140) uniform GridParams
  62. {
  63. // Offsets at which specific light types begin in gLights buffer
  64. // Assumed directional lights start at 0
  65. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  66. uvec3 gLightOffsets;
  67. uint gNumCells;
  68. uvec3 gGridSize;
  69. uint gMaxNumLightsPerCell;
  70. uvec2 gGridPixelSize;
  71. };
  72. float convertToNDCZ(float viewZ)
  73. {
  74. return -gNDCZToWorldZ.y + (gNDCZToWorldZ.x / viewZ);
  75. }
  76. float calcViewZFromCellZ(uint cellZ)
  77. {
  78. // See HLSL version for reasons behind this formulation
  79. // Note: Some of these calculations could be moved to CPU
  80. float viewZ = (pow(cellZ, 2) / pow(gGridSize.z, 2)) * (gNearFar.y - gNearFar.x) + gNearFar.x;
  81. return -viewZ;
  82. }
  83. uint calcCellZFromViewZ(float viewZ)
  84. {
  85. // Inverse of calculation in calcViewZFromCellZ
  86. uint cellZ = min(uint(floor(sqrt(((-viewZ - gNearFar.x)*pow(gGridSize.z, 2))/(gNearFar.y - gNearFar.x)))), gGridSize.z);
  87. return cellZ;
  88. }
  89. int calcCellIdx(uvec2 pixelPos, float deviceZ)
  90. {
  91. // OpenGL uses lower left for window space origin, we use upper-left
  92. #ifdef OPENGL
  93. pixelPos.y = gViewportRectangle.w - pixelPos.y;
  94. #endif
  95. // Note: Use bitshift to divide since gGridPixelSize will be a power of 2
  96. uvec2 cellXY = pixelPos / gGridPixelSize;
  97. uint cellZ = calcCellZFromViewZ(convertFromDeviceZ(deviceZ));
  98. int cellIdx = int((cellZ * gGridSize.y + cellXY.y) * gGridSize.x + cellXY.x);
  99. return cellIdx;
  100. }
  101. };
  102. };
  103. };