Dbg.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (C) 2009-2020, 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 probes
  82. if(threadId == 0)
  83. {
  84. for(const GlobalIlluminationProbeQueueElement& el : ctx.m_renderQueue->m_giProbes)
  85. {
  86. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  87. el.m_debugDrawCallback(dctx, a);
  88. }
  89. }
  90. // Draw lights
  91. if(threadId == 0)
  92. {
  93. U32 count = ctx.m_renderQueue->m_pointLights.getSize();
  94. while(count--)
  95. {
  96. const PointLightQueueElement& el = ctx.m_renderQueue->m_pointLights[count];
  97. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  98. el.m_debugDrawCallback(dctx, a);
  99. }
  100. for(const SpotLightQueueElement& el : ctx.m_renderQueue->m_spotLights)
  101. {
  102. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  103. el.m_debugDrawCallback(dctx, a);
  104. }
  105. }
  106. // Decals
  107. if(threadId == 0)
  108. {
  109. for(const DecalQueueElement& el : ctx.m_renderQueue->m_decals)
  110. {
  111. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  112. el.m_debugDrawCallback(dctx, a);
  113. }
  114. }
  115. // Reflection probes
  116. if(threadId == 0)
  117. {
  118. for(const ReflectionProbeQueueElement& el : ctx.m_renderQueue->m_reflectionProbes)
  119. {
  120. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  121. el.m_debugDrawCallback(dctx, a);
  122. }
  123. }
  124. // GI probes
  125. if(threadId == 0)
  126. {
  127. for(const GlobalIlluminationProbeQueueElement& el : ctx.m_renderQueue->m_giProbes)
  128. {
  129. Array<void*, 1> a = {const_cast<void*>(el.m_debugDrawCallbackUserData)};
  130. el.m_debugDrawCallback(dctx, a);
  131. }
  132. }
  133. }
  134. void Dbg::populateRenderGraph(RenderingContext& ctx)
  135. {
  136. if(!m_initialized)
  137. {
  138. if(lazyInit())
  139. {
  140. return;
  141. }
  142. m_initialized = true;
  143. }
  144. m_runCtx.m_ctx = &ctx;
  145. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  146. // Create RT
  147. m_runCtx.m_rt = rgraph.newRenderTarget(m_rtDescr);
  148. // Create pass
  149. GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("DBG");
  150. pass.setWork(
  151. [](RenderPassWorkContext& rgraphCtx) {
  152. Dbg* self = static_cast<Dbg*>(rgraphCtx.m_userData);
  153. self->run(rgraphCtx, *self->m_runCtx.m_ctx);
  154. },
  155. this, computeNumberOfSecondLevelCommandBuffers(ctx.m_renderQueue->m_renderables.getSize()));
  156. pass.setFramebufferInfo(m_fbDescr, {m_runCtx.m_rt}, m_r->getGBuffer().getDepthRt());
  157. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE});
  158. pass.newDependency({m_r->getGBuffer().getDepthRt(),
  159. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ});
  160. }
  161. } // end namespace anki