Renderer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // Copyright (C) 2009-2021, 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/Renderer.h>
  6. #include <AnKi/Renderer/RenderQueue.h>
  7. #include <AnKi/Util/Tracer.h>
  8. #include <AnKi/Util/ThreadHive.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. #include <AnKi/Util/HighRezTimer.h>
  11. #include <AnKi/Collision/Aabb.h>
  12. #include <AnKi/Collision/Plane.h>
  13. #include <AnKi/Collision/Functions.h>
  14. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  15. #include <AnKi/Renderer/ProbeReflections.h>
  16. #include <AnKi/Renderer/GBuffer.h>
  17. #include <AnKi/Renderer/GBufferPost.h>
  18. #include <AnKi/Renderer/LightShading.h>
  19. #include <AnKi/Renderer/ShadowMapping.h>
  20. #include <AnKi/Renderer/FinalComposite.h>
  21. #include <AnKi/Renderer/Ssao.h>
  22. #include <AnKi/Renderer/Bloom.h>
  23. #include <AnKi/Renderer/Tonemapping.h>
  24. #include <AnKi/Renderer/ForwardShading.h>
  25. #include <AnKi/Renderer/LensFlare.h>
  26. #include <AnKi/Renderer/Dbg.h>
  27. #include <AnKi/Renderer/DownscaleBlur.h>
  28. #include <AnKi/Renderer/VolumetricFog.h>
  29. #include <AnKi/Renderer/DepthDownscale.h>
  30. #include <AnKi/Renderer/TemporalAA.h>
  31. #include <AnKi/Renderer/UiStage.h>
  32. #include <AnKi/Renderer/Ssr.h>
  33. #include <AnKi/Renderer/Ssgi.h>
  34. #include <AnKi/Renderer/VolumetricLightingAccumulation.h>
  35. #include <AnKi/Renderer/GlobalIllumination.h>
  36. #include <AnKi/Renderer/GenericCompute.h>
  37. #include <AnKi/Renderer/ShadowmapsResolve.h>
  38. #include <AnKi/Renderer/RtShadows.h>
  39. #include <AnKi/Renderer/AccelerationStructureBuilder.h>
  40. #include <AnKi/Renderer/MotionVectors.h>
  41. #include <AnKi/Renderer/ClusterBinning.h>
  42. namespace anki
  43. {
  44. Renderer::Renderer()
  45. : m_sceneDrawer(this)
  46. {
  47. }
  48. Renderer::~Renderer()
  49. {
  50. for(DebugRtInfo& info : m_debugRts)
  51. {
  52. info.m_rtName.destroy(getAllocator());
  53. }
  54. m_debugRts.destroy(getAllocator());
  55. m_currentDebugRtName.destroy(getAllocator());
  56. }
  57. Error Renderer::init(ThreadHive* hive, ResourceManager* resources, GrManager* gl, StagingGpuMemoryManager* stagingMem,
  58. UiManager* ui, HeapAllocator<U8> alloc, const ConfigSet& config, Timestamp* globTimestamp)
  59. {
  60. ANKI_TRACE_SCOPED_EVENT(R_INIT);
  61. m_globTimestamp = globTimestamp;
  62. m_threadHive = hive;
  63. m_resources = resources;
  64. m_gr = gl;
  65. m_stagingMem = stagingMem;
  66. m_ui = ui;
  67. m_alloc = alloc;
  68. Error err = initInternal(config);
  69. if(err)
  70. {
  71. ANKI_R_LOGE("Failed to initialize the renderer");
  72. }
  73. return err;
  74. }
  75. Error Renderer::initInternal(const ConfigSet& config)
  76. {
  77. m_frameCount = 0;
  78. // Set from the config
  79. m_width = config.getNumberU32("width");
  80. m_height = config.getNumberU32("height");
  81. ANKI_R_LOGI("Initializing offscreen renderer. Size %ux%u", m_width, m_height);
  82. m_tileSize = config.getNumberU32("r_tileSize");
  83. m_tileCounts.x() = (m_width + m_tileSize - 1) / m_tileSize;
  84. m_tileCounts.y() = (m_height + m_tileSize - 1) / m_tileSize;
  85. m_zSplitCount = config.getNumberU32("r_zSplitCount");
  86. // A few sanity checks
  87. if(m_width < 10 || m_height < 10)
  88. {
  89. ANKI_R_LOGE("Incorrect sizes");
  90. return Error::USER_DATA;
  91. }
  92. {
  93. TextureInitInfo texinit;
  94. texinit.m_width = texinit.m_height = 4;
  95. texinit.m_usage = TextureUsageBit::ALL_SAMPLED;
  96. texinit.m_format = Format::R8G8B8A8_UNORM;
  97. texinit.m_initialUsage = TextureUsageBit::ALL_SAMPLED;
  98. TexturePtr tex = getGrManager().newTexture(texinit);
  99. TextureViewInitInfo viewinit(tex);
  100. m_dummyTexView2d = getGrManager().newTextureView(viewinit);
  101. texinit.m_depth = 4;
  102. texinit.m_type = TextureType::_3D;
  103. tex = getGrManager().newTexture(texinit);
  104. viewinit = TextureViewInitInfo(tex);
  105. m_dummyTexView3d = getGrManager().newTextureView(viewinit);
  106. }
  107. m_dummyBuff = getGrManager().newBuffer(BufferInitInfo(
  108. 1024, BufferUsageBit::ALL_UNIFORM | BufferUsageBit::ALL_STORAGE, BufferMapAccessBit::NONE, "Dummy"));
  109. ANKI_CHECK(m_resources->loadResource("Shaders/ClearTextureCompute.ankiprog", m_clearTexComputeProg));
  110. // Init the stages. Careful with the order!!!!!!!!!!
  111. m_genericCompute.reset(m_alloc.newInstance<GenericCompute>(this));
  112. ANKI_CHECK(m_genericCompute->init(config));
  113. m_volLighting.reset(m_alloc.newInstance<VolumetricLightingAccumulation>(this));
  114. ANKI_CHECK(m_volLighting->init(config));
  115. m_gi.reset(m_alloc.newInstance<GlobalIllumination>(this));
  116. ANKI_CHECK(m_gi->init(config));
  117. m_probeReflections.reset(m_alloc.newInstance<ProbeReflections>(this));
  118. ANKI_CHECK(m_probeReflections->init(config));
  119. m_gbuffer.reset(m_alloc.newInstance<GBuffer>(this));
  120. ANKI_CHECK(m_gbuffer->init(config));
  121. m_gbufferPost.reset(m_alloc.newInstance<GBufferPost>(this));
  122. ANKI_CHECK(m_gbufferPost->init(config));
  123. m_shadowMapping.reset(m_alloc.newInstance<ShadowMapping>(this));
  124. ANKI_CHECK(m_shadowMapping->init(config));
  125. m_volFog.reset(m_alloc.newInstance<VolumetricFog>(this));
  126. ANKI_CHECK(m_volFog->init(config));
  127. m_lightShading.reset(m_alloc.newInstance<LightShading>(this));
  128. ANKI_CHECK(m_lightShading->init(config));
  129. m_depth.reset(m_alloc.newInstance<DepthDownscale>(this));
  130. ANKI_CHECK(m_depth->init(config));
  131. m_forwardShading.reset(m_alloc.newInstance<ForwardShading>(this));
  132. ANKI_CHECK(m_forwardShading->init(config));
  133. m_lensFlare.reset(m_alloc.newInstance<LensFlare>(this));
  134. ANKI_CHECK(m_lensFlare->init(config));
  135. m_ssao.reset(m_alloc.newInstance<Ssao>(this));
  136. ANKI_CHECK(m_ssao->init(config));
  137. m_downscale.reset(getAllocator().newInstance<DownscaleBlur>(this));
  138. ANKI_CHECK(m_downscale->init(config));
  139. m_ssr.reset(m_alloc.newInstance<Ssr>(this));
  140. ANKI_CHECK(m_ssr->init(config));
  141. m_ssgi.reset(m_alloc.newInstance<Ssgi>(this));
  142. ANKI_CHECK(m_ssgi->init(config));
  143. m_tonemapping.reset(getAllocator().newInstance<Tonemapping>(this));
  144. ANKI_CHECK(m_tonemapping->init(config));
  145. m_temporalAA.reset(getAllocator().newInstance<TemporalAA>(this));
  146. ANKI_CHECK(m_temporalAA->init(config));
  147. m_bloom.reset(m_alloc.newInstance<Bloom>(this));
  148. ANKI_CHECK(m_bloom->init(config));
  149. m_finalComposite.reset(m_alloc.newInstance<FinalComposite>(this));
  150. ANKI_CHECK(m_finalComposite->init(config));
  151. m_dbg.reset(m_alloc.newInstance<Dbg>(this));
  152. ANKI_CHECK(m_dbg->init(config));
  153. m_uiStage.reset(m_alloc.newInstance<UiStage>(this));
  154. ANKI_CHECK(m_uiStage->init(config));
  155. if(getGrManager().getDeviceCapabilities().m_rayTracingEnabled && config.getBool("scene_rayTracedShadows"))
  156. {
  157. m_accelerationStructureBuilder.reset(m_alloc.newInstance<AccelerationStructureBuilder>(this));
  158. ANKI_CHECK(m_accelerationStructureBuilder->init(config));
  159. m_rtShadows.reset(m_alloc.newInstance<RtShadows>(this));
  160. ANKI_CHECK(m_rtShadows->init(config));
  161. }
  162. else
  163. {
  164. m_smResolve.reset(m_alloc.newInstance<ShadowmapsResolve>(this));
  165. ANKI_CHECK(m_smResolve->init(config));
  166. }
  167. m_motionVectors.reset(m_alloc.newInstance<MotionVectors>(this));
  168. ANKI_CHECK(m_motionVectors->init(config));
  169. m_clusterBinning.reset(m_alloc.newInstance<ClusterBinning>(this));
  170. ANKI_CHECK(m_clusterBinning->init(config));
  171. // Init samplers
  172. {
  173. SamplerInitInfo sinit("Renderer");
  174. sinit.m_addressing = SamplingAddressing::CLAMP;
  175. sinit.m_mipmapFilter = SamplingFilter::NEAREST;
  176. sinit.m_minMagFilter = SamplingFilter::NEAREST;
  177. m_samplers.m_nearestNearestClamp = m_gr->newSampler(sinit);
  178. sinit.m_minMagFilter = SamplingFilter::LINEAR;
  179. sinit.m_mipmapFilter = SamplingFilter::LINEAR;
  180. m_samplers.m_trilinearClamp = m_gr->newSampler(sinit);
  181. sinit.m_addressing = SamplingAddressing::REPEAT;
  182. m_samplers.m_trilinearRepeat = m_gr->newSampler(sinit);
  183. sinit.m_anisotropyLevel = U8(config.getNumberU32("r_textureAnisotropy"));
  184. m_samplers.m_trilinearRepeatAniso = m_gr->newSampler(sinit);
  185. }
  186. initJitteredMats();
  187. return Error::NONE;
  188. }
  189. void Renderer::initJitteredMats()
  190. {
  191. static const Array<Vec2, 16> SAMPLE_LOCS_16 = {
  192. {Vec2(-8.0, 0.0), Vec2(-6.0, -4.0), Vec2(-3.0, -2.0), Vec2(-2.0, -6.0), Vec2(1.0, -1.0), Vec2(2.0, -5.0),
  193. Vec2(6.0, -7.0), Vec2(5.0, -3.0), Vec2(4.0, 1.0), Vec2(7.0, 4.0), Vec2(3.0, 5.0), Vec2(0.0, 7.0),
  194. Vec2(-1.0, 3.0), Vec2(-4.0, 6.0), Vec2(-7.0, 8.0), Vec2(-5.0, 2.0)}};
  195. for(U i = 0; i < 16; ++i)
  196. {
  197. Vec2 texSize(1.0f / Vec2(F32(m_width), F32(m_height))); // Texel size
  198. texSize *= 2.0f; // Move it to NDC
  199. Vec2 S = SAMPLE_LOCS_16[i] / 8.0f; // In [-1, 1]
  200. Vec2 subSample = S * texSize; // In [-texSize, texSize]
  201. subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
  202. m_jitteredMats16x[i] = Mat4::getIdentity();
  203. m_jitteredMats16x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
  204. }
  205. static const Array<Vec2, 8> SAMPLE_LOCS_8 = {Vec2(-7.0, 1.0), Vec2(-5.0, -5.0), Vec2(-1.0, -3.0), Vec2(3.0, -7.0),
  206. Vec2(5.0, -1.0), Vec2(7.0, 7.0), Vec2(1.0, 3.0), Vec2(-3.0, 5.0)};
  207. for(U i = 0; i < 8; ++i)
  208. {
  209. Vec2 texSize(1.0f / Vec2(F32(m_width), F32(m_height))); // Texel size
  210. texSize *= 2.0f; // Move it to NDC
  211. Vec2 S = SAMPLE_LOCS_8[i] / 8.0f; // In [-1, 1]
  212. Vec2 subSample = S * texSize; // In [-texSize, texSize]
  213. subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
  214. m_jitteredMats8x[i] = Mat4::getIdentity();
  215. m_jitteredMats8x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
  216. }
  217. }
  218. Error Renderer::populateRenderGraph(RenderingContext& ctx)
  219. {
  220. ctx.m_matrices.m_cameraTransform = ctx.m_renderQueue->m_cameraTransform;
  221. ctx.m_matrices.m_view = ctx.m_renderQueue->m_viewMatrix;
  222. ctx.m_matrices.m_projection = ctx.m_renderQueue->m_projectionMatrix;
  223. ctx.m_matrices.m_viewProjection = ctx.m_renderQueue->m_viewProjectionMatrix;
  224. ctx.m_matrices.m_jitter = m_jitteredMats8x[m_frameCount & (m_jitteredMats8x.getSize() - 1)];
  225. ctx.m_matrices.m_projectionJitter = ctx.m_matrices.m_jitter * ctx.m_matrices.m_projection;
  226. ctx.m_matrices.m_viewProjectionJitter = ctx.m_matrices.m_projectionJitter * ctx.m_matrices.m_view;
  227. ctx.m_matrices.m_invertedViewProjectionJitter = ctx.m_matrices.m_viewProjectionJitter.getInverse();
  228. ctx.m_matrices.m_invertedViewProjection = ctx.m_matrices.m_viewProjection.getInverse();
  229. ctx.m_matrices.m_invertedProjectionJitter = ctx.m_matrices.m_projectionJitter.getInverse();
  230. ctx.m_matrices.m_invertedView = ctx.m_matrices.m_view.getInverse();
  231. ctx.m_matrices.m_unprojectionParameters = ctx.m_matrices.m_projection.extractPerspectiveUnprojectionParams();
  232. ctx.m_prevMatrices = m_prevMatrices;
  233. // Check if resources got loaded
  234. if(m_prevLoadRequestCount != m_resources->getLoadingRequestCount()
  235. || m_prevAsyncTasksCompleted != m_resources->getAsyncTaskCompletedCount())
  236. {
  237. m_prevLoadRequestCount = m_resources->getLoadingRequestCount();
  238. m_prevAsyncTasksCompleted = m_resources->getAsyncTaskCompletedCount();
  239. m_resourcesDirty = true;
  240. }
  241. else
  242. {
  243. m_resourcesDirty = false;
  244. }
  245. // Import RTs first
  246. m_downscale->importRenderTargets(ctx);
  247. m_tonemapping->importRenderTargets(ctx);
  248. m_depth->importRenderTargets(ctx);
  249. // Populate render graph. WARNING Watch the order
  250. m_genericCompute->populateRenderGraph(ctx);
  251. m_clusterBinning->populateRenderGraph(ctx);
  252. if(m_accelerationStructureBuilder)
  253. {
  254. m_accelerationStructureBuilder->populateRenderGraph(ctx);
  255. }
  256. m_shadowMapping->populateRenderGraph(ctx);
  257. m_gi->populateRenderGraph(ctx);
  258. m_probeReflections->populateRenderGraph(ctx);
  259. m_volLighting->populateRenderGraph(ctx);
  260. m_gbuffer->populateRenderGraph(ctx);
  261. m_motionVectors->populateRenderGraph(ctx);
  262. m_gbufferPost->populateRenderGraph(ctx);
  263. m_depth->populateRenderGraph(ctx);
  264. if(m_rtShadows)
  265. {
  266. m_rtShadows->populateRenderGraph(ctx);
  267. }
  268. else
  269. {
  270. m_smResolve->populateRenderGraph(ctx);
  271. }
  272. m_volFog->populateRenderGraph(ctx);
  273. m_ssao->populateRenderGraph(ctx);
  274. m_lensFlare->populateRenderGraph(ctx);
  275. m_ssr->populateRenderGraph(ctx);
  276. m_ssgi->populateRenderGraph(ctx);
  277. m_lightShading->populateRenderGraph(ctx);
  278. m_temporalAA->populateRenderGraph(ctx);
  279. m_downscale->populateRenderGraph(ctx);
  280. m_tonemapping->populateRenderGraph(ctx);
  281. m_bloom->populateRenderGraph(ctx);
  282. if(m_dbg->getEnabled())
  283. {
  284. m_dbg->populateRenderGraph(ctx);
  285. }
  286. m_finalComposite->populateRenderGraph(ctx);
  287. // Populate the uniforms
  288. m_clusterBinning->writeClusterBuffersAsync();
  289. return Error::NONE;
  290. }
  291. void Renderer::finalize(const RenderingContext& ctx)
  292. {
  293. ++m_frameCount;
  294. m_prevMatrices = ctx.m_matrices;
  295. // Inform about the HiZ map. Do it as late as possible
  296. if(ctx.m_renderQueue->m_fillCoverageBufferCallback)
  297. {
  298. F32* depthValues;
  299. U32 width;
  300. U32 height;
  301. m_depth->getClientDepthMapInfo(depthValues, width, height);
  302. ctx.m_renderQueue->m_fillCoverageBufferCallback(ctx.m_renderQueue->m_fillCoverageBufferCallbackUserData,
  303. depthValues, width, height);
  304. }
  305. }
  306. TextureInitInfo Renderer::create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage, CString name)
  307. {
  308. ANKI_ASSERT(!!(usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE)
  309. || !!(usage & TextureUsageBit::IMAGE_COMPUTE_WRITE));
  310. TextureInitInfo init(name);
  311. init.m_width = w;
  312. init.m_height = h;
  313. init.m_depth = 1;
  314. init.m_layerCount = 1;
  315. init.m_type = TextureType::_2D;
  316. init.m_format = format;
  317. init.m_mipmapCount = 1;
  318. init.m_samples = 1;
  319. init.m_usage = usage;
  320. return init;
  321. }
  322. RenderTargetDescription Renderer::create2DRenderTargetDescription(U32 w, U32 h, Format format, CString name)
  323. {
  324. RenderTargetDescription init(name);
  325. init.m_width = w;
  326. init.m_height = h;
  327. init.m_depth = 1;
  328. init.m_layerCount = 1;
  329. init.m_type = TextureType::_2D;
  330. init.m_format = format;
  331. init.m_mipmapCount = 1;
  332. init.m_samples = 1;
  333. init.m_usage = TextureUsageBit::NONE;
  334. return init;
  335. }
  336. TexturePtr Renderer::createAndClearRenderTarget(const TextureInitInfo& inf, const ClearValue& clearVal)
  337. {
  338. ANKI_ASSERT(!!(inf.m_usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE)
  339. || !!(inf.m_usage & TextureUsageBit::IMAGE_COMPUTE_WRITE));
  340. const U faceCount = (inf.m_type == TextureType::CUBE || inf.m_type == TextureType::CUBE_ARRAY) ? 6 : 1;
  341. Bool useCompute = false;
  342. if(!!(inf.m_usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE))
  343. {
  344. useCompute = false;
  345. }
  346. else if(!!(inf.m_usage & TextureUsageBit::IMAGE_COMPUTE_WRITE))
  347. {
  348. useCompute = true;
  349. }
  350. else
  351. {
  352. ANKI_ASSERT(!"Can't handle that");
  353. }
  354. // Create tex
  355. TexturePtr tex = m_gr->newTexture(inf);
  356. // Clear all surfaces
  357. CommandBufferInitInfo cmdbinit;
  358. cmdbinit.m_flags = CommandBufferFlag::GENERAL_WORK;
  359. if((inf.m_mipmapCount * faceCount * inf.m_layerCount * 4) < COMMAND_BUFFER_SMALL_BATCH_MAX_COMMANDS)
  360. {
  361. cmdbinit.m_flags |= CommandBufferFlag::SMALL_BATCH;
  362. }
  363. CommandBufferPtr cmdb = m_gr->newCommandBuffer(cmdbinit);
  364. for(U32 mip = 0; mip < inf.m_mipmapCount; ++mip)
  365. {
  366. for(U32 face = 0; face < faceCount; ++face)
  367. {
  368. for(U32 layer = 0; layer < inf.m_layerCount; ++layer)
  369. {
  370. TextureSurfaceInfo surf(mip, 0, face, layer);
  371. if(!useCompute)
  372. {
  373. FramebufferInitInfo fbInit("RendererClearRT");
  374. Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS> colUsage = {};
  375. TextureUsageBit dsUsage = TextureUsageBit::NONE;
  376. if(formatIsDepthStencil(inf.m_format))
  377. {
  378. DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE;
  379. if(formatIsDepth(inf.m_format))
  380. {
  381. aspect |= DepthStencilAspectBit::DEPTH;
  382. }
  383. if(formatIsStencil(inf.m_format))
  384. {
  385. aspect |= DepthStencilAspectBit::STENCIL;
  386. }
  387. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf, aspect));
  388. fbInit.m_depthStencilAttachment.m_textureView = view;
  389. fbInit.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::CLEAR;
  390. fbInit.m_depthStencilAttachment.m_stencilLoadOperation = AttachmentLoadOperation::CLEAR;
  391. fbInit.m_depthStencilAttachment.m_clearValue = clearVal;
  392. dsUsage = TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE;
  393. }
  394. else
  395. {
  396. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf));
  397. fbInit.m_colorAttachmentCount = 1;
  398. fbInit.m_colorAttachments[0].m_textureView = view;
  399. fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
  400. fbInit.m_colorAttachments[0].m_clearValue = clearVal;
  401. colUsage[0] = TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE;
  402. }
  403. FramebufferPtr fb = m_gr->newFramebuffer(fbInit);
  404. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::NONE,
  405. TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, surf);
  406. cmdb->beginRenderPass(fb, colUsage, dsUsage);
  407. cmdb->endRenderPass();
  408. if(!!inf.m_initialUsage)
  409. {
  410. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
  411. inf.m_initialUsage, surf);
  412. }
  413. }
  414. else
  415. {
  416. // Compute
  417. ShaderProgramResourceVariantInitInfo variantInitInfo(m_clearTexComputeProg);
  418. variantInitInfo.addMutation("TEXTURE_DIMENSIONS", I32((inf.m_type == TextureType::_3D) ? 3 : 2));
  419. const FormatInfo formatInfo = getFormatInfo(inf.m_format);
  420. I32 componentType = 0;
  421. if(formatInfo.m_shaderType == 0)
  422. {
  423. componentType = 0;
  424. }
  425. else if(formatInfo.m_shaderType == 1)
  426. {
  427. componentType = 1;
  428. }
  429. else
  430. {
  431. ANKI_ASSERT(!"Not supported");
  432. }
  433. variantInitInfo.addMutation("COMPONENT_TYPE", componentType);
  434. const ShaderProgramResourceVariant* variant;
  435. m_clearTexComputeProg->getOrCreateVariant(variantInitInfo, variant);
  436. cmdb->bindShaderProgram(variant->getProgram());
  437. cmdb->setPushConstants(&clearVal.m_colorf[0], sizeof(clearVal.m_colorf));
  438. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf));
  439. cmdb->bindImage(0, 0, view);
  440. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::NONE, TextureUsageBit::IMAGE_COMPUTE_WRITE,
  441. surf);
  442. UVec3 wgSize;
  443. wgSize.x() = (8 - 1 + (tex->getWidth() >> mip)) / 8;
  444. wgSize.y() = (8 - 1 + (tex->getHeight() >> mip)) / 8;
  445. wgSize.z() = (inf.m_type == TextureType::_3D) ? ((8 - 1 + (tex->getDepth() >> mip)) / 8) : 1;
  446. cmdb->dispatchCompute(wgSize.x(), wgSize.y(), wgSize.z());
  447. if(!!inf.m_initialUsage)
  448. {
  449. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::IMAGE_COMPUTE_WRITE, inf.m_initialUsage,
  450. surf);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. cmdb->flush();
  457. return tex;
  458. }
  459. void Renderer::registerDebugRenderTarget(RendererObject* obj, CString rtName)
  460. {
  461. #if ANKI_ENABLE_ASSERTIONS
  462. for(const DebugRtInfo& inf : m_debugRts)
  463. {
  464. ANKI_ASSERT(inf.m_rtName != rtName && "Choose different name");
  465. }
  466. #endif
  467. ANKI_ASSERT(obj);
  468. DebugRtInfo inf;
  469. inf.m_obj = obj;
  470. inf.m_rtName.create(getAllocator(), rtName);
  471. m_debugRts.emplaceBack(getAllocator(), std::move(inf));
  472. }
  473. void Renderer::getCurrentDebugRenderTarget(RenderTargetHandle& handle, Bool& handleValid,
  474. ShaderProgramPtr& optionalShaderProgram)
  475. {
  476. if(ANKI_LIKELY(m_currentDebugRtName.isEmpty()))
  477. {
  478. handleValid = false;
  479. return;
  480. }
  481. RendererObject* obj = nullptr;
  482. for(const DebugRtInfo& inf : m_debugRts)
  483. {
  484. if(inf.m_rtName == m_currentDebugRtName)
  485. {
  486. obj = inf.m_obj;
  487. }
  488. }
  489. ANKI_ASSERT(obj);
  490. obj->getDebugRenderTarget(m_currentDebugRtName, handle, optionalShaderProgram);
  491. handleValid = true;
  492. }
  493. void Renderer::setCurrentDebugRenderTarget(CString rtName)
  494. {
  495. m_currentDebugRtName.destroy(getAllocator());
  496. if(!rtName.isEmpty() && rtName.getLength() > 0)
  497. {
  498. m_currentDebugRtName.create(getAllocator(), rtName);
  499. }
  500. }
  501. } // end namespace anki