Fixed memory corruption issue caused by SHMaterialInstance::SetMaterial()

This commit is contained in:
Kah Wei 2022-11-16 16:32:12 +08:00
parent 3a6f1f852b
commit 3e3a66f261
2 changed files with 30 additions and 17 deletions

View File

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

View File

@ -35,9 +35,20 @@ namespace SHADE
dataStore.reset(new char[dataStoreSize]); dataStore.reset(new char[dataStoreSize]);
} }
// 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)
{
return od.Index == VAR_IDX;
});
// Otherwise, create it
if (existingOverride == overrideData.end())
{
OverrideData od; OverrideData od;
od.Index = SHADER_INFO->GetVariableIndex(key); od.Index = VAR_IDX;
od.DataSize = sizeof(T); od.DataSize = sizeof(T);
if (overrideData.empty()) if (overrideData.empty())
{ {
od.StoredDataOffset = 0; od.StoredDataOffset = 0;
@ -48,12 +59,14 @@ namespace SHADE
od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize; od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize;
} }
// Get offset and modify the memory directly
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + od.StoredDataOffset);
*dataPtr = value;
// Save the override data information // Save the override data information
overrideData.emplace_back(std::move(od)); overrideData.emplace_back(std::move(od));
existingOverride = overrideData.end() - 1;
}
// Get offset and modify the memory directly
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + existingOverride->StoredDataOffset);
*dataPtr = value;
// Flag // Flag
dataWasChanged = true; dataWasChanged = true;