DebugRendererRecorder.h 3.6 KB

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