Make aligned buffer allocation a common function, out of serialiser

This commit is contained in:
baldurk
2017-09-26 18:27:45 +01:00
parent eb7e14d46d
commit 9ef0c5282c
14 changed files with 85 additions and 85 deletions
+41
View File
@@ -238,6 +238,47 @@ uint32_t CalcNumMips(int w, int h, int d)
return mipLevels;
}
byte *AllocAlignedBuffer(uint64_t size, uint64_t alignment)
{
byte *rawAlloc = NULL;
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
try
#endif
{
rawAlloc = new byte[(size_t)size + sizeof(byte *) + (size_t)alignment];
}
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
catch(std::bad_alloc &)
{
rawAlloc = NULL;
}
#endif
if(rawAlloc == NULL)
RDCFATAL("Allocation for %llu bytes failed", size);
RDCASSERT(rawAlloc);
byte *alignedAlloc = (byte *)AlignUp(uint64_t(rawAlloc + sizeof(byte *)), alignment);
byte **realPointer = (byte **)alignedAlloc;
realPointer[-1] = rawAlloc;
return alignedAlloc;
}
void FreeAlignedBuffer(byte *buf)
{
if(buf == NULL)
return;
byte **realPointer = (byte **)buf;
byte *rawAlloc = realPointer[-1];
delete[] rawAlloc;
}
uint32_t Log2Floor(uint32_t value)
{
RDCASSERT(value > 0);
+3
View File
@@ -158,6 +158,9 @@ inline T AlignUpPtr(T x, A a)
bool FindDiffRange(void *a, void *b, size_t bufSize, size_t &diffStart, size_t &diffEnd);
uint32_t CalcNumMips(int Width, int Height, int Depth);
byte *AllocAlignedBuffer(uint64_t size, uint64_t alignment = 64);
void FreeAlignedBuffer(byte *buf);
uint32_t Log2Floor(uint32_t value);
#if ENABLED(RDOC_X64)
uint64_t Log2Floor(uint64_t value);
+3 -3
View File
@@ -690,7 +690,7 @@ void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::SetInit
if(it != m_InitialContents.end())
{
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
FreeAlignedBuffer(it->second.blob);
m_InitialContents.erase(it);
}
@@ -790,7 +790,7 @@ void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::FreeIni
{
auto it = m_InitialContents.begin();
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
FreeAlignedBuffer(it->second.blob);
if(!m_InitialContents.empty())
m_InitialContents.erase(m_InitialContents.begin());
}
@@ -825,7 +825,7 @@ void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::CreateI
if(neededInitials.find(id) == neededInitials.end())
{
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
FreeAlignedBuffer(it->second.blob);
++it;
m_InitialContents.erase(id);
}
+4 -4
View File
@@ -66,8 +66,8 @@ struct D3D11ResourceRecord : public ResourceRecord
{
if(ShadowPtr[ctx][0] == NULL)
{
ShadowPtr[ctx][0] = Serialiser::AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[ctx][1] = Serialiser::AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[ctx][0] = AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[ctx][1] = AllocAlignedBuffer(size + sizeof(markerValue));
memcpy(ShadowPtr[ctx][0] + size, markerValue, sizeof(markerValue));
memcpy(ShadowPtr[ctx][1] + size, markerValue, sizeof(markerValue));
@@ -95,8 +95,8 @@ struct D3D11ResourceRecord : public ResourceRecord
{
if(ShadowPtr[i][0] != NULL)
{
Serialiser::FreeAlignedBuffer(ShadowPtr[i][0]);
Serialiser::FreeAlignedBuffer(ShadowPtr[i][1]);
FreeAlignedBuffer(ShadowPtr[i][0]);
FreeAlignedBuffer(ShadowPtr[i][1]);
}
ShadowPtr[i][0] = ShadowPtr[i][1] = NULL;
}
+1 -1
View File
@@ -820,7 +820,7 @@ bool D3D12ResourceManager::Prepare_InitialState(ID3D12DeviceChild *res)
UINT numElems = heap->GetDesc().NumDescriptors;
D3D12Descriptor *descs =
(D3D12Descriptor *)Serialiser::AllocAlignedBuffer(sizeof(D3D12Descriptor) * numElems);
(D3D12Descriptor *)AllocAlignedBuffer(sizeof(D3D12Descriptor) * numElems);
memcpy(descs, heap->GetDescriptors(), sizeof(D3D12Descriptor) * numElems);
+4 -4
View File
@@ -270,7 +270,7 @@ WrappedID3D12Resource::~WrappedID3D12Resource()
{
m_pDevice->Unmap(this, (UINT)i, map[i].realPtr, NULL);
Serialiser::FreeAlignedBuffer(map[i].shadowPtr);
FreeAlignedBuffer(map[i].shadowPtr);
map[i].realPtr = NULL;
map[i].shadowPtr = NULL;
}
@@ -317,7 +317,7 @@ void WrappedID3D12Resource::AllocShadow(UINT Subresource, size_t size)
map.resize(Subresource + 1);
if(map[Subresource].shadowPtr == NULL)
map[Subresource].shadowPtr = Serialiser::AllocAlignedBuffer(size);
map[Subresource].shadowPtr = AllocAlignedBuffer(size);
}
void WrappedID3D12Resource::FreeShadow()
@@ -326,7 +326,7 @@ void WrappedID3D12Resource::FreeShadow()
for(size_t i = 0; i < map.size(); i++)
{
Serialiser::FreeAlignedBuffer(map[i].shadowPtr);
FreeAlignedBuffer(map[i].shadowPtr);
map[i].shadowPtr = NULL;
}
}
@@ -383,7 +383,7 @@ void STDMETHODCALLTYPE WrappedID3D12Resource::Unmap(UINT Subresource, const D3D1
{
m_pDevice->Unmap(this, Subresource, map[Subresource].realPtr, pWrittenRange);
Serialiser::FreeAlignedBuffer(map[Subresource].shadowPtr);
FreeAlignedBuffer(map[Subresource].shadowPtr);
map[Subresource].realPtr = NULL;
map[Subresource].shadowPtr = NULL;
}
+10 -10
View File
@@ -487,7 +487,7 @@ bool GLResourceManager::Prepare_InitialState(GLResource res)
}
else if(res.Namespace == eResFramebuffer)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(FramebufferInitialData));
byte *data = AllocAlignedBuffer(sizeof(FramebufferInitialData));
RDCEraseMem(data, sizeof(FramebufferInitialData));
SetInitialContents(Id, InitialContentData(res.Namespace, GLResource(MakeNullResource), 0, data));
@@ -518,7 +518,7 @@ bool GLResourceManager::Prepare_InitialState(GLResource res)
}
else if(res.Namespace == eResFeedback)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(FeedbackInitialData));
byte *data = AllocAlignedBuffer(sizeof(FeedbackInitialData));
RDCEraseMem(data, sizeof(FeedbackInitialData));
SetInitialContents(Id, InitialContentData(res.Namespace, GLResource(MakeNullResource), 0, data));
@@ -536,7 +536,7 @@ bool GLResourceManager::Prepare_InitialState(GLResource res)
}
else if(res.Namespace == eResVertexArray)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(VAOInitialData));
byte *data = AllocAlignedBuffer(sizeof(VAOInitialData));
RDCEraseMem(data, sizeof(VAOInitialData));
SetInitialContents(Id, InitialContentData(res.Namespace, GLResource(MakeNullResource), 0, data));
@@ -684,7 +684,7 @@ void GLResourceManager::PrepareTextureInitialContents(ResourceId liveid, Resourc
WrappedOpenGL::TextureData &details = m_GL->m_Textures[liveid];
TextureStateInitialData *state =
(TextureStateInitialData *)Serialiser::AllocAlignedBuffer(sizeof(TextureStateInitialData));
(TextureStateInitialData *)AllocAlignedBuffer(sizeof(TextureStateInitialData));
RDCEraseMem(state, sizeof(TextureStateInitialData));
if(details.internalFormat == eGL_NONE)
@@ -1371,8 +1371,8 @@ bool GLResourceManager::Serialise_InitialState(ResourceId resid, GLResource res)
ResetPixelUnpackState(gl, false, 1);
TextureStateInitialData *state = (TextureStateInitialData *)Serialiser::AllocAlignedBuffer(
sizeof(TextureStateInitialData));
TextureStateInitialData *state =
(TextureStateInitialData *)AllocAlignedBuffer(sizeof(TextureStateInitialData));
RDCEraseMem(state, sizeof(TextureStateInitialData));
m_pSerialiser->Serialise("state", *state);
@@ -1638,7 +1638,7 @@ bool GLResourceManager::Serialise_InitialState(ResourceId resid, GLResource res)
if(m_State < WRITING)
{
byte *blob = Serialiser::AllocAlignedBuffer(sizeof(data));
byte *blob = AllocAlignedBuffer(sizeof(data));
memcpy(blob, &data, sizeof(data));
SetInitialContents(Id,
@@ -1659,7 +1659,7 @@ bool GLResourceManager::Serialise_InitialState(ResourceId resid, GLResource res)
if(m_State < WRITING)
{
byte *blob = Serialiser::AllocAlignedBuffer(sizeof(data));
byte *blob = AllocAlignedBuffer(sizeof(data));
memcpy(blob, &data, sizeof(data));
SetInitialContents(Id,
@@ -1686,7 +1686,7 @@ bool GLResourceManager::Serialise_InitialState(ResourceId resid, GLResource res)
if(m_State < WRITING)
{
byte *blob = Serialiser::AllocAlignedBuffer(sizeof(data));
byte *blob = AllocAlignedBuffer(sizeof(data));
memcpy(blob, &data, sizeof(data));
SetInitialContents(Id,
@@ -1721,7 +1721,7 @@ void GLResourceManager::Create_InitialState(ResourceId id, GLResource live, bool
}
else if(live.Namespace == eResVertexArray)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(VAOInitialData));
byte *data = AllocAlignedBuffer(sizeof(VAOInitialData));
RDCEraseMem(data, sizeof(VAOInitialData));
SetInitialContents(id,
+4 -4
View File
@@ -279,8 +279,8 @@ struct GLResourceRecord : public ResourceRecord
{
if(ShadowPtr[0] == NULL)
{
ShadowPtr[0] = Serialiser::AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[1] = Serialiser::AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[0] = AllocAlignedBuffer(size + sizeof(markerValue));
ShadowPtr[1] = AllocAlignedBuffer(size + sizeof(markerValue));
memcpy(ShadowPtr[0] + size, markerValue, sizeof(markerValue));
memcpy(ShadowPtr[1] + size, markerValue, sizeof(markerValue));
@@ -304,8 +304,8 @@ struct GLResourceRecord : public ResourceRecord
{
if(ShadowPtr[0] != NULL)
{
Serialiser::FreeAlignedBuffer(ShadowPtr[0]);
Serialiser::FreeAlignedBuffer(ShadowPtr[1]);
FreeAlignedBuffer(ShadowPtr[0]);
FreeAlignedBuffer(ShadowPtr[1]);
}
ShadowPtr[0] = ShadowPtr[1] = NULL;
}
+1 -1
View File
@@ -1252,7 +1252,7 @@ bool WrappedVulkan::EndFrameCapture(void *dev, void *wnd)
SCOPED_LOCK(m_CoherentMapsLock);
for(auto it = m_CoherentMaps.begin(); it != m_CoherentMaps.end(); ++it)
{
Serialiser::FreeAlignedBuffer((*it)->memMapState->refData);
FreeAlignedBuffer((*it)->memMapState->refData);
(*it)->memMapState->refData = NULL;
(*it)->memMapState->needRefData = false;
}
+7 -7
View File
@@ -100,7 +100,7 @@ bool WrappedVulkan::Prepare_SparseInitialState(WrappedVkBuffer *buf)
uint32_t numElems = (uint32_t)buf->record->sparseInfo->opaquemappings.size();
SparseBufferInitState *info = (SparseBufferInitState *)Serialiser::AllocAlignedBuffer(
SparseBufferInitState *info = (SparseBufferInitState *)AllocAlignedBuffer(
sizeof(SparseBufferInitState) + sizeof(VkSparseMemoryBind) * numElems +
sizeof(MemIDOffset) * boundMems.size());
@@ -249,7 +249,7 @@ bool WrappedVulkan::Prepare_SparseInitialState(WrappedVkImage *im)
uint32_t opaqueCount = (uint32_t)sparse->opaquemappings.size();
byte *blob = Serialiser::AllocAlignedBuffer(
byte *blob = AllocAlignedBuffer(
sizeof(SparseImageInitState) + sizeof(VkSparseMemoryBind) * opaqueCount +
sizeof(MemIDOffset) * totalPageCount + sizeof(MemIDOffset) * boundMems.size());
@@ -430,7 +430,7 @@ bool WrappedVulkan::Serialise_SparseBufferInitialState(
m_pSerialiser->Serialise("numBinds", numBinds);
m_pSerialiser->Serialise("numUniqueMems", numUniqueMems);
SparseBufferInitState *info = (SparseBufferInitState *)Serialiser::AllocAlignedBuffer(
SparseBufferInitState *info = (SparseBufferInitState *)AllocAlignedBuffer(
sizeof(SparseBufferInitState) + sizeof(VkSparseMemoryBind) * numBinds +
sizeof(MemIDOffset) * numUniqueMems);
@@ -587,7 +587,7 @@ bool WrappedVulkan::Serialise_SparseImageInitialState(ResourceId id,
m_pSerialiser->Serialise("pagedim", pagedim);
m_pSerialiser->Serialise("numUniqueMems", numUniqueMems);
byte *blob = Serialiser::AllocAlignedBuffer(
byte *blob = AllocAlignedBuffer(
sizeof(SparseImageInitState) + sizeof(VkSparseMemoryBind) * opaqueCount +
sizeof(VkSparseImageMemoryBind) * pageCount + sizeof(MemIDOffset) * numUniqueMems);
@@ -978,7 +978,7 @@ bool WrappedVulkan::Prepare_InitialState(WrappedVkRes *res)
numElems += layout.bindings[i].descriptorCount;
DescriptorSetSlot *info =
(DescriptorSetSlot *)Serialiser::AllocAlignedBuffer(sizeof(DescriptorSetSlot) * numElems);
(DescriptorSetSlot *)AllocAlignedBuffer(sizeof(DescriptorSetSlot) * numElems);
RDCEraseMem(info, sizeof(DescriptorSetSlot) * numElems);
uint32_t e = 0;
@@ -1484,8 +1484,8 @@ bool WrappedVulkan::Serialise_InitialState(ResourceId resid, WrappedVkRes *)
// allocate memory to keep the element structures around, as well as a WriteDescriptorSet
// array
byte *blob = Serialiser::AllocAlignedBuffer(sizeof(VkDescriptorBufferInfo) * numElems +
sizeof(VkWriteDescriptorSet) * numBinds);
byte *blob = AllocAlignedBuffer(sizeof(VkDescriptorBufferInfo) * numElems +
sizeof(VkWriteDescriptorSet) * numBinds);
RDCCOMPILE_ASSERT(sizeof(VkDescriptorBufferInfo) >= sizeof(VkDescriptorImageInfo),
"Descriptor structs sizes are unexpected, ensure largest size is used");
+1 -1
View File
@@ -771,7 +771,7 @@ VkResourceRecord::~VkResourceRecord()
if(resType == eResDeviceMemory && memMapState)
{
Serialiser::FreeAlignedBuffer(memMapState->refData);
FreeAlignedBuffer(memMapState->refData);
SAFE_DELETE(memMapState);
}
@@ -502,7 +502,7 @@ void WrappedVulkan::vkFreeMemory(VkDevice device, VkDeviceMemory memory,
// there is an implicit unmap on free, so make sure to tidy up
if(wrapped->record->memMapState && wrapped->record->memMapState->refData)
{
Serialiser::FreeAlignedBuffer(wrapped->record->memMapState->refData);
FreeAlignedBuffer(wrapped->record->memMapState->refData);
wrapped->record->memMapState->refData = NULL;
}
@@ -670,7 +670,7 @@ void WrappedVulkan::vkUnmapMemory(VkDevice device, VkDeviceMemory mem)
state.mappedPtr = NULL;
}
Serialiser::FreeAlignedBuffer(state.refData);
FreeAlignedBuffer(state.refData);
state.refData = NULL;
if(state.mapCoherent)
@@ -725,7 +725,7 @@ bool WrappedVulkan::Serialise_vkFlushMappedMemoryRanges(Serialiser *localSeriali
RDCASSERT(memOffset == 0 && memSize == state->mapSize);
// allocate ref data so we can compare next time to minimise serialised data
state->refData = Serialiser::AllocAlignedBuffer((size_t)state->mapSize);
state->refData = AllocAlignedBuffer((size_t)state->mapSize);
}
// it's no longer safe to use state->mappedPtr, we need to save *precisely* what
+3 -44
View File
@@ -249,7 +249,7 @@ Chunk::Chunk(Serialiser *ser, uint32_t chunkType, bool temporary)
if(ser->HasAlignedData())
{
m_Data = Serialiser::AllocAlignedBuffer(m_Length);
m_Data = AllocAlignedBuffer(m_Length);
m_AlignedData = true;
}
else
@@ -289,7 +289,7 @@ Chunk *Chunk::Duplicate()
ret->m_AlignedData = m_AlignedData;
if(m_AlignedData)
ret->m_Data = Serialiser::AllocAlignedBuffer(m_Length);
ret->m_Data = AllocAlignedBuffer(m_Length);
else
ret->m_Data = new byte[m_Length];
@@ -321,7 +321,7 @@ Chunk::~Chunk()
if(m_AlignedData)
{
if(m_Data)
Serialiser::FreeAlignedBuffer(m_Data);
FreeAlignedBuffer(m_Data);
m_Data = NULL;
}
@@ -1157,47 +1157,6 @@ void Serialiser::ReadFromFile(uint64_t bufferOffs, size_t length)
}
}
byte *Serialiser::AllocAlignedBuffer(size_t size, size_t alignment)
{
byte *rawAlloc = NULL;
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
try
#endif
{
rawAlloc = new byte[size + sizeof(byte *) + alignment];
}
#if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
catch(std::bad_alloc &)
{
rawAlloc = NULL;
}
#endif
if(rawAlloc == NULL)
RDCFATAL("Allocation for %llu bytes failed", (uint64_t)size);
RDCASSERT(rawAlloc);
byte *alignedAlloc = (byte *)AlignUp((size_t)(rawAlloc + sizeof(byte *)), alignment);
byte **realPointer = (byte **)alignedAlloc;
realPointer[-1] = rawAlloc;
return alignedAlloc;
}
void Serialiser::FreeAlignedBuffer(byte *buf)
{
if(buf == NULL)
return;
byte **realPointer = (byte **)buf;
byte *rawAlloc = realPointer[-1];
delete[] rawAlloc;
}
void Serialiser::SetPersistentBlock(uint64_t offs)
{
// as long as this is called immediately after pushing the chunk context at the
-3
View File
@@ -627,9 +627,6 @@ public:
// prints to the debug output log
void DebugPrint(const char *fmt, ...);
static byte *AllocAlignedBuffer(size_t size, size_t align = 64);
static void FreeAlignedBuffer(byte *buf);
void FlushToDisk();
// set a function used when serialising a text representation