blob: 8b20758f14b50e10ac2cd3350cf92116f58976e4 [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
Alexis Hetu924513c2018-01-05 15:48:12 -050029namespace
30{
31 GLenum glVariableType(const TType &type)
32 {
33 switch(type.getBasicType())
34 {
35 case EbtFloat:
36 if(type.isScalar())
37 {
38 return GL_FLOAT;
39 }
40 else if(type.isVector())
41 {
42 switch(type.getNominalSize())
43 {
44 case 2: return GL_FLOAT_VEC2;
45 case 3: return GL_FLOAT_VEC3;
46 case 4: return GL_FLOAT_VEC4;
47 default: UNREACHABLE(type.getNominalSize());
48 }
49 }
50 else if(type.isMatrix())
51 {
52 switch(type.getNominalSize())
53 {
54 case 2:
55 switch(type.getSecondarySize())
56 {
57 case 2: return GL_FLOAT_MAT2;
58 case 3: return GL_FLOAT_MAT2x3;
59 case 4: return GL_FLOAT_MAT2x4;
60 default: UNREACHABLE(type.getSecondarySize());
61 }
62 case 3:
63 switch(type.getSecondarySize())
64 {
65 case 2: return GL_FLOAT_MAT3x2;
66 case 3: return GL_FLOAT_MAT3;
67 case 4: return GL_FLOAT_MAT3x4;
68 default: UNREACHABLE(type.getSecondarySize());
69 }
70 case 4:
71 switch(type.getSecondarySize())
72 {
73 case 2: return GL_FLOAT_MAT4x2;
74 case 3: return GL_FLOAT_MAT4x3;
75 case 4: return GL_FLOAT_MAT4;
76 default: UNREACHABLE(type.getSecondarySize());
77 }
78 default: UNREACHABLE(type.getNominalSize());
79 }
80 }
81 else UNREACHABLE(0);
82 break;
83 case EbtInt:
84 if(type.isScalar())
85 {
86 return GL_INT;
87 }
88 else if(type.isVector())
89 {
90 switch(type.getNominalSize())
91 {
92 case 2: return GL_INT_VEC2;
93 case 3: return GL_INT_VEC3;
94 case 4: return GL_INT_VEC4;
95 default: UNREACHABLE(type.getNominalSize());
96 }
97 }
98 else UNREACHABLE(0);
99 break;
100 case EbtUInt:
101 if(type.isScalar())
102 {
103 return GL_UNSIGNED_INT;
104 }
105 else if(type.isVector())
106 {
107 switch(type.getNominalSize())
108 {
109 case 2: return GL_UNSIGNED_INT_VEC2;
110 case 3: return GL_UNSIGNED_INT_VEC3;
111 case 4: return GL_UNSIGNED_INT_VEC4;
112 default: UNREACHABLE(type.getNominalSize());
113 }
114 }
115 else UNREACHABLE(0);
116 break;
117 case EbtBool:
118 if(type.isScalar())
119 {
120 return GL_BOOL;
121 }
122 else if(type.isVector())
123 {
124 switch(type.getNominalSize())
125 {
126 case 2: return GL_BOOL_VEC2;
127 case 3: return GL_BOOL_VEC3;
128 case 4: return GL_BOOL_VEC4;
129 default: UNREACHABLE(type.getNominalSize());
130 }
131 }
132 else UNREACHABLE(0);
133 break;
134 case EbtSampler2D:
135 return GL_SAMPLER_2D;
136 case EbtISampler2D:
137 return GL_INT_SAMPLER_2D;
138 case EbtUSampler2D:
139 return GL_UNSIGNED_INT_SAMPLER_2D;
140 case EbtSamplerCube:
141 return GL_SAMPLER_CUBE;
Alexis Hetu46768622018-01-16 22:09:28 -0500142 case EbtSampler2DRect:
143 return GL_SAMPLER_2D_RECT_ARB;
Alexis Hetu924513c2018-01-05 15:48:12 -0500144 case EbtISamplerCube:
145 return GL_INT_SAMPLER_CUBE;
146 case EbtUSamplerCube:
147 return GL_UNSIGNED_INT_SAMPLER_CUBE;
148 case EbtSamplerExternalOES:
149 return GL_SAMPLER_EXTERNAL_OES;
150 case EbtSampler3D:
151 return GL_SAMPLER_3D_OES;
152 case EbtISampler3D:
153 return GL_INT_SAMPLER_3D;
154 case EbtUSampler3D:
155 return GL_UNSIGNED_INT_SAMPLER_3D;
156 case EbtSampler2DArray:
157 return GL_SAMPLER_2D_ARRAY;
158 case EbtISampler2DArray:
159 return GL_INT_SAMPLER_2D_ARRAY;
160 case EbtUSampler2DArray:
161 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
162 case EbtSampler2DShadow:
163 return GL_SAMPLER_2D_SHADOW;
164 case EbtSamplerCubeShadow:
165 return GL_SAMPLER_CUBE_SHADOW;
166 case EbtSampler2DArrayShadow:
167 return GL_SAMPLER_2D_ARRAY_SHADOW;
168 default:
169 UNREACHABLE(type.getBasicType());
170 break;
171 }
172
173 return GL_NONE;
174 }
175
176 GLenum glVariablePrecision(const TType &type)
177 {
178 if(type.getBasicType() == EbtFloat)
179 {
180 switch(type.getPrecision())
181 {
182 case EbpHigh: return GL_HIGH_FLOAT;
183 case EbpMedium: return GL_MEDIUM_FLOAT;
184 case EbpLow: return GL_LOW_FLOAT;
185 case EbpUndefined:
186 // Should be defined as the default precision by the parser
187 default: UNREACHABLE(type.getPrecision());
188 }
189 }
190 else if(type.getBasicType() == EbtInt)
191 {
192 switch(type.getPrecision())
193 {
194 case EbpHigh: return GL_HIGH_INT;
195 case EbpMedium: return GL_MEDIUM_INT;
196 case EbpLow: return GL_LOW_INT;
197 case EbpUndefined:
198 // Should be defined as the default precision by the parser
199 default: UNREACHABLE(type.getPrecision());
200 }
201 }
202
203 // Other types (boolean, sampler) don't have a precision
204 return GL_NONE;
205 }
206}
207
Nicolas Capens0bac2852016-05-07 06:09:58 -0400208namespace glsl
209{
210 // Integer to TString conversion
211 TString str(int i)
212 {
213 char buffer[20];
214 sprintf(buffer, "%d", i);
215 return buffer;
216 }
217
218 class Temporary : public TIntermSymbol
219 {
220 public:
221 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
222 {
223 }
224
225 ~Temporary()
226 {
227 assembler->freeTemporary(this);
228 }
229
230 private:
231 OutputASM *const assembler;
232 };
233
234 class Constant : public TIntermConstantUnion
235 {
236 public:
237 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
238 {
239 constants[0].setFConst(x);
240 constants[1].setFConst(y);
241 constants[2].setFConst(z);
242 constants[3].setFConst(w);
243 }
244
245 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
246 {
247 constants[0].setBConst(b);
248 }
249
250 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
251 {
252 constants[0].setIConst(i);
253 }
254
255 ~Constant()
256 {
257 }
258
259 private:
260 ConstantUnion constants[4];
261 };
262
Alexis Hetu924513c2018-01-05 15:48:12 -0500263 ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
264 type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
265 name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
266 {
267 if(type.isStruct())
268 {
269 for(const auto& field : type.getStruct()->fields())
270 {
271 fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
272 }
273 }
274 }
275
276 Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
277 ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400278 {
279 }
280
281 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
282 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
283 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
284 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
285 {
286 }
287
Alexis Hetud2742532018-01-23 16:53:41 -0500288 BlockLayoutEncoder::BlockLayoutEncoder()
289 : mCurrentOffset(0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400290 {
291 }
292
293 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
294 {
295 int arrayStride;
296 int matrixStride;
297
Alexis Hetud2742532018-01-23 16:53:41 -0500298 bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
299 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400300
301 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
302 static_cast<int>(arrayStride * BytesPerComponent),
303 static_cast<int>(matrixStride * BytesPerComponent),
Alexis Hetud2742532018-01-23 16:53:41 -0500304 (matrixStride > 0) && isRowMajor);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400305
Alexis Hetud2742532018-01-23 16:53:41 -0500306 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400307
308 return memberInfo;
309 }
310
311 // static
312 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
313 {
314 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
315 }
316
317 // static
318 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
319 {
320 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
321 }
322
323 void BlockLayoutEncoder::nextRegister()
324 {
325 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
326 }
327
Alexis Hetud2742532018-01-23 16:53:41 -0500328 Std140BlockEncoder::Std140BlockEncoder() : BlockLayoutEncoder()
Nicolas Capens0bac2852016-05-07 06:09:58 -0400329 {
330 }
331
332 void Std140BlockEncoder::enterAggregateType()
333 {
334 nextRegister();
335 }
336
337 void Std140BlockEncoder::exitAggregateType()
338 {
339 nextRegister();
340 }
341
342 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
343 {
344 size_t baseAlignment = 0;
345 int matrixStride = 0;
346 int arrayStride = 0;
347
348 if(type.isMatrix())
349 {
350 baseAlignment = ComponentsPerRegister;
351 matrixStride = ComponentsPerRegister;
352
353 if(arraySize > 0)
354 {
355 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
356 arrayStride = ComponentsPerRegister * numRegisters;
357 }
358 }
359 else if(arraySize > 0)
360 {
361 baseAlignment = ComponentsPerRegister;
362 arrayStride = ComponentsPerRegister;
363 }
364 else
365 {
366 const size_t numComponents = type.getElementSize();
367 baseAlignment = (numComponents == 3 ? 4u : numComponents);
368 }
369
370 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
371
372 *matrixStrideOut = matrixStride;
373 *arrayStrideOut = arrayStride;
374 }
375
376 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
377 {
378 if(arraySize > 0)
379 {
380 mCurrentOffset += arrayStride * arraySize;
381 }
382 else if(type.isMatrix())
383 {
384 ASSERT(matrixStride == ComponentsPerRegister);
385 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
386 mCurrentOffset += ComponentsPerRegister * numRegisters;
387 }
388 else
389 {
390 mCurrentOffset += type.getElementSize();
391 }
392 }
393
394 Attribute::Attribute()
395 {
396 type = GL_NONE;
397 arraySize = 0;
398 registerIndex = 0;
399 }
400
401 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
402 {
403 this->type = type;
404 this->name = name;
405 this->arraySize = arraySize;
406 this->location = location;
407 this->registerIndex = registerIndex;
408 }
409
410 sw::PixelShader *Shader::getPixelShader() const
411 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500412 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400413 }
414
415 sw::VertexShader *Shader::getVertexShader() const
416 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500417 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400418 }
419
420 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
421 {
422 TString name = TFunction::unmangleName(nodeName);
423
Alexis Hetuc0632c92018-03-02 15:10:16 -0500424 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D" || name == "texture2DRect")
Nicolas Capens0bac2852016-05-07 06:09:58 -0400425 {
426 method = IMPLICIT;
427 }
Alexis Hetuc0632c92018-03-02 15:10:16 -0500428 else if(name == "texture2DProj" || name == "textureProj" || name == "texture2DRectProj")
Nicolas Capens0bac2852016-05-07 06:09:58 -0400429 {
430 method = IMPLICIT;
431 proj = true;
432 }
433 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
434 {
435 method = LOD;
436 }
437 else if(name == "texture2DProjLod" || name == "textureProjLod")
438 {
439 method = LOD;
440 proj = true;
441 }
442 else if(name == "textureSize")
443 {
444 method = SIZE;
445 }
446 else if(name == "textureOffset")
447 {
448 method = IMPLICIT;
449 offset = true;
450 }
451 else if(name == "textureProjOffset")
452 {
453 method = IMPLICIT;
454 offset = true;
455 proj = true;
456 }
457 else if(name == "textureLodOffset")
458 {
459 method = LOD;
460 offset = true;
461 }
462 else if(name == "textureProjLodOffset")
463 {
464 method = LOD;
465 proj = true;
466 offset = true;
467 }
468 else if(name == "texelFetch")
469 {
470 method = FETCH;
471 }
472 else if(name == "texelFetchOffset")
473 {
474 method = FETCH;
475 offset = true;
476 }
477 else if(name == "textureGrad")
478 {
479 method = GRAD;
480 }
481 else if(name == "textureGradOffset")
482 {
483 method = GRAD;
484 offset = true;
485 }
486 else if(name == "textureProjGrad")
487 {
488 method = GRAD;
489 proj = true;
490 }
491 else if(name == "textureProjGradOffset")
492 {
493 method = GRAD;
494 proj = true;
495 offset = true;
496 }
497 else UNREACHABLE(0);
498 }
499
500 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
501 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500502 shader = nullptr;
503 pixelShader = nullptr;
504 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400505
506 if(shaderObject)
507 {
508 shader = shaderObject->getShader();
509 pixelShader = shaderObject->getPixelShader();
510 vertexShader = shaderObject->getVertexShader();
511 }
512
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500513 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400514 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500515 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400516 }
517
518 OutputASM::~OutputASM()
519 {
520 }
521
522 void OutputASM::output()
523 {
524 if(shader)
525 {
526 emitShader(GLOBAL);
527
528 if(functionArray.size() > 1) // Only call main() when there are other functions
529 {
530 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
531 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
532 callMain->dst.index = 0; // main()
533
534 emit(sw::Shader::OPCODE_RET);
535 }
536
537 emitShader(FUNCTION);
538 }
539 }
540
541 void OutputASM::emitShader(Scope scope)
542 {
543 emitScope = scope;
544 currentScope = GLOBAL;
545 mContext.getTreeRoot()->traverse(this);
546 }
547
548 void OutputASM::freeTemporary(Temporary *temporary)
549 {
550 free(temporaries, temporary);
551 }
552
553 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
554 {
555 TBasicType baseType = in->getType().getBasicType();
556
557 switch(op)
558 {
559 case sw::Shader::OPCODE_NEG:
560 switch(baseType)
561 {
562 case EbtInt:
563 case EbtUInt:
564 return sw::Shader::OPCODE_INEG;
565 case EbtFloat:
566 default:
567 return op;
568 }
569 case sw::Shader::OPCODE_ABS:
570 switch(baseType)
571 {
572 case EbtInt:
573 return sw::Shader::OPCODE_IABS;
574 case EbtFloat:
575 default:
576 return op;
577 }
578 case sw::Shader::OPCODE_SGN:
579 switch(baseType)
580 {
581 case EbtInt:
582 return sw::Shader::OPCODE_ISGN;
583 case EbtFloat:
584 default:
585 return op;
586 }
587 case sw::Shader::OPCODE_ADD:
588 switch(baseType)
589 {
590 case EbtInt:
591 case EbtUInt:
592 return sw::Shader::OPCODE_IADD;
593 case EbtFloat:
594 default:
595 return op;
596 }
597 case sw::Shader::OPCODE_SUB:
598 switch(baseType)
599 {
600 case EbtInt:
601 case EbtUInt:
602 return sw::Shader::OPCODE_ISUB;
603 case EbtFloat:
604 default:
605 return op;
606 }
607 case sw::Shader::OPCODE_MUL:
608 switch(baseType)
609 {
610 case EbtInt:
611 case EbtUInt:
612 return sw::Shader::OPCODE_IMUL;
613 case EbtFloat:
614 default:
615 return op;
616 }
617 case sw::Shader::OPCODE_DIV:
618 switch(baseType)
619 {
620 case EbtInt:
621 return sw::Shader::OPCODE_IDIV;
622 case EbtUInt:
623 return sw::Shader::OPCODE_UDIV;
624 case EbtFloat:
625 default:
626 return op;
627 }
628 case sw::Shader::OPCODE_IMOD:
629 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
630 case sw::Shader::OPCODE_ISHR:
631 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
632 case sw::Shader::OPCODE_MIN:
633 switch(baseType)
634 {
635 case EbtInt:
636 return sw::Shader::OPCODE_IMIN;
637 case EbtUInt:
638 return sw::Shader::OPCODE_UMIN;
639 case EbtFloat:
640 default:
641 return op;
642 }
643 case sw::Shader::OPCODE_MAX:
644 switch(baseType)
645 {
646 case EbtInt:
647 return sw::Shader::OPCODE_IMAX;
648 case EbtUInt:
649 return sw::Shader::OPCODE_UMAX;
650 case EbtFloat:
651 default:
652 return op;
653 }
654 default:
655 return op;
656 }
657 }
658
659 void OutputASM::visitSymbol(TIntermSymbol *symbol)
660 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500661 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
662 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
663 switch(symbol->getQualifier())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400664 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500665 case EvqVaryingIn:
666 case EvqVaryingOut:
667 case EvqInvariantVaryingIn:
668 case EvqInvariantVaryingOut:
669 case EvqVertexOut:
670 case EvqFragmentIn:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400671 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
672 {
673 declareVarying(symbol, -1);
674 }
Nicolas Capens6896e352018-01-10 12:46:52 -0500675 break;
Alexis Hetu930df972018-01-30 16:54:13 -0500676 case EvqFragmentOut:
677 declareFragmentOutput(symbol);
678 break;
Nicolas Capens6896e352018-01-10 12:46:52 -0500679 default:
680 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400681 }
682
683 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
684 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
685 // "All members of a named uniform block declared with a shared or std140 layout qualifier
686 // are considered active, even if they are not referenced in any shader in the program.
687 // The uniform block itself is also considered active, even if no member of the block is referenced."
688 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
689 {
690 uniformRegister(symbol);
691 }
692 }
693
694 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
695 {
696 if(currentScope != emitScope)
697 {
698 return false;
699 }
700
701 TIntermTyped *result = node;
702 TIntermTyped *left = node->getLeft();
703 TIntermTyped *right = node->getRight();
704 const TType &leftType = left->getType();
705 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400706
707 if(isSamplerRegister(result))
708 {
709 return false; // Don't traverse, the register index is determined statically
710 }
711
712 switch(node->getOp())
713 {
714 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500715 assert(visit == PreVisit);
716 right->traverse(this);
717 assignLvalue(left, right);
718 copy(result, right);
719 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500721 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500722 // Constant arrays go into the constant register file.
723 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
724 {
725 for(int i = 0; i < left->totalRegisterCount(); i++)
726 {
727 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
728 }
729 }
730 else
731 {
732 right->traverse(this);
733 copy(left, right);
734 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500735 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400736 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500737 assert(visit == PreVisit);
738 right->traverse(this);
739 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400740 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500741 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400742 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500743
744 assignLvalue(left, result);
745 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400746 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500747 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400748 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500749 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400750 int size = leftType.getNominalSize();
751
752 for(int i = 0; i < size; i++)
753 {
754 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
755 dot->dst.mask = 1 << i;
756 }
757
758 assignLvalue(left, result);
759 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500760 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400761 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500762 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400763 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500764 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400765 int dim = leftType.getNominalSize();
766
767 for(int i = 0; i < dim; i++)
768 {
769 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
770 mul->src[1].swizzle = 0x00;
771
772 for(int j = 1; j < dim; j++)
773 {
774 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
775 mad->src[1].swizzle = j * 0x55;
776 }
777 }
778
779 assignLvalue(left, result);
780 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500781 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400782 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400783 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400784 case EOpIndexDirectStruct:
785 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500786 assert(visit == PreVisit);
787 evaluateRvalue(node);
788 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400789 case EOpVectorSwizzle:
790 if(visit == PostVisit)
791 {
792 int swizzle = 0;
793 TIntermAggregate *components = right->getAsAggregate();
794
795 if(components)
796 {
797 TIntermSequence &sequence = components->getSequence();
798 int component = 0;
799
800 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
801 {
802 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
803
804 if(element)
805 {
806 int i = element->getUnionArrayPointer()[0].getIConst();
807 swizzle |= i << (component * 2);
808 component++;
809 }
810 else UNREACHABLE(0);
811 }
812 }
813 else UNREACHABLE(0);
814
815 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
816 mov->src[0].swizzle = swizzle;
817 }
818 break;
819 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
820 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
821 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
822 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
823 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
824 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
825 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
826 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
827 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
828 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
829 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
830 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
831 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
832 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
833 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
834 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
835 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
836 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
837 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
838 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
839 case EOpEqual:
840 if(visit == PostVisit)
841 {
842 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
843
844 for(int index = 1; index < left->totalRegisterCount(); index++)
845 {
846 Temporary equal(this);
847 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
848 emit(sw::Shader::OPCODE_AND, result, result, &equal);
849 }
850 }
851 break;
852 case EOpNotEqual:
853 if(visit == PostVisit)
854 {
855 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
856
857 for(int index = 1; index < left->totalRegisterCount(); index++)
858 {
859 Temporary notEqual(this);
860 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
861 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
862 }
863 }
864 break;
865 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
866 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
867 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
868 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
869 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
870 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
871 case EOpMatrixTimesScalar:
872 if(visit == PostVisit)
873 {
874 if(left->isMatrix())
875 {
876 for(int i = 0; i < leftType.getNominalSize(); i++)
877 {
878 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
879 }
880 }
881 else if(right->isMatrix())
882 {
883 for(int i = 0; i < rightType.getNominalSize(); i++)
884 {
885 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
886 }
887 }
888 else UNREACHABLE(0);
889 }
890 break;
891 case EOpVectorTimesMatrix:
892 if(visit == PostVisit)
893 {
894 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
895
896 int size = rightType.getNominalSize();
897 for(int i = 0; i < size; i++)
898 {
899 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
900 dot->dst.mask = 1 << i;
901 }
902 }
903 break;
904 case EOpMatrixTimesVector:
905 if(visit == PostVisit)
906 {
907 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
908 mul->src[1].swizzle = 0x00;
909
910 int size = rightType.getNominalSize();
911 for(int i = 1; i < size; i++)
912 {
913 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
914 mad->src[1].swizzle = i * 0x55;
915 }
916 }
917 break;
918 case EOpMatrixTimesMatrix:
919 if(visit == PostVisit)
920 {
921 int dim = leftType.getNominalSize();
922
923 int size = rightType.getNominalSize();
924 for(int i = 0; i < size; i++)
925 {
926 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
927 mul->src[1].swizzle = 0x00;
928
929 for(int j = 1; j < dim; j++)
930 {
931 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
932 mad->src[1].swizzle = j * 0x55;
933 }
934 }
935 }
936 break;
937 case EOpLogicalOr:
938 if(trivial(right, 6))
939 {
940 if(visit == PostVisit)
941 {
942 emit(sw::Shader::OPCODE_OR, result, left, right);
943 }
944 }
945 else // Short-circuit evaluation
946 {
947 if(visit == InVisit)
948 {
949 emit(sw::Shader::OPCODE_MOV, result, left);
950 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
951 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
952 }
953 else if(visit == PostVisit)
954 {
955 emit(sw::Shader::OPCODE_MOV, result, right);
956 emit(sw::Shader::OPCODE_ENDIF);
957 }
958 }
959 break;
960 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
961 case EOpLogicalAnd:
962 if(trivial(right, 6))
963 {
964 if(visit == PostVisit)
965 {
966 emit(sw::Shader::OPCODE_AND, result, left, right);
967 }
968 }
969 else // Short-circuit evaluation
970 {
971 if(visit == InVisit)
972 {
973 emit(sw::Shader::OPCODE_MOV, result, left);
974 emit(sw::Shader::OPCODE_IF, 0, result);
975 }
976 else if(visit == PostVisit)
977 {
978 emit(sw::Shader::OPCODE_MOV, result, right);
979 emit(sw::Shader::OPCODE_ENDIF);
980 }
981 }
982 break;
983 default: UNREACHABLE(node->getOp());
984 }
985
986 return true;
987 }
988
989 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
990 {
991 switch(size)
992 {
993 case 1: // Used for cofactor computation only
994 {
995 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
996 bool isMov = (row == col);
997 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
998 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
999 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1000 mov->dst.mask = 1 << outRow;
1001 }
1002 break;
1003 case 2:
1004 {
1005 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1006
1007 bool isCofactor = (col >= 0) && (row >= 0);
1008 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1009 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1010 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1011
1012 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1013 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1014 det->dst.mask = 1 << outRow;
1015 }
1016 break;
1017 case 3:
1018 {
1019 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1020
1021 bool isCofactor = (col >= 0) && (row >= 0);
1022 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1023 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1024 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1025 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1026
1027 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1028 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1029 det->dst.mask = 1 << outRow;
1030 }
1031 break;
1032 case 4:
1033 {
1034 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1035 det->dst.mask = 1 << outRow;
1036 }
1037 break;
1038 default:
1039 UNREACHABLE(size);
1040 break;
1041 }
1042 }
1043
1044 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1045 {
1046 if(currentScope != emitScope)
1047 {
1048 return false;
1049 }
1050
1051 TIntermTyped *result = node;
1052 TIntermTyped *arg = node->getOperand();
1053 TBasicType basicType = arg->getType().getBasicType();
1054
1055 union
1056 {
1057 float f;
1058 int i;
1059 } one_value;
1060
1061 if(basicType == EbtInt || basicType == EbtUInt)
1062 {
1063 one_value.i = 1;
1064 }
1065 else
1066 {
1067 one_value.f = 1.0f;
1068 }
1069
1070 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1071 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1072 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1073
1074 switch(node->getOp())
1075 {
1076 case EOpNegative:
1077 if(visit == PostVisit)
1078 {
1079 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1080 for(int index = 0; index < arg->totalRegisterCount(); index++)
1081 {
1082 emit(negOpcode, result, index, arg, index);
1083 }
1084 }
1085 break;
1086 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1087 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001088 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001089 case EOpPostIncrement:
1090 if(visit == PostVisit)
1091 {
1092 copy(result, arg);
1093
1094 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1095 for(int index = 0; index < arg->totalRegisterCount(); index++)
1096 {
1097 emit(addOpcode, arg, index, arg, index, &one);
1098 }
1099
1100 assignLvalue(arg, arg);
1101 }
1102 break;
1103 case EOpPostDecrement:
1104 if(visit == PostVisit)
1105 {
1106 copy(result, arg);
1107
1108 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1109 for(int index = 0; index < arg->totalRegisterCount(); index++)
1110 {
1111 emit(subOpcode, arg, index, arg, index, &one);
1112 }
1113
1114 assignLvalue(arg, arg);
1115 }
1116 break;
1117 case EOpPreIncrement:
1118 if(visit == PostVisit)
1119 {
1120 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1121 for(int index = 0; index < arg->totalRegisterCount(); index++)
1122 {
1123 emit(addOpcode, result, index, arg, index, &one);
1124 }
1125
1126 assignLvalue(arg, result);
1127 }
1128 break;
1129 case EOpPreDecrement:
1130 if(visit == PostVisit)
1131 {
1132 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1133 for(int index = 0; index < arg->totalRegisterCount(); index++)
1134 {
1135 emit(subOpcode, result, index, arg, index, &one);
1136 }
1137
1138 assignLvalue(arg, result);
1139 }
1140 break;
1141 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1142 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1143 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1144 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1145 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1146 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1147 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1148 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1149 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1150 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1151 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1152 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1153 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1154 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1155 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1156 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1157 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1158 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1159 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1160 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1161 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1162 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1163 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1164 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1165 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1166 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1167 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1168 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1169 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1170 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1171 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1172 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1173 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1174 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1175 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1176 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1177 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1178 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1179 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1180 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1181 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1182 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1183 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1184 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1185 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1186 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1187 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1188 case EOpTranspose:
1189 if(visit == PostVisit)
1190 {
1191 int numCols = arg->getNominalSize();
1192 int numRows = arg->getSecondarySize();
1193 for(int i = 0; i < numCols; ++i)
1194 {
1195 for(int j = 0; j < numRows; ++j)
1196 {
1197 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1198 mov->src[0].swizzle = 0x55 * j;
1199 mov->dst.mask = 1 << i;
1200 }
1201 }
1202 }
1203 break;
1204 case EOpDeterminant:
1205 if(visit == PostVisit)
1206 {
1207 int size = arg->getNominalSize();
1208 ASSERT(size == arg->getSecondarySize());
1209
1210 emitDeterminant(result, arg, size);
1211 }
1212 break;
1213 case EOpInverse:
1214 if(visit == PostVisit)
1215 {
1216 int size = arg->getNominalSize();
1217 ASSERT(size == arg->getSecondarySize());
1218
1219 // Compute transposed matrix of cofactors
1220 for(int i = 0; i < size; ++i)
1221 {
1222 for(int j = 0; j < size; ++j)
1223 {
1224 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1225 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1226 emitDeterminant(result, arg, size - 1, j, i, i, j);
1227 }
1228 }
1229
1230 // Compute 1 / determinant
1231 Temporary invDet(this);
1232 emitDeterminant(&invDet, arg, size);
1233 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1234 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1235 div->src[1].swizzle = 0x00; // xxxx
1236
1237 // Divide transposed matrix of cofactors by determinant
1238 for(int i = 0; i < size; ++i)
1239 {
1240 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1241 }
1242 }
1243 break;
1244 default: UNREACHABLE(node->getOp());
1245 }
1246
1247 return true;
1248 }
1249
1250 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1251 {
1252 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1253 {
1254 return false;
1255 }
1256
1257 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1258
1259 TIntermTyped *result = node;
1260 const TType &resultType = node->getType();
1261 TIntermSequence &arg = node->getSequence();
1262 size_t argumentCount = arg.size();
1263
1264 switch(node->getOp())
1265 {
1266 case EOpSequence: break;
1267 case EOpDeclaration: break;
1268 case EOpInvariantDeclaration: break;
1269 case EOpPrototype: break;
1270 case EOpComma:
1271 if(visit == PostVisit)
1272 {
1273 copy(result, arg[1]);
1274 }
1275 break;
1276 case EOpFunction:
1277 if(visit == PreVisit)
1278 {
1279 const TString &name = node->getName();
1280
1281 if(emitScope == FUNCTION)
1282 {
1283 if(functionArray.size() > 1) // No need for a label when there's only main()
1284 {
1285 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1286 label->dst.type = sw::Shader::PARAMETER_LABEL;
1287
1288 const Function *function = findFunction(name);
1289 ASSERT(function); // Should have been added during global pass
1290 label->dst.index = function->label;
1291 currentFunction = function->label;
1292 }
1293 }
1294 else if(emitScope == GLOBAL)
1295 {
1296 if(name != "main(")
1297 {
1298 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1299 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1300 }
1301 }
1302 else UNREACHABLE(emitScope);
1303
1304 currentScope = FUNCTION;
1305 }
1306 else if(visit == PostVisit)
1307 {
1308 if(emitScope == FUNCTION)
1309 {
1310 if(functionArray.size() > 1) // No need to return when there's only main()
1311 {
1312 emit(sw::Shader::OPCODE_RET);
1313 }
1314 }
1315
1316 currentScope = GLOBAL;
1317 }
1318 break;
1319 case EOpFunctionCall:
1320 if(visit == PostVisit)
1321 {
1322 if(node->isUserDefined())
1323 {
1324 const TString &name = node->getName();
1325 const Function *function = findFunction(name);
1326
1327 if(!function)
1328 {
1329 mContext.error(node->getLine(), "function definition not found", name.c_str());
1330 return false;
1331 }
1332
1333 TIntermSequence &arguments = *function->arg;
1334
1335 for(size_t i = 0; i < argumentCount; i++)
1336 {
1337 TIntermTyped *in = arguments[i]->getAsTyped();
1338
1339 if(in->getQualifier() == EvqIn ||
1340 in->getQualifier() == EvqInOut ||
1341 in->getQualifier() == EvqConstReadOnly)
1342 {
1343 copy(in, arg[i]);
1344 }
1345 }
1346
1347 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1348 call->dst.type = sw::Shader::PARAMETER_LABEL;
1349 call->dst.index = function->label;
1350
1351 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1352 {
1353 copy(result, function->ret);
1354 }
1355
1356 for(size_t i = 0; i < argumentCount; i++)
1357 {
1358 TIntermTyped *argument = arguments[i]->getAsTyped();
1359 TIntermTyped *out = arg[i]->getAsTyped();
1360
1361 if(argument->getQualifier() == EvqOut ||
1362 argument->getQualifier() == EvqInOut)
1363 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001364 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001365 }
1366 }
1367 }
1368 else
1369 {
1370 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001371 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001372 TIntermTyped *t = arg[1]->getAsTyped();
1373
1374 Temporary coord(this);
1375
1376 if(textureFunction.proj)
1377 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001378 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1379 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1380 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001381
Nicolas Capens0484c792016-06-13 22:02:36 -04001382 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1383 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001384
1385 if(IsShadowSampler(s->getBasicType()))
1386 {
1387 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1388 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1389 mov->src[0].swizzle = 0xA4;
1390 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001391 }
1392 else
1393 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001394 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1395
1396 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1397 {
1398 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1399 mov->src[0].swizzle = 0xA4;
1400 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001401 }
1402
1403 switch(textureFunction.method)
1404 {
1405 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001406 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001407 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001408 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001409 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001410 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001411 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001412 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001413 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001414 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1415 }
1416 else UNREACHABLE(argumentCount);
1417 }
1418 else // Offset
1419 {
1420 if(argumentCount == 3)
1421 {
1422 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1423 }
1424 else if(argumentCount == 4) // Bias
1425 {
1426 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001427 }
1428 else UNREACHABLE(argumentCount);
1429 }
1430 break;
1431 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001432 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001433 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001434 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001435 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001436 else if(argumentCount == 4) // Offset
1437 {
1438 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1439 }
1440 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001441 break;
1442 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001443 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001444 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001445 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001446 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001447 else if(argumentCount == 4) // Offset
1448 {
1449 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1450 }
1451 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001452 break;
1453 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001454 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001455 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001456 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001457 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001458 else if(argumentCount == 5) // Offset
1459 {
1460 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1461 }
1462 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001463 break;
1464 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001465 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001466 break;
1467 default:
1468 UNREACHABLE(textureFunction.method);
1469 }
1470 }
1471 }
1472 break;
1473 case EOpParameters:
1474 break;
1475 case EOpConstructFloat:
1476 case EOpConstructVec2:
1477 case EOpConstructVec3:
1478 case EOpConstructVec4:
1479 case EOpConstructBool:
1480 case EOpConstructBVec2:
1481 case EOpConstructBVec3:
1482 case EOpConstructBVec4:
1483 case EOpConstructInt:
1484 case EOpConstructIVec2:
1485 case EOpConstructIVec3:
1486 case EOpConstructIVec4:
1487 case EOpConstructUInt:
1488 case EOpConstructUVec2:
1489 case EOpConstructUVec3:
1490 case EOpConstructUVec4:
1491 if(visit == PostVisit)
1492 {
1493 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001494 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1495 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001496 for(size_t i = 0; i < argumentCount; i++)
1497 {
1498 TIntermTyped *argi = arg[i]->getAsTyped();
1499 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001500 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1501 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001502
1503 if(!argi->isMatrix())
1504 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001505 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1506 mov->dst.mask = (0xF << swizzle) & 0xF;
1507 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001508
1509 component += size;
1510 }
Alexis Hetu38338612018-01-18 15:53:36 -05001511 else if(!result->isMatrix()) // Construct a non matrix from a matrix
1512 {
1513 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1514 mov->dst.mask = (0xF << swizzle) & 0xF;
1515 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1516
1517 // At most one more instruction when constructing a vec3 from a mat2 or a vec4 from a mat2/mat3
1518 if(result->getNominalSize() > size)
1519 {
1520 Instruction *mov = emitCast(result, arrayIndex, argi, 1);
1521 mov->dst.mask = (0xF << (swizzle + size)) & 0xF;
1522 // mat2: xxxy (0x40), mat3: xxxx (0x00)
1523 mov->src[0].swizzle = ((size == 2) ? 0x40 : 0x00) << (swizzle * 2);
1524 }
1525
1526 component += size;
1527 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001528 else // Matrix
1529 {
1530 int column = 0;
1531
1532 while(component < resultType.getNominalSize())
1533 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001534 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1535 mov->dst.mask = (0xF << swizzle) & 0xF;
1536 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001537
1538 column++;
1539 component += size;
1540 }
1541 }
1542 }
1543 }
1544 break;
1545 case EOpConstructMat2:
1546 case EOpConstructMat2x3:
1547 case EOpConstructMat2x4:
1548 case EOpConstructMat3x2:
1549 case EOpConstructMat3:
1550 case EOpConstructMat3x4:
1551 case EOpConstructMat4x2:
1552 case EOpConstructMat4x3:
1553 case EOpConstructMat4:
1554 if(visit == PostVisit)
1555 {
1556 TIntermTyped *arg0 = arg[0]->getAsTyped();
1557 const int outCols = result->getNominalSize();
1558 const int outRows = result->getSecondarySize();
1559
1560 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1561 {
1562 for(int i = 0; i < outCols; i++)
1563 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001564 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001565 Instruction *mov = emitCast(result, i, arg0, 0);
1566 mov->dst.mask = 1 << i;
1567 ASSERT(mov->src[0].swizzle == 0x00);
1568 }
1569 }
1570 else if(arg0->isMatrix())
1571 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001572 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001573
Alexis Hetu2a198552016-09-27 20:50:45 -04001574 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001575 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001576 TIntermTyped *argi = arg[n]->getAsTyped();
1577 const int inCols = argi->getNominalSize();
1578 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001579
Alexis Hetu2a198552016-09-27 20:50:45 -04001580 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001581 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001582 if(i >= inCols || outRows > inRows)
1583 {
1584 // Initialize to identity matrix
1585 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));
1586 emitCast(result, i + n * outCols, &col, 0);
1587 }
1588
1589 if(i < inCols)
1590 {
1591 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1592 mov->dst.mask = 0xF >> (4 - inRows);
1593 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001594 }
1595 }
1596 }
1597 else
1598 {
1599 int column = 0;
1600 int row = 0;
1601
1602 for(size_t i = 0; i < argumentCount; i++)
1603 {
1604 TIntermTyped *argi = arg[i]->getAsTyped();
1605 int size = argi->getNominalSize();
1606 int element = 0;
1607
1608 while(element < size)
1609 {
1610 Instruction *mov = emitCast(result, column, argi, 0);
1611 mov->dst.mask = (0xF << row) & 0xF;
1612 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1613
1614 int end = row + size - element;
1615 column = end >= outRows ? column + 1 : column;
1616 element = element + outRows - row;
1617 row = end >= outRows ? 0 : end;
1618 }
1619 }
1620 }
1621 }
1622 break;
1623 case EOpConstructStruct:
1624 if(visit == PostVisit)
1625 {
1626 int offset = 0;
1627 for(size_t i = 0; i < argumentCount; i++)
1628 {
1629 TIntermTyped *argi = arg[i]->getAsTyped();
1630 int size = argi->totalRegisterCount();
1631
1632 for(int index = 0; index < size; index++)
1633 {
1634 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1635 mov->dst.mask = writeMask(result, offset + index);
1636 }
1637
1638 offset += size;
1639 }
1640 }
1641 break;
1642 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1643 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1644 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1645 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1646 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1647 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1648 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1649 case EOpModf:
1650 if(visit == PostVisit)
1651 {
1652 TIntermTyped* arg1 = arg[1]->getAsTyped();
1653 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1654 assignLvalue(arg1, arg1);
1655 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1656 }
1657 break;
1658 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1659 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1660 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1661 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1662 case EOpClamp:
1663 if(visit == PostVisit)
1664 {
1665 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1666 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1667 }
1668 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001669 case EOpMix:
1670 if(visit == PostVisit)
1671 {
1672 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1673 {
1674 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1675 }
1676 else
1677 {
1678 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1679 }
1680 }
1681 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001682 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1683 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1684 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1685 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1686 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1687 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1688 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1689 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1690 case EOpMul:
1691 if(visit == PostVisit)
1692 {
1693 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001694 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1695 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001696
1697 int size = arg0->getNominalSize();
1698 for(int i = 0; i < size; i++)
1699 {
1700 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1701 }
1702 }
1703 break;
1704 case EOpOuterProduct:
1705 if(visit == PostVisit)
1706 {
1707 for(int i = 0; i < dim(arg[1]); i++)
1708 {
1709 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1710 mul->src[1].swizzle = 0x55 * i;
1711 }
1712 }
1713 break;
1714 default: UNREACHABLE(node->getOp());
1715 }
1716
1717 return true;
1718 }
1719
1720 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1721 {
1722 if(currentScope != emitScope)
1723 {
1724 return false;
1725 }
1726
1727 TIntermTyped *condition = node->getCondition();
1728 TIntermNode *trueBlock = node->getTrueBlock();
1729 TIntermNode *falseBlock = node->getFalseBlock();
1730 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1731
1732 condition->traverse(this);
1733
1734 if(node->usesTernaryOperator())
1735 {
1736 if(constantCondition)
1737 {
1738 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1739
1740 if(trueCondition)
1741 {
1742 trueBlock->traverse(this);
1743 copy(node, trueBlock);
1744 }
1745 else
1746 {
1747 falseBlock->traverse(this);
1748 copy(node, falseBlock);
1749 }
1750 }
1751 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1752 {
1753 trueBlock->traverse(this);
1754 falseBlock->traverse(this);
1755 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1756 }
1757 else
1758 {
1759 emit(sw::Shader::OPCODE_IF, 0, condition);
1760
1761 if(trueBlock)
1762 {
1763 trueBlock->traverse(this);
1764 copy(node, trueBlock);
1765 }
1766
1767 if(falseBlock)
1768 {
1769 emit(sw::Shader::OPCODE_ELSE);
1770 falseBlock->traverse(this);
1771 copy(node, falseBlock);
1772 }
1773
1774 emit(sw::Shader::OPCODE_ENDIF);
1775 }
1776 }
1777 else // if/else statement
1778 {
1779 if(constantCondition)
1780 {
1781 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1782
1783 if(trueCondition)
1784 {
1785 if(trueBlock)
1786 {
1787 trueBlock->traverse(this);
1788 }
1789 }
1790 else
1791 {
1792 if(falseBlock)
1793 {
1794 falseBlock->traverse(this);
1795 }
1796 }
1797 }
1798 else
1799 {
1800 emit(sw::Shader::OPCODE_IF, 0, condition);
1801
1802 if(trueBlock)
1803 {
1804 trueBlock->traverse(this);
1805 }
1806
1807 if(falseBlock)
1808 {
1809 emit(sw::Shader::OPCODE_ELSE);
1810 falseBlock->traverse(this);
1811 }
1812
1813 emit(sw::Shader::OPCODE_ENDIF);
1814 }
1815 }
1816
1817 return false;
1818 }
1819
1820 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1821 {
1822 if(currentScope != emitScope)
1823 {
1824 return false;
1825 }
1826
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001827 LoopInfo loop(node);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001828
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001829 if(loop.iterations == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001830 {
1831 return false;
1832 }
1833
Nicolas Capens4b743732018-05-28 13:22:07 -04001834 if(loop.isDeterministic())
1835 {
1836 deterministicVariables.insert(loop.index->getId());
1837 }
1838
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001839 bool unroll = (loop.iterations <= 4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001840
1841 TIntermNode *init = node->getInit();
1842 TIntermTyped *condition = node->getCondition();
1843 TIntermTyped *expression = node->getExpression();
1844 TIntermNode *body = node->getBody();
1845 Constant True(true);
1846
1847 if(node->getType() == ELoopDoWhile)
1848 {
1849 Temporary iterate(this);
1850 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1851
1852 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1853
1854 if(body)
1855 {
1856 body->traverse(this);
1857 }
1858
1859 emit(sw::Shader::OPCODE_TEST);
1860
1861 condition->traverse(this);
1862 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1863
1864 emit(sw::Shader::OPCODE_ENDWHILE);
1865 }
1866 else
1867 {
1868 if(init)
1869 {
1870 init->traverse(this);
1871 }
1872
1873 if(unroll)
1874 {
Nicolas Capens493fc542018-05-29 17:11:37 -04001875 mContext.info(node->getLine(), "loop unrolled", "for");
1876
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001877 for(unsigned int i = 0; i < loop.iterations; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001878 {
1879 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1880
1881 if(body)
1882 {
1883 body->traverse(this);
1884 }
1885
1886 if(expression)
1887 {
1888 expression->traverse(this);
1889 }
1890 }
1891 }
1892 else
1893 {
1894 if(condition)
1895 {
1896 condition->traverse(this);
1897 }
1898 else
1899 {
1900 condition = &True;
1901 }
1902
1903 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1904
1905 if(body)
1906 {
1907 body->traverse(this);
1908 }
1909
1910 emit(sw::Shader::OPCODE_TEST);
1911
1912 if(expression)
1913 {
1914 expression->traverse(this);
1915 }
1916
1917 if(condition)
1918 {
1919 condition->traverse(this);
1920 }
1921
1922 emit(sw::Shader::OPCODE_ENDWHILE);
1923 }
1924 }
1925
Nicolas Capens4b743732018-05-28 13:22:07 -04001926 if(loop.isDeterministic())
1927 {
1928 deterministicVariables.erase(loop.index->getId());
1929 }
1930
Nicolas Capens0bac2852016-05-07 06:09:58 -04001931 return false;
1932 }
1933
1934 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1935 {
1936 if(currentScope != emitScope)
1937 {
1938 return false;
1939 }
1940
1941 switch(node->getFlowOp())
1942 {
1943 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1944 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1945 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1946 case EOpReturn:
1947 if(visit == PostVisit)
1948 {
1949 TIntermTyped *value = node->getExpression();
1950
1951 if(value)
1952 {
1953 copy(functionArray[currentFunction].ret, value);
1954 }
1955
1956 emit(sw::Shader::OPCODE_LEAVE);
1957 }
1958 break;
1959 default: UNREACHABLE(node->getFlowOp());
1960 }
1961
1962 return true;
1963 }
1964
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001965 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1966 {
1967 if(currentScope != emitScope)
1968 {
1969 return false;
1970 }
1971
1972 TIntermTyped* switchValue = node->getInit();
1973 TIntermAggregate* opList = node->getStatementList();
1974
1975 if(!switchValue || !opList)
1976 {
1977 return false;
1978 }
1979
1980 switchValue->traverse(this);
1981
1982 emit(sw::Shader::OPCODE_SWITCH);
1983
1984 TIntermSequence& sequence = opList->getSequence();
1985 TIntermSequence::iterator it = sequence.begin();
1986 TIntermSequence::iterator defaultIt = sequence.end();
1987 int nbCases = 0;
1988 for(; it != sequence.end(); ++it)
1989 {
1990 TIntermCase* currentCase = (*it)->getAsCaseNode();
1991 if(currentCase)
1992 {
1993 TIntermSequence::iterator caseIt = it;
1994
1995 TIntermTyped* condition = currentCase->getCondition();
1996 if(condition) // non default case
1997 {
1998 if(nbCases != 0)
1999 {
2000 emit(sw::Shader::OPCODE_ELSE);
2001 }
2002
2003 condition->traverse(this);
2004 Temporary result(this);
2005 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
2006 emit(sw::Shader::OPCODE_IF, 0, &result);
2007 nbCases++;
2008
Nicolas Capens6d123312018-01-08 12:57:52 -05002009 // Emit the code for this case and all subsequent cases until we hit a break statement.
2010 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002011 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2012 {
2013 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05002014
2015 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2016 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2017 // Note that this eliminates useless operations but shouldn't affect correctness.
2018 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002019 {
2020 break;
2021 }
2022 }
2023 }
2024 else
2025 {
2026 defaultIt = it; // The default case might not be the last case, keep it for last
2027 }
2028 }
2029 }
2030
2031 // If there's a default case, traverse it here
2032 if(defaultIt != sequence.end())
2033 {
2034 emit(sw::Shader::OPCODE_ELSE);
2035 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2036 {
2037 (*defaultIt)->traverse(this);
2038 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2039 {
2040 break;
2041 }
2042 }
2043 }
2044
2045 for(int i = 0; i < nbCases; ++i)
2046 {
2047 emit(sw::Shader::OPCODE_ENDIF);
2048 }
2049
2050 emit(sw::Shader::OPCODE_ENDSWITCH);
2051
2052 return false;
2053 }
2054
Nicolas Capens0bac2852016-05-07 06:09:58 -04002055 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2056 {
2057 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2058 }
2059
2060 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2061 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2062 {
2063 Instruction *instruction = new Instruction(op);
2064
2065 if(dst)
2066 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002067 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002068 }
2069
Alexis Hetu929c6b02017-11-07 16:04:25 -05002070 if(src0)
2071 {
2072 TIntermTyped* src = src0->getAsTyped();
2073 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2074 }
2075
Nicolas Capens0530b452017-11-15 16:39:47 -05002076 source(instruction->src[0], src0, index0);
2077 source(instruction->src[1], src1, index1);
2078 source(instruction->src[2], src2, index2);
2079 source(instruction->src[3], src3, index3);
2080 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002081
2082 shader->append(instruction);
2083
2084 return instruction;
2085 }
2086
2087 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2088 {
2089 return emitCast(dst, 0, src, 0);
2090 }
2091
2092 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2093 {
2094 switch(src->getBasicType())
2095 {
2096 case EbtBool:
2097 switch(dst->getBasicType())
2098 {
2099 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2100 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2101 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2102 default: break;
2103 }
2104 break;
2105 case EbtInt:
2106 switch(dst->getBasicType())
2107 {
2108 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2109 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2110 default: break;
2111 }
2112 break;
2113 case EbtUInt:
2114 switch(dst->getBasicType())
2115 {
2116 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2117 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2118 default: break;
2119 }
2120 break;
2121 case EbtFloat:
2122 switch(dst->getBasicType())
2123 {
2124 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2125 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2126 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2127 default: break;
2128 }
2129 break;
2130 default:
2131 break;
2132 }
2133
2134 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2135 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2136 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2137
2138 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2139 }
2140
2141 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2142 {
2143 for(int index = 0; index < dst->elementRegisterCount(); index++)
2144 {
2145 emit(op, dst, index, src0, index, src1, index, src2, index);
2146 }
2147 }
2148
2149 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2150 {
2151 emitBinary(op, result, src0, src1);
2152 assignLvalue(lhs, result);
2153 }
2154
2155 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2156 {
2157 sw::Shader::Opcode opcode;
2158 switch(left->getAsTyped()->getBasicType())
2159 {
2160 case EbtBool:
2161 case EbtInt:
2162 opcode = sw::Shader::OPCODE_ICMP;
2163 break;
2164 case EbtUInt:
2165 opcode = sw::Shader::OPCODE_UCMP;
2166 break;
2167 default:
2168 opcode = sw::Shader::OPCODE_CMP;
2169 break;
2170 }
2171
2172 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2173 cmp->control = cmpOp;
2174 }
2175
2176 int componentCount(const TType &type, int registers)
2177 {
2178 if(registers == 0)
2179 {
2180 return 0;
2181 }
2182
2183 if(type.isArray() && registers >= type.elementRegisterCount())
2184 {
2185 int index = registers / type.elementRegisterCount();
2186 registers -= index * type.elementRegisterCount();
2187 return index * type.getElementSize() + componentCount(type, registers);
2188 }
2189
2190 if(type.isStruct() || type.isInterfaceBlock())
2191 {
2192 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2193 int elements = 0;
2194
Alexis Hetuda163ed2018-01-03 16:36:14 -05002195 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002196 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002197 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002198
2199 if(fieldType.totalRegisterCount() <= registers)
2200 {
2201 registers -= fieldType.totalRegisterCount();
2202 elements += fieldType.getObjectSize();
2203 }
2204 else // Register within this field
2205 {
2206 return elements + componentCount(fieldType, registers);
2207 }
2208 }
2209 }
2210 else if(type.isMatrix())
2211 {
2212 return registers * type.registerSize();
2213 }
2214
2215 UNREACHABLE(0);
2216 return 0;
2217 }
2218
2219 int registerSize(const TType &type, int registers)
2220 {
2221 if(registers == 0)
2222 {
2223 if(type.isStruct())
2224 {
2225 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2226 }
2227 else if(type.isInterfaceBlock())
2228 {
2229 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2230 }
2231
2232 return type.registerSize();
2233 }
2234
2235 if(type.isArray() && registers >= type.elementRegisterCount())
2236 {
2237 int index = registers / type.elementRegisterCount();
2238 registers -= index * type.elementRegisterCount();
2239 return registerSize(type, registers);
2240 }
2241
2242 if(type.isStruct() || type.isInterfaceBlock())
2243 {
2244 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2245 int elements = 0;
2246
Alexis Hetuda163ed2018-01-03 16:36:14 -05002247 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002248 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002249 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002250
2251 if(fieldType.totalRegisterCount() <= registers)
2252 {
2253 registers -= fieldType.totalRegisterCount();
2254 elements += fieldType.getObjectSize();
2255 }
2256 else // Register within this field
2257 {
2258 return registerSize(fieldType, registers);
2259 }
2260 }
2261 }
2262 else if(type.isMatrix())
2263 {
2264 return registerSize(type, 0);
2265 }
2266
2267 UNREACHABLE(0);
2268 return 0;
2269 }
2270
2271 int OutputASM::getBlockId(TIntermTyped *arg)
2272 {
2273 if(arg)
2274 {
2275 const TType &type = arg->getType();
2276 TInterfaceBlock* block = type.getInterfaceBlock();
2277 if(block && (type.getQualifier() == EvqUniform))
2278 {
2279 // Make sure the uniform block is declared
2280 uniformRegister(arg);
2281
2282 const char* blockName = block->name().c_str();
2283
2284 // Fetch uniform block index from array of blocks
2285 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2286 {
2287 if(blockName == it->name)
2288 {
2289 return it->blockId;
2290 }
2291 }
2292
2293 ASSERT(false);
2294 }
2295 }
2296
2297 return -1;
2298 }
2299
2300 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2301 {
2302 const TType &type = arg->getType();
2303 int blockId = getBlockId(arg);
2304 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2305 if(blockId != -1)
2306 {
2307 argumentInfo.bufferIndex = 0;
2308 for(int i = 0; i < blockId; ++i)
2309 {
2310 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2311 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2312 }
2313
2314 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2315
2316 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2317 BlockDefinitionIndexMap::const_iterator it = itEnd;
2318
2319 argumentInfo.clampedIndex = index;
2320 if(type.isInterfaceBlock())
2321 {
2322 // Offset index to the beginning of the selected instance
2323 int blockRegisters = type.elementRegisterCount();
2324 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2325 argumentInfo.bufferIndex += bufferOffset;
2326 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2327 }
2328
2329 int regIndex = registerIndex(arg);
2330 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2331 {
2332 it = blockDefinition.find(i);
2333 if(it != itEnd)
2334 {
2335 argumentInfo.clampedIndex -= (i - regIndex);
2336 break;
2337 }
2338 }
2339 ASSERT(it != itEnd);
2340
2341 argumentInfo.typedMemberInfo = it->second;
2342
2343 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2344 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2345 }
2346 else
2347 {
2348 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2349 }
2350
2351 return argumentInfo;
2352 }
2353
Nicolas Capens0530b452017-11-15 16:39:47 -05002354 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002355 {
2356 if(argument)
2357 {
2358 TIntermTyped *arg = argument->getAsTyped();
2359 Temporary unpackedUniform(this);
2360
2361 const TType& srcType = arg->getType();
2362 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2363 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2364 {
2365 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2366 const TType &memberType = argumentInfo.typedMemberInfo.type;
2367
2368 if(memberType.getBasicType() == EbtBool)
2369 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002370 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002371
2372 // Convert the packed bool, which is currently an int, to a true bool
2373 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2374 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2375 instruction->dst.index = registerIndex(&unpackedUniform);
2376 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2377 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2378 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2379
2380 shader->append(instruction);
2381
2382 arg = &unpackedUniform;
2383 index = 0;
2384 }
Alexis Hetud2742532018-01-23 16:53:41 -05002385 else if((memberType.getLayoutQualifier().matrixPacking == EmpRowMajor) && memberType.isMatrix())
Nicolas Capens0bac2852016-05-07 06:09:58 -04002386 {
2387 int numCols = memberType.getNominalSize();
2388 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002389
Alexis Hetue97a31e2016-11-14 14:10:47 -05002390 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002391
2392 unsigned int dstIndex = registerIndex(&unpackedUniform);
2393 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2394 int arrayIndex = argumentInfo.clampedIndex / numCols;
2395 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2396
2397 for(int j = 0; j < numRows; ++j)
2398 {
2399 // Transpose the row major matrix
2400 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2401 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2402 instruction->dst.index = dstIndex;
2403 instruction->dst.mask = 1 << j;
2404 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2405 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2406 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2407 instruction->src[0].swizzle = srcSwizzle;
2408
2409 shader->append(instruction);
2410 }
2411
2412 arg = &unpackedUniform;
2413 index = 0;
2414 }
2415 }
2416
2417 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2418 const TType &type = argumentInfo.typedMemberInfo.type;
2419
2420 int size = registerSize(type, argumentInfo.clampedIndex);
2421
2422 parameter.type = registerType(arg);
2423 parameter.bufferIndex = argumentInfo.bufferIndex;
2424
2425 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2426 {
2427 int component = componentCount(type, argumentInfo.clampedIndex);
2428 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2429
2430 for(int i = 0; i < 4; i++)
2431 {
2432 if(size == 1) // Replicate
2433 {
2434 parameter.value[i] = constants[component + 0].getAsFloat();
2435 }
2436 else if(i < size)
2437 {
2438 parameter.value[i] = constants[component + i].getAsFloat();
2439 }
2440 else
2441 {
2442 parameter.value[i] = 0.0f;
2443 }
2444 }
2445 }
2446 else
2447 {
2448 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2449
2450 if(parameter.bufferIndex != -1)
2451 {
2452 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2453 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2454 }
2455 }
2456
2457 if(!IsSampler(arg->getBasicType()))
2458 {
2459 parameter.swizzle = readSwizzle(arg, size);
2460 }
2461 }
2462 }
2463
Nicolas Capens0530b452017-11-15 16:39:47 -05002464 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2465 {
2466 parameter.type = registerType(arg);
2467 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002468 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002469 }
2470
Nicolas Capens0bac2852016-05-07 06:09:58 -04002471 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2472 {
2473 for(int index = 0; index < dst->totalRegisterCount(); index++)
2474 {
2475 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002476 }
2477 }
2478
2479 int swizzleElement(int swizzle, int index)
2480 {
2481 return (swizzle >> (index * 2)) & 0x03;
2482 }
2483
2484 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2485 {
2486 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2487 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2488 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2489 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2490 }
2491
2492 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2493 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002494 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2495 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002496 {
2497 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2498 }
2499
2500 TIntermBinary *binary = dst->getAsBinaryNode();
2501
2502 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2503 {
2504 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2505
Nicolas Capens6986b282017-11-16 10:38:19 -05002506 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002507
2508 insert->src[0].type = insert->dst.type;
2509 insert->src[0].index = insert->dst.index;
2510 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002511 source(insert->src[1], src);
2512 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002513
2514 shader->append(insert);
2515 }
2516 else
2517 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002518 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2519
Nicolas Capens6986b282017-11-16 10:38:19 -05002520 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002521
Nicolas Capens0530b452017-11-15 16:39:47 -05002522 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002523 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2524
2525 shader->append(mov1);
2526
2527 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002528 {
2529 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2530
Nicolas Capens84249fd2017-11-09 11:20:51 -05002531 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002532 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002533 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002534
Nicolas Capens0530b452017-11-15 16:39:47 -05002535 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002536
2537 shader->append(mov);
2538 }
2539 }
2540 }
2541
Nicolas Capensd469de22017-11-16 10:42:20 -05002542 void OutputASM::evaluateRvalue(TIntermTyped *node)
2543 {
2544 TIntermBinary *binary = node->getAsBinaryNode();
2545
2546 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2547 {
2548 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2549
2550 destination(insert->dst, node);
2551
2552 Temporary address(this);
2553 unsigned char mask;
2554 TIntermTyped *root = nullptr;
2555 unsigned int offset = 0;
2556 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2557
2558 source(insert->src[0], root, offset);
2559 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2560
2561 source(insert->src[1], binary->getRight());
2562
2563 shader->append(insert);
2564 }
2565 else
2566 {
2567 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2568
2569 destination(mov1->dst, node, 0);
2570
2571 Temporary address(this);
2572 unsigned char mask;
2573 TIntermTyped *root = nullptr;
2574 unsigned int offset = 0;
2575 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2576
2577 source(mov1->src[0], root, offset);
2578 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2579
2580 shader->append(mov1);
2581
2582 for(int i = 1; i < node->totalRegisterCount(); i++)
2583 {
2584 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2585 mov->src[0].rel = mov1->src[0].rel;
2586 }
2587 }
2588 }
2589
Nicolas Capens6986b282017-11-16 10:38:19 -05002590 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002591 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002592 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002593 TIntermTyped *root = nullptr;
2594 unsigned int offset = 0;
2595 unsigned char mask = 0xF;
2596 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2597
2598 dst.type = registerType(root);
2599 dst.index = registerIndex(root) + offset;
2600 dst.mask = mask;
2601
2602 return swizzle;
2603 }
2604
2605 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2606 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002607 TIntermTyped *result = node;
2608 TIntermBinary *binary = node->getAsBinaryNode();
2609 TIntermSymbol *symbol = node->getAsSymbolNode();
2610
2611 if(binary)
2612 {
2613 TIntermTyped *left = binary->getLeft();
2614 TIntermTyped *right = binary->getRight();
2615
Nicolas Capens0530b452017-11-15 16:39:47 -05002616 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002617
2618 switch(binary->getOp())
2619 {
2620 case EOpIndexDirect:
2621 {
2622 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2623
2624 if(left->isRegister())
2625 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002626 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002627
Nicolas Capens0530b452017-11-15 16:39:47 -05002628 mask = 1;
2629 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002630 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002631 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002632 }
2633
2634 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002635 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002636
2637 return element;
2638 }
2639 else if(left->isArray() || left->isMatrix())
2640 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002641 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002642 return 0xE4;
2643 }
2644 else UNREACHABLE(0);
2645 }
2646 break;
2647 case EOpIndexIndirect:
2648 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002649 right->traverse(this);
2650
Nicolas Capens0bac2852016-05-07 06:09:58 -04002651 if(left->isRegister())
2652 {
2653 // Requires INSERT instruction (handled by calling function)
2654 }
2655 else if(left->isArray() || left->isMatrix())
2656 {
2657 int scale = result->totalRegisterCount();
2658
Nicolas Capens0530b452017-11-15 16:39:47 -05002659 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002660 {
2661 if(left->totalRegisterCount() > 1)
2662 {
2663 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002664 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002665
Nicolas Capens4b743732018-05-28 13:22:07 -04002666 int indexId = right->getAsSymbolNode() ? right->getAsSymbolNode()->getId() : 0;
2667
Nicolas Capens0530b452017-11-15 16:39:47 -05002668 rel.index = relativeRegister.index;
2669 rel.type = relativeRegister.type;
2670 rel.scale = scale;
Nicolas Capens4b743732018-05-28 13:22:07 -04002671 rel.dynamic = (right->getQualifier() != EvqUniform) && (deterministicVariables.count(indexId) == 0);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002672 }
2673 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002674 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002675 {
2676 if(scale == 1)
2677 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002678 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002679 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002680 mad->src[0].index = rel.index;
2681 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002682 }
2683 else
2684 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002685 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002686 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002687 mul->src[0].index = rel.index;
2688 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002689
2690 Constant newScale(scale);
2691 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2692 }
2693
Nicolas Capens0530b452017-11-15 16:39:47 -05002694 rel.type = sw::Shader::PARAMETER_TEMP;
2695 rel.index = registerIndex(&address);
2696 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002697 }
2698 else // Just add the new index to the address register
2699 {
2700 if(scale == 1)
2701 {
2702 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2703 }
2704 else
2705 {
2706 Constant newScale(scale);
2707 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2708 }
2709 }
2710 }
2711 else UNREACHABLE(0);
2712 }
2713 break;
2714 case EOpIndexDirectStruct:
2715 case EOpIndexDirectInterfaceBlock:
2716 {
2717 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2718 left->getType().getStruct()->fields() :
2719 left->getType().getInterfaceBlock()->fields();
2720 int index = right->getAsConstantUnion()->getIConst(0);
2721 int fieldOffset = 0;
2722
2723 for(int i = 0; i < index; i++)
2724 {
2725 fieldOffset += fields[i]->type()->totalRegisterCount();
2726 }
2727
Nicolas Capens0530b452017-11-15 16:39:47 -05002728 offset += fieldOffset;
2729 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002730
2731 return 0xE4;
2732 }
2733 break;
2734 case EOpVectorSwizzle:
2735 {
2736 ASSERT(left->isRegister());
2737
Nicolas Capens0530b452017-11-15 16:39:47 -05002738 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002739
2740 int swizzle = 0;
2741 int rightMask = 0;
2742
2743 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2744
2745 for(unsigned int i = 0; i < sequence.size(); i++)
2746 {
2747 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2748
2749 int element = swizzleElement(leftSwizzle, index);
2750 rightMask = rightMask | (1 << element);
2751 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2752 }
2753
Nicolas Capens0530b452017-11-15 16:39:47 -05002754 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002755
2756 return swizzle;
2757 }
2758 break;
2759 default:
2760 UNREACHABLE(binary->getOp()); // Not an l-value operator
2761 break;
2762 }
2763 }
2764 else if(symbol)
2765 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002766 root = symbol;
2767 offset = 0;
2768 mask = writeMask(symbol);
2769
2770 return 0xE4;
2771 }
2772 else
2773 {
2774 node->traverse(this);
2775
2776 root = node;
2777 offset = 0;
2778 mask = writeMask(node);
2779
Nicolas Capens0bac2852016-05-07 06:09:58 -04002780 return 0xE4;
2781 }
2782
2783 return 0xE4;
2784 }
2785
2786 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2787 {
2788 if(isSamplerRegister(operand))
2789 {
2790 return sw::Shader::PARAMETER_SAMPLER;
2791 }
2792
2793 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002794 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002795 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002796 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2797 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002798 {
2799 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2800 }
2801 outputQualifier = qualifier;
2802 }
2803
2804 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2805 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002806 // Constant arrays are in the constant register file.
2807 if(operand->isArray() && operand->getArraySize() > 1)
2808 {
2809 return sw::Shader::PARAMETER_CONST;
2810 }
2811 else
2812 {
2813 return sw::Shader::PARAMETER_TEMP;
2814 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002815 }
2816
2817 switch(qualifier)
2818 {
2819 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2820 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2821 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2822 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2823 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2824 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2825 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2826 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2827 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2828 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2829 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2830 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2831 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2832 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2833 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2834 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2835 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2836 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2837 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2838 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2839 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2840 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2841 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2842 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2843 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2844 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002845 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002846 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2847 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2848 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2849 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2850 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2851 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2852 default: UNREACHABLE(qualifier);
2853 }
2854
2855 return sw::Shader::PARAMETER_VOID;
2856 }
2857
Alexis Hetu12b00502016-05-20 13:01:11 -04002858 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2859 {
2860 const TQualifier qualifier = operand->getQualifier();
2861 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2862 }
2863
Nicolas Capens0bac2852016-05-07 06:09:58 -04002864 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2865 {
2866 if(isSamplerRegister(operand))
2867 {
2868 return samplerRegister(operand);
2869 }
Alexis Hetud87b3a82018-04-24 11:13:33 -04002870 else if(operand->getType().totalSamplerRegisterCount() > 0) // Struct containing a sampler
2871 {
2872 samplerRegister(operand); // Make sure the sampler is declared
2873 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002874
2875 switch(operand->getQualifier())
2876 {
2877 case EvqTemporary: return temporaryRegister(operand);
2878 case EvqGlobal: return temporaryRegister(operand);
2879 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2880 case EvqAttribute: return attributeRegister(operand);
2881 case EvqVaryingIn: return varyingRegister(operand);
2882 case EvqVaryingOut: return varyingRegister(operand);
2883 case EvqVertexIn: return attributeRegister(operand);
2884 case EvqFragmentOut: return fragmentOutputRegister(operand);
2885 case EvqVertexOut: return varyingRegister(operand);
2886 case EvqFragmentIn: return varyingRegister(operand);
2887 case EvqInvariantVaryingIn: return varyingRegister(operand);
2888 case EvqInvariantVaryingOut: return varyingRegister(operand);
2889 case EvqSmooth: return varyingRegister(operand);
2890 case EvqFlat: return varyingRegister(operand);
2891 case EvqCentroidOut: return varyingRegister(operand);
2892 case EvqSmoothIn: return varyingRegister(operand);
2893 case EvqFlatIn: return varyingRegister(operand);
2894 case EvqCentroidIn: return varyingRegister(operand);
2895 case EvqUniform: return uniformRegister(operand);
2896 case EvqIn: return temporaryRegister(operand);
2897 case EvqOut: return temporaryRegister(operand);
2898 case EvqInOut: return temporaryRegister(operand);
2899 case EvqConstReadOnly: return temporaryRegister(operand);
2900 case EvqPosition: return varyingRegister(operand);
2901 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002902 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2903 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2904 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2905 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002906 case EvqPointCoord: return varyingRegister(operand);
2907 case EvqFragColor: return 0;
2908 case EvqFragData: return fragmentOutputRegister(operand);
2909 case EvqFragDepth: return 0;
2910 default: UNREACHABLE(operand->getQualifier());
2911 }
2912
2913 return 0;
2914 }
2915
2916 int OutputASM::writeMask(TIntermTyped *destination, int index)
2917 {
2918 if(destination->getQualifier() == EvqPointSize)
2919 {
2920 return 0x2; // Point size stored in the y component
2921 }
2922
2923 return 0xF >> (4 - registerSize(destination->getType(), index));
2924 }
2925
2926 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2927 {
2928 if(argument->getQualifier() == EvqPointSize)
2929 {
2930 return 0x55; // Point size stored in the y component
2931 }
2932
2933 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2934
2935 return swizzleSize[size];
2936 }
2937
2938 // Conservatively checks whether an expression is fast to compute and has no side effects
2939 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2940 {
2941 if(!expression->isRegister())
2942 {
2943 return false;
2944 }
2945
2946 return cost(expression, budget) >= 0;
2947 }
2948
2949 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2950 int OutputASM::cost(TIntermNode *expression, int budget)
2951 {
2952 if(budget < 0)
2953 {
2954 return budget;
2955 }
2956
2957 if(expression->getAsSymbolNode())
2958 {
2959 return budget;
2960 }
2961 else if(expression->getAsConstantUnion())
2962 {
2963 return budget;
2964 }
2965 else if(expression->getAsBinaryNode())
2966 {
2967 TIntermBinary *binary = expression->getAsBinaryNode();
2968
2969 switch(binary->getOp())
2970 {
2971 case EOpVectorSwizzle:
2972 case EOpIndexDirect:
2973 case EOpIndexDirectStruct:
2974 case EOpIndexDirectInterfaceBlock:
2975 return cost(binary->getLeft(), budget - 0);
2976 case EOpAdd:
2977 case EOpSub:
2978 case EOpMul:
2979 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2980 default:
2981 return -1;
2982 }
2983 }
2984 else if(expression->getAsUnaryNode())
2985 {
2986 TIntermUnary *unary = expression->getAsUnaryNode();
2987
2988 switch(unary->getOp())
2989 {
2990 case EOpAbs:
2991 case EOpNegative:
2992 return cost(unary->getOperand(), budget - 1);
2993 default:
2994 return -1;
2995 }
2996 }
2997 else if(expression->getAsSelectionNode())
2998 {
2999 TIntermSelection *selection = expression->getAsSelectionNode();
3000
3001 if(selection->usesTernaryOperator())
3002 {
3003 TIntermTyped *condition = selection->getCondition();
3004 TIntermNode *trueBlock = selection->getTrueBlock();
3005 TIntermNode *falseBlock = selection->getFalseBlock();
3006 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
3007
3008 if(constantCondition)
3009 {
3010 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
3011
3012 if(trueCondition)
3013 {
3014 return cost(trueBlock, budget - 0);
3015 }
3016 else
3017 {
3018 return cost(falseBlock, budget - 0);
3019 }
3020 }
3021 else
3022 {
3023 return cost(trueBlock, cost(falseBlock, budget - 2));
3024 }
3025 }
3026 }
3027
3028 return -1;
3029 }
3030
3031 const Function *OutputASM::findFunction(const TString &name)
3032 {
3033 for(unsigned int f = 0; f < functionArray.size(); f++)
3034 {
3035 if(functionArray[f].name == name)
3036 {
3037 return &functionArray[f];
3038 }
3039 }
3040
3041 return 0;
3042 }
3043
3044 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3045 {
Alexis Hetu329747c2018-04-09 13:47:34 -04003046 int index = allocate(temporaries, temporary);
3047 if(index >= sw::NUM_TEMPORARY_REGISTERS)
3048 {
3049 mContext.error(temporary->getLine(),
3050 "Too many temporary registers required to compile shader",
3051 pixelShader ? "pixel shader" : "vertex shader");
3052 }
3053 return index;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003054 }
3055
Alexis Hetu49351232017-11-02 16:00:32 -04003056 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3057 {
3058 if(type.isStruct())
3059 {
3060 const TFieldList &fields = type.getStruct()->fields();
3061 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003062 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003063 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003064 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003065 setPixelShaderInputs(fieldType, fieldVar, flat);
3066 fieldVar += fieldType.totalRegisterCount();
3067 }
3068 }
3069 else
3070 {
3071 for(int i = 0; i < type.totalRegisterCount(); i++)
3072 {
3073 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3074 }
3075 }
3076 }
3077
Nicolas Capens0bac2852016-05-07 06:09:58 -04003078 int OutputASM::varyingRegister(TIntermTyped *varying)
3079 {
3080 int var = lookup(varyings, varying);
3081
3082 if(var == -1)
3083 {
3084 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003085 int registerCount = varying->totalRegisterCount();
3086
3087 if(pixelShader)
3088 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003089 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003090 {
3091 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3092 return 0;
3093 }
3094
3095 if(varying->getQualifier() == EvqPointCoord)
3096 {
3097 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003098 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003099 }
3100 else
3101 {
Alexis Hetu49351232017-11-02 16:00:32 -04003102 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003103 }
3104 }
3105 else if(vertexShader)
3106 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003107 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003108 {
3109 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3110 return 0;
3111 }
3112
3113 if(varying->getQualifier() == EvqPosition)
3114 {
3115 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003116 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003117 }
3118 else if(varying->getQualifier() == EvqPointSize)
3119 {
3120 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003121 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003122 }
3123 else
3124 {
3125 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3126 }
3127 }
3128 else UNREACHABLE(0);
3129
3130 declareVarying(varying, var);
3131 }
3132
3133 return var;
3134 }
3135
3136 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3137 {
3138 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3139 {
Alexis Hetu49351232017-11-02 16:00:32 -04003140 TIntermSymbol *symbol = varying->getAsSymbolNode();
3141 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3142 }
3143 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003144
Alexis Hetu49351232017-11-02 16:00:32 -04003145 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3146 {
3147 const char *name = varyingName.c_str();
3148 VaryingList &activeVaryings = shaderObject->varyings;
3149
3150 TStructure* structure = type.getStruct();
3151 if(structure)
3152 {
3153 int fieldRegisterIndex = registerIndex;
3154
3155 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003156 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003157 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003158 const TType& fieldType = *(field->type());
3159 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003160 if(fieldRegisterIndex >= 0)
3161 {
3162 fieldRegisterIndex += fieldType.totalRegisterCount();
3163 }
3164 }
3165 }
3166 else
3167 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003168 // Check if this varying has been declared before without having a register assigned
3169 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3170 {
3171 if(v->name == name)
3172 {
Alexis Hetu49351232017-11-02 16:00:32 -04003173 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003174 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003175 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3176 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003177 }
3178
3179 return;
3180 }
3181 }
3182
Alexis Hetu924513c2018-01-05 15:48:12 -05003183 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003184 }
3185 }
3186
Alexis Hetu930df972018-01-30 16:54:13 -05003187 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3188 {
3189 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003190 int registerCount = fragmentOutput->totalRegisterCount();
3191 if(requestedLocation < 0)
Alexis Hetu930df972018-01-30 16:54:13 -05003192 {
Alexis Hetu3c1d6cf2018-02-06 10:54:49 -05003193 ASSERT(requestedLocation == -1); // All other negative values would have been prevented in TParseContext::parseLayoutQualifier
3194 return; // No requested location
Alexis Hetu930df972018-01-30 16:54:13 -05003195 }
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003196 else if((requestedLocation + registerCount) > sw::RENDERTARGETS)
Alexis Hetu930df972018-01-30 16:54:13 -05003197 {
3198 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3199 }
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003200 else
3201 {
3202 int currentIndex = lookup(fragmentOutputs, fragmentOutput);
3203 if(requestedLocation != currentIndex)
3204 {
3205 if(currentIndex != -1)
3206 {
3207 mContext.error(fragmentOutput->getLine(), "Multiple locations for fragment output", "fragment shader");
3208 }
3209 else
3210 {
3211 if(fragmentOutputs.size() <= (size_t)requestedLocation)
3212 {
3213 while(fragmentOutputs.size() < (size_t)requestedLocation)
3214 {
3215 fragmentOutputs.push_back(nullptr);
3216 }
3217 for(int i = 0; i < registerCount; i++)
3218 {
3219 fragmentOutputs.push_back(fragmentOutput);
3220 }
3221 }
3222 else
3223 {
3224 for(int i = 0; i < registerCount; i++)
3225 {
3226 if(!fragmentOutputs[requestedLocation + i])
3227 {
3228 fragmentOutputs[requestedLocation + i] = fragmentOutput;
3229 }
3230 else
3231 {
3232 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3233 return;
3234 }
3235 }
3236 }
3237 }
3238 }
3239 }
Alexis Hetu930df972018-01-30 16:54:13 -05003240 }
3241
Nicolas Capens0bac2852016-05-07 06:09:58 -04003242 int OutputASM::uniformRegister(TIntermTyped *uniform)
3243 {
3244 const TType &type = uniform->getType();
3245 ASSERT(!IsSampler(type.getBasicType()));
3246 TInterfaceBlock *block = type.getAsInterfaceBlock();
3247 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3248 ASSERT(symbol || block);
3249
3250 if(symbol || block)
3251 {
3252 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3253 bool isBlockMember = (!block && parentBlock);
3254 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3255
3256 if(index == -1 || isBlockMember)
3257 {
3258 if(index == -1)
3259 {
3260 index = allocate(uniforms, uniform);
3261 }
3262
3263 // Verify if the current uniform is a member of an already declared block
3264 const TString &name = symbol ? symbol->getSymbol() : block->name();
3265 int blockMemberIndex = blockMemberLookup(type, name, index);
3266 if(blockMemberIndex == -1)
3267 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003268 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003269 }
3270 else
3271 {
3272 index = blockMemberIndex;
3273 }
3274 }
3275
3276 return index;
3277 }
3278
3279 return 0;
3280 }
3281
3282 int OutputASM::attributeRegister(TIntermTyped *attribute)
3283 {
3284 ASSERT(!attribute->isArray());
3285
3286 int index = lookup(attributes, attribute);
3287
3288 if(index == -1)
3289 {
3290 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3291 ASSERT(symbol);
3292
3293 if(symbol)
3294 {
3295 index = allocate(attributes, attribute);
3296 const TType &type = attribute->getType();
3297 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003298 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3299 switch(type.getBasicType())
3300 {
3301 case EbtInt:
3302 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3303 break;
3304 case EbtUInt:
3305 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3306 break;
3307 case EbtFloat:
3308 default:
3309 break;
3310 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003311
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003312 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003313 {
3314 for(int i = 0; i < registerCount; i++)
3315 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003316 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003317 }
3318 }
3319
3320 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3321
3322 const char *name = symbol->getSymbol().c_str();
3323 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3324 }
3325 }
3326
3327 return index;
3328 }
3329
3330 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3331 {
3332 return allocate(fragmentOutputs, fragmentOutput);
3333 }
3334
3335 int OutputASM::samplerRegister(TIntermTyped *sampler)
3336 {
3337 const TType &type = sampler->getType();
3338 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3339
3340 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3341 TIntermBinary *binary = sampler->getAsBinaryNode();
3342
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003343 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003344 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003345 switch(type.getQualifier())
3346 {
3347 case EvqUniform:
3348 return samplerRegister(symbol);
3349 case EvqIn:
3350 case EvqConstReadOnly:
3351 // Function arguments are not (uniform) sampler registers
3352 return -1;
3353 default:
3354 UNREACHABLE(type.getQualifier());
3355 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003356 }
3357 else if(binary)
3358 {
3359 TIntermTyped *left = binary->getLeft();
3360 TIntermTyped *right = binary->getRight();
3361 const TType &leftType = left->getType();
3362 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3363 int offset = 0;
3364
3365 switch(binary->getOp())
3366 {
3367 case EOpIndexDirect:
3368 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003369 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003370 break;
3371 case EOpIndexDirectStruct:
3372 ASSERT(leftType.isStruct());
3373 {
3374 const TFieldList &fields = leftType.getStruct()->fields();
3375
3376 for(int i = 0; i < index; i++)
3377 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003378 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003379 }
3380 }
3381 break;
3382 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3383 return -1;
3384 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3385 default:
3386 UNREACHABLE(binary->getOp());
3387 return -1;
3388 }
3389
3390 int base = samplerRegister(left);
3391
3392 if(base < 0)
3393 {
3394 return -1;
3395 }
3396
3397 return base + offset;
3398 }
3399
3400 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003401 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003402 }
3403
3404 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3405 {
3406 const TType &type = sampler->getType();
3407 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3408
3409 int index = lookup(samplers, sampler);
3410
3411 if(index == -1)
3412 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003413 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003414
3415 if(sampler->getQualifier() == EvqUniform)
3416 {
3417 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003418 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003419 }
3420 }
3421
3422 return index;
3423 }
3424
3425 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3426 {
3427 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3428 }
3429
3430 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3431 {
3432 for(unsigned int i = 0; i < list.size(); i++)
3433 {
3434 if(list[i] == variable)
3435 {
3436 return i; // Pointer match
3437 }
3438 }
3439
3440 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3441 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3442
3443 if(varBlock)
3444 {
3445 for(unsigned int i = 0; i < list.size(); i++)
3446 {
3447 if(list[i])
3448 {
3449 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3450
3451 if(listBlock)
3452 {
3453 if(listBlock->name() == varBlock->name())
3454 {
3455 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3456 ASSERT(listBlock->fields() == varBlock->fields());
3457 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3458 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3459
3460 return i;
3461 }
3462 }
3463 }
3464 }
3465 }
3466 else if(varSymbol)
3467 {
3468 for(unsigned int i = 0; i < list.size(); i++)
3469 {
3470 if(list[i])
3471 {
3472 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3473
3474 if(listSymbol)
3475 {
3476 if(listSymbol->getId() == varSymbol->getId())
3477 {
3478 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3479 ASSERT(listSymbol->getType() == varSymbol->getType());
3480 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3481
3482 return i;
3483 }
3484 }
3485 }
3486 }
3487 }
3488
3489 return -1;
3490 }
3491
3492 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3493 {
3494 for(unsigned int i = 0; i < list.size(); i++)
3495 {
3496 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3497 {
3498 return i; // Pointer match
3499 }
3500 }
3501 return -1;
3502 }
3503
Alexis Hetuda163ed2018-01-03 16:36:14 -05003504 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003505 {
3506 int index = lookup(list, variable);
3507
3508 if(index == -1)
3509 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003510 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003511
3512 for(unsigned int i = 0; i < list.size(); i++)
3513 {
3514 if(list[i] == 0)
3515 {
3516 unsigned int j = 1;
3517 for( ; j < registerCount && (i + j) < list.size(); j++)
3518 {
3519 if(list[i + j] != 0)
3520 {
3521 break;
3522 }
3523 }
3524
3525 if(j == registerCount) // Found free slots
3526 {
3527 for(unsigned int j = 0; j < registerCount; j++)
3528 {
3529 list[i + j] = variable;
3530 }
3531
3532 return i;
3533 }
3534 }
3535 }
3536
3537 index = list.size();
3538
3539 for(unsigned int i = 0; i < registerCount; i++)
3540 {
3541 list.push_back(variable);
3542 }
3543 }
3544
3545 return index;
3546 }
3547
3548 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3549 {
3550 int index = lookup(list, variable);
3551
3552 if(index >= 0)
3553 {
3554 list[index] = 0;
3555 }
3556 }
3557
3558 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3559 {
3560 const TInterfaceBlock *block = type.getInterfaceBlock();
3561
3562 if(block)
3563 {
3564 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3565 const TFieldList& fields = block->fields();
3566 const TString &blockName = block->name();
3567 int fieldRegisterIndex = registerIndex;
3568
3569 if(!type.isInterfaceBlock())
3570 {
3571 // This is a uniform that's part of a block, let's see if the block is already defined
3572 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3573 {
3574 if(activeUniformBlocks[i].name == blockName.c_str())
3575 {
3576 // The block is already defined, find the register for the current uniform and return it
3577 for(size_t j = 0; j < fields.size(); j++)
3578 {
3579 const TString &fieldName = fields[j]->name();
3580 if(fieldName == name)
3581 {
3582 return fieldRegisterIndex;
3583 }
3584
3585 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3586 }
3587
3588 ASSERT(false);
3589 return fieldRegisterIndex;
3590 }
3591 }
3592 }
3593 }
3594
3595 return -1;
3596 }
3597
Alexis Hetuda163ed2018-01-03 16:36:14 -05003598 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003599 {
3600 const TStructure *structure = type.getStruct();
3601 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3602
3603 if(!structure && !block)
3604 {
3605 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3606 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3607 if(blockId >= 0)
3608 {
Nicolas Capensbb2bcae2018-02-05 11:12:10 -05003609 blockDefinitions[blockId].insert(BlockDefinitionIndexMap::value_type(registerIndex, TypedMemberInfo(blockInfo, type)));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003610 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3611 }
3612 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003613 bool isSampler = IsSampler(type.getBasicType());
3614 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003615 {
3616 for(int i = 0; i < type.totalRegisterCount(); i++)
3617 {
3618 shader->declareSampler(fieldRegisterIndex + i);
3619 }
3620 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003621 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003622 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003623 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003624 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003625 }
3626 else if(block)
3627 {
3628 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3629 const TFieldList& fields = block->fields();
3630 const TString &blockName = block->name();
3631 int fieldRegisterIndex = registerIndex;
3632 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3633
3634 blockId = activeUniformBlocks.size();
3635 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3636 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3637 block->blockStorage(), isRowMajor, registerIndex, blockId));
3638 blockDefinitions.push_back(BlockDefinitionIndexMap());
3639
Alexis Hetud2742532018-01-23 16:53:41 -05003640 Std140BlockEncoder currentBlockEncoder;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003641 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003642 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003643 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003644 const TType &fieldType = *(field->type());
3645 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003646 if(isUniformBlockMember && (fieldName == name))
3647 {
3648 registerIndex = fieldRegisterIndex;
3649 }
3650
3651 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3652
Alexis Hetuda163ed2018-01-03 16:36:14 -05003653 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003654 fieldRegisterIndex += fieldType.totalRegisterCount();
3655 }
3656 currentBlockEncoder.exitAggregateType();
3657 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3658 }
3659 else
3660 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003661 // Store struct for program link time validation
3662 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3663
Nicolas Capens0bac2852016-05-07 06:09:58 -04003664 int fieldRegisterIndex = registerIndex;
3665
3666 const TFieldList& fields = structure->fields();
3667 if(type.isArray() && (structure || type.isInterfaceBlock()))
3668 {
3669 for(int i = 0; i < type.getArraySize(); i++)
3670 {
3671 if(encoder)
3672 {
3673 encoder->enterAggregateType();
3674 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003675 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003676 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003677 const TType &fieldType = *(field->type());
3678 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003679 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3680
Alexis Hetuda163ed2018-01-03 16:36:14 -05003681 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3682 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003683 }
3684 if(encoder)
3685 {
3686 encoder->exitAggregateType();
3687 }
3688 }
3689 }
3690 else
3691 {
3692 if(encoder)
3693 {
3694 encoder->enterAggregateType();
3695 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003696 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003697 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003698 const TType &fieldType = *(field->type());
3699 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003700 const TString uniformName = name + "." + fieldName;
3701
Alexis Hetuda163ed2018-01-03 16:36:14 -05003702 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3703 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003704 }
3705 if(encoder)
3706 {
3707 encoder->exitAggregateType();
3708 }
3709 }
3710 }
3711 }
3712
Nicolas Capens0bac2852016-05-07 06:09:58 -04003713 int OutputASM::dim(TIntermNode *v)
3714 {
3715 TIntermTyped *vector = v->getAsTyped();
3716 ASSERT(vector && vector->isRegister());
3717 return vector->getNominalSize();
3718 }
3719
3720 int OutputASM::dim2(TIntermNode *m)
3721 {
3722 TIntermTyped *matrix = m->getAsTyped();
3723 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3724 return matrix->getSecondarySize();
3725 }
3726
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003727 // Sets iterations to ~0u if no loop count could be statically determined.
3728 OutputASM::LoopInfo::LoopInfo(TIntermLoop *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003729 {
3730 // Parse loops of the form:
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003731 // for(int index = initial; index [comparator] limit; index [op] increment)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003732
3733 // Parse index name and intial value
3734 if(node->getInit())
3735 {
3736 TIntermAggregate *init = node->getInit()->getAsAggregate();
3737
3738 if(init)
3739 {
3740 TIntermSequence &sequence = init->getSequence();
3741 TIntermTyped *variable = sequence[0]->getAsTyped();
3742
Nicolas Capense3f05552017-05-24 10:45:56 -04003743 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003744 {
3745 TIntermBinary *assign = variable->getAsBinaryNode();
3746
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003747 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003748 {
3749 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3750 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3751
3752 if(symbol && constant)
3753 {
3754 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3755 {
3756 index = symbol;
3757 initial = constant->getUnionArrayPointer()[0].getIConst();
3758 }
3759 }
3760 }
3761 }
3762 }
3763 }
3764
3765 // Parse comparator and limit value
3766 if(index && node->getCondition())
3767 {
3768 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003769 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003770
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003771 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003772 {
3773 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3774
3775 if(constant)
3776 {
3777 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3778 {
3779 comparator = test->getOp();
3780 limit = constant->getUnionArrayPointer()[0].getIConst();
3781 }
3782 }
3783 }
3784 }
3785
3786 // Parse increment
3787 if(index && comparator != EOpNull && node->getExpression())
3788 {
3789 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3790 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3791
3792 if(binaryTerminal)
3793 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003794 TIntermSymbol *operand = binaryTerminal->getLeft()->getAsSymbolNode();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003795
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003796 if(operand && operand->getId() == index->getId())
Nicolas Capens0bac2852016-05-07 06:09:58 -04003797 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003798 TOperator op = binaryTerminal->getOp();
3799 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003800
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003801 if(constant)
3802 {
3803 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003804 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003805 int value = constant->getUnionArrayPointer()[0].getIConst();
3806
3807 switch(op)
3808 {
3809 case EOpAddAssign: increment = value; break;
3810 case EOpSubAssign: increment = -value; break;
3811 default: increment = 0; break; // Rare cases left unhandled. Treated as non-deterministic.
3812 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003813 }
3814 }
3815 }
3816 }
3817 else if(unaryTerminal)
3818 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003819 TIntermSymbol *operand = unaryTerminal->getOperand()->getAsSymbolNode();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003820
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003821 if(operand && operand->getId() == index->getId())
Nicolas Capens0bac2852016-05-07 06:09:58 -04003822 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003823 TOperator op = unaryTerminal->getOp();
3824
3825 switch(op)
3826 {
3827 case EOpPostIncrement: increment = 1; break;
3828 case EOpPostDecrement: increment = -1; break;
3829 case EOpPreIncrement: increment = 1; break;
3830 case EOpPreDecrement: increment = -1; break;
3831 default: increment = 0; break; // Rare cases left unhandled. Treated as non-deterministic.
3832 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003833 }
3834 }
3835 }
3836
3837 if(index && comparator != EOpNull && increment != 0)
3838 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003839 // Check the loop body for return statements or changes to the index variable that make it non-deterministic.
3840 LoopUnrollable loopUnrollable;
3841 bool unrollable = loopUnrollable.traverse(node, index->getId());
3842
3843 if(!unrollable)
3844 {
3845 iterations = ~0u;
3846 return;
3847 }
3848
Nicolas Capens0bac2852016-05-07 06:09:58 -04003849 if(comparator == EOpLessThanEqual)
3850 {
3851 comparator = EOpLessThan;
3852 limit += 1;
3853 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003854 else if(comparator == EOpGreaterThanEqual)
3855 {
3856 comparator = EOpLessThan;
3857 limit -= 1;
3858 std::swap(initial, limit);
3859 increment = -increment;
3860 }
3861 else if(comparator == EOpGreaterThan)
3862 {
3863 comparator = EOpLessThan;
3864 std::swap(initial, limit);
3865 increment = -increment;
3866 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003867
3868 if(comparator == EOpLessThan)
3869 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003870 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003871 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003872 iterations = 0;
Nicolas Capens930b7002017-01-06 17:22:13 -05003873 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003874 else if(increment < 0)
Nicolas Capens930b7002017-01-06 17:22:13 -05003875 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003876 iterations = ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003877 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003878 else
3879 {
3880 iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3881 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003882 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003883 else
3884 {
3885 // Rare cases left unhandled. Treated as non-deterministic.
3886 iterations = ~0u;
3887 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003888 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003889 }
3890
Nicolas Capens493fc542018-05-29 17:11:37 -04003891 bool LoopUnrollable::traverse(TIntermLoop *loop, int indexId)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003892 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003893 loopUnrollable = true;
3894
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003895 loopIndexId = indexId;
Nicolas Capens493fc542018-05-29 17:11:37 -04003896 TIntermNode *body = loop->getBody();
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003897
Nicolas Capens493fc542018-05-29 17:11:37 -04003898 if(body)
3899 {
3900 body->traverse(this);
3901 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003902
3903 return loopUnrollable;
3904 }
3905
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003906 void LoopUnrollable::visitSymbol(TIntermSymbol *node)
3907 {
3908 // Check that the loop index is not used as the argument to a function out or inout parameter.
3909 if(node->getId() == loopIndexId)
3910 {
3911 if(node->getQualifier() == EvqOut || node->getQualifier() == EvqInOut)
3912 {
3913 loopUnrollable = false;
3914 }
3915 }
3916 }
3917
3918 bool LoopUnrollable::visitBinary(Visit visit, TIntermBinary *node)
3919 {
3920 if(!loopUnrollable)
3921 {
3922 return false;
3923 }
3924
3925 // Check that the loop index is not statically assigned to.
3926 TIntermSymbol *symbol = node->getLeft()->getAsSymbolNode();
Nicolas Capens493fc542018-05-29 17:11:37 -04003927 loopUnrollable = !(node->modifiesState() && symbol && (symbol->getId() == loopIndexId));
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003928
3929 return loopUnrollable;
3930 }
3931
3932 bool LoopUnrollable::visitUnary(Visit visit, TIntermUnary *node)
3933 {
3934 if(!loopUnrollable)
3935 {
3936 return false;
3937 }
3938
3939 // Check that the loop index is not statically assigned to.
3940 TIntermSymbol *symbol = node->getOperand()->getAsSymbolNode();
Nicolas Capens493fc542018-05-29 17:11:37 -04003941 loopUnrollable = !(node->modifiesState() && symbol && (symbol->getId() == loopIndexId));
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003942
3943 return loopUnrollable;
3944 }
3945
Nicolas Capens0bac2852016-05-07 06:09:58 -04003946 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3947 {
3948 if(!loopUnrollable)
3949 {
3950 return false;
3951 }
3952
Nicolas Capens0bac2852016-05-07 06:09:58 -04003953 switch(node->getFlowOp())
3954 {
3955 case EOpKill:
3956 case EOpReturn:
Nicolas Capens0bac2852016-05-07 06:09:58 -04003957 case EOpBreak:
3958 case EOpContinue:
3959 loopUnrollable = false;
3960 break;
3961 default: UNREACHABLE(node->getFlowOp());
3962 }
3963
3964 return loopUnrollable;
3965 }
3966
3967 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3968 {
3969 return loopUnrollable;
3970 }
3971}