blob: 0d45f81ae9dc4723608820a16780ff8e7d857fbe [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;
Nicolas Capens0530b452017-11-15 16:39:47 -0500617 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400618
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 {
Nicolas Capens0530b452017-11-15 16:39:47 -05001890 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001891 }
1892
Alexis Hetu929c6b02017-11-07 16:04:25 -05001893 if(src0)
1894 {
1895 TIntermTyped* src = src0->getAsTyped();
1896 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
1897 }
1898
Nicolas Capens0530b452017-11-15 16:39:47 -05001899 source(instruction->src[0], src0, index0);
1900 source(instruction->src[1], src1, index1);
1901 source(instruction->src[2], src2, index2);
1902 source(instruction->src[3], src3, index3);
1903 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001904
1905 shader->append(instruction);
1906
1907 return instruction;
1908 }
1909
1910 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
1911 {
1912 return emitCast(dst, 0, src, 0);
1913 }
1914
1915 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
1916 {
1917 switch(src->getBasicType())
1918 {
1919 case EbtBool:
1920 switch(dst->getBasicType())
1921 {
1922 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1923 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1924 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
1925 default: break;
1926 }
1927 break;
1928 case EbtInt:
1929 switch(dst->getBasicType())
1930 {
1931 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1932 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
1933 default: break;
1934 }
1935 break;
1936 case EbtUInt:
1937 switch(dst->getBasicType())
1938 {
1939 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1940 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
1941 default: break;
1942 }
1943 break;
1944 case EbtFloat:
1945 switch(dst->getBasicType())
1946 {
1947 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
1948 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
1949 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
1950 default: break;
1951 }
1952 break;
1953 default:
1954 break;
1955 }
1956
1957 ASSERT((src->getBasicType() == dst->getBasicType()) ||
1958 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
1959 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
1960
1961 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
1962 }
1963
1964 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1965 {
1966 for(int index = 0; index < dst->elementRegisterCount(); index++)
1967 {
1968 emit(op, dst, index, src0, index, src1, index, src2, index);
1969 }
1970 }
1971
1972 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1973 {
1974 emitBinary(op, result, src0, src1);
1975 assignLvalue(lhs, result);
1976 }
1977
1978 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1979 {
1980 sw::Shader::Opcode opcode;
1981 switch(left->getAsTyped()->getBasicType())
1982 {
1983 case EbtBool:
1984 case EbtInt:
1985 opcode = sw::Shader::OPCODE_ICMP;
1986 break;
1987 case EbtUInt:
1988 opcode = sw::Shader::OPCODE_UCMP;
1989 break;
1990 default:
1991 opcode = sw::Shader::OPCODE_CMP;
1992 break;
1993 }
1994
1995 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
1996 cmp->control = cmpOp;
1997 }
1998
1999 int componentCount(const TType &type, int registers)
2000 {
2001 if(registers == 0)
2002 {
2003 return 0;
2004 }
2005
2006 if(type.isArray() && registers >= type.elementRegisterCount())
2007 {
2008 int index = registers / type.elementRegisterCount();
2009 registers -= index * type.elementRegisterCount();
2010 return index * type.getElementSize() + componentCount(type, registers);
2011 }
2012
2013 if(type.isStruct() || type.isInterfaceBlock())
2014 {
2015 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2016 int elements = 0;
2017
2018 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2019 {
2020 const TType &fieldType = *((*field)->type());
2021
2022 if(fieldType.totalRegisterCount() <= registers)
2023 {
2024 registers -= fieldType.totalRegisterCount();
2025 elements += fieldType.getObjectSize();
2026 }
2027 else // Register within this field
2028 {
2029 return elements + componentCount(fieldType, registers);
2030 }
2031 }
2032 }
2033 else if(type.isMatrix())
2034 {
2035 return registers * type.registerSize();
2036 }
2037
2038 UNREACHABLE(0);
2039 return 0;
2040 }
2041
2042 int registerSize(const TType &type, int registers)
2043 {
2044 if(registers == 0)
2045 {
2046 if(type.isStruct())
2047 {
2048 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2049 }
2050 else if(type.isInterfaceBlock())
2051 {
2052 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2053 }
2054
2055 return type.registerSize();
2056 }
2057
2058 if(type.isArray() && registers >= type.elementRegisterCount())
2059 {
2060 int index = registers / type.elementRegisterCount();
2061 registers -= index * type.elementRegisterCount();
2062 return registerSize(type, registers);
2063 }
2064
2065 if(type.isStruct() || type.isInterfaceBlock())
2066 {
2067 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2068 int elements = 0;
2069
2070 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2071 {
2072 const TType &fieldType = *((*field)->type());
2073
2074 if(fieldType.totalRegisterCount() <= registers)
2075 {
2076 registers -= fieldType.totalRegisterCount();
2077 elements += fieldType.getObjectSize();
2078 }
2079 else // Register within this field
2080 {
2081 return registerSize(fieldType, registers);
2082 }
2083 }
2084 }
2085 else if(type.isMatrix())
2086 {
2087 return registerSize(type, 0);
2088 }
2089
2090 UNREACHABLE(0);
2091 return 0;
2092 }
2093
2094 int OutputASM::getBlockId(TIntermTyped *arg)
2095 {
2096 if(arg)
2097 {
2098 const TType &type = arg->getType();
2099 TInterfaceBlock* block = type.getInterfaceBlock();
2100 if(block && (type.getQualifier() == EvqUniform))
2101 {
2102 // Make sure the uniform block is declared
2103 uniformRegister(arg);
2104
2105 const char* blockName = block->name().c_str();
2106
2107 // Fetch uniform block index from array of blocks
2108 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2109 {
2110 if(blockName == it->name)
2111 {
2112 return it->blockId;
2113 }
2114 }
2115
2116 ASSERT(false);
2117 }
2118 }
2119
2120 return -1;
2121 }
2122
2123 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2124 {
2125 const TType &type = arg->getType();
2126 int blockId = getBlockId(arg);
2127 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2128 if(blockId != -1)
2129 {
2130 argumentInfo.bufferIndex = 0;
2131 for(int i = 0; i < blockId; ++i)
2132 {
2133 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2134 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2135 }
2136
2137 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2138
2139 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2140 BlockDefinitionIndexMap::const_iterator it = itEnd;
2141
2142 argumentInfo.clampedIndex = index;
2143 if(type.isInterfaceBlock())
2144 {
2145 // Offset index to the beginning of the selected instance
2146 int blockRegisters = type.elementRegisterCount();
2147 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2148 argumentInfo.bufferIndex += bufferOffset;
2149 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2150 }
2151
2152 int regIndex = registerIndex(arg);
2153 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2154 {
2155 it = blockDefinition.find(i);
2156 if(it != itEnd)
2157 {
2158 argumentInfo.clampedIndex -= (i - regIndex);
2159 break;
2160 }
2161 }
2162 ASSERT(it != itEnd);
2163
2164 argumentInfo.typedMemberInfo = it->second;
2165
2166 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2167 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2168 }
2169 else
2170 {
2171 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2172 }
2173
2174 return argumentInfo;
2175 }
2176
Nicolas Capens0530b452017-11-15 16:39:47 -05002177 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002178 {
2179 if(argument)
2180 {
2181 TIntermTyped *arg = argument->getAsTyped();
2182 Temporary unpackedUniform(this);
2183
2184 const TType& srcType = arg->getType();
2185 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2186 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2187 {
2188 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2189 const TType &memberType = argumentInfo.typedMemberInfo.type;
2190
2191 if(memberType.getBasicType() == EbtBool)
2192 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002193 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002194
2195 // Convert the packed bool, which is currently an int, to a true bool
2196 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2197 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2198 instruction->dst.index = registerIndex(&unpackedUniform);
2199 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2200 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2201 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2202
2203 shader->append(instruction);
2204
2205 arg = &unpackedUniform;
2206 index = 0;
2207 }
2208 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2209 {
2210 int numCols = memberType.getNominalSize();
2211 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002212
Alexis Hetue97a31e2016-11-14 14:10:47 -05002213 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002214
2215 unsigned int dstIndex = registerIndex(&unpackedUniform);
2216 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2217 int arrayIndex = argumentInfo.clampedIndex / numCols;
2218 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2219
2220 for(int j = 0; j < numRows; ++j)
2221 {
2222 // Transpose the row major matrix
2223 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2224 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2225 instruction->dst.index = dstIndex;
2226 instruction->dst.mask = 1 << j;
2227 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2228 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2229 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2230 instruction->src[0].swizzle = srcSwizzle;
2231
2232 shader->append(instruction);
2233 }
2234
2235 arg = &unpackedUniform;
2236 index = 0;
2237 }
2238 }
2239
2240 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2241 const TType &type = argumentInfo.typedMemberInfo.type;
2242
2243 int size = registerSize(type, argumentInfo.clampedIndex);
2244
2245 parameter.type = registerType(arg);
2246 parameter.bufferIndex = argumentInfo.bufferIndex;
2247
2248 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2249 {
2250 int component = componentCount(type, argumentInfo.clampedIndex);
2251 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2252
2253 for(int i = 0; i < 4; i++)
2254 {
2255 if(size == 1) // Replicate
2256 {
2257 parameter.value[i] = constants[component + 0].getAsFloat();
2258 }
2259 else if(i < size)
2260 {
2261 parameter.value[i] = constants[component + i].getAsFloat();
2262 }
2263 else
2264 {
2265 parameter.value[i] = 0.0f;
2266 }
2267 }
2268 }
2269 else
2270 {
2271 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2272
2273 if(parameter.bufferIndex != -1)
2274 {
2275 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2276 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2277 }
2278 }
2279
2280 if(!IsSampler(arg->getBasicType()))
2281 {
2282 parameter.swizzle = readSwizzle(arg, size);
2283 }
2284 }
2285 }
2286
Nicolas Capens0530b452017-11-15 16:39:47 -05002287 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2288 {
2289 parameter.type = registerType(arg);
2290 parameter.index = registerIndex(arg) + index;
2291 parameter.mask = writeMask(arg);
2292 }
2293
Nicolas Capens0bac2852016-05-07 06:09:58 -04002294 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2295 {
2296 for(int index = 0; index < dst->totalRegisterCount(); index++)
2297 {
2298 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
2299 mov->dst.mask = writeMask(dst, index);
2300 }
2301 }
2302
2303 int swizzleElement(int swizzle, int index)
2304 {
2305 return (swizzle >> (index * 2)) & 0x03;
2306 }
2307
2308 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2309 {
2310 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2311 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2312 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2313 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2314 }
2315
2316 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2317 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002318 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2319 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002320 {
2321 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2322 }
2323
2324 TIntermBinary *binary = dst->getAsBinaryNode();
2325
2326 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2327 {
2328 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2329
Nicolas Capens6986b282017-11-16 10:38:19 -05002330 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002331
2332 insert->src[0].type = insert->dst.type;
2333 insert->src[0].index = insert->dst.index;
2334 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002335 source(insert->src[1], src);
2336 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002337
2338 shader->append(insert);
2339 }
2340 else
2341 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002342 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2343
Nicolas Capens6986b282017-11-16 10:38:19 -05002344 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002345
Nicolas Capens0530b452017-11-15 16:39:47 -05002346 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002347 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2348
2349 shader->append(mov1);
2350
2351 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002352 {
2353 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2354
Nicolas Capens84249fd2017-11-09 11:20:51 -05002355 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002356 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002357 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002358
Nicolas Capens0530b452017-11-15 16:39:47 -05002359 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002360
2361 shader->append(mov);
2362 }
2363 }
2364 }
2365
Nicolas Capens6986b282017-11-16 10:38:19 -05002366 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002367 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002368 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002369 TIntermTyped *root = nullptr;
2370 unsigned int offset = 0;
2371 unsigned char mask = 0xF;
2372 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2373
2374 dst.type = registerType(root);
2375 dst.index = registerIndex(root) + offset;
2376 dst.mask = mask;
2377
2378 return swizzle;
2379 }
2380
2381 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2382 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002383 TIntermTyped *result = node;
2384 TIntermBinary *binary = node->getAsBinaryNode();
2385 TIntermSymbol *symbol = node->getAsSymbolNode();
2386
2387 if(binary)
2388 {
2389 TIntermTyped *left = binary->getLeft();
2390 TIntermTyped *right = binary->getRight();
2391
Nicolas Capens0530b452017-11-15 16:39:47 -05002392 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002393
2394 switch(binary->getOp())
2395 {
2396 case EOpIndexDirect:
2397 {
2398 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2399
2400 if(left->isRegister())
2401 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002402 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002403
Nicolas Capens0530b452017-11-15 16:39:47 -05002404 mask = 1;
2405 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002406 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002407 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002408 }
2409
2410 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002411 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002412
2413 return element;
2414 }
2415 else if(left->isArray() || left->isMatrix())
2416 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002417 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002418 return 0xE4;
2419 }
2420 else UNREACHABLE(0);
2421 }
2422 break;
2423 case EOpIndexIndirect:
2424 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002425 right->traverse(this);
2426
Nicolas Capens0bac2852016-05-07 06:09:58 -04002427 if(left->isRegister())
2428 {
2429 // Requires INSERT instruction (handled by calling function)
2430 }
2431 else if(left->isArray() || left->isMatrix())
2432 {
2433 int scale = result->totalRegisterCount();
2434
Nicolas Capens0530b452017-11-15 16:39:47 -05002435 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002436 {
2437 if(left->totalRegisterCount() > 1)
2438 {
2439 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002440 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002441
Nicolas Capens0530b452017-11-15 16:39:47 -05002442 rel.index = relativeRegister.index;
2443 rel.type = relativeRegister.type;
2444 rel.scale = scale;
2445 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002446 }
2447 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002448 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002449 {
2450 if(scale == 1)
2451 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002452 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002453 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002454 mad->src[0].index = rel.index;
2455 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002456 }
2457 else
2458 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002459 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002460 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002461 mul->src[0].index = rel.index;
2462 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002463
2464 Constant newScale(scale);
2465 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2466 }
2467
Nicolas Capens0530b452017-11-15 16:39:47 -05002468 rel.type = sw::Shader::PARAMETER_TEMP;
2469 rel.index = registerIndex(&address);
2470 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002471 }
2472 else // Just add the new index to the address register
2473 {
2474 if(scale == 1)
2475 {
2476 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2477 }
2478 else
2479 {
2480 Constant newScale(scale);
2481 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2482 }
2483 }
2484 }
2485 else UNREACHABLE(0);
2486 }
2487 break;
2488 case EOpIndexDirectStruct:
2489 case EOpIndexDirectInterfaceBlock:
2490 {
2491 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2492 left->getType().getStruct()->fields() :
2493 left->getType().getInterfaceBlock()->fields();
2494 int index = right->getAsConstantUnion()->getIConst(0);
2495 int fieldOffset = 0;
2496
2497 for(int i = 0; i < index; i++)
2498 {
2499 fieldOffset += fields[i]->type()->totalRegisterCount();
2500 }
2501
Nicolas Capens0530b452017-11-15 16:39:47 -05002502 offset += fieldOffset;
2503 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002504
2505 return 0xE4;
2506 }
2507 break;
2508 case EOpVectorSwizzle:
2509 {
2510 ASSERT(left->isRegister());
2511
Nicolas Capens0530b452017-11-15 16:39:47 -05002512 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002513
2514 int swizzle = 0;
2515 int rightMask = 0;
2516
2517 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2518
2519 for(unsigned int i = 0; i < sequence.size(); i++)
2520 {
2521 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2522
2523 int element = swizzleElement(leftSwizzle, index);
2524 rightMask = rightMask | (1 << element);
2525 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2526 }
2527
Nicolas Capens0530b452017-11-15 16:39:47 -05002528 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002529
2530 return swizzle;
2531 }
2532 break;
2533 default:
2534 UNREACHABLE(binary->getOp()); // Not an l-value operator
2535 break;
2536 }
2537 }
2538 else if(symbol)
2539 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002540 root = symbol;
2541 offset = 0;
2542 mask = writeMask(symbol);
2543
2544 return 0xE4;
2545 }
2546 else
2547 {
2548 node->traverse(this);
2549
2550 root = node;
2551 offset = 0;
2552 mask = writeMask(node);
2553
Nicolas Capens0bac2852016-05-07 06:09:58 -04002554 return 0xE4;
2555 }
2556
2557 return 0xE4;
2558 }
2559
2560 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2561 {
2562 if(isSamplerRegister(operand))
2563 {
2564 return sw::Shader::PARAMETER_SAMPLER;
2565 }
2566
2567 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002568 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002569 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002570 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2571 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002572 {
2573 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2574 }
2575 outputQualifier = qualifier;
2576 }
2577
2578 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2579 {
2580 return sw::Shader::PARAMETER_TEMP;
2581 }
2582
2583 switch(qualifier)
2584 {
2585 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2586 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2587 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2588 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2589 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2590 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2591 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2592 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2593 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2594 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2595 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2596 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2597 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2598 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2599 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2600 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2601 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2602 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2603 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2604 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2605 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2606 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2607 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2608 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2609 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2610 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002611 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002612 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2613 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2614 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2615 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2616 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2617 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2618 default: UNREACHABLE(qualifier);
2619 }
2620
2621 return sw::Shader::PARAMETER_VOID;
2622 }
2623
Alexis Hetu12b00502016-05-20 13:01:11 -04002624 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2625 {
2626 const TQualifier qualifier = operand->getQualifier();
2627 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2628 }
2629
Nicolas Capens0bac2852016-05-07 06:09:58 -04002630 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2631 {
2632 if(isSamplerRegister(operand))
2633 {
2634 return samplerRegister(operand);
2635 }
2636
2637 switch(operand->getQualifier())
2638 {
2639 case EvqTemporary: return temporaryRegister(operand);
2640 case EvqGlobal: return temporaryRegister(operand);
2641 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2642 case EvqAttribute: return attributeRegister(operand);
2643 case EvqVaryingIn: return varyingRegister(operand);
2644 case EvqVaryingOut: return varyingRegister(operand);
2645 case EvqVertexIn: return attributeRegister(operand);
2646 case EvqFragmentOut: return fragmentOutputRegister(operand);
2647 case EvqVertexOut: return varyingRegister(operand);
2648 case EvqFragmentIn: return varyingRegister(operand);
2649 case EvqInvariantVaryingIn: return varyingRegister(operand);
2650 case EvqInvariantVaryingOut: return varyingRegister(operand);
2651 case EvqSmooth: return varyingRegister(operand);
2652 case EvqFlat: return varyingRegister(operand);
2653 case EvqCentroidOut: return varyingRegister(operand);
2654 case EvqSmoothIn: return varyingRegister(operand);
2655 case EvqFlatIn: return varyingRegister(operand);
2656 case EvqCentroidIn: return varyingRegister(operand);
2657 case EvqUniform: return uniformRegister(operand);
2658 case EvqIn: return temporaryRegister(operand);
2659 case EvqOut: return temporaryRegister(operand);
2660 case EvqInOut: return temporaryRegister(operand);
2661 case EvqConstReadOnly: return temporaryRegister(operand);
2662 case EvqPosition: return varyingRegister(operand);
2663 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002664 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2665 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2666 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2667 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002668 case EvqPointCoord: return varyingRegister(operand);
2669 case EvqFragColor: return 0;
2670 case EvqFragData: return fragmentOutputRegister(operand);
2671 case EvqFragDepth: return 0;
2672 default: UNREACHABLE(operand->getQualifier());
2673 }
2674
2675 return 0;
2676 }
2677
2678 int OutputASM::writeMask(TIntermTyped *destination, int index)
2679 {
2680 if(destination->getQualifier() == EvqPointSize)
2681 {
2682 return 0x2; // Point size stored in the y component
2683 }
2684
2685 return 0xF >> (4 - registerSize(destination->getType(), index));
2686 }
2687
2688 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2689 {
2690 if(argument->getQualifier() == EvqPointSize)
2691 {
2692 return 0x55; // Point size stored in the y component
2693 }
2694
2695 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2696
2697 return swizzleSize[size];
2698 }
2699
2700 // Conservatively checks whether an expression is fast to compute and has no side effects
2701 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2702 {
2703 if(!expression->isRegister())
2704 {
2705 return false;
2706 }
2707
2708 return cost(expression, budget) >= 0;
2709 }
2710
2711 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2712 int OutputASM::cost(TIntermNode *expression, int budget)
2713 {
2714 if(budget < 0)
2715 {
2716 return budget;
2717 }
2718
2719 if(expression->getAsSymbolNode())
2720 {
2721 return budget;
2722 }
2723 else if(expression->getAsConstantUnion())
2724 {
2725 return budget;
2726 }
2727 else if(expression->getAsBinaryNode())
2728 {
2729 TIntermBinary *binary = expression->getAsBinaryNode();
2730
2731 switch(binary->getOp())
2732 {
2733 case EOpVectorSwizzle:
2734 case EOpIndexDirect:
2735 case EOpIndexDirectStruct:
2736 case EOpIndexDirectInterfaceBlock:
2737 return cost(binary->getLeft(), budget - 0);
2738 case EOpAdd:
2739 case EOpSub:
2740 case EOpMul:
2741 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2742 default:
2743 return -1;
2744 }
2745 }
2746 else if(expression->getAsUnaryNode())
2747 {
2748 TIntermUnary *unary = expression->getAsUnaryNode();
2749
2750 switch(unary->getOp())
2751 {
2752 case EOpAbs:
2753 case EOpNegative:
2754 return cost(unary->getOperand(), budget - 1);
2755 default:
2756 return -1;
2757 }
2758 }
2759 else if(expression->getAsSelectionNode())
2760 {
2761 TIntermSelection *selection = expression->getAsSelectionNode();
2762
2763 if(selection->usesTernaryOperator())
2764 {
2765 TIntermTyped *condition = selection->getCondition();
2766 TIntermNode *trueBlock = selection->getTrueBlock();
2767 TIntermNode *falseBlock = selection->getFalseBlock();
2768 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2769
2770 if(constantCondition)
2771 {
2772 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2773
2774 if(trueCondition)
2775 {
2776 return cost(trueBlock, budget - 0);
2777 }
2778 else
2779 {
2780 return cost(falseBlock, budget - 0);
2781 }
2782 }
2783 else
2784 {
2785 return cost(trueBlock, cost(falseBlock, budget - 2));
2786 }
2787 }
2788 }
2789
2790 return -1;
2791 }
2792
2793 const Function *OutputASM::findFunction(const TString &name)
2794 {
2795 for(unsigned int f = 0; f < functionArray.size(); f++)
2796 {
2797 if(functionArray[f].name == name)
2798 {
2799 return &functionArray[f];
2800 }
2801 }
2802
2803 return 0;
2804 }
2805
2806 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2807 {
2808 return allocate(temporaries, temporary);
2809 }
2810
Alexis Hetu49351232017-11-02 16:00:32 -04002811 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
2812 {
2813 if(type.isStruct())
2814 {
2815 const TFieldList &fields = type.getStruct()->fields();
2816 int fieldVar = var;
2817 for(size_t i = 0; i < fields.size(); i++)
2818 {
2819 const TType& fieldType = *(fields[i]->type());
2820 setPixelShaderInputs(fieldType, fieldVar, flat);
2821 fieldVar += fieldType.totalRegisterCount();
2822 }
2823 }
2824 else
2825 {
2826 for(int i = 0; i < type.totalRegisterCount(); i++)
2827 {
2828 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2829 }
2830 }
2831 }
2832
Nicolas Capens0bac2852016-05-07 06:09:58 -04002833 int OutputASM::varyingRegister(TIntermTyped *varying)
2834 {
2835 int var = lookup(varyings, varying);
2836
2837 if(var == -1)
2838 {
2839 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002840 int registerCount = varying->totalRegisterCount();
2841
2842 if(pixelShader)
2843 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04002844 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002845 {
2846 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2847 return 0;
2848 }
2849
2850 if(varying->getQualifier() == EvqPointCoord)
2851 {
2852 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04002853 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002854 }
2855 else
2856 {
Alexis Hetu49351232017-11-02 16:00:32 -04002857 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002858 }
2859 }
2860 else if(vertexShader)
2861 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04002862 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002863 {
2864 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2865 return 0;
2866 }
2867
2868 if(varying->getQualifier() == EvqPosition)
2869 {
2870 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002871 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002872 }
2873 else if(varying->getQualifier() == EvqPointSize)
2874 {
2875 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002876 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002877 }
2878 else
2879 {
2880 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2881 }
2882 }
2883 else UNREACHABLE(0);
2884
2885 declareVarying(varying, var);
2886 }
2887
2888 return var;
2889 }
2890
2891 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2892 {
2893 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
2894 {
Alexis Hetu49351232017-11-02 16:00:32 -04002895 TIntermSymbol *symbol = varying->getAsSymbolNode();
2896 declareVarying(varying->getType(), symbol->getSymbol(), reg);
2897 }
2898 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002899
Alexis Hetu49351232017-11-02 16:00:32 -04002900 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
2901 {
2902 const char *name = varyingName.c_str();
2903 VaryingList &activeVaryings = shaderObject->varyings;
2904
2905 TStructure* structure = type.getStruct();
2906 if(structure)
2907 {
2908 int fieldRegisterIndex = registerIndex;
2909
2910 const TFieldList &fields = type.getStruct()->fields();
2911 for(size_t i = 0; i < fields.size(); i++)
2912 {
2913 const TType& fieldType = *(fields[i]->type());
2914 declareVarying(fieldType, varyingName + "." + fields[i]->name(), fieldRegisterIndex);
2915 if(fieldRegisterIndex >= 0)
2916 {
2917 fieldRegisterIndex += fieldType.totalRegisterCount();
2918 }
2919 }
2920 }
2921 else
2922 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002923 // Check if this varying has been declared before without having a register assigned
2924 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2925 {
2926 if(v->name == name)
2927 {
Alexis Hetu49351232017-11-02 16:00:32 -04002928 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002929 {
Alexis Hetu49351232017-11-02 16:00:32 -04002930 ASSERT(v->reg < 0 || v->reg == registerIndex);
2931 v->reg = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002932 }
2933
2934 return;
2935 }
2936 }
2937
Alexis Hetu49351232017-11-02 16:00:32 -04002938 activeVaryings.push_back(glsl::Varying(glVariableType(type), name, type.getArraySize(), registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002939 }
2940 }
2941
2942 int OutputASM::uniformRegister(TIntermTyped *uniform)
2943 {
2944 const TType &type = uniform->getType();
2945 ASSERT(!IsSampler(type.getBasicType()));
2946 TInterfaceBlock *block = type.getAsInterfaceBlock();
2947 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2948 ASSERT(symbol || block);
2949
2950 if(symbol || block)
2951 {
2952 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2953 bool isBlockMember = (!block && parentBlock);
2954 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2955
2956 if(index == -1 || isBlockMember)
2957 {
2958 if(index == -1)
2959 {
2960 index = allocate(uniforms, uniform);
2961 }
2962
2963 // Verify if the current uniform is a member of an already declared block
2964 const TString &name = symbol ? symbol->getSymbol() : block->name();
2965 int blockMemberIndex = blockMemberLookup(type, name, index);
2966 if(blockMemberIndex == -1)
2967 {
2968 declareUniform(type, name, index);
2969 }
2970 else
2971 {
2972 index = blockMemberIndex;
2973 }
2974 }
2975
2976 return index;
2977 }
2978
2979 return 0;
2980 }
2981
2982 int OutputASM::attributeRegister(TIntermTyped *attribute)
2983 {
2984 ASSERT(!attribute->isArray());
2985
2986 int index = lookup(attributes, attribute);
2987
2988 if(index == -1)
2989 {
2990 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2991 ASSERT(symbol);
2992
2993 if(symbol)
2994 {
2995 index = allocate(attributes, attribute);
2996 const TType &type = attribute->getType();
2997 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04002998 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2999 switch(type.getBasicType())
3000 {
3001 case EbtInt:
3002 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3003 break;
3004 case EbtUInt:
3005 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3006 break;
3007 case EbtFloat:
3008 default:
3009 break;
3010 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003011
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003012 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003013 {
3014 for(int i = 0; i < registerCount; i++)
3015 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003016 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003017 }
3018 }
3019
3020 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3021
3022 const char *name = symbol->getSymbol().c_str();
3023 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3024 }
3025 }
3026
3027 return index;
3028 }
3029
3030 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3031 {
3032 return allocate(fragmentOutputs, fragmentOutput);
3033 }
3034
3035 int OutputASM::samplerRegister(TIntermTyped *sampler)
3036 {
3037 const TType &type = sampler->getType();
3038 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3039
3040 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3041 TIntermBinary *binary = sampler->getAsBinaryNode();
3042
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003043 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003044 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003045 switch(type.getQualifier())
3046 {
3047 case EvqUniform:
3048 return samplerRegister(symbol);
3049 case EvqIn:
3050 case EvqConstReadOnly:
3051 // Function arguments are not (uniform) sampler registers
3052 return -1;
3053 default:
3054 UNREACHABLE(type.getQualifier());
3055 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003056 }
3057 else if(binary)
3058 {
3059 TIntermTyped *left = binary->getLeft();
3060 TIntermTyped *right = binary->getRight();
3061 const TType &leftType = left->getType();
3062 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3063 int offset = 0;
3064
3065 switch(binary->getOp())
3066 {
3067 case EOpIndexDirect:
3068 ASSERT(left->isArray());
3069 offset = index * leftType.elementRegisterCount();
3070 break;
3071 case EOpIndexDirectStruct:
3072 ASSERT(leftType.isStruct());
3073 {
3074 const TFieldList &fields = leftType.getStruct()->fields();
3075
3076 for(int i = 0; i < index; i++)
3077 {
3078 offset += fields[i]->type()->totalRegisterCount();
3079 }
3080 }
3081 break;
3082 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3083 return -1;
3084 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3085 default:
3086 UNREACHABLE(binary->getOp());
3087 return -1;
3088 }
3089
3090 int base = samplerRegister(left);
3091
3092 if(base < 0)
3093 {
3094 return -1;
3095 }
3096
3097 return base + offset;
3098 }
3099
3100 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003101 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003102 }
3103
3104 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3105 {
3106 const TType &type = sampler->getType();
3107 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3108
3109 int index = lookup(samplers, sampler);
3110
3111 if(index == -1)
3112 {
3113 index = allocate(samplers, sampler);
3114
3115 if(sampler->getQualifier() == EvqUniform)
3116 {
3117 const char *name = sampler->getSymbol().c_str();
3118 declareUniform(type, name, index);
3119 }
3120 }
3121
3122 return index;
3123 }
3124
3125 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3126 {
3127 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3128 }
3129
3130 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3131 {
3132 for(unsigned int i = 0; i < list.size(); i++)
3133 {
3134 if(list[i] == variable)
3135 {
3136 return i; // Pointer match
3137 }
3138 }
3139
3140 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3141 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3142
3143 if(varBlock)
3144 {
3145 for(unsigned int i = 0; i < list.size(); i++)
3146 {
3147 if(list[i])
3148 {
3149 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3150
3151 if(listBlock)
3152 {
3153 if(listBlock->name() == varBlock->name())
3154 {
3155 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3156 ASSERT(listBlock->fields() == varBlock->fields());
3157 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3158 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3159
3160 return i;
3161 }
3162 }
3163 }
3164 }
3165 }
3166 else if(varSymbol)
3167 {
3168 for(unsigned int i = 0; i < list.size(); i++)
3169 {
3170 if(list[i])
3171 {
3172 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3173
3174 if(listSymbol)
3175 {
3176 if(listSymbol->getId() == varSymbol->getId())
3177 {
3178 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3179 ASSERT(listSymbol->getType() == varSymbol->getType());
3180 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3181
3182 return i;
3183 }
3184 }
3185 }
3186 }
3187 }
3188
3189 return -1;
3190 }
3191
3192 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3193 {
3194 for(unsigned int i = 0; i < list.size(); i++)
3195 {
3196 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3197 {
3198 return i; // Pointer match
3199 }
3200 }
3201 return -1;
3202 }
3203
3204 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
3205 {
3206 int index = lookup(list, variable);
3207
3208 if(index == -1)
3209 {
3210 unsigned int registerCount = variable->blockRegisterCount();
3211
3212 for(unsigned int i = 0; i < list.size(); i++)
3213 {
3214 if(list[i] == 0)
3215 {
3216 unsigned int j = 1;
3217 for( ; j < registerCount && (i + j) < list.size(); j++)
3218 {
3219 if(list[i + j] != 0)
3220 {
3221 break;
3222 }
3223 }
3224
3225 if(j == registerCount) // Found free slots
3226 {
3227 for(unsigned int j = 0; j < registerCount; j++)
3228 {
3229 list[i + j] = variable;
3230 }
3231
3232 return i;
3233 }
3234 }
3235 }
3236
3237 index = list.size();
3238
3239 for(unsigned int i = 0; i < registerCount; i++)
3240 {
3241 list.push_back(variable);
3242 }
3243 }
3244
3245 return index;
3246 }
3247
3248 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3249 {
3250 int index = lookup(list, variable);
3251
3252 if(index >= 0)
3253 {
3254 list[index] = 0;
3255 }
3256 }
3257
3258 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3259 {
3260 const TInterfaceBlock *block = type.getInterfaceBlock();
3261
3262 if(block)
3263 {
3264 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3265 const TFieldList& fields = block->fields();
3266 const TString &blockName = block->name();
3267 int fieldRegisterIndex = registerIndex;
3268
3269 if(!type.isInterfaceBlock())
3270 {
3271 // This is a uniform that's part of a block, let's see if the block is already defined
3272 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3273 {
3274 if(activeUniformBlocks[i].name == blockName.c_str())
3275 {
3276 // The block is already defined, find the register for the current uniform and return it
3277 for(size_t j = 0; j < fields.size(); j++)
3278 {
3279 const TString &fieldName = fields[j]->name();
3280 if(fieldName == name)
3281 {
3282 return fieldRegisterIndex;
3283 }
3284
3285 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3286 }
3287
3288 ASSERT(false);
3289 return fieldRegisterIndex;
3290 }
3291 }
3292 }
3293 }
3294
3295 return -1;
3296 }
3297
3298 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int blockId, BlockLayoutEncoder* encoder)
3299 {
3300 const TStructure *structure = type.getStruct();
3301 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3302
3303 if(!structure && !block)
3304 {
3305 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3306 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3307 if(blockId >= 0)
3308 {
3309 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3310 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3311 }
3312 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3313 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3314 fieldRegisterIndex, blockId, blockInfo));
3315 if(IsSampler(type.getBasicType()))
3316 {
3317 for(int i = 0; i < type.totalRegisterCount(); i++)
3318 {
3319 shader->declareSampler(fieldRegisterIndex + i);
3320 }
3321 }
3322 }
3323 else if(block)
3324 {
3325 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3326 const TFieldList& fields = block->fields();
3327 const TString &blockName = block->name();
3328 int fieldRegisterIndex = registerIndex;
3329 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3330
3331 blockId = activeUniformBlocks.size();
3332 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3333 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3334 block->blockStorage(), isRowMajor, registerIndex, blockId));
3335 blockDefinitions.push_back(BlockDefinitionIndexMap());
3336
3337 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3338 currentBlockEncoder.enterAggregateType();
3339 for(size_t i = 0; i < fields.size(); i++)
3340 {
3341 const TType &fieldType = *(fields[i]->type());
3342 const TString &fieldName = fields[i]->name();
3343 if(isUniformBlockMember && (fieldName == name))
3344 {
3345 registerIndex = fieldRegisterIndex;
3346 }
3347
3348 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3349
3350 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, &currentBlockEncoder);
3351 fieldRegisterIndex += fieldType.totalRegisterCount();
3352 }
3353 currentBlockEncoder.exitAggregateType();
3354 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3355 }
3356 else
3357 {
3358 int fieldRegisterIndex = registerIndex;
3359
3360 const TFieldList& fields = structure->fields();
3361 if(type.isArray() && (structure || type.isInterfaceBlock()))
3362 {
3363 for(int i = 0; i < type.getArraySize(); i++)
3364 {
3365 if(encoder)
3366 {
3367 encoder->enterAggregateType();
3368 }
3369 for(size_t j = 0; j < fields.size(); j++)
3370 {
3371 const TType &fieldType = *(fields[j]->type());
3372 const TString &fieldName = fields[j]->name();
3373 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3374
3375 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3376 fieldRegisterIndex += fieldType.totalRegisterCount();
3377 }
3378 if(encoder)
3379 {
3380 encoder->exitAggregateType();
3381 }
3382 }
3383 }
3384 else
3385 {
3386 if(encoder)
3387 {
3388 encoder->enterAggregateType();
3389 }
3390 for(size_t i = 0; i < fields.size(); i++)
3391 {
3392 const TType &fieldType = *(fields[i]->type());
3393 const TString &fieldName = fields[i]->name();
3394 const TString uniformName = name + "." + fieldName;
3395
3396 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3397 fieldRegisterIndex += fieldType.totalRegisterCount();
3398 }
3399 if(encoder)
3400 {
3401 encoder->exitAggregateType();
3402 }
3403 }
3404 }
3405 }
3406
3407 GLenum OutputASM::glVariableType(const TType &type)
3408 {
3409 switch(type.getBasicType())
3410 {
3411 case EbtFloat:
3412 if(type.isScalar())
3413 {
3414 return GL_FLOAT;
3415 }
3416 else if(type.isVector())
3417 {
3418 switch(type.getNominalSize())
3419 {
3420 case 2: return GL_FLOAT_VEC2;
3421 case 3: return GL_FLOAT_VEC3;
3422 case 4: return GL_FLOAT_VEC4;
3423 default: UNREACHABLE(type.getNominalSize());
3424 }
3425 }
3426 else if(type.isMatrix())
3427 {
3428 switch(type.getNominalSize())
3429 {
3430 case 2:
3431 switch(type.getSecondarySize())
3432 {
3433 case 2: return GL_FLOAT_MAT2;
3434 case 3: return GL_FLOAT_MAT2x3;
3435 case 4: return GL_FLOAT_MAT2x4;
3436 default: UNREACHABLE(type.getSecondarySize());
3437 }
3438 case 3:
3439 switch(type.getSecondarySize())
3440 {
3441 case 2: return GL_FLOAT_MAT3x2;
3442 case 3: return GL_FLOAT_MAT3;
3443 case 4: return GL_FLOAT_MAT3x4;
3444 default: UNREACHABLE(type.getSecondarySize());
3445 }
3446 case 4:
3447 switch(type.getSecondarySize())
3448 {
3449 case 2: return GL_FLOAT_MAT4x2;
3450 case 3: return GL_FLOAT_MAT4x3;
3451 case 4: return GL_FLOAT_MAT4;
3452 default: UNREACHABLE(type.getSecondarySize());
3453 }
3454 default: UNREACHABLE(type.getNominalSize());
3455 }
3456 }
3457 else UNREACHABLE(0);
3458 break;
3459 case EbtInt:
3460 if(type.isScalar())
3461 {
3462 return GL_INT;
3463 }
3464 else if(type.isVector())
3465 {
3466 switch(type.getNominalSize())
3467 {
3468 case 2: return GL_INT_VEC2;
3469 case 3: return GL_INT_VEC3;
3470 case 4: return GL_INT_VEC4;
3471 default: UNREACHABLE(type.getNominalSize());
3472 }
3473 }
3474 else UNREACHABLE(0);
3475 break;
3476 case EbtUInt:
3477 if(type.isScalar())
3478 {
3479 return GL_UNSIGNED_INT;
3480 }
3481 else if(type.isVector())
3482 {
3483 switch(type.getNominalSize())
3484 {
3485 case 2: return GL_UNSIGNED_INT_VEC2;
3486 case 3: return GL_UNSIGNED_INT_VEC3;
3487 case 4: return GL_UNSIGNED_INT_VEC4;
3488 default: UNREACHABLE(type.getNominalSize());
3489 }
3490 }
3491 else UNREACHABLE(0);
3492 break;
3493 case EbtBool:
3494 if(type.isScalar())
3495 {
3496 return GL_BOOL;
3497 }
3498 else if(type.isVector())
3499 {
3500 switch(type.getNominalSize())
3501 {
3502 case 2: return GL_BOOL_VEC2;
3503 case 3: return GL_BOOL_VEC3;
3504 case 4: return GL_BOOL_VEC4;
3505 default: UNREACHABLE(type.getNominalSize());
3506 }
3507 }
3508 else UNREACHABLE(0);
3509 break;
3510 case EbtSampler2D:
3511 return GL_SAMPLER_2D;
3512 case EbtISampler2D:
3513 return GL_INT_SAMPLER_2D;
3514 case EbtUSampler2D:
3515 return GL_UNSIGNED_INT_SAMPLER_2D;
3516 case EbtSamplerCube:
3517 return GL_SAMPLER_CUBE;
3518 case EbtISamplerCube:
3519 return GL_INT_SAMPLER_CUBE;
3520 case EbtUSamplerCube:
3521 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3522 case EbtSamplerExternalOES:
3523 return GL_SAMPLER_EXTERNAL_OES;
3524 case EbtSampler3D:
3525 return GL_SAMPLER_3D_OES;
3526 case EbtISampler3D:
3527 return GL_INT_SAMPLER_3D;
3528 case EbtUSampler3D:
3529 return GL_UNSIGNED_INT_SAMPLER_3D;
3530 case EbtSampler2DArray:
3531 return GL_SAMPLER_2D_ARRAY;
3532 case EbtISampler2DArray:
3533 return GL_INT_SAMPLER_2D_ARRAY;
3534 case EbtUSampler2DArray:
3535 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3536 case EbtSampler2DShadow:
3537 return GL_SAMPLER_2D_SHADOW;
3538 case EbtSamplerCubeShadow:
3539 return GL_SAMPLER_CUBE_SHADOW;
3540 case EbtSampler2DArrayShadow:
3541 return GL_SAMPLER_2D_ARRAY_SHADOW;
3542 default:
3543 UNREACHABLE(type.getBasicType());
3544 break;
3545 }
3546
3547 return GL_NONE;
3548 }
3549
3550 GLenum OutputASM::glVariablePrecision(const TType &type)
3551 {
3552 if(type.getBasicType() == EbtFloat)
3553 {
3554 switch(type.getPrecision())
3555 {
3556 case EbpHigh: return GL_HIGH_FLOAT;
3557 case EbpMedium: return GL_MEDIUM_FLOAT;
3558 case EbpLow: return GL_LOW_FLOAT;
3559 case EbpUndefined:
3560 // Should be defined as the default precision by the parser
3561 default: UNREACHABLE(type.getPrecision());
3562 }
3563 }
3564 else if(type.getBasicType() == EbtInt)
3565 {
3566 switch(type.getPrecision())
3567 {
3568 case EbpHigh: return GL_HIGH_INT;
3569 case EbpMedium: return GL_MEDIUM_INT;
3570 case EbpLow: return GL_LOW_INT;
3571 case EbpUndefined:
3572 // Should be defined as the default precision by the parser
3573 default: UNREACHABLE(type.getPrecision());
3574 }
3575 }
3576
3577 // Other types (boolean, sampler) don't have a precision
3578 return GL_NONE;
3579 }
3580
3581 int OutputASM::dim(TIntermNode *v)
3582 {
3583 TIntermTyped *vector = v->getAsTyped();
3584 ASSERT(vector && vector->isRegister());
3585 return vector->getNominalSize();
3586 }
3587
3588 int OutputASM::dim2(TIntermNode *m)
3589 {
3590 TIntermTyped *matrix = m->getAsTyped();
3591 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3592 return matrix->getSecondarySize();
3593 }
3594
3595 // Returns ~0u if no loop count could be determined
3596 unsigned int OutputASM::loopCount(TIntermLoop *node)
3597 {
3598 // Parse loops of the form:
3599 // for(int index = initial; index [comparator] limit; index += increment)
3600 TIntermSymbol *index = 0;
3601 TOperator comparator = EOpNull;
3602 int initial = 0;
3603 int limit = 0;
3604 int increment = 0;
3605
3606 // Parse index name and intial value
3607 if(node->getInit())
3608 {
3609 TIntermAggregate *init = node->getInit()->getAsAggregate();
3610
3611 if(init)
3612 {
3613 TIntermSequence &sequence = init->getSequence();
3614 TIntermTyped *variable = sequence[0]->getAsTyped();
3615
Nicolas Capense3f05552017-05-24 10:45:56 -04003616 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003617 {
3618 TIntermBinary *assign = variable->getAsBinaryNode();
3619
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003620 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003621 {
3622 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3623 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3624
3625 if(symbol && constant)
3626 {
3627 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3628 {
3629 index = symbol;
3630 initial = constant->getUnionArrayPointer()[0].getIConst();
3631 }
3632 }
3633 }
3634 }
3635 }
3636 }
3637
3638 // Parse comparator and limit value
3639 if(index && node->getCondition())
3640 {
3641 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003642 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003643
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003644 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003645 {
3646 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3647
3648 if(constant)
3649 {
3650 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3651 {
3652 comparator = test->getOp();
3653 limit = constant->getUnionArrayPointer()[0].getIConst();
3654 }
3655 }
3656 }
3657 }
3658
3659 // Parse increment
3660 if(index && comparator != EOpNull && node->getExpression())
3661 {
3662 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3663 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3664
3665 if(binaryTerminal)
3666 {
3667 TOperator op = binaryTerminal->getOp();
3668 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3669
3670 if(constant)
3671 {
3672 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3673 {
3674 int value = constant->getUnionArrayPointer()[0].getIConst();
3675
3676 switch(op)
3677 {
3678 case EOpAddAssign: increment = value; break;
3679 case EOpSubAssign: increment = -value; break;
3680 default: UNIMPLEMENTED();
3681 }
3682 }
3683 }
3684 }
3685 else if(unaryTerminal)
3686 {
3687 TOperator op = unaryTerminal->getOp();
3688
3689 switch(op)
3690 {
3691 case EOpPostIncrement: increment = 1; break;
3692 case EOpPostDecrement: increment = -1; break;
3693 case EOpPreIncrement: increment = 1; break;
3694 case EOpPreDecrement: increment = -1; break;
3695 default: UNIMPLEMENTED();
3696 }
3697 }
3698 }
3699
3700 if(index && comparator != EOpNull && increment != 0)
3701 {
3702 if(comparator == EOpLessThanEqual)
3703 {
3704 comparator = EOpLessThan;
3705 limit += 1;
3706 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003707 else if(comparator == EOpGreaterThanEqual)
3708 {
3709 comparator = EOpLessThan;
3710 limit -= 1;
3711 std::swap(initial, limit);
3712 increment = -increment;
3713 }
3714 else if(comparator == EOpGreaterThan)
3715 {
3716 comparator = EOpLessThan;
3717 std::swap(initial, limit);
3718 increment = -increment;
3719 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003720
3721 if(comparator == EOpLessThan)
3722 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003723 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003724 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003725 return 0;
3726 }
3727
3728 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3729
3730 if(iterations < 0)
3731 {
3732 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003733 }
3734
3735 return iterations;
3736 }
3737 else UNIMPLEMENTED(); // Falls through
3738 }
3739
3740 return ~0u;
3741 }
3742
3743 bool LoopUnrollable::traverse(TIntermNode *node)
3744 {
3745 loopDepth = 0;
3746 loopUnrollable = true;
3747
3748 node->traverse(this);
3749
3750 return loopUnrollable;
3751 }
3752
3753 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3754 {
3755 if(visit == PreVisit)
3756 {
3757 loopDepth++;
3758 }
3759 else if(visit == PostVisit)
3760 {
3761 loopDepth++;
3762 }
3763
3764 return true;
3765 }
3766
3767 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3768 {
3769 if(!loopUnrollable)
3770 {
3771 return false;
3772 }
3773
3774 if(!loopDepth)
3775 {
3776 return true;
3777 }
3778
3779 switch(node->getFlowOp())
3780 {
3781 case EOpKill:
3782 case EOpReturn:
3783 break;
3784 case EOpBreak:
3785 case EOpContinue:
3786 loopUnrollable = false;
3787 break;
3788 default: UNREACHABLE(node->getFlowOp());
3789 }
3790
3791 return loopUnrollable;
3792 }
3793
3794 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3795 {
3796 return loopUnrollable;
3797 }
3798}