DebugRendererImp.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/RenderPrimitive.h>
  17. #include <Renderer/RenderInstances.h>
  18. #include <Jolt/Core/Mutex.h>
  19. #include <Jolt/Core/UnorderedMap.h>
  20. class Renderer;
  21. class Font;
  22. /// Implementation of DebugRenderer
  23. class DebugRendererImp final : public DebugRenderer
  24. {
  25. public:
  26. JPH_OVERRIDE_NEW_DELETE
  27. /// Constructor
  28. DebugRendererImp(Renderer *inRenderer, const Font *inFont);
  29. /// Implementation of DebugRenderer interface
  30. virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) override;
  31. virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off) override;
  32. virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) override;
  33. virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) override;
  34. virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) override;
  35. virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight) override;
  36. /// Draw all primitives that were added
  37. void Draw();
  38. /// Clear all primitives (to be called after drawing)
  39. void Clear();
  40. private:
  41. /// Helper functions to draw sub parts
  42. void DrawLines();
  43. void DrawTriangles();
  44. void DrawTexts();
  45. /// Helper functions to clear sub parts
  46. void ClearLines();
  47. void ClearTriangles();
  48. void ClearTexts();
  49. /// Implementation specific batch object
  50. class BatchImpl : public RefTargetVirtual, public RenderPrimitive
  51. {
  52. public:
  53. JPH_OVERRIDE_NEW_DELETE
  54. BatchImpl(Renderer *inRenderer, D3D_PRIMITIVE_TOPOLOGY inType) : RenderPrimitive(inRenderer, inType) { }
  55. virtual void AddRef() override { RenderPrimitive::AddRef(); }
  56. virtual void Release() override { if (--mRefCount == 0) delete this; }
  57. };
  58. /// Finalize the current locked primitive and add it to the primitives to draw
  59. void FinalizePrimitive();
  60. /// Ensure that the current locked primitive has space for a primitive consisting inVtxSize vertices
  61. void EnsurePrimitiveSpace(int inVtxSize);
  62. Renderer * mRenderer;
  63. /// Shaders for triangles
  64. unique_ptr<PipelineState> mTriangleStateBF;
  65. unique_ptr<PipelineState> mTriangleStateFF;
  66. unique_ptr<PipelineState> mTriangleStateWire;
  67. /// Shaders for shadow pass for triangles
  68. unique_ptr<PipelineState> mShadowStateBF;
  69. unique_ptr<PipelineState> mShadowStateFF;
  70. unique_ptr<PipelineState> mShadowStateWire;
  71. /// The shadow buffer (depth buffer rendered from the light)
  72. Ref<Texture> mDepthTexture;
  73. /// Lock that protects the triangle batches from being accessed from multiple threads
  74. Mutex mPrimitivesLock;
  75. Batch mEmptyBatch;
  76. /// Properties for a single rendered instance
  77. struct Instance
  78. {
  79. /// Constructor
  80. Instance(Mat44Arg inModelMatrix, Mat44Arg inModelMatrixInvTrans, ColorArg inModelColor) : mModelMatrix(inModelMatrix), mModelMatrixInvTrans(inModelMatrixInvTrans), mModelColor(inModelColor) { }
  81. Mat44 mModelMatrix;
  82. Mat44 mModelMatrixInvTrans;
  83. Color mModelColor;
  84. };
  85. /// Rendered instance with added information for lodding
  86. struct InstanceWithLODInfo : public Instance
  87. {
  88. /// Constructor
  89. InstanceWithLODInfo(Mat44Arg inModelMatrix, Mat44Arg inModelMatrixInvTrans, ColorArg inModelColor, const AABox &inWorldSpaceBounds, float inLODScaleSq) : Instance(inModelMatrix, inModelMatrixInvTrans, inModelColor), mWorldSpaceBounds(inWorldSpaceBounds), mLODScaleSq(inLODScaleSq) { }
  90. /// Bounding box for culling
  91. AABox mWorldSpaceBounds;
  92. /// Square of scale factor for LODding (1 = original, > 1 = lod out further, < 1 = lod out earlier)
  93. float mLODScaleSq;
  94. };
  95. /// Properties for a batch of instances that have the same primitive
  96. struct Instances
  97. {
  98. Array<InstanceWithLODInfo> mInstances;
  99. /// 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.
  100. Array<int> mGeometryStartIdx;
  101. /// 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.
  102. Array<int> mLightStartIdx;
  103. };
  104. using InstanceMap = UnorderedMap<GeometryRef, Instances>;
  105. /// Clear map of instances and make it ready for the next frame
  106. void ClearMap(InstanceMap &ioInstances);
  107. /// Helper function to draw instances
  108. inline void DrawInstances(const Geometry *inGeometry, const Array<int> &inStartIdx);
  109. /// List of primitives that are finished and ready for drawing
  110. InstanceMap mWireframePrimitives;
  111. InstanceMap mPrimitives;
  112. InstanceMap mTempPrimitives;
  113. InstanceMap mPrimitivesBackFacing;
  114. int mNumInstances = 0;
  115. Ref<RenderInstances> mInstancesBuffer[Renderer::cFrameCount];
  116. /// Primitive that is being built + its properties
  117. Batch mLockedPrimitive;
  118. Vertex * mLockedVerticesStart = nullptr;
  119. Vertex * mLockedVertices = nullptr;
  120. Vertex * mLockedVerticesEnd = nullptr;
  121. AABox mLockedPrimitiveBounds;
  122. /// A single text string
  123. struct Text
  124. {
  125. Text(Vec3Arg inPosition, const string_view &inText, ColorArg inColor, float inHeight) : mPosition(inPosition), mText(inText), mColor(inColor), mHeight(inHeight) { }
  126. Vec3 mPosition;
  127. String mText;
  128. Color mColor;
  129. float mHeight;
  130. };
  131. /// All text strings that are to be drawn on screen
  132. Array<Text> mTexts;
  133. Mutex mTextsLock;
  134. /// Font with which to draw the texts
  135. RefConst<Font> mFont;
  136. /// A single line segment
  137. struct Line
  138. {
  139. Float3 mFrom;
  140. Color mFromColor;
  141. Float3 mTo;
  142. Color mToColor;
  143. };
  144. /// The list of line segments
  145. Array<Line> mLines;
  146. Mutex mLinesLock;
  147. /// The shaders for the line segments
  148. unique_ptr<PipelineState> mLineState;
  149. };