Skip to content

Commit f79b95c

Browse files
committed
Update active thread DXIL global pointers to the local backing memory
1 parent 1881b26 commit f79b95c

File tree

2 files changed

+84
-37
lines changed

2 files changed

+84
-37
lines changed

renderdoc/driver/shaders/dxil/dxil_debug.cpp

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5166,8 +5166,8 @@ bool ThreadState::ExecuteInstruction(DebugAPIWrapper *apiWrapper,
51665166
ShaderVariable originalValue(m_Variables[ptrId]);
51675167

51685168
const MemoryTracking::Pointer &ptr = itPtr->second;
5169-
Id baseMemoryId = ptr.baseMemoryId;
5170-
void *memory = ptr.memory;
5169+
const Id baseMemoryId = ptr.baseMemoryId;
5170+
void *const memory = ptr.memory;
51715171
uint64_t allocSize = ptr.size;
51725172

51735173
RDCASSERT(memory);
@@ -5196,22 +5196,8 @@ bool ThreadState::ExecuteInstruction(DebugAPIWrapper *apiWrapper,
51965196

51975197
// active lane : writes to a GSM variable, write to local and global backing memory
51985198
if(m_State)
5199-
{
5200-
if(m_GlobalState.groupSharedMemoryIds.contains(baseMemoryId))
5201-
{
5202-
// Compute the local pointer offset and apply it to the global base memory
5203-
ptrdiff_t offset = (uintptr_t)memory - (uintptr_t)allocation.backingMemory;
5204-
auto globalMem = m_GlobalState.memory.m_Allocations.find(baseMemoryId);
5205-
if(globalMem == m_GlobalState.memory.m_Allocations.end())
5206-
{
5207-
RDCERR("Unknown global memory allocation Id %u", baseMemoryId);
5208-
break;
5209-
}
5210-
void *globalMemory = (void *)((uintptr_t)globalMem->second.backingMemory + offset);
5211-
allocSize = ptr.size;
5212-
UpdateBackingMemoryFromVariable(globalMemory, allocSize, val);
5213-
}
5214-
}
5199+
UpdateGlobalBackingMemory(ptrId, ptr, allocation, val);
5200+
52155201
// record the change to the base memory variable if it is not the ptrId variable
52165202
if(recordBaseMemoryChange)
52175203
{
@@ -6185,11 +6171,11 @@ bool ThreadState::ExecuteInstruction(DebugAPIWrapper *apiWrapper,
61856171
}
61866172

61876173
const MemoryTracking::Pointer &ptr = itPtr->second;
6188-
Id baseMemoryId = ptr.baseMemoryId;
6174+
const Id baseMemoryId = ptr.baseMemoryId;
61896175

61906176
RDCASSERTNOTEQUAL(baseMemoryId, DXILDebug::INVALID_ID);
61916177

6192-
void *memory = ptr.memory;
6178+
void *const memory = ptr.memory;
61936179
RDCASSERT(memory);
61946180
uint64_t allocSize = ptr.size;
61956181

@@ -6336,22 +6322,7 @@ bool ThreadState::ExecuteInstruction(DebugAPIWrapper *apiWrapper,
63366322

63376323
// active lane : writes to a GSM variable, write to local and global backing memory
63386324
if(m_State)
6339-
{
6340-
if(m_GlobalState.groupSharedMemoryIds.contains(baseMemoryId))
6341-
{
6342-
// Compute the local pointer offset and apply it to the global base memory
6343-
ptrdiff_t offset = (uintptr_t)memory - (uintptr_t)allocation.backingMemory;
6344-
auto globalMem = m_GlobalState.memory.m_Allocations.find(baseMemoryId);
6345-
if(globalMem == m_GlobalState.memory.m_Allocations.end())
6346-
{
6347-
RDCERR("Unknown global memory allocation Id %u", baseMemoryId);
6348-
break;
6349-
}
6350-
void *globalMemory = (void *)((uintptr_t)globalMem->second.backingMemory + offset);
6351-
allocSize = ptr.size;
6352-
UpdateBackingMemoryFromVariable(globalMemory, allocSize, res);
6353-
}
6354-
}
6325+
UpdateGlobalBackingMemory(ptrId, ptr, allocation, res);
63556326

63566327
// record the change to the base memory variable
63576328
if(recordBaseMemoryChange)
@@ -6738,6 +6709,35 @@ void ThreadState::UpdateMemoryVariableFromBackingMemory(Id memoryId, const void
67386709
UpdateShaderVariableFromBackingMemory(baseMemory, ptr);
67396710
}
67406711

6712+
void ThreadState::UpdateGlobalBackingMemory(Id ptrId, const MemoryTracking::Pointer &ptr,
6713+
const MemoryTracking::Allocation &allocation,
6714+
const ShaderVariable &val)
6715+
{
6716+
Id baseMemoryId = ptr.baseMemoryId;
6717+
if(m_GlobalState.groupSharedMemoryIds.contains(baseMemoryId))
6718+
{
6719+
void *const memory = ptr.memory;
6720+
6721+
// Compute the local pointer offset and apply it to the global base memory
6722+
ptrdiff_t offset = (uintptr_t)memory - (uintptr_t)allocation.backingMemory;
6723+
if(offset < 0)
6724+
{
6725+
RDCERR("Invalid global memory allocation offset ptrId %u Id %u", ptrId, baseMemoryId);
6726+
return;
6727+
}
6728+
auto globalMem = m_GlobalState.memory.m_Allocations.find(baseMemoryId);
6729+
if(globalMem == m_GlobalState.memory.m_Allocations.end())
6730+
{
6731+
RDCERR("Unknown global memory allocation Id %u", baseMemoryId);
6732+
return;
6733+
;
6734+
}
6735+
void *globalMemory = (void *)((uintptr_t)globalMem->second.backingMemory + offset);
6736+
uint64_t allocSize = ptr.size;
6737+
UpdateBackingMemoryFromVariable(globalMemory, allocSize, val);
6738+
}
6739+
}
6740+
67416741
void ThreadState::PerformGPUResourceOp(const rdcarray<ThreadState> &workgroup, Operation opCode,
67426742
DXOp dxOpCode, const ResourceReferenceInfo &resRefInfo,
67436743
DebugAPIWrapper *apiWrapper, const DXIL::Instruction &inst,
@@ -9528,14 +9528,58 @@ rdcarray<ShaderDebugState> Debugger::ContinueDebug(DebugAPIWrapper *apiWrapper)
95289528
// active lane : needs it own local backing memory, copied from global at the start
95299529
for(Id id : m_GlobalState.groupSharedMemoryIds)
95309530
{
9531-
MemoryTracking::Allocation &globalAlloc = active.m_Memory.m_Allocations[id];
9531+
MemoryTracking::Allocation &globalAlloc = m_GlobalState.memory.m_Allocations[id];
95329532
RDCASSERT(globalAlloc.globalVarAlloc);
95339533
const size_t allocSize = (size_t)globalAlloc.size;
95349534
void *localBackingMem = malloc(allocSize);
95359535
memcpy(localBackingMem, globalAlloc.backingMemory, allocSize);
95369536
active.m_Memory.m_Allocations[id] = {localBackingMem, globalAlloc.size, true, true};
95379537
active.m_Memory.m_Pointers[id] = {id, localBackingMem, globalAlloc.size};
95389538
}
9539+
// active lane: update the backing memory pointer of any pointers to the new local allocations
9540+
for(const auto &itGlobalPtr : m_GlobalState.memory.m_Pointers)
9541+
{
9542+
const MemoryTracking::Pointer &globalPtr = itGlobalPtr.second;
9543+
Id ptrId = itGlobalPtr.first;
9544+
Id baseMemoryId = globalPtr.baseMemoryId;
9545+
// pointers for backing allocations have already have been updated
9546+
if(ptrId == baseMemoryId)
9547+
continue;
9548+
9549+
auto itAlloc = m_GlobalState.memory.m_Allocations.find(baseMemoryId);
9550+
if(itAlloc == m_GlobalState.memory.m_Allocations.end())
9551+
{
9552+
RDCERR("Could not find global backing memory allocation for ptr %u BaseMemoryId %u", ptrId,
9553+
baseMemoryId);
9554+
continue;
9555+
}
9556+
const MemoryTracking::Allocation &globalAlloc = itAlloc->second;
9557+
ptrdiff_t offset = (uintptr_t)globalPtr.memory - (uintptr_t)globalAlloc.backingMemory;
9558+
if(offset < 0)
9559+
{
9560+
RDCERR("Invalid memory allocation offset ptrId %u BaseMemoryId %u", ptrId, baseMemoryId);
9561+
continue;
9562+
}
9563+
itAlloc = active.m_Memory.m_Allocations.find(baseMemoryId);
9564+
if(itAlloc == active.m_Memory.m_Allocations.end())
9565+
{
9566+
RDCERR("Could not find local backing memory allocation for ptr %u BaseMemoryId %u", ptrId,
9567+
baseMemoryId);
9568+
continue;
9569+
}
9570+
const MemoryTracking::Allocation &localAlloc = itAlloc->second;
9571+
void *localMemory = (void *)((uintptr_t)localAlloc.backingMemory + offset);
9572+
auto itLocalPtr = active.m_Memory.m_Pointers.find(ptrId);
9573+
if(itLocalPtr == active.m_Memory.m_Pointers.end())
9574+
{
9575+
RDCERR("Could not find local ptr %u", ptrId);
9576+
continue;
9577+
}
9578+
MemoryTracking::Pointer &localPtr = itLocalPtr->second;
9579+
RDCASSERTEQUAL(localPtr.baseMemoryId, baseMemoryId);
9580+
RDCASSERTEQUAL(localPtr.size, globalPtr.size);
9581+
localPtr.memory = localMemory;
9582+
}
95399583

95409584
// globals won't be filled out by entering the entry point, ensure their change is registered.
95419585
for(const GlobalVariable &gv : m_GlobalState.globals)

renderdoc/driver/shaders/dxil/dxil_debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ struct ThreadState
279279
bool GetVariableHelper(DXIL::Operation op, DXIL::DXOp dxOpCode, ShaderVariable &var) const;
280280
void UpdateBackingMemoryFromVariable(void *ptr, uint64_t &allocSize, const ShaderVariable &var);
281281
void UpdateMemoryVariableFromBackingMemory(Id memoryId, const void *ptr);
282+
void UpdateGlobalBackingMemory(Id ptrId, const MemoryTracking::Pointer &ptr,
283+
const MemoryTracking::Allocation &allocation,
284+
const ShaderVariable &val);
282285

283286
void PerformGPUResourceOp(const rdcarray<ThreadState> &workgroup, DXIL::Operation opCode,
284287
DXIL::DXOp dxOpCode, const ResourceReferenceInfo &resRef,

0 commit comments

Comments
 (0)