blob: fbdc113ed022eb58b65defbcb4d8278627346952 [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;
Alexis Hetu930df972018-01-30 16:54:13 -0500679 case EvqFragmentOut:
680 declareFragmentOutput(symbol);
681 break;
Nicolas Capens6896e352018-01-10 12:46:52 -0500682 default:
683 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400684 }
685
686 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
687 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
688 // "All members of a named uniform block declared with a shared or std140 layout qualifier
689 // are considered active, even if they are not referenced in any shader in the program.
690 // The uniform block itself is also considered active, even if no member of the block is referenced."
691 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
692 {
693 uniformRegister(symbol);
694 }
695 }
696
697 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
698 {
699 if(currentScope != emitScope)
700 {
701 return false;
702 }
703
704 TIntermTyped *result = node;
705 TIntermTyped *left = node->getLeft();
706 TIntermTyped *right = node->getRight();
707 const TType &leftType = left->getType();
708 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400709
710 if(isSamplerRegister(result))
711 {
712 return false; // Don't traverse, the register index is determined statically
713 }
714
715 switch(node->getOp())
716 {
717 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500718 assert(visit == PreVisit);
719 right->traverse(this);
720 assignLvalue(left, right);
721 copy(result, right);
722 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400723 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500724 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500725 // Constant arrays go into the constant register file.
726 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
727 {
728 for(int i = 0; i < left->totalRegisterCount(); i++)
729 {
730 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
731 }
732 }
733 else
734 {
735 right->traverse(this);
736 copy(left, right);
737 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500738 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400739 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500740 assert(visit == PreVisit);
741 right->traverse(this);
742 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400743 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500744 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400745 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500746
747 assignLvalue(left, result);
748 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400749 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500750 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400751 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500752 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400753 int size = leftType.getNominalSize();
754
755 for(int i = 0; i < size; i++)
756 {
757 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
758 dot->dst.mask = 1 << i;
759 }
760
761 assignLvalue(left, result);
762 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500763 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400764 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500765 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400766 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500767 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400768 int dim = leftType.getNominalSize();
769
770 for(int i = 0; i < dim; i++)
771 {
772 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
773 mul->src[1].swizzle = 0x00;
774
775 for(int j = 1; j < dim; j++)
776 {
777 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
778 mad->src[1].swizzle = j * 0x55;
779 }
780 }
781
782 assignLvalue(left, result);
783 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500784 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400785 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400786 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400787 case EOpIndexDirectStruct:
788 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500789 assert(visit == PreVisit);
790 evaluateRvalue(node);
791 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400792 case EOpVectorSwizzle:
793 if(visit == PostVisit)
794 {
795 int swizzle = 0;
796 TIntermAggregate *components = right->getAsAggregate();
797
798 if(components)
799 {
800 TIntermSequence &sequence = components->getSequence();
801 int component = 0;
802
803 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
804 {
805 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
806
807 if(element)
808 {
809 int i = element->getUnionArrayPointer()[0].getIConst();
810 swizzle |= i << (component * 2);
811 component++;
812 }
813 else UNREACHABLE(0);
814 }
815 }
816 else UNREACHABLE(0);
817
818 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
819 mov->src[0].swizzle = swizzle;
820 }
821 break;
822 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
823 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
824 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
825 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
826 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
827 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
828 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
829 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
830 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
831 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
832 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
833 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
834 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
835 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
836 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
837 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
838 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
839 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
840 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
841 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
842 case EOpEqual:
843 if(visit == PostVisit)
844 {
845 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
846
847 for(int index = 1; index < left->totalRegisterCount(); index++)
848 {
849 Temporary equal(this);
850 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
851 emit(sw::Shader::OPCODE_AND, result, result, &equal);
852 }
853 }
854 break;
855 case EOpNotEqual:
856 if(visit == PostVisit)
857 {
858 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
859
860 for(int index = 1; index < left->totalRegisterCount(); index++)
861 {
862 Temporary notEqual(this);
863 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
864 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
865 }
866 }
867 break;
868 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
869 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
870 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
871 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
872 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
873 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
874 case EOpMatrixTimesScalar:
875 if(visit == PostVisit)
876 {
877 if(left->isMatrix())
878 {
879 for(int i = 0; i < leftType.getNominalSize(); i++)
880 {
881 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
882 }
883 }
884 else if(right->isMatrix())
885 {
886 for(int i = 0; i < rightType.getNominalSize(); i++)
887 {
888 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
889 }
890 }
891 else UNREACHABLE(0);
892 }
893 break;
894 case EOpVectorTimesMatrix:
895 if(visit == PostVisit)
896 {
897 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
898
899 int size = rightType.getNominalSize();
900 for(int i = 0; i < size; i++)
901 {
902 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
903 dot->dst.mask = 1 << i;
904 }
905 }
906 break;
907 case EOpMatrixTimesVector:
908 if(visit == PostVisit)
909 {
910 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
911 mul->src[1].swizzle = 0x00;
912
913 int size = rightType.getNominalSize();
914 for(int i = 1; i < size; i++)
915 {
916 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
917 mad->src[1].swizzle = i * 0x55;
918 }
919 }
920 break;
921 case EOpMatrixTimesMatrix:
922 if(visit == PostVisit)
923 {
924 int dim = leftType.getNominalSize();
925
926 int size = rightType.getNominalSize();
927 for(int i = 0; i < size; i++)
928 {
929 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
930 mul->src[1].swizzle = 0x00;
931
932 for(int j = 1; j < dim; j++)
933 {
934 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
935 mad->src[1].swizzle = j * 0x55;
936 }
937 }
938 }
939 break;
940 case EOpLogicalOr:
941 if(trivial(right, 6))
942 {
943 if(visit == PostVisit)
944 {
945 emit(sw::Shader::OPCODE_OR, result, left, right);
946 }
947 }
948 else // Short-circuit evaluation
949 {
950 if(visit == InVisit)
951 {
952 emit(sw::Shader::OPCODE_MOV, result, left);
953 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
954 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
955 }
956 else if(visit == PostVisit)
957 {
958 emit(sw::Shader::OPCODE_MOV, result, right);
959 emit(sw::Shader::OPCODE_ENDIF);
960 }
961 }
962 break;
963 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
964 case EOpLogicalAnd:
965 if(trivial(right, 6))
966 {
967 if(visit == PostVisit)
968 {
969 emit(sw::Shader::OPCODE_AND, result, left, right);
970 }
971 }
972 else // Short-circuit evaluation
973 {
974 if(visit == InVisit)
975 {
976 emit(sw::Shader::OPCODE_MOV, result, left);
977 emit(sw::Shader::OPCODE_IF, 0, result);
978 }
979 else if(visit == PostVisit)
980 {
981 emit(sw::Shader::OPCODE_MOV, result, right);
982 emit(sw::Shader::OPCODE_ENDIF);
983 }
984 }
985 break;
986 default: UNREACHABLE(node->getOp());
987 }
988
989 return true;
990 }
991
992 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
993 {
994 switch(size)
995 {
996 case 1: // Used for cofactor computation only
997 {
998 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
999 bool isMov = (row == col);
1000 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
1001 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
1002 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1003 mov->dst.mask = 1 << outRow;
1004 }
1005 break;
1006 case 2:
1007 {
1008 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1009
1010 bool isCofactor = (col >= 0) && (row >= 0);
1011 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1012 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1013 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1014
1015 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1016 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1017 det->dst.mask = 1 << outRow;
1018 }
1019 break;
1020 case 3:
1021 {
1022 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1023
1024 bool isCofactor = (col >= 0) && (row >= 0);
1025 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1026 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1027 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1028 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1029
1030 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1031 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1032 det->dst.mask = 1 << outRow;
1033 }
1034 break;
1035 case 4:
1036 {
1037 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1038 det->dst.mask = 1 << outRow;
1039 }
1040 break;
1041 default:
1042 UNREACHABLE(size);
1043 break;
1044 }
1045 }
1046
1047 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1048 {
1049 if(currentScope != emitScope)
1050 {
1051 return false;
1052 }
1053
1054 TIntermTyped *result = node;
1055 TIntermTyped *arg = node->getOperand();
1056 TBasicType basicType = arg->getType().getBasicType();
1057
1058 union
1059 {
1060 float f;
1061 int i;
1062 } one_value;
1063
1064 if(basicType == EbtInt || basicType == EbtUInt)
1065 {
1066 one_value.i = 1;
1067 }
1068 else
1069 {
1070 one_value.f = 1.0f;
1071 }
1072
1073 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1074 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1075 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1076
1077 switch(node->getOp())
1078 {
1079 case EOpNegative:
1080 if(visit == PostVisit)
1081 {
1082 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1083 for(int index = 0; index < arg->totalRegisterCount(); index++)
1084 {
1085 emit(negOpcode, result, index, arg, index);
1086 }
1087 }
1088 break;
1089 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1090 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001091 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001092 case EOpPostIncrement:
1093 if(visit == PostVisit)
1094 {
1095 copy(result, arg);
1096
1097 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1098 for(int index = 0; index < arg->totalRegisterCount(); index++)
1099 {
1100 emit(addOpcode, arg, index, arg, index, &one);
1101 }
1102
1103 assignLvalue(arg, arg);
1104 }
1105 break;
1106 case EOpPostDecrement:
1107 if(visit == PostVisit)
1108 {
1109 copy(result, arg);
1110
1111 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1112 for(int index = 0; index < arg->totalRegisterCount(); index++)
1113 {
1114 emit(subOpcode, arg, index, arg, index, &one);
1115 }
1116
1117 assignLvalue(arg, arg);
1118 }
1119 break;
1120 case EOpPreIncrement:
1121 if(visit == PostVisit)
1122 {
1123 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1124 for(int index = 0; index < arg->totalRegisterCount(); index++)
1125 {
1126 emit(addOpcode, result, index, arg, index, &one);
1127 }
1128
1129 assignLvalue(arg, result);
1130 }
1131 break;
1132 case EOpPreDecrement:
1133 if(visit == PostVisit)
1134 {
1135 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1136 for(int index = 0; index < arg->totalRegisterCount(); index++)
1137 {
1138 emit(subOpcode, result, index, arg, index, &one);
1139 }
1140
1141 assignLvalue(arg, result);
1142 }
1143 break;
1144 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1145 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1146 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1147 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1148 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1149 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1150 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1151 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1152 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1153 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1154 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1155 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1156 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1157 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1158 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1159 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1160 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1161 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1162 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1163 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1164 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1165 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1166 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1167 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1168 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1169 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1170 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1171 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1172 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1173 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1174 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1175 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1176 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1177 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1178 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1179 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1180 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1181 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1182 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1183 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1184 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1185 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1186 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1187 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1188 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1189 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1190 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1191 case EOpTranspose:
1192 if(visit == PostVisit)
1193 {
1194 int numCols = arg->getNominalSize();
1195 int numRows = arg->getSecondarySize();
1196 for(int i = 0; i < numCols; ++i)
1197 {
1198 for(int j = 0; j < numRows; ++j)
1199 {
1200 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1201 mov->src[0].swizzle = 0x55 * j;
1202 mov->dst.mask = 1 << i;
1203 }
1204 }
1205 }
1206 break;
1207 case EOpDeterminant:
1208 if(visit == PostVisit)
1209 {
1210 int size = arg->getNominalSize();
1211 ASSERT(size == arg->getSecondarySize());
1212
1213 emitDeterminant(result, arg, size);
1214 }
1215 break;
1216 case EOpInverse:
1217 if(visit == PostVisit)
1218 {
1219 int size = arg->getNominalSize();
1220 ASSERT(size == arg->getSecondarySize());
1221
1222 // Compute transposed matrix of cofactors
1223 for(int i = 0; i < size; ++i)
1224 {
1225 for(int j = 0; j < size; ++j)
1226 {
1227 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1228 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1229 emitDeterminant(result, arg, size - 1, j, i, i, j);
1230 }
1231 }
1232
1233 // Compute 1 / determinant
1234 Temporary invDet(this);
1235 emitDeterminant(&invDet, arg, size);
1236 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1237 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1238 div->src[1].swizzle = 0x00; // xxxx
1239
1240 // Divide transposed matrix of cofactors by determinant
1241 for(int i = 0; i < size; ++i)
1242 {
1243 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1244 }
1245 }
1246 break;
1247 default: UNREACHABLE(node->getOp());
1248 }
1249
1250 return true;
1251 }
1252
1253 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1254 {
1255 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1256 {
1257 return false;
1258 }
1259
1260 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1261
1262 TIntermTyped *result = node;
1263 const TType &resultType = node->getType();
1264 TIntermSequence &arg = node->getSequence();
1265 size_t argumentCount = arg.size();
1266
1267 switch(node->getOp())
1268 {
1269 case EOpSequence: break;
1270 case EOpDeclaration: break;
1271 case EOpInvariantDeclaration: break;
1272 case EOpPrototype: break;
1273 case EOpComma:
1274 if(visit == PostVisit)
1275 {
1276 copy(result, arg[1]);
1277 }
1278 break;
1279 case EOpFunction:
1280 if(visit == PreVisit)
1281 {
1282 const TString &name = node->getName();
1283
1284 if(emitScope == FUNCTION)
1285 {
1286 if(functionArray.size() > 1) // No need for a label when there's only main()
1287 {
1288 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1289 label->dst.type = sw::Shader::PARAMETER_LABEL;
1290
1291 const Function *function = findFunction(name);
1292 ASSERT(function); // Should have been added during global pass
1293 label->dst.index = function->label;
1294 currentFunction = function->label;
1295 }
1296 }
1297 else if(emitScope == GLOBAL)
1298 {
1299 if(name != "main(")
1300 {
1301 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1302 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1303 }
1304 }
1305 else UNREACHABLE(emitScope);
1306
1307 currentScope = FUNCTION;
1308 }
1309 else if(visit == PostVisit)
1310 {
1311 if(emitScope == FUNCTION)
1312 {
1313 if(functionArray.size() > 1) // No need to return when there's only main()
1314 {
1315 emit(sw::Shader::OPCODE_RET);
1316 }
1317 }
1318
1319 currentScope = GLOBAL;
1320 }
1321 break;
1322 case EOpFunctionCall:
1323 if(visit == PostVisit)
1324 {
1325 if(node->isUserDefined())
1326 {
1327 const TString &name = node->getName();
1328 const Function *function = findFunction(name);
1329
1330 if(!function)
1331 {
1332 mContext.error(node->getLine(), "function definition not found", name.c_str());
1333 return false;
1334 }
1335
1336 TIntermSequence &arguments = *function->arg;
1337
1338 for(size_t i = 0; i < argumentCount; i++)
1339 {
1340 TIntermTyped *in = arguments[i]->getAsTyped();
1341
1342 if(in->getQualifier() == EvqIn ||
1343 in->getQualifier() == EvqInOut ||
1344 in->getQualifier() == EvqConstReadOnly)
1345 {
1346 copy(in, arg[i]);
1347 }
1348 }
1349
1350 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1351 call->dst.type = sw::Shader::PARAMETER_LABEL;
1352 call->dst.index = function->label;
1353
1354 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1355 {
1356 copy(result, function->ret);
1357 }
1358
1359 for(size_t i = 0; i < argumentCount; i++)
1360 {
1361 TIntermTyped *argument = arguments[i]->getAsTyped();
1362 TIntermTyped *out = arg[i]->getAsTyped();
1363
1364 if(argument->getQualifier() == EvqOut ||
1365 argument->getQualifier() == EvqInOut)
1366 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001367 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001368 }
1369 }
1370 }
1371 else
1372 {
1373 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001374 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001375 TIntermTyped *t = arg[1]->getAsTyped();
1376
1377 Temporary coord(this);
1378
1379 if(textureFunction.proj)
1380 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001381 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1382 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1383 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001384
Nicolas Capens0484c792016-06-13 22:02:36 -04001385 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1386 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001387
1388 if(IsShadowSampler(s->getBasicType()))
1389 {
1390 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1391 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1392 mov->src[0].swizzle = 0xA4;
1393 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001394 }
1395 else
1396 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001397 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1398
1399 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1400 {
1401 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1402 mov->src[0].swizzle = 0xA4;
1403 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001404 }
1405
1406 switch(textureFunction.method)
1407 {
1408 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001409 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001410 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001411 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001412 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001413 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001414 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001415 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001416 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001417 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1418 }
1419 else UNREACHABLE(argumentCount);
1420 }
1421 else // Offset
1422 {
1423 if(argumentCount == 3)
1424 {
1425 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1426 }
1427 else if(argumentCount == 4) // Bias
1428 {
1429 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001430 }
1431 else UNREACHABLE(argumentCount);
1432 }
1433 break;
1434 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001435 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001436 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001437 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001438 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001439 else if(argumentCount == 4) // Offset
1440 {
1441 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1442 }
1443 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001444 break;
1445 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001446 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001447 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001448 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001449 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001450 else if(argumentCount == 4) // Offset
1451 {
1452 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1453 }
1454 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001455 break;
1456 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001457 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001458 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001459 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001460 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001461 else if(argumentCount == 5) // Offset
1462 {
1463 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1464 }
1465 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001466 break;
1467 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001468 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001469 break;
1470 default:
1471 UNREACHABLE(textureFunction.method);
1472 }
1473 }
1474 }
1475 break;
1476 case EOpParameters:
1477 break;
1478 case EOpConstructFloat:
1479 case EOpConstructVec2:
1480 case EOpConstructVec3:
1481 case EOpConstructVec4:
1482 case EOpConstructBool:
1483 case EOpConstructBVec2:
1484 case EOpConstructBVec3:
1485 case EOpConstructBVec4:
1486 case EOpConstructInt:
1487 case EOpConstructIVec2:
1488 case EOpConstructIVec3:
1489 case EOpConstructIVec4:
1490 case EOpConstructUInt:
1491 case EOpConstructUVec2:
1492 case EOpConstructUVec3:
1493 case EOpConstructUVec4:
1494 if(visit == PostVisit)
1495 {
1496 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001497 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1498 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001499 for(size_t i = 0; i < argumentCount; i++)
1500 {
1501 TIntermTyped *argi = arg[i]->getAsTyped();
1502 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001503 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1504 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001505
1506 if(!argi->isMatrix())
1507 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001508 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1509 mov->dst.mask = (0xF << swizzle) & 0xF;
1510 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001511
1512 component += size;
1513 }
1514 else // Matrix
1515 {
1516 int column = 0;
1517
1518 while(component < resultType.getNominalSize())
1519 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001520 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1521 mov->dst.mask = (0xF << swizzle) & 0xF;
1522 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001523
1524 column++;
1525 component += size;
1526 }
1527 }
1528 }
1529 }
1530 break;
1531 case EOpConstructMat2:
1532 case EOpConstructMat2x3:
1533 case EOpConstructMat2x4:
1534 case EOpConstructMat3x2:
1535 case EOpConstructMat3:
1536 case EOpConstructMat3x4:
1537 case EOpConstructMat4x2:
1538 case EOpConstructMat4x3:
1539 case EOpConstructMat4:
1540 if(visit == PostVisit)
1541 {
1542 TIntermTyped *arg0 = arg[0]->getAsTyped();
1543 const int outCols = result->getNominalSize();
1544 const int outRows = result->getSecondarySize();
1545
1546 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1547 {
1548 for(int i = 0; i < outCols; i++)
1549 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001550 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001551 Instruction *mov = emitCast(result, i, arg0, 0);
1552 mov->dst.mask = 1 << i;
1553 ASSERT(mov->src[0].swizzle == 0x00);
1554 }
1555 }
1556 else if(arg0->isMatrix())
1557 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001558 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001559
Alexis Hetu2a198552016-09-27 20:50:45 -04001560 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001561 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001562 TIntermTyped *argi = arg[n]->getAsTyped();
1563 const int inCols = argi->getNominalSize();
1564 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001565
Alexis Hetu2a198552016-09-27 20:50:45 -04001566 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001567 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001568 if(i >= inCols || outRows > inRows)
1569 {
1570 // Initialize to identity matrix
1571 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));
1572 emitCast(result, i + n * outCols, &col, 0);
1573 }
1574
1575 if(i < inCols)
1576 {
1577 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1578 mov->dst.mask = 0xF >> (4 - inRows);
1579 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001580 }
1581 }
1582 }
1583 else
1584 {
1585 int column = 0;
1586 int row = 0;
1587
1588 for(size_t i = 0; i < argumentCount; i++)
1589 {
1590 TIntermTyped *argi = arg[i]->getAsTyped();
1591 int size = argi->getNominalSize();
1592 int element = 0;
1593
1594 while(element < size)
1595 {
1596 Instruction *mov = emitCast(result, column, argi, 0);
1597 mov->dst.mask = (0xF << row) & 0xF;
1598 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1599
1600 int end = row + size - element;
1601 column = end >= outRows ? column + 1 : column;
1602 element = element + outRows - row;
1603 row = end >= outRows ? 0 : end;
1604 }
1605 }
1606 }
1607 }
1608 break;
1609 case EOpConstructStruct:
1610 if(visit == PostVisit)
1611 {
1612 int offset = 0;
1613 for(size_t i = 0; i < argumentCount; i++)
1614 {
1615 TIntermTyped *argi = arg[i]->getAsTyped();
1616 int size = argi->totalRegisterCount();
1617
1618 for(int index = 0; index < size; index++)
1619 {
1620 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1621 mov->dst.mask = writeMask(result, offset + index);
1622 }
1623
1624 offset += size;
1625 }
1626 }
1627 break;
1628 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1629 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1630 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1631 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1632 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1633 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1634 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1635 case EOpModf:
1636 if(visit == PostVisit)
1637 {
1638 TIntermTyped* arg1 = arg[1]->getAsTyped();
1639 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1640 assignLvalue(arg1, arg1);
1641 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1642 }
1643 break;
1644 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1645 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1646 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1647 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1648 case EOpClamp:
1649 if(visit == PostVisit)
1650 {
1651 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1652 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1653 }
1654 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001655 case EOpMix:
1656 if(visit == PostVisit)
1657 {
1658 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1659 {
1660 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1661 }
1662 else
1663 {
1664 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1665 }
1666 }
1667 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001668 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1669 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1670 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1671 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1672 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1673 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1674 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1675 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1676 case EOpMul:
1677 if(visit == PostVisit)
1678 {
1679 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001680 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1681 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001682
1683 int size = arg0->getNominalSize();
1684 for(int i = 0; i < size; i++)
1685 {
1686 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1687 }
1688 }
1689 break;
1690 case EOpOuterProduct:
1691 if(visit == PostVisit)
1692 {
1693 for(int i = 0; i < dim(arg[1]); i++)
1694 {
1695 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1696 mul->src[1].swizzle = 0x55 * i;
1697 }
1698 }
1699 break;
1700 default: UNREACHABLE(node->getOp());
1701 }
1702
1703 return true;
1704 }
1705
1706 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1707 {
1708 if(currentScope != emitScope)
1709 {
1710 return false;
1711 }
1712
1713 TIntermTyped *condition = node->getCondition();
1714 TIntermNode *trueBlock = node->getTrueBlock();
1715 TIntermNode *falseBlock = node->getFalseBlock();
1716 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1717
1718 condition->traverse(this);
1719
1720 if(node->usesTernaryOperator())
1721 {
1722 if(constantCondition)
1723 {
1724 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1725
1726 if(trueCondition)
1727 {
1728 trueBlock->traverse(this);
1729 copy(node, trueBlock);
1730 }
1731 else
1732 {
1733 falseBlock->traverse(this);
1734 copy(node, falseBlock);
1735 }
1736 }
1737 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1738 {
1739 trueBlock->traverse(this);
1740 falseBlock->traverse(this);
1741 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1742 }
1743 else
1744 {
1745 emit(sw::Shader::OPCODE_IF, 0, condition);
1746
1747 if(trueBlock)
1748 {
1749 trueBlock->traverse(this);
1750 copy(node, trueBlock);
1751 }
1752
1753 if(falseBlock)
1754 {
1755 emit(sw::Shader::OPCODE_ELSE);
1756 falseBlock->traverse(this);
1757 copy(node, falseBlock);
1758 }
1759
1760 emit(sw::Shader::OPCODE_ENDIF);
1761 }
1762 }
1763 else // if/else statement
1764 {
1765 if(constantCondition)
1766 {
1767 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1768
1769 if(trueCondition)
1770 {
1771 if(trueBlock)
1772 {
1773 trueBlock->traverse(this);
1774 }
1775 }
1776 else
1777 {
1778 if(falseBlock)
1779 {
1780 falseBlock->traverse(this);
1781 }
1782 }
1783 }
1784 else
1785 {
1786 emit(sw::Shader::OPCODE_IF, 0, condition);
1787
1788 if(trueBlock)
1789 {
1790 trueBlock->traverse(this);
1791 }
1792
1793 if(falseBlock)
1794 {
1795 emit(sw::Shader::OPCODE_ELSE);
1796 falseBlock->traverse(this);
1797 }
1798
1799 emit(sw::Shader::OPCODE_ENDIF);
1800 }
1801 }
1802
1803 return false;
1804 }
1805
1806 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1807 {
1808 if(currentScope != emitScope)
1809 {
1810 return false;
1811 }
1812
1813 unsigned int iterations = loopCount(node);
1814
1815 if(iterations == 0)
1816 {
1817 return false;
1818 }
1819
1820 bool unroll = (iterations <= 4);
1821
1822 if(unroll)
1823 {
1824 LoopUnrollable loopUnrollable;
1825 unroll = loopUnrollable.traverse(node);
1826 }
1827
1828 TIntermNode *init = node->getInit();
1829 TIntermTyped *condition = node->getCondition();
1830 TIntermTyped *expression = node->getExpression();
1831 TIntermNode *body = node->getBody();
1832 Constant True(true);
1833
1834 if(node->getType() == ELoopDoWhile)
1835 {
1836 Temporary iterate(this);
1837 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1838
1839 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1840
1841 if(body)
1842 {
1843 body->traverse(this);
1844 }
1845
1846 emit(sw::Shader::OPCODE_TEST);
1847
1848 condition->traverse(this);
1849 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1850
1851 emit(sw::Shader::OPCODE_ENDWHILE);
1852 }
1853 else
1854 {
1855 if(init)
1856 {
1857 init->traverse(this);
1858 }
1859
1860 if(unroll)
1861 {
1862 for(unsigned int i = 0; i < iterations; i++)
1863 {
1864 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1865
1866 if(body)
1867 {
1868 body->traverse(this);
1869 }
1870
1871 if(expression)
1872 {
1873 expression->traverse(this);
1874 }
1875 }
1876 }
1877 else
1878 {
1879 if(condition)
1880 {
1881 condition->traverse(this);
1882 }
1883 else
1884 {
1885 condition = &True;
1886 }
1887
1888 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1889
1890 if(body)
1891 {
1892 body->traverse(this);
1893 }
1894
1895 emit(sw::Shader::OPCODE_TEST);
1896
1897 if(expression)
1898 {
1899 expression->traverse(this);
1900 }
1901
1902 if(condition)
1903 {
1904 condition->traverse(this);
1905 }
1906
1907 emit(sw::Shader::OPCODE_ENDWHILE);
1908 }
1909 }
1910
1911 return false;
1912 }
1913
1914 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1915 {
1916 if(currentScope != emitScope)
1917 {
1918 return false;
1919 }
1920
1921 switch(node->getFlowOp())
1922 {
1923 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1924 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1925 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1926 case EOpReturn:
1927 if(visit == PostVisit)
1928 {
1929 TIntermTyped *value = node->getExpression();
1930
1931 if(value)
1932 {
1933 copy(functionArray[currentFunction].ret, value);
1934 }
1935
1936 emit(sw::Shader::OPCODE_LEAVE);
1937 }
1938 break;
1939 default: UNREACHABLE(node->getFlowOp());
1940 }
1941
1942 return true;
1943 }
1944
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001945 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1946 {
1947 if(currentScope != emitScope)
1948 {
1949 return false;
1950 }
1951
1952 TIntermTyped* switchValue = node->getInit();
1953 TIntermAggregate* opList = node->getStatementList();
1954
1955 if(!switchValue || !opList)
1956 {
1957 return false;
1958 }
1959
1960 switchValue->traverse(this);
1961
1962 emit(sw::Shader::OPCODE_SWITCH);
1963
1964 TIntermSequence& sequence = opList->getSequence();
1965 TIntermSequence::iterator it = sequence.begin();
1966 TIntermSequence::iterator defaultIt = sequence.end();
1967 int nbCases = 0;
1968 for(; it != sequence.end(); ++it)
1969 {
1970 TIntermCase* currentCase = (*it)->getAsCaseNode();
1971 if(currentCase)
1972 {
1973 TIntermSequence::iterator caseIt = it;
1974
1975 TIntermTyped* condition = currentCase->getCondition();
1976 if(condition) // non default case
1977 {
1978 if(nbCases != 0)
1979 {
1980 emit(sw::Shader::OPCODE_ELSE);
1981 }
1982
1983 condition->traverse(this);
1984 Temporary result(this);
1985 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1986 emit(sw::Shader::OPCODE_IF, 0, &result);
1987 nbCases++;
1988
Nicolas Capens6d123312018-01-08 12:57:52 -05001989 // Emit the code for this case and all subsequent cases until we hit a break statement.
1990 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001991 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1992 {
1993 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05001994
1995 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
1996 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
1997 // Note that this eliminates useless operations but shouldn't affect correctness.
1998 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001999 {
2000 break;
2001 }
2002 }
2003 }
2004 else
2005 {
2006 defaultIt = it; // The default case might not be the last case, keep it for last
2007 }
2008 }
2009 }
2010
2011 // If there's a default case, traverse it here
2012 if(defaultIt != sequence.end())
2013 {
2014 emit(sw::Shader::OPCODE_ELSE);
2015 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2016 {
2017 (*defaultIt)->traverse(this);
2018 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2019 {
2020 break;
2021 }
2022 }
2023 }
2024
2025 for(int i = 0; i < nbCases; ++i)
2026 {
2027 emit(sw::Shader::OPCODE_ENDIF);
2028 }
2029
2030 emit(sw::Shader::OPCODE_ENDSWITCH);
2031
2032 return false;
2033 }
2034
Nicolas Capens0bac2852016-05-07 06:09:58 -04002035 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2036 {
2037 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2038 }
2039
2040 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2041 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2042 {
2043 Instruction *instruction = new Instruction(op);
2044
2045 if(dst)
2046 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002047 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002048 }
2049
Alexis Hetu929c6b02017-11-07 16:04:25 -05002050 if(src0)
2051 {
2052 TIntermTyped* src = src0->getAsTyped();
2053 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2054 }
2055
Nicolas Capens0530b452017-11-15 16:39:47 -05002056 source(instruction->src[0], src0, index0);
2057 source(instruction->src[1], src1, index1);
2058 source(instruction->src[2], src2, index2);
2059 source(instruction->src[3], src3, index3);
2060 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002061
2062 shader->append(instruction);
2063
2064 return instruction;
2065 }
2066
2067 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2068 {
2069 return emitCast(dst, 0, src, 0);
2070 }
2071
2072 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2073 {
2074 switch(src->getBasicType())
2075 {
2076 case EbtBool:
2077 switch(dst->getBasicType())
2078 {
2079 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2080 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2081 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2082 default: break;
2083 }
2084 break;
2085 case EbtInt:
2086 switch(dst->getBasicType())
2087 {
2088 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2089 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2090 default: break;
2091 }
2092 break;
2093 case EbtUInt:
2094 switch(dst->getBasicType())
2095 {
2096 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2097 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2098 default: break;
2099 }
2100 break;
2101 case EbtFloat:
2102 switch(dst->getBasicType())
2103 {
2104 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2105 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2106 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2107 default: break;
2108 }
2109 break;
2110 default:
2111 break;
2112 }
2113
2114 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2115 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2116 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2117
2118 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2119 }
2120
2121 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2122 {
2123 for(int index = 0; index < dst->elementRegisterCount(); index++)
2124 {
2125 emit(op, dst, index, src0, index, src1, index, src2, index);
2126 }
2127 }
2128
2129 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2130 {
2131 emitBinary(op, result, src0, src1);
2132 assignLvalue(lhs, result);
2133 }
2134
2135 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2136 {
2137 sw::Shader::Opcode opcode;
2138 switch(left->getAsTyped()->getBasicType())
2139 {
2140 case EbtBool:
2141 case EbtInt:
2142 opcode = sw::Shader::OPCODE_ICMP;
2143 break;
2144 case EbtUInt:
2145 opcode = sw::Shader::OPCODE_UCMP;
2146 break;
2147 default:
2148 opcode = sw::Shader::OPCODE_CMP;
2149 break;
2150 }
2151
2152 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2153 cmp->control = cmpOp;
2154 }
2155
2156 int componentCount(const TType &type, int registers)
2157 {
2158 if(registers == 0)
2159 {
2160 return 0;
2161 }
2162
2163 if(type.isArray() && registers >= type.elementRegisterCount())
2164 {
2165 int index = registers / type.elementRegisterCount();
2166 registers -= index * type.elementRegisterCount();
2167 return index * type.getElementSize() + componentCount(type, registers);
2168 }
2169
2170 if(type.isStruct() || type.isInterfaceBlock())
2171 {
2172 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2173 int elements = 0;
2174
Alexis Hetuda163ed2018-01-03 16:36:14 -05002175 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002176 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002177 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002178
2179 if(fieldType.totalRegisterCount() <= registers)
2180 {
2181 registers -= fieldType.totalRegisterCount();
2182 elements += fieldType.getObjectSize();
2183 }
2184 else // Register within this field
2185 {
2186 return elements + componentCount(fieldType, registers);
2187 }
2188 }
2189 }
2190 else if(type.isMatrix())
2191 {
2192 return registers * type.registerSize();
2193 }
2194
2195 UNREACHABLE(0);
2196 return 0;
2197 }
2198
2199 int registerSize(const TType &type, int registers)
2200 {
2201 if(registers == 0)
2202 {
2203 if(type.isStruct())
2204 {
2205 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2206 }
2207 else if(type.isInterfaceBlock())
2208 {
2209 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2210 }
2211
2212 return type.registerSize();
2213 }
2214
2215 if(type.isArray() && registers >= type.elementRegisterCount())
2216 {
2217 int index = registers / type.elementRegisterCount();
2218 registers -= index * type.elementRegisterCount();
2219 return registerSize(type, registers);
2220 }
2221
2222 if(type.isStruct() || type.isInterfaceBlock())
2223 {
2224 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2225 int elements = 0;
2226
Alexis Hetuda163ed2018-01-03 16:36:14 -05002227 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002228 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002229 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002230
2231 if(fieldType.totalRegisterCount() <= registers)
2232 {
2233 registers -= fieldType.totalRegisterCount();
2234 elements += fieldType.getObjectSize();
2235 }
2236 else // Register within this field
2237 {
2238 return registerSize(fieldType, registers);
2239 }
2240 }
2241 }
2242 else if(type.isMatrix())
2243 {
2244 return registerSize(type, 0);
2245 }
2246
2247 UNREACHABLE(0);
2248 return 0;
2249 }
2250
2251 int OutputASM::getBlockId(TIntermTyped *arg)
2252 {
2253 if(arg)
2254 {
2255 const TType &type = arg->getType();
2256 TInterfaceBlock* block = type.getInterfaceBlock();
2257 if(block && (type.getQualifier() == EvqUniform))
2258 {
2259 // Make sure the uniform block is declared
2260 uniformRegister(arg);
2261
2262 const char* blockName = block->name().c_str();
2263
2264 // Fetch uniform block index from array of blocks
2265 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2266 {
2267 if(blockName == it->name)
2268 {
2269 return it->blockId;
2270 }
2271 }
2272
2273 ASSERT(false);
2274 }
2275 }
2276
2277 return -1;
2278 }
2279
2280 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2281 {
2282 const TType &type = arg->getType();
2283 int blockId = getBlockId(arg);
2284 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2285 if(blockId != -1)
2286 {
2287 argumentInfo.bufferIndex = 0;
2288 for(int i = 0; i < blockId; ++i)
2289 {
2290 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2291 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2292 }
2293
2294 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2295
2296 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2297 BlockDefinitionIndexMap::const_iterator it = itEnd;
2298
2299 argumentInfo.clampedIndex = index;
2300 if(type.isInterfaceBlock())
2301 {
2302 // Offset index to the beginning of the selected instance
2303 int blockRegisters = type.elementRegisterCount();
2304 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2305 argumentInfo.bufferIndex += bufferOffset;
2306 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2307 }
2308
2309 int regIndex = registerIndex(arg);
2310 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2311 {
2312 it = blockDefinition.find(i);
2313 if(it != itEnd)
2314 {
2315 argumentInfo.clampedIndex -= (i - regIndex);
2316 break;
2317 }
2318 }
2319 ASSERT(it != itEnd);
2320
2321 argumentInfo.typedMemberInfo = it->second;
2322
2323 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2324 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2325 }
2326 else
2327 {
2328 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2329 }
2330
2331 return argumentInfo;
2332 }
2333
Nicolas Capens0530b452017-11-15 16:39:47 -05002334 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002335 {
2336 if(argument)
2337 {
2338 TIntermTyped *arg = argument->getAsTyped();
2339 Temporary unpackedUniform(this);
2340
2341 const TType& srcType = arg->getType();
2342 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2343 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2344 {
2345 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2346 const TType &memberType = argumentInfo.typedMemberInfo.type;
2347
2348 if(memberType.getBasicType() == EbtBool)
2349 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002350 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002351
2352 // Convert the packed bool, which is currently an int, to a true bool
2353 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2354 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2355 instruction->dst.index = registerIndex(&unpackedUniform);
2356 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2357 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2358 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2359
2360 shader->append(instruction);
2361
2362 arg = &unpackedUniform;
2363 index = 0;
2364 }
2365 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2366 {
2367 int numCols = memberType.getNominalSize();
2368 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002369
Alexis Hetue97a31e2016-11-14 14:10:47 -05002370 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002371
2372 unsigned int dstIndex = registerIndex(&unpackedUniform);
2373 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2374 int arrayIndex = argumentInfo.clampedIndex / numCols;
2375 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2376
2377 for(int j = 0; j < numRows; ++j)
2378 {
2379 // Transpose the row major matrix
2380 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2381 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2382 instruction->dst.index = dstIndex;
2383 instruction->dst.mask = 1 << j;
2384 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2385 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2386 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2387 instruction->src[0].swizzle = srcSwizzle;
2388
2389 shader->append(instruction);
2390 }
2391
2392 arg = &unpackedUniform;
2393 index = 0;
2394 }
2395 }
2396
2397 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2398 const TType &type = argumentInfo.typedMemberInfo.type;
2399
2400 int size = registerSize(type, argumentInfo.clampedIndex);
2401
2402 parameter.type = registerType(arg);
2403 parameter.bufferIndex = argumentInfo.bufferIndex;
2404
2405 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2406 {
2407 int component = componentCount(type, argumentInfo.clampedIndex);
2408 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2409
2410 for(int i = 0; i < 4; i++)
2411 {
2412 if(size == 1) // Replicate
2413 {
2414 parameter.value[i] = constants[component + 0].getAsFloat();
2415 }
2416 else if(i < size)
2417 {
2418 parameter.value[i] = constants[component + i].getAsFloat();
2419 }
2420 else
2421 {
2422 parameter.value[i] = 0.0f;
2423 }
2424 }
2425 }
2426 else
2427 {
2428 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2429
2430 if(parameter.bufferIndex != -1)
2431 {
2432 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2433 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2434 }
2435 }
2436
2437 if(!IsSampler(arg->getBasicType()))
2438 {
2439 parameter.swizzle = readSwizzle(arg, size);
2440 }
2441 }
2442 }
2443
Nicolas Capens0530b452017-11-15 16:39:47 -05002444 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2445 {
2446 parameter.type = registerType(arg);
2447 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002448 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002449 }
2450
Nicolas Capens0bac2852016-05-07 06:09:58 -04002451 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2452 {
2453 for(int index = 0; index < dst->totalRegisterCount(); index++)
2454 {
2455 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002456 }
2457 }
2458
2459 int swizzleElement(int swizzle, int index)
2460 {
2461 return (swizzle >> (index * 2)) & 0x03;
2462 }
2463
2464 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2465 {
2466 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2467 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2468 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2469 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2470 }
2471
2472 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2473 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002474 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2475 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002476 {
2477 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2478 }
2479
2480 TIntermBinary *binary = dst->getAsBinaryNode();
2481
2482 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2483 {
2484 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2485
Nicolas Capens6986b282017-11-16 10:38:19 -05002486 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002487
2488 insert->src[0].type = insert->dst.type;
2489 insert->src[0].index = insert->dst.index;
2490 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002491 source(insert->src[1], src);
2492 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002493
2494 shader->append(insert);
2495 }
2496 else
2497 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002498 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2499
Nicolas Capens6986b282017-11-16 10:38:19 -05002500 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002501
Nicolas Capens0530b452017-11-15 16:39:47 -05002502 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002503 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2504
2505 shader->append(mov1);
2506
2507 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002508 {
2509 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2510
Nicolas Capens84249fd2017-11-09 11:20:51 -05002511 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002512 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002513 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002514
Nicolas Capens0530b452017-11-15 16:39:47 -05002515 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002516
2517 shader->append(mov);
2518 }
2519 }
2520 }
2521
Nicolas Capensd469de22017-11-16 10:42:20 -05002522 void OutputASM::evaluateRvalue(TIntermTyped *node)
2523 {
2524 TIntermBinary *binary = node->getAsBinaryNode();
2525
2526 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2527 {
2528 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2529
2530 destination(insert->dst, node);
2531
2532 Temporary address(this);
2533 unsigned char mask;
2534 TIntermTyped *root = nullptr;
2535 unsigned int offset = 0;
2536 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2537
2538 source(insert->src[0], root, offset);
2539 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2540
2541 source(insert->src[1], binary->getRight());
2542
2543 shader->append(insert);
2544 }
2545 else
2546 {
2547 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2548
2549 destination(mov1->dst, node, 0);
2550
2551 Temporary address(this);
2552 unsigned char mask;
2553 TIntermTyped *root = nullptr;
2554 unsigned int offset = 0;
2555 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2556
2557 source(mov1->src[0], root, offset);
2558 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2559
2560 shader->append(mov1);
2561
2562 for(int i = 1; i < node->totalRegisterCount(); i++)
2563 {
2564 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2565 mov->src[0].rel = mov1->src[0].rel;
2566 }
2567 }
2568 }
2569
Nicolas Capens6986b282017-11-16 10:38:19 -05002570 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002571 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002572 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002573 TIntermTyped *root = nullptr;
2574 unsigned int offset = 0;
2575 unsigned char mask = 0xF;
2576 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2577
2578 dst.type = registerType(root);
2579 dst.index = registerIndex(root) + offset;
2580 dst.mask = mask;
2581
2582 return swizzle;
2583 }
2584
2585 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2586 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002587 TIntermTyped *result = node;
2588 TIntermBinary *binary = node->getAsBinaryNode();
2589 TIntermSymbol *symbol = node->getAsSymbolNode();
2590
2591 if(binary)
2592 {
2593 TIntermTyped *left = binary->getLeft();
2594 TIntermTyped *right = binary->getRight();
2595
Nicolas Capens0530b452017-11-15 16:39:47 -05002596 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002597
2598 switch(binary->getOp())
2599 {
2600 case EOpIndexDirect:
2601 {
2602 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2603
2604 if(left->isRegister())
2605 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002606 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002607
Nicolas Capens0530b452017-11-15 16:39:47 -05002608 mask = 1;
2609 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002610 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002611 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002612 }
2613
2614 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002615 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002616
2617 return element;
2618 }
2619 else if(left->isArray() || left->isMatrix())
2620 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002621 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002622 return 0xE4;
2623 }
2624 else UNREACHABLE(0);
2625 }
2626 break;
2627 case EOpIndexIndirect:
2628 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002629 right->traverse(this);
2630
Nicolas Capens0bac2852016-05-07 06:09:58 -04002631 if(left->isRegister())
2632 {
2633 // Requires INSERT instruction (handled by calling function)
2634 }
2635 else if(left->isArray() || left->isMatrix())
2636 {
2637 int scale = result->totalRegisterCount();
2638
Nicolas Capens0530b452017-11-15 16:39:47 -05002639 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002640 {
2641 if(left->totalRegisterCount() > 1)
2642 {
2643 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002644 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002645
Nicolas Capens0530b452017-11-15 16:39:47 -05002646 rel.index = relativeRegister.index;
2647 rel.type = relativeRegister.type;
2648 rel.scale = scale;
2649 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002650 }
2651 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002652 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002653 {
2654 if(scale == 1)
2655 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002656 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002657 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002658 mad->src[0].index = rel.index;
2659 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002660 }
2661 else
2662 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002663 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002664 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002665 mul->src[0].index = rel.index;
2666 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002667
2668 Constant newScale(scale);
2669 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2670 }
2671
Nicolas Capens0530b452017-11-15 16:39:47 -05002672 rel.type = sw::Shader::PARAMETER_TEMP;
2673 rel.index = registerIndex(&address);
2674 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002675 }
2676 else // Just add the new index to the address register
2677 {
2678 if(scale == 1)
2679 {
2680 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2681 }
2682 else
2683 {
2684 Constant newScale(scale);
2685 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2686 }
2687 }
2688 }
2689 else UNREACHABLE(0);
2690 }
2691 break;
2692 case EOpIndexDirectStruct:
2693 case EOpIndexDirectInterfaceBlock:
2694 {
2695 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2696 left->getType().getStruct()->fields() :
2697 left->getType().getInterfaceBlock()->fields();
2698 int index = right->getAsConstantUnion()->getIConst(0);
2699 int fieldOffset = 0;
2700
2701 for(int i = 0; i < index; i++)
2702 {
2703 fieldOffset += fields[i]->type()->totalRegisterCount();
2704 }
2705
Nicolas Capens0530b452017-11-15 16:39:47 -05002706 offset += fieldOffset;
2707 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002708
2709 return 0xE4;
2710 }
2711 break;
2712 case EOpVectorSwizzle:
2713 {
2714 ASSERT(left->isRegister());
2715
Nicolas Capens0530b452017-11-15 16:39:47 -05002716 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002717
2718 int swizzle = 0;
2719 int rightMask = 0;
2720
2721 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2722
2723 for(unsigned int i = 0; i < sequence.size(); i++)
2724 {
2725 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2726
2727 int element = swizzleElement(leftSwizzle, index);
2728 rightMask = rightMask | (1 << element);
2729 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2730 }
2731
Nicolas Capens0530b452017-11-15 16:39:47 -05002732 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002733
2734 return swizzle;
2735 }
2736 break;
2737 default:
2738 UNREACHABLE(binary->getOp()); // Not an l-value operator
2739 break;
2740 }
2741 }
2742 else if(symbol)
2743 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002744 root = symbol;
2745 offset = 0;
2746 mask = writeMask(symbol);
2747
2748 return 0xE4;
2749 }
2750 else
2751 {
2752 node->traverse(this);
2753
2754 root = node;
2755 offset = 0;
2756 mask = writeMask(node);
2757
Nicolas Capens0bac2852016-05-07 06:09:58 -04002758 return 0xE4;
2759 }
2760
2761 return 0xE4;
2762 }
2763
2764 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2765 {
2766 if(isSamplerRegister(operand))
2767 {
2768 return sw::Shader::PARAMETER_SAMPLER;
2769 }
2770
2771 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002772 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002773 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002774 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2775 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002776 {
2777 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2778 }
2779 outputQualifier = qualifier;
2780 }
2781
2782 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2783 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002784 // Constant arrays are in the constant register file.
2785 if(operand->isArray() && operand->getArraySize() > 1)
2786 {
2787 return sw::Shader::PARAMETER_CONST;
2788 }
2789 else
2790 {
2791 return sw::Shader::PARAMETER_TEMP;
2792 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002793 }
2794
2795 switch(qualifier)
2796 {
2797 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2798 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2799 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2800 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2801 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2802 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2803 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2804 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2805 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2806 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2807 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2808 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2809 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2810 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2811 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2812 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2813 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2814 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2815 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2816 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2817 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2818 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2819 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2820 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2821 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2822 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002823 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002824 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2825 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2826 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2827 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2828 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2829 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2830 default: UNREACHABLE(qualifier);
2831 }
2832
2833 return sw::Shader::PARAMETER_VOID;
2834 }
2835
Alexis Hetu12b00502016-05-20 13:01:11 -04002836 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2837 {
2838 const TQualifier qualifier = operand->getQualifier();
2839 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2840 }
2841
Nicolas Capens0bac2852016-05-07 06:09:58 -04002842 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2843 {
2844 if(isSamplerRegister(operand))
2845 {
2846 return samplerRegister(operand);
2847 }
2848
2849 switch(operand->getQualifier())
2850 {
2851 case EvqTemporary: return temporaryRegister(operand);
2852 case EvqGlobal: return temporaryRegister(operand);
2853 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2854 case EvqAttribute: return attributeRegister(operand);
2855 case EvqVaryingIn: return varyingRegister(operand);
2856 case EvqVaryingOut: return varyingRegister(operand);
2857 case EvqVertexIn: return attributeRegister(operand);
2858 case EvqFragmentOut: return fragmentOutputRegister(operand);
2859 case EvqVertexOut: return varyingRegister(operand);
2860 case EvqFragmentIn: return varyingRegister(operand);
2861 case EvqInvariantVaryingIn: return varyingRegister(operand);
2862 case EvqInvariantVaryingOut: return varyingRegister(operand);
2863 case EvqSmooth: return varyingRegister(operand);
2864 case EvqFlat: return varyingRegister(operand);
2865 case EvqCentroidOut: return varyingRegister(operand);
2866 case EvqSmoothIn: return varyingRegister(operand);
2867 case EvqFlatIn: return varyingRegister(operand);
2868 case EvqCentroidIn: return varyingRegister(operand);
2869 case EvqUniform: return uniformRegister(operand);
2870 case EvqIn: return temporaryRegister(operand);
2871 case EvqOut: return temporaryRegister(operand);
2872 case EvqInOut: return temporaryRegister(operand);
2873 case EvqConstReadOnly: return temporaryRegister(operand);
2874 case EvqPosition: return varyingRegister(operand);
2875 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002876 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2877 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2878 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2879 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002880 case EvqPointCoord: return varyingRegister(operand);
2881 case EvqFragColor: return 0;
2882 case EvqFragData: return fragmentOutputRegister(operand);
2883 case EvqFragDepth: return 0;
2884 default: UNREACHABLE(operand->getQualifier());
2885 }
2886
2887 return 0;
2888 }
2889
2890 int OutputASM::writeMask(TIntermTyped *destination, int index)
2891 {
2892 if(destination->getQualifier() == EvqPointSize)
2893 {
2894 return 0x2; // Point size stored in the y component
2895 }
2896
2897 return 0xF >> (4 - registerSize(destination->getType(), index));
2898 }
2899
2900 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2901 {
2902 if(argument->getQualifier() == EvqPointSize)
2903 {
2904 return 0x55; // Point size stored in the y component
2905 }
2906
2907 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2908
2909 return swizzleSize[size];
2910 }
2911
2912 // Conservatively checks whether an expression is fast to compute and has no side effects
2913 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2914 {
2915 if(!expression->isRegister())
2916 {
2917 return false;
2918 }
2919
2920 return cost(expression, budget) >= 0;
2921 }
2922
2923 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2924 int OutputASM::cost(TIntermNode *expression, int budget)
2925 {
2926 if(budget < 0)
2927 {
2928 return budget;
2929 }
2930
2931 if(expression->getAsSymbolNode())
2932 {
2933 return budget;
2934 }
2935 else if(expression->getAsConstantUnion())
2936 {
2937 return budget;
2938 }
2939 else if(expression->getAsBinaryNode())
2940 {
2941 TIntermBinary *binary = expression->getAsBinaryNode();
2942
2943 switch(binary->getOp())
2944 {
2945 case EOpVectorSwizzle:
2946 case EOpIndexDirect:
2947 case EOpIndexDirectStruct:
2948 case EOpIndexDirectInterfaceBlock:
2949 return cost(binary->getLeft(), budget - 0);
2950 case EOpAdd:
2951 case EOpSub:
2952 case EOpMul:
2953 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2954 default:
2955 return -1;
2956 }
2957 }
2958 else if(expression->getAsUnaryNode())
2959 {
2960 TIntermUnary *unary = expression->getAsUnaryNode();
2961
2962 switch(unary->getOp())
2963 {
2964 case EOpAbs:
2965 case EOpNegative:
2966 return cost(unary->getOperand(), budget - 1);
2967 default:
2968 return -1;
2969 }
2970 }
2971 else if(expression->getAsSelectionNode())
2972 {
2973 TIntermSelection *selection = expression->getAsSelectionNode();
2974
2975 if(selection->usesTernaryOperator())
2976 {
2977 TIntermTyped *condition = selection->getCondition();
2978 TIntermNode *trueBlock = selection->getTrueBlock();
2979 TIntermNode *falseBlock = selection->getFalseBlock();
2980 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2981
2982 if(constantCondition)
2983 {
2984 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2985
2986 if(trueCondition)
2987 {
2988 return cost(trueBlock, budget - 0);
2989 }
2990 else
2991 {
2992 return cost(falseBlock, budget - 0);
2993 }
2994 }
2995 else
2996 {
2997 return cost(trueBlock, cost(falseBlock, budget - 2));
2998 }
2999 }
3000 }
3001
3002 return -1;
3003 }
3004
3005 const Function *OutputASM::findFunction(const TString &name)
3006 {
3007 for(unsigned int f = 0; f < functionArray.size(); f++)
3008 {
3009 if(functionArray[f].name == name)
3010 {
3011 return &functionArray[f];
3012 }
3013 }
3014
3015 return 0;
3016 }
3017
3018 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3019 {
3020 return allocate(temporaries, temporary);
3021 }
3022
Alexis Hetu49351232017-11-02 16:00:32 -04003023 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3024 {
3025 if(type.isStruct())
3026 {
3027 const TFieldList &fields = type.getStruct()->fields();
3028 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003029 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003030 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003031 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003032 setPixelShaderInputs(fieldType, fieldVar, flat);
3033 fieldVar += fieldType.totalRegisterCount();
3034 }
3035 }
3036 else
3037 {
3038 for(int i = 0; i < type.totalRegisterCount(); i++)
3039 {
3040 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3041 }
3042 }
3043 }
3044
Nicolas Capens0bac2852016-05-07 06:09:58 -04003045 int OutputASM::varyingRegister(TIntermTyped *varying)
3046 {
3047 int var = lookup(varyings, varying);
3048
3049 if(var == -1)
3050 {
3051 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003052 int registerCount = varying->totalRegisterCount();
3053
3054 if(pixelShader)
3055 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003056 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003057 {
3058 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3059 return 0;
3060 }
3061
3062 if(varying->getQualifier() == EvqPointCoord)
3063 {
3064 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003065 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003066 }
3067 else
3068 {
Alexis Hetu49351232017-11-02 16:00:32 -04003069 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003070 }
3071 }
3072 else if(vertexShader)
3073 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003074 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003075 {
3076 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3077 return 0;
3078 }
3079
3080 if(varying->getQualifier() == EvqPosition)
3081 {
3082 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003083 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003084 }
3085 else if(varying->getQualifier() == EvqPointSize)
3086 {
3087 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003088 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003089 }
3090 else
3091 {
3092 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3093 }
3094 }
3095 else UNREACHABLE(0);
3096
3097 declareVarying(varying, var);
3098 }
3099
3100 return var;
3101 }
3102
3103 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3104 {
3105 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3106 {
Alexis Hetu49351232017-11-02 16:00:32 -04003107 TIntermSymbol *symbol = varying->getAsSymbolNode();
3108 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3109 }
3110 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003111
Alexis Hetu49351232017-11-02 16:00:32 -04003112 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3113 {
3114 const char *name = varyingName.c_str();
3115 VaryingList &activeVaryings = shaderObject->varyings;
3116
3117 TStructure* structure = type.getStruct();
3118 if(structure)
3119 {
3120 int fieldRegisterIndex = registerIndex;
3121
3122 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003123 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003124 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003125 const TType& fieldType = *(field->type());
3126 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003127 if(fieldRegisterIndex >= 0)
3128 {
3129 fieldRegisterIndex += fieldType.totalRegisterCount();
3130 }
3131 }
3132 }
3133 else
3134 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003135 // Check if this varying has been declared before without having a register assigned
3136 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3137 {
3138 if(v->name == name)
3139 {
Alexis Hetu49351232017-11-02 16:00:32 -04003140 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003141 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003142 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3143 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003144 }
3145
3146 return;
3147 }
3148 }
3149
Alexis Hetu924513c2018-01-05 15:48:12 -05003150 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003151 }
3152 }
3153
Alexis Hetu930df972018-01-30 16:54:13 -05003154 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3155 {
3156 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
3157 if((requestedLocation >= 0) && (requestedLocation < sw::RENDERTARGETS))
3158 {
3159 if(fragmentOutputs.size() <= requestedLocation)
3160 {
3161 while(fragmentOutputs.size() < requestedLocation)
3162 {
3163 fragmentOutputs.push_back(nullptr);
3164 }
3165 fragmentOutputs.push_back(fragmentOutput);
3166 }
3167 else if(!fragmentOutputs[requestedLocation])
3168 {
3169 fragmentOutputs[requestedLocation] = fragmentOutput;
3170 }
3171 else
3172 {
3173 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3174 }
3175 }
3176 else if(requestedLocation >= sw::RENDERTARGETS)
3177 {
3178 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3179 }
3180 }
3181
Nicolas Capens0bac2852016-05-07 06:09:58 -04003182 int OutputASM::uniformRegister(TIntermTyped *uniform)
3183 {
3184 const TType &type = uniform->getType();
3185 ASSERT(!IsSampler(type.getBasicType()));
3186 TInterfaceBlock *block = type.getAsInterfaceBlock();
3187 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3188 ASSERT(symbol || block);
3189
3190 if(symbol || block)
3191 {
3192 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3193 bool isBlockMember = (!block && parentBlock);
3194 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3195
3196 if(index == -1 || isBlockMember)
3197 {
3198 if(index == -1)
3199 {
3200 index = allocate(uniforms, uniform);
3201 }
3202
3203 // Verify if the current uniform is a member of an already declared block
3204 const TString &name = symbol ? symbol->getSymbol() : block->name();
3205 int blockMemberIndex = blockMemberLookup(type, name, index);
3206 if(blockMemberIndex == -1)
3207 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003208 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003209 }
3210 else
3211 {
3212 index = blockMemberIndex;
3213 }
3214 }
3215
3216 return index;
3217 }
3218
3219 return 0;
3220 }
3221
3222 int OutputASM::attributeRegister(TIntermTyped *attribute)
3223 {
3224 ASSERT(!attribute->isArray());
3225
3226 int index = lookup(attributes, attribute);
3227
3228 if(index == -1)
3229 {
3230 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3231 ASSERT(symbol);
3232
3233 if(symbol)
3234 {
3235 index = allocate(attributes, attribute);
3236 const TType &type = attribute->getType();
3237 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003238 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3239 switch(type.getBasicType())
3240 {
3241 case EbtInt:
3242 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3243 break;
3244 case EbtUInt:
3245 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3246 break;
3247 case EbtFloat:
3248 default:
3249 break;
3250 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003251
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003252 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003253 {
3254 for(int i = 0; i < registerCount; i++)
3255 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003256 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003257 }
3258 }
3259
3260 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3261
3262 const char *name = symbol->getSymbol().c_str();
3263 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3264 }
3265 }
3266
3267 return index;
3268 }
3269
3270 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3271 {
3272 return allocate(fragmentOutputs, fragmentOutput);
3273 }
3274
3275 int OutputASM::samplerRegister(TIntermTyped *sampler)
3276 {
3277 const TType &type = sampler->getType();
3278 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3279
3280 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3281 TIntermBinary *binary = sampler->getAsBinaryNode();
3282
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003283 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003284 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003285 switch(type.getQualifier())
3286 {
3287 case EvqUniform:
3288 return samplerRegister(symbol);
3289 case EvqIn:
3290 case EvqConstReadOnly:
3291 // Function arguments are not (uniform) sampler registers
3292 return -1;
3293 default:
3294 UNREACHABLE(type.getQualifier());
3295 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003296 }
3297 else if(binary)
3298 {
3299 TIntermTyped *left = binary->getLeft();
3300 TIntermTyped *right = binary->getRight();
3301 const TType &leftType = left->getType();
3302 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3303 int offset = 0;
3304
3305 switch(binary->getOp())
3306 {
3307 case EOpIndexDirect:
3308 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003309 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003310 break;
3311 case EOpIndexDirectStruct:
3312 ASSERT(leftType.isStruct());
3313 {
3314 const TFieldList &fields = leftType.getStruct()->fields();
3315
3316 for(int i = 0; i < index; i++)
3317 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003318 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003319 }
3320 }
3321 break;
3322 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3323 return -1;
3324 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3325 default:
3326 UNREACHABLE(binary->getOp());
3327 return -1;
3328 }
3329
3330 int base = samplerRegister(left);
3331
3332 if(base < 0)
3333 {
3334 return -1;
3335 }
3336
3337 return base + offset;
3338 }
3339
3340 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003341 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003342 }
3343
3344 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3345 {
3346 const TType &type = sampler->getType();
3347 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3348
3349 int index = lookup(samplers, sampler);
3350
3351 if(index == -1)
3352 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003353 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003354
3355 if(sampler->getQualifier() == EvqUniform)
3356 {
3357 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003358 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003359 }
3360 }
3361
3362 return index;
3363 }
3364
3365 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3366 {
3367 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3368 }
3369
3370 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3371 {
3372 for(unsigned int i = 0; i < list.size(); i++)
3373 {
3374 if(list[i] == variable)
3375 {
3376 return i; // Pointer match
3377 }
3378 }
3379
3380 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3381 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3382
3383 if(varBlock)
3384 {
3385 for(unsigned int i = 0; i < list.size(); i++)
3386 {
3387 if(list[i])
3388 {
3389 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3390
3391 if(listBlock)
3392 {
3393 if(listBlock->name() == varBlock->name())
3394 {
3395 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3396 ASSERT(listBlock->fields() == varBlock->fields());
3397 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3398 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3399
3400 return i;
3401 }
3402 }
3403 }
3404 }
3405 }
3406 else if(varSymbol)
3407 {
3408 for(unsigned int i = 0; i < list.size(); i++)
3409 {
3410 if(list[i])
3411 {
3412 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3413
3414 if(listSymbol)
3415 {
3416 if(listSymbol->getId() == varSymbol->getId())
3417 {
3418 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3419 ASSERT(listSymbol->getType() == varSymbol->getType());
3420 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3421
3422 return i;
3423 }
3424 }
3425 }
3426 }
3427 }
3428
3429 return -1;
3430 }
3431
3432 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3433 {
3434 for(unsigned int i = 0; i < list.size(); i++)
3435 {
3436 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3437 {
3438 return i; // Pointer match
3439 }
3440 }
3441 return -1;
3442 }
3443
Alexis Hetuda163ed2018-01-03 16:36:14 -05003444 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003445 {
3446 int index = lookup(list, variable);
3447
3448 if(index == -1)
3449 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003450 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003451
3452 for(unsigned int i = 0; i < list.size(); i++)
3453 {
3454 if(list[i] == 0)
3455 {
3456 unsigned int j = 1;
3457 for( ; j < registerCount && (i + j) < list.size(); j++)
3458 {
3459 if(list[i + j] != 0)
3460 {
3461 break;
3462 }
3463 }
3464
3465 if(j == registerCount) // Found free slots
3466 {
3467 for(unsigned int j = 0; j < registerCount; j++)
3468 {
3469 list[i + j] = variable;
3470 }
3471
3472 return i;
3473 }
3474 }
3475 }
3476
3477 index = list.size();
3478
3479 for(unsigned int i = 0; i < registerCount; i++)
3480 {
3481 list.push_back(variable);
3482 }
3483 }
3484
3485 return index;
3486 }
3487
3488 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3489 {
3490 int index = lookup(list, variable);
3491
3492 if(index >= 0)
3493 {
3494 list[index] = 0;
3495 }
3496 }
3497
3498 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3499 {
3500 const TInterfaceBlock *block = type.getInterfaceBlock();
3501
3502 if(block)
3503 {
3504 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3505 const TFieldList& fields = block->fields();
3506 const TString &blockName = block->name();
3507 int fieldRegisterIndex = registerIndex;
3508
3509 if(!type.isInterfaceBlock())
3510 {
3511 // This is a uniform that's part of a block, let's see if the block is already defined
3512 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3513 {
3514 if(activeUniformBlocks[i].name == blockName.c_str())
3515 {
3516 // The block is already defined, find the register for the current uniform and return it
3517 for(size_t j = 0; j < fields.size(); j++)
3518 {
3519 const TString &fieldName = fields[j]->name();
3520 if(fieldName == name)
3521 {
3522 return fieldRegisterIndex;
3523 }
3524
3525 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3526 }
3527
3528 ASSERT(false);
3529 return fieldRegisterIndex;
3530 }
3531 }
3532 }
3533 }
3534
3535 return -1;
3536 }
3537
Alexis Hetuda163ed2018-01-03 16:36:14 -05003538 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003539 {
3540 const TStructure *structure = type.getStruct();
3541 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3542
3543 if(!structure && !block)
3544 {
3545 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3546 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3547 if(blockId >= 0)
3548 {
3549 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3550 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3551 }
3552 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003553 bool isSampler = IsSampler(type.getBasicType());
3554 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003555 {
3556 for(int i = 0; i < type.totalRegisterCount(); i++)
3557 {
3558 shader->declareSampler(fieldRegisterIndex + i);
3559 }
3560 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003561 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003562 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003563 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003564 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003565 }
3566 else if(block)
3567 {
3568 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3569 const TFieldList& fields = block->fields();
3570 const TString &blockName = block->name();
3571 int fieldRegisterIndex = registerIndex;
3572 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3573
3574 blockId = activeUniformBlocks.size();
3575 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3576 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3577 block->blockStorage(), isRowMajor, registerIndex, blockId));
3578 blockDefinitions.push_back(BlockDefinitionIndexMap());
3579
3580 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3581 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003582 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003583 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003584 const TType &fieldType = *(field->type());
3585 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003586 if(isUniformBlockMember && (fieldName == name))
3587 {
3588 registerIndex = fieldRegisterIndex;
3589 }
3590
3591 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3592
Alexis Hetuda163ed2018-01-03 16:36:14 -05003593 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003594 fieldRegisterIndex += fieldType.totalRegisterCount();
3595 }
3596 currentBlockEncoder.exitAggregateType();
3597 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3598 }
3599 else
3600 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003601 // Store struct for program link time validation
3602 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3603
Nicolas Capens0bac2852016-05-07 06:09:58 -04003604 int fieldRegisterIndex = registerIndex;
3605
3606 const TFieldList& fields = structure->fields();
3607 if(type.isArray() && (structure || type.isInterfaceBlock()))
3608 {
3609 for(int i = 0; i < type.getArraySize(); i++)
3610 {
3611 if(encoder)
3612 {
3613 encoder->enterAggregateType();
3614 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003615 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003616 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003617 const TType &fieldType = *(field->type());
3618 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003619 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3620
Alexis Hetuda163ed2018-01-03 16:36:14 -05003621 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3622 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003623 }
3624 if(encoder)
3625 {
3626 encoder->exitAggregateType();
3627 }
3628 }
3629 }
3630 else
3631 {
3632 if(encoder)
3633 {
3634 encoder->enterAggregateType();
3635 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003636 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003637 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003638 const TType &fieldType = *(field->type());
3639 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003640 const TString uniformName = name + "." + fieldName;
3641
Alexis Hetuda163ed2018-01-03 16:36:14 -05003642 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3643 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003644 }
3645 if(encoder)
3646 {
3647 encoder->exitAggregateType();
3648 }
3649 }
3650 }
3651 }
3652
Nicolas Capens0bac2852016-05-07 06:09:58 -04003653 int OutputASM::dim(TIntermNode *v)
3654 {
3655 TIntermTyped *vector = v->getAsTyped();
3656 ASSERT(vector && vector->isRegister());
3657 return vector->getNominalSize();
3658 }
3659
3660 int OutputASM::dim2(TIntermNode *m)
3661 {
3662 TIntermTyped *matrix = m->getAsTyped();
3663 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3664 return matrix->getSecondarySize();
3665 }
3666
3667 // Returns ~0u if no loop count could be determined
3668 unsigned int OutputASM::loopCount(TIntermLoop *node)
3669 {
3670 // Parse loops of the form:
3671 // for(int index = initial; index [comparator] limit; index += increment)
3672 TIntermSymbol *index = 0;
3673 TOperator comparator = EOpNull;
3674 int initial = 0;
3675 int limit = 0;
3676 int increment = 0;
3677
3678 // Parse index name and intial value
3679 if(node->getInit())
3680 {
3681 TIntermAggregate *init = node->getInit()->getAsAggregate();
3682
3683 if(init)
3684 {
3685 TIntermSequence &sequence = init->getSequence();
3686 TIntermTyped *variable = sequence[0]->getAsTyped();
3687
Nicolas Capense3f05552017-05-24 10:45:56 -04003688 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003689 {
3690 TIntermBinary *assign = variable->getAsBinaryNode();
3691
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003692 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003693 {
3694 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3695 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3696
3697 if(symbol && constant)
3698 {
3699 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3700 {
3701 index = symbol;
3702 initial = constant->getUnionArrayPointer()[0].getIConst();
3703 }
3704 }
3705 }
3706 }
3707 }
3708 }
3709
3710 // Parse comparator and limit value
3711 if(index && node->getCondition())
3712 {
3713 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003714 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003715
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003716 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003717 {
3718 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3719
3720 if(constant)
3721 {
3722 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3723 {
3724 comparator = test->getOp();
3725 limit = constant->getUnionArrayPointer()[0].getIConst();
3726 }
3727 }
3728 }
3729 }
3730
3731 // Parse increment
3732 if(index && comparator != EOpNull && node->getExpression())
3733 {
3734 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3735 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3736
3737 if(binaryTerminal)
3738 {
3739 TOperator op = binaryTerminal->getOp();
3740 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3741
3742 if(constant)
3743 {
3744 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3745 {
3746 int value = constant->getUnionArrayPointer()[0].getIConst();
3747
3748 switch(op)
3749 {
3750 case EOpAddAssign: increment = value; break;
3751 case EOpSubAssign: increment = -value; break;
3752 default: UNIMPLEMENTED();
3753 }
3754 }
3755 }
3756 }
3757 else if(unaryTerminal)
3758 {
3759 TOperator op = unaryTerminal->getOp();
3760
3761 switch(op)
3762 {
3763 case EOpPostIncrement: increment = 1; break;
3764 case EOpPostDecrement: increment = -1; break;
3765 case EOpPreIncrement: increment = 1; break;
3766 case EOpPreDecrement: increment = -1; break;
3767 default: UNIMPLEMENTED();
3768 }
3769 }
3770 }
3771
3772 if(index && comparator != EOpNull && increment != 0)
3773 {
3774 if(comparator == EOpLessThanEqual)
3775 {
3776 comparator = EOpLessThan;
3777 limit += 1;
3778 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003779 else if(comparator == EOpGreaterThanEqual)
3780 {
3781 comparator = EOpLessThan;
3782 limit -= 1;
3783 std::swap(initial, limit);
3784 increment = -increment;
3785 }
3786 else if(comparator == EOpGreaterThan)
3787 {
3788 comparator = EOpLessThan;
3789 std::swap(initial, limit);
3790 increment = -increment;
3791 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003792
3793 if(comparator == EOpLessThan)
3794 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003795 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003796 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003797 return 0;
3798 }
3799
3800 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3801
3802 if(iterations < 0)
3803 {
3804 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003805 }
3806
3807 return iterations;
3808 }
3809 else UNIMPLEMENTED(); // Falls through
3810 }
3811
3812 return ~0u;
3813 }
3814
3815 bool LoopUnrollable::traverse(TIntermNode *node)
3816 {
3817 loopDepth = 0;
3818 loopUnrollable = true;
3819
3820 node->traverse(this);
3821
3822 return loopUnrollable;
3823 }
3824
3825 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3826 {
3827 if(visit == PreVisit)
3828 {
3829 loopDepth++;
3830 }
3831 else if(visit == PostVisit)
3832 {
3833 loopDepth++;
3834 }
3835
3836 return true;
3837 }
3838
3839 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3840 {
3841 if(!loopUnrollable)
3842 {
3843 return false;
3844 }
3845
3846 if(!loopDepth)
3847 {
3848 return true;
3849 }
3850
3851 switch(node->getFlowOp())
3852 {
3853 case EOpKill:
3854 case EOpReturn:
3855 break;
3856 case EOpBreak:
3857 case EOpContinue:
3858 loopUnrollable = false;
3859 break;
3860 default: UNREACHABLE(node->getFlowOp());
3861 }
3862
3863 return loopUnrollable;
3864 }
3865
3866 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3867 {
3868 return loopUnrollable;
3869 }
3870}