| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #ifdef JPH_DEBUG_RENDERER |
| 8 | |
| 9 | #include <Jolt/Renderer/DebugRendererRecorder.h> |
| 10 | |
| 11 | JPH_NAMESPACE_BEGIN |
| 12 | |
| 13 | void DebugRendererRecorder::DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) |
| 14 | { |
| 15 | lock_guard lock(mMutex); |
| 16 | |
| 17 | mCurrentFrame.mLines.push_back({ inFrom, inTo, inColor }); |
| 18 | } |
| 19 | |
| 20 | void DebugRendererRecorder::DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow) |
| 21 | { |
| 22 | lock_guard lock(mMutex); |
| 23 | |
| 24 | mCurrentFrame.mTriangles.push_back({ inV1, inV2, inV3, inColor, inCastShadow }); |
| 25 | } |
| 26 | |
| 27 | DebugRenderer::Batch DebugRendererRecorder::CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) |
| 28 | { |
| 29 | if (inTriangles == nullptr || inTriangleCount == 0) |
| 30 | return new BatchImpl(0); |
| 31 | |
| 32 | lock_guard lock(mMutex); |
| 33 | |
| 34 | mStream.Write(ECommand::CreateBatch); |
| 35 | |
| 36 | uint32 batch_id = mNextBatchID++; |
| 37 | JPH_ASSERT(batch_id != 0); |
| 38 | mStream.Write(batch_id); |
| 39 | mStream.Write((uint32)inTriangleCount); |
| 40 | mStream.WriteBytes(inTriangles, inTriangleCount * sizeof(Triangle)); |
| 41 | |
| 42 | return new BatchImpl(batch_id); |
| 43 | } |
| 44 | |
| 45 | DebugRenderer::Batch DebugRendererRecorder::CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) |
| 46 | { |
| 47 | if (inVertices == nullptr || inVertexCount == 0 || inIndices == nullptr || inIndexCount == 0) |
| 48 | return new BatchImpl(0); |
| 49 | |
| 50 | lock_guard lock(mMutex); |
| 51 | |
| 52 | mStream.Write(ECommand::CreateBatchIndexed); |
| 53 | |
| 54 | uint32 batch_id = mNextBatchID++; |
| 55 | JPH_ASSERT(batch_id != 0); |
| 56 | mStream.Write(batch_id); |
| 57 | mStream.Write((uint32)inVertexCount); |
| 58 | mStream.WriteBytes(inVertices, inVertexCount * sizeof(Vertex)); |
| 59 | mStream.Write((uint32)inIndexCount); |
| 60 | mStream.WriteBytes(inIndices, inIndexCount * sizeof(uint32)); |
| 61 | |
| 62 | return new BatchImpl(batch_id); |
| 63 | } |
| 64 | |
| 65 | void DebugRendererRecorder::DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) |
| 66 | { |
| 67 | lock_guard lock(mMutex); |
| 68 | |
| 69 | // See if this geometry was used before |
| 70 | uint32 &geometry_id = mGeometries[inGeometry]; |
| 71 | if (geometry_id == 0) |
| 72 | { |
| 73 | mStream.Write(ECommand::CreateGeometry); |
| 74 | |
| 75 | // Create a new ID |
| 76 | geometry_id = mNextGeometryID++; |
| 77 | JPH_ASSERT(geometry_id != 0); |
| 78 | mStream.Write(geometry_id); |
| 79 | |
| 80 | // Save bounds |
| 81 | mStream.Write(inGeometry->mBounds.mMin); |
| 82 | mStream.Write(inGeometry->mBounds.mMax); |
| 83 | |
| 84 | // Save the LODs |
| 85 | mStream.Write((uint32)inGeometry->mLODs.size()); |
| 86 | for (const LOD & lod : inGeometry->mLODs) |
| 87 | { |
| 88 | mStream.Write(lod.mDistance); |
| 89 | mStream.Write(static_cast<const BatchImpl *>(lod.mTriangleBatch.GetPtr())->mID); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | mCurrentFrame.mGeometries.push_back({ inModelMatrix, inModelColor, geometry_id, inCullMode, inCastShadow, inDrawMode }); |
| 94 | } |
| 95 | |
| 96 | void DebugRendererRecorder::DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight) |
| 97 | { |
| 98 | lock_guard lock(mMutex); |
| 99 | |
| 100 | mCurrentFrame.mTexts.push_back({ inPosition, inString, inColor, inHeight }); |
| 101 | } |
| 102 | |
| 103 | void DebugRendererRecorder::EndFrame() |
| 104 | { |
| 105 | lock_guard lock(mMutex); |
| 106 | |
| 107 | mStream.Write(ECommand::EndFrame); |
| 108 | |
| 109 | // Write all lines |
| 110 | mStream.Write((uint32)mCurrentFrame.mLines.size()); |
| 111 | for (const LineBlob &line : mCurrentFrame.mLines) |
| 112 | { |
| 113 | mStream.Write(line.mFrom); |
| 114 | mStream.Write(line.mTo); |
| 115 | mStream.Write(line.mColor); |
| 116 | } |
| 117 | mCurrentFrame.mLines.clear(); |
| 118 | |
| 119 | // Write all triangles |
| 120 | mStream.Write((uint32)mCurrentFrame.mTriangles.size()); |
| 121 | for (const TriangleBlob &triangle : mCurrentFrame.mTriangles) |
| 122 | { |
| 123 | mStream.Write(triangle.mV1); |
| 124 | mStream.Write(triangle.mV2); |
| 125 | mStream.Write(triangle.mV3); |
| 126 | mStream.Write(triangle.mColor); |
| 127 | mStream.Write(triangle.mCastShadow); |
| 128 | } |
| 129 | mCurrentFrame.mTriangles.clear(); |
| 130 | |
| 131 | // Write all texts |
| 132 | mStream.Write((uint32)mCurrentFrame.mTexts.size()); |
| 133 | for (const TextBlob &text : mCurrentFrame.mTexts) |
| 134 | { |
| 135 | mStream.Write(text.mPosition); |
| 136 | mStream.Write(text.mString); |
| 137 | mStream.Write(text.mColor); |
| 138 | mStream.Write(text.mHeight); |
| 139 | } |
| 140 | mCurrentFrame.mTexts.clear(); |
| 141 | |
| 142 | // Write all geometries |
| 143 | mStream.Write((uint32)mCurrentFrame.mGeometries.size()); |
| 144 | for (const GeometryBlob &geom : mCurrentFrame.mGeometries) |
| 145 | { |
| 146 | mStream.Write(geom.mModelMatrix); |
| 147 | mStream.Write(geom.mModelColor); |
| 148 | mStream.Write(geom.mGeometryID); |
| 149 | mStream.Write(geom.mCullMode); |
| 150 | mStream.Write(geom.mCastShadow); |
| 151 | mStream.Write(geom.mDrawMode); |
| 152 | } |
| 153 | mCurrentFrame.mGeometries.clear(); |
| 154 | } |
| 155 | |
| 156 | JPH_NAMESPACE_END |
| 157 | |
| 158 | #endif // JPH_DEBUG_RENDERER |