LightGridCommon.bslinc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. mixin LightGridCommon
  2. {
  3. code
  4. {
  5. cbuffer GridParams : register(b4)
  6. {
  7. // Offsets at which specific light types begin in gLights buffer
  8. // Assumed directional lights start at 0
  9. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  10. uint3 gLightOffsets;
  11. uint gNumReflProbes;
  12. uint gNumCells;
  13. uint3 gGridSize;
  14. uint gMaxNumLightsPerCell;
  15. uint2 gGridPixelSize;
  16. }
  17. float calcViewZFromCellZ(uint cellZ)
  18. {
  19. // We don't want to subdivide depth uniformly because XY sizes will be much
  20. // smaller closer to the near plane, and larger towards far plane. We want
  21. // our cells to be as close to cube shape as possible, so that width/height/depth
  22. // are all similar. Ideally we would use either width or height as calculated for
  23. // purposes of the projection matrix, for the depth. But since we'll be splitting
  24. // the depth range into multiple slices, in practice this ends up with many tiny
  25. // cells close to the near plane. Instead we use a square function, which is
  26. // somewhere between the two extremes:
  27. // view = slice^2
  28. // We need it in range [near, far] so we normalize and scale
  29. // view = slice^2 / maxSlices^2 * (far - near) + near
  30. // Note: Some of these calculations could be moved to CPU
  31. float viewZ = (pow(cellZ, 2) / pow(gGridSize.z, 2)) * (gNearFar.y - gNearFar.x) + gNearFar.x;
  32. return -viewZ;
  33. }
  34. uint calcCellZFromViewZ(float viewZ)
  35. {
  36. // Inverse of calculation in calcViewZFromCellZ
  37. uint cellZ = min((uint)floor(sqrt(((-viewZ - gNearFar.x)*pow(gGridSize.z, 2))/(gNearFar.y - gNearFar.x))), gGridSize.z);
  38. return cellZ;
  39. }
  40. uint calcCellIdx(uint2 pixelPos, float deviceZ)
  41. {
  42. // OpenGL uses lower left for window space origin
  43. #ifdef OPENGL
  44. pixelPos.y = gViewportRectangle.w - pixelPos.y;
  45. #endif
  46. // Note: Use bitshift to divide since gGridPixelSize will be a power of 2
  47. uint2 cellXY = pixelPos / gGridPixelSize;
  48. uint cellZ = calcCellZFromViewZ(convertFromDeviceZ(deviceZ));
  49. uint cellIdx = (cellZ * gGridSize.y + cellXY.y) * gGridSize.x + cellXY.x;
  50. return cellIdx;
  51. }
  52. };
  53. };