ShadowMapping.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/renderer/ShadowMapping.h>
  6. #include <anki/renderer/Renderer.h>
  7. #include <anki/renderer/RenderQueue.h>
  8. #include <anki/core/ConfigSet.h>
  9. #include <anki/util/ThreadHive.h>
  10. #include <anki/util/Tracer.h>
  11. namespace anki
  12. {
  13. class ShadowMapping::Scratch::WorkItem
  14. {
  15. public:
  16. Array<U32, 4> m_viewport;
  17. RenderQueue* m_renderQueue;
  18. U32 m_firstRenderableElement;
  19. U32 m_renderableElementCount;
  20. U32 m_threadPoolTaskIdx;
  21. };
  22. class ShadowMapping::Scratch::LightToRenderToScratchInfo
  23. {
  24. public:
  25. Array<U32, 4> m_viewport;
  26. RenderQueue* m_renderQueue;
  27. U32 m_drawcallCount;
  28. };
  29. class ShadowMapping::Atlas::ResolveWorkItem
  30. {
  31. public:
  32. Vec4 m_uvIn; ///< UV + size that point to the scratch buffer.
  33. Array<U32, 4> m_viewportOut; ///< Viewport in the atlas RT.
  34. Bool m_blur;
  35. };
  36. ShadowMapping::~ShadowMapping()
  37. {
  38. }
  39. Error ShadowMapping::init(const ConfigSet& config)
  40. {
  41. ANKI_R_LOGI("Initializing shadowmapping");
  42. const Error err = initInternal(config);
  43. if(err)
  44. {
  45. ANKI_R_LOGE("Failed to initialize shadowmapping");
  46. }
  47. ANKI_R_LOGI("\tScratch size %ux%u. atlas size %ux%u", m_scratch.m_tileCountX * m_scratch.m_tileResolution,
  48. m_scratch.m_tileCountY * m_scratch.m_tileResolution,
  49. m_atlas.m_tileCountBothAxis * m_atlas.m_tileResolution,
  50. m_atlas.m_tileCountBothAxis * m_atlas.m_tileResolution);
  51. return err;
  52. }
  53. Error ShadowMapping::initScratch(const ConfigSet& cfg)
  54. {
  55. // Init the shadowmaps and FBs
  56. {
  57. m_scratch.m_tileCountX = cfg.getNumberU32("r_shadowMappingScratchTileCountX");
  58. m_scratch.m_tileCountY = cfg.getNumberU32("r_shadowMappingScratchTileCountY");
  59. m_scratch.m_tileResolution = cfg.getNumberU32("r_shadowMappingTileResolution");
  60. // RT
  61. m_scratch.m_rtDescr = m_r->create2DRenderTargetDescription(m_scratch.m_tileResolution * m_scratch.m_tileCountX,
  62. m_scratch.m_tileResolution * m_scratch.m_tileCountY,
  63. SHADOW_DEPTH_PIXEL_FORMAT, "SM scratch");
  64. m_scratch.m_rtDescr.bake();
  65. // FB
  66. m_scratch.m_fbDescr.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::CLEAR;
  67. m_scratch.m_fbDescr.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_depth = 1.0f;
  68. m_scratch.m_fbDescr.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::DEPTH;
  69. m_scratch.m_fbDescr.bake();
  70. }
  71. m_scratch.m_tileAlloc.init(getAllocator(), m_scratch.m_tileCountX, m_scratch.m_tileCountY, m_lodCount, false);
  72. return Error::NONE;
  73. }
  74. Error ShadowMapping::initAtlas(const ConfigSet& cfg)
  75. {
  76. // Init RT
  77. {
  78. m_atlas.m_tileResolution = cfg.getNumberU32("r_shadowMappingTileResolution");
  79. m_atlas.m_tileCountBothAxis = cfg.getNumberU32("r_shadowMappingTileCountPerRowOrColumn");
  80. // RT
  81. TextureInitInfo texinit = m_r->create2DRenderTargetInitInfo(
  82. m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis,
  83. m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis, SHADOW_COLOR_PIXEL_FORMAT,
  84. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::IMAGE_COMPUTE_WRITE | TextureUsageBit::SAMPLED_COMPUTE,
  85. "SM atlas");
  86. texinit.m_initialUsage = TextureUsageBit::SAMPLED_FRAGMENT;
  87. ClearValue clearVal;
  88. clearVal.m_colorf[0] = 1.0f;
  89. m_atlas.m_tex = m_r->createAndClearRenderTarget(texinit, clearVal);
  90. }
  91. // Tiles
  92. m_atlas.m_tileAlloc.init(getAllocator(), m_atlas.m_tileCountBothAxis, m_atlas.m_tileCountBothAxis, m_lodCount,
  93. true);
  94. // Programs and shaders
  95. {
  96. ANKI_CHECK(getResourceManager().loadResource("shaders/ExponentialShadowmappingResolve.ankiprog",
  97. m_atlas.m_resolveProg));
  98. ShaderProgramResourceVariantInitInfo variantInitInfo(m_atlas.m_resolveProg);
  99. variantInitInfo.addConstant("INPUT_TEXTURE_SIZE", UVec2(m_scratch.m_tileCountX * m_scratch.m_tileResolution,
  100. m_scratch.m_tileCountY * m_scratch.m_tileResolution));
  101. const ShaderProgramResourceVariant* variant;
  102. m_atlas.m_resolveProg->getOrCreateVariant(variantInitInfo, variant);
  103. m_atlas.m_resolveGrProg = variant->getProgram();
  104. }
  105. return Error::NONE;
  106. }
  107. Error ShadowMapping::initInternal(const ConfigSet& cfg)
  108. {
  109. ANKI_CHECK(initScratch(cfg));
  110. ANKI_CHECK(initAtlas(cfg));
  111. m_lodDistances[0] = cfg.getNumberF32("r_shadowMappingLightLodDistance0");
  112. m_lodDistances[1] = cfg.getNumberF32("r_shadowMappingLightLodDistance1");
  113. return Error::NONE;
  114. }
  115. void ShadowMapping::runAtlas(RenderPassWorkContext& rgraphCtx)
  116. {
  117. ANKI_ASSERT(m_atlas.m_resolveWorkItems.getSize());
  118. ANKI_TRACE_SCOPED_EVENT(R_SM);
  119. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  120. cmdb->bindShaderProgram(m_atlas.m_resolveGrProg);
  121. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  122. rgraphCtx.bindTexture(0, 1, m_scratch.m_rt, TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  123. rgraphCtx.bindImage(0, 2, m_atlas.m_rt, {});
  124. for(const Atlas::ResolveWorkItem& workItem : m_atlas.m_resolveWorkItems)
  125. {
  126. ANKI_TRACE_INC_COUNTER(R_SHADOW_PASSES, 1);
  127. struct Uniforms
  128. {
  129. UVec4 m_viewport;
  130. Vec2 m_uvScale;
  131. Vec2 m_uvTranslation;
  132. U32 m_blur;
  133. U32 m_padding0;
  134. U32 m_padding1;
  135. U32 m_padding2;
  136. } unis;
  137. unis.m_uvScale = workItem.m_uvIn.zw();
  138. unis.m_uvTranslation = workItem.m_uvIn.xy();
  139. unis.m_viewport = UVec4(workItem.m_viewportOut[0], workItem.m_viewportOut[1], workItem.m_viewportOut[2],
  140. workItem.m_viewportOut[3]);
  141. unis.m_blur = workItem.m_blur;
  142. cmdb->setPushConstants(&unis, sizeof(unis));
  143. dispatchPPCompute(cmdb, 8, 8, workItem.m_viewportOut[2], workItem.m_viewportOut[3]);
  144. }
  145. }
  146. void ShadowMapping::runShadowMapping(RenderPassWorkContext& rgraphCtx)
  147. {
  148. ANKI_ASSERT(m_scratch.m_workItems.getSize());
  149. ANKI_TRACE_SCOPED_EVENT(R_SM);
  150. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  151. const U threadIdx = rgraphCtx.m_currentSecondLevelCommandBufferIndex;
  152. for(Scratch::WorkItem& work : m_scratch.m_workItems)
  153. {
  154. if(work.m_threadPoolTaskIdx != threadIdx)
  155. {
  156. continue;
  157. }
  158. // Set state
  159. cmdb->setViewport(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
  160. cmdb->setScissor(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
  161. m_r->getSceneDrawer().drawRange(Pass::SM, work.m_renderQueue->m_viewMatrix,
  162. work.m_renderQueue->m_viewProjectionMatrix,
  163. Mat4::getIdentity(), // Don't care about prev matrices here
  164. cmdb, m_r->getSamplers().m_trilinearRepeatAniso,
  165. work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement,
  166. work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement
  167. + work.m_renderableElementCount,
  168. MAX_LOD_COUNT - 1);
  169. }
  170. }
  171. void ShadowMapping::populateRenderGraph(RenderingContext& ctx)
  172. {
  173. ANKI_TRACE_SCOPED_EVENT(R_SM);
  174. // First process the lights
  175. U32 threadCountForScratchPass = 0;
  176. processLights(ctx, threadCountForScratchPass);
  177. // Build the render graph
  178. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  179. if(m_scratch.m_workItems.getSize())
  180. {
  181. // Will have to create render passes
  182. // Scratch pass
  183. {
  184. // Compute render area
  185. const U32 minx = 0, miny = 0;
  186. const U32 height = m_scratch.m_maxViewportHeight;
  187. const U32 width = m_scratch.m_maxViewportWidth;
  188. GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("SM scratch");
  189. m_scratch.m_rt = rgraph.newRenderTarget(m_scratch.m_rtDescr);
  190. pass.setFramebufferInfo(m_scratch.m_fbDescr, {}, m_scratch.m_rt, minx, miny, width, height);
  191. ANKI_ASSERT(threadCountForScratchPass
  192. && threadCountForScratchPass <= m_r->getThreadHive().getThreadCount());
  193. pass.setWork(
  194. [](RenderPassWorkContext& rgraphCtx) {
  195. static_cast<ShadowMapping*>(rgraphCtx.m_userData)->runShadowMapping(rgraphCtx);
  196. },
  197. this, threadCountForScratchPass);
  198. TextureSubresourceInfo subresource = TextureSubresourceInfo(DepthStencilAspectBit::DEPTH);
  199. pass.newDependency({m_scratch.m_rt, TextureUsageBit::ALL_FRAMEBUFFER_ATTACHMENT, subresource});
  200. }
  201. // Atlas pass
  202. {
  203. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("SM atlas");
  204. m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex, TextureUsageBit::SAMPLED_FRAGMENT);
  205. pass.setWork(
  206. [](RenderPassWorkContext& rgraphCtx) {
  207. static_cast<ShadowMapping*>(rgraphCtx.m_userData)->runAtlas(rgraphCtx);
  208. },
  209. this, 0);
  210. pass.newDependency({m_scratch.m_rt, TextureUsageBit::SAMPLED_COMPUTE,
  211. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH)});
  212. pass.newDependency({m_atlas.m_rt, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  213. }
  214. }
  215. else
  216. {
  217. // No need for shadowmapping passes, just import the atlas
  218. m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex, TextureUsageBit::SAMPLED_FRAGMENT);
  219. }
  220. }
  221. Mat4 ShadowMapping::createSpotLightTextureMatrix(const Viewport& viewport) const
  222. {
  223. const F32 atlasSize = F32(m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis);
  224. #if ANKI_COMPILER_GCC_COMPATIBLE
  225. # pragma GCC diagnostic push
  226. # pragma GCC diagnostic ignored "-Wpedantic" // Because GCC and clang throw an incorrect warning
  227. #endif
  228. const Vec2 uv(F32(viewport[0]) / atlasSize, F32(viewport[1]) / atlasSize);
  229. #if ANKI_COMPILER_GCC_COMPATIBLE
  230. # pragma GCC diagnostic pop
  231. #endif
  232. ANKI_ASSERT(uv >= Vec2(0.0f) && uv <= Vec2(1.0f));
  233. ANKI_ASSERT(viewport[2] == viewport[3]);
  234. const F32 sizeTextureSpace = F32(viewport[2]) / atlasSize;
  235. return Mat4(sizeTextureSpace, 0.0f, 0.0f, uv.x(), 0.0f, sizeTextureSpace, 0.0f, uv.y(), 0.0f, 0.0f, 1.0f, 0.0f,
  236. 0.0f, 0.0f, 0.0f, 1.0f);
  237. }
  238. U32 ShadowMapping::choseLod(const Vec4& cameraOrigin, const PointLightQueueElement& light, Bool& blurAtlas) const
  239. {
  240. const F32 distFromTheCamera = (cameraOrigin - light.m_worldPosition.xyz0()).getLength() - light.m_radius;
  241. if(distFromTheCamera < m_lodDistances[0])
  242. {
  243. ANKI_ASSERT(m_pointLightsMaxLod == 1);
  244. blurAtlas = true;
  245. return 1;
  246. }
  247. else
  248. {
  249. blurAtlas = false;
  250. return 0;
  251. }
  252. }
  253. U32 ShadowMapping::choseLod(const Vec4& cameraOrigin, const SpotLightQueueElement& light, Bool& blurAtlas) const
  254. {
  255. // Get some data
  256. const Vec4 coneOrigin = light.m_worldTransform.getTranslationPart().xyz0();
  257. const Vec4 coneDir = -light.m_worldTransform.getZAxis().xyz0();
  258. const F32 coneAngle = light.m_outerAngle;
  259. // Compute the distance from the camera to the light cone
  260. const Vec4 V = cameraOrigin - coneOrigin;
  261. const F32 VlenSq = V.dot(V);
  262. const F32 V1len = V.dot(coneDir);
  263. const F32 distFromTheCamera = cos(coneAngle) * sqrt(VlenSq - V1len * V1len) - V1len * sin(coneAngle);
  264. U32 lod;
  265. if(distFromTheCamera < m_lodDistances[0])
  266. {
  267. blurAtlas = true;
  268. lod = 2;
  269. }
  270. else if(distFromTheCamera < m_lodDistances[1])
  271. {
  272. blurAtlas = false;
  273. lod = 1;
  274. }
  275. else
  276. {
  277. blurAtlas = false;
  278. lod = 0;
  279. }
  280. return lod;
  281. }
  282. TileAllocatorResult ShadowMapping::allocateTilesAndScratchTiles(U64 lightUuid, U32 faceCount, const U64* faceTimestamps,
  283. const U32* faceIndices, const U32* drawcallsCount,
  284. const U32* lods, Viewport* atlasTileViewports,
  285. Viewport* scratchTileViewports,
  286. TileAllocatorResult* subResults)
  287. {
  288. ANKI_ASSERT(lightUuid > 0);
  289. ANKI_ASSERT(faceCount > 0);
  290. ANKI_ASSERT(faceTimestamps);
  291. ANKI_ASSERT(faceIndices);
  292. ANKI_ASSERT(drawcallsCount);
  293. ANKI_ASSERT(lods);
  294. TileAllocatorResult res = TileAllocatorResult::ALLOCATION_FAILED;
  295. // Allocate atlas tiles first. They may be cached and that will affect how many scratch tiles we'll need
  296. for(U i = 0; i < faceCount; ++i)
  297. {
  298. res = m_atlas.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), faceTimestamps[i], lightUuid, faceIndices[i],
  299. drawcallsCount[i], lods[i], atlasTileViewports[i]);
  300. if(res == TileAllocatorResult::ALLOCATION_FAILED)
  301. {
  302. ANKI_R_LOGW("There is not enough space in the shadow atlas for more shadow maps. "
  303. "Increase the r_shadowMappingTileCountPerRowOrColumn or decrease the scene's shadow casters");
  304. // Invalidate cache entries for what we already allocated
  305. for(U j = 0; j < i; ++j)
  306. {
  307. m_atlas.m_tileAlloc.invalidateCache(lightUuid, faceIndices[j]);
  308. }
  309. return res;
  310. }
  311. subResults[i] = res;
  312. // Fix viewport
  313. atlasTileViewports[i][0] *= m_atlas.m_tileResolution;
  314. atlasTileViewports[i][1] *= m_atlas.m_tileResolution;
  315. atlasTileViewports[i][2] *= m_atlas.m_tileResolution;
  316. atlasTileViewports[i][3] *= m_atlas.m_tileResolution;
  317. }
  318. // Allocate scratch tiles
  319. for(U i = 0; i < faceCount; ++i)
  320. {
  321. if(subResults[i] == TileAllocatorResult::CACHED)
  322. {
  323. continue;
  324. }
  325. ANKI_ASSERT(subResults[i] == TileAllocatorResult::ALLOCATION_SUCCEEDED);
  326. res = m_scratch.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), faceTimestamps[i], lightUuid, faceIndices[i],
  327. drawcallsCount[i], lods[i], scratchTileViewports[i]);
  328. if(res == TileAllocatorResult::ALLOCATION_FAILED)
  329. {
  330. ANKI_R_LOGW("Don't have enough space in the scratch shadow mapping buffer. "
  331. "If you see this message too often increase r_shadowMappingScratchTileCountX/Y");
  332. // Invalidate atlas tiles
  333. for(U j = 0; j < faceCount; ++j)
  334. {
  335. m_atlas.m_tileAlloc.invalidateCache(lightUuid, faceIndices[j]);
  336. }
  337. return res;
  338. }
  339. // Fix viewport
  340. scratchTileViewports[i][0] *= m_scratch.m_tileResolution;
  341. scratchTileViewports[i][1] *= m_scratch.m_tileResolution;
  342. scratchTileViewports[i][2] *= m_scratch.m_tileResolution;
  343. scratchTileViewports[i][3] *= m_scratch.m_tileResolution;
  344. // Update the max view width
  345. m_scratch.m_maxViewportWidth =
  346. max(m_scratch.m_maxViewportWidth, scratchTileViewports[i][0] + scratchTileViewports[i][2]);
  347. m_scratch.m_maxViewportHeight =
  348. max(m_scratch.m_maxViewportHeight, scratchTileViewports[i][1] + scratchTileViewports[i][3]);
  349. }
  350. return res;
  351. }
  352. void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScratchPass)
  353. {
  354. // Reset the scratch viewport width
  355. m_scratch.m_maxViewportWidth = 0;
  356. m_scratch.m_maxViewportHeight = 0;
  357. // Vars
  358. const Vec4 cameraOrigin = ctx.m_renderQueue->m_cameraTransform.getTranslationPart().xyz0();
  359. DynamicArrayAuto<Scratch::LightToRenderToScratchInfo> lightsToRender(ctx.m_tempAllocator);
  360. U32 drawcallCount = 0;
  361. DynamicArrayAuto<Atlas::ResolveWorkItem> atlasWorkItems(ctx.m_tempAllocator);
  362. // First thing, allocate an empty tile for empty faces of point lights
  363. Viewport emptyTileViewport;
  364. {
  365. const TileAllocatorResult res = m_atlas.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), 1, MAX_U64, 0, 1,
  366. m_pointLightsMaxLod, emptyTileViewport);
  367. (void)res;
  368. #if ANKI_ENABLE_ASSERTS
  369. static Bool firstRun = true;
  370. if(firstRun)
  371. {
  372. ANKI_ASSERT(res == TileAllocatorResult::ALLOCATION_SUCCEEDED);
  373. firstRun = false;
  374. }
  375. else
  376. {
  377. ANKI_ASSERT(res == TileAllocatorResult::CACHED);
  378. }
  379. #endif
  380. }
  381. // Process the directional light first.
  382. if(ctx.m_renderQueue->m_directionalLight.m_shadowCascadeCount > 0)
  383. {
  384. DirectionalLightQueueElement& light = ctx.m_renderQueue->m_directionalLight;
  385. Array<U64, MAX_SHADOW_CASCADES> timestamps;
  386. Array<U32, MAX_SHADOW_CASCADES> cascadeIndices;
  387. Array<U32, MAX_SHADOW_CASCADES> drawcallCounts;
  388. Array<Viewport, MAX_SHADOW_CASCADES> atlasViewports;
  389. Array<Viewport, MAX_SHADOW_CASCADES> scratchViewports;
  390. Array<TileAllocatorResult, MAX_SHADOW_CASCADES> subResults;
  391. Array<U32, MAX_SHADOW_CASCADES> lods;
  392. Array<Bool, MAX_SHADOW_CASCADES> blurAtlass;
  393. U32 activeCascades = 0;
  394. for(U32 cascade = 0; cascade < light.m_shadowCascadeCount; ++cascade)
  395. {
  396. ANKI_ASSERT(light.m_shadowRenderQueues[cascade]);
  397. if(light.m_shadowRenderQueues[cascade]->m_renderables.getSize() > 0)
  398. {
  399. // Cascade with drawcalls, will need tiles
  400. timestamps[activeCascades] = m_r->getGlobalTimestamp(); // This light is always updated
  401. cascadeIndices[activeCascades] = cascade;
  402. drawcallCounts[activeCascades] = 1; // Doesn't matter
  403. // Change the quality per cascade
  404. blurAtlass[activeCascades] = (cascade <= 1);
  405. lods[activeCascades] = (cascade <= 1) ? (m_lodCount - 1) : (lods[0] - 1);
  406. ++activeCascades;
  407. }
  408. }
  409. const Bool allocationFailed =
  410. activeCascades == 0
  411. || allocateTilesAndScratchTiles(light.m_uuid, activeCascades, &timestamps[0], &cascadeIndices[0],
  412. &drawcallCounts[0], &lods[0], &atlasViewports[0], &scratchViewports[0],
  413. &subResults[0])
  414. == TileAllocatorResult::ALLOCATION_FAILED;
  415. if(!allocationFailed)
  416. {
  417. activeCascades = 0;
  418. for(U cascade = 0; cascade < light.m_shadowCascadeCount; ++cascade)
  419. {
  420. if(light.m_shadowRenderQueues[cascade]->m_renderables.getSize() > 0)
  421. {
  422. // Cascade with drawcalls, push some work for it
  423. // Update the texture matrix to point to the correct region in the atlas
  424. light.m_textureMatrices[cascade] =
  425. createSpotLightTextureMatrix(atlasViewports[activeCascades]) * light.m_textureMatrices[cascade];
  426. // Push work
  427. newScratchAndAtlasResloveRenderWorkItems(
  428. atlasViewports[activeCascades], scratchViewports[activeCascades], blurAtlass[activeCascades],
  429. light.m_shadowRenderQueues[cascade], lightsToRender, atlasWorkItems, drawcallCount);
  430. ++activeCascades;
  431. }
  432. else
  433. {
  434. // Empty cascade, point it to the empty tile
  435. light.m_textureMatrices[cascade] =
  436. createSpotLightTextureMatrix(emptyTileViewport) * light.m_textureMatrices[cascade];
  437. }
  438. }
  439. }
  440. else
  441. {
  442. // Light can't be a caster this frame
  443. light.m_shadowCascadeCount = 0;
  444. zeroMemory(light.m_shadowRenderQueues);
  445. }
  446. }
  447. // Process the point lights.
  448. for(PointLightQueueElement* light : ctx.m_renderQueue->m_shadowPointLights)
  449. {
  450. // Prepare data to allocate tiles and allocate
  451. Array<U64, 6> timestamps;
  452. Array<U32, 6> faceIndices;
  453. Array<U32, 6> drawcallCounts;
  454. Array<Viewport, 6> atlasViewports;
  455. Array<Viewport, 6> scratchViewports;
  456. Array<TileAllocatorResult, 6> subResults;
  457. Array<U32, 6> lods;
  458. U32 numOfFacesThatHaveDrawcalls = 0;
  459. Bool blurAtlas;
  460. const U32 lod = choseLod(cameraOrigin, *light, blurAtlas);
  461. for(U32 face = 0; face < 6; ++face)
  462. {
  463. ANKI_ASSERT(light->m_shadowRenderQueues[face]);
  464. if(light->m_shadowRenderQueues[face]->m_renderables.getSize())
  465. {
  466. // Has renderables, need to allocate tiles for it so add it to the arrays
  467. faceIndices[numOfFacesThatHaveDrawcalls] = face;
  468. timestamps[numOfFacesThatHaveDrawcalls] =
  469. light->m_shadowRenderQueues[face]->m_shadowRenderablesLastUpdateTimestamp;
  470. drawcallCounts[numOfFacesThatHaveDrawcalls] =
  471. light->m_shadowRenderQueues[face]->m_renderables.getSize();
  472. lods[numOfFacesThatHaveDrawcalls] = lod;
  473. ++numOfFacesThatHaveDrawcalls;
  474. }
  475. }
  476. const Bool allocationFailed =
  477. numOfFacesThatHaveDrawcalls == 0
  478. || allocateTilesAndScratchTiles(light->m_uuid, numOfFacesThatHaveDrawcalls, &timestamps[0], &faceIndices[0],
  479. &drawcallCounts[0], &lods[0], &atlasViewports[0], &scratchViewports[0],
  480. &subResults[0])
  481. == TileAllocatorResult::ALLOCATION_FAILED;
  482. if(!allocationFailed)
  483. {
  484. // All good, update the lights
  485. const F32 atlasResolution = F32(m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis);
  486. F32 superTileSize = F32(atlasViewports[0][2]); // Should be the same for all tiles and faces
  487. superTileSize -= 1.0f; // Remove 2 half texels to avoid bilinear filtering bleeding
  488. light->m_shadowAtlasTileSize = superTileSize / atlasResolution;
  489. numOfFacesThatHaveDrawcalls = 0;
  490. for(U face = 0; face < 6; ++face)
  491. {
  492. if(light->m_shadowRenderQueues[face]->m_renderables.getSize())
  493. {
  494. // Has drawcalls, asigned it to a tile
  495. const Viewport& atlasViewport = atlasViewports[numOfFacesThatHaveDrawcalls];
  496. const Viewport& scratchViewport = scratchViewports[numOfFacesThatHaveDrawcalls];
  497. // Add a half texel to the viewport's start to avoid bilinear filtering bleeding
  498. light->m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + 0.5f) / atlasResolution;
  499. light->m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + 0.5f) / atlasResolution;
  500. if(subResults[numOfFacesThatHaveDrawcalls] != TileAllocatorResult::CACHED)
  501. {
  502. newScratchAndAtlasResloveRenderWorkItems(atlasViewport, scratchViewport, blurAtlas,
  503. light->m_shadowRenderQueues[face], lightsToRender,
  504. atlasWorkItems, drawcallCount);
  505. }
  506. ++numOfFacesThatHaveDrawcalls;
  507. }
  508. else
  509. {
  510. // Doesn't have renderables, point the face to the empty tile
  511. Viewport atlasViewport = emptyTileViewport;
  512. ANKI_ASSERT(F32(atlasViewport[2]) <= superTileSize && F32(atlasViewport[3]) <= superTileSize);
  513. atlasViewport[2] = U32(superTileSize);
  514. atlasViewport[3] = U32(superTileSize);
  515. light->m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + 0.5f) / atlasResolution;
  516. light->m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + 0.5f) / atlasResolution;
  517. }
  518. }
  519. }
  520. else
  521. {
  522. // Light can't be a caster this frame
  523. zeroMemory(light->m_shadowRenderQueues);
  524. }
  525. }
  526. // Process the spot lights
  527. for(SpotLightQueueElement* light : ctx.m_renderQueue->m_shadowSpotLights)
  528. {
  529. ANKI_ASSERT(light->m_shadowRenderQueue);
  530. // Allocate tiles
  531. U32 faceIdx = 0;
  532. TileAllocatorResult subResult;
  533. Viewport atlasViewport;
  534. Viewport scratchViewport;
  535. const U32 localDrawcallCount = light->m_shadowRenderQueue->m_renderables.getSize();
  536. Bool blurAtlas;
  537. const U32 lod = choseLod(cameraOrigin, *light, blurAtlas);
  538. const Bool allocationFailed =
  539. localDrawcallCount == 0
  540. || allocateTilesAndScratchTiles(
  541. light->m_uuid, 1, &light->m_shadowRenderQueue->m_shadowRenderablesLastUpdateTimestamp, &faceIdx,
  542. &localDrawcallCount, &lod, &atlasViewport, &scratchViewport, &subResult)
  543. == TileAllocatorResult::ALLOCATION_FAILED;
  544. if(!allocationFailed)
  545. {
  546. // All good, update the light
  547. // Update the texture matrix to point to the correct region in the atlas
  548. light->m_textureMatrix = createSpotLightTextureMatrix(atlasViewport) * light->m_textureMatrix;
  549. if(subResult != TileAllocatorResult::CACHED)
  550. {
  551. newScratchAndAtlasResloveRenderWorkItems(atlasViewport, scratchViewport, blurAtlas,
  552. light->m_shadowRenderQueue, lightsToRender, atlasWorkItems,
  553. drawcallCount);
  554. }
  555. }
  556. else
  557. {
  558. // Doesn't have renderables or the allocation failed, won't be a shadow caster
  559. light->m_shadowRenderQueue = nullptr;
  560. }
  561. }
  562. // Split the work that will happen in the scratch buffer
  563. if(lightsToRender.getSize())
  564. {
  565. DynamicArrayAuto<Scratch::WorkItem> workItems(ctx.m_tempAllocator);
  566. Scratch::LightToRenderToScratchInfo* lightToRender = lightsToRender.getBegin();
  567. U32 lightToRenderDrawcallCount = lightToRender->m_drawcallCount;
  568. const Scratch::LightToRenderToScratchInfo* lightToRenderEnd = lightsToRender.getEnd();
  569. const U32 threadCount = computeNumberOfSecondLevelCommandBuffers(drawcallCount);
  570. threadCountForScratchPass = threadCount;
  571. for(U32 taskId = 0; taskId < threadCount; ++taskId)
  572. {
  573. U32 start, end;
  574. splitThreadedProblem(taskId, threadCount, drawcallCount, start, end);
  575. // While there are drawcalls in this task emit new work items
  576. U32 taskDrawcallCount = end - start;
  577. ANKI_ASSERT(taskDrawcallCount > 0 && "Because we used computeNumberOfSecondLevelCommandBuffers()");
  578. while(taskDrawcallCount)
  579. {
  580. ANKI_ASSERT(lightToRender != lightToRenderEnd);
  581. const U32 workItemDrawcallCount = min(lightToRenderDrawcallCount, taskDrawcallCount);
  582. Scratch::WorkItem workItem;
  583. workItem.m_viewport = lightToRender->m_viewport;
  584. workItem.m_renderQueue = lightToRender->m_renderQueue;
  585. workItem.m_firstRenderableElement = lightToRender->m_drawcallCount - lightToRenderDrawcallCount;
  586. workItem.m_renderableElementCount = workItemDrawcallCount;
  587. workItem.m_threadPoolTaskIdx = taskId;
  588. workItems.emplaceBack(workItem);
  589. // Decrease the drawcall counts for the task and the light
  590. ANKI_ASSERT(taskDrawcallCount >= workItemDrawcallCount);
  591. taskDrawcallCount -= workItemDrawcallCount;
  592. ANKI_ASSERT(lightToRenderDrawcallCount >= workItemDrawcallCount);
  593. lightToRenderDrawcallCount -= workItemDrawcallCount;
  594. // Move to the next light
  595. if(lightToRenderDrawcallCount == 0)
  596. {
  597. ++lightToRender;
  598. lightToRenderDrawcallCount =
  599. (lightToRender != lightToRenderEnd) ? lightToRender->m_drawcallCount : 0;
  600. }
  601. }
  602. }
  603. ANKI_ASSERT(lightToRender == lightToRenderEnd);
  604. ANKI_ASSERT(lightsToRender.getSize() <= workItems.getSize());
  605. // All good, store the work items for the threads to pick up
  606. {
  607. Scratch::WorkItem* items;
  608. U32 itemSize;
  609. U32 itemStorageSize;
  610. workItems.moveAndReset(items, itemSize, itemStorageSize);
  611. ANKI_ASSERT(items && itemSize && itemStorageSize);
  612. m_scratch.m_workItems = WeakArray<Scratch::WorkItem>(items, itemSize);
  613. Atlas::ResolveWorkItem* atlasItems;
  614. atlasWorkItems.moveAndReset(atlasItems, itemSize, itemStorageSize);
  615. ANKI_ASSERT(atlasItems && itemSize && itemStorageSize);
  616. m_atlas.m_resolveWorkItems = WeakArray<Atlas::ResolveWorkItem>(atlasItems, itemSize);
  617. }
  618. }
  619. else
  620. {
  621. m_scratch.m_workItems = WeakArray<Scratch::WorkItem>();
  622. m_atlas.m_resolveWorkItems = WeakArray<Atlas::ResolveWorkItem>();
  623. }
  624. }
  625. void ShadowMapping::newScratchAndAtlasResloveRenderWorkItems(
  626. const Viewport& atlasViewport, const Viewport& scratchVewport, Bool blurAtlas, RenderQueue* lightRenderQueue,
  627. DynamicArrayAuto<Scratch::LightToRenderToScratchInfo>& scratchWorkItem,
  628. DynamicArrayAuto<Atlas::ResolveWorkItem>& atlasResolveWorkItem, U32& drawcallCount) const
  629. {
  630. // Scratch work item
  631. {
  632. Scratch::LightToRenderToScratchInfo toRender = {scratchVewport, lightRenderQueue,
  633. lightRenderQueue->m_renderables.getSize()};
  634. scratchWorkItem.emplaceBack(toRender);
  635. drawcallCount += lightRenderQueue->m_renderables.getSize();
  636. }
  637. // Atlas resolve work item
  638. {
  639. const F32 scratchAtlasWidth = F32(m_scratch.m_tileCountX * m_scratch.m_tileResolution);
  640. const F32 scratchAtlasHeight = F32(m_scratch.m_tileCountY * m_scratch.m_tileResolution);
  641. Atlas::ResolveWorkItem atlasItem;
  642. atlasItem.m_uvIn[0] = F32(scratchVewport[0]) / scratchAtlasWidth;
  643. atlasItem.m_uvIn[1] = F32(scratchVewport[1]) / scratchAtlasHeight;
  644. atlasItem.m_uvIn[2] = F32(scratchVewport[2]) / scratchAtlasWidth;
  645. atlasItem.m_uvIn[3] = F32(scratchVewport[3]) / scratchAtlasHeight;
  646. atlasItem.m_viewportOut = atlasViewport;
  647. atlasItem.m_blur = blurAtlas;
  648. atlasResolveWorkItem.emplaceBack(atlasItem);
  649. }
  650. }
  651. } // end namespace anki