blob: 82168878cab8e2e27adc312405e73558c2eb80fe [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;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002291 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002292 }
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);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002299 }
2300 }
2301
2302 int swizzleElement(int swizzle, int index)
2303 {
2304 return (swizzle >> (index * 2)) & 0x03;
2305 }
2306
2307 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2308 {
2309 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2310 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2311 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2312 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2313 }
2314
2315 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2316 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002317 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2318 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002319 {
2320 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2321 }
2322
2323 TIntermBinary *binary = dst->getAsBinaryNode();
2324
2325 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2326 {
2327 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2328
Nicolas Capens6986b282017-11-16 10:38:19 -05002329 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002330
2331 insert->src[0].type = insert->dst.type;
2332 insert->src[0].index = insert->dst.index;
2333 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002334 source(insert->src[1], src);
2335 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002336
2337 shader->append(insert);
2338 }
2339 else
2340 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002341 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2342
Nicolas Capens6986b282017-11-16 10:38:19 -05002343 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002344
Nicolas Capens0530b452017-11-15 16:39:47 -05002345 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002346 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2347
2348 shader->append(mov1);
2349
2350 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002351 {
2352 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2353
Nicolas Capens84249fd2017-11-09 11:20:51 -05002354 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002355 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002356 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002357
Nicolas Capens0530b452017-11-15 16:39:47 -05002358 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002359
2360 shader->append(mov);
2361 }
2362 }
2363 }
2364
Nicolas Capens6986b282017-11-16 10:38:19 -05002365 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002366 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002367 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002368 TIntermTyped *root = nullptr;
2369 unsigned int offset = 0;
2370 unsigned char mask = 0xF;
2371 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2372
2373 dst.type = registerType(root);
2374 dst.index = registerIndex(root) + offset;
2375 dst.mask = mask;
2376
2377 return swizzle;
2378 }
2379
2380 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2381 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002382 TIntermTyped *result = node;
2383 TIntermBinary *binary = node->getAsBinaryNode();
2384 TIntermSymbol *symbol = node->getAsSymbolNode();
2385
2386 if(binary)
2387 {
2388 TIntermTyped *left = binary->getLeft();
2389 TIntermTyped *right = binary->getRight();
2390
Nicolas Capens0530b452017-11-15 16:39:47 -05002391 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002392
2393 switch(binary->getOp())
2394 {
2395 case EOpIndexDirect:
2396 {
2397 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2398
2399 if(left->isRegister())
2400 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002401 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002402
Nicolas Capens0530b452017-11-15 16:39:47 -05002403 mask = 1;
2404 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002405 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002406 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002407 }
2408
2409 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002410 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002411
2412 return element;
2413 }
2414 else if(left->isArray() || left->isMatrix())
2415 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002416 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002417 return 0xE4;
2418 }
2419 else UNREACHABLE(0);
2420 }
2421 break;
2422 case EOpIndexIndirect:
2423 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002424 right->traverse(this);
2425
Nicolas Capens0bac2852016-05-07 06:09:58 -04002426 if(left->isRegister())
2427 {
2428 // Requires INSERT instruction (handled by calling function)
2429 }
2430 else if(left->isArray() || left->isMatrix())
2431 {
2432 int scale = result->totalRegisterCount();
2433
Nicolas Capens0530b452017-11-15 16:39:47 -05002434 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002435 {
2436 if(left->totalRegisterCount() > 1)
2437 {
2438 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002439 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002440
Nicolas Capens0530b452017-11-15 16:39:47 -05002441 rel.index = relativeRegister.index;
2442 rel.type = relativeRegister.type;
2443 rel.scale = scale;
2444 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002445 }
2446 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002447 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002448 {
2449 if(scale == 1)
2450 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002451 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002452 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002453 mad->src[0].index = rel.index;
2454 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002455 }
2456 else
2457 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002458 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002459 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002460 mul->src[0].index = rel.index;
2461 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002462
2463 Constant newScale(scale);
2464 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2465 }
2466
Nicolas Capens0530b452017-11-15 16:39:47 -05002467 rel.type = sw::Shader::PARAMETER_TEMP;
2468 rel.index = registerIndex(&address);
2469 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002470 }
2471 else // Just add the new index to the address register
2472 {
2473 if(scale == 1)
2474 {
2475 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2476 }
2477 else
2478 {
2479 Constant newScale(scale);
2480 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2481 }
2482 }
2483 }
2484 else UNREACHABLE(0);
2485 }
2486 break;
2487 case EOpIndexDirectStruct:
2488 case EOpIndexDirectInterfaceBlock:
2489 {
2490 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2491 left->getType().getStruct()->fields() :
2492 left->getType().getInterfaceBlock()->fields();
2493 int index = right->getAsConstantUnion()->getIConst(0);
2494 int fieldOffset = 0;
2495
2496 for(int i = 0; i < index; i++)
2497 {
2498 fieldOffset += fields[i]->type()->totalRegisterCount();
2499 }
2500
Nicolas Capens0530b452017-11-15 16:39:47 -05002501 offset += fieldOffset;
2502 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002503
2504 return 0xE4;
2505 }
2506 break;
2507 case EOpVectorSwizzle:
2508 {
2509 ASSERT(left->isRegister());
2510
Nicolas Capens0530b452017-11-15 16:39:47 -05002511 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002512
2513 int swizzle = 0;
2514 int rightMask = 0;
2515
2516 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2517
2518 for(unsigned int i = 0; i < sequence.size(); i++)
2519 {
2520 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2521
2522 int element = swizzleElement(leftSwizzle, index);
2523 rightMask = rightMask | (1 << element);
2524 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2525 }
2526
Nicolas Capens0530b452017-11-15 16:39:47 -05002527 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002528
2529 return swizzle;
2530 }
2531 break;
2532 default:
2533 UNREACHABLE(binary->getOp()); // Not an l-value operator
2534 break;
2535 }
2536 }
2537 else if(symbol)
2538 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002539 root = symbol;
2540 offset = 0;
2541 mask = writeMask(symbol);
2542
2543 return 0xE4;
2544 }
2545 else
2546 {
2547 node->traverse(this);
2548
2549 root = node;
2550 offset = 0;
2551 mask = writeMask(node);
2552
Nicolas Capens0bac2852016-05-07 06:09:58 -04002553 return 0xE4;
2554 }
2555
2556 return 0xE4;
2557 }
2558
2559 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2560 {
2561 if(isSamplerRegister(operand))
2562 {
2563 return sw::Shader::PARAMETER_SAMPLER;
2564 }
2565
2566 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002567 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002568 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002569 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2570 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002571 {
2572 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2573 }
2574 outputQualifier = qualifier;
2575 }
2576
2577 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2578 {
2579 return sw::Shader::PARAMETER_TEMP;
2580 }
2581
2582 switch(qualifier)
2583 {
2584 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2585 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2586 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2587 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2588 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2589 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2590 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2591 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2592 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2593 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2594 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2595 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2596 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2597 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2598 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2599 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2600 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2601 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2602 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2603 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2604 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2605 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2606 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2607 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2608 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2609 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002610 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002611 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2612 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2613 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2614 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2615 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2616 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2617 default: UNREACHABLE(qualifier);
2618 }
2619
2620 return sw::Shader::PARAMETER_VOID;
2621 }
2622
Alexis Hetu12b00502016-05-20 13:01:11 -04002623 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2624 {
2625 const TQualifier qualifier = operand->getQualifier();
2626 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2627 }
2628
Nicolas Capens0bac2852016-05-07 06:09:58 -04002629 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2630 {
2631 if(isSamplerRegister(operand))
2632 {
2633 return samplerRegister(operand);
2634 }
2635
2636 switch(operand->getQualifier())
2637 {
2638 case EvqTemporary: return temporaryRegister(operand);
2639 case EvqGlobal: return temporaryRegister(operand);
2640 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2641 case EvqAttribute: return attributeRegister(operand);
2642 case EvqVaryingIn: return varyingRegister(operand);
2643 case EvqVaryingOut: return varyingRegister(operand);
2644 case EvqVertexIn: return attributeRegister(operand);
2645 case EvqFragmentOut: return fragmentOutputRegister(operand);
2646 case EvqVertexOut: return varyingRegister(operand);
2647 case EvqFragmentIn: return varyingRegister(operand);
2648 case EvqInvariantVaryingIn: return varyingRegister(operand);
2649 case EvqInvariantVaryingOut: return varyingRegister(operand);
2650 case EvqSmooth: return varyingRegister(operand);
2651 case EvqFlat: return varyingRegister(operand);
2652 case EvqCentroidOut: return varyingRegister(operand);
2653 case EvqSmoothIn: return varyingRegister(operand);
2654 case EvqFlatIn: return varyingRegister(operand);
2655 case EvqCentroidIn: return varyingRegister(operand);
2656 case EvqUniform: return uniformRegister(operand);
2657 case EvqIn: return temporaryRegister(operand);
2658 case EvqOut: return temporaryRegister(operand);
2659 case EvqInOut: return temporaryRegister(operand);
2660 case EvqConstReadOnly: return temporaryRegister(operand);
2661 case EvqPosition: return varyingRegister(operand);
2662 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002663 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2664 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2665 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2666 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002667 case EvqPointCoord: return varyingRegister(operand);
2668 case EvqFragColor: return 0;
2669 case EvqFragData: return fragmentOutputRegister(operand);
2670 case EvqFragDepth: return 0;
2671 default: UNREACHABLE(operand->getQualifier());
2672 }
2673
2674 return 0;
2675 }
2676
2677 int OutputASM::writeMask(TIntermTyped *destination, int index)
2678 {
2679 if(destination->getQualifier() == EvqPointSize)
2680 {
2681 return 0x2; // Point size stored in the y component
2682 }
2683
2684 return 0xF >> (4 - registerSize(destination->getType(), index));
2685 }
2686
2687 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2688 {
2689 if(argument->getQualifier() == EvqPointSize)
2690 {
2691 return 0x55; // Point size stored in the y component
2692 }
2693
2694 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2695
2696 return swizzleSize[size];
2697 }
2698
2699 // Conservatively checks whether an expression is fast to compute and has no side effects
2700 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2701 {
2702 if(!expression->isRegister())
2703 {
2704 return false;
2705 }
2706
2707 return cost(expression, budget) >= 0;
2708 }
2709
2710 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2711 int OutputASM::cost(TIntermNode *expression, int budget)
2712 {
2713 if(budget < 0)
2714 {
2715 return budget;
2716 }
2717
2718 if(expression->getAsSymbolNode())
2719 {
2720 return budget;
2721 }
2722 else if(expression->getAsConstantUnion())
2723 {
2724 return budget;
2725 }
2726 else if(expression->getAsBinaryNode())
2727 {
2728 TIntermBinary *binary = expression->getAsBinaryNode();
2729
2730 switch(binary->getOp())
2731 {
2732 case EOpVectorSwizzle:
2733 case EOpIndexDirect:
2734 case EOpIndexDirectStruct:
2735 case EOpIndexDirectInterfaceBlock:
2736 return cost(binary->getLeft(), budget - 0);
2737 case EOpAdd:
2738 case EOpSub:
2739 case EOpMul:
2740 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2741 default:
2742 return -1;
2743 }
2744 }
2745 else if(expression->getAsUnaryNode())
2746 {
2747 TIntermUnary *unary = expression->getAsUnaryNode();
2748
2749 switch(unary->getOp())
2750 {
2751 case EOpAbs:
2752 case EOpNegative:
2753 return cost(unary->getOperand(), budget - 1);
2754 default:
2755 return -1;
2756 }
2757 }
2758 else if(expression->getAsSelectionNode())
2759 {
2760 TIntermSelection *selection = expression->getAsSelectionNode();
2761
2762 if(selection->usesTernaryOperator())
2763 {
2764 TIntermTyped *condition = selection->getCondition();
2765 TIntermNode *trueBlock = selection->getTrueBlock();
2766 TIntermNode *falseBlock = selection->getFalseBlock();
2767 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2768
2769 if(constantCondition)
2770 {
2771 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2772
2773 if(trueCondition)
2774 {
2775 return cost(trueBlock, budget - 0);
2776 }
2777 else
2778 {
2779 return cost(falseBlock, budget - 0);
2780 }
2781 }
2782 else
2783 {
2784 return cost(trueBlock, cost(falseBlock, budget - 2));
2785 }
2786 }
2787 }
2788
2789 return -1;
2790 }
2791
2792 const Function *OutputASM::findFunction(const TString &name)
2793 {
2794 for(unsigned int f = 0; f < functionArray.size(); f++)
2795 {
2796 if(functionArray[f].name == name)
2797 {
2798 return &functionArray[f];
2799 }
2800 }
2801
2802 return 0;
2803 }
2804
2805 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2806 {
2807 return allocate(temporaries, temporary);
2808 }
2809
Alexis Hetu49351232017-11-02 16:00:32 -04002810 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
2811 {
2812 if(type.isStruct())
2813 {
2814 const TFieldList &fields = type.getStruct()->fields();
2815 int fieldVar = var;
2816 for(size_t i = 0; i < fields.size(); i++)
2817 {
2818 const TType& fieldType = *(fields[i]->type());
2819 setPixelShaderInputs(fieldType, fieldVar, flat);
2820 fieldVar += fieldType.totalRegisterCount();
2821 }
2822 }
2823 else
2824 {
2825 for(int i = 0; i < type.totalRegisterCount(); i++)
2826 {
2827 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2828 }
2829 }
2830 }
2831
Nicolas Capens0bac2852016-05-07 06:09:58 -04002832 int OutputASM::varyingRegister(TIntermTyped *varying)
2833 {
2834 int var = lookup(varyings, varying);
2835
2836 if(var == -1)
2837 {
2838 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002839 int registerCount = varying->totalRegisterCount();
2840
2841 if(pixelShader)
2842 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04002843 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002844 {
2845 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2846 return 0;
2847 }
2848
2849 if(varying->getQualifier() == EvqPointCoord)
2850 {
2851 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04002852 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002853 }
2854 else
2855 {
Alexis Hetu49351232017-11-02 16:00:32 -04002856 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002857 }
2858 }
2859 else if(vertexShader)
2860 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04002861 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002862 {
2863 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2864 return 0;
2865 }
2866
2867 if(varying->getQualifier() == EvqPosition)
2868 {
2869 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002870 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002871 }
2872 else if(varying->getQualifier() == EvqPointSize)
2873 {
2874 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002875 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002876 }
2877 else
2878 {
2879 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2880 }
2881 }
2882 else UNREACHABLE(0);
2883
2884 declareVarying(varying, var);
2885 }
2886
2887 return var;
2888 }
2889
2890 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2891 {
2892 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
2893 {
Alexis Hetu49351232017-11-02 16:00:32 -04002894 TIntermSymbol *symbol = varying->getAsSymbolNode();
2895 declareVarying(varying->getType(), symbol->getSymbol(), reg);
2896 }
2897 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002898
Alexis Hetu49351232017-11-02 16:00:32 -04002899 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
2900 {
2901 const char *name = varyingName.c_str();
2902 VaryingList &activeVaryings = shaderObject->varyings;
2903
2904 TStructure* structure = type.getStruct();
2905 if(structure)
2906 {
2907 int fieldRegisterIndex = registerIndex;
2908
2909 const TFieldList &fields = type.getStruct()->fields();
2910 for(size_t i = 0; i < fields.size(); i++)
2911 {
2912 const TType& fieldType = *(fields[i]->type());
2913 declareVarying(fieldType, varyingName + "." + fields[i]->name(), fieldRegisterIndex);
2914 if(fieldRegisterIndex >= 0)
2915 {
2916 fieldRegisterIndex += fieldType.totalRegisterCount();
2917 }
2918 }
2919 }
2920 else
2921 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002922 // Check if this varying has been declared before without having a register assigned
2923 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2924 {
2925 if(v->name == name)
2926 {
Alexis Hetu49351232017-11-02 16:00:32 -04002927 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002928 {
Alexis Hetu49351232017-11-02 16:00:32 -04002929 ASSERT(v->reg < 0 || v->reg == registerIndex);
2930 v->reg = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002931 }
2932
2933 return;
2934 }
2935 }
2936
Alexis Hetu49351232017-11-02 16:00:32 -04002937 activeVaryings.push_back(glsl::Varying(glVariableType(type), name, type.getArraySize(), registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002938 }
2939 }
2940
2941 int OutputASM::uniformRegister(TIntermTyped *uniform)
2942 {
2943 const TType &type = uniform->getType();
2944 ASSERT(!IsSampler(type.getBasicType()));
2945 TInterfaceBlock *block = type.getAsInterfaceBlock();
2946 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2947 ASSERT(symbol || block);
2948
2949 if(symbol || block)
2950 {
2951 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2952 bool isBlockMember = (!block && parentBlock);
2953 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2954
2955 if(index == -1 || isBlockMember)
2956 {
2957 if(index == -1)
2958 {
2959 index = allocate(uniforms, uniform);
2960 }
2961
2962 // Verify if the current uniform is a member of an already declared block
2963 const TString &name = symbol ? symbol->getSymbol() : block->name();
2964 int blockMemberIndex = blockMemberLookup(type, name, index);
2965 if(blockMemberIndex == -1)
2966 {
2967 declareUniform(type, name, index);
2968 }
2969 else
2970 {
2971 index = blockMemberIndex;
2972 }
2973 }
2974
2975 return index;
2976 }
2977
2978 return 0;
2979 }
2980
2981 int OutputASM::attributeRegister(TIntermTyped *attribute)
2982 {
2983 ASSERT(!attribute->isArray());
2984
2985 int index = lookup(attributes, attribute);
2986
2987 if(index == -1)
2988 {
2989 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2990 ASSERT(symbol);
2991
2992 if(symbol)
2993 {
2994 index = allocate(attributes, attribute);
2995 const TType &type = attribute->getType();
2996 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04002997 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2998 switch(type.getBasicType())
2999 {
3000 case EbtInt:
3001 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3002 break;
3003 case EbtUInt:
3004 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3005 break;
3006 case EbtFloat:
3007 default:
3008 break;
3009 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003010
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003011 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003012 {
3013 for(int i = 0; i < registerCount; i++)
3014 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003015 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003016 }
3017 }
3018
3019 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3020
3021 const char *name = symbol->getSymbol().c_str();
3022 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3023 }
3024 }
3025
3026 return index;
3027 }
3028
3029 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3030 {
3031 return allocate(fragmentOutputs, fragmentOutput);
3032 }
3033
3034 int OutputASM::samplerRegister(TIntermTyped *sampler)
3035 {
3036 const TType &type = sampler->getType();
3037 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3038
3039 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3040 TIntermBinary *binary = sampler->getAsBinaryNode();
3041
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003042 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003043 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003044 switch(type.getQualifier())
3045 {
3046 case EvqUniform:
3047 return samplerRegister(symbol);
3048 case EvqIn:
3049 case EvqConstReadOnly:
3050 // Function arguments are not (uniform) sampler registers
3051 return -1;
3052 default:
3053 UNREACHABLE(type.getQualifier());
3054 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003055 }
3056 else if(binary)
3057 {
3058 TIntermTyped *left = binary->getLeft();
3059 TIntermTyped *right = binary->getRight();
3060 const TType &leftType = left->getType();
3061 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3062 int offset = 0;
3063
3064 switch(binary->getOp())
3065 {
3066 case EOpIndexDirect:
3067 ASSERT(left->isArray());
3068 offset = index * leftType.elementRegisterCount();
3069 break;
3070 case EOpIndexDirectStruct:
3071 ASSERT(leftType.isStruct());
3072 {
3073 const TFieldList &fields = leftType.getStruct()->fields();
3074
3075 for(int i = 0; i < index; i++)
3076 {
3077 offset += fields[i]->type()->totalRegisterCount();
3078 }
3079 }
3080 break;
3081 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3082 return -1;
3083 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3084 default:
3085 UNREACHABLE(binary->getOp());
3086 return -1;
3087 }
3088
3089 int base = samplerRegister(left);
3090
3091 if(base < 0)
3092 {
3093 return -1;
3094 }
3095
3096 return base + offset;
3097 }
3098
3099 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003100 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003101 }
3102
3103 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3104 {
3105 const TType &type = sampler->getType();
3106 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3107
3108 int index = lookup(samplers, sampler);
3109
3110 if(index == -1)
3111 {
3112 index = allocate(samplers, sampler);
3113
3114 if(sampler->getQualifier() == EvqUniform)
3115 {
3116 const char *name = sampler->getSymbol().c_str();
3117 declareUniform(type, name, index);
3118 }
3119 }
3120
3121 return index;
3122 }
3123
3124 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3125 {
3126 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3127 }
3128
3129 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3130 {
3131 for(unsigned int i = 0; i < list.size(); i++)
3132 {
3133 if(list[i] == variable)
3134 {
3135 return i; // Pointer match
3136 }
3137 }
3138
3139 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3140 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3141
3142 if(varBlock)
3143 {
3144 for(unsigned int i = 0; i < list.size(); i++)
3145 {
3146 if(list[i])
3147 {
3148 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3149
3150 if(listBlock)
3151 {
3152 if(listBlock->name() == varBlock->name())
3153 {
3154 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3155 ASSERT(listBlock->fields() == varBlock->fields());
3156 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3157 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3158
3159 return i;
3160 }
3161 }
3162 }
3163 }
3164 }
3165 else if(varSymbol)
3166 {
3167 for(unsigned int i = 0; i < list.size(); i++)
3168 {
3169 if(list[i])
3170 {
3171 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3172
3173 if(listSymbol)
3174 {
3175 if(listSymbol->getId() == varSymbol->getId())
3176 {
3177 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3178 ASSERT(listSymbol->getType() == varSymbol->getType());
3179 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3180
3181 return i;
3182 }
3183 }
3184 }
3185 }
3186 }
3187
3188 return -1;
3189 }
3190
3191 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3192 {
3193 for(unsigned int i = 0; i < list.size(); i++)
3194 {
3195 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3196 {
3197 return i; // Pointer match
3198 }
3199 }
3200 return -1;
3201 }
3202
3203 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
3204 {
3205 int index = lookup(list, variable);
3206
3207 if(index == -1)
3208 {
3209 unsigned int registerCount = variable->blockRegisterCount();
3210
3211 for(unsigned int i = 0; i < list.size(); i++)
3212 {
3213 if(list[i] == 0)
3214 {
3215 unsigned int j = 1;
3216 for( ; j < registerCount && (i + j) < list.size(); j++)
3217 {
3218 if(list[i + j] != 0)
3219 {
3220 break;
3221 }
3222 }
3223
3224 if(j == registerCount) // Found free slots
3225 {
3226 for(unsigned int j = 0; j < registerCount; j++)
3227 {
3228 list[i + j] = variable;
3229 }
3230
3231 return i;
3232 }
3233 }
3234 }
3235
3236 index = list.size();
3237
3238 for(unsigned int i = 0; i < registerCount; i++)
3239 {
3240 list.push_back(variable);
3241 }
3242 }
3243
3244 return index;
3245 }
3246
3247 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3248 {
3249 int index = lookup(list, variable);
3250
3251 if(index >= 0)
3252 {
3253 list[index] = 0;
3254 }
3255 }
3256
3257 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3258 {
3259 const TInterfaceBlock *block = type.getInterfaceBlock();
3260
3261 if(block)
3262 {
3263 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3264 const TFieldList& fields = block->fields();
3265 const TString &blockName = block->name();
3266 int fieldRegisterIndex = registerIndex;
3267
3268 if(!type.isInterfaceBlock())
3269 {
3270 // This is a uniform that's part of a block, let's see if the block is already defined
3271 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3272 {
3273 if(activeUniformBlocks[i].name == blockName.c_str())
3274 {
3275 // The block is already defined, find the register for the current uniform and return it
3276 for(size_t j = 0; j < fields.size(); j++)
3277 {
3278 const TString &fieldName = fields[j]->name();
3279 if(fieldName == name)
3280 {
3281 return fieldRegisterIndex;
3282 }
3283
3284 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3285 }
3286
3287 ASSERT(false);
3288 return fieldRegisterIndex;
3289 }
3290 }
3291 }
3292 }
3293
3294 return -1;
3295 }
3296
3297 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int blockId, BlockLayoutEncoder* encoder)
3298 {
3299 const TStructure *structure = type.getStruct();
3300 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3301
3302 if(!structure && !block)
3303 {
3304 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3305 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3306 if(blockId >= 0)
3307 {
3308 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3309 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3310 }
3311 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3312 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3313 fieldRegisterIndex, blockId, blockInfo));
3314 if(IsSampler(type.getBasicType()))
3315 {
3316 for(int i = 0; i < type.totalRegisterCount(); i++)
3317 {
3318 shader->declareSampler(fieldRegisterIndex + i);
3319 }
3320 }
3321 }
3322 else if(block)
3323 {
3324 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3325 const TFieldList& fields = block->fields();
3326 const TString &blockName = block->name();
3327 int fieldRegisterIndex = registerIndex;
3328 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3329
3330 blockId = activeUniformBlocks.size();
3331 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3332 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3333 block->blockStorage(), isRowMajor, registerIndex, blockId));
3334 blockDefinitions.push_back(BlockDefinitionIndexMap());
3335
3336 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3337 currentBlockEncoder.enterAggregateType();
3338 for(size_t i = 0; i < fields.size(); i++)
3339 {
3340 const TType &fieldType = *(fields[i]->type());
3341 const TString &fieldName = fields[i]->name();
3342 if(isUniformBlockMember && (fieldName == name))
3343 {
3344 registerIndex = fieldRegisterIndex;
3345 }
3346
3347 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3348
3349 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, &currentBlockEncoder);
3350 fieldRegisterIndex += fieldType.totalRegisterCount();
3351 }
3352 currentBlockEncoder.exitAggregateType();
3353 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3354 }
3355 else
3356 {
3357 int fieldRegisterIndex = registerIndex;
3358
3359 const TFieldList& fields = structure->fields();
3360 if(type.isArray() && (structure || type.isInterfaceBlock()))
3361 {
3362 for(int i = 0; i < type.getArraySize(); i++)
3363 {
3364 if(encoder)
3365 {
3366 encoder->enterAggregateType();
3367 }
3368 for(size_t j = 0; j < fields.size(); j++)
3369 {
3370 const TType &fieldType = *(fields[j]->type());
3371 const TString &fieldName = fields[j]->name();
3372 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3373
3374 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3375 fieldRegisterIndex += fieldType.totalRegisterCount();
3376 }
3377 if(encoder)
3378 {
3379 encoder->exitAggregateType();
3380 }
3381 }
3382 }
3383 else
3384 {
3385 if(encoder)
3386 {
3387 encoder->enterAggregateType();
3388 }
3389 for(size_t i = 0; i < fields.size(); i++)
3390 {
3391 const TType &fieldType = *(fields[i]->type());
3392 const TString &fieldName = fields[i]->name();
3393 const TString uniformName = name + "." + fieldName;
3394
3395 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3396 fieldRegisterIndex += fieldType.totalRegisterCount();
3397 }
3398 if(encoder)
3399 {
3400 encoder->exitAggregateType();
3401 }
3402 }
3403 }
3404 }
3405
3406 GLenum OutputASM::glVariableType(const TType &type)
3407 {
3408 switch(type.getBasicType())
3409 {
3410 case EbtFloat:
3411 if(type.isScalar())
3412 {
3413 return GL_FLOAT;
3414 }
3415 else if(type.isVector())
3416 {
3417 switch(type.getNominalSize())
3418 {
3419 case 2: return GL_FLOAT_VEC2;
3420 case 3: return GL_FLOAT_VEC3;
3421 case 4: return GL_FLOAT_VEC4;
3422 default: UNREACHABLE(type.getNominalSize());
3423 }
3424 }
3425 else if(type.isMatrix())
3426 {
3427 switch(type.getNominalSize())
3428 {
3429 case 2:
3430 switch(type.getSecondarySize())
3431 {
3432 case 2: return GL_FLOAT_MAT2;
3433 case 3: return GL_FLOAT_MAT2x3;
3434 case 4: return GL_FLOAT_MAT2x4;
3435 default: UNREACHABLE(type.getSecondarySize());
3436 }
3437 case 3:
3438 switch(type.getSecondarySize())
3439 {
3440 case 2: return GL_FLOAT_MAT3x2;
3441 case 3: return GL_FLOAT_MAT3;
3442 case 4: return GL_FLOAT_MAT3x4;
3443 default: UNREACHABLE(type.getSecondarySize());
3444 }
3445 case 4:
3446 switch(type.getSecondarySize())
3447 {
3448 case 2: return GL_FLOAT_MAT4x2;
3449 case 3: return GL_FLOAT_MAT4x3;
3450 case 4: return GL_FLOAT_MAT4;
3451 default: UNREACHABLE(type.getSecondarySize());
3452 }
3453 default: UNREACHABLE(type.getNominalSize());
3454 }
3455 }
3456 else UNREACHABLE(0);
3457 break;
3458 case EbtInt:
3459 if(type.isScalar())
3460 {
3461 return GL_INT;
3462 }
3463 else if(type.isVector())
3464 {
3465 switch(type.getNominalSize())
3466 {
3467 case 2: return GL_INT_VEC2;
3468 case 3: return GL_INT_VEC3;
3469 case 4: return GL_INT_VEC4;
3470 default: UNREACHABLE(type.getNominalSize());
3471 }
3472 }
3473 else UNREACHABLE(0);
3474 break;
3475 case EbtUInt:
3476 if(type.isScalar())
3477 {
3478 return GL_UNSIGNED_INT;
3479 }
3480 else if(type.isVector())
3481 {
3482 switch(type.getNominalSize())
3483 {
3484 case 2: return GL_UNSIGNED_INT_VEC2;
3485 case 3: return GL_UNSIGNED_INT_VEC3;
3486 case 4: return GL_UNSIGNED_INT_VEC4;
3487 default: UNREACHABLE(type.getNominalSize());
3488 }
3489 }
3490 else UNREACHABLE(0);
3491 break;
3492 case EbtBool:
3493 if(type.isScalar())
3494 {
3495 return GL_BOOL;
3496 }
3497 else if(type.isVector())
3498 {
3499 switch(type.getNominalSize())
3500 {
3501 case 2: return GL_BOOL_VEC2;
3502 case 3: return GL_BOOL_VEC3;
3503 case 4: return GL_BOOL_VEC4;
3504 default: UNREACHABLE(type.getNominalSize());
3505 }
3506 }
3507 else UNREACHABLE(0);
3508 break;
3509 case EbtSampler2D:
3510 return GL_SAMPLER_2D;
3511 case EbtISampler2D:
3512 return GL_INT_SAMPLER_2D;
3513 case EbtUSampler2D:
3514 return GL_UNSIGNED_INT_SAMPLER_2D;
3515 case EbtSamplerCube:
3516 return GL_SAMPLER_CUBE;
3517 case EbtISamplerCube:
3518 return GL_INT_SAMPLER_CUBE;
3519 case EbtUSamplerCube:
3520 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3521 case EbtSamplerExternalOES:
3522 return GL_SAMPLER_EXTERNAL_OES;
3523 case EbtSampler3D:
3524 return GL_SAMPLER_3D_OES;
3525 case EbtISampler3D:
3526 return GL_INT_SAMPLER_3D;
3527 case EbtUSampler3D:
3528 return GL_UNSIGNED_INT_SAMPLER_3D;
3529 case EbtSampler2DArray:
3530 return GL_SAMPLER_2D_ARRAY;
3531 case EbtISampler2DArray:
3532 return GL_INT_SAMPLER_2D_ARRAY;
3533 case EbtUSampler2DArray:
3534 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3535 case EbtSampler2DShadow:
3536 return GL_SAMPLER_2D_SHADOW;
3537 case EbtSamplerCubeShadow:
3538 return GL_SAMPLER_CUBE_SHADOW;
3539 case EbtSampler2DArrayShadow:
3540 return GL_SAMPLER_2D_ARRAY_SHADOW;
3541 default:
3542 UNREACHABLE(type.getBasicType());
3543 break;
3544 }
3545
3546 return GL_NONE;
3547 }
3548
3549 GLenum OutputASM::glVariablePrecision(const TType &type)
3550 {
3551 if(type.getBasicType() == EbtFloat)
3552 {
3553 switch(type.getPrecision())
3554 {
3555 case EbpHigh: return GL_HIGH_FLOAT;
3556 case EbpMedium: return GL_MEDIUM_FLOAT;
3557 case EbpLow: return GL_LOW_FLOAT;
3558 case EbpUndefined:
3559 // Should be defined as the default precision by the parser
3560 default: UNREACHABLE(type.getPrecision());
3561 }
3562 }
3563 else if(type.getBasicType() == EbtInt)
3564 {
3565 switch(type.getPrecision())
3566 {
3567 case EbpHigh: return GL_HIGH_INT;
3568 case EbpMedium: return GL_MEDIUM_INT;
3569 case EbpLow: return GL_LOW_INT;
3570 case EbpUndefined:
3571 // Should be defined as the default precision by the parser
3572 default: UNREACHABLE(type.getPrecision());
3573 }
3574 }
3575
3576 // Other types (boolean, sampler) don't have a precision
3577 return GL_NONE;
3578 }
3579
3580 int OutputASM::dim(TIntermNode *v)
3581 {
3582 TIntermTyped *vector = v->getAsTyped();
3583 ASSERT(vector && vector->isRegister());
3584 return vector->getNominalSize();
3585 }
3586
3587 int OutputASM::dim2(TIntermNode *m)
3588 {
3589 TIntermTyped *matrix = m->getAsTyped();
3590 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3591 return matrix->getSecondarySize();
3592 }
3593
3594 // Returns ~0u if no loop count could be determined
3595 unsigned int OutputASM::loopCount(TIntermLoop *node)
3596 {
3597 // Parse loops of the form:
3598 // for(int index = initial; index [comparator] limit; index += increment)
3599 TIntermSymbol *index = 0;
3600 TOperator comparator = EOpNull;
3601 int initial = 0;
3602 int limit = 0;
3603 int increment = 0;
3604
3605 // Parse index name and intial value
3606 if(node->getInit())
3607 {
3608 TIntermAggregate *init = node->getInit()->getAsAggregate();
3609
3610 if(init)
3611 {
3612 TIntermSequence &sequence = init->getSequence();
3613 TIntermTyped *variable = sequence[0]->getAsTyped();
3614
Nicolas Capense3f05552017-05-24 10:45:56 -04003615 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003616 {
3617 TIntermBinary *assign = variable->getAsBinaryNode();
3618
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003619 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003620 {
3621 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3622 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3623
3624 if(symbol && constant)
3625 {
3626 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3627 {
3628 index = symbol;
3629 initial = constant->getUnionArrayPointer()[0].getIConst();
3630 }
3631 }
3632 }
3633 }
3634 }
3635 }
3636
3637 // Parse comparator and limit value
3638 if(index && node->getCondition())
3639 {
3640 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003641 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003642
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003643 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003644 {
3645 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3646
3647 if(constant)
3648 {
3649 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3650 {
3651 comparator = test->getOp();
3652 limit = constant->getUnionArrayPointer()[0].getIConst();
3653 }
3654 }
3655 }
3656 }
3657
3658 // Parse increment
3659 if(index && comparator != EOpNull && node->getExpression())
3660 {
3661 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3662 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3663
3664 if(binaryTerminal)
3665 {
3666 TOperator op = binaryTerminal->getOp();
3667 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3668
3669 if(constant)
3670 {
3671 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3672 {
3673 int value = constant->getUnionArrayPointer()[0].getIConst();
3674
3675 switch(op)
3676 {
3677 case EOpAddAssign: increment = value; break;
3678 case EOpSubAssign: increment = -value; break;
3679 default: UNIMPLEMENTED();
3680 }
3681 }
3682 }
3683 }
3684 else if(unaryTerminal)
3685 {
3686 TOperator op = unaryTerminal->getOp();
3687
3688 switch(op)
3689 {
3690 case EOpPostIncrement: increment = 1; break;
3691 case EOpPostDecrement: increment = -1; break;
3692 case EOpPreIncrement: increment = 1; break;
3693 case EOpPreDecrement: increment = -1; break;
3694 default: UNIMPLEMENTED();
3695 }
3696 }
3697 }
3698
3699 if(index && comparator != EOpNull && increment != 0)
3700 {
3701 if(comparator == EOpLessThanEqual)
3702 {
3703 comparator = EOpLessThan;
3704 limit += 1;
3705 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003706 else if(comparator == EOpGreaterThanEqual)
3707 {
3708 comparator = EOpLessThan;
3709 limit -= 1;
3710 std::swap(initial, limit);
3711 increment = -increment;
3712 }
3713 else if(comparator == EOpGreaterThan)
3714 {
3715 comparator = EOpLessThan;
3716 std::swap(initial, limit);
3717 increment = -increment;
3718 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003719
3720 if(comparator == EOpLessThan)
3721 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003722 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003723 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003724 return 0;
3725 }
3726
3727 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3728
3729 if(iterations < 0)
3730 {
3731 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003732 }
3733
3734 return iterations;
3735 }
3736 else UNIMPLEMENTED(); // Falls through
3737 }
3738
3739 return ~0u;
3740 }
3741
3742 bool LoopUnrollable::traverse(TIntermNode *node)
3743 {
3744 loopDepth = 0;
3745 loopUnrollable = true;
3746
3747 node->traverse(this);
3748
3749 return loopUnrollable;
3750 }
3751
3752 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3753 {
3754 if(visit == PreVisit)
3755 {
3756 loopDepth++;
3757 }
3758 else if(visit == PostVisit)
3759 {
3760 loopDepth++;
3761 }
3762
3763 return true;
3764 }
3765
3766 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3767 {
3768 if(!loopUnrollable)
3769 {
3770 return false;
3771 }
3772
3773 if(!loopDepth)
3774 {
3775 return true;
3776 }
3777
3778 switch(node->getFlowOp())
3779 {
3780 case EOpKill:
3781 case EOpReturn:
3782 break;
3783 case EOpBreak:
3784 case EOpContinue:
3785 loopUnrollable = false;
3786 break;
3787 default: UNREACHABLE(node->getFlowOp());
3788 }
3789
3790 return loopUnrollable;
3791 }
3792
3793 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3794 {
3795 return loopUnrollable;
3796 }
3797}