Bloom.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/Bloom.h>
  6. #include <AnKi/Renderer/DownscaleBlur.h>
  7. #include <AnKi/Renderer/FinalComposite.h>
  8. #include <AnKi/Renderer/Renderer.h>
  9. #include <AnKi/Renderer/Tonemapping.h>
  10. #include <AnKi/Core/ConfigSet.h>
  11. namespace anki {
  12. Bloom::Bloom(Renderer* r)
  13. : RendererObject(r)
  14. {
  15. registerDebugRenderTarget("Bloom");
  16. }
  17. Bloom::~Bloom()
  18. {
  19. }
  20. Error Bloom::initInternal()
  21. {
  22. ANKI_R_LOGV("Initializing bloom");
  23. ANKI_CHECK(initExposure());
  24. ANKI_CHECK(initUpscale());
  25. m_fbDescr.m_colorAttachmentCount = 1;
  26. m_fbDescr.bake();
  27. return Error::NONE;
  28. }
  29. Error Bloom::initExposure()
  30. {
  31. m_exposure.m_width = m_r->getDownscaleBlur().getPassWidth(MAX_U32) * 2;
  32. m_exposure.m_height = m_r->getDownscaleBlur().getPassHeight(MAX_U32) * 2;
  33. // Create RT info
  34. m_exposure.m_rtDescr =
  35. m_r->create2DRenderTargetDescription(m_exposure.m_width, m_exposure.m_height, RT_PIXEL_FORMAT, "Bloom Exp");
  36. m_exposure.m_rtDescr.bake();
  37. // init shaders
  38. ANKI_CHECK(getResourceManager().loadResource((getConfig().getRPreferCompute())
  39. ? "ShaderBinaries/BloomCompute.ankiprogbin"
  40. : "ShaderBinaries/BloomRaster.ankiprogbin",
  41. m_exposure.m_prog));
  42. ShaderProgramResourceVariantInitInfo variantInitInfo(m_exposure.m_prog);
  43. if(getConfig().getRPreferCompute())
  44. {
  45. variantInitInfo.addConstant("FB_SIZE", UVec2(m_exposure.m_width, m_exposure.m_height));
  46. }
  47. const ShaderProgramResourceVariant* variant;
  48. m_exposure.m_prog->getOrCreateVariant(variantInitInfo, variant);
  49. m_exposure.m_grProg = variant->getProgram();
  50. return Error::NONE;
  51. }
  52. Error Bloom::initUpscale()
  53. {
  54. m_upscale.m_width = m_r->getPostProcessResolution().x() / BLOOM_FRACTION;
  55. m_upscale.m_height = m_r->getPostProcessResolution().y() / BLOOM_FRACTION;
  56. // Create RT descr
  57. m_upscale.m_rtDescr =
  58. m_r->create2DRenderTargetDescription(m_upscale.m_width, m_upscale.m_height, RT_PIXEL_FORMAT, "Bloom Upscale");
  59. m_upscale.m_rtDescr.bake();
  60. // init shaders
  61. ANKI_CHECK(getResourceManager().loadResource((getConfig().getRPreferCompute())
  62. ? "ShaderBinaries/BloomUpscaleCompute.ankiprogbin"
  63. : "ShaderBinaries/BloomUpscaleRaster.ankiprogbin",
  64. m_upscale.m_prog));
  65. ShaderProgramResourceVariantInitInfo variantInitInfo(m_upscale.m_prog);
  66. variantInitInfo.addConstant("INPUT_TEX_SIZE", UVec2(m_exposure.m_width, m_exposure.m_height));
  67. if(getConfig().getRPreferCompute())
  68. {
  69. variantInitInfo.addConstant("FB_SIZE", UVec2(m_upscale.m_width, m_upscale.m_height));
  70. }
  71. const ShaderProgramResourceVariant* variant;
  72. m_upscale.m_prog->getOrCreateVariant(variantInitInfo, variant);
  73. m_upscale.m_grProg = variant->getProgram();
  74. // Textures
  75. ANKI_CHECK(getResourceManager().loadResource("EngineAssets/LensDirt.ankitex", m_upscale.m_lensDirtImage));
  76. return Error::NONE;
  77. }
  78. void Bloom::populateRenderGraph(RenderingContext& ctx)
  79. {
  80. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  81. const Bool preferCompute = getConfig().getRPreferCompute();
  82. // Main pass
  83. {
  84. // Ask for render target
  85. m_runCtx.m_exposureRt = rgraph.newRenderTarget(m_exposure.m_rtDescr);
  86. // Set the render pass
  87. TextureSubresourceInfo inputTexSubresource;
  88. inputTexSubresource.m_firstMipmap = m_r->getDownscaleBlur().getMipmapCount() - 1;
  89. RenderPassDescriptionBase* prpass;
  90. if(preferCompute)
  91. {
  92. ComputeRenderPassDescription& rpass = rgraph.newComputeRenderPass("Bloom Main");
  93. rpass.newDependency(RenderPassDependency(m_r->getDownscaleBlur().getRt(), TextureUsageBit::SAMPLED_COMPUTE,
  94. inputTexSubresource));
  95. rpass.newDependency(RenderPassDependency(m_runCtx.m_exposureRt, TextureUsageBit::IMAGE_COMPUTE_WRITE));
  96. prpass = &rpass;
  97. }
  98. else
  99. {
  100. GraphicsRenderPassDescription& rpass = rgraph.newGraphicsRenderPass("Bloom Main");
  101. rpass.setFramebufferInfo(m_fbDescr, {m_runCtx.m_exposureRt});
  102. rpass.newDependency(RenderPassDependency(m_r->getDownscaleBlur().getRt(), TextureUsageBit::SAMPLED_FRAGMENT,
  103. inputTexSubresource));
  104. rpass.newDependency(
  105. RenderPassDependency(m_runCtx.m_exposureRt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE));
  106. prpass = &rpass;
  107. }
  108. prpass->setWork([this](RenderPassWorkContext& rgraphCtx) {
  109. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  110. cmdb->bindShaderProgram(m_exposure.m_grProg);
  111. TextureSubresourceInfo inputTexSubresource;
  112. inputTexSubresource.m_firstMipmap = m_r->getDownscaleBlur().getMipmapCount() - 1;
  113. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  114. rgraphCtx.bindTexture(0, 1, m_r->getDownscaleBlur().getRt(), inputTexSubresource);
  115. const Vec4 uniforms(getConfig().getRBloomThreshold(), getConfig().getRBloomScale(), 0.0f, 0.0f);
  116. cmdb->setPushConstants(&uniforms, sizeof(uniforms));
  117. rgraphCtx.bindStorageBuffer(0, 2, m_r->getTonemapping().getAverageLuminanceBuffer());
  118. if(getConfig().getRPreferCompute())
  119. {
  120. rgraphCtx.bindImage(0, 3, m_runCtx.m_exposureRt, TextureSubresourceInfo());
  121. dispatchPPCompute(cmdb, m_workgroupSize[0], m_workgroupSize[1], m_exposure.m_width,
  122. m_exposure.m_height);
  123. }
  124. else
  125. {
  126. cmdb->setViewport(0, 0, m_exposure.m_width, m_exposure.m_height);
  127. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3);
  128. }
  129. });
  130. }
  131. // Upscale & SSLF pass
  132. {
  133. // Ask for render target
  134. m_runCtx.m_upscaleRt = rgraph.newRenderTarget(m_upscale.m_rtDescr);
  135. // Set the render pass
  136. RenderPassDescriptionBase* prpass;
  137. if(preferCompute)
  138. {
  139. ComputeRenderPassDescription& rpass = rgraph.newComputeRenderPass("Bloom Upscale");
  140. rpass.newDependency({m_runCtx.m_exposureRt, TextureUsageBit::SAMPLED_COMPUTE});
  141. rpass.newDependency({m_runCtx.m_upscaleRt, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  142. prpass = &rpass;
  143. }
  144. else
  145. {
  146. GraphicsRenderPassDescription& rpass = rgraph.newGraphicsRenderPass("Bloom Upscale");
  147. rpass.setFramebufferInfo(m_fbDescr, {m_runCtx.m_upscaleRt});
  148. rpass.newDependency({m_runCtx.m_exposureRt, TextureUsageBit::SAMPLED_FRAGMENT});
  149. rpass.newDependency({m_runCtx.m_upscaleRt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE});
  150. prpass = &rpass;
  151. }
  152. prpass->setWork([this](RenderPassWorkContext& rgraphCtx) {
  153. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  154. cmdb->bindShaderProgram(m_upscale.m_grProg);
  155. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  156. rgraphCtx.bindColorTexture(0, 1, m_runCtx.m_exposureRt);
  157. cmdb->bindTexture(0, 2, m_upscale.m_lensDirtImage->getTextureView());
  158. if(getConfig().getRPreferCompute())
  159. {
  160. rgraphCtx.bindImage(0, 3, m_runCtx.m_upscaleRt, TextureSubresourceInfo());
  161. dispatchPPCompute(cmdb, m_workgroupSize[0], m_workgroupSize[1], m_upscale.m_width, m_upscale.m_height);
  162. }
  163. else
  164. {
  165. cmdb->setViewport(0, 0, m_upscale.m_width, m_upscale.m_height);
  166. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3);
  167. }
  168. });
  169. }
  170. }
  171. void Bloom::getDebugRenderTarget([[maybe_unused]] CString rtName, RenderTargetHandle& handle,
  172. [[maybe_unused]] ShaderProgramPtr& optionalShaderProgram) const
  173. {
  174. ANKI_ASSERT(rtName == "Bloom");
  175. handle = m_runCtx.m_upscaleRt;
  176. }
  177. } // end namespace anki