diff --git a/Assets/Shaders/DebugDrawMesh_VS.glsl b/Assets/Shaders/DebugDrawMesh_VS.glsl new file mode 100644 index 00000000..f84dd35a --- /dev/null +++ b/Assets/Shaders/DebugDrawMesh_VS.glsl @@ -0,0 +1,27 @@ +#version 450 +#extension GL_KHR_vulkan_glsl : enable + +layout(location = 0) in vec4 aVertexPos; +layout(location = 1) in mat4 worldTransform; +layout(location = 5) in vec4 color; + +// Output +layout(location = 0) out struct +{ + vec4 Color; +} Out; + +layout(set = 2, binding = 0) uniform CameraData +{ + vec4 position; + mat4 vpMat; + mat4 viewMat; + mat4 perspectiveMat; + mat4 orthoMat; +} cameraData; + +void main() +{ + gl_Position = cameraData.vpMat * worldTransform * vec4 (aVertexPos.xyz, 1.0f); + Out.Color = color; +} \ No newline at end of file diff --git a/Assets/Shaders/DebugDrawMesh_VS.shshaderb b/Assets/Shaders/DebugDrawMesh_VS.shshaderb new file mode 100644 index 00000000..55d82090 Binary files /dev/null and b/Assets/Shaders/DebugDrawMesh_VS.shshaderb differ diff --git a/Assets/Shaders/DebugDrawMesh_VS.shshaderb.shmeta b/Assets/Shaders/DebugDrawMesh_VS.shshaderb.shmeta new file mode 100644 index 00000000..043dd287 --- /dev/null +++ b/Assets/Shaders/DebugDrawMesh_VS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: DebugDrawMesh_VS +ID: 42127043 +Type: 2 diff --git a/Assets/Shaders/DebugDraw_VS.glsl b/Assets/Shaders/DebugDraw_VS.glsl index 7b370730..a027d596 100644 --- a/Assets/Shaders/DebugDraw_VS.glsl +++ b/Assets/Shaders/DebugDraw_VS.glsl @@ -4,11 +4,10 @@ layout(location = 0) in vec4 aVertexPos; layout(location = 1) in vec4 aVertColor; - +// Output layout(location = 0) out struct { vec4 vertColor; // location 0 - } Out; layout(set = 2, binding = 0) uniform CameraData diff --git a/Assets/Shaders/DebugDraw_VS.shshaderb b/Assets/Shaders/DebugDraw_VS.shshaderb index 2d8cd0ed..30c8d6db 100644 Binary files a/Assets/Shaders/DebugDraw_VS.shshaderb and b/Assets/Shaders/DebugDraw_VS.shshaderb differ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index 0bfa89a2..f090af0d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -25,308 +25,326 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* DrawRoutine */ - /*---------------------------------------------------------------------------------*/ - SHDebugDrawSystem::ProcessPointsRoutine::ProcessPointsRoutine() + /*-----------------------------------------------------------------------------------*/ + /* DrawRoutine */ + /*-----------------------------------------------------------------------------------*/ + SHDebugDrawSystem::ProcessPointsRoutine::ProcessPointsRoutine() : SHSystemRoutine("Debug Draw", true) + { + SystemFamily::GetID(); + } + + void SHDebugDrawSystem::ProcessPointsRoutine::Execute(double dt) noexcept + { + auto gfxSys = SHSystemManager::GetSystem(); + if (!gfxSys) { - SystemFamily::GetID(); + SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; } - void SHDebugDrawSystem::ProcessPointsRoutine::Execute(double dt) noexcept + // Get the system + SHDebugDrawSystem* system = static_cast(GetSystem()); + + // Get current frame index + const uint32_t FRAME_IDX = gfxSys->GetCurrentFrameIndex(); + for (auto& batch : system->lineBatches) { - auto gfxSys = SHSystemManager::GetSystem(); - if (!gfxSys) - { - SHLOG_WARNING("[DebugDraw] Attempted to do debug draw without a graphics system."); - return; - } - - // Get the system - SHDebugDrawSystem* system = static_cast(GetSystem()); - - // Get current frame index - const uint32_t FRAME_IDX = gfxSys->GetCurrentFrameIndex(); - - /* Non-Persistent Buffer */ - // Update the buffer - system->numPoints[FRAME_IDX] = system->points.size(); - const uint32_t DATA_SIZE = sizeof(PointVertex) * system->points.size(); - if (DATA_SIZE > 0) - { - system->vertexBuffers[FRAME_IDX]->WriteToMemory(system->points.data(), DATA_SIZE, 0, 0); - } - - // Reset for next frame - system->points.clear(); - - /* Persistent Buffer */ - // Check if there are changes - if (system->persistentBuffersCleared[FRAME_IDX] - || - system->numPersistentPoints[FRAME_IDX] != system->persistentPoints.size()) - { - // Update Buffer - system->numPersistentPoints[FRAME_IDX] = system->persistentPoints.size(); - const uint32_t DATA_SIZE = sizeof(PointVertex) * system->persistentPoints.size(); - if (DATA_SIZE > 0) - { - system->persistentVertexBuffers[FRAME_IDX]->WriteToMemory(system->persistentPoints.data(), DATA_SIZE, 0, 0); - } - - // Reset Flag - system->persistentBuffersCleared[FRAME_IDX] = false; - } + system->prepareBatch(batch, FRAME_IDX); + batch.Points.clear(); } - - /*---------------------------------------------------------------------------------*/ - /* SHSystem overrides */ - /*---------------------------------------------------------------------------------*/ - void SHDebugDrawSystem::Init() + for (auto& batch : system->persistentLineBatches) { - // Register function for subpass - const auto* GFX_SYSTEM = SHSystemManager::GetSystem(); - auto const& RENDERERS = GFX_SYSTEM->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) - { - // Get Current frame index - const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); + system->prepareBatch(batch, FRAME_IDX); + } + } - // Don't draw if no points - if (numPoints[FRAME_IDX] > 0) - { - cmdBuffer->BeginLabeledSegment("SHDebugDraw"); - cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); - cmdBuffer->SetLineWidth(LineWidth); - cmdBuffer->BindVertexBuffer(0, vertexBuffers[FRAME_IDX], 0); - cmdBuffer->DrawArrays(numPoints[FRAME_IDX], 1, 0, 0); - } - }); - auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); - subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer, uint32_t frameIndex) - { - // Get Current frame index - const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); - - // Don't draw if no points - if (numPersistentPoints[FRAME_IDX] > 0) - { - cmdBuffer->BeginLabeledSegment("SHDebugDraw (Persistent)"); - cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawDepthPipeline()); - cmdBuffer->SetLineWidth(LineWidth); - cmdBuffer->BindVertexBuffer(0, persistentVertexBuffers[FRAME_IDX], 0); - cmdBuffer->DrawArrays(numPersistentPoints[FRAME_IDX], 1, 0, 0); - cmdBuffer->EndLabeledSegment(); - } - }); - - // Reset trackers - std::fill_n(numPoints.begin(), numPoints.size(), 0); - std::fill_n(numPersistentPoints.begin(), numPersistentPoints.size(), 0); - for (bool& cleared : persistentBuffersCleared) - cleared = true; - - // Allocate buffers - // - Non-Persistent Draws - static constexpr uint32_t BUFFER_SIZE = MAX_POINTS * sizeof(PointVertex); - for (Handle& bufHandle : vertexBuffers) - { - bufHandle = GFX_SYSTEM->GetDevice()->CreateBuffer - ( - BUFFER_SIZE, - nullptr, - 0, - vk::BufferUsageFlagBits::eVertexBuffer, - VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, - VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT, - "Debug Draw Non-Persistent Vertex Buffer" - ); - } - // - Persistent Draws - for (Handle& bufHandle : persistentVertexBuffers) - { - bufHandle = GFX_SYSTEM->GetDevice()->CreateBuffer - ( - BUFFER_SIZE, - nullptr, - 0, - vk::BufferUsageFlagBits::eVertexBuffer, - VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, - VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT, - "Debug Draw Persistent Vertex Buffer" - ); - } + /*-----------------------------------------------------------------------------------*/ + /* SHSystem overrides */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDrawSystem::Init() + { + const auto* GFX_SYSTEM = SHSystemManager::GetSystem(); + if (!GFX_SYSTEM) + { + SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; } - void SHDebugDrawSystem::Exit() + // Create all batches + createLineBatches(); + createMeshBatches(); + + // Register function for subpass + auto const& RENDERERS = GFX_SYSTEM->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) { - for (auto vertexBuffer : vertexBuffers) - { - if (vertexBuffer) - vertexBuffer.Free(); - } - for (auto vertexBuffer : persistentVertexBuffers) - { - if (vertexBuffer) - vertexBuffer.Free(); - } + 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); + }); + auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); + subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](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); + }); + } + + void SHDebugDrawSystem::Exit() + { + // Destroy buffers in the batches + destroyLineBatches(); + destroyMeshBatches(); + } + + /*-----------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDrawSystem::DrawLine(const SHVec3& start, const SHVec3& end, const SHColour& color, bool depthTested) + { + // Insert into the batch + drawLine(getLineBatch(depthTested), start, end, color); + } + + void SHDebugDrawSystem::DrawLineLoop(std::initializer_list points, const SHColour& color, bool depthTested) + { + DrawLineLoop(points.begin(), points.end(), color, depthTested); + } + + void SHDebugDrawSystem::DrawTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color, bool depthTested) + { + DrawLineLoop({ p1, p2, p3 }, color, depthTested); + } + + /*-----------------------------------------------------------------------------------*/ + /* Persistent Draw Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDrawSystem::DrawPersistentLine(const SHVec3& start, const SHVec3& end, const SHColour& color, bool depthTested) + { + // Insert into the batch + drawLine(getPersistentLineBatch(depthTested), start, end, color); + } + void SHDebugDrawSystem::DrawPersistentLineLoop(std::initializer_list points, const SHColour& color, bool depthTested) + { + DrawPersistentLineLoop(points.begin(), points.end(), color, depthTested); + } + + void SHDebugDrawSystem::DrawPersistentTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color, bool depthTested) + { + DrawPersistentLineLoop({ p1, p2, p3 }, color, depthTested); + } + + /*-----------------------------------------------------------------------------------*/ + /* Helper Draw Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDrawSystem::drawLine(LinesBatch& batch, const SHVec3& start, const SHVec3& end, const SHColour& color) + { + // Check if points exceeded max + if (batch.Points.size() >= MAX_POINTS) + { + SHLOG_WARNING("[SHDebugDrawSystem] Exceeded maximum size of drawable debug lines. Ignoring."); + return; } - /*---------------------------------------------------------------------------------*/ - /* Draw Functions */ - /*---------------------------------------------------------------------------------*/ - void SHDebugDrawSystem::DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) + batch.Points.emplace_back(start, color); + batch.Points.emplace_back(end, color); + } + + /*-----------------------------------------------------------------------------------*/ + /* Helper Batch Functions - Lines */ + /*-----------------------------------------------------------------------------------*/ + SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getLineBatch(bool depthTested) + { + return depthTested ? lineBatches[static_cast(LineRenderMode::DepthTested)] + : lineBatches[static_cast(LineRenderMode::NoDepthTest)]; + } + SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getPersistentLineBatch(bool depthTested) + { + return depthTested ? persistentLineBatches[static_cast(LineRenderMode::DepthTested)] + : persistentLineBatches[static_cast(LineRenderMode::NoDepthTest)]; + } + + void SHDebugDrawSystem::createLineBatches() + { + auto gfxSys = SHSystemManager::GetSystem(); + if (!gfxSys) { - drawLine(points, color, startPt, endPt); + SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; } - void SHDebugDrawSystem::DrawTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3) + // Line Batches + initBatch + ( + getLineBatch(false), + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineNoDepthTest), + "Debug Draw Non-Persistent Vertex Buffer" + ); + initBatch + ( + getLineBatch(true), + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineDepthTested), + "Debug Draw Depth Tested Non-Persistent Vertex Buffer" + ); + + // Persistent Line Batches + initBatch + ( + getPersistentLineBatch(false), + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineNoDepthTest), + "Debug Draw Persistent Vertex Buffer" + ); + initBatch + ( + getPersistentLineBatch(true), + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineDepthTested), + "Debug Draw Depth Tested Persistent Vertex Buffer" + ); + } + + void SHDebugDrawSystem::initBatch(LinesBatch& batch, Handle pipeline, const std::string& vertexBufferName) + { + auto gfxSys = SHSystemManager::GetSystem(); + if (!gfxSys) { - drawPoly(points, color, { pt1, pt2, pt3 }); + SHLOG_WARNING("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; } - void SHDebugDrawSystem::DrawQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4) + batch.Pipeline = pipeline; + for (auto& vBuffer : batch.VertexBuffers) { - drawPoly(points, color, { pt1, pt2, pt3, pt4 }); + vBuffer = gfxSys->GetDevice()->CreateBuffer + ( + sizeof(PointVertex) * MAX_POINTS, + nullptr, + 0, + vk::BufferUsageFlagBits::eVertexBuffer, + VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, + VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | + VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT, + vertexBufferName + ); + } + } + + + void SHDebugDrawSystem::prepareBatch(LinesBatch& batch, uint32_t frameIndex) + { + // Parameter checks + if (frameIndex > batch.VertexBuffers.size()) + { + SHLOG_ERROR("[SHDebugDrawSystem] An invalid frame index was specified for debug drawing. Skipping."); + return; } - void SHDebugDrawSystem::DrawPoly(const SHVec4& color, std::initializer_list pointList) + // Fill data into the buffers + batch.NumPoints[frameIndex] = batch.Points.size(); + const uint32_t DATA_SIZE = sizeof(PointVertex) * batch.Points.size(); + if (DATA_SIZE > 0) { - drawPoly(points, color, pointList.begin(), pointList.end()); + batch.VertexBuffers[frameIndex]->WriteToMemory(batch.Points.data(), DATA_SIZE, 0, 0); + } + } + + void SHDebugDrawSystem::renderBatch(LinesBatch& batch, Handle cmdBuffer, uint32_t frameIndex) + { + 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) + { + if (vBuffer) + { + vBuffer.Free(); + vBuffer = {}; + } + } + } + + void SHDebugDrawSystem::destroyLineBatches() + { + destroyBatch(getLineBatch(true)); + destroyBatch(getLineBatch(false)); + destroyBatch(getPersistentLineBatch(true)); + destroyBatch(getPersistentLineBatch(false)); + } + + /*-----------------------------------------------------------------------------------*/ + /* Helper Batch Functions - Meshes */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDrawSystem::createMeshBatches() + { + auto gfxSys = SHSystemManager::GetSystem(); + if (!gfxSys) + { + SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); + return; } - void SHDebugDrawSystem::DrawCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size) + // Set up batches + initBatch + ( + meshBatches[static_cast(MeshRenderMode::FilledNoDepthTest)], + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::FilledMeshNoDepthTest) + ); + initBatch + ( + meshBatches[static_cast(MeshRenderMode::FilledDepthTested)], + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::FilledMeshDepthTested) + ); + initBatch + ( + meshBatches[static_cast(MeshRenderMode::WireNoDepthTest)], + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineMeshNoDepthTest) + ); + initBatch + ( + meshBatches[static_cast(MeshRenderMode::WireDepthTested)], + gfxSys->GetDebugDrawPipeline(DebugDrawPipelineType::LineMeshDepthTested) + ); + } + void SHDebugDrawSystem::initBatch(MeshBatch& batch, Handle pipeline) + { + batch.Pipeline = pipeline; + } + void SHDebugDrawSystem::destroyBatch(MeshBatch& batch) + { + for (auto& buffer : batch.InstanceDataBuffer) { - drawCube(points, color, pos, size); + if (buffer) + { + buffer.Free(); + buffer = {}; + } } - - void SHDebugDrawSystem::DrawSphere(const SHVec4& color, const SHVec3& pos, double radius) + for (auto& buffer : batch.MDIBuffer) { - drawSphere(points, color, pos, radius); - } - - /*---------------------------------------------------------------------------------*/ - /* Persistent Draw Functions */ - /*---------------------------------------------------------------------------------*/ - void SHDebugDrawSystem::DrawPersistentLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) - { - drawLine(persistentPoints, color, startPt, endPt); - } - - void SHDebugDrawSystem::DrawPersistentTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3) - { - drawPoly(persistentPoints, color, { pt1, pt2, pt3 }); - } - - void SHDebugDrawSystem::DrawPersistentQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4) - { - drawPoly(persistentPoints, color, { pt1, pt2, pt3, pt4 }); - } - - void SHDebugDrawSystem::DrawPersistentPoly(const SHVec4& color, std::initializer_list pointList) - { - drawPoly(persistentPoints, color, pointList.begin(), pointList.end()); - } - - void SHDebugDrawSystem::DrawPersistentCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size) - { - drawCube(persistentPoints, color, pos, size); - } - - void SHDebugDrawSystem::DrawPersistentSphere(const SHVec4& color, const SHVec3& pos, double radius) - { - drawSphere(persistentPoints, color, pos, radius); - } - - void SHDebugDrawSystem::ClearPersistentDraws() - { - persistentPoints.clear(); - for (bool& cleared : persistentBuffersCleared) - cleared = true; - } - - void SHDebugDrawSystem::drawLine(std::vector& storage, const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) - { - if (storage.size() > MAX_POINTS) - { - SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); - return; - } - - storage.emplace_back(PointVertex{ startPt, color }); - storage.emplace_back(PointVertex{ endPt, color }); - } - - void SHDebugDrawSystem::drawLineSet(std::vector& storage, const SHVec4& color, std::initializer_list pointList) - { - drawLineSet(storage, color, pointList.begin(), pointList.end()); - } - - void SHDebugDrawSystem::drawPoly(std::vector& storage, const SHVec4& color, std::initializer_list pointList) - { - drawPoly(storage, color, pointList.begin(), pointList.end()); - } - - void SHDebugDrawSystem::drawCube(std::vector& storage, const SHVec4& color, const SHVec3& pos, const SHVec3& size) - { - static const SHVec3 EXTENTS = SHVec3{ 0.5f, 0.5f, 0.5f }; - static const SHVec3 UNIT_BOT_LEFT_FRONT = SHVec3{ pos - EXTENTS }; - static const SHVec3 UNIT_BOT_RIGHT_FRONT = SHVec3{ pos + SHVec3 { EXTENTS.x, -EXTENTS.y, -EXTENTS.z } }; - static const SHVec3 UNIT_BOT_RIGHT_BACK = SHVec3{ pos + SHVec3 { EXTENTS.x, -EXTENTS.y, EXTENTS.z } }; - static const SHVec3 UNIT_BOT_LEFT_BACK = SHVec3{ pos + SHVec3 { -EXTENTS.x, -EXTENTS.y, EXTENTS.z } }; - static const SHVec3 UNIT_TOP_LEFT_BACK = SHVec3{ pos + SHVec3 { -EXTENTS.x, EXTENTS.y, EXTENTS.z } }; - static const SHVec3 UNIT_TOP_RIGHT_FRONT = SHVec3{ pos + SHVec3 { EXTENTS.x, EXTENTS.y, -EXTENTS.z } }; - static const SHVec3 UNIT_TOP_LEFT_FRONT = SHVec3{ pos + SHVec3 { -EXTENTS.x, EXTENTS.y, -EXTENTS.z } }; - static const SHVec3 UNIT_TOP_RIGHT_BACK = SHVec3{ pos + EXTENTS }; - - const SHVec3 BOT_LEFT_BACK = UNIT_BOT_LEFT_BACK * size; - const SHVec3 BOT_RIGHT_BACK = UNIT_BOT_RIGHT_BACK * size; - const SHVec3 BOT_LEFT_FRONT = UNIT_BOT_LEFT_FRONT * size; - const SHVec3 BOT_RIGHT_FRONT = UNIT_BOT_RIGHT_FRONT * size; - const SHVec3 TOP_LEFT_BACK = UNIT_TOP_LEFT_BACK * size; - const SHVec3 TOP_RIGHT_BACK = UNIT_TOP_RIGHT_BACK * size; - const SHVec3 TOP_LEFT_FRONT = UNIT_TOP_LEFT_FRONT * size; - const SHVec3 TOP_RIGHT_FRONT = UNIT_TOP_RIGHT_FRONT * size; - - drawLineSet - ( - storage, - color, - { - // Bottom Square - BOT_LEFT_BACK , BOT_RIGHT_BACK, - BOT_RIGHT_BACK , BOT_RIGHT_FRONT, - BOT_RIGHT_FRONT, BOT_LEFT_FRONT, - BOT_LEFT_FRONT , BOT_LEFT_BACK, - // Top Square - TOP_LEFT_BACK , TOP_RIGHT_BACK, - TOP_RIGHT_BACK , TOP_RIGHT_FRONT, - TOP_RIGHT_FRONT, TOP_LEFT_FRONT, - TOP_LEFT_FRONT , TOP_LEFT_BACK, - // Middle Lines - TOP_LEFT_BACK , BOT_LEFT_BACK, - TOP_RIGHT_BACK , BOT_RIGHT_BACK, - TOP_RIGHT_FRONT, BOT_RIGHT_FRONT, - TOP_LEFT_FRONT , BOT_LEFT_FRONT - } - ); - } - - void SHDebugDrawSystem::drawSphere(std::vector& storage, const SHVec4& color, const SHVec3& pos, double radius) - { - //if (spherePoints.empty()) - { - spherePoints.clear(); - // Generate - static const SHMeshData SPHERE = SHPrimitiveGenerator::Sphere(); - for (const auto& idx : SPHERE.Indices) - { - spherePoints.emplace_back(SPHERE.VertexPositions[idx] * radius + pos); - } - } - drawLineSet(storage, color, spherePoints.begin(), spherePoints.end()); + if (buffer) + { + buffer.Free(); + buffer = {}; + } } + } + void SHDebugDrawSystem::destroyMeshBatches() + { + destroyBatch(meshBatches[static_cast(MeshRenderMode::FilledNoDepthTest)]); + destroyBatch(meshBatches[static_cast(MeshRenderMode::FilledDepthTested)]); + destroyBatch(meshBatches[static_cast(MeshRenderMode::WireNoDepthTest)]); + destroyBatch(meshBatches[static_cast(MeshRenderMode::WireDepthTested)]); + } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 20ddcd42..de3595e3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -18,6 +18,7 @@ of DigiPen Institute of Technology is prohibited. #include "Math/Vector/SHVec2.h" #include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec4.h" +#include "Math/SHMatrix.h" #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" #include "Resource/SHHandle.h" @@ -31,6 +32,8 @@ namespace SHADE /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ class SHVkBuffer; + class SHMesh; + class SHVkPipeline; /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ @@ -42,6 +45,29 @@ namespace SHADE { public: /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Defines the rendering mode for debug lines. + /// + enum class LineRenderMode : uint32_t + { + NoDepthTest, + DepthTested, + Count + }; + /// + /// Defines the rendering mode for debug meshes. + /// + enum class MeshRenderMode : uint32_t + { + FilledNoDepthTest, + FilledDepthTested, + WireNoDepthTest, + WireDepthTested, + Count + }; + /*---------------------------------------------------------------------------------*/ /* System Routines */ /*---------------------------------------------------------------------------------*/ class SH_API ProcessPointsRoutine final : public SHSystemRoutine @@ -68,185 +94,130 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Draw Functions */ /*---------------------------------------------------------------------------------*/ - /// - /// Renders a line between two points in world space. - /// - /// Colour of the line. - /// First point of the line. - /// Second point of the line. - void DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt); - /// - /// Renders a triangle indicated by three points in world space. - /// - /// Colour of the triangle. - /// First point of the triangle. - /// Second point of the triangle. - /// Third point of the triangle. - void DrawTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3); - /// - /// Renders a quadrilateral indicated by four points in world space. - /// - /// Colour of the quadrilateral. - /// First point of the triangle. - /// Second point of the quadrilateral. - /// Third point of the quadrilateral. - /// Third point of the quadrilateral. - void DrawQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); - /// - /// Renders a polygon indicated by the specified set of points in world space. - /// - /// Colour of the polygon. - /// List of points for the polygon. - void DrawPoly(const SHVec4& color, std::initializer_list pointList); - /// - /// Renders a polygon indicated by the specified set of points in world space. - /// - /// Iterator for a STL-like container. - /// Colour of the polygon. - /// - /// Iterator to the first point of the point container. - /// - /// - /// One past last iterator of the point container. - /// + void DrawLine(const SHVec3& start, const SHVec3& end, const SHColour& color = SHColour::WHITE, bool depthTested = false); + void DrawLineLoop(std::initializer_list points, const SHColour& color = SHColour::WHITE, bool depthTested = false); template - void DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd); - /// - /// Renders a wireframe cube centered around the position specified in world space. - /// - /// Colour of the cube. - /// Position where the cube wil be centered at. - /// Size of the rendered cube. - void DrawCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size); - /// - /// Renders a wireframe sphere centered around the position specified in world space. - /// - /// Colour of the sphere. - /// Position where the sphere wil be centered at. - /// Size of the rendered sphere. - void DrawSphere(const SHVec4& color, const SHVec3& pos, double radius); + 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); /*---------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ /*---------------------------------------------------------------------------------*/ - /// - /// Renders a line between two points in world space that will persist until - /// ClearPersistentDraws() is called. These lines are depth tested. - /// - /// Colour of the line. - /// First point of the line. - /// Second point of the line. - void DrawPersistentLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt); - /// - /// Renders a triangle indicated by three points in world space that will persist - /// until ClearPersistentDraws() is called. These lines are depth tested. - /// - /// Colour of the triangle. - /// First point of the triangle. - /// Second point of the triangle. - /// Third point of the triangle. - void DrawPersistentTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3); - /// - /// Renders a quadrilateral indicated by four points in world space that will persist - /// until ClearPersistentDraws() is called. These lines are depth tested. - /// - /// Colour of the quadrilateral. - /// First point of the triangle. - /// Second point of the quadrilateral. - /// Third point of the quadrilateral. - /// Third point of the quadrilateral. - void DrawPersistentQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); - /// - /// Renders a polygon indicated by the specified set of points in world space that - /// will persist until ClearPersistentDraws() is called. These lines are depth - /// tested. - /// - /// Colour of the polygon. - /// List of points for the polygon. - void DrawPersistentPoly(const SHVec4& color, std::initializer_list pointList); - /// - /// Renders a polygon indicated by the specified set of points in world space that - /// will persist until ClearPersistentDraws() is called. These lines are depth - /// tested. - /// - /// Iterator for a STL-like container. - /// Colour of the polygon. - /// - /// Iterator to the first point of the point container. - /// - /// - /// One past last iterator of the point container. - /// + void DrawPersistentLine(const SHVec3& start, const SHVec3& end, const SHColour& color = SHColour::WHITE, bool depthTested = false); + void DrawPersistentLineLoop(std::initializer_list points, const SHColour& color = SHColour::WHITE, bool depthTested = false); template - void DrawPersistentPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd); - /// - /// Renders a wireframe cube centered around the position specified in world space - /// that will persist until ClearPersistentDraws() is called. These lines are depth - /// tested. - /// - /// Colour of the cube. - /// Position where the cube wil be centered at. - /// Size of the rendered cube. - void DrawPersistentCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size); - /// - /// Renders a wireframe sphere centered around the position specified in world space - /// that will persist until ClearPersistentDraws() is called. These lines are depth - /// tested. - /// - /// Colour of the sphere. - /// Position where the sphere wil be centered at. - /// Size of the rendered sphere. - void DrawPersistentSphere(const SHVec4& color, const SHVec3& pos, double radius); - /// - /// Clears any persistent drawn debug primitives. - /// - void ClearPersistentDraws(); + 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); private: /*---------------------------------------------------------------------------------*/ /* Type Definitions */ /*---------------------------------------------------------------------------------*/ + using TripleBuffer = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; + using TripleUInt = std::array; + using TripleBool = std::array; + /// + /// Defines a coloured Vertex + /// struct SH_API PointVertex { SHVec4 Position; SHVec4 Color; }; - using TripleBuffer = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; - using TripleUInt = std::array; - using TripleBool = std::array; + struct Batch + { + /*-------------------------------------------------------------------------------*/ + /* Data Members */ + /*-------------------------------------------------------------------------------*/ + Handle Pipeline; + }; + + struct LinesBatch : public Batch + { + /*-------------------------------------------------------------------------------*/ + /* Data Members */ + /*-------------------------------------------------------------------------------*/ + // CPU Buffers + std::vector Points; + // GPU Buffers + TripleBuffer VertexBuffers; + TripleUInt NumPoints; + + }; + + struct MeshBatch : public Batch + { + /*-------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-------------------------------------------------------------------------------*/ + struct InstanceData + { + SHMatrix Transform; + SHVec4 Color; + }; + struct MultiDrawSet + { + Handle Mesh; + std::vector Instances; + }; + + /*-------------------------------------------------------------------------------*/ + /* Data Members */ + /*-------------------------------------------------------------------------------*/ + // CPU Buffers + std::vector SubBatches; + std::vector InstanceData; + // GPU Buffers + TripleBuffer MDIBuffer; + TripleBuffer InstanceDataBuffer; + }; /*---------------------------------------------------------------------------------*/ /* Constants */ /*---------------------------------------------------------------------------------*/ static constexpr uint32_t MAX_POINTS = 100'000; + static constexpr size_t LINE_MODE_COUNT = static_cast(LineRenderMode::Count); + static constexpr size_t MESH_MODE_COUNT = static_cast(MeshRenderMode::Count); /*---------------------------------------------------------------------------------*/ /* Data Members */ /*---------------------------------------------------------------------------------*/ - // CPU Buffers - std::vector points; - std::vector persistentPoints; - // GPU Buffers - TripleBuffer vertexBuffers; - TripleUInt numPoints; - TripleBuffer persistentVertexBuffers; - TripleUInt numPersistentPoints; - TripleBool persistentBuffersCleared; - // Cached Points for polygon drawing - std::vector spherePoints; + // Batches + std::array lineBatches; + std::array persistentLineBatches; + std::array meshBatches; + // Tracking + TripleBool persistentBuffersCleared; // TODO: Use this /*---------------------------------------------------------------------------------*/ /* Helper Draw Functions */ /*---------------------------------------------------------------------------------*/ - void drawLine(std::vector& storage, const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt); - void drawLineSet(std::vector& storage, const SHVec4& color, std::initializer_list pointList); + void drawLine(LinesBatch& batch, const SHVec3& start, const SHVec3& end, const SHColour& color); template - void drawLineSet(std::vector& storage, const SHVec4& color, IterType pointListBegin, IterType pointListEnd); - void drawPoly(std::vector& storage, const SHVec4& color, std::initializer_list pointList); - template - void drawPoly(std::vector& storage, const SHVec4& color, IterType pointListBegin, IterType pointListEnd); - void drawCube(std::vector& storage, const SHVec4& color, const SHVec3& pos, const SHVec3& size); - void drawSphere(std::vector& storage, const SHVec4& color, const SHVec3& pos, double radius); + void drawLineLoop(LinesBatch& batch, IterType pointListBegin, IterType pointListEnd, const SHColour& color); + + /*---------------------------------------------------------------------------------*/ + /* Helper Batch Functions - Lines */ + /*---------------------------------------------------------------------------------*/ + LinesBatch& getLineBatch(bool depthTested); + LinesBatch& getPersistentLineBatch(bool depthTested); + void createLineBatches(); + void initBatch(LinesBatch& batch, Handle pipeline, const std::string& vertexBufferName); + void prepareBatch(LinesBatch& batch, uint32_t frameIndex); + void renderBatch(LinesBatch& batch, Handle cmdBuffer, uint32_t frameIndex); + void destroyBatch(LinesBatch& batch); + void destroyLineBatches(); + + /*---------------------------------------------------------------------------------*/ + /* Helper Batch Functions - Meshes */ + /*---------------------------------------------------------------------------------*/ + void createMeshBatches(); + void initBatch(MeshBatch& batch, Handle pipeline); + void prepareBatch(MeshBatch& batch, uint32_t frameIndex); + void renderBatch(MeshBatch& batch, Handle cmdBuffer, uint32_t frameIndex); + void destroyBatch(MeshBatch& batch);; + void destroyMeshBatches(); }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp index 2a171e73..36c7d2b0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -17,73 +17,44 @@ namespace SHADE { /*-----------------------------------------------------------------------------------*/ /* Draw Functions */ - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ template - void SHDebugDrawSystem::DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd) + void SHDebugDrawSystem::DrawLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color, bool depthTested) { - drawPoly(points, color, pointListBegin, pointListEnd); + // Get Batch + drawLineLoop(getLineBatch(depthTested), pointListBegin, pointListEnd, color); } - + template + void SHADE::SHDebugDrawSystem::DrawPersistentLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color , bool depthTested) + { + // Get Batch + drawLineLoop(getPersistentLineBatch(depthTested), pointListBegin, pointListEnd, color); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Draw Functions */ /*-----------------------------------------------------------------------------------*/ template - void SHDebugDrawSystem::drawLineSet(std::vector& storage, const SHVec4& color, IterType pointListBegin, IterType pointListEnd) + void SHDebugDrawSystem::drawLineLoop(LinesBatch& batch, IterType pointListBegin, IterType pointListEnd, const SHColour& color) { // Ensure dereferenced type is SHVec3 - static_assert(std::is_same_v>, "Parameters to DrawPoly must be SHVec3."); - - // Check if points exceeded max - if (storage.size() > MAX_POINTS) - { - SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); - return; - } - - const size_t POINTS_COUNT = pointListEnd - pointListBegin; + static_assert(std::is_same_v>, "Parameters to DrawLineLoop must be SHVec3."); + // Invalid polygon + const size_t POINTS_COUNT = pointListEnd - pointListBegin; if (POINTS_COUNT < 2) { - SHLOG_WARNING("[SHDebugDraw] Invalid polygon provided to DrawPoly()."); - return; - } - - const size_t POINTS_ROUNDED_COUNT = POINTS_COUNT / 2 * 2; - for (auto pointIter = pointListBegin; pointIter != (pointListBegin + POINTS_ROUNDED_COUNT); ++pointIter) - { - storage.emplace_back(PointVertex{ *pointIter, color }); - } - } - template - void SHDebugDrawSystem::drawPoly(std::vector& storage, const SHVec4& color, IterType pointListBegin, IterType pointListEnd) - { - // Ensure dereferenced type is SHVec3 - static_assert(std::is_same_v>, "Parameters to DrawPoly must be SHVec3."); - - // Check if points exceeded max - if (storage.size() > MAX_POINTS) - { - SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); - return; - } - - const size_t POINTS_COUNT = pointListEnd - pointListBegin; - // Invalid polygon - if (POINTS_COUNT < 2) - { - SHLOG_WARNING("[SHDebugDraw] Invalid polygon provided to DrawPoly()."); + SHLOG_WARNING("[SHDebugDrawSystem] Insufficient points provided to drawLineLoop()."); return; } // Trace the polygon for (auto pointIter = pointListBegin + 1; pointIter != pointListEnd; ++pointIter) { - storage.emplace_back(PointVertex{ *(pointIter - 1), color }); - storage.emplace_back(PointVertex{ *pointIter , color }); + drawLine(batch, *(pointIter - 1), *pointIter, color); } // Close the line loop - storage.emplace_back(PointVertex{ *(pointListEnd - 1), color }); - storage.emplace_back(PointVertex{ *pointListBegin , color }); + drawLine(batch, *(pointListEnd - 1), *pointListBegin, color); } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index bd1d60cd..71133df2 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -124,20 +124,12 @@ namespace SHADE SHFreetypeInstance::Init(); - //SHAssetManager::CompileAsset("../../Assets/Shaders/Text_VS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/Text_FS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/TestCube_VS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/UI_VS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/UI_FS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Models/Quad.gltf", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/ToSwapchain_VS.glsl", false); - //SHAssetManager::CompileAsset("../../Assets/Shaders/ToSwapchain_FS.glsl", false); - // Load Built In Shaders static constexpr AssetID VS_DEFAULT = 39210065; defaultVertShader = SHResourceManager::LoadOrGet(VS_DEFAULT); static constexpr AssetID FS_DEFAULT = 46377769; defaultFragShader = SHResourceManager::LoadOrGet(FS_DEFAULT); static constexpr AssetID VS_DEBUG = 48002439; debugVertShader = SHResourceManager::LoadOrGet(VS_DEBUG); static constexpr AssetID FS_DEBUG = 36671027; debugFragShader = SHResourceManager::LoadOrGet(FS_DEBUG); + static constexpr AssetID VS_DEBUG_MESH = 42127043; debugMeshVertShader = SHResourceManager::LoadOrGet(VS_DEBUG_MESH); static constexpr AssetID CS_COMPOSITE = 45072428; deferredCompositeShader = SHResourceManager::LoadOrGet(CS_COMPOSITE); static constexpr AssetID SSAO = 38430899; ssaoShader = SHResourceManager::LoadOrGet(SSAO); static constexpr AssetID SSAO_BLUR = 39760835; ssaoBlurShader = SHResourceManager::LoadOrGet(SSAO_BLUR); @@ -335,14 +327,31 @@ namespace SHADE screenRenderer->SetCamera(screenCamera); screenRenderer->SetCameraDirector(worldCameraDirector); - // Create debug draw pipeline - debugDrawPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass); + debugDrawPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass, false, false, false); SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawPipeline->GetVkPipeline(), "[Pipeline] Debug Draw"); - SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline Layout] Debug Draw Pipeline Layout"); - debugDrawDepthPipeline = createDebugDrawPipeline(debugDrawNodeDepth->GetRenderpass(), debugDrawDepthSubpass); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline Layout] Debug Draw"); + debugDrawDepthPipeline = createDebugDrawPipeline(debugDrawNodeDepth->GetRenderpass(), debugDrawDepthSubpass, false, false, false); SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawDepthPipeline->GetVkPipeline(), "[Pipeline Layout] Debug Draw with Depth Test"); - SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawDepthPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline] Debug Draw with Depth Test Pipeline Layout"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawDepthPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline] Debug Draw with Depth Test"); + debugDrawLineMeshPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass, false, false, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawLineMeshPipeline->GetVkPipeline(), "[Pipeline] Debug Draw Line Mesh"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawLineMeshPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline Layout] Debug Draw Line Mesh"); + debugDrawLineMeshDepthPipeline = createDebugDrawPipeline(debugDrawNodeDepth->GetRenderpass(), debugDrawDepthSubpass, false, false, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawLineMeshDepthPipeline->GetVkPipeline(), "[Pipeline Layout] Debug Draw Line Mesh with Depth Test"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawLineMeshDepthPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline] Debug Draw Line Mesh with Depth Test"); + debugDrawWireMeshPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass, false, true, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawWireMeshPipeline->GetVkPipeline(), "[Pipeline] Debug Draw Wire Mesh"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawWireMeshPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline Layout] Debug Draw Wire Mesh"); + debugDrawWireMeshDepthPipeline = createDebugDrawPipeline(debugDrawNodeDepth->GetRenderpass(), debugDrawDepthSubpass, false, true, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawWireMeshDepthPipeline->GetVkPipeline(), "[Pipeline Layout] Debug Draw Wire Mesh with Depth Test"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawWireMeshDepthPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline] Debug Draw Wire Mesh with Depth Test"); + debugDrawFilledPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass, true, true, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawFilledPipeline->GetVkPipeline(), "[Pipeline] Debug Draw Filled Mesh"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawFilledPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline Layout] Debug Draw Filled Mesh"); + debugDrawFilledDepthPipeline = createDebugDrawPipeline(debugDrawNodeDepth->GetRenderpass(), debugDrawDepthSubpass, true, true, true); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipeline, debugDrawFilledDepthPipeline->GetVkPipeline(), "[Pipeline Layout] Debug Draw Filled Mesh with Depth Test"); + SET_VK_OBJ_NAME(device, vk::ObjectType::ePipelineLayout, debugDrawFilledDepthPipeline->GetPipelineLayout()->GetVkPipelineLayout(), "[Pipeline] Debug Draw Filled Mesh with Depth Test"); } void SHGraphicsSystem::InitMiddleEnd(void) noexcept @@ -1083,34 +1092,68 @@ namespace SHADE return worldRenderGraph->GetNode(G_BUFFER_RENDER_GRAPH_NODE_NAME.data()); } - SHADE::SHFontLibrary const& SHGraphicsSystem::GetFontLibrary(void) const noexcept + Handle SHGraphicsSystem::GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept + { + switch (type) + { + case DebugDrawPipelineType::LineNoDepthTest: return debugDrawPipeline; + case DebugDrawPipelineType::LineDepthTested: return debugDrawDepthPipeline; + case DebugDrawPipelineType::LineMeshNoDepthTest: return debugDrawLineMeshPipeline; + case DebugDrawPipelineType::LineMeshDepthTested: return debugDrawLineMeshDepthPipeline; + case DebugDrawPipelineType::WireMeshNoDepthTest: return debugDrawWireMeshPipeline; + case DebugDrawPipelineType::WireMeshDepthTested: return debugDrawWireMeshDepthPipeline; + case DebugDrawPipelineType::FilledMeshNoDepthTest: return debugDrawFilledPipeline; + case DebugDrawPipelineType::FilledMeshDepthTested: return debugDrawFilledDepthPipeline; + } + + SHLOG_WARNING("[SHGraphicsSystem] Attempted to retrieve an invalid Debug Draw Pipeline. Default Debug Draw Pipeline returned."); + return debugDrawPipeline; + } + + SHFontLibrary const& SHGraphicsSystem::GetFontLibrary(void) const noexcept { return fontLibrary; } - Handle SHGraphicsSystem::createDebugDrawPipeline(Handle renderPass, Handle subpass) + Handle SHGraphicsSystem::createDebugDrawPipeline(Handle renderPass, Handle subpass, bool filled, bool triMesh, bool instanced) { auto pipelineLayout = resourceManager.Create ( device, SHPipelineLayoutParams { - .shaderModules = { debugVertShader, debugFragShader }, + .shaderModules = { (triMesh ? debugMeshVertShader : debugVertShader) , debugFragShader }, .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() } ); auto pipeline = resourceManager.Create(device, pipelineLayout, nullptr, renderPass, subpass); pipeline->GetPipelineState().SetRasterizationState(SHRasterizationState { - .polygonMode = vk::PolygonMode::eLine, - .cull_mode = vk::CullModeFlagBits::eNone + .polygonMode = filled ? vk::PolygonMode::eFill : vk::PolygonMode::eLine, + .cull_mode = filled ? vk::CullModeFlagBits::eBack : vk::CullModeFlagBits::eNone }); pipeline->GetPipelineState().SetInputAssemblyState(SHInputAssemblyState { - .topology = vk::PrimitiveTopology::eLineList + .topology = triMesh ? vk::PrimitiveTopology::eTriangleList : vk::PrimitiveTopology::eLineList }); SHVertexInputState debugDrawVertexInputState; - debugDrawVertexInputState.AddBinding(false, true, { SHVertexAttribute(SHAttribFormat::FLOAT_4D), SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); + if (instanced) + { + debugDrawVertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // 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 + } + else + { + debugDrawVertexInputState.AddBinding + ( + false, true, + { + SHVertexAttribute(SHAttribFormat::FLOAT_4D), // Vertex World Space Position + SHVertexAttribute(SHAttribFormat::FLOAT_4D) // Vertex Color + } + ); + } pipeline->GetPipelineState().SetVertexInputState(debugDrawVertexInputState); SHColorBlendState colorBlendState{}; colorBlendState.logic_op_enable = VK_FALSE; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 8c65f233..4a4d114f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -73,6 +73,18 @@ namespace SHADE Sphere }; static constexpr int MAX_PRIMITIVE_TYPES = 2; + enum class DebugDrawPipelineType + { + LineNoDepthTest, + LineDepthTested, + LineMeshNoDepthTest, + LineMeshDepthTested, + WireMeshNoDepthTest, + WireMeshDepthTested, + FilledMeshNoDepthTest, + FilledMeshDepthTested + }; + static constexpr int MAX_DEBUG_DRAW_PIPELINE_TYPES = 8; /***********************************************************************************/ /*! @@ -371,8 +383,7 @@ namespace SHADE Handle GetMousePickSystem(void) const noexcept {return mousePickSystem;}; Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;}; Handle GetPrimaryRenderpass() const noexcept; - Handle GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; } - Handle GetDebugDrawDepthPipeline(void) const noexcept { return debugDrawDepthPipeline; } + Handle GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept; uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } SHFontLibrary const& GetFontLibrary (void) const noexcept; @@ -439,6 +450,7 @@ namespace SHADE Handle defaultFragShader; Handle debugVertShader; Handle debugFragShader; + Handle debugMeshVertShader; Handle deferredCompositeShader; Handle ssaoShader; Handle ssaoBlurShader; @@ -454,6 +466,12 @@ namespace SHADE Handle defaultMaterial; Handle debugDrawPipeline; Handle debugDrawDepthPipeline; + Handle debugDrawLineMeshPipeline; + Handle debugDrawLineMeshDepthPipeline; + Handle debugDrawWireMeshPipeline; + Handle debugDrawWireMeshDepthPipeline; + Handle debugDrawFilledPipeline; + Handle debugDrawFilledDepthPipeline; // Built-In Textures Handle defaultTexture; @@ -482,6 +500,6 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Helper Functions */ /*---------------------------------------------------------------------------------*/ - Handle createDebugDrawPipeline(Handle renderPass, Handle subpass); + Handle createDebugDrawPipeline(Handle renderPass, Handle subpass, bool filled, bool triMesh, bool instanced); }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp index 1ca7ae39..96af5e39 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp @@ -195,7 +195,7 @@ namespace SHADE const auto& TRI_ARRAY = rp3dRenderer->getTrianglesArray(); for (int i = 0; i < NUM_TRIS; ++i) - debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3); + debugRenderer->DrawTri(TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3, SHColour::RED); } #else @@ -207,7 +207,7 @@ namespace SHADE const auto& TRI_ARRAY = rp3dRenderer->getTrianglesArray(); for (int i = 0; i < NUM_TRIS; ++i) - debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3); + debugRenderer->DrawTri(TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3, SHColour::RED); #endif } @@ -225,7 +225,7 @@ namespace SHADE const auto& LINE_ARRAY = rp3dRenderer->getLinesArray(); for (int i = 0; i < NUM_LINES; ++i) - debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2); + debugRenderer->DrawLine(LINE_ARRAY[i].point1, LINE_ARRAY[i].point2, SHColour::RED); } #else @@ -237,7 +237,7 @@ namespace SHADE const auto& LINE_ARRAY = rp3dRenderer->getLinesArray(); for (int i = 0; i < NUM_LINES; ++i) - debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2); + debugRenderer->DrawLine(LINE_ARRAY[i].point1, LINE_ARRAY[i].point2, SHColour::RED); #endif } @@ -261,7 +261,7 @@ namespace SHADE const float RENDER_DIST = raycastResult.distance == std::numeric_limits::infinity() ? SHRay::MAX_RAYCAST_DIST : raycastResult.distance; const SHVec3 END_POS = ray.position + (ray.direction * RENDER_DIST); - debugRenderer->DrawLine(RAY_COLOUR, ray.position, END_POS); + debugRenderer->DrawLine(ray.position, END_POS, RAY_COLOUR); } } @@ -306,16 +306,16 @@ namespace SHADE transformedVertices[IDX2] = SHVec3::Transform(boxVertices[IDX2], FINAL_TRS); // Draw 4 line to connect the quads - debugRenderer->DrawLine(COLLIDER_COLOUR, transformedVertices[IDX1], transformedVertices[IDX2]); + debugRenderer->DrawLine(transformedVertices[IDX1], transformedVertices[IDX2], COLLIDER_COLOUR); } // A, B, C, D std::array backQuad { transformedVertices[0], transformedVertices[1], transformedVertices[3], transformedVertices[2] }; - debugRenderer->DrawPoly(COLLIDER_COLOUR, backQuad.begin(), backQuad.end()); + debugRenderer->DrawLineLoop(backQuad.begin(), backQuad.end(), COLLIDER_COLOUR); // E, F, G, H std::array frontQuad { transformedVertices[4], transformedVertices[5], transformedVertices[7], transformedVertices[6] }; - debugRenderer->DrawPoly(COLLIDER_COLOUR, frontQuad.begin(), frontQuad.end()); + debugRenderer->DrawLineLoop(frontQuad.begin(), frontQuad.end(), COLLIDER_COLOUR); } void SHPhysicsDebugDrawSystem::debugDrawSphere(SHDebugDrawSystem* debugRenderer, const SHColliderComponent& colliderComponent, const SHCollisionShape& collisionShape) noexcept @@ -327,8 +327,9 @@ namespace SHADE // Calculate final position & orientation const SHQuaternion FINAL_ROT = colliderComponent.GetOrientation() * SHQuaternion::FromEuler(collisionShape.GetRotationOffset()); const SHMatrix TR = SHMatrix::Rotate(FINAL_ROT) * SHMatrix::Translate(colliderComponent.GetPosition()); - - debugRenderer->DrawSphere(COLLIDER_COLOUR, SHVec3::Transform(collisionShape.GetPositionOffset(), TR), SPHERE->GetWorldRadius()); + + /* #KWFix */ + //debugRenderer->DrawSphere(COLLIDER_COLOUR, SHVec3::Transform(collisionShape.GetPositionOffset(), TR), SPHERE->GetWorldRadius()); } } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.cpp b/SHADE_Engine/src/Tools/SHDebugDraw.cpp index b8aa8b0e..839a9b84 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.cpp +++ b/SHADE_Engine/src/Tools/SHDebugDraw.cpp @@ -43,67 +43,67 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ void SHDebugDraw::Line(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) { - dbgDrawSys->DrawLine(color, startPt, endPt); + dbgDrawSys->DrawLine(startPt, endPt, color); } void SHDebugDraw::Tri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3) { - dbgDrawSys->DrawTri(color, pt1, pt2, pt3); + //dbgDrawSys->DrawTri(color, pt1, pt2, pt3); } void SHDebugDraw::Quad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4) { - dbgDrawSys->DrawQuad(color, pt1, pt2, pt3, pt4); + //dbgDrawSys->DrawQuad(color, pt1, pt2, pt3, pt4); } void SHDebugDraw::Poly(const SHVec4& color, std::initializer_list pointList) { - dbgDrawSys->DrawPoly(color, pointList); + //dbgDrawSys->DrawPoly(color, pointList); } void SHDebugDraw::Cube(const SHVec4& color, const SHVec3& pos, const SHVec3& size) { - dbgDrawSys->DrawCube(color, pos, size); + //dbgDrawSys->DrawCube(color, pos, size); } void SHDebugDraw::Sphere(const SHVec4& color, const SHVec3& pos, double radius) { - dbgDrawSys->DrawSphere(color, pos, radius); + //dbgDrawSys->DrawSphere(color, pos, radius); } void SHDebugDraw::PersistentLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) { - dbgDrawSys->DrawPersistentLine(color, startPt, endPt); + dbgDrawSys->DrawPersistentLine(startPt, endPt, color); } void SHDebugDraw::PersistentTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3) { - dbgDrawSys->DrawPersistentTri(color, pt1, pt2, pt3); + //dbgDrawSys->DrawPersistentTri(color, pt1, pt2, pt3); } void SHDebugDraw::PersistentQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4) { - dbgDrawSys->DrawPersistentQuad(color, pt1, pt2, pt3, pt4); + //dbgDrawSys->DrawPersistentQuad(color, pt1, pt2, pt3, pt4); } void SHDebugDraw::PersistentPoly(const SHVec4& color, std::initializer_list pointList) { - dbgDrawSys->DrawPersistentPoly(color, pointList); + //dbgDrawSys->DrawPersistentPoly(color, pointList); } void SHDebugDraw::PersistentCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size) { - dbgDrawSys->DrawPersistentCube(color, pos, size); + //dbgDrawSys->DrawPersistentCube(color, pos, size); } void SHDebugDraw::PersistentSphere(const SHVec4& color, const SHVec3& pos, double radius) { - dbgDrawSys->DrawPersistentSphere(color, pos, radius); + //dbgDrawSys->DrawPersistentSphere(color, pos, radius); } void SHDebugDraw::ClearPersistentDraws() { - dbgDrawSys->ClearPersistentDraws(); + //dbgDrawSys->ClearPersistentDraws(); } } \ No newline at end of file