Materials now keep track of their modified state and associated MaterialInstances keep track of changes to their base

This commit is contained in:
Kah Wei 2022-11-16 17:46:36 +08:00
parent 760c0386a9
commit 506b8836fe
5 changed files with 56 additions and 46 deletions

View File

@ -12,9 +12,9 @@
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Pipeline Functions */ /* Pipeline Functions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHMaterial::SetPipeline(Handle<SHVkPipeline> _pipeline) void SHMaterial::SetPipeline(Handle<SHVkPipeline> _pipeline)
{ {
// Reassignment, we ignore // Reassignment, we ignore
@ -24,40 +24,26 @@ namespace SHADE
pipeline = _pipeline; pipeline = _pipeline;
// Set up properties based on the pipeline // Set up properties based on the pipeline
if (!pipeline) if (pipeline)
{ {
// Clear memory and all that // Allocate memory for properties
propMemory.reset(); const Handle<SHShaderBlockInterface> SHADER_INFO = GetShaderBlockInterface();
return; propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0;
} if (propMemorySize <= 0)
// Allocate memory for properties
const Handle<SHShaderBlockInterface> SHADER_INFO = GetShaderBlockInterface();
propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0;
if (propMemorySize <= 0)
{
propMemory.reset();
}
else
{
propMemory.reset(new char[propMemorySize]);
}
ResetProperties();
// Search all material instances for instances that use this base material
// to force a reset of properties
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSystem)
{
auto [matInstBegin, matInstEnd] = gfxSystem->GetAllMaterialInstances();
for (auto iter = matInstBegin; iter != matInstEnd; ++iter)
{ {
if (iter->GetBaseMaterial() == GetHandle()) propMemory.reset();
{ }
iter->ResetProperties(); else
} {
propMemory.reset(new char[propMemorySize]);
} }
} }
// Reset since pipeline changed
ResetProperties();
// Mark changed so that we know to update dependent material instances
propertiesChanged = true;
} }
Handle<SHVkPipeline> SHMaterial::GetPipeline() const Handle<SHVkPipeline> SHMaterial::GetPipeline() const
@ -65,9 +51,9 @@ namespace SHADE
return pipeline; return pipeline;
} }
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Property Functions */ /* Property Functions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHMaterial::ResetProperties() void SHMaterial::ResetProperties()
{ {
// Reset all the properties to default values // Reset all the properties to default values
@ -89,6 +75,8 @@ namespace SHADE
break; break;
} }
} }
propertiesChanged = true;
} }
void SHMaterial::ExportProperties(void* dest) const noexcept void SHMaterial::ExportProperties(void* dest) const noexcept
@ -103,9 +91,9 @@ namespace SHADE
return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0;
} }
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
Handle<SHShaderBlockInterface> SHMaterial::GetShaderBlockInterface() const noexcept Handle<SHShaderBlockInterface> SHMaterial::GetShaderBlockInterface() const noexcept
{ {
return pipeline->GetPipelineLayout()->GetShaderBlockInterface return pipeline->GetPipelineLayout()->GetShaderBlockInterface
@ -115,4 +103,12 @@ namespace SHADE
vk::ShaderStageFlagBits::eFragment vk::ShaderStageFlagBits::eFragment
); );
} }
/*-----------------------------------------------------------------------------------*/
/* Query Functions */
/*-----------------------------------------------------------------------------------*/
void SHMaterial::ClearChangeFlag() noexcept
{
propertiesChanged = false;
}
} }

View File

@ -68,6 +68,10 @@ namespace SHADE
/* Query Functions */ /* Query Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
Handle<SHShaderBlockInterface> GetShaderBlockInterface() const noexcept; Handle<SHShaderBlockInterface> GetShaderBlockInterface() const noexcept;
bool HasPipelineChanged() const noexcept { return pipelineChanged; }
bool HasPropertiesChanged() const noexcept { return propertiesChanged; }
bool HasChanged() const noexcept { return pipelineChanged || propertiesChanged; }
void ClearChangeFlag() noexcept;
private: private:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -76,6 +80,8 @@ namespace SHADE
Handle<SHVkPipeline> pipeline; Handle<SHVkPipeline> pipeline;
std::unique_ptr<char> propMemory; std::unique_ptr<char> propMemory;
Byte propMemorySize = 0; Byte propMemorySize = 0;
bool propertiesChanged = true;
bool pipelineChanged = true;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */

View File

@ -33,8 +33,7 @@ namespace SHADE
} }
// Get offset and modify the memory directly // Get offset and modify the memory directly
T* dataPtr = reinterpret_cast<T*>(propMemory.get() + PROP_INFO->offset); setPropertyUnsafe(PROP_INFO->offset, value);
*dataPtr = value;
} }
template<typename T> template<typename T>
@ -86,5 +85,6 @@ namespace SHADE
void SHMaterial::setPropertyUnsafe(uint32_t memOffset, const T& value) void SHMaterial::setPropertyUnsafe(uint32_t memOffset, const T& value)
{ {
(*reinterpret_cast<T*>(propMemory.get() + memOffset)) = value; (*reinterpret_cast<T*>(propMemory.get() + memOffset)) = value;
propertiesChanged = true;
} }
} }

View File

@ -62,12 +62,20 @@ namespace SHADE
} }
// Data was exported so unflag // Data was exported so unflag
//dataWasChanged = false; dataWasChanged = false;
} }
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Query Functions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
bool SHMaterialInstance::HasChanged() const noexcept
{
return dataWasChanged || (baseMaterial && baseMaterial->HasChanged());
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
Handle<SHShaderBlockInterface> SHMaterialInstance::getShaderBlockInterface() const noexcept Handle<SHShaderBlockInterface> SHMaterialInstance::getShaderBlockInterface() const noexcept
{ {
return baseMaterial->GetPipeline()->GetPipelineLayout()->GetShaderBlockInterface return baseMaterial->GetPipeline()->GetPipelineLayout()->GetShaderBlockInterface

View File

@ -69,7 +69,7 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
Handle<SHMaterial> GetBaseMaterial() const noexcept { return baseMaterial; } Handle<SHMaterial> GetBaseMaterial() const noexcept { return baseMaterial; }
bool HasChanged() const noexcept { return dataWasChanged; } bool HasChanged() const noexcept;
bool IsBlank() const noexcept { return overrideData.empty(); } // No overrides bool IsBlank() const noexcept { return overrideData.empty(); } // No overrides
private: private: