LightGridLLCreation.bsl 8.0 KB

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