MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / getPackedSize

Method getPackedSize

source/opt/struct_packing_pass.cpp:233–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

231}
232
233uint32_t StructPackingPass::getPackedSize(const analysis::Type& type) const {
234 switch (type.kind()) {
235 case analysis::Type::kArray: {
236 if (const analysis::Array* arrayType = type.AsArray()) {
237 uint32_t size =
238 getPackedArrayStride(*arrayType) * getArrayLength(*arrayType);
239
240 // For arrays of vector and matrices in HLSL, the last element has a
241 // size depending on its vector/matrix size to allow packing other
242 // vectors in the last element.
243 const analysis::Type* arraySubType = arrayType->element_type();
244 if (isPackingHlsl(packingRules_) &&
245 arraySubType->kind() != analysis::Type::kStruct) {
246 size -= (4 - getScalarElementCount(*arraySubType)) *
247 getPackedBaseSize(*arraySubType);
248 }
249 return size;
250 }
251 break;
252 }
253 case analysis::Type::kStruct: {
254 uint32_t size = 0;
255 uint32_t padAlignment = 1;
256 for (const analysis::Type* memberType :
257 type.AsStruct()->element_types()) {
258 const uint32_t packedAlignment = getPackedAlignment(*memberType);
259 const uint32_t alignment =
260 std::max<uint32_t>(packedAlignment, padAlignment);
261 padAlignment = getPadAlignment(*memberType, packedAlignment);
262 size = alignPow2(size, alignment);
263 size += getPackedSize(*memberType);
264 }
265 return size;
266 }
267 default: {
268 const uint32_t baseAlignment = getPackedBaseSize(type);
269 if (isPackingScalar(packingRules_)) {
270 return getScalarElementCount(type) * baseAlignment;
271 } else {
272 uint32_t size = 0;
273 if (const analysis::Matrix* matrixType = type.AsMatrix()) {
274 const analysis::Vector* matrixSubType =
275 matrixType->element_type()->AsVector();
276 assert(matrixSubType != nullptr &&
277 "Matrix sub-type is expected to be a vector type");
278 if (isPackingVec4Padded(packingRules_) ||
279 matrixType->element_count() == 3)
280 size = matrixSubType->element_count() * baseAlignment * 4;
281 else
282 size = matrixSubType->element_count() * baseAlignment *
283 matrixType->element_count();
284
285 // For matrices in HLSL, the last element has a size depending on its
286 // vector size to allow packing other vectors in the last element.
287 if (isPackingHlsl(packingRules_)) {
288 size -= (4 - matrixSubType->element_count()) *
289 getPackedBaseSize(*matrixSubType);
290 }

Callers

nothing calls this directly

Calls 14

isPackingHlslFunction · 0.85
getScalarElementCountFunction · 0.85
getPackedBaseSizeFunction · 0.85
getPadAlignmentFunction · 0.85
alignPow2Function · 0.85
isPackingScalarFunction · 0.85
isPackingVec4PaddedFunction · 0.85
kindMethod · 0.80
AsArrayMethod · 0.80
AsStructMethod · 0.80
AsMatrixMethod · 0.80
AsVectorMethod · 0.80

Tested by

no test coverage detected