DebugRendererImp.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #ifdef JPH_DEBUG_RENDERER
  6. #include <Jolt/Renderer/DebugRenderer.h>
  7. #else
  8. // Hack to still compile DebugRenderer inside the test framework when Jolt is compiled without
  9. #define JPH_DEBUG_RENDERER
  10. // Make sure the debug renderer symbols don't get imported or exported
  11. #define JPH_DEBUG_RENDERER_EXPORT
  12. #include <Jolt/Renderer/DebugRenderer.h>
  13. #undef JPH_DEBUG_RENDERER
  14. #undef JPH_DEBUG_RENDERER_EXPORT
  15. #endif
  16. #include <Renderer/Renderer.h>
  17. #include <Jolt/Core/Mutex.h>
  18. #include <Jolt/Core/UnorderedMap.h>
  19. class Renderer;
  20. class Font;
  21. /// Implementation of DebugRenderer
  22. class DebugRendererImp final : public DebugRenderer
  23. {
  24. public:
  25. JPH_OVERRIDE_NEW_DELETE
  26. /// Constructor
  27. DebugRendererImp(Renderer *inRenderer, const Font *inFont);
  28. /// Implementation of DebugRenderer interface
  29. virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) override;
  30. virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off) override;
  31. virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) override;
  32. virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) override;
  33. virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) override;
  34. virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight) override;
  35. /// Draw all primitives from the light source
  36. void DrawShadowPass();
  37. /// Draw all primitives that were added
  38. void Draw();
  39. /// Clear all primitives (to be called after drawing)
  40. void Clear();
  41. private:
  42. /// Helper functions to draw sub parts
  43. void DrawLines();
  44. void DrawTriangles();
  45. void DrawTexts();
  46. /// Helper functions to clear sub parts
  47. void ClearLines();
  48. void ClearTriangles();
  49. void ClearTexts();
  50. /// Finalize the current locked primitive and add it to the primitives to draw
  51. void FinalizePrimitive();
  52. /// Ensure that the current locked primitive has space for a primitive consisting inVtxSize vertices
  53. void EnsurePrimitiveSpace(int inVtxSize);
  54. Renderer * mRenderer;
  55. /// Shaders for triangles
  56. unique_ptr<PipelineState> mTriangleStateBF;
  57. unique_ptr<PipelineState> mTriangleStateFF;
  58. unique_ptr<PipelineState> mTriangleStateWire;
  59. /// Shaders for shadow pass for triangles
  60. unique_ptr<PipelineState> mShadowStateBF;
  61. unique_ptr<PipelineState> mShadowStateFF;
  62. unique_ptr<PipelineState> mShadowStateWire;
  63. /// Lock that protects the triangle batches from being accessed from multiple threads
  64. Mutex mPrimitivesLock;
  65. Batch mEmptyBatch;
  66. /// Properties for a single rendered instance
  67. struct Instance
  68. {
  69. /// Constructor
  70. Instance(Mat44Arg inModelMatrix, Mat44Arg inModelMatrixInvTrans, ColorArg inModelColor) : mModelMatrix(inModelMatrix), mModelMatrixInvTrans(inModelMatrixInvTrans), mModelColor(inModelColor) { }
  71. Mat44 mModelMatrix;
  72. Mat44 mModelMatrixInvTrans;
  73. Color mModelColor;
  74. };
  75. /// Rendered instance with added information for lodding
  76. struct InstanceWithLODInfo : public Instance
  77. {
  78. /// Constructor
  79. InstanceWithLODInfo(Mat44Arg inModelMatrix, Mat44Arg inModelMatrixInvTrans, ColorArg inModelColor, const AABox &inWorldSpaceBounds, float inLODScaleSq) : Instance(inModelMatrix, inModelMatrixInvTrans, inModelColor), mWorldSpaceBounds(inWorldSpaceBounds), mLODScaleSq(inLODScaleSq) { }
  80. /// Bounding box for culling
  81. AABox mWorldSpaceBounds;
  82. /// Square of scale factor for LODding (1 = original, > 1 = lod out further, < 1 = lod out earlier)
  83. float mLODScaleSq;
  84. };
  85. /// Properties for a batch of instances that have the same primitive
  86. struct Instances
  87. {
  88. Array<InstanceWithLODInfo> mInstances;
  89. /// Start index in mInstancesBuffer for each of the LOD in the geometry pass. Length is one longer than the number of LODs to indicate how many instances the last lod has.
  90. Array<int> mGeometryStartIdx;
  91. /// Start index in mInstancesBuffer for each of the LOD in the light pass. Length is one longer than the number of LODs to indicate how many instances the last lod has.
  92. Array<int> mLightStartIdx;
  93. };
  94. using InstanceMap = UnorderedMap<GeometryRef, Instances>;
  95. /// Clear map of instances and make it ready for the next frame
  96. void ClearMap(InstanceMap &ioInstances);
  97. /// Helper function to draw instances
  98. inline void DrawInstances(const Geometry *inGeometry, const Array<int> &inStartIdx);
  99. /// List of primitives that are finished and ready for drawing
  100. InstanceMap mWireframePrimitives;
  101. InstanceMap mPrimitives;
  102. InstanceMap mTempPrimitives;
  103. InstanceMap mPrimitivesBackFacing;
  104. int mNumInstances = 0;
  105. Ref<RenderInstances> mInstancesBuffer[Renderer::cFrameCount];
  106. /// Primitive that is being built + its properties
  107. Ref<RenderPrimitive> mLockedPrimitive;
  108. Vertex * mLockedVerticesStart = nullptr;
  109. Vertex * mLockedVertices = nullptr;
  110. Vertex * mLockedVerticesEnd = nullptr;
  111. AABox mLockedPrimitiveBounds;
  112. /// A single text string
  113. struct Text
  114. {
  115. Text(Vec3Arg inPosition, const string_view &inText, ColorArg inColor, float inHeight) : mPosition(inPosition), mText(inText), mColor(inColor), mHeight(inHeight) { }
  116. Vec3 mPosition;
  117. String mText;
  118. Color mColor;
  119. float mHeight;
  120. };
  121. /// All text strings that are to be drawn on screen
  122. Array<Text> mTexts;
  123. Mutex mTextsLock;
  124. /// Font with which to draw the texts
  125. RefConst<Font> mFont;
  126. /// A single line segment
  127. struct Line
  128. {
  129. Float3 mFrom;
  130. Color mFromColor;
  131. Float3 mTo;
  132. Color mToColor;
  133. };
  134. /// The list of line segments
  135. Array<Line> mLines;
  136. Mutex mLinesLock;
  137. /// The shaders for the line segments
  138. unique_ptr<PipelineState> mLineState;
  139. };