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