DebugRendererRecorder.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #ifndef JPH_DEBUG_RENDERER
  6. #error This file should only be included when JPH_DEBUG_RENDERER is defined
  7. #endif // !JPH_DEBUG_RENDERER
  8. #include <Jolt/Renderer/DebugRenderer.h>
  9. #include <Jolt/Core/StreamOut.h>
  10. #include <Jolt/Core/Mutex.h>
  11. #include <Jolt/Core/UnorderedMap.h>
  12. JPH_NAMESPACE_BEGIN
  13. /// Implementation of DebugRenderer that records the API invocations to be played back later
  14. class JPH_DEBUG_RENDERER_EXPORT DebugRendererRecorder final : public DebugRenderer
  15. {
  16. public:
  17. JPH_OVERRIDE_NEW_DELETE
  18. /// Constructor
  19. DebugRendererRecorder(StreamOut &inStream) : mStream(inStream) { Initialize(); }
  20. /// Implementation of DebugRenderer interface
  21. virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) override;
  22. virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow) override;
  23. virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) override;
  24. virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) override;
  25. virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) override;
  26. virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight) override;
  27. /// Mark the end of a frame
  28. void EndFrame();
  29. /// Control commands written into the stream
  30. enum class ECommand : uint8
  31. {
  32. CreateBatch,
  33. CreateBatchIndexed,
  34. CreateGeometry,
  35. EndFrame
  36. };
  37. /// Holds a single line segment
  38. struct LineBlob
  39. {
  40. RVec3 mFrom;
  41. RVec3 mTo;
  42. Color mColor;
  43. };
  44. /// Holds a single triangle
  45. struct TriangleBlob
  46. {
  47. RVec3 mV1;
  48. RVec3 mV2;
  49. RVec3 mV3;
  50. Color mColor;
  51. ECastShadow mCastShadow;
  52. };
  53. /// Holds a single text entry
  54. struct TextBlob
  55. {
  56. TextBlob() = default;
  57. TextBlob(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight) : mPosition(inPosition), mString(inString), mColor(inColor), mHeight(inHeight) { }
  58. RVec3 mPosition;
  59. String mString;
  60. Color mColor;
  61. float mHeight;
  62. };
  63. /// Holds a single geometry draw call
  64. struct GeometryBlob
  65. {
  66. RMat44 mModelMatrix;
  67. Color mModelColor;
  68. uint32 mGeometryID;
  69. ECullMode mCullMode;
  70. ECastShadow mCastShadow;
  71. EDrawMode mDrawMode;
  72. };
  73. /// All information for a single frame
  74. struct Frame
  75. {
  76. Array<LineBlob> mLines;
  77. Array<TriangleBlob> mTriangles;
  78. Array<TextBlob> mTexts;
  79. Array<GeometryBlob> mGeometries;
  80. };
  81. private:
  82. /// Implementation specific batch object
  83. class BatchImpl : public RefTargetVirtual
  84. {
  85. public:
  86. JPH_OVERRIDE_NEW_DELETE
  87. BatchImpl(uint32 inID) : mID(inID) { }
  88. virtual void AddRef() override { ++mRefCount; }
  89. virtual void Release() override { if (--mRefCount == 0) delete this; }
  90. atomic<uint32> mRefCount = 0;
  91. uint32 mID;
  92. };
  93. /// Lock that prevents concurrent access to the internal structures
  94. Mutex mMutex;
  95. /// Stream that recorded data will be sent to
  96. StreamOut & mStream;
  97. /// Next available ID
  98. uint32 mNextBatchID = 1;
  99. uint32 mNextGeometryID = 1;
  100. /// Cached geometries and their IDs
  101. UnorderedMap<GeometryRef, uint32> mGeometries;
  102. /// Data that is being accumulated for the current frame
  103. Frame mCurrentFrame;
  104. };
  105. JPH_NAMESPACE_END