LightGridLLCreation.bsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "$ENGINE$\PerCameraData.bslinc"
  2. #define USE_LIGHT_GRID_INDICES 1
  3. #include "$ENGINE$\LightingCommon.bslinc"
  4. #include "$ENGINE$\ImageBasedLighting.bslinc"
  5. #include "$ENGINE$\LightGridCommon.bslinc"
  6. technique LightGridLLCreation
  7. {
  8. mixin PerCameraData;
  9. mixin LightingCommon;
  10. mixin LightGridCommon;
  11. mixin ImageBasedLighting;
  12. featureset = HighEnd;
  13. code
  14. {
  15. StructuredBuffer<LightData> gLights;
  16. StructuredBuffer<ReflProbeData> gReflectionProbes;
  17. [layout(r32ui)]
  18. RWBuffer<uint> gLightsCounter;
  19. [layout(r32ui)]
  20. RWBuffer<uint> gLightsLLHeads;
  21. RWBuffer<uint4> gLightsLL;
  22. [layout(r32ui)]
  23. RWBuffer<uint> gProbesCounter;
  24. [layout(r32ui)]
  25. RWBuffer<uint> gProbesLLHeads;
  26. RWBuffer<uint2> gProbesLL;
  27. // Generates a an axis aligned bounding box in NDC and transforms it to view space.
  28. // Note: This will overlap other cells, so it might be better to use frustum planes
  29. // instead of AABB, although frustum testing procedure could result in more false positive
  30. void calcCellAABB(uint3 cellIdx, out float3 center, out float3 extent)
  31. {
  32. // Note:: AABB calculation in tiled deferred image based lighting shader uses less instructions than this,
  33. // see if it can be applied here.
  34. // Convert grid XY coordinates to clip coordinates
  35. float2 a = 2.0f / gGridSize.xy;
  36. float3 ndcMin;
  37. float3 ndcMax;
  38. ndcMin.xy = cellIdx.xy * a - float2(1.0f, 1.0f);
  39. ndcMax.xy = (cellIdx.xy + 1) * a - float2(1.0f, 1.0f);
  40. // Flip Y depending on render API, depending if Y in NDC is facing up or down
  41. // (We negate the value because we want NDC with Y flipped, so origin is top left)
  42. float flipY = -sign(gMatProj[1][1]);
  43. ndcMin.y *= flipY;
  44. ndcMax.y *= flipY;
  45. // Because we're viewing along negative Z, farther end is the minimum
  46. float viewZMin = calcViewZFromCellZ(cellIdx.z + 1);
  47. float viewZMax = calcViewZFromCellZ(cellIdx.z);
  48. ndcMin.z = convertToNDCZ(viewZMax);
  49. ndcMax.z = convertToNDCZ(viewZMin);
  50. float4 corner[8];
  51. // Near
  52. corner[0] = mul(gMatInvProj, float4(ndcMin.x, ndcMin.y, ndcMin.z, 1.0f));
  53. corner[1] = mul(gMatInvProj, float4(ndcMax.x, ndcMin.y, ndcMin.z, 1.0f));
  54. corner[2] = mul(gMatInvProj, float4(ndcMax.x, ndcMax.y, ndcMin.z, 1.0f));
  55. corner[3] = mul(gMatInvProj, float4(ndcMin.x, ndcMax.y, ndcMin.z, 1.0f));
  56. // Far
  57. corner[4] = mul(gMatInvProj, float4(ndcMin.x, ndcMin.y, ndcMax.z, 1.0f));
  58. corner[5] = mul(gMatInvProj, float4(ndcMax.x, ndcMin.y, ndcMax.z, 1.0f));
  59. corner[6] = mul(gMatInvProj, float4(ndcMax.x, ndcMax.y, ndcMax.z, 1.0f));
  60. corner[7] = mul(gMatInvProj, float4(ndcMin.x, ndcMax.y, ndcMax.z, 1.0f));
  61. [unroll]
  62. for(uint i = 0; i < 8; ++i)
  63. corner[i].xy /= corner[i].w;
  64. float3 viewMin = float3(corner[0].xy, viewZMin);
  65. float3 viewMax = float3(corner[0].xy, viewZMax);
  66. [unroll]
  67. for(uint i = 1; i < 8; ++i)
  68. {
  69. viewMin.xy = min(viewMin.xy, corner[i].xy);
  70. viewMax.xy = max(viewMax.xy, corner[i].xy);
  71. }
  72. extent = (viewMax - viewMin) * 0.5f;
  73. center = viewMin + extent;
  74. }
  75. [numthreads(THREADGROUP_SIZE, THREADGROUP_SIZE, THREADGROUP_SIZE)]
  76. void csmain(
  77. uint3 groupId : SV_GroupID,
  78. uint3 groupThreadId : SV_GroupThreadID,
  79. uint3 dispatchThreadId : SV_DispatchThreadID)
  80. {
  81. // Ignore pixels out of valid range
  82. if (any(dispatchThreadId.xy >= gGridSize.xy))
  83. return;
  84. uint maxNumLinks = gNumCells * gMaxNumLightsPerCell;
  85. uint cellIdx = (dispatchThreadId.z * gGridSize.y + dispatchThreadId.y) * gGridSize.x + dispatchThreadId.x;
  86. float3 cellCenter;
  87. float3 cellExtent;
  88. calcCellAABB(dispatchThreadId, cellCenter, cellExtent);
  89. for(uint type = 1; type < 3; ++type)
  90. {
  91. uint lightsStart = gLightStrides[type - 1];
  92. uint lightsEnd = lightsStart + gLightCounts[type];
  93. for(uint i = lightsStart; i < lightsEnd; ++i)
  94. {
  95. float4 lightPosition = mul(gMatView, float4(gLights[i].position, 1.0f));
  96. float lightRadius = gLights[i].attRadius;
  97. // Calculate distance from box to light
  98. float3 distances = max(abs(lightPosition - cellCenter) - cellExtent, 0);
  99. float distSqrd = dot(distances, distances);
  100. if(distSqrd <= (lightRadius * lightRadius))
  101. {
  102. uint nextLink;
  103. InterlockedAdd(gLightsCounter[0], 1U, nextLink);
  104. if(nextLink < maxNumLinks)
  105. {
  106. uint prevLink;
  107. InterlockedExchange(gLightsLLHeads[cellIdx], nextLink, prevLink);
  108. gLightsLL[nextLink] = uint4(i, type, prevLink, 0);
  109. }
  110. }
  111. }
  112. }
  113. for(uint i = 0; i < gNumReflProbes; ++i)
  114. {
  115. float4 probePosition = mul(gMatView, float4(gReflectionProbes[i].position, 1.0f));
  116. float probeRadius = gReflectionProbes[i].radius;
  117. // Calculate distance from box to light
  118. float3 distances = max(abs(probePosition - cellCenter) - cellExtent, 0);
  119. float distSqrd = dot(distances, distances);
  120. if(distSqrd <= (probeRadius * probeRadius))
  121. {
  122. uint nextLink;
  123. InterlockedAdd(gProbesCounter[0], 1U, nextLink);
  124. if(nextLink < maxNumLinks)
  125. {
  126. uint prevLink;
  127. InterlockedExchange(gProbesLLHeads[cellIdx], nextLink, prevLink);
  128. gProbesLL[nextLink] = uint2(i, prevLink);
  129. }
  130. }
  131. }
  132. }
  133. };
  134. };