DebugRendererImp.h 6.5 KB

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