Bloom.cpp 6.9 KB

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