HLSL: Deal with partially filled 16-byte word in cbuffers.

The last element of an array or matrix in HLSL cbuffers are not filled
completely, but only have a size equal to the base vector.
This commit is contained in:
Hans-Kristian Arntzen 2020-08-20 16:05:21 +02:00
parent dd1f53ff15
commit fad36a6b28
3 changed files with 135 additions and 13 deletions

View File

@ -0,0 +1,42 @@
struct Foo
{
row_major float3x3 m[2];
float v;
};
struct Bar
{
row_major float3x3 m;
float v;
};
cbuffer FooUBO : register(b0)
{
Foo _6_foo : packoffset(c0);
};
cbuffer BarUBO : register(b1)
{
Bar _9_bar : packoffset(c0);
};
static float4 FragColor;
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
FragColor = (_6_foo.v + _9_bar.v).xxxx;
}
SPIRV_Cross_Output main()
{
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}

View File

@ -0,0 +1,77 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 10
; Bound: 33
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %FragColor "FragColor"
OpName %Foo "Foo"
OpMemberName %Foo 0 "m"
OpMemberName %Foo 1 "v"
OpName %FooUBO "FooUBO"
OpMemberName %FooUBO 0 "foo"
OpName %_ ""
OpName %Bar "Bar"
OpMemberName %Bar 0 "m"
OpMemberName %Bar 1 "v"
OpName %BarUBO "BarUBO"
OpMemberName %BarUBO 0 "bar"
OpName %__0 ""
OpDecorate %FragColor Location 0
OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
OpMemberDecorate %Foo 0 ColMajor
OpMemberDecorate %Foo 0 Offset 0
OpMemberDecorate %Foo 0 MatrixStride 16
OpMemberDecorate %Foo 1 Offset 92
OpMemberDecorate %FooUBO 0 Offset 0
OpDecorate %FooUBO Block
OpDecorate %_ DescriptorSet 0
OpDecorate %_ Binding 0
OpMemberDecorate %Bar 0 ColMajor
OpMemberDecorate %Bar 0 Offset 0
OpMemberDecorate %Bar 0 MatrixStride 16
OpMemberDecorate %Bar 1 Offset 44
OpMemberDecorate %BarUBO 0 Offset 0
OpDecorate %BarUBO Block
OpDecorate %__0 DescriptorSet 0
OpDecorate %__0 Binding 1
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%FragColor = OpVariable %_ptr_Output_v4float Output
%v3float = OpTypeVector %float 3
%mat3v3float = OpTypeMatrix %v3float 3
%uint = OpTypeInt 32 0
%uint_2 = OpConstant %uint 2
%_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
%Foo = OpTypeStruct %_arr_mat3v3float_uint_2 %float
%FooUBO = OpTypeStruct %Foo
%_ptr_Uniform_FooUBO = OpTypePointer Uniform %FooUBO
%_ = OpVariable %_ptr_Uniform_FooUBO Uniform
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%int_1 = OpConstant %int 1
%_ptr_Uniform_float = OpTypePointer Uniform %float
%Bar = OpTypeStruct %mat3v3float %float
%BarUBO = OpTypeStruct %Bar
%_ptr_Uniform_BarUBO = OpTypePointer Uniform %BarUBO
%__0 = OpVariable %_ptr_Uniform_BarUBO Uniform
%main = OpFunction %void None %3
%5 = OpLabel
%23 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %int_1
%24 = OpLoad %float %23
%29 = OpAccessChain %_ptr_Uniform_float %__0 %int_0 %int_1
%30 = OpLoad %float %29
%31 = OpFAdd %float %24 %30
%32 = OpCompositeConstruct %v4float %31 %31 %31 %31
OpStore %FragColor %32
OpReturn
OpFunctionEnd

View File

@ -1294,24 +1294,22 @@ uint32_t CompilerGLSL::type_to_packed_array_stride(const SPIRType &type, const B
auto &tmp = get<SPIRType>(parent);
uint32_t size = type_to_packed_size(tmp, flags, packing);
if (tmp.array.empty())
{
uint32_t alignment = type_to_packed_alignment(type, flags, packing);
return (size + alignment - 1) & ~(alignment - 1);
}
else
{
// For multidimensional arrays, array stride always matches size of subtype.
// The alignment cannot change because multidimensional arrays are basically N * M array elements.
return size;
}
uint32_t alignment = type_to_packed_alignment(type, flags, packing);
return (size + alignment - 1) & ~(alignment - 1);
}
uint32_t CompilerGLSL::type_to_packed_size(const SPIRType &type, const Bitset &flags, BufferPackingStandard packing)
{
if (!type.array.empty())
{
return to_array_size_literal(type) * type_to_packed_array_stride(type, flags, packing);
uint32_t packed_size = to_array_size_literal(type) * type_to_packed_array_stride(type, flags, packing);
// For arrays of vectors and matrices in HLSL, the last element has a size which depends on its vector size,
// so that it is possible to pack other vectors into the last element.
if (packing_is_hlsl(packing) && type.basetype != SPIRType::Struct)
packed_size -= (4 - type.vecsize) * (type.width / 8);
return packed_size;
}
// If using PhysicalStorageBufferEXT storage class, this is a pointer,
@ -1384,6 +1382,11 @@ uint32_t CompilerGLSL::type_to_packed_size(const SPIRType &type, const Bitset &f
else
size = type.vecsize * type.columns * base_alignment;
}
// For matrices in HLSL, the last element has a size which depends on its vector size,
// so that it is possible to pack other vectors into the last element.
if (packing_is_hlsl(packing) && type.columns > 1)
size -= (4 - type.vecsize) * (type.width / 8);
}
}
@ -1436,7 +1439,7 @@ bool CompilerGLSL::buffer_is_packing_standard(const SPIRType &type, BufferPackin
is_top_level_block && size_t(i + 1) == type.member_types.size() && !memb_type.array.empty();
uint32_t packed_size = 0;
if (!member_can_be_unsized)
if (!member_can_be_unsized || packing_is_hlsl(packing))
packed_size = type_to_packed_size(memb_type, member_flags, packing);
// We only need to care about this if we have non-array types which can straddle the vec4 boundary.