Fix alignment issues with heap lookup data

This commit is contained in:
baldurk
2024-05-27 10:13:57 +01:00
parent 0497e2d226
commit 7e8aa58ea1
3 changed files with 65 additions and 60 deletions
+28 -6
View File
@@ -288,6 +288,13 @@ struct InstanceDesc
cbuffer RayDispatchPatchCB REG(b0)
{
// declare GPUAddresses first to avoid padding/alignment issues
GPUAddress wrapped_sampHeapBase;
GPUAddress wrapped_srvHeapBase;
GPUAddress unwrapped_sampHeapBase;
GPUAddress unwrapped_srvHeapBase;
uint raydispatch_missoffs;
uint raydispatch_missstride;
uint raydispatch_misscount;
@@ -300,12 +307,6 @@ cbuffer RayDispatchPatchCB REG(b0)
uint raydispatch_callstride;
uint raydispatch_callcount;
GPUAddress wrapped_sampHeapBase;
GPUAddress wrapped_srvHeapBase;
GPUAddress unwrapped_sampHeapBase;
GPUAddress unwrapped_srvHeapBase;
uint wrapped_sampHeapSize;
uint wrapped_srvHeapSize;
uint unwrapped_heapStrides; // LSB = sampler, MSB = srv
@@ -313,7 +314,28 @@ cbuffer RayDispatchPatchCB REG(b0)
uint numPatchingAddrs;
};
struct StateObjectLookup
{
uint2 id; // ResourceId
uint offset;
uint pad;
};
struct ShaderRecordData
{
uint4 identifier[2]; // 32-byte real identifier
uint rootSigIndex; // only lower 16-bits are valid
};
#define MAX_LOCALSIG_PARAMS 31
struct LocalRootSigData
{
uint numParams;
uint paramOffsets[MAX_LOCALSIG_PARAMS];
};
#define WRAPPED_DESCRIPTOR_STRIDE 64
cbuffer DebugSampleOperation REG(b0)
+6 -26
View File
@@ -57,29 +57,9 @@ bool InRange(BlasAddressRange addressRange, GPUAddress address)
instanceDescs[dispatchGroup.x].blasAddress = 0;
}
struct StateObjectLookup
{
uint2 id; // ResourceId
uint offset;
};
StructuredBuffer<StateObjectLookup> stateObjects : register(t0);
struct RecordData
{
uint4 identifier[2]; // 32-byte real identifier
uint rootSigIndex; // only lower 16-bits are valid
};
StructuredBuffer<RecordData> records : register(t1);
struct RootSig
{
uint numParams;
uint paramOffsets[MAX_LOCALSIG_PARAMS];
};
StructuredBuffer<RootSig> rootsigs : register(t2);
StructuredBuffer<ShaderRecordData> records : register(t1);
StructuredBuffer<LocalRootSigData> rootsigs : register(t2);
struct WrappedRecord
{
@@ -136,7 +116,7 @@ void PatchTable(uint byteOffset)
// the exports from this state object are contiguous starting from the given index, look up this
// identifier's export
RecordData recordData = records[objectLookup.offset + wrappedRecord.index];
ShaderRecordData recordData = records[objectLookup.offset + wrappedRecord.index];
// store the unwrapped shader identifier
bufferToPatch.Store4(byteOffset, recordData.identifier[0]);
@@ -146,7 +126,7 @@ void PatchTable(uint byteOffset)
if(rootSigIndex != 0xffff)
{
RootSig sig = rootsigs[rootSigIndex];
LocalRootSigData sig = rootsigs[rootSigIndex];
DescriptorHeapData heaps[2];
@@ -181,8 +161,8 @@ void PatchTable(uint byteOffset)
uint index = sub(wrappedHandlePtr, heaps[h].wrapped_base).x / WRAPPED_DESCRIPTOR_STRIDE;
GPUAddress handleOffset = GPUAddress(index * heaps[h].unwrapped_stride, 0);
bufferToPatch.Store2(byteOffset + paramOffset,
add(heaps[h].unwrapped_base, handleOffset));
GPUAddress unwrapped = add(heaps[h].unwrapped_base, handleOffset);
bufferToPatch.Store2(byteOffset + paramOffset, unwrapped);
patched = true;
break;
}
+31 -28
View File
@@ -837,6 +837,8 @@ PatchedRayDispatch D3D12RaytracingResourceAndUtilHandler::PatchRayDispatch(
D3D12GpuBuffer *scratchBuffer = NULL;
RayDispatchPatchCB cbufferData = {};
uint32_t patchDataSize = 0;
const uint32_t raygenOffs = patchDataSize;
@@ -854,6 +856,30 @@ PatchedRayDispatch D3D12RaytracingResourceAndUtilHandler::PatchRayDispatch(
const uint32_t callOffs = patchDataSize;
patchDataSize += (uint32_t)desc.CallableShaderTable.SizeInBytes;
cbufferData.raydispatch_missoffs = missOffs;
cbufferData.raydispatch_missstride = (uint32_t)desc.MissShaderTable.StrideInBytes;
if(desc.MissShaderTable.SizeInBytes > 0)
cbufferData.raydispatch_misscount =
desc.MissShaderTable.StrideInBytes == 0
? 1
: uint32_t(desc.MissShaderTable.SizeInBytes / desc.MissShaderTable.StrideInBytes);
cbufferData.raydispatch_hitoffs = hitOffs;
cbufferData.raydispatch_hitstride = (uint32_t)desc.HitGroupTable.StrideInBytes;
if(desc.HitGroupTable.SizeInBytes > 0)
cbufferData.raydispatch_hitcount =
desc.HitGroupTable.StrideInBytes == 0
? 1
: uint32_t(desc.HitGroupTable.SizeInBytes / desc.HitGroupTable.StrideInBytes);
cbufferData.raydispatch_calloffs = callOffs;
cbufferData.raydispatch_callstride = (uint32_t)desc.CallableShaderTable.StrideInBytes;
if(desc.CallableShaderTable.SizeInBytes > 0)
cbufferData.raydispatch_callcount =
desc.CallableShaderTable.StrideInBytes == 0
? 1
: uint32_t(desc.CallableShaderTable.SizeInBytes / desc.CallableShaderTable.StrideInBytes);
D3D12GpuBufferAllocator::Inst()->Alloc(
D3D12GpuBufferHeapType::DefaultHeapWithUav, D3D12GpuBufferHeapMemoryFlag::Default,
patchDataSize, D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT, &scratchBuffer);
@@ -969,32 +995,6 @@ PatchedRayDispatch D3D12RaytracingResourceAndUtilHandler::PatchRayDispatch(
unwrappedCmd->ResourceBarrier(1, &barrier);
}
RayDispatchPatchCB cbufferData = {};
cbufferData.raydispatch_missoffs = missOffs;
cbufferData.raydispatch_missstride = (uint32_t)desc.MissShaderTable.StrideInBytes;
if(desc.MissShaderTable.SizeInBytes > 0)
cbufferData.raydispatch_misscount =
desc.MissShaderTable.StrideInBytes == 0
? 1
: uint32_t(desc.MissShaderTable.SizeInBytes / desc.MissShaderTable.StrideInBytes);
cbufferData.raydispatch_hitoffs = hitOffs;
cbufferData.raydispatch_hitstride = (uint32_t)desc.HitGroupTable.StrideInBytes;
if(desc.HitGroupTable.SizeInBytes > 0)
cbufferData.raydispatch_hitcount =
desc.HitGroupTable.StrideInBytes == 0
? 1
: uint32_t(desc.HitGroupTable.SizeInBytes / desc.HitGroupTable.StrideInBytes);
cbufferData.raydispatch_calloffs = callOffs;
cbufferData.raydispatch_callstride = (uint32_t)desc.CallableShaderTable.StrideInBytes;
if(desc.CallableShaderTable.SizeInBytes > 0)
cbufferData.raydispatch_callcount =
desc.CallableShaderTable.StrideInBytes == 0
? 1
: uint32_t(desc.CallableShaderTable.SizeInBytes / desc.CallableShaderTable.StrideInBytes);
RDCCOMPILE_ASSERT(WRAPPED_DESCRIPTOR_STRIDE == sizeof(D3D12Descriptor),
"Shader descriptor stride is wrong");
@@ -1068,9 +1068,12 @@ void D3D12RaytracingResourceAndUtilHandler::PrepareRayDispatchBuffer(
bytebuf lookupData;
const size_t ObjectLookupStride = sizeof(ResourceId) + sizeof(uint32_t);
const size_t ObjectLookupStride = sizeof(StateObjectLookup);
const size_t RecordDataStride = sizeof(D3D12ShaderExportDatabase::ExportedIdentifier);
const size_t RootSigStride = sizeof(uint32_t) * 32;
const size_t RootSigStride = sizeof(LocalRootSigData);
RDCCOMPILE_ASSERT(int(ObjectLookupStride / 8) * 8 == ObjectLookupStride, "Not aligned");
RDCCOMPILE_ASSERT(RecordDataStride == sizeof(ShaderRecordData), "Not identically sized");
size_t numExports = 0;
for(size_t i = 0; i < m_ExportDatabases.size(); i++)