Dbg.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/Dbg.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/LightShading.h>
  9. #include <AnKi/Renderer/FinalComposite.h>
  10. #include <AnKi/Renderer/RenderQueue.h>
  11. #include <AnKi/Scene.h>
  12. #include <AnKi/Util/Logger.h>
  13. #include <AnKi/Util/Enum.h>
  14. #include <AnKi/Core/ConfigSet.h>
  15. #include <AnKi/Collision/ConvexHullShape.h>
  16. namespace anki {
  17. Dbg::Dbg(Renderer* r)
  18. : RendererObject(r)
  19. {
  20. }
  21. Dbg::~Dbg()
  22. {
  23. }
  24. Error Dbg::init(const ConfigSet& initializer)
  25. {
  26. m_enabled = initializer.getBool("r_dbgEnabled");
  27. return Error::NONE;
  28. }
  29. Error Dbg::lazyInit()
  30. {
  31. ANKI_ASSERT(!m_initialized);
  32. // RT descr
  33. m_rtDescr = m_r->create2DRenderTargetDescription(m_r->getInternalResolution().x(), m_r->getInternalResolution().y(),
  34. DBG_COLOR_ATTACHMENT_PIXEL_FORMAT, "Dbg");
  35. m_rtDescr.bake();
  36. // Create FB descr
  37. m_fbDescr.m_colorAttachmentCount = 1;
  38. m_fbDescr.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
  39. m_fbDescr.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::LOAD;
  40. m_fbDescr.m_depthStencilAttachment.m_stencilLoadOperation = AttachmentLoadOperation::DONT_CARE;
  41. m_fbDescr.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::DEPTH;
  42. m_fbDescr.bake();
  43. return Error::NONE;
  44. }
  45. void Dbg::run(RenderPassWorkContext& rgraphCtx, const RenderingContext& ctx)
  46. {
  47. ANKI_ASSERT(m_enabled);
  48. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  49. // Set common state
  50. cmdb->setViewport(0, 0, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
  51. cmdb->setDepthWrite(false);
  52. cmdb->bindSampler(0, 0, m_r->getSamplers().m_nearestNearestClamp);
  53. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(), TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  54. cmdb->setBlendFactors(0, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA);
  55. // Set the context
  56. RenderQueueDrawContext dctx;
  57. dctx.m_viewMatrix = ctx.m_renderQueue->m_viewMatrix;
  58. dctx.m_viewProjectionMatrix = ctx.m_renderQueue->m_viewProjectionMatrix;
  59. dctx.m_projectionMatrix = ctx.m_renderQueue->m_projectionMatrix;
  60. dctx.m_cameraTransform = ctx.m_renderQueue->m_viewMatrix.getInverse();
  61. dctx.m_stagingGpuAllocator = &m_r->getStagingGpuMemoryManager();
  62. dctx.m_frameAllocator = ctx.m_tempAllocator;
  63. dctx.m_commandBuffer = cmdb;
  64. dctx.m_sampler = m_r->getSamplers().m_trilinearRepeatAniso;
  65. dctx.m_key = RenderingKey(Pass::FS, 0, 1, false, false);
  66. dctx.m_debugDraw = true;
  67. dctx.m_debugDrawFlags = m_debugDrawFlags;
  68. // Draw renderables
  69. const U32 threadId = rgraphCtx.m_currentSecondLevelCommandBufferIndex;
  70. const U32 threadCount = rgraphCtx.m_secondLevelCommandBufferCount;
  71. const U32 problemSize = ctx.m_renderQueue->m_renderables.getSize();
  72. U32 start, end;
  73. splitThreadedProblem(threadId, threadCount, problemSize, start, end);
  74. for(U32 i = start; i < end; ++i)
  75. {
  76. const RenderableQueueElement& el = ctx.m_renderQueue->m_renderables[i];
  77. Array<void*, 1> a = {const_cast<void*>(el.m_userData)};
  78. el.m_callback(dctx, a);
  79. }
  80. // Draw forward shaded
  81. if(threadId == 0)
  82. {
  83. for(const RenderableQueueElement& el : ctx.m_renderQueue->m_forwardShadingRenderables)
  84. {
  85. Array<void*, 1> a = {const_cast<void*>(el.m_userData)};
  86. el.m_callback(dctx, a);
  87. }
  88. }
  89. // Draw probes
  90. if(threadId == 0)
  91. {
  92. for(const GlobalIlluminationProbeQueueElement& el : ctx.m_renderQueue->m_giProbes)
  93. {
  94. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  95. el.m_debugDrawCallback(dctx, a);
  96. }
  97. }
  98. // Draw lights
  99. if(threadId == 0)
  100. {
  101. U32 count = ctx.m_renderQueue->m_pointLights.getSize();
  102. while(count--)
  103. {
  104. const PointLightQueueElement& el = ctx.m_renderQueue->m_pointLights[count];
  105. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  106. el.m_debugDrawCallback(dctx, a);
  107. }
  108. for(const SpotLightQueueElement& el : ctx.m_renderQueue->m_spotLights)
  109. {
  110. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  111. el.m_debugDrawCallback(dctx, a);
  112. }
  113. }
  114. // Decals
  115. if(threadId == 0)
  116. {
  117. for(const DecalQueueElement& el : ctx.m_renderQueue->m_decals)
  118. {
  119. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  120. el.m_debugDrawCallback(dctx, a);
  121. }
  122. }
  123. // Reflection probes
  124. if(threadId == 0)
  125. {
  126. for(const ReflectionProbeQueueElement& el : ctx.m_renderQueue->m_reflectionProbes)
  127. {
  128. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  129. el.m_debugDrawCallback(dctx, a);
  130. }
  131. }
  132. // GI probes
  133. if(threadId == 0)
  134. {
  135. for(const GlobalIlluminationProbeQueueElement& el : ctx.m_renderQueue->m_giProbes)
  136. {
  137. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  138. el.m_debugDrawCallback(dctx, a);
  139. }
  140. }
  141. }
  142. void Dbg::populateRenderGraph(RenderingContext& ctx)
  143. {
  144. if(!m_initialized)
  145. {
  146. if(lazyInit())
  147. {
  148. return;
  149. }
  150. m_initialized = true;
  151. }
  152. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  153. // Create RT
  154. m_runCtx.m_rt = rgraph.newRenderTarget(m_rtDescr);
  155. // Create pass
  156. GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("DBG");
  157. pass.setWork(computeNumberOfSecondLevelCommandBuffers(ctx.m_renderQueue->m_renderables.getSize()),
  158. [this, &ctx](RenderPassWorkContext& rgraphCtx) {
  159. run(rgraphCtx, ctx);
  160. });
  161. pass.setFramebufferInfo(m_fbDescr, {m_runCtx.m_rt}, m_r->getGBuffer().getDepthRt());
  162. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE});
  163. pass.newDependency({m_r->getGBuffer().getDepthRt(),
  164. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ});
  165. }
  166. } // end namespace anki