Added full support for runtime editing of material properties #215

Merged
Pycorax merged 14 commits from SP3-1-MaterialEditSupport into main 2022-11-17 09:22:53 +08:00
5 changed files with 56 additions and 46 deletions
Showing only changes of commit 506b8836fe - Show all commits

View File

@ -12,9 +12,9 @@
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Pipeline Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Pipeline Functions */
/*-----------------------------------------------------------------------------------*/
void SHMaterial::SetPipeline(Handle<SHVkPipeline> _pipeline)
{
// Reassignment, we ignore
@ -24,40 +24,26 @@ namespace SHADE
pipeline = _pipeline;
// Set up properties based on the pipeline
if (!pipeline)
if (pipeline)
{
// Clear memory and all that
propMemory.reset();
return;
}
// 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)
// Allocate memory for properties
const Handle<SHShaderBlockInterface> SHADER_INFO = GetShaderBlockInterface();
propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0;
if (propMemorySize <= 0)
{
if (iter->GetBaseMaterial() == GetHandle())
{
iter->ResetProperties();
}
propMemory.reset();
}
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
@ -65,9 +51,9 @@ namespace SHADE
return pipeline;
}
/*---------------------------------------------------------------------------------*/
/* Property Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Property Functions */
/*-----------------------------------------------------------------------------------*/
void SHMaterial::ResetProperties()
{
// Reset all the properties to default values
@ -89,6 +75,8 @@ namespace SHADE
break;
}
}
propertiesChanged = true;
}
void SHMaterial::ExportProperties(void* dest) const noexcept
@ -103,9 +91,9 @@ namespace SHADE
return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0;
}
/*---------------------------------------------------------------------------------*/
/* Helper Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
Handle<SHShaderBlockInterface> SHMaterial::GetShaderBlockInterface() const noexcept
{
return pipeline->GetPipelineLayout()->GetShaderBlockInterface
@ -115,4 +103,12 @@ namespace SHADE
vk::ShaderStageFlagBits::eFragment
);
}
/*-----------------------------------------------------------------------------------*/
/* Query Functions */
/*-----------------------------------------------------------------------------------*/
void SHMaterial::ClearChangeFlag() noexcept
{
propertiesChanged = false;
}
}

View File

@ -68,6 +68,10 @@ namespace SHADE
/* Query Functions */
/*-----------------------------------------------------------------------------*/
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:
/*-----------------------------------------------------------------------------*/
@ -76,6 +80,8 @@ namespace SHADE
Handle<SHVkPipeline> pipeline;
std::unique_ptr<char> propMemory;
Byte propMemorySize = 0;
bool propertiesChanged = true;
bool pipelineChanged = true;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */

View File

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

View File

@ -62,12 +62,20 @@ namespace SHADE
}
// 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
{
return baseMaterial->GetPipeline()->GetPipelineLayout()->GetShaderBlockInterface

View File

@ -69,7 +69,7 @@ namespace SHADE
/* Getter Functions */
/*-----------------------------------------------------------------------------*/
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
private: