GpuVisibilityLocalLights.ankiprog 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Terminology:
  6. // - Grid: The volume we are looking to gather lights for
  7. // - Cell: The grid is dividied in cells
  8. // - Light index list: An array of indices that point the GPU scene lights. Each cell points to a part of this list
  9. #pragma anki technique Setup comp
  10. #pragma anki technique Count comp
  11. #pragma anki technique PrefixSum comp
  12. #pragma anki technique Fill comp
  13. #include <AnKi/Shaders/Functions.hlsl>
  14. #include <AnKi/Shaders/Include/GpuSceneTypes.h>
  15. #include <AnKi/Shaders/Include/GpuVisibilityTypes.h>
  16. #include <AnKi/Shaders/VisibilityAndCollisionFunctions.hlsl>
  17. constexpr U32 kPrefixSumThreadCount = 1024; // Common for most GPUs
  18. constexpr U32 kPrefixSumElementCountPerThreadgroup =
  19. kPrefixSumThreadCount * 2; // Now many elements a single threadgroup can calculate their prfix sum
  20. Bool insideFrustum(Vec4 planes[5], Vec3 aabbMin, Vec3 aabbMax)
  21. {
  22. [unroll] for(U32 i = 0; i < 5; ++i)
  23. {
  24. if(testPlaneAabb(planes[i].xyz, planes[i].w, aabbMin, aabbMax) < 0.0)
  25. {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. template<typename TFunc, typename TFunc2>
  32. void lightVsCellVisibility(StructuredBuffer<GpuSceneLight> lights, U32 cellIdx, GpuVisibilityLocalLightsConsts consts,
  33. RWStructuredBuffer<U32> lightIndexCount, Bool detailedTests, TFunc binLightToCellFunc, TFunc2 informLightIndexCountFunc)
  34. {
  35. if(cellIdx >= consts.m_cellCount)
  36. {
  37. return;
  38. }
  39. UVec3 cellId;
  40. unflatten3dArrayIndex(consts.m_cellCounts.z, consts.m_cellCounts.y, consts.m_cellCounts.x, cellIdx, cellId.z, cellId.y, cellId.x);
  41. const Vec3 cellMin = cellId * consts.m_cellSize + consts.m_gridVolumeMin;
  42. const Vec3 cellMax = cellMin + consts.m_cellSize;
  43. U32 visibleLightCount = 0;
  44. const U32 lightCount = getStructuredBufferElementCount(lights);
  45. for(U32 i = 0; i < lightCount; ++i)
  46. {
  47. const GpuSceneLight light = lights[i];
  48. // Get the light bounds
  49. Vec3 worldLightAabbMin;
  50. Vec3 worldLightAabbMax;
  51. if(light.m_isPointLight)
  52. {
  53. worldLightAabbMin = light.m_position - light.m_radius;
  54. worldLightAabbMax = light.m_position + light.m_radius;
  55. }
  56. else
  57. {
  58. worldLightAabbMin = light.m_position;
  59. worldLightAabbMax = light.m_position;
  60. [unroll] for(U32 i = 0; i < 4; ++i)
  61. {
  62. worldLightAabbMin = min(worldLightAabbMin, light.m_edgePoints[i]);
  63. worldLightAabbMax = max(worldLightAabbMax, light.m_edgePoints[i]);
  64. }
  65. }
  66. if(!aabbAabbOverlap(worldLightAabbMin, worldLightAabbMax, cellMin, cellMax))
  67. {
  68. continue;
  69. }
  70. if(detailedTests)
  71. {
  72. Vec4 spotLightPlanes[5];
  73. if(light.m_isSpotLight)
  74. {
  75. const Vec3 pe = light.m_position;
  76. const Vec3 p0 = light.m_edgePoints[0];
  77. const Vec3 p1 = light.m_edgePoints[1];
  78. const Vec3 p2 = light.m_edgePoints[2];
  79. const Vec3 p3 = light.m_edgePoints[3];
  80. spotLightPlanes[0] = computePlane(pe, p0, p3);
  81. spotLightPlanes[1] = computePlane(pe, p1, p0);
  82. spotLightPlanes[2] = computePlane(pe, p2, p1);
  83. spotLightPlanes[3] = computePlane(pe, p3, p2);
  84. spotLightPlanes[4] = computePlane(p3, p0, p1);
  85. }
  86. if(light.m_isPointLight && !aabbSphereOverlap(cellMin, cellMax, light.m_position, light.m_radius))
  87. {
  88. continue;
  89. }
  90. else if(light.m_isSpotLight && !insideFrustum(spotLightPlanes, cellMin, cellMax))
  91. {
  92. continue;
  93. }
  94. }
  95. U32 count;
  96. InterlockedAdd(SBUFF(lightIndexCount, 0), 1, count);
  97. ++count;
  98. if(count > consts.m_maxLightIndices)
  99. {
  100. // Light index list is too small
  101. break;
  102. }
  103. ++visibleLightCount;
  104. binLightToCellFunc(cellIdx, i);
  105. }
  106. informLightIndexCountFunc(cellIdx, visibleLightCount);
  107. }
  108. // ===========================================================================
  109. // Setup =
  110. // ===========================================================================
  111. #if NOT_ZERO(ANKI_TECHNIQUE_Setup)
  112. RWStructuredBuffer<U32> g_lightIndexCountsPerCell : register(u0);
  113. RWStructuredBuffer<U32> g_lightIndexCount : register(u1);
  114. RWStructuredBuffer<U32> g_groupWidePrefixSums : register(u2);
  115. RWStructuredBuffer<U32> g_threadgroupCount : register(u3);
  116. [numthreads(64, 1, 1)] void main(COMPUTE_ARGS)
  117. {
  118. if(svDispatchThreadId.x == 0)
  119. {
  120. SBUFF(g_lightIndexCount, 0) = 0;
  121. SBUFF(g_threadgroupCount, 0) = 0;
  122. }
  123. if(svDispatchThreadId.x < getStructuredBufferElementCount(g_lightIndexCountsPerCell))
  124. {
  125. SBUFF(g_lightIndexCountsPerCell, svDispatchThreadId.x) = 0;
  126. }
  127. if(svDispatchThreadId.x < getStructuredBufferElementCount(g_groupWidePrefixSums))
  128. {
  129. SBUFF(g_groupWidePrefixSums, svDispatchThreadId.x) = 0;
  130. }
  131. }
  132. #endif
  133. // ===========================================================================
  134. // Count =
  135. // ===========================================================================
  136. // Counts the light indices per cell
  137. #if NOT_ZERO(ANKI_TECHNIQUE_Count)
  138. StructuredBuffer<GpuSceneLight> g_lights : register(t0);
  139. RWStructuredBuffer<U32> g_lightIndexCountsPerCell : register(u0);
  140. RWStructuredBuffer<U32> g_lightIndexCount : register(u1);
  141. RWStructuredBuffer<U32> g_groupWidePrefixSums : register(u2);
  142. RWStructuredBuffer<U32> g_threadgroupCount : register(u3);
  143. ANKI_FAST_CONSTANTS(GpuVisibilityLocalLightsConsts, g_consts)
  144. struct Func
  145. {
  146. void operator()(U32 cellIdx, U32 lightIdx)
  147. {
  148. InterlockedAdd(SBUFF(g_lightIndexCountsPerCell, cellIdx), 1);
  149. }
  150. };
  151. struct Func2
  152. {
  153. void operator()(U32 cellIdx, U32 visibleLightCount)
  154. {
  155. if(visibleLightCount)
  156. {
  157. const U32 group = cellIdx / kPrefixSumElementCountPerThreadgroup;
  158. InterlockedAdd(SBUFF(g_groupWidePrefixSums, group), visibleLightCount);
  159. }
  160. }
  161. };
  162. constexpr U32 kThreadCount = 64;
  163. [numthreads(kThreadCount, 1, 1)] void main(COMPUTE_ARGS)
  164. {
  165. Func func;
  166. Func2 func2;
  167. lightVsCellVisibility(g_lights, svDispatchThreadId.x, g_consts, g_lightIndexCount, false, func, func2);
  168. // Sync to make sure all the atomic ops have finished before the following code reads them
  169. AllMemoryBarrierWithGroupSync();
  170. // Compute the group prefix sum
  171. if(svGroupIndex == 0)
  172. {
  173. U32 threadgroupIdx;
  174. InterlockedAdd(SBUFF(g_threadgroupCount, 0), 1, threadgroupIdx);
  175. const U32 threadgroupCount = (g_consts.m_cellCount + kThreadCount - 1) / kThreadCount;
  176. const Bool lastThreadgroupExecuting = (threadgroupIdx + 1 == threadgroupCount);
  177. if(lastThreadgroupExecuting)
  178. {
  179. const U32 prefixSumGroupCount = getStructuredBufferElementCount(g_groupWidePrefixSums);
  180. U32 count = 0;
  181. for(U32 i = 0; i < prefixSumGroupCount; ++i)
  182. {
  183. const U32 c = SBUFF(g_groupWidePrefixSums, i);
  184. SBUFF(g_groupWidePrefixSums, i) = count;
  185. count += c;
  186. }
  187. }
  188. }
  189. }
  190. #endif
  191. // ===========================================================================
  192. // PrefixSum =
  193. // ===========================================================================
  194. // Parallel prefix based on: https://developer.nvidia.com/gpugems/gpugems3/part-vi-gpu-computing/chapter-39-parallel-prefix-sum-scan-cuda
  195. // But it runs multiple iterations to support bigger arrays
  196. #if NOT_ZERO(ANKI_TECHNIQUE_PrefixSum)
  197. StructuredBuffer<U32> g_groupWidePrefixSums : register(t0);
  198. RWStructuredBuffer<U32> g_inputElements : register(u0); // It's the g_lightIndexCountsPerCell. RW because we want to zero it at the end
  199. RWStructuredBuffer<U32> g_outputElements : register(u1);
  200. // Some stuff to zero
  201. RWStructuredBuffer<U32> g_lightIndexCount : register(u2);
  202. ANKI_FAST_CONSTANTS(GpuVisibilityLocalLightsConsts, g_consts)
  203. groupshared U32 g_tmp[kPrefixSumElementCountPerThreadgroup];
  204. [numthreads(kPrefixSumThreadCount, 1, 1)] void main(COMPUTE_ARGS)
  205. {
  206. const U32 elementCount = g_consts.m_cellCount;
  207. const U32 tid = svGroupIndex;
  208. const U32 group = svGroupId.x;
  209. const U32 firstElement = group * kPrefixSumElementCountPerThreadgroup;
  210. const U32 endElement = min((group + 1) * kPrefixSumElementCountPerThreadgroup, elementCount);
  211. // Load input into shared memory
  212. const U32 inIdx1 = 2 * tid + firstElement;
  213. const U32 value1 = (inIdx1 < endElement) ? SBUFF(g_inputElements, inIdx1) : 0;
  214. g_tmp[2 * tid] = value1;
  215. const U32 inIdx2 = 2 * tid + 1 + firstElement;
  216. const U32 value2 = (inIdx2 < endElement) ? SBUFF(g_inputElements, inIdx2) : 0;
  217. g_tmp[2 * tid + 1] = value2;
  218. // Since g_inputElements have been read reset them to be reused in the next job
  219. if(inIdx1 < endElement)
  220. {
  221. SBUFF(g_inputElements, inIdx1) = 0;
  222. }
  223. if(inIdx2 < endElement)
  224. {
  225. SBUFF(g_inputElements, inIdx2) = 0;
  226. }
  227. // Perform reduction
  228. U32 offset = 1;
  229. for(U32 d = kPrefixSumElementCountPerThreadgroup >> 1; d > 0; d >>= 1)
  230. {
  231. GroupMemoryBarrierWithGroupSync();
  232. if(tid < d)
  233. {
  234. const U32 ai = offset * (2 * tid + 1) - 1;
  235. const U32 bi = offset * (2 * tid + 2) - 1;
  236. g_tmp[bi] += g_tmp[ai];
  237. }
  238. offset *= 2;
  239. }
  240. // Clear the last element
  241. if(tid == 0)
  242. {
  243. g_tmp[kPrefixSumElementCountPerThreadgroup - 1] = 0;
  244. }
  245. // Perform downsweep and build scan
  246. for(U32 d = 1; d < kPrefixSumElementCountPerThreadgroup; d *= 2)
  247. {
  248. offset >>= 1;
  249. GroupMemoryBarrierWithGroupSync();
  250. if(tid < d)
  251. {
  252. const U32 ai = offset * (2 * tid + 1) - 1;
  253. const U32 bi = offset * (2 * tid + 2) - 1;
  254. const U32 t = g_tmp[ai];
  255. g_tmp[ai] = g_tmp[bi];
  256. g_tmp[bi] += t;
  257. }
  258. }
  259. GroupMemoryBarrierWithGroupSync();
  260. // Write to output buffer
  261. const U32 groupPrefixSum = SBUFF(g_groupWidePrefixSums, group);
  262. if(inIdx1 < endElement)
  263. {
  264. SBUFF(g_outputElements, inIdx1) = g_tmp[2 * tid] + groupPrefixSum;
  265. }
  266. if(inIdx2 < endElement)
  267. {
  268. SBUFF(g_outputElements, inIdx2) = g_tmp[2 * tid + 1] + groupPrefixSum;
  269. }
  270. // Abuse this compute job to also reset that buffer
  271. if(svDispatchThreadId.x == 0)
  272. {
  273. SBUFF(g_lightIndexCount, 0) = 0;
  274. }
  275. }
  276. #endif
  277. // ===========================================================================
  278. // Fill =
  279. // ===========================================================================
  280. // After the prefix sum is complete this job can store the results
  281. #if NOT_ZERO(ANKI_TECHNIQUE_Fill)
  282. StructuredBuffer<GpuSceneLight> g_lights : register(t0);
  283. StructuredBuffer<U32> g_lightIndexListOffsets : register(t1); // Basically the prefix sum. One per cell
  284. RWStructuredBuffer<U32> g_lightIndexCount : register(u0);
  285. RWStructuredBuffer<U32> g_lightIndexCountsPerCell : register(u1);
  286. RWStructuredBuffer<U32> g_lightIndexList : register(u2);
  287. ANKI_FAST_CONSTANTS(GpuVisibilityLocalLightsConsts, g_consts)
  288. struct Func
  289. {
  290. void operator()(U32 clusterIdx, U32 lightIdx)
  291. {
  292. U32 offset;
  293. InterlockedAdd(SBUFF(g_lightIndexCountsPerCell, clusterIdx), 1, offset);
  294. offset += SBUFF(g_lightIndexListOffsets, clusterIdx);
  295. SBUFF(g_lightIndexList, offset) = lightIdx;
  296. }
  297. };
  298. struct Func2
  299. {
  300. void operator()(U32 clusterIdx, U32 visibleLightCount)
  301. {
  302. }
  303. };
  304. [numthreads(64, 1, 1)] void main(COMPUTE_ARGS)
  305. {
  306. Func func;
  307. Func2 func2;
  308. lightVsCellVisibility(g_lights, svDispatchThreadId.x, g_consts, g_lightIndexCount, true, func, func2);
  309. }
  310. #endif