Dbg.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Renderer/RendererObject.h>
  7. #include <AnKi/Renderer/Utils/Readback.h>
  8. #include <AnKi/Gr.h>
  9. #include <AnKi/Util/Enum.h>
  10. namespace anki {
  11. enum class DbgOption : U8
  12. {
  13. kNone = 0,
  14. // Flags that draw something somewhere
  15. kBoundingBoxes = 1 << 0,
  16. kIcons = 1 << 1,
  17. kPhysics = 1 << 2,
  18. kObjectPicking = 1 << 3,
  19. kSelectedObjectOutline = 1 << 4,
  20. // Flags that affect how things are drawn
  21. kDepthTest = 1 << 5,
  22. // Agregate flags
  23. kGatherAabbs = kBoundingBoxes | kObjectPicking,
  24. kDbgScene = kBoundingBoxes | kIcons | kPhysics | kSelectedObjectOutline,
  25. kDbgEnabled = kDbgScene | kObjectPicking | kSelectedObjectOutline,
  26. };
  27. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(DbgOption)
  28. class DbgObjectPickingResult
  29. {
  30. public:
  31. U32 m_sceneNodeUuid = 0;
  32. U32 m_translationAxis = kMaxU32; // Can be 0, 1 or 2
  33. U32 m_scaleAxis = kMaxU32;
  34. U32 m_rotationAxis = kMaxU32;
  35. Bool operator==(const DbgObjectPickingResult&) const = default;
  36. Bool isValid() const
  37. {
  38. return *this != DbgObjectPickingResult();
  39. }
  40. };
  41. // Debugging visualization of the scene.
  42. class Dbg : public RendererObject
  43. {
  44. public:
  45. Dbg();
  46. ~Dbg();
  47. Error init();
  48. void populateRenderGraph(RenderingContext& ctx);
  49. RenderTargetHandle getRt() const
  50. {
  51. return m_runCtx.m_rt;
  52. }
  53. void setOptions(DbgOption options)
  54. {
  55. m_options = options;
  56. }
  57. void enableOptions(DbgOption options)
  58. {
  59. m_options |= options;
  60. }
  61. DbgOption getOptions() const
  62. {
  63. return m_options;
  64. }
  65. Bool isEnabled() const
  66. {
  67. return !!(m_options & DbgOption::kDbgEnabled);
  68. }
  69. const DbgObjectPickingResult& getObjectPickingResultAtMousePosition() const
  70. {
  71. ANKI_ASSERT(!!(m_options & DbgOption::kObjectPicking));
  72. return m_runCtx.m_objPickingRes;
  73. }
  74. void enableGizmos(const Transform& trf, Bool enableGizmos)
  75. {
  76. m_gizmos.m_trf = Mat3x4(trf.getOrigin().xyz, trf.getRotation().getRotationPart());
  77. m_gizmos.m_enabled = enableGizmos;
  78. }
  79. // Draw a debug solid cube in world space
  80. void enableDebugCube(Vec3 pos, F32 size, Vec4 color, Bool enable, Bool enableDepthTesting = true)
  81. {
  82. ANKI_ASSERT(size > kEpsilonf);
  83. m_debugPoint.m_position = (enable) ? pos : Vec3(kMaxF32);
  84. m_debugPoint.m_size = size;
  85. m_debugPoint.m_color = color;
  86. m_debugPoint.m_enableDepthTest = enableDepthTesting;
  87. }
  88. private:
  89. RenderTargetDesc m_rtDescr;
  90. RenderTargetDesc m_objectPickingRtDescr;
  91. RenderTargetDesc m_objectPickingDepthRtDescr;
  92. ImageResourcePtr m_giProbeImage;
  93. ImageResourcePtr m_pointLightImage;
  94. ImageResourcePtr m_spotLightImage;
  95. ImageResourcePtr m_decalImage;
  96. ImageResourcePtr m_reflectionImage;
  97. ShaderProgramResourcePtr m_dbgProg;
  98. MultiframeReadbackToken m_readback;
  99. class
  100. {
  101. public:
  102. BufferPtr m_positionsBuff;
  103. BufferPtr m_indexBuff;
  104. } m_boxLines;
  105. class
  106. {
  107. public:
  108. BufferPtr m_arrowPositions;
  109. BufferPtr m_arrowIndices;
  110. BufferPtr m_scalePositions;
  111. BufferPtr m_scaleIndices;
  112. BufferPtr m_ringPositions;
  113. BufferPtr m_ringIndices;
  114. Mat3x4 m_trf;
  115. Bool m_enabled = false;
  116. } m_gizmos;
  117. class
  118. {
  119. public:
  120. Vec3 m_position = Vec3(kMaxF32);
  121. BufferPtr m_positionsBuff;
  122. F32 m_size = 1.0f;
  123. Vec4 m_color = Vec4(1.0f);
  124. Bool m_enableDepthTest = false;
  125. } m_debugPoint;
  126. DbgOption m_options = DbgOption::kDepthTest;
  127. class
  128. {
  129. public:
  130. RenderTargetHandle m_rt;
  131. RenderTargetHandle m_objectPickingRt;
  132. DbgObjectPickingResult m_objPickingRes;
  133. } m_runCtx;
  134. void initGizmos();
  135. void populateRenderGraphMain(RenderingContext& ctx);
  136. void populateRenderGraphObjectPicking(RenderingContext& ctx);
  137. void drawNonRenderable(GpuSceneNonRenderableObjectType type, U32 objCount, const RenderingContext& ctx, const ImageResource& image,
  138. CommandBuffer& cmdb);
  139. void drawGizmos(const Mat3x4& worldTransform, const RenderingContext& ctx, Bool objectPicking, CommandBuffer& cmdb) const;
  140. void getDebugRenderTarget([[maybe_unused]] CString rtName, Array<RenderTargetHandle, U32(DebugRenderTargetRegister::kCount)>& handles,
  141. DebugRenderTargetDrawStyle& drawStyle) const override
  142. {
  143. handles[DebugRenderTargetRegister::kUintTex] = m_runCtx.m_objectPickingRt;
  144. drawStyle = DebugRenderTargetDrawStyle::kIntegerTexture;
  145. }
  146. };
  147. } // end namespace anki