diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 15c8ec5d..fc157d7d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -710,6 +710,11 @@ namespace SHADE return resourceManager.Create(materialInst->GetBaseMaterial()); } + std::pair, typename SHResourceHub::dense_iterator> SHGraphicsSystem::GetAllMaterialInstances() + { + return resourceManager.GetDenseAccess(); + } + void SHGraphicsSystem::RemoveMaterialInstance(Handle materialInstance) { resourceManager.Free(materialInstance); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index a5a5ada0..569477d8 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -156,6 +156,7 @@ namespace SHADE Handle AddOrGetBaseMaterialInstance(); Handle AddOrGetBaseMaterialInstance(Handle material); Handle AddMaterialInstanceCopy(Handle materialInst); + std::pair, typename SHResourceHub::dense_iterator> GetAllMaterialInstances(); void RemoveMaterialInstance(Handle materialInstance); Handle GetDefaultMaterial() { return defaultMaterial; } Handle GetDefaultMaterialInstance() { return AddOrGetBaseMaterialInstance(defaultMaterial); } @@ -166,10 +167,10 @@ namespace SHADE /*******************************************************************************/ /*! - \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. + \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. diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp index b27f48b9..528472a7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp @@ -6,91 +6,113 @@ #include "Graphics/Shaders/BlockInterface/SHShaderBlockInterface.h" #include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec4.h" +#include "ECS_Base/Managers/SHSystemManager.h" +#include "SHGraphicsSystem.h" +#include "SHMaterialInstance.h" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Pipeline Functions */ - /*---------------------------------------------------------------------------------*/ - void SHMaterial::SetPipeline(Handle _pipeline) - { - pipeline = _pipeline; + /*---------------------------------------------------------------------------------*/ + /* Pipeline Functions */ + /*---------------------------------------------------------------------------------*/ + void SHMaterial::SetPipeline(Handle _pipeline) + { + // Reassignment, we ignore + if (_pipeline == pipeline) + return; - // Set up properties based on the pipeline - if (!pipeline) - { - // Clear memory and all that - propMemory.reset(); - return; - } + pipeline = _pipeline; - // Allocate memory for properties - const Handle SHADER_INFO = GetShaderBlockInterface(); - propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; - if (propMemorySize <= 0) - { - propMemory.reset(); - } - else - { - propMemory.reset(new char[propMemorySize]); - } - ResetProperties(); - } - - Handle SHMaterial::GetPipeline() const - { - return pipeline; - } - - /*---------------------------------------------------------------------------------*/ - /* Property Functions */ - /*---------------------------------------------------------------------------------*/ - void SHMaterial::ResetProperties() + // Set up properties based on the pipeline + if (!pipeline) { - // Reset all the properties to default values - if (propMemory) - memset(propMemory.get(), 0, propMemorySize); - - // Initialize Vectors to all 1.0 by default - const Handle SHADER_INFO = GetShaderBlockInterface(); - for (int i = 0; i < SHADER_INFO->GetVariableCount(); ++i) - { - const auto& VAR = SHADER_INFO->GetVariable(i); - switch (VAR->type) - { - case SHShaderBlockInterface::Variable::Type::VECTOR3: - setPropertyUnsafe(VAR->offset, SHVec3::One); - break; - case SHShaderBlockInterface::Variable::Type::VECTOR4: - setPropertyUnsafe(VAR->offset, SHVec4::One); - break; - } - } + // Clear memory and all that + propMemory.reset(); + return; } - void SHMaterial::ExportProperties(void* dest) const noexcept - { - if (propMemory) - memcpy(dest, propMemory.get(), propMemorySize); - } + // Allocate memory for properties + const Handle SHADER_INFO = GetShaderBlockInterface(); + propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; + if (propMemorySize <= 0) + { + propMemory.reset(); + } + else + { + propMemory.reset(new char[propMemorySize]); + } + ResetProperties(); - size_t SHMaterial::GetPropertiesMemorySize() const noexcept - { - const Handle SHADER_INFO = GetShaderBlockInterface(); - return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; - } + // Search all material instances for instances that use this base material + // to force a reset of properties + auto gfxSystem = SHSystemManager::GetSystem(); + if (gfxSystem) + { + auto [matInstBegin, matInstEnd] = gfxSystem->GetAllMaterialInstances(); + for (auto iter = matInstBegin; iter != matInstEnd; ++iter) + { + if (iter->GetBaseMaterial() == GetHandle()) + { + iter->ResetProperties(); + } + } + } + } - /*---------------------------------------------------------------------------------*/ - /* Helper Functions */ - /*---------------------------------------------------------------------------------*/ - Handle SHMaterial::GetShaderBlockInterface() const noexcept - { - return pipeline->GetPipelineLayout()->GetShaderBlockInterface - ( - SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, - SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA, - vk::ShaderStageFlagBits::eFragment - ); - } + Handle SHMaterial::GetPipeline() const + { + return pipeline; + } + + /*---------------------------------------------------------------------------------*/ + /* Property Functions */ + /*---------------------------------------------------------------------------------*/ + void SHMaterial::ResetProperties() + { + // Reset all the properties to default values + if (propMemory) + memset(propMemory.get(), 0, propMemorySize); + + // Initialize Vectors to all 1.0 by default + const Handle SHADER_INFO = GetShaderBlockInterface(); + for (int i = 0; i < SHADER_INFO->GetVariableCount(); ++i) + { + const auto& VAR = SHADER_INFO->GetVariable(i); + switch (VAR->type) + { + case SHShaderBlockInterface::Variable::Type::VECTOR3: + setPropertyUnsafe(VAR->offset, SHVec3::One); + break; + case SHShaderBlockInterface::Variable::Type::VECTOR4: + setPropertyUnsafe(VAR->offset, SHVec4::One); + break; + } + } + } + + void SHMaterial::ExportProperties(void* dest) const noexcept + { + if (propMemory) + memcpy(dest, propMemory.get(), propMemorySize); + } + + size_t SHMaterial::GetPropertiesMemorySize() const noexcept + { + const Handle SHADER_INFO = GetShaderBlockInterface(); + return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; + } + + /*---------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*---------------------------------------------------------------------------------*/ + Handle SHMaterial::GetShaderBlockInterface() const noexcept + { + return pipeline->GetPipelineLayout()->GetShaderBlockInterface + ( + SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, + SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA, + vk::ShaderStageFlagBits::eFragment + ); + } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h index 964f9e34..201090b7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h @@ -35,7 +35,7 @@ namespace SHADE Describes a Pipeline along with it's associated properties for this instance. */ /***********************************************************************************/ - class SHMaterial + class SHMaterial : public ISelfHandle { public: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.cpp index 350580bf..b30acfb8 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.cpp @@ -31,7 +31,6 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ void SHMaterialInstance::ResetProperties() noexcept { - // Reset all the properties to default values memset(dataStore.get(), 0, dataStoreSize); overrideData.clear(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp index 495a3d37..05bd8813 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp @@ -34,7 +34,7 @@ namespace SHADE { colorBlendState.attachments.push_back(vk::PipelineColorBlendAttachmentState { - .blendEnable = SHVkUtil::IsBlendCompatible (subpass->GetFormatFromAttachmentReference(att.attachment)) ? true : false, + .blendEnable = SHVkUtil::IsBlendCompatible(subpass->GetFormatFromAttachmentReference(att.attachment)), .srcColorBlendFactor = vk::BlendFactor::eSrcAlpha, .dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha, .colorBlendOp = vk::BlendOp::eAdd, diff --git a/SHADE_Engine/src/Resource/SHResourceLibrary.h b/SHADE_Engine/src/Resource/SHResourceLibrary.h index 46ae4572..a6f772fe 100644 --- a/SHADE_Engine/src/Resource/SHResourceLibrary.h +++ b/SHADE_Engine/src/Resource/SHResourceLibrary.h @@ -38,6 +38,11 @@ namespace SHADE class SHResourceLibrary : public SHResourceLibraryBase { public: + /*-----------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------*/ + using dense_iterator = typename SparseSet::dense_iterator; + /*-----------------------------------------------------------------------------*/ /* Constructor */ /*-----------------------------------------------------------------------------*/ @@ -74,6 +79,16 @@ namespace SHADE /// Read-only reference to the resource object. const T& Get(Handle handle) const; + /*-----------------------------------------------------------------------------*/ + /* Direct Dense Access Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Provides access to the dense array of the SparseSet. + /// These iterators should not be used to manipulate the underlying vector. + /// + /// Pair of begin and end iterators to the dense vector. + std::pair GetDenseAccess(); + private: /*-----------------------------------------------------------------------------*/ /* Data Members */ @@ -96,6 +111,12 @@ namespace SHADE class SHResourceHub final { public: + /*-----------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------*/ + template + using dense_iterator = typename SHResourceLibrary::dense_iterator; + /*-----------------------------------------------------------------------------*/ /* Constructors/Destructors */ /*-----------------------------------------------------------------------------*/ @@ -138,6 +159,18 @@ namespace SHADE template const T& Get(Handle handle) const; + /*-----------------------------------------------------------------------------*/ + /* Direct Dense Access Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Provides access to the dense array of the SparseSet. + /// These iterators should not be used to manipulate the underlying vector. + /// + /// Type of resource to access. + /// Pair of begin and end iterators to the dense vector. + template + std::pair, dense_iterator> GetDenseAccess(); + private: /*-----------------------------------------------------------------------------*/ /* Type Definition */ diff --git a/SHADE_Engine/src/Resource/SHResourceLibrary.hpp b/SHADE_Engine/src/Resource/SHResourceLibrary.hpp index 411f6bf5..98ad91a3 100644 --- a/SHADE_Engine/src/Resource/SHResourceLibrary.hpp +++ b/SHADE_Engine/src/Resource/SHResourceLibrary.hpp @@ -79,6 +79,15 @@ namespace SHADE return objects[handle.GetId().Data.Index]; } + /*---------------------------------------------------------------------------------*/ + /* ResourceLibrary - Direct Dense Access Functions */ + /*---------------------------------------------------------------------------------*/ + template + std::pair::dense_iterator, typename SHResourceLibrary::dense_iterator> SHResourceLibrary::GetDenseAccess() + { + return objects.GetDenseAccess(); + } + /*---------------------------------------------------------------------------------*/ /* ResourceLibrary - Helper Functions */ /*---------------------------------------------------------------------------------*/ @@ -105,7 +114,7 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* ResourceManager - Usage Functions */ + /* ResourceHub - Usage Functions */ /*---------------------------------------------------------------------------------*/ template Handle SHResourceHub::Create(Args&&... args) @@ -132,7 +141,7 @@ namespace SHADE } /*-----------------------------------------------------------------------------*/ - /* ResourceManager - Helper Functions */ + /* ResourceHub - Helper Functions */ /*-----------------------------------------------------------------------------*/ template SHResourceLibrary& SHResourceHub::getLibrary() @@ -161,4 +170,13 @@ namespace SHADE { return const_cast(this).getLibrary(); } + + /*---------------------------------------------------------------------------------*/ + /* ResourceHub - Direct Dense Access Functions */ + /*---------------------------------------------------------------------------------*/ + template + std::pair, typename SHResourceHub::dense_iterator> SHResourceHub::GetDenseAccess() + { + return getLibrary().GetDenseAccess(); + } } diff --git a/SHADE_Engine/src/Resource/SHResourceManager.h b/SHADE_Engine/src/Resource/SHResourceManager.h index d660ada7..15c37f83 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.h +++ b/SHADE_Engine/src/Resource/SHResourceManager.h @@ -13,8 +13,6 @@ of DigiPen Institute of Technology is prohibited. // STL Includes #include - -namespace SHADE { class SHMaterial; } // Project Includes #include "SH_API.h" #include "SHResourceLibrary.h" @@ -31,17 +29,26 @@ namespace SHADE { class SHMaterial; } namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + class SHMaterial; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ /// /// Template structs that maps a resource to their loaded asset representation type. /// template struct SHResourceLoader { using AssetType = void; }; - template<> struct SHResourceLoader { using AssetType = SHMeshData; }; + template<> struct SHResourceLoader { using AssetType = SHMeshData; }; template<> struct SHResourceLoader { using AssetType = SHTextureAsset; }; template<> struct SHResourceLoader { using AssetType = SHShaderAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialSpec; }; -/// + + /// /// Static class responsible for loading and caching runtime resources from their /// serialised Asset IDs. /// diff --git a/SHADE_Engine/src/Resource/SparseSet.h b/SHADE_Engine/src/Resource/SparseSet.h index fb4a8311..3d763b01 100644 --- a/SHADE_Engine/src/Resource/SparseSet.h +++ b/SHADE_Engine/src/Resource/SparseSet.h @@ -49,6 +49,7 @@ namespace SHADE using const_pointer = const T*; using reference = T&; using const_reference = const T&; + using dense_iterator = typename std::vector::iterator; /*-----------------------------------------------------------------------------*/ /* Constructors/Destructors */ @@ -59,10 +60,6 @@ namespace SHADE SparseSet(); ~SparseSet() = default; - //// Disallow moving or copying - //SparseSet(const SparseSet&) = delete; - //SparseSet(SparseSet&&) = delete; - /*-----------------------------------------------------------------------------*/ /* Usage Functions */ /*-----------------------------------------------------------------------------*/ @@ -192,6 +189,16 @@ namespace SHADE /// const T& operator[](index_type idx) const; + /*-----------------------------------------------------------------------------*/ + /* Direct Dense Access Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Provides access to the dense array of the SparseSet. + /// These iterators should not be used to manipulate the underlying vector. + /// + /// Pair of begin and end iterators to the dense vector. + std::pair GetDenseAccess(); + protected: /*-----------------------------------------------------------------------------*/ /* Constants */ diff --git a/SHADE_Engine/src/Resource/SparseSet.hpp b/SHADE_Engine/src/Resource/SparseSet.hpp index 816ca432..b6c7a511 100644 --- a/SHADE_Engine/src/Resource/SparseSet.hpp +++ b/SHADE_Engine/src/Resource/SparseSet.hpp @@ -143,4 +143,13 @@ namespace SHADE { return at(idx); } + + /*---------------------------------------------------------------------------------*/ + /* Direct Dense Access Functions */ + /*---------------------------------------------------------------------------------*/ + template + std::pair::dense_iterator, typename SparseSet::dense_iterator> SparseSet::GetDenseAccess() + { + return { denseArray.begin(), denseArray.end() }; + } } \ No newline at end of file