Renderer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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_scale.reset(m_alloc.newInstance<Scale>(this));
  130. ANKI_CHECK(m_scale->init());
  131. m_gbuffer.reset(m_alloc.newInstance<GBuffer>(this));
  132. ANKI_CHECK(m_gbuffer->init());
  133. m_gbufferPost.reset(m_alloc.newInstance<GBufferPost>(this));
  134. ANKI_CHECK(m_gbufferPost->init());
  135. m_shadowMapping.reset(m_alloc.newInstance<ShadowMapping>(this));
  136. ANKI_CHECK(m_shadowMapping->init());
  137. m_volumetricFog.reset(m_alloc.newInstance<VolumetricFog>(this));
  138. ANKI_CHECK(m_volumetricFog->init());
  139. m_lightShading.reset(m_alloc.newInstance<LightShading>(this));
  140. ANKI_CHECK(m_lightShading->init());
  141. m_depthDownscale.reset(m_alloc.newInstance<DepthDownscale>(this));
  142. ANKI_CHECK(m_depthDownscale->init());
  143. m_forwardShading.reset(m_alloc.newInstance<ForwardShading>(this));
  144. ANKI_CHECK(m_forwardShading->init());
  145. m_lensFlare.reset(m_alloc.newInstance<LensFlare>(this));
  146. ANKI_CHECK(m_lensFlare->init());
  147. m_downscaleBlur.reset(getAllocator().newInstance<DownscaleBlur>(this));
  148. ANKI_CHECK(m_downscaleBlur->init());
  149. m_indirectSpecular.reset(m_alloc.newInstance<IndirectSpecular>(this));
  150. ANKI_CHECK(m_indirectSpecular->init());
  151. m_tonemapping.reset(getAllocator().newInstance<Tonemapping>(this));
  152. ANKI_CHECK(m_tonemapping->init());
  153. m_temporalAA.reset(getAllocator().newInstance<TemporalAA>(this));
  154. ANKI_CHECK(m_temporalAA->init());
  155. m_bloom.reset(m_alloc.newInstance<Bloom>(this));
  156. ANKI_CHECK(m_bloom->init());
  157. m_finalComposite.reset(m_alloc.newInstance<FinalComposite>(this));
  158. ANKI_CHECK(m_finalComposite->init());
  159. m_dbg.reset(m_alloc.newInstance<Dbg>(this));
  160. ANKI_CHECK(m_dbg->init());
  161. m_uiStage.reset(m_alloc.newInstance<UiStage>(this));
  162. ANKI_CHECK(m_uiStage->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. F32 scalingMipBias = log2(F32(m_internalResolution.x()) / F32(m_postProcessResolution.x()));
  196. if(getScale().getUsingGrUpscaler())
  197. {
  198. // DLSS wants more bias
  199. scalingMipBias -= 1.0f;
  200. }
  201. sinit.m_lodBias = scalingMipBias;
  202. m_samplers.m_trilinearRepeatAnisoResolutionScalingBias = m_gr->newSampler(sinit);
  203. }
  204. initJitteredMats();
  205. return Error::NONE;
  206. }
  207. void Renderer::initJitteredMats()
  208. {
  209. static const Array<Vec2, 16> SAMPLE_LOCS_16 = {
  210. {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),
  211. 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),
  212. Vec2(-1.0, 3.0), Vec2(-4.0, 6.0), Vec2(-7.0, 8.0), Vec2(-5.0, 2.0)}};
  213. for(U i = 0; i < 16; ++i)
  214. {
  215. Vec2 texSize(1.0f / Vec2(F32(m_internalResolution.x()), F32(m_internalResolution.y()))); // Texel size
  216. texSize *= 2.0f; // Move it to NDC
  217. Vec2 S = SAMPLE_LOCS_16[i] / 8.0f; // In [-1, 1]
  218. Vec2 subSample = S * texSize; // In [-texSize, texSize]
  219. subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
  220. m_jitteredMats16x[i] = Mat4::getIdentity();
  221. m_jitteredMats16x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
  222. }
  223. 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),
  224. Vec2(5.0, -1.0), Vec2(7.0, 7.0), Vec2(1.0, 3.0), Vec2(-3.0, 5.0)};
  225. for(U i = 0; i < 8; ++i)
  226. {
  227. Vec2 texSize(1.0f / Vec2(F32(m_internalResolution.x()), F32(m_internalResolution.y()))); // Texel size
  228. texSize *= 2.0f; // Move it to NDC
  229. Vec2 S = SAMPLE_LOCS_8[i] / 8.0f; // In [-1, 1]
  230. Vec2 subSample = S * texSize; // In [-texSize, texSize]
  231. subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
  232. m_jitteredMats8x[i] = Mat4::getIdentity();
  233. m_jitteredMats8x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
  234. }
  235. }
  236. Error Renderer::populateRenderGraph(RenderingContext& ctx)
  237. {
  238. ctx.m_prevMatrices = m_prevMatrices;
  239. ctx.m_matrices.m_cameraTransform = ctx.m_renderQueue->m_cameraTransform;
  240. ctx.m_matrices.m_view = ctx.m_renderQueue->m_viewMatrix;
  241. ctx.m_matrices.m_projection = ctx.m_renderQueue->m_projectionMatrix;
  242. ctx.m_matrices.m_viewProjection = ctx.m_renderQueue->m_viewProjectionMatrix;
  243. ctx.m_matrices.m_jitter = m_jitteredMats8x[m_frameCount & (m_jitteredMats8x.getSize() - 1)];
  244. ctx.m_matrices.m_projectionJitter = ctx.m_matrices.m_jitter * ctx.m_matrices.m_projection;
  245. ctx.m_matrices.m_viewProjectionJitter =
  246. ctx.m_matrices.m_projectionJitter * Mat4(ctx.m_matrices.m_view, Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  247. ctx.m_matrices.m_invertedViewProjectionJitter = ctx.m_matrices.m_viewProjectionJitter.getInverse();
  248. ctx.m_matrices.m_invertedViewProjection = ctx.m_matrices.m_viewProjection.getInverse();
  249. ctx.m_matrices.m_invertedProjectionJitter = ctx.m_matrices.m_projectionJitter.getInverse();
  250. ctx.m_matrices.m_reprojection =
  251. ctx.m_matrices.m_jitter * ctx.m_prevMatrices.m_viewProjection * ctx.m_matrices.m_invertedViewProjectionJitter;
  252. ctx.m_matrices.m_unprojectionParameters = ctx.m_matrices.m_projection.extractPerspectiveUnprojectionParams();
  253. // Check if resources got loaded
  254. if(m_prevLoadRequestCount != m_resources->getLoadingRequestCount()
  255. || m_prevAsyncTasksCompleted != m_resources->getAsyncTaskCompletedCount())
  256. {
  257. m_prevLoadRequestCount = m_resources->getLoadingRequestCount();
  258. m_prevAsyncTasksCompleted = m_resources->getAsyncTaskCompletedCount();
  259. m_resourcesDirty = true;
  260. }
  261. else
  262. {
  263. m_resourcesDirty = false;
  264. }
  265. // Import RTs first
  266. m_downscaleBlur->importRenderTargets(ctx);
  267. m_tonemapping->importRenderTargets(ctx);
  268. m_depthDownscale->importRenderTargets(ctx);
  269. m_vrsSriGeneration->importRenderTargets(ctx);
  270. // Populate render graph. WARNING Watch the order
  271. m_genericCompute->populateRenderGraph(ctx);
  272. m_clusterBinning->populateRenderGraph(ctx);
  273. if(m_accelerationStructureBuilder)
  274. {
  275. m_accelerationStructureBuilder->populateRenderGraph(ctx);
  276. }
  277. m_shadowMapping->populateRenderGraph(ctx);
  278. m_indirectDiffuseProbes->populateRenderGraph(ctx);
  279. m_probeReflections->populateRenderGraph(ctx);
  280. m_volumetricLightingAccumulation->populateRenderGraph(ctx);
  281. m_gbuffer->populateRenderGraph(ctx);
  282. m_motionVectors->populateRenderGraph(ctx);
  283. m_gbufferPost->populateRenderGraph(ctx);
  284. m_depthDownscale->populateRenderGraph(ctx);
  285. if(m_rtShadows)
  286. {
  287. m_rtShadows->populateRenderGraph(ctx);
  288. }
  289. else
  290. {
  291. m_shadowmapsResolve->populateRenderGraph(ctx);
  292. }
  293. m_volumetricFog->populateRenderGraph(ctx);
  294. m_lensFlare->populateRenderGraph(ctx);
  295. m_indirectSpecular->populateRenderGraph(ctx);
  296. m_indirectDiffuse->populateRenderGraph(ctx);
  297. m_lightShading->populateRenderGraph(ctx);
  298. if(!getScale().getUsingGrUpscaler())
  299. {
  300. m_temporalAA->populateRenderGraph(ctx);
  301. }
  302. m_vrsSriGeneration->populateRenderGraph(ctx);
  303. m_scale->populateRenderGraph(ctx);
  304. m_downscaleBlur->populateRenderGraph(ctx);
  305. m_tonemapping->populateRenderGraph(ctx);
  306. m_bloom->populateRenderGraph(ctx);
  307. m_dbg->populateRenderGraph(ctx);
  308. m_finalComposite->populateRenderGraph(ctx);
  309. // Populate the uniforms
  310. m_clusterBinning->writeClusterBuffersAsync();
  311. return Error::NONE;
  312. }
  313. void Renderer::finalize(const RenderingContext& ctx)
  314. {
  315. ++m_frameCount;
  316. m_prevMatrices = ctx.m_matrices;
  317. // Inform about the HiZ map. Do it as late as possible
  318. if(ctx.m_renderQueue->m_fillCoverageBufferCallback)
  319. {
  320. F32* depthValues;
  321. U32 width;
  322. U32 height;
  323. m_depthDownscale->getClientDepthMapInfo(depthValues, width, height);
  324. ctx.m_renderQueue->m_fillCoverageBufferCallback(ctx.m_renderQueue->m_fillCoverageBufferCallbackUserData,
  325. depthValues, width, height);
  326. }
  327. }
  328. TextureInitInfo Renderer::create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage, CString name)
  329. {
  330. ANKI_ASSERT(!!(usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE)
  331. || !!(usage & TextureUsageBit::IMAGE_COMPUTE_WRITE));
  332. TextureInitInfo init(name);
  333. init.m_width = w;
  334. init.m_height = h;
  335. init.m_depth = 1;
  336. init.m_layerCount = 1;
  337. init.m_type = TextureType::_2D;
  338. init.m_format = format;
  339. init.m_mipmapCount = 1;
  340. init.m_samples = 1;
  341. init.m_usage = usage;
  342. return init;
  343. }
  344. RenderTargetDescription Renderer::create2DRenderTargetDescription(U32 w, U32 h, Format format, CString name)
  345. {
  346. RenderTargetDescription init(name);
  347. init.m_width = w;
  348. init.m_height = h;
  349. init.m_depth = 1;
  350. init.m_layerCount = 1;
  351. init.m_type = TextureType::_2D;
  352. init.m_format = format;
  353. init.m_mipmapCount = 1;
  354. init.m_samples = 1;
  355. init.m_usage = TextureUsageBit::NONE;
  356. return init;
  357. }
  358. TexturePtr Renderer::createAndClearRenderTarget(const TextureInitInfo& inf, TextureUsageBit initialUsage,
  359. const ClearValue& clearVal)
  360. {
  361. ANKI_ASSERT(!!(inf.m_usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE)
  362. || !!(inf.m_usage & TextureUsageBit::IMAGE_COMPUTE_WRITE));
  363. const U faceCount = (inf.m_type == TextureType::CUBE || inf.m_type == TextureType::CUBE_ARRAY) ? 6 : 1;
  364. Bool useCompute = false;
  365. if(!!(inf.m_usage & TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE))
  366. {
  367. useCompute = false;
  368. }
  369. else if(!!(inf.m_usage & TextureUsageBit::IMAGE_COMPUTE_WRITE))
  370. {
  371. useCompute = true;
  372. }
  373. else
  374. {
  375. ANKI_ASSERT(!"Can't handle that");
  376. }
  377. // Create tex
  378. TexturePtr tex = m_gr->newTexture(inf);
  379. // Clear all surfaces
  380. CommandBufferInitInfo cmdbinit;
  381. cmdbinit.m_flags = CommandBufferFlag::GENERAL_WORK;
  382. if((inf.m_mipmapCount * faceCount * inf.m_layerCount * 4) < COMMAND_BUFFER_SMALL_BATCH_MAX_COMMANDS)
  383. {
  384. cmdbinit.m_flags |= CommandBufferFlag::SMALL_BATCH;
  385. }
  386. CommandBufferPtr cmdb = m_gr->newCommandBuffer(cmdbinit);
  387. for(U32 mip = 0; mip < inf.m_mipmapCount; ++mip)
  388. {
  389. for(U32 face = 0; face < faceCount; ++face)
  390. {
  391. for(U32 layer = 0; layer < inf.m_layerCount; ++layer)
  392. {
  393. TextureSurfaceInfo surf(mip, 0, face, layer);
  394. if(!useCompute)
  395. {
  396. FramebufferInitInfo fbInit("RendererClearRT");
  397. Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS> colUsage = {};
  398. TextureUsageBit dsUsage = TextureUsageBit::NONE;
  399. if(getFormatInfo(inf.m_format).isDepthStencil())
  400. {
  401. DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE;
  402. if(getFormatInfo(inf.m_format).isDepth())
  403. {
  404. aspect |= DepthStencilAspectBit::DEPTH;
  405. }
  406. if(getFormatInfo(inf.m_format).isStencil())
  407. {
  408. aspect |= DepthStencilAspectBit::STENCIL;
  409. }
  410. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf, aspect));
  411. fbInit.m_depthStencilAttachment.m_textureView = view;
  412. fbInit.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::CLEAR;
  413. fbInit.m_depthStencilAttachment.m_stencilLoadOperation = AttachmentLoadOperation::CLEAR;
  414. fbInit.m_depthStencilAttachment.m_clearValue = clearVal;
  415. dsUsage = TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE;
  416. }
  417. else
  418. {
  419. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf));
  420. fbInit.m_colorAttachmentCount = 1;
  421. fbInit.m_colorAttachments[0].m_textureView = view;
  422. fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
  423. fbInit.m_colorAttachments[0].m_clearValue = clearVal;
  424. colUsage[0] = TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE;
  425. }
  426. FramebufferPtr fb = m_gr->newFramebuffer(fbInit);
  427. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::NONE,
  428. TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, surf);
  429. cmdb->beginRenderPass(fb, colUsage, dsUsage);
  430. cmdb->endRenderPass();
  431. if(!!initialUsage)
  432. {
  433. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, initialUsage,
  434. surf);
  435. }
  436. }
  437. else
  438. {
  439. // Compute
  440. ShaderProgramResourceVariantInitInfo variantInitInfo(m_clearTexComputeProg);
  441. variantInitInfo.addMutation("TEXTURE_DIMENSIONS", I32((inf.m_type == TextureType::_3D) ? 3 : 2));
  442. const FormatInfo formatInfo = getFormatInfo(inf.m_format);
  443. I32 componentType = 0;
  444. if(formatInfo.m_shaderType == 0)
  445. {
  446. componentType = 0;
  447. }
  448. else if(formatInfo.m_shaderType == 1)
  449. {
  450. componentType = 1;
  451. }
  452. else
  453. {
  454. ANKI_ASSERT(!"Not supported");
  455. }
  456. variantInitInfo.addMutation("COMPONENT_TYPE", componentType);
  457. const ShaderProgramResourceVariant* variant;
  458. m_clearTexComputeProg->getOrCreateVariant(variantInitInfo, variant);
  459. cmdb->bindShaderProgram(variant->getProgram());
  460. cmdb->setPushConstants(&clearVal.m_colorf[0], sizeof(clearVal.m_colorf));
  461. TextureViewPtr view = getGrManager().newTextureView(TextureViewInitInfo(tex, surf));
  462. cmdb->bindImage(0, 0, view);
  463. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::NONE, TextureUsageBit::IMAGE_COMPUTE_WRITE,
  464. surf);
  465. UVec3 wgSize;
  466. wgSize.x() = (8 - 1 + (tex->getWidth() >> mip)) / 8;
  467. wgSize.y() = (8 - 1 + (tex->getHeight() >> mip)) / 8;
  468. wgSize.z() = (inf.m_type == TextureType::_3D) ? ((8 - 1 + (tex->getDepth() >> mip)) / 8) : 1;
  469. cmdb->dispatchCompute(wgSize.x(), wgSize.y(), wgSize.z());
  470. if(!!initialUsage)
  471. {
  472. cmdb->setTextureSurfaceBarrier(tex, TextureUsageBit::IMAGE_COMPUTE_WRITE, initialUsage, surf);
  473. }
  474. }
  475. }
  476. }
  477. }
  478. cmdb->flush();
  479. return tex;
  480. }
  481. void Renderer::registerDebugRenderTarget(RendererObject* obj, CString rtName)
  482. {
  483. #if ANKI_ENABLE_ASSERTIONS
  484. for(const DebugRtInfo& inf : m_debugRts)
  485. {
  486. ANKI_ASSERT(inf.m_rtName != rtName && "Choose different name");
  487. }
  488. #endif
  489. ANKI_ASSERT(obj);
  490. DebugRtInfo inf;
  491. inf.m_obj = obj;
  492. inf.m_rtName.create(getAllocator(), rtName);
  493. m_debugRts.emplaceBack(getAllocator(), std::move(inf));
  494. }
  495. void Renderer::getCurrentDebugRenderTarget(RenderTargetHandle& handle, Bool& handleValid,
  496. ShaderProgramPtr& optionalShaderProgram)
  497. {
  498. if(ANKI_LIKELY(m_currentDebugRtName.isEmpty()))
  499. {
  500. handleValid = false;
  501. return;
  502. }
  503. RendererObject* obj = nullptr;
  504. for(const DebugRtInfo& inf : m_debugRts)
  505. {
  506. if(inf.m_rtName == m_currentDebugRtName)
  507. {
  508. obj = inf.m_obj;
  509. }
  510. }
  511. ANKI_ASSERT(obj);
  512. obj->getDebugRenderTarget(m_currentDebugRtName, handle, optionalShaderProgram);
  513. handleValid = true;
  514. }
  515. void Renderer::setCurrentDebugRenderTarget(CString rtName)
  516. {
  517. m_currentDebugRtName.destroy(getAllocator());
  518. if(!rtName.isEmpty() && rtName.getLength() > 0)
  519. {
  520. m_currentDebugRtName.create(getAllocator(), rtName);
  521. }
  522. }
  523. Format Renderer::getHdrFormat() const
  524. {
  525. Format out;
  526. if(!m_config->getRHighQualityHdr())
  527. {
  528. out = Format::B10G11R11_UFLOAT_PACK32;
  529. }
  530. else if(m_gr->getDeviceCapabilities().m_unalignedBbpTextureFormats)
  531. {
  532. out = Format::R16G16B16_SFLOAT;
  533. }
  534. else
  535. {
  536. out = Format::R16G16B16A16_SFLOAT;
  537. }
  538. return out;
  539. }
  540. Format Renderer::getDepthNoStencilFormat() const
  541. {
  542. if(ANKI_PLATFORM_MOBILE)
  543. {
  544. return Format::X8_D24_UNORM_PACK32;
  545. }
  546. else
  547. {
  548. return Format::D32_SFLOAT;
  549. }
  550. }
  551. } // end namespace anki