VrsSriGeneration.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (C) 2009-2023, 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/VrsSriGeneration.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/LightShading.h>
  8. #include <AnKi/Core/ConfigSet.h>
  9. namespace anki {
  10. VrsSriGeneration::VrsSriGeneration(Renderer* r)
  11. : RendererObject(r)
  12. {
  13. registerDebugRenderTarget("VrsSri");
  14. registerDebugRenderTarget("VrsSriDownscaled");
  15. }
  16. VrsSriGeneration::~VrsSriGeneration()
  17. {
  18. }
  19. Error VrsSriGeneration::init()
  20. {
  21. const Error err = initInternal();
  22. if(err)
  23. {
  24. ANKI_R_LOGE("Failed to initialize VRS SRI generation");
  25. }
  26. return err;
  27. }
  28. Error VrsSriGeneration::initInternal()
  29. {
  30. if(!getExternalSubsystems().m_grManager->getDeviceCapabilities().m_vrs)
  31. {
  32. return Error::kNone;
  33. }
  34. m_sriTexelDimension = getExternalSubsystems().m_grManager->getDeviceCapabilities().m_minShadingRateImageTexelSize;
  35. ANKI_ASSERT(m_sriTexelDimension == 8 || m_sriTexelDimension == 16);
  36. const UVec2 rez = (m_r->getInternalResolution() + m_sriTexelDimension - 1) / m_sriTexelDimension;
  37. ANKI_R_LOGV("Intializing VRS SRI generation. SRI resolution %ux%u", rez.x(), rez.y());
  38. // Create textures
  39. const TextureUsageBit texUsage =
  40. TextureUsageBit::kFramebufferShadingRate | TextureUsageBit::kImageComputeWrite | TextureUsageBit::kAllSampled;
  41. TextureInitInfo sriInitInfo =
  42. m_r->create2DRenderTargetInitInfo(rez.x(), rez.y(), Format::kR8_Uint, texUsage, "VrsSri");
  43. m_sriTex = m_r->createAndClearRenderTarget(sriInitInfo, TextureUsageBit::kFramebufferShadingRate);
  44. const UVec2 rezDownscaled = (m_r->getInternalResolution() / 2 + m_sriTexelDimension - 1) / m_sriTexelDimension;
  45. sriInitInfo = m_r->create2DRenderTargetInitInfo(rezDownscaled.x(), rezDownscaled.y(), Format::kR8_Uint, texUsage,
  46. "VrsSriDownscaled");
  47. m_downscaledSriTex = m_r->createAndClearRenderTarget(sriInitInfo, TextureUsageBit::kFramebufferShadingRate);
  48. // Load programs
  49. ANKI_CHECK(getExternalSubsystems().m_resourceManager->loadResource(
  50. "ShaderBinaries/VrsSriGenerationCompute.ankiprogbin", m_prog));
  51. ShaderProgramResourceVariantInitInfo variantInit(m_prog);
  52. variantInit.addMutation("SRI_TEXEL_DIMENSION", m_sriTexelDimension);
  53. if(m_sriTexelDimension == 16
  54. && getExternalSubsystems().m_grManager->getDeviceCapabilities().m_minSubgroupSize >= 32)
  55. {
  56. // Algorithm's workgroup size is 32, GPU's subgroup size is min 32 -> each workgroup has 1 subgroup -> No need
  57. // for shared mem
  58. variantInit.addMutation("SHARED_MEMORY", 0);
  59. }
  60. else if(m_sriTexelDimension == 8
  61. && getExternalSubsystems().m_grManager->getDeviceCapabilities().m_minSubgroupSize >= 16)
  62. {
  63. // Algorithm's workgroup size is 16, GPU's subgroup size is min 16 -> each workgroup has 1 subgroup -> No need
  64. // for shared mem
  65. variantInit.addMutation("SHARED_MEMORY", 0);
  66. }
  67. else
  68. {
  69. variantInit.addMutation("SHARED_MEMORY", 1);
  70. }
  71. variantInit.addMutation("LIMIT_RATE_TO_2X2", ConfigSet::getSingleton().getRVrsLimitTo2x2());
  72. const ShaderProgramResourceVariant* variant;
  73. m_prog->getOrCreateVariant(variantInit, variant);
  74. m_grProg = variant->getProgram();
  75. ANKI_CHECK(getExternalSubsystems().m_resourceManager->loadResource(
  76. "ShaderBinaries/VrsSriVisualizeRenderTarget.ankiprogbin", m_visualizeProg));
  77. m_visualizeProg->getOrCreateVariant(variant);
  78. m_visualizeGrProg = variant->getProgram();
  79. ANKI_CHECK(getExternalSubsystems().m_resourceManager->loadResource("ShaderBinaries/VrsSriDownscale.ankiprogbin",
  80. m_downscaleProg));
  81. m_downscaleProg->getOrCreateVariant(variant);
  82. m_downscaleGrProg = variant->getProgram();
  83. return Error::kNone;
  84. }
  85. void VrsSriGeneration::getDebugRenderTarget(CString rtName, Array<RenderTargetHandle, kMaxDebugRenderTargets>& handles,
  86. ShaderProgramPtr& optionalShaderProgram) const
  87. {
  88. if(rtName == "VrsSri")
  89. {
  90. handles[0] = m_runCtx.m_rt;
  91. }
  92. else
  93. {
  94. ANKI_ASSERT(rtName == "VrsSriDownscaled");
  95. handles[0] = m_runCtx.m_downscaledRt;
  96. }
  97. optionalShaderProgram = m_visualizeGrProg;
  98. }
  99. void VrsSriGeneration::importRenderTargets(RenderingContext& ctx)
  100. {
  101. const Bool enableVrs =
  102. getExternalSubsystems().m_grManager->getDeviceCapabilities().m_vrs && ConfigSet::getSingleton().getRVrs();
  103. if(!enableVrs)
  104. {
  105. return;
  106. }
  107. if(m_sriTexImportedOnce)
  108. {
  109. m_runCtx.m_rt = ctx.m_renderGraphDescr.importRenderTarget(m_sriTex);
  110. m_runCtx.m_downscaledRt = ctx.m_renderGraphDescr.importRenderTarget(m_downscaledSriTex);
  111. }
  112. else
  113. {
  114. m_runCtx.m_rt = ctx.m_renderGraphDescr.importRenderTarget(m_sriTex, TextureUsageBit::kFramebufferShadingRate);
  115. m_runCtx.m_downscaledRt =
  116. ctx.m_renderGraphDescr.importRenderTarget(m_downscaledSriTex, TextureUsageBit::kFramebufferShadingRate);
  117. m_sriTexImportedOnce = true;
  118. }
  119. }
  120. void VrsSriGeneration::populateRenderGraph(RenderingContext& ctx)
  121. {
  122. const Bool enableVrs =
  123. getExternalSubsystems().m_grManager->getDeviceCapabilities().m_vrs && ConfigSet::getSingleton().getRVrs();
  124. if(!enableVrs)
  125. {
  126. return;
  127. }
  128. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  129. // SRI generation
  130. {
  131. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("VRS SRI generation");
  132. pass.newTextureDependency(m_runCtx.m_rt, TextureUsageBit::kImageComputeWrite);
  133. pass.newTextureDependency(m_r->getLightShading().getRt(), TextureUsageBit::kSampledCompute);
  134. pass.setWork([this](RenderPassWorkContext& rgraphCtx) {
  135. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  136. cmdb->bindShaderProgram(m_grProg);
  137. rgraphCtx.bindColorTexture(0, 0, m_r->getLightShading().getRt());
  138. cmdb->bindSampler(0, 1, m_r->getSamplers().m_nearestNearestClamp);
  139. rgraphCtx.bindImage(0, 2, m_runCtx.m_rt);
  140. const Vec4 pc(1.0f / Vec2(m_r->getInternalResolution()), ConfigSet::getSingleton().getRVrsThreshold(),
  141. 0.0f);
  142. cmdb->setPushConstants(&pc, sizeof(pc));
  143. const U32 fakeWorkgroupSizeXorY = m_sriTexelDimension;
  144. dispatchPPCompute(cmdb, fakeWorkgroupSizeXorY, fakeWorkgroupSizeXorY, m_r->getInternalResolution().x(),
  145. m_r->getInternalResolution().y());
  146. });
  147. }
  148. // Downscale
  149. {
  150. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("VRS SRI downscale");
  151. pass.newTextureDependency(m_runCtx.m_rt, TextureUsageBit::kSampledCompute);
  152. pass.newTextureDependency(m_runCtx.m_downscaledRt, TextureUsageBit::kImageComputeWrite);
  153. pass.setWork([this](RenderPassWorkContext& rgraphCtx) {
  154. const UVec2 rezDownscaled =
  155. (m_r->getInternalResolution() / 2 + m_sriTexelDimension - 1) / m_sriTexelDimension;
  156. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  157. cmdb->bindShaderProgram(m_downscaleGrProg);
  158. rgraphCtx.bindColorTexture(0, 0, m_runCtx.m_rt);
  159. cmdb->bindSampler(0, 1, m_r->getSamplers().m_nearestNearestClamp);
  160. rgraphCtx.bindImage(0, 2, m_runCtx.m_downscaledRt);
  161. const Vec4 pc(1.0f / Vec2(rezDownscaled), 0.0f, 0.0f);
  162. cmdb->setPushConstants(&pc, sizeof(pc));
  163. dispatchPPCompute(cmdb, 8, 8, rezDownscaled.x(), rezDownscaled.y());
  164. });
  165. }
  166. }
  167. } // end namespace anki