MCPcopy Create free account
hub / github.com/KhronosGroup/glslang / createMatrixConstructor

Method createMatrixConstructor

SPIRV/SpvBuilder.cpp:4308–4435  ·  view source on GitHub ↗

Comments in header

Source from the content-addressed store, hash-verified

4306
4307// Comments in header
4308Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
4309{
4310 Id componentTypeId = getScalarTypeId(resultTypeId);
4311 unsigned int numCols = getTypeNumColumns(resultTypeId);
4312 unsigned int numRows = getTypeNumRows(resultTypeId);
4313
4314 Instruction* instr = module.getInstruction(componentTypeId);
4315 const unsigned bitCount = instr->getImmediateOperand(0);
4316
4317 // Optimize matrix constructed from a bigger matrix
4318 if (isMatrix(sources[0]) && getNumColumns(sources[0]) >= numCols && getNumRows(sources[0]) >= numRows) {
4319 // To truncate the matrix to a smaller number of rows/columns, we need to:
4320 // 1. For each column, extract the column and truncate it to the required size using shuffle
4321 // 2. Assemble the resulting matrix from all columns
4322 Id matrix = sources[0];
4323 Id columnTypeId = getContainedTypeId(resultTypeId);
4324 Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
4325
4326 std::vector<unsigned> channels;
4327 for (unsigned int row = 0; row < numRows; ++row)
4328 channels.push_back(row);
4329
4330 std::vector<Id> matrixColumns;
4331 for (unsigned int col = 0; col < numCols; ++col) {
4332 std::vector<unsigned> indexes;
4333 indexes.push_back(col);
4334 Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
4335 setPrecision(colv, precision);
4336
4337 if (numRows != getNumRows(matrix)) {
4338 matrixColumns.push_back(createRvalueSwizzle(precision, columnTypeId, colv, channels));
4339 } else {
4340 matrixColumns.push_back(colv);
4341 }
4342 }
4343
4344 return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
4345 }
4346
4347 // Detect a matrix being constructed from a repeated vector of the correct size.
4348 // Create the composite directly from it.
4349 if (sources.size() == numCols && isVector(sources[0]) && getNumComponents(sources[0]) == numRows &&
4350 std::equal(sources.begin() + 1, sources.end(), sources.begin())) {
4351 return setPrecision(createCompositeConstruct(resultTypeId, sources), precision);
4352 }
4353
4354 // Otherwise, will use a two step process
4355 // 1. make a compile-time 2D array of values
4356 // 2. construct a matrix from that array
4357
4358 // Step 1.
4359
4360 // initialize the array to the identity matrix
4361 Id ids[maxMatrixSize][maxMatrixSize];
4362 Id one = (bitCount == 64 ? makeDoubleConstant(1.0) : makeFloatConstant(1.0));
4363 Id zero = (bitCount == 64 ? makeDoubleConstant(0.0) : makeFloatConstant(0.0));
4364 for (int col = 0; col < 4; ++col) {
4365 for (int row = 0; row < 4; ++row) {

Callers 1

visitAggregateMethod · 0.80

Calls 7

getInstructionMethod · 0.80
getImmediateOperandMethod · 0.80
beginMethod · 0.80
endMethod · 0.80
pop_backMethod · 0.80
push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected