Added deebug drawing of cubes of any transform

This commit is contained in:
Kah Wei 2022-12-15 01:45:44 +08:00
parent 06cc969658
commit 98ff16d00c
12 changed files with 371 additions and 47 deletions

View File

@ -1,7 +1,7 @@
#version 450 #version 450
#extension GL_KHR_vulkan_glsl : enable #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 = 1) in mat4 worldTransform;
layout(location = 5) in vec4 color; layout(location = 5) in vec4 color;

View File

@ -1,7 +1,7 @@
#version 450 #version 450
#extension GL_KHR_vulkan_glsl : enable #extension GL_KHR_vulkan_glsl : enable
layout(location = 0) in vec4 aVertexPos; layout(location = 0) in vec3 aVertexPos;
layout(location = 1) in vec4 aVertColor; layout(location = 1) in vec4 aVertColor;
// Output // Output
@ -21,6 +21,6 @@ layout(set = 2, binding = 0) uniform CameraData
void main() void main()
{ {
gl_Position = cameraData.vpMat * vec4 (aVertexPos.xyz, 1.0f); gl_Position = cameraData.vpMat * vec4 (aVertexPos, 1.0f);
Out.vertColor = aVertColor; Out.vertColor = aVertColor;
} }

View File

@ -48,14 +48,33 @@ namespace SHADE
// Get current frame index // Get current frame index
const uint32_t FRAME_IDX = gfxSys->GetCurrentFrameIndex(); const uint32_t FRAME_IDX = gfxSys->GetCurrentFrameIndex();
// Set up line batches
for (auto& batch : system->lineBatches) for (auto& batch : system->lineBatches)
{ {
system->prepareBatch(batch, FRAME_IDX); system->prepareBatch(batch, FRAME_IDX);
batch.Points.clear(); batch.Points.clear();
} }
for (auto& batch : system->persistentLineBatches)
// Set up mesh batches
for (auto& batch : system->meshBatches)
{ {
system->prepareBatch(batch, FRAME_IDX); 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() void SHDebugDrawSystem::Init()
{ {
const auto* GFX_SYSTEM = SHSystemManager::GetSystem<SHGraphicsSystem>(); gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (!GFX_SYSTEM) if (!gfxSystem)
{ {
SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system."); SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system.");
return; return;
@ -76,21 +95,49 @@ namespace SHADE
createMeshBatches(); createMeshBatches();
// Register function for subpass // 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 renderGraph = RENDERERS[SHGraphicsConstants::RenderGraphIndices::WORLD]->GetRenderGraph();
auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw");
subPass->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle<SHVkCommandBuffer>& cmdBuffer, uint32_t frameIndex) subPass->AddExteriorDrawCalls([this](Handle<SHVkCommandBuffer>& cmdBuffer, uint32_t frameIndex)
{ {
const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex();
renderBatch(lineBatches[static_cast<int>(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); cmdBuffer->BeginLabeledSegment("SHDebugDraw (No Depth Test)");
renderBatch(persistentLineBatches[static_cast<int>(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX); {
cmdBuffer->BeginLabeledSegment("SHDebugDraw (Lines)");
{
renderBatch(lineBatches[static_cast<int>(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX);
renderBatch(persistentLineBatches[static_cast<int>(LineRenderMode::NoDepthTest)], cmdBuffer, FRAME_IDX);
}
cmdBuffer->EndLabeledSegment();
cmdBuffer->BeginLabeledSegment("SHDebugDraw (Meshes)");
{
renderBatch(meshBatches[static_cast<int>(MeshRenderMode::WireNoDepthTest)], cmdBuffer, FRAME_IDX);
}
cmdBuffer->EndLabeledSegment();
}
cmdBuffer->EndLabeledSegment();
}); });
auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth");
subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle<SHVkCommandBuffer>& cmdBuffer, uint32_t frameIndex) subPassWithDepth->AddExteriorDrawCalls([this](Handle<SHVkCommandBuffer>& cmdBuffer, uint32_t frameIndex)
{ {
const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex();
renderBatch(lineBatches[static_cast<int>(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); cmdBuffer->BeginLabeledSegment("SHDebugDraw (Depth Tested)");
renderBatch(persistentLineBatches[static_cast<int>(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX); {
cmdBuffer->BeginLabeledSegment("SHDebugDraw (Lines)");
{
renderBatch(lineBatches[static_cast<int>(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX);
renderBatch(persistentLineBatches[static_cast<int>(LineRenderMode::DepthTested)], cmdBuffer, FRAME_IDX);
}
cmdBuffer->EndLabeledSegment();
cmdBuffer->BeginLabeledSegment("SHDebugDraw (Meshes)");
{
renderBatch(meshBatches[static_cast<int>(MeshRenderMode::WireDepthTested)], cmdBuffer, FRAME_IDX);
}
cmdBuffer->EndLabeledSegment();
}
cmdBuffer->EndLabeledSegment();
}); });
} }
@ -120,6 +167,11 @@ namespace SHADE
DrawLineLoop({ p1, p2, p3 }, color, depthTested); 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 */ /* Persistent Draw Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -138,6 +190,14 @@ namespace SHADE
DrawPersistentLineLoop({ p1, p2, p3 }, color, depthTested); 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 */ /* Helper Draw Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -154,18 +214,41 @@ namespace SHADE
batch.Points.emplace_back(end, color); batch.Points.emplace_back(end, color);
} }
void SHDebugDrawSystem::drawWireBox(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color)
{
const auto* GFX_SYSTEM = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (!GFX_SYSTEM)
{
SHLOG_ERROR("[DebugDraw] Attempted to do debug draw without a graphics system.");
return;
}
Handle<SHMesh> 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 */ /* Helper Batch Functions - Lines */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getLineBatch(bool depthTested) SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getLineBatch(bool depthTested)
{ {
return depthTested ? lineBatches[static_cast<int>(LineRenderMode::DepthTested)] return lineBatches[static_cast<int>(depthTested ? LineRenderMode::DepthTested : LineRenderMode::NoDepthTest)];
: lineBatches[static_cast<int>(LineRenderMode::NoDepthTest)];
} }
SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getPersistentLineBatch(bool depthTested) SHDebugDrawSystem::LinesBatch& SHDebugDrawSystem::getPersistentLineBatch(bool depthTested)
{ {
return depthTested ? persistentLineBatches[static_cast<int>(LineRenderMode::DepthTested)] return persistentLineBatches[static_cast<int>(depthTested ? LineRenderMode::DepthTested : LineRenderMode::NoDepthTest)];
: persistentLineBatches[static_cast<int>(LineRenderMode::NoDepthTest)];
} }
void SHDebugDrawSystem::createLineBatches() void SHDebugDrawSystem::createLineBatches()
@ -255,14 +338,13 @@ namespace SHADE
{ {
if (batch.NumPoints[frameIndex] > 0) if (batch.NumPoints[frameIndex] > 0)
{ {
cmdBuffer->BeginLabeledSegment("SHDebugDraw");
cmdBuffer->BindPipeline(batch.Pipeline); cmdBuffer->BindPipeline(batch.Pipeline);
cmdBuffer->SetLineWidth(LineWidth); cmdBuffer->SetLineWidth(LineWidth);
cmdBuffer->BindVertexBuffer(0, batch.VertexBuffers[frameIndex], 0); cmdBuffer->BindVertexBuffer(0, batch.VertexBuffers[frameIndex], 0);
cmdBuffer->DrawArrays(batch.NumPoints[frameIndex], 1, 0, 0); cmdBuffer->DrawArrays(batch.NumPoints[frameIndex], 1, 0, 0);
} }
} }
void SHDebugDrawSystem::destroyBatch(LinesBatch& batch) void SHDebugDrawSystem::destroyBatch(LinesBatch& batch)
{ {
for (auto& vBuffer : batch.VertexBuffers) for (auto& vBuffer : batch.VertexBuffers)
@ -286,6 +368,24 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Batch Functions - Meshes */ /* 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<int>(mode)];
}
void SHDebugDrawSystem::createMeshBatches() void SHDebugDrawSystem::createMeshBatches()
{ {
auto gfxSys = SHSystemManager::GetSystem<SHGraphicsSystem>(); auto gfxSys = SHSystemManager::GetSystem<SHGraphicsSystem>();
@ -321,9 +421,115 @@ namespace SHADE
{ {
batch.Pipeline = pipeline; 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<uint32_t>(multiDrawSet.InstanceTransforms.size()),
.firstIndex = multiDrawSet.Mesh->FirstIndex,
.vertexOffset = multiDrawSet.Mesh->FirstVertex,
.firstInstance = static_cast<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(batch.InstanceColors.size() * sizeof(SHVec4)),
vk::BufferUsageFlagBits::eVertexBuffer,
"Debug Draw Mesh Batch Instance Color Buffer"
);
}
}
void SHDebugDrawSystem::renderBatch(MeshBatch& batch, Handle<SHVkCommandBuffer> 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<uint32_t>(batch.MDIData.size()));
}
void SHDebugDrawSystem::destroyBatch(MeshBatch& batch) 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) if (buffer)
{ {

View File

@ -31,6 +31,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Forward Declarations */ /* Forward Declarations */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
class SHGraphicsSystem;
class SHVkBuffer; class SHVkBuffer;
class SHMesh; class SHMesh;
class SHVkPipeline; class SHVkPipeline;
@ -99,7 +100,7 @@ namespace SHADE
template<typename IterType> template<typename IterType>
void DrawLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); 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 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 */ /* Persistent Draw Functions */
@ -109,6 +110,10 @@ namespace SHADE
template<typename IterType> template<typename IterType>
void DrawPersistentLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); 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); void DrawPersistentTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color = SHColour::WHITE, bool depthTested = false);
/// <summary>
/// Clears any persistent drawn debug primitives.
/// </summary>
void ClearPersistentDraws();
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -151,26 +156,25 @@ namespace SHADE
/*-------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
/*-------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------*/
struct InstanceData
{
SHMatrix Transform;
SHVec4 Color;
};
struct MultiDrawSet struct MultiDrawSet
{ {
Handle<SHMesh> Mesh; Handle<SHMesh> Mesh;
std::vector<InstanceData> Instances; std::vector<SHMatrix> InstanceTransforms;
std::vector<SHVec4> InstanceColors;
}; };
/*-------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------*/
// CPU Buffers // CPU Buffers
std::vector<MultiDrawSet> SubBatches; std::unordered_map<Handle<SHMesh>, MultiDrawSet> SubBatches;
std::vector<InstanceData> InstanceData; std::vector<SHMatrix> InstanceTransforms;
std::vector<SHVec4> InstanceColors;
std::vector<vk::DrawIndexedIndirectCommand> MDIData;
// GPU Buffers // GPU Buffers
TripleBuffer MDIBuffer; TripleBuffer MDIBuffer;
TripleBuffer InstanceDataBuffer; TripleBuffer InstanceTransformBuffer;
TripleBuffer InstanceColorBuffer;
}; };
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -183,6 +187,8 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
// References
SHGraphicsSystem* gfxSystem = nullptr;
// Batches // Batches
std::array<LinesBatch, LINE_MODE_COUNT> lineBatches; std::array<LinesBatch, LINE_MODE_COUNT> lineBatches;
std::array<LinesBatch, LINE_MODE_COUNT> persistentLineBatches; std::array<LinesBatch, LINE_MODE_COUNT> persistentLineBatches;
@ -196,6 +202,7 @@ namespace SHADE
void drawLine(LinesBatch& batch, const SHVec3& start, const SHVec3& end, const SHColour& color); void drawLine(LinesBatch& batch, const SHVec3& start, const SHVec3& end, const SHColour& color);
template<typename IterType> template<typename IterType>
void drawLineLoop(LinesBatch& batch, IterType pointListBegin, IterType pointListEnd, const SHColour& color); 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 */ /* Helper Batch Functions - Lines */
@ -212,6 +219,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Helper Batch Functions - Meshes */ /* Helper Batch Functions - Meshes */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
MeshBatch& getMeshBatch(bool filled, bool depthTested);
void createMeshBatches(); void createMeshBatches();
void initBatch(MeshBatch& batch, Handle<SHVkPipeline> pipeline); void initBatch(MeshBatch& batch, Handle<SHVkPipeline> pipeline);
void prepareBatch(MeshBatch& batch, uint32_t frameIndex); void prepareBatch(MeshBatch& batch, uint32_t frameIndex);

View File

@ -413,6 +413,7 @@ namespace SHADE
// Create default meshes // Create default meshes
primitiveMeshes[static_cast<int>(PrimitiveType::Cube)] = SHPrimitiveGenerator::Cube(meshLibrary); primitiveMeshes[static_cast<int>(PrimitiveType::Cube)] = SHPrimitiveGenerator::Cube(meshLibrary);
primitiveMeshes[static_cast<int>(PrimitiveType::LineCube)] = SHPrimitiveGenerator::LineCube(meshLibrary);
primitiveMeshes[static_cast<int>(PrimitiveType::Sphere)] = SHPrimitiveGenerator::Sphere(meshLibrary); primitiveMeshes[static_cast<int>(PrimitiveType::Sphere)] = SHPrimitiveGenerator::Sphere(meshLibrary);
BuildMeshBuffers(); BuildMeshBuffers();
@ -838,6 +839,7 @@ namespace SHADE
{ {
case PrimitiveType::Cube: case PrimitiveType::Cube:
case PrimitiveType::Sphere: case PrimitiveType::Sphere:
case PrimitiveType::LineCube:
return primitiveMeshes[static_cast<int>(type)]; return primitiveMeshes[static_cast<int>(type)];
default: default:
return {}; return {};
@ -1121,7 +1123,7 @@ namespace SHADE
( (
device, SHPipelineLayoutParams device, SHPipelineLayoutParams
{ {
.shaderModules = { (triMesh ? debugMeshVertShader : debugVertShader) , debugFragShader }, .shaderModules = { (instanced ? debugMeshVertShader : debugVertShader) , debugFragShader },
.globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts()
} }
); );
@ -1139,7 +1141,7 @@ namespace SHADE
SHVertexInputState debugDrawVertexInputState; SHVertexInputState debugDrawVertexInputState;
if (instanced) 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::MAT_4D) }); // 1: Instance Transform Matrix (4 Slots)
debugDrawVertexInputState.AddBinding(true , true , { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // 5: Instance Color debugDrawVertexInputState.AddBinding(true , true , { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // 5: Instance Color
} }

View File

@ -70,9 +70,10 @@ namespace SHADE
enum class PrimitiveType enum class PrimitiveType
{ {
Cube, Cube,
Sphere Sphere,
LineCube
}; };
static constexpr int MAX_PRIMITIVE_TYPES = 2; static constexpr int MAX_PRIMITIVE_TYPES = 3;
enum class DebugDrawPipelineType enum class DebugDrawPipelineType
{ {
LineNoDepthTest, LineNoDepthTest,
@ -386,6 +387,7 @@ namespace SHADE
Handle<SHVkPipeline> GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept; Handle<SHVkPipeline> GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept;
uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); }
SHFontLibrary const& GetFontLibrary (void) const noexcept; SHFontLibrary const& GetFontLibrary (void) const noexcept;
const SHMeshLibrary& GetMeshLibrary() const noexcept { return meshLibrary; };
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Getters */ /* Getters */

View File

@ -23,6 +23,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHMeshData SHPrimitiveGenerator::cubeMesh; SHMeshData SHPrimitiveGenerator::cubeMesh;
SHMeshData SHPrimitiveGenerator::sphereMesh; SHMeshData SHPrimitiveGenerator::sphereMesh;
SHMeshData SHPrimitiveGenerator::lineCubeMesh;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Primitive Generation Functions */ /* Primitive Generation Functions */
@ -207,14 +208,14 @@ namespace SHADE
return addMeshDataTo(cubeMesh, meshLibrary); return addMeshDataTo(cubeMesh, meshLibrary);
} }
Handle<SHADE::SHMesh> SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept Handle<SHMesh> SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept
{ {
if (cubeMesh.VertexPositions.empty()) if (cubeMesh.VertexPositions.empty())
cubeMesh = Cube(); cubeMesh = Cube();
return addMeshDataTo(cubeMesh, gfxSystem); return addMeshDataTo(cubeMesh, gfxSystem);
} }
SHADE::SHMeshData SHPrimitiveGenerator::Sphere() noexcept SHMeshData SHPrimitiveGenerator::Sphere() noexcept
{ {
SHMeshData meshData; SHMeshData meshData;
@ -265,7 +266,7 @@ namespace SHADE
return meshData; return meshData;
} }
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept Handle<SHMesh> SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept
{ {
if (sphereMesh.VertexPositions.empty()) if (sphereMesh.VertexPositions.empty())
sphereMesh = Sphere(); sphereMesh = Sphere();
@ -273,7 +274,7 @@ namespace SHADE
return addMeshDataTo(sphereMesh, meshLibrary); return addMeshDataTo(sphereMesh, meshLibrary);
} }
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept Handle<SHMesh> SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept
{ {
if (sphereMesh.VertexPositions.empty()) if (sphereMesh.VertexPositions.empty())
sphereMesh = Sphere(); sphereMesh = Sphere();
@ -281,6 +282,65 @@ namespace SHADE
return addMeshDataTo(sphereMesh, gfxSystem); 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<SHMesh> SHPrimitiveGenerator::LineCube(SHMeshLibrary& meshLibrary) noexcept
{
if (lineCubeMesh.VertexPositions.empty())
lineCubeMesh = LineCube();
return addMeshDataTo(lineCubeMesh, meshLibrary);
}
Handle<SHMesh> SHPrimitiveGenerator::LineCube(SHGraphicsSystem& gfxSystem) noexcept
{
if (lineCubeMesh.VertexPositions.empty())
lineCubeMesh = LineCube();
return addMeshDataTo(lineCubeMesh, gfxSystem);
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -288,7 +348,7 @@ namespace SHADE
{ {
return meshLibrary.AddMesh return meshLibrary.AddMesh
( (
static_cast<uint32_t>(meshData.VertexPositions.size()), static_cast<uint32_t>(meshData.VertexPositions.size()),
meshData.VertexPositions.data(), meshData.VertexPositions.data(),
meshData.VertexTexCoords.data(), meshData.VertexTexCoords.data(),
meshData.VertexTangents.data(), meshData.VertexTangents.data(),
@ -302,12 +362,12 @@ namespace SHADE
{ {
return gfxSystem.AddMesh return gfxSystem.AddMesh
( (
static_cast<uint32_t>(meshData.VertexPositions.size()), static_cast<uint32_t>(meshData.VertexPositions.size()),
meshData.VertexPositions.data(), meshData.VertexPositions.data(),
meshData.VertexTexCoords.data(), meshData.VertexTexCoords.data(),
meshData.VertexTangents.data(), meshData.VertexTangents.data(),
meshData.VertexNormals.data(), meshData.VertexNormals.data(),
static_cast<uint32_t>(meshData.Indices.size()), static_cast<uint32_t>(meshData.Indices.size()),
meshData.Indices.data() meshData.Indices.data()
); );
} }

View File

@ -43,7 +43,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Primitive Generation Functions */ /* Primitive Generation Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/***********************************************************************************/ /***********************************************************************************/
/*! /*!
\brief \brief
@ -116,6 +116,44 @@ namespace SHADE
*/ */
/***********************************************************************************/ /***********************************************************************************/
[[nodiscard]] static Handle<SHMesh> Sphere(SHGraphicsSystem& gfxSystem) noexcept; [[nodiscard]] static Handle<SHMesh> 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<SHMesh> 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<SHMesh> LineCube(SHGraphicsSystem& gfxSystem) noexcept;
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -129,5 +167,6 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
static SHMeshData cubeMesh; static SHMeshData cubeMesh;
static SHMeshData sphereMesh; static SHMeshData sphereMesh;
static SHMeshData lineCubeMesh;
}; };
} }

View File

@ -71,6 +71,11 @@ namespace SHADE
//dbgDrawSys->DrawSphere(color, pos, radius); //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) void SHDebugDraw::PersistentLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt)
{ {
dbgDrawSys->DrawPersistentLine(startPt, endPt, color); dbgDrawSys->DrawPersistentLine(startPt, endPt, color);
@ -103,7 +108,6 @@ namespace SHADE
void SHDebugDraw::ClearPersistentDraws() void SHDebugDraw::ClearPersistentDraws()
{ {
//dbgDrawSys->ClearPersistentDraws(); dbgDrawSys->ClearPersistentDraws();
} }
}
}

View File

@ -22,6 +22,7 @@ namespace SHADE
class SHDebugDrawSystem; class SHDebugDrawSystem;
class SHVec4; class SHVec4;
class SHVec3; class SHVec3;
class SHMatrix;
class SHColour; class SHColour;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -92,6 +93,8 @@ namespace SHADE
/// <param name="size">Size of the rendered sphere.</param> /// <param name="size">Size of the rendered sphere.</param>
static void Sphere(const SHVec4& color, const SHVec3& pos, double radius); static void Sphere(const SHVec4& color, const SHVec3& pos, double radius);
static void WireCube(const SHMatrix& mat, const SHVec4& color);
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Persistent Draw Functions */ /* Persistent Draw Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/