1 //
2 // Copyright (C) 2013 LunarG, Inc.
3 // Copyright (C) 2017 ARM Limited.
4 // Copyright (C) 2015-2018 Google, Inc.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 //    Redistributions of source code must retain the above copyright
13 //    notice, this list of conditions and the following disclaimer.
14 //
15 //    Redistributions in binary form must reproduce the above
16 //    copyright notice, this list of conditions and the following
17 //    disclaimer in the documentation and/or other materials provided
18 //    with the distribution.
19 //
20 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 //    contributors may be used to endorse or promote products derived
22 //    from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 
38 //
39 // Do link-time merging and validation of intermediate representations.
40 //
41 // Basic model is that during compilation, each compilation unit (shader) is
42 // compiled into one TIntermediate instance.  Then, at link time, multiple
43 // units for the same stage can be merged together, which can generate errors.
44 // Then, after all merging, a single instance of TIntermediate represents
45 // the whole stage.  A final error check can be done on the resulting stage,
46 // even if no merging was done (i.e., the stage was only one compilation unit).
47 //
48 
49 #include "localintermediate.h"
50 #include "../Include/InfoSink.h"
51 
52 namespace glslang {
53 
54 //
55 // Link-time error emitter.
56 //
error(TInfoSink & infoSink,const char * message)57 void TIntermediate::error(TInfoSink& infoSink, const char* message)
58 {
59 #ifndef GLSLANG_WEB
60     infoSink.info.prefix(EPrefixError);
61     infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
62 #endif
63 
64     ++numErrors;
65 }
66 
67 // Link-time warning.
warn(TInfoSink & infoSink,const char * message)68 void TIntermediate::warn(TInfoSink& infoSink, const char* message)
69 {
70 #ifndef GLSLANG_WEB
71     infoSink.info.prefix(EPrefixWarning);
72     infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
73 #endif
74 }
75 
76 // TODO: 4.4 offset/align:  "Two blocks linked together in the same program with the same block
77 // name must have the exact same set of members qualified with offset and their integral-constant
78 // expression values must be the same, or a link-time error results."
79 
80 //
81 // Merge the information from 'unit' into 'this'
82 //
merge(TInfoSink & infoSink,TIntermediate & unit)83 void TIntermediate::merge(TInfoSink& infoSink, TIntermediate& unit)
84 {
85 #ifndef GLSLANG_WEB
86     mergeCallGraphs(infoSink, unit);
87     mergeModes(infoSink, unit);
88     mergeTrees(infoSink, unit);
89 #endif
90 }
91 
mergeCallGraphs(TInfoSink & infoSink,TIntermediate & unit)92 void TIntermediate::mergeCallGraphs(TInfoSink& infoSink, TIntermediate& unit)
93 {
94     if (unit.getNumEntryPoints() > 0) {
95         if (getNumEntryPoints() > 0)
96             error(infoSink, "can't handle multiple entry points per stage");
97         else {
98             entryPointName = unit.getEntryPointName();
99             entryPointMangledName = unit.getEntryPointMangledName();
100         }
101     }
102     numEntryPoints += unit.getNumEntryPoints();
103 
104     callGraph.insert(callGraph.end(), unit.callGraph.begin(), unit.callGraph.end());
105 }
106 
107 #ifndef GLSLANG_WEB
108 
109 #define MERGE_MAX(member) member = std::max(member, unit.member)
110 #define MERGE_TRUE(member) if (unit.member) member = unit.member;
111 
mergeModes(TInfoSink & infoSink,TIntermediate & unit)112 void TIntermediate::mergeModes(TInfoSink& infoSink, TIntermediate& unit)
113 {
114     if (language != unit.language)
115         error(infoSink, "stages must match when linking into a single stage");
116 
117     if (getSource() == EShSourceNone)
118         setSource(unit.getSource());
119     if (getSource() != unit.getSource())
120         error(infoSink, "can't link compilation units from different source languages");
121 
122     if (treeRoot == nullptr) {
123         profile = unit.profile;
124         version = unit.version;
125         requestedExtensions = unit.requestedExtensions;
126     } else {
127         if ((isEsProfile()) != (unit.isEsProfile()))
128             error(infoSink, "Cannot cross link ES and desktop profiles");
129         else if (unit.profile == ECompatibilityProfile)
130             profile = ECompatibilityProfile;
131         version = std::max(version, unit.version);
132         requestedExtensions.insert(unit.requestedExtensions.begin(), unit.requestedExtensions.end());
133     }
134 
135     MERGE_MAX(spvVersion.spv);
136     MERGE_MAX(spvVersion.vulkanGlsl);
137     MERGE_MAX(spvVersion.vulkan);
138     MERGE_MAX(spvVersion.openGl);
139 
140     numErrors += unit.getNumErrors();
141     numPushConstants += unit.numPushConstants;
142 
143     if (unit.invocations != TQualifier::layoutNotSet) {
144         if (invocations == TQualifier::layoutNotSet)
145             invocations = unit.invocations;
146         else if (invocations != unit.invocations)
147             error(infoSink, "number of invocations must match between compilation units");
148     }
149 
150     if (vertices == TQualifier::layoutNotSet)
151         vertices = unit.vertices;
152     else if (vertices != unit.vertices) {
153         if (language == EShLangGeometry || language == EShLangMeshNV)
154             error(infoSink, "Contradictory layout max_vertices values");
155         else if (language == EShLangTessControl)
156             error(infoSink, "Contradictory layout vertices values");
157         else
158             assert(0);
159     }
160     if (primitives == TQualifier::layoutNotSet)
161         primitives = unit.primitives;
162     else if (primitives != unit.primitives) {
163         if (language == EShLangMeshNV)
164             error(infoSink, "Contradictory layout max_primitives values");
165         else
166             assert(0);
167     }
168 
169     if (inputPrimitive == ElgNone)
170         inputPrimitive = unit.inputPrimitive;
171     else if (inputPrimitive != unit.inputPrimitive)
172         error(infoSink, "Contradictory input layout primitives");
173 
174     if (outputPrimitive == ElgNone)
175         outputPrimitive = unit.outputPrimitive;
176     else if (outputPrimitive != unit.outputPrimitive)
177         error(infoSink, "Contradictory output layout primitives");
178 
179     if (originUpperLeft != unit.originUpperLeft || pixelCenterInteger != unit.pixelCenterInteger)
180         error(infoSink, "gl_FragCoord redeclarations must match across shaders");
181 
182     if (vertexSpacing == EvsNone)
183         vertexSpacing = unit.vertexSpacing;
184     else if (vertexSpacing != unit.vertexSpacing)
185         error(infoSink, "Contradictory input vertex spacing");
186 
187     if (vertexOrder == EvoNone)
188         vertexOrder = unit.vertexOrder;
189     else if (vertexOrder != unit.vertexOrder)
190         error(infoSink, "Contradictory triangle ordering");
191 
192     MERGE_TRUE(pointMode);
193 
194     for (int i = 0; i < 3; ++i) {
195         if (localSize[i] > 1)
196             localSize[i] = unit.localSize[i];
197         else if (localSize[i] != unit.localSize[i])
198             error(infoSink, "Contradictory local size");
199 
200         if (localSizeSpecId[i] != TQualifier::layoutNotSet)
201             localSizeSpecId[i] = unit.localSizeSpecId[i];
202         else if (localSizeSpecId[i] != unit.localSizeSpecId[i])
203             error(infoSink, "Contradictory local size specialization ids");
204     }
205 
206     MERGE_TRUE(earlyFragmentTests);
207     MERGE_TRUE(postDepthCoverage);
208 
209     if (depthLayout == EldNone)
210         depthLayout = unit.depthLayout;
211     else if (depthLayout != unit.depthLayout)
212         error(infoSink, "Contradictory depth layouts");
213 
214     MERGE_TRUE(depthReplacing);
215     MERGE_TRUE(hlslFunctionality1);
216 
217     blendEquations |= unit.blendEquations;
218 
219     MERGE_TRUE(xfbMode);
220 
221     for (size_t b = 0; b < xfbBuffers.size(); ++b) {
222         if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
223             xfbBuffers[b].stride = unit.xfbBuffers[b].stride;
224         else if (xfbBuffers[b].stride != unit.xfbBuffers[b].stride)
225             error(infoSink, "Contradictory xfb_stride");
226         xfbBuffers[b].implicitStride = std::max(xfbBuffers[b].implicitStride, unit.xfbBuffers[b].implicitStride);
227         if (unit.xfbBuffers[b].contains64BitType)
228             xfbBuffers[b].contains64BitType = true;
229         if (unit.xfbBuffers[b].contains32BitType)
230             xfbBuffers[b].contains32BitType = true;
231         if (unit.xfbBuffers[b].contains16BitType)
232             xfbBuffers[b].contains16BitType = true;
233         // TODO: 4.4 link: enhanced layouts: compare ranges
234     }
235 
236     MERGE_TRUE(multiStream);
237     MERGE_TRUE(layoutOverrideCoverage);
238     MERGE_TRUE(geoPassthroughEXT);
239 
240     for (unsigned int i = 0; i < unit.shiftBinding.size(); ++i) {
241         if (unit.shiftBinding[i] > 0)
242             setShiftBinding((TResourceType)i, unit.shiftBinding[i]);
243     }
244 
245     for (unsigned int i = 0; i < unit.shiftBindingForSet.size(); ++i) {
246         for (auto it = unit.shiftBindingForSet[i].begin(); it != unit.shiftBindingForSet[i].end(); ++it)
247             setShiftBindingForSet((TResourceType)i, it->second, it->first);
248     }
249 
250     resourceSetBinding.insert(resourceSetBinding.end(), unit.resourceSetBinding.begin(), unit.resourceSetBinding.end());
251 
252     MERGE_TRUE(autoMapBindings);
253     MERGE_TRUE(autoMapLocations);
254     MERGE_TRUE(invertY);
255     MERGE_TRUE(flattenUniformArrays);
256     MERGE_TRUE(useUnknownFormat);
257     MERGE_TRUE(hlslOffsets);
258     MERGE_TRUE(useStorageBuffer);
259     MERGE_TRUE(hlslIoMapping);
260 
261     // TODO: sourceFile
262     // TODO: sourceText
263     // TODO: processes
264 
265     MERGE_TRUE(needToLegalize);
266     MERGE_TRUE(binaryDoubleOutput);
267     MERGE_TRUE(usePhysicalStorageBuffer);
268 }
269 
270 //
271 // Merge the 'unit' AST into 'this' AST.
272 // That includes rationalizing the unique IDs, which were set up independently,
273 // and might have overlaps that are not the same symbol, or might have different
274 // IDs for what should be the same shared symbol.
275 //
mergeTrees(TInfoSink & infoSink,TIntermediate & unit)276 void TIntermediate::mergeTrees(TInfoSink& infoSink, TIntermediate& unit)
277 {
278     if (unit.treeRoot == nullptr)
279         return;
280 
281     if (treeRoot == nullptr) {
282         treeRoot = unit.treeRoot;
283         return;
284     }
285 
286     // Getting this far means we have two existing trees to merge...
287     numShaderRecordNVBlocks += unit.numShaderRecordNVBlocks;
288     numTaskNVBlocks += unit.numTaskNVBlocks;
289 
290     // Get the top-level globals of each unit
291     TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
292     TIntermSequence& unitGlobals = unit.treeRoot->getAsAggregate()->getSequence();
293 
294     // Get the linker-object lists
295     TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
296     const TIntermSequence& unitLinkerObjects = unit.findLinkerObjects()->getSequence();
297 
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;
301     int maxId;
302     seedIdMap(idMap, maxId);
303     remapIds(idMap, maxId + 1, unit);
304 
305     mergeBodies(infoSink, globals, unitGlobals);
306     mergeLinkerObjects(infoSink, linkerObjects, unitLinkerObjects);
307     ioAccessed.insert(unit.ioAccessed.begin(), unit.ioAccessed.end());
308 }
309 
310 #endif
311 
312 // Traverser that seeds an ID map with all built-ins, and tracks the
313 // maximum ID used.
314 // (It would be nice to put this in a function, but that causes warnings
315 // on having no bodies for the copy-constructor/operator=.)
316 class TBuiltInIdTraverser : public TIntermTraverser {
317 public:
TBuiltInIdTraverser(TMap<TString,int> & idMap)318     TBuiltInIdTraverser(TMap<TString, int>& idMap) : idMap(idMap), maxId(0) { }
319     // If it's a built in, add it to the map.
320     // Track the max ID.
visitSymbol(TIntermSymbol * symbol)321     virtual void visitSymbol(TIntermSymbol* symbol)
322     {
323         const TQualifier& qualifier = symbol->getType().getQualifier();
324         if (qualifier.builtIn != EbvNone)
325             idMap[symbol->getName()] = symbol->getId();
326         maxId = std::max(maxId, symbol->getId());
327     }
getMaxId() const328     int getMaxId() const { return maxId; }
329 protected:
330     TBuiltInIdTraverser(TBuiltInIdTraverser&);
331     TBuiltInIdTraverser& operator=(TBuiltInIdTraverser&);
332     TMap<TString, int>& idMap;
333     int maxId;
334 };
335 
336 // Traverser that seeds an ID map with non-builtins.
337 // (It would be nice to put this in a function, but that causes warnings
338 // on having no bodies for the copy-constructor/operator=.)
339 class TUserIdTraverser : public TIntermTraverser {
340 public:
TUserIdTraverser(TMap<TString,int> & idMap)341     TUserIdTraverser(TMap<TString, int>& idMap) : idMap(idMap) { }
342     // If its a non-built-in global, add it to the map.
visitSymbol(TIntermSymbol * symbol)343     virtual void visitSymbol(TIntermSymbol* symbol)
344     {
345         const TQualifier& qualifier = symbol->getType().getQualifier();
346         if (qualifier.builtIn == EbvNone)
347             idMap[symbol->getName()] = symbol->getId();
348     }
349 
350 protected:
351     TUserIdTraverser(TUserIdTraverser&);
352     TUserIdTraverser& operator=(TUserIdTraverser&);
353     TMap<TString, int>& idMap; // over biggest id
354 };
355 
356 // Initialize the the ID map with what we know of 'this' AST.
seedIdMap(TMap<TString,int> & idMap,int & maxId)357 void TIntermediate::seedIdMap(TMap<TString, int>& idMap, int& maxId)
358 {
359     // all built-ins everywhere need to align on IDs and contribute to the max ID
360     TBuiltInIdTraverser builtInIdTraverser(idMap);
361     treeRoot->traverse(&builtInIdTraverser);
362     maxId = builtInIdTraverser.getMaxId();
363 
364     // user variables in the linker object list need to align on ids
365     TUserIdTraverser userIdTraverser(idMap);
366     findLinkerObjects()->traverse(&userIdTraverser);
367 }
368 
369 // Traverser to map an AST ID to what was known from the seeding AST.
370 // (It would be nice to put this in a function, but that causes warnings
371 // on having no bodies for the copy-constructor/operator=.)
372 class TRemapIdTraverser : public TIntermTraverser {
373 public:
TRemapIdTraverser(const TMap<TString,int> & idMap,int idShift)374     TRemapIdTraverser(const TMap<TString, int>& idMap, int idShift) : idMap(idMap), idShift(idShift) { }
375     // Do the mapping:
376     //  - if the same symbol, adopt the 'this' ID
377     //  - otherwise, ensure a unique ID by shifting to a new space
visitSymbol(TIntermSymbol * symbol)378     virtual void visitSymbol(TIntermSymbol* symbol)
379     {
380         const TQualifier& qualifier = symbol->getType().getQualifier();
381         bool remapped = false;
382         if (qualifier.isLinkable() || qualifier.builtIn != EbvNone) {
383             auto it = idMap.find(symbol->getName());
384             if (it != idMap.end()) {
385                 symbol->changeId(it->second);
386                 remapped = true;
387             }
388         }
389         if (!remapped)
390             symbol->changeId(symbol->getId() + idShift);
391     }
392 protected:
393     TRemapIdTraverser(TRemapIdTraverser&);
394     TRemapIdTraverser& operator=(TRemapIdTraverser&);
395     const TMap<TString, int>& idMap;
396     int idShift;
397 };
398 
remapIds(const TMap<TString,int> & idMap,int idShift,TIntermediate & unit)399 void TIntermediate::remapIds(const TMap<TString, int>& idMap, int idShift, TIntermediate& unit)
400 {
401     // Remap all IDs to either share or be unique, as dictated by the idMap and idShift.
402     TRemapIdTraverser idTraverser(idMap, idShift);
403     unit.getTreeRoot()->traverse(&idTraverser);
404 }
405 
406 //
407 // Merge the function bodies and global-level initializers from unitGlobals into globals.
408 // Will error check duplication of function bodies for the same signature.
409 //
mergeBodies(TInfoSink & infoSink,TIntermSequence & globals,const TIntermSequence & unitGlobals)410 void TIntermediate::mergeBodies(TInfoSink& infoSink, TIntermSequence& globals, const TIntermSequence& unitGlobals)
411 {
412     // TODO: link-time performance: Processing in alphabetical order will be faster
413 
414     // Error check the global objects, not including the linker objects
415     for (unsigned int child = 0; child < globals.size() - 1; ++child) {
416         for (unsigned int unitChild = 0; unitChild < unitGlobals.size() - 1; ++unitChild) {
417             TIntermAggregate* body = globals[child]->getAsAggregate();
418             TIntermAggregate* unitBody = unitGlobals[unitChild]->getAsAggregate();
419             if (body && unitBody && body->getOp() == EOpFunction && unitBody->getOp() == EOpFunction && body->getName() == unitBody->getName()) {
420                 error(infoSink, "Multiple function bodies in multiple compilation units for the same signature in the same stage:");
421                 infoSink.info << "    " << globals[child]->getAsAggregate()->getName() << "\n";
422             }
423         }
424     }
425 
426     // Merge the global objects, just in front of the linker objects
427     globals.insert(globals.end() - 1, unitGlobals.begin(), unitGlobals.end() - 1);
428 }
429 
430 //
431 // Merge the linker objects from unitLinkerObjects into linkerObjects.
432 // Duplication is expected and filtered out, but contradictions are an error.
433 //
mergeLinkerObjects(TInfoSink & infoSink,TIntermSequence & linkerObjects,const TIntermSequence & unitLinkerObjects)434 void TIntermediate::mergeLinkerObjects(TInfoSink& infoSink, TIntermSequence& linkerObjects, const TIntermSequence& unitLinkerObjects)
435 {
436     // Error check and merge the linker objects (duplicates should not be created)
437     std::size_t initialNumLinkerObjects = linkerObjects.size();
438     for (unsigned int unitLinkObj = 0; unitLinkObj < unitLinkerObjects.size(); ++unitLinkObj) {
439         bool merge = true;
440         for (std::size_t linkObj = 0; linkObj < initialNumLinkerObjects; ++linkObj) {
441             TIntermSymbol* symbol = linkerObjects[linkObj]->getAsSymbolNode();
442             TIntermSymbol* unitSymbol = unitLinkerObjects[unitLinkObj]->getAsSymbolNode();
443             assert(symbol && unitSymbol);
444             if (symbol->getName() == unitSymbol->getName()) {
445                 // filter out copy
446                 merge = false;
447 
448                 // but if one has an initializer and the other does not, update
449                 // the initializer
450                 if (symbol->getConstArray().empty() && ! unitSymbol->getConstArray().empty())
451                     symbol->setConstArray(unitSymbol->getConstArray());
452 
453                 // Similarly for binding
454                 if (! symbol->getQualifier().hasBinding() && unitSymbol->getQualifier().hasBinding())
455                     symbol->getQualifier().layoutBinding = unitSymbol->getQualifier().layoutBinding;
456 
457                 // Update implicit array sizes
458                 mergeImplicitArraySizes(symbol->getWritableType(), unitSymbol->getType());
459 
460                 // Check for consistent types/qualification/initializers etc.
461                 mergeErrorCheck(infoSink, *symbol, *unitSymbol, false);
462             }
463         }
464         if (merge)
465             linkerObjects.push_back(unitLinkerObjects[unitLinkObj]);
466     }
467 }
468 
469 // TODO 4.5 link functionality: cull distance array size checking
470 
471 // Recursively merge the implicit array sizes through the objects' respective type trees.
mergeImplicitArraySizes(TType & type,const TType & unitType)472 void TIntermediate::mergeImplicitArraySizes(TType& type, const TType& unitType)
473 {
474     if (type.isUnsizedArray()) {
475         if (unitType.isUnsizedArray()) {
476             type.updateImplicitArraySize(unitType.getImplicitArraySize());
477             if (unitType.isArrayVariablyIndexed())
478                 type.setArrayVariablyIndexed();
479         } else if (unitType.isSizedArray())
480             type.changeOuterArraySize(unitType.getOuterArraySize());
481     }
482 
483     // Type mismatches are caught and reported after this, just be careful for now.
484     if (! type.isStruct() || ! unitType.isStruct() || type.getStruct()->size() != unitType.getStruct()->size())
485         return;
486 
487     for (int i = 0; i < (int)type.getStruct()->size(); ++i)
488         mergeImplicitArraySizes(*(*type.getStruct())[i].type, *(*unitType.getStruct())[i].type);
489 }
490 
491 //
492 // Compare two global objects from two compilation units and see if they match
493 // well enough.  Rules can be different for intra- vs. cross-stage matching.
494 //
495 // This function only does one of intra- or cross-stage matching per call.
496 //
mergeErrorCheck(TInfoSink & infoSink,const TIntermSymbol & symbol,const TIntermSymbol & unitSymbol,bool crossStage)497 void TIntermediate::mergeErrorCheck(TInfoSink& infoSink, const TIntermSymbol& symbol, const TIntermSymbol& unitSymbol, bool crossStage)
498 {
499 #ifndef GLSLANG_WEB
500     bool writeTypeComparison = false;
501 
502     // Types have to match
503     if (symbol.getType() != unitSymbol.getType()) {
504         // but, we make an exception if one is an implicit array and the other is sized
505         if (! (symbol.getType().isArray() && unitSymbol.getType().isArray() &&
506                 symbol.getType().sameElementType(unitSymbol.getType()) &&
507                 (symbol.getType().isUnsizedArray() || unitSymbol.getType().isUnsizedArray()))) {
508             error(infoSink, "Types must match:");
509             writeTypeComparison = true;
510         }
511     }
512 
513     // Qualifiers have to (almost) match
514 
515     // Storage...
516     if (symbol.getQualifier().storage != unitSymbol.getQualifier().storage) {
517         error(infoSink, "Storage qualifiers must match:");
518         writeTypeComparison = true;
519     }
520 
521     // Precision...
522     if (symbol.getQualifier().precision != unitSymbol.getQualifier().precision) {
523         error(infoSink, "Precision qualifiers must match:");
524         writeTypeComparison = true;
525     }
526 
527     // Invariance...
528     if (! crossStage && symbol.getQualifier().invariant != unitSymbol.getQualifier().invariant) {
529         error(infoSink, "Presence of invariant qualifier must match:");
530         writeTypeComparison = true;
531     }
532 
533     // Precise...
534     if (! crossStage && symbol.getQualifier().isNoContraction() != unitSymbol.getQualifier().isNoContraction()) {
535         error(infoSink, "Presence of precise qualifier must match:");
536         writeTypeComparison = true;
537     }
538 
539     // Auxiliary and interpolation...
540     if (symbol.getQualifier().centroid  != unitSymbol.getQualifier().centroid ||
541         symbol.getQualifier().smooth    != unitSymbol.getQualifier().smooth ||
542         symbol.getQualifier().flat      != unitSymbol.getQualifier().flat ||
543         symbol.getQualifier().isSample()!= unitSymbol.getQualifier().isSample() ||
544         symbol.getQualifier().isPatch() != unitSymbol.getQualifier().isPatch() ||
545         symbol.getQualifier().isNonPerspective() != unitSymbol.getQualifier().isNonPerspective()) {
546         error(infoSink, "Interpolation and auxiliary storage qualifiers must match:");
547         writeTypeComparison = true;
548     }
549 
550     // Memory...
551     if (symbol.getQualifier().coherent          != unitSymbol.getQualifier().coherent ||
552         symbol.getQualifier().devicecoherent    != unitSymbol.getQualifier().devicecoherent ||
553         symbol.getQualifier().queuefamilycoherent  != unitSymbol.getQualifier().queuefamilycoherent ||
554         symbol.getQualifier().workgroupcoherent != unitSymbol.getQualifier().workgroupcoherent ||
555         symbol.getQualifier().subgroupcoherent  != unitSymbol.getQualifier().subgroupcoherent ||
556         symbol.getQualifier().nonprivate        != unitSymbol.getQualifier().nonprivate ||
557         symbol.getQualifier().volatil           != unitSymbol.getQualifier().volatil ||
558         symbol.getQualifier().restrict          != unitSymbol.getQualifier().restrict ||
559         symbol.getQualifier().readonly          != unitSymbol.getQualifier().readonly ||
560         symbol.getQualifier().writeonly         != unitSymbol.getQualifier().writeonly) {
561         error(infoSink, "Memory qualifiers must match:");
562         writeTypeComparison = true;
563     }
564 
565     // Layouts...
566     // TODO: 4.4 enhanced layouts: Generalize to include offset/align: current spec
567     //       requires separate user-supplied offset from actual computed offset, but
568     //       current implementation only has one offset.
569     if (symbol.getQualifier().layoutMatrix    != unitSymbol.getQualifier().layoutMatrix ||
570         symbol.getQualifier().layoutPacking   != unitSymbol.getQualifier().layoutPacking ||
571         symbol.getQualifier().layoutLocation  != unitSymbol.getQualifier().layoutLocation ||
572         symbol.getQualifier().layoutComponent != unitSymbol.getQualifier().layoutComponent ||
573         symbol.getQualifier().layoutIndex     != unitSymbol.getQualifier().layoutIndex ||
574         symbol.getQualifier().layoutBinding   != unitSymbol.getQualifier().layoutBinding ||
575         (symbol.getQualifier().hasBinding() && (symbol.getQualifier().layoutOffset != unitSymbol.getQualifier().layoutOffset))) {
576         error(infoSink, "Layout qualification must match:");
577         writeTypeComparison = true;
578     }
579 
580     // Initializers have to match, if both are present, and if we don't already know the types don't match
581     if (! writeTypeComparison) {
582         if (! symbol.getConstArray().empty() && ! unitSymbol.getConstArray().empty()) {
583             if (symbol.getConstArray() != unitSymbol.getConstArray()) {
584                 error(infoSink, "Initializers must match:");
585                 infoSink.info << "    " << symbol.getName() << "\n";
586             }
587         }
588     }
589 
590     if (writeTypeComparison)
591         infoSink.info << "    " << symbol.getName() << ": \"" << symbol.getType().getCompleteString() << "\" versus \"" <<
592                                                              unitSymbol.getType().getCompleteString() << "\"\n";
593 #endif
594 }
595 
596 //
597 // Do final link-time error checking of a complete (merged) intermediate representation.
598 // (Much error checking was done during merging).
599 //
600 // Also, lock in defaults of things not set, including array sizes.
601 //
finalCheck(TInfoSink & infoSink,bool keepUncalled)602 void TIntermediate::finalCheck(TInfoSink& infoSink, bool keepUncalled)
603 {
604     if (getTreeRoot() == nullptr)
605         return;
606 
607     if (numEntryPoints < 1) {
608         if (getSource() == EShSourceGlsl)
609             error(infoSink, "Missing entry point: Each stage requires one entry point");
610         else
611             warn(infoSink, "Entry point not found");
612     }
613 
614     // recursion and missing body checking
615     checkCallGraphCycles(infoSink);
616     checkCallGraphBodies(infoSink, keepUncalled);
617 
618     // overlap/alias/missing I/O, etc.
619     inOutLocationCheck(infoSink);
620 
621 #ifndef GLSLANG_WEB
622     if (getNumPushConstants() > 1)
623         error(infoSink, "Only one push_constant block is allowed per stage");
624 
625     // invocations
626     if (invocations == TQualifier::layoutNotSet)
627         invocations = 1;
628 
629     if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipVertex"))
630         error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
631     if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_ClipVertex"))
632         error(infoSink, "Can only use one of gl_CullDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
633 
634     if (userOutputUsed() && (inIoAccessed("gl_FragColor") || inIoAccessed("gl_FragData")))
635         error(infoSink, "Cannot use gl_FragColor or gl_FragData when using user-defined outputs");
636     if (inIoAccessed("gl_FragColor") && inIoAccessed("gl_FragData"))
637         error(infoSink, "Cannot use both gl_FragColor and gl_FragData");
638 
639     for (size_t b = 0; b < xfbBuffers.size(); ++b) {
640         if (xfbBuffers[b].contains64BitType)
641             RoundToPow2(xfbBuffers[b].implicitStride, 8);
642         else if (xfbBuffers[b].contains32BitType)
643             RoundToPow2(xfbBuffers[b].implicitStride, 4);
644         else if (xfbBuffers[b].contains16BitType)
645             RoundToPow2(xfbBuffers[b].implicitStride, 2);
646 
647         // "It is a compile-time or link-time error to have
648         // any xfb_offset that overflows xfb_stride, whether stated on declarations before or after the xfb_stride, or
649         // in different compilation units. While xfb_stride can be declared multiple times for the same buffer, it is a
650         // compile-time or link-time error to have different values specified for the stride for the same buffer."
651         if (xfbBuffers[b].stride != TQualifier::layoutXfbStrideEnd && xfbBuffers[b].implicitStride > xfbBuffers[b].stride) {
652             error(infoSink, "xfb_stride is too small to hold all buffer entries:");
653             infoSink.info.prefix(EPrefixError);
654             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << ", minimum stride needed: " << xfbBuffers[b].implicitStride << "\n";
655         }
656         if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
657             xfbBuffers[b].stride = xfbBuffers[b].implicitStride;
658 
659         // "If the buffer is capturing any
660         // outputs with double-precision or 64-bit integer components, the stride must be a multiple of 8, otherwise it must be a
661         // multiple of 4, or a compile-time or link-time error results."
662         if (xfbBuffers[b].contains64BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 8)) {
663             error(infoSink, "xfb_stride must be multiple of 8 for buffer holding a double or 64-bit integer:");
664             infoSink.info.prefix(EPrefixError);
665             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
666         } else if (xfbBuffers[b].contains32BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 4)) {
667             error(infoSink, "xfb_stride must be multiple of 4:");
668             infoSink.info.prefix(EPrefixError);
669             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
670         }
671         // "If the buffer is capturing any
672         // outputs with half-precision or 16-bit integer components, the stride must be a multiple of 2"
673         else if (xfbBuffers[b].contains16BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 2)) {
674             error(infoSink, "xfb_stride must be multiple of 2 for buffer holding a half float or 16-bit integer:");
675             infoSink.info.prefix(EPrefixError);
676             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
677         }
678 
679         // "The resulting stride (implicit or explicit), when divided by 4, must be less than or equal to the
680         // implementation-dependent constant gl_MaxTransformFeedbackInterleavedComponents."
681         if (xfbBuffers[b].stride > (unsigned int)(4 * resources.maxTransformFeedbackInterleavedComponents)) {
682             error(infoSink, "xfb_stride is too large:");
683             infoSink.info.prefix(EPrefixError);
684             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", components (1/4 stride) needed are " << xfbBuffers[b].stride/4 << ", gl_MaxTransformFeedbackInterleavedComponents is " << resources.maxTransformFeedbackInterleavedComponents << "\n";
685         }
686     }
687 
688     switch (language) {
689     case EShLangVertex:
690         break;
691     case EShLangTessControl:
692         if (vertices == TQualifier::layoutNotSet)
693             error(infoSink, "At least one shader must specify an output layout(vertices=...)");
694         break;
695     case EShLangTessEvaluation:
696         if (getSource() == EShSourceGlsl) {
697             if (inputPrimitive == ElgNone)
698                 error(infoSink, "At least one shader must specify an input layout primitive");
699             if (vertexSpacing == EvsNone)
700                 vertexSpacing = EvsEqual;
701             if (vertexOrder == EvoNone)
702                 vertexOrder = EvoCcw;
703         }
704         break;
705     case EShLangGeometry:
706         if (inputPrimitive == ElgNone)
707             error(infoSink, "At least one shader must specify an input layout primitive");
708         if (outputPrimitive == ElgNone)
709             error(infoSink, "At least one shader must specify an output layout primitive");
710         if (vertices == TQualifier::layoutNotSet)
711             error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
712         break;
713     case EShLangFragment:
714         // for GL_ARB_post_depth_coverage, EarlyFragmentTest is set automatically in
715         // ParseHelper.cpp. So if we reach here, this must be GL_EXT_post_depth_coverage
716         // requiring explicit early_fragment_tests
717         if (getPostDepthCoverage() && !getEarlyFragmentTests())
718             error(infoSink, "post_depth_coverage requires early_fragment_tests");
719         break;
720     case EShLangCompute:
721         break;
722     case EShLangRayGenNV:
723     case EShLangIntersectNV:
724     case EShLangAnyHitNV:
725     case EShLangClosestHitNV:
726     case EShLangMissNV:
727     case EShLangCallableNV:
728         if (numShaderRecordNVBlocks > 1)
729             error(infoSink, "Only one shaderRecordNV buffer block is allowed per stage");
730         break;
731     case EShLangMeshNV:
732         // NV_mesh_shader doesn't allow use of both single-view and per-view builtins.
733         if (inIoAccessed("gl_Position") && inIoAccessed("gl_PositionPerViewNV"))
734             error(infoSink, "Can only use one of gl_Position or gl_PositionPerViewNV");
735         if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipDistancePerViewNV"))
736             error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipDistancePerViewNV");
737         if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_CullDistancePerViewNV"))
738             error(infoSink, "Can only use one of gl_CullDistance or gl_CullDistancePerViewNV");
739         if (inIoAccessed("gl_Layer") && inIoAccessed("gl_LayerPerViewNV"))
740             error(infoSink, "Can only use one of gl_Layer or gl_LayerPerViewNV");
741         if (inIoAccessed("gl_ViewportMask") && inIoAccessed("gl_ViewportMaskPerViewNV"))
742             error(infoSink, "Can only use one of gl_ViewportMask or gl_ViewportMaskPerViewNV");
743         if (outputPrimitive == ElgNone)
744             error(infoSink, "At least one shader must specify an output layout primitive");
745         if (vertices == TQualifier::layoutNotSet)
746             error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
747         if (primitives == TQualifier::layoutNotSet)
748             error(infoSink, "At least one shader must specify a layout(max_primitives = value)");
749         // fall through
750     case EShLangTaskNV:
751         if (numTaskNVBlocks > 1)
752             error(infoSink, "Only one taskNV interface block is allowed per shader");
753         break;
754     default:
755         error(infoSink, "Unknown Stage.");
756         break;
757     }
758 
759     // Process the tree for any node-specific work.
760     class TFinalLinkTraverser : public TIntermTraverser {
761     public:
762         TFinalLinkTraverser() { }
763         virtual ~TFinalLinkTraverser() { }
764 
765         virtual void visitSymbol(TIntermSymbol* symbol)
766         {
767             // Implicitly size arrays.
768             // If an unsized array is left as unsized, it effectively
769             // becomes run-time sized.
770             symbol->getWritableType().adoptImplicitArraySizes(false);
771         }
772     } finalLinkTraverser;
773 
774     treeRoot->traverse(&finalLinkTraverser);
775 #endif
776 }
777 
778 //
779 // See if the call graph contains any static recursion, which is disallowed
780 // by the specification.
781 //
checkCallGraphCycles(TInfoSink & infoSink)782 void TIntermediate::checkCallGraphCycles(TInfoSink& infoSink)
783 {
784     // Clear fields we'll use for this.
785     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
786         call->visited = false;
787         call->currentPath = false;
788         call->errorGiven = false;
789     }
790 
791     //
792     // Loop, looking for a new connected subgraph.  One subgraph is handled per loop iteration.
793     //
794 
795     TCall* newRoot;
796     do {
797         // See if we have unvisited parts of the graph.
798         newRoot = 0;
799         for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
800             if (! call->visited) {
801                 newRoot = &(*call);
802                 break;
803             }
804         }
805 
806         // If not, we are done.
807         if (! newRoot)
808             break;
809 
810         // Otherwise, we found a new subgraph, process it:
811         // See what all can be reached by this new root, and if any of
812         // that is recursive.  This is done by depth-first traversals, seeing
813         // if a new call is found that was already in the currentPath (a back edge),
814         // thereby detecting recursion.
815         std::list<TCall*> stack;
816         newRoot->currentPath = true; // currentPath will be true iff it is on the stack
817         stack.push_back(newRoot);
818         while (! stack.empty()) {
819             // get a caller
820             TCall* call = stack.back();
821 
822             // Add to the stack just one callee.
823             // This algorithm always terminates, because only !visited and !currentPath causes a push
824             // and all pushes change currentPath to true, and all pops change visited to true.
825             TGraph::iterator child = callGraph.begin();
826             for (; child != callGraph.end(); ++child) {
827 
828                 // If we already visited this node, its whole subgraph has already been processed, so skip it.
829                 if (child->visited)
830                     continue;
831 
832                 if (call->callee == child->caller) {
833                     if (child->currentPath) {
834                         // Then, we found a back edge
835                         if (! child->errorGiven) {
836                             error(infoSink, "Recursion detected:");
837                             infoSink.info << "    " << call->callee << " calling " << child->callee << "\n";
838                             child->errorGiven = true;
839                             recursive = true;
840                         }
841                     } else {
842                         child->currentPath = true;
843                         stack.push_back(&(*child));
844                         break;
845                     }
846                 }
847             }
848             if (child == callGraph.end()) {
849                 // no more callees, we bottomed out, never look at this node again
850                 stack.back()->currentPath = false;
851                 stack.back()->visited = true;
852                 stack.pop_back();
853             }
854         }  // end while, meaning nothing left to process in this subtree
855 
856     } while (newRoot);  // redundant loop check; should always exit via the 'break' above
857 }
858 
859 //
860 // See which functions are reachable from the entry point and which have bodies.
861 // Reachable ones with missing bodies are errors.
862 // Unreachable bodies are dead code.
863 //
checkCallGraphBodies(TInfoSink & infoSink,bool keepUncalled)864 void TIntermediate::checkCallGraphBodies(TInfoSink& infoSink, bool keepUncalled)
865 {
866     // Clear fields we'll use for this.
867     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
868         call->visited = false;
869         call->calleeBodyPosition = -1;
870     }
871 
872     // The top level of the AST includes function definitions (bodies).
873     // Compare these to function calls in the call graph.
874     // We'll end up knowing which have bodies, and if so,
875     // how to map the call-graph node to the location in the AST.
876     TIntermSequence &functionSequence = getTreeRoot()->getAsAggregate()->getSequence();
877     std::vector<bool> reachable(functionSequence.size(), true); // so that non-functions are reachable
878     for (int f = 0; f < (int)functionSequence.size(); ++f) {
879         glslang::TIntermAggregate* node = functionSequence[f]->getAsAggregate();
880         if (node && (node->getOp() == glslang::EOpFunction)) {
881             if (node->getName().compare(getEntryPointMangledName().c_str()) != 0)
882                 reachable[f] = false; // so that function bodies are unreachable, until proven otherwise
883             for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
884                 if (call->callee == node->getName())
885                     call->calleeBodyPosition = f;
886             }
887         }
888     }
889 
890     // Start call-graph traversal by visiting the entry point nodes.
891     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
892         if (call->caller.compare(getEntryPointMangledName().c_str()) == 0)
893             call->visited = true;
894     }
895 
896     // Propagate 'visited' through the call-graph to every part of the graph it
897     // can reach (seeded with the entry-point setting above).
898     bool changed;
899     do {
900         changed = false;
901         for (auto call1 = callGraph.begin(); call1 != callGraph.end(); ++call1) {
902             if (call1->visited) {
903                 for (TGraph::iterator call2 = callGraph.begin(); call2 != callGraph.end(); ++call2) {
904                     if (! call2->visited) {
905                         if (call1->callee == call2->caller) {
906                             changed = true;
907                             call2->visited = true;
908                         }
909                     }
910                 }
911             }
912         }
913     } while (changed);
914 
915     // Any call-graph node set to visited but without a callee body is an error.
916     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
917         if (call->visited) {
918             if (call->calleeBodyPosition == -1) {
919                 error(infoSink, "No function definition (body) found: ");
920                 infoSink.info << "    " << call->callee << "\n";
921             } else
922                 reachable[call->calleeBodyPosition] = true;
923         }
924     }
925 
926     // Bodies in the AST not reached by the call graph are dead;
927     // clear them out, since they can't be reached and also can't
928     // be translated further due to possibility of being ill defined.
929     if (! keepUncalled) {
930         for (int f = 0; f < (int)functionSequence.size(); ++f) {
931             if (! reachable[f])
932                 functionSequence[f] = nullptr;
933         }
934         functionSequence.erase(std::remove(functionSequence.begin(), functionSequence.end(), nullptr), functionSequence.end());
935     }
936 }
937 
938 //
939 // Satisfy rules for location qualifiers on inputs and outputs
940 //
inOutLocationCheck(TInfoSink & infoSink)941 void TIntermediate::inOutLocationCheck(TInfoSink& infoSink)
942 {
943     // ES 3.0 requires all outputs to have location qualifiers if there is more than one output
944     bool fragOutWithNoLocation = false;
945     int numFragOut = 0;
946 
947     // TODO: linker functionality: location collision checking
948 
949     TIntermSequence& linkObjects = findLinkerObjects()->getSequence();
950     for (size_t i = 0; i < linkObjects.size(); ++i) {
951         const TType& type = linkObjects[i]->getAsTyped()->getType();
952         const TQualifier& qualifier = type.getQualifier();
953         if (language == EShLangFragment) {
954             if (qualifier.storage == EvqVaryingOut && qualifier.builtIn == EbvNone) {
955                 ++numFragOut;
956                 if (!qualifier.hasAnyLocation())
957                     fragOutWithNoLocation = true;
958             }
959         }
960     }
961 
962     if (isEsProfile()) {
963         if (numFragOut > 1 && fragOutWithNoLocation)
964             error(infoSink, "when more than one fragment shader output, all must have location qualifiers");
965     }
966 }
967 
findLinkerObjects() const968 TIntermAggregate* TIntermediate::findLinkerObjects() const
969 {
970     // Get the top-level globals
971     TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
972 
973     // Get the last member of the sequences, expected to be the linker-object lists
974     assert(globals.back()->getAsAggregate()->getOp() == EOpLinkerObjects);
975 
976     return globals.back()->getAsAggregate();
977 }
978 
979 // See if a variable was both a user-declared output and used.
980 // Note: the spec discusses writing to one, but this looks at read or write, which
981 // is more useful, and perhaps the spec should be changed to reflect that.
userOutputUsed() const982 bool TIntermediate::userOutputUsed() const
983 {
984     const TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
985 
986     bool found = false;
987     for (size_t i = 0; i < linkerObjects.size(); ++i) {
988         const TIntermSymbol& symbolNode = *linkerObjects[i]->getAsSymbolNode();
989         if (symbolNode.getQualifier().storage == EvqVaryingOut &&
990             symbolNode.getName().compare(0, 3, "gl_") != 0 &&
991             inIoAccessed(symbolNode.getName())) {
992             found = true;
993             break;
994         }
995     }
996 
997     return found;
998 }
999 
1000 // Accumulate locations used for inputs, outputs, and uniforms, and check for collisions
1001 // as the accumulation is done.
1002 //
1003 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1004 //
1005 // typeCollision is set to true if there is no direct collision, but the types in the same location
1006 // are different.
1007 //
addUsedLocation(const TQualifier & qualifier,const TType & type,bool & typeCollision)1008 int TIntermediate::addUsedLocation(const TQualifier& qualifier, const TType& type, bool& typeCollision)
1009 {
1010     typeCollision = false;
1011 
1012     int set;
1013     if (qualifier.isPipeInput())
1014         set = 0;
1015     else if (qualifier.isPipeOutput())
1016         set = 1;
1017     else if (qualifier.storage == EvqUniform)
1018         set = 2;
1019     else if (qualifier.storage == EvqBuffer)
1020         set = 3;
1021     else
1022         return -1;
1023 
1024     int size;
1025     if (qualifier.isUniformOrBuffer() || qualifier.isTaskMemory()) {
1026         if (type.isSizedArray())
1027             size = type.getCumulativeArraySize();
1028         else
1029             size = 1;
1030     } else {
1031         // Strip off the outer array dimension for those having an extra one.
1032         if (type.isArray() && qualifier.isArrayedIo(language)) {
1033             TType elementType(type, 0);
1034             size = computeTypeLocationSize(elementType, language);
1035         } else
1036             size = computeTypeLocationSize(type, language);
1037     }
1038 
1039     // Locations, and components within locations.
1040     //
1041     // Almost always, dealing with components means a single location is involved.
1042     // The exception is a dvec3. From the spec:
1043     //
1044     // "A dvec3 will consume all four components of the first location and components 0 and 1 of
1045     // the second location. This leaves components 2 and 3 available for other component-qualified
1046     // declarations."
1047     //
1048     // That means, without ever mentioning a component, a component range
1049     // for a different location gets specified, if it's not a vertex shader input. (!)
1050     // (A vertex shader input will show using only one location, even for a dvec3/4.)
1051     //
1052     // So, for the case of dvec3, we need two independent ioRanges.
1053 
1054     int collision = -1; // no collision
1055 #ifndef GLSLANG_WEB
1056     if (size == 2 && type.getBasicType() == EbtDouble && type.getVectorSize() == 3 &&
1057         (qualifier.isPipeInput() || qualifier.isPipeOutput())) {
1058         // Dealing with dvec3 in/out split across two locations.
1059         // Need two io-ranges.
1060         // The case where the dvec3 doesn't start at component 0 was previously caught as overflow.
1061 
1062         // First range:
1063         TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation);
1064         TRange componentRange(0, 3);
1065         TIoRange range(locationRange, componentRange, type.getBasicType(), 0);
1066 
1067         // check for collisions
1068         collision = checkLocationRange(set, range, type, typeCollision);
1069         if (collision < 0) {
1070             usedIo[set].push_back(range);
1071 
1072             // Second range:
1073             TRange locationRange2(qualifier.layoutLocation + 1, qualifier.layoutLocation + 1);
1074             TRange componentRange2(0, 1);
1075             TIoRange range2(locationRange2, componentRange2, type.getBasicType(), 0);
1076 
1077             // check for collisions
1078             collision = checkLocationRange(set, range2, type, typeCollision);
1079             if (collision < 0)
1080                 usedIo[set].push_back(range2);
1081         }
1082     } else
1083 #endif
1084     {
1085         // Not a dvec3 in/out split across two locations, generic path.
1086         // Need a single IO-range block.
1087 
1088         TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation + size - 1);
1089         TRange componentRange(0, 3);
1090         if (qualifier.hasComponent() || type.getVectorSize() > 0) {
1091             int consumedComponents = type.getVectorSize() * (type.getBasicType() == EbtDouble ? 2 : 1);
1092             if (qualifier.hasComponent())
1093                 componentRange.start = qualifier.layoutComponent;
1094             componentRange.last  = componentRange.start + consumedComponents - 1;
1095         }
1096 
1097         // combine location and component ranges
1098         TIoRange range(locationRange, componentRange, type.getBasicType(), qualifier.hasIndex() ? qualifier.getIndex() : 0);
1099 
1100         // check for collisions, except for vertex inputs on desktop targeting OpenGL
1101         if (! (!isEsProfile() && language == EShLangVertex && qualifier.isPipeInput()) || spvVersion.vulkan > 0)
1102             collision = checkLocationRange(set, range, type, typeCollision);
1103 
1104         if (collision < 0)
1105             usedIo[set].push_back(range);
1106     }
1107 
1108     return collision;
1109 }
1110 
1111 // Compare a new (the passed in) 'range' against the existing set, and see
1112 // if there are any collisions.
1113 //
1114 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1115 //
checkLocationRange(int set,const TIoRange & range,const TType & type,bool & typeCollision)1116 int TIntermediate::checkLocationRange(int set, const TIoRange& range, const TType& type, bool& typeCollision)
1117 {
1118     for (size_t r = 0; r < usedIo[set].size(); ++r) {
1119         if (range.overlap(usedIo[set][r])) {
1120             // there is a collision; pick one
1121             return std::max(range.location.start, usedIo[set][r].location.start);
1122         } else if (range.location.overlap(usedIo[set][r].location) && type.getBasicType() != usedIo[set][r].basicType) {
1123             // aliased-type mismatch
1124             typeCollision = true;
1125             return std::max(range.location.start, usedIo[set][r].location.start);
1126         }
1127     }
1128 
1129     return -1; // no collision
1130 }
1131 
1132 // Accumulate bindings and offsets, and check for collisions
1133 // as the accumulation is done.
1134 //
1135 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1136 //
addUsedOffsets(int binding,int offset,int numOffsets)1137 int TIntermediate::addUsedOffsets(int binding, int offset, int numOffsets)
1138 {
1139     TRange bindingRange(binding, binding);
1140     TRange offsetRange(offset, offset + numOffsets - 1);
1141     TOffsetRange range(bindingRange, offsetRange);
1142 
1143     // check for collisions, except for vertex inputs on desktop
1144     for (size_t r = 0; r < usedAtomics.size(); ++r) {
1145         if (range.overlap(usedAtomics[r])) {
1146             // there is a collision; pick one
1147             return std::max(offset, usedAtomics[r].offset.start);
1148         }
1149     }
1150 
1151     usedAtomics.push_back(range);
1152 
1153     return -1; // no collision
1154 }
1155 
1156 // Accumulate used constant_id values.
1157 //
1158 // Return false is one was already used.
addUsedConstantId(int id)1159 bool TIntermediate::addUsedConstantId(int id)
1160 {
1161     if (usedConstantId.find(id) != usedConstantId.end())
1162         return false;
1163 
1164     usedConstantId.insert(id);
1165 
1166     return true;
1167 }
1168 
1169 // Recursively figure out how many locations are used up by an input or output type.
1170 // Return the size of type, as measured by "locations".
computeTypeLocationSize(const TType & type,EShLanguage stage)1171 int TIntermediate::computeTypeLocationSize(const TType& type, EShLanguage stage)
1172 {
1173     // "If the declared input is an array of size n and each element takes m locations, it will be assigned m * n
1174     // consecutive locations..."
1175     if (type.isArray()) {
1176         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1177         // TODO: are there valid cases of having an unsized array with a location?  If so, running this code too early.
1178         TType elementType(type, 0);
1179         if (type.isSizedArray() && !type.getQualifier().isPerView())
1180             return type.getOuterArraySize() * computeTypeLocationSize(elementType, stage);
1181         else {
1182 #ifndef GLSLANG_WEB
1183             // unset perViewNV attributes for arrayed per-view outputs: "perviewNV vec4 v[MAX_VIEWS][3];"
1184             elementType.getQualifier().perViewNV = false;
1185 #endif
1186             return computeTypeLocationSize(elementType, stage);
1187         }
1188     }
1189 
1190     // "The locations consumed by block and structure members are determined by applying the rules above
1191     // recursively..."
1192     if (type.isStruct()) {
1193         int size = 0;
1194         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1195             TType memberType(type, member);
1196             size += computeTypeLocationSize(memberType, stage);
1197         }
1198         return size;
1199     }
1200 
1201     // ES: "If a shader input is any scalar or vector type, it will consume a single location."
1202 
1203     // Desktop: "If a vertex shader input is any scalar or vector type, it will consume a single location. If a non-vertex
1204     // shader input is a scalar or vector type other than dvec3 or dvec4, it will consume a single location, while
1205     // types dvec3 or dvec4 will consume two consecutive locations. Inputs of type double and dvec2 will
1206     // consume only a single location, in all stages."
1207     if (type.isScalar())
1208         return 1;
1209     if (type.isVector()) {
1210         if (stage == EShLangVertex && type.getQualifier().isPipeInput())
1211             return 1;
1212         if (type.getBasicType() == EbtDouble && type.getVectorSize() > 2)
1213             return 2;
1214         else
1215             return 1;
1216     }
1217 
1218     // "If the declared input is an n x m single- or double-precision matrix, ...
1219     // The number of locations assigned for each matrix will be the same as
1220     // for an n-element array of m-component vectors..."
1221     if (type.isMatrix()) {
1222         TType columnType(type, 0);
1223         return type.getMatrixCols() * computeTypeLocationSize(columnType, stage);
1224     }
1225 
1226     assert(0);
1227     return 1;
1228 }
1229 
1230 // Same as computeTypeLocationSize but for uniforms
computeTypeUniformLocationSize(const TType & type)1231 int TIntermediate::computeTypeUniformLocationSize(const TType& type)
1232 {
1233     // "Individual elements of a uniform array are assigned
1234     // consecutive locations with the first element taking location
1235     // location."
1236     if (type.isArray()) {
1237         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1238         TType elementType(type, 0);
1239         if (type.isSizedArray()) {
1240             return type.getOuterArraySize() * computeTypeUniformLocationSize(elementType);
1241         } else {
1242             // TODO: are there valid cases of having an implicitly-sized array with a location?  If so, running this code too early.
1243             return computeTypeUniformLocationSize(elementType);
1244         }
1245     }
1246 
1247     // "Each subsequent inner-most member or element gets incremental
1248     // locations for the entire structure or array."
1249     if (type.isStruct()) {
1250         int size = 0;
1251         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1252             TType memberType(type, member);
1253             size += computeTypeUniformLocationSize(memberType);
1254         }
1255         return size;
1256     }
1257 
1258     return 1;
1259 }
1260 
1261 #ifndef GLSLANG_WEB
1262 
1263 // Accumulate xfb buffer ranges and check for collisions as the accumulation is done.
1264 //
1265 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1266 //
addXfbBufferOffset(const TType & type)1267 int TIntermediate::addXfbBufferOffset(const TType& type)
1268 {
1269     const TQualifier& qualifier = type.getQualifier();
1270 
1271     assert(qualifier.hasXfbOffset() && qualifier.hasXfbBuffer());
1272     TXfbBuffer& buffer = xfbBuffers[qualifier.layoutXfbBuffer];
1273 
1274     // compute the range
1275     unsigned int size = computeTypeXfbSize(type, buffer.contains64BitType, buffer.contains32BitType, buffer.contains16BitType);
1276     buffer.implicitStride = std::max(buffer.implicitStride, qualifier.layoutXfbOffset + size);
1277     TRange range(qualifier.layoutXfbOffset, qualifier.layoutXfbOffset + size - 1);
1278 
1279     // check for collisions
1280     for (size_t r = 0; r < buffer.ranges.size(); ++r) {
1281         if (range.overlap(buffer.ranges[r])) {
1282             // there is a collision; pick an example to return
1283             return std::max(range.start, buffer.ranges[r].start);
1284         }
1285     }
1286 
1287     buffer.ranges.push_back(range);
1288 
1289     return -1;  // no collision
1290 }
1291 
1292 // Recursively figure out how many bytes of xfb buffer are used by the given type.
1293 // Return the size of type, in bytes.
1294 // Sets contains64BitType to true if the type contains a 64-bit data type.
1295 // Sets contains32BitType to true if the type contains a 32-bit data type.
1296 // Sets contains16BitType to true if the type contains a 16-bit data type.
1297 // N.B. Caller must set contains64BitType, contains32BitType, and contains16BitType to false before calling.
computeTypeXfbSize(const TType & type,bool & contains64BitType,bool & contains32BitType,bool & contains16BitType) const1298 unsigned int TIntermediate::computeTypeXfbSize(const TType& type, bool& contains64BitType, bool& contains32BitType, bool& contains16BitType) const
1299 {
1300     // "...if applied to an aggregate containing a double or 64-bit integer, the offset must also be a multiple of 8,
1301     // and the space taken in the buffer will be a multiple of 8.
1302     // ...within the qualified entity, subsequent components are each
1303     // assigned, in order, to the next available offset aligned to a multiple of
1304     // that component's size.  Aggregate types are flattened down to the component
1305     // level to get this sequence of components."
1306 
1307     if (type.isArray()) {
1308         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1309         assert(type.isSizedArray());
1310         TType elementType(type, 0);
1311         return type.getOuterArraySize() * computeTypeXfbSize(elementType, contains64BitType, contains16BitType, contains16BitType);
1312     }
1313 
1314     if (type.isStruct()) {
1315         unsigned int size = 0;
1316         bool structContains64BitType = false;
1317         bool structContains32BitType = false;
1318         bool structContains16BitType = false;
1319         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1320             TType memberType(type, member);
1321             // "... if applied to
1322             // an aggregate containing a double or 64-bit integer, the offset must also be a multiple of 8,
1323             // and the space taken in the buffer will be a multiple of 8."
1324             bool memberContains64BitType = false;
1325             bool memberContains32BitType = false;
1326             bool memberContains16BitType = false;
1327             int memberSize = computeTypeXfbSize(memberType, memberContains64BitType, memberContains32BitType, memberContains16BitType);
1328             if (memberContains64BitType) {
1329                 structContains64BitType = true;
1330                 RoundToPow2(size, 8);
1331             } else if (memberContains32BitType) {
1332                 structContains32BitType = true;
1333                 RoundToPow2(size, 4);
1334             } else if (memberContains16BitType) {
1335                 structContains16BitType = true;
1336                 RoundToPow2(size, 2);
1337             }
1338             size += memberSize;
1339         }
1340 
1341         if (structContains64BitType) {
1342             contains64BitType = true;
1343             RoundToPow2(size, 8);
1344         } else if (structContains32BitType) {
1345             contains32BitType = true;
1346             RoundToPow2(size, 4);
1347         } else if (structContains16BitType) {
1348             contains16BitType = true;
1349             RoundToPow2(size, 2);
1350         }
1351         return size;
1352     }
1353 
1354     int numComponents;
1355     if (type.isScalar())
1356         numComponents = 1;
1357     else if (type.isVector())
1358         numComponents = type.getVectorSize();
1359     else if (type.isMatrix())
1360         numComponents = type.getMatrixCols() * type.getMatrixRows();
1361     else {
1362         assert(0);
1363         numComponents = 1;
1364     }
1365 
1366     if (type.getBasicType() == EbtDouble || type.getBasicType() == EbtInt64 || type.getBasicType() == EbtUint64) {
1367         contains64BitType = true;
1368         return 8 * numComponents;
1369     } else if (type.getBasicType() == EbtFloat16 || type.getBasicType() == EbtInt16 || type.getBasicType() == EbtUint16) {
1370         contains16BitType = true;
1371         return 2 * numComponents;
1372     } else if (type.getBasicType() == EbtInt8 || type.getBasicType() == EbtUint8)
1373         return numComponents;
1374     else {
1375         contains32BitType = true;
1376         return 4 * numComponents;
1377     }
1378 }
1379 
1380 #endif
1381 
1382 const int baseAlignmentVec4Std140 = 16;
1383 
1384 // Return the size and alignment of a component of the given type.
1385 // The size is returned in the 'size' parameter
1386 // Return value is the alignment..
getBaseAlignmentScalar(const TType & type,int & size)1387 int TIntermediate::getBaseAlignmentScalar(const TType& type, int& size)
1388 {
1389 #ifdef GLSLANG_WEB
1390     size = 4; return 4;
1391 #endif
1392 
1393     switch (type.getBasicType()) {
1394     case EbtInt64:
1395     case EbtUint64:
1396     case EbtDouble:  size = 8; return 8;
1397     case EbtFloat16: size = 2; return 2;
1398     case EbtInt8:
1399     case EbtUint8:   size = 1; return 1;
1400     case EbtInt16:
1401     case EbtUint16:  size = 2; return 2;
1402     case EbtReference: size = 8; return 8;
1403     default:         size = 4; return 4;
1404     }
1405 }
1406 
1407 // Implement base-alignment and size rules from section 7.6.2.2 Standard Uniform Block Layout
1408 // Operates recursively.
1409 //
1410 // If std140 is true, it does the rounding up to vec4 size required by std140,
1411 // otherwise it does not, yielding std430 rules.
1412 //
1413 // The size is returned in the 'size' parameter
1414 //
1415 // The stride is only non-0 for arrays or matrices, and is the stride of the
1416 // top-level object nested within the type.  E.g., for an array of matrices,
1417 // it is the distances needed between matrices, despite the rules saying the
1418 // stride comes from the flattening down to vectors.
1419 //
1420 // Return value is the alignment of the type.
getBaseAlignment(const TType & type,int & size,int & stride,TLayoutPacking layoutPacking,bool rowMajor)1421 int TIntermediate::getBaseAlignment(const TType& type, int& size, int& stride, TLayoutPacking layoutPacking, bool rowMajor)
1422 {
1423     int alignment;
1424 
1425     bool std140 = layoutPacking == glslang::ElpStd140;
1426     // When using the std140 storage layout, structures will be laid out in buffer
1427     // storage with its members stored in monotonically increasing order based on their
1428     // location in the declaration. A structure and each structure member have a base
1429     // offset and a base alignment, from which an aligned offset is computed by rounding
1430     // the base offset up to a multiple of the base alignment. The base offset of the first
1431     // member of a structure is taken from the aligned offset of the structure itself. The
1432     // base offset of all other structure members is derived by taking the offset of the
1433     // last basic machine unit consumed by the previous member and adding one. Each
1434     // structure member is stored in memory at its aligned offset. The members of a top-
1435     // level uniform block are laid out in buffer storage by treating the uniform block as
1436     // a structure with a base offset of zero.
1437     //
1438     //   1. If the member is a scalar consuming N basic machine units, the base alignment is N.
1439     //
1440     //   2. If the member is a two- or four-component vector with components consuming N basic
1441     //      machine units, the base alignment is 2N or 4N, respectively.
1442     //
1443     //   3. If the member is a three-component vector with components consuming N
1444     //      basic machine units, the base alignment is 4N.
1445     //
1446     //   4. If the member is an array of scalars or vectors, the base alignment and array
1447     //      stride are set to match the base alignment of a single array element, according
1448     //      to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The
1449     //      array may have padding at the end; the base offset of the member following
1450     //      the array is rounded up to the next multiple of the base alignment.
1451     //
1452     //   5. If the member is a column-major matrix with C columns and R rows, the
1453     //      matrix is stored identically to an array of C column vectors with R
1454     //      components each, according to rule (4).
1455     //
1456     //   6. If the member is an array of S column-major matrices with C columns and
1457     //      R rows, the matrix is stored identically to a row of S X C column vectors
1458     //      with R components each, according to rule (4).
1459     //
1460     //   7. If the member is a row-major matrix with C columns and R rows, the matrix
1461     //      is stored identically to an array of R row vectors with C components each,
1462     //      according to rule (4).
1463     //
1464     //   8. If the member is an array of S row-major matrices with C columns and R
1465     //      rows, the matrix is stored identically to a row of S X R row vectors with C
1466     //      components each, according to rule (4).
1467     //
1468     //   9. If the member is a structure, the base alignment of the structure is N , where
1469     //      N is the largest base alignment value of any    of its members, and rounded
1470     //      up to the base alignment of a vec4. The individual members of this substructure
1471     //      are then assigned offsets by applying this set of rules recursively,
1472     //      where the base offset of the first member of the sub-structure is equal to the
1473     //      aligned offset of the structure. The structure may have padding at the end;
1474     //      the base offset of the member following the sub-structure is rounded up to
1475     //      the next multiple of the base alignment of the structure.
1476     //
1477     //   10. If the member is an array of S structures, the S elements of the array are laid
1478     //       out in order, according to rule (9).
1479     //
1480     //   Assuming, for rule 10:  The stride is the same as the size of an element.
1481 
1482     stride = 0;
1483     int dummyStride;
1484 
1485     // rules 4, 6, 8, and 10
1486     if (type.isArray()) {
1487         // TODO: perf: this might be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1488         TType derefType(type, 0);
1489         alignment = getBaseAlignment(derefType, size, dummyStride, layoutPacking, rowMajor);
1490         if (std140)
1491             alignment = std::max(baseAlignmentVec4Std140, alignment);
1492         RoundToPow2(size, alignment);
1493         stride = size;  // uses full matrix size for stride of an array of matrices (not quite what rule 6/8, but what's expected)
1494                         // uses the assumption for rule 10 in the comment above
1495         size = stride * type.getOuterArraySize();
1496         return alignment;
1497     }
1498 
1499     // rule 9
1500     if (type.getBasicType() == EbtStruct) {
1501         const TTypeList& memberList = *type.getStruct();
1502 
1503         size = 0;
1504         int maxAlignment = std140 ? baseAlignmentVec4Std140 : 0;
1505         for (size_t m = 0; m < memberList.size(); ++m) {
1506             int memberSize;
1507             // modify just the children's view of matrix layout, if there is one for this member
1508             TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
1509             int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, dummyStride, layoutPacking,
1510                                                    (subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
1511             maxAlignment = std::max(maxAlignment, memberAlignment);
1512             RoundToPow2(size, memberAlignment);
1513             size += memberSize;
1514         }
1515 
1516         // The structure may have padding at the end; the base offset of
1517         // the member following the sub-structure is rounded up to the next
1518         // multiple of the base alignment of the structure.
1519         RoundToPow2(size, maxAlignment);
1520 
1521         return maxAlignment;
1522     }
1523 
1524     // rule 1
1525     if (type.isScalar())
1526         return getBaseAlignmentScalar(type, size);
1527 
1528     // rules 2 and 3
1529     if (type.isVector()) {
1530         int scalarAlign = getBaseAlignmentScalar(type, size);
1531         switch (type.getVectorSize()) {
1532         case 1: // HLSL has this, GLSL does not
1533             return scalarAlign;
1534         case 2:
1535             size *= 2;
1536             return 2 * scalarAlign;
1537         default:
1538             size *= type.getVectorSize();
1539             return 4 * scalarAlign;
1540         }
1541     }
1542 
1543     // rules 5 and 7
1544     if (type.isMatrix()) {
1545         // rule 5: deref to row, not to column, meaning the size of vector is num columns instead of num rows
1546         TType derefType(type, 0, rowMajor);
1547 
1548         alignment = getBaseAlignment(derefType, size, dummyStride, layoutPacking, rowMajor);
1549         if (std140)
1550             alignment = std::max(baseAlignmentVec4Std140, alignment);
1551         RoundToPow2(size, alignment);
1552         stride = size;  // use intra-matrix stride for stride of a just a matrix
1553         if (rowMajor)
1554             size = stride * type.getMatrixRows();
1555         else
1556             size = stride * type.getMatrixCols();
1557 
1558         return alignment;
1559     }
1560 
1561     assert(0);  // all cases should be covered above
1562     size = baseAlignmentVec4Std140;
1563     return baseAlignmentVec4Std140;
1564 }
1565 
1566 // To aid the basic HLSL rule about crossing vec4 boundaries.
improperStraddle(const TType & type,int size,int offset)1567 bool TIntermediate::improperStraddle(const TType& type, int size, int offset)
1568 {
1569     if (! type.isVector() || type.isArray())
1570         return false;
1571 
1572     return size <= 16 ? offset / 16 != (offset + size - 1) / 16
1573                       : offset % 16 != 0;
1574 }
1575 
getScalarAlignment(const TType & type,int & size,int & stride,bool rowMajor)1576 int TIntermediate::getScalarAlignment(const TType& type, int& size, int& stride, bool rowMajor)
1577 {
1578     int alignment;
1579 
1580     stride = 0;
1581     int dummyStride;
1582 
1583     if (type.isArray()) {
1584         TType derefType(type, 0);
1585         alignment = getScalarAlignment(derefType, size, dummyStride, rowMajor);
1586 
1587         stride = size;
1588         RoundToPow2(stride, alignment);
1589 
1590         size = stride * (type.getOuterArraySize() - 1) + size;
1591         return alignment;
1592     }
1593 
1594     if (type.getBasicType() == EbtStruct) {
1595         const TTypeList& memberList = *type.getStruct();
1596 
1597         size = 0;
1598         int maxAlignment = 0;
1599         for (size_t m = 0; m < memberList.size(); ++m) {
1600             int memberSize;
1601             // modify just the children's view of matrix layout, if there is one for this member
1602             TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
1603             int memberAlignment = getScalarAlignment(*memberList[m].type, memberSize, dummyStride,
1604                                                      (subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
1605             maxAlignment = std::max(maxAlignment, memberAlignment);
1606             RoundToPow2(size, memberAlignment);
1607             size += memberSize;
1608         }
1609 
1610         return maxAlignment;
1611     }
1612 
1613     if (type.isScalar())
1614         return getBaseAlignmentScalar(type, size);
1615 
1616     if (type.isVector()) {
1617         int scalarAlign = getBaseAlignmentScalar(type, size);
1618 
1619         size *= type.getVectorSize();
1620         return scalarAlign;
1621     }
1622 
1623     if (type.isMatrix()) {
1624         TType derefType(type, 0, rowMajor);
1625 
1626         alignment = getScalarAlignment(derefType, size, dummyStride, rowMajor);
1627 
1628         stride = size;  // use intra-matrix stride for stride of a just a matrix
1629         if (rowMajor)
1630             size = stride * type.getMatrixRows();
1631         else
1632             size = stride * type.getMatrixCols();
1633 
1634         return alignment;
1635     }
1636 
1637     assert(0);  // all cases should be covered above
1638     size = 1;
1639     return 1;
1640 }
1641 
getMemberAlignment(const TType & type,int & size,int & stride,TLayoutPacking layoutPacking,bool rowMajor)1642 int TIntermediate::getMemberAlignment(const TType& type, int& size, int& stride, TLayoutPacking layoutPacking, bool rowMajor)
1643 {
1644     if (layoutPacking == glslang::ElpScalar) {
1645         return getScalarAlignment(type, size, stride, rowMajor);
1646     } else {
1647         return getBaseAlignment(type, size, stride, layoutPacking, rowMajor);
1648     }
1649 }
1650 
1651 // shared calculation by getOffset and getOffsets
updateOffset(const TType & parentType,const TType & memberType,int & offset,int & memberSize)1652 void TIntermediate::updateOffset(const TType& parentType, const TType& memberType, int& offset, int& memberSize)
1653 {
1654     int dummyStride;
1655 
1656     // modify just the children's view of matrix layout, if there is one for this member
1657     TLayoutMatrix subMatrixLayout = memberType.getQualifier().layoutMatrix;
1658     int memberAlignment = getMemberAlignment(memberType, memberSize, dummyStride,
1659                                              parentType.getQualifier().layoutPacking,
1660                                              subMatrixLayout != ElmNone
1661                                                  ? subMatrixLayout == ElmRowMajor
1662                                                  : parentType.getQualifier().layoutMatrix == ElmRowMajor);
1663     RoundToPow2(offset, memberAlignment);
1664 }
1665 
1666 // Lookup or calculate the offset of a block member, using the recursively
1667 // defined block offset rules.
getOffset(const TType & type,int index)1668 int TIntermediate::getOffset(const TType& type, int index)
1669 {
1670     const TTypeList& memberList = *type.getStruct();
1671 
1672     // Don't calculate offset if one is present, it could be user supplied
1673     // and different than what would be calculated.  That is, this is faster,
1674     // but not just an optimization.
1675     if (memberList[index].type->getQualifier().hasOffset())
1676         return memberList[index].type->getQualifier().layoutOffset;
1677 
1678     int memberSize = 0;
1679     int offset = 0;
1680     for (int m = 0; m <= index; ++m) {
1681         updateOffset(type, *memberList[m].type, offset, memberSize);
1682 
1683         if (m < index)
1684             offset += memberSize;
1685     }
1686 
1687     return offset;
1688 }
1689 
1690 // Calculate the block data size.
1691 // Block arrayness is not taken into account, each element is backed by a separate buffer.
getBlockSize(const TType & blockType)1692 int TIntermediate::getBlockSize(const TType& blockType)
1693 {
1694     const TTypeList& memberList = *blockType.getStruct();
1695     int lastIndex = (int)memberList.size() - 1;
1696     int lastOffset = getOffset(blockType, lastIndex);
1697 
1698     int lastMemberSize;
1699     int dummyStride;
1700     getMemberAlignment(*memberList[lastIndex].type, lastMemberSize, dummyStride,
1701                        blockType.getQualifier().layoutPacking,
1702                        blockType.getQualifier().layoutMatrix == ElmRowMajor);
1703 
1704     return lastOffset + lastMemberSize;
1705 }
1706 
computeBufferReferenceTypeSize(const TType & type)1707 int TIntermediate::computeBufferReferenceTypeSize(const TType& type)
1708 {
1709     assert(type.isReference());
1710     int size = getBlockSize(*type.getReferentType());
1711 
1712     int align = type.getBufferReferenceAlignment();
1713 
1714     if (align) {
1715         size = (size + align - 1) & ~(align-1);
1716     }
1717 
1718     return size;
1719 }
1720 
1721 } // end namespace glslang
1722