Debian Codegraph @

thirdparty/jolt_physics/Jolt/Renderer/DebugRendererRecorder.h

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