DebugRendererRecorder.h 3.2 KB

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