blob: c18ecf73578a0f74fc2aedac28e27cf1910a060d [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>
Nicolas Capense92a88e2018-03-21 14:25:19 -040026#include <GL/glcorearb.h>
27#include <GL/glext.h>
Nicolas Capens0bac2852016-05-07 06:09:58 -040028
Nicolas Capens930b7002017-01-06 17:22:13 -050029#include <stdlib.h>
30
Alexis Hetu924513c2018-01-05 15:48:12 -050031namespace
32{
33 GLenum glVariableType(const TType &type)
34 {
35 switch(type.getBasicType())
36 {
37 case EbtFloat:
38 if(type.isScalar())
39 {
40 return GL_FLOAT;
41 }
42 else if(type.isVector())
43 {
44 switch(type.getNominalSize())
45 {
46 case 2: return GL_FLOAT_VEC2;
47 case 3: return GL_FLOAT_VEC3;
48 case 4: return GL_FLOAT_VEC4;
49 default: UNREACHABLE(type.getNominalSize());
50 }
51 }
52 else if(type.isMatrix())
53 {
54 switch(type.getNominalSize())
55 {
56 case 2:
57 switch(type.getSecondarySize())
58 {
59 case 2: return GL_FLOAT_MAT2;
60 case 3: return GL_FLOAT_MAT2x3;
61 case 4: return GL_FLOAT_MAT2x4;
62 default: UNREACHABLE(type.getSecondarySize());
63 }
64 case 3:
65 switch(type.getSecondarySize())
66 {
67 case 2: return GL_FLOAT_MAT3x2;
68 case 3: return GL_FLOAT_MAT3;
69 case 4: return GL_FLOAT_MAT3x4;
70 default: UNREACHABLE(type.getSecondarySize());
71 }
72 case 4:
73 switch(type.getSecondarySize())
74 {
75 case 2: return GL_FLOAT_MAT4x2;
76 case 3: return GL_FLOAT_MAT4x3;
77 case 4: return GL_FLOAT_MAT4;
78 default: UNREACHABLE(type.getSecondarySize());
79 }
80 default: UNREACHABLE(type.getNominalSize());
81 }
82 }
83 else UNREACHABLE(0);
84 break;
85 case EbtInt:
86 if(type.isScalar())
87 {
88 return GL_INT;
89 }
90 else if(type.isVector())
91 {
92 switch(type.getNominalSize())
93 {
94 case 2: return GL_INT_VEC2;
95 case 3: return GL_INT_VEC3;
96 case 4: return GL_INT_VEC4;
97 default: UNREACHABLE(type.getNominalSize());
98 }
99 }
100 else UNREACHABLE(0);
101 break;
102 case EbtUInt:
103 if(type.isScalar())
104 {
105 return GL_UNSIGNED_INT;
106 }
107 else if(type.isVector())
108 {
109 switch(type.getNominalSize())
110 {
111 case 2: return GL_UNSIGNED_INT_VEC2;
112 case 3: return GL_UNSIGNED_INT_VEC3;
113 case 4: return GL_UNSIGNED_INT_VEC4;
114 default: UNREACHABLE(type.getNominalSize());
115 }
116 }
117 else UNREACHABLE(0);
118 break;
119 case EbtBool:
120 if(type.isScalar())
121 {
122 return GL_BOOL;
123 }
124 else if(type.isVector())
125 {
126 switch(type.getNominalSize())
127 {
128 case 2: return GL_BOOL_VEC2;
129 case 3: return GL_BOOL_VEC3;
130 case 4: return GL_BOOL_VEC4;
131 default: UNREACHABLE(type.getNominalSize());
132 }
133 }
134 else UNREACHABLE(0);
135 break;
136 case EbtSampler2D:
137 return GL_SAMPLER_2D;
138 case EbtISampler2D:
139 return GL_INT_SAMPLER_2D;
140 case EbtUSampler2D:
141 return GL_UNSIGNED_INT_SAMPLER_2D;
142 case EbtSamplerCube:
143 return GL_SAMPLER_CUBE;
Alexis Hetu46768622018-01-16 22:09:28 -0500144 case EbtSampler2DRect:
145 return GL_SAMPLER_2D_RECT_ARB;
Alexis Hetu924513c2018-01-05 15:48:12 -0500146 case EbtISamplerCube:
147 return GL_INT_SAMPLER_CUBE;
148 case EbtUSamplerCube:
149 return GL_UNSIGNED_INT_SAMPLER_CUBE;
150 case EbtSamplerExternalOES:
151 return GL_SAMPLER_EXTERNAL_OES;
152 case EbtSampler3D:
153 return GL_SAMPLER_3D_OES;
154 case EbtISampler3D:
155 return GL_INT_SAMPLER_3D;
156 case EbtUSampler3D:
157 return GL_UNSIGNED_INT_SAMPLER_3D;
158 case EbtSampler2DArray:
159 return GL_SAMPLER_2D_ARRAY;
160 case EbtISampler2DArray:
161 return GL_INT_SAMPLER_2D_ARRAY;
162 case EbtUSampler2DArray:
163 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
164 case EbtSampler2DShadow:
165 return GL_SAMPLER_2D_SHADOW;
166 case EbtSamplerCubeShadow:
167 return GL_SAMPLER_CUBE_SHADOW;
168 case EbtSampler2DArrayShadow:
169 return GL_SAMPLER_2D_ARRAY_SHADOW;
170 default:
171 UNREACHABLE(type.getBasicType());
172 break;
173 }
174
175 return GL_NONE;
176 }
177
178 GLenum glVariablePrecision(const TType &type)
179 {
180 if(type.getBasicType() == EbtFloat)
181 {
182 switch(type.getPrecision())
183 {
184 case EbpHigh: return GL_HIGH_FLOAT;
185 case EbpMedium: return GL_MEDIUM_FLOAT;
186 case EbpLow: return GL_LOW_FLOAT;
187 case EbpUndefined:
188 // Should be defined as the default precision by the parser
189 default: UNREACHABLE(type.getPrecision());
190 }
191 }
192 else if(type.getBasicType() == EbtInt)
193 {
194 switch(type.getPrecision())
195 {
196 case EbpHigh: return GL_HIGH_INT;
197 case EbpMedium: return GL_MEDIUM_INT;
198 case EbpLow: return GL_LOW_INT;
199 case EbpUndefined:
200 // Should be defined as the default precision by the parser
201 default: UNREACHABLE(type.getPrecision());
202 }
203 }
204
205 // Other types (boolean, sampler) don't have a precision
206 return GL_NONE;
207 }
208}
209
Nicolas Capens0bac2852016-05-07 06:09:58 -0400210namespace glsl
211{
212 // Integer to TString conversion
213 TString str(int i)
214 {
215 char buffer[20];
216 sprintf(buffer, "%d", i);
217 return buffer;
218 }
219
220 class Temporary : public TIntermSymbol
221 {
222 public:
223 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
224 {
225 }
226
227 ~Temporary()
228 {
229 assembler->freeTemporary(this);
230 }
231
232 private:
233 OutputASM *const assembler;
234 };
235
236 class Constant : public TIntermConstantUnion
237 {
238 public:
239 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
240 {
241 constants[0].setFConst(x);
242 constants[1].setFConst(y);
243 constants[2].setFConst(z);
244 constants[3].setFConst(w);
245 }
246
247 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
248 {
249 constants[0].setBConst(b);
250 }
251
252 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
253 {
254 constants[0].setIConst(i);
255 }
256
257 ~Constant()
258 {
259 }
260
261 private:
262 ConstantUnion constants[4];
263 };
264
Alexis Hetu924513c2018-01-05 15:48:12 -0500265 ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
266 type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
267 name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
268 {
269 if(type.isStruct())
270 {
271 for(const auto& field : type.getStruct()->fields())
272 {
273 fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
274 }
275 }
276 }
277
278 Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
279 ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400280 {
281 }
282
283 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
284 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
285 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
286 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
287 {
288 }
289
Alexis Hetud2742532018-01-23 16:53:41 -0500290 BlockLayoutEncoder::BlockLayoutEncoder()
291 : mCurrentOffset(0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400292 {
293 }
294
295 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
296 {
297 int arrayStride;
298 int matrixStride;
299
Alexis Hetud2742532018-01-23 16:53:41 -0500300 bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
301 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400302
303 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
304 static_cast<int>(arrayStride * BytesPerComponent),
305 static_cast<int>(matrixStride * BytesPerComponent),
Alexis Hetud2742532018-01-23 16:53:41 -0500306 (matrixStride > 0) && isRowMajor);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400307
Alexis Hetud2742532018-01-23 16:53:41 -0500308 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400309
310 return memberInfo;
311 }
312
313 // static
314 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
315 {
316 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
317 }
318
319 // static
320 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
321 {
322 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
323 }
324
325 void BlockLayoutEncoder::nextRegister()
326 {
327 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
328 }
329
Alexis Hetud2742532018-01-23 16:53:41 -0500330 Std140BlockEncoder::Std140BlockEncoder() : BlockLayoutEncoder()
Nicolas Capens0bac2852016-05-07 06:09:58 -0400331 {
332 }
333
334 void Std140BlockEncoder::enterAggregateType()
335 {
336 nextRegister();
337 }
338
339 void Std140BlockEncoder::exitAggregateType()
340 {
341 nextRegister();
342 }
343
344 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
345 {
346 size_t baseAlignment = 0;
347 int matrixStride = 0;
348 int arrayStride = 0;
349
350 if(type.isMatrix())
351 {
352 baseAlignment = ComponentsPerRegister;
353 matrixStride = ComponentsPerRegister;
354
355 if(arraySize > 0)
356 {
357 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
358 arrayStride = ComponentsPerRegister * numRegisters;
359 }
360 }
361 else if(arraySize > 0)
362 {
363 baseAlignment = ComponentsPerRegister;
364 arrayStride = ComponentsPerRegister;
365 }
366 else
367 {
368 const size_t numComponents = type.getElementSize();
369 baseAlignment = (numComponents == 3 ? 4u : numComponents);
370 }
371
372 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
373
374 *matrixStrideOut = matrixStride;
375 *arrayStrideOut = arrayStride;
376 }
377
378 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
379 {
380 if(arraySize > 0)
381 {
382 mCurrentOffset += arrayStride * arraySize;
383 }
384 else if(type.isMatrix())
385 {
386 ASSERT(matrixStride == ComponentsPerRegister);
387 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
388 mCurrentOffset += ComponentsPerRegister * numRegisters;
389 }
390 else
391 {
392 mCurrentOffset += type.getElementSize();
393 }
394 }
395
396 Attribute::Attribute()
397 {
398 type = GL_NONE;
399 arraySize = 0;
400 registerIndex = 0;
401 }
402
Nicolas Capens378c4342018-08-07 23:57:21 -0400403 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int layoutLocation, int registerIndex)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400404 {
405 this->type = type;
406 this->name = name;
407 this->arraySize = arraySize;
Nicolas Capens378c4342018-08-07 23:57:21 -0400408 this->layoutLocation = layoutLocation;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400409 this->registerIndex = registerIndex;
410 }
411
412 sw::PixelShader *Shader::getPixelShader() const
413 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500414 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400415 }
416
417 sw::VertexShader *Shader::getVertexShader() const
418 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500419 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400420 }
421
422 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
423 {
424 TString name = TFunction::unmangleName(nodeName);
425
Alexis Hetuc0632c92018-03-02 15:10:16 -0500426 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D" || name == "texture2DRect")
Nicolas Capens0bac2852016-05-07 06:09:58 -0400427 {
428 method = IMPLICIT;
429 }
Alexis Hetuc0632c92018-03-02 15:10:16 -0500430 else if(name == "texture2DProj" || name == "textureProj" || name == "texture2DRectProj")
Nicolas Capens0bac2852016-05-07 06:09:58 -0400431 {
432 method = IMPLICIT;
433 proj = true;
434 }
435 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
436 {
437 method = LOD;
438 }
439 else if(name == "texture2DProjLod" || name == "textureProjLod")
440 {
441 method = LOD;
442 proj = true;
443 }
444 else if(name == "textureSize")
445 {
446 method = SIZE;
447 }
448 else if(name == "textureOffset")
449 {
450 method = IMPLICIT;
451 offset = true;
452 }
453 else if(name == "textureProjOffset")
454 {
455 method = IMPLICIT;
456 offset = true;
457 proj = true;
458 }
459 else if(name == "textureLodOffset")
460 {
461 method = LOD;
462 offset = true;
463 }
464 else if(name == "textureProjLodOffset")
465 {
466 method = LOD;
467 proj = true;
468 offset = true;
469 }
470 else if(name == "texelFetch")
471 {
472 method = FETCH;
473 }
474 else if(name == "texelFetchOffset")
475 {
476 method = FETCH;
477 offset = true;
478 }
479 else if(name == "textureGrad")
480 {
481 method = GRAD;
482 }
483 else if(name == "textureGradOffset")
484 {
485 method = GRAD;
486 offset = true;
487 }
488 else if(name == "textureProjGrad")
489 {
490 method = GRAD;
491 proj = true;
492 }
493 else if(name == "textureProjGradOffset")
494 {
495 method = GRAD;
496 proj = true;
497 offset = true;
498 }
499 else UNREACHABLE(0);
500 }
501
502 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
503 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500504 shader = nullptr;
505 pixelShader = nullptr;
506 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400507
508 if(shaderObject)
509 {
510 shader = shaderObject->getShader();
511 pixelShader = shaderObject->getPixelShader();
512 vertexShader = shaderObject->getVertexShader();
513 }
514
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500515 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400516 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500517 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400518 }
519
520 OutputASM::~OutputASM()
521 {
522 }
523
524 void OutputASM::output()
525 {
526 if(shader)
527 {
528 emitShader(GLOBAL);
529
530 if(functionArray.size() > 1) // Only call main() when there are other functions
531 {
532 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
533 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
534 callMain->dst.index = 0; // main()
535
536 emit(sw::Shader::OPCODE_RET);
537 }
538
539 emitShader(FUNCTION);
540 }
541 }
542
543 void OutputASM::emitShader(Scope scope)
544 {
545 emitScope = scope;
546 currentScope = GLOBAL;
547 mContext.getTreeRoot()->traverse(this);
548 }
549
550 void OutputASM::freeTemporary(Temporary *temporary)
551 {
552 free(temporaries, temporary);
553 }
554
555 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
556 {
557 TBasicType baseType = in->getType().getBasicType();
558
559 switch(op)
560 {
561 case sw::Shader::OPCODE_NEG:
562 switch(baseType)
563 {
564 case EbtInt:
565 case EbtUInt:
566 return sw::Shader::OPCODE_INEG;
567 case EbtFloat:
568 default:
569 return op;
570 }
571 case sw::Shader::OPCODE_ABS:
572 switch(baseType)
573 {
574 case EbtInt:
575 return sw::Shader::OPCODE_IABS;
576 case EbtFloat:
577 default:
578 return op;
579 }
580 case sw::Shader::OPCODE_SGN:
581 switch(baseType)
582 {
583 case EbtInt:
584 return sw::Shader::OPCODE_ISGN;
585 case EbtFloat:
586 default:
587 return op;
588 }
589 case sw::Shader::OPCODE_ADD:
590 switch(baseType)
591 {
592 case EbtInt:
593 case EbtUInt:
594 return sw::Shader::OPCODE_IADD;
595 case EbtFloat:
596 default:
597 return op;
598 }
599 case sw::Shader::OPCODE_SUB:
600 switch(baseType)
601 {
602 case EbtInt:
603 case EbtUInt:
604 return sw::Shader::OPCODE_ISUB;
605 case EbtFloat:
606 default:
607 return op;
608 }
609 case sw::Shader::OPCODE_MUL:
610 switch(baseType)
611 {
612 case EbtInt:
613 case EbtUInt:
614 return sw::Shader::OPCODE_IMUL;
615 case EbtFloat:
616 default:
617 return op;
618 }
619 case sw::Shader::OPCODE_DIV:
620 switch(baseType)
621 {
622 case EbtInt:
623 return sw::Shader::OPCODE_IDIV;
624 case EbtUInt:
625 return sw::Shader::OPCODE_UDIV;
626 case EbtFloat:
627 default:
628 return op;
629 }
630 case sw::Shader::OPCODE_IMOD:
631 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
632 case sw::Shader::OPCODE_ISHR:
633 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
634 case sw::Shader::OPCODE_MIN:
635 switch(baseType)
636 {
637 case EbtInt:
638 return sw::Shader::OPCODE_IMIN;
639 case EbtUInt:
640 return sw::Shader::OPCODE_UMIN;
641 case EbtFloat:
642 default:
643 return op;
644 }
645 case sw::Shader::OPCODE_MAX:
646 switch(baseType)
647 {
648 case EbtInt:
649 return sw::Shader::OPCODE_IMAX;
650 case EbtUInt:
651 return sw::Shader::OPCODE_UMAX;
652 case EbtFloat:
653 default:
654 return op;
655 }
656 default:
657 return op;
658 }
659 }
660
661 void OutputASM::visitSymbol(TIntermSymbol *symbol)
662 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500663 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
664 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
665 switch(symbol->getQualifier())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400666 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500667 case EvqVaryingIn:
668 case EvqVaryingOut:
669 case EvqInvariantVaryingIn:
670 case EvqInvariantVaryingOut:
671 case EvqVertexOut:
672 case EvqFragmentIn:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400673 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
674 {
675 declareVarying(symbol, -1);
676 }
Nicolas Capens6896e352018-01-10 12:46:52 -0500677 break;
Alexis Hetu930df972018-01-30 16:54:13 -0500678 case EvqFragmentOut:
679 declareFragmentOutput(symbol);
680 break;
Nicolas Capens6896e352018-01-10 12:46:52 -0500681 default:
682 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400683 }
684
685 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
686 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
687 // "All members of a named uniform block declared with a shared or std140 layout qualifier
688 // are considered active, even if they are not referenced in any shader in the program.
689 // The uniform block itself is also considered active, even if no member of the block is referenced."
690 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
691 {
692 uniformRegister(symbol);
693 }
694 }
695
696 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
697 {
698 if(currentScope != emitScope)
699 {
700 return false;
701 }
702
703 TIntermTyped *result = node;
704 TIntermTyped *left = node->getLeft();
705 TIntermTyped *right = node->getRight();
706 const TType &leftType = left->getType();
707 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400708
709 if(isSamplerRegister(result))
710 {
711 return false; // Don't traverse, the register index is determined statically
712 }
713
714 switch(node->getOp())
715 {
716 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500717 assert(visit == PreVisit);
718 right->traverse(this);
719 assignLvalue(left, right);
720 copy(result, right);
721 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400722 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500723 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500724 // Constant arrays go into the constant register file.
725 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
726 {
727 for(int i = 0; i < left->totalRegisterCount(); i++)
728 {
729 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
730 }
731 }
732 else
733 {
734 right->traverse(this);
735 copy(left, right);
736 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500737 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400738 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500739 assert(visit == PreVisit);
740 right->traverse(this);
741 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400742 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500743 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400744 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500745
746 assignLvalue(left, result);
747 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400748 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500749 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400750 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500751 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400752 int size = leftType.getNominalSize();
753
754 for(int i = 0; i < size; i++)
755 {
756 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
757 dot->dst.mask = 1 << i;
758 }
759
760 assignLvalue(left, result);
761 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500762 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400763 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500764 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400765 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500766 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400767 int dim = leftType.getNominalSize();
768
769 for(int i = 0; i < dim; i++)
770 {
771 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
772 mul->src[1].swizzle = 0x00;
773
774 for(int j = 1; j < dim; j++)
775 {
776 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
777 mad->src[1].swizzle = j * 0x55;
778 }
779 }
780
781 assignLvalue(left, result);
782 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500783 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400784 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400785 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400786 case EOpIndexDirectStruct:
787 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500788 assert(visit == PreVisit);
789 evaluateRvalue(node);
790 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400791 case EOpVectorSwizzle:
792 if(visit == PostVisit)
793 {
794 int swizzle = 0;
795 TIntermAggregate *components = right->getAsAggregate();
796
797 if(components)
798 {
799 TIntermSequence &sequence = components->getSequence();
800 int component = 0;
801
802 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
803 {
804 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
805
806 if(element)
807 {
808 int i = element->getUnionArrayPointer()[0].getIConst();
809 swizzle |= i << (component * 2);
810 component++;
811 }
812 else UNREACHABLE(0);
813 }
814 }
815 else UNREACHABLE(0);
816
817 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
818 mov->src[0].swizzle = swizzle;
819 }
820 break;
821 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
822 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
823 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
824 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
825 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
826 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
827 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
828 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
829 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
830 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
831 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
832 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
833 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
834 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
835 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
836 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
837 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
838 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
839 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
840 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
841 case EOpEqual:
842 if(visit == PostVisit)
843 {
844 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
845
846 for(int index = 1; index < left->totalRegisterCount(); index++)
847 {
848 Temporary equal(this);
849 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
850 emit(sw::Shader::OPCODE_AND, result, result, &equal);
851 }
852 }
853 break;
854 case EOpNotEqual:
855 if(visit == PostVisit)
856 {
857 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
858
859 for(int index = 1; index < left->totalRegisterCount(); index++)
860 {
861 Temporary notEqual(this);
862 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
863 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
864 }
865 }
866 break;
867 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
868 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
869 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
870 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
871 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
872 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
873 case EOpMatrixTimesScalar:
874 if(visit == PostVisit)
875 {
876 if(left->isMatrix())
877 {
878 for(int i = 0; i < leftType.getNominalSize(); i++)
879 {
880 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
881 }
882 }
883 else if(right->isMatrix())
884 {
885 for(int i = 0; i < rightType.getNominalSize(); i++)
886 {
887 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
888 }
889 }
890 else UNREACHABLE(0);
891 }
892 break;
893 case EOpVectorTimesMatrix:
894 if(visit == PostVisit)
895 {
896 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
897
898 int size = rightType.getNominalSize();
899 for(int i = 0; i < size; i++)
900 {
901 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
902 dot->dst.mask = 1 << i;
903 }
904 }
905 break;
906 case EOpMatrixTimesVector:
907 if(visit == PostVisit)
908 {
909 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
910 mul->src[1].swizzle = 0x00;
911
912 int size = rightType.getNominalSize();
913 for(int i = 1; i < size; i++)
914 {
915 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
916 mad->src[1].swizzle = i * 0x55;
917 }
918 }
919 break;
920 case EOpMatrixTimesMatrix:
921 if(visit == PostVisit)
922 {
923 int dim = leftType.getNominalSize();
924
925 int size = rightType.getNominalSize();
926 for(int i = 0; i < size; i++)
927 {
928 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
929 mul->src[1].swizzle = 0x00;
930
931 for(int j = 1; j < dim; j++)
932 {
933 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
934 mad->src[1].swizzle = j * 0x55;
935 }
936 }
937 }
938 break;
939 case EOpLogicalOr:
940 if(trivial(right, 6))
941 {
942 if(visit == PostVisit)
943 {
944 emit(sw::Shader::OPCODE_OR, result, left, right);
945 }
946 }
947 else // Short-circuit evaluation
948 {
949 if(visit == InVisit)
950 {
951 emit(sw::Shader::OPCODE_MOV, result, left);
952 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
953 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
954 }
955 else if(visit == PostVisit)
956 {
957 emit(sw::Shader::OPCODE_MOV, result, right);
958 emit(sw::Shader::OPCODE_ENDIF);
959 }
960 }
961 break;
962 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
963 case EOpLogicalAnd:
964 if(trivial(right, 6))
965 {
966 if(visit == PostVisit)
967 {
968 emit(sw::Shader::OPCODE_AND, result, left, right);
969 }
970 }
971 else // Short-circuit evaluation
972 {
973 if(visit == InVisit)
974 {
975 emit(sw::Shader::OPCODE_MOV, result, left);
976 emit(sw::Shader::OPCODE_IF, 0, result);
977 }
978 else if(visit == PostVisit)
979 {
980 emit(sw::Shader::OPCODE_MOV, result, right);
981 emit(sw::Shader::OPCODE_ENDIF);
982 }
983 }
984 break;
985 default: UNREACHABLE(node->getOp());
986 }
987
988 return true;
989 }
990
991 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
992 {
993 switch(size)
994 {
995 case 1: // Used for cofactor computation only
996 {
997 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
998 bool isMov = (row == col);
999 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
1000 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
1001 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1002 mov->dst.mask = 1 << outRow;
1003 }
1004 break;
1005 case 2:
1006 {
1007 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1008
1009 bool isCofactor = (col >= 0) && (row >= 0);
1010 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1011 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1012 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1013
1014 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1015 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1016 det->dst.mask = 1 << outRow;
1017 }
1018 break;
1019 case 3:
1020 {
1021 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1022
1023 bool isCofactor = (col >= 0) && (row >= 0);
1024 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1025 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1026 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1027 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1028
1029 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1030 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1031 det->dst.mask = 1 << outRow;
1032 }
1033 break;
1034 case 4:
1035 {
1036 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1037 det->dst.mask = 1 << outRow;
1038 }
1039 break;
1040 default:
1041 UNREACHABLE(size);
1042 break;
1043 }
1044 }
1045
1046 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1047 {
1048 if(currentScope != emitScope)
1049 {
1050 return false;
1051 }
1052
1053 TIntermTyped *result = node;
1054 TIntermTyped *arg = node->getOperand();
1055 TBasicType basicType = arg->getType().getBasicType();
1056
1057 union
1058 {
1059 float f;
1060 int i;
1061 } one_value;
1062
1063 if(basicType == EbtInt || basicType == EbtUInt)
1064 {
1065 one_value.i = 1;
1066 }
1067 else
1068 {
1069 one_value.f = 1.0f;
1070 }
1071
1072 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1073 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1074 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1075
1076 switch(node->getOp())
1077 {
1078 case EOpNegative:
1079 if(visit == PostVisit)
1080 {
1081 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1082 for(int index = 0; index < arg->totalRegisterCount(); index++)
1083 {
1084 emit(negOpcode, result, index, arg, index);
1085 }
1086 }
1087 break;
1088 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1089 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001090 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001091 case EOpPostIncrement:
1092 if(visit == PostVisit)
1093 {
1094 copy(result, arg);
1095
1096 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1097 for(int index = 0; index < arg->totalRegisterCount(); index++)
1098 {
1099 emit(addOpcode, arg, index, arg, index, &one);
1100 }
1101
1102 assignLvalue(arg, arg);
1103 }
1104 break;
1105 case EOpPostDecrement:
1106 if(visit == PostVisit)
1107 {
1108 copy(result, arg);
1109
1110 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1111 for(int index = 0; index < arg->totalRegisterCount(); index++)
1112 {
1113 emit(subOpcode, arg, index, arg, index, &one);
1114 }
1115
1116 assignLvalue(arg, arg);
1117 }
1118 break;
1119 case EOpPreIncrement:
1120 if(visit == PostVisit)
1121 {
1122 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1123 for(int index = 0; index < arg->totalRegisterCount(); index++)
1124 {
1125 emit(addOpcode, result, index, arg, index, &one);
1126 }
1127
1128 assignLvalue(arg, result);
1129 }
1130 break;
1131 case EOpPreDecrement:
1132 if(visit == PostVisit)
1133 {
1134 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1135 for(int index = 0; index < arg->totalRegisterCount(); index++)
1136 {
1137 emit(subOpcode, result, index, arg, index, &one);
1138 }
1139
1140 assignLvalue(arg, result);
1141 }
1142 break;
1143 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1144 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1145 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1146 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1147 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1148 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1149 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1150 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1151 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1152 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1153 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1154 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1155 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1156 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1157 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1158 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1159 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1160 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1161 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1162 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1163 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1164 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1165 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1166 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1167 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1168 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1169 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1170 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1171 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1172 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1173 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1174 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1175 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1176 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1177 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1178 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1179 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1180 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1181 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1182 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1183 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1184 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1185 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1186 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1187 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1188 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1189 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1190 case EOpTranspose:
1191 if(visit == PostVisit)
1192 {
1193 int numCols = arg->getNominalSize();
1194 int numRows = arg->getSecondarySize();
1195 for(int i = 0; i < numCols; ++i)
1196 {
1197 for(int j = 0; j < numRows; ++j)
1198 {
1199 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1200 mov->src[0].swizzle = 0x55 * j;
1201 mov->dst.mask = 1 << i;
1202 }
1203 }
1204 }
1205 break;
1206 case EOpDeterminant:
1207 if(visit == PostVisit)
1208 {
1209 int size = arg->getNominalSize();
1210 ASSERT(size == arg->getSecondarySize());
1211
1212 emitDeterminant(result, arg, size);
1213 }
1214 break;
1215 case EOpInverse:
1216 if(visit == PostVisit)
1217 {
1218 int size = arg->getNominalSize();
1219 ASSERT(size == arg->getSecondarySize());
1220
1221 // Compute transposed matrix of cofactors
1222 for(int i = 0; i < size; ++i)
1223 {
1224 for(int j = 0; j < size; ++j)
1225 {
1226 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1227 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1228 emitDeterminant(result, arg, size - 1, j, i, i, j);
1229 }
1230 }
1231
1232 // Compute 1 / determinant
1233 Temporary invDet(this);
1234 emitDeterminant(&invDet, arg, size);
1235 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1236 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1237 div->src[1].swizzle = 0x00; // xxxx
1238
1239 // Divide transposed matrix of cofactors by determinant
1240 for(int i = 0; i < size; ++i)
1241 {
1242 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1243 }
1244 }
1245 break;
1246 default: UNREACHABLE(node->getOp());
1247 }
1248
1249 return true;
1250 }
1251
1252 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1253 {
1254 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1255 {
1256 return false;
1257 }
1258
1259 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1260
1261 TIntermTyped *result = node;
1262 const TType &resultType = node->getType();
1263 TIntermSequence &arg = node->getSequence();
1264 size_t argumentCount = arg.size();
1265
1266 switch(node->getOp())
1267 {
1268 case EOpSequence: break;
1269 case EOpDeclaration: break;
1270 case EOpInvariantDeclaration: break;
1271 case EOpPrototype: break;
1272 case EOpComma:
1273 if(visit == PostVisit)
1274 {
1275 copy(result, arg[1]);
1276 }
1277 break;
1278 case EOpFunction:
1279 if(visit == PreVisit)
1280 {
1281 const TString &name = node->getName();
1282
1283 if(emitScope == FUNCTION)
1284 {
1285 if(functionArray.size() > 1) // No need for a label when there's only main()
1286 {
1287 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1288 label->dst.type = sw::Shader::PARAMETER_LABEL;
1289
1290 const Function *function = findFunction(name);
1291 ASSERT(function); // Should have been added during global pass
1292 label->dst.index = function->label;
1293 currentFunction = function->label;
1294 }
1295 }
1296 else if(emitScope == GLOBAL)
1297 {
1298 if(name != "main(")
1299 {
1300 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1301 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1302 }
1303 }
1304 else UNREACHABLE(emitScope);
1305
1306 currentScope = FUNCTION;
1307 }
1308 else if(visit == PostVisit)
1309 {
1310 if(emitScope == FUNCTION)
1311 {
1312 if(functionArray.size() > 1) // No need to return when there's only main()
1313 {
1314 emit(sw::Shader::OPCODE_RET);
1315 }
1316 }
1317
1318 currentScope = GLOBAL;
1319 }
1320 break;
1321 case EOpFunctionCall:
1322 if(visit == PostVisit)
1323 {
1324 if(node->isUserDefined())
1325 {
1326 const TString &name = node->getName();
1327 const Function *function = findFunction(name);
1328
1329 if(!function)
1330 {
1331 mContext.error(node->getLine(), "function definition not found", name.c_str());
1332 return false;
1333 }
1334
1335 TIntermSequence &arguments = *function->arg;
1336
1337 for(size_t i = 0; i < argumentCount; i++)
1338 {
1339 TIntermTyped *in = arguments[i]->getAsTyped();
1340
1341 if(in->getQualifier() == EvqIn ||
1342 in->getQualifier() == EvqInOut ||
1343 in->getQualifier() == EvqConstReadOnly)
1344 {
1345 copy(in, arg[i]);
1346 }
1347 }
1348
1349 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1350 call->dst.type = sw::Shader::PARAMETER_LABEL;
1351 call->dst.index = function->label;
1352
1353 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1354 {
1355 copy(result, function->ret);
1356 }
1357
1358 for(size_t i = 0; i < argumentCount; i++)
1359 {
1360 TIntermTyped *argument = arguments[i]->getAsTyped();
1361 TIntermTyped *out = arg[i]->getAsTyped();
1362
1363 if(argument->getQualifier() == EvqOut ||
1364 argument->getQualifier() == EvqInOut)
1365 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001366 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001367 }
1368 }
1369 }
1370 else
1371 {
1372 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001373 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001374 TIntermTyped *t = arg[1]->getAsTyped();
1375
1376 Temporary coord(this);
1377
1378 if(textureFunction.proj)
1379 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001380 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1381 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1382 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001383
Nicolas Capens0484c792016-06-13 22:02:36 -04001384 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1385 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001386
1387 if(IsShadowSampler(s->getBasicType()))
1388 {
1389 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1390 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1391 mov->src[0].swizzle = 0xA4;
1392 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001393 }
1394 else
1395 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001396 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1397
1398 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1399 {
1400 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1401 mov->src[0].swizzle = 0xA4;
1402 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001403 }
1404
1405 switch(textureFunction.method)
1406 {
1407 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001408 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001409 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001410 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001411 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001412 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001413 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001414 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001415 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001416 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1417 }
1418 else UNREACHABLE(argumentCount);
1419 }
1420 else // Offset
1421 {
1422 if(argumentCount == 3)
1423 {
1424 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1425 }
1426 else if(argumentCount == 4) // Bias
1427 {
1428 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001429 }
1430 else UNREACHABLE(argumentCount);
1431 }
1432 break;
1433 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001434 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001435 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001436 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001437 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001438 else if(argumentCount == 4) // Offset
1439 {
1440 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1441 }
1442 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001443 break;
1444 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001445 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001446 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001447 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001448 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001449 else if(argumentCount == 4) // Offset
1450 {
1451 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1452 }
1453 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001454 break;
1455 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001456 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001457 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001458 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001459 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001460 else if(argumentCount == 5) // Offset
1461 {
1462 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1463 }
1464 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001465 break;
1466 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001467 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001468 break;
1469 default:
1470 UNREACHABLE(textureFunction.method);
1471 }
1472 }
1473 }
1474 break;
1475 case EOpParameters:
1476 break;
1477 case EOpConstructFloat:
1478 case EOpConstructVec2:
1479 case EOpConstructVec3:
1480 case EOpConstructVec4:
1481 case EOpConstructBool:
1482 case EOpConstructBVec2:
1483 case EOpConstructBVec3:
1484 case EOpConstructBVec4:
1485 case EOpConstructInt:
1486 case EOpConstructIVec2:
1487 case EOpConstructIVec3:
1488 case EOpConstructIVec4:
1489 case EOpConstructUInt:
1490 case EOpConstructUVec2:
1491 case EOpConstructUVec3:
1492 case EOpConstructUVec4:
1493 if(visit == PostVisit)
1494 {
1495 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001496 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1497 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001498 for(size_t i = 0; i < argumentCount; i++)
1499 {
1500 TIntermTyped *argi = arg[i]->getAsTyped();
1501 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001502 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1503 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001504
1505 if(!argi->isMatrix())
1506 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001507 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1508 mov->dst.mask = (0xF << swizzle) & 0xF;
1509 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001510
1511 component += size;
1512 }
Alexis Hetu38338612018-01-18 15:53:36 -05001513 else if(!result->isMatrix()) // Construct a non matrix from a matrix
1514 {
1515 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1516 mov->dst.mask = (0xF << swizzle) & 0xF;
1517 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1518
1519 // At most one more instruction when constructing a vec3 from a mat2 or a vec4 from a mat2/mat3
1520 if(result->getNominalSize() > size)
1521 {
1522 Instruction *mov = emitCast(result, arrayIndex, argi, 1);
1523 mov->dst.mask = (0xF << (swizzle + size)) & 0xF;
1524 // mat2: xxxy (0x40), mat3: xxxx (0x00)
1525 mov->src[0].swizzle = ((size == 2) ? 0x40 : 0x00) << (swizzle * 2);
1526 }
1527
1528 component += size;
1529 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001530 else // Matrix
1531 {
1532 int column = 0;
1533
1534 while(component < resultType.getNominalSize())
1535 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001536 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1537 mov->dst.mask = (0xF << swizzle) & 0xF;
1538 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001539
1540 column++;
1541 component += size;
1542 }
1543 }
1544 }
1545 }
1546 break;
1547 case EOpConstructMat2:
1548 case EOpConstructMat2x3:
1549 case EOpConstructMat2x4:
1550 case EOpConstructMat3x2:
1551 case EOpConstructMat3:
1552 case EOpConstructMat3x4:
1553 case EOpConstructMat4x2:
1554 case EOpConstructMat4x3:
1555 case EOpConstructMat4:
1556 if(visit == PostVisit)
1557 {
1558 TIntermTyped *arg0 = arg[0]->getAsTyped();
1559 const int outCols = result->getNominalSize();
1560 const int outRows = result->getSecondarySize();
1561
1562 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1563 {
1564 for(int i = 0; i < outCols; i++)
1565 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001566 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001567 Instruction *mov = emitCast(result, i, arg0, 0);
1568 mov->dst.mask = 1 << i;
1569 ASSERT(mov->src[0].swizzle == 0x00);
1570 }
1571 }
1572 else if(arg0->isMatrix())
1573 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001574 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001575
Alexis Hetu2a198552016-09-27 20:50:45 -04001576 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001577 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001578 TIntermTyped *argi = arg[n]->getAsTyped();
1579 const int inCols = argi->getNominalSize();
1580 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001581
Alexis Hetu2a198552016-09-27 20:50:45 -04001582 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001583 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001584 if(i >= inCols || outRows > inRows)
1585 {
1586 // Initialize to identity matrix
1587 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));
1588 emitCast(result, i + n * outCols, &col, 0);
1589 }
1590
1591 if(i < inCols)
1592 {
1593 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1594 mov->dst.mask = 0xF >> (4 - inRows);
1595 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001596 }
1597 }
1598 }
1599 else
1600 {
1601 int column = 0;
1602 int row = 0;
1603
1604 for(size_t i = 0; i < argumentCount; i++)
1605 {
1606 TIntermTyped *argi = arg[i]->getAsTyped();
1607 int size = argi->getNominalSize();
1608 int element = 0;
1609
1610 while(element < size)
1611 {
1612 Instruction *mov = emitCast(result, column, argi, 0);
1613 mov->dst.mask = (0xF << row) & 0xF;
1614 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1615
1616 int end = row + size - element;
1617 column = end >= outRows ? column + 1 : column;
1618 element = element + outRows - row;
1619 row = end >= outRows ? 0 : end;
1620 }
1621 }
1622 }
1623 }
1624 break;
1625 case EOpConstructStruct:
1626 if(visit == PostVisit)
1627 {
1628 int offset = 0;
1629 for(size_t i = 0; i < argumentCount; i++)
1630 {
1631 TIntermTyped *argi = arg[i]->getAsTyped();
1632 int size = argi->totalRegisterCount();
1633
1634 for(int index = 0; index < size; index++)
1635 {
1636 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1637 mov->dst.mask = writeMask(result, offset + index);
1638 }
1639
1640 offset += size;
1641 }
1642 }
1643 break;
1644 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1645 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1646 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1647 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1648 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1649 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1650 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1651 case EOpModf:
1652 if(visit == PostVisit)
1653 {
1654 TIntermTyped* arg1 = arg[1]->getAsTyped();
1655 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1656 assignLvalue(arg1, arg1);
1657 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1658 }
1659 break;
1660 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1661 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1662 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1663 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1664 case EOpClamp:
1665 if(visit == PostVisit)
1666 {
1667 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1668 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1669 }
1670 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001671 case EOpMix:
1672 if(visit == PostVisit)
1673 {
1674 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1675 {
1676 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1677 }
1678 else
1679 {
1680 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1681 }
1682 }
1683 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001684 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1685 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1686 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1687 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1688 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1689 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1690 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1691 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1692 case EOpMul:
1693 if(visit == PostVisit)
1694 {
1695 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001696 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1697 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001698
1699 int size = arg0->getNominalSize();
1700 for(int i = 0; i < size; i++)
1701 {
1702 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1703 }
1704 }
1705 break;
1706 case EOpOuterProduct:
1707 if(visit == PostVisit)
1708 {
1709 for(int i = 0; i < dim(arg[1]); i++)
1710 {
1711 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1712 mul->src[1].swizzle = 0x55 * i;
1713 }
1714 }
1715 break;
1716 default: UNREACHABLE(node->getOp());
1717 }
1718
1719 return true;
1720 }
1721
1722 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1723 {
1724 if(currentScope != emitScope)
1725 {
1726 return false;
1727 }
1728
1729 TIntermTyped *condition = node->getCondition();
1730 TIntermNode *trueBlock = node->getTrueBlock();
1731 TIntermNode *falseBlock = node->getFalseBlock();
1732 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1733
1734 condition->traverse(this);
1735
1736 if(node->usesTernaryOperator())
1737 {
1738 if(constantCondition)
1739 {
1740 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1741
1742 if(trueCondition)
1743 {
1744 trueBlock->traverse(this);
1745 copy(node, trueBlock);
1746 }
1747 else
1748 {
1749 falseBlock->traverse(this);
1750 copy(node, falseBlock);
1751 }
1752 }
1753 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1754 {
1755 trueBlock->traverse(this);
1756 falseBlock->traverse(this);
1757 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1758 }
1759 else
1760 {
1761 emit(sw::Shader::OPCODE_IF, 0, condition);
1762
1763 if(trueBlock)
1764 {
1765 trueBlock->traverse(this);
1766 copy(node, trueBlock);
1767 }
1768
1769 if(falseBlock)
1770 {
1771 emit(sw::Shader::OPCODE_ELSE);
1772 falseBlock->traverse(this);
1773 copy(node, falseBlock);
1774 }
1775
1776 emit(sw::Shader::OPCODE_ENDIF);
1777 }
1778 }
1779 else // if/else statement
1780 {
1781 if(constantCondition)
1782 {
1783 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1784
1785 if(trueCondition)
1786 {
1787 if(trueBlock)
1788 {
1789 trueBlock->traverse(this);
1790 }
1791 }
1792 else
1793 {
1794 if(falseBlock)
1795 {
1796 falseBlock->traverse(this);
1797 }
1798 }
1799 }
1800 else
1801 {
1802 emit(sw::Shader::OPCODE_IF, 0, condition);
1803
1804 if(trueBlock)
1805 {
1806 trueBlock->traverse(this);
1807 }
1808
1809 if(falseBlock)
1810 {
1811 emit(sw::Shader::OPCODE_ELSE);
1812 falseBlock->traverse(this);
1813 }
1814
1815 emit(sw::Shader::OPCODE_ENDIF);
1816 }
1817 }
1818
1819 return false;
1820 }
1821
1822 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1823 {
1824 if(currentScope != emitScope)
1825 {
1826 return false;
1827 }
1828
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001829 LoopInfo loop(node);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001830
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001831 if(loop.iterations == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001832 {
1833 return false;
1834 }
1835
Nicolas Capens4b743732018-05-28 13:22:07 -04001836 if(loop.isDeterministic())
1837 {
1838 deterministicVariables.insert(loop.index->getId());
1839 }
1840
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001841 bool unroll = (loop.iterations <= 4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001842
1843 TIntermNode *init = node->getInit();
1844 TIntermTyped *condition = node->getCondition();
1845 TIntermTyped *expression = node->getExpression();
1846 TIntermNode *body = node->getBody();
1847 Constant True(true);
1848
1849 if(node->getType() == ELoopDoWhile)
1850 {
1851 Temporary iterate(this);
1852 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1853
1854 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1855
1856 if(body)
1857 {
1858 body->traverse(this);
1859 }
1860
1861 emit(sw::Shader::OPCODE_TEST);
1862
1863 condition->traverse(this);
1864 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1865
1866 emit(sw::Shader::OPCODE_ENDWHILE);
1867 }
1868 else
1869 {
1870 if(init)
1871 {
1872 init->traverse(this);
1873 }
1874
1875 if(unroll)
1876 {
Nicolas Capens493fc542018-05-29 17:11:37 -04001877 mContext.info(node->getLine(), "loop unrolled", "for");
1878
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04001879 for(unsigned int i = 0; i < loop.iterations; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001880 {
1881 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1882
1883 if(body)
1884 {
1885 body->traverse(this);
1886 }
1887
1888 if(expression)
1889 {
1890 expression->traverse(this);
1891 }
1892 }
1893 }
1894 else
1895 {
1896 if(condition)
1897 {
1898 condition->traverse(this);
1899 }
1900 else
1901 {
1902 condition = &True;
1903 }
1904
1905 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1906
1907 if(body)
1908 {
1909 body->traverse(this);
1910 }
1911
1912 emit(sw::Shader::OPCODE_TEST);
1913
1914 if(expression)
1915 {
1916 expression->traverse(this);
1917 }
1918
1919 if(condition)
1920 {
1921 condition->traverse(this);
1922 }
1923
1924 emit(sw::Shader::OPCODE_ENDWHILE);
1925 }
1926 }
1927
Nicolas Capens4b743732018-05-28 13:22:07 -04001928 if(loop.isDeterministic())
1929 {
1930 deterministicVariables.erase(loop.index->getId());
1931 }
1932
Nicolas Capens0bac2852016-05-07 06:09:58 -04001933 return false;
1934 }
1935
1936 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1937 {
1938 if(currentScope != emitScope)
1939 {
1940 return false;
1941 }
1942
1943 switch(node->getFlowOp())
1944 {
1945 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1946 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1947 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1948 case EOpReturn:
1949 if(visit == PostVisit)
1950 {
1951 TIntermTyped *value = node->getExpression();
1952
1953 if(value)
1954 {
1955 copy(functionArray[currentFunction].ret, value);
1956 }
1957
1958 emit(sw::Shader::OPCODE_LEAVE);
1959 }
1960 break;
1961 default: UNREACHABLE(node->getFlowOp());
1962 }
1963
1964 return true;
1965 }
1966
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001967 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1968 {
1969 if(currentScope != emitScope)
1970 {
1971 return false;
1972 }
1973
1974 TIntermTyped* switchValue = node->getInit();
1975 TIntermAggregate* opList = node->getStatementList();
1976
1977 if(!switchValue || !opList)
1978 {
1979 return false;
1980 }
1981
1982 switchValue->traverse(this);
1983
1984 emit(sw::Shader::OPCODE_SWITCH);
1985
1986 TIntermSequence& sequence = opList->getSequence();
1987 TIntermSequence::iterator it = sequence.begin();
1988 TIntermSequence::iterator defaultIt = sequence.end();
1989 int nbCases = 0;
1990 for(; it != sequence.end(); ++it)
1991 {
1992 TIntermCase* currentCase = (*it)->getAsCaseNode();
1993 if(currentCase)
1994 {
1995 TIntermSequence::iterator caseIt = it;
1996
1997 TIntermTyped* condition = currentCase->getCondition();
1998 if(condition) // non default case
1999 {
2000 if(nbCases != 0)
2001 {
2002 emit(sw::Shader::OPCODE_ELSE);
2003 }
2004
2005 condition->traverse(this);
2006 Temporary result(this);
2007 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
2008 emit(sw::Shader::OPCODE_IF, 0, &result);
2009 nbCases++;
2010
Nicolas Capens6d123312018-01-08 12:57:52 -05002011 // Emit the code for this case and all subsequent cases until we hit a break statement.
2012 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002013 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2014 {
2015 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05002016
2017 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2018 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2019 // Note that this eliminates useless operations but shouldn't affect correctness.
2020 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002021 {
2022 break;
2023 }
2024 }
2025 }
2026 else
2027 {
2028 defaultIt = it; // The default case might not be the last case, keep it for last
2029 }
2030 }
2031 }
2032
2033 // If there's a default case, traverse it here
2034 if(defaultIt != sequence.end())
2035 {
2036 emit(sw::Shader::OPCODE_ELSE);
2037 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2038 {
2039 (*defaultIt)->traverse(this);
2040 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2041 {
2042 break;
2043 }
2044 }
2045 }
2046
2047 for(int i = 0; i < nbCases; ++i)
2048 {
2049 emit(sw::Shader::OPCODE_ENDIF);
2050 }
2051
2052 emit(sw::Shader::OPCODE_ENDSWITCH);
2053
2054 return false;
2055 }
2056
Nicolas Capens0bac2852016-05-07 06:09:58 -04002057 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2058 {
2059 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2060 }
2061
2062 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2063 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2064 {
2065 Instruction *instruction = new Instruction(op);
2066
2067 if(dst)
2068 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002069 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002070 }
2071
Alexis Hetu929c6b02017-11-07 16:04:25 -05002072 if(src0)
2073 {
2074 TIntermTyped* src = src0->getAsTyped();
2075 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2076 }
2077
Nicolas Capens0530b452017-11-15 16:39:47 -05002078 source(instruction->src[0], src0, index0);
2079 source(instruction->src[1], src1, index1);
2080 source(instruction->src[2], src2, index2);
2081 source(instruction->src[3], src3, index3);
2082 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002083
2084 shader->append(instruction);
2085
2086 return instruction;
2087 }
2088
2089 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2090 {
2091 return emitCast(dst, 0, src, 0);
2092 }
2093
2094 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2095 {
2096 switch(src->getBasicType())
2097 {
2098 case EbtBool:
2099 switch(dst->getBasicType())
2100 {
2101 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2102 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2103 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2104 default: break;
2105 }
2106 break;
2107 case EbtInt:
2108 switch(dst->getBasicType())
2109 {
2110 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2111 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2112 default: break;
2113 }
2114 break;
2115 case EbtUInt:
2116 switch(dst->getBasicType())
2117 {
2118 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2119 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2120 default: break;
2121 }
2122 break;
2123 case EbtFloat:
2124 switch(dst->getBasicType())
2125 {
2126 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2127 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2128 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2129 default: break;
2130 }
2131 break;
2132 default:
2133 break;
2134 }
2135
2136 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2137 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2138 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2139
2140 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2141 }
2142
2143 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2144 {
2145 for(int index = 0; index < dst->elementRegisterCount(); index++)
2146 {
2147 emit(op, dst, index, src0, index, src1, index, src2, index);
2148 }
2149 }
2150
2151 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2152 {
2153 emitBinary(op, result, src0, src1);
2154 assignLvalue(lhs, result);
2155 }
2156
2157 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2158 {
2159 sw::Shader::Opcode opcode;
2160 switch(left->getAsTyped()->getBasicType())
2161 {
2162 case EbtBool:
2163 case EbtInt:
2164 opcode = sw::Shader::OPCODE_ICMP;
2165 break;
2166 case EbtUInt:
2167 opcode = sw::Shader::OPCODE_UCMP;
2168 break;
2169 default:
2170 opcode = sw::Shader::OPCODE_CMP;
2171 break;
2172 }
2173
2174 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2175 cmp->control = cmpOp;
2176 }
2177
2178 int componentCount(const TType &type, int registers)
2179 {
2180 if(registers == 0)
2181 {
2182 return 0;
2183 }
2184
2185 if(type.isArray() && registers >= type.elementRegisterCount())
2186 {
2187 int index = registers / type.elementRegisterCount();
2188 registers -= index * type.elementRegisterCount();
2189 return index * type.getElementSize() + componentCount(type, registers);
2190 }
2191
2192 if(type.isStruct() || type.isInterfaceBlock())
2193 {
2194 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2195 int elements = 0;
2196
Alexis Hetuda163ed2018-01-03 16:36:14 -05002197 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002198 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002199 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002200
2201 if(fieldType.totalRegisterCount() <= registers)
2202 {
2203 registers -= fieldType.totalRegisterCount();
2204 elements += fieldType.getObjectSize();
2205 }
2206 else // Register within this field
2207 {
2208 return elements + componentCount(fieldType, registers);
2209 }
2210 }
2211 }
2212 else if(type.isMatrix())
2213 {
2214 return registers * type.registerSize();
2215 }
2216
2217 UNREACHABLE(0);
2218 return 0;
2219 }
2220
2221 int registerSize(const TType &type, int registers)
2222 {
2223 if(registers == 0)
2224 {
2225 if(type.isStruct())
2226 {
2227 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2228 }
2229 else if(type.isInterfaceBlock())
2230 {
2231 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2232 }
2233
2234 return type.registerSize();
2235 }
2236
2237 if(type.isArray() && registers >= type.elementRegisterCount())
2238 {
2239 int index = registers / type.elementRegisterCount();
2240 registers -= index * type.elementRegisterCount();
2241 return registerSize(type, registers);
2242 }
2243
2244 if(type.isStruct() || type.isInterfaceBlock())
2245 {
2246 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2247 int elements = 0;
2248
Alexis Hetuda163ed2018-01-03 16:36:14 -05002249 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002250 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002251 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002252
2253 if(fieldType.totalRegisterCount() <= registers)
2254 {
2255 registers -= fieldType.totalRegisterCount();
2256 elements += fieldType.getObjectSize();
2257 }
2258 else // Register within this field
2259 {
2260 return registerSize(fieldType, registers);
2261 }
2262 }
2263 }
2264 else if(type.isMatrix())
2265 {
2266 return registerSize(type, 0);
2267 }
2268
2269 UNREACHABLE(0);
2270 return 0;
2271 }
2272
2273 int OutputASM::getBlockId(TIntermTyped *arg)
2274 {
2275 if(arg)
2276 {
2277 const TType &type = arg->getType();
2278 TInterfaceBlock* block = type.getInterfaceBlock();
2279 if(block && (type.getQualifier() == EvqUniform))
2280 {
2281 // Make sure the uniform block is declared
2282 uniformRegister(arg);
2283
2284 const char* blockName = block->name().c_str();
2285
2286 // Fetch uniform block index from array of blocks
2287 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2288 {
2289 if(blockName == it->name)
2290 {
2291 return it->blockId;
2292 }
2293 }
2294
2295 ASSERT(false);
2296 }
2297 }
2298
2299 return -1;
2300 }
2301
2302 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2303 {
2304 const TType &type = arg->getType();
2305 int blockId = getBlockId(arg);
2306 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2307 if(blockId != -1)
2308 {
2309 argumentInfo.bufferIndex = 0;
2310 for(int i = 0; i < blockId; ++i)
2311 {
2312 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2313 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2314 }
2315
2316 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2317
2318 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2319 BlockDefinitionIndexMap::const_iterator it = itEnd;
2320
2321 argumentInfo.clampedIndex = index;
2322 if(type.isInterfaceBlock())
2323 {
2324 // Offset index to the beginning of the selected instance
2325 int blockRegisters = type.elementRegisterCount();
2326 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2327 argumentInfo.bufferIndex += bufferOffset;
2328 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2329 }
2330
2331 int regIndex = registerIndex(arg);
2332 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2333 {
2334 it = blockDefinition.find(i);
2335 if(it != itEnd)
2336 {
2337 argumentInfo.clampedIndex -= (i - regIndex);
2338 break;
2339 }
2340 }
2341 ASSERT(it != itEnd);
2342
2343 argumentInfo.typedMemberInfo = it->second;
2344
2345 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2346 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2347 }
2348 else
2349 {
2350 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2351 }
2352
2353 return argumentInfo;
2354 }
2355
Nicolas Capens0530b452017-11-15 16:39:47 -05002356 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002357 {
2358 if(argument)
2359 {
2360 TIntermTyped *arg = argument->getAsTyped();
2361 Temporary unpackedUniform(this);
2362
2363 const TType& srcType = arg->getType();
2364 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2365 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2366 {
2367 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2368 const TType &memberType = argumentInfo.typedMemberInfo.type;
2369
2370 if(memberType.getBasicType() == EbtBool)
2371 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002372 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002373
2374 // Convert the packed bool, which is currently an int, to a true bool
2375 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2376 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2377 instruction->dst.index = registerIndex(&unpackedUniform);
2378 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2379 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2380 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2381
2382 shader->append(instruction);
2383
2384 arg = &unpackedUniform;
2385 index = 0;
2386 }
Alexis Hetud2742532018-01-23 16:53:41 -05002387 else if((memberType.getLayoutQualifier().matrixPacking == EmpRowMajor) && memberType.isMatrix())
Nicolas Capens0bac2852016-05-07 06:09:58 -04002388 {
2389 int numCols = memberType.getNominalSize();
2390 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002391
Alexis Hetue97a31e2016-11-14 14:10:47 -05002392 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002393
2394 unsigned int dstIndex = registerIndex(&unpackedUniform);
2395 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2396 int arrayIndex = argumentInfo.clampedIndex / numCols;
2397 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2398
2399 for(int j = 0; j < numRows; ++j)
2400 {
2401 // Transpose the row major matrix
2402 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2403 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2404 instruction->dst.index = dstIndex;
2405 instruction->dst.mask = 1 << j;
2406 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2407 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2408 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2409 instruction->src[0].swizzle = srcSwizzle;
2410
2411 shader->append(instruction);
2412 }
2413
2414 arg = &unpackedUniform;
2415 index = 0;
2416 }
2417 }
2418
2419 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2420 const TType &type = argumentInfo.typedMemberInfo.type;
2421
2422 int size = registerSize(type, argumentInfo.clampedIndex);
2423
2424 parameter.type = registerType(arg);
2425 parameter.bufferIndex = argumentInfo.bufferIndex;
2426
2427 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2428 {
2429 int component = componentCount(type, argumentInfo.clampedIndex);
2430 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2431
2432 for(int i = 0; i < 4; i++)
2433 {
2434 if(size == 1) // Replicate
2435 {
2436 parameter.value[i] = constants[component + 0].getAsFloat();
2437 }
2438 else if(i < size)
2439 {
2440 parameter.value[i] = constants[component + i].getAsFloat();
2441 }
2442 else
2443 {
2444 parameter.value[i] = 0.0f;
2445 }
2446 }
2447 }
2448 else
2449 {
2450 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2451
2452 if(parameter.bufferIndex != -1)
2453 {
2454 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2455 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2456 }
2457 }
2458
2459 if(!IsSampler(arg->getBasicType()))
2460 {
2461 parameter.swizzle = readSwizzle(arg, size);
2462 }
2463 }
2464 }
2465
Nicolas Capens0530b452017-11-15 16:39:47 -05002466 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2467 {
2468 parameter.type = registerType(arg);
2469 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002470 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002471 }
2472
Nicolas Capens0bac2852016-05-07 06:09:58 -04002473 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2474 {
2475 for(int index = 0; index < dst->totalRegisterCount(); index++)
2476 {
2477 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002478 }
2479 }
2480
2481 int swizzleElement(int swizzle, int index)
2482 {
2483 return (swizzle >> (index * 2)) & 0x03;
2484 }
2485
2486 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2487 {
2488 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2489 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2490 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2491 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2492 }
2493
2494 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2495 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002496 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2497 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002498 {
2499 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2500 }
2501
2502 TIntermBinary *binary = dst->getAsBinaryNode();
2503
2504 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2505 {
2506 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2507
Nicolas Capens6986b282017-11-16 10:38:19 -05002508 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002509
2510 insert->src[0].type = insert->dst.type;
2511 insert->src[0].index = insert->dst.index;
2512 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002513 source(insert->src[1], src);
2514 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002515
2516 shader->append(insert);
2517 }
2518 else
2519 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002520 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2521
Nicolas Capens6986b282017-11-16 10:38:19 -05002522 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002523
Nicolas Capens0530b452017-11-15 16:39:47 -05002524 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002525 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2526
2527 shader->append(mov1);
2528
2529 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002530 {
2531 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2532
Nicolas Capens84249fd2017-11-09 11:20:51 -05002533 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002534 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002535 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002536
Nicolas Capens0530b452017-11-15 16:39:47 -05002537 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002538
2539 shader->append(mov);
2540 }
2541 }
2542 }
2543
Nicolas Capensd469de22017-11-16 10:42:20 -05002544 void OutputASM::evaluateRvalue(TIntermTyped *node)
2545 {
2546 TIntermBinary *binary = node->getAsBinaryNode();
2547
2548 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2549 {
2550 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2551
2552 destination(insert->dst, node);
2553
2554 Temporary address(this);
2555 unsigned char mask;
2556 TIntermTyped *root = nullptr;
2557 unsigned int offset = 0;
2558 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2559
2560 source(insert->src[0], root, offset);
2561 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2562
2563 source(insert->src[1], binary->getRight());
2564
2565 shader->append(insert);
2566 }
2567 else
2568 {
2569 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2570
2571 destination(mov1->dst, node, 0);
2572
2573 Temporary address(this);
2574 unsigned char mask;
2575 TIntermTyped *root = nullptr;
2576 unsigned int offset = 0;
2577 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2578
2579 source(mov1->src[0], root, offset);
2580 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2581
2582 shader->append(mov1);
2583
2584 for(int i = 1; i < node->totalRegisterCount(); i++)
2585 {
2586 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2587 mov->src[0].rel = mov1->src[0].rel;
2588 }
2589 }
2590 }
2591
Nicolas Capens6986b282017-11-16 10:38:19 -05002592 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002593 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002594 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002595 TIntermTyped *root = nullptr;
2596 unsigned int offset = 0;
2597 unsigned char mask = 0xF;
2598 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2599
2600 dst.type = registerType(root);
2601 dst.index = registerIndex(root) + offset;
2602 dst.mask = mask;
2603
2604 return swizzle;
2605 }
2606
2607 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2608 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002609 TIntermTyped *result = node;
2610 TIntermBinary *binary = node->getAsBinaryNode();
2611 TIntermSymbol *symbol = node->getAsSymbolNode();
2612
2613 if(binary)
2614 {
2615 TIntermTyped *left = binary->getLeft();
2616 TIntermTyped *right = binary->getRight();
2617
Nicolas Capens0530b452017-11-15 16:39:47 -05002618 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002619
2620 switch(binary->getOp())
2621 {
2622 case EOpIndexDirect:
2623 {
2624 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2625
2626 if(left->isRegister())
2627 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002628 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002629
Nicolas Capens0530b452017-11-15 16:39:47 -05002630 mask = 1;
2631 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002632 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002633 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002634 }
2635
2636 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002637 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002638
2639 return element;
2640 }
2641 else if(left->isArray() || left->isMatrix())
2642 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002643 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002644 return 0xE4;
2645 }
2646 else UNREACHABLE(0);
2647 }
2648 break;
2649 case EOpIndexIndirect:
2650 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002651 right->traverse(this);
2652
Nicolas Capens0bac2852016-05-07 06:09:58 -04002653 if(left->isRegister())
2654 {
2655 // Requires INSERT instruction (handled by calling function)
2656 }
2657 else if(left->isArray() || left->isMatrix())
2658 {
2659 int scale = result->totalRegisterCount();
2660
Nicolas Capens0530b452017-11-15 16:39:47 -05002661 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002662 {
2663 if(left->totalRegisterCount() > 1)
2664 {
2665 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002666 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002667
Nicolas Capens4b743732018-05-28 13:22:07 -04002668 int indexId = right->getAsSymbolNode() ? right->getAsSymbolNode()->getId() : 0;
2669
Nicolas Capens0530b452017-11-15 16:39:47 -05002670 rel.index = relativeRegister.index;
2671 rel.type = relativeRegister.type;
2672 rel.scale = scale;
Nicolas Capens4b743732018-05-28 13:22:07 -04002673 rel.dynamic = (right->getQualifier() != EvqUniform) && (deterministicVariables.count(indexId) == 0);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002674 }
2675 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002676 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002677 {
2678 if(scale == 1)
2679 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002680 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002681 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002682 mad->src[0].index = rel.index;
2683 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002684 }
2685 else
2686 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002687 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002688 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002689 mul->src[0].index = rel.index;
2690 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002691
2692 Constant newScale(scale);
2693 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2694 }
2695
Nicolas Capens0530b452017-11-15 16:39:47 -05002696 rel.type = sw::Shader::PARAMETER_TEMP;
2697 rel.index = registerIndex(&address);
2698 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002699 }
2700 else // Just add the new index to the address register
2701 {
2702 if(scale == 1)
2703 {
2704 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2705 }
2706 else
2707 {
2708 Constant newScale(scale);
2709 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2710 }
2711 }
2712 }
2713 else UNREACHABLE(0);
2714 }
2715 break;
2716 case EOpIndexDirectStruct:
2717 case EOpIndexDirectInterfaceBlock:
2718 {
2719 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2720 left->getType().getStruct()->fields() :
2721 left->getType().getInterfaceBlock()->fields();
2722 int index = right->getAsConstantUnion()->getIConst(0);
2723 int fieldOffset = 0;
2724
2725 for(int i = 0; i < index; i++)
2726 {
2727 fieldOffset += fields[i]->type()->totalRegisterCount();
2728 }
2729
Nicolas Capens0530b452017-11-15 16:39:47 -05002730 offset += fieldOffset;
2731 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002732
2733 return 0xE4;
2734 }
2735 break;
2736 case EOpVectorSwizzle:
2737 {
2738 ASSERT(left->isRegister());
2739
Nicolas Capens0530b452017-11-15 16:39:47 -05002740 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002741
2742 int swizzle = 0;
2743 int rightMask = 0;
2744
2745 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2746
2747 for(unsigned int i = 0; i < sequence.size(); i++)
2748 {
2749 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2750
2751 int element = swizzleElement(leftSwizzle, index);
2752 rightMask = rightMask | (1 << element);
2753 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2754 }
2755
Nicolas Capens0530b452017-11-15 16:39:47 -05002756 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002757
2758 return swizzle;
2759 }
2760 break;
2761 default:
2762 UNREACHABLE(binary->getOp()); // Not an l-value operator
2763 break;
2764 }
2765 }
2766 else if(symbol)
2767 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002768 root = symbol;
2769 offset = 0;
2770 mask = writeMask(symbol);
2771
2772 return 0xE4;
2773 }
2774 else
2775 {
2776 node->traverse(this);
2777
2778 root = node;
2779 offset = 0;
2780 mask = writeMask(node);
2781
Nicolas Capens0bac2852016-05-07 06:09:58 -04002782 return 0xE4;
2783 }
2784
2785 return 0xE4;
2786 }
2787
2788 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2789 {
2790 if(isSamplerRegister(operand))
2791 {
2792 return sw::Shader::PARAMETER_SAMPLER;
2793 }
2794
2795 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002796 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002797 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002798 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2799 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002800 {
2801 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2802 }
2803 outputQualifier = qualifier;
2804 }
2805
2806 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2807 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002808 // Constant arrays are in the constant register file.
2809 if(operand->isArray() && operand->getArraySize() > 1)
2810 {
2811 return sw::Shader::PARAMETER_CONST;
2812 }
2813 else
2814 {
2815 return sw::Shader::PARAMETER_TEMP;
2816 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002817 }
2818
2819 switch(qualifier)
2820 {
2821 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2822 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2823 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2824 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2825 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2826 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2827 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2828 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2829 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2830 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2831 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2832 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2833 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2834 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2835 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2836 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2837 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2838 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2839 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2840 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2841 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2842 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2843 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2844 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2845 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2846 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002847 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002848 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2849 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2850 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2851 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2852 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2853 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2854 default: UNREACHABLE(qualifier);
2855 }
2856
2857 return sw::Shader::PARAMETER_VOID;
2858 }
2859
Alexis Hetu12b00502016-05-20 13:01:11 -04002860 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2861 {
2862 const TQualifier qualifier = operand->getQualifier();
2863 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2864 }
2865
Nicolas Capens0bac2852016-05-07 06:09:58 -04002866 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2867 {
2868 if(isSamplerRegister(operand))
2869 {
2870 return samplerRegister(operand);
2871 }
Alexis Hetud87b3a82018-04-24 11:13:33 -04002872 else if(operand->getType().totalSamplerRegisterCount() > 0) // Struct containing a sampler
2873 {
2874 samplerRegister(operand); // Make sure the sampler is declared
2875 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002876
2877 switch(operand->getQualifier())
2878 {
2879 case EvqTemporary: return temporaryRegister(operand);
2880 case EvqGlobal: return temporaryRegister(operand);
2881 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2882 case EvqAttribute: return attributeRegister(operand);
2883 case EvqVaryingIn: return varyingRegister(operand);
2884 case EvqVaryingOut: return varyingRegister(operand);
2885 case EvqVertexIn: return attributeRegister(operand);
2886 case EvqFragmentOut: return fragmentOutputRegister(operand);
2887 case EvqVertexOut: return varyingRegister(operand);
2888 case EvqFragmentIn: return varyingRegister(operand);
2889 case EvqInvariantVaryingIn: return varyingRegister(operand);
2890 case EvqInvariantVaryingOut: return varyingRegister(operand);
2891 case EvqSmooth: return varyingRegister(operand);
2892 case EvqFlat: return varyingRegister(operand);
2893 case EvqCentroidOut: return varyingRegister(operand);
2894 case EvqSmoothIn: return varyingRegister(operand);
2895 case EvqFlatIn: return varyingRegister(operand);
2896 case EvqCentroidIn: return varyingRegister(operand);
2897 case EvqUniform: return uniformRegister(operand);
2898 case EvqIn: return temporaryRegister(operand);
2899 case EvqOut: return temporaryRegister(operand);
2900 case EvqInOut: return temporaryRegister(operand);
2901 case EvqConstReadOnly: return temporaryRegister(operand);
2902 case EvqPosition: return varyingRegister(operand);
2903 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002904 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2905 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2906 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2907 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002908 case EvqPointCoord: return varyingRegister(operand);
2909 case EvqFragColor: return 0;
2910 case EvqFragData: return fragmentOutputRegister(operand);
2911 case EvqFragDepth: return 0;
2912 default: UNREACHABLE(operand->getQualifier());
2913 }
2914
2915 return 0;
2916 }
2917
2918 int OutputASM::writeMask(TIntermTyped *destination, int index)
2919 {
2920 if(destination->getQualifier() == EvqPointSize)
2921 {
2922 return 0x2; // Point size stored in the y component
2923 }
2924
2925 return 0xF >> (4 - registerSize(destination->getType(), index));
2926 }
2927
2928 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2929 {
2930 if(argument->getQualifier() == EvqPointSize)
2931 {
2932 return 0x55; // Point size stored in the y component
2933 }
2934
2935 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2936
2937 return swizzleSize[size];
2938 }
2939
2940 // Conservatively checks whether an expression is fast to compute and has no side effects
2941 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2942 {
2943 if(!expression->isRegister())
2944 {
2945 return false;
2946 }
2947
2948 return cost(expression, budget) >= 0;
2949 }
2950
2951 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2952 int OutputASM::cost(TIntermNode *expression, int budget)
2953 {
2954 if(budget < 0)
2955 {
2956 return budget;
2957 }
2958
2959 if(expression->getAsSymbolNode())
2960 {
2961 return budget;
2962 }
2963 else if(expression->getAsConstantUnion())
2964 {
2965 return budget;
2966 }
2967 else if(expression->getAsBinaryNode())
2968 {
2969 TIntermBinary *binary = expression->getAsBinaryNode();
2970
2971 switch(binary->getOp())
2972 {
2973 case EOpVectorSwizzle:
2974 case EOpIndexDirect:
2975 case EOpIndexDirectStruct:
2976 case EOpIndexDirectInterfaceBlock:
2977 return cost(binary->getLeft(), budget - 0);
2978 case EOpAdd:
2979 case EOpSub:
2980 case EOpMul:
2981 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2982 default:
2983 return -1;
2984 }
2985 }
2986 else if(expression->getAsUnaryNode())
2987 {
2988 TIntermUnary *unary = expression->getAsUnaryNode();
2989
2990 switch(unary->getOp())
2991 {
2992 case EOpAbs:
2993 case EOpNegative:
2994 return cost(unary->getOperand(), budget - 1);
2995 default:
2996 return -1;
2997 }
2998 }
2999 else if(expression->getAsSelectionNode())
3000 {
3001 TIntermSelection *selection = expression->getAsSelectionNode();
3002
3003 if(selection->usesTernaryOperator())
3004 {
3005 TIntermTyped *condition = selection->getCondition();
3006 TIntermNode *trueBlock = selection->getTrueBlock();
3007 TIntermNode *falseBlock = selection->getFalseBlock();
3008 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
3009
3010 if(constantCondition)
3011 {
3012 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
3013
3014 if(trueCondition)
3015 {
3016 return cost(trueBlock, budget - 0);
3017 }
3018 else
3019 {
3020 return cost(falseBlock, budget - 0);
3021 }
3022 }
3023 else
3024 {
3025 return cost(trueBlock, cost(falseBlock, budget - 2));
3026 }
3027 }
3028 }
3029
3030 return -1;
3031 }
3032
3033 const Function *OutputASM::findFunction(const TString &name)
3034 {
3035 for(unsigned int f = 0; f < functionArray.size(); f++)
3036 {
3037 if(functionArray[f].name == name)
3038 {
3039 return &functionArray[f];
3040 }
3041 }
3042
3043 return 0;
3044 }
3045
3046 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3047 {
Alexis Hetu329747c2018-04-09 13:47:34 -04003048 int index = allocate(temporaries, temporary);
3049 if(index >= sw::NUM_TEMPORARY_REGISTERS)
3050 {
3051 mContext.error(temporary->getLine(),
3052 "Too many temporary registers required to compile shader",
3053 pixelShader ? "pixel shader" : "vertex shader");
3054 }
3055 return index;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003056 }
3057
Alexis Hetu49351232017-11-02 16:00:32 -04003058 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3059 {
3060 if(type.isStruct())
3061 {
3062 const TFieldList &fields = type.getStruct()->fields();
3063 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003064 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003065 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003066 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003067 setPixelShaderInputs(fieldType, fieldVar, flat);
3068 fieldVar += fieldType.totalRegisterCount();
3069 }
3070 }
3071 else
3072 {
3073 for(int i = 0; i < type.totalRegisterCount(); i++)
3074 {
3075 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3076 }
3077 }
3078 }
3079
Nicolas Capens0bac2852016-05-07 06:09:58 -04003080 int OutputASM::varyingRegister(TIntermTyped *varying)
3081 {
3082 int var = lookup(varyings, varying);
3083
3084 if(var == -1)
3085 {
3086 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003087 int registerCount = varying->totalRegisterCount();
3088
3089 if(pixelShader)
3090 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003091 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003092 {
3093 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3094 return 0;
3095 }
3096
3097 if(varying->getQualifier() == EvqPointCoord)
3098 {
3099 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003100 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003101 }
3102 else
3103 {
Alexis Hetu49351232017-11-02 16:00:32 -04003104 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003105 }
3106 }
3107 else if(vertexShader)
3108 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003109 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003110 {
3111 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3112 return 0;
3113 }
3114
3115 if(varying->getQualifier() == EvqPosition)
3116 {
3117 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003118 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003119 }
3120 else if(varying->getQualifier() == EvqPointSize)
3121 {
3122 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003123 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003124 }
3125 else
3126 {
3127 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3128 }
3129 }
3130 else UNREACHABLE(0);
3131
3132 declareVarying(varying, var);
3133 }
3134
3135 return var;
3136 }
3137
3138 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3139 {
3140 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3141 {
Alexis Hetu49351232017-11-02 16:00:32 -04003142 TIntermSymbol *symbol = varying->getAsSymbolNode();
3143 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3144 }
3145 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003146
Alexis Hetu49351232017-11-02 16:00:32 -04003147 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3148 {
3149 const char *name = varyingName.c_str();
3150 VaryingList &activeVaryings = shaderObject->varyings;
3151
3152 TStructure* structure = type.getStruct();
3153 if(structure)
3154 {
3155 int fieldRegisterIndex = registerIndex;
3156
3157 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003158 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003159 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003160 const TType& fieldType = *(field->type());
3161 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003162 if(fieldRegisterIndex >= 0)
3163 {
3164 fieldRegisterIndex += fieldType.totalRegisterCount();
3165 }
3166 }
3167 }
3168 else
3169 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003170 // Check if this varying has been declared before without having a register assigned
3171 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3172 {
3173 if(v->name == name)
3174 {
Alexis Hetu49351232017-11-02 16:00:32 -04003175 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003176 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003177 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3178 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003179 }
3180
3181 return;
3182 }
3183 }
3184
Alexis Hetu924513c2018-01-05 15:48:12 -05003185 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003186 }
3187 }
3188
Alexis Hetu930df972018-01-30 16:54:13 -05003189 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3190 {
3191 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003192 int registerCount = fragmentOutput->totalRegisterCount();
3193 if(requestedLocation < 0)
Alexis Hetu930df972018-01-30 16:54:13 -05003194 {
Alexis Hetu3c1d6cf2018-02-06 10:54:49 -05003195 ASSERT(requestedLocation == -1); // All other negative values would have been prevented in TParseContext::parseLayoutQualifier
3196 return; // No requested location
Alexis Hetu930df972018-01-30 16:54:13 -05003197 }
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003198 else if((requestedLocation + registerCount) > sw::RENDERTARGETS)
Alexis Hetu930df972018-01-30 16:54:13 -05003199 {
3200 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3201 }
Alexis Hetu17e2e2f2018-02-05 10:41:47 -05003202 else
3203 {
3204 int currentIndex = lookup(fragmentOutputs, fragmentOutput);
3205 if(requestedLocation != currentIndex)
3206 {
3207 if(currentIndex != -1)
3208 {
3209 mContext.error(fragmentOutput->getLine(), "Multiple locations for fragment output", "fragment shader");
3210 }
3211 else
3212 {
3213 if(fragmentOutputs.size() <= (size_t)requestedLocation)
3214 {
3215 while(fragmentOutputs.size() < (size_t)requestedLocation)
3216 {
3217 fragmentOutputs.push_back(nullptr);
3218 }
3219 for(int i = 0; i < registerCount; i++)
3220 {
3221 fragmentOutputs.push_back(fragmentOutput);
3222 }
3223 }
3224 else
3225 {
3226 for(int i = 0; i < registerCount; i++)
3227 {
3228 if(!fragmentOutputs[requestedLocation + i])
3229 {
3230 fragmentOutputs[requestedLocation + i] = fragmentOutput;
3231 }
3232 else
3233 {
3234 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3235 return;
3236 }
3237 }
3238 }
3239 }
3240 }
3241 }
Alexis Hetu930df972018-01-30 16:54:13 -05003242 }
3243
Nicolas Capens0bac2852016-05-07 06:09:58 -04003244 int OutputASM::uniformRegister(TIntermTyped *uniform)
3245 {
3246 const TType &type = uniform->getType();
3247 ASSERT(!IsSampler(type.getBasicType()));
3248 TInterfaceBlock *block = type.getAsInterfaceBlock();
3249 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3250 ASSERT(symbol || block);
3251
3252 if(symbol || block)
3253 {
3254 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3255 bool isBlockMember = (!block && parentBlock);
3256 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3257
3258 if(index == -1 || isBlockMember)
3259 {
3260 if(index == -1)
3261 {
3262 index = allocate(uniforms, uniform);
3263 }
3264
3265 // Verify if the current uniform is a member of an already declared block
3266 const TString &name = symbol ? symbol->getSymbol() : block->name();
3267 int blockMemberIndex = blockMemberLookup(type, name, index);
3268 if(blockMemberIndex == -1)
3269 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003270 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003271 }
3272 else
3273 {
3274 index = blockMemberIndex;
3275 }
3276 }
3277
3278 return index;
3279 }
3280
3281 return 0;
3282 }
3283
3284 int OutputASM::attributeRegister(TIntermTyped *attribute)
3285 {
3286 ASSERT(!attribute->isArray());
3287
3288 int index = lookup(attributes, attribute);
3289
3290 if(index == -1)
3291 {
3292 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3293 ASSERT(symbol);
3294
3295 if(symbol)
3296 {
3297 index = allocate(attributes, attribute);
3298 const TType &type = attribute->getType();
3299 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003300 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3301 switch(type.getBasicType())
3302 {
3303 case EbtInt:
3304 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3305 break;
3306 case EbtUInt:
3307 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3308 break;
3309 case EbtFloat:
3310 default:
3311 break;
3312 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003313
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003314 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003315 {
3316 for(int i = 0; i < registerCount; i++)
3317 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003318 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003319 }
3320 }
3321
3322 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3323
3324 const char *name = symbol->getSymbol().c_str();
3325 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3326 }
3327 }
3328
3329 return index;
3330 }
3331
3332 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3333 {
3334 return allocate(fragmentOutputs, fragmentOutput);
3335 }
3336
3337 int OutputASM::samplerRegister(TIntermTyped *sampler)
3338 {
3339 const TType &type = sampler->getType();
3340 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3341
3342 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3343 TIntermBinary *binary = sampler->getAsBinaryNode();
3344
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003345 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003346 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003347 switch(type.getQualifier())
3348 {
3349 case EvqUniform:
3350 return samplerRegister(symbol);
3351 case EvqIn:
3352 case EvqConstReadOnly:
3353 // Function arguments are not (uniform) sampler registers
3354 return -1;
3355 default:
3356 UNREACHABLE(type.getQualifier());
3357 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003358 }
3359 else if(binary)
3360 {
3361 TIntermTyped *left = binary->getLeft();
3362 TIntermTyped *right = binary->getRight();
3363 const TType &leftType = left->getType();
3364 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3365 int offset = 0;
3366
3367 switch(binary->getOp())
3368 {
3369 case EOpIndexDirect:
3370 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003371 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003372 break;
3373 case EOpIndexDirectStruct:
3374 ASSERT(leftType.isStruct());
3375 {
3376 const TFieldList &fields = leftType.getStruct()->fields();
3377
3378 for(int i = 0; i < index; i++)
3379 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003380 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003381 }
3382 }
3383 break;
3384 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3385 return -1;
3386 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3387 default:
3388 UNREACHABLE(binary->getOp());
3389 return -1;
3390 }
3391
3392 int base = samplerRegister(left);
3393
3394 if(base < 0)
3395 {
3396 return -1;
3397 }
3398
3399 return base + offset;
3400 }
3401
3402 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003403 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003404 }
3405
3406 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3407 {
3408 const TType &type = sampler->getType();
3409 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3410
3411 int index = lookup(samplers, sampler);
3412
3413 if(index == -1)
3414 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003415 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003416
3417 if(sampler->getQualifier() == EvqUniform)
3418 {
3419 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003420 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003421 }
3422 }
3423
3424 return index;
3425 }
3426
3427 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3428 {
3429 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3430 }
3431
3432 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3433 {
3434 for(unsigned int i = 0; i < list.size(); i++)
3435 {
3436 if(list[i] == variable)
3437 {
3438 return i; // Pointer match
3439 }
3440 }
3441
3442 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3443 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3444
3445 if(varBlock)
3446 {
3447 for(unsigned int i = 0; i < list.size(); i++)
3448 {
3449 if(list[i])
3450 {
3451 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3452
3453 if(listBlock)
3454 {
3455 if(listBlock->name() == varBlock->name())
3456 {
3457 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3458 ASSERT(listBlock->fields() == varBlock->fields());
3459 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3460 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3461
3462 return i;
3463 }
3464 }
3465 }
3466 }
3467 }
3468 else if(varSymbol)
3469 {
3470 for(unsigned int i = 0; i < list.size(); i++)
3471 {
3472 if(list[i])
3473 {
3474 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3475
3476 if(listSymbol)
3477 {
3478 if(listSymbol->getId() == varSymbol->getId())
3479 {
3480 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3481 ASSERT(listSymbol->getType() == varSymbol->getType());
3482 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3483
3484 return i;
3485 }
3486 }
3487 }
3488 }
3489 }
3490
3491 return -1;
3492 }
3493
3494 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3495 {
3496 for(unsigned int i = 0; i < list.size(); i++)
3497 {
3498 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3499 {
3500 return i; // Pointer match
3501 }
3502 }
3503 return -1;
3504 }
3505
Alexis Hetuda163ed2018-01-03 16:36:14 -05003506 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003507 {
3508 int index = lookup(list, variable);
3509
3510 if(index == -1)
3511 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003512 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003513
3514 for(unsigned int i = 0; i < list.size(); i++)
3515 {
3516 if(list[i] == 0)
3517 {
3518 unsigned int j = 1;
3519 for( ; j < registerCount && (i + j) < list.size(); j++)
3520 {
3521 if(list[i + j] != 0)
3522 {
3523 break;
3524 }
3525 }
3526
3527 if(j == registerCount) // Found free slots
3528 {
3529 for(unsigned int j = 0; j < registerCount; j++)
3530 {
3531 list[i + j] = variable;
3532 }
3533
3534 return i;
3535 }
3536 }
3537 }
3538
3539 index = list.size();
3540
3541 for(unsigned int i = 0; i < registerCount; i++)
3542 {
3543 list.push_back(variable);
3544 }
3545 }
3546
3547 return index;
3548 }
3549
3550 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3551 {
3552 int index = lookup(list, variable);
3553
3554 if(index >= 0)
3555 {
3556 list[index] = 0;
3557 }
3558 }
3559
3560 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3561 {
3562 const TInterfaceBlock *block = type.getInterfaceBlock();
3563
3564 if(block)
3565 {
3566 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3567 const TFieldList& fields = block->fields();
3568 const TString &blockName = block->name();
3569 int fieldRegisterIndex = registerIndex;
3570
3571 if(!type.isInterfaceBlock())
3572 {
3573 // This is a uniform that's part of a block, let's see if the block is already defined
3574 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3575 {
3576 if(activeUniformBlocks[i].name == blockName.c_str())
3577 {
3578 // The block is already defined, find the register for the current uniform and return it
3579 for(size_t j = 0; j < fields.size(); j++)
3580 {
3581 const TString &fieldName = fields[j]->name();
3582 if(fieldName == name)
3583 {
3584 return fieldRegisterIndex;
3585 }
3586
3587 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3588 }
3589
3590 ASSERT(false);
3591 return fieldRegisterIndex;
3592 }
3593 }
3594 }
3595 }
3596
3597 return -1;
3598 }
3599
Alexis Hetuda163ed2018-01-03 16:36:14 -05003600 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003601 {
3602 const TStructure *structure = type.getStruct();
3603 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3604
3605 if(!structure && !block)
3606 {
3607 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3608 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3609 if(blockId >= 0)
3610 {
Nicolas Capensbb2bcae2018-02-05 11:12:10 -05003611 blockDefinitions[blockId].insert(BlockDefinitionIndexMap::value_type(registerIndex, TypedMemberInfo(blockInfo, type)));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003612 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3613 }
3614 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003615 bool isSampler = IsSampler(type.getBasicType());
3616 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003617 {
3618 for(int i = 0; i < type.totalRegisterCount(); i++)
3619 {
3620 shader->declareSampler(fieldRegisterIndex + i);
3621 }
3622 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003623 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003624 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003625 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003626 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003627 }
3628 else if(block)
3629 {
3630 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3631 const TFieldList& fields = block->fields();
3632 const TString &blockName = block->name();
3633 int fieldRegisterIndex = registerIndex;
3634 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3635
3636 blockId = activeUniformBlocks.size();
3637 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3638 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3639 block->blockStorage(), isRowMajor, registerIndex, blockId));
3640 blockDefinitions.push_back(BlockDefinitionIndexMap());
3641
Alexis Hetud2742532018-01-23 16:53:41 -05003642 Std140BlockEncoder currentBlockEncoder;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003643 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003644 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003645 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003646 const TType &fieldType = *(field->type());
3647 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003648 if(isUniformBlockMember && (fieldName == name))
3649 {
3650 registerIndex = fieldRegisterIndex;
3651 }
3652
3653 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3654
Alexis Hetuda163ed2018-01-03 16:36:14 -05003655 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003656 fieldRegisterIndex += fieldType.totalRegisterCount();
3657 }
3658 currentBlockEncoder.exitAggregateType();
3659 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3660 }
3661 else
3662 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003663 // Store struct for program link time validation
3664 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3665
Nicolas Capens0bac2852016-05-07 06:09:58 -04003666 int fieldRegisterIndex = registerIndex;
3667
3668 const TFieldList& fields = structure->fields();
3669 if(type.isArray() && (structure || type.isInterfaceBlock()))
3670 {
3671 for(int i = 0; i < type.getArraySize(); i++)
3672 {
3673 if(encoder)
3674 {
3675 encoder->enterAggregateType();
3676 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003677 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003678 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003679 const TType &fieldType = *(field->type());
3680 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003681 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3682
Alexis Hetuda163ed2018-01-03 16:36:14 -05003683 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3684 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003685 }
3686 if(encoder)
3687 {
3688 encoder->exitAggregateType();
3689 }
3690 }
3691 }
3692 else
3693 {
3694 if(encoder)
3695 {
3696 encoder->enterAggregateType();
3697 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003698 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003699 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003700 const TType &fieldType = *(field->type());
3701 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003702 const TString uniformName = name + "." + fieldName;
3703
Alexis Hetuda163ed2018-01-03 16:36:14 -05003704 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3705 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003706 }
3707 if(encoder)
3708 {
3709 encoder->exitAggregateType();
3710 }
3711 }
3712 }
3713 }
3714
Nicolas Capens0bac2852016-05-07 06:09:58 -04003715 int OutputASM::dim(TIntermNode *v)
3716 {
3717 TIntermTyped *vector = v->getAsTyped();
3718 ASSERT(vector && vector->isRegister());
3719 return vector->getNominalSize();
3720 }
3721
3722 int OutputASM::dim2(TIntermNode *m)
3723 {
3724 TIntermTyped *matrix = m->getAsTyped();
3725 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3726 return matrix->getSecondarySize();
3727 }
3728
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003729 // Sets iterations to ~0u if no loop count could be statically determined.
3730 OutputASM::LoopInfo::LoopInfo(TIntermLoop *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003731 {
3732 // Parse loops of the form:
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003733 // for(int index = initial; index [comparator] limit; index [op] increment)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003734
3735 // Parse index name and intial value
3736 if(node->getInit())
3737 {
3738 TIntermAggregate *init = node->getInit()->getAsAggregate();
3739
3740 if(init)
3741 {
3742 TIntermSequence &sequence = init->getSequence();
3743 TIntermTyped *variable = sequence[0]->getAsTyped();
3744
Nicolas Capense3f05552017-05-24 10:45:56 -04003745 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003746 {
3747 TIntermBinary *assign = variable->getAsBinaryNode();
3748
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003749 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003750 {
3751 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3752 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3753
3754 if(symbol && constant)
3755 {
3756 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3757 {
3758 index = symbol;
3759 initial = constant->getUnionArrayPointer()[0].getIConst();
3760 }
3761 }
3762 }
3763 }
3764 }
3765 }
3766
3767 // Parse comparator and limit value
3768 if(index && node->getCondition())
3769 {
3770 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003771 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003772
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003773 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003774 {
3775 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3776
3777 if(constant)
3778 {
3779 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3780 {
3781 comparator = test->getOp();
3782 limit = constant->getUnionArrayPointer()[0].getIConst();
3783 }
3784 }
3785 }
3786 }
3787
3788 // Parse increment
3789 if(index && comparator != EOpNull && node->getExpression())
3790 {
3791 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3792 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3793
3794 if(binaryTerminal)
3795 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003796 TIntermSymbol *operand = binaryTerminal->getLeft()->getAsSymbolNode();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003797
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003798 if(operand && operand->getId() == index->getId())
Nicolas Capens0bac2852016-05-07 06:09:58 -04003799 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003800 TOperator op = binaryTerminal->getOp();
3801 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003802
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003803 if(constant)
3804 {
3805 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003806 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003807 int value = constant->getUnionArrayPointer()[0].getIConst();
3808
3809 switch(op)
3810 {
3811 case EOpAddAssign: increment = value; break;
3812 case EOpSubAssign: increment = -value; break;
3813 default: increment = 0; break; // Rare cases left unhandled. Treated as non-deterministic.
3814 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003815 }
3816 }
3817 }
3818 }
3819 else if(unaryTerminal)
3820 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003821 TIntermSymbol *operand = unaryTerminal->getOperand()->getAsSymbolNode();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003822
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003823 if(operand && operand->getId() == index->getId())
Nicolas Capens0bac2852016-05-07 06:09:58 -04003824 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003825 TOperator op = unaryTerminal->getOp();
3826
3827 switch(op)
3828 {
3829 case EOpPostIncrement: increment = 1; break;
3830 case EOpPostDecrement: increment = -1; break;
3831 case EOpPreIncrement: increment = 1; break;
3832 case EOpPreDecrement: increment = -1; break;
3833 default: increment = 0; break; // Rare cases left unhandled. Treated as non-deterministic.
3834 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003835 }
3836 }
3837 }
3838
3839 if(index && comparator != EOpNull && increment != 0)
3840 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003841 // Check the loop body for return statements or changes to the index variable that make it non-deterministic.
3842 LoopUnrollable loopUnrollable;
3843 bool unrollable = loopUnrollable.traverse(node, index->getId());
3844
3845 if(!unrollable)
3846 {
3847 iterations = ~0u;
3848 return;
3849 }
3850
Nicolas Capens0bac2852016-05-07 06:09:58 -04003851 if(comparator == EOpLessThanEqual)
3852 {
3853 comparator = EOpLessThan;
3854 limit += 1;
3855 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003856 else if(comparator == EOpGreaterThanEqual)
3857 {
3858 comparator = EOpLessThan;
3859 limit -= 1;
3860 std::swap(initial, limit);
3861 increment = -increment;
3862 }
3863 else if(comparator == EOpGreaterThan)
3864 {
3865 comparator = EOpLessThan;
3866 std::swap(initial, limit);
3867 increment = -increment;
3868 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003869
3870 if(comparator == EOpLessThan)
3871 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003872 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003873 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003874 iterations = 0;
Nicolas Capens930b7002017-01-06 17:22:13 -05003875 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003876 else if(increment < 0)
Nicolas Capens930b7002017-01-06 17:22:13 -05003877 {
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003878 iterations = ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003879 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003880 else
3881 {
3882 iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3883 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003884 }
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003885 else
3886 {
3887 // Rare cases left unhandled. Treated as non-deterministic.
3888 iterations = ~0u;
3889 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003890 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003891 }
3892
Nicolas Capens493fc542018-05-29 17:11:37 -04003893 bool LoopUnrollable::traverse(TIntermLoop *loop, int indexId)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003894 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003895 loopUnrollable = true;
3896
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003897 loopIndexId = indexId;
Nicolas Capens493fc542018-05-29 17:11:37 -04003898 TIntermNode *body = loop->getBody();
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003899
Nicolas Capens493fc542018-05-29 17:11:37 -04003900 if(body)
3901 {
3902 body->traverse(this);
3903 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003904
3905 return loopUnrollable;
3906 }
3907
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003908 void LoopUnrollable::visitSymbol(TIntermSymbol *node)
3909 {
3910 // Check that the loop index is not used as the argument to a function out or inout parameter.
3911 if(node->getId() == loopIndexId)
3912 {
3913 if(node->getQualifier() == EvqOut || node->getQualifier() == EvqInOut)
3914 {
3915 loopUnrollable = false;
3916 }
3917 }
3918 }
3919
3920 bool LoopUnrollable::visitBinary(Visit visit, TIntermBinary *node)
3921 {
3922 if(!loopUnrollable)
3923 {
3924 return false;
3925 }
3926
3927 // Check that the loop index is not statically assigned to.
3928 TIntermSymbol *symbol = node->getLeft()->getAsSymbolNode();
Nicolas Capens493fc542018-05-29 17:11:37 -04003929 loopUnrollable = !(node->modifiesState() && symbol && (symbol->getId() == loopIndexId));
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003930
3931 return loopUnrollable;
3932 }
3933
3934 bool LoopUnrollable::visitUnary(Visit visit, TIntermUnary *node)
3935 {
3936 if(!loopUnrollable)
3937 {
3938 return false;
3939 }
3940
3941 // Check that the loop index is not statically assigned to.
3942 TIntermSymbol *symbol = node->getOperand()->getAsSymbolNode();
Nicolas Capens493fc542018-05-29 17:11:37 -04003943 loopUnrollable = !(node->modifiesState() && symbol && (symbol->getId() == loopIndexId));
Nicolas Capensac3f2fa2018-05-28 12:25:57 -04003944
3945 return loopUnrollable;
3946 }
3947
Nicolas Capens0bac2852016-05-07 06:09:58 -04003948 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3949 {
3950 if(!loopUnrollable)
3951 {
3952 return false;
3953 }
3954
Nicolas Capens0bac2852016-05-07 06:09:58 -04003955 switch(node->getFlowOp())
3956 {
3957 case EOpKill:
3958 case EOpReturn:
Nicolas Capens0bac2852016-05-07 06:09:58 -04003959 case EOpBreak:
3960 case EOpContinue:
3961 loopUnrollable = false;
3962 break;
3963 default: UNREACHABLE(node->getFlowOp());
3964 }
3965
3966 return loopUnrollable;
3967 }
3968
3969 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3970 {
3971 return loopUnrollable;
3972 }
3973}