From b9185eab185fe038ec9e50fb6b73dc86f71c4e7f Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 18 Sep 2022 23:23:48 +0800 Subject: [PATCH] Added Mesh Registration Functions to SHGraphicsSystem and a SHGraphicsSystem compatible version of mesh generation functions for SHPrimitiveGenerator --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 21 ++++++ .../MiddleEnd/Interface/SHGraphicsSystem.h | 71 +++++++++++++++++-- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 54 +++++++++----- .../MiddleEnd/Meshes/SHPrimitiveGenerator.h | 25 +++++-- 4 files changed, 140 insertions(+), 31 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 09177993..cc3bd124 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -355,6 +355,27 @@ namespace SHADE { resourceManager.Free(materialInstance); } + + /*---------------------------------------------------------------------------------*/ + /* Mesh Registration Functions */ + /*---------------------------------------------------------------------------------*/ + SHADE::Handle SHGraphicsSystem::AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices) + { + return meshLibrary.AddMesh(vertexCount, positions, texCoords, tangents, normals, indexCount, indices); + } + + void SHGraphicsSystem::RemoveMesh(Handle mesh) + { + meshLibrary.RemoveMesh(mesh); + } + + void SHGraphicsSystem::BuildMeshBuffers() + { + transferCmdBuffer->BeginRecording(); + meshLibrary.BuildBuffers(device, transferCmdBuffer); + transferCmdBuffer->EndRecording(); + queue->SubmitCommandBuffer({ transferCmdBuffer }); + } void SHGraphicsSystem::SetWindow(SHWindow* wind) noexcept { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 1c1d7710..7c8cd6a3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -25,6 +25,7 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/System/SHSystemRoutine.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Graphics/RenderGraph/SHRenderGraph.h" +#include "SHMeshLibrary.h" namespace SHADE { @@ -120,6 +121,67 @@ namespace SHADE Handle AddMaterialInstance(Handle material); void RemoveMaterialInstance(Handle materialInstance); + /*-----------------------------------------------------------------------------*/ + /* Mesh Registration Functions */ + /*-----------------------------------------------------------------------------*/ + /*******************************************************************************/ + /*! + + \brief + Adds a mesh to the Mesh Library. But this does not mean that the meshes have + been added yet. A call to "BuildBuffers()" is required to transfer all + meshes into the GPU. + + \param vertexCount + Number of vertices in this Mesh. + \param positions + Pointer to the first in a contiguous array of SHMathVec3s that define vertex + positions. + \param texCoords + Pointer to the first in a contiguous array of SHMathVec2s that define vertex + texture coordinates. + \param tangents + Pointer to the first in a contiguous array of SHMathVec3s that define vertex + tangents. + \param normals + Pointer to the first in a contiguous array of SHMathVec3s that define vertex + normals. + \param indexCount + Number of indices in this mesh. + \param indices + Pointer to the first in a contiguous array of uint32_ts that define mesh + indices. + + \return + Handle to the created Mesh. This is not valid to be used until a call to + BuildBuffers(). + + */ + /*******************************************************************************/ + Handle AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices); + /*******************************************************************************/ + /*! + + \brief + Removes a mesh from the MeshLibrary. But this does not mean that the meshes + have been removed yet. A call to "BuildBuffers()" is required to finalise all + changes. + + \param mesh + Handle to the mesh to remove. + + */ + /*******************************************************************************/ + void RemoveMesh(Handle mesh); + /***************************************************************************/ + /*! + + \brief + Finalises all changes to the MeshLibrary into the GPU buffers. + + */ + /***************************************************************************/ + void BuildMeshBuffers(); /*-----------------------------------------------------------------------------*/ /* Setters */ @@ -154,16 +216,13 @@ namespace SHADE Handle transferCmdPool; Handle transferCmdBuffer; SHRenderContext renderContext; - std::array, 2> graphSemaphores; - + std::array, 2> graphSemaphores; // Not Owned Resources SHWindow* window; - - std::vector> globalDescSetLayouts; - // Middle End Resources - ResourceManager resourceManager; + ResourceManager resourceManager; + SHMeshLibrary meshLibrary; // Viewports Handle defaultViewport; // Whole screen std::vector> viewports; // Additional viewports diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index 59f43712..d096da53 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -13,6 +13,8 @@ of DigiPen Institute of Technology is prohibited. #include "SHpch.h" #include "SHPrimitiveGenerator.h" +#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" + namespace SHADE { SHMeshData SHPrimitiveGenerator::Cube() noexcept @@ -34,10 +36,10 @@ namespace SHADE SHVec3( 0.5f, -0.5f, -0.5f), // top - 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), + 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), // bottom SHVec3(-0.5f, -0.5f, -0.5f), @@ -66,10 +68,10 @@ namespace SHADE SHVec2(0.0f, 0.0f), - SHVec2(0.0f, 1.0f), - SHVec2(1.0f, 1.0f), - SHVec2(1.0f, 0.0f), - SHVec2(0.0f, 0.0f), + SHVec2(0.0f, 1.0f), + SHVec2(1.0f, 1.0f), + SHVec2(1.0f, 0.0f), + SHVec2(0.0f, 0.0f), SHVec2(0.0f, 1.0f), @@ -148,7 +150,7 @@ namespace SHADE SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, -1.0f), - SHVec3(0.0f, 0.0f, -1.0f), + SHVec3(0.0f, 0.0f, -1.0f), // top SHVec3(0.0f, 1.0f, 0.0f), @@ -160,13 +162,13 @@ namespace SHADE SHVec3(0.0f, -1.0f, 0.0f), SHVec3(0.0f, -1.0f, 0.0f), SHVec3(0.0f, -1.0f, 0.0f), - SHVec3(0.0f, -1.0f, 0.0f), + SHVec3(0.0f, -1.0f, 0.0f), // right SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 0.0f, 0.0f), - SHVec3(1.0f, 0.0f, 0.0f), + SHVec3(1.0f, 0.0f, 0.0f), // left SHVec3(-1.0f, 0.0f, 0.0f), @@ -192,15 +194,29 @@ namespace SHADE { SHMeshData meshData = Cube(); return meshLibrary.AddMesh - ( - meshData.VertexPositions.size(), - meshData.VertexPositions.data(), - meshData.VertexTexCoords.data(), - meshData.VertexTangents.data(), - meshData.VertexNormals.data(), - meshData.Indices.size(), - meshData.Indices.data() + ( + meshData.VertexPositions.size(), + meshData.VertexPositions.data(), + meshData.VertexTexCoords.data(), + meshData.VertexTangents.data(), + meshData.VertexNormals.data(), + meshData.Indices.size(), + meshData.Indices.data() ); } + Handle SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept + { + SHMeshData meshData = Cube(); + return gfxSystem.AddMesh + ( + meshData.VertexPositions.size(), + meshData.VertexPositions.data(), + meshData.VertexTexCoords.data(), + meshData.VertexTangents.data(), + meshData.VertexNormals.data(), + meshData.Indices.size(), + meshData.Indices.data() + ); + } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index 88b243f5..493d7e35 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -17,12 +17,12 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { - /*************************************************************************************/ - /*! - \brief - Static class that contains functions for generating 3D primitives. - */ - /*************************************************************************************/ + /*************************************************************************************/ + /*! + \brief + Static class that contains functions for generating 3D primitives. + */ + /*************************************************************************************/ class SHPrimitiveGenerator { /*---------------------------------------------------------------------------------*/ @@ -56,5 +56,18 @@ namespace SHADE */ /***********************************************************************************/ [[nodiscard]] static Handle Cube(SHMeshLibrary& meshLibrary) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a cube and constructs a SHMesh using the SHGraphicsSystem provided. + + \param gfxSystem + Reference to the SHGraphicsSystem to produce and store a cube mesh in. + + \return + SHMesh object that points to the generated cube mesh in the SHGraphicsSystem. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle Cube(SHGraphicsSystem& gfxSystem) noexcept; }; } \ No newline at end of file