DebugDrawer.h 3.9 KB

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