Commit af201e8a by John Kessenich

Merge branch GitHub 'master' into GitLab master

parents 4c42a976 c9e0a42b
......@@ -1651,10 +1651,37 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
}
if (type.isArray()) {
int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
// Do all but the outer dimension
for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
assert(type.getArraySizes()->getDimSize(dim) > 0);
spvType = builder.makeArrayType(spvType, type.getArraySizes()->getDimSize(dim));
if (type.getArraySizes()->getNumDims() > 1) {
if (explicitLayout != glslang::ElpNone) {
// Use a dummy glslang type for querying internal strides of
// arrays of arrays, but using just a one-dimensional array.
glslang::TType simpleArrayType(type, 0); // deference type of the array
while (simpleArrayType.getArraySizes().getNumDims() > 1)
simpleArrayType.getArraySizes().dereference();
// Will compute the higher-order strides here, rather than making a whole
// pile of types and doing repetitive recursion on their contents.
stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
}
for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
int size = type.getArraySizes()->getDimSize(dim);
assert(size > 0);
spvType = builder.makeArrayType(spvType, size, stride);
if (stride > 0)
builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
stride *= size;
}
} else {
// single-dimensional array, and don't yet have stride
// We need to decorate array strides for types needing explicit layout,
// except for the very top if it is an array of blocks; that array is
// not laid out in memory in a way needing a stride.
if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
}
// Do the outer dimension, which might not be known for a runtime-sized array
......@@ -1662,18 +1689,10 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
spvType = builder.makeRuntimeArray(spvType);
} else {
assert(type.getOuterArraySize() > 0);
spvType = builder.makeArrayType(spvType, type.getOuterArraySize());
spvType = builder.makeArrayType(spvType, type.getOuterArraySize(), stride);
}
// TODO: explicit layout still needs to be done hierarchically for arrays of arrays, which
// may still require additional "link time" support from the front-end
// for arrays of arrays
// We need to decorate array strides for types needing explicit layout,
// except for the very top if it is an array of blocks; that array is
// not laid out in memory in a way needing a stride.
if (explicitLayout && type.getBasicType() != glslang::EbtBlock)
builder.addDecoration(spvType, spv::DecorationArrayStride, getArrayStride(type, explicitLayout, qualifier.layoutMatrix));
if (stride > 0)
builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
}
return spvType;
......@@ -1707,25 +1726,25 @@ glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang:
int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
{
int size;
int stride = glslangIntermediate->getBaseAlignment(arrayType, size, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
if (arrayType.isMatrix()) {
// GLSL strides are set to alignments of the matrix flattened to individual rows/cols,
// but SPV needs an array stride for the whole matrix, not the rows/cols
if (matrixLayout == glslang::ElmRowMajor)
stride *= arrayType.getMatrixRows();
else
stride *= arrayType.getMatrixCols();
}
int stride;
glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
return stride;
}
// Given a matrix type, returns the integer stride required for that matrix
// Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix
// when used as a member of an interface block
int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
{
glslang::TType elementType;
elementType.shallowCopy(matrixType);
elementType.clearArraySizes();
int size;
return glslangIntermediate->getBaseAlignment(matrixType, size, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
int stride;
glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
return stride;
}
// Given a member type of a struct, realign the current offset for it, and compute
......@@ -1764,7 +1783,8 @@ void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType
// but possibly not yet correctly aligned.
int memberSize;
int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
int dummyStride;
int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, dummyStride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
glslang::RoundToPow2(currentOffset, memberAlignment);
nextOffset = currentOffset + memberSize;
}
......
......@@ -281,18 +281,23 @@ Id Builder::makeMatrixType(Id component, int cols, int rows)
return type->getResultId();
}
Id Builder::makeArrayType(Id element, unsigned size)
// TODO: performance: track arrays per stride
// If a stride is supplied (non-zero) make an array.
// If no stride (0), reuse previous array types.
Id Builder::makeArrayType(Id element, unsigned size, int stride)
{
// First, we need a constant instruction for the size
Id sizeId = makeUintConstant(size);
// try to find existing type
Instruction* type;
for (int t = 0; t < (int)groupedTypes[OpTypeArray].size(); ++t) {
type = groupedTypes[OpTypeArray][t];
if (type->getIdOperand(0) == element &&
type->getIdOperand(1) == sizeId)
return type->getResultId();
if (stride == 0) {
// try to find existing type
for (int t = 0; t < (int)groupedTypes[OpTypeArray].size(); ++t) {
type = groupedTypes[OpTypeArray][t];
if (type->getIdOperand(0) == element &&
type->getIdOperand(1) == sizeId)
return type->getResultId();
}
}
// not found, make it
......
......@@ -102,7 +102,7 @@ public:
Id makeStructResultType(Id type0, Id type1);
Id makeVectorType(Id component, int size);
Id makeMatrixType(Id component, int cols, int rows);
Id makeArrayType(Id element, unsigned size);
Id makeArrayType(Id element, unsigned size, int stride); // 0 means no stride decoration
Id makeRuntimeArray(Id element);
Id makeFunctionType(Id returnType, std::vector<Id>& paramTypes);
Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format);
......
......@@ -5,7 +5,7 @@ Linked fragment stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 99
// Id's are bound by 100
Capability Shader
1: ExtInstImport "GLSL.std.450"
......@@ -24,39 +24,39 @@ Linked fragment stage:
Name 55 "sampR"
Name 63 "sampB"
Name 86 "samp2Da"
Name 90 "bn"
MemberName 90(bn) 0 "matra"
MemberName 90(bn) 1 "matca"
MemberName 90(bn) 2 "matr"
MemberName 90(bn) 3 "matc"
MemberName 90(bn) 4 "matrdef"
Name 92 ""
Name 95 "bi"
MemberName 95(bi) 0 "v"
Name 98 "bname"
Name 91 "bn"
MemberName 91(bn) 0 "matra"
MemberName 91(bn) 1 "matca"
MemberName 91(bn) 2 "matr"
MemberName 91(bn) 3 "matc"
MemberName 91(bn) 4 "matrdef"
Name 93 ""
Name 96 "bi"
MemberName 96(bi) 0 "v"
Name 99 "bname"
Decorate 16(gl_FrontFacing) BuiltIn FrontFacing
Decorate 33(gl_ClipDistance) BuiltIn ClipDistance
Decorate 89 ArrayStride 64
Decorate 89 ArrayStride 64
MemberDecorate 90(bn) 0 RowMajor
MemberDecorate 90(bn) 0 Offset 0
MemberDecorate 90(bn) 0 MatrixStride 16
MemberDecorate 90(bn) 1 ColMajor
MemberDecorate 90(bn) 1 Offset 256
MemberDecorate 90(bn) 1 MatrixStride 16
MemberDecorate 90(bn) 2 RowMajor
MemberDecorate 90(bn) 2 Offset 512
MemberDecorate 90(bn) 2 MatrixStride 16
MemberDecorate 90(bn) 3 ColMajor
MemberDecorate 90(bn) 3 Offset 576
MemberDecorate 90(bn) 3 MatrixStride 16
MemberDecorate 90(bn) 4 RowMajor
MemberDecorate 90(bn) 4 Offset 640
MemberDecorate 90(bn) 4 MatrixStride 16
Decorate 90(bn) Block
Decorate 94 ArrayStride 16
MemberDecorate 95(bi) 0 Offset 0
Decorate 95(bi) Block
Decorate 90 ArrayStride 64
MemberDecorate 91(bn) 0 RowMajor
MemberDecorate 91(bn) 0 Offset 0
MemberDecorate 91(bn) 0 MatrixStride 16
MemberDecorate 91(bn) 1 ColMajor
MemberDecorate 91(bn) 1 Offset 256
MemberDecorate 91(bn) 1 MatrixStride 16
MemberDecorate 91(bn) 2 RowMajor
MemberDecorate 91(bn) 2 Offset 512
MemberDecorate 91(bn) 2 MatrixStride 16
MemberDecorate 91(bn) 3 ColMajor
MemberDecorate 91(bn) 3 Offset 576
MemberDecorate 91(bn) 3 MatrixStride 16
MemberDecorate 91(bn) 4 RowMajor
MemberDecorate 91(bn) 4 Offset 640
MemberDecorate 91(bn) 4 MatrixStride 16
Decorate 91(bn) Block
Decorate 95 ArrayStride 16
MemberDecorate 96(bi) 0 Offset 0
Decorate 96(bi) Block
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
......@@ -108,15 +108,16 @@ Linked fragment stage:
87: TypeMatrix 26(fvec4) 4
88: 29(int) Constant 4
89: TypeArray 87 88
90(bn): TypeStruct 89 89 87 87 87
91: TypePointer Uniform 90(bn)
92: 91(ptr) Variable Uniform
93: TypeVector 6(float) 3
94: TypeArray 93(fvec3) 50
95(bi): TypeStruct 94
96: TypeArray 95(bi) 88
97: TypePointer Uniform 96
98(bname): 97(ptr) Variable Uniform
90: TypeArray 87 88
91(bn): TypeStruct 89 90 87 87 87
92: TypePointer Uniform 91(bn)
93: 92(ptr) Variable Uniform
94: TypeVector 6(float) 3
95: TypeArray 94(fvec3) 50
96(bi): TypeStruct 95
97: TypeArray 96(bi) 88
98: TypePointer Uniform 97
99(bname): 98(ptr) Variable Uniform
4(main): 2 Function None 3
5: Label
13: 12(ptr) Variable Function
......
......@@ -1206,6 +1206,10 @@ public:
arraySizes = new TArraySizes;
*arraySizes = s;
}
void clearArraySizes()
{
arraySizes = 0;
}
void addArrayOuterSizes(const TArraySizes& s)
{
if (arraySizes == nullptr)
......
......@@ -5353,7 +5353,8 @@ void TParseContext::fixBlockUniformOffsets(TQualifier& qualifier, TTypeList& typ
// modify just the children's view of matrix layout, if there is one for this member
TLayoutMatrix subMatrixLayout = typeList[member].type->getQualifier().layoutMatrix;
int memberAlignment = intermediate.getBaseAlignment(*typeList[member].type, memberSize, qualifier.layoutPacking == ElpStd140,
int dummyStride;
int memberAlignment = intermediate.getBaseAlignment(*typeList[member].type, memberSize, dummyStride, qualifier.layoutPacking == ElpStd140,
subMatrixLayout != ElmNone ? subMatrixLayout == ElmRowMajor : qualifier.layoutMatrix == ElmRowMajor);
if (memberQualifier.hasOffset()) {
// "The specified offset must be a multiple
......
......@@ -858,8 +858,14 @@ int TIntermediate::getBaseAlignmentScalar(const TType& type, int& size)
// otherwise it does not, yielding std430 rules.
//
// The size is returned in the 'size' parameter
//
// The stride is only non-0 for arrays or matrices, and is the stride of the
// top-level object nested within the type. E.g., for an array of matrices,
// it is the distances needed between matrices, despite the rules saying the
// stride comes from the flattening down to vectors.
//
// Return value is the alignment of the type.
int TIntermediate::getBaseAlignment(const TType& type, int& size, bool std140, bool rowMajor)
int TIntermediate::getBaseAlignment(const TType& type, int& size, int& stride, bool std140, bool rowMajor)
{
int alignment;
......@@ -916,16 +922,23 @@ int TIntermediate::getBaseAlignment(const TType& type, int& size, bool std140, b
//
// 10. If the member is an array of S structures, the S elements of the array are laid
// out in order, according to rule (9).
//
// Assuming, for rule 10: The stride is the same as the size of an element.
stride = 0;
int dummyStride;
// rules 4, 6, and 8
// rules 4, 6, 8, and 10
if (type.isArray()) {
// TODO: perf: this might be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
TType derefType(type, 0);
alignment = getBaseAlignment(derefType, size, std140, rowMajor);
alignment = getBaseAlignment(derefType, size, dummyStride, std140, rowMajor);
if (std140)
alignment = std::max(baseAlignmentVec4Std140, alignment);
RoundToPow2(size, alignment);
size *= type.getOuterArraySize();
stride = size; // uses full matrix size for stride of an array of matrices (not quite what rule 6/8, but what's expected)
// uses the assumption for rule 10 in the comment above
size = stride * type.getOuterArraySize();
return alignment;
}
......@@ -939,7 +952,7 @@ int TIntermediate::getBaseAlignment(const TType& type, int& size, bool std140, b
int memberSize;
// modify just the children's view of matrix layout, if there is one for this member
TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, std140,
int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, dummyStride, std140,
(subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
maxAlignment = std::max(maxAlignment, memberAlignment);
RoundToPow2(size, memberAlignment);
......@@ -971,14 +984,15 @@ int TIntermediate::getBaseAlignment(const TType& type, int& size, bool std140, b
// rule 5: deref to row, not to column, meaning the size of vector is num columns instead of num rows
TType derefType(type, 0, type.getQualifier().layoutMatrix == ElmRowMajor);
alignment = getBaseAlignment(derefType, size, std140, rowMajor);
alignment = getBaseAlignment(derefType, size, dummyStride, std140, rowMajor);
if (std140)
alignment = std::max(baseAlignmentVec4Std140, alignment);
RoundToPow2(size, alignment);
stride = size; // use intra-matrix stride for stride of a just a matrix
if (rowMajor)
size *= type.getMatrixRows();
size = stride * type.getMatrixRows();
else
size *= type.getMatrixCols();
size = stride * type.getMatrixCols();
return alignment;
}
......
......@@ -305,7 +305,7 @@ public:
}
int addXfbBufferOffset(const TType&);
unsigned int computeTypeXfbSize(const TType&, bool& containsDouble) const;
static int getBaseAlignment(const TType&, int& size, bool std140, bool rowMajor);
static int getBaseAlignment(const TType&, int& size, int& stride, bool std140, bool rowMajor);
protected:
void error(TInfoSink& infoSink, const char*);
......
......@@ -121,11 +121,12 @@ public:
return memberList[index].type->getQualifier().layoutOffset;
int memberSize;
int dummyStride;
int offset = 0;
for (int m = 0; m <= index; ++m) {
// modify just the children's view of matrix layout, if there is one for this member
TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
int memberAlignment = intermediate.getBaseAlignment(*memberList[m].type, memberSize, type.getQualifier().layoutPacking == ElpStd140,
int memberAlignment = intermediate.getBaseAlignment(*memberList[m].type, memberSize, dummyStride, type.getQualifier().layoutPacking == ElpStd140,
subMatrixLayout != ElmNone ? subMatrixLayout == ElmRowMajor : type.getQualifier().layoutMatrix == ElmRowMajor);
RoundToPow2(offset, memberAlignment);
if (m < index)
......@@ -144,7 +145,8 @@ public:
int lastOffset = getOffset(blockType, lastIndex);
int lastMemberSize;
intermediate.getBaseAlignment(*memberList[lastIndex].type, lastMemberSize, blockType.getQualifier().layoutPacking == ElpStd140,
int dummyStride;
intermediate.getBaseAlignment(*memberList[lastIndex].type, lastMemberSize, dummyStride, blockType.getQualifier().layoutPacking == ElpStd140,
blockType.getQualifier().layoutMatrix == ElmRowMajor);
return lastOffset + lastMemberSize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment