Merge pull request #2874 from mbechard/master
[platform/upstream/glslang.git] / glslang / MachineIndependent / linkValidate.cpp
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 #include "SymbolTable.h"
52
53 namespace glslang {
54
55 //
56 // Link-time error emitter.
57 //
58 void TIntermediate::error(TInfoSink& infoSink, const char* message)
59 {
60 #ifndef GLSLANG_WEB
61     infoSink.info.prefix(EPrefixError);
62     infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
63 #endif
64
65     ++numErrors;
66 }
67
68 // Link-time warning.
69 void TIntermediate::warn(TInfoSink& infoSink, const char* message)
70 {
71 #ifndef GLSLANG_WEB
72     infoSink.info.prefix(EPrefixWarning);
73     infoSink.info << "Linking " << StageName(language) << " stage: " << message << "\n";
74 #endif
75 }
76
77 // TODO: 4.4 offset/align:  "Two blocks linked together in the same program with the same block
78 // name must have the exact same set of members qualified with offset and their integral-constant
79 // expression values must be the same, or a link-time error results."
80
81 //
82 // Merge the information from 'unit' into 'this'
83 //
84 void TIntermediate::merge(TInfoSink& infoSink, TIntermediate& unit)
85 {
86 #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
87     mergeCallGraphs(infoSink, unit);
88     mergeModes(infoSink, unit);
89     mergeTrees(infoSink, unit);
90 #endif
91 }
92
93 //
94 // check that link objects between stages
95 //
96 void TIntermediate::mergeUniformObjects(TInfoSink& infoSink, TIntermediate& unit) {
97     if (unit.treeRoot == nullptr || treeRoot == nullptr)
98         return;
99
100     // Get the linker-object lists
101     TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
102     TIntermSequence unitLinkerObjects = unit.findLinkerObjects()->getSequence();
103
104     // filter unitLinkerObjects to only contain uniforms
105     auto end = std::remove_if(unitLinkerObjects.begin(), unitLinkerObjects.end(),
106         [](TIntermNode* node) {return node->getAsSymbolNode()->getQualifier().storage != EvqUniform &&
107                                       node->getAsSymbolNode()->getQualifier().storage != EvqBuffer; });
108     unitLinkerObjects.resize(end - unitLinkerObjects.begin());
109
110     // merge uniforms and do error checking
111     bool mergeExistingOnly = false;
112     mergeGlobalUniformBlocks(infoSink, unit, mergeExistingOnly);
113     mergeLinkerObjects(infoSink, linkerObjects, unitLinkerObjects, unit.getStage());
114 }
115
116 //
117 // do error checking on the shader boundary in / out vars 
118 //
119 void TIntermediate::checkStageIO(TInfoSink& infoSink, TIntermediate& unit) {
120     if (unit.treeRoot == nullptr || treeRoot == nullptr)
121         return;
122
123     // Get copies of the linker-object lists
124     TIntermSequence linkerObjects = findLinkerObjects()->getSequence();
125     TIntermSequence unitLinkerObjects = unit.findLinkerObjects()->getSequence();
126
127     // filter linkerObjects to only contain out variables
128     auto end = std::remove_if(linkerObjects.begin(), linkerObjects.end(),
129         [](TIntermNode* node) {return node->getAsSymbolNode()->getQualifier().storage != EvqVaryingOut; });
130     linkerObjects.resize(end - linkerObjects.begin());
131
132     // filter unitLinkerObjects to only contain in variables
133     auto unitEnd = std::remove_if(unitLinkerObjects.begin(), unitLinkerObjects.end(),
134         [](TIntermNode* node) {return node->getAsSymbolNode()->getQualifier().storage != EvqVaryingIn; });
135     unitLinkerObjects.resize(unitEnd - unitLinkerObjects.begin());
136
137     // do matching and error checking
138     mergeLinkerObjects(infoSink, linkerObjects, unitLinkerObjects, unit.getStage());
139
140     // TODO: final check; make sure that any statically used `in` have matching `out` written to
141 }
142
143 void TIntermediate::mergeCallGraphs(TInfoSink& infoSink, TIntermediate& unit)
144 {
145     if (unit.getNumEntryPoints() > 0) {
146         if (getNumEntryPoints() > 0)
147             error(infoSink, "can't handle multiple entry points per stage");
148         else {
149             entryPointName = unit.getEntryPointName();
150             entryPointMangledName = unit.getEntryPointMangledName();
151         }
152     }
153     numEntryPoints += unit.getNumEntryPoints();
154
155     callGraph.insert(callGraph.end(), unit.callGraph.begin(), unit.callGraph.end());
156 }
157
158 #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
159
160 #define MERGE_MAX(member) member = std::max(member, unit.member)
161 #define MERGE_TRUE(member) if (unit.member) member = unit.member;
162
163 void TIntermediate::mergeModes(TInfoSink& infoSink, TIntermediate& unit)
164 {
165     if (language != unit.language)
166         error(infoSink, "stages must match when linking into a single stage");
167
168     if (getSource() == EShSourceNone)
169         setSource(unit.getSource());
170     if (getSource() != unit.getSource())
171         error(infoSink, "can't link compilation units from different source languages");
172
173     if (treeRoot == nullptr) {
174         profile = unit.profile;
175         version = unit.version;
176         requestedExtensions = unit.requestedExtensions;
177     } else {
178         if ((isEsProfile()) != (unit.isEsProfile()))
179             error(infoSink, "Cannot cross link ES and desktop profiles");
180         else if (unit.profile == ECompatibilityProfile)
181             profile = ECompatibilityProfile;
182         version = std::max(version, unit.version);
183         requestedExtensions.insert(unit.requestedExtensions.begin(), unit.requestedExtensions.end());
184     }
185
186     MERGE_MAX(spvVersion.spv);
187     MERGE_MAX(spvVersion.vulkanGlsl);
188     MERGE_MAX(spvVersion.vulkan);
189     MERGE_MAX(spvVersion.openGl);
190     MERGE_TRUE(spvVersion.vulkanRelaxed);
191
192     numErrors += unit.getNumErrors();
193     // Only one push_constant is allowed, mergeLinkerObjects() will ensure the push_constant
194     // is the same for all units.
195     if (numPushConstants > 1 || unit.numPushConstants > 1)
196         error(infoSink, "Only one push_constant block is allowed per stage");
197     numPushConstants = std::min(numPushConstants + unit.numPushConstants, 1);
198
199     if (unit.invocations != TQualifier::layoutNotSet) {
200         if (invocations == TQualifier::layoutNotSet)
201             invocations = unit.invocations;
202         else if (invocations != unit.invocations)
203             error(infoSink, "number of invocations must match between compilation units");
204     }
205
206     if (vertices == TQualifier::layoutNotSet)
207         vertices = unit.vertices;
208     else if (unit.vertices != TQualifier::layoutNotSet && vertices != unit.vertices) {
209         if (language == EShLangGeometry || language == EShLangMeshNV)
210             error(infoSink, "Contradictory layout max_vertices values");
211         else if (language == EShLangTessControl)
212             error(infoSink, "Contradictory layout vertices values");
213         else
214             assert(0);
215     }
216     if (primitives == TQualifier::layoutNotSet)
217         primitives = unit.primitives;
218     else if (primitives != unit.primitives) {
219         if (language == EShLangMeshNV)
220             error(infoSink, "Contradictory layout max_primitives values");
221         else
222             assert(0);
223     }
224
225     if (inputPrimitive == ElgNone)
226         inputPrimitive = unit.inputPrimitive;
227     else if (unit.inputPrimitive != ElgNone && inputPrimitive != unit.inputPrimitive)
228         error(infoSink, "Contradictory input layout primitives");
229
230     if (outputPrimitive == ElgNone)
231         outputPrimitive = unit.outputPrimitive;
232     else if (unit.outputPrimitive != ElgNone && outputPrimitive != unit.outputPrimitive)
233         error(infoSink, "Contradictory output layout primitives");
234
235     if (originUpperLeft != unit.originUpperLeft || pixelCenterInteger != unit.pixelCenterInteger)
236         error(infoSink, "gl_FragCoord redeclarations must match across shaders");
237
238     if (vertexSpacing == EvsNone)
239         vertexSpacing = unit.vertexSpacing;
240     else if (vertexSpacing != unit.vertexSpacing)
241         error(infoSink, "Contradictory input vertex spacing");
242
243     if (vertexOrder == EvoNone)
244         vertexOrder = unit.vertexOrder;
245     else if (vertexOrder != unit.vertexOrder)
246         error(infoSink, "Contradictory triangle ordering");
247
248     MERGE_TRUE(pointMode);
249
250     for (int i = 0; i < 3; ++i) {
251         if (unit.localSizeNotDefault[i]) {
252             if (!localSizeNotDefault[i]) {
253                 localSize[i] = unit.localSize[i];
254                 localSizeNotDefault[i] = true;
255             }
256             else if (localSize[i] != unit.localSize[i])
257                 error(infoSink, "Contradictory local size");
258         }
259
260         if (localSizeSpecId[i] == TQualifier::layoutNotSet)
261             localSizeSpecId[i] = unit.localSizeSpecId[i];
262         else if (localSizeSpecId[i] != unit.localSizeSpecId[i])
263             error(infoSink, "Contradictory local size specialization ids");
264     }
265
266     MERGE_TRUE(earlyFragmentTests);
267     MERGE_TRUE(postDepthCoverage);
268
269     if (depthLayout == EldNone)
270         depthLayout = unit.depthLayout;
271     else if (depthLayout != unit.depthLayout)
272         error(infoSink, "Contradictory depth layouts");
273
274     MERGE_TRUE(depthReplacing);
275     MERGE_TRUE(hlslFunctionality1);
276
277     blendEquations |= unit.blendEquations;
278
279     MERGE_TRUE(xfbMode);
280
281     for (size_t b = 0; b < xfbBuffers.size(); ++b) {
282         if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
283             xfbBuffers[b].stride = unit.xfbBuffers[b].stride;
284         else if (xfbBuffers[b].stride != unit.xfbBuffers[b].stride)
285             error(infoSink, "Contradictory xfb_stride");
286         xfbBuffers[b].implicitStride = std::max(xfbBuffers[b].implicitStride, unit.xfbBuffers[b].implicitStride);
287         if (unit.xfbBuffers[b].contains64BitType)
288             xfbBuffers[b].contains64BitType = true;
289         if (unit.xfbBuffers[b].contains32BitType)
290             xfbBuffers[b].contains32BitType = true;
291         if (unit.xfbBuffers[b].contains16BitType)
292             xfbBuffers[b].contains16BitType = true;
293         // TODO: 4.4 link: enhanced layouts: compare ranges
294     }
295
296     MERGE_TRUE(multiStream);
297     MERGE_TRUE(layoutOverrideCoverage);
298     MERGE_TRUE(geoPassthroughEXT);
299
300     for (unsigned int i = 0; i < unit.shiftBinding.size(); ++i) {
301         if (unit.shiftBinding[i] > 0)
302             setShiftBinding((TResourceType)i, unit.shiftBinding[i]);
303     }
304
305     for (unsigned int i = 0; i < unit.shiftBindingForSet.size(); ++i) {
306         for (auto it = unit.shiftBindingForSet[i].begin(); it != unit.shiftBindingForSet[i].end(); ++it)
307             setShiftBindingForSet((TResourceType)i, it->second, it->first);
308     }
309
310     resourceSetBinding.insert(resourceSetBinding.end(), unit.resourceSetBinding.begin(), unit.resourceSetBinding.end());
311
312     MERGE_TRUE(autoMapBindings);
313     MERGE_TRUE(autoMapLocations);
314     MERGE_TRUE(invertY);
315     MERGE_TRUE(dxPositionW);
316     MERGE_TRUE(flattenUniformArrays);
317     MERGE_TRUE(useUnknownFormat);
318     MERGE_TRUE(hlslOffsets);
319     MERGE_TRUE(useStorageBuffer);
320     MERGE_TRUE(invariantAll);
321     MERGE_TRUE(hlslIoMapping);
322
323     // TODO: sourceFile
324     // TODO: sourceText
325     // TODO: processes
326
327     MERGE_TRUE(needToLegalize);
328     MERGE_TRUE(binaryDoubleOutput);
329     MERGE_TRUE(usePhysicalStorageBuffer);
330 }
331
332 //
333 // Merge the 'unit' AST into 'this' AST.
334 // That includes rationalizing the unique IDs, which were set up independently,
335 // and might have overlaps that are not the same symbol, or might have different
336 // IDs for what should be the same shared symbol.
337 //
338 void TIntermediate::mergeTrees(TInfoSink& infoSink, TIntermediate& unit)
339 {
340     if (unit.treeRoot == nullptr)
341         return;
342
343     if (treeRoot == nullptr) {
344         treeRoot = unit.treeRoot;
345         return;
346     }
347
348     // Getting this far means we have two existing trees to merge...
349     numShaderRecordBlocks += unit.numShaderRecordBlocks;
350     numTaskNVBlocks += unit.numTaskNVBlocks;
351
352     // Get the top-level globals of each unit
353     TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
354     TIntermSequence& unitGlobals = unit.treeRoot->getAsAggregate()->getSequence();
355
356     // Get the linker-object lists
357     TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
358     const TIntermSequence& unitLinkerObjects = unit.findLinkerObjects()->getSequence();
359
360     // Map by global name to unique ID to rationalize the same object having
361     // differing IDs in different trees.
362     TIdMaps idMaps;
363     long long idShift;
364     seedIdMap(idMaps, idShift);
365     remapIds(idMaps, idShift + 1, unit);
366
367     mergeBodies(infoSink, globals, unitGlobals);
368     bool mergeExistingOnly = false;
369     mergeGlobalUniformBlocks(infoSink, unit, mergeExistingOnly);
370     mergeLinkerObjects(infoSink, linkerObjects, unitLinkerObjects, unit.getStage());
371     ioAccessed.insert(unit.ioAccessed.begin(), unit.ioAccessed.end());
372 }
373
374 #endif
375
376 static const TString& getNameForIdMap(TIntermSymbol* symbol)
377 {
378     TShaderInterface si = symbol->getType().getShaderInterface();
379     if (si == EsiNone)
380         return symbol->getName();
381     else
382         return symbol->getType().getTypeName();
383 }
384
385
386
387 // Traverser that seeds an ID map with all built-ins, and tracks the
388 // maximum ID used, currently using (maximum ID + 1) as new symbol id shift seed.
389 // Level id will keep same after shifting.
390 // (It would be nice to put this in a function, but that causes warnings
391 // on having no bodies for the copy-constructor/operator=.)
392 class TBuiltInIdTraverser : public TIntermTraverser {
393 public:
394     TBuiltInIdTraverser(TIdMaps& idMaps) : idMaps(idMaps), idShift(0) { }
395     // If it's a built in, add it to the map.
396     virtual void visitSymbol(TIntermSymbol* symbol)
397     {
398         const TQualifier& qualifier = symbol->getType().getQualifier();
399         if (qualifier.builtIn != EbvNone) {
400             TShaderInterface si = symbol->getType().getShaderInterface();
401             idMaps[si][getNameForIdMap(symbol)] = symbol->getId();
402         }
403         idShift = (symbol->getId() & ~TSymbolTable::uniqueIdMask) |
404                 std::max(idShift & TSymbolTable::uniqueIdMask,
405                          symbol->getId() & TSymbolTable::uniqueIdMask);
406     }
407     long long getIdShift() const { return idShift; }
408 protected:
409     TBuiltInIdTraverser(TBuiltInIdTraverser&);
410     TBuiltInIdTraverser& operator=(TBuiltInIdTraverser&);
411     TIdMaps& idMaps;
412     long long idShift;
413 };
414
415 // Traverser that seeds an ID map with non-builtins.
416 // (It would be nice to put this in a function, but that causes warnings
417 // on having no bodies for the copy-constructor/operator=.)
418 class TUserIdTraverser : public TIntermTraverser {
419 public:
420     TUserIdTraverser(TIdMaps& idMaps) : idMaps(idMaps) { }
421     // If its a non-built-in global, add it to the map.
422     virtual void visitSymbol(TIntermSymbol* symbol)
423     {
424         const TQualifier& qualifier = symbol->getType().getQualifier();
425         if (qualifier.builtIn == EbvNone) {
426             TShaderInterface si = symbol->getType().getShaderInterface();
427             idMaps[si][getNameForIdMap(symbol)] = symbol->getId();
428         }
429     }
430
431 protected:
432     TUserIdTraverser(TUserIdTraverser&);
433     TUserIdTraverser& operator=(TUserIdTraverser&);
434     TIdMaps& idMaps; // over biggest id
435 };
436
437 // Initialize the the ID map with what we know of 'this' AST.
438 void TIntermediate::seedIdMap(TIdMaps& idMaps, long long& idShift)
439 {
440     // all built-ins everywhere need to align on IDs and contribute to the max ID
441     TBuiltInIdTraverser builtInIdTraverser(idMaps);
442     treeRoot->traverse(&builtInIdTraverser);
443     idShift = builtInIdTraverser.getIdShift() & TSymbolTable::uniqueIdMask;
444
445     // user variables in the linker object list need to align on ids
446     TUserIdTraverser userIdTraverser(idMaps);
447     findLinkerObjects()->traverse(&userIdTraverser);
448 }
449
450 // Traverser to map an AST ID to what was known from the seeding AST.
451 // (It would be nice to put this in a function, but that causes warnings
452 // on having no bodies for the copy-constructor/operator=.)
453 class TRemapIdTraverser : public TIntermTraverser {
454 public:
455     TRemapIdTraverser(const TIdMaps& idMaps, long long idShift) : idMaps(idMaps), idShift(idShift) { }
456     // Do the mapping:
457     //  - if the same symbol, adopt the 'this' ID
458     //  - otherwise, ensure a unique ID by shifting to a new space
459     virtual void visitSymbol(TIntermSymbol* symbol)
460     {
461         const TQualifier& qualifier = symbol->getType().getQualifier();
462         bool remapped = false;
463         if (qualifier.isLinkable() || qualifier.builtIn != EbvNone) {
464             TShaderInterface si = symbol->getType().getShaderInterface();
465             auto it = idMaps[si].find(getNameForIdMap(symbol));
466             if (it != idMaps[si].end()) {
467                 uint64_t id = (symbol->getId() & ~TSymbolTable::uniqueIdMask) |
468                     (it->second & TSymbolTable::uniqueIdMask);
469                 symbol->changeId(id);
470                 remapped = true;
471             }
472         }
473         if (!remapped)
474             symbol->changeId(symbol->getId() + idShift);
475     }
476 protected:
477     TRemapIdTraverser(TRemapIdTraverser&);
478     TRemapIdTraverser& operator=(TRemapIdTraverser&);
479     const TIdMaps& idMaps;
480     long long idShift;
481 };
482
483 void TIntermediate::remapIds(const TIdMaps& idMaps, long long idShift, TIntermediate& unit)
484 {
485     // Remap all IDs to either share or be unique, as dictated by the idMap and idShift.
486     TRemapIdTraverser idTraverser(idMaps, idShift);
487     unit.getTreeRoot()->traverse(&idTraverser);
488 }
489
490 //
491 // Merge the function bodies and global-level initializers from unitGlobals into globals.
492 // Will error check duplication of function bodies for the same signature.
493 //
494 void TIntermediate::mergeBodies(TInfoSink& infoSink, TIntermSequence& globals, const TIntermSequence& unitGlobals)
495 {
496     // TODO: link-time performance: Processing in alphabetical order will be faster
497
498     // Error check the global objects, not including the linker objects
499     for (unsigned int child = 0; child < globals.size() - 1; ++child) {
500         for (unsigned int unitChild = 0; unitChild < unitGlobals.size() - 1; ++unitChild) {
501             TIntermAggregate* body = globals[child]->getAsAggregate();
502             TIntermAggregate* unitBody = unitGlobals[unitChild]->getAsAggregate();
503             if (body && unitBody && body->getOp() == EOpFunction && unitBody->getOp() == EOpFunction && body->getName() == unitBody->getName()) {
504                 error(infoSink, "Multiple function bodies in multiple compilation units for the same signature in the same stage:");
505                 infoSink.info << "    " << globals[child]->getAsAggregate()->getName() << "\n";
506             }
507         }
508     }
509
510     // Merge the global objects, just in front of the linker objects
511     globals.insert(globals.end() - 1, unitGlobals.begin(), unitGlobals.end() - 1);
512 }
513
514 static inline bool isSameInterface(TIntermSymbol* symbol, EShLanguage stage, TIntermSymbol* unitSymbol, EShLanguage unitStage) {
515     return // 1) same stage and same shader interface
516         (stage == unitStage && symbol->getType().getShaderInterface() == unitSymbol->getType().getShaderInterface()) ||
517         // 2) accross stages and both are uniform or buffer
518         (symbol->getQualifier().storage == EvqUniform  && unitSymbol->getQualifier().storage == EvqUniform) ||
519         (symbol->getQualifier().storage == EvqBuffer   && unitSymbol->getQualifier().storage == EvqBuffer) ||
520         // 3) in/out matched across stage boundary
521         (stage < unitStage && symbol->getQualifier().storage == EvqVaryingOut  && unitSymbol->getQualifier().storage == EvqVaryingIn) ||
522         (unitStage < stage && symbol->getQualifier().storage == EvqVaryingIn && unitSymbol->getQualifier().storage == EvqVaryingOut);
523 }
524
525 //
526 // Global Unfiform block stores any default uniforms (i.e. uniforms without a block)
527 // If two linked stages declare the same member, they are meant to be the same uniform
528 // and need to be in the same block
529 // merge the members of different stages to allow them to be linked properly
530 // as a single block
531 //
532 void TIntermediate::mergeGlobalUniformBlocks(TInfoSink& infoSink, TIntermediate& unit, bool mergeExistingOnly)
533 {
534     TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
535     TIntermSequence& unitLinkerObjects = unit.findLinkerObjects()->getSequence();
536
537     // build lists of default blocks from the intermediates
538     TIntermSequence defaultBlocks;
539     TIntermSequence unitDefaultBlocks;
540
541     auto filter = [](TIntermSequence& list, TIntermNode* node) {
542         if (node->getAsSymbolNode()->getQualifier().defaultBlock) {
543             list.push_back(node);
544         }
545     };
546
547     std::for_each(linkerObjects.begin(), linkerObjects.end(),
548         [&defaultBlocks, &filter](TIntermNode* node) {
549             filter(defaultBlocks, node);
550         });
551     std::for_each(unitLinkerObjects.begin(), unitLinkerObjects.end(),
552         [&unitDefaultBlocks, &filter](TIntermNode* node) {
553             filter(unitDefaultBlocks, node);
554     });
555
556     auto itUnitBlock = unitDefaultBlocks.begin();
557     for (; itUnitBlock != unitDefaultBlocks.end(); itUnitBlock++) {
558
559         bool add = !mergeExistingOnly;
560         auto itBlock = defaultBlocks.begin();
561
562         for (; itBlock != defaultBlocks.end(); itBlock++) {
563             TIntermSymbol* block = (*itBlock)->getAsSymbolNode();
564             TIntermSymbol* unitBlock = (*itUnitBlock)->getAsSymbolNode();
565
566             assert(block && unitBlock);
567
568             // if the two default blocks match, then merge their definitions
569             if (block->getType().getTypeName() == unitBlock->getType().getTypeName() &&
570                 block->getQualifier().storage == unitBlock->getQualifier().storage) {
571                 add = false;
572                 mergeBlockDefinitions(infoSink, block, unitBlock, &unit);
573             }
574         }
575         if (add) {
576             // push back on original list; won't change the size of the list we're iterating over
577             linkerObjects.push_back(*itUnitBlock);
578         }
579     }
580 }
581
582 void TIntermediate::mergeBlockDefinitions(TInfoSink& infoSink, TIntermSymbol* block, TIntermSymbol* unitBlock, TIntermediate* unit) {
583
584     if (block->getType().getTypeName() != unitBlock->getType().getTypeName() ||
585         block->getType().getBasicType() != unitBlock->getType().getBasicType() ||
586         block->getQualifier().storage != unitBlock->getQualifier().storage ||
587         block->getQualifier().layoutSet != unitBlock->getQualifier().layoutSet) {
588         // different block names likely means different blocks
589         return;
590     }
591
592     // merge the struct
593     // order of declarations doesn't matter and they matched based on member name
594     TTypeList* memberList = block->getType().getWritableStruct();
595     TTypeList* unitMemberList = unitBlock->getType().getWritableStruct();
596
597     // keep track of which members have changed position
598     // so we don't have to search the array again
599     std::map<unsigned int, unsigned int> memberIndexUpdates;
600
601     size_t memberListStartSize = memberList->size();
602     for (unsigned int i = 0; i < unitMemberList->size(); ++i) {
603         bool merge = true;
604         for (unsigned int j = 0; j < memberListStartSize; ++j) {
605             if ((*memberList)[j].type->getFieldName() == (*unitMemberList)[i].type->getFieldName()) {
606                 merge = false;
607                 const TType* memberType = (*memberList)[j].type;
608                 const TType* unitMemberType = (*unitMemberList)[i].type;
609
610                 // compare types
611                 // don't need as many checks as when merging symbols, since
612                 // initializers and most qualifiers are stripped when the member is moved into the block
613                 if ((*memberType) != (*unitMemberType)) {
614                     error(infoSink, "Types must match:");
615                     infoSink.info << "    " << memberType->getFieldName() << ": ";
616                     infoSink.info << "\"" << memberType->getCompleteString() << "\" versus ";
617                     infoSink.info << "\"" << unitMemberType->getCompleteString() << "\"\n";
618                 }
619
620                 memberIndexUpdates[i] = j;
621             }
622         }
623         if (merge) {
624             memberList->push_back((*unitMemberList)[i]);
625             memberIndexUpdates[i] = (unsigned int)memberList->size() - 1;
626         }
627     }
628
629     // update symbol node in unit tree,
630     // and other nodes that may reference it
631     class TMergeBlockTraverser : public TIntermTraverser {
632     public:
633         TMergeBlockTraverser(const TIntermSymbol* newSym)
634             : newSymbol(newSym), unitType(nullptr), unit(nullptr), memberIndexUpdates(nullptr)
635         {
636         }
637         TMergeBlockTraverser(const TIntermSymbol* newSym, const glslang::TType* unitType, glslang::TIntermediate* unit,
638                              const std::map<unsigned int, unsigned int>* memberIdxUpdates)
639             : newSymbol(newSym), unitType(unitType), unit(unit), memberIndexUpdates(memberIdxUpdates)
640         {
641         }
642         virtual ~TMergeBlockTraverser() {}
643
644         const TIntermSymbol* newSymbol;
645         const glslang::TType* unitType; // copy of original type
646         glslang::TIntermediate* unit;   // intermediate that is being updated
647         const std::map<unsigned int, unsigned int>* memberIndexUpdates;
648
649         virtual void visitSymbol(TIntermSymbol* symbol)
650         {
651             if (newSymbol->getAccessName() == symbol->getAccessName() &&
652                 newSymbol->getQualifier().getBlockStorage() == symbol->getQualifier().getBlockStorage()) {
653                 // Each symbol node may have a local copy of the block structure.
654                 // Update those structures to match the new one post-merge
655                 *(symbol->getWritableType().getWritableStruct()) = *(newSymbol->getType().getStruct());
656             }
657         }
658
659         virtual bool visitBinary(TVisit, glslang::TIntermBinary* node)
660         {
661             if (!unit || !unitType || !memberIndexUpdates || memberIndexUpdates->empty())
662                 return true;
663
664             if (node->getOp() == EOpIndexDirectStruct && node->getLeft()->getType() == *unitType) {
665                 // this is a dereference to a member of the block since the
666                 // member list changed, need to update this to point to the
667                 // right index
668                 assert(node->getRight()->getAsConstantUnion());
669
670                 glslang::TIntermConstantUnion* constNode = node->getRight()->getAsConstantUnion();
671                 unsigned int memberIdx = constNode->getConstArray()[0].getUConst();
672                 unsigned int newIdx = memberIndexUpdates->at(memberIdx);
673                 TIntermTyped* newConstNode = unit->addConstantUnion(newIdx, node->getRight()->getLoc());
674
675                 node->setRight(newConstNode);
676                 delete constNode;
677
678                 return true;
679             }
680             return true;
681         }
682     };
683
684     // 'this' may have symbols that are using the old block structure, so traverse the tree to update those
685     // in 'visitSymbol'
686     TMergeBlockTraverser finalLinkTraverser(block);
687     getTreeRoot()->traverse(&finalLinkTraverser);
688
689     // The 'unit' intermediate needs the block structures update, but also structure entry indices 
690     // may have changed from the old block to the new one that it was merged into, so update those
691     // in 'visitBinary'
692     TType unitType;
693     unitType.shallowCopy(unitBlock->getType());
694     TMergeBlockTraverser unitFinalLinkTraverser(block, &unitType, unit, &memberIndexUpdates);
695     unit->getTreeRoot()->traverse(&unitFinalLinkTraverser);
696
697     // update the member list
698     (*unitMemberList) = (*memberList);
699 }
700
701 //
702 // Merge the linker objects from unitLinkerObjects into linkerObjects.
703 // Duplication is expected and filtered out, but contradictions are an error.
704 //
705 void TIntermediate::mergeLinkerObjects(TInfoSink& infoSink, TIntermSequence& linkerObjects, const TIntermSequence& unitLinkerObjects, EShLanguage unitStage)
706 {
707     // Error check and merge the linker objects (duplicates should not be created)
708     std::size_t initialNumLinkerObjects = linkerObjects.size();
709     for (unsigned int unitLinkObj = 0; unitLinkObj < unitLinkerObjects.size(); ++unitLinkObj) {
710         bool merge = true;
711         for (std::size_t linkObj = 0; linkObj < initialNumLinkerObjects; ++linkObj) {
712             TIntermSymbol* symbol = linkerObjects[linkObj]->getAsSymbolNode();
713             TIntermSymbol* unitSymbol = unitLinkerObjects[unitLinkObj]->getAsSymbolNode();
714             assert(symbol && unitSymbol);
715
716             bool isSameSymbol = false;
717             // If they are both blocks in the same shader interface,
718             // match by the block-name, not the identifier name.
719             if (symbol->getType().getBasicType() == EbtBlock && unitSymbol->getType().getBasicType() == EbtBlock) {
720                 if (isSameInterface(symbol, getStage(), unitSymbol, unitStage)) {
721                     isSameSymbol = symbol->getType().getTypeName() == unitSymbol->getType().getTypeName();
722                 }
723             }
724             else if (symbol->getName() == unitSymbol->getName())
725                 isSameSymbol = true;
726
727             if (isSameSymbol) {
728                 // filter out copy
729                 merge = false;
730
731                 // but if one has an initializer and the other does not, update
732                 // the initializer
733                 if (symbol->getConstArray().empty() && ! unitSymbol->getConstArray().empty())
734                     symbol->setConstArray(unitSymbol->getConstArray());
735
736                 // Similarly for binding
737                 if (! symbol->getQualifier().hasBinding() && unitSymbol->getQualifier().hasBinding())
738                     symbol->getQualifier().layoutBinding = unitSymbol->getQualifier().layoutBinding;
739
740                 // Similarly for location
741                 if (!symbol->getQualifier().hasLocation() && unitSymbol->getQualifier().hasLocation()) {
742                     symbol->getQualifier().layoutLocation = unitSymbol->getQualifier().layoutLocation;
743                 }
744
745                 // Update implicit array sizes
746                 mergeImplicitArraySizes(symbol->getWritableType(), unitSymbol->getType());
747
748                 // Check for consistent types/qualification/initializers etc.
749                 mergeErrorCheck(infoSink, *symbol, *unitSymbol, unitStage);
750             }
751             // If different symbols, verify they arn't push_constant since there can only be one per stage
752             else if (symbol->getQualifier().isPushConstant() && unitSymbol->getQualifier().isPushConstant() && getStage() == unitStage)
753                 error(infoSink, "Only one push_constant block is allowed per stage");
754         }
755         if (merge) {
756             linkerObjects.push_back(unitLinkerObjects[unitLinkObj]);
757
758             // for anonymous blocks, check that their members don't conflict with other names
759             if (unitLinkerObjects[unitLinkObj]->getAsSymbolNode()->getBasicType() == EbtBlock &&
760                 IsAnonymous(unitLinkerObjects[unitLinkObj]->getAsSymbolNode()->getName())) {
761                 for (std::size_t linkObj = 0; linkObj < initialNumLinkerObjects; ++linkObj) {
762                     TIntermSymbol* symbol = linkerObjects[linkObj]->getAsSymbolNode();
763                     TIntermSymbol* unitSymbol = unitLinkerObjects[unitLinkObj]->getAsSymbolNode();
764                     assert(symbol && unitSymbol);
765
766                     auto checkName = [this, unitSymbol, &infoSink](const TString& name) {
767                         for (unsigned int i = 0; i < unitSymbol->getType().getStruct()->size(); ++i) {
768                             if (name == (*unitSymbol->getType().getStruct())[i].type->getFieldName()
769                                 && !((*unitSymbol->getType().getStruct())[i].type->getQualifier().hasLocation()
770                                     || unitSymbol->getType().getQualifier().hasLocation())
771                                 ) {
772                                 error(infoSink, "Anonymous member name used for global variable or other anonymous member: ");
773                                 infoSink.info << (*unitSymbol->getType().getStruct())[i].type->getCompleteString() << "\n";
774                             }
775                         }
776                     };
777
778                     if (isSameInterface(symbol, getStage(), unitSymbol, unitStage)) {
779                         checkName(symbol->getName());
780
781                         // check members of other anonymous blocks
782                         if (symbol->getBasicType() == EbtBlock && IsAnonymous(symbol->getName())) {
783                             for (unsigned int i = 0; i < symbol->getType().getStruct()->size(); ++i) {
784                                 checkName((*symbol->getType().getStruct())[i].type->getFieldName());
785                             }
786                         }
787                     }
788                 }
789             }
790         }
791     }
792 }
793
794 // TODO 4.5 link functionality: cull distance array size checking
795
796 // Recursively merge the implicit array sizes through the objects' respective type trees.
797 void TIntermediate::mergeImplicitArraySizes(TType& type, const TType& unitType)
798 {
799     if (type.isUnsizedArray()) {
800         if (unitType.isUnsizedArray()) {
801             type.updateImplicitArraySize(unitType.getImplicitArraySize());
802             if (unitType.isArrayVariablyIndexed())
803                 type.setArrayVariablyIndexed();
804         } else if (unitType.isSizedArray())
805             type.changeOuterArraySize(unitType.getOuterArraySize());
806     }
807
808     // Type mismatches are caught and reported after this, just be careful for now.
809     if (! type.isStruct() || ! unitType.isStruct() || type.getStruct()->size() != unitType.getStruct()->size())
810         return;
811
812     for (int i = 0; i < (int)type.getStruct()->size(); ++i)
813         mergeImplicitArraySizes(*(*type.getStruct())[i].type, *(*unitType.getStruct())[i].type);
814 }
815
816 //
817 // Compare two global objects from two compilation units and see if they match
818 // well enough.  Rules can be different for intra- vs. cross-stage matching.
819 //
820 // This function only does one of intra- or cross-stage matching per call.
821 //
822 void TIntermediate::mergeErrorCheck(TInfoSink& infoSink, const TIntermSymbol& symbol, const TIntermSymbol& unitSymbol, EShLanguage unitStage)
823 {
824 #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
825     bool crossStage = getStage() != unitStage;
826     bool writeTypeComparison = false;
827
828     // Types have to match
829     {
830         // but, we make an exception if one is an implicit array and the other is sized
831         // or if the array sizes differ because of the extra array dimension on some in/out boundaries
832         bool arraysMatch = false;
833         if (isIoResizeArray(symbol.getType(), getStage()) || isIoResizeArray(unitSymbol.getType(), unitStage)) {
834             // if the arrays have an extra dimension because of the stage.
835             // compare dimensions while ignoring the outer dimension
836             unsigned int firstDim = isIoResizeArray(symbol.getType(), getStage()) ? 1 : 0;
837             unsigned int numDim = symbol.getArraySizes()
838                 ? symbol.getArraySizes()->getNumDims() : 0;
839             unsigned int unitFirstDim = isIoResizeArray(unitSymbol.getType(), unitStage) ? 1 : 0;
840             unsigned int unitNumDim = unitSymbol.getArraySizes()
841                 ? unitSymbol.getArraySizes()->getNumDims() : 0;
842             arraysMatch = (numDim - firstDim) == (unitNumDim - unitFirstDim);
843             // check that array sizes match as well
844             for (unsigned int i = 0; i < (numDim - firstDim) && arraysMatch; i++) {
845                 if (symbol.getArraySizes()->getDimSize(firstDim + i) !=
846                     unitSymbol.getArraySizes()->getDimSize(unitFirstDim + i)) {
847                     arraysMatch = false;
848                     break;
849                 }
850             }
851         }
852         else {
853             arraysMatch = symbol.getType().sameArrayness(unitSymbol.getType()) ||
854                 (symbol.getType().isArray() && unitSymbol.getType().isArray() &&
855                 (symbol.getType().isUnsizedArray() || unitSymbol.getType().isUnsizedArray()));
856         }
857
858         if (!symbol.getType().sameElementType(unitSymbol.getType()) ||
859             !symbol.getType().sameTypeParameters(unitSymbol.getType()) ||
860             !arraysMatch ) {
861             writeTypeComparison = true;
862             error(infoSink, "Types must match:");
863         }
864     }
865
866     // Interface block  member-wise layout qualifiers have to match
867     if (symbol.getType().getBasicType() == EbtBlock && unitSymbol.getType().getBasicType() == EbtBlock &&
868         symbol.getType().getStruct() && unitSymbol.getType().getStruct() &&
869         symbol.getType().sameStructType(unitSymbol.getType())) {
870         unsigned int li = 0;
871         unsigned int ri = 0;
872         while (li < symbol.getType().getStruct()->size() && ri < unitSymbol.getType().getStruct()->size()) {
873             if ((*symbol.getType().getStruct())[li].type->hiddenMember()) {
874                 ++li;
875                 continue;
876             }
877             if ((*unitSymbol.getType().getStruct())[ri].type->hiddenMember()) {
878                 ++ri;
879                 continue;
880             }
881             const TQualifier& qualifier = (*symbol.getType().getStruct())[li].type->getQualifier();
882             const TQualifier & unitQualifier = (*unitSymbol.getType().getStruct())[ri].type->getQualifier();
883             if (qualifier.layoutMatrix     != unitQualifier.layoutMatrix ||
884                 qualifier.layoutOffset     != unitQualifier.layoutOffset ||
885                 qualifier.layoutAlign      != unitQualifier.layoutAlign ||
886                 qualifier.layoutLocation   != unitQualifier.layoutLocation ||
887                 qualifier.layoutComponent  != unitQualifier.layoutComponent) {
888                 error(infoSink, "Interface block member layout qualifiers must match:");
889                 writeTypeComparison = true;
890             }
891             ++li;
892             ++ri;
893         }
894     }
895
896     bool isInOut = crossStage &&
897                    ((symbol.getQualifier().storage == EvqVaryingIn && unitSymbol.getQualifier().storage == EvqVaryingOut) ||
898                    (symbol.getQualifier().storage == EvqVaryingOut && unitSymbol.getQualifier().storage == EvqVaryingIn));
899
900     // Qualifiers have to (almost) match
901     // Storage...
902     if (!isInOut && symbol.getQualifier().storage != unitSymbol.getQualifier().storage) {
903         error(infoSink, "Storage qualifiers must match:");
904         writeTypeComparison = true;
905     }
906
907     // Uniform and buffer blocks must either both have an instance name, or
908     // must both be anonymous. The names don't need to match though.
909     if (symbol.getQualifier().isUniformOrBuffer() &&
910         (IsAnonymous(symbol.getName()) != IsAnonymous(unitSymbol.getName()))) {
911         error(infoSink, "Matched Uniform or Storage blocks must all be anonymous,"
912                         " or all be named:");
913         writeTypeComparison = true;
914     }
915
916     if (symbol.getQualifier().storage == unitSymbol.getQualifier().storage &&
917         (IsAnonymous(symbol.getName()) != IsAnonymous(unitSymbol.getName()) ||
918          (!IsAnonymous(symbol.getName()) && symbol.getName() != unitSymbol.getName()))) {
919         warn(infoSink, "Matched shader interfaces are using different instance names.");
920         writeTypeComparison = true;
921     }
922
923     // Precision...
924     if (!isInOut && symbol.getQualifier().precision != unitSymbol.getQualifier().precision) {
925         error(infoSink, "Precision qualifiers must match:");
926         writeTypeComparison = true;
927     }
928
929     // Invariance...
930     if (! crossStage && symbol.getQualifier().invariant != unitSymbol.getQualifier().invariant) {
931         error(infoSink, "Presence of invariant qualifier must match:");
932         writeTypeComparison = true;
933     }
934
935     // Precise...
936     if (! crossStage && symbol.getQualifier().isNoContraction() != unitSymbol.getQualifier().isNoContraction()) {
937         error(infoSink, "Presence of precise qualifier must match:");
938         writeTypeComparison = true;
939     }
940
941     // Auxiliary and interpolation...
942     // "interpolation qualification (e.g., flat) and auxiliary qualification (e.g. centroid) may differ.  
943     //  These mismatches are allowed between any pair of stages ...
944     //  those provided in the fragment shader supersede those provided in previous stages."
945     if (!crossStage &&
946         (symbol.getQualifier().centroid  != unitSymbol.getQualifier().centroid ||
947         symbol.getQualifier().smooth    != unitSymbol.getQualifier().smooth ||
948         symbol.getQualifier().flat      != unitSymbol.getQualifier().flat ||
949         symbol.getQualifier().isSample()!= unitSymbol.getQualifier().isSample() ||
950         symbol.getQualifier().isPatch() != unitSymbol.getQualifier().isPatch() ||
951         symbol.getQualifier().isNonPerspective() != unitSymbol.getQualifier().isNonPerspective())) {
952         error(infoSink, "Interpolation and auxiliary storage qualifiers must match:");
953         writeTypeComparison = true;
954     }
955
956     // Memory...
957     if (symbol.getQualifier().coherent          != unitSymbol.getQualifier().coherent ||
958         symbol.getQualifier().devicecoherent    != unitSymbol.getQualifier().devicecoherent ||
959         symbol.getQualifier().queuefamilycoherent  != unitSymbol.getQualifier().queuefamilycoherent ||
960         symbol.getQualifier().workgroupcoherent != unitSymbol.getQualifier().workgroupcoherent ||
961         symbol.getQualifier().subgroupcoherent  != unitSymbol.getQualifier().subgroupcoherent ||
962         symbol.getQualifier().shadercallcoherent!= unitSymbol.getQualifier().shadercallcoherent ||
963         symbol.getQualifier().nonprivate        != unitSymbol.getQualifier().nonprivate ||
964         symbol.getQualifier().volatil           != unitSymbol.getQualifier().volatil ||
965         symbol.getQualifier().restrict          != unitSymbol.getQualifier().restrict ||
966         symbol.getQualifier().readonly          != unitSymbol.getQualifier().readonly ||
967         symbol.getQualifier().writeonly         != unitSymbol.getQualifier().writeonly) {
968         error(infoSink, "Memory qualifiers must match:");
969         writeTypeComparison = true;
970     }
971
972     // Layouts...
973     // TODO: 4.4 enhanced layouts: Generalize to include offset/align: current spec
974     //       requires separate user-supplied offset from actual computed offset, but
975     //       current implementation only has one offset.
976     if (symbol.getQualifier().layoutMatrix    != unitSymbol.getQualifier().layoutMatrix ||
977         symbol.getQualifier().layoutPacking   != unitSymbol.getQualifier().layoutPacking ||
978         (symbol.getQualifier().hasLocation() && unitSymbol.getQualifier().hasLocation() && symbol.getQualifier().layoutLocation != unitSymbol.getQualifier().layoutLocation) ||
979         symbol.getQualifier().layoutComponent != unitSymbol.getQualifier().layoutComponent ||
980         symbol.getQualifier().layoutIndex     != unitSymbol.getQualifier().layoutIndex ||
981         (symbol.getQualifier().hasBinding() && unitSymbol.getQualifier().hasBinding() && symbol.getQualifier().layoutBinding != unitSymbol.getQualifier().layoutBinding) ||
982         (symbol.getQualifier().hasBinding() && (symbol.getQualifier().layoutOffset != unitSymbol.getQualifier().layoutOffset))) {
983         error(infoSink, "Layout qualification must match:");
984         writeTypeComparison = true;
985     }
986
987     // Initializers have to match, if both are present, and if we don't already know the types don't match
988     if (! writeTypeComparison) {
989         if (! symbol.getConstArray().empty() && ! unitSymbol.getConstArray().empty()) {
990             if (symbol.getConstArray() != unitSymbol.getConstArray()) {
991                 error(infoSink, "Initializers must match:");
992                 infoSink.info << "    " << symbol.getName() << "\n";
993             }
994         }
995     }
996
997     if (writeTypeComparison) {
998         infoSink.info << "    " << symbol.getName() << ": \"" << symbol.getType().getCompleteString() << "\" versus ";
999         if (symbol.getName() != unitSymbol.getName())
1000             infoSink.info << unitSymbol.getName() << ": ";
1001
1002         infoSink.info << "\"" << unitSymbol.getType().getCompleteString() << "\"\n";
1003     }
1004 #endif
1005 }
1006
1007 void TIntermediate::sharedBlockCheck(TInfoSink& infoSink)
1008 {
1009     bool has_shared_block = false;
1010     bool has_shared_non_block = false;
1011     TIntermSequence& linkObjects = findLinkerObjects()->getSequence();
1012     for (size_t i = 0; i < linkObjects.size(); ++i) {
1013         const TType& type = linkObjects[i]->getAsTyped()->getType();
1014         const TQualifier& qualifier = type.getQualifier();
1015         if (qualifier.storage == glslang::EvqShared) {
1016             if (type.getBasicType() == glslang::EbtBlock)
1017                 has_shared_block = true;
1018             else
1019                 has_shared_non_block = true;
1020         }
1021     }
1022     if (has_shared_block && has_shared_non_block)
1023         error(infoSink, "cannot mix use of shared variables inside and outside blocks");
1024 }
1025
1026 //
1027 // Do final link-time error checking of a complete (merged) intermediate representation.
1028 // (Much error checking was done during merging).
1029 //
1030 // Also, lock in defaults of things not set, including array sizes.
1031 //
1032 void TIntermediate::finalCheck(TInfoSink& infoSink, bool keepUncalled)
1033 {
1034     if (getTreeRoot() == nullptr)
1035         return;
1036
1037     if (numEntryPoints < 1) {
1038         if (getSource() == EShSourceGlsl)
1039             error(infoSink, "Missing entry point: Each stage requires one entry point");
1040         else
1041             warn(infoSink, "Entry point not found");
1042     }
1043
1044     // recursion and missing body checking
1045     checkCallGraphCycles(infoSink);
1046     checkCallGraphBodies(infoSink, keepUncalled);
1047
1048     // overlap/alias/missing I/O, etc.
1049     inOutLocationCheck(infoSink);
1050
1051 #ifndef GLSLANG_WEB
1052     if (getNumPushConstants() > 1)
1053         error(infoSink, "Only one push_constant block is allowed per stage");
1054
1055     // invocations
1056     if (invocations == TQualifier::layoutNotSet)
1057         invocations = 1;
1058
1059     if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipVertex"))
1060         error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
1061     if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_ClipVertex"))
1062         error(infoSink, "Can only use one of gl_CullDistance or gl_ClipVertex (gl_ClipDistance is preferred)");
1063
1064     if (userOutputUsed() && (inIoAccessed("gl_FragColor") || inIoAccessed("gl_FragData")))
1065         error(infoSink, "Cannot use gl_FragColor or gl_FragData when using user-defined outputs");
1066     if (inIoAccessed("gl_FragColor") && inIoAccessed("gl_FragData"))
1067         error(infoSink, "Cannot use both gl_FragColor and gl_FragData");
1068
1069     for (size_t b = 0; b < xfbBuffers.size(); ++b) {
1070         if (xfbBuffers[b].contains64BitType)
1071             RoundToPow2(xfbBuffers[b].implicitStride, 8);
1072         else if (xfbBuffers[b].contains32BitType)
1073             RoundToPow2(xfbBuffers[b].implicitStride, 4);
1074         else if (xfbBuffers[b].contains16BitType)
1075             RoundToPow2(xfbBuffers[b].implicitStride, 2);
1076
1077         // "It is a compile-time or link-time error to have
1078         // any xfb_offset that overflows xfb_stride, whether stated on declarations before or after the xfb_stride, or
1079         // in different compilation units. While xfb_stride can be declared multiple times for the same buffer, it is a
1080         // compile-time or link-time error to have different values specified for the stride for the same buffer."
1081         if (xfbBuffers[b].stride != TQualifier::layoutXfbStrideEnd && xfbBuffers[b].implicitStride > xfbBuffers[b].stride) {
1082             error(infoSink, "xfb_stride is too small to hold all buffer entries:");
1083             infoSink.info.prefix(EPrefixError);
1084             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << ", minimum stride needed: " << xfbBuffers[b].implicitStride << "\n";
1085         }
1086         if (xfbBuffers[b].stride == TQualifier::layoutXfbStrideEnd)
1087             xfbBuffers[b].stride = xfbBuffers[b].implicitStride;
1088
1089         // "If the buffer is capturing any
1090         // outputs with double-precision or 64-bit integer components, the stride must be a multiple of 8, otherwise it must be a
1091         // multiple of 4, or a compile-time or link-time error results."
1092         if (xfbBuffers[b].contains64BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 8)) {
1093             error(infoSink, "xfb_stride must be multiple of 8 for buffer holding a double or 64-bit integer:");
1094             infoSink.info.prefix(EPrefixError);
1095             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
1096         } else if (xfbBuffers[b].contains32BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 4)) {
1097             error(infoSink, "xfb_stride must be multiple of 4:");
1098             infoSink.info.prefix(EPrefixError);
1099             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
1100         }
1101         // "If the buffer is capturing any
1102         // outputs with half-precision or 16-bit integer components, the stride must be a multiple of 2"
1103         else if (xfbBuffers[b].contains16BitType && ! IsMultipleOfPow2(xfbBuffers[b].stride, 2)) {
1104             error(infoSink, "xfb_stride must be multiple of 2 for buffer holding a half float or 16-bit integer:");
1105             infoSink.info.prefix(EPrefixError);
1106             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", xfb_stride " << xfbBuffers[b].stride << "\n";
1107         }
1108
1109         // "The resulting stride (implicit or explicit), when divided by 4, must be less than or equal to the
1110         // implementation-dependent constant gl_MaxTransformFeedbackInterleavedComponents."
1111         if (xfbBuffers[b].stride > (unsigned int)(4 * resources->maxTransformFeedbackInterleavedComponents)) {
1112             error(infoSink, "xfb_stride is too large:");
1113             infoSink.info.prefix(EPrefixError);
1114             infoSink.info << "    xfb_buffer " << (unsigned int)b << ", components (1/4 stride) needed are " << xfbBuffers[b].stride/4 << ", gl_MaxTransformFeedbackInterleavedComponents is " << resources->maxTransformFeedbackInterleavedComponents << "\n";
1115         }
1116     }
1117
1118     switch (language) {
1119     case EShLangVertex:
1120         break;
1121     case EShLangTessControl:
1122         if (vertices == TQualifier::layoutNotSet)
1123             error(infoSink, "At least one shader must specify an output layout(vertices=...)");
1124         break;
1125     case EShLangTessEvaluation:
1126         if (getSource() == EShSourceGlsl) {
1127             if (inputPrimitive == ElgNone)
1128                 error(infoSink, "At least one shader must specify an input layout primitive");
1129             if (vertexSpacing == EvsNone)
1130                 vertexSpacing = EvsEqual;
1131             if (vertexOrder == EvoNone)
1132                 vertexOrder = EvoCcw;
1133         }
1134         break;
1135     case EShLangGeometry:
1136         if (inputPrimitive == ElgNone)
1137             error(infoSink, "At least one shader must specify an input layout primitive");
1138         if (outputPrimitive == ElgNone)
1139             error(infoSink, "At least one shader must specify an output layout primitive");
1140         if (vertices == TQualifier::layoutNotSet)
1141             error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
1142         break;
1143     case EShLangFragment:
1144         // for GL_ARB_post_depth_coverage, EarlyFragmentTest is set automatically in 
1145         // ParseHelper.cpp. So if we reach here, this must be GL_EXT_post_depth_coverage 
1146         // requiring explicit early_fragment_tests
1147         if (getPostDepthCoverage() && !getEarlyFragmentTests())
1148             error(infoSink, "post_depth_coverage requires early_fragment_tests");
1149         break;
1150     case EShLangCompute:
1151         sharedBlockCheck(infoSink);
1152         break;
1153     case EShLangRayGen:
1154     case EShLangIntersect:
1155     case EShLangAnyHit:
1156     case EShLangClosestHit:
1157     case EShLangMiss:
1158     case EShLangCallable:
1159         if (numShaderRecordBlocks > 1)
1160             error(infoSink, "Only one shaderRecordNV buffer block is allowed per stage");
1161         break;
1162     case EShLangMeshNV:
1163         // NV_mesh_shader doesn't allow use of both single-view and per-view builtins.
1164         if (inIoAccessed("gl_Position") && inIoAccessed("gl_PositionPerViewNV"))
1165             error(infoSink, "Can only use one of gl_Position or gl_PositionPerViewNV");
1166         if (inIoAccessed("gl_ClipDistance") && inIoAccessed("gl_ClipDistancePerViewNV"))
1167             error(infoSink, "Can only use one of gl_ClipDistance or gl_ClipDistancePerViewNV");
1168         if (inIoAccessed("gl_CullDistance") && inIoAccessed("gl_CullDistancePerViewNV"))
1169             error(infoSink, "Can only use one of gl_CullDistance or gl_CullDistancePerViewNV");
1170         if (inIoAccessed("gl_Layer") && inIoAccessed("gl_LayerPerViewNV"))
1171             error(infoSink, "Can only use one of gl_Layer or gl_LayerPerViewNV");
1172         if (inIoAccessed("gl_ViewportMask") && inIoAccessed("gl_ViewportMaskPerViewNV"))
1173             error(infoSink, "Can only use one of gl_ViewportMask or gl_ViewportMaskPerViewNV");
1174         if (outputPrimitive == ElgNone)
1175             error(infoSink, "At least one shader must specify an output layout primitive");
1176         if (vertices == TQualifier::layoutNotSet)
1177             error(infoSink, "At least one shader must specify a layout(max_vertices = value)");
1178         if (primitives == TQualifier::layoutNotSet)
1179             error(infoSink, "At least one shader must specify a layout(max_primitives = value)");
1180         // fall through
1181     case EShLangTaskNV:
1182         if (numTaskNVBlocks > 1)
1183             error(infoSink, "Only one taskNV interface block is allowed per shader");
1184         sharedBlockCheck(infoSink);
1185         break;
1186     default:
1187         error(infoSink, "Unknown Stage.");
1188         break;
1189     }
1190
1191     // Process the tree for any node-specific work.
1192     class TFinalLinkTraverser : public TIntermTraverser {
1193     public:
1194         TFinalLinkTraverser() { }
1195         virtual ~TFinalLinkTraverser() { }
1196
1197         virtual void visitSymbol(TIntermSymbol* symbol)
1198         {
1199             // Implicitly size arrays.
1200             // If an unsized array is left as unsized, it effectively
1201             // becomes run-time sized.
1202             symbol->getWritableType().adoptImplicitArraySizes(false);
1203         }
1204     } finalLinkTraverser;
1205
1206     treeRoot->traverse(&finalLinkTraverser);
1207 #endif
1208 }
1209
1210 //
1211 // See if the call graph contains any static recursion, which is disallowed
1212 // by the specification.
1213 //
1214 void TIntermediate::checkCallGraphCycles(TInfoSink& infoSink)
1215 {
1216     // Clear fields we'll use for this.
1217     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1218         call->visited = false;
1219         call->currentPath = false;
1220         call->errorGiven = false;
1221     }
1222
1223     //
1224     // Loop, looking for a new connected subgraph.  One subgraph is handled per loop iteration.
1225     //
1226
1227     TCall* newRoot;
1228     do {
1229         // See if we have unvisited parts of the graph.
1230         newRoot = 0;
1231         for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1232             if (! call->visited) {
1233                 newRoot = &(*call);
1234                 break;
1235             }
1236         }
1237
1238         // If not, we are done.
1239         if (! newRoot)
1240             break;
1241
1242         // Otherwise, we found a new subgraph, process it:
1243         // See what all can be reached by this new root, and if any of
1244         // that is recursive.  This is done by depth-first traversals, seeing
1245         // if a new call is found that was already in the currentPath (a back edge),
1246         // thereby detecting recursion.
1247         std::list<TCall*> stack;
1248         newRoot->currentPath = true; // currentPath will be true iff it is on the stack
1249         stack.push_back(newRoot);
1250         while (! stack.empty()) {
1251             // get a caller
1252             TCall* call = stack.back();
1253
1254             // Add to the stack just one callee.
1255             // This algorithm always terminates, because only !visited and !currentPath causes a push
1256             // and all pushes change currentPath to true, and all pops change visited to true.
1257             TGraph::iterator child = callGraph.begin();
1258             for (; child != callGraph.end(); ++child) {
1259
1260                 // If we already visited this node, its whole subgraph has already been processed, so skip it.
1261                 if (child->visited)
1262                     continue;
1263
1264                 if (call->callee == child->caller) {
1265                     if (child->currentPath) {
1266                         // Then, we found a back edge
1267                         if (! child->errorGiven) {
1268                             error(infoSink, "Recursion detected:");
1269                             infoSink.info << "    " << call->callee << " calling " << child->callee << "\n";
1270                             child->errorGiven = true;
1271                             recursive = true;
1272                         }
1273                     } else {
1274                         child->currentPath = true;
1275                         stack.push_back(&(*child));
1276                         break;
1277                     }
1278                 }
1279             }
1280             if (child == callGraph.end()) {
1281                 // no more callees, we bottomed out, never look at this node again
1282                 stack.back()->currentPath = false;
1283                 stack.back()->visited = true;
1284                 stack.pop_back();
1285             }
1286         }  // end while, meaning nothing left to process in this subtree
1287
1288     } while (newRoot);  // redundant loop check; should always exit via the 'break' above
1289 }
1290
1291 //
1292 // See which functions are reachable from the entry point and which have bodies.
1293 // Reachable ones with missing bodies are errors.
1294 // Unreachable bodies are dead code.
1295 //
1296 void TIntermediate::checkCallGraphBodies(TInfoSink& infoSink, bool keepUncalled)
1297 {
1298     // Clear fields we'll use for this.
1299     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1300         call->visited = false;
1301         call->calleeBodyPosition = -1;
1302     }
1303
1304     // The top level of the AST includes function definitions (bodies).
1305     // Compare these to function calls in the call graph.
1306     // We'll end up knowing which have bodies, and if so,
1307     // how to map the call-graph node to the location in the AST.
1308     TIntermSequence &functionSequence = getTreeRoot()->getAsAggregate()->getSequence();
1309     std::vector<bool> reachable(functionSequence.size(), true); // so that non-functions are reachable
1310     for (int f = 0; f < (int)functionSequence.size(); ++f) {
1311         glslang::TIntermAggregate* node = functionSequence[f]->getAsAggregate();
1312         if (node && (node->getOp() == glslang::EOpFunction)) {
1313             if (node->getName().compare(getEntryPointMangledName().c_str()) != 0)
1314                 reachable[f] = false; // so that function bodies are unreachable, until proven otherwise
1315             for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1316                 if (call->callee == node->getName())
1317                     call->calleeBodyPosition = f;
1318             }
1319         }
1320     }
1321
1322     // Start call-graph traversal by visiting the entry point nodes.
1323     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1324         if (call->caller.compare(getEntryPointMangledName().c_str()) == 0)
1325             call->visited = true;
1326     }
1327
1328     // Propagate 'visited' through the call-graph to every part of the graph it
1329     // can reach (seeded with the entry-point setting above).
1330     bool changed;
1331     do {
1332         changed = false;
1333         for (auto call1 = callGraph.begin(); call1 != callGraph.end(); ++call1) {
1334             if (call1->visited) {
1335                 for (TGraph::iterator call2 = callGraph.begin(); call2 != callGraph.end(); ++call2) {
1336                     if (! call2->visited) {
1337                         if (call1->callee == call2->caller) {
1338                             changed = true;
1339                             call2->visited = true;
1340                         }
1341                     }
1342                 }
1343             }
1344         }
1345     } while (changed);
1346
1347     // Any call-graph node set to visited but without a callee body is an error.
1348     for (TGraph::iterator call = callGraph.begin(); call != callGraph.end(); ++call) {
1349         if (call->visited) {
1350             if (call->calleeBodyPosition == -1) {
1351                 error(infoSink, "No function definition (body) found: ");
1352                 infoSink.info << "    " << call->callee << "\n";
1353             } else
1354                 reachable[call->calleeBodyPosition] = true;
1355         }
1356     }
1357
1358     // Bodies in the AST not reached by the call graph are dead;
1359     // clear them out, since they can't be reached and also can't
1360     // be translated further due to possibility of being ill defined.
1361     if (! keepUncalled) {
1362         for (int f = 0; f < (int)functionSequence.size(); ++f) {
1363             if (! reachable[f])
1364                 functionSequence[f] = nullptr;
1365         }
1366         functionSequence.erase(std::remove(functionSequence.begin(), functionSequence.end(), nullptr), functionSequence.end());
1367     }
1368 }
1369
1370 //
1371 // Satisfy rules for location qualifiers on inputs and outputs
1372 //
1373 void TIntermediate::inOutLocationCheck(TInfoSink& infoSink)
1374 {
1375     // ES 3.0 requires all outputs to have location qualifiers if there is more than one output
1376     bool fragOutWithNoLocation = false;
1377     int numFragOut = 0;
1378
1379     // TODO: linker functionality: location collision checking
1380
1381     TIntermSequence& linkObjects = findLinkerObjects()->getSequence();
1382     for (size_t i = 0; i < linkObjects.size(); ++i) {
1383         const TType& type = linkObjects[i]->getAsTyped()->getType();
1384         const TQualifier& qualifier = type.getQualifier();
1385         if (language == EShLangFragment) {
1386             if (qualifier.storage == EvqVaryingOut && qualifier.builtIn == EbvNone) {
1387                 ++numFragOut;
1388                 if (!qualifier.hasAnyLocation())
1389                     fragOutWithNoLocation = true;
1390             }
1391         }
1392     }
1393
1394     if (isEsProfile()) {
1395         if (numFragOut > 1 && fragOutWithNoLocation)
1396             error(infoSink, "when more than one fragment shader output, all must have location qualifiers");
1397     }
1398 }
1399
1400 TIntermAggregate* TIntermediate::findLinkerObjects() const
1401 {
1402     // Get the top-level globals
1403     TIntermSequence& globals = treeRoot->getAsAggregate()->getSequence();
1404
1405     // Get the last member of the sequences, expected to be the linker-object lists
1406     assert(globals.back()->getAsAggregate()->getOp() == EOpLinkerObjects);
1407
1408     return globals.back()->getAsAggregate();
1409 }
1410
1411 // See if a variable was both a user-declared output and used.
1412 // Note: the spec discusses writing to one, but this looks at read or write, which
1413 // is more useful, and perhaps the spec should be changed to reflect that.
1414 bool TIntermediate::userOutputUsed() const
1415 {
1416     const TIntermSequence& linkerObjects = findLinkerObjects()->getSequence();
1417
1418     bool found = false;
1419     for (size_t i = 0; i < linkerObjects.size(); ++i) {
1420         const TIntermSymbol& symbolNode = *linkerObjects[i]->getAsSymbolNode();
1421         if (symbolNode.getQualifier().storage == EvqVaryingOut &&
1422             symbolNode.getName().compare(0, 3, "gl_") != 0 &&
1423             inIoAccessed(symbolNode.getName())) {
1424             found = true;
1425             break;
1426         }
1427     }
1428
1429     return found;
1430 }
1431
1432 // Accumulate locations used for inputs, outputs, and uniforms, payload and callable data
1433 // and check for collisions as the accumulation is done.
1434 //
1435 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1436 //
1437 // typeCollision is set to true if there is no direct collision, but the types in the same location
1438 // are different.
1439 //
1440 int TIntermediate::addUsedLocation(const TQualifier& qualifier, const TType& type, bool& typeCollision)
1441 {
1442     typeCollision = false;
1443
1444     int set;
1445     int setRT;
1446     if (qualifier.isPipeInput())
1447         set = 0;
1448     else if (qualifier.isPipeOutput())
1449         set = 1;
1450     else if (qualifier.storage == EvqUniform)
1451         set = 2;
1452     else if (qualifier.storage == EvqBuffer)
1453         set = 3;
1454     else if (qualifier.isAnyPayload())
1455         setRT = 0;
1456     else if (qualifier.isAnyCallable())
1457         setRT = 1;
1458     else
1459         return -1;
1460
1461     int size;
1462     if (qualifier.isAnyPayload() || qualifier.isAnyCallable()) {
1463         size = 1;
1464     } else if (qualifier.isUniformOrBuffer() || qualifier.isTaskMemory()) {
1465         if (type.isSizedArray())
1466             size = type.getCumulativeArraySize();
1467         else
1468             size = 1;
1469     } else {
1470         // Strip off the outer array dimension for those having an extra one.
1471         if (type.isArray() && qualifier.isArrayedIo(language)) {
1472             TType elementType(type, 0);
1473             size = computeTypeLocationSize(elementType, language);
1474         } else
1475             size = computeTypeLocationSize(type, language);
1476     }
1477
1478     // Locations, and components within locations.
1479     //
1480     // Almost always, dealing with components means a single location is involved.
1481     // The exception is a dvec3. From the spec:
1482     //
1483     // "A dvec3 will consume all four components of the first location and components 0 and 1 of
1484     // the second location. This leaves components 2 and 3 available for other component-qualified
1485     // declarations."
1486     //
1487     // That means, without ever mentioning a component, a component range
1488     // for a different location gets specified, if it's not a vertex shader input. (!)
1489     // (A vertex shader input will show using only one location, even for a dvec3/4.)
1490     //
1491     // So, for the case of dvec3, we need two independent ioRanges.
1492     //
1493     // For raytracing IO (payloads and callabledata) each declaration occupies a single
1494     // slot irrespective of type.
1495     int collision = -1; // no collision
1496 #ifndef GLSLANG_WEB
1497     if (qualifier.isAnyPayload() || qualifier.isAnyCallable()) {
1498         TRange range(qualifier.layoutLocation, qualifier.layoutLocation);
1499         collision = checkLocationRT(setRT, qualifier.layoutLocation);
1500         if (collision < 0)
1501             usedIoRT[setRT].push_back(range);
1502     } else if (size == 2 && type.getBasicType() == EbtDouble && type.getVectorSize() == 3 &&
1503         (qualifier.isPipeInput() || qualifier.isPipeOutput())) {
1504         // Dealing with dvec3 in/out split across two locations.
1505         // Need two io-ranges.
1506         // The case where the dvec3 doesn't start at component 0 was previously caught as overflow.
1507
1508         // First range:
1509         TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation);
1510         TRange componentRange(0, 3);
1511         TIoRange range(locationRange, componentRange, type.getBasicType(), 0);
1512
1513         // check for collisions
1514         collision = checkLocationRange(set, range, type, typeCollision);
1515         if (collision < 0) {
1516             usedIo[set].push_back(range);
1517
1518             // Second range:
1519             TRange locationRange2(qualifier.layoutLocation + 1, qualifier.layoutLocation + 1);
1520             TRange componentRange2(0, 1);
1521             TIoRange range2(locationRange2, componentRange2, type.getBasicType(), 0);
1522
1523             // check for collisions
1524             collision = checkLocationRange(set, range2, type, typeCollision);
1525             if (collision < 0)
1526                 usedIo[set].push_back(range2);
1527         }
1528     } else
1529 #endif
1530     {
1531         // Not a dvec3 in/out split across two locations, generic path.
1532         // Need a single IO-range block.
1533
1534         TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation + size - 1);
1535         TRange componentRange(0, 3);
1536         if (qualifier.hasComponent() || type.getVectorSize() > 0) {
1537             int consumedComponents = type.getVectorSize() * (type.getBasicType() == EbtDouble ? 2 : 1);
1538             if (qualifier.hasComponent())
1539                 componentRange.start = qualifier.layoutComponent;
1540             componentRange.last  = componentRange.start + consumedComponents - 1;
1541         }
1542
1543         // combine location and component ranges
1544         TIoRange range(locationRange, componentRange, type.getBasicType(), qualifier.hasIndex() ? qualifier.getIndex() : 0);
1545
1546         // check for collisions, except for vertex inputs on desktop targeting OpenGL
1547         if (! (!isEsProfile() && language == EShLangVertex && qualifier.isPipeInput()) || spvVersion.vulkan > 0)
1548             collision = checkLocationRange(set, range, type, typeCollision);
1549
1550         if (collision < 0)
1551             usedIo[set].push_back(range);
1552     }
1553
1554     return collision;
1555 }
1556
1557 // Compare a new (the passed in) 'range' against the existing set, and see
1558 // if there are any collisions.
1559 //
1560 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1561 //
1562 int TIntermediate::checkLocationRange(int set, const TIoRange& range, const TType& type, bool& typeCollision)
1563 {
1564     for (size_t r = 0; r < usedIo[set].size(); ++r) {
1565         if (range.overlap(usedIo[set][r])) {
1566             // there is a collision; pick one
1567             return std::max(range.location.start, usedIo[set][r].location.start);
1568         } else if (range.location.overlap(usedIo[set][r].location) && type.getBasicType() != usedIo[set][r].basicType) {
1569             // aliased-type mismatch
1570             typeCollision = true;
1571             return std::max(range.location.start, usedIo[set][r].location.start);
1572         }
1573     }
1574
1575     return -1; // no collision
1576 }
1577
1578 int TIntermediate::checkLocationRT(int set, int location) {
1579     TRange range(location, location);
1580     for (size_t r = 0; r < usedIoRT[set].size(); ++r) {
1581         if (range.overlap(usedIoRT[set][r])) {
1582             return range.start;
1583         }
1584     }
1585     return -1; // no collision
1586 }
1587
1588 // Accumulate bindings and offsets, and check for collisions
1589 // as the accumulation is done.
1590 //
1591 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1592 //
1593 int TIntermediate::addUsedOffsets(int binding, int offset, int numOffsets)
1594 {
1595     TRange bindingRange(binding, binding);
1596     TRange offsetRange(offset, offset + numOffsets - 1);
1597     TOffsetRange range(bindingRange, offsetRange);
1598
1599     // check for collisions, except for vertex inputs on desktop
1600     for (size_t r = 0; r < usedAtomics.size(); ++r) {
1601         if (range.overlap(usedAtomics[r])) {
1602             // there is a collision; pick one
1603             return std::max(offset, usedAtomics[r].offset.start);
1604         }
1605     }
1606
1607     usedAtomics.push_back(range);
1608
1609     return -1; // no collision
1610 }
1611
1612 // Accumulate used constant_id values.
1613 //
1614 // Return false is one was already used.
1615 bool TIntermediate::addUsedConstantId(int id)
1616 {
1617     if (usedConstantId.find(id) != usedConstantId.end())
1618         return false;
1619
1620     usedConstantId.insert(id);
1621
1622     return true;
1623 }
1624
1625 // Recursively figure out how many locations are used up by an input or output type.
1626 // Return the size of type, as measured by "locations".
1627 int TIntermediate::computeTypeLocationSize(const TType& type, EShLanguage stage)
1628 {
1629     // "If the declared input is an array of size n and each element takes m locations, it will be assigned m * n
1630     // consecutive locations..."
1631     if (type.isArray()) {
1632         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1633         // TODO: are there valid cases of having an unsized array with a location?  If so, running this code too early.
1634         TType elementType(type, 0);
1635         if (type.isSizedArray() && !type.getQualifier().isPerView())
1636             return type.getOuterArraySize() * computeTypeLocationSize(elementType, stage);
1637         else {
1638 #ifndef GLSLANG_WEB
1639             // unset perViewNV attributes for arrayed per-view outputs: "perviewNV vec4 v[MAX_VIEWS][3];"
1640             elementType.getQualifier().perViewNV = false;
1641 #endif
1642             return computeTypeLocationSize(elementType, stage);
1643         }
1644     }
1645
1646     // "The locations consumed by block and structure members are determined by applying the rules above
1647     // recursively..."
1648     if (type.isStruct()) {
1649         int size = 0;
1650         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1651             TType memberType(type, member);
1652             size += computeTypeLocationSize(memberType, stage);
1653         }
1654         return size;
1655     }
1656
1657     // ES: "If a shader input is any scalar or vector type, it will consume a single location."
1658
1659     // Desktop: "If a vertex shader input is any scalar or vector type, it will consume a single location. If a non-vertex
1660     // shader input is a scalar or vector type other than dvec3 or dvec4, it will consume a single location, while
1661     // types dvec3 or dvec4 will consume two consecutive locations. Inputs of type double and dvec2 will
1662     // consume only a single location, in all stages."
1663     if (type.isScalar())
1664         return 1;
1665     if (type.isVector()) {
1666         if (stage == EShLangVertex && type.getQualifier().isPipeInput())
1667             return 1;
1668         if (type.getBasicType() == EbtDouble && type.getVectorSize() > 2)
1669             return 2;
1670         else
1671             return 1;
1672     }
1673
1674     // "If the declared input is an n x m single- or double-precision matrix, ...
1675     // The number of locations assigned for each matrix will be the same as
1676     // for an n-element array of m-component vectors..."
1677     if (type.isMatrix()) {
1678         TType columnType(type, 0);
1679         return type.getMatrixCols() * computeTypeLocationSize(columnType, stage);
1680     }
1681
1682     assert(0);
1683     return 1;
1684 }
1685
1686 // Same as computeTypeLocationSize but for uniforms
1687 int TIntermediate::computeTypeUniformLocationSize(const TType& type)
1688 {
1689     // "Individual elements of a uniform array are assigned
1690     // consecutive locations with the first element taking location
1691     // location."
1692     if (type.isArray()) {
1693         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1694         TType elementType(type, 0);
1695         if (type.isSizedArray()) {
1696             return type.getOuterArraySize() * computeTypeUniformLocationSize(elementType);
1697         } else {
1698             // TODO: are there valid cases of having an implicitly-sized array with a location?  If so, running this code too early.
1699             return computeTypeUniformLocationSize(elementType);
1700         }
1701     }
1702
1703     // "Each subsequent inner-most member or element gets incremental
1704     // locations for the entire structure or array."
1705     if (type.isStruct()) {
1706         int size = 0;
1707         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1708             TType memberType(type, member);
1709             size += computeTypeUniformLocationSize(memberType);
1710         }
1711         return size;
1712     }
1713
1714     return 1;
1715 }
1716
1717 #ifndef GLSLANG_WEB
1718
1719 // Accumulate xfb buffer ranges and check for collisions as the accumulation is done.
1720 //
1721 // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value.
1722 //
1723 int TIntermediate::addXfbBufferOffset(const TType& type)
1724 {
1725     const TQualifier& qualifier = type.getQualifier();
1726
1727     assert(qualifier.hasXfbOffset() && qualifier.hasXfbBuffer());
1728     TXfbBuffer& buffer = xfbBuffers[qualifier.layoutXfbBuffer];
1729
1730     // compute the range
1731     unsigned int size = computeTypeXfbSize(type, buffer.contains64BitType, buffer.contains32BitType, buffer.contains16BitType);
1732     buffer.implicitStride = std::max(buffer.implicitStride, qualifier.layoutXfbOffset + size);
1733     TRange range(qualifier.layoutXfbOffset, qualifier.layoutXfbOffset + size - 1);
1734
1735     // check for collisions
1736     for (size_t r = 0; r < buffer.ranges.size(); ++r) {
1737         if (range.overlap(buffer.ranges[r])) {
1738             // there is a collision; pick an example to return
1739             return std::max(range.start, buffer.ranges[r].start);
1740         }
1741     }
1742
1743     buffer.ranges.push_back(range);
1744
1745     return -1;  // no collision
1746 }
1747
1748 // Recursively figure out how many bytes of xfb buffer are used by the given type.
1749 // Return the size of type, in bytes.
1750 // Sets contains64BitType to true if the type contains a 64-bit data type.
1751 // Sets contains32BitType to true if the type contains a 32-bit data type.
1752 // Sets contains16BitType to true if the type contains a 16-bit data type.
1753 // N.B. Caller must set contains64BitType, contains32BitType, and contains16BitType to false before calling.
1754 unsigned int TIntermediate::computeTypeXfbSize(const TType& type, bool& contains64BitType, bool& contains32BitType, bool& contains16BitType) const
1755 {
1756     // "...if applied to an aggregate containing a double or 64-bit integer, the offset must also be a multiple of 8,
1757     // and the space taken in the buffer will be a multiple of 8.
1758     // ...within the qualified entity, subsequent components are each
1759     // assigned, in order, to the next available offset aligned to a multiple of
1760     // that component's size.  Aggregate types are flattened down to the component
1761     // level to get this sequence of components."
1762
1763     if (type.isSizedArray()) {
1764         // TODO: perf: this can be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1765         // Unsized array use to xfb should be a compile error.
1766         TType elementType(type, 0);
1767         return type.getOuterArraySize() * computeTypeXfbSize(elementType, contains64BitType, contains16BitType, contains16BitType);
1768     }
1769
1770     if (type.isStruct()) {
1771         unsigned int size = 0;
1772         bool structContains64BitType = false;
1773         bool structContains32BitType = false;
1774         bool structContains16BitType = false;
1775         for (int member = 0; member < (int)type.getStruct()->size(); ++member) {
1776             TType memberType(type, member);
1777             // "... if applied to
1778             // an aggregate containing a double or 64-bit integer, the offset must also be a multiple of 8,
1779             // and the space taken in the buffer will be a multiple of 8."
1780             bool memberContains64BitType = false;
1781             bool memberContains32BitType = false;
1782             bool memberContains16BitType = false;
1783             int memberSize = computeTypeXfbSize(memberType, memberContains64BitType, memberContains32BitType, memberContains16BitType);
1784             if (memberContains64BitType) {
1785                 structContains64BitType = true;
1786                 RoundToPow2(size, 8);
1787             } else if (memberContains32BitType) {
1788                 structContains32BitType = true;
1789                 RoundToPow2(size, 4);
1790             } else if (memberContains16BitType) {
1791                 structContains16BitType = true;
1792                 RoundToPow2(size, 2);
1793             }
1794             size += memberSize;
1795         }
1796
1797         if (structContains64BitType) {
1798             contains64BitType = true;
1799             RoundToPow2(size, 8);
1800         } else if (structContains32BitType) {
1801             contains32BitType = true;
1802             RoundToPow2(size, 4);
1803         } else if (structContains16BitType) {
1804             contains16BitType = true;
1805             RoundToPow2(size, 2);
1806         }
1807         return size;
1808     }
1809
1810     int numComponents {0};
1811     if (type.isScalar())
1812         numComponents = 1;
1813     else if (type.isVector())
1814         numComponents = type.getVectorSize();
1815     else if (type.isMatrix())
1816         numComponents = type.getMatrixCols() * type.getMatrixRows();
1817     else {
1818         assert(0);
1819         numComponents = 1;
1820     }
1821
1822     if (type.getBasicType() == EbtDouble || type.getBasicType() == EbtInt64 || type.getBasicType() == EbtUint64) {
1823         contains64BitType = true;
1824         return 8 * numComponents;
1825     } else if (type.getBasicType() == EbtFloat16 || type.getBasicType() == EbtInt16 || type.getBasicType() == EbtUint16) {
1826         contains16BitType = true;
1827         return 2 * numComponents;
1828     } else if (type.getBasicType() == EbtInt8 || type.getBasicType() == EbtUint8)
1829         return numComponents;
1830     else {
1831         contains32BitType = true;
1832         return 4 * numComponents;
1833     }
1834 }
1835
1836 #endif
1837
1838 const int baseAlignmentVec4Std140 = 16;
1839
1840 // Return the size and alignment of a component of the given type.
1841 // The size is returned in the 'size' parameter
1842 // Return value is the alignment..
1843 int TIntermediate::getBaseAlignmentScalar(const TType& type, int& size)
1844 {
1845 #ifdef GLSLANG_WEB
1846     size = 4; return 4;
1847 #endif
1848
1849     switch (type.getBasicType()) {
1850     case EbtInt64:
1851     case EbtUint64:
1852     case EbtDouble:  size = 8; return 8;
1853     case EbtFloat16: size = 2; return 2;
1854     case EbtInt8:
1855     case EbtUint8:   size = 1; return 1;
1856     case EbtInt16:
1857     case EbtUint16:  size = 2; return 2;
1858     case EbtReference: size = 8; return 8;
1859     default:         size = 4; return 4;
1860     }
1861 }
1862
1863 // Implement base-alignment and size rules from section 7.6.2.2 Standard Uniform Block Layout
1864 // Operates recursively.
1865 //
1866 // If std140 is true, it does the rounding up to vec4 size required by std140,
1867 // otherwise it does not, yielding std430 rules.
1868 //
1869 // The size is returned in the 'size' parameter
1870 //
1871 // The stride is only non-0 for arrays or matrices, and is the stride of the
1872 // top-level object nested within the type.  E.g., for an array of matrices,
1873 // it is the distances needed between matrices, despite the rules saying the
1874 // stride comes from the flattening down to vectors.
1875 //
1876 // Return value is the alignment of the type.
1877 int TIntermediate::getBaseAlignment(const TType& type, int& size, int& stride, TLayoutPacking layoutPacking, bool rowMajor)
1878 {
1879     int alignment;
1880
1881     bool std140 = layoutPacking == glslang::ElpStd140;
1882     // When using the std140 storage layout, structures will be laid out in buffer
1883     // storage with its members stored in monotonically increasing order based on their
1884     // location in the declaration. A structure and each structure member have a base
1885     // offset and a base alignment, from which an aligned offset is computed by rounding
1886     // the base offset up to a multiple of the base alignment. The base offset of the first
1887     // member of a structure is taken from the aligned offset of the structure itself. The
1888     // base offset of all other structure members is derived by taking the offset of the
1889     // last basic machine unit consumed by the previous member and adding one. Each
1890     // structure member is stored in memory at its aligned offset. The members of a top-
1891     // level uniform block are laid out in buffer storage by treating the uniform block as
1892     // a structure with a base offset of zero.
1893     //
1894     //   1. If the member is a scalar consuming N basic machine units, the base alignment is N.
1895     //
1896     //   2. If the member is a two- or four-component vector with components consuming N basic
1897     //      machine units, the base alignment is 2N or 4N, respectively.
1898     //
1899     //   3. If the member is a three-component vector with components consuming N
1900     //      basic machine units, the base alignment is 4N.
1901     //
1902     //   4. If the member is an array of scalars or vectors, the base alignment and array
1903     //      stride are set to match the base alignment of a single array element, according
1904     //      to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The
1905     //      array may have padding at the end; the base offset of the member following
1906     //      the array is rounded up to the next multiple of the base alignment.
1907     //
1908     //   5. If the member is a column-major matrix with C columns and R rows, the
1909     //      matrix is stored identically to an array of C column vectors with R
1910     //      components each, according to rule (4).
1911     //
1912     //   6. If the member is an array of S column-major matrices with C columns and
1913     //      R rows, the matrix is stored identically to a row of S X C column vectors
1914     //      with R components each, according to rule (4).
1915     //
1916     //   7. If the member is a row-major matrix with C columns and R rows, the matrix
1917     //      is stored identically to an array of R row vectors with C components each,
1918     //      according to rule (4).
1919     //
1920     //   8. If the member is an array of S row-major matrices with C columns and R
1921     //      rows, the matrix is stored identically to a row of S X R row vectors with C
1922     //      components each, according to rule (4).
1923     //
1924     //   9. If the member is a structure, the base alignment of the structure is N , where
1925     //      N is the largest base alignment value of any    of its members, and rounded
1926     //      up to the base alignment of a vec4. The individual members of this substructure
1927     //      are then assigned offsets by applying this set of rules recursively,
1928     //      where the base offset of the first member of the sub-structure is equal to the
1929     //      aligned offset of the structure. The structure may have padding at the end;
1930     //      the base offset of the member following the sub-structure is rounded up to
1931     //      the next multiple of the base alignment of the structure.
1932     //
1933     //   10. If the member is an array of S structures, the S elements of the array are laid
1934     //       out in order, according to rule (9).
1935     //
1936     //   Assuming, for rule 10:  The stride is the same as the size of an element.
1937
1938     stride = 0;
1939     int dummyStride;
1940
1941     // rules 4, 6, 8, and 10
1942     if (type.isArray()) {
1943         // TODO: perf: this might be flattened by using getCumulativeArraySize(), and a deref that discards all arrayness
1944         TType derefType(type, 0);
1945         alignment = getBaseAlignment(derefType, size, dummyStride, layoutPacking, rowMajor);
1946         if (std140)
1947             alignment = std::max(baseAlignmentVec4Std140, alignment);
1948         RoundToPow2(size, alignment);
1949         stride = size;  // uses full matrix size for stride of an array of matrices (not quite what rule 6/8, but what's expected)
1950                         // uses the assumption for rule 10 in the comment above
1951         // use one element to represent the last member of SSBO which is unsized array
1952         int arraySize = (type.isUnsizedArray() && (type.getOuterArraySize() == 0)) ? 1 : type.getOuterArraySize();
1953         size = stride * arraySize;
1954         return alignment;
1955     }
1956
1957     // rule 9
1958     if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtBlock) {
1959         const TTypeList& memberList = *type.getStruct();
1960
1961         size = 0;
1962         int maxAlignment = std140 ? baseAlignmentVec4Std140 : 0;
1963         for (size_t m = 0; m < memberList.size(); ++m) {
1964             int memberSize;
1965             // modify just the children's view of matrix layout, if there is one for this member
1966             TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
1967             int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, dummyStride, layoutPacking,
1968                                                    (subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
1969             maxAlignment = std::max(maxAlignment, memberAlignment);
1970             RoundToPow2(size, memberAlignment);
1971             size += memberSize;
1972         }
1973
1974         // The structure may have padding at the end; the base offset of
1975         // the member following the sub-structure is rounded up to the next
1976         // multiple of the base alignment of the structure.
1977         RoundToPow2(size, maxAlignment);
1978
1979         return maxAlignment;
1980     }
1981
1982     // rule 1
1983     if (type.isScalar())
1984         return getBaseAlignmentScalar(type, size);
1985
1986     // rules 2 and 3
1987     if (type.isVector()) {
1988         int scalarAlign = getBaseAlignmentScalar(type, size);
1989         switch (type.getVectorSize()) {
1990         case 1: // HLSL has this, GLSL does not
1991             return scalarAlign;
1992         case 2:
1993             size *= 2;
1994             return 2 * scalarAlign;
1995         default:
1996             size *= type.getVectorSize();
1997             return 4 * scalarAlign;
1998         }
1999     }
2000
2001     // rules 5 and 7
2002     if (type.isMatrix()) {
2003         // rule 5: deref to row, not to column, meaning the size of vector is num columns instead of num rows
2004         TType derefType(type, 0, rowMajor);
2005
2006         alignment = getBaseAlignment(derefType, size, dummyStride, layoutPacking, rowMajor);
2007         if (std140)
2008             alignment = std::max(baseAlignmentVec4Std140, alignment);
2009         RoundToPow2(size, alignment);
2010         stride = size;  // use intra-matrix stride for stride of a just a matrix
2011         if (rowMajor)
2012             size = stride * type.getMatrixRows();
2013         else
2014             size = stride * type.getMatrixCols();
2015
2016         return alignment;
2017     }
2018
2019     assert(0);  // all cases should be covered above
2020     size = baseAlignmentVec4Std140;
2021     return baseAlignmentVec4Std140;
2022 }
2023
2024 // To aid the basic HLSL rule about crossing vec4 boundaries.
2025 bool TIntermediate::improperStraddle(const TType& type, int size, int offset)
2026 {
2027     if (! type.isVector() || type.isArray())
2028         return false;
2029
2030     return size <= 16 ? offset / 16 != (offset + size - 1) / 16
2031                       : offset % 16 != 0;
2032 }
2033
2034 int TIntermediate::getScalarAlignment(const TType& type, int& size, int& stride, bool rowMajor)
2035 {
2036     int alignment;
2037
2038     stride = 0;
2039     int dummyStride;
2040
2041     if (type.isArray()) {
2042         TType derefType(type, 0);
2043         alignment = getScalarAlignment(derefType, size, dummyStride, rowMajor);
2044
2045         stride = size;
2046         RoundToPow2(stride, alignment);
2047
2048         size = stride * (type.getOuterArraySize() - 1) + size;
2049         return alignment;
2050     }
2051
2052     if (type.getBasicType() == EbtStruct) {
2053         const TTypeList& memberList = *type.getStruct();
2054
2055         size = 0;
2056         int maxAlignment = 0;
2057         for (size_t m = 0; m < memberList.size(); ++m) {
2058             int memberSize;
2059             // modify just the children's view of matrix layout, if there is one for this member
2060             TLayoutMatrix subMatrixLayout = memberList[m].type->getQualifier().layoutMatrix;
2061             int memberAlignment = getScalarAlignment(*memberList[m].type, memberSize, dummyStride,
2062                                                      (subMatrixLayout != ElmNone) ? (subMatrixLayout == ElmRowMajor) : rowMajor);
2063             maxAlignment = std::max(maxAlignment, memberAlignment);
2064             RoundToPow2(size, memberAlignment);
2065             size += memberSize;
2066         }
2067
2068         return maxAlignment;
2069     }
2070
2071     if (type.isScalar())
2072         return getBaseAlignmentScalar(type, size);
2073
2074     if (type.isVector()) {
2075         int scalarAlign = getBaseAlignmentScalar(type, size);
2076         
2077         size *= type.getVectorSize();
2078         return scalarAlign;
2079     }
2080
2081     if (type.isMatrix()) {
2082         TType derefType(type, 0, rowMajor);
2083
2084         alignment = getScalarAlignment(derefType, size, dummyStride, rowMajor);
2085
2086         stride = size;  // use intra-matrix stride for stride of a just a matrix
2087         if (rowMajor)
2088             size = stride * type.getMatrixRows();
2089         else
2090             size = stride * type.getMatrixCols();
2091
2092         return alignment;
2093     }
2094
2095     assert(0);  // all cases should be covered above
2096     size = 1;
2097     return 1;    
2098 }
2099
2100 int TIntermediate::getMemberAlignment(const TType& type, int& size, int& stride, TLayoutPacking layoutPacking, bool rowMajor)
2101 {
2102     if (layoutPacking == glslang::ElpScalar) {
2103         return getScalarAlignment(type, size, stride, rowMajor);
2104     } else {
2105         return getBaseAlignment(type, size, stride, layoutPacking, rowMajor);
2106     }
2107 }
2108
2109 // shared calculation by getOffset and getOffsets
2110 void TIntermediate::updateOffset(const TType& parentType, const TType& memberType, int& offset, int& memberSize)
2111 {
2112     int dummyStride;
2113
2114     // modify just the children's view of matrix layout, if there is one for this member
2115     TLayoutMatrix subMatrixLayout = memberType.getQualifier().layoutMatrix;
2116     int memberAlignment = getMemberAlignment(memberType, memberSize, dummyStride,
2117                                              parentType.getQualifier().layoutPacking,
2118                                              subMatrixLayout != ElmNone
2119                                                  ? subMatrixLayout == ElmRowMajor
2120                                                  : parentType.getQualifier().layoutMatrix == ElmRowMajor);
2121     RoundToPow2(offset, memberAlignment);
2122 }
2123
2124 // Lookup or calculate the offset of a block member, using the recursively
2125 // defined block offset rules.
2126 int TIntermediate::getOffset(const TType& type, int index)
2127 {
2128     const TTypeList& memberList = *type.getStruct();
2129
2130     // Don't calculate offset if one is present, it could be user supplied
2131     // and different than what would be calculated.  That is, this is faster,
2132     // but not just an optimization.
2133     if (memberList[index].type->getQualifier().hasOffset())
2134         return memberList[index].type->getQualifier().layoutOffset;
2135
2136     int memberSize = 0;
2137     int offset = 0;
2138     for (int m = 0; m <= index; ++m) {
2139         updateOffset(type, *memberList[m].type, offset, memberSize);
2140
2141         if (m < index)
2142             offset += memberSize;
2143     }
2144
2145     return offset;
2146 }
2147
2148 // Calculate the block data size.
2149 // Block arrayness is not taken into account, each element is backed by a separate buffer.
2150 int TIntermediate::getBlockSize(const TType& blockType)
2151 {
2152     const TTypeList& memberList = *blockType.getStruct();
2153     int lastIndex = (int)memberList.size() - 1;
2154     int lastOffset = getOffset(blockType, lastIndex);
2155
2156     int lastMemberSize;
2157     int dummyStride;
2158     getMemberAlignment(*memberList[lastIndex].type, lastMemberSize, dummyStride,
2159                        blockType.getQualifier().layoutPacking,
2160                        blockType.getQualifier().layoutMatrix == ElmRowMajor);
2161
2162     return lastOffset + lastMemberSize;
2163 }
2164
2165 int TIntermediate::computeBufferReferenceTypeSize(const TType& type)
2166 {
2167     assert(type.isReference());
2168     int size = getBlockSize(*type.getReferentType());
2169
2170     int align = type.getBufferReferenceAlignment();
2171
2172     if (align) {
2173         size = (size + align - 1) & ~(align-1);
2174     }
2175
2176     return size;
2177 }
2178
2179 #ifndef GLSLANG_WEB
2180 bool TIntermediate::isIoResizeArray(const TType& type, EShLanguage language) {
2181     return type.isArray() &&
2182             ((language == EShLangGeometry    && type.getQualifier().storage == EvqVaryingIn) ||
2183             (language == EShLangTessControl && (type.getQualifier().storage == EvqVaryingIn || type.getQualifier().storage == EvqVaryingOut) &&
2184                 ! type.getQualifier().patch) ||
2185             (language == EShLangTessEvaluation && type.getQualifier().storage == EvqVaryingIn) ||
2186             (language == EShLangFragment && type.getQualifier().storage == EvqVaryingIn &&
2187                 type.getQualifier().pervertexNV) ||
2188             (language == EShLangMeshNV && type.getQualifier().storage == EvqVaryingOut &&
2189                 !type.getQualifier().perTaskNV));
2190 }
2191 #endif // not GLSLANG_WEB
2192
2193 } // end namespace glslang