LightGridCommon.bslinc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 gNumCells;
  15. uint3 gGridSize;
  16. uint gMaxNumLightsPerCell;
  17. uint2 gGridPixelSize;
  18. }
  19. float convertToNDCZ(float viewZ)
  20. {
  21. return -gNDCZToWorldZ.y + (gNDCZToWorldZ.x / viewZ);
  22. }
  23. float calcViewZFromCellZ(uint cellZ)
  24. {
  25. // TODO - Need better Z distribution. Currently I uniformly distribute in view space, but this
  26. // results in very elongated cells along Z
  27. float viewZ = gNearFar.x + (gNearFar.y - gNearFar.x) * cellZ / (float)gGridSize.z;
  28. return -viewZ;
  29. }
  30. uint calcCellZFromViewZ(float viewZ)
  31. {
  32. // TODO - Need better Z distribution. Currently I uniformly distribute in view space, but this
  33. // results in very elongated cells along Z
  34. uint cellZ = min((gGridSize.z * (-viewZ - gNearFar.x))/(gNearFar.y - gNearFar.x), gGridSize.z);
  35. return cellZ;
  36. }
  37. uint calcCellIdx(uint2 pixelPos, float deviceZ)
  38. {
  39. // Note: Use bitshift to divide since gGridPixelSize will be a power of 2
  40. uint2 cellXY = pixelPos / gGridPixelSize;
  41. uint cellZ = calcCellZFromViewZ(convertFromDeviceZ(deviceZ));
  42. uint cellIdx = (cellZ * gGridSize.y + cellXY.y) * gGridSize.x + cellXY.x;
  43. return cellIdx;
  44. }
  45. };
  46. };
  47. };
  48. Technique : base("LightGridCommon") =
  49. {
  50. Language = "GLSL";
  51. Pass =
  52. {
  53. Common =
  54. {
  55. layout(binding = 4, std140) uniform GridParams
  56. {
  57. // Offsets at which specific light types begin in gLights buffer
  58. // Assumed directional lights start at 0
  59. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  60. uvec3 gLightOffsets;
  61. uint gNumCells;
  62. uvec3 gGridSize;
  63. uint gMaxNumLightsPerCell;
  64. uvec2 gGridPixelSize;
  65. };
  66. float convertToNDCZ(float viewZ)
  67. {
  68. return -gNDCZToWorldZ.y + (gNDCZToWorldZ.x / viewZ);
  69. }
  70. float calcViewZFromCellZ(uint cellZ)
  71. {
  72. // TODO - Need better Z distribution. Currently I uniformly distribute in view space, but this
  73. // results in very elongated cells along Z
  74. float viewZ = gNearFar.x + (gNearFar.y - gNearFar.x) * cellZ / float(gGridSize.z);
  75. return -viewZ;
  76. }
  77. uint calcCellZFromViewZ(float viewZ)
  78. {
  79. // TODO - Need better Z distribution. Currently I uniformly distribute in view space, but this
  80. // results in very elongated cells along Z
  81. uint cellZ = min(uint((gGridSize.z * (-viewZ - gNearFar.x))/(gNearFar.y - gNearFar.x)), gGridSize.z);
  82. return cellZ;
  83. }
  84. int calcCellIdx(uvec2 pixelPos, float deviceZ)
  85. {
  86. // OpenGL uses lower left for window space origin, we use upper-left
  87. #ifdef OPENGL
  88. pixelPos.y = gViewportRectangle.w - pixelPos.y;
  89. #endif
  90. // Note: Use bitshift to divide since gGridPixelSize will be a power of 2
  91. uvec2 cellXY = pixelPos / gGridPixelSize;
  92. uint cellZ = calcCellZFromViewZ(convertFromDeviceZ(deviceZ));
  93. int cellIdx = int((cellZ * gGridSize.y + cellXY.y) * gGridSize.x + cellXY.x);
  94. return cellIdx;
  95. }
  96. };
  97. };
  98. };