Dbg.cpp 5.5 KB

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