LightGridLLCreation.bsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "$ENGINE$\PerCameraData.bslinc"
  2. #define USE_LIGHT_GRID_INDICES
  3. #include "$ENGINE$\LightingCommon.bslinc"
  4. #include "$ENGINE$\LightGridCommon.bslinc"
  5. Blocks =
  6. {
  7. Block PerCamera : auto("PerCamera");
  8. Block GridParams : auto("GridParams");
  9. };
  10. Technique
  11. : inherits("PerCameraData")
  12. : inherits("LightingCommon")
  13. : inherits("LightGridCommon") =
  14. {
  15. Language = "HLSL11";
  16. Pass =
  17. {
  18. Compute =
  19. {
  20. RWBuffer<uint> gLinkedListCounter : register(u0);
  21. RWBuffer<uint> gLinkedListHeads : register(u1);
  22. RWBuffer<uint4> gLinkedList : register(u2);
  23. // Generates a an axis aligned bounding box in NDC and transforms it to view space.
  24. // Note: This will overlap other cells, so it might be better to use frustum planes
  25. // instead of AABB, although frustum testing procedure could result in more false positive
  26. void calcCellAABB(uint3 cellIdx, out float3 center, out float3 extent)
  27. {
  28. // Convert grid XY coordinates to clip coordinates
  29. float2 a = 2.0f / gGridSize.xy;
  30. float3 ndcMin;
  31. float3 ndcMax;
  32. ndcMin.xy = cellIdx.xy * a - float2(1.0f, 1.0f);
  33. ndcMax.xy = (cellIdx.xy + 1) * a - float2(1.0f, 1.0f);
  34. // Flip Y depending on render API, depending if Y in NDC is facing up or down
  35. // (We negate the value because we want NDC with Y flipped, so origin is top left)
  36. float flipY = -sign(gMatProj[1][1]);
  37. ndcMin.y *= flipY;
  38. ndcMax.y *= flipY;
  39. // Because we're viewing along negative Z, farther end is the minimum
  40. float viewZMin = calcViewZFromCellZ(cellIdx.z + 1);
  41. float viewZMax = calcViewZFromCellZ(cellIdx.z);
  42. ndcMin.z = convertToNDCZ(viewZMax);
  43. ndcMax.z = convertToNDCZ(viewZMin);
  44. float4 corner[8];
  45. // Near
  46. corner[0] = mul(gMatInvProj, float4(ndcMin.x, ndcMin.y, ndcMin.z, 1.0f));
  47. corner[1] = mul(gMatInvProj, float4(ndcMax.x, ndcMin.y, ndcMin.z, 1.0f));
  48. corner[2] = mul(gMatInvProj, float4(ndcMax.x, ndcMax.y, ndcMin.z, 1.0f));
  49. corner[3] = mul(gMatInvProj, float4(ndcMin.x, ndcMax.y, ndcMin.z, 1.0f));
  50. // Far
  51. corner[4] = mul(gMatInvProj, float4(ndcMin.x, ndcMin.y, ndcMax.z, 1.0f));
  52. corner[5] = mul(gMatInvProj, float4(ndcMax.x, ndcMin.y, ndcMax.z, 1.0f));
  53. corner[6] = mul(gMatInvProj, float4(ndcMax.x, ndcMax.y, ndcMax.z, 1.0f));
  54. corner[7] = mul(gMatInvProj, float4(ndcMin.x, ndcMax.y, ndcMax.z, 1.0f));
  55. [unroll]
  56. for(uint i = 0; i < 8; ++i)
  57. corner[i].xy /= corner[i].w;
  58. float3 viewMin = float3(corner[0].xy, viewZMin);
  59. float3 viewMax = float3(corner[0].xy, viewZMax);
  60. [unroll]
  61. for(uint i = 1; i < 8; ++i)
  62. {
  63. viewMin.xy = min(viewMin.xy, corner[i].xy);
  64. viewMax.xy = max(viewMax.xy, corner[i].xy);
  65. }
  66. extent = (viewMax - viewMin) * 0.5f;
  67. center = viewMin + extent;
  68. }
  69. [numthreads(THREADGROUP_SIZE, THREADGROUP_SIZE, THREADGROUP_SIZE)]
  70. void main(
  71. uint3 groupId : SV_GroupID,
  72. uint3 groupThreadId : SV_GroupThreadID,
  73. uint3 dispatchThreadId : SV_DispatchThreadID)
  74. {
  75. // Ignore pixels out of valid range
  76. if (any(dispatchThreadId.xy >= gGridSize.xy))
  77. return;
  78. uint maxNumLinks = gNumCells * gMaxNumLightsPerCell;
  79. uint cellIdx = (dispatchThreadId.z * gGridSize.y + dispatchThreadId.y) * gGridSize.x + dispatchThreadId.x;
  80. float3 cellCenter;
  81. float3 cellExtent;
  82. calcCellAABB(dispatchThreadId, cellCenter, cellExtent);
  83. for(uint type = 1; type < 3; ++type)
  84. {
  85. uint lightOffset = gLightOffsets[type - 1];
  86. uint lightEnd = gLightOffsets[type];
  87. for(uint i = lightOffset; i < lightEnd; ++i)
  88. {
  89. float4 lightPosition = mul(gMatView, float4(gLights[i].position, 1.0f));
  90. float lightRadius = gLights[i].attRadius;
  91. // Calculate distance from box to light
  92. float3 distances = max(abs(lightPosition - cellCenter) - cellExtent, 0);
  93. float distSqrd = dot(distances, distances);
  94. if(distSqrd <= (lightRadius * lightRadius))
  95. {
  96. uint nextLink;
  97. InterlockedAdd(gLinkedListCounter[0], 1U, nextLink);
  98. if(nextLink < maxNumLinks)
  99. {
  100. uint prevLink;
  101. InterlockedExchange(gLinkedListHeads[cellIdx], nextLink, prevLink);
  102. gLinkedList[nextLink] = uint4(i, type, prevLink, 0);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. };
  109. };
  110. };
  111. Technique
  112. : inherits("PerCameraData")
  113. : inherits("LightingCommon")
  114. : inherits("LightGridCommon") =
  115. {
  116. Language = "GLSL";
  117. Pass =
  118. {
  119. Compute =
  120. {
  121. layout (local_size_x = THREADGROUP_SIZE, local_size_y = THREADGROUP_SIZE, local_size_z = THREADGROUP_SIZE) in;
  122. layout(std430, binding = 1) readonly buffer gLights
  123. {
  124. LightData[] gLightsData;
  125. };
  126. layout(binding = 2, r32ui) uniform uimageBuffer gLinkedListCounter;
  127. layout(binding = 3, r32ui) uniform uimageBuffer gLinkedListHeads;
  128. layout(binding = 5, rgba32ui) uniform uimageBuffer gLinkedList;
  129. void calcCellAABB(uvec3 cellIdx, out vec3 center, out vec3 extent)
  130. {
  131. // Convert grid XY coordinates to clip coordinates
  132. vec2 a = 2.0f / gGridSize.xy;
  133. vec3 ndcMin;
  134. vec3 ndcMax;
  135. ndcMin.xy = cellIdx.xy * a - vec2(1.0f, 1.0f);
  136. ndcMax.xy = (cellIdx.xy + 1) * a - vec2(1.0f, 1.0f);
  137. // Flip Y depending on render API, depending if Y in NDC is facing up or down
  138. // (We negate the value because we want NDC with Y flipped, so origin is top left)
  139. float flipY = -sign(gMatProj[1][1]);
  140. ndcMin.y *= flipY;
  141. ndcMax.y *= flipY;
  142. // Because we're viewing along negative Z, farther end is the minimum
  143. float viewZMin = calcViewZFromCellZ(cellIdx.z + 1);
  144. float viewZMax = calcViewZFromCellZ(cellIdx.z);
  145. ndcMin.z = convertToNDCZ(viewZMax);
  146. ndcMax.z = convertToNDCZ(viewZMin);
  147. vec4 corner[8];
  148. // Near
  149. corner[0] = gMatInvProj * vec4(ndcMin.x, ndcMin.y, ndcMin.z, 1.0f);
  150. corner[1] = gMatInvProj * vec4(ndcMax.x, ndcMin.y, ndcMin.z, 1.0f);
  151. corner[2] = gMatInvProj * vec4(ndcMax.x, ndcMax.y, ndcMin.z, 1.0f);
  152. corner[3] = gMatInvProj * vec4(ndcMin.x, ndcMax.y, ndcMin.z, 1.0f);
  153. // Far
  154. corner[4] = gMatInvProj * vec4(ndcMin.x, ndcMin.y, ndcMax.z, 1.0f);
  155. corner[5] = gMatInvProj * vec4(ndcMax.x, ndcMin.y, ndcMax.z, 1.0f);
  156. corner[6] = gMatInvProj * vec4(ndcMax.x, ndcMax.y, ndcMax.z, 1.0f);
  157. corner[7] = gMatInvProj * vec4(ndcMin.x, ndcMax.y, ndcMax.z, 1.0f);
  158. for(uint i = 0; i < 8; ++i)
  159. corner[i].xy /= corner[i].w;
  160. vec3 viewMin = vec3(corner[0].xy, viewZMin);
  161. vec3 viewMax = vec3(corner[0].xy, viewZMax);
  162. for(uint i = 1; i < 8; ++i)
  163. {
  164. viewMin.xy = min(viewMin.xy, corner[i].xy);
  165. viewMax.xy = max(viewMax.xy, corner[i].xy);
  166. }
  167. extent = (viewMax - viewMin) * 0.5f;
  168. center = viewMin + extent;
  169. }
  170. void main()
  171. {
  172. // Ignore pixels out of valid range
  173. if (any(greaterThanEqual(gl_GlobalInvocationID.xy, gGridSize.xy)))
  174. return;
  175. uint maxNumLinks = gNumCells * gMaxNumLightsPerCell;
  176. uint cellIdx = (gl_GlobalInvocationID.z * gGridSize.y + gl_GlobalInvocationID.y) * gGridSize.x + gl_GlobalInvocationID.x;
  177. vec3 cellCenter;
  178. vec3 cellExtent;
  179. calcCellAABB(gl_GlobalInvocationID, cellCenter, cellExtent);
  180. for(uint type = 1; type < 3; ++type)
  181. {
  182. uint lightOffset = gLightOffsets[type - 1];
  183. uint lightEnd = gLightOffsets[type];
  184. for(uint i = lightOffset; i < lightEnd; ++i)
  185. {
  186. vec4 lightPosition = gMatView * vec4(gLightsData[i].position, 1.0f);
  187. float lightRadius = gLightsData[i].attRadius;
  188. // Calculate distance from box to light
  189. vec3 distances = max(abs(lightPosition.xyz - cellCenter) - cellExtent, 0);
  190. float distSqrd = dot(distances, distances);
  191. if(distSqrd <= (lightRadius * lightRadius))
  192. {
  193. uint nextLink = imageAtomicAdd(gLinkedListCounter, 0, 1U);
  194. if(nextLink < maxNumLinks)
  195. {
  196. uint prevLink = imageAtomicExchange(gLinkedListHeads, int(cellIdx), nextLink);
  197. imageStore(gLinkedList, int(nextLink), uvec4(i, type, prevLink, 0));
  198. }
  199. }
  200. }
  201. }
  202. }
  203. };
  204. };
  205. };