LightGridCommon.bslinc 2.1 KB

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