Dbg.cpp 5.7 KB

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