blob: 56fde3dba07253830baf833a8cbeb967e3d72b3f [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:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500510 assert(visit == PreVisit);
511 right->traverse(this);
512 assignLvalue(left, right);
513 copy(result, right);
514 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400515 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500516 assert(visit == PreVisit);
517 right->traverse(this);
518 copy(left, right);
519 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400520 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500521 assert(visit == PreVisit);
522 right->traverse(this);
523 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400524 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500525 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400526 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500527
528 assignLvalue(left, result);
529 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400530 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500531 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400532 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500533 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400534 int size = leftType.getNominalSize();
535
536 for(int i = 0; i < size; i++)
537 {
538 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
539 dot->dst.mask = 1 << i;
540 }
541
542 assignLvalue(left, result);
543 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500544 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400545 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500546 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400547 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500548 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400549 int dim = leftType.getNominalSize();
550
551 for(int i = 0; i < dim; i++)
552 {
553 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
554 mul->src[1].swizzle = 0x00;
555
556 for(int j = 1; j < dim; j++)
557 {
558 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
559 mad->src[1].swizzle = j * 0x55;
560 }
561 }
562
563 assignLvalue(left, result);
564 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500565 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400566 case EOpIndexDirect:
567 if(visit == PostVisit)
568 {
569 int index = right->getAsConstantUnion()->getIConst(0);
570
571 if(result->isMatrix() || result->isStruct() || result->isInterfaceBlock())
572 {
573 ASSERT(left->isArray());
574 copy(result, left, index * left->elementRegisterCount());
575 }
576 else if(result->isRegister())
577 {
578 int srcIndex = 0;
579 if(left->isRegister())
580 {
581 srcIndex = 0;
582 }
583 else if(left->isArray())
584 {
585 srcIndex = index * left->elementRegisterCount();
586 }
587 else if(left->isMatrix())
588 {
589 ASSERT(index < left->getNominalSize()); // FIXME: Report semantic error
590 srcIndex = index;
591 }
592 else UNREACHABLE(0);
593
594 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, 0, left, srcIndex);
595
596 if(left->isRegister())
597 {
598 mov->src[0].swizzle = index;
599 }
600 }
601 else UNREACHABLE(0);
602 }
603 break;
604 case EOpIndexIndirect:
605 if(visit == PostVisit)
606 {
607 if(left->isArray() || left->isMatrix())
608 {
609 for(int index = 0; index < result->totalRegisterCount(); index++)
610 {
611 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index, left, index);
612 mov->dst.mask = writeMask(result, index);
613
614 if(left->totalRegisterCount() > 1)
615 {
616 sw::Shader::SourceParameter relativeRegister;
617 argument(relativeRegister, right);
618
619 mov->src[0].rel.type = relativeRegister.type;
620 mov->src[0].rel.index = relativeRegister.index;
621 mov->src[0].rel.scale = result->totalRegisterCount();
622 mov->src[0].rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
623 }
624 }
625 }
626 else if(left->isRegister())
627 {
628 emit(sw::Shader::OPCODE_EXTRACT, result, left, right);
629 }
630 else UNREACHABLE(0);
631 }
632 break;
633 case EOpIndexDirectStruct:
634 case EOpIndexDirectInterfaceBlock:
635 if(visit == PostVisit)
636 {
637 ASSERT(leftType.isStruct() || (leftType.isInterfaceBlock()));
638
639 const TFieldList& fields = (node->getOp() == EOpIndexDirectStruct) ?
640 leftType.getStruct()->fields() :
641 leftType.getInterfaceBlock()->fields();
642 int index = right->getAsConstantUnion()->getIConst(0);
643 int fieldOffset = 0;
644
645 for(int i = 0; i < index; i++)
646 {
647 fieldOffset += fields[i]->type()->totalRegisterCount();
648 }
649
650 copy(result, left, fieldOffset);
651 }
652 break;
653 case EOpVectorSwizzle:
654 if(visit == PostVisit)
655 {
656 int swizzle = 0;
657 TIntermAggregate *components = right->getAsAggregate();
658
659 if(components)
660 {
661 TIntermSequence &sequence = components->getSequence();
662 int component = 0;
663
664 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
665 {
666 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
667
668 if(element)
669 {
670 int i = element->getUnionArrayPointer()[0].getIConst();
671 swizzle |= i << (component * 2);
672 component++;
673 }
674 else UNREACHABLE(0);
675 }
676 }
677 else UNREACHABLE(0);
678
679 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
680 mov->src[0].swizzle = swizzle;
681 }
682 break;
683 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
684 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
685 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
686 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
687 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
688 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
689 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
690 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
691 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
692 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
693 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
694 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
695 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
696 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
697 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
698 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
699 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
700 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
701 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
702 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
703 case EOpEqual:
704 if(visit == PostVisit)
705 {
706 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
707
708 for(int index = 1; index < left->totalRegisterCount(); index++)
709 {
710 Temporary equal(this);
711 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
712 emit(sw::Shader::OPCODE_AND, result, result, &equal);
713 }
714 }
715 break;
716 case EOpNotEqual:
717 if(visit == PostVisit)
718 {
719 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
720
721 for(int index = 1; index < left->totalRegisterCount(); index++)
722 {
723 Temporary notEqual(this);
724 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
725 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
726 }
727 }
728 break;
729 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
730 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
731 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
732 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
733 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
734 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
735 case EOpMatrixTimesScalar:
736 if(visit == PostVisit)
737 {
738 if(left->isMatrix())
739 {
740 for(int i = 0; i < leftType.getNominalSize(); i++)
741 {
742 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
743 }
744 }
745 else if(right->isMatrix())
746 {
747 for(int i = 0; i < rightType.getNominalSize(); i++)
748 {
749 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
750 }
751 }
752 else UNREACHABLE(0);
753 }
754 break;
755 case EOpVectorTimesMatrix:
756 if(visit == PostVisit)
757 {
758 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
759
760 int size = rightType.getNominalSize();
761 for(int i = 0; i < size; i++)
762 {
763 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
764 dot->dst.mask = 1 << i;
765 }
766 }
767 break;
768 case EOpMatrixTimesVector:
769 if(visit == PostVisit)
770 {
771 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
772 mul->src[1].swizzle = 0x00;
773
774 int size = rightType.getNominalSize();
775 for(int i = 1; i < size; i++)
776 {
777 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
778 mad->src[1].swizzle = i * 0x55;
779 }
780 }
781 break;
782 case EOpMatrixTimesMatrix:
783 if(visit == PostVisit)
784 {
785 int dim = leftType.getNominalSize();
786
787 int size = rightType.getNominalSize();
788 for(int i = 0; i < size; i++)
789 {
790 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
791 mul->src[1].swizzle = 0x00;
792
793 for(int j = 1; j < dim; j++)
794 {
795 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
796 mad->src[1].swizzle = j * 0x55;
797 }
798 }
799 }
800 break;
801 case EOpLogicalOr:
802 if(trivial(right, 6))
803 {
804 if(visit == PostVisit)
805 {
806 emit(sw::Shader::OPCODE_OR, result, left, right);
807 }
808 }
809 else // Short-circuit evaluation
810 {
811 if(visit == InVisit)
812 {
813 emit(sw::Shader::OPCODE_MOV, result, left);
814 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
815 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
816 }
817 else if(visit == PostVisit)
818 {
819 emit(sw::Shader::OPCODE_MOV, result, right);
820 emit(sw::Shader::OPCODE_ENDIF);
821 }
822 }
823 break;
824 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
825 case EOpLogicalAnd:
826 if(trivial(right, 6))
827 {
828 if(visit == PostVisit)
829 {
830 emit(sw::Shader::OPCODE_AND, result, left, right);
831 }
832 }
833 else // Short-circuit evaluation
834 {
835 if(visit == InVisit)
836 {
837 emit(sw::Shader::OPCODE_MOV, result, left);
838 emit(sw::Shader::OPCODE_IF, 0, result);
839 }
840 else if(visit == PostVisit)
841 {
842 emit(sw::Shader::OPCODE_MOV, result, right);
843 emit(sw::Shader::OPCODE_ENDIF);
844 }
845 }
846 break;
847 default: UNREACHABLE(node->getOp());
848 }
849
850 return true;
851 }
852
853 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
854 {
855 switch(size)
856 {
857 case 1: // Used for cofactor computation only
858 {
859 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
860 bool isMov = (row == col);
861 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
862 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
863 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
864 mov->dst.mask = 1 << outRow;
865 }
866 break;
867 case 2:
868 {
869 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
870
871 bool isCofactor = (col >= 0) && (row >= 0);
872 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
873 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
874 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
875
876 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
877 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
878 det->dst.mask = 1 << outRow;
879 }
880 break;
881 case 3:
882 {
883 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
884
885 bool isCofactor = (col >= 0) && (row >= 0);
886 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
887 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
888 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
889 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
890
891 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
892 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
893 det->dst.mask = 1 << outRow;
894 }
895 break;
896 case 4:
897 {
898 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
899 det->dst.mask = 1 << outRow;
900 }
901 break;
902 default:
903 UNREACHABLE(size);
904 break;
905 }
906 }
907
908 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
909 {
910 if(currentScope != emitScope)
911 {
912 return false;
913 }
914
915 TIntermTyped *result = node;
916 TIntermTyped *arg = node->getOperand();
917 TBasicType basicType = arg->getType().getBasicType();
918
919 union
920 {
921 float f;
922 int i;
923 } one_value;
924
925 if(basicType == EbtInt || basicType == EbtUInt)
926 {
927 one_value.i = 1;
928 }
929 else
930 {
931 one_value.f = 1.0f;
932 }
933
934 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
935 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
936 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
937
938 switch(node->getOp())
939 {
940 case EOpNegative:
941 if(visit == PostVisit)
942 {
943 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
944 for(int index = 0; index < arg->totalRegisterCount(); index++)
945 {
946 emit(negOpcode, result, index, arg, index);
947 }
948 }
949 break;
950 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
951 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -0400952 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400953 case EOpPostIncrement:
954 if(visit == PostVisit)
955 {
956 copy(result, arg);
957
958 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
959 for(int index = 0; index < arg->totalRegisterCount(); index++)
960 {
961 emit(addOpcode, arg, index, arg, index, &one);
962 }
963
964 assignLvalue(arg, arg);
965 }
966 break;
967 case EOpPostDecrement:
968 if(visit == PostVisit)
969 {
970 copy(result, arg);
971
972 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
973 for(int index = 0; index < arg->totalRegisterCount(); index++)
974 {
975 emit(subOpcode, arg, index, arg, index, &one);
976 }
977
978 assignLvalue(arg, arg);
979 }
980 break;
981 case EOpPreIncrement:
982 if(visit == PostVisit)
983 {
984 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
985 for(int index = 0; index < arg->totalRegisterCount(); index++)
986 {
987 emit(addOpcode, result, index, arg, index, &one);
988 }
989
990 assignLvalue(arg, result);
991 }
992 break;
993 case EOpPreDecrement:
994 if(visit == PostVisit)
995 {
996 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
997 for(int index = 0; index < arg->totalRegisterCount(); index++)
998 {
999 emit(subOpcode, result, index, arg, index, &one);
1000 }
1001
1002 assignLvalue(arg, result);
1003 }
1004 break;
1005 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1006 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1007 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1008 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1009 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1010 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1011 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1012 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1013 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1014 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1015 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1016 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1017 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1018 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1019 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1020 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1021 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1022 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1023 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1024 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1025 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1026 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1027 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1028 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1029 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1030 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1031 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1032 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1033 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1034 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1035 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1036 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1037 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1038 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1039 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1040 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1041 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1042 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1043 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1044 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1045 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1046 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1047 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1048 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1049 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1050 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1051 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1052 case EOpTranspose:
1053 if(visit == PostVisit)
1054 {
1055 int numCols = arg->getNominalSize();
1056 int numRows = arg->getSecondarySize();
1057 for(int i = 0; i < numCols; ++i)
1058 {
1059 for(int j = 0; j < numRows; ++j)
1060 {
1061 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1062 mov->src[0].swizzle = 0x55 * j;
1063 mov->dst.mask = 1 << i;
1064 }
1065 }
1066 }
1067 break;
1068 case EOpDeterminant:
1069 if(visit == PostVisit)
1070 {
1071 int size = arg->getNominalSize();
1072 ASSERT(size == arg->getSecondarySize());
1073
1074 emitDeterminant(result, arg, size);
1075 }
1076 break;
1077 case EOpInverse:
1078 if(visit == PostVisit)
1079 {
1080 int size = arg->getNominalSize();
1081 ASSERT(size == arg->getSecondarySize());
1082
1083 // Compute transposed matrix of cofactors
1084 for(int i = 0; i < size; ++i)
1085 {
1086 for(int j = 0; j < size; ++j)
1087 {
1088 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1089 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1090 emitDeterminant(result, arg, size - 1, j, i, i, j);
1091 }
1092 }
1093
1094 // Compute 1 / determinant
1095 Temporary invDet(this);
1096 emitDeterminant(&invDet, arg, size);
1097 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1098 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1099 div->src[1].swizzle = 0x00; // xxxx
1100
1101 // Divide transposed matrix of cofactors by determinant
1102 for(int i = 0; i < size; ++i)
1103 {
1104 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1105 }
1106 }
1107 break;
1108 default: UNREACHABLE(node->getOp());
1109 }
1110
1111 return true;
1112 }
1113
1114 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1115 {
1116 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1117 {
1118 return false;
1119 }
1120
1121 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1122
1123 TIntermTyped *result = node;
1124 const TType &resultType = node->getType();
1125 TIntermSequence &arg = node->getSequence();
1126 size_t argumentCount = arg.size();
1127
1128 switch(node->getOp())
1129 {
1130 case EOpSequence: break;
1131 case EOpDeclaration: break;
1132 case EOpInvariantDeclaration: break;
1133 case EOpPrototype: break;
1134 case EOpComma:
1135 if(visit == PostVisit)
1136 {
1137 copy(result, arg[1]);
1138 }
1139 break;
1140 case EOpFunction:
1141 if(visit == PreVisit)
1142 {
1143 const TString &name = node->getName();
1144
1145 if(emitScope == FUNCTION)
1146 {
1147 if(functionArray.size() > 1) // No need for a label when there's only main()
1148 {
1149 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1150 label->dst.type = sw::Shader::PARAMETER_LABEL;
1151
1152 const Function *function = findFunction(name);
1153 ASSERT(function); // Should have been added during global pass
1154 label->dst.index = function->label;
1155 currentFunction = function->label;
1156 }
1157 }
1158 else if(emitScope == GLOBAL)
1159 {
1160 if(name != "main(")
1161 {
1162 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1163 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1164 }
1165 }
1166 else UNREACHABLE(emitScope);
1167
1168 currentScope = FUNCTION;
1169 }
1170 else if(visit == PostVisit)
1171 {
1172 if(emitScope == FUNCTION)
1173 {
1174 if(functionArray.size() > 1) // No need to return when there's only main()
1175 {
1176 emit(sw::Shader::OPCODE_RET);
1177 }
1178 }
1179
1180 currentScope = GLOBAL;
1181 }
1182 break;
1183 case EOpFunctionCall:
1184 if(visit == PostVisit)
1185 {
1186 if(node->isUserDefined())
1187 {
1188 const TString &name = node->getName();
1189 const Function *function = findFunction(name);
1190
1191 if(!function)
1192 {
1193 mContext.error(node->getLine(), "function definition not found", name.c_str());
1194 return false;
1195 }
1196
1197 TIntermSequence &arguments = *function->arg;
1198
1199 for(size_t i = 0; i < argumentCount; i++)
1200 {
1201 TIntermTyped *in = arguments[i]->getAsTyped();
1202
1203 if(in->getQualifier() == EvqIn ||
1204 in->getQualifier() == EvqInOut ||
1205 in->getQualifier() == EvqConstReadOnly)
1206 {
1207 copy(in, arg[i]);
1208 }
1209 }
1210
1211 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1212 call->dst.type = sw::Shader::PARAMETER_LABEL;
1213 call->dst.index = function->label;
1214
1215 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1216 {
1217 copy(result, function->ret);
1218 }
1219
1220 for(size_t i = 0; i < argumentCount; i++)
1221 {
1222 TIntermTyped *argument = arguments[i]->getAsTyped();
1223 TIntermTyped *out = arg[i]->getAsTyped();
1224
1225 if(argument->getQualifier() == EvqOut ||
1226 argument->getQualifier() == EvqInOut)
1227 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001228 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001229 }
1230 }
1231 }
1232 else
1233 {
1234 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001235 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001236 TIntermTyped *t = arg[1]->getAsTyped();
1237
1238 Temporary coord(this);
1239
1240 if(textureFunction.proj)
1241 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001242 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1243 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1244 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001245
Nicolas Capens0484c792016-06-13 22:02:36 -04001246 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1247 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001248
1249 if(IsShadowSampler(s->getBasicType()))
1250 {
1251 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1252 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1253 mov->src[0].swizzle = 0xA4;
1254 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001255 }
1256 else
1257 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001258 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1259
1260 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1261 {
1262 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1263 mov->src[0].swizzle = 0xA4;
1264 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001265 }
1266
1267 switch(textureFunction.method)
1268 {
1269 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001270 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001271 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001272 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001273 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001274 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001275 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001276 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001277 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001278 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1279 }
1280 else UNREACHABLE(argumentCount);
1281 }
1282 else // Offset
1283 {
1284 if(argumentCount == 3)
1285 {
1286 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1287 }
1288 else if(argumentCount == 4) // Bias
1289 {
1290 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001291 }
1292 else UNREACHABLE(argumentCount);
1293 }
1294 break;
1295 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001296 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001297 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001298 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001299 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001300 else if(argumentCount == 4) // Offset
1301 {
1302 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1303 }
1304 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001305 break;
1306 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001307 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001308 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001309 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001310 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001311 else if(argumentCount == 4) // Offset
1312 {
1313 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1314 }
1315 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001316 break;
1317 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001318 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001319 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001320 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001321 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001322 else if(argumentCount == 5) // Offset
1323 {
1324 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1325 }
1326 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001327 break;
1328 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001329 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001330 break;
1331 default:
1332 UNREACHABLE(textureFunction.method);
1333 }
1334 }
1335 }
1336 break;
1337 case EOpParameters:
1338 break;
1339 case EOpConstructFloat:
1340 case EOpConstructVec2:
1341 case EOpConstructVec3:
1342 case EOpConstructVec4:
1343 case EOpConstructBool:
1344 case EOpConstructBVec2:
1345 case EOpConstructBVec3:
1346 case EOpConstructBVec4:
1347 case EOpConstructInt:
1348 case EOpConstructIVec2:
1349 case EOpConstructIVec3:
1350 case EOpConstructIVec4:
1351 case EOpConstructUInt:
1352 case EOpConstructUVec2:
1353 case EOpConstructUVec3:
1354 case EOpConstructUVec4:
1355 if(visit == PostVisit)
1356 {
1357 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001358 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1359 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001360 for(size_t i = 0; i < argumentCount; i++)
1361 {
1362 TIntermTyped *argi = arg[i]->getAsTyped();
1363 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001364 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1365 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001366
1367 if(!argi->isMatrix())
1368 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001369 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1370 mov->dst.mask = (0xF << swizzle) & 0xF;
1371 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001372
1373 component += size;
1374 }
1375 else // Matrix
1376 {
1377 int column = 0;
1378
1379 while(component < resultType.getNominalSize())
1380 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001381 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1382 mov->dst.mask = (0xF << swizzle) & 0xF;
1383 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001384
1385 column++;
1386 component += size;
1387 }
1388 }
1389 }
1390 }
1391 break;
1392 case EOpConstructMat2:
1393 case EOpConstructMat2x3:
1394 case EOpConstructMat2x4:
1395 case EOpConstructMat3x2:
1396 case EOpConstructMat3:
1397 case EOpConstructMat3x4:
1398 case EOpConstructMat4x2:
1399 case EOpConstructMat4x3:
1400 case EOpConstructMat4:
1401 if(visit == PostVisit)
1402 {
1403 TIntermTyped *arg0 = arg[0]->getAsTyped();
1404 const int outCols = result->getNominalSize();
1405 const int outRows = result->getSecondarySize();
1406
1407 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1408 {
1409 for(int i = 0; i < outCols; i++)
1410 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001411 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001412 Instruction *mov = emitCast(result, i, arg0, 0);
1413 mov->dst.mask = 1 << i;
1414 ASSERT(mov->src[0].swizzle == 0x00);
1415 }
1416 }
1417 else if(arg0->isMatrix())
1418 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001419 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001420
Alexis Hetu2a198552016-09-27 20:50:45 -04001421 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001422 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001423 TIntermTyped *argi = arg[n]->getAsTyped();
1424 const int inCols = argi->getNominalSize();
1425 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001426
Alexis Hetu2a198552016-09-27 20:50:45 -04001427 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001428 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001429 if(i >= inCols || outRows > inRows)
1430 {
1431 // Initialize to identity matrix
1432 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));
1433 emitCast(result, i + n * outCols, &col, 0);
1434 }
1435
1436 if(i < inCols)
1437 {
1438 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1439 mov->dst.mask = 0xF >> (4 - inRows);
1440 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001441 }
1442 }
1443 }
1444 else
1445 {
1446 int column = 0;
1447 int row = 0;
1448
1449 for(size_t i = 0; i < argumentCount; i++)
1450 {
1451 TIntermTyped *argi = arg[i]->getAsTyped();
1452 int size = argi->getNominalSize();
1453 int element = 0;
1454
1455 while(element < size)
1456 {
1457 Instruction *mov = emitCast(result, column, argi, 0);
1458 mov->dst.mask = (0xF << row) & 0xF;
1459 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1460
1461 int end = row + size - element;
1462 column = end >= outRows ? column + 1 : column;
1463 element = element + outRows - row;
1464 row = end >= outRows ? 0 : end;
1465 }
1466 }
1467 }
1468 }
1469 break;
1470 case EOpConstructStruct:
1471 if(visit == PostVisit)
1472 {
1473 int offset = 0;
1474 for(size_t i = 0; i < argumentCount; i++)
1475 {
1476 TIntermTyped *argi = arg[i]->getAsTyped();
1477 int size = argi->totalRegisterCount();
1478
1479 for(int index = 0; index < size; index++)
1480 {
1481 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1482 mov->dst.mask = writeMask(result, offset + index);
1483 }
1484
1485 offset += size;
1486 }
1487 }
1488 break;
1489 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1490 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1491 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1492 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1493 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1494 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1495 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1496 case EOpModf:
1497 if(visit == PostVisit)
1498 {
1499 TIntermTyped* arg1 = arg[1]->getAsTyped();
1500 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1501 assignLvalue(arg1, arg1);
1502 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1503 }
1504 break;
1505 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1506 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1507 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1508 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1509 case EOpClamp:
1510 if(visit == PostVisit)
1511 {
1512 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1513 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1514 }
1515 break;
1516 case EOpMix: if(visit == PostVisit) emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]); break;
1517 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1518 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1519 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1520 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1521 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1522 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1523 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1524 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1525 case EOpMul:
1526 if(visit == PostVisit)
1527 {
1528 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001529 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1530 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001531
1532 int size = arg0->getNominalSize();
1533 for(int i = 0; i < size; i++)
1534 {
1535 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1536 }
1537 }
1538 break;
1539 case EOpOuterProduct:
1540 if(visit == PostVisit)
1541 {
1542 for(int i = 0; i < dim(arg[1]); i++)
1543 {
1544 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1545 mul->src[1].swizzle = 0x55 * i;
1546 }
1547 }
1548 break;
1549 default: UNREACHABLE(node->getOp());
1550 }
1551
1552 return true;
1553 }
1554
1555 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1556 {
1557 if(currentScope != emitScope)
1558 {
1559 return false;
1560 }
1561
1562 TIntermTyped *condition = node->getCondition();
1563 TIntermNode *trueBlock = node->getTrueBlock();
1564 TIntermNode *falseBlock = node->getFalseBlock();
1565 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1566
1567 condition->traverse(this);
1568
1569 if(node->usesTernaryOperator())
1570 {
1571 if(constantCondition)
1572 {
1573 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1574
1575 if(trueCondition)
1576 {
1577 trueBlock->traverse(this);
1578 copy(node, trueBlock);
1579 }
1580 else
1581 {
1582 falseBlock->traverse(this);
1583 copy(node, falseBlock);
1584 }
1585 }
1586 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1587 {
1588 trueBlock->traverse(this);
1589 falseBlock->traverse(this);
1590 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1591 }
1592 else
1593 {
1594 emit(sw::Shader::OPCODE_IF, 0, condition);
1595
1596 if(trueBlock)
1597 {
1598 trueBlock->traverse(this);
1599 copy(node, trueBlock);
1600 }
1601
1602 if(falseBlock)
1603 {
1604 emit(sw::Shader::OPCODE_ELSE);
1605 falseBlock->traverse(this);
1606 copy(node, falseBlock);
1607 }
1608
1609 emit(sw::Shader::OPCODE_ENDIF);
1610 }
1611 }
1612 else // if/else statement
1613 {
1614 if(constantCondition)
1615 {
1616 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1617
1618 if(trueCondition)
1619 {
1620 if(trueBlock)
1621 {
1622 trueBlock->traverse(this);
1623 }
1624 }
1625 else
1626 {
1627 if(falseBlock)
1628 {
1629 falseBlock->traverse(this);
1630 }
1631 }
1632 }
1633 else
1634 {
1635 emit(sw::Shader::OPCODE_IF, 0, condition);
1636
1637 if(trueBlock)
1638 {
1639 trueBlock->traverse(this);
1640 }
1641
1642 if(falseBlock)
1643 {
1644 emit(sw::Shader::OPCODE_ELSE);
1645 falseBlock->traverse(this);
1646 }
1647
1648 emit(sw::Shader::OPCODE_ENDIF);
1649 }
1650 }
1651
1652 return false;
1653 }
1654
1655 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1656 {
1657 if(currentScope != emitScope)
1658 {
1659 return false;
1660 }
1661
1662 unsigned int iterations = loopCount(node);
1663
1664 if(iterations == 0)
1665 {
1666 return false;
1667 }
1668
1669 bool unroll = (iterations <= 4);
1670
1671 if(unroll)
1672 {
1673 LoopUnrollable loopUnrollable;
1674 unroll = loopUnrollable.traverse(node);
1675 }
1676
1677 TIntermNode *init = node->getInit();
1678 TIntermTyped *condition = node->getCondition();
1679 TIntermTyped *expression = node->getExpression();
1680 TIntermNode *body = node->getBody();
1681 Constant True(true);
1682
1683 if(node->getType() == ELoopDoWhile)
1684 {
1685 Temporary iterate(this);
1686 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1687
1688 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1689
1690 if(body)
1691 {
1692 body->traverse(this);
1693 }
1694
1695 emit(sw::Shader::OPCODE_TEST);
1696
1697 condition->traverse(this);
1698 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1699
1700 emit(sw::Shader::OPCODE_ENDWHILE);
1701 }
1702 else
1703 {
1704 if(init)
1705 {
1706 init->traverse(this);
1707 }
1708
1709 if(unroll)
1710 {
1711 for(unsigned int i = 0; i < iterations; i++)
1712 {
1713 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1714
1715 if(body)
1716 {
1717 body->traverse(this);
1718 }
1719
1720 if(expression)
1721 {
1722 expression->traverse(this);
1723 }
1724 }
1725 }
1726 else
1727 {
1728 if(condition)
1729 {
1730 condition->traverse(this);
1731 }
1732 else
1733 {
1734 condition = &True;
1735 }
1736
1737 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1738
1739 if(body)
1740 {
1741 body->traverse(this);
1742 }
1743
1744 emit(sw::Shader::OPCODE_TEST);
1745
1746 if(expression)
1747 {
1748 expression->traverse(this);
1749 }
1750
1751 if(condition)
1752 {
1753 condition->traverse(this);
1754 }
1755
1756 emit(sw::Shader::OPCODE_ENDWHILE);
1757 }
1758 }
1759
1760 return false;
1761 }
1762
1763 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1764 {
1765 if(currentScope != emitScope)
1766 {
1767 return false;
1768 }
1769
1770 switch(node->getFlowOp())
1771 {
1772 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1773 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1774 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1775 case EOpReturn:
1776 if(visit == PostVisit)
1777 {
1778 TIntermTyped *value = node->getExpression();
1779
1780 if(value)
1781 {
1782 copy(functionArray[currentFunction].ret, value);
1783 }
1784
1785 emit(sw::Shader::OPCODE_LEAVE);
1786 }
1787 break;
1788 default: UNREACHABLE(node->getFlowOp());
1789 }
1790
1791 return true;
1792 }
1793
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001794 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1795 {
1796 if(currentScope != emitScope)
1797 {
1798 return false;
1799 }
1800
1801 TIntermTyped* switchValue = node->getInit();
1802 TIntermAggregate* opList = node->getStatementList();
1803
1804 if(!switchValue || !opList)
1805 {
1806 return false;
1807 }
1808
1809 switchValue->traverse(this);
1810
1811 emit(sw::Shader::OPCODE_SWITCH);
1812
1813 TIntermSequence& sequence = opList->getSequence();
1814 TIntermSequence::iterator it = sequence.begin();
1815 TIntermSequence::iterator defaultIt = sequence.end();
1816 int nbCases = 0;
1817 for(; it != sequence.end(); ++it)
1818 {
1819 TIntermCase* currentCase = (*it)->getAsCaseNode();
1820 if(currentCase)
1821 {
1822 TIntermSequence::iterator caseIt = it;
1823
1824 TIntermTyped* condition = currentCase->getCondition();
1825 if(condition) // non default case
1826 {
1827 if(nbCases != 0)
1828 {
1829 emit(sw::Shader::OPCODE_ELSE);
1830 }
1831
1832 condition->traverse(this);
1833 Temporary result(this);
1834 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1835 emit(sw::Shader::OPCODE_IF, 0, &result);
1836 nbCases++;
1837
1838 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1839 {
1840 (*caseIt)->traverse(this);
1841 if((*caseIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1842 {
1843 break;
1844 }
1845 }
1846 }
1847 else
1848 {
1849 defaultIt = it; // The default case might not be the last case, keep it for last
1850 }
1851 }
1852 }
1853
1854 // If there's a default case, traverse it here
1855 if(defaultIt != sequence.end())
1856 {
1857 emit(sw::Shader::OPCODE_ELSE);
1858 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
1859 {
1860 (*defaultIt)->traverse(this);
1861 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1862 {
1863 break;
1864 }
1865 }
1866 }
1867
1868 for(int i = 0; i < nbCases; ++i)
1869 {
1870 emit(sw::Shader::OPCODE_ENDIF);
1871 }
1872
1873 emit(sw::Shader::OPCODE_ENDSWITCH);
1874
1875 return false;
1876 }
1877
Nicolas Capens0bac2852016-05-07 06:09:58 -04001878 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
1879 {
1880 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
1881 }
1882
1883 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
1884 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
1885 {
1886 Instruction *instruction = new Instruction(op);
1887
1888 if(dst)
1889 {
1890 instruction->dst.type = registerType(dst);
1891 instruction->dst.index = registerIndex(dst) + dstIndex;
1892 instruction->dst.mask = writeMask(dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001893 }
1894
Alexis Hetu929c6b02017-11-07 16:04:25 -05001895 if(src0)
1896 {
1897 TIntermTyped* src = src0->getAsTyped();
1898 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
1899 }
1900
Nicolas Capens0bac2852016-05-07 06:09:58 -04001901 argument(instruction->src[0], src0, index0);
1902 argument(instruction->src[1], src1, index1);
1903 argument(instruction->src[2], src2, index2);
1904 argument(instruction->src[3], src3, index3);
1905 argument(instruction->src[4], src4, index4);
1906
1907 shader->append(instruction);
1908
1909 return instruction;
1910 }
1911
1912 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
1913 {
1914 return emitCast(dst, 0, src, 0);
1915 }
1916
1917 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
1918 {
1919 switch(src->getBasicType())
1920 {
1921 case EbtBool:
1922 switch(dst->getBasicType())
1923 {
1924 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1925 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1926 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
1927 default: break;
1928 }
1929 break;
1930 case EbtInt:
1931 switch(dst->getBasicType())
1932 {
1933 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1934 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
1935 default: break;
1936 }
1937 break;
1938 case EbtUInt:
1939 switch(dst->getBasicType())
1940 {
1941 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1942 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
1943 default: break;
1944 }
1945 break;
1946 case EbtFloat:
1947 switch(dst->getBasicType())
1948 {
1949 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
1950 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
1951 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
1952 default: break;
1953 }
1954 break;
1955 default:
1956 break;
1957 }
1958
1959 ASSERT((src->getBasicType() == dst->getBasicType()) ||
1960 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
1961 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
1962
1963 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
1964 }
1965
1966 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1967 {
1968 for(int index = 0; index < dst->elementRegisterCount(); index++)
1969 {
1970 emit(op, dst, index, src0, index, src1, index, src2, index);
1971 }
1972 }
1973
1974 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1975 {
1976 emitBinary(op, result, src0, src1);
1977 assignLvalue(lhs, result);
1978 }
1979
1980 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1981 {
1982 sw::Shader::Opcode opcode;
1983 switch(left->getAsTyped()->getBasicType())
1984 {
1985 case EbtBool:
1986 case EbtInt:
1987 opcode = sw::Shader::OPCODE_ICMP;
1988 break;
1989 case EbtUInt:
1990 opcode = sw::Shader::OPCODE_UCMP;
1991 break;
1992 default:
1993 opcode = sw::Shader::OPCODE_CMP;
1994 break;
1995 }
1996
1997 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
1998 cmp->control = cmpOp;
1999 }
2000
2001 int componentCount(const TType &type, int registers)
2002 {
2003 if(registers == 0)
2004 {
2005 return 0;
2006 }
2007
2008 if(type.isArray() && registers >= type.elementRegisterCount())
2009 {
2010 int index = registers / type.elementRegisterCount();
2011 registers -= index * type.elementRegisterCount();
2012 return index * type.getElementSize() + componentCount(type, registers);
2013 }
2014
2015 if(type.isStruct() || type.isInterfaceBlock())
2016 {
2017 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2018 int elements = 0;
2019
2020 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2021 {
2022 const TType &fieldType = *((*field)->type());
2023
2024 if(fieldType.totalRegisterCount() <= registers)
2025 {
2026 registers -= fieldType.totalRegisterCount();
2027 elements += fieldType.getObjectSize();
2028 }
2029 else // Register within this field
2030 {
2031 return elements + componentCount(fieldType, registers);
2032 }
2033 }
2034 }
2035 else if(type.isMatrix())
2036 {
2037 return registers * type.registerSize();
2038 }
2039
2040 UNREACHABLE(0);
2041 return 0;
2042 }
2043
2044 int registerSize(const TType &type, int registers)
2045 {
2046 if(registers == 0)
2047 {
2048 if(type.isStruct())
2049 {
2050 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2051 }
2052 else if(type.isInterfaceBlock())
2053 {
2054 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2055 }
2056
2057 return type.registerSize();
2058 }
2059
2060 if(type.isArray() && registers >= type.elementRegisterCount())
2061 {
2062 int index = registers / type.elementRegisterCount();
2063 registers -= index * type.elementRegisterCount();
2064 return registerSize(type, registers);
2065 }
2066
2067 if(type.isStruct() || type.isInterfaceBlock())
2068 {
2069 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2070 int elements = 0;
2071
2072 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2073 {
2074 const TType &fieldType = *((*field)->type());
2075
2076 if(fieldType.totalRegisterCount() <= registers)
2077 {
2078 registers -= fieldType.totalRegisterCount();
2079 elements += fieldType.getObjectSize();
2080 }
2081 else // Register within this field
2082 {
2083 return registerSize(fieldType, registers);
2084 }
2085 }
2086 }
2087 else if(type.isMatrix())
2088 {
2089 return registerSize(type, 0);
2090 }
2091
2092 UNREACHABLE(0);
2093 return 0;
2094 }
2095
2096 int OutputASM::getBlockId(TIntermTyped *arg)
2097 {
2098 if(arg)
2099 {
2100 const TType &type = arg->getType();
2101 TInterfaceBlock* block = type.getInterfaceBlock();
2102 if(block && (type.getQualifier() == EvqUniform))
2103 {
2104 // Make sure the uniform block is declared
2105 uniformRegister(arg);
2106
2107 const char* blockName = block->name().c_str();
2108
2109 // Fetch uniform block index from array of blocks
2110 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2111 {
2112 if(blockName == it->name)
2113 {
2114 return it->blockId;
2115 }
2116 }
2117
2118 ASSERT(false);
2119 }
2120 }
2121
2122 return -1;
2123 }
2124
2125 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2126 {
2127 const TType &type = arg->getType();
2128 int blockId = getBlockId(arg);
2129 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2130 if(blockId != -1)
2131 {
2132 argumentInfo.bufferIndex = 0;
2133 for(int i = 0; i < blockId; ++i)
2134 {
2135 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2136 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2137 }
2138
2139 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2140
2141 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2142 BlockDefinitionIndexMap::const_iterator it = itEnd;
2143
2144 argumentInfo.clampedIndex = index;
2145 if(type.isInterfaceBlock())
2146 {
2147 // Offset index to the beginning of the selected instance
2148 int blockRegisters = type.elementRegisterCount();
2149 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2150 argumentInfo.bufferIndex += bufferOffset;
2151 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2152 }
2153
2154 int regIndex = registerIndex(arg);
2155 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2156 {
2157 it = blockDefinition.find(i);
2158 if(it != itEnd)
2159 {
2160 argumentInfo.clampedIndex -= (i - regIndex);
2161 break;
2162 }
2163 }
2164 ASSERT(it != itEnd);
2165
2166 argumentInfo.typedMemberInfo = it->second;
2167
2168 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2169 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2170 }
2171 else
2172 {
2173 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2174 }
2175
2176 return argumentInfo;
2177 }
2178
2179 void OutputASM::argument(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
2180 {
2181 if(argument)
2182 {
2183 TIntermTyped *arg = argument->getAsTyped();
2184 Temporary unpackedUniform(this);
2185
2186 const TType& srcType = arg->getType();
2187 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2188 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2189 {
2190 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2191 const TType &memberType = argumentInfo.typedMemberInfo.type;
2192
2193 if(memberType.getBasicType() == EbtBool)
2194 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002195 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002196
2197 // Convert the packed bool, which is currently an int, to a true bool
2198 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2199 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2200 instruction->dst.index = registerIndex(&unpackedUniform);
2201 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2202 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2203 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2204
2205 shader->append(instruction);
2206
2207 arg = &unpackedUniform;
2208 index = 0;
2209 }
2210 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2211 {
2212 int numCols = memberType.getNominalSize();
2213 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002214
Alexis Hetue97a31e2016-11-14 14:10:47 -05002215 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002216
2217 unsigned int dstIndex = registerIndex(&unpackedUniform);
2218 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2219 int arrayIndex = argumentInfo.clampedIndex / numCols;
2220 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2221
2222 for(int j = 0; j < numRows; ++j)
2223 {
2224 // Transpose the row major matrix
2225 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2226 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2227 instruction->dst.index = dstIndex;
2228 instruction->dst.mask = 1 << j;
2229 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2230 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2231 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2232 instruction->src[0].swizzle = srcSwizzle;
2233
2234 shader->append(instruction);
2235 }
2236
2237 arg = &unpackedUniform;
2238 index = 0;
2239 }
2240 }
2241
2242 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2243 const TType &type = argumentInfo.typedMemberInfo.type;
2244
2245 int size = registerSize(type, argumentInfo.clampedIndex);
2246
2247 parameter.type = registerType(arg);
2248 parameter.bufferIndex = argumentInfo.bufferIndex;
2249
2250 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2251 {
2252 int component = componentCount(type, argumentInfo.clampedIndex);
2253 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2254
2255 for(int i = 0; i < 4; i++)
2256 {
2257 if(size == 1) // Replicate
2258 {
2259 parameter.value[i] = constants[component + 0].getAsFloat();
2260 }
2261 else if(i < size)
2262 {
2263 parameter.value[i] = constants[component + i].getAsFloat();
2264 }
2265 else
2266 {
2267 parameter.value[i] = 0.0f;
2268 }
2269 }
2270 }
2271 else
2272 {
2273 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2274
2275 if(parameter.bufferIndex != -1)
2276 {
2277 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2278 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2279 }
2280 }
2281
2282 if(!IsSampler(arg->getBasicType()))
2283 {
2284 parameter.swizzle = readSwizzle(arg, size);
2285 }
2286 }
2287 }
2288
2289 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2290 {
2291 for(int index = 0; index < dst->totalRegisterCount(); index++)
2292 {
2293 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
2294 mov->dst.mask = writeMask(dst, index);
2295 }
2296 }
2297
2298 int swizzleElement(int swizzle, int index)
2299 {
2300 return (swizzle >> (index * 2)) & 0x03;
2301 }
2302
2303 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2304 {
2305 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2306 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2307 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2308 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2309 }
2310
2311 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2312 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002313 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2314 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002315 {
2316 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2317 }
2318
2319 TIntermBinary *binary = dst->getAsBinaryNode();
2320
2321 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2322 {
2323 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2324
2325 Temporary address(this);
2326 lvalue(insert->dst, address, dst);
2327
2328 insert->src[0].type = insert->dst.type;
2329 insert->src[0].index = insert->dst.index;
2330 insert->src[0].rel = insert->dst.rel;
2331 argument(insert->src[1], src);
2332 argument(insert->src[2], binary->getRight());
2333
2334 shader->append(insert);
2335 }
2336 else
2337 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002338 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2339
2340 Temporary address(this);
2341 int swizzle = lvalue(mov1->dst, address, dst);
2342
2343 argument(mov1->src[0], src);
2344 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2345
2346 shader->append(mov1);
2347
2348 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002349 {
2350 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2351
Nicolas Capens84249fd2017-11-09 11:20:51 -05002352 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002353 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002354 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002355
2356 argument(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002357
2358 shader->append(mov);
2359 }
2360 }
2361 }
2362
2363 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node)
2364 {
2365 TIntermTyped *result = node;
2366 TIntermBinary *binary = node->getAsBinaryNode();
2367 TIntermSymbol *symbol = node->getAsSymbolNode();
2368
2369 if(binary)
2370 {
2371 TIntermTyped *left = binary->getLeft();
2372 TIntermTyped *right = binary->getRight();
2373
2374 int leftSwizzle = lvalue(dst, address, left); // Resolve the l-value of the left side
2375
2376 switch(binary->getOp())
2377 {
2378 case EOpIndexDirect:
2379 {
2380 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2381
2382 if(left->isRegister())
2383 {
2384 int leftMask = dst.mask;
2385
2386 dst.mask = 1;
2387 while((leftMask & dst.mask) == 0)
2388 {
2389 dst.mask = dst.mask << 1;
2390 }
2391
2392 int element = swizzleElement(leftSwizzle, rightIndex);
2393 dst.mask = 1 << element;
2394
2395 return element;
2396 }
2397 else if(left->isArray() || left->isMatrix())
2398 {
2399 dst.index += rightIndex * result->totalRegisterCount();
2400 return 0xE4;
2401 }
2402 else UNREACHABLE(0);
2403 }
2404 break;
2405 case EOpIndexIndirect:
2406 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002407 right->traverse(this);
2408
Nicolas Capens0bac2852016-05-07 06:09:58 -04002409 if(left->isRegister())
2410 {
2411 // Requires INSERT instruction (handled by calling function)
2412 }
2413 else if(left->isArray() || left->isMatrix())
2414 {
2415 int scale = result->totalRegisterCount();
2416
2417 if(dst.rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
2418 {
2419 if(left->totalRegisterCount() > 1)
2420 {
2421 sw::Shader::SourceParameter relativeRegister;
2422 argument(relativeRegister, right);
2423
2424 dst.rel.index = relativeRegister.index;
2425 dst.rel.type = relativeRegister.type;
2426 dst.rel.scale = scale;
2427 dst.rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
2428 }
2429 }
2430 else if(dst.rel.index != registerIndex(&address)) // Move the previous index register to the address register
2431 {
2432 if(scale == 1)
2433 {
2434 Constant oldScale((int)dst.rel.scale);
2435 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
2436 mad->src[0].index = dst.rel.index;
2437 mad->src[0].type = dst.rel.type;
2438 }
2439 else
2440 {
2441 Constant oldScale((int)dst.rel.scale);
2442 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
2443 mul->src[0].index = dst.rel.index;
2444 mul->src[0].type = dst.rel.type;
2445
2446 Constant newScale(scale);
2447 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2448 }
2449
2450 dst.rel.type = sw::Shader::PARAMETER_TEMP;
2451 dst.rel.index = registerIndex(&address);
2452 dst.rel.scale = 1;
2453 }
2454 else // Just add the new index to the address register
2455 {
2456 if(scale == 1)
2457 {
2458 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2459 }
2460 else
2461 {
2462 Constant newScale(scale);
2463 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2464 }
2465 }
2466 }
2467 else UNREACHABLE(0);
2468 }
2469 break;
2470 case EOpIndexDirectStruct:
2471 case EOpIndexDirectInterfaceBlock:
2472 {
2473 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2474 left->getType().getStruct()->fields() :
2475 left->getType().getInterfaceBlock()->fields();
2476 int index = right->getAsConstantUnion()->getIConst(0);
2477 int fieldOffset = 0;
2478
2479 for(int i = 0; i < index; i++)
2480 {
2481 fieldOffset += fields[i]->type()->totalRegisterCount();
2482 }
2483
2484 dst.type = registerType(left);
2485 dst.index += fieldOffset;
Nicolas Capens8157d5c2017-01-04 11:30:45 -05002486 dst.mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002487
2488 return 0xE4;
2489 }
2490 break;
2491 case EOpVectorSwizzle:
2492 {
2493 ASSERT(left->isRegister());
2494
2495 int leftMask = dst.mask;
2496
2497 int swizzle = 0;
2498 int rightMask = 0;
2499
2500 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2501
2502 for(unsigned int i = 0; i < sequence.size(); i++)
2503 {
2504 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2505
2506 int element = swizzleElement(leftSwizzle, index);
2507 rightMask = rightMask | (1 << element);
2508 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2509 }
2510
2511 dst.mask = leftMask & rightMask;
2512
2513 return swizzle;
2514 }
2515 break;
2516 default:
2517 UNREACHABLE(binary->getOp()); // Not an l-value operator
2518 break;
2519 }
2520 }
2521 else if(symbol)
2522 {
2523 dst.type = registerType(symbol);
2524 dst.index = registerIndex(symbol);
2525 dst.mask = writeMask(symbol);
2526 return 0xE4;
2527 }
2528
2529 return 0xE4;
2530 }
2531
2532 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2533 {
2534 if(isSamplerRegister(operand))
2535 {
2536 return sw::Shader::PARAMETER_SAMPLER;
2537 }
2538
2539 const TQualifier qualifier = operand->getQualifier();
2540 if((EvqFragColor == qualifier) || (EvqFragData == qualifier))
2541 {
2542 if(((EvqFragData == qualifier) && (EvqFragColor == outputQualifier)) ||
2543 ((EvqFragColor == qualifier) && (EvqFragData == outputQualifier)))
2544 {
2545 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2546 }
2547 outputQualifier = qualifier;
2548 }
2549
2550 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2551 {
2552 return sw::Shader::PARAMETER_TEMP;
2553 }
2554
2555 switch(qualifier)
2556 {
2557 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2558 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2559 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2560 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2561 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2562 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2563 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2564 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2565 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2566 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2567 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2568 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2569 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2570 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2571 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2572 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2573 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2574 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2575 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2576 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2577 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2578 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2579 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2580 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2581 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2582 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002583 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002584 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2585 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2586 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2587 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2588 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2589 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2590 default: UNREACHABLE(qualifier);
2591 }
2592
2593 return sw::Shader::PARAMETER_VOID;
2594 }
2595
Alexis Hetu12b00502016-05-20 13:01:11 -04002596 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2597 {
2598 const TQualifier qualifier = operand->getQualifier();
2599 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2600 }
2601
Nicolas Capens0bac2852016-05-07 06:09:58 -04002602 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2603 {
2604 if(isSamplerRegister(operand))
2605 {
2606 return samplerRegister(operand);
2607 }
2608
2609 switch(operand->getQualifier())
2610 {
2611 case EvqTemporary: return temporaryRegister(operand);
2612 case EvqGlobal: return temporaryRegister(operand);
2613 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2614 case EvqAttribute: return attributeRegister(operand);
2615 case EvqVaryingIn: return varyingRegister(operand);
2616 case EvqVaryingOut: return varyingRegister(operand);
2617 case EvqVertexIn: return attributeRegister(operand);
2618 case EvqFragmentOut: return fragmentOutputRegister(operand);
2619 case EvqVertexOut: return varyingRegister(operand);
2620 case EvqFragmentIn: return varyingRegister(operand);
2621 case EvqInvariantVaryingIn: return varyingRegister(operand);
2622 case EvqInvariantVaryingOut: return varyingRegister(operand);
2623 case EvqSmooth: return varyingRegister(operand);
2624 case EvqFlat: return varyingRegister(operand);
2625 case EvqCentroidOut: return varyingRegister(operand);
2626 case EvqSmoothIn: return varyingRegister(operand);
2627 case EvqFlatIn: return varyingRegister(operand);
2628 case EvqCentroidIn: return varyingRegister(operand);
2629 case EvqUniform: return uniformRegister(operand);
2630 case EvqIn: return temporaryRegister(operand);
2631 case EvqOut: return temporaryRegister(operand);
2632 case EvqInOut: return temporaryRegister(operand);
2633 case EvqConstReadOnly: return temporaryRegister(operand);
2634 case EvqPosition: return varyingRegister(operand);
2635 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002636 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2637 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2638 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2639 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002640 case EvqPointCoord: return varyingRegister(operand);
2641 case EvqFragColor: return 0;
2642 case EvqFragData: return fragmentOutputRegister(operand);
2643 case EvqFragDepth: return 0;
2644 default: UNREACHABLE(operand->getQualifier());
2645 }
2646
2647 return 0;
2648 }
2649
2650 int OutputASM::writeMask(TIntermTyped *destination, int index)
2651 {
2652 if(destination->getQualifier() == EvqPointSize)
2653 {
2654 return 0x2; // Point size stored in the y component
2655 }
2656
2657 return 0xF >> (4 - registerSize(destination->getType(), index));
2658 }
2659
2660 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2661 {
2662 if(argument->getQualifier() == EvqPointSize)
2663 {
2664 return 0x55; // Point size stored in the y component
2665 }
2666
2667 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2668
2669 return swizzleSize[size];
2670 }
2671
2672 // Conservatively checks whether an expression is fast to compute and has no side effects
2673 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2674 {
2675 if(!expression->isRegister())
2676 {
2677 return false;
2678 }
2679
2680 return cost(expression, budget) >= 0;
2681 }
2682
2683 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2684 int OutputASM::cost(TIntermNode *expression, int budget)
2685 {
2686 if(budget < 0)
2687 {
2688 return budget;
2689 }
2690
2691 if(expression->getAsSymbolNode())
2692 {
2693 return budget;
2694 }
2695 else if(expression->getAsConstantUnion())
2696 {
2697 return budget;
2698 }
2699 else if(expression->getAsBinaryNode())
2700 {
2701 TIntermBinary *binary = expression->getAsBinaryNode();
2702
2703 switch(binary->getOp())
2704 {
2705 case EOpVectorSwizzle:
2706 case EOpIndexDirect:
2707 case EOpIndexDirectStruct:
2708 case EOpIndexDirectInterfaceBlock:
2709 return cost(binary->getLeft(), budget - 0);
2710 case EOpAdd:
2711 case EOpSub:
2712 case EOpMul:
2713 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2714 default:
2715 return -1;
2716 }
2717 }
2718 else if(expression->getAsUnaryNode())
2719 {
2720 TIntermUnary *unary = expression->getAsUnaryNode();
2721
2722 switch(unary->getOp())
2723 {
2724 case EOpAbs:
2725 case EOpNegative:
2726 return cost(unary->getOperand(), budget - 1);
2727 default:
2728 return -1;
2729 }
2730 }
2731 else if(expression->getAsSelectionNode())
2732 {
2733 TIntermSelection *selection = expression->getAsSelectionNode();
2734
2735 if(selection->usesTernaryOperator())
2736 {
2737 TIntermTyped *condition = selection->getCondition();
2738 TIntermNode *trueBlock = selection->getTrueBlock();
2739 TIntermNode *falseBlock = selection->getFalseBlock();
2740 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2741
2742 if(constantCondition)
2743 {
2744 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2745
2746 if(trueCondition)
2747 {
2748 return cost(trueBlock, budget - 0);
2749 }
2750 else
2751 {
2752 return cost(falseBlock, budget - 0);
2753 }
2754 }
2755 else
2756 {
2757 return cost(trueBlock, cost(falseBlock, budget - 2));
2758 }
2759 }
2760 }
2761
2762 return -1;
2763 }
2764
2765 const Function *OutputASM::findFunction(const TString &name)
2766 {
2767 for(unsigned int f = 0; f < functionArray.size(); f++)
2768 {
2769 if(functionArray[f].name == name)
2770 {
2771 return &functionArray[f];
2772 }
2773 }
2774
2775 return 0;
2776 }
2777
2778 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2779 {
2780 return allocate(temporaries, temporary);
2781 }
2782
Alexis Hetu49351232017-11-02 16:00:32 -04002783 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
2784 {
2785 if(type.isStruct())
2786 {
2787 const TFieldList &fields = type.getStruct()->fields();
2788 int fieldVar = var;
2789 for(size_t i = 0; i < fields.size(); i++)
2790 {
2791 const TType& fieldType = *(fields[i]->type());
2792 setPixelShaderInputs(fieldType, fieldVar, flat);
2793 fieldVar += fieldType.totalRegisterCount();
2794 }
2795 }
2796 else
2797 {
2798 for(int i = 0; i < type.totalRegisterCount(); i++)
2799 {
2800 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2801 }
2802 }
2803 }
2804
Nicolas Capens0bac2852016-05-07 06:09:58 -04002805 int OutputASM::varyingRegister(TIntermTyped *varying)
2806 {
2807 int var = lookup(varyings, varying);
2808
2809 if(var == -1)
2810 {
2811 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002812 int registerCount = varying->totalRegisterCount();
2813
2814 if(pixelShader)
2815 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04002816 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002817 {
2818 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2819 return 0;
2820 }
2821
2822 if(varying->getQualifier() == EvqPointCoord)
2823 {
2824 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04002825 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002826 }
2827 else
2828 {
Alexis Hetu49351232017-11-02 16:00:32 -04002829 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002830 }
2831 }
2832 else if(vertexShader)
2833 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04002834 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002835 {
2836 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2837 return 0;
2838 }
2839
2840 if(varying->getQualifier() == EvqPosition)
2841 {
2842 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002843 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002844 }
2845 else if(varying->getQualifier() == EvqPointSize)
2846 {
2847 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002848 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002849 }
2850 else
2851 {
2852 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2853 }
2854 }
2855 else UNREACHABLE(0);
2856
2857 declareVarying(varying, var);
2858 }
2859
2860 return var;
2861 }
2862
2863 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2864 {
2865 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
2866 {
Alexis Hetu49351232017-11-02 16:00:32 -04002867 TIntermSymbol *symbol = varying->getAsSymbolNode();
2868 declareVarying(varying->getType(), symbol->getSymbol(), reg);
2869 }
2870 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002871
Alexis Hetu49351232017-11-02 16:00:32 -04002872 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
2873 {
2874 const char *name = varyingName.c_str();
2875 VaryingList &activeVaryings = shaderObject->varyings;
2876
2877 TStructure* structure = type.getStruct();
2878 if(structure)
2879 {
2880 int fieldRegisterIndex = registerIndex;
2881
2882 const TFieldList &fields = type.getStruct()->fields();
2883 for(size_t i = 0; i < fields.size(); i++)
2884 {
2885 const TType& fieldType = *(fields[i]->type());
2886 declareVarying(fieldType, varyingName + "." + fields[i]->name(), fieldRegisterIndex);
2887 if(fieldRegisterIndex >= 0)
2888 {
2889 fieldRegisterIndex += fieldType.totalRegisterCount();
2890 }
2891 }
2892 }
2893 else
2894 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002895 // Check if this varying has been declared before without having a register assigned
2896 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2897 {
2898 if(v->name == name)
2899 {
Alexis Hetu49351232017-11-02 16:00:32 -04002900 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002901 {
Alexis Hetu49351232017-11-02 16:00:32 -04002902 ASSERT(v->reg < 0 || v->reg == registerIndex);
2903 v->reg = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002904 }
2905
2906 return;
2907 }
2908 }
2909
Alexis Hetu49351232017-11-02 16:00:32 -04002910 activeVaryings.push_back(glsl::Varying(glVariableType(type), name, type.getArraySize(), registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002911 }
2912 }
2913
2914 int OutputASM::uniformRegister(TIntermTyped *uniform)
2915 {
2916 const TType &type = uniform->getType();
2917 ASSERT(!IsSampler(type.getBasicType()));
2918 TInterfaceBlock *block = type.getAsInterfaceBlock();
2919 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2920 ASSERT(symbol || block);
2921
2922 if(symbol || block)
2923 {
2924 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2925 bool isBlockMember = (!block && parentBlock);
2926 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2927
2928 if(index == -1 || isBlockMember)
2929 {
2930 if(index == -1)
2931 {
2932 index = allocate(uniforms, uniform);
2933 }
2934
2935 // Verify if the current uniform is a member of an already declared block
2936 const TString &name = symbol ? symbol->getSymbol() : block->name();
2937 int blockMemberIndex = blockMemberLookup(type, name, index);
2938 if(blockMemberIndex == -1)
2939 {
2940 declareUniform(type, name, index);
2941 }
2942 else
2943 {
2944 index = blockMemberIndex;
2945 }
2946 }
2947
2948 return index;
2949 }
2950
2951 return 0;
2952 }
2953
2954 int OutputASM::attributeRegister(TIntermTyped *attribute)
2955 {
2956 ASSERT(!attribute->isArray());
2957
2958 int index = lookup(attributes, attribute);
2959
2960 if(index == -1)
2961 {
2962 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2963 ASSERT(symbol);
2964
2965 if(symbol)
2966 {
2967 index = allocate(attributes, attribute);
2968 const TType &type = attribute->getType();
2969 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04002970 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2971 switch(type.getBasicType())
2972 {
2973 case EbtInt:
2974 attribType = sw::VertexShader::ATTRIBTYPE_INT;
2975 break;
2976 case EbtUInt:
2977 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
2978 break;
2979 case EbtFloat:
2980 default:
2981 break;
2982 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002983
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04002984 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002985 {
2986 for(int i = 0; i < registerCount; i++)
2987 {
Alexis Hetub7508b82016-09-22 15:36:45 -04002988 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002989 }
2990 }
2991
2992 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
2993
2994 const char *name = symbol->getSymbol().c_str();
2995 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
2996 }
2997 }
2998
2999 return index;
3000 }
3001
3002 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3003 {
3004 return allocate(fragmentOutputs, fragmentOutput);
3005 }
3006
3007 int OutputASM::samplerRegister(TIntermTyped *sampler)
3008 {
3009 const TType &type = sampler->getType();
3010 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3011
3012 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3013 TIntermBinary *binary = sampler->getAsBinaryNode();
3014
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003015 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003016 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003017 switch(type.getQualifier())
3018 {
3019 case EvqUniform:
3020 return samplerRegister(symbol);
3021 case EvqIn:
3022 case EvqConstReadOnly:
3023 // Function arguments are not (uniform) sampler registers
3024 return -1;
3025 default:
3026 UNREACHABLE(type.getQualifier());
3027 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003028 }
3029 else if(binary)
3030 {
3031 TIntermTyped *left = binary->getLeft();
3032 TIntermTyped *right = binary->getRight();
3033 const TType &leftType = left->getType();
3034 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3035 int offset = 0;
3036
3037 switch(binary->getOp())
3038 {
3039 case EOpIndexDirect:
3040 ASSERT(left->isArray());
3041 offset = index * leftType.elementRegisterCount();
3042 break;
3043 case EOpIndexDirectStruct:
3044 ASSERT(leftType.isStruct());
3045 {
3046 const TFieldList &fields = leftType.getStruct()->fields();
3047
3048 for(int i = 0; i < index; i++)
3049 {
3050 offset += fields[i]->type()->totalRegisterCount();
3051 }
3052 }
3053 break;
3054 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3055 return -1;
3056 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3057 default:
3058 UNREACHABLE(binary->getOp());
3059 return -1;
3060 }
3061
3062 int base = samplerRegister(left);
3063
3064 if(base < 0)
3065 {
3066 return -1;
3067 }
3068
3069 return base + offset;
3070 }
3071
3072 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003073 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003074 }
3075
3076 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3077 {
3078 const TType &type = sampler->getType();
3079 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3080
3081 int index = lookup(samplers, sampler);
3082
3083 if(index == -1)
3084 {
3085 index = allocate(samplers, sampler);
3086
3087 if(sampler->getQualifier() == EvqUniform)
3088 {
3089 const char *name = sampler->getSymbol().c_str();
3090 declareUniform(type, name, index);
3091 }
3092 }
3093
3094 return index;
3095 }
3096
3097 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3098 {
3099 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3100 }
3101
3102 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3103 {
3104 for(unsigned int i = 0; i < list.size(); i++)
3105 {
3106 if(list[i] == variable)
3107 {
3108 return i; // Pointer match
3109 }
3110 }
3111
3112 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3113 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3114
3115 if(varBlock)
3116 {
3117 for(unsigned int i = 0; i < list.size(); i++)
3118 {
3119 if(list[i])
3120 {
3121 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3122
3123 if(listBlock)
3124 {
3125 if(listBlock->name() == varBlock->name())
3126 {
3127 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3128 ASSERT(listBlock->fields() == varBlock->fields());
3129 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3130 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3131
3132 return i;
3133 }
3134 }
3135 }
3136 }
3137 }
3138 else if(varSymbol)
3139 {
3140 for(unsigned int i = 0; i < list.size(); i++)
3141 {
3142 if(list[i])
3143 {
3144 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3145
3146 if(listSymbol)
3147 {
3148 if(listSymbol->getId() == varSymbol->getId())
3149 {
3150 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3151 ASSERT(listSymbol->getType() == varSymbol->getType());
3152 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3153
3154 return i;
3155 }
3156 }
3157 }
3158 }
3159 }
3160
3161 return -1;
3162 }
3163
3164 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3165 {
3166 for(unsigned int i = 0; i < list.size(); i++)
3167 {
3168 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3169 {
3170 return i; // Pointer match
3171 }
3172 }
3173 return -1;
3174 }
3175
3176 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
3177 {
3178 int index = lookup(list, variable);
3179
3180 if(index == -1)
3181 {
3182 unsigned int registerCount = variable->blockRegisterCount();
3183
3184 for(unsigned int i = 0; i < list.size(); i++)
3185 {
3186 if(list[i] == 0)
3187 {
3188 unsigned int j = 1;
3189 for( ; j < registerCount && (i + j) < list.size(); j++)
3190 {
3191 if(list[i + j] != 0)
3192 {
3193 break;
3194 }
3195 }
3196
3197 if(j == registerCount) // Found free slots
3198 {
3199 for(unsigned int j = 0; j < registerCount; j++)
3200 {
3201 list[i + j] = variable;
3202 }
3203
3204 return i;
3205 }
3206 }
3207 }
3208
3209 index = list.size();
3210
3211 for(unsigned int i = 0; i < registerCount; i++)
3212 {
3213 list.push_back(variable);
3214 }
3215 }
3216
3217 return index;
3218 }
3219
3220 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3221 {
3222 int index = lookup(list, variable);
3223
3224 if(index >= 0)
3225 {
3226 list[index] = 0;
3227 }
3228 }
3229
3230 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3231 {
3232 const TInterfaceBlock *block = type.getInterfaceBlock();
3233
3234 if(block)
3235 {
3236 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3237 const TFieldList& fields = block->fields();
3238 const TString &blockName = block->name();
3239 int fieldRegisterIndex = registerIndex;
3240
3241 if(!type.isInterfaceBlock())
3242 {
3243 // This is a uniform that's part of a block, let's see if the block is already defined
3244 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3245 {
3246 if(activeUniformBlocks[i].name == blockName.c_str())
3247 {
3248 // The block is already defined, find the register for the current uniform and return it
3249 for(size_t j = 0; j < fields.size(); j++)
3250 {
3251 const TString &fieldName = fields[j]->name();
3252 if(fieldName == name)
3253 {
3254 return fieldRegisterIndex;
3255 }
3256
3257 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3258 }
3259
3260 ASSERT(false);
3261 return fieldRegisterIndex;
3262 }
3263 }
3264 }
3265 }
3266
3267 return -1;
3268 }
3269
3270 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int blockId, BlockLayoutEncoder* encoder)
3271 {
3272 const TStructure *structure = type.getStruct();
3273 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3274
3275 if(!structure && !block)
3276 {
3277 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3278 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3279 if(blockId >= 0)
3280 {
3281 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3282 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3283 }
3284 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3285 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3286 fieldRegisterIndex, blockId, blockInfo));
3287 if(IsSampler(type.getBasicType()))
3288 {
3289 for(int i = 0; i < type.totalRegisterCount(); i++)
3290 {
3291 shader->declareSampler(fieldRegisterIndex + i);
3292 }
3293 }
3294 }
3295 else if(block)
3296 {
3297 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3298 const TFieldList& fields = block->fields();
3299 const TString &blockName = block->name();
3300 int fieldRegisterIndex = registerIndex;
3301 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3302
3303 blockId = activeUniformBlocks.size();
3304 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3305 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3306 block->blockStorage(), isRowMajor, registerIndex, blockId));
3307 blockDefinitions.push_back(BlockDefinitionIndexMap());
3308
3309 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3310 currentBlockEncoder.enterAggregateType();
3311 for(size_t i = 0; i < fields.size(); i++)
3312 {
3313 const TType &fieldType = *(fields[i]->type());
3314 const TString &fieldName = fields[i]->name();
3315 if(isUniformBlockMember && (fieldName == name))
3316 {
3317 registerIndex = fieldRegisterIndex;
3318 }
3319
3320 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3321
3322 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, &currentBlockEncoder);
3323 fieldRegisterIndex += fieldType.totalRegisterCount();
3324 }
3325 currentBlockEncoder.exitAggregateType();
3326 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3327 }
3328 else
3329 {
3330 int fieldRegisterIndex = registerIndex;
3331
3332 const TFieldList& fields = structure->fields();
3333 if(type.isArray() && (structure || type.isInterfaceBlock()))
3334 {
3335 for(int i = 0; i < type.getArraySize(); i++)
3336 {
3337 if(encoder)
3338 {
3339 encoder->enterAggregateType();
3340 }
3341 for(size_t j = 0; j < fields.size(); j++)
3342 {
3343 const TType &fieldType = *(fields[j]->type());
3344 const TString &fieldName = fields[j]->name();
3345 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3346
3347 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3348 fieldRegisterIndex += fieldType.totalRegisterCount();
3349 }
3350 if(encoder)
3351 {
3352 encoder->exitAggregateType();
3353 }
3354 }
3355 }
3356 else
3357 {
3358 if(encoder)
3359 {
3360 encoder->enterAggregateType();
3361 }
3362 for(size_t i = 0; i < fields.size(); i++)
3363 {
3364 const TType &fieldType = *(fields[i]->type());
3365 const TString &fieldName = fields[i]->name();
3366 const TString uniformName = name + "." + fieldName;
3367
3368 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3369 fieldRegisterIndex += fieldType.totalRegisterCount();
3370 }
3371 if(encoder)
3372 {
3373 encoder->exitAggregateType();
3374 }
3375 }
3376 }
3377 }
3378
3379 GLenum OutputASM::glVariableType(const TType &type)
3380 {
3381 switch(type.getBasicType())
3382 {
3383 case EbtFloat:
3384 if(type.isScalar())
3385 {
3386 return GL_FLOAT;
3387 }
3388 else if(type.isVector())
3389 {
3390 switch(type.getNominalSize())
3391 {
3392 case 2: return GL_FLOAT_VEC2;
3393 case 3: return GL_FLOAT_VEC3;
3394 case 4: return GL_FLOAT_VEC4;
3395 default: UNREACHABLE(type.getNominalSize());
3396 }
3397 }
3398 else if(type.isMatrix())
3399 {
3400 switch(type.getNominalSize())
3401 {
3402 case 2:
3403 switch(type.getSecondarySize())
3404 {
3405 case 2: return GL_FLOAT_MAT2;
3406 case 3: return GL_FLOAT_MAT2x3;
3407 case 4: return GL_FLOAT_MAT2x4;
3408 default: UNREACHABLE(type.getSecondarySize());
3409 }
3410 case 3:
3411 switch(type.getSecondarySize())
3412 {
3413 case 2: return GL_FLOAT_MAT3x2;
3414 case 3: return GL_FLOAT_MAT3;
3415 case 4: return GL_FLOAT_MAT3x4;
3416 default: UNREACHABLE(type.getSecondarySize());
3417 }
3418 case 4:
3419 switch(type.getSecondarySize())
3420 {
3421 case 2: return GL_FLOAT_MAT4x2;
3422 case 3: return GL_FLOAT_MAT4x3;
3423 case 4: return GL_FLOAT_MAT4;
3424 default: UNREACHABLE(type.getSecondarySize());
3425 }
3426 default: UNREACHABLE(type.getNominalSize());
3427 }
3428 }
3429 else UNREACHABLE(0);
3430 break;
3431 case EbtInt:
3432 if(type.isScalar())
3433 {
3434 return GL_INT;
3435 }
3436 else if(type.isVector())
3437 {
3438 switch(type.getNominalSize())
3439 {
3440 case 2: return GL_INT_VEC2;
3441 case 3: return GL_INT_VEC3;
3442 case 4: return GL_INT_VEC4;
3443 default: UNREACHABLE(type.getNominalSize());
3444 }
3445 }
3446 else UNREACHABLE(0);
3447 break;
3448 case EbtUInt:
3449 if(type.isScalar())
3450 {
3451 return GL_UNSIGNED_INT;
3452 }
3453 else if(type.isVector())
3454 {
3455 switch(type.getNominalSize())
3456 {
3457 case 2: return GL_UNSIGNED_INT_VEC2;
3458 case 3: return GL_UNSIGNED_INT_VEC3;
3459 case 4: return GL_UNSIGNED_INT_VEC4;
3460 default: UNREACHABLE(type.getNominalSize());
3461 }
3462 }
3463 else UNREACHABLE(0);
3464 break;
3465 case EbtBool:
3466 if(type.isScalar())
3467 {
3468 return GL_BOOL;
3469 }
3470 else if(type.isVector())
3471 {
3472 switch(type.getNominalSize())
3473 {
3474 case 2: return GL_BOOL_VEC2;
3475 case 3: return GL_BOOL_VEC3;
3476 case 4: return GL_BOOL_VEC4;
3477 default: UNREACHABLE(type.getNominalSize());
3478 }
3479 }
3480 else UNREACHABLE(0);
3481 break;
3482 case EbtSampler2D:
3483 return GL_SAMPLER_2D;
3484 case EbtISampler2D:
3485 return GL_INT_SAMPLER_2D;
3486 case EbtUSampler2D:
3487 return GL_UNSIGNED_INT_SAMPLER_2D;
3488 case EbtSamplerCube:
3489 return GL_SAMPLER_CUBE;
3490 case EbtISamplerCube:
3491 return GL_INT_SAMPLER_CUBE;
3492 case EbtUSamplerCube:
3493 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3494 case EbtSamplerExternalOES:
3495 return GL_SAMPLER_EXTERNAL_OES;
3496 case EbtSampler3D:
3497 return GL_SAMPLER_3D_OES;
3498 case EbtISampler3D:
3499 return GL_INT_SAMPLER_3D;
3500 case EbtUSampler3D:
3501 return GL_UNSIGNED_INT_SAMPLER_3D;
3502 case EbtSampler2DArray:
3503 return GL_SAMPLER_2D_ARRAY;
3504 case EbtISampler2DArray:
3505 return GL_INT_SAMPLER_2D_ARRAY;
3506 case EbtUSampler2DArray:
3507 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3508 case EbtSampler2DShadow:
3509 return GL_SAMPLER_2D_SHADOW;
3510 case EbtSamplerCubeShadow:
3511 return GL_SAMPLER_CUBE_SHADOW;
3512 case EbtSampler2DArrayShadow:
3513 return GL_SAMPLER_2D_ARRAY_SHADOW;
3514 default:
3515 UNREACHABLE(type.getBasicType());
3516 break;
3517 }
3518
3519 return GL_NONE;
3520 }
3521
3522 GLenum OutputASM::glVariablePrecision(const TType &type)
3523 {
3524 if(type.getBasicType() == EbtFloat)
3525 {
3526 switch(type.getPrecision())
3527 {
3528 case EbpHigh: return GL_HIGH_FLOAT;
3529 case EbpMedium: return GL_MEDIUM_FLOAT;
3530 case EbpLow: return GL_LOW_FLOAT;
3531 case EbpUndefined:
3532 // Should be defined as the default precision by the parser
3533 default: UNREACHABLE(type.getPrecision());
3534 }
3535 }
3536 else if(type.getBasicType() == EbtInt)
3537 {
3538 switch(type.getPrecision())
3539 {
3540 case EbpHigh: return GL_HIGH_INT;
3541 case EbpMedium: return GL_MEDIUM_INT;
3542 case EbpLow: return GL_LOW_INT;
3543 case EbpUndefined:
3544 // Should be defined as the default precision by the parser
3545 default: UNREACHABLE(type.getPrecision());
3546 }
3547 }
3548
3549 // Other types (boolean, sampler) don't have a precision
3550 return GL_NONE;
3551 }
3552
3553 int OutputASM::dim(TIntermNode *v)
3554 {
3555 TIntermTyped *vector = v->getAsTyped();
3556 ASSERT(vector && vector->isRegister());
3557 return vector->getNominalSize();
3558 }
3559
3560 int OutputASM::dim2(TIntermNode *m)
3561 {
3562 TIntermTyped *matrix = m->getAsTyped();
3563 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3564 return matrix->getSecondarySize();
3565 }
3566
3567 // Returns ~0u if no loop count could be determined
3568 unsigned int OutputASM::loopCount(TIntermLoop *node)
3569 {
3570 // Parse loops of the form:
3571 // for(int index = initial; index [comparator] limit; index += increment)
3572 TIntermSymbol *index = 0;
3573 TOperator comparator = EOpNull;
3574 int initial = 0;
3575 int limit = 0;
3576 int increment = 0;
3577
3578 // Parse index name and intial value
3579 if(node->getInit())
3580 {
3581 TIntermAggregate *init = node->getInit()->getAsAggregate();
3582
3583 if(init)
3584 {
3585 TIntermSequence &sequence = init->getSequence();
3586 TIntermTyped *variable = sequence[0]->getAsTyped();
3587
Nicolas Capense3f05552017-05-24 10:45:56 -04003588 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003589 {
3590 TIntermBinary *assign = variable->getAsBinaryNode();
3591
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003592 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003593 {
3594 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3595 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3596
3597 if(symbol && constant)
3598 {
3599 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3600 {
3601 index = symbol;
3602 initial = constant->getUnionArrayPointer()[0].getIConst();
3603 }
3604 }
3605 }
3606 }
3607 }
3608 }
3609
3610 // Parse comparator and limit value
3611 if(index && node->getCondition())
3612 {
3613 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003614 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003615
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003616 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003617 {
3618 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3619
3620 if(constant)
3621 {
3622 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3623 {
3624 comparator = test->getOp();
3625 limit = constant->getUnionArrayPointer()[0].getIConst();
3626 }
3627 }
3628 }
3629 }
3630
3631 // Parse increment
3632 if(index && comparator != EOpNull && node->getExpression())
3633 {
3634 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3635 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3636
3637 if(binaryTerminal)
3638 {
3639 TOperator op = binaryTerminal->getOp();
3640 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3641
3642 if(constant)
3643 {
3644 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3645 {
3646 int value = constant->getUnionArrayPointer()[0].getIConst();
3647
3648 switch(op)
3649 {
3650 case EOpAddAssign: increment = value; break;
3651 case EOpSubAssign: increment = -value; break;
3652 default: UNIMPLEMENTED();
3653 }
3654 }
3655 }
3656 }
3657 else if(unaryTerminal)
3658 {
3659 TOperator op = unaryTerminal->getOp();
3660
3661 switch(op)
3662 {
3663 case EOpPostIncrement: increment = 1; break;
3664 case EOpPostDecrement: increment = -1; break;
3665 case EOpPreIncrement: increment = 1; break;
3666 case EOpPreDecrement: increment = -1; break;
3667 default: UNIMPLEMENTED();
3668 }
3669 }
3670 }
3671
3672 if(index && comparator != EOpNull && increment != 0)
3673 {
3674 if(comparator == EOpLessThanEqual)
3675 {
3676 comparator = EOpLessThan;
3677 limit += 1;
3678 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003679 else if(comparator == EOpGreaterThanEqual)
3680 {
3681 comparator = EOpLessThan;
3682 limit -= 1;
3683 std::swap(initial, limit);
3684 increment = -increment;
3685 }
3686 else if(comparator == EOpGreaterThan)
3687 {
3688 comparator = EOpLessThan;
3689 std::swap(initial, limit);
3690 increment = -increment;
3691 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003692
3693 if(comparator == EOpLessThan)
3694 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003695 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003696 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003697 return 0;
3698 }
3699
3700 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3701
3702 if(iterations < 0)
3703 {
3704 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003705 }
3706
3707 return iterations;
3708 }
3709 else UNIMPLEMENTED(); // Falls through
3710 }
3711
3712 return ~0u;
3713 }
3714
3715 bool LoopUnrollable::traverse(TIntermNode *node)
3716 {
3717 loopDepth = 0;
3718 loopUnrollable = true;
3719
3720 node->traverse(this);
3721
3722 return loopUnrollable;
3723 }
3724
3725 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3726 {
3727 if(visit == PreVisit)
3728 {
3729 loopDepth++;
3730 }
3731 else if(visit == PostVisit)
3732 {
3733 loopDepth++;
3734 }
3735
3736 return true;
3737 }
3738
3739 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3740 {
3741 if(!loopUnrollable)
3742 {
3743 return false;
3744 }
3745
3746 if(!loopDepth)
3747 {
3748 return true;
3749 }
3750
3751 switch(node->getFlowOp())
3752 {
3753 case EOpKill:
3754 case EOpReturn:
3755 break;
3756 case EOpBreak:
3757 case EOpContinue:
3758 loopUnrollable = false;
3759 break;
3760 default: UNREACHABLE(node->getFlowOp());
3761 }
3762
3763 return loopUnrollable;
3764 }
3765
3766 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3767 {
3768 return loopUnrollable;
3769 }
3770}