Move scalar-offset calculating helper into common DXBC code

This commit is contained in:
baldurk
2025-03-13 16:17:31 +00:00
parent 2f6d881b8e
commit 7171eea3f9
3 changed files with 33 additions and 32 deletions
@@ -434,6 +434,7 @@ struct CBufferVariableType
rdcarray<CBufferVariable> members;
};
void RecalculateScalarOffsetsSizes(CBufferVariableType &type);
rdcstr TypeName(CBufferVariableType desc);
struct CBufferVariable
@@ -386,6 +386,38 @@ ShaderStage GetShaderStage(ShaderType type)
}
}
// DXIL wonderfully provides us with offsets that are completely useless/pointless for structured
// buffers. We need to recalculate them now based on tight packing
void RecalculateScalarOffsetsSizes(CBufferVariableType &type)
{
uint32_t offset = 0;
uint32_t pendingOffsetIncr = 0;
uint32_t lastBitfieldOffset = 0;
for(DXBC::CBufferVariable &var : type.members)
{
// if we encounter a non-bitfield, or the offset goes backwards, apply the 'real' offset now
if(var.bitFieldSize == 0 || var.bitFieldOffset < lastBitfieldOffset)
{
offset += pendingOffsetIncr;
pendingOffsetIncr = 0;
}
var.offset = offset;
// all bitfields share the same offset, which will be incremented at the next bitfield boundary (above)
if(var.bitFieldSize > 0)
{
pendingOffsetIncr = var.type.bytesize;
lastBitfieldOffset = var.bitFieldOffset + var.bitFieldSize;
continue;
}
offset += var.type.rows * var.type.cols * VarTypeByteSize(var.type.varType) * var.type.elements;
RecalculateScalarOffsetsSizes(var.type);
}
}
rdcstr TypeName(CBufferVariableType desc)
{
rdcstr ret;
@@ -1255,38 +1255,6 @@ static DXBC::CBufferVariableType MakeCBufferVariableType(const TypeInfo &typeInf
return ret;
}
// DXIL wonderfully provides us with offsets that are completely useless/pointless for structured
// buffers. We need to recalculate them now based on tight packing
void RecalculateScalarOffsetsSizes(DXBC::CBufferVariableType &type)
{
uint32_t offset = 0;
uint32_t pendingOffsetIncr = 0;
uint32_t lastBitfieldOffset = 0;
for(DXBC::CBufferVariable &var : type.members)
{
// if we encounter a non-bitfield, or the offset goes backwards, apply the 'real' offset now
if(var.bitFieldSize == 0 || var.bitFieldOffset < lastBitfieldOffset)
{
offset += pendingOffsetIncr;
pendingOffsetIncr = 0;
}
var.offset = offset;
// all bitfields share the same offset, which will be incremented at the next bitfield boundary (above)
if(var.bitFieldSize > 0)
{
pendingOffsetIncr = var.type.bytesize;
lastBitfieldOffset = var.bitFieldOffset + var.bitFieldSize;
continue;
}
offset += var.type.rows * var.type.cols * VarTypeByteSize(var.type.varType) * var.type.elements;
RecalculateScalarOffsetsSizes(var.type);
}
}
static void AddResourceBind(DXBC::Reflection *refl, const TypeInfo &typeInfo, const Metadata *r,
const bool srv)
{