DebugRendererImp.h 6.5 KB

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