GBufferPost.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009-2021, 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/GBufferPost.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/LightShading.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. namespace anki
  11. {
  12. GBufferPost::~GBufferPost()
  13. {
  14. }
  15. Error GBufferPost::init(const ConfigSet& cfg)
  16. {
  17. const Error err = initInternal(cfg);
  18. if(err)
  19. {
  20. ANKI_R_LOGE("Failed to initialize GBufferPost pass");
  21. }
  22. return err;
  23. }
  24. Error GBufferPost::initInternal(const ConfigSet& cfg)
  25. {
  26. ANKI_R_LOGI("Initializing GBufferPost pass");
  27. // Load shaders
  28. ANKI_CHECK(getResourceManager().loadResource("Shaders/GBufferPost.ankiprog", m_prog));
  29. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  30. variantInitInfo.addConstant("TILE_COUNTS", m_r->getTileCounts());
  31. variantInitInfo.addConstant("Z_SPLIT_COUNT", m_r->getZSplitCount());
  32. variantInitInfo.addConstant("TILE_SIZE", m_r->getTileSize());
  33. const ShaderProgramResourceVariant* variant;
  34. m_prog->getOrCreateVariant(variantInitInfo, variant);
  35. m_grProg = variant->getProgram();
  36. // Create FB descr
  37. m_fbDescr.m_colorAttachmentCount = 2;
  38. m_fbDescr.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::LOAD;
  39. m_fbDescr.m_colorAttachments[1].m_loadOperation = AttachmentLoadOperation::LOAD;
  40. m_fbDescr.bake();
  41. return Error::NONE;
  42. }
  43. void GBufferPost::populateRenderGraph(RenderingContext& ctx)
  44. {
  45. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  46. m_runCtx.m_ctx = &ctx;
  47. // Create pass
  48. GraphicsRenderPassDescription& rpass = rgraph.newGraphicsRenderPass("GBuffPost");
  49. rpass.setWork(
  50. [](RenderPassWorkContext& rgraphCtx) { static_cast<GBufferPost*>(rgraphCtx.m_userData)->run(rgraphCtx); }, this,
  51. 0);
  52. rpass.setFramebufferInfo(m_fbDescr, {m_r->getGBuffer().getColorRt(0), m_r->getGBuffer().getColorRt(1)}, {});
  53. rpass.newDependency(
  54. RenderPassDependency(m_r->getGBuffer().getColorRt(0), TextureUsageBit::ALL_FRAMEBUFFER_ATTACHMENT));
  55. rpass.newDependency(
  56. RenderPassDependency(m_r->getGBuffer().getColorRt(1), TextureUsageBit::ALL_FRAMEBUFFER_ATTACHMENT));
  57. rpass.newDependency(RenderPassDependency(m_r->getGBuffer().getDepthRt(), TextureUsageBit::SAMPLED_FRAGMENT,
  58. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH)));
  59. rpass.newDependency(
  60. RenderPassDependency(ctx.m_clusteredShading.m_clustersBufferHandle, BufferUsageBit::STORAGE_FRAGMENT_READ));
  61. }
  62. void GBufferPost::run(RenderPassWorkContext& rgraphCtx)
  63. {
  64. const RenderingContext& ctx = *m_runCtx.m_ctx;
  65. const ClusteredShadingContext& rsrc = ctx.m_clusteredShading;
  66. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  67. cmdb->setViewport(0, 0, m_r->getWidth(), m_r->getHeight());
  68. cmdb->bindShaderProgram(m_grProg);
  69. cmdb->setBlendFactors(0, BlendFactor::ONE, BlendFactor::SRC_ALPHA, BlendFactor::ZERO, BlendFactor::ONE);
  70. cmdb->setBlendFactors(1, BlendFactor::ONE, BlendFactor::SRC_ALPHA, BlendFactor::ZERO, BlendFactor::ONE);
  71. // Bind all
  72. cmdb->bindSampler(0, 0, m_r->getSamplers().m_nearestNearestClamp);
  73. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(), TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  74. cmdb->bindSampler(0, 2, m_r->getSamplers().m_trilinearRepeat);
  75. bindUniforms(cmdb, 0, 3, rsrc.m_clusteredShadingUniformsToken);
  76. bindUniforms(cmdb, 0, 4, rsrc.m_decalsToken);
  77. cmdb->bindTexture(0, 5,
  78. (rsrc.m_diffuseDecalTextureView) ? rsrc.m_diffuseDecalTextureView : m_r->getDummyTextureView2d(),
  79. TextureUsageBit::SAMPLED_FRAGMENT);
  80. cmdb->bindTexture(0, 6,
  81. (rsrc.m_specularRoughnessDecalTextureView) ? rsrc.m_specularRoughnessDecalTextureView
  82. : m_r->getDummyTextureView2d(),
  83. TextureUsageBit::SAMPLED_FRAGMENT);
  84. bindStorage(cmdb, 0, 7, rsrc.m_clustersToken);
  85. // Draw
  86. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3);
  87. // Restore state
  88. cmdb->setBlendFactors(0, BlendFactor::ONE, BlendFactor::ZERO);
  89. cmdb->setBlendFactors(1, BlendFactor::ONE, BlendFactor::ZERO);
  90. }
  91. } // end namespace anki