Renderer.cpp 21 KB

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