DebugDrawer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #pragma once
  6. #include <AnKi/Scene/Common.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Gr.h>
  9. #include <AnKi/Physics/PhysicsDrawer.h>
  10. #include <AnKi/Resource/ShaderProgramResource.h>
  11. #include <AnKi/Util/Array.h>
  12. namespace anki {
  13. // Forward
  14. class RenderQueueDrawContext;
  15. class StagingGpuMemoryManager;
  16. class StagingGpuMemoryToken;
  17. /// @addtogroup renderer
  18. /// @{
  19. /// Allocate memory for a line cube and populate it.
  20. void allocateAndPopulateDebugBox(StagingGpuMemoryManager& stagingGpuAllocator, StagingGpuMemoryToken& vertsToken,
  21. StagingGpuMemoryToken& indicesToken, U32& indexCount);
  22. /// Debug drawer.
  23. class DebugDrawer2
  24. {
  25. public:
  26. ANKI_USE_RESULT Error init(ResourceManager* rsrcManager);
  27. Bool isInitialized() const
  28. {
  29. return m_prog.isCreated();
  30. }
  31. void drawCubes(ConstWeakArray<Mat4> mvps, const Vec4& color, F32 lineSize, Bool ditherFailedDepth, F32 cubeSideSize,
  32. StagingGpuMemoryManager& stagingGpuAllocator, CommandBufferPtr& cmdb) const;
  33. void drawCube(const Mat4& mvp, const Vec4& color, F32 lineSize, Bool ditherFailedDepth, F32 cubeSideSize,
  34. StagingGpuMemoryManager& stagingGpuAllocator, CommandBufferPtr& cmdb) const
  35. {
  36. drawCubes(ConstWeakArray<Mat4>(&mvp, 1), color, lineSize, ditherFailedDepth, cubeSideSize, stagingGpuAllocator,
  37. cmdb);
  38. }
  39. void drawLines(ConstWeakArray<Mat4> mvps, const Vec4& color, F32 lineSize, Bool ditherFailedDepth,
  40. ConstWeakArray<Vec3> linePositions, StagingGpuMemoryManager& stagingGpuAllocator,
  41. CommandBufferPtr& cmdb) const;
  42. void drawLine(const Mat4& mvp, const Vec4& color, F32 lineSize, Bool ditherFailedDepth, const Vec3& a,
  43. const Vec3& b, StagingGpuMemoryManager& stagingGpuAllocator, CommandBufferPtr& cmdb) const
  44. {
  45. Array<Vec3, 2> points = {a, b};
  46. drawLines(ConstWeakArray<Mat4>(&mvp, 1), color, lineSize, ditherFailedDepth, points, stagingGpuAllocator, cmdb);
  47. }
  48. void drawBillboardTextures(const Mat4& projMat, const Mat4& viewMat, ConstWeakArray<Vec3> positions,
  49. const Vec4& color, Bool ditherFailedDepth, TextureViewPtr tex, SamplerPtr sampler,
  50. Vec2 billboardSize, StagingGpuMemoryManager& stagingGpuAllocator,
  51. CommandBufferPtr& cmdb) const;
  52. void drawBillboardTexture(const Mat4& projMat, const Mat4& viewMat, Vec3 position, const Vec4& color,
  53. Bool ditherFailedDepth, TextureViewPtr tex, SamplerPtr sampler, Vec2 billboardSize,
  54. StagingGpuMemoryManager& stagingGpuAllocator, CommandBufferPtr& cmdb) const
  55. {
  56. drawBillboardTextures(projMat, viewMat, ConstWeakArray<Vec3>(&position, 1), color, ditherFailedDepth, tex,
  57. sampler, billboardSize, stagingGpuAllocator, cmdb);
  58. }
  59. private:
  60. ShaderProgramResourcePtr m_prog;
  61. BufferPtr m_cubePositionsBuffer;
  62. BufferPtr m_cubeIndicesBuffer;
  63. };
  64. /// Implement physics debug drawer.
  65. class PhysicsDebugDrawer : public PhysicsDrawer
  66. {
  67. public:
  68. PhysicsDebugDrawer(const DebugDrawer2* dbg)
  69. : m_dbg(dbg)
  70. {
  71. }
  72. void start(const Mat4& mvp, CommandBufferPtr& cmdb, StagingGpuMemoryManager* stagingGpuAllocator)
  73. {
  74. ANKI_ASSERT(stagingGpuAllocator);
  75. ANKI_ASSERT(m_vertCount == 0);
  76. m_mvp = mvp;
  77. m_cmdb = cmdb;
  78. m_stagingGpuAllocator = stagingGpuAllocator;
  79. }
  80. void drawLines(const Vec3* lines, const U32 vertCount, const Vec4& color) final;
  81. void end()
  82. {
  83. flush();
  84. m_cmdb.reset(nullptr); // This is essential!!!
  85. m_stagingGpuAllocator = nullptr;
  86. }
  87. private:
  88. const DebugDrawer2* m_dbg; ///< The debug drawer
  89. Mat4 m_mvp = Mat4::getIdentity();
  90. CommandBufferPtr m_cmdb;
  91. StagingGpuMemoryManager* m_stagingGpuAllocator = nullptr;
  92. // Use a vertex cache because drawLines() is practically called for every line
  93. Array<Vec3, 32> m_vertCache;
  94. U32 m_vertCount = 0;
  95. Vec4 m_currentColor = Vec4(-1.0f);
  96. void flush();
  97. };
  98. /// @}
  99. } // end namespace anki