From 98ff16d00c2993e30dbee4a73590806bed11b5b6 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 15 Dec 2022 01:45:44 +0800 Subject: [PATCH] Added deebug drawing of cubes of any transform --- Assets/Shaders/DebugDrawMesh_VS.glsl | 2 +- Assets/Shaders/DebugDrawMesh_VS.shshaderb | Bin 1869 -> 1853 bytes Assets/Shaders/DebugDraw_VS.glsl | 4 +- Assets/Shaders/DebugDraw_VS.shshaderb | Bin 1765 -> 1749 bytes .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 244 ++++++++++++++++-- .../MiddleEnd/Interface/SHDebugDrawSystem.h | 28 +- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 6 +- .../MiddleEnd/Interface/SHGraphicsSystem.h | 6 +- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 74 +++++- .../MiddleEnd/Meshes/SHPrimitiveGenerator.h | 41 ++- SHADE_Engine/src/Tools/SHDebugDraw.cpp | 10 +- SHADE_Engine/src/Tools/SHDebugDraw.h | 3 + 12 files changed, 371 insertions(+), 47 deletions(-) diff --git a/Assets/Shaders/DebugDrawMesh_VS.glsl b/Assets/Shaders/DebugDrawMesh_VS.glsl index f84dd35a..2f035261 100644 --- a/Assets/Shaders/DebugDrawMesh_VS.glsl +++ b/Assets/Shaders/DebugDrawMesh_VS.glsl @@ -1,7 +1,7 @@ #version 450 #extension GL_KHR_vulkan_glsl : enable -layout(location = 0) in vec4 aVertexPos; +layout(location = 0) in vec3 aVertexPos; layout(location = 1) in mat4 worldTransform; layout(location = 5) in vec4 color; diff --git a/Assets/Shaders/DebugDrawMesh_VS.shshaderb b/Assets/Shaders/DebugDrawMesh_VS.shshaderb index 55d82090c9be148c039e83910d671b2e8f7d6442..442cd789d87ff082d13ea456d7891105d3a27d52 100644 GIT binary patch delta 463 zcmX|-&q@MO6vodT9fcsJHZC-qEc-M5St$fj5iQ!P3%4TTCV~NN(N14Mdlp;5w^d5?0MD{(j%OuA=d9I{Tb{^^&<+Xc@aj)0ZPl z>6MTcUiS@43uUwu3 If9)QA0jV7zE&u=k delta 479 zcmXw!&q_j35XR>my%s@6s}}j^R+iPRc7h-kQqVGM;Z{W4L~u!4w9{9R9-?))_7Qra zAn5nWIRiiL%s1cMGqWoHl)bxO_)$_dv+Vn2=~~K${qf{|{L#y&W{DN-3{6p@iuqms znorQw&z8^r%k65kCLi%&MdYpGz|B(93X+2j=!9B!X?xTfD?tG=YUKxDHow(;lT1C$ z*TP21)jJP8ZKi*#&Vokmt@8PsOlPv!LC-$g=K9GrXwns0$HNn6X@x^bK@qhh@)YEa zS|ezh?XjfZItI?<&Qx~nt_Y}5a2X44gwS3pyq?t>&fat#>*PYR? KTAn{vYr{Xpbs)X~ diff --git a/Assets/Shaders/DebugDraw_VS.glsl b/Assets/Shaders/DebugDraw_VS.glsl index a027d596..42a48c01 100644 --- a/Assets/Shaders/DebugDraw_VS.glsl +++ b/Assets/Shaders/DebugDraw_VS.glsl @@ -1,7 +1,7 @@ #version 450 #extension GL_KHR_vulkan_glsl : enable -layout(location = 0) in vec4 aVertexPos; +layout(location = 0) in vec3 aVertexPos; layout(location = 1) in vec4 aVertColor; // Output @@ -21,6 +21,6 @@ layout(set = 2, binding = 0) uniform CameraData void main() { - gl_Position = cameraData.vpMat * vec4 (aVertexPos.xyz, 1.0f); + gl_Position = cameraData.vpMat * vec4 (aVertexPos, 1.0f); Out.vertColor = aVertColor; } \ No newline at end of file diff --git a/Assets/Shaders/DebugDraw_VS.shshaderb b/Assets/Shaders/DebugDraw_VS.shshaderb index 30c8d6db49fe8eddd97693f148de32007989390b..9f5f7766a6fa15f0cfbb3fda364868075c99fad5 100644 GIT binary patch delta 444 zcmX|+F;4PMMp}@-I5+H;Q{{o?;w}6>=T|0pO58G~5AS^<~AOsciV_pZuKlogBaku2so&vMaw)7W7 z^5#I!1og#N^gPqoC7^F#SVLc)d5&k#X(Q>>VW&Iy zT)^9-?{<`}UwYLw{+zz5RY3t%z!Mzn+}R~b0uDioos>)X$cQrCfNS6l)*wz7KXGXT DuIM3( delta 460 zcmXw!y-osA5QXR7Wfz-`u>wJ1{auA85TnFcP=XDup}@*y6EwySUx85a6o!Pt_pr0o z=kR>w?rcu>%$YNHX1>$k)VrUBe~kCdEdP93xhvRYJe|Ex-$%uZS!|J=Nh1<{C4ZCU zzIZNX((&Z$o?p2=+FN)P1FJ~>i~4S!iPj}Kp(z?!=qy&?8aza)wZ+l! zmRc4xdPiE4Sw{l>_%pNd_MU|vrg$vW1Wb3*xbIX*gGetCurrentFrameIndex(); + + // Set up line batches for (auto& batch : system->lineBatches) { system->prepareBatch(batch, FRAME_IDX); batch.Points.clear(); } - for (auto& batch : system->persistentLineBatches) + + // Set up mesh batches + for (auto& batch : system->meshBatches) { system->prepareBatch(batch, FRAME_IDX); + for (auto& subBatch : batch.SubBatches) + { + subBatch.second.InstanceColors.clear(); + subBatch.second.InstanceTransforms.clear(); + } + } + + // Set up persistent batches if it was wiped + if (system->persistentBuffersCleared[FRAME_IDX]) + { + for (auto& batch : system->persistentLineBatches) + { + system->prepareBatch(batch, FRAME_IDX); + } + system->persistentBuffersCleared[FRAME_IDX] = false; } } @@ -64,8 +83,8 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ void SHDebugDrawSystem::Init() { - const auto* GFX_SYSTEM = SHSystemManager::GetSystem(); - if (!GFX_SYSTEM) + gfxSystem = SHSystemManager::GetSystem(); + if (!gfxSystem) { SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); return; @@ -76,21 +95,49 @@ namespace SHADE createMeshBatches(); // Register function for subpass - auto const& RENDERERS = GFX_SYSTEM->GetDefaultViewport()->GetRenderers(); + auto const& RENDERERS = gfxSystem->GetDefaultViewport()->GetRenderers(); auto renderGraph = RENDERERS[SHGraphicsConstants::RenderGraphIndices::WORLD]->GetRenderGraph(); auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); - subPass->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer, uint32_t frameIndex) + subPass->AddExteriorDrawCalls([this](Handle& cmdBuffer, uint32_t frameIndex) { - const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); - renderBatch(lineBatches[static_cast(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); - renderBatch(persistentLineBatches[static_cast(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); + const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); + cmdBuffer->BeginLabeledSegment("SHDebugDraw (No Depth Test)"); + { + cmdBuffer->BeginLabeledSegment("SHDebugDraw (Lines)"); + { + renderBatch(lineBatches[static_cast(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); + renderBatch(persistentLineBatches[static_cast(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); + } + cmdBuffer->EndLabeledSegment(); + + cmdBuffer->BeginLabeledSegment("SHDebugDraw (Meshes)"); + { + renderBatch(meshBatches[static_cast(MeshRenderMode::WireNoDepthTest)], cmdBuffer, FRAME_IDX); + } + cmdBuffer->EndLabeledSegment(); + } + cmdBuffer->EndLabeledSegment(); }); auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); - subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer, uint32_t frameIndex) + subPassWithDepth->AddExteriorDrawCalls([this](Handle& cmdBuffer, uint32_t frameIndex) { - const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); - renderBatch(lineBatches[static_cast(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); - renderBatch(persistentLineBatches[static_cast(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); + const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); + cmdBuffer->BeginLabeledSegment("SHDebugDraw (Depth Tested)"); + { + cmdBuffer->BeginLabeledSegment("SHDebugDraw (Lines)"); + { + renderBatch(lineBatches[static_cast(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); + renderBatch(persistentLineBatches[static_cast(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); + } + cmdBuffer->EndLabeledSegment(); + + cmdBuffer->BeginLabeledSegment("SHDebugDraw (Meshes)"); + { + renderBatch(meshBatches[static_cast(MeshRenderMode::WireDepthTested)], cmdBuffer, FRAME_IDX); + } + cmdBuffer->EndLabeledSegment(); + } + cmdBuffer->EndLabeledSegment(); }); } @@ -120,6 +167,11 @@ namespace SHADE DrawLineLoop({ p1, p2, p3 }, color, depthTested); } + void SHDebugDrawSystem::DrawWireBox(const SHMatrix& matrix, const SHColour& color, bool depthTested) + { + drawWireBox(getMeshBatch(false, depthTested), matrix, color); + } + /*-----------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ /*-----------------------------------------------------------------------------------*/ @@ -138,6 +190,14 @@ namespace SHADE DrawPersistentLineLoop({ p1, p2, p3 }, color, depthTested); } + void SHDebugDrawSystem::ClearPersistentDraws() + { + for (auto& batch : persistentLineBatches) + batch.Points.clear(); + for (bool& cleared : persistentBuffersCleared) + cleared = true; + } + /*-----------------------------------------------------------------------------------*/ /* Helper Draw Functions */ /*-----------------------------------------------------------------------------------*/ @@ -154,18 +214,41 @@ namespace SHADE batch.Points.emplace_back(end, color); } + void SHDebugDrawSystem::drawWireBox(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color) + { + + const auto* GFX_SYSTEM = SHSystemManager::GetSystem(); + if (!GFX_SYSTEM) + { + SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; + } + + Handle box = GFX_SYSTEM->GetMeshPrimitive(PrimitiveType::LineCube); + + // Create if doesn't exist + if (!batch.SubBatches.contains(box)) + { + MeshBatch::MultiDrawSet set; + set.Mesh = box; + batch.SubBatches.emplace(box, std::move(set)); + } + + // Add to the batch + batch.SubBatches[box].InstanceTransforms.emplace_back(transformMatrix); + batch.SubBatches[box].InstanceColors.emplace_back(color); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ /*-----------------------------------------------------------------------------------*/ SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getLineBatch(bool depthTested) { - return depthTested ? lineBatches[static_cast(LineRenderMode::DepthTested)] - : lineBatches[static_cast(LineRenderMode::NoDepthTest)]; + return lineBatches[static_cast(depthTested ? LineRenderMode::DepthTested : LineRenderMode::NoDepthTest)]; } SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getPersistentLineBatch(bool depthTested) { - return depthTested ? persistentLineBatches[static_cast(LineRenderMode::DepthTested)] - : persistentLineBatches[static_cast(LineRenderMode::NoDepthTest)]; + return persistentLineBatches[static_cast(depthTested ? LineRenderMode::DepthTested : LineRenderMode::NoDepthTest)]; } void SHDebugDrawSystem::createLineBatches() @@ -255,14 +338,13 @@ namespace SHADE { if (batch.NumPoints[frameIndex] > 0) { - cmdBuffer->BeginLabeledSegment("SHDebugDraw"); cmdBuffer->BindPipeline(batch.Pipeline); cmdBuffer->SetLineWidth(LineWidth); cmdBuffer->BindVertexBuffer(0, batch.VertexBuffers[frameIndex], 0); cmdBuffer->DrawArrays(batch.NumPoints[frameIndex], 1, 0, 0); } } - + void SHDebugDrawSystem::destroyBatch(LinesBatch& batch) { for (auto& vBuffer : batch.VertexBuffers) @@ -286,6 +368,24 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Helper Batch Functions - Meshes */ /*-----------------------------------------------------------------------------------*/ + SHDebugDrawSystem::MeshBatch& SHDebugDrawSystem::getMeshBatch(bool filled, bool depthTested) + { + MeshRenderMode mode = {}; + + if (filled) + { + mode = depthTested ? MeshRenderMode::FilledDepthTested + : MeshRenderMode::FilledNoDepthTest; + } + else + { + mode = depthTested ? MeshRenderMode::WireDepthTested + : MeshRenderMode::WireNoDepthTest; + } + + return meshBatches[static_cast(mode)]; + } + void SHDebugDrawSystem::createMeshBatches() { auto gfxSys = SHSystemManager::GetSystem(); @@ -321,9 +421,115 @@ namespace SHADE { batch.Pipeline = pipeline; } + + void SHDebugDrawSystem::prepareBatch(MeshBatch& batch, uint32_t frameIndex) + { + // Parameter checks + if (frameIndex > batch.MDIBuffer.size()) + { + SHLOG_ERROR("[SHDebugDrawSystem] An invalid frame index was specified for debug drawing. Skipping."); + return; + } + + // Clear existing data + batch.InstanceTransforms.clear(); + batch.InstanceColors.clear(); + batch.MDIData.clear(); + + // Populate + for (auto& subBatch : batch.SubBatches) + { + auto& multiDrawSet = subBatch.second; + + // Nothing to populate with + if (multiDrawSet.InstanceTransforms.empty()) + continue; + + // Populate batch data on CPU + batch.MDIData.emplace_back(vk::DrawIndexedIndirectCommand + { + .indexCount = multiDrawSet.Mesh->IndexCount, + .instanceCount = static_cast(multiDrawSet.InstanceTransforms.size()), + .firstIndex = multiDrawSet.Mesh->FirstIndex, + .vertexOffset = multiDrawSet.Mesh->FirstVertex, + .firstInstance = static_cast(batch.InstanceTransforms.size()) + }); + batch.InstanceTransforms.insert + ( + batch.InstanceTransforms.end(), + multiDrawSet.InstanceTransforms.begin(), + multiDrawSet.InstanceTransforms.end() + ); + batch.InstanceColors.insert + ( + batch.InstanceColors.end(), + multiDrawSet.InstanceColors.begin(), + multiDrawSet.InstanceColors.end() + ); + + // Copy to GPU + SHVkUtil::EnsureBufferAndCopyHostVisibleData + ( + gfxSystem->GetDevice(), + batch.MDIBuffer[frameIndex], + batch.MDIData.data(), static_cast(batch.MDIData.size() * sizeof(vk::DrawIndexedIndirectCommand)), + vk::BufferUsageFlagBits::eIndirectBuffer, + "Debug Draw Mesh Batch MDI Buffer" + ); + SHVkUtil::EnsureBufferAndCopyHostVisibleData + ( + gfxSystem->GetDevice(), + batch.InstanceTransformBuffer[frameIndex], + batch.InstanceTransforms.data(), static_cast(batch.InstanceTransforms.size() * sizeof(SHMatrix)), + vk::BufferUsageFlagBits::eVertexBuffer, + "Debug Draw Mesh Batch Instance Transform Buffer" + ); + SHVkUtil::EnsureBufferAndCopyHostVisibleData + ( + gfxSystem->GetDevice(), + batch.InstanceColorBuffer[frameIndex], + batch.InstanceColors.data(), static_cast(batch.InstanceColors.size() * sizeof(SHVec4)), + vk::BufferUsageFlagBits::eVertexBuffer, + "Debug Draw Mesh Batch Instance Color Buffer" + ); + } + } + + void SHDebugDrawSystem::renderBatch(MeshBatch& batch, Handle cmdBuffer, uint32_t frameIndex) + { + static constexpr uint32_t TRANSFORM_BIND_PT = 1; + static constexpr uint32_t COLOR_BIND_PT = 2; + + // Nothing to draw + if (batch.MDIData.empty()) + return; + + // Bind Pipeline + cmdBuffer->BindPipeline(batch.Pipeline); + + // Bind meshes + cmdBuffer->BindVertexBuffer(0, gfxSystem->GetMeshLibrary().GetVertexPositionsBuffer(), 0); + cmdBuffer->BindIndexBuffer(gfxSystem->GetMeshLibrary().GetIndexBuffer(), 0); + + // Bind instance attributes + cmdBuffer->BindVertexBuffer(TRANSFORM_BIND_PT, batch.InstanceTransformBuffer[frameIndex], 0); + cmdBuffer->BindVertexBuffer(COLOR_BIND_PT, batch.InstanceColorBuffer[frameIndex], 0); + + // Execute draw + cmdBuffer->DrawMultiIndirect(batch.MDIBuffer[frameIndex], static_cast(batch.MDIData.size())); + } + void SHDebugDrawSystem::destroyBatch(MeshBatch& batch) { - for (auto& buffer : batch.InstanceDataBuffer) + for (auto& buffer : batch.InstanceColorBuffer) + { + if (buffer) + { + buffer.Free(); + buffer = {}; + } + } + for (auto& buffer : batch.InstanceTransformBuffer) { if (buffer) { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index de3595e3..035357e3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -31,6 +31,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ + class SHGraphicsSystem; class SHVkBuffer; class SHMesh; class SHVkPipeline; @@ -99,7 +100,7 @@ namespace SHADE template void DrawLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color = SHColour::WHITE, bool depthTested = false); - //void DrawWireBox(const SHVec3& position, const SHVec3& scale, const SHColour& color = SHColour::WHITE, bool depthTested = false); + void DrawWireCube(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); /*---------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ @@ -109,6 +110,10 @@ namespace SHADE template void DrawPersistentLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color = SHColour::WHITE, bool depthTested = false); + /// + /// Clears any persistent drawn debug primitives. + /// + void ClearPersistentDraws(); private: /*---------------------------------------------------------------------------------*/ @@ -151,26 +156,25 @@ namespace SHADE /*-------------------------------------------------------------------------------*/ /* Type Definitions */ /*-------------------------------------------------------------------------------*/ - struct InstanceData - { - SHMatrix Transform; - SHVec4 Color; - }; struct MultiDrawSet { Handle Mesh; - std::vector Instances; + std::vector InstanceTransforms; + std::vector InstanceColors; }; /*-------------------------------------------------------------------------------*/ /* Data Members */ /*-------------------------------------------------------------------------------*/ // CPU Buffers - std::vector SubBatches; - std::vector InstanceData; + std::unordered_map, MultiDrawSet> SubBatches; + std::vector InstanceTransforms; + std::vector InstanceColors; + std::vector MDIData; // GPU Buffers TripleBuffer MDIBuffer; - TripleBuffer InstanceDataBuffer; + TripleBuffer InstanceTransformBuffer; + TripleBuffer InstanceColorBuffer; }; /*---------------------------------------------------------------------------------*/ @@ -183,6 +187,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Data Members */ /*---------------------------------------------------------------------------------*/ + // References + SHGraphicsSystem* gfxSystem = nullptr; // Batches std::array lineBatches; std::array persistentLineBatches; @@ -196,6 +202,7 @@ namespace SHADE void drawLine(LinesBatch& batch, const SHVec3& start, const SHVec3& end, const SHColour& color); template void drawLineLoop(LinesBatch& batch, IterType pointListBegin, IterType pointListEnd, const SHColour& color); + void drawWireBox(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); /*---------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ @@ -212,6 +219,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Helper Batch Functions - Meshes */ /*---------------------------------------------------------------------------------*/ + MeshBatch& getMeshBatch(bool filled, bool depthTested); void createMeshBatches(); void initBatch(MeshBatch& batch, Handle pipeline); void prepareBatch(MeshBatch& batch, uint32_t frameIndex); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 71133df2..8b18d4d3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -413,6 +413,7 @@ namespace SHADE // Create default meshes primitiveMeshes[static_cast(PrimitiveType::Cube)] = SHPrimitiveGenerator::Cube(meshLibrary); + primitiveMeshes[static_cast(PrimitiveType::LineCube)] = SHPrimitiveGenerator::LineCube(meshLibrary); primitiveMeshes[static_cast(PrimitiveType::Sphere)] = SHPrimitiveGenerator::Sphere(meshLibrary); BuildMeshBuffers(); @@ -838,6 +839,7 @@ namespace SHADE { case PrimitiveType::Cube: case PrimitiveType::Sphere: + case PrimitiveType::LineCube: return primitiveMeshes[static_cast(type)]; default: return {}; @@ -1121,7 +1123,7 @@ namespace SHADE ( device, SHPipelineLayoutParams { - .shaderModules = { (triMesh ? debugMeshVertShader : debugVertShader) , debugFragShader }, + .shaderModules = { (instanced ? debugMeshVertShader : debugVertShader) , debugFragShader }, .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() } ); @@ -1139,7 +1141,7 @@ namespace SHADE SHVertexInputState debugDrawVertexInputState; if (instanced) { - debugDrawVertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // 0: Vertex World Space Position + debugDrawVertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_3D) }); // 0: Vertex World Space Position debugDrawVertexInputState.AddBinding(true , true , { SHVertexAttribute(SHAttribFormat::MAT_4D) }); // 1: Instance Transform Matrix (4 Slots) debugDrawVertexInputState.AddBinding(true , true , { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // 5: Instance Color } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 4a4d114f..609ff3df 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -70,9 +70,10 @@ namespace SHADE enum class PrimitiveType { Cube, - Sphere + Sphere, + LineCube }; - static constexpr int MAX_PRIMITIVE_TYPES = 2; + static constexpr int MAX_PRIMITIVE_TYPES = 3; enum class DebugDrawPipelineType { LineNoDepthTest, @@ -386,6 +387,7 @@ namespace SHADE Handle GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept; uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } SHFontLibrary const& GetFontLibrary (void) const noexcept; + const SHMeshLibrary& GetMeshLibrary() const noexcept { return meshLibrary; }; /*-----------------------------------------------------------------------------*/ /* Getters */ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index 60decded..93e4be1a 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -23,6 +23,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHMeshData SHPrimitiveGenerator::cubeMesh; SHMeshData SHPrimitiveGenerator::sphereMesh; + SHMeshData SHPrimitiveGenerator::lineCubeMesh; /*-----------------------------------------------------------------------------------*/ /* Primitive Generation Functions */ @@ -207,14 +208,14 @@ namespace SHADE return addMeshDataTo(cubeMesh, meshLibrary); } - Handle SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept + Handle SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept { if (cubeMesh.VertexPositions.empty()) cubeMesh = Cube(); return addMeshDataTo(cubeMesh, gfxSystem); } - SHADE::SHMeshData SHPrimitiveGenerator::Sphere() noexcept + SHMeshData SHPrimitiveGenerator::Sphere() noexcept { SHMeshData meshData; @@ -265,7 +266,7 @@ namespace SHADE return meshData; } - SHADE::Handle SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept + Handle SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept { if (sphereMesh.VertexPositions.empty()) sphereMesh = Sphere(); @@ -273,7 +274,7 @@ namespace SHADE return addMeshDataTo(sphereMesh, meshLibrary); } - SHADE::Handle SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept + Handle SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept { if (sphereMesh.VertexPositions.empty()) sphereMesh = Sphere(); @@ -281,6 +282,65 @@ namespace SHADE return addMeshDataTo(sphereMesh, gfxSystem); } + SHMeshData SHPrimitiveGenerator::LineCube() noexcept + { + SHMeshData mesh; + + mesh.VertexPositions = + { + // Front + SHVec3(-0.5f, -0.5f, 0.5f), + SHVec3( 0.5f, -0.5f, 0.5f), + SHVec3( 0.5f, 0.5f, 0.5f), + SHVec3(-0.5f, 0.5f, 0.5f), + + // Back + SHVec3(-0.5f, -0.5f, -0.5f), + SHVec3( 0.5f, -0.5f, -0.5f), + SHVec3( 0.5f, 0.5f, -0.5f), + SHVec3(-0.5f, 0.5f, -0.5f) + }; + mesh.VertexNormals.resize(mesh.VertexPositions.size()); + mesh.VertexTangents.resize(mesh.VertexPositions.size()); + mesh.VertexTexCoords.resize(mesh.VertexPositions.size()); + mesh.Indices = + { + // Front + 0, 1, + 1, 2, + 2, 3, + 3, 0, + // Connectors + 0, 4, + 1, 5, + 2, 6, + 3, 7, + // Back + 4, 5, + 5, 6, + 6, 7, + 7, 4, + }; + + return mesh; + } + + Handle SHPrimitiveGenerator::LineCube(SHMeshLibrary& meshLibrary) noexcept + { + if (lineCubeMesh.VertexPositions.empty()) + lineCubeMesh = LineCube(); + + return addMeshDataTo(lineCubeMesh, meshLibrary); + } + + Handle SHPrimitiveGenerator::LineCube(SHGraphicsSystem& gfxSystem) noexcept + { + if (lineCubeMesh.VertexPositions.empty()) + lineCubeMesh = LineCube(); + + return addMeshDataTo(lineCubeMesh, gfxSystem); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ @@ -288,7 +348,7 @@ namespace SHADE { return meshLibrary.AddMesh ( - static_cast(meshData.VertexPositions.size()), + static_cast(meshData.VertexPositions.size()), meshData.VertexPositions.data(), meshData.VertexTexCoords.data(), meshData.VertexTangents.data(), @@ -302,12 +362,12 @@ namespace SHADE { return gfxSystem.AddMesh ( - static_cast(meshData.VertexPositions.size()), + static_cast(meshData.VertexPositions.size()), meshData.VertexPositions.data(), meshData.VertexTexCoords.data(), meshData.VertexTangents.data(), meshData.VertexNormals.data(), - static_cast(meshData.Indices.size()), + static_cast(meshData.Indices.size()), meshData.Indices.data() ); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index 7719e4c3..8586b480 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -43,7 +43,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Primitive Generation Functions */ - /*---------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ /***********************************************************************************/ /*! \brief @@ -116,6 +116,44 @@ namespace SHADE */ /***********************************************************************************/ [[nodiscard]] static Handle Sphere(SHGraphicsSystem& gfxSystem) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a cube that is comprised only of lines with no diagonal lines and store + the data in a SHMeshData object. + + \return + SHMeshData object containing vertex data for the line cube. + */ + /***********************************************************************************/ + [[nodiscard]] static SHMeshData LineCube() noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line cube and constructs a SHMesh using the SHGraphicsSystem provided. + + \param meshLibrary + Reference to the SHMeshLibrary to produce and store a line cube mesh in. + + \return + SHMesh object that points to the generated line cube mesh in the SHMeshLibrary. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCube(SHMeshLibrary& meshLibrary) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line cube and constructs a SHMesh using the SHGraphicsSystem provided. + + \param gfxSystem + Reference to the SHGraphicsSystem to produce and store a line cube mesh in. + + \return + SHMesh object that points to the generated line cube mesh in the + SHGraphicsSystem. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCube(SHGraphicsSystem& gfxSystem) noexcept; private: /*---------------------------------------------------------------------------------*/ @@ -129,5 +167,6 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ static SHMeshData cubeMesh; static SHMeshData sphereMesh; + static SHMeshData lineCubeMesh; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.cpp b/SHADE_Engine/src/Tools/SHDebugDraw.cpp index 839a9b84..b04f8629 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.cpp +++ b/SHADE_Engine/src/Tools/SHDebugDraw.cpp @@ -71,6 +71,11 @@ namespace SHADE //dbgDrawSys->DrawSphere(color, pos, radius); } + void SHDebugDraw::WireCube(const SHMatrix& mat, const SHVec4& color) + { + dbgDrawSys->DrawWireCube(mat, color); + } + void SHDebugDraw::PersistentLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) { dbgDrawSys->DrawPersistentLine(startPt, endPt, color); @@ -103,7 +108,6 @@ namespace SHADE void SHDebugDraw::ClearPersistentDraws() { - //dbgDrawSys->ClearPersistentDraws(); + dbgDrawSys->ClearPersistentDraws(); } - -} \ No newline at end of file +} diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.h b/SHADE_Engine/src/Tools/SHDebugDraw.h index 04504c3a..f3890e17 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.h +++ b/SHADE_Engine/src/Tools/SHDebugDraw.h @@ -22,6 +22,7 @@ namespace SHADE class SHDebugDrawSystem; class SHVec4; class SHVec3; + class SHMatrix; class SHColour; /*-----------------------------------------------------------------------------------*/ @@ -92,6 +93,8 @@ namespace SHADE /// Size of the rendered sphere. static void Sphere(const SHVec4& color, const SHVec3& pos, double radius); + static void WireCube(const SHMatrix& mat, const SHVec4& color); + /*---------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ /*---------------------------------------------------------------------------------*/