2 // Copyright (C) 2013 LunarG, Inc.
3 // Copyright (C) 2017 ARM Limited.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
38 // Do link-time merging and validation of intermediate representations.
40 // Basic model is that during compilation, each compilation unit (shader) is
41 // compiled into one TIntermediate instance. Then, at link time, multiple
42 // units for the same stage can be merged together, which can generate errors.
43 // Then, after all merging, a single instance of TIntermediate represents
44 // the whole stage. A final error check can be done on the resulting stage,
45 // even if no merging was done (i.e., the stage was only one compilation unit).
48 #include "localintermediate.h"
49 #include "../Include/InfoSink.h"
54 // Link-time error emitter.
56 void TIntermediate::error(TInfoSink& infoSink, const char* message)
58 infoSink.info.prefix(EPrefixError);
59 infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
65 void TIntermediate::warn(TInfoSink& infoSink, const char* message)
67 infoSink.info.prefix(EPrefixWarning);
68 infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
71 // TODO: 4.4 offset/align: "Two blocks linked together in the same program with the same block
72 // name must have the exact same set of members qualified with offset and their integral-constant
73 // expression values must be the same, or a link-time error results."
76 // Merge the information from 'unit' into 'this'
78 void TIntermediate::merge(TInfoSink& infoSink, TIntermediate& unit)
80 mergeCallGraphs(infoSink, unit);
81 mergeModes(infoSink, unit);
82 mergeTrees(infoSink, unit);
85 void TIntermediate::mergeCallGraphs(TInfoSink& infoSink, TIntermediate& unit)
87 if (unit.getNumEntryPoints() > 0) {
88 if (getNumEntryPoints() > 0)
89 error(infoSink, "can't handle multiple entry points per stage");
91 entryPointName = unit.getEntryPointName();
92 entryPointMangledName = unit.getEntryPointMangledName();
95 numEntryPoints += unit.getNumEntryPoints();
97 callGraph.insert(callGraph.end(), unit.callGraph.begin(), unit.callGraph.end());
100 #define MERGE_MAX(member) member = std::max(member, unit.member)
101 #define MERGE_TRUE(member) if (unit.member) member = unit.member;
103 void TIntermediate::mergeModes(TInfoSink& infoSink, TIntermediate& unit)
105 if (language != unit.language)
106 error(infoSink, "stages must match when linking into a single stage");
108 if (source == EShSourceNone)
109 source = unit.source;
110 if (source != unit.source)
111 error(infoSink, "can't link compilation units from different source languages");
113 if (treeRoot == nullptr) {
114 profile = unit.profile;
115 version = unit.version;
116 requestedExtensions = unit.requestedExtensions;
118 if ((profile == EEsProfile) != (unit.profile == EEsProfile))
119 error(infoSink, "Cannot cross link ES and desktop profiles");
120 else if (unit.profile == ECompatibilityProfile)
121 profile = ECompatibilityProfile;
122 version = std::max(version, unit.version);
123 requestedExtensions.insert(unit.requestedExtensions.begin(), unit.requestedExtensions.end());
126 MERGE_MAX(spvVersion.spv);
127 MERGE_MAX(spvVersion.vulkanGlsl);
128 MERGE_MAX(spvVersion.vulkan);
129 MERGE_MAX(spvVersion.openGl);
131 numErrors += unit.getNumErrors();
132 numPushConstants += unit.numPushConstants;
134 if (unit.invocations != TQualifier::layoutNotSet) {
135 if (invocations == TQualifier::layoutNotSet)
136 invocations = unit.invocations;
137 else if (invocations != unit.invocations)
138 error(infoSink, "number of invocations must match between compilation units");
141 if (vertices == TQualifier::layoutNotSet)
142 vertices = unit.vertices;
143 else if (vertices != unit.vertices) {
144 if (language == EShLangGeometry
146 || language == EShLangMeshNV
149 error(infoSink, "Contradictory layout max_vertices values");
150 else if (language == EShLangTessControl)
151 error(infoSink, "Contradictory layout vertices values");
156 if (primitives == TQualifier::layoutNotSet)
157 primitives = unit.primitives;
158 else if (primitives != unit.primitives) {
159 if (language == EShLangMeshNV)
160 error(infoSink, "Contradictory layout max_primitives values");
166 if (inputPrimitive == ElgNone)
167 inputPrimitive = unit.inputPrimitive;
168 else if (inputPrimitive != unit.inputPrimitive)
169 error(infoSink, "Contradictory input layout primitives");
171 if (outputPrimitive == ElgNone)
172 outputPrimitive = unit.outputPrimitive;
173 else if (outputPrimitive != unit.outputPrimitive)
174 error(infoSink, "Contradictory output layout primitives");
176 if (originUpperLeft != unit.originUpperLeft || pixelCenterInteger != unit.pixelCenterInteger)
177 error(infoSink, "gl_FragCoord redeclarations must match across shaders");
179 if (vertexSpacing == EvsNone)
180 vertexSpacing = unit.vertexSpacing;
181 else if (vertexSpacing != unit.vertexSpacing)
182 error(infoSink, "Contradictory input vertex spacing");
184 if (vertexOrder == EvoNone)
185 vertexOrder = unit.vertexOrder;
186 else if (vertexOrder != unit.vertexOrder)
187 error(infoSink, "Contradictory triangle ordering");
189 MERGE_TRUE(pointMode);
191 for (int i = 0; i < 3; ++i) {
192 if (localSize[i] > 1)
193 localSize[i] = unit.localSize[i];
194 else if (localSize[i] != unit.localSize[i])
195 error(infoSink, "Contradictory local size");
197 if (localSizeSpecId[i] != TQualifier::layoutNotSet)
198 localSizeSpecId[i] = unit.localSizeSpecId[i];
199 else if (localSizeSpecId[i] != unit.localSizeSpecId[i])
200 error(infoSink, "Contradictory local size specialization ids");
203 MERGE_TRUE(earlyFragmentTests);
204 MERGE_TRUE(postDepthCoverage);
206 if (depthLayout == EldNone)
207 depthLayout = unit.depthLayout;
208 else if (depthLayout != unit.depthLayout)
209 error(infoSink, "Contradictory depth layouts");
211 MERGE_TRUE(depthReplacing);
212 MERGE_TRUE(hlslFunctionality1);
214 blendEquations |= unit.blendEquations;
218 for (size_t b = 0; b < xfbBuffers.size(); ++b) {
219 if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
220 xfbBuffers[b].stride = unit.xfbBuffers[b].stride;
221 else if (xfbBuffers[b].stride != unit.xfbBuffers[b].stride)
222 error(infoSink, "Contradictory xfb_stride");
223 xfbBuffers[b].implicitStride = std::max(xfbBuffers[b].implicitStride, unit.xfbBuffers[b].implicitStride);
224 if (unit.xfbBuffers[b].containsDouble)
225 xfbBuffers[b].containsDouble = true;
226 // TODO: 4.4 link: enhanced layouts: compare ranges
229 MERGE_TRUE(multiStream);
232 MERGE_TRUE(layoutOverrideCoverage);
233 MERGE_TRUE(geoPassthroughEXT);
236 for (unsigned int i = 0; i < unit.shiftBinding.size(); ++i) {
237 if (unit.shiftBinding[i] > 0)
238 setShiftBinding((TResourceType)i, unit.shiftBinding[i]);
241 for (unsigned int i = 0; i < unit.shiftBindingForSet.size(); ++i) {
242 for (auto it = unit.shiftBindingForSet[i].begin(); it != unit.shiftBindingForSet[i].end(); ++it)
243 setShiftBindingForSet((TResourceType)i, it->second, it->first);
246 resourceSetBinding.insert(resourceSetBinding.end(), unit.resourceSetBinding.begin(), unit.resourceSetBinding.end());
248 MERGE_TRUE(autoMapBindings);
249 MERGE_TRUE(autoMapLocations);
251 MERGE_TRUE(flattenUniformArrays);
252 MERGE_TRUE(useUnknownFormat);
253 MERGE_TRUE(hlslOffsets);
254 MERGE_TRUE(useStorageBuffer);
255 MERGE_TRUE(hlslIoMapping);
261 MERGE_TRUE(needToLegalize);
262 MERGE_TRUE(binaryDoubleOutput);
266 // Merge the 'unit' AST into 'this' AST.
267 // That includes rationalizing the unique IDs, which were set up independently,
268 // and might have overlaps that are not the same symbol, or might have different
269 // IDs for what should be the same shared symbol.
271 void TIntermediate::mergeTrees(TInfoSink& infoSink, TIntermediate& unit)
273 if (unit.treeRoot == nullptr)
276 if (treeRoot == nullptr) {
277 treeRoot = unit.treeRoot;
281 // Getting this far means we have two existing trees to merge...
283 numShaderRecordNVBlocks += unit.numShaderRecordNVBlocks;
287 numTaskNVBlocks += unit.numTaskNVBlocks;
290 // Get the top-level globals of each unit
291 TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
292 TIntermSequence& unitGlobals = unit.treeRoot->getAsAggregate()->getSequence();
294 // Get the linker-object lists
295 TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
296 const TIntermSequence& unitLinkerObjects = unit.findLinkerObjects()->getSequence();
298 // Map by global name to unique ID to rationalize the same object having
299 // differing IDs in different trees.
300 TMap<TString, int> idMap;
302 seedIdMap(idMap, maxId);
303 remapIds(idMap, maxId + 1, unit);
305 mergeBodies(infoSink, globals, unitGlobals);
306 mergeLinkerObjects(infoSink, linkerObjects, unitLinkerObjects);
307 ioAccessed.insert(unit.ioAccessed.begin(), unit.ioAccessed.end());
310 // Traverser that seeds an ID map with all built-ins, and tracks the
312 // (It would be nice to put this in a function, but that causes warnings
313 // on having no bodies for the copy-constructor/operator=.)
314 class TBuiltInIdTraverser : public TIntermTraverser {
316 TBuiltInIdTraverser(TMap<TString, int>& idMap) : idMap(idMap), maxId(0) { }
317 // If it's a built in, add it to the map.
319 virtual void visitSymbol(TIntermSymbol* symbol)
321 const TQualifier& qualifier = symbol->getType().getQualifier();
322 if (qualifier.builtIn != EbvNone)
323 idMap[symbol->getName()] = symbol->getId();
324 maxId = std::max(maxId, symbol->getId());
326 int getMaxId() const { return maxId; }
328 TBuiltInIdTraverser(TBuiltInIdTraverser&);
329 TBuiltInIdTraverser& operator=(TBuiltInIdTraverser&);
330 TMap<TString, int>& idMap;
334 // Traverser that seeds an ID map with non-builtins.
335 // (It would be nice to put this in a function, but that causes warnings
336 // on having no bodies for the copy-constructor/operator=.)
337 class TUserIdTraverser : public TIntermTraverser {
339 TUserIdTraverser(TMap<TString, int>& idMap) : idMap(idMap) { }
340 // If its a non-built-in global, add it to the map.
341 virtual void visitSymbol(TIntermSymbol* symbol)
343 const TQualifier& qualifier = symbol->getType().getQualifier();
344 if (qualifier.builtIn == EbvNone)
345 idMap[symbol->getName()] = symbol->getId();
349 TUserIdTraverser(TUserIdTraverser&);
350 TUserIdTraverser& operator=(TUserIdTraverser&);
351 TMap<TString, int>& idMap; // over biggest id
354 // Initialize the the ID map with what we know of 'this' AST.
355 void TIntermediate::seedIdMap(TMap<TString, int>& idMap, int& maxId)
357 // all built-ins everywhere need to align on IDs and contribute to the max ID
358 TBuiltInIdTraverser builtInIdTraverser(idMap);
359 treeRoot->traverse(&builtInIdTraverser);
360 maxId = builtInIdTraverser.getMaxId();
362 // user variables in the linker object list need to align on ids
363 TUserIdTraverser userIdTraverser(idMap);
364 findLinkerObjects()->traverse(&userIdTraverser);
367 // Traverser to map an AST ID to what was known from the seeding AST.
368 // (It would be nice to put this in a function, but that causes warnings
369 // on having no bodies for the copy-constructor/operator=.)
370 class TRemapIdTraverser : public TIntermTraverser {
372 TRemapIdTraverser(const TMap<TString, int>& idMap, int idShift) : idMap(idMap), idShift(idShift) { }
374 // - if the same symbol, adopt the 'this' ID
375 // - otherwise, ensure a unique ID by shifting to a new space
376 virtual void visitSymbol(TIntermSymbol* symbol)
378 const TQualifier& qualifier = symbol->getType().getQualifier();
379 bool remapped = false;
380 if (qualifier.isLinkable() || qualifier.builtIn != EbvNone) {
381 auto it = idMap.find(symbol->getName());
382 if (it != idMap.end()) {
383 symbol->changeId(it->second);
388 symbol->changeId(symbol->getId() + idShift);
391 TRemapIdTraverser(TRemapIdTraverser&);
392 TRemapIdTraverser& operator=(TRemapIdTraverser&);
393 const TMap<TString, int>& idMap;
397 void TIntermediate::remapIds(const TMap<TString, int>& idMap, int idShift, TIntermediate& unit)
399 // Remap all IDs to either share or be unique, as dictated by the idMap and idShift.
400 TRemapIdTraverser idTraverser(idMap, idShift);
401 unit.getTreeRoot()->traverse(&idTraverser);
405 // Merge the function bodies and global-level initializers from unitGlobals into globals.
406 // Will error check duplication of function bodies for the same signature.
408 void TIntermediate::mergeBodies(TInfoSink& infoSink, TIntermSequence& globals, const TIntermSequence& unitGlobals)
410 // TODO: link-time performance: Processing in alphabetical order will be faster
412 // Error check the global objects, not including the linker objects
413 for (unsigned int child = 0; child < globals.size() - 1; ++child) {
414 for (unsigned int unitChild = 0; unitChild < unitGlobals.size() - 1; ++unitChild) {
415 TIntermAggregate* body = globals[child]->getAsAggregate();
416 TIntermAggregate* unitBody = unitGlobals[unitChild]->getAsAggregate();
417 if (body && unitBody && body->getOp() == EOpFunction && unitBody->getOp() == EOpFunction && body->getName() == unitBody->getName()) {
418 error(infoSink, "Multiple function bodies in multiple compilation units for the same signature in the same stage:");
419 infoSink.info << " " << globals[child]->getAsAggregate()->getName() << "\n";
424 // Merge the global objects, just in front of the linker objects
425 globals.insert(globals.end() - 1, unitGlobals.begin(), unitGlobals.end() - 1);
429 // Merge the linker objects from unitLinkerObjects into linkerObjects.
430 // Duplication is expected and filtered out, but contradictions are an error.
432 void TIntermediate::mergeLinkerObjects(TInfoSink& infoSink, TIntermSequence& linkerObjects, const TIntermSequence& unitLinkerObjects)
434 // Error check and merge the linker objects (duplicates should not be created)
435 std::size_t initialNumLinkerObjects = linkerObjects.size();
436 for (unsigned int unitLinkObj = 0; unitLinkObj < unitLinkerObjects.size(); ++unitLinkObj) {
438 for (std::size_t linkObj = 0; linkObj < initialNumLinkerObjects; ++linkObj) {
439 TIntermSymbol* symbol = linkerObjects[linkObj]->getAsSymbolNode();
440 TIntermSymbol* unitSymbol = unitLinkerObjects[unitLinkObj]->getAsSymbolNode();
441 assert(symbol && unitSymbol);
442 if (symbol->getName() == unitSymbol->getName()) {
446 // but if one has an initializer and the other does not, update
448 if (symbol->getConstArray().empty() && ! unitSymbol->getConstArray().empty())
449 symbol->setConstArray(unitSymbol->getConstArray());
451 // Similarly for binding
452 if (! symbol->getQualifier().hasBinding() && unitSymbol->getQualifier().hasBinding())
453 symbol->getQualifier().layoutBinding = unitSymbol->getQualifier().layoutBinding;
455 // Update implicit array sizes
456 mergeImplicitArraySizes(symbol->getWritableType(), unitSymbol->getType());
458 // Check for consistent types/qualification/initializers etc.
459 mergeErrorCheck(infoSink, *symbol, *unitSymbol, false);
463 linkerObjects.push_back(unitLinkerObjects[unitLinkObj]);
467 // TODO 4.5 link functionality: cull distance array size checking
469 // Recursively merge the implicit array sizes through the objects' respective type trees.
470 void TIntermediate::mergeImplicitArraySizes(TType& type, const TType& unitType)
472 if (type.isUnsizedArray()) {
473 if (unitType.isUnsizedArray()) {
474 type.updateImplicitArraySize(unitType.getImplicitArraySize());
475 if (unitType.isArrayVariablyIndexed())
476 type.setArrayVariablyIndexed();
477 } else if (unitType.isSizedArray())
478 type.changeOuterArraySize(unitType.getOuterArraySize());
481 // Type mismatches are caught and reported after this, just be careful for now.
482 if (! type.isStruct() || ! unitType.isStruct() || type.getStruct()->size() != unitType.getStruct()->size())
485 for (int i = 0; i < (int)type.getStruct()->size(); ++i)
486 mergeImplicitArraySizes(*(*type.getStruct())[i].type, *(*unitType.getStruct())[i].type);
490 // Compare two global objects from two compilation units and see if they match
491 // well enough. Rules can be different for intra- vs. cross-stage matching.
493 // This function only does one of intra- or cross-stage matching per call.
495 void TIntermediate::mergeErrorCheck(TInfoSink& infoSink, const TIntermSymbol& symbol, const TIntermSymbol& unitSymbol, bool crossStage)
497 bool writeTypeComparison = false;
499 // Types have to match
500 if (symbol.getType() != unitSymbol.getType()) {
501 // but, we make an exception if one is an implicit array and the other is sized
502 if (! (symbol.getType().isArray() && unitSymbol.getType().isArray() &&
503 symbol.getType().sameElementType(unitSymbol.getType()) &&
504 (symbol.getType().isUnsizedArray() || unitSymbol.getType().isUnsizedArray()))) {
505 error(infoSink, "Types must match:");
506 writeTypeComparison = true;
510 // Qualifiers have to (almost) match
513 if (symbol.getQualifier().storage != unitSymbol.getQualifier().storage) {
514 error(infoSink, "Storage qualifiers must match:");
515 writeTypeComparison = true;
519 if (symbol.getQualifier().precision != unitSymbol.getQualifier().precision) {
520 error(infoSink, "Precision qualifiers must match:");
521 writeTypeComparison = true;
525 if (! crossStage && symbol.getQualifier().invariant != unitSymbol.getQualifier().invariant) {
526 error(infoSink, "Presence of invariant qualifier must match:");
527 writeTypeComparison = true;
531 if (! crossStage && symbol.getQualifier().noContraction != unitSymbol.getQualifier().noContraction) {
532 error(infoSink, "Presence of precise qualifier must match:");
533 writeTypeComparison = true;
536 // Auxiliary and interpolation...
537 if (symbol.getQualifier().centroid != unitSymbol.getQualifier().centroid ||
538 symbol.getQualifier().smooth != unitSymbol.getQualifier().smooth ||
539 symbol.getQualifier().flat != unitSymbol.getQualifier().flat ||
540 symbol.getQualifier().sample != unitSymbol.getQualifier().sample ||
541 symbol.getQualifier().patch != unitSymbol.getQualifier().patch ||
542 symbol.getQualifier().nopersp != unitSymbol.getQualifier().nopersp) {
543 error(infoSink, "Interpolation and auxiliary storage qualifiers must match:");
544 writeTypeComparison = true;
548 if (symbol.getQualifier().coherent != unitSymbol.getQualifier().coherent ||
549 symbol.getQualifier().devicecoherent != unitSymbol.getQualifier().devicecoherent ||
550 symbol.getQualifier().queuefamilycoherent != unitSymbol.getQualifier().queuefamilycoherent ||
551 symbol.getQualifier().workgroupcoherent != unitSymbol.getQualifier().workgroupcoherent ||
552 symbol.getQualifier().subgroupcoherent != unitSymbol.getQualifier().subgroupcoherent ||
553 symbol.getQualifier().nonprivate != unitSymbol.getQualifier().nonprivate ||
554 symbol.getQualifier().volatil != unitSymbol.getQualifier().volatil ||
555 symbol.getQualifier().restrict != unitSymbol.getQualifier().restrict ||
556 symbol.getQualifier().readonly != unitSymbol.getQualifier().readonly ||
557 symbol.getQualifier().writeonly != unitSymbol.getQualifier().writeonly) {
558 error(infoSink, "Memory qualifiers must match:");
559 writeTypeComparison = true;
563 // TODO: 4.4 enhanced layouts: Generalize to include offset/align: current spec
564 // requires separate user-supplied offset from actual computed offset, but
565 // current implementation only has one offset.
566 if (symbol.getQualifier().layoutMatrix != unitSymbol.getQualifier().layoutMatrix ||
567 symbol.getQualifier().layoutPacking != unitSymbol.getQualifier().layoutPacking ||
568 symbol.getQualifier().layoutLocation != unitSymbol.getQualifier().layoutLocation ||
569 symbol.getQualifier().layoutComponent != unitSymbol.getQualifier().layoutComponent ||
570 symbol.getQualifier().layoutIndex != unitSymbol.getQualifier().layoutIndex ||
571 symbol.getQualifier().layoutBinding != unitSymbol.getQualifier().layoutBinding ||
572 (symbol.getQualifier().hasBinding() && (symbol.getQualifier().layoutOffset != unitSymbol.getQualifier().layoutOffset))) {
573 error(infoSink, "Layout qualification must match:");
574 writeTypeComparison = true;
577 // Initializers have to match, if both are present, and if we don't already know the types don't match
578 if (! writeTypeComparison) {
579 if (! symbol.getConstArray().empty() && ! unitSymbol.getConstArray().empty()) {
580 if (symbol.getConstArray() != unitSymbol.getConstArray()) {
581 error(infoSink, "Initializers must match:");
582 infoSink.info << " " << symbol.getName() << "\n";
587 if (writeTypeComparison)
588 infoSink.info << " " << symbol.getName() << ": \"" << symbol.getType().getCompleteString() << "\" versus \"" <<
589 unitSymbol.getType().getCompleteString() << "\"\n";
593 // Do final link-time error checking of a complete (merged) intermediate representation.
594 // (Much error checking was done during merging).
596 // Also, lock in defaults of things not set, including array sizes.
598 void TIntermediate::finalCheck(TInfoSink& infoSink, bool keepUncalled)
600 if (getTreeRoot() == nullptr)
603 if (numEntryPoints < 1) {
604 if (source == EShSourceGlsl)
605 error(infoSink, "Missing entry point: Each stage requires one entry point");
607 warn(infoSink, "Entry point not found");
610 if (numPushConstants > 1)
611 error(infoSink, "Only one push_constant block is allowed per stage");
613 // recursion and missing body checking
614 checkCallGraphCycles(infoSink);
615 checkCallGraphBodies(infoSink, keepUncalled);
617 // overlap/alias/missing I/O, etc.
618 inOutLocationCheck(infoSink);
621 if (invocations == TQualifier::layoutNotSet)
624 if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipVertex"))
625 error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
626 if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_ClipVertex"))
627 error(infoSink, "Can only use one of gl_CullDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
629 if (userOutputUsed() && (inIoAccessed("gl_FragColor") || inIoAccessed("gl_FragData")))
630 error(infoSink, "Cannot use gl_FragColor or gl_FragData when using user-defined outputs");
631 if (inIoAccessed("gl_FragColor") && inIoAccessed("gl_FragData"))
632 error(infoSink, "Cannot use both gl_FragColor and gl_FragData");
634 for (size_t b = 0; b < xfbBuffers.size(); ++b) {
635 if (xfbBuffers[b].containsDouble)
636 RoundToPow2(xfbBuffers[b].implicitStride, 8);
638 // "It is a compile-time or link-time error to have
639 // any xfb_offset that overflows xfb_stride, whether stated on declarations before or after the xfb_stride, or
640 // in different compilation units. While xfb_stride can be declared multiple times for the same buffer, it is a
641 // compile-time or link-time error to have different values specified for the stride for the same buffer."
642 if (xfbBuffers[b].stride != TQualifier::layoutXfbStrideEnd && xfbBuffers[b].implicitStride > xfbBuffers[b].stride) {
643 error(infoSink, "xfb_stride is too small to hold all buffer entries:");
644 infoSink.info.prefix(EPrefixError);
645 infoSink.info << " xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << ", minimum stride needed: " << xfbBuffers[b].implicitStride << "\n";
647 if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
648 xfbBuffers[b].stride = xfbBuffers[b].implicitStride;
650 // "If the buffer is capturing any
651 // outputs with double-precision components, the stride must be a multiple of 8, otherwise it must be a
652 // multiple of 4, or a compile-time or link-time error results."
653 if (xfbBuffers[b].containsDouble && ! IsMultipleOfPow2(xfbBuffers[b].stride, 8)) {
654 error(infoSink, "xfb_stride must be multiple of 8 for buffer holding a double:");
655 infoSink.info.prefix(EPrefixError);
656 infoSink.info << " xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
657 } else if (! IsMultipleOfPow2(xfbBuffers[b].stride, 4)) {
658 error(infoSink, "xfb_stride must be multiple of 4:");
659 infoSink.info.prefix(EPrefixError);
660 infoSink.info << " xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
663 // "The resulting stride (implicit or explicit), when divided by 4, must be less than or equal to the
664 // implementation-dependent constant gl_MaxTransformFeedbackInterleavedComponents."
665 if (xfbBuffers[b].stride > (unsigned int)(4 * resources.maxTransformFeedbackInterleavedComponents)) {
666 error(infoSink, "xfb_stride is too large:");
667 infoSink.info.prefix(EPrefixError);
668 infoSink.info << " xfb_buffer " << (unsigned int)b << ", components (1/4 stride) needed are " << xfbBuffers[b].stride/4 << ", gl_MaxTransformFeedbackInterleavedComponents is " << resources.maxTransformFeedbackInterleavedComponents << "\n";
675 case EShLangTessControl:
676 if (vertices == TQualifier::layoutNotSet)
677 error(infoSink, "At least one shader must specify an output layout(vertices=...)");
679 case EShLangTessEvaluation:
680 if (source == EShSourceGlsl) {
681 if (inputPrimitive == ElgNone)
682 error(infoSink, "At least one shader must specify an input layout primitive");
683 if (vertexSpacing == EvsNone)
684 vertexSpacing = EvsEqual;
685 if (vertexOrder == EvoNone)
686 vertexOrder = EvoCcw;
689 case EShLangGeometry:
690 if (inputPrimitive == ElgNone)
691 error(infoSink, "At least one shader must specify an input layout primitive");
692 if (outputPrimitive == ElgNone)
693 error(infoSink, "At least one shader must specify an output layout primitive");
694 if (vertices == TQualifier::layoutNotSet)
695 error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
697 case EShLangFragment:
698 // for GL_ARB_post_depth_coverage, EarlyFragmentTest is set automatically in
699 // ParseHelper.cpp. So if we reach here, this must be GL_EXT_post_depth_coverage
700 // requiring explicit early_fragment_tests
701 if (getPostDepthCoverage() && !getEarlyFragmentTests())
702 error(infoSink, "post_depth_coverage requires early_fragment_tests");
708 case EShLangRayGenNV:
709 case EShLangIntersectNV:
710 case EShLangAnyHitNV:
711 case EShLangClosestHitNV:
713 case EShLangCallableNV:
714 if (numShaderRecordNVBlocks > 1)
715 error(infoSink, "Only one shaderRecordNVX buffer block is allowed per stage");
718 // NV_mesh_shader doesn't allow use of both single-view and per-view builtins.
719 if (inIoAccessed("gl_Position") && inIoAccessed("gl_PositionPerViewNV"))
720 error(infoSink, "Can only use one of gl_Position or gl_PositionPerViewNV");
721 if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipDistancePerViewNV"))
722 error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipDistancePerViewNV");
723 if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_CullDistancePerViewNV"))
724 error(infoSink, "Can only use one of gl_CullDistance or gl_CullDistancePerViewNV");
725 if (inIoAccessed("gl_Layer") && inIoAccessed("gl_LayerPerViewNV"))
726 error(infoSink, "Can only use one of gl_Layer or gl_LayerPerViewNV");
727 if (inIoAccessed("gl_ViewportMask") && inIoAccessed("gl_ViewportMaskPerViewNV"))
728 error(infoSink, "Can only use one of gl_ViewportMask or gl_ViewportMaskPerViewNV");
729 if (outputPrimitive == ElgNone)
730 error(infoSink, "At least one shader must specify an output layout primitive");
731 if (vertices == TQualifier::layoutNotSet)
732 error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
733 if (primitives == TQualifier::layoutNotSet)
734 error(infoSink, "At least one shader must specify a layout(max_primitives = value)");
737 if (numTaskNVBlocks > 1)
738 error(infoSink, "Only one taskNV interface block is allowed per shader");
743 error(infoSink, "Unknown Stage.");
747 // Process the tree for any node-specific work.
748 class TFinalLinkTraverser : public TIntermTraverser {
750 TFinalLinkTraverser() { }
751 virtual ~TFinalLinkTraverser() { }
753 virtual void visitSymbol(TIntermSymbol* symbol)
755 // Implicitly size arrays.
756 // If an unsized array is left as unsized, it effectively
757 // becomes run-time sized.
758 symbol->getWritableType().adoptImplicitArraySizes(false);
760 } finalLinkTraverser;
762 treeRoot->traverse(&finalLinkTraverser);
766 // See if the call graph contains any static recursion, which is disallowed
767 // by the specification.
769 void TIntermediate::checkCallGraphCycles(TInfoSink& infoSink)
771 // Clear fields we'll use for this.
772 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
773 call->visited = false;
774 call->currentPath = false;
775 call->errorGiven = false;
779 // Loop, looking for a new connected subgraph. One subgraph is handled per loop iteration.
784 // See if we have unvisited parts of the graph.
786 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
787 if (! call->visited) {
793 // If not, we are done.
797 // Otherwise, we found a new subgraph, process it:
798 // See what all can be reached by this new root, and if any of
799 // that is recursive. This is done by depth-first traversals, seeing
800 // if a new call is found that was already in the currentPath (a back edge),
801 // thereby detecting recursion.
802 std::list<TCall*> stack;
803 newRoot->currentPath = true; // currentPath will be true iff it is on the stack
804 stack.push_back(newRoot);
805 while (! stack.empty()) {
807 TCall* call = stack.back();
809 // Add to the stack just one callee.
810 // This algorithm always terminates, because only !visited and !currentPath causes a push
811 // and all pushes change currentPath to true, and all pops change visited to true.
812 TGraph::iterator child = callGraph.begin();
813 for (; child != callGraph.end(); ++child) {
815 // If we already visited this node, its whole subgraph has already been processed, so skip it.
819 if (call->callee == child->caller) {
820 if (child->currentPath) {
821 // Then, we found a back edge
822 if (! child->errorGiven) {
823 error(infoSink, "Recursion detected:");
824 infoSink.info << " " << call->callee << " calling " << child->callee << "\n";
825 child->errorGiven = true;
829 child->currentPath = true;
830 stack.push_back(&(*child));
835 if (child == callGraph.end()) {
836 // no more callees, we bottomed out, never look at this node again
837 stack.back()->currentPath = false;
838 stack.back()->visited = true;
841 } // end while, meaning nothing left to process in this subtree
843 } while (newRoot); // redundant loop check; should always exit via the 'break' above
847 // See which functions are reachable from the entry point and which have bodies.
848 // Reachable ones with missing bodies are errors.
849 // Unreachable bodies are dead code.
851 void TIntermediate::checkCallGraphBodies(TInfoSink& infoSink, bool keepUncalled)
853 // Clear fields we'll use for this.
854 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
855 call->visited = false;
856 call->calleeBodyPosition = -1;
859 // The top level of the AST includes function definitions (bodies).
860 // Compare these to function calls in the call graph.
861 // We'll end up knowing which have bodies, and if so,
862 // how to map the call-graph node to the location in the AST.
863 TIntermSequence &functionSequence = getTreeRoot()->getAsAggregate()->getSequence();
864 std::vector<bool> reachable(functionSequence.size(), true); // so that non-functions are reachable
865 for (int f = 0; f < (int)functionSequence.size(); ++f) {
866 glslang::TIntermAggregate* node = functionSequence[f]->getAsAggregate();
867 if (node && (node->getOp() == glslang::EOpFunction)) {
868 if (node->getName().compare(getEntryPointMangledName().c_str()) != 0)
869 reachable[f] = false; // so that function bodies are unreachable, until proven otherwise
870 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
871 if (call->callee == node->getName())
872 call->calleeBodyPosition = f;
877 // Start call-graph traversal by visiting the entry point nodes.
878 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
879 if (call->caller.compare(getEntryPointMangledName().c_str()) == 0)
880 call->visited = true;
883 // Propagate 'visited' through the call-graph to every part of the graph it
884 // can reach (seeded with the entry-point setting above).
888 for (auto call1 = callGraph.begin(); call1 != callGraph.end(); ++call1) {
889 if (call1->visited) {
890 for (TGraph::iterator call2 = callGraph.begin(); call2 != callGraph.end(); ++call2) {
891 if (! call2->visited) {
892 if (call1->callee == call2->caller) {
894 call2->visited = true;
902 // Any call-graph node set to visited but without a callee body is an error.
903 for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
905 if (call->calleeBodyPosition == -1) {
906 error(infoSink, "No function definition (body) found: ");
907 infoSink.info << " " << call->callee << "\n";
909 reachable[call->calleeBodyPosition] = true;
913 // Bodies in the AST not reached by the call graph are dead;
914 // clear them out, since they can't be reached and also can't
915 // be translated further due to possibility of being ill defined.
916 if (! keepUncalled) {
917 for (int f = 0; f < (int)functionSequence.size(); ++f) {
919 functionSequence[f] = nullptr;
921 functionSequence.erase(std::remove(functionSequence.begin(), functionSequence.end(), nullptr), functionSequence.end());
926 // Satisfy rules for location qualifiers on inputs and outputs
928 void TIntermediate::inOutLocationCheck(TInfoSink& infoSink)
930 // ES 3.0 requires all outputs to have location qualifiers if there is more than one output
931 bool fragOutWithNoLocation = false;
934 // TODO: linker functionality: location collision checking
936 TIntermSequence& linkObjects = findLinkerObjects()->getSequence();
937 for (size_t i = 0; i < linkObjects.size(); ++i) {
938 const TType& type = linkObjects[i]->getAsTyped()->getType();
939 const TQualifier& qualifier = type.getQualifier();
940 if (language == EShLangFragment) {
941 if (qualifier.storage == EvqVaryingOut && qualifier.builtIn == EbvNone) {
943 if (!qualifier.hasAnyLocation())
944 fragOutWithNoLocation = true;
949 if (profile == EEsProfile) {
950 if (numFragOut > 1 && fragOutWithNoLocation)
951 error(infoSink, "when more than one fragment shader output, all must have location qualifiers");
955 TIntermAggregate* TIntermediate::findLinkerObjects() const
957 // Get the top-level globals
958 TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
960 // Get the last member of the sequences, expected to be the linker-object lists
961 assert(globals.back()->getAsAggregate()->getOp() == EOpLinkerObjects);
963 return globals.back()->getAsAggregate();
966 // See if a variable was both a user-declared output and used.
967 // Note: the spec discusses writing to one, but this looks at read or write, which
968 // is more useful, and perhaps the spec should be changed to reflect that.
969 bool TIntermediate::userOutputUsed() const
971 const TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
974 for (size_t i = 0; i < linkerObjects.size(); ++i) {
975 const TIntermSymbol& symbolNode = *linkerObjects[i]->getAsSymbolNode();
976 if (symbolNode.getQualifier().storage == EvqVaryingOut &&
977 symbolNode.getName().compare(0, 3, "gl_") != 0 &&
978 inIoAccessed(symbolNode.getName())) {
987 // Accumulate locations used for inputs, outputs, and uniforms, and check for collisions
988 // as the accumulation is done.
990 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
992 // typeCollision is set to true if there is no direct collision, but the types in the same location
995 int TIntermediate::addUsedLocation(const TQualifier& qualifier, const TType& type, bool& typeCollision)
997 typeCollision = false;
1000 if (qualifier.isPipeInput())
1002 else if (qualifier.isPipeOutput())
1004 else if (qualifier.storage == EvqUniform)
1006 else if (qualifier.storage == EvqBuffer)
1012 if (qualifier.isUniformOrBuffer() || qualifier.isTaskMemory()) {
1013 if (type.isSizedArray())
1014 size = type.getCumulativeArraySize();
1018 // Strip off the outer array dimension for those having an extra one.
1019 if (type.isArray() && qualifier.isArrayedIo(language)) {
1020 TType elementType(type, 0);
1021 size = computeTypeLocationSize(elementType, language);
1023 size = computeTypeLocationSize(type, language);
1026 // Locations, and components within locations.
1028 // Almost always, dealing with components means a single location is involved.
1029 // The exception is a dvec3. From the spec:
1031 // "A dvec3 will consume all four components of the first location and components 0 and 1 of
1032 // the second location. This leaves components 2 and 3 available for other component-qualified
1035 // That means, without ever mentioning a component, a component range
1036 // for a different location gets specified, if it's not a vertex shader input. (!)
1037 // (A vertex shader input will show using only one location, even for a dvec3/4.)
1039 // So, for the case of dvec3, we need two independent ioRanges.
1041 int collision = -1; // no collision
1042 if (size == 2 && type.getBasicType() == EbtDouble && type.getVectorSize() == 3 &&
1043 (qualifier.isPipeInput() || qualifier.isPipeOutput())) {
1044 // Dealing with dvec3 in/out split across two locations.
1045 // Need two io-ranges.
1046 // The case where the dvec3 doesn't start at component 0 was previously caught as overflow.
1049 TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation);
1050 TRange componentRange(0, 3);
1051 TIoRange range(locationRange, componentRange, type.getBasicType(), 0);
1053 // check for collisions
1054 collision = checkLocationRange(set, range, type, typeCollision);
1055 if (collision < 0) {
1056 usedIo[set].push_back(range);
1059 TRange locationRange2(qualifier.layoutLocation + 1, qualifier.layoutLocation + 1);
1060 TRange componentRange2(0, 1);
1061 TIoRange range2(locationRange2, componentRange2, type.getBasicType(), 0);
1063 // check for collisions
1064 collision = checkLocationRange(set, range2, type, typeCollision);
1066 usedIo[set].push_back(range2);
1069 // Not a dvec3 in/out split across two locations, generic path.
1070 // Need a single IO-range block.
1072 TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation + size - 1);
1073 TRange componentRange(0, 3);
1074 if (qualifier.hasComponent() || type.getVectorSize() > 0) {
1075 int consumedComponents = type.getVectorSize() * (type.getBasicType() == EbtDouble ? 2 : 1);
1076 if (qualifier.hasComponent())
1077 componentRange.start = qualifier.layoutComponent;
1078 componentRange.last = componentRange.start + consumedComponents - 1;
1081 // combine location and component ranges
1082 TIoRange range(locationRange, componentRange, type.getBasicType(), qualifier.hasIndex() ? qualifier.layoutIndex : 0);
1084 // check for collisions, except for vertex inputs on desktop targeting OpenGL
1085 if (! (profile != EEsProfile && language == EShLangVertex && qualifier.isPipeInput()) || spvVersion.vulkan > 0)
1086 collision = checkLocationRange(set, range, type, typeCollision);
1089 usedIo[set].push_back(range);
1095 // Compare a new (the passed in) 'range' against the existing set, and see
1096 // if there are any collisions.
1098 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1100 int TIntermediate::checkLocationRange(int set, const TIoRange& range, const TType& type, bool& typeCollision)
1102 for (size_t r = 0; r < usedIo[set].size(); ++r) {
1103 if (range.overlap(usedIo[set][r])) {
1104 // there is a collision; pick one
1105 return std::max(range.location.start, usedIo[set][r].location.start);
1106 } else if (range.location.overlap(usedIo[set][r].location) && type.getBasicType() != usedIo[set][r].basicType) {
1107 // aliased-type mismatch
1108 typeCollision = true;
1109 return std::max(range.location.start, usedIo[set][r].location.start);
1113 return -1; // no collision
1116 // Accumulate bindings and offsets, and check for collisions
1117 // as the accumulation is done.
1119 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1121 int TIntermediate::addUsedOffsets(int binding, int offset, int numOffsets)
1123 TRange bindingRange(binding, binding);
1124 TRange offsetRange(offset, offset + numOffsets - 1);
1125 TOffsetRange range(bindingRange, offsetRange);
1127 // check for collisions, except for vertex inputs on desktop
1128 for (size_t r = 0; r < usedAtomics.size(); ++r) {
1129 if (range.overlap(usedAtomics[r])) {
1130 // there is a collision; pick one
1131 return std::max(offset, usedAtomics[r].offset.start);
1135 usedAtomics.push_back(range);
1137 return -1; // no collision
1140 // Accumulate used constant_id values.
1142 // Return false is one was already used.
1143 bool TIntermediate::addUsedConstantId(int id)
1145 if (usedConstantId.find(id) != usedConstantId.end())
1148 usedConstantId.insert(id);
1153 // Recursively figure out how many locations are used up by an input or output type.
1154 // Return the size of type, as measured by "locations".
1155 int TIntermediate::computeTypeLocationSize(const TType& type, EShLanguage stage)
1157 // "If the declared input is an array of size n and each element takes m locations, it will be assigned m * n
1158 // consecutive locations..."
1159 if (type.isArray()) {
1160 // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1161 // TODO: are there valid cases of having an unsized array with a location? If so, running this code too early.
1162 TType elementType(type, 0);
1163 if (type.isSizedArray()
1164 #ifdef NV_EXTENSIONS
1165 && !type.getQualifier().isPerView()
1168 return type.getOuterArraySize() * computeTypeLocationSize(elementType, stage);
1170 #ifdef NV_EXTENSIONS
1171 // unset perViewNV attributes for arrayed per-view outputs: "perviewNV vec4 v[MAX_VIEWS][3];"
1172 elementType.getQualifier().perViewNV = false;
1174 return computeTypeLocationSize(elementType, stage);
1178 // "The locations consumed by block and structure members are determined by applying the rules above
1180 if (type.isStruct()) {
1182 for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1183 TType memberType(type, member);
1184 size += computeTypeLocationSize(memberType, stage);
1189 // ES: "If a shader input is any scalar or vector type, it will consume a single location."
1191 // Desktop: "If a vertex shader input is any scalar or vector type, it will consume a single location. If a non-vertex
1192 // shader input is a scalar or vector type other than dvec3 or dvec4, it will consume a single location, while
1193 // types dvec3 or dvec4 will consume two consecutive locations. Inputs of type double and dvec2 will
1194 // consume only a single location, in all stages."
1195 if (type.isScalar())
1197 if (type.isVector()) {
1198 if (stage == EShLangVertex && type.getQualifier().isPipeInput())
1200 if (type.getBasicType() == EbtDouble && type.getVectorSize() > 2)
1206 // "If the declared input is an n x m single- or double-precision matrix, ...
1207 // The number of locations assigned for each matrix will be the same as
1208 // for an n-element array of m-component vectors..."
1209 if (type.isMatrix()) {
1210 TType columnType(type, 0);
1211 return type.getMatrixCols() * computeTypeLocationSize(columnType, stage);
1218 // Same as computeTypeLocationSize but for uniforms
1219 int TIntermediate::computeTypeUniformLocationSize(const TType& type)
1221 // "Individual elements of a uniform array are assigned
1222 // consecutive locations with the first element taking location
1224 if (type.isArray()) {
1225 // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1226 TType elementType(type, 0);
1227 if (type.isSizedArray()) {
1228 return type.getOuterArraySize() * computeTypeUniformLocationSize(elementType);
1230 // TODO: are there valid cases of having an implicitly-sized array with a location? If so, running this code too early.
1231 return computeTypeUniformLocationSize(elementType);
1235 // "Each subsequent inner-most member or element gets incremental
1236 // locations for the entire structure or array."
1237 if (type.isStruct()) {
1239 for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1240 TType memberType(type, member);
1241 size += computeTypeUniformLocationSize(memberType);
1249 // Accumulate xfb buffer ranges and check for collisions as the accumulation is done.
1251 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1253 int TIntermediate::addXfbBufferOffset(const TType& type)
1255 const TQualifier& qualifier = type.getQualifier();
1257 assert(qualifier.hasXfbOffset() && qualifier.hasXfbBuffer());
1258 TXfbBuffer& buffer = xfbBuffers[qualifier.layoutXfbBuffer];
1260 // compute the range
1261 unsigned int size = computeTypeXfbSize(type, buffer.containsDouble);
1262 buffer.implicitStride = std::max(buffer.implicitStride, qualifier.layoutXfbOffset + size);
1263 TRange range(qualifier.layoutXfbOffset, qualifier.layoutXfbOffset + size - 1);
1265 // check for collisions
1266 for (size_t r = 0; r < buffer.ranges.size(); ++r) {
1267 if (range.overlap(buffer.ranges[r])) {
1268 // there is a collision; pick an example to return
1269 return std::max(range.start, buffer.ranges[r].start);
1273 buffer.ranges.push_back(range);
1275 return -1; // no collision
1278 // Recursively figure out how many bytes of xfb buffer are used by the given type.
1279 // Return the size of type, in bytes.
1280 // Sets containsDouble to true if the type contains a double.
1281 // N.B. Caller must set containsDouble to false before calling.
1282 unsigned int TIntermediate::computeTypeXfbSize(const TType& type, bool& containsDouble) const
1284 // "...if applied to an aggregate containing a double, the offset must also be a multiple of 8,
1285 // and the space taken in the buffer will be a multiple of 8.
1286 // ...within the qualified entity, subsequent components are each
1287 // assigned, in order, to the next available offset aligned to a multiple of
1288 // that component's size. Aggregate types are flattened down to the component
1289 // level to get this sequence of components."
1291 if (type.isArray()) {
1292 // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1293 assert(type.isSizedArray());
1294 TType elementType(type, 0);
1295 return type.getOuterArraySize() * computeTypeXfbSize(elementType, containsDouble);
1298 if (type.isStruct()) {
1299 unsigned int size = 0;
1300 bool structContainsDouble = false;
1301 for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1302 TType memberType(type, member);
1303 // "... if applied to
1304 // an aggregate containing a double, the offset must also be a multiple of 8,
1305 // and the space taken in the buffer will be a multiple of 8."
1306 bool memberContainsDouble = false;
1307 int memberSize = computeTypeXfbSize(memberType, memberContainsDouble);
1308 if (memberContainsDouble) {
1309 structContainsDouble = true;
1310 RoundToPow2(size, 8);
1315 if (structContainsDouble) {
1316 containsDouble = true;
1317 RoundToPow2(size, 8);
1323 if (type.isScalar())
1325 else if (type.isVector())
1326 numComponents = type.getVectorSize();
1327 else if (type.isMatrix())
1328 numComponents = type.getMatrixCols() * type.getMatrixRows();
1334 if (type.getBasicType() == EbtDouble) {
1335 containsDouble = true;
1336 return 8 * numComponents;
1338 return 4 * numComponents;
1341 const int baseAlignmentVec4Std140 = 16;
1343 // Return the size and alignment of a component of the given type.
1344 // The size is returned in the 'size' parameter
1345 // Return value is the alignment..
1346 int TIntermediate::getBaseAlignmentScalar(const TType& type, int& size)
1348 switch (type.getBasicType()) {
1351 case EbtDouble: size = 8; return 8;
1352 case EbtFloat16: size = 2; return 2;
1354 case EbtUint8: size = 1; return 1;
1356 case EbtUint16: size = 2; return 2;
1357 default: size = 4; return 4;
1361 // Implement base-alignment and size rules from section 7.6.2.2 Standard Uniform Block Layout
1362 // Operates recursively.
1364 // If std140 is true, it does the rounding up to vec4 size required by std140,
1365 // otherwise it does not, yielding std430 rules.
1367 // The size is returned in the 'size' parameter
1369 // The stride is only non-0 for arrays or matrices, and is the stride of the
1370 // top-level object nested within the type. E.g., for an array of matrices,
1371 // it is the distances needed between matrices, despite the rules saying the
1372 // stride comes from the flattening down to vectors.
1374 // Return value is the alignment of the type.
1375 int TIntermediate::getBaseAlignment(const TType& type, int& size, int& stride, bool std140, bool rowMajor)
1379 // When using the std140 storage layout, structures will be laid out in buffer
1380 // storage with its members stored in monotonically increasing order based on their
1381 // location in the declaration. A structure and each structure member have a base
1382 // offset and a base alignment, from which an aligned offset is computed by rounding
1383 // the base offset up to a multiple of the base alignment. The base offset of the first
1384 // member of a structure is taken from the aligned offset of the structure itself. The
1385 // base offset of all other structure members is derived by taking the offset of the
1386 // last basic machine unit consumed by the previous member and adding one. Each
1387 // structure member is stored in memory at its aligned offset. The members of a top-
1388 // level uniform block are laid out in buffer storage by treating the uniform block as
1389 // a structure with a base offset of zero.
1391 // 1. If the member is a scalar consuming N basic machine units, the base alignment is N.
1393 // 2. If the member is a two- or four-component vector with components consuming N basic
1394 // machine units, the base alignment is 2N or 4N, respectively.
1396 // 3. If the member is a three-component vector with components consuming N
1397 // basic machine units, the base alignment is 4N.
1399 // 4. If the member is an array of scalars or vectors, the base alignment and array
1400 // stride are set to match the base alignment of a single array element, according
1401 // to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The
1402 // array may have padding at the end; the base offset of the member following
1403 // the array is rounded up to the next multiple of the base alignment.
1405 // 5. If the member is a column-major matrix with C columns and R rows, the
1406 // matrix is stored identically to an array of C column vectors with R
1407 // components each, according to rule (4).
1409 // 6. If the member is an array of S column-major matrices with C columns and
1410 // R rows, the matrix is stored identically to a row of S X C column vectors
1411 // with R components each, according to rule (4).
1413 // 7. If the member is a row-major matrix with C columns and R rows, the matrix
1414 // is stored identically to an array of R row vectors with C components each,
1415 // according to rule (4).
1417 // 8. If the member is an array of S row-major matrices with C columns and R
1418 // rows, the matrix is stored identically to a row of S X R row vectors with C
1419 // components each, according to rule (4).
1421 // 9. If the member is a structure, the base alignment of the structure is N , where
1422 // N is the largest base alignment value of any of its members, and rounded
1423 // up to the base alignment of a vec4. The individual members of this substructure
1424 // are then assigned offsets by applying this set of rules recursively,
1425 // where the base offset of the first member of the sub-structure is equal to the
1426 // aligned offset of the structure. The structure may have padding at the end;
1427 // the base offset of the member following the sub-structure is rounded up to
1428 // the next multiple of the base alignment of the structure.
1430 // 10. If the member is an array of S structures, the S elements of the array are laid
1431 // out in order, according to rule (9).
1433 // Assuming, for rule 10: The stride is the same as the size of an element.
1438 // rules 4, 6, 8, and 10
1439 if (type.isArray()) {
1440 // TODO: perf: this might be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1441 TType derefType(type, 0);
1442 alignment = getBaseAlignment(derefType, size, dummyStride, std140, rowMajor);
1444 alignment = std::max(baseAlignmentVec4Std140, alignment);
1445 RoundToPow2(size, alignment);
1446 stride = size; // uses full matrix size for stride of an array of matrices (not quite what rule 6/8, but what's expected)
1447 // uses the assumption for rule 10 in the comment above
1448 size = stride * type.getOuterArraySize();
1453 if (type.getBasicType() == EbtStruct) {
1454 const TTypeList& memberList = *type.getStruct();
1457 int maxAlignment = std140 ? baseAlignmentVec4Std140 : 0;
1458 for (size_t m = 0; m < memberList.size(); ++m) {
1460 // modify just the children's view of matrix layout, if there is one for this member
1461 TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
1462 int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, dummyStride, std140,
1463 (subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
1464 maxAlignment = std::max(maxAlignment, memberAlignment);
1465 RoundToPow2(size, memberAlignment);
1469 // The structure may have padding at the end; the base offset of
1470 // the member following the sub-structure is rounded up to the next
1471 // multiple of the base alignment of the structure.
1472 RoundToPow2(size, maxAlignment);
1474 return maxAlignment;
1478 if (type.isScalar())
1479 return getBaseAlignmentScalar(type, size);
1482 if (type.isVector()) {
1483 int scalarAlign = getBaseAlignmentScalar(type, size);
1484 switch (type.getVectorSize()) {
1485 case 1: // HLSL has this, GLSL does not
1489 return 2 * scalarAlign;
1491 size *= type.getVectorSize();
1492 return 4 * scalarAlign;
1497 if (type.isMatrix()) {
1498 // rule 5: deref to row, not to column, meaning the size of vector is num columns instead of num rows
1499 TType derefType(type, 0, rowMajor);
1501 alignment = getBaseAlignment(derefType, size, dummyStride, std140, rowMajor);
1503 alignment = std::max(baseAlignmentVec4Std140, alignment);
1504 RoundToPow2(size, alignment);
1505 stride = size; // use intra-matrix stride for stride of a just a matrix
1507 size = stride * type.getMatrixRows();
1509 size = stride * type.getMatrixCols();
1514 assert(0); // all cases should be covered above
1515 size = baseAlignmentVec4Std140;
1516 return baseAlignmentVec4Std140;
1519 // To aid the basic HLSL rule about crossing vec4 boundaries.
1520 bool TIntermediate::improperStraddle(const TType& type, int size, int offset)
1522 if (! type.isVector() || type.isArray())
1525 return size <= 16 ? offset / 16 != (offset + size - 1) / 16
1529 } // end namespace glslang