diff --git a/source/slang/slang-ast-natural-layout.cpp b/source/slang/slang-ast-natural-layout.cpp index f15dee1d1eb..7643e043361 100644 --- a/source/slang/slang-ast-natural-layout.cpp +++ b/source/slang/slang-ast-natural-layout.cpp @@ -153,6 +153,12 @@ NaturalSize ASTNaturalLayoutContext::_calcSizeImpl(Type* type) // Initialize empty NaturalSize size = NaturalSize::makeEmpty(); + // We can't compute the size of an abstract type pack yet. + if (isAbstractTypePack(tupleType->getTypePack())) + { + return NaturalSize::makeInvalid(); + } + // Accumulate over all the member types for (auto cur = 0; cur < tupleType->getMemberCount(); cur++) { diff --git a/source/slang/slang-ir-layout.cpp b/source/slang/slang-ir-layout.cpp index 795f47f55c3..ac0c7960111 100644 --- a/source/slang/slang-ir-layout.cpp +++ b/source/slang/slang-ir-layout.cpp @@ -275,15 +275,26 @@ static Result _calcSizeAndAlignment( { auto tupleType = cast(type); IRSizeAndAlignment resultLayout; + IRIntegerValue lastFieldAlignment = 0; + IRType* lastFieldType = NULL; for (UInt i = 0; i < tupleType->getOperandCount(); i++) { auto elementType = tupleType->getOperand(i); IRSizeAndAlignment fieldTypeLayout; SLANG_RETURN_ON_FAIL( getSizeAndAlignment(optionSet, rules, (IRType*)elementType, &fieldTypeLayout)); + resultLayout.size = rules->adjustOffset( + resultLayout.size, + fieldTypeLayout.size, + lastFieldType, + lastFieldAlignment); resultLayout.size = align(resultLayout.size, fieldTypeLayout.alignment); resultLayout.alignment = std::max(resultLayout.alignment, fieldTypeLayout.alignment); + + resultLayout.size += fieldTypeLayout.size; + lastFieldType = as(elementType); + lastFieldAlignment = fieldTypeLayout.alignment; } *outSizeAndAlignment = rules->alignCompositeElement(resultLayout); return SLANG_OK; diff --git a/source/slang/slang-ir-peephole.cpp b/source/slang/slang-ir-peephole.cpp index 7f3ff1d689e..cafe978f991 100644 --- a/source/slang/slang-ir-peephole.cpp +++ b/source/slang/slang-ir-peephole.cpp @@ -281,7 +281,7 @@ struct PeepholeContext : InstPassBase baseType, &sizeAlignment))) break; - if (sizeAlignment.size == 0) + if (sizeAlignment.size == IRSizeAndAlignment::kIndeterminateSize) break; IRBuilder builder(module); diff --git a/tests/hlsl-intrinsic/size-of/size-of-tuple.slang b/tests/hlsl-intrinsic/size-of/size-of-tuple.slang new file mode 100644 index 00000000000..2c7d18e7152 --- /dev/null +++ b/tests/hlsl-intrinsic/size-of/size-of-tuple.slang @@ -0,0 +1,48 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-slang -compute -dx12 -shaderobj +//TEST(compute, vulkan):COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda -compute -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0], stride=4):out,name outputBuffer + +RWStructuredBuffer outputBuffer; + +struct Thing +{ + int val; + Tuple tuple; +}; + +int tupleSize1(T vals) +{ + return sizeof(Tuple); +} + +int tupleSize2(T vals) +{ + return sizeof(Tuple); +} + +int tupleSize3(T vals) +{ + return sizeof(Tuple); +} + +int tupleSize4(T vals) +{ + return sizeof(Thing); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + outputBuffer[0] = tupleSize2(); // CHECK: 0 + outputBuffer[1] = tupleSize3(); // CHECK-NEXT: 4 + outputBuffer[2] = tupleSize4(); // CHECK-NEXT: 4 + + outputBuffer[3] = tupleSize1(1); // CHECK-NEXT: 4 + outputBuffer[4] = tupleSize2(1,2); // CHECK-NEXT: 8 + outputBuffer[5] = tupleSize3(1,2,3); // CHECK-NEXT: 10 + outputBuffer[6] = tupleSize4(1,2,3,4); // CHECK-NEXT: 14 +}