Fixed material property setting at runtime #214

Merged
Pycorax merged 4 commits from SP3-1-MaterialUpdate into main 2022-11-16 17:01:57 +08:00
2 changed files with 30 additions and 17 deletions
Showing only changes of commit 3e3a66f261 - Show all commits

View File

@ -43,9 +43,9 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
struct OverrideData
{
size_t Index;
size_t DataSize;
size_t StoredDataOffset;
uint32_t Index;
uint32_t DataSize;
uint32_t StoredDataOffset;
};
/*-----------------------------------------------------------------------------*/

View File

@ -35,26 +35,39 @@ namespace SHADE
dataStore.reset(new char[dataStoreSize]);
}
OverrideData od;
od.Index = SHADER_INFO->GetVariableIndex(key);
od.DataSize = sizeof(T);
if (overrideData.empty())
// Check if this was stored before
const uint32_t VAR_IDX = SHADER_INFO->GetVariableIndex(key);
auto existingOverride = std::find_if(overrideData.begin(), overrideData.end(), [&](const OverrideData& od)
{
od.StoredDataOffset = 0;
}
else
return od.Index == VAR_IDX;
});
// Otherwise, create it
if (existingOverride == overrideData.end())
{
const OverrideData& lastInsertedData = overrideData.back();
od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize;
OverrideData od;
od.Index = VAR_IDX;
od.DataSize = sizeof(T);
if (overrideData.empty())
{
od.StoredDataOffset = 0;
}
else
{
const OverrideData& lastInsertedData = overrideData.back();
od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize;
}
// Save the override data information
overrideData.emplace_back(std::move(od));
existingOverride = overrideData.end() - 1;
}
// Get offset and modify the memory directly
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + od.StoredDataOffset);
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + existingOverride->StoredDataOffset);
*dataPtr = value;
// Save the override data information
overrideData.emplace_back(std::move(od));
// Flag
dataWasChanged = true;
}