OSDN Git Service

Fixed clang warnings and unmuted these warnings
[android-x86/external-swiftshader.git] / src / OpenGL / compiler / OutputASM.cpp
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "OutputASM.h"
16 #include "Common/Math.hpp"
17
18 #include "common/debug.h"
19 #include "InfoSink.h"
20
21 #include "libGLESv2/Shader.h"
22
23 #include <GLES2/gl2.h>
24 #include <GLES2/gl2ext.h>
25 #include <GLES3/gl3.h>
26
27 namespace glsl
28 {
29         // Integer to TString conversion
30         TString str(int i)
31         {
32                 char buffer[20];
33                 sprintf(buffer, "%d", i);
34                 return buffer;
35         }
36
37         class Temporary : public TIntermSymbol
38         {
39         public:
40                 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
41                 {
42                 }
43
44                 ~Temporary()
45                 {
46                         assembler->freeTemporary(this);
47                 }
48
49         private:
50                 OutputASM *const assembler;
51         };
52
53         class Constant : public TIntermConstantUnion
54         {
55         public:
56                 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
57                 {
58                         constants[0].setFConst(x);
59                         constants[1].setFConst(y);
60                         constants[2].setFConst(z);
61                         constants[3].setFConst(w);
62                 }
63
64                 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
65                 {
66                         constants[0].setBConst(b);
67                 }
68
69                 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
70                 {
71                         constants[0].setIConst(i);
72                 }
73
74                 ~Constant()
75                 {
76                 }
77
78         private:
79                 ConstantUnion constants[4];
80         };
81
82         Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
83                 type(type), precision(precision), name(name), arraySize(arraySize), registerIndex(registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
84         {
85         }
86
87         UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
88                                    TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
89                 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
90                 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
91         {
92         }
93
94         BlockLayoutEncoder::BlockLayoutEncoder(bool rowMajor)
95                 : mCurrentOffset(0), isRowMajor(rowMajor)
96         {
97         }
98
99         BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
100         {
101                 int arrayStride;
102                 int matrixStride;
103
104                 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
105
106                 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
107                                                  static_cast<int>(arrayStride * BytesPerComponent),
108                                                  static_cast<int>(matrixStride * BytesPerComponent),
109                                                  (matrixStride > 0) && isRowMajor);
110
111                 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
112
113                 return memberInfo;
114         }
115
116         // static
117         size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
118         {
119                 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
120         }
121
122         // static
123         size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
124         {
125                 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
126         }
127
128         void BlockLayoutEncoder::nextRegister()
129         {
130                 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
131         }
132
133         Std140BlockEncoder::Std140BlockEncoder(bool rowMajor) : BlockLayoutEncoder(rowMajor)
134         {
135         }
136
137         void Std140BlockEncoder::enterAggregateType()
138         {
139                 nextRegister();
140         }
141
142         void Std140BlockEncoder::exitAggregateType()
143         {
144                 nextRegister();
145         }
146
147         void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
148         {
149                 size_t baseAlignment = 0;
150                 int matrixStride = 0;
151                 int arrayStride = 0;
152
153                 if(type.isMatrix())
154                 {
155                         baseAlignment = ComponentsPerRegister;
156                         matrixStride = ComponentsPerRegister;
157
158                         if(arraySize > 0)
159                         {
160                                 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
161                                 arrayStride = ComponentsPerRegister * numRegisters;
162                         }
163                 }
164                 else if(arraySize > 0)
165                 {
166                         baseAlignment = ComponentsPerRegister;
167                         arrayStride = ComponentsPerRegister;
168                 }
169                 else
170                 {
171                         const size_t numComponents = type.getElementSize();
172                         baseAlignment = (numComponents == 3 ? 4u : numComponents);
173                 }
174
175                 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
176
177                 *matrixStrideOut = matrixStride;
178                 *arrayStrideOut = arrayStride;
179         }
180
181         void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
182         {
183                 if(arraySize > 0)
184                 {
185                         mCurrentOffset += arrayStride * arraySize;
186                 }
187                 else if(type.isMatrix())
188                 {
189                         ASSERT(matrixStride == ComponentsPerRegister);
190                         const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
191                         mCurrentOffset += ComponentsPerRegister * numRegisters;
192                 }
193                 else
194                 {
195                         mCurrentOffset += type.getElementSize();
196                 }
197         }
198
199         Attribute::Attribute()
200         {
201                 type = GL_NONE;
202                 arraySize = 0;
203                 registerIndex = 0;
204         }
205
206         Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
207         {
208                 this->type = type;
209                 this->name = name;
210                 this->arraySize = arraySize;
211                 this->location = location;
212                 this->registerIndex = registerIndex;
213         }
214
215         sw::PixelShader *Shader::getPixelShader() const
216         {
217                 return 0;
218         }
219
220         sw::VertexShader *Shader::getVertexShader() const
221         {
222                 return 0;
223         }
224
225         OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
226         {
227                 TString name = TFunction::unmangleName(nodeName);
228
229                 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D")
230                 {
231                         method = IMPLICIT;
232                 }
233                 else if(name == "texture2DProj" || name == "textureProj")
234                 {
235                         method = IMPLICIT;
236                         proj = true;
237                 }
238                 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
239                 {
240                         method = LOD;
241                 }
242                 else if(name == "texture2DProjLod" || name == "textureProjLod")
243                 {
244                         method = LOD;
245                         proj = true;
246                 }
247                 else if(name == "textureSize")
248                 {
249                         method = SIZE;
250                 }
251                 else if(name == "textureOffset")
252                 {
253                         method = IMPLICIT;
254                         offset = true;
255                 }
256                 else if(name == "textureProjOffset")
257                 {
258                         method = IMPLICIT;
259                         offset = true;
260                         proj = true;
261                 }
262                 else if(name == "textureLodOffset")
263                 {
264                         method = LOD;
265                         offset = true;
266                 }
267                 else if(name == "textureProjLodOffset")
268                 {
269                         method = LOD;
270                         proj = true;
271                         offset = true;
272                 }
273                 else if(name == "texelFetch")
274                 {
275                         method = FETCH;
276                 }
277                 else if(name == "texelFetchOffset")
278                 {
279                         method = FETCH;
280                         offset = true;
281                 }
282                 else if(name == "textureGrad")
283                 {
284                         method = GRAD;
285                 }
286                 else if(name == "textureGradOffset")
287                 {
288                         method = GRAD;
289                         offset = true;
290                 }
291                 else if(name == "textureProjGrad")
292                 {
293                         method = GRAD;
294                         proj = true;
295                 }
296                 else if(name == "textureProjGradOffset")
297                 {
298                         method = GRAD;
299                         proj = true;
300                         offset = true;
301                 }
302                 else UNREACHABLE(0);
303         }
304
305         OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
306         {
307                 shader = 0;
308                 pixelShader = 0;
309                 vertexShader = 0;
310
311                 if(shaderObject)
312                 {
313                         shader = shaderObject->getShader();
314                         pixelShader = shaderObject->getPixelShader();
315                         vertexShader = shaderObject->getVertexShader();
316                 }
317
318                 functionArray.push_back(Function(0, "main(", 0, 0));
319                 currentFunction = 0;
320                 outputQualifier = EvqOutput; // Set outputQualifier to any value other than EvqFragColor or EvqFragData
321         }
322
323         OutputASM::~OutputASM()
324         {
325         }
326
327         void OutputASM::output()
328         {
329                 if(shader)
330                 {
331                         emitShader(GLOBAL);
332
333                         if(functionArray.size() > 1)   // Only call main() when there are other functions
334                         {
335                                 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
336                                 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
337                                 callMain->dst.index = 0;   // main()
338
339                                 emit(sw::Shader::OPCODE_RET);
340                         }
341
342                         emitShader(FUNCTION);
343                 }
344         }
345
346         void OutputASM::emitShader(Scope scope)
347         {
348                 emitScope = scope;
349                 currentScope = GLOBAL;
350                 mContext.getTreeRoot()->traverse(this);
351         }
352
353         void OutputASM::freeTemporary(Temporary *temporary)
354         {
355                 free(temporaries, temporary);
356         }
357
358         sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
359         {
360                 TBasicType baseType = in->getType().getBasicType();
361
362                 switch(op)
363                 {
364                 case sw::Shader::OPCODE_NEG:
365                         switch(baseType)
366                         {
367                         case EbtInt:
368                         case EbtUInt:
369                                 return sw::Shader::OPCODE_INEG;
370                         case EbtFloat:
371                         default:
372                                 return op;
373                         }
374                 case sw::Shader::OPCODE_ABS:
375                         switch(baseType)
376                         {
377                         case EbtInt:
378                                 return sw::Shader::OPCODE_IABS;
379                         case EbtFloat:
380                         default:
381                                 return op;
382                         }
383                 case sw::Shader::OPCODE_SGN:
384                         switch(baseType)
385                         {
386                         case EbtInt:
387                                 return sw::Shader::OPCODE_ISGN;
388                         case EbtFloat:
389                         default:
390                                 return op;
391                         }
392                 case sw::Shader::OPCODE_ADD:
393                         switch(baseType)
394                         {
395                         case EbtInt:
396                         case EbtUInt:
397                                 return sw::Shader::OPCODE_IADD;
398                         case EbtFloat:
399                         default:
400                                 return op;
401                         }
402                 case sw::Shader::OPCODE_SUB:
403                         switch(baseType)
404                         {
405                         case EbtInt:
406                         case EbtUInt:
407                                 return sw::Shader::OPCODE_ISUB;
408                         case EbtFloat:
409                         default:
410                                 return op;
411                         }
412                 case sw::Shader::OPCODE_MUL:
413                         switch(baseType)
414                         {
415                         case EbtInt:
416                         case EbtUInt:
417                                 return sw::Shader::OPCODE_IMUL;
418                         case EbtFloat:
419                         default:
420                                 return op;
421                         }
422                 case sw::Shader::OPCODE_DIV:
423                         switch(baseType)
424                         {
425                         case EbtInt:
426                                 return sw::Shader::OPCODE_IDIV;
427                         case EbtUInt:
428                                 return sw::Shader::OPCODE_UDIV;
429                         case EbtFloat:
430                         default:
431                                 return op;
432                         }
433                 case sw::Shader::OPCODE_IMOD:
434                         return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
435                 case sw::Shader::OPCODE_ISHR:
436                         return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
437                 case sw::Shader::OPCODE_MIN:
438                         switch(baseType)
439                         {
440                         case EbtInt:
441                                 return sw::Shader::OPCODE_IMIN;
442                         case EbtUInt:
443                                 return sw::Shader::OPCODE_UMIN;
444                         case EbtFloat:
445                         default:
446                                 return op;
447                         }
448                 case sw::Shader::OPCODE_MAX:
449                         switch(baseType)
450                         {
451                         case EbtInt:
452                                 return sw::Shader::OPCODE_IMAX;
453                         case EbtUInt:
454                                 return sw::Shader::OPCODE_UMAX;
455                         case EbtFloat:
456                         default:
457                                 return op;
458                         }
459                 default:
460                         return op;
461                 }
462         }
463
464         void OutputASM::visitSymbol(TIntermSymbol *symbol)
465         {
466                 // Vertex varyings don't have to be actively used to successfully link
467                 // against pixel shaders that use them. So make sure they're declared.
468                 if(symbol->getQualifier() == EvqVaryingOut || symbol->getQualifier() == EvqInvariantVaryingOut || symbol->getQualifier() == EvqVertexOut)
469                 {
470                         if(symbol->getBasicType() != EbtInvariant)   // Typeless declarations are not new varyings
471                         {
472                                 declareVarying(symbol, -1);
473                         }
474                 }
475
476                 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
477                 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
478                 // "All members of a named uniform block declared with a shared or std140 layout qualifier
479                 // are considered active, even if they are not referenced in any shader in the program.
480                 // The uniform block itself is also considered active, even if no member of the block is referenced."
481                 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
482                 {
483                         uniformRegister(symbol);
484                 }
485         }
486
487         bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
488         {
489                 if(currentScope != emitScope)
490                 {
491                         return false;
492                 }
493
494                 TIntermTyped *result = node;
495                 TIntermTyped *left = node->getLeft();
496                 TIntermTyped *right = node->getRight();
497                 const TType &leftType = left->getType();
498                 const TType &rightType = right->getType();
499
500                 if(isSamplerRegister(result))
501                 {
502                         return false;   // Don't traverse, the register index is determined statically
503                 }
504
505                 switch(node->getOp())
506                 {
507                 case EOpAssign:
508                         if(visit == PostVisit)
509                         {
510                                 assignLvalue(left, right);
511                                 copy(result, right);
512                         }
513                         break;
514                 case EOpInitialize:
515                         if(visit == PostVisit)
516                         {
517                                 copy(left, right);
518                         }
519                         break;
520                 case EOpMatrixTimesScalarAssign:
521                         if(visit == PostVisit)
522                         {
523                                 for(int i = 0; i < leftType.getNominalSize(); i++)
524                                 {
525                                         emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
526                                 }
527
528                                 assignLvalue(left, result);
529                         }
530                         break;
531                 case EOpVectorTimesMatrixAssign:
532                         if(visit == PostVisit)
533                         {
534                                 int size = leftType.getNominalSize();
535
536                                 for(int i = 0; i < size; i++)
537                                 {
538                                         Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
539                                         dot->dst.mask = 1 << i;
540                                 }
541
542                                 assignLvalue(left, result);
543                         }
544                         break;
545                 case EOpMatrixTimesMatrixAssign:
546                         if(visit == PostVisit)
547                         {
548                                 int dim = leftType.getNominalSize();
549
550                                 for(int i = 0; i < dim; i++)
551                                 {
552                                         Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
553                                         mul->src[1].swizzle = 0x00;
554
555                                         for(int j = 1; j < dim; j++)
556                                         {
557                                                 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
558                                                 mad->src[1].swizzle = j * 0x55;
559                                         }
560                                 }
561
562                                 assignLvalue(left, result);
563                         }
564                         break;
565                 case EOpIndexDirect:
566                         if(visit == PostVisit)
567                         {
568                                 int index = right->getAsConstantUnion()->getIConst(0);
569
570                                 if(result->isMatrix() || result->isStruct() || result->isInterfaceBlock())
571                                 {
572                                         ASSERT(left->isArray());
573                                         copy(result, left, index * left->elementRegisterCount());
574                                 }
575                                 else if(result->isRegister())
576                                 {
577                                         int srcIndex = 0;
578                                         if(left->isRegister())
579                                         {
580                                                 srcIndex = 0;
581                                         }
582                                         else if(left->isArray())
583                                         {
584                                                 srcIndex = index * left->elementRegisterCount();
585                                         }
586                                         else if(left->isMatrix())
587                                         {
588                                                 ASSERT(index < left->getNominalSize());   // FIXME: Report semantic error
589                                                 srcIndex = index;
590                                         }
591                                         else UNREACHABLE(0);
592
593                                         Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, 0, left, srcIndex);
594
595                                         if(left->isRegister())
596                                         {
597                                                 mov->src[0].swizzle = index;
598                                         }
599                                 }
600                                 else UNREACHABLE(0);
601                         }
602                         break;
603                 case EOpIndexIndirect:
604                         if(visit == PostVisit)
605                         {
606                                 if(left->isArray() || left->isMatrix())
607                                 {
608                                         for(int index = 0; index < result->totalRegisterCount(); index++)
609                                         {
610                                                 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index, left, index);
611                                                 mov->dst.mask = writeMask(result, index);
612
613                                                 if(left->totalRegisterCount() > 1)
614                                                 {
615                                                         sw::Shader::SourceParameter relativeRegister;
616                                                         argument(relativeRegister, right);
617
618                                                         mov->src[0].rel.type = relativeRegister.type;
619                                                         mov->src[0].rel.index = relativeRegister.index;
620                                                         mov->src[0].rel.scale = result->totalRegisterCount();
621                                                         mov->src[0].rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
622                                                 }
623                                         }
624                                 }
625                                 else if(left->isRegister())
626                                 {
627                                         emit(sw::Shader::OPCODE_EXTRACT, result, left, right);
628                                 }
629                                 else UNREACHABLE(0);
630                         }
631                         break;
632                 case EOpIndexDirectStruct:
633                 case EOpIndexDirectInterfaceBlock:
634                         if(visit == PostVisit)
635                         {
636                                 ASSERT(leftType.isStruct() || (leftType.isInterfaceBlock()));
637
638                                 const TFieldList& fields = (node->getOp() == EOpIndexDirectStruct) ?
639                                                            leftType.getStruct()->fields() :
640                                                            leftType.getInterfaceBlock()->fields();
641                                 int index = right->getAsConstantUnion()->getIConst(0);
642                                 int fieldOffset = 0;
643
644                                 for(int i = 0; i < index; i++)
645                                 {
646                                         fieldOffset += fields[i]->type()->totalRegisterCount();
647                                 }
648
649                                 copy(result, left, fieldOffset);
650                         }
651                         break;
652                 case EOpVectorSwizzle:
653                         if(visit == PostVisit)
654                         {
655                                 int swizzle = 0;
656                                 TIntermAggregate *components = right->getAsAggregate();
657
658                                 if(components)
659                                 {
660                                         TIntermSequence &sequence = components->getSequence();
661                                         int component = 0;
662
663                                         for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
664                                         {
665                                                 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
666
667                                                 if(element)
668                                                 {
669                                                         int i = element->getUnionArrayPointer()[0].getIConst();
670                                                         swizzle |= i << (component * 2);
671                                                         component++;
672                                                 }
673                                                 else UNREACHABLE(0);
674                                         }
675                                 }
676                                 else UNREACHABLE(0);
677
678                                 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
679                                 mov->src[0].swizzle = swizzle;
680                         }
681                         break;
682                 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
683                 case EOpAdd:       if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right);       break;
684                 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
685                 case EOpSub:       if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right);       break;
686                 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
687                 case EOpMul:       if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right);       break;
688                 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
689                 case EOpDiv:       if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right);       break;
690                 case EOpIModAssign:          if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
691                 case EOpIMod:                if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right);       break;
692                 case EOpBitShiftLeftAssign:  if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
693                 case EOpBitShiftLeft:        if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right);       break;
694                 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
695                 case EOpBitShiftRight:       if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right);       break;
696                 case EOpBitwiseAndAssign:    if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
697                 case EOpBitwiseAnd:          if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right);       break;
698                 case EOpBitwiseXorAssign:    if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
699                 case EOpBitwiseXor:          if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right);       break;
700                 case EOpBitwiseOrAssign:     if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right);  break;
701                 case EOpBitwiseOr:           if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right);        break;
702                 case EOpEqual:
703                         if(visit == PostVisit)
704                         {
705                                 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
706
707                                 for(int index = 1; index < left->totalRegisterCount(); index++)
708                                 {
709                                         Temporary equal(this);
710                                         emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
711                                         emit(sw::Shader::OPCODE_AND, result, result, &equal);
712                                 }
713                         }
714                         break;
715                 case EOpNotEqual:
716                         if(visit == PostVisit)
717                         {
718                                 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
719
720                                 for(int index = 1; index < left->totalRegisterCount(); index++)
721                                 {
722                                         Temporary notEqual(this);
723                                         emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
724                                         emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
725                                 }
726                         }
727                         break;
728                 case EOpLessThan:                if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
729                 case EOpGreaterThan:             if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
730                 case EOpLessThanEqual:           if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
731                 case EOpGreaterThanEqual:        if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
732                 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
733                 case EOpVectorTimesScalar:       if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
734                 case EOpMatrixTimesScalar:
735                         if(visit == PostVisit)
736                         {
737                                 if(left->isMatrix())
738                                 {
739                                         for(int i = 0; i < leftType.getNominalSize(); i++)
740                                         {
741                                                 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
742                                         }
743                                 }
744                                 else if(right->isMatrix())
745                                 {
746                                         for(int i = 0; i < rightType.getNominalSize(); i++)
747                                         {
748                                                 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
749                                         }
750                                 }
751                                 else UNREACHABLE(0);
752                         }
753                         break;
754                 case EOpVectorTimesMatrix:
755                         if(visit == PostVisit)
756                         {
757                                 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
758
759                                 int size = rightType.getNominalSize();
760                                 for(int i = 0; i < size; i++)
761                                 {
762                                         Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
763                                         dot->dst.mask = 1 << i;
764                                 }
765                         }
766                         break;
767                 case EOpMatrixTimesVector:
768                         if(visit == PostVisit)
769                         {
770                                 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
771                                 mul->src[1].swizzle = 0x00;
772
773                                 int size = rightType.getNominalSize();
774                                 for(int i = 1; i < size; i++)
775                                 {
776                                         Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
777                                         mad->src[1].swizzle = i * 0x55;
778                                 }
779                         }
780                         break;
781                 case EOpMatrixTimesMatrix:
782                         if(visit == PostVisit)
783                         {
784                                 int dim = leftType.getNominalSize();
785
786                                 int size = rightType.getNominalSize();
787                                 for(int i = 0; i < size; i++)
788                                 {
789                                         Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
790                                         mul->src[1].swizzle = 0x00;
791
792                                         for(int j = 1; j < dim; j++)
793                                         {
794                                                 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
795                                                 mad->src[1].swizzle = j * 0x55;
796                                         }
797                                 }
798                         }
799                         break;
800                 case EOpLogicalOr:
801                         if(trivial(right, 6))
802                         {
803                                 if(visit == PostVisit)
804                                 {
805                                         emit(sw::Shader::OPCODE_OR, result, left, right);
806                                 }
807                         }
808                         else   // Short-circuit evaluation
809                         {
810                                 if(visit == InVisit)
811                                 {
812                                         emit(sw::Shader::OPCODE_MOV, result, left);
813                                         Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
814                                         ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
815                                 }
816                                 else if(visit == PostVisit)
817                                 {
818                                         emit(sw::Shader::OPCODE_MOV, result, right);
819                                         emit(sw::Shader::OPCODE_ENDIF);
820                                 }
821                         }
822                         break;
823                 case EOpLogicalXor:        if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
824                 case EOpLogicalAnd:
825                         if(trivial(right, 6))
826                         {
827                                 if(visit == PostVisit)
828                                 {
829                                         emit(sw::Shader::OPCODE_AND, result, left, right);
830                                 }
831                         }
832                         else   // Short-circuit evaluation
833                         {
834                                 if(visit == InVisit)
835                                 {
836                                         emit(sw::Shader::OPCODE_MOV, result, left);
837                                         emit(sw::Shader::OPCODE_IF, 0, result);
838                                 }
839                                 else if(visit == PostVisit)
840                                 {
841                                         emit(sw::Shader::OPCODE_MOV, result, right);
842                                         emit(sw::Shader::OPCODE_ENDIF);
843                                 }
844                         }
845                         break;
846                 default: UNREACHABLE(node->getOp());
847                 }
848
849                 return true;
850         }
851
852         void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
853         {
854                 switch(size)
855                 {
856                 case 1: // Used for cofactor computation only
857                         {
858                                 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
859                                 bool isMov = (row == col);
860                                 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
861                                 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
862                                 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
863                                 mov->dst.mask = 1 << outRow;
864                         }
865                         break;
866                 case 2:
867                         {
868                                 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
869
870                                 bool isCofactor = (col >= 0) && (row >= 0);
871                                 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
872                                 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
873                                 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
874
875                                 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
876                                 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
877                                 det->dst.mask = 1 << outRow;
878                         }
879                         break;
880                 case 3:
881                         {
882                                 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
883
884                                 bool isCofactor = (col >= 0) && (row >= 0);
885                                 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
886                                 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
887                                 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
888                                 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
889
890                                 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
891                                 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
892                                 det->dst.mask = 1 << outRow;
893                         }
894                         break;
895                 case 4:
896                         {
897                                 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
898                                 det->dst.mask = 1 << outRow;
899                         }
900                         break;
901                 default:
902                         UNREACHABLE(size);
903                         break;
904                 }
905         }
906
907         bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
908         {
909                 if(currentScope != emitScope)
910                 {
911                         return false;
912                 }
913
914                 TIntermTyped *result = node;
915                 TIntermTyped *arg = node->getOperand();
916                 TBasicType basicType = arg->getType().getBasicType();
917
918                 union
919                 {
920                         float f;
921                         int i;
922                 } one_value;
923
924                 if(basicType == EbtInt || basicType == EbtUInt)
925                 {
926                         one_value.i = 1;
927                 }
928                 else
929                 {
930                         one_value.f = 1.0f;
931                 }
932
933                 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
934                 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
935                 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
936
937                 switch(node->getOp())
938                 {
939                 case EOpNegative:
940                         if(visit == PostVisit)
941                         {
942                                 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
943                                 for(int index = 0; index < arg->totalRegisterCount(); index++)
944                                 {
945                                         emit(negOpcode, result, index, arg, index);
946                                 }
947                         }
948                         break;
949                 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
950                 case EOpLogicalNot:       if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
951                 case EOpPostIncrement:
952                         if(visit == PostVisit)
953                         {
954                                 copy(result, arg);
955
956                                 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
957                                 for(int index = 0; index < arg->totalRegisterCount(); index++)
958                                 {
959                                         emit(addOpcode, arg, index, arg, index, &one);
960                                 }
961
962                                 assignLvalue(arg, arg);
963                         }
964                         break;
965                 case EOpPostDecrement:
966                         if(visit == PostVisit)
967                         {
968                                 copy(result, arg);
969
970                                 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
971                                 for(int index = 0; index < arg->totalRegisterCount(); index++)
972                                 {
973                                         emit(subOpcode, arg, index, arg, index, &one);
974                                 }
975
976                                 assignLvalue(arg, arg);
977                         }
978                         break;
979                 case EOpPreIncrement:
980                         if(visit == PostVisit)
981                         {
982                                 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
983                                 for(int index = 0; index < arg->totalRegisterCount(); index++)
984                                 {
985                                         emit(addOpcode, result, index, arg, index, &one);
986                                 }
987
988                                 assignLvalue(arg, result);
989                         }
990                         break;
991                 case EOpPreDecrement:
992                         if(visit == PostVisit)
993                         {
994                                 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
995                                 for(int index = 0; index < arg->totalRegisterCount(); index++)
996                                 {
997                                         emit(subOpcode, result, index, arg, index, &one);
998                                 }
999
1000                                 assignLvalue(arg, result);
1001                         }
1002                         break;
1003                 case EOpRadians:          if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1004                 case EOpDegrees:          if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1005                 case EOpSin:              if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1006                 case EOpCos:              if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1007                 case EOpTan:              if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1008                 case EOpAsin:             if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1009                 case EOpAcos:             if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1010                 case EOpAtan:             if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1011                 case EOpSinh:             if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1012                 case EOpCosh:             if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1013                 case EOpTanh:             if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1014                 case EOpAsinh:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1015                 case EOpAcosh:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1016                 case EOpAtanh:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1017                 case EOpExp:              if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1018                 case EOpLog:              if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1019                 case EOpExp2:             if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1020                 case EOpLog2:             if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1021                 case EOpSqrt:             if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1022                 case EOpInverseSqrt:      if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1023                 case EOpAbs:              if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1024                 case EOpSign:             if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1025                 case EOpFloor:            if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1026                 case EOpTrunc:            if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1027                 case EOpRound:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1028                 case EOpRoundEven:        if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1029                 case EOpCeil:             if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1030                 case EOpFract:            if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1031                 case EOpIsNan:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1032                 case EOpIsInf:            if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1033                 case EOpLength:           if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1034                 case EOpNormalize:        if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1035                 case EOpDFdx:             if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1036                 case EOpDFdy:             if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1037                 case EOpFwidth:           if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1038                 case EOpAny:              if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1039                 case EOpAll:              if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1040                 case EOpFloatBitsToInt:   if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1041                 case EOpFloatBitsToUint:  if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1042                 case EOpIntBitsToFloat:   if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1043                 case EOpUintBitsToFloat:  if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1044                 case EOpPackSnorm2x16:    if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1045                 case EOpPackUnorm2x16:    if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1046                 case EOpPackHalf2x16:     if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1047                 case EOpUnpackSnorm2x16:  if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1048                 case EOpUnpackUnorm2x16:  if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1049                 case EOpUnpackHalf2x16:   if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1050                 case EOpTranspose:
1051                         if(visit == PostVisit)
1052                         {
1053                                 int numCols = arg->getNominalSize();
1054                                 int numRows = arg->getSecondarySize();
1055                                 for(int i = 0; i < numCols; ++i)
1056                                 {
1057                                         for(int j = 0; j < numRows; ++j)
1058                                         {
1059                                                 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1060                                                 mov->src[0].swizzle = 0x55 * j;
1061                                                 mov->dst.mask = 1 << i;
1062                                         }
1063                                 }
1064                         }
1065                         break;
1066                 case EOpDeterminant:
1067                         if(visit == PostVisit)
1068                         {
1069                                 int size = arg->getNominalSize();
1070                                 ASSERT(size == arg->getSecondarySize());
1071
1072                                 emitDeterminant(result, arg, size);
1073                         }
1074                         break;
1075                 case EOpInverse:
1076                         if(visit == PostVisit)
1077                         {
1078                                 int size = arg->getNominalSize();
1079                                 ASSERT(size == arg->getSecondarySize());
1080
1081                                 // Compute transposed matrix of cofactors
1082                                 for(int i = 0; i < size; ++i)
1083                                 {
1084                                         for(int j = 0; j < size; ++j)
1085                                         {
1086                                                 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1087                                                 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1088                                                 emitDeterminant(result, arg, size - 1, j, i, i, j);
1089                                         }
1090                                 }
1091
1092                                 // Compute 1 / determinant
1093                                 Temporary invDet(this);
1094                                 emitDeterminant(&invDet, arg, size);
1095                                 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1096                                 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1097                                 div->src[1].swizzle = 0x00; // xxxx
1098
1099                                 // Divide transposed matrix of cofactors by determinant
1100                                 for(int i = 0; i < size; ++i)
1101                                 {
1102                                         emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1103                                 }
1104                         }
1105                         break;
1106                 default: UNREACHABLE(node->getOp());
1107                 }
1108
1109                 return true;
1110         }
1111
1112         bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1113         {
1114                 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1115                 {
1116                         return false;
1117                 }
1118
1119                 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1120
1121                 TIntermTyped *result = node;
1122                 const TType &resultType = node->getType();
1123                 TIntermSequence &arg = node->getSequence();
1124                 size_t argumentCount = arg.size();
1125
1126                 switch(node->getOp())
1127                 {
1128                 case EOpSequence:             break;
1129                 case EOpDeclaration:          break;
1130                 case EOpInvariantDeclaration: break;
1131                 case EOpPrototype:            break;
1132                 case EOpComma:
1133                         if(visit == PostVisit)
1134                         {
1135                                 copy(result, arg[1]);
1136                         }
1137                         break;
1138                 case EOpFunction:
1139                         if(visit == PreVisit)
1140                         {
1141                                 const TString &name = node->getName();
1142
1143                                 if(emitScope == FUNCTION)
1144                                 {
1145                                         if(functionArray.size() > 1)   // No need for a label when there's only main()
1146                                         {
1147                                                 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1148                                                 label->dst.type = sw::Shader::PARAMETER_LABEL;
1149
1150                                                 const Function *function = findFunction(name);
1151                                                 ASSERT(function);   // Should have been added during global pass
1152                                                 label->dst.index = function->label;
1153                                                 currentFunction = function->label;
1154                                         }
1155                                 }
1156                                 else if(emitScope == GLOBAL)
1157                                 {
1158                                         if(name != "main(")
1159                                         {
1160                                                 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1161                                                 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1162                                         }
1163                                 }
1164                                 else UNREACHABLE(emitScope);
1165
1166                                 currentScope = FUNCTION;
1167                         }
1168                         else if(visit == PostVisit)
1169                         {
1170                                 if(emitScope == FUNCTION)
1171                                 {
1172                                         if(functionArray.size() > 1)   // No need to return when there's only main()
1173                                         {
1174                                                 emit(sw::Shader::OPCODE_RET);
1175                                         }
1176                                 }
1177
1178                                 currentScope = GLOBAL;
1179                         }
1180                         break;
1181                 case EOpFunctionCall:
1182                         if(visit == PostVisit)
1183                         {
1184                                 if(node->isUserDefined())
1185                                 {
1186                                         const TString &name = node->getName();
1187                                         const Function *function = findFunction(name);
1188
1189                                         if(!function)
1190                                         {
1191                                                 mContext.error(node->getLine(), "function definition not found", name.c_str());
1192                                                 return false;
1193                                         }
1194
1195                                         TIntermSequence &arguments = *function->arg;
1196
1197                                         for(size_t i = 0; i < argumentCount; i++)
1198                                         {
1199                                                 TIntermTyped *in = arguments[i]->getAsTyped();
1200
1201                                                 if(in->getQualifier() == EvqIn ||
1202                                                    in->getQualifier() == EvqInOut ||
1203                                                    in->getQualifier() == EvqConstReadOnly)
1204                                                 {
1205                                                         copy(in, arg[i]);
1206                                                 }
1207                                         }
1208
1209                                         Instruction *call = emit(sw::Shader::OPCODE_CALL);
1210                                         call->dst.type = sw::Shader::PARAMETER_LABEL;
1211                                         call->dst.index = function->label;
1212
1213                                         if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1214                                         {
1215                                                 copy(result, function->ret);
1216                                         }
1217
1218                                         for(size_t i = 0; i < argumentCount; i++)
1219                                         {
1220                                                 TIntermTyped *argument = arguments[i]->getAsTyped();
1221                                                 TIntermTyped *out = arg[i]->getAsTyped();
1222
1223                                                 if(argument->getQualifier() == EvqOut ||
1224                                                    argument->getQualifier() == EvqInOut)
1225                                                 {
1226                                                         assignLvalue(out, argument);
1227                                                 }
1228                                         }
1229                                 }
1230                                 else
1231                                 {
1232                                         const TextureFunction textureFunction(node->getName());
1233                                         TIntermTyped *t = arg[1]->getAsTyped();
1234
1235                                         Temporary coord(this);
1236
1237                                         if(textureFunction.proj)
1238                                         {
1239                                                 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1240                                                 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1241                                                 rcp->dst.mask = 0x7;
1242
1243                                                 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1244                                                 mul->dst.mask = 0x7;
1245                                         }
1246                                         else
1247                                         {
1248                                                 emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1249                                         }
1250
1251                                         switch(textureFunction.method)
1252                                         {
1253                                         case TextureFunction::IMPLICIT:
1254                                                 {
1255                                                         TIntermNode* offset = textureFunction.offset ? arg[2] : 0;
1256
1257                                                         if(argumentCount == 2 || (textureFunction.offset && argumentCount == 3))
1258                                                         {
1259                                                                 emit(textureFunction.offset ? sw::Shader::OPCODE_TEXOFFSET : sw::Shader::OPCODE_TEX,
1260                                                                      result, &coord, arg[0], offset);
1261                                                         }
1262                                                         else if(argumentCount == 3 || (textureFunction.offset && argumentCount == 4))   // bias
1263                                                         {
1264                                                                 Instruction *bias = emit(sw::Shader::OPCODE_MOV, &coord, arg[textureFunction.offset ? 3 : 2]);
1265                                                                 bias->dst.mask = 0x8;
1266
1267                                                                 Instruction *tex = emit(textureFunction.offset ? sw::Shader::OPCODE_TEXOFFSET : sw::Shader::OPCODE_TEX,
1268                                                                                         result, &coord, arg[0], offset); // FIXME: Implement an efficient TEXLDB instruction
1269                                                                 tex->bias = true;
1270                                                         }
1271                                                         else UNREACHABLE(argumentCount);
1272                                                 }
1273                                                 break;
1274                                         case TextureFunction::LOD:
1275                                                 {
1276                                                         Instruction *lod = emit(sw::Shader::OPCODE_MOV, &coord, arg[2]);
1277                                                         lod->dst.mask = 0x8;
1278
1279                                                         emit(textureFunction.offset ? sw::Shader::OPCODE_TEXLDLOFFSET : sw::Shader::OPCODE_TEXLDL,
1280                                                              result, &coord, arg[0], textureFunction.offset ? arg[3] : nullptr);
1281                                                 }
1282                                                 break;
1283                                         case TextureFunction::FETCH:
1284                                                 {
1285                                                         if(argumentCount == 3 || (textureFunction.offset && argumentCount == 4))
1286                                                         {
1287                                                                 Instruction *lod = emit(sw::Shader::OPCODE_MOV, &coord, arg[2]);
1288                                                                 lod->dst.mask = 0x8;
1289
1290                                                                 TIntermNode *offset = textureFunction.offset ? arg[3] : nullptr;
1291
1292                                                                 emit(textureFunction.offset ? sw::Shader::OPCODE_TEXELFETCHOFFSET : sw::Shader::OPCODE_TEXELFETCH,
1293                                                                      result, &coord, arg[0], offset);
1294                                                         }
1295                                                         else UNREACHABLE(argumentCount);
1296                                                 }
1297                                                 break;
1298                                         case TextureFunction::GRAD:
1299                                                 {
1300                                                         if(argumentCount == 4 || (textureFunction.offset && argumentCount == 5))
1301                                                         {
1302                                                                 TIntermNode *offset = textureFunction.offset ? arg[4] : nullptr;
1303
1304                                                                 emit(textureFunction.offset ? sw::Shader::OPCODE_TEXGRADOFFSET : sw::Shader::OPCODE_TEXGRAD,
1305                                                                      result, &coord, arg[0], arg[2], arg[3], offset);
1306                                                         }
1307                                                         else UNREACHABLE(argumentCount);
1308                                                 }
1309                                                 break;
1310                                         case TextureFunction::SIZE:
1311                                                 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], arg[0]);
1312                                                 break;
1313                                         default:
1314                                                 UNREACHABLE(textureFunction.method);
1315                                         }
1316                                 }
1317                         }
1318                         break;
1319                 case EOpParameters:
1320                         break;
1321                 case EOpConstructFloat:
1322                 case EOpConstructVec2:
1323                 case EOpConstructVec3:
1324                 case EOpConstructVec4:
1325                 case EOpConstructBool:
1326                 case EOpConstructBVec2:
1327                 case EOpConstructBVec3:
1328                 case EOpConstructBVec4:
1329                 case EOpConstructInt:
1330                 case EOpConstructIVec2:
1331                 case EOpConstructIVec3:
1332                 case EOpConstructIVec4:
1333                 case EOpConstructUInt:
1334                 case EOpConstructUVec2:
1335                 case EOpConstructUVec3:
1336                 case EOpConstructUVec4:
1337                         if(visit == PostVisit)
1338                         {
1339                                 int component = 0;
1340                                 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1341                                 int arrayComponents = result->getType().getElementSize();
1342                                 for(size_t i = 0; i < argumentCount; i++)
1343                                 {
1344                                         TIntermTyped *argi = arg[i]->getAsTyped();
1345                                         int size = argi->getNominalSize();
1346                                         int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1347                                         int swizzle = component - (arrayIndex * arrayComponents);
1348
1349                                         if(!argi->isMatrix())
1350                                         {
1351                                                 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1352                                                 mov->dst.mask = (0xF << swizzle) & 0xF;
1353                                                 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1354
1355                                                 component += size;
1356                                         }
1357                                         else   // Matrix
1358                                         {
1359                                                 int column = 0;
1360
1361                                                 while(component < resultType.getNominalSize())
1362                                                 {
1363                                                         Instruction *mov = emitCast(result, arrayIndex, argi, column);
1364                                                         mov->dst.mask = (0xF << swizzle) & 0xF;
1365                                                         mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1366
1367                                                         column++;
1368                                                         component += size;
1369                                                 }
1370                                         }
1371                                 }
1372                         }
1373                         break;
1374                 case EOpConstructMat2:
1375                 case EOpConstructMat2x3:
1376                 case EOpConstructMat2x4:
1377                 case EOpConstructMat3x2:
1378                 case EOpConstructMat3:
1379                 case EOpConstructMat3x4:
1380                 case EOpConstructMat4x2:
1381                 case EOpConstructMat4x3:
1382                 case EOpConstructMat4:
1383                         if(visit == PostVisit)
1384                         {
1385                                 TIntermTyped *arg0 = arg[0]->getAsTyped();
1386                                 const int outCols = result->getNominalSize();
1387                                 const int outRows = result->getSecondarySize();
1388
1389                                 if(arg0->isScalar() && arg.size() == 1)   // Construct scale matrix
1390                                 {
1391                                         for(int i = 0; i < outCols; i++)
1392                                         {
1393                                                 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
1394                                                 Instruction *mov = emitCast(result, i, arg0, 0);
1395                                                 mov->dst.mask = 1 << i;
1396                                                 ASSERT(mov->src[0].swizzle == 0x00);
1397                                         }
1398                                 }
1399                                 else if(arg0->isMatrix())
1400                                 {
1401                                         int arraySize = result->isArray() ? result->getArraySize() : 1;
1402
1403                                         for(int n = 0; n < arraySize; n++)
1404                                         {
1405                                                 TIntermTyped *argi = arg[n]->getAsTyped();
1406                                                 const int inCols = argi->getNominalSize();
1407                                                 const int inRows = argi->getSecondarySize();
1408
1409                                                 for(int i = 0; i < outCols; i++)
1410                                                 {
1411                                                         if(i >= inCols || outRows > inRows)
1412                                                         {
1413                                                                 // Initialize to identity matrix
1414                                                                 Constant col((i == 0 ? 1.0f : 0.0f), (i == 1 ? 1.0f : 0.0f), (i == 2 ? 1.0f : 0.0f), (i == 3 ? 1.0f : 0.0f));
1415                                                                 emitCast(result, i + n * outCols, &col, 0);
1416                                                         }
1417
1418                                                         if(i < inCols)
1419                                                         {
1420                                                                 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1421                                                                 mov->dst.mask = 0xF >> (4 - inRows);
1422                                                         }
1423                                                 }
1424                                         }
1425                                 }
1426                                 else
1427                                 {
1428                                         int column = 0;
1429                                         int row = 0;
1430
1431                                         for(size_t i = 0; i < argumentCount; i++)
1432                                         {
1433                                                 TIntermTyped *argi = arg[i]->getAsTyped();
1434                                                 int size = argi->getNominalSize();
1435                                                 int element = 0;
1436
1437                                                 while(element < size)
1438                                                 {
1439                                                         Instruction *mov = emitCast(result, column, argi, 0);
1440                                                         mov->dst.mask = (0xF << row) & 0xF;
1441                                                         mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1442
1443                                                         int end = row + size - element;
1444                                                         column = end >= outRows ? column + 1 : column;
1445                                                         element = element + outRows - row;
1446                                                         row = end >= outRows ? 0 : end;
1447                                                 }
1448                                         }
1449                                 }
1450                         }
1451                         break;
1452                 case EOpConstructStruct:
1453                         if(visit == PostVisit)
1454                         {
1455                                 int offset = 0;
1456                                 for(size_t i = 0; i < argumentCount; i++)
1457                                 {
1458                                         TIntermTyped *argi = arg[i]->getAsTyped();
1459                                         int size = argi->totalRegisterCount();
1460
1461                                         for(int index = 0; index < size; index++)
1462                                         {
1463                                                 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1464                                                 mov->dst.mask = writeMask(result, offset + index);
1465                                         }
1466
1467                                         offset += size;
1468                                 }
1469                         }
1470                         break;
1471                 case EOpLessThan:         if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1472                 case EOpGreaterThan:      if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1473                 case EOpLessThanEqual:    if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1474                 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1475                 case EOpVectorEqual:      if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1476                 case EOpVectorNotEqual:   if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1477                 case EOpMod:              if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1478                 case EOpModf:
1479                         if(visit == PostVisit)
1480                         {
1481                                 TIntermTyped* arg1 = arg[1]->getAsTyped();
1482                                 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1483                                 assignLvalue(arg1, arg1);
1484                                 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1485                         }
1486                         break;
1487                 case EOpPow:              if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1488                 case EOpAtan:             if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1489                 case EOpMin:              if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1490                 case EOpMax:              if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1491                 case EOpClamp:
1492                         if(visit == PostVisit)
1493                         {
1494                                 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1495                                 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1496                         }
1497                         break;
1498                 case EOpMix:         if(visit == PostVisit) emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]); break;
1499                 case EOpStep:        if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1500                 case EOpSmoothStep:  if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1501                 case EOpDistance:    if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1502                 case EOpDot:         if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1503                 case EOpCross:       if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1504                 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1505                 case EOpReflect:     if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1506                 case EOpRefract:     if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1507                 case EOpMul:
1508                         if(visit == PostVisit)
1509                         {
1510                                 TIntermTyped *arg0 = arg[0]->getAsTyped();
1511                                 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1512                                        (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
1513
1514                                 int size = arg0->getNominalSize();
1515                                 for(int i = 0; i < size; i++)
1516                                 {
1517                                         emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1518                                 }
1519                         }
1520                         break;
1521                 case EOpOuterProduct:
1522                         if(visit == PostVisit)
1523                         {
1524                                 for(int i = 0; i < dim(arg[1]); i++)
1525                                 {
1526                                         Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1527                                         mul->src[1].swizzle = 0x55 * i;
1528                                 }
1529                         }
1530                         break;
1531                 default: UNREACHABLE(node->getOp());
1532                 }
1533
1534                 return true;
1535         }
1536
1537         bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1538         {
1539                 if(currentScope != emitScope)
1540                 {
1541                         return false;
1542                 }
1543
1544                 TIntermTyped *condition = node->getCondition();
1545                 TIntermNode *trueBlock = node->getTrueBlock();
1546                 TIntermNode *falseBlock = node->getFalseBlock();
1547                 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1548
1549                 condition->traverse(this);
1550
1551                 if(node->usesTernaryOperator())
1552                 {
1553                         if(constantCondition)
1554                         {
1555                                 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1556
1557                                 if(trueCondition)
1558                                 {
1559                                         trueBlock->traverse(this);
1560                                         copy(node, trueBlock);
1561                                 }
1562                                 else
1563                                 {
1564                                         falseBlock->traverse(this);
1565                                         copy(node, falseBlock);
1566                                 }
1567                         }
1568                         else if(trivial(node, 6))   // Fast to compute both potential results and no side effects
1569                         {
1570                                 trueBlock->traverse(this);
1571                                 falseBlock->traverse(this);
1572                                 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1573                         }
1574                         else
1575                         {
1576                                 emit(sw::Shader::OPCODE_IF, 0, condition);
1577
1578                                 if(trueBlock)
1579                                 {
1580                                         trueBlock->traverse(this);
1581                                         copy(node, trueBlock);
1582                                 }
1583
1584                                 if(falseBlock)
1585                                 {
1586                                         emit(sw::Shader::OPCODE_ELSE);
1587                                         falseBlock->traverse(this);
1588                                         copy(node, falseBlock);
1589                                 }
1590
1591                                 emit(sw::Shader::OPCODE_ENDIF);
1592                         }
1593                 }
1594                 else  // if/else statement
1595                 {
1596                         if(constantCondition)
1597                         {
1598                                 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1599
1600                                 if(trueCondition)
1601                                 {
1602                                         if(trueBlock)
1603                                         {
1604                                                 trueBlock->traverse(this);
1605                                         }
1606                                 }
1607                                 else
1608                                 {
1609                                         if(falseBlock)
1610                                         {
1611                                                 falseBlock->traverse(this);
1612                                         }
1613                                 }
1614                         }
1615                         else
1616                         {
1617                                 emit(sw::Shader::OPCODE_IF, 0, condition);
1618
1619                                 if(trueBlock)
1620                                 {
1621                                         trueBlock->traverse(this);
1622                                 }
1623
1624                                 if(falseBlock)
1625                                 {
1626                                         emit(sw::Shader::OPCODE_ELSE);
1627                                         falseBlock->traverse(this);
1628                                 }
1629
1630                                 emit(sw::Shader::OPCODE_ENDIF);
1631                         }
1632                 }
1633
1634                 return false;
1635         }
1636
1637         bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1638         {
1639                 if(currentScope != emitScope)
1640                 {
1641                         return false;
1642                 }
1643
1644                 unsigned int iterations = loopCount(node);
1645
1646                 if(iterations == 0)
1647                 {
1648                         return false;
1649                 }
1650
1651                 bool unroll = (iterations <= 4);
1652
1653                 if(unroll)
1654                 {
1655                         LoopUnrollable loopUnrollable;
1656                         unroll = loopUnrollable.traverse(node);
1657                 }
1658
1659                 TIntermNode *init = node->getInit();
1660                 TIntermTyped *condition = node->getCondition();
1661                 TIntermTyped *expression = node->getExpression();
1662                 TIntermNode *body = node->getBody();
1663                 Constant True(true);
1664
1665                 if(node->getType() == ELoopDoWhile)
1666                 {
1667                         Temporary iterate(this);
1668                         emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1669
1670                         emit(sw::Shader::OPCODE_WHILE, 0, &iterate);   // FIXME: Implement real do-while
1671
1672                         if(body)
1673                         {
1674                                 body->traverse(this);
1675                         }
1676
1677                         emit(sw::Shader::OPCODE_TEST);
1678
1679                         condition->traverse(this);
1680                         emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1681
1682                         emit(sw::Shader::OPCODE_ENDWHILE);
1683                 }
1684                 else
1685                 {
1686                         if(init)
1687                         {
1688                                 init->traverse(this);
1689                         }
1690
1691                         if(unroll)
1692                         {
1693                                 for(unsigned int i = 0; i < iterations; i++)
1694                                 {
1695                                 //      condition->traverse(this);   // Condition could contain statements, but not in an unrollable loop
1696
1697                                         if(body)
1698                                         {
1699                                                 body->traverse(this);
1700                                         }
1701
1702                                         if(expression)
1703                                         {
1704                                                 expression->traverse(this);
1705                                         }
1706                                 }
1707                         }
1708                         else
1709                         {
1710                                 if(condition)
1711                                 {
1712                                         condition->traverse(this);
1713                                 }
1714                                 else
1715                                 {
1716                                         condition = &True;
1717                                 }
1718
1719                                 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1720
1721                                 if(body)
1722                                 {
1723                                         body->traverse(this);
1724                                 }
1725
1726                                 emit(sw::Shader::OPCODE_TEST);
1727
1728                                 if(expression)
1729                                 {
1730                                         expression->traverse(this);
1731                                 }
1732
1733                                 if(condition)
1734                                 {
1735                                         condition->traverse(this);
1736                                 }
1737
1738                                 emit(sw::Shader::OPCODE_ENDWHILE);
1739                         }
1740                 }
1741
1742                 return false;
1743         }
1744
1745         bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1746         {
1747                 if(currentScope != emitScope)
1748                 {
1749                         return false;
1750                 }
1751
1752                 switch(node->getFlowOp())
1753                 {
1754                 case EOpKill:      if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD);  break;
1755                 case EOpBreak:     if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK);    break;
1756                 case EOpContinue:  if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1757                 case EOpReturn:
1758                         if(visit == PostVisit)
1759                         {
1760                                 TIntermTyped *value = node->getExpression();
1761
1762                                 if(value)
1763                                 {
1764                                         copy(functionArray[currentFunction].ret, value);
1765                                 }
1766
1767                                 emit(sw::Shader::OPCODE_LEAVE);
1768                         }
1769                         break;
1770                 default: UNREACHABLE(node->getFlowOp());
1771                 }
1772
1773                 return true;
1774         }
1775
1776         bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1777         {
1778                 if(currentScope != emitScope)
1779                 {
1780                         return false;
1781                 }
1782
1783                 TIntermTyped* switchValue = node->getInit();
1784                 TIntermAggregate* opList = node->getStatementList();
1785
1786                 if(!switchValue || !opList)
1787                 {
1788                         return false;
1789                 }
1790
1791                 switchValue->traverse(this);
1792
1793                 emit(sw::Shader::OPCODE_SWITCH);
1794
1795                 TIntermSequence& sequence = opList->getSequence();
1796                 TIntermSequence::iterator it = sequence.begin();
1797                 TIntermSequence::iterator defaultIt = sequence.end();
1798                 int nbCases = 0;
1799                 for(; it != sequence.end(); ++it)
1800                 {
1801                         TIntermCase* currentCase = (*it)->getAsCaseNode();
1802                         if(currentCase)
1803                         {
1804                                 TIntermSequence::iterator caseIt = it;
1805
1806                                 TIntermTyped* condition = currentCase->getCondition();
1807                                 if(condition) // non default case
1808                                 {
1809                                         if(nbCases != 0)
1810                                         {
1811                                                 emit(sw::Shader::OPCODE_ELSE);
1812                                         }
1813
1814                                         condition->traverse(this);
1815                                         Temporary result(this);
1816                                         emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1817                                         emit(sw::Shader::OPCODE_IF, 0, &result);
1818                                         nbCases++;
1819
1820                                         for(++caseIt; caseIt != sequence.end(); ++caseIt)
1821                                         {
1822                                                 (*caseIt)->traverse(this);
1823                                                 if((*caseIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1824                                                 {
1825                                                         break;
1826                                                 }
1827                                         }
1828                                 }
1829                                 else
1830                                 {
1831                                         defaultIt = it; // The default case might not be the last case, keep it for last
1832                                 }
1833                         }
1834                 }
1835
1836                 // If there's a default case, traverse it here
1837                 if(defaultIt != sequence.end())
1838                 {
1839                         emit(sw::Shader::OPCODE_ELSE);
1840                         for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
1841                         {
1842                                 (*defaultIt)->traverse(this);
1843                                 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1844                                 {
1845                                         break;
1846                                 }
1847                         }
1848                 }
1849
1850                 for(int i = 0; i < nbCases; ++i)
1851                 {
1852                         emit(sw::Shader::OPCODE_ENDIF);
1853                 }
1854
1855                 emit(sw::Shader::OPCODE_ENDSWITCH);
1856
1857                 return false;
1858         }
1859
1860         Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
1861         {
1862                 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
1863         }
1864
1865         Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
1866                                      TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
1867         {
1868                 Instruction *instruction = new Instruction(op);
1869
1870                 if(dst)
1871                 {
1872                         instruction->dst.type = registerType(dst);
1873                         instruction->dst.index = registerIndex(dst) + dstIndex;
1874                         instruction->dst.mask = writeMask(dst);
1875                         instruction->dst.integer = (dst->getBasicType() == EbtInt);
1876                 }
1877
1878                 argument(instruction->src[0], src0, index0);
1879                 argument(instruction->src[1], src1, index1);
1880                 argument(instruction->src[2], src2, index2);
1881                 argument(instruction->src[3], src3, index3);
1882                 argument(instruction->src[4], src4, index4);
1883
1884                 shader->append(instruction);
1885
1886                 return instruction;
1887         }
1888
1889         Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
1890         {
1891                 return emitCast(dst, 0, src, 0);
1892         }
1893
1894         Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
1895         {
1896                 switch(src->getBasicType())
1897                 {
1898                 case EbtBool:
1899                         switch(dst->getBasicType())
1900                         {
1901                         case EbtInt:   return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1902                         case EbtUInt:  return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1903                         case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
1904                         default:       break;
1905                         }
1906                         break;
1907                 case EbtInt:
1908                         switch(dst->getBasicType())
1909                         {
1910                         case EbtBool:  return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1911                         case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
1912                         default:       break;
1913                         }
1914                         break;
1915                 case EbtUInt:
1916                         switch(dst->getBasicType())
1917                         {
1918                         case EbtBool:  return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1919                         case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
1920                         default:       break;
1921                         }
1922                         break;
1923                 case EbtFloat:
1924                         switch(dst->getBasicType())
1925                         {
1926                         case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
1927                         case EbtInt:  return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
1928                         case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
1929                         default:      break;
1930                         }
1931                         break;
1932                 default:
1933                         break;
1934                 }
1935
1936                 ASSERT((src->getBasicType() == dst->getBasicType()) ||
1937                       ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
1938                       ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
1939
1940                 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
1941         }
1942
1943         void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1944         {
1945                 for(int index = 0; index < dst->elementRegisterCount(); index++)
1946                 {
1947                         emit(op, dst, index, src0, index, src1, index, src2, index);
1948                 }
1949         }
1950
1951         void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1952         {
1953                 emitBinary(op, result, src0, src1);
1954                 assignLvalue(lhs, result);
1955         }
1956
1957         void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1958         {
1959                 sw::Shader::Opcode opcode;
1960                 switch(left->getAsTyped()->getBasicType())
1961                 {
1962                 case EbtBool:
1963                 case EbtInt:
1964                         opcode = sw::Shader::OPCODE_ICMP;
1965                         break;
1966                 case EbtUInt:
1967                         opcode = sw::Shader::OPCODE_UCMP;
1968                         break;
1969                 default:
1970                         opcode = sw::Shader::OPCODE_CMP;
1971                         break;
1972                 }
1973
1974                 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
1975                 cmp->control = cmpOp;
1976         }
1977
1978         int componentCount(const TType &type, int registers)
1979         {
1980                 if(registers == 0)
1981                 {
1982                         return 0;
1983                 }
1984
1985                 if(type.isArray() && registers >= type.elementRegisterCount())
1986                 {
1987                         int index = registers / type.elementRegisterCount();
1988                         registers -= index * type.elementRegisterCount();
1989                         return index * type.getElementSize() + componentCount(type, registers);
1990                 }
1991
1992                 if(type.isStruct() || type.isInterfaceBlock())
1993                 {
1994                         const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
1995                         int elements = 0;
1996
1997                         for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
1998                         {
1999                                 const TType &fieldType = *((*field)->type());
2000
2001                                 if(fieldType.totalRegisterCount() <= registers)
2002                                 {
2003                                         registers -= fieldType.totalRegisterCount();
2004                                         elements += fieldType.getObjectSize();
2005                                 }
2006                                 else   // Register within this field
2007                                 {
2008                                         return elements + componentCount(fieldType, registers);
2009                                 }
2010                         }
2011                 }
2012                 else if(type.isMatrix())
2013                 {
2014                         return registers * type.registerSize();
2015                 }
2016
2017                 UNREACHABLE(0);
2018                 return 0;
2019         }
2020
2021         int registerSize(const TType &type, int registers)
2022         {
2023                 if(registers == 0)
2024                 {
2025                         if(type.isStruct())
2026                         {
2027                                 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2028                         }
2029                         else if(type.isInterfaceBlock())
2030                         {
2031                                 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2032                         }
2033
2034                         return type.registerSize();
2035                 }
2036
2037                 if(type.isArray() && registers >= type.elementRegisterCount())
2038                 {
2039                         int index = registers / type.elementRegisterCount();
2040                         registers -= index * type.elementRegisterCount();
2041                         return registerSize(type, registers);
2042                 }
2043
2044                 if(type.isStruct() || type.isInterfaceBlock())
2045                 {
2046                         const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2047                         int elements = 0;
2048
2049                         for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2050                         {
2051                                 const TType &fieldType = *((*field)->type());
2052
2053                                 if(fieldType.totalRegisterCount() <= registers)
2054                                 {
2055                                         registers -= fieldType.totalRegisterCount();
2056                                         elements += fieldType.getObjectSize();
2057                                 }
2058                                 else   // Register within this field
2059                                 {
2060                                         return registerSize(fieldType, registers);
2061                                 }
2062                         }
2063                 }
2064                 else if(type.isMatrix())
2065                 {
2066                         return registerSize(type, 0);
2067                 }
2068
2069                 UNREACHABLE(0);
2070                 return 0;
2071         }
2072
2073         int OutputASM::getBlockId(TIntermTyped *arg)
2074         {
2075                 if(arg)
2076                 {
2077                         const TType &type = arg->getType();
2078                         TInterfaceBlock* block = type.getInterfaceBlock();
2079                         if(block && (type.getQualifier() == EvqUniform))
2080                         {
2081                                 // Make sure the uniform block is declared
2082                                 uniformRegister(arg);
2083
2084                                 const char* blockName = block->name().c_str();
2085
2086                                 // Fetch uniform block index from array of blocks
2087                                 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2088                                 {
2089                                         if(blockName == it->name)
2090                                         {
2091                                                 return it->blockId;
2092                                         }
2093                                 }
2094
2095                                 ASSERT(false);
2096                         }
2097                 }
2098
2099                 return -1;
2100         }
2101
2102         OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2103         {
2104                 const TType &type = arg->getType();
2105                 int blockId = getBlockId(arg);
2106                 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2107                 if(blockId != -1)
2108                 {
2109                         argumentInfo.bufferIndex = 0;
2110                         for(int i = 0; i < blockId; ++i)
2111                         {
2112                                 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2113                                 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2114                         }
2115
2116                         const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2117
2118                         BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2119                         BlockDefinitionIndexMap::const_iterator it = itEnd;
2120
2121                         argumentInfo.clampedIndex = index;
2122                         if(type.isInterfaceBlock())
2123                         {
2124                                 // Offset index to the beginning of the selected instance
2125                                 int blockRegisters = type.elementRegisterCount();
2126                                 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2127                                 argumentInfo.bufferIndex += bufferOffset;
2128                                 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2129                         }
2130
2131                         int regIndex = registerIndex(arg);
2132                         for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2133                         {
2134                                 it = blockDefinition.find(i);
2135                                 if(it != itEnd)
2136                                 {
2137                                         argumentInfo.clampedIndex -= (i - regIndex);
2138                                         break;
2139                                 }
2140                         }
2141                         ASSERT(it != itEnd);
2142
2143                         argumentInfo.typedMemberInfo = it->second;
2144
2145                         int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2146                         argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2147                 }
2148                 else
2149                 {
2150                         argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2151                 }
2152
2153                 return argumentInfo;
2154         }
2155
2156         void OutputASM::argument(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
2157         {
2158                 if(argument)
2159                 {
2160                         TIntermTyped *arg = argument->getAsTyped();
2161                         Temporary unpackedUniform(this);
2162
2163                         const TType& srcType = arg->getType();
2164                         TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2165                         if(srcBlock && (srcType.getQualifier() == EvqUniform))
2166                         {
2167                                 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2168                                 const TType &memberType = argumentInfo.typedMemberInfo.type;
2169
2170                                 if(memberType.getBasicType() == EbtBool)
2171                                 {
2172                                         ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
2173
2174                                         // Convert the packed bool, which is currently an int, to a true bool
2175                                         Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2176                                         instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2177                                         instruction->dst.index = registerIndex(&unpackedUniform);
2178                                         instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2179                                         instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2180                                         instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2181
2182                                         shader->append(instruction);
2183
2184                                         arg = &unpackedUniform;
2185                                         index = 0;
2186                                 }
2187                                 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2188                                 {
2189                                         int numCols = memberType.getNominalSize();
2190                                         int numRows = memberType.getSecondarySize();
2191
2192                                         ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
2193
2194                                         unsigned int dstIndex = registerIndex(&unpackedUniform);
2195                                         unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2196                                         int arrayIndex = argumentInfo.clampedIndex / numCols;
2197                                         int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2198
2199                                         for(int j = 0; j < numRows; ++j)
2200                                         {
2201                                                 // Transpose the row major matrix
2202                                                 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2203                                                 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2204                                                 instruction->dst.index = dstIndex;
2205                                                 instruction->dst.mask = 1 << j;
2206                                                 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2207                                                 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2208                                                 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2209                                                 instruction->src[0].swizzle = srcSwizzle;
2210
2211                                                 shader->append(instruction);
2212                                         }
2213
2214                                         arg = &unpackedUniform;
2215                                         index = 0;
2216                                 }
2217                         }
2218
2219                         const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2220                         const TType &type = argumentInfo.typedMemberInfo.type;
2221
2222                         int size = registerSize(type, argumentInfo.clampedIndex);
2223
2224                         parameter.type = registerType(arg);
2225                         parameter.bufferIndex = argumentInfo.bufferIndex;
2226
2227                         if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2228                         {
2229                                 int component = componentCount(type, argumentInfo.clampedIndex);
2230                                 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2231
2232                                 for(int i = 0; i < 4; i++)
2233                                 {
2234                                         if(size == 1)   // Replicate
2235                                         {
2236                                                 parameter.value[i] = constants[component + 0].getAsFloat();
2237                                         }
2238                                         else if(i < size)
2239                                         {
2240                                                 parameter.value[i] = constants[component + i].getAsFloat();
2241                                         }
2242                                         else
2243                                         {
2244                                                 parameter.value[i] = 0.0f;
2245                                         }
2246                                 }
2247                         }
2248                         else
2249                         {
2250                                 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2251
2252                                 if(parameter.bufferIndex != -1)
2253                                 {
2254                                         int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2255                                         parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2256                                 }
2257                         }
2258
2259                         if(!IsSampler(arg->getBasicType()))
2260                         {
2261                                 parameter.swizzle = readSwizzle(arg, size);
2262                         }
2263                 }
2264         }
2265
2266         void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2267         {
2268                 for(int index = 0; index < dst->totalRegisterCount(); index++)
2269                 {
2270                         Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
2271                         mov->dst.mask = writeMask(dst, index);
2272                 }
2273         }
2274
2275         int swizzleElement(int swizzle, int index)
2276         {
2277                 return (swizzle >> (index * 2)) & 0x03;
2278         }
2279
2280         int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2281         {
2282                 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2283                        (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2284                        (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2285                        (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2286         }
2287
2288         void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2289         {
2290                 if(src &&
2291                         ((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2292                          (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize())))))
2293                 {
2294                         return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2295                 }
2296
2297                 TIntermBinary *binary = dst->getAsBinaryNode();
2298
2299                 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2300                 {
2301                         Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2302
2303                         Temporary address(this);
2304                         lvalue(insert->dst, address, dst);
2305
2306                         insert->src[0].type = insert->dst.type;
2307                         insert->src[0].index = insert->dst.index;
2308                         insert->src[0].rel = insert->dst.rel;
2309                         argument(insert->src[1], src);
2310                         argument(insert->src[2], binary->getRight());
2311
2312                         shader->append(insert);
2313                 }
2314                 else
2315                 {
2316                         for(int offset = 0; offset < dst->totalRegisterCount(); offset++)
2317                         {
2318                                 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2319
2320                                 Temporary address(this);
2321                                 int swizzle = lvalue(mov->dst, address, dst);
2322                                 mov->dst.index += offset;
2323
2324                                 if(offset > 0)
2325                                 {
2326                                         mov->dst.mask = writeMask(dst, offset);
2327                                 }
2328
2329                                 argument(mov->src[0], src, offset);
2330                                 mov->src[0].swizzle = swizzleSwizzle(mov->src[0].swizzle, swizzle);
2331
2332                                 shader->append(mov);
2333                         }
2334                 }
2335         }
2336
2337         int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node)
2338         {
2339                 TIntermTyped *result = node;
2340                 TIntermBinary *binary = node->getAsBinaryNode();
2341                 TIntermSymbol *symbol = node->getAsSymbolNode();
2342
2343                 if(binary)
2344                 {
2345                         TIntermTyped *left = binary->getLeft();
2346                         TIntermTyped *right = binary->getRight();
2347
2348                         int leftSwizzle = lvalue(dst, address, left);   // Resolve the l-value of the left side
2349
2350                         switch(binary->getOp())
2351                         {
2352                         case EOpIndexDirect:
2353                                 {
2354                                         int rightIndex = right->getAsConstantUnion()->getIConst(0);
2355
2356                                         if(left->isRegister())
2357                                         {
2358                                                 int leftMask = dst.mask;
2359
2360                                                 dst.mask = 1;
2361                                                 while((leftMask & dst.mask) == 0)
2362                                                 {
2363                                                         dst.mask = dst.mask << 1;
2364                                                 }
2365
2366                                                 int element = swizzleElement(leftSwizzle, rightIndex);
2367                                                 dst.mask = 1 << element;
2368
2369                                                 return element;
2370                                         }
2371                                         else if(left->isArray() || left->isMatrix())
2372                                         {
2373                                                 dst.index += rightIndex * result->totalRegisterCount();
2374                                                 return 0xE4;
2375                                         }
2376                                         else UNREACHABLE(0);
2377                                 }
2378                                 break;
2379                         case EOpIndexIndirect:
2380                                 {
2381                                         if(left->isRegister())
2382                                         {
2383                                                 // Requires INSERT instruction (handled by calling function)
2384                                         }
2385                                         else if(left->isArray() || left->isMatrix())
2386                                         {
2387                                                 int scale = result->totalRegisterCount();
2388
2389                                                 if(dst.rel.type == sw::Shader::PARAMETER_VOID)   // Use the index register as the relative address directly
2390                                                 {
2391                                                         if(left->totalRegisterCount() > 1)
2392                                                         {
2393                                                                 sw::Shader::SourceParameter relativeRegister;
2394                                                                 argument(relativeRegister, right);
2395
2396                                                                 dst.rel.index = relativeRegister.index;
2397                                                                 dst.rel.type = relativeRegister.type;
2398                                                                 dst.rel.scale = scale;
2399                                                                 dst.rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
2400                                                         }
2401                                                 }
2402                                                 else if(dst.rel.index != registerIndex(&address))   // Move the previous index register to the address register
2403                                                 {
2404                                                         if(scale == 1)
2405                                                         {
2406                                                                 Constant oldScale((int)dst.rel.scale);
2407                                                                 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
2408                                                                 mad->src[0].index = dst.rel.index;
2409                                                                 mad->src[0].type = dst.rel.type;
2410                                                         }
2411                                                         else
2412                                                         {
2413                                                                 Constant oldScale((int)dst.rel.scale);
2414                                                                 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
2415                                                                 mul->src[0].index = dst.rel.index;
2416                                                                 mul->src[0].type = dst.rel.type;
2417
2418                                                                 Constant newScale(scale);
2419                                                                 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2420                                                         }
2421
2422                                                         dst.rel.type = sw::Shader::PARAMETER_TEMP;
2423                                                         dst.rel.index = registerIndex(&address);
2424                                                         dst.rel.scale = 1;
2425                                                 }
2426                                                 else   // Just add the new index to the address register
2427                                                 {
2428                                                         if(scale == 1)
2429                                                         {
2430                                                                 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2431                                                         }
2432                                                         else
2433                                                         {
2434                                                                 Constant newScale(scale);
2435                                                                 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2436                                                         }
2437                                                 }
2438                                         }
2439                                         else UNREACHABLE(0);
2440                                 }
2441                                 break;
2442                         case EOpIndexDirectStruct:
2443                         case EOpIndexDirectInterfaceBlock:
2444                                 {
2445                                         const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2446                                                                    left->getType().getStruct()->fields() :
2447                                                                    left->getType().getInterfaceBlock()->fields();
2448                                         int index = right->getAsConstantUnion()->getIConst(0);
2449                                         int fieldOffset = 0;
2450
2451                                         for(int i = 0; i < index; i++)
2452                                         {
2453                                                 fieldOffset += fields[i]->type()->totalRegisterCount();
2454                                         }
2455
2456                                         dst.type = registerType(left);
2457                                         dst.index += fieldOffset;
2458                                         dst.mask = writeMask(right);
2459
2460                                         return 0xE4;
2461                                 }
2462                                 break;
2463                         case EOpVectorSwizzle:
2464                                 {
2465                                         ASSERT(left->isRegister());
2466
2467                                         int leftMask = dst.mask;
2468
2469                                         int swizzle = 0;
2470                                         int rightMask = 0;
2471
2472                                         TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2473
2474                                         for(unsigned int i = 0; i < sequence.size(); i++)
2475                                         {
2476                                                 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2477
2478                                                 int element = swizzleElement(leftSwizzle, index);
2479                                                 rightMask = rightMask | (1 << element);
2480                                                 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2481                                         }
2482
2483                                         dst.mask = leftMask & rightMask;
2484
2485                                         return swizzle;
2486                                 }
2487                                 break;
2488                         default:
2489                                 UNREACHABLE(binary->getOp());   // Not an l-value operator
2490                                 break;
2491                         }
2492                 }
2493                 else if(symbol)
2494                 {
2495                         dst.type = registerType(symbol);
2496                         dst.index = registerIndex(symbol);
2497                         dst.mask = writeMask(symbol);
2498                         return 0xE4;
2499                 }
2500
2501                 return 0xE4;
2502         }
2503
2504         sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2505         {
2506                 if(isSamplerRegister(operand))
2507                 {
2508                         return sw::Shader::PARAMETER_SAMPLER;
2509                 }
2510
2511                 const TQualifier qualifier = operand->getQualifier();
2512                 if((EvqFragColor == qualifier) || (EvqFragData == qualifier))
2513                 {
2514                         if(((EvqFragData == qualifier) && (EvqFragColor == outputQualifier)) ||
2515                            ((EvqFragColor == qualifier) && (EvqFragData == outputQualifier)))
2516                         {
2517                                 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2518                         }
2519                         outputQualifier = qualifier;
2520                 }
2521
2522                 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2523                 {
2524                         return sw::Shader::PARAMETER_TEMP;
2525                 }
2526
2527                 switch(qualifier)
2528                 {
2529                 case EvqTemporary:           return sw::Shader::PARAMETER_TEMP;
2530                 case EvqGlobal:              return sw::Shader::PARAMETER_TEMP;
2531                 case EvqConstExpr:           return sw::Shader::PARAMETER_FLOAT4LITERAL;   // All converted to float
2532                 case EvqAttribute:           return sw::Shader::PARAMETER_INPUT;
2533                 case EvqVaryingIn:           return sw::Shader::PARAMETER_INPUT;
2534                 case EvqVaryingOut:          return sw::Shader::PARAMETER_OUTPUT;
2535                 case EvqVertexIn:            return sw::Shader::PARAMETER_INPUT;
2536                 case EvqFragmentOut:         return sw::Shader::PARAMETER_COLOROUT;
2537                 case EvqVertexOut:           return sw::Shader::PARAMETER_OUTPUT;
2538                 case EvqFragmentIn:          return sw::Shader::PARAMETER_INPUT;
2539                 case EvqInvariantVaryingIn:  return sw::Shader::PARAMETER_INPUT;    // FIXME: Guarantee invariance at the backend
2540                 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT;   // FIXME: Guarantee invariance at the backend
2541                 case EvqSmooth:              return sw::Shader::PARAMETER_OUTPUT;
2542                 case EvqFlat:                return sw::Shader::PARAMETER_OUTPUT;
2543                 case EvqCentroidOut:         return sw::Shader::PARAMETER_OUTPUT;
2544                 case EvqSmoothIn:            return sw::Shader::PARAMETER_INPUT;
2545                 case EvqFlatIn:              return sw::Shader::PARAMETER_INPUT;
2546                 case EvqCentroidIn:          return sw::Shader::PARAMETER_INPUT;
2547                 case EvqUniform:             return sw::Shader::PARAMETER_CONST;
2548                 case EvqIn:                  return sw::Shader::PARAMETER_TEMP;
2549                 case EvqOut:                 return sw::Shader::PARAMETER_TEMP;
2550                 case EvqInOut:               return sw::Shader::PARAMETER_TEMP;
2551                 case EvqConstReadOnly:       return sw::Shader::PARAMETER_TEMP;
2552                 case EvqPosition:            return sw::Shader::PARAMETER_OUTPUT;
2553                 case EvqPointSize:           return sw::Shader::PARAMETER_OUTPUT;
2554                 case EvqInstanceID:          return sw::Shader::PARAMETER_MISCTYPE;
2555                 case EvqFragCoord:           return sw::Shader::PARAMETER_MISCTYPE;
2556                 case EvqFrontFacing:         return sw::Shader::PARAMETER_MISCTYPE;
2557                 case EvqPointCoord:          return sw::Shader::PARAMETER_INPUT;
2558                 case EvqFragColor:           return sw::Shader::PARAMETER_COLOROUT;
2559                 case EvqFragData:            return sw::Shader::PARAMETER_COLOROUT;
2560                 case EvqFragDepth:           return sw::Shader::PARAMETER_DEPTHOUT;
2561                 default: UNREACHABLE(qualifier);
2562                 }
2563
2564                 return sw::Shader::PARAMETER_VOID;
2565         }
2566
2567         bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2568         {
2569                 const TQualifier qualifier = operand->getQualifier();
2570                 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2571         }
2572
2573         unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2574         {
2575                 if(isSamplerRegister(operand))
2576                 {
2577                         return samplerRegister(operand);
2578                 }
2579
2580                 switch(operand->getQualifier())
2581                 {
2582                 case EvqTemporary:           return temporaryRegister(operand);
2583                 case EvqGlobal:              return temporaryRegister(operand);
2584                 case EvqConstExpr:           return temporaryRegister(operand);   // Unevaluated constant expression
2585                 case EvqAttribute:           return attributeRegister(operand);
2586                 case EvqVaryingIn:           return varyingRegister(operand);
2587                 case EvqVaryingOut:          return varyingRegister(operand);
2588                 case EvqVertexIn:            return attributeRegister(operand);
2589                 case EvqFragmentOut:         return fragmentOutputRegister(operand);
2590                 case EvqVertexOut:           return varyingRegister(operand);
2591                 case EvqFragmentIn:          return varyingRegister(operand);
2592                 case EvqInvariantVaryingIn:  return varyingRegister(operand);
2593                 case EvqInvariantVaryingOut: return varyingRegister(operand);
2594                 case EvqSmooth:              return varyingRegister(operand);
2595                 case EvqFlat:                return varyingRegister(operand);
2596                 case EvqCentroidOut:         return varyingRegister(operand);
2597                 case EvqSmoothIn:            return varyingRegister(operand);
2598                 case EvqFlatIn:              return varyingRegister(operand);
2599                 case EvqCentroidIn:          return varyingRegister(operand);
2600                 case EvqUniform:             return uniformRegister(operand);
2601                 case EvqIn:                  return temporaryRegister(operand);
2602                 case EvqOut:                 return temporaryRegister(operand);
2603                 case EvqInOut:               return temporaryRegister(operand);
2604                 case EvqConstReadOnly:       return temporaryRegister(operand);
2605                 case EvqPosition:            return varyingRegister(operand);
2606                 case EvqPointSize:           return varyingRegister(operand);
2607                 case EvqInstanceID:          vertexShader->declareInstanceId(); return 0;
2608                 case EvqFragCoord:           pixelShader->declareVPos();  return 0;
2609                 case EvqFrontFacing:         pixelShader->declareVFace(); return 1;
2610                 case EvqPointCoord:          return varyingRegister(operand);
2611                 case EvqFragColor:           return 0;
2612                 case EvqFragData:            return fragmentOutputRegister(operand);
2613                 case EvqFragDepth:           return 0;
2614                 default: UNREACHABLE(operand->getQualifier());
2615                 }
2616
2617                 return 0;
2618         }
2619
2620         int OutputASM::writeMask(TIntermTyped *destination, int index)
2621         {
2622                 if(destination->getQualifier() == EvqPointSize)
2623                 {
2624                         return 0x2;   // Point size stored in the y component
2625                 }
2626
2627                 return 0xF >> (4 - registerSize(destination->getType(), index));
2628         }
2629
2630         int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2631         {
2632                 if(argument->getQualifier() == EvqPointSize)
2633                 {
2634                         return 0x55;   // Point size stored in the y component
2635                 }
2636
2637                 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4};   // (void), xxxx, xyyy, xyzz, xyzw
2638
2639                 return swizzleSize[size];
2640         }
2641
2642         // Conservatively checks whether an expression is fast to compute and has no side effects
2643         bool OutputASM::trivial(TIntermTyped *expression, int budget)
2644         {
2645                 if(!expression->isRegister())
2646                 {
2647                         return false;
2648                 }
2649
2650                 return cost(expression, budget) >= 0;
2651         }
2652
2653         // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2654         int OutputASM::cost(TIntermNode *expression, int budget)
2655         {
2656                 if(budget < 0)
2657                 {
2658                         return budget;
2659                 }
2660
2661                 if(expression->getAsSymbolNode())
2662                 {
2663                         return budget;
2664                 }
2665                 else if(expression->getAsConstantUnion())
2666                 {
2667                         return budget;
2668                 }
2669                 else if(expression->getAsBinaryNode())
2670                 {
2671                         TIntermBinary *binary = expression->getAsBinaryNode();
2672
2673                         switch(binary->getOp())
2674                         {
2675                         case EOpVectorSwizzle:
2676                         case EOpIndexDirect:
2677                         case EOpIndexDirectStruct:
2678                         case EOpIndexDirectInterfaceBlock:
2679                                 return cost(binary->getLeft(), budget - 0);
2680                         case EOpAdd:
2681                         case EOpSub:
2682                         case EOpMul:
2683                                 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2684                         default:
2685                                 return -1;
2686                         }
2687                 }
2688                 else if(expression->getAsUnaryNode())
2689                 {
2690                         TIntermUnary *unary = expression->getAsUnaryNode();
2691
2692                         switch(unary->getOp())
2693                         {
2694                         case EOpAbs:
2695                         case EOpNegative:
2696                                 return cost(unary->getOperand(), budget - 1);
2697                         default:
2698                                 return -1;
2699                         }
2700                 }
2701                 else if(expression->getAsSelectionNode())
2702                 {
2703                         TIntermSelection *selection = expression->getAsSelectionNode();
2704
2705                         if(selection->usesTernaryOperator())
2706                         {
2707                                 TIntermTyped *condition = selection->getCondition();
2708                                 TIntermNode *trueBlock = selection->getTrueBlock();
2709                                 TIntermNode *falseBlock = selection->getFalseBlock();
2710                                 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2711
2712                                 if(constantCondition)
2713                                 {
2714                                         bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2715
2716                                         if(trueCondition)
2717                                         {
2718                                                 return cost(trueBlock, budget - 0);
2719                                         }
2720                                         else
2721                                         {
2722                                                 return cost(falseBlock, budget - 0);
2723                                         }
2724                                 }
2725                                 else
2726                                 {
2727                                         return cost(trueBlock, cost(falseBlock, budget - 2));
2728                                 }
2729                         }
2730                 }
2731
2732                 return -1;
2733         }
2734
2735         const Function *OutputASM::findFunction(const TString &name)
2736         {
2737                 for(unsigned int f = 0; f < functionArray.size(); f++)
2738                 {
2739                         if(functionArray[f].name == name)
2740                         {
2741                                 return &functionArray[f];
2742                         }
2743                 }
2744
2745                 return 0;
2746         }
2747
2748         int OutputASM::temporaryRegister(TIntermTyped *temporary)
2749         {
2750                 return allocate(temporaries, temporary);
2751         }
2752
2753         int OutputASM::varyingRegister(TIntermTyped *varying)
2754         {
2755                 int var = lookup(varyings, varying);
2756
2757                 if(var == -1)
2758                 {
2759                         var = allocate(varyings, varying);
2760                         int componentCount = varying->registerSize();
2761                         int registerCount = varying->totalRegisterCount();
2762
2763                         if(pixelShader)
2764                         {
2765                                 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
2766                                 {
2767                                         mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2768                                         return 0;
2769                                 }
2770
2771                                 if(varying->getQualifier() == EvqPointCoord)
2772                                 {
2773                                         ASSERT(varying->isRegister());
2774                                         pixelShader->setInput(var, componentCount, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
2775                                 }
2776                                 else
2777                                 {
2778                                         for(int i = 0; i < varying->totalRegisterCount(); i++)
2779                                         {
2780                                                 bool flat = hasFlatQualifier(varying);
2781
2782                                                 pixelShader->setInput(var + i, componentCount, sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2783                                         }
2784                                 }
2785                         }
2786                         else if(vertexShader)
2787                         {
2788                                 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
2789                                 {
2790                                         mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2791                                         return 0;
2792                                 }
2793
2794                                 if(varying->getQualifier() == EvqPosition)
2795                                 {
2796                                         ASSERT(varying->isRegister());
2797                                         vertexShader->setPositionRegister(var);
2798                                 }
2799                                 else if(varying->getQualifier() == EvqPointSize)
2800                                 {
2801                                         ASSERT(varying->isRegister());
2802                                         vertexShader->setPointSizeRegister(var);
2803                                 }
2804                                 else
2805                                 {
2806                                         // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2807                                 }
2808                         }
2809                         else UNREACHABLE(0);
2810
2811                         declareVarying(varying, var);
2812                 }
2813
2814                 return var;
2815         }
2816
2817         void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2818         {
2819                 if(varying->getQualifier() != EvqPointCoord)   // gl_PointCoord does not need linking
2820                 {
2821                         const TType &type = varying->getType();
2822                         const char *name = varying->getAsSymbolNode()->getSymbol().c_str();
2823                         VaryingList &activeVaryings = shaderObject->varyings;
2824
2825                         // Check if this varying has been declared before without having a register assigned
2826                         for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2827                         {
2828                                 if(v->name == name)
2829                                 {
2830                                         if(reg >= 0)
2831                                         {
2832                                                 ASSERT(v->reg < 0 || v->reg == reg);
2833                                                 v->reg = reg;
2834                                         }
2835
2836                                         return;
2837                                 }
2838                         }
2839
2840                         activeVaryings.push_back(glsl::Varying(glVariableType(type), name, varying->getArraySize(), reg, 0));
2841                 }
2842         }
2843
2844         int OutputASM::uniformRegister(TIntermTyped *uniform)
2845         {
2846                 const TType &type = uniform->getType();
2847                 ASSERT(!IsSampler(type.getBasicType()));
2848                 TInterfaceBlock *block = type.getAsInterfaceBlock();
2849                 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2850                 ASSERT(symbol || block);
2851
2852                 if(symbol || block)
2853                 {
2854                         TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2855                         bool isBlockMember = (!block && parentBlock);
2856                         int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2857
2858                         if(index == -1 || isBlockMember)
2859                         {
2860                                 if(index == -1)
2861                                 {
2862                                         index = allocate(uniforms, uniform);
2863                                 }
2864
2865                                 // Verify if the current uniform is a member of an already declared block
2866                                 const TString &name = symbol ? symbol->getSymbol() : block->name();
2867                                 int blockMemberIndex = blockMemberLookup(type, name, index);
2868                                 if(blockMemberIndex == -1)
2869                                 {
2870                                         declareUniform(type, name, index);
2871                                 }
2872                                 else
2873                                 {
2874                                         index = blockMemberIndex;
2875                                 }
2876                         }
2877
2878                         return index;
2879                 }
2880
2881                 return 0;
2882         }
2883
2884         int OutputASM::attributeRegister(TIntermTyped *attribute)
2885         {
2886                 ASSERT(!attribute->isArray());
2887
2888                 int index = lookup(attributes, attribute);
2889
2890                 if(index == -1)
2891                 {
2892                         TIntermSymbol *symbol = attribute->getAsSymbolNode();
2893                         ASSERT(symbol);
2894
2895                         if(symbol)
2896                         {
2897                                 index = allocate(attributes, attribute);
2898                                 const TType &type = attribute->getType();
2899                                 int registerCount = attribute->totalRegisterCount();
2900                                 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2901                                 switch(type.getBasicType())
2902                                 {
2903                                 case EbtInt:
2904                                         attribType = sw::VertexShader::ATTRIBTYPE_INT;
2905                                         break;
2906                                 case EbtUInt:
2907                                         attribType = sw::VertexShader::ATTRIBTYPE_UINT;
2908                                         break;
2909                                 case EbtFloat:
2910                                 default:
2911                                         break;
2912                                 }
2913
2914                                 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
2915                                 {
2916                                         for(int i = 0; i < registerCount; i++)
2917                                         {
2918                                                 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
2919                                         }
2920                                 }
2921
2922                                 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
2923
2924                                 const char *name = symbol->getSymbol().c_str();
2925                                 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
2926                         }
2927                 }
2928
2929                 return index;
2930         }
2931
2932         int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
2933         {
2934                 return allocate(fragmentOutputs, fragmentOutput);
2935         }
2936
2937         int OutputASM::samplerRegister(TIntermTyped *sampler)
2938         {
2939                 const TType &type = sampler->getType();
2940                 ASSERT(IsSampler(type.getBasicType()) || type.isStruct());   // Structures can contain samplers
2941
2942                 TIntermSymbol *symbol = sampler->getAsSymbolNode();
2943                 TIntermBinary *binary = sampler->getAsBinaryNode();
2944
2945                 if(symbol && type.getQualifier() == EvqUniform)
2946                 {
2947                         return samplerRegister(symbol);
2948                 }
2949                 else if(binary)
2950                 {
2951                         TIntermTyped *left = binary->getLeft();
2952                         TIntermTyped *right = binary->getRight();
2953                         const TType &leftType = left->getType();
2954                         int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
2955                         int offset = 0;
2956
2957                         switch(binary->getOp())
2958                         {
2959                         case EOpIndexDirect:
2960                                 ASSERT(left->isArray());
2961                                 offset = index * leftType.elementRegisterCount();
2962                                 break;
2963                         case EOpIndexDirectStruct:
2964                                 ASSERT(leftType.isStruct());
2965                                 {
2966                                         const TFieldList &fields = leftType.getStruct()->fields();
2967
2968                                         for(int i = 0; i < index; i++)
2969                                         {
2970                                                 offset += fields[i]->type()->totalRegisterCount();
2971                                         }
2972                                 }
2973                                 break;
2974                         case EOpIndexIndirect:               // Indirect indexing produces a temporary, not a sampler register
2975                                 return -1;
2976                         case EOpIndexDirectInterfaceBlock:   // Interface blocks can't contain samplers
2977                         default:
2978                                 UNREACHABLE(binary->getOp());
2979                                 return -1;
2980                         }
2981
2982                         int base = samplerRegister(left);
2983
2984                         if(base < 0)
2985                         {
2986                                 return -1;
2987                         }
2988
2989                         return base + offset;
2990                 }
2991
2992                 UNREACHABLE(0);
2993                 return -1;   // Not a sampler register
2994         }
2995
2996         int OutputASM::samplerRegister(TIntermSymbol *sampler)
2997         {
2998                 const TType &type = sampler->getType();
2999                 ASSERT(IsSampler(type.getBasicType()) || type.isStruct());   // Structures can contain samplers
3000
3001                 int index = lookup(samplers, sampler);
3002
3003                 if(index == -1)
3004                 {
3005                         index = allocate(samplers, sampler);
3006
3007                         if(sampler->getQualifier() == EvqUniform)
3008                         {
3009                                 const char *name = sampler->getSymbol().c_str();
3010                                 declareUniform(type, name, index);
3011                         }
3012                 }
3013
3014                 return index;
3015         }
3016
3017         bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3018         {
3019                 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3020         }
3021
3022         int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3023         {
3024                 for(unsigned int i = 0; i < list.size(); i++)
3025                 {
3026                         if(list[i] == variable)
3027                         {
3028                                 return i;   // Pointer match
3029                         }
3030                 }
3031
3032                 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3033                 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3034
3035                 if(varBlock)
3036                 {
3037                         for(unsigned int i = 0; i < list.size(); i++)
3038                         {
3039                                 if(list[i])
3040                                 {
3041                                         TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3042
3043                                         if(listBlock)
3044                                         {
3045                                                 if(listBlock->name() == varBlock->name())
3046                                                 {
3047                                                         ASSERT(listBlock->arraySize() == varBlock->arraySize());
3048                                                         ASSERT(listBlock->fields() == varBlock->fields());
3049                                                         ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3050                                                         ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3051
3052                                                         return i;
3053                                                 }
3054                                         }
3055                                 }
3056                         }
3057                 }
3058                 else if(varSymbol)
3059                 {
3060                         for(unsigned int i = 0; i < list.size(); i++)
3061                         {
3062                                 if(list[i])
3063                                 {
3064                                         TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3065
3066                                         if(listSymbol)
3067                                         {
3068                                                 if(listSymbol->getId() == varSymbol->getId())
3069                                                 {
3070                                                         ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3071                                                         ASSERT(listSymbol->getType() == varSymbol->getType());
3072                                                         ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3073
3074                                                         return i;
3075                                                 }
3076                                         }
3077                                 }
3078                         }
3079                 }
3080
3081                 return -1;
3082         }
3083
3084         int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3085         {
3086                 for(unsigned int i = 0; i < list.size(); i++)
3087                 {
3088                         if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3089                         {
3090                                 return i;   // Pointer match
3091                         }
3092                 }
3093                 return -1;
3094         }
3095
3096         int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
3097         {
3098                 int index = lookup(list, variable);
3099
3100                 if(index == -1)
3101                 {
3102                         unsigned int registerCount = variable->blockRegisterCount();
3103
3104                         for(unsigned int i = 0; i < list.size(); i++)
3105                         {
3106                                 if(list[i] == 0)
3107                                 {
3108                                         unsigned int j = 1;
3109                                         for( ; j < registerCount && (i + j) < list.size(); j++)
3110                                         {
3111                                                 if(list[i + j] != 0)
3112                                                 {
3113                                                         break;
3114                                                 }
3115                                         }
3116
3117                                         if(j == registerCount)   // Found free slots
3118                                         {
3119                                                 for(unsigned int j = 0; j < registerCount; j++)
3120                                                 {
3121                                                         list[i + j] = variable;
3122                                                 }
3123
3124                                                 return i;
3125                                         }
3126                                 }
3127                         }
3128
3129                         index = list.size();
3130
3131                         for(unsigned int i = 0; i < registerCount; i++)
3132                         {
3133                                 list.push_back(variable);
3134                         }
3135                 }
3136
3137                 return index;
3138         }
3139
3140         void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3141         {
3142                 int index = lookup(list, variable);
3143
3144                 if(index >= 0)
3145                 {
3146                         list[index] = 0;
3147                 }
3148         }
3149
3150         int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3151         {
3152                 const TInterfaceBlock *block = type.getInterfaceBlock();
3153
3154                 if(block)
3155                 {
3156                         ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3157                         const TFieldList& fields = block->fields();
3158                         const TString &blockName = block->name();
3159                         int fieldRegisterIndex = registerIndex;
3160
3161                         if(!type.isInterfaceBlock())
3162                         {
3163                                 // This is a uniform that's part of a block, let's see if the block is already defined
3164                                 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3165                                 {
3166                                         if(activeUniformBlocks[i].name == blockName.c_str())
3167                                         {
3168                                                 // The block is already defined, find the register for the current uniform and return it
3169                                                 for(size_t j = 0; j < fields.size(); j++)
3170                                                 {
3171                                                         const TString &fieldName = fields[j]->name();
3172                                                         if(fieldName == name)
3173                                                         {
3174                                                                 return fieldRegisterIndex;
3175                                                         }
3176
3177                                                         fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3178                                                 }
3179
3180                                                 ASSERT(false);
3181                                                 return fieldRegisterIndex;
3182                                         }
3183                                 }
3184                         }
3185                 }
3186
3187                 return -1;
3188         }
3189
3190         void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int blockId, BlockLayoutEncoder* encoder)
3191         {
3192                 const TStructure *structure = type.getStruct();
3193                 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3194
3195                 if(!structure && !block)
3196                 {
3197                         ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3198                         const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3199                         if(blockId >= 0)
3200                         {
3201                                 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3202                                 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3203                         }
3204                         int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3205                         activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3206                                                          fieldRegisterIndex, blockId, blockInfo));
3207                         if(IsSampler(type.getBasicType()))
3208                         {
3209                                 for(int i = 0; i < type.totalRegisterCount(); i++)
3210                                 {
3211                                         shader->declareSampler(fieldRegisterIndex + i);
3212                                 }
3213                         }
3214                 }
3215                 else if(block)
3216                 {
3217                         ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3218                         const TFieldList& fields = block->fields();
3219                         const TString &blockName = block->name();
3220                         int fieldRegisterIndex = registerIndex;
3221                         bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3222
3223                         blockId = activeUniformBlocks.size();
3224                         bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3225                         activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3226                                                                    block->blockStorage(), isRowMajor, registerIndex, blockId));
3227                         blockDefinitions.push_back(BlockDefinitionIndexMap());
3228
3229                         Std140BlockEncoder currentBlockEncoder(isRowMajor);
3230                         currentBlockEncoder.enterAggregateType();
3231                         for(size_t i = 0; i < fields.size(); i++)
3232                         {
3233                                 const TType &fieldType = *(fields[i]->type());
3234                                 const TString &fieldName = fields[i]->name();
3235                                 if(isUniformBlockMember && (fieldName == name))
3236                                 {
3237                                         registerIndex = fieldRegisterIndex;
3238                                 }
3239
3240                                 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3241
3242                                 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, &currentBlockEncoder);
3243                                 fieldRegisterIndex += fieldType.totalRegisterCount();
3244                         }
3245                         currentBlockEncoder.exitAggregateType();
3246                         activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3247                 }
3248                 else
3249                 {
3250                         int fieldRegisterIndex = registerIndex;
3251
3252                         const TFieldList& fields = structure->fields();
3253                         if(type.isArray() && (structure || type.isInterfaceBlock()))
3254                         {
3255                                 for(int i = 0; i < type.getArraySize(); i++)
3256                                 {
3257                                         if(encoder)
3258                                         {
3259                                                 encoder->enterAggregateType();
3260                                         }
3261                                         for(size_t j = 0; j < fields.size(); j++)
3262                                         {
3263                                                 const TType &fieldType = *(fields[j]->type());
3264                                                 const TString &fieldName = fields[j]->name();
3265                                                 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3266
3267                                                 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3268                                                 fieldRegisterIndex += fieldType.totalRegisterCount();
3269                                         }
3270                                         if(encoder)
3271                                         {
3272                                                 encoder->exitAggregateType();
3273                                         }
3274                                 }
3275                         }
3276                         else
3277                         {
3278                                 if(encoder)
3279                                 {
3280                                         encoder->enterAggregateType();
3281                                 }
3282                                 for(size_t i = 0; i < fields.size(); i++)
3283                                 {
3284                                         const TType &fieldType = *(fields[i]->type());
3285                                         const TString &fieldName = fields[i]->name();
3286                                         const TString uniformName = name + "." + fieldName;
3287
3288                                         declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3289                                         fieldRegisterIndex += fieldType.totalRegisterCount();
3290                                 }
3291                                 if(encoder)
3292                                 {
3293                                         encoder->exitAggregateType();
3294                                 }
3295                         }
3296                 }
3297         }
3298
3299         GLenum OutputASM::glVariableType(const TType &type)
3300         {
3301                 switch(type.getBasicType())
3302                 {
3303                 case EbtFloat:
3304                         if(type.isScalar())
3305                         {
3306                                 return GL_FLOAT;
3307                         }
3308                         else if(type.isVector())
3309                         {
3310                                 switch(type.getNominalSize())
3311                                 {
3312                                 case 2: return GL_FLOAT_VEC2;
3313                                 case 3: return GL_FLOAT_VEC3;
3314                                 case 4: return GL_FLOAT_VEC4;
3315                                 default: UNREACHABLE(type.getNominalSize());
3316                                 }
3317                         }
3318                         else if(type.isMatrix())
3319                         {
3320                                 switch(type.getNominalSize())
3321                                 {
3322                                 case 2:
3323                                         switch(type.getSecondarySize())
3324                                         {
3325                                         case 2: return GL_FLOAT_MAT2;
3326                                         case 3: return GL_FLOAT_MAT2x3;
3327                                         case 4: return GL_FLOAT_MAT2x4;
3328                                         default: UNREACHABLE(type.getSecondarySize());
3329                                         }
3330                                 case 3:
3331                                         switch(type.getSecondarySize())
3332                                         {
3333                                         case 2: return GL_FLOAT_MAT3x2;
3334                                         case 3: return GL_FLOAT_MAT3;
3335                                         case 4: return GL_FLOAT_MAT3x4;
3336                                         default: UNREACHABLE(type.getSecondarySize());
3337                                         }
3338                                 case 4:
3339                                         switch(type.getSecondarySize())
3340                                         {
3341                                         case 2: return GL_FLOAT_MAT4x2;
3342                                         case 3: return GL_FLOAT_MAT4x3;
3343                                         case 4: return GL_FLOAT_MAT4;
3344                                         default: UNREACHABLE(type.getSecondarySize());
3345                                         }
3346                                 default: UNREACHABLE(type.getNominalSize());
3347                                 }
3348                         }
3349                         else UNREACHABLE(0);
3350                         break;
3351                 case EbtInt:
3352                         if(type.isScalar())
3353                         {
3354                                 return GL_INT;
3355                         }
3356                         else if(type.isVector())
3357                         {
3358                                 switch(type.getNominalSize())
3359                                 {
3360                                 case 2: return GL_INT_VEC2;
3361                                 case 3: return GL_INT_VEC3;
3362                                 case 4: return GL_INT_VEC4;
3363                                 default: UNREACHABLE(type.getNominalSize());
3364                                 }
3365                         }
3366                         else UNREACHABLE(0);
3367                         break;
3368                 case EbtUInt:
3369                         if(type.isScalar())
3370                         {
3371                                 return GL_UNSIGNED_INT;
3372                         }
3373                         else if(type.isVector())
3374                         {
3375                                 switch(type.getNominalSize())
3376                                 {
3377                                 case 2: return GL_UNSIGNED_INT_VEC2;
3378                                 case 3: return GL_UNSIGNED_INT_VEC3;
3379                                 case 4: return GL_UNSIGNED_INT_VEC4;
3380                                 default: UNREACHABLE(type.getNominalSize());
3381                                 }
3382                         }
3383                         else UNREACHABLE(0);
3384                         break;
3385                 case EbtBool:
3386                         if(type.isScalar())
3387                         {
3388                                 return GL_BOOL;
3389                         }
3390                         else if(type.isVector())
3391                         {
3392                                 switch(type.getNominalSize())
3393                                 {
3394                                 case 2: return GL_BOOL_VEC2;
3395                                 case 3: return GL_BOOL_VEC3;
3396                                 case 4: return GL_BOOL_VEC4;
3397                                 default: UNREACHABLE(type.getNominalSize());
3398                                 }
3399                         }
3400                         else UNREACHABLE(0);
3401                         break;
3402                 case EbtSampler2D:
3403                         return GL_SAMPLER_2D;
3404                 case EbtISampler2D:
3405                         return GL_INT_SAMPLER_2D;
3406                 case EbtUSampler2D:
3407                         return GL_UNSIGNED_INT_SAMPLER_2D;
3408                 case EbtSamplerCube:
3409                         return GL_SAMPLER_CUBE;
3410                 case EbtISamplerCube:
3411                         return GL_INT_SAMPLER_CUBE;
3412                 case EbtUSamplerCube:
3413                         return GL_UNSIGNED_INT_SAMPLER_CUBE;
3414                 case EbtSamplerExternalOES:
3415                         return GL_SAMPLER_EXTERNAL_OES;
3416                 case EbtSampler3D:
3417                         return GL_SAMPLER_3D_OES;
3418                 case EbtISampler3D:
3419                         return GL_INT_SAMPLER_3D;
3420                 case EbtUSampler3D:
3421                         return GL_UNSIGNED_INT_SAMPLER_3D;
3422                 case EbtSampler2DArray:
3423                         return GL_SAMPLER_2D_ARRAY;
3424                 case EbtISampler2DArray:
3425                         return GL_INT_SAMPLER_2D_ARRAY;
3426                 case EbtUSampler2DArray:
3427                         return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3428                 case EbtSampler2DShadow:
3429                         return GL_SAMPLER_2D_SHADOW;
3430                 case EbtSamplerCubeShadow:
3431                         return GL_SAMPLER_CUBE_SHADOW;
3432                 case EbtSampler2DArrayShadow:
3433                         return GL_SAMPLER_2D_ARRAY_SHADOW;
3434                 default:
3435                         UNREACHABLE(type.getBasicType());
3436                         break;
3437                 }
3438
3439                 return GL_NONE;
3440         }
3441
3442         GLenum OutputASM::glVariablePrecision(const TType &type)
3443         {
3444                 if(type.getBasicType() == EbtFloat)
3445                 {
3446                         switch(type.getPrecision())
3447                         {
3448                         case EbpHigh:   return GL_HIGH_FLOAT;
3449                         case EbpMedium: return GL_MEDIUM_FLOAT;
3450                         case EbpLow:    return GL_LOW_FLOAT;
3451                         case EbpUndefined:
3452                                 // Should be defined as the default precision by the parser
3453                         default: UNREACHABLE(type.getPrecision());
3454                         }
3455                 }
3456                 else if(type.getBasicType() == EbtInt)
3457                 {
3458                         switch(type.getPrecision())
3459                         {
3460                         case EbpHigh:   return GL_HIGH_INT;
3461                         case EbpMedium: return GL_MEDIUM_INT;
3462                         case EbpLow:    return GL_LOW_INT;
3463                         case EbpUndefined:
3464                                 // Should be defined as the default precision by the parser
3465                         default: UNREACHABLE(type.getPrecision());
3466                         }
3467                 }
3468
3469                 // Other types (boolean, sampler) don't have a precision
3470                 return GL_NONE;
3471         }
3472
3473         int OutputASM::dim(TIntermNode *v)
3474         {
3475                 TIntermTyped *vector = v->getAsTyped();
3476                 ASSERT(vector && vector->isRegister());
3477                 return vector->getNominalSize();
3478         }
3479
3480         int OutputASM::dim2(TIntermNode *m)
3481         {
3482                 TIntermTyped *matrix = m->getAsTyped();
3483                 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3484                 return matrix->getSecondarySize();
3485         }
3486
3487         // Returns ~0u if no loop count could be determined
3488         unsigned int OutputASM::loopCount(TIntermLoop *node)
3489         {
3490                 // Parse loops of the form:
3491                 // for(int index = initial; index [comparator] limit; index += increment)
3492                 TIntermSymbol *index = 0;
3493                 TOperator comparator = EOpNull;
3494                 int initial = 0;
3495                 int limit = 0;
3496                 int increment = 0;
3497
3498                 // Parse index name and intial value
3499                 if(node->getInit())
3500                 {
3501                         TIntermAggregate *init = node->getInit()->getAsAggregate();
3502
3503                         if(init)
3504                         {
3505                                 TIntermSequence &sequence = init->getSequence();
3506                                 TIntermTyped *variable = sequence[0]->getAsTyped();
3507
3508                                 if(variable && variable->getQualifier() == EvqTemporary)
3509                                 {
3510                                         TIntermBinary *assign = variable->getAsBinaryNode();
3511
3512                                         if(assign->getOp() == EOpInitialize)
3513                                         {
3514                                                 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3515                                                 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3516
3517                                                 if(symbol && constant)
3518                                                 {
3519                                                         if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3520                                                         {
3521                                                                 index = symbol;
3522                                                                 initial = constant->getUnionArrayPointer()[0].getIConst();
3523                                                         }
3524                                                 }
3525                                         }
3526                                 }
3527                         }
3528                 }
3529
3530                 // Parse comparator and limit value
3531                 if(index && node->getCondition())
3532                 {
3533                         TIntermBinary *test = node->getCondition()->getAsBinaryNode();
3534                         TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
3535
3536                         if(left && (left->getId() == index->getId()))
3537                         {
3538                                 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3539
3540                                 if(constant)
3541                                 {
3542                                         if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3543                                         {
3544                                                 comparator = test->getOp();
3545                                                 limit = constant->getUnionArrayPointer()[0].getIConst();
3546                                         }
3547                                 }
3548                         }
3549                 }
3550
3551                 // Parse increment
3552                 if(index && comparator != EOpNull && node->getExpression())
3553                 {
3554                         TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3555                         TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3556
3557                         if(binaryTerminal)
3558                         {
3559                                 TOperator op = binaryTerminal->getOp();
3560                                 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3561
3562                                 if(constant)
3563                                 {
3564                                         if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3565                                         {
3566                                                 int value = constant->getUnionArrayPointer()[0].getIConst();
3567
3568                                                 switch(op)
3569                                                 {
3570                                                 case EOpAddAssign: increment = value;  break;
3571                                                 case EOpSubAssign: increment = -value; break;
3572                                                 default: UNIMPLEMENTED();
3573                                                 }
3574                                         }
3575                                 }
3576                         }
3577                         else if(unaryTerminal)
3578                         {
3579                                 TOperator op = unaryTerminal->getOp();
3580
3581                                 switch(op)
3582                                 {
3583                                 case EOpPostIncrement: increment = 1;  break;
3584                                 case EOpPostDecrement: increment = -1; break;
3585                                 case EOpPreIncrement:  increment = 1;  break;
3586                                 case EOpPreDecrement:  increment = -1; break;
3587                                 default: UNIMPLEMENTED();
3588                                 }
3589                         }
3590                 }
3591
3592                 if(index && comparator != EOpNull && increment != 0)
3593                 {
3594                         if(comparator == EOpLessThanEqual)
3595                         {
3596                                 comparator = EOpLessThan;
3597                                 limit += 1;
3598                         }
3599
3600                         if(comparator == EOpLessThan)
3601                         {
3602                                 int iterations = (limit - initial) / increment;
3603
3604                                 if(iterations <= 0)
3605                                 {
3606                                         iterations = 0;
3607                                 }
3608
3609                                 return iterations;
3610                         }
3611                         else UNIMPLEMENTED();   // Falls through
3612                 }
3613
3614                 return ~0u;
3615         }
3616
3617         bool LoopUnrollable::traverse(TIntermNode *node)
3618         {
3619                 loopDepth = 0;
3620                 loopUnrollable = true;
3621
3622                 node->traverse(this);
3623
3624                 return loopUnrollable;
3625         }
3626
3627         bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3628         {
3629                 if(visit == PreVisit)
3630                 {
3631                         loopDepth++;
3632                 }
3633                 else if(visit == PostVisit)
3634                 {
3635                         loopDepth++;
3636                 }
3637
3638                 return true;
3639         }
3640
3641         bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3642         {
3643                 if(!loopUnrollable)
3644                 {
3645                         return false;
3646                 }
3647
3648                 if(!loopDepth)
3649                 {
3650                         return true;
3651                 }
3652
3653                 switch(node->getFlowOp())
3654                 {
3655                 case EOpKill:
3656                 case EOpReturn:
3657                         break;
3658                 case EOpBreak:
3659                 case EOpContinue:
3660                         loopUnrollable = false;
3661                         break;
3662                 default: UNREACHABLE(node->getFlowOp());
3663                 }
3664
3665                 return loopUnrollable;
3666         }
3667
3668         bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3669         {
3670                 return loopUnrollable;
3671         }
3672 }