blob: 5f05e46754494e0c20032ec69adcb8f48875c056 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "OutputASM.h"
16#include "Common/Math.hpp"
17
18#include "common/debug.h"
19#include "InfoSink.h"
20
21#include "libGLESv2/Shader.h"
22
23#include <GLES2/gl2.h>
24#include <GLES2/gl2ext.h>
25#include <GLES3/gl3.h>
26
Nicolas Capens930b7002017-01-06 17:22:13 -050027#include <stdlib.h>
28
Alexis Hetu924513c2018-01-05 15:48:12 -050029namespace
30{
31 GLenum glVariableType(const TType &type)
32 {
33 switch(type.getBasicType())
34 {
35 case EbtFloat:
36 if(type.isScalar())
37 {
38 return GL_FLOAT;
39 }
40 else if(type.isVector())
41 {
42 switch(type.getNominalSize())
43 {
44 case 2: return GL_FLOAT_VEC2;
45 case 3: return GL_FLOAT_VEC3;
46 case 4: return GL_FLOAT_VEC4;
47 default: UNREACHABLE(type.getNominalSize());
48 }
49 }
50 else if(type.isMatrix())
51 {
52 switch(type.getNominalSize())
53 {
54 case 2:
55 switch(type.getSecondarySize())
56 {
57 case 2: return GL_FLOAT_MAT2;
58 case 3: return GL_FLOAT_MAT2x3;
59 case 4: return GL_FLOAT_MAT2x4;
60 default: UNREACHABLE(type.getSecondarySize());
61 }
62 case 3:
63 switch(type.getSecondarySize())
64 {
65 case 2: return GL_FLOAT_MAT3x2;
66 case 3: return GL_FLOAT_MAT3;
67 case 4: return GL_FLOAT_MAT3x4;
68 default: UNREACHABLE(type.getSecondarySize());
69 }
70 case 4:
71 switch(type.getSecondarySize())
72 {
73 case 2: return GL_FLOAT_MAT4x2;
74 case 3: return GL_FLOAT_MAT4x3;
75 case 4: return GL_FLOAT_MAT4;
76 default: UNREACHABLE(type.getSecondarySize());
77 }
78 default: UNREACHABLE(type.getNominalSize());
79 }
80 }
81 else UNREACHABLE(0);
82 break;
83 case EbtInt:
84 if(type.isScalar())
85 {
86 return GL_INT;
87 }
88 else if(type.isVector())
89 {
90 switch(type.getNominalSize())
91 {
92 case 2: return GL_INT_VEC2;
93 case 3: return GL_INT_VEC3;
94 case 4: return GL_INT_VEC4;
95 default: UNREACHABLE(type.getNominalSize());
96 }
97 }
98 else UNREACHABLE(0);
99 break;
100 case EbtUInt:
101 if(type.isScalar())
102 {
103 return GL_UNSIGNED_INT;
104 }
105 else if(type.isVector())
106 {
107 switch(type.getNominalSize())
108 {
109 case 2: return GL_UNSIGNED_INT_VEC2;
110 case 3: return GL_UNSIGNED_INT_VEC3;
111 case 4: return GL_UNSIGNED_INT_VEC4;
112 default: UNREACHABLE(type.getNominalSize());
113 }
114 }
115 else UNREACHABLE(0);
116 break;
117 case EbtBool:
118 if(type.isScalar())
119 {
120 return GL_BOOL;
121 }
122 else if(type.isVector())
123 {
124 switch(type.getNominalSize())
125 {
126 case 2: return GL_BOOL_VEC2;
127 case 3: return GL_BOOL_VEC3;
128 case 4: return GL_BOOL_VEC4;
129 default: UNREACHABLE(type.getNominalSize());
130 }
131 }
132 else UNREACHABLE(0);
133 break;
134 case EbtSampler2D:
135 return GL_SAMPLER_2D;
136 case EbtISampler2D:
137 return GL_INT_SAMPLER_2D;
138 case EbtUSampler2D:
139 return GL_UNSIGNED_INT_SAMPLER_2D;
140 case EbtSamplerCube:
141 return GL_SAMPLER_CUBE;
142 case EbtISamplerCube:
143 return GL_INT_SAMPLER_CUBE;
144 case EbtUSamplerCube:
145 return GL_UNSIGNED_INT_SAMPLER_CUBE;
146 case EbtSamplerExternalOES:
147 return GL_SAMPLER_EXTERNAL_OES;
148 case EbtSampler3D:
149 return GL_SAMPLER_3D_OES;
150 case EbtISampler3D:
151 return GL_INT_SAMPLER_3D;
152 case EbtUSampler3D:
153 return GL_UNSIGNED_INT_SAMPLER_3D;
154 case EbtSampler2DArray:
155 return GL_SAMPLER_2D_ARRAY;
156 case EbtISampler2DArray:
157 return GL_INT_SAMPLER_2D_ARRAY;
158 case EbtUSampler2DArray:
159 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
160 case EbtSampler2DShadow:
161 return GL_SAMPLER_2D_SHADOW;
162 case EbtSamplerCubeShadow:
163 return GL_SAMPLER_CUBE_SHADOW;
164 case EbtSampler2DArrayShadow:
165 return GL_SAMPLER_2D_ARRAY_SHADOW;
166 default:
167 UNREACHABLE(type.getBasicType());
168 break;
169 }
170
171 return GL_NONE;
172 }
173
174 GLenum glVariablePrecision(const TType &type)
175 {
176 if(type.getBasicType() == EbtFloat)
177 {
178 switch(type.getPrecision())
179 {
180 case EbpHigh: return GL_HIGH_FLOAT;
181 case EbpMedium: return GL_MEDIUM_FLOAT;
182 case EbpLow: return GL_LOW_FLOAT;
183 case EbpUndefined:
184 // Should be defined as the default precision by the parser
185 default: UNREACHABLE(type.getPrecision());
186 }
187 }
188 else if(type.getBasicType() == EbtInt)
189 {
190 switch(type.getPrecision())
191 {
192 case EbpHigh: return GL_HIGH_INT;
193 case EbpMedium: return GL_MEDIUM_INT;
194 case EbpLow: return GL_LOW_INT;
195 case EbpUndefined:
196 // Should be defined as the default precision by the parser
197 default: UNREACHABLE(type.getPrecision());
198 }
199 }
200
201 // Other types (boolean, sampler) don't have a precision
202 return GL_NONE;
203 }
204}
205
Nicolas Capens0bac2852016-05-07 06:09:58 -0400206namespace glsl
207{
208 // Integer to TString conversion
209 TString str(int i)
210 {
211 char buffer[20];
212 sprintf(buffer, "%d", i);
213 return buffer;
214 }
215
216 class Temporary : public TIntermSymbol
217 {
218 public:
219 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
220 {
221 }
222
223 ~Temporary()
224 {
225 assembler->freeTemporary(this);
226 }
227
228 private:
229 OutputASM *const assembler;
230 };
231
232 class Constant : public TIntermConstantUnion
233 {
234 public:
235 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
236 {
237 constants[0].setFConst(x);
238 constants[1].setFConst(y);
239 constants[2].setFConst(z);
240 constants[3].setFConst(w);
241 }
242
243 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
244 {
245 constants[0].setBConst(b);
246 }
247
248 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
249 {
250 constants[0].setIConst(i);
251 }
252
253 ~Constant()
254 {
255 }
256
257 private:
258 ConstantUnion constants[4];
259 };
260
Alexis Hetu924513c2018-01-05 15:48:12 -0500261 ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
262 type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
263 name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
264 {
265 if(type.isStruct())
266 {
267 for(const auto& field : type.getStruct()->fields())
268 {
269 fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
270 }
271 }
272 }
273
274 Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
275 ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400276 {
277 }
278
279 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
280 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
281 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
282 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
283 {
284 }
285
286 BlockLayoutEncoder::BlockLayoutEncoder(bool rowMajor)
287 : mCurrentOffset(0), isRowMajor(rowMajor)
288 {
289 }
290
291 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
292 {
293 int arrayStride;
294 int matrixStride;
295
Alexis Hetubc648b92018-01-04 17:39:15 -0500296 bool isVariableRowMajor = isRowMajor;
297 TLayoutMatrixPacking matrixPacking = type.getLayoutQualifier().matrixPacking;
298 if(matrixPacking != EmpUnspecified)
299 {
300 isVariableRowMajor = (matrixPacking == EmpRowMajor);
301 }
302 getBlockLayoutInfo(type, type.getArraySize(), isVariableRowMajor, &arrayStride, &matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400303
304 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
305 static_cast<int>(arrayStride * BytesPerComponent),
306 static_cast<int>(matrixStride * BytesPerComponent),
Alexis Hetubc648b92018-01-04 17:39:15 -0500307 (matrixStride > 0) && isVariableRowMajor);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400308
Alexis Hetubc648b92018-01-04 17:39:15 -0500309 advanceOffset(type, type.getArraySize(), isVariableRowMajor, arrayStride, matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400310
311 return memberInfo;
312 }
313
314 // static
315 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
316 {
317 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
318 }
319
320 // static
321 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
322 {
323 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
324 }
325
326 void BlockLayoutEncoder::nextRegister()
327 {
328 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
329 }
330
331 Std140BlockEncoder::Std140BlockEncoder(bool rowMajor) : BlockLayoutEncoder(rowMajor)
332 {
333 }
334
335 void Std140BlockEncoder::enterAggregateType()
336 {
337 nextRegister();
338 }
339
340 void Std140BlockEncoder::exitAggregateType()
341 {
342 nextRegister();
343 }
344
345 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
346 {
347 size_t baseAlignment = 0;
348 int matrixStride = 0;
349 int arrayStride = 0;
350
351 if(type.isMatrix())
352 {
353 baseAlignment = ComponentsPerRegister;
354 matrixStride = ComponentsPerRegister;
355
356 if(arraySize > 0)
357 {
358 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
359 arrayStride = ComponentsPerRegister * numRegisters;
360 }
361 }
362 else if(arraySize > 0)
363 {
364 baseAlignment = ComponentsPerRegister;
365 arrayStride = ComponentsPerRegister;
366 }
367 else
368 {
369 const size_t numComponents = type.getElementSize();
370 baseAlignment = (numComponents == 3 ? 4u : numComponents);
371 }
372
373 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
374
375 *matrixStrideOut = matrixStride;
376 *arrayStrideOut = arrayStride;
377 }
378
379 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
380 {
381 if(arraySize > 0)
382 {
383 mCurrentOffset += arrayStride * arraySize;
384 }
385 else if(type.isMatrix())
386 {
387 ASSERT(matrixStride == ComponentsPerRegister);
388 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
389 mCurrentOffset += ComponentsPerRegister * numRegisters;
390 }
391 else
392 {
393 mCurrentOffset += type.getElementSize();
394 }
395 }
396
397 Attribute::Attribute()
398 {
399 type = GL_NONE;
400 arraySize = 0;
401 registerIndex = 0;
402 }
403
404 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
405 {
406 this->type = type;
407 this->name = name;
408 this->arraySize = arraySize;
409 this->location = location;
410 this->registerIndex = registerIndex;
411 }
412
413 sw::PixelShader *Shader::getPixelShader() const
414 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500415 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400416 }
417
418 sw::VertexShader *Shader::getVertexShader() const
419 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500420 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400421 }
422
423 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
424 {
425 TString name = TFunction::unmangleName(nodeName);
426
427 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D")
428 {
429 method = IMPLICIT;
430 }
431 else if(name == "texture2DProj" || name == "textureProj")
432 {
433 method = IMPLICIT;
434 proj = true;
435 }
436 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
437 {
438 method = LOD;
439 }
440 else if(name == "texture2DProjLod" || name == "textureProjLod")
441 {
442 method = LOD;
443 proj = true;
444 }
445 else if(name == "textureSize")
446 {
447 method = SIZE;
448 }
449 else if(name == "textureOffset")
450 {
451 method = IMPLICIT;
452 offset = true;
453 }
454 else if(name == "textureProjOffset")
455 {
456 method = IMPLICIT;
457 offset = true;
458 proj = true;
459 }
460 else if(name == "textureLodOffset")
461 {
462 method = LOD;
463 offset = true;
464 }
465 else if(name == "textureProjLodOffset")
466 {
467 method = LOD;
468 proj = true;
469 offset = true;
470 }
471 else if(name == "texelFetch")
472 {
473 method = FETCH;
474 }
475 else if(name == "texelFetchOffset")
476 {
477 method = FETCH;
478 offset = true;
479 }
480 else if(name == "textureGrad")
481 {
482 method = GRAD;
483 }
484 else if(name == "textureGradOffset")
485 {
486 method = GRAD;
487 offset = true;
488 }
489 else if(name == "textureProjGrad")
490 {
491 method = GRAD;
492 proj = true;
493 }
494 else if(name == "textureProjGradOffset")
495 {
496 method = GRAD;
497 proj = true;
498 offset = true;
499 }
500 else UNREACHABLE(0);
501 }
502
503 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
504 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500505 shader = nullptr;
506 pixelShader = nullptr;
507 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400508
509 if(shaderObject)
510 {
511 shader = shaderObject->getShader();
512 pixelShader = shaderObject->getPixelShader();
513 vertexShader = shaderObject->getVertexShader();
514 }
515
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500516 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400517 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500518 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400519 }
520
521 OutputASM::~OutputASM()
522 {
523 }
524
525 void OutputASM::output()
526 {
527 if(shader)
528 {
529 emitShader(GLOBAL);
530
531 if(functionArray.size() > 1) // Only call main() when there are other functions
532 {
533 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
534 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
535 callMain->dst.index = 0; // main()
536
537 emit(sw::Shader::OPCODE_RET);
538 }
539
540 emitShader(FUNCTION);
541 }
542 }
543
544 void OutputASM::emitShader(Scope scope)
545 {
546 emitScope = scope;
547 currentScope = GLOBAL;
548 mContext.getTreeRoot()->traverse(this);
549 }
550
551 void OutputASM::freeTemporary(Temporary *temporary)
552 {
553 free(temporaries, temporary);
554 }
555
556 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
557 {
558 TBasicType baseType = in->getType().getBasicType();
559
560 switch(op)
561 {
562 case sw::Shader::OPCODE_NEG:
563 switch(baseType)
564 {
565 case EbtInt:
566 case EbtUInt:
567 return sw::Shader::OPCODE_INEG;
568 case EbtFloat:
569 default:
570 return op;
571 }
572 case sw::Shader::OPCODE_ABS:
573 switch(baseType)
574 {
575 case EbtInt:
576 return sw::Shader::OPCODE_IABS;
577 case EbtFloat:
578 default:
579 return op;
580 }
581 case sw::Shader::OPCODE_SGN:
582 switch(baseType)
583 {
584 case EbtInt:
585 return sw::Shader::OPCODE_ISGN;
586 case EbtFloat:
587 default:
588 return op;
589 }
590 case sw::Shader::OPCODE_ADD:
591 switch(baseType)
592 {
593 case EbtInt:
594 case EbtUInt:
595 return sw::Shader::OPCODE_IADD;
596 case EbtFloat:
597 default:
598 return op;
599 }
600 case sw::Shader::OPCODE_SUB:
601 switch(baseType)
602 {
603 case EbtInt:
604 case EbtUInt:
605 return sw::Shader::OPCODE_ISUB;
606 case EbtFloat:
607 default:
608 return op;
609 }
610 case sw::Shader::OPCODE_MUL:
611 switch(baseType)
612 {
613 case EbtInt:
614 case EbtUInt:
615 return sw::Shader::OPCODE_IMUL;
616 case EbtFloat:
617 default:
618 return op;
619 }
620 case sw::Shader::OPCODE_DIV:
621 switch(baseType)
622 {
623 case EbtInt:
624 return sw::Shader::OPCODE_IDIV;
625 case EbtUInt:
626 return sw::Shader::OPCODE_UDIV;
627 case EbtFloat:
628 default:
629 return op;
630 }
631 case sw::Shader::OPCODE_IMOD:
632 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
633 case sw::Shader::OPCODE_ISHR:
634 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
635 case sw::Shader::OPCODE_MIN:
636 switch(baseType)
637 {
638 case EbtInt:
639 return sw::Shader::OPCODE_IMIN;
640 case EbtUInt:
641 return sw::Shader::OPCODE_UMIN;
642 case EbtFloat:
643 default:
644 return op;
645 }
646 case sw::Shader::OPCODE_MAX:
647 switch(baseType)
648 {
649 case EbtInt:
650 return sw::Shader::OPCODE_IMAX;
651 case EbtUInt:
652 return sw::Shader::OPCODE_UMAX;
653 case EbtFloat:
654 default:
655 return op;
656 }
657 default:
658 return op;
659 }
660 }
661
662 void OutputASM::visitSymbol(TIntermSymbol *symbol)
663 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500664 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
665 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
666 switch(symbol->getQualifier())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400667 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500668 case EvqVaryingIn:
669 case EvqVaryingOut:
670 case EvqInvariantVaryingIn:
671 case EvqInvariantVaryingOut:
672 case EvqVertexOut:
673 case EvqFragmentIn:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400674 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
675 {
676 declareVarying(symbol, -1);
677 }
Nicolas Capens6896e352018-01-10 12:46:52 -0500678 break;
679 default:
680 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400681 }
682
683 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
684 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
685 // "All members of a named uniform block declared with a shared or std140 layout qualifier
686 // are considered active, even if they are not referenced in any shader in the program.
687 // The uniform block itself is also considered active, even if no member of the block is referenced."
688 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
689 {
690 uniformRegister(symbol);
691 }
692 }
693
694 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
695 {
696 if(currentScope != emitScope)
697 {
698 return false;
699 }
700
701 TIntermTyped *result = node;
702 TIntermTyped *left = node->getLeft();
703 TIntermTyped *right = node->getRight();
704 const TType &leftType = left->getType();
705 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400706
707 if(isSamplerRegister(result))
708 {
709 return false; // Don't traverse, the register index is determined statically
710 }
711
712 switch(node->getOp())
713 {
714 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500715 assert(visit == PreVisit);
716 right->traverse(this);
717 assignLvalue(left, right);
718 copy(result, right);
719 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500721 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500722 // Constant arrays go into the constant register file.
723 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
724 {
725 for(int i = 0; i < left->totalRegisterCount(); i++)
726 {
727 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
728 }
729 }
730 else
731 {
732 right->traverse(this);
733 copy(left, right);
734 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500735 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400736 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500737 assert(visit == PreVisit);
738 right->traverse(this);
739 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400740 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500741 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400742 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500743
744 assignLvalue(left, result);
745 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400746 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500747 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400748 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500749 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400750 int size = leftType.getNominalSize();
751
752 for(int i = 0; i < size; i++)
753 {
754 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
755 dot->dst.mask = 1 << i;
756 }
757
758 assignLvalue(left, result);
759 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500760 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400761 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500762 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400763 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500764 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400765 int dim = leftType.getNominalSize();
766
767 for(int i = 0; i < dim; i++)
768 {
769 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
770 mul->src[1].swizzle = 0x00;
771
772 for(int j = 1; j < dim; j++)
773 {
774 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
775 mad->src[1].swizzle = j * 0x55;
776 }
777 }
778
779 assignLvalue(left, result);
780 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500781 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400782 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400783 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400784 case EOpIndexDirectStruct:
785 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500786 assert(visit == PreVisit);
787 evaluateRvalue(node);
788 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400789 case EOpVectorSwizzle:
790 if(visit == PostVisit)
791 {
792 int swizzle = 0;
793 TIntermAggregate *components = right->getAsAggregate();
794
795 if(components)
796 {
797 TIntermSequence &sequence = components->getSequence();
798 int component = 0;
799
800 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
801 {
802 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
803
804 if(element)
805 {
806 int i = element->getUnionArrayPointer()[0].getIConst();
807 swizzle |= i << (component * 2);
808 component++;
809 }
810 else UNREACHABLE(0);
811 }
812 }
813 else UNREACHABLE(0);
814
815 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
816 mov->src[0].swizzle = swizzle;
817 }
818 break;
819 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
820 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
821 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
822 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
823 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
824 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
825 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
826 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
827 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
828 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
829 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
830 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
831 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
832 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
833 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
834 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
835 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
836 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
837 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
838 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
839 case EOpEqual:
840 if(visit == PostVisit)
841 {
842 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
843
844 for(int index = 1; index < left->totalRegisterCount(); index++)
845 {
846 Temporary equal(this);
847 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
848 emit(sw::Shader::OPCODE_AND, result, result, &equal);
849 }
850 }
851 break;
852 case EOpNotEqual:
853 if(visit == PostVisit)
854 {
855 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
856
857 for(int index = 1; index < left->totalRegisterCount(); index++)
858 {
859 Temporary notEqual(this);
860 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
861 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
862 }
863 }
864 break;
865 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
866 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
867 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
868 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
869 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
870 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
871 case EOpMatrixTimesScalar:
872 if(visit == PostVisit)
873 {
874 if(left->isMatrix())
875 {
876 for(int i = 0; i < leftType.getNominalSize(); i++)
877 {
878 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
879 }
880 }
881 else if(right->isMatrix())
882 {
883 for(int i = 0; i < rightType.getNominalSize(); i++)
884 {
885 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
886 }
887 }
888 else UNREACHABLE(0);
889 }
890 break;
891 case EOpVectorTimesMatrix:
892 if(visit == PostVisit)
893 {
894 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
895
896 int size = rightType.getNominalSize();
897 for(int i = 0; i < size; i++)
898 {
899 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
900 dot->dst.mask = 1 << i;
901 }
902 }
903 break;
904 case EOpMatrixTimesVector:
905 if(visit == PostVisit)
906 {
907 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
908 mul->src[1].swizzle = 0x00;
909
910 int size = rightType.getNominalSize();
911 for(int i = 1; i < size; i++)
912 {
913 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
914 mad->src[1].swizzle = i * 0x55;
915 }
916 }
917 break;
918 case EOpMatrixTimesMatrix:
919 if(visit == PostVisit)
920 {
921 int dim = leftType.getNominalSize();
922
923 int size = rightType.getNominalSize();
924 for(int i = 0; i < size; i++)
925 {
926 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
927 mul->src[1].swizzle = 0x00;
928
929 for(int j = 1; j < dim; j++)
930 {
931 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
932 mad->src[1].swizzle = j * 0x55;
933 }
934 }
935 }
936 break;
937 case EOpLogicalOr:
938 if(trivial(right, 6))
939 {
940 if(visit == PostVisit)
941 {
942 emit(sw::Shader::OPCODE_OR, result, left, right);
943 }
944 }
945 else // Short-circuit evaluation
946 {
947 if(visit == InVisit)
948 {
949 emit(sw::Shader::OPCODE_MOV, result, left);
950 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
951 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
952 }
953 else if(visit == PostVisit)
954 {
955 emit(sw::Shader::OPCODE_MOV, result, right);
956 emit(sw::Shader::OPCODE_ENDIF);
957 }
958 }
959 break;
960 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
961 case EOpLogicalAnd:
962 if(trivial(right, 6))
963 {
964 if(visit == PostVisit)
965 {
966 emit(sw::Shader::OPCODE_AND, result, left, right);
967 }
968 }
969 else // Short-circuit evaluation
970 {
971 if(visit == InVisit)
972 {
973 emit(sw::Shader::OPCODE_MOV, result, left);
974 emit(sw::Shader::OPCODE_IF, 0, result);
975 }
976 else if(visit == PostVisit)
977 {
978 emit(sw::Shader::OPCODE_MOV, result, right);
979 emit(sw::Shader::OPCODE_ENDIF);
980 }
981 }
982 break;
983 default: UNREACHABLE(node->getOp());
984 }
985
986 return true;
987 }
988
989 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
990 {
991 switch(size)
992 {
993 case 1: // Used for cofactor computation only
994 {
995 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
996 bool isMov = (row == col);
997 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
998 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
999 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1000 mov->dst.mask = 1 << outRow;
1001 }
1002 break;
1003 case 2:
1004 {
1005 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1006
1007 bool isCofactor = (col >= 0) && (row >= 0);
1008 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1009 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1010 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1011
1012 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1013 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1014 det->dst.mask = 1 << outRow;
1015 }
1016 break;
1017 case 3:
1018 {
1019 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1020
1021 bool isCofactor = (col >= 0) && (row >= 0);
1022 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1023 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1024 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1025 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1026
1027 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1028 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1029 det->dst.mask = 1 << outRow;
1030 }
1031 break;
1032 case 4:
1033 {
1034 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1035 det->dst.mask = 1 << outRow;
1036 }
1037 break;
1038 default:
1039 UNREACHABLE(size);
1040 break;
1041 }
1042 }
1043
1044 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1045 {
1046 if(currentScope != emitScope)
1047 {
1048 return false;
1049 }
1050
1051 TIntermTyped *result = node;
1052 TIntermTyped *arg = node->getOperand();
1053 TBasicType basicType = arg->getType().getBasicType();
1054
1055 union
1056 {
1057 float f;
1058 int i;
1059 } one_value;
1060
1061 if(basicType == EbtInt || basicType == EbtUInt)
1062 {
1063 one_value.i = 1;
1064 }
1065 else
1066 {
1067 one_value.f = 1.0f;
1068 }
1069
1070 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1071 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1072 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1073
1074 switch(node->getOp())
1075 {
1076 case EOpNegative:
1077 if(visit == PostVisit)
1078 {
1079 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1080 for(int index = 0; index < arg->totalRegisterCount(); index++)
1081 {
1082 emit(negOpcode, result, index, arg, index);
1083 }
1084 }
1085 break;
1086 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1087 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001088 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001089 case EOpPostIncrement:
1090 if(visit == PostVisit)
1091 {
1092 copy(result, arg);
1093
1094 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1095 for(int index = 0; index < arg->totalRegisterCount(); index++)
1096 {
1097 emit(addOpcode, arg, index, arg, index, &one);
1098 }
1099
1100 assignLvalue(arg, arg);
1101 }
1102 break;
1103 case EOpPostDecrement:
1104 if(visit == PostVisit)
1105 {
1106 copy(result, arg);
1107
1108 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1109 for(int index = 0; index < arg->totalRegisterCount(); index++)
1110 {
1111 emit(subOpcode, arg, index, arg, index, &one);
1112 }
1113
1114 assignLvalue(arg, arg);
1115 }
1116 break;
1117 case EOpPreIncrement:
1118 if(visit == PostVisit)
1119 {
1120 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1121 for(int index = 0; index < arg->totalRegisterCount(); index++)
1122 {
1123 emit(addOpcode, result, index, arg, index, &one);
1124 }
1125
1126 assignLvalue(arg, result);
1127 }
1128 break;
1129 case EOpPreDecrement:
1130 if(visit == PostVisit)
1131 {
1132 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1133 for(int index = 0; index < arg->totalRegisterCount(); index++)
1134 {
1135 emit(subOpcode, result, index, arg, index, &one);
1136 }
1137
1138 assignLvalue(arg, result);
1139 }
1140 break;
1141 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1142 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1143 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1144 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1145 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1146 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1147 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1148 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1149 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1150 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1151 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1152 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1153 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1154 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1155 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1156 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1157 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1158 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1159 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1160 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1161 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1162 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1163 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1164 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1165 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1166 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1167 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1168 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1169 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1170 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1171 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1172 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1173 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1174 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1175 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1176 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1177 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1178 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1179 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1180 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1181 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1182 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1183 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1184 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1185 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1186 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1187 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1188 case EOpTranspose:
1189 if(visit == PostVisit)
1190 {
1191 int numCols = arg->getNominalSize();
1192 int numRows = arg->getSecondarySize();
1193 for(int i = 0; i < numCols; ++i)
1194 {
1195 for(int j = 0; j < numRows; ++j)
1196 {
1197 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1198 mov->src[0].swizzle = 0x55 * j;
1199 mov->dst.mask = 1 << i;
1200 }
1201 }
1202 }
1203 break;
1204 case EOpDeterminant:
1205 if(visit == PostVisit)
1206 {
1207 int size = arg->getNominalSize();
1208 ASSERT(size == arg->getSecondarySize());
1209
1210 emitDeterminant(result, arg, size);
1211 }
1212 break;
1213 case EOpInverse:
1214 if(visit == PostVisit)
1215 {
1216 int size = arg->getNominalSize();
1217 ASSERT(size == arg->getSecondarySize());
1218
1219 // Compute transposed matrix of cofactors
1220 for(int i = 0; i < size; ++i)
1221 {
1222 for(int j = 0; j < size; ++j)
1223 {
1224 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1225 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1226 emitDeterminant(result, arg, size - 1, j, i, i, j);
1227 }
1228 }
1229
1230 // Compute 1 / determinant
1231 Temporary invDet(this);
1232 emitDeterminant(&invDet, arg, size);
1233 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1234 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1235 div->src[1].swizzle = 0x00; // xxxx
1236
1237 // Divide transposed matrix of cofactors by determinant
1238 for(int i = 0; i < size; ++i)
1239 {
1240 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1241 }
1242 }
1243 break;
1244 default: UNREACHABLE(node->getOp());
1245 }
1246
1247 return true;
1248 }
1249
1250 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1251 {
1252 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1253 {
1254 return false;
1255 }
1256
1257 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1258
1259 TIntermTyped *result = node;
1260 const TType &resultType = node->getType();
1261 TIntermSequence &arg = node->getSequence();
1262 size_t argumentCount = arg.size();
1263
1264 switch(node->getOp())
1265 {
1266 case EOpSequence: break;
1267 case EOpDeclaration: break;
1268 case EOpInvariantDeclaration: break;
1269 case EOpPrototype: break;
1270 case EOpComma:
1271 if(visit == PostVisit)
1272 {
1273 copy(result, arg[1]);
1274 }
1275 break;
1276 case EOpFunction:
1277 if(visit == PreVisit)
1278 {
1279 const TString &name = node->getName();
1280
1281 if(emitScope == FUNCTION)
1282 {
1283 if(functionArray.size() > 1) // No need for a label when there's only main()
1284 {
1285 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1286 label->dst.type = sw::Shader::PARAMETER_LABEL;
1287
1288 const Function *function = findFunction(name);
1289 ASSERT(function); // Should have been added during global pass
1290 label->dst.index = function->label;
1291 currentFunction = function->label;
1292 }
1293 }
1294 else if(emitScope == GLOBAL)
1295 {
1296 if(name != "main(")
1297 {
1298 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1299 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1300 }
1301 }
1302 else UNREACHABLE(emitScope);
1303
1304 currentScope = FUNCTION;
1305 }
1306 else if(visit == PostVisit)
1307 {
1308 if(emitScope == FUNCTION)
1309 {
1310 if(functionArray.size() > 1) // No need to return when there's only main()
1311 {
1312 emit(sw::Shader::OPCODE_RET);
1313 }
1314 }
1315
1316 currentScope = GLOBAL;
1317 }
1318 break;
1319 case EOpFunctionCall:
1320 if(visit == PostVisit)
1321 {
1322 if(node->isUserDefined())
1323 {
1324 const TString &name = node->getName();
1325 const Function *function = findFunction(name);
1326
1327 if(!function)
1328 {
1329 mContext.error(node->getLine(), "function definition not found", name.c_str());
1330 return false;
1331 }
1332
1333 TIntermSequence &arguments = *function->arg;
1334
1335 for(size_t i = 0; i < argumentCount; i++)
1336 {
1337 TIntermTyped *in = arguments[i]->getAsTyped();
1338
1339 if(in->getQualifier() == EvqIn ||
1340 in->getQualifier() == EvqInOut ||
1341 in->getQualifier() == EvqConstReadOnly)
1342 {
1343 copy(in, arg[i]);
1344 }
1345 }
1346
1347 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1348 call->dst.type = sw::Shader::PARAMETER_LABEL;
1349 call->dst.index = function->label;
1350
1351 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1352 {
1353 copy(result, function->ret);
1354 }
1355
1356 for(size_t i = 0; i < argumentCount; i++)
1357 {
1358 TIntermTyped *argument = arguments[i]->getAsTyped();
1359 TIntermTyped *out = arg[i]->getAsTyped();
1360
1361 if(argument->getQualifier() == EvqOut ||
1362 argument->getQualifier() == EvqInOut)
1363 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001364 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001365 }
1366 }
1367 }
1368 else
1369 {
1370 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001371 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001372 TIntermTyped *t = arg[1]->getAsTyped();
1373
1374 Temporary coord(this);
1375
1376 if(textureFunction.proj)
1377 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001378 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1379 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1380 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001381
Nicolas Capens0484c792016-06-13 22:02:36 -04001382 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1383 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001384
1385 if(IsShadowSampler(s->getBasicType()))
1386 {
1387 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1388 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1389 mov->src[0].swizzle = 0xA4;
1390 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001391 }
1392 else
1393 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001394 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1395
1396 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1397 {
1398 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1399 mov->src[0].swizzle = 0xA4;
1400 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001401 }
1402
1403 switch(textureFunction.method)
1404 {
1405 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001406 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001407 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001408 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001409 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001410 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001411 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001412 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001413 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001414 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1415 }
1416 else UNREACHABLE(argumentCount);
1417 }
1418 else // Offset
1419 {
1420 if(argumentCount == 3)
1421 {
1422 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1423 }
1424 else if(argumentCount == 4) // Bias
1425 {
1426 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001427 }
1428 else UNREACHABLE(argumentCount);
1429 }
1430 break;
1431 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001432 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001433 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001434 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001435 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001436 else if(argumentCount == 4) // Offset
1437 {
1438 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1439 }
1440 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001441 break;
1442 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001443 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001444 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001445 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001446 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001447 else if(argumentCount == 4) // Offset
1448 {
1449 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1450 }
1451 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001452 break;
1453 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001454 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001455 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001456 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001457 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001458 else if(argumentCount == 5) // Offset
1459 {
1460 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1461 }
1462 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001463 break;
1464 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001465 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001466 break;
1467 default:
1468 UNREACHABLE(textureFunction.method);
1469 }
1470 }
1471 }
1472 break;
1473 case EOpParameters:
1474 break;
1475 case EOpConstructFloat:
1476 case EOpConstructVec2:
1477 case EOpConstructVec3:
1478 case EOpConstructVec4:
1479 case EOpConstructBool:
1480 case EOpConstructBVec2:
1481 case EOpConstructBVec3:
1482 case EOpConstructBVec4:
1483 case EOpConstructInt:
1484 case EOpConstructIVec2:
1485 case EOpConstructIVec3:
1486 case EOpConstructIVec4:
1487 case EOpConstructUInt:
1488 case EOpConstructUVec2:
1489 case EOpConstructUVec3:
1490 case EOpConstructUVec4:
1491 if(visit == PostVisit)
1492 {
1493 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001494 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1495 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001496 for(size_t i = 0; i < argumentCount; i++)
1497 {
1498 TIntermTyped *argi = arg[i]->getAsTyped();
1499 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001500 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1501 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001502
1503 if(!argi->isMatrix())
1504 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001505 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1506 mov->dst.mask = (0xF << swizzle) & 0xF;
1507 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001508
1509 component += size;
1510 }
1511 else // Matrix
1512 {
1513 int column = 0;
1514
1515 while(component < resultType.getNominalSize())
1516 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001517 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1518 mov->dst.mask = (0xF << swizzle) & 0xF;
1519 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001520
1521 column++;
1522 component += size;
1523 }
1524 }
1525 }
1526 }
1527 break;
1528 case EOpConstructMat2:
1529 case EOpConstructMat2x3:
1530 case EOpConstructMat2x4:
1531 case EOpConstructMat3x2:
1532 case EOpConstructMat3:
1533 case EOpConstructMat3x4:
1534 case EOpConstructMat4x2:
1535 case EOpConstructMat4x3:
1536 case EOpConstructMat4:
1537 if(visit == PostVisit)
1538 {
1539 TIntermTyped *arg0 = arg[0]->getAsTyped();
1540 const int outCols = result->getNominalSize();
1541 const int outRows = result->getSecondarySize();
1542
1543 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1544 {
1545 for(int i = 0; i < outCols; i++)
1546 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001547 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001548 Instruction *mov = emitCast(result, i, arg0, 0);
1549 mov->dst.mask = 1 << i;
1550 ASSERT(mov->src[0].swizzle == 0x00);
1551 }
1552 }
1553 else if(arg0->isMatrix())
1554 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001555 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001556
Alexis Hetu2a198552016-09-27 20:50:45 -04001557 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001558 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001559 TIntermTyped *argi = arg[n]->getAsTyped();
1560 const int inCols = argi->getNominalSize();
1561 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001562
Alexis Hetu2a198552016-09-27 20:50:45 -04001563 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001564 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001565 if(i >= inCols || outRows > inRows)
1566 {
1567 // Initialize to identity matrix
1568 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));
1569 emitCast(result, i + n * outCols, &col, 0);
1570 }
1571
1572 if(i < inCols)
1573 {
1574 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1575 mov->dst.mask = 0xF >> (4 - inRows);
1576 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001577 }
1578 }
1579 }
1580 else
1581 {
1582 int column = 0;
1583 int row = 0;
1584
1585 for(size_t i = 0; i < argumentCount; i++)
1586 {
1587 TIntermTyped *argi = arg[i]->getAsTyped();
1588 int size = argi->getNominalSize();
1589 int element = 0;
1590
1591 while(element < size)
1592 {
1593 Instruction *mov = emitCast(result, column, argi, 0);
1594 mov->dst.mask = (0xF << row) & 0xF;
1595 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1596
1597 int end = row + size - element;
1598 column = end >= outRows ? column + 1 : column;
1599 element = element + outRows - row;
1600 row = end >= outRows ? 0 : end;
1601 }
1602 }
1603 }
1604 }
1605 break;
1606 case EOpConstructStruct:
1607 if(visit == PostVisit)
1608 {
1609 int offset = 0;
1610 for(size_t i = 0; i < argumentCount; i++)
1611 {
1612 TIntermTyped *argi = arg[i]->getAsTyped();
1613 int size = argi->totalRegisterCount();
1614
1615 for(int index = 0; index < size; index++)
1616 {
1617 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1618 mov->dst.mask = writeMask(result, offset + index);
1619 }
1620
1621 offset += size;
1622 }
1623 }
1624 break;
1625 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1626 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1627 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1628 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1629 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1630 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1631 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1632 case EOpModf:
1633 if(visit == PostVisit)
1634 {
1635 TIntermTyped* arg1 = arg[1]->getAsTyped();
1636 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1637 assignLvalue(arg1, arg1);
1638 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1639 }
1640 break;
1641 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1642 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1643 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1644 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1645 case EOpClamp:
1646 if(visit == PostVisit)
1647 {
1648 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1649 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1650 }
1651 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001652 case EOpMix:
1653 if(visit == PostVisit)
1654 {
1655 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1656 {
1657 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1658 }
1659 else
1660 {
1661 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1662 }
1663 }
1664 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001665 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1666 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1667 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1668 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1669 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1670 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1671 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1672 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1673 case EOpMul:
1674 if(visit == PostVisit)
1675 {
1676 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001677 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1678 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001679
1680 int size = arg0->getNominalSize();
1681 for(int i = 0; i < size; i++)
1682 {
1683 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1684 }
1685 }
1686 break;
1687 case EOpOuterProduct:
1688 if(visit == PostVisit)
1689 {
1690 for(int i = 0; i < dim(arg[1]); i++)
1691 {
1692 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1693 mul->src[1].swizzle = 0x55 * i;
1694 }
1695 }
1696 break;
1697 default: UNREACHABLE(node->getOp());
1698 }
1699
1700 return true;
1701 }
1702
1703 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1704 {
1705 if(currentScope != emitScope)
1706 {
1707 return false;
1708 }
1709
1710 TIntermTyped *condition = node->getCondition();
1711 TIntermNode *trueBlock = node->getTrueBlock();
1712 TIntermNode *falseBlock = node->getFalseBlock();
1713 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1714
1715 condition->traverse(this);
1716
1717 if(node->usesTernaryOperator())
1718 {
1719 if(constantCondition)
1720 {
1721 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1722
1723 if(trueCondition)
1724 {
1725 trueBlock->traverse(this);
1726 copy(node, trueBlock);
1727 }
1728 else
1729 {
1730 falseBlock->traverse(this);
1731 copy(node, falseBlock);
1732 }
1733 }
1734 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1735 {
1736 trueBlock->traverse(this);
1737 falseBlock->traverse(this);
1738 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1739 }
1740 else
1741 {
1742 emit(sw::Shader::OPCODE_IF, 0, condition);
1743
1744 if(trueBlock)
1745 {
1746 trueBlock->traverse(this);
1747 copy(node, trueBlock);
1748 }
1749
1750 if(falseBlock)
1751 {
1752 emit(sw::Shader::OPCODE_ELSE);
1753 falseBlock->traverse(this);
1754 copy(node, falseBlock);
1755 }
1756
1757 emit(sw::Shader::OPCODE_ENDIF);
1758 }
1759 }
1760 else // if/else statement
1761 {
1762 if(constantCondition)
1763 {
1764 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1765
1766 if(trueCondition)
1767 {
1768 if(trueBlock)
1769 {
1770 trueBlock->traverse(this);
1771 }
1772 }
1773 else
1774 {
1775 if(falseBlock)
1776 {
1777 falseBlock->traverse(this);
1778 }
1779 }
1780 }
1781 else
1782 {
1783 emit(sw::Shader::OPCODE_IF, 0, condition);
1784
1785 if(trueBlock)
1786 {
1787 trueBlock->traverse(this);
1788 }
1789
1790 if(falseBlock)
1791 {
1792 emit(sw::Shader::OPCODE_ELSE);
1793 falseBlock->traverse(this);
1794 }
1795
1796 emit(sw::Shader::OPCODE_ENDIF);
1797 }
1798 }
1799
1800 return false;
1801 }
1802
1803 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1804 {
1805 if(currentScope != emitScope)
1806 {
1807 return false;
1808 }
1809
1810 unsigned int iterations = loopCount(node);
1811
1812 if(iterations == 0)
1813 {
1814 return false;
1815 }
1816
1817 bool unroll = (iterations <= 4);
1818
1819 if(unroll)
1820 {
1821 LoopUnrollable loopUnrollable;
1822 unroll = loopUnrollable.traverse(node);
1823 }
1824
1825 TIntermNode *init = node->getInit();
1826 TIntermTyped *condition = node->getCondition();
1827 TIntermTyped *expression = node->getExpression();
1828 TIntermNode *body = node->getBody();
1829 Constant True(true);
1830
1831 if(node->getType() == ELoopDoWhile)
1832 {
1833 Temporary iterate(this);
1834 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1835
1836 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1837
1838 if(body)
1839 {
1840 body->traverse(this);
1841 }
1842
1843 emit(sw::Shader::OPCODE_TEST);
1844
1845 condition->traverse(this);
1846 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1847
1848 emit(sw::Shader::OPCODE_ENDWHILE);
1849 }
1850 else
1851 {
1852 if(init)
1853 {
1854 init->traverse(this);
1855 }
1856
1857 if(unroll)
1858 {
1859 for(unsigned int i = 0; i < iterations; i++)
1860 {
1861 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1862
1863 if(body)
1864 {
1865 body->traverse(this);
1866 }
1867
1868 if(expression)
1869 {
1870 expression->traverse(this);
1871 }
1872 }
1873 }
1874 else
1875 {
1876 if(condition)
1877 {
1878 condition->traverse(this);
1879 }
1880 else
1881 {
1882 condition = &True;
1883 }
1884
1885 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1886
1887 if(body)
1888 {
1889 body->traverse(this);
1890 }
1891
1892 emit(sw::Shader::OPCODE_TEST);
1893
1894 if(expression)
1895 {
1896 expression->traverse(this);
1897 }
1898
1899 if(condition)
1900 {
1901 condition->traverse(this);
1902 }
1903
1904 emit(sw::Shader::OPCODE_ENDWHILE);
1905 }
1906 }
1907
1908 return false;
1909 }
1910
1911 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1912 {
1913 if(currentScope != emitScope)
1914 {
1915 return false;
1916 }
1917
1918 switch(node->getFlowOp())
1919 {
1920 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1921 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1922 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1923 case EOpReturn:
1924 if(visit == PostVisit)
1925 {
1926 TIntermTyped *value = node->getExpression();
1927
1928 if(value)
1929 {
1930 copy(functionArray[currentFunction].ret, value);
1931 }
1932
1933 emit(sw::Shader::OPCODE_LEAVE);
1934 }
1935 break;
1936 default: UNREACHABLE(node->getFlowOp());
1937 }
1938
1939 return true;
1940 }
1941
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001942 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1943 {
1944 if(currentScope != emitScope)
1945 {
1946 return false;
1947 }
1948
1949 TIntermTyped* switchValue = node->getInit();
1950 TIntermAggregate* opList = node->getStatementList();
1951
1952 if(!switchValue || !opList)
1953 {
1954 return false;
1955 }
1956
1957 switchValue->traverse(this);
1958
1959 emit(sw::Shader::OPCODE_SWITCH);
1960
1961 TIntermSequence& sequence = opList->getSequence();
1962 TIntermSequence::iterator it = sequence.begin();
1963 TIntermSequence::iterator defaultIt = sequence.end();
1964 int nbCases = 0;
1965 for(; it != sequence.end(); ++it)
1966 {
1967 TIntermCase* currentCase = (*it)->getAsCaseNode();
1968 if(currentCase)
1969 {
1970 TIntermSequence::iterator caseIt = it;
1971
1972 TIntermTyped* condition = currentCase->getCondition();
1973 if(condition) // non default case
1974 {
1975 if(nbCases != 0)
1976 {
1977 emit(sw::Shader::OPCODE_ELSE);
1978 }
1979
1980 condition->traverse(this);
1981 Temporary result(this);
1982 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1983 emit(sw::Shader::OPCODE_IF, 0, &result);
1984 nbCases++;
1985
Nicolas Capens6d123312018-01-08 12:57:52 -05001986 // Emit the code for this case and all subsequent cases until we hit a break statement.
1987 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001988 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1989 {
1990 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05001991
1992 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
1993 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
1994 // Note that this eliminates useless operations but shouldn't affect correctness.
1995 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001996 {
1997 break;
1998 }
1999 }
2000 }
2001 else
2002 {
2003 defaultIt = it; // The default case might not be the last case, keep it for last
2004 }
2005 }
2006 }
2007
2008 // If there's a default case, traverse it here
2009 if(defaultIt != sequence.end())
2010 {
2011 emit(sw::Shader::OPCODE_ELSE);
2012 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2013 {
2014 (*defaultIt)->traverse(this);
2015 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2016 {
2017 break;
2018 }
2019 }
2020 }
2021
2022 for(int i = 0; i < nbCases; ++i)
2023 {
2024 emit(sw::Shader::OPCODE_ENDIF);
2025 }
2026
2027 emit(sw::Shader::OPCODE_ENDSWITCH);
2028
2029 return false;
2030 }
2031
Nicolas Capens0bac2852016-05-07 06:09:58 -04002032 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2033 {
2034 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2035 }
2036
2037 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2038 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2039 {
2040 Instruction *instruction = new Instruction(op);
2041
2042 if(dst)
2043 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002044 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002045 }
2046
Alexis Hetu929c6b02017-11-07 16:04:25 -05002047 if(src0)
2048 {
2049 TIntermTyped* src = src0->getAsTyped();
2050 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2051 }
2052
Nicolas Capens0530b452017-11-15 16:39:47 -05002053 source(instruction->src[0], src0, index0);
2054 source(instruction->src[1], src1, index1);
2055 source(instruction->src[2], src2, index2);
2056 source(instruction->src[3], src3, index3);
2057 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002058
2059 shader->append(instruction);
2060
2061 return instruction;
2062 }
2063
2064 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2065 {
2066 return emitCast(dst, 0, src, 0);
2067 }
2068
2069 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2070 {
2071 switch(src->getBasicType())
2072 {
2073 case EbtBool:
2074 switch(dst->getBasicType())
2075 {
2076 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2077 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2078 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2079 default: break;
2080 }
2081 break;
2082 case EbtInt:
2083 switch(dst->getBasicType())
2084 {
2085 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2086 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2087 default: break;
2088 }
2089 break;
2090 case EbtUInt:
2091 switch(dst->getBasicType())
2092 {
2093 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2094 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2095 default: break;
2096 }
2097 break;
2098 case EbtFloat:
2099 switch(dst->getBasicType())
2100 {
2101 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2102 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2103 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2104 default: break;
2105 }
2106 break;
2107 default:
2108 break;
2109 }
2110
2111 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2112 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2113 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2114
2115 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2116 }
2117
2118 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2119 {
2120 for(int index = 0; index < dst->elementRegisterCount(); index++)
2121 {
2122 emit(op, dst, index, src0, index, src1, index, src2, index);
2123 }
2124 }
2125
2126 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2127 {
2128 emitBinary(op, result, src0, src1);
2129 assignLvalue(lhs, result);
2130 }
2131
2132 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2133 {
2134 sw::Shader::Opcode opcode;
2135 switch(left->getAsTyped()->getBasicType())
2136 {
2137 case EbtBool:
2138 case EbtInt:
2139 opcode = sw::Shader::OPCODE_ICMP;
2140 break;
2141 case EbtUInt:
2142 opcode = sw::Shader::OPCODE_UCMP;
2143 break;
2144 default:
2145 opcode = sw::Shader::OPCODE_CMP;
2146 break;
2147 }
2148
2149 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2150 cmp->control = cmpOp;
2151 }
2152
2153 int componentCount(const TType &type, int registers)
2154 {
2155 if(registers == 0)
2156 {
2157 return 0;
2158 }
2159
2160 if(type.isArray() && registers >= type.elementRegisterCount())
2161 {
2162 int index = registers / type.elementRegisterCount();
2163 registers -= index * type.elementRegisterCount();
2164 return index * type.getElementSize() + componentCount(type, registers);
2165 }
2166
2167 if(type.isStruct() || type.isInterfaceBlock())
2168 {
2169 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2170 int elements = 0;
2171
Alexis Hetuda163ed2018-01-03 16:36:14 -05002172 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002173 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002174 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002175
2176 if(fieldType.totalRegisterCount() <= registers)
2177 {
2178 registers -= fieldType.totalRegisterCount();
2179 elements += fieldType.getObjectSize();
2180 }
2181 else // Register within this field
2182 {
2183 return elements + componentCount(fieldType, registers);
2184 }
2185 }
2186 }
2187 else if(type.isMatrix())
2188 {
2189 return registers * type.registerSize();
2190 }
2191
2192 UNREACHABLE(0);
2193 return 0;
2194 }
2195
2196 int registerSize(const TType &type, int registers)
2197 {
2198 if(registers == 0)
2199 {
2200 if(type.isStruct())
2201 {
2202 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2203 }
2204 else if(type.isInterfaceBlock())
2205 {
2206 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2207 }
2208
2209 return type.registerSize();
2210 }
2211
2212 if(type.isArray() && registers >= type.elementRegisterCount())
2213 {
2214 int index = registers / type.elementRegisterCount();
2215 registers -= index * type.elementRegisterCount();
2216 return registerSize(type, registers);
2217 }
2218
2219 if(type.isStruct() || type.isInterfaceBlock())
2220 {
2221 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2222 int elements = 0;
2223
Alexis Hetuda163ed2018-01-03 16:36:14 -05002224 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002225 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002226 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002227
2228 if(fieldType.totalRegisterCount() <= registers)
2229 {
2230 registers -= fieldType.totalRegisterCount();
2231 elements += fieldType.getObjectSize();
2232 }
2233 else // Register within this field
2234 {
2235 return registerSize(fieldType, registers);
2236 }
2237 }
2238 }
2239 else if(type.isMatrix())
2240 {
2241 return registerSize(type, 0);
2242 }
2243
2244 UNREACHABLE(0);
2245 return 0;
2246 }
2247
2248 int OutputASM::getBlockId(TIntermTyped *arg)
2249 {
2250 if(arg)
2251 {
2252 const TType &type = arg->getType();
2253 TInterfaceBlock* block = type.getInterfaceBlock();
2254 if(block && (type.getQualifier() == EvqUniform))
2255 {
2256 // Make sure the uniform block is declared
2257 uniformRegister(arg);
2258
2259 const char* blockName = block->name().c_str();
2260
2261 // Fetch uniform block index from array of blocks
2262 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2263 {
2264 if(blockName == it->name)
2265 {
2266 return it->blockId;
2267 }
2268 }
2269
2270 ASSERT(false);
2271 }
2272 }
2273
2274 return -1;
2275 }
2276
2277 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2278 {
2279 const TType &type = arg->getType();
2280 int blockId = getBlockId(arg);
2281 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2282 if(blockId != -1)
2283 {
2284 argumentInfo.bufferIndex = 0;
2285 for(int i = 0; i < blockId; ++i)
2286 {
2287 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2288 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2289 }
2290
2291 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2292
2293 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2294 BlockDefinitionIndexMap::const_iterator it = itEnd;
2295
2296 argumentInfo.clampedIndex = index;
2297 if(type.isInterfaceBlock())
2298 {
2299 // Offset index to the beginning of the selected instance
2300 int blockRegisters = type.elementRegisterCount();
2301 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2302 argumentInfo.bufferIndex += bufferOffset;
2303 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2304 }
2305
2306 int regIndex = registerIndex(arg);
2307 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2308 {
2309 it = blockDefinition.find(i);
2310 if(it != itEnd)
2311 {
2312 argumentInfo.clampedIndex -= (i - regIndex);
2313 break;
2314 }
2315 }
2316 ASSERT(it != itEnd);
2317
2318 argumentInfo.typedMemberInfo = it->second;
2319
2320 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2321 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2322 }
2323 else
2324 {
2325 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2326 }
2327
2328 return argumentInfo;
2329 }
2330
Nicolas Capens0530b452017-11-15 16:39:47 -05002331 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002332 {
2333 if(argument)
2334 {
2335 TIntermTyped *arg = argument->getAsTyped();
2336 Temporary unpackedUniform(this);
2337
2338 const TType& srcType = arg->getType();
2339 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2340 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2341 {
2342 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2343 const TType &memberType = argumentInfo.typedMemberInfo.type;
2344
2345 if(memberType.getBasicType() == EbtBool)
2346 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002347 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002348
2349 // Convert the packed bool, which is currently an int, to a true bool
2350 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2351 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2352 instruction->dst.index = registerIndex(&unpackedUniform);
2353 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2354 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2355 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2356
2357 shader->append(instruction);
2358
2359 arg = &unpackedUniform;
2360 index = 0;
2361 }
2362 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2363 {
2364 int numCols = memberType.getNominalSize();
2365 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002366
Alexis Hetue97a31e2016-11-14 14:10:47 -05002367 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002368
2369 unsigned int dstIndex = registerIndex(&unpackedUniform);
2370 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2371 int arrayIndex = argumentInfo.clampedIndex / numCols;
2372 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2373
2374 for(int j = 0; j < numRows; ++j)
2375 {
2376 // Transpose the row major matrix
2377 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2378 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2379 instruction->dst.index = dstIndex;
2380 instruction->dst.mask = 1 << j;
2381 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2382 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2383 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2384 instruction->src[0].swizzle = srcSwizzle;
2385
2386 shader->append(instruction);
2387 }
2388
2389 arg = &unpackedUniform;
2390 index = 0;
2391 }
2392 }
2393
2394 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2395 const TType &type = argumentInfo.typedMemberInfo.type;
2396
2397 int size = registerSize(type, argumentInfo.clampedIndex);
2398
2399 parameter.type = registerType(arg);
2400 parameter.bufferIndex = argumentInfo.bufferIndex;
2401
2402 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2403 {
2404 int component = componentCount(type, argumentInfo.clampedIndex);
2405 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2406
2407 for(int i = 0; i < 4; i++)
2408 {
2409 if(size == 1) // Replicate
2410 {
2411 parameter.value[i] = constants[component + 0].getAsFloat();
2412 }
2413 else if(i < size)
2414 {
2415 parameter.value[i] = constants[component + i].getAsFloat();
2416 }
2417 else
2418 {
2419 parameter.value[i] = 0.0f;
2420 }
2421 }
2422 }
2423 else
2424 {
2425 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2426
2427 if(parameter.bufferIndex != -1)
2428 {
2429 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2430 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2431 }
2432 }
2433
2434 if(!IsSampler(arg->getBasicType()))
2435 {
2436 parameter.swizzle = readSwizzle(arg, size);
2437 }
2438 }
2439 }
2440
Nicolas Capens0530b452017-11-15 16:39:47 -05002441 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2442 {
2443 parameter.type = registerType(arg);
2444 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002445 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002446 }
2447
Nicolas Capens0bac2852016-05-07 06:09:58 -04002448 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2449 {
2450 for(int index = 0; index < dst->totalRegisterCount(); index++)
2451 {
2452 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002453 }
2454 }
2455
2456 int swizzleElement(int swizzle, int index)
2457 {
2458 return (swizzle >> (index * 2)) & 0x03;
2459 }
2460
2461 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2462 {
2463 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2464 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2465 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2466 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2467 }
2468
2469 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2470 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002471 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2472 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002473 {
2474 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2475 }
2476
2477 TIntermBinary *binary = dst->getAsBinaryNode();
2478
2479 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2480 {
2481 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2482
Nicolas Capens6986b282017-11-16 10:38:19 -05002483 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002484
2485 insert->src[0].type = insert->dst.type;
2486 insert->src[0].index = insert->dst.index;
2487 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002488 source(insert->src[1], src);
2489 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002490
2491 shader->append(insert);
2492 }
2493 else
2494 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002495 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2496
Nicolas Capens6986b282017-11-16 10:38:19 -05002497 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002498
Nicolas Capens0530b452017-11-15 16:39:47 -05002499 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002500 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2501
2502 shader->append(mov1);
2503
2504 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002505 {
2506 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2507
Nicolas Capens84249fd2017-11-09 11:20:51 -05002508 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002509 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002510 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002511
Nicolas Capens0530b452017-11-15 16:39:47 -05002512 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002513
2514 shader->append(mov);
2515 }
2516 }
2517 }
2518
Nicolas Capensd469de22017-11-16 10:42:20 -05002519 void OutputASM::evaluateRvalue(TIntermTyped *node)
2520 {
2521 TIntermBinary *binary = node->getAsBinaryNode();
2522
2523 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2524 {
2525 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2526
2527 destination(insert->dst, node);
2528
2529 Temporary address(this);
2530 unsigned char mask;
2531 TIntermTyped *root = nullptr;
2532 unsigned int offset = 0;
2533 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2534
2535 source(insert->src[0], root, offset);
2536 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2537
2538 source(insert->src[1], binary->getRight());
2539
2540 shader->append(insert);
2541 }
2542 else
2543 {
2544 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2545
2546 destination(mov1->dst, node, 0);
2547
2548 Temporary address(this);
2549 unsigned char mask;
2550 TIntermTyped *root = nullptr;
2551 unsigned int offset = 0;
2552 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2553
2554 source(mov1->src[0], root, offset);
2555 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2556
2557 shader->append(mov1);
2558
2559 for(int i = 1; i < node->totalRegisterCount(); i++)
2560 {
2561 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2562 mov->src[0].rel = mov1->src[0].rel;
2563 }
2564 }
2565 }
2566
Nicolas Capens6986b282017-11-16 10:38:19 -05002567 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002568 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002569 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002570 TIntermTyped *root = nullptr;
2571 unsigned int offset = 0;
2572 unsigned char mask = 0xF;
2573 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2574
2575 dst.type = registerType(root);
2576 dst.index = registerIndex(root) + offset;
2577 dst.mask = mask;
2578
2579 return swizzle;
2580 }
2581
2582 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2583 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002584 TIntermTyped *result = node;
2585 TIntermBinary *binary = node->getAsBinaryNode();
2586 TIntermSymbol *symbol = node->getAsSymbolNode();
2587
2588 if(binary)
2589 {
2590 TIntermTyped *left = binary->getLeft();
2591 TIntermTyped *right = binary->getRight();
2592
Nicolas Capens0530b452017-11-15 16:39:47 -05002593 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002594
2595 switch(binary->getOp())
2596 {
2597 case EOpIndexDirect:
2598 {
2599 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2600
2601 if(left->isRegister())
2602 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002603 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002604
Nicolas Capens0530b452017-11-15 16:39:47 -05002605 mask = 1;
2606 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002607 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002608 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002609 }
2610
2611 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002612 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002613
2614 return element;
2615 }
2616 else if(left->isArray() || left->isMatrix())
2617 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002618 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002619 return 0xE4;
2620 }
2621 else UNREACHABLE(0);
2622 }
2623 break;
2624 case EOpIndexIndirect:
2625 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002626 right->traverse(this);
2627
Nicolas Capens0bac2852016-05-07 06:09:58 -04002628 if(left->isRegister())
2629 {
2630 // Requires INSERT instruction (handled by calling function)
2631 }
2632 else if(left->isArray() || left->isMatrix())
2633 {
2634 int scale = result->totalRegisterCount();
2635
Nicolas Capens0530b452017-11-15 16:39:47 -05002636 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002637 {
2638 if(left->totalRegisterCount() > 1)
2639 {
2640 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002641 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002642
Nicolas Capens0530b452017-11-15 16:39:47 -05002643 rel.index = relativeRegister.index;
2644 rel.type = relativeRegister.type;
2645 rel.scale = scale;
2646 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002647 }
2648 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002649 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002650 {
2651 if(scale == 1)
2652 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002653 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002654 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002655 mad->src[0].index = rel.index;
2656 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002657 }
2658 else
2659 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002660 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002661 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002662 mul->src[0].index = rel.index;
2663 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002664
2665 Constant newScale(scale);
2666 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2667 }
2668
Nicolas Capens0530b452017-11-15 16:39:47 -05002669 rel.type = sw::Shader::PARAMETER_TEMP;
2670 rel.index = registerIndex(&address);
2671 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002672 }
2673 else // Just add the new index to the address register
2674 {
2675 if(scale == 1)
2676 {
2677 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2678 }
2679 else
2680 {
2681 Constant newScale(scale);
2682 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2683 }
2684 }
2685 }
2686 else UNREACHABLE(0);
2687 }
2688 break;
2689 case EOpIndexDirectStruct:
2690 case EOpIndexDirectInterfaceBlock:
2691 {
2692 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2693 left->getType().getStruct()->fields() :
2694 left->getType().getInterfaceBlock()->fields();
2695 int index = right->getAsConstantUnion()->getIConst(0);
2696 int fieldOffset = 0;
2697
2698 for(int i = 0; i < index; i++)
2699 {
2700 fieldOffset += fields[i]->type()->totalRegisterCount();
2701 }
2702
Nicolas Capens0530b452017-11-15 16:39:47 -05002703 offset += fieldOffset;
2704 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002705
2706 return 0xE4;
2707 }
2708 break;
2709 case EOpVectorSwizzle:
2710 {
2711 ASSERT(left->isRegister());
2712
Nicolas Capens0530b452017-11-15 16:39:47 -05002713 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002714
2715 int swizzle = 0;
2716 int rightMask = 0;
2717
2718 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2719
2720 for(unsigned int i = 0; i < sequence.size(); i++)
2721 {
2722 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2723
2724 int element = swizzleElement(leftSwizzle, index);
2725 rightMask = rightMask | (1 << element);
2726 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2727 }
2728
Nicolas Capens0530b452017-11-15 16:39:47 -05002729 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002730
2731 return swizzle;
2732 }
2733 break;
2734 default:
2735 UNREACHABLE(binary->getOp()); // Not an l-value operator
2736 break;
2737 }
2738 }
2739 else if(symbol)
2740 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002741 root = symbol;
2742 offset = 0;
2743 mask = writeMask(symbol);
2744
2745 return 0xE4;
2746 }
2747 else
2748 {
2749 node->traverse(this);
2750
2751 root = node;
2752 offset = 0;
2753 mask = writeMask(node);
2754
Nicolas Capens0bac2852016-05-07 06:09:58 -04002755 return 0xE4;
2756 }
2757
2758 return 0xE4;
2759 }
2760
2761 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2762 {
2763 if(isSamplerRegister(operand))
2764 {
2765 return sw::Shader::PARAMETER_SAMPLER;
2766 }
2767
2768 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002769 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002770 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002771 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2772 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002773 {
2774 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2775 }
2776 outputQualifier = qualifier;
2777 }
2778
2779 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2780 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002781 // Constant arrays are in the constant register file.
2782 if(operand->isArray() && operand->getArraySize() > 1)
2783 {
2784 return sw::Shader::PARAMETER_CONST;
2785 }
2786 else
2787 {
2788 return sw::Shader::PARAMETER_TEMP;
2789 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002790 }
2791
2792 switch(qualifier)
2793 {
2794 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2795 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2796 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2797 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2798 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2799 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2800 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2801 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2802 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2803 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2804 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2805 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2806 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2807 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2808 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2809 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2810 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2811 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2812 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2813 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2814 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2815 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2816 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2817 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2818 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2819 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002820 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002821 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2822 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2823 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2824 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2825 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2826 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2827 default: UNREACHABLE(qualifier);
2828 }
2829
2830 return sw::Shader::PARAMETER_VOID;
2831 }
2832
Alexis Hetu12b00502016-05-20 13:01:11 -04002833 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2834 {
2835 const TQualifier qualifier = operand->getQualifier();
2836 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2837 }
2838
Nicolas Capens0bac2852016-05-07 06:09:58 -04002839 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2840 {
2841 if(isSamplerRegister(operand))
2842 {
2843 return samplerRegister(operand);
2844 }
2845
2846 switch(operand->getQualifier())
2847 {
2848 case EvqTemporary: return temporaryRegister(operand);
2849 case EvqGlobal: return temporaryRegister(operand);
2850 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2851 case EvqAttribute: return attributeRegister(operand);
2852 case EvqVaryingIn: return varyingRegister(operand);
2853 case EvqVaryingOut: return varyingRegister(operand);
2854 case EvqVertexIn: return attributeRegister(operand);
2855 case EvqFragmentOut: return fragmentOutputRegister(operand);
2856 case EvqVertexOut: return varyingRegister(operand);
2857 case EvqFragmentIn: return varyingRegister(operand);
2858 case EvqInvariantVaryingIn: return varyingRegister(operand);
2859 case EvqInvariantVaryingOut: return varyingRegister(operand);
2860 case EvqSmooth: return varyingRegister(operand);
2861 case EvqFlat: return varyingRegister(operand);
2862 case EvqCentroidOut: return varyingRegister(operand);
2863 case EvqSmoothIn: return varyingRegister(operand);
2864 case EvqFlatIn: return varyingRegister(operand);
2865 case EvqCentroidIn: return varyingRegister(operand);
2866 case EvqUniform: return uniformRegister(operand);
2867 case EvqIn: return temporaryRegister(operand);
2868 case EvqOut: return temporaryRegister(operand);
2869 case EvqInOut: return temporaryRegister(operand);
2870 case EvqConstReadOnly: return temporaryRegister(operand);
2871 case EvqPosition: return varyingRegister(operand);
2872 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002873 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2874 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2875 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2876 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002877 case EvqPointCoord: return varyingRegister(operand);
2878 case EvqFragColor: return 0;
2879 case EvqFragData: return fragmentOutputRegister(operand);
2880 case EvqFragDepth: return 0;
2881 default: UNREACHABLE(operand->getQualifier());
2882 }
2883
2884 return 0;
2885 }
2886
2887 int OutputASM::writeMask(TIntermTyped *destination, int index)
2888 {
2889 if(destination->getQualifier() == EvqPointSize)
2890 {
2891 return 0x2; // Point size stored in the y component
2892 }
2893
2894 return 0xF >> (4 - registerSize(destination->getType(), index));
2895 }
2896
2897 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2898 {
2899 if(argument->getQualifier() == EvqPointSize)
2900 {
2901 return 0x55; // Point size stored in the y component
2902 }
2903
2904 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2905
2906 return swizzleSize[size];
2907 }
2908
2909 // Conservatively checks whether an expression is fast to compute and has no side effects
2910 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2911 {
2912 if(!expression->isRegister())
2913 {
2914 return false;
2915 }
2916
2917 return cost(expression, budget) >= 0;
2918 }
2919
2920 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2921 int OutputASM::cost(TIntermNode *expression, int budget)
2922 {
2923 if(budget < 0)
2924 {
2925 return budget;
2926 }
2927
2928 if(expression->getAsSymbolNode())
2929 {
2930 return budget;
2931 }
2932 else if(expression->getAsConstantUnion())
2933 {
2934 return budget;
2935 }
2936 else if(expression->getAsBinaryNode())
2937 {
2938 TIntermBinary *binary = expression->getAsBinaryNode();
2939
2940 switch(binary->getOp())
2941 {
2942 case EOpVectorSwizzle:
2943 case EOpIndexDirect:
2944 case EOpIndexDirectStruct:
2945 case EOpIndexDirectInterfaceBlock:
2946 return cost(binary->getLeft(), budget - 0);
2947 case EOpAdd:
2948 case EOpSub:
2949 case EOpMul:
2950 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2951 default:
2952 return -1;
2953 }
2954 }
2955 else if(expression->getAsUnaryNode())
2956 {
2957 TIntermUnary *unary = expression->getAsUnaryNode();
2958
2959 switch(unary->getOp())
2960 {
2961 case EOpAbs:
2962 case EOpNegative:
2963 return cost(unary->getOperand(), budget - 1);
2964 default:
2965 return -1;
2966 }
2967 }
2968 else if(expression->getAsSelectionNode())
2969 {
2970 TIntermSelection *selection = expression->getAsSelectionNode();
2971
2972 if(selection->usesTernaryOperator())
2973 {
2974 TIntermTyped *condition = selection->getCondition();
2975 TIntermNode *trueBlock = selection->getTrueBlock();
2976 TIntermNode *falseBlock = selection->getFalseBlock();
2977 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2978
2979 if(constantCondition)
2980 {
2981 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2982
2983 if(trueCondition)
2984 {
2985 return cost(trueBlock, budget - 0);
2986 }
2987 else
2988 {
2989 return cost(falseBlock, budget - 0);
2990 }
2991 }
2992 else
2993 {
2994 return cost(trueBlock, cost(falseBlock, budget - 2));
2995 }
2996 }
2997 }
2998
2999 return -1;
3000 }
3001
3002 const Function *OutputASM::findFunction(const TString &name)
3003 {
3004 for(unsigned int f = 0; f < functionArray.size(); f++)
3005 {
3006 if(functionArray[f].name == name)
3007 {
3008 return &functionArray[f];
3009 }
3010 }
3011
3012 return 0;
3013 }
3014
3015 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3016 {
3017 return allocate(temporaries, temporary);
3018 }
3019
Alexis Hetu49351232017-11-02 16:00:32 -04003020 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3021 {
3022 if(type.isStruct())
3023 {
3024 const TFieldList &fields = type.getStruct()->fields();
3025 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003026 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003027 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003028 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003029 setPixelShaderInputs(fieldType, fieldVar, flat);
3030 fieldVar += fieldType.totalRegisterCount();
3031 }
3032 }
3033 else
3034 {
3035 for(int i = 0; i < type.totalRegisterCount(); i++)
3036 {
3037 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3038 }
3039 }
3040 }
3041
Nicolas Capens0bac2852016-05-07 06:09:58 -04003042 int OutputASM::varyingRegister(TIntermTyped *varying)
3043 {
3044 int var = lookup(varyings, varying);
3045
3046 if(var == -1)
3047 {
3048 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003049 int registerCount = varying->totalRegisterCount();
3050
3051 if(pixelShader)
3052 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003053 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003054 {
3055 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3056 return 0;
3057 }
3058
3059 if(varying->getQualifier() == EvqPointCoord)
3060 {
3061 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003062 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003063 }
3064 else
3065 {
Alexis Hetu49351232017-11-02 16:00:32 -04003066 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003067 }
3068 }
3069 else if(vertexShader)
3070 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003071 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003072 {
3073 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3074 return 0;
3075 }
3076
3077 if(varying->getQualifier() == EvqPosition)
3078 {
3079 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003080 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003081 }
3082 else if(varying->getQualifier() == EvqPointSize)
3083 {
3084 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003085 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003086 }
3087 else
3088 {
3089 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3090 }
3091 }
3092 else UNREACHABLE(0);
3093
3094 declareVarying(varying, var);
3095 }
3096
3097 return var;
3098 }
3099
3100 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3101 {
3102 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3103 {
Alexis Hetu49351232017-11-02 16:00:32 -04003104 TIntermSymbol *symbol = varying->getAsSymbolNode();
3105 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3106 }
3107 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003108
Alexis Hetu49351232017-11-02 16:00:32 -04003109 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3110 {
3111 const char *name = varyingName.c_str();
3112 VaryingList &activeVaryings = shaderObject->varyings;
3113
3114 TStructure* structure = type.getStruct();
3115 if(structure)
3116 {
3117 int fieldRegisterIndex = registerIndex;
3118
3119 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003120 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003121 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003122 const TType& fieldType = *(field->type());
3123 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003124 if(fieldRegisterIndex >= 0)
3125 {
3126 fieldRegisterIndex += fieldType.totalRegisterCount();
3127 }
3128 }
3129 }
3130 else
3131 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003132 // Check if this varying has been declared before without having a register assigned
3133 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3134 {
3135 if(v->name == name)
3136 {
Alexis Hetu49351232017-11-02 16:00:32 -04003137 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003138 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003139 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3140 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003141 }
3142
3143 return;
3144 }
3145 }
3146
Alexis Hetu924513c2018-01-05 15:48:12 -05003147 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003148 }
3149 }
3150
3151 int OutputASM::uniformRegister(TIntermTyped *uniform)
3152 {
3153 const TType &type = uniform->getType();
3154 ASSERT(!IsSampler(type.getBasicType()));
3155 TInterfaceBlock *block = type.getAsInterfaceBlock();
3156 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3157 ASSERT(symbol || block);
3158
3159 if(symbol || block)
3160 {
3161 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3162 bool isBlockMember = (!block && parentBlock);
3163 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3164
3165 if(index == -1 || isBlockMember)
3166 {
3167 if(index == -1)
3168 {
3169 index = allocate(uniforms, uniform);
3170 }
3171
3172 // Verify if the current uniform is a member of an already declared block
3173 const TString &name = symbol ? symbol->getSymbol() : block->name();
3174 int blockMemberIndex = blockMemberLookup(type, name, index);
3175 if(blockMemberIndex == -1)
3176 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003177 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003178 }
3179 else
3180 {
3181 index = blockMemberIndex;
3182 }
3183 }
3184
3185 return index;
3186 }
3187
3188 return 0;
3189 }
3190
3191 int OutputASM::attributeRegister(TIntermTyped *attribute)
3192 {
3193 ASSERT(!attribute->isArray());
3194
3195 int index = lookup(attributes, attribute);
3196
3197 if(index == -1)
3198 {
3199 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3200 ASSERT(symbol);
3201
3202 if(symbol)
3203 {
3204 index = allocate(attributes, attribute);
3205 const TType &type = attribute->getType();
3206 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003207 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3208 switch(type.getBasicType())
3209 {
3210 case EbtInt:
3211 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3212 break;
3213 case EbtUInt:
3214 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3215 break;
3216 case EbtFloat:
3217 default:
3218 break;
3219 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003220
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003221 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003222 {
3223 for(int i = 0; i < registerCount; i++)
3224 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003225 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003226 }
3227 }
3228
3229 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3230
3231 const char *name = symbol->getSymbol().c_str();
3232 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3233 }
3234 }
3235
3236 return index;
3237 }
3238
3239 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3240 {
3241 return allocate(fragmentOutputs, fragmentOutput);
3242 }
3243
3244 int OutputASM::samplerRegister(TIntermTyped *sampler)
3245 {
3246 const TType &type = sampler->getType();
3247 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3248
3249 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3250 TIntermBinary *binary = sampler->getAsBinaryNode();
3251
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003252 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003253 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003254 switch(type.getQualifier())
3255 {
3256 case EvqUniform:
3257 return samplerRegister(symbol);
3258 case EvqIn:
3259 case EvqConstReadOnly:
3260 // Function arguments are not (uniform) sampler registers
3261 return -1;
3262 default:
3263 UNREACHABLE(type.getQualifier());
3264 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003265 }
3266 else if(binary)
3267 {
3268 TIntermTyped *left = binary->getLeft();
3269 TIntermTyped *right = binary->getRight();
3270 const TType &leftType = left->getType();
3271 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3272 int offset = 0;
3273
3274 switch(binary->getOp())
3275 {
3276 case EOpIndexDirect:
3277 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003278 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003279 break;
3280 case EOpIndexDirectStruct:
3281 ASSERT(leftType.isStruct());
3282 {
3283 const TFieldList &fields = leftType.getStruct()->fields();
3284
3285 for(int i = 0; i < index; i++)
3286 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003287 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003288 }
3289 }
3290 break;
3291 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3292 return -1;
3293 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3294 default:
3295 UNREACHABLE(binary->getOp());
3296 return -1;
3297 }
3298
3299 int base = samplerRegister(left);
3300
3301 if(base < 0)
3302 {
3303 return -1;
3304 }
3305
3306 return base + offset;
3307 }
3308
3309 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003310 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003311 }
3312
3313 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3314 {
3315 const TType &type = sampler->getType();
3316 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3317
3318 int index = lookup(samplers, sampler);
3319
3320 if(index == -1)
3321 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003322 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003323
3324 if(sampler->getQualifier() == EvqUniform)
3325 {
3326 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003327 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003328 }
3329 }
3330
3331 return index;
3332 }
3333
3334 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3335 {
3336 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3337 }
3338
3339 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3340 {
3341 for(unsigned int i = 0; i < list.size(); i++)
3342 {
3343 if(list[i] == variable)
3344 {
3345 return i; // Pointer match
3346 }
3347 }
3348
3349 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3350 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3351
3352 if(varBlock)
3353 {
3354 for(unsigned int i = 0; i < list.size(); i++)
3355 {
3356 if(list[i])
3357 {
3358 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3359
3360 if(listBlock)
3361 {
3362 if(listBlock->name() == varBlock->name())
3363 {
3364 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3365 ASSERT(listBlock->fields() == varBlock->fields());
3366 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3367 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3368
3369 return i;
3370 }
3371 }
3372 }
3373 }
3374 }
3375 else if(varSymbol)
3376 {
3377 for(unsigned int i = 0; i < list.size(); i++)
3378 {
3379 if(list[i])
3380 {
3381 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3382
3383 if(listSymbol)
3384 {
3385 if(listSymbol->getId() == varSymbol->getId())
3386 {
3387 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3388 ASSERT(listSymbol->getType() == varSymbol->getType());
3389 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3390
3391 return i;
3392 }
3393 }
3394 }
3395 }
3396 }
3397
3398 return -1;
3399 }
3400
3401 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3402 {
3403 for(unsigned int i = 0; i < list.size(); i++)
3404 {
3405 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3406 {
3407 return i; // Pointer match
3408 }
3409 }
3410 return -1;
3411 }
3412
Alexis Hetuda163ed2018-01-03 16:36:14 -05003413 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003414 {
3415 int index = lookup(list, variable);
3416
3417 if(index == -1)
3418 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003419 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003420
3421 for(unsigned int i = 0; i < list.size(); i++)
3422 {
3423 if(list[i] == 0)
3424 {
3425 unsigned int j = 1;
3426 for( ; j < registerCount && (i + j) < list.size(); j++)
3427 {
3428 if(list[i + j] != 0)
3429 {
3430 break;
3431 }
3432 }
3433
3434 if(j == registerCount) // Found free slots
3435 {
3436 for(unsigned int j = 0; j < registerCount; j++)
3437 {
3438 list[i + j] = variable;
3439 }
3440
3441 return i;
3442 }
3443 }
3444 }
3445
3446 index = list.size();
3447
3448 for(unsigned int i = 0; i < registerCount; i++)
3449 {
3450 list.push_back(variable);
3451 }
3452 }
3453
3454 return index;
3455 }
3456
3457 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3458 {
3459 int index = lookup(list, variable);
3460
3461 if(index >= 0)
3462 {
3463 list[index] = 0;
3464 }
3465 }
3466
3467 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3468 {
3469 const TInterfaceBlock *block = type.getInterfaceBlock();
3470
3471 if(block)
3472 {
3473 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3474 const TFieldList& fields = block->fields();
3475 const TString &blockName = block->name();
3476 int fieldRegisterIndex = registerIndex;
3477
3478 if(!type.isInterfaceBlock())
3479 {
3480 // This is a uniform that's part of a block, let's see if the block is already defined
3481 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3482 {
3483 if(activeUniformBlocks[i].name == blockName.c_str())
3484 {
3485 // The block is already defined, find the register for the current uniform and return it
3486 for(size_t j = 0; j < fields.size(); j++)
3487 {
3488 const TString &fieldName = fields[j]->name();
3489 if(fieldName == name)
3490 {
3491 return fieldRegisterIndex;
3492 }
3493
3494 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3495 }
3496
3497 ASSERT(false);
3498 return fieldRegisterIndex;
3499 }
3500 }
3501 }
3502 }
3503
3504 return -1;
3505 }
3506
Alexis Hetuda163ed2018-01-03 16:36:14 -05003507 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003508 {
3509 const TStructure *structure = type.getStruct();
3510 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3511
3512 if(!structure && !block)
3513 {
3514 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3515 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3516 if(blockId >= 0)
3517 {
3518 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3519 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3520 }
3521 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003522 bool isSampler = IsSampler(type.getBasicType());
3523 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003524 {
3525 for(int i = 0; i < type.totalRegisterCount(); i++)
3526 {
3527 shader->declareSampler(fieldRegisterIndex + i);
3528 }
3529 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003530 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003531 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003532 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003533 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003534 }
3535 else if(block)
3536 {
3537 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3538 const TFieldList& fields = block->fields();
3539 const TString &blockName = block->name();
3540 int fieldRegisterIndex = registerIndex;
3541 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3542
3543 blockId = activeUniformBlocks.size();
3544 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3545 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3546 block->blockStorage(), isRowMajor, registerIndex, blockId));
3547 blockDefinitions.push_back(BlockDefinitionIndexMap());
3548
3549 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3550 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003551 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003552 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003553 const TType &fieldType = *(field->type());
3554 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003555 if(isUniformBlockMember && (fieldName == name))
3556 {
3557 registerIndex = fieldRegisterIndex;
3558 }
3559
3560 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3561
Alexis Hetuda163ed2018-01-03 16:36:14 -05003562 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003563 fieldRegisterIndex += fieldType.totalRegisterCount();
3564 }
3565 currentBlockEncoder.exitAggregateType();
3566 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3567 }
3568 else
3569 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003570 // Store struct for program link time validation
3571 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3572
Nicolas Capens0bac2852016-05-07 06:09:58 -04003573 int fieldRegisterIndex = registerIndex;
3574
3575 const TFieldList& fields = structure->fields();
3576 if(type.isArray() && (structure || type.isInterfaceBlock()))
3577 {
3578 for(int i = 0; i < type.getArraySize(); i++)
3579 {
3580 if(encoder)
3581 {
3582 encoder->enterAggregateType();
3583 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003584 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003585 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003586 const TType &fieldType = *(field->type());
3587 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003588 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3589
Alexis Hetuda163ed2018-01-03 16:36:14 -05003590 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3591 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003592 }
3593 if(encoder)
3594 {
3595 encoder->exitAggregateType();
3596 }
3597 }
3598 }
3599 else
3600 {
3601 if(encoder)
3602 {
3603 encoder->enterAggregateType();
3604 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003605 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003606 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003607 const TType &fieldType = *(field->type());
3608 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003609 const TString uniformName = name + "." + fieldName;
3610
Alexis Hetuda163ed2018-01-03 16:36:14 -05003611 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3612 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003613 }
3614 if(encoder)
3615 {
3616 encoder->exitAggregateType();
3617 }
3618 }
3619 }
3620 }
3621
Nicolas Capens0bac2852016-05-07 06:09:58 -04003622 int OutputASM::dim(TIntermNode *v)
3623 {
3624 TIntermTyped *vector = v->getAsTyped();
3625 ASSERT(vector && vector->isRegister());
3626 return vector->getNominalSize();
3627 }
3628
3629 int OutputASM::dim2(TIntermNode *m)
3630 {
3631 TIntermTyped *matrix = m->getAsTyped();
3632 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3633 return matrix->getSecondarySize();
3634 }
3635
3636 // Returns ~0u if no loop count could be determined
3637 unsigned int OutputASM::loopCount(TIntermLoop *node)
3638 {
3639 // Parse loops of the form:
3640 // for(int index = initial; index [comparator] limit; index += increment)
3641 TIntermSymbol *index = 0;
3642 TOperator comparator = EOpNull;
3643 int initial = 0;
3644 int limit = 0;
3645 int increment = 0;
3646
3647 // Parse index name and intial value
3648 if(node->getInit())
3649 {
3650 TIntermAggregate *init = node->getInit()->getAsAggregate();
3651
3652 if(init)
3653 {
3654 TIntermSequence &sequence = init->getSequence();
3655 TIntermTyped *variable = sequence[0]->getAsTyped();
3656
Nicolas Capense3f05552017-05-24 10:45:56 -04003657 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003658 {
3659 TIntermBinary *assign = variable->getAsBinaryNode();
3660
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003661 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003662 {
3663 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3664 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3665
3666 if(symbol && constant)
3667 {
3668 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3669 {
3670 index = symbol;
3671 initial = constant->getUnionArrayPointer()[0].getIConst();
3672 }
3673 }
3674 }
3675 }
3676 }
3677 }
3678
3679 // Parse comparator and limit value
3680 if(index && node->getCondition())
3681 {
3682 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003683 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003684
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003685 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003686 {
3687 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3688
3689 if(constant)
3690 {
3691 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3692 {
3693 comparator = test->getOp();
3694 limit = constant->getUnionArrayPointer()[0].getIConst();
3695 }
3696 }
3697 }
3698 }
3699
3700 // Parse increment
3701 if(index && comparator != EOpNull && node->getExpression())
3702 {
3703 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3704 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3705
3706 if(binaryTerminal)
3707 {
3708 TOperator op = binaryTerminal->getOp();
3709 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3710
3711 if(constant)
3712 {
3713 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3714 {
3715 int value = constant->getUnionArrayPointer()[0].getIConst();
3716
3717 switch(op)
3718 {
3719 case EOpAddAssign: increment = value; break;
3720 case EOpSubAssign: increment = -value; break;
3721 default: UNIMPLEMENTED();
3722 }
3723 }
3724 }
3725 }
3726 else if(unaryTerminal)
3727 {
3728 TOperator op = unaryTerminal->getOp();
3729
3730 switch(op)
3731 {
3732 case EOpPostIncrement: increment = 1; break;
3733 case EOpPostDecrement: increment = -1; break;
3734 case EOpPreIncrement: increment = 1; break;
3735 case EOpPreDecrement: increment = -1; break;
3736 default: UNIMPLEMENTED();
3737 }
3738 }
3739 }
3740
3741 if(index && comparator != EOpNull && increment != 0)
3742 {
3743 if(comparator == EOpLessThanEqual)
3744 {
3745 comparator = EOpLessThan;
3746 limit += 1;
3747 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003748 else if(comparator == EOpGreaterThanEqual)
3749 {
3750 comparator = EOpLessThan;
3751 limit -= 1;
3752 std::swap(initial, limit);
3753 increment = -increment;
3754 }
3755 else if(comparator == EOpGreaterThan)
3756 {
3757 comparator = EOpLessThan;
3758 std::swap(initial, limit);
3759 increment = -increment;
3760 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003761
3762 if(comparator == EOpLessThan)
3763 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003764 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003765 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003766 return 0;
3767 }
3768
3769 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3770
3771 if(iterations < 0)
3772 {
3773 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003774 }
3775
3776 return iterations;
3777 }
3778 else UNIMPLEMENTED(); // Falls through
3779 }
3780
3781 return ~0u;
3782 }
3783
3784 bool LoopUnrollable::traverse(TIntermNode *node)
3785 {
3786 loopDepth = 0;
3787 loopUnrollable = true;
3788
3789 node->traverse(this);
3790
3791 return loopUnrollable;
3792 }
3793
3794 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3795 {
3796 if(visit == PreVisit)
3797 {
3798 loopDepth++;
3799 }
3800 else if(visit == PostVisit)
3801 {
3802 loopDepth++;
3803 }
3804
3805 return true;
3806 }
3807
3808 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3809 {
3810 if(!loopUnrollable)
3811 {
3812 return false;
3813 }
3814
3815 if(!loopDepth)
3816 {
3817 return true;
3818 }
3819
3820 switch(node->getFlowOp())
3821 {
3822 case EOpKill:
3823 case EOpReturn:
3824 break;
3825 case EOpBreak:
3826 case EOpContinue:
3827 loopUnrollable = false;
3828 break;
3829 default: UNREACHABLE(node->getFlowOp());
3830 }
3831
3832 return loopUnrollable;
3833 }
3834
3835 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3836 {
3837 return loopUnrollable;
3838 }
3839}