blob: 8edd09906f242cbdd1f7abad234d3150924532f2 [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 {
664 // Vertex varyings don't have to be actively used to successfully link
665 // against pixel shaders that use them. So make sure they're declared.
666 if(symbol->getQualifier() == EvqVaryingOut || symbol->getQualifier() == EvqInvariantVaryingOut || symbol->getQualifier() == EvqVertexOut)
667 {
668 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
669 {
670 declareVarying(symbol, -1);
671 }
672 }
673
674 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
675 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
676 // "All members of a named uniform block declared with a shared or std140 layout qualifier
677 // are considered active, even if they are not referenced in any shader in the program.
678 // The uniform block itself is also considered active, even if no member of the block is referenced."
679 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
680 {
681 uniformRegister(symbol);
682 }
683 }
684
685 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
686 {
687 if(currentScope != emitScope)
688 {
689 return false;
690 }
691
692 TIntermTyped *result = node;
693 TIntermTyped *left = node->getLeft();
694 TIntermTyped *right = node->getRight();
695 const TType &leftType = left->getType();
696 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400697
698 if(isSamplerRegister(result))
699 {
700 return false; // Don't traverse, the register index is determined statically
701 }
702
703 switch(node->getOp())
704 {
705 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500706 assert(visit == PreVisit);
707 right->traverse(this);
708 assignLvalue(left, right);
709 copy(result, right);
710 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400711 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500712 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500713 // Constant arrays go into the constant register file.
714 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
715 {
716 for(int i = 0; i < left->totalRegisterCount(); i++)
717 {
718 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
719 }
720 }
721 else
722 {
723 right->traverse(this);
724 copy(left, right);
725 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500726 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400727 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500728 assert(visit == PreVisit);
729 right->traverse(this);
730 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400731 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500732 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400733 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500734
735 assignLvalue(left, result);
736 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400737 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500738 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400739 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500740 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400741 int size = leftType.getNominalSize();
742
743 for(int i = 0; i < size; i++)
744 {
745 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
746 dot->dst.mask = 1 << i;
747 }
748
749 assignLvalue(left, result);
750 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500751 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400752 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500753 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400754 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500755 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400756 int dim = leftType.getNominalSize();
757
758 for(int i = 0; i < dim; i++)
759 {
760 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
761 mul->src[1].swizzle = 0x00;
762
763 for(int j = 1; j < dim; j++)
764 {
765 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
766 mad->src[1].swizzle = j * 0x55;
767 }
768 }
769
770 assignLvalue(left, result);
771 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500772 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400773 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400774 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400775 case EOpIndexDirectStruct:
776 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500777 assert(visit == PreVisit);
778 evaluateRvalue(node);
779 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400780 case EOpVectorSwizzle:
781 if(visit == PostVisit)
782 {
783 int swizzle = 0;
784 TIntermAggregate *components = right->getAsAggregate();
785
786 if(components)
787 {
788 TIntermSequence &sequence = components->getSequence();
789 int component = 0;
790
791 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
792 {
793 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
794
795 if(element)
796 {
797 int i = element->getUnionArrayPointer()[0].getIConst();
798 swizzle |= i << (component * 2);
799 component++;
800 }
801 else UNREACHABLE(0);
802 }
803 }
804 else UNREACHABLE(0);
805
806 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
807 mov->src[0].swizzle = swizzle;
808 }
809 break;
810 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
811 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
812 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
813 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
814 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
815 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
816 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
817 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
818 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
819 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
820 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
821 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
822 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
823 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
824 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
825 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
826 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
827 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
828 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
829 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
830 case EOpEqual:
831 if(visit == PostVisit)
832 {
833 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
834
835 for(int index = 1; index < left->totalRegisterCount(); index++)
836 {
837 Temporary equal(this);
838 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
839 emit(sw::Shader::OPCODE_AND, result, result, &equal);
840 }
841 }
842 break;
843 case EOpNotEqual:
844 if(visit == PostVisit)
845 {
846 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
847
848 for(int index = 1; index < left->totalRegisterCount(); index++)
849 {
850 Temporary notEqual(this);
851 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
852 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
853 }
854 }
855 break;
856 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
857 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
858 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
859 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
860 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
861 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
862 case EOpMatrixTimesScalar:
863 if(visit == PostVisit)
864 {
865 if(left->isMatrix())
866 {
867 for(int i = 0; i < leftType.getNominalSize(); i++)
868 {
869 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
870 }
871 }
872 else if(right->isMatrix())
873 {
874 for(int i = 0; i < rightType.getNominalSize(); i++)
875 {
876 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
877 }
878 }
879 else UNREACHABLE(0);
880 }
881 break;
882 case EOpVectorTimesMatrix:
883 if(visit == PostVisit)
884 {
885 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
886
887 int size = rightType.getNominalSize();
888 for(int i = 0; i < size; i++)
889 {
890 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
891 dot->dst.mask = 1 << i;
892 }
893 }
894 break;
895 case EOpMatrixTimesVector:
896 if(visit == PostVisit)
897 {
898 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
899 mul->src[1].swizzle = 0x00;
900
901 int size = rightType.getNominalSize();
902 for(int i = 1; i < size; i++)
903 {
904 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
905 mad->src[1].swizzle = i * 0x55;
906 }
907 }
908 break;
909 case EOpMatrixTimesMatrix:
910 if(visit == PostVisit)
911 {
912 int dim = leftType.getNominalSize();
913
914 int size = rightType.getNominalSize();
915 for(int i = 0; i < size; i++)
916 {
917 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
918 mul->src[1].swizzle = 0x00;
919
920 for(int j = 1; j < dim; j++)
921 {
922 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
923 mad->src[1].swizzle = j * 0x55;
924 }
925 }
926 }
927 break;
928 case EOpLogicalOr:
929 if(trivial(right, 6))
930 {
931 if(visit == PostVisit)
932 {
933 emit(sw::Shader::OPCODE_OR, result, left, right);
934 }
935 }
936 else // Short-circuit evaluation
937 {
938 if(visit == InVisit)
939 {
940 emit(sw::Shader::OPCODE_MOV, result, left);
941 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
942 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
943 }
944 else if(visit == PostVisit)
945 {
946 emit(sw::Shader::OPCODE_MOV, result, right);
947 emit(sw::Shader::OPCODE_ENDIF);
948 }
949 }
950 break;
951 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
952 case EOpLogicalAnd:
953 if(trivial(right, 6))
954 {
955 if(visit == PostVisit)
956 {
957 emit(sw::Shader::OPCODE_AND, result, left, right);
958 }
959 }
960 else // Short-circuit evaluation
961 {
962 if(visit == InVisit)
963 {
964 emit(sw::Shader::OPCODE_MOV, result, left);
965 emit(sw::Shader::OPCODE_IF, 0, result);
966 }
967 else if(visit == PostVisit)
968 {
969 emit(sw::Shader::OPCODE_MOV, result, right);
970 emit(sw::Shader::OPCODE_ENDIF);
971 }
972 }
973 break;
974 default: UNREACHABLE(node->getOp());
975 }
976
977 return true;
978 }
979
980 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
981 {
982 switch(size)
983 {
984 case 1: // Used for cofactor computation only
985 {
986 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
987 bool isMov = (row == col);
988 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
989 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
990 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
991 mov->dst.mask = 1 << outRow;
992 }
993 break;
994 case 2:
995 {
996 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
997
998 bool isCofactor = (col >= 0) && (row >= 0);
999 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1000 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1001 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1002
1003 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1004 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1005 det->dst.mask = 1 << outRow;
1006 }
1007 break;
1008 case 3:
1009 {
1010 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1011
1012 bool isCofactor = (col >= 0) && (row >= 0);
1013 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1014 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1015 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1016 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1017
1018 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1019 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1020 det->dst.mask = 1 << outRow;
1021 }
1022 break;
1023 case 4:
1024 {
1025 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1026 det->dst.mask = 1 << outRow;
1027 }
1028 break;
1029 default:
1030 UNREACHABLE(size);
1031 break;
1032 }
1033 }
1034
1035 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1036 {
1037 if(currentScope != emitScope)
1038 {
1039 return false;
1040 }
1041
1042 TIntermTyped *result = node;
1043 TIntermTyped *arg = node->getOperand();
1044 TBasicType basicType = arg->getType().getBasicType();
1045
1046 union
1047 {
1048 float f;
1049 int i;
1050 } one_value;
1051
1052 if(basicType == EbtInt || basicType == EbtUInt)
1053 {
1054 one_value.i = 1;
1055 }
1056 else
1057 {
1058 one_value.f = 1.0f;
1059 }
1060
1061 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1062 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1063 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1064
1065 switch(node->getOp())
1066 {
1067 case EOpNegative:
1068 if(visit == PostVisit)
1069 {
1070 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1071 for(int index = 0; index < arg->totalRegisterCount(); index++)
1072 {
1073 emit(negOpcode, result, index, arg, index);
1074 }
1075 }
1076 break;
1077 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1078 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001079 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001080 case EOpPostIncrement:
1081 if(visit == PostVisit)
1082 {
1083 copy(result, arg);
1084
1085 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1086 for(int index = 0; index < arg->totalRegisterCount(); index++)
1087 {
1088 emit(addOpcode, arg, index, arg, index, &one);
1089 }
1090
1091 assignLvalue(arg, arg);
1092 }
1093 break;
1094 case EOpPostDecrement:
1095 if(visit == PostVisit)
1096 {
1097 copy(result, arg);
1098
1099 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1100 for(int index = 0; index < arg->totalRegisterCount(); index++)
1101 {
1102 emit(subOpcode, arg, index, arg, index, &one);
1103 }
1104
1105 assignLvalue(arg, arg);
1106 }
1107 break;
1108 case EOpPreIncrement:
1109 if(visit == PostVisit)
1110 {
1111 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1112 for(int index = 0; index < arg->totalRegisterCount(); index++)
1113 {
1114 emit(addOpcode, result, index, arg, index, &one);
1115 }
1116
1117 assignLvalue(arg, result);
1118 }
1119 break;
1120 case EOpPreDecrement:
1121 if(visit == PostVisit)
1122 {
1123 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1124 for(int index = 0; index < arg->totalRegisterCount(); index++)
1125 {
1126 emit(subOpcode, result, index, arg, index, &one);
1127 }
1128
1129 assignLvalue(arg, result);
1130 }
1131 break;
1132 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1133 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1134 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1135 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1136 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1137 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1138 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1139 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1140 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1141 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1142 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1143 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1144 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1145 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1146 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1147 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1148 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1149 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1150 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1151 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1152 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1153 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1154 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1155 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1156 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1157 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1158 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1159 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1160 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1161 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1162 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1163 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1164 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1165 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1166 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1167 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1168 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1169 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1170 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1171 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1172 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1173 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1174 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1175 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1176 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1177 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1178 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1179 case EOpTranspose:
1180 if(visit == PostVisit)
1181 {
1182 int numCols = arg->getNominalSize();
1183 int numRows = arg->getSecondarySize();
1184 for(int i = 0; i < numCols; ++i)
1185 {
1186 for(int j = 0; j < numRows; ++j)
1187 {
1188 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1189 mov->src[0].swizzle = 0x55 * j;
1190 mov->dst.mask = 1 << i;
1191 }
1192 }
1193 }
1194 break;
1195 case EOpDeterminant:
1196 if(visit == PostVisit)
1197 {
1198 int size = arg->getNominalSize();
1199 ASSERT(size == arg->getSecondarySize());
1200
1201 emitDeterminant(result, arg, size);
1202 }
1203 break;
1204 case EOpInverse:
1205 if(visit == PostVisit)
1206 {
1207 int size = arg->getNominalSize();
1208 ASSERT(size == arg->getSecondarySize());
1209
1210 // Compute transposed matrix of cofactors
1211 for(int i = 0; i < size; ++i)
1212 {
1213 for(int j = 0; j < size; ++j)
1214 {
1215 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1216 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1217 emitDeterminant(result, arg, size - 1, j, i, i, j);
1218 }
1219 }
1220
1221 // Compute 1 / determinant
1222 Temporary invDet(this);
1223 emitDeterminant(&invDet, arg, size);
1224 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1225 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1226 div->src[1].swizzle = 0x00; // xxxx
1227
1228 // Divide transposed matrix of cofactors by determinant
1229 for(int i = 0; i < size; ++i)
1230 {
1231 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1232 }
1233 }
1234 break;
1235 default: UNREACHABLE(node->getOp());
1236 }
1237
1238 return true;
1239 }
1240
1241 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1242 {
1243 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1244 {
1245 return false;
1246 }
1247
1248 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1249
1250 TIntermTyped *result = node;
1251 const TType &resultType = node->getType();
1252 TIntermSequence &arg = node->getSequence();
1253 size_t argumentCount = arg.size();
1254
1255 switch(node->getOp())
1256 {
1257 case EOpSequence: break;
1258 case EOpDeclaration: break;
1259 case EOpInvariantDeclaration: break;
1260 case EOpPrototype: break;
1261 case EOpComma:
1262 if(visit == PostVisit)
1263 {
1264 copy(result, arg[1]);
1265 }
1266 break;
1267 case EOpFunction:
1268 if(visit == PreVisit)
1269 {
1270 const TString &name = node->getName();
1271
1272 if(emitScope == FUNCTION)
1273 {
1274 if(functionArray.size() > 1) // No need for a label when there's only main()
1275 {
1276 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1277 label->dst.type = sw::Shader::PARAMETER_LABEL;
1278
1279 const Function *function = findFunction(name);
1280 ASSERT(function); // Should have been added during global pass
1281 label->dst.index = function->label;
1282 currentFunction = function->label;
1283 }
1284 }
1285 else if(emitScope == GLOBAL)
1286 {
1287 if(name != "main(")
1288 {
1289 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1290 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1291 }
1292 }
1293 else UNREACHABLE(emitScope);
1294
1295 currentScope = FUNCTION;
1296 }
1297 else if(visit == PostVisit)
1298 {
1299 if(emitScope == FUNCTION)
1300 {
1301 if(functionArray.size() > 1) // No need to return when there's only main()
1302 {
1303 emit(sw::Shader::OPCODE_RET);
1304 }
1305 }
1306
1307 currentScope = GLOBAL;
1308 }
1309 break;
1310 case EOpFunctionCall:
1311 if(visit == PostVisit)
1312 {
1313 if(node->isUserDefined())
1314 {
1315 const TString &name = node->getName();
1316 const Function *function = findFunction(name);
1317
1318 if(!function)
1319 {
1320 mContext.error(node->getLine(), "function definition not found", name.c_str());
1321 return false;
1322 }
1323
1324 TIntermSequence &arguments = *function->arg;
1325
1326 for(size_t i = 0; i < argumentCount; i++)
1327 {
1328 TIntermTyped *in = arguments[i]->getAsTyped();
1329
1330 if(in->getQualifier() == EvqIn ||
1331 in->getQualifier() == EvqInOut ||
1332 in->getQualifier() == EvqConstReadOnly)
1333 {
1334 copy(in, arg[i]);
1335 }
1336 }
1337
1338 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1339 call->dst.type = sw::Shader::PARAMETER_LABEL;
1340 call->dst.index = function->label;
1341
1342 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1343 {
1344 copy(result, function->ret);
1345 }
1346
1347 for(size_t i = 0; i < argumentCount; i++)
1348 {
1349 TIntermTyped *argument = arguments[i]->getAsTyped();
1350 TIntermTyped *out = arg[i]->getAsTyped();
1351
1352 if(argument->getQualifier() == EvqOut ||
1353 argument->getQualifier() == EvqInOut)
1354 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001355 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001356 }
1357 }
1358 }
1359 else
1360 {
1361 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001362 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001363 TIntermTyped *t = arg[1]->getAsTyped();
1364
1365 Temporary coord(this);
1366
1367 if(textureFunction.proj)
1368 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001369 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1370 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1371 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001372
Nicolas Capens0484c792016-06-13 22:02:36 -04001373 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1374 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001375
1376 if(IsShadowSampler(s->getBasicType()))
1377 {
1378 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1379 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1380 mov->src[0].swizzle = 0xA4;
1381 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001382 }
1383 else
1384 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001385 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1386
1387 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1388 {
1389 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1390 mov->src[0].swizzle = 0xA4;
1391 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001392 }
1393
1394 switch(textureFunction.method)
1395 {
1396 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001397 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001398 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001399 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001400 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001401 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001402 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001403 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001404 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001405 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1406 }
1407 else UNREACHABLE(argumentCount);
1408 }
1409 else // Offset
1410 {
1411 if(argumentCount == 3)
1412 {
1413 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1414 }
1415 else if(argumentCount == 4) // Bias
1416 {
1417 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001418 }
1419 else UNREACHABLE(argumentCount);
1420 }
1421 break;
1422 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001423 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001424 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001425 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001426 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001427 else if(argumentCount == 4) // Offset
1428 {
1429 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1430 }
1431 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001432 break;
1433 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001434 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001435 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001436 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001437 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001438 else if(argumentCount == 4) // Offset
1439 {
1440 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1441 }
1442 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001443 break;
1444 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001445 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001446 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001447 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001448 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001449 else if(argumentCount == 5) // Offset
1450 {
1451 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1452 }
1453 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001454 break;
1455 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001456 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001457 break;
1458 default:
1459 UNREACHABLE(textureFunction.method);
1460 }
1461 }
1462 }
1463 break;
1464 case EOpParameters:
1465 break;
1466 case EOpConstructFloat:
1467 case EOpConstructVec2:
1468 case EOpConstructVec3:
1469 case EOpConstructVec4:
1470 case EOpConstructBool:
1471 case EOpConstructBVec2:
1472 case EOpConstructBVec3:
1473 case EOpConstructBVec4:
1474 case EOpConstructInt:
1475 case EOpConstructIVec2:
1476 case EOpConstructIVec3:
1477 case EOpConstructIVec4:
1478 case EOpConstructUInt:
1479 case EOpConstructUVec2:
1480 case EOpConstructUVec3:
1481 case EOpConstructUVec4:
1482 if(visit == PostVisit)
1483 {
1484 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001485 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1486 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001487 for(size_t i = 0; i < argumentCount; i++)
1488 {
1489 TIntermTyped *argi = arg[i]->getAsTyped();
1490 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001491 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1492 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001493
1494 if(!argi->isMatrix())
1495 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001496 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1497 mov->dst.mask = (0xF << swizzle) & 0xF;
1498 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001499
1500 component += size;
1501 }
1502 else // Matrix
1503 {
1504 int column = 0;
1505
1506 while(component < resultType.getNominalSize())
1507 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001508 Instruction *mov = emitCast(result, arrayIndex, argi, column);
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 column++;
1513 component += size;
1514 }
1515 }
1516 }
1517 }
1518 break;
1519 case EOpConstructMat2:
1520 case EOpConstructMat2x3:
1521 case EOpConstructMat2x4:
1522 case EOpConstructMat3x2:
1523 case EOpConstructMat3:
1524 case EOpConstructMat3x4:
1525 case EOpConstructMat4x2:
1526 case EOpConstructMat4x3:
1527 case EOpConstructMat4:
1528 if(visit == PostVisit)
1529 {
1530 TIntermTyped *arg0 = arg[0]->getAsTyped();
1531 const int outCols = result->getNominalSize();
1532 const int outRows = result->getSecondarySize();
1533
1534 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1535 {
1536 for(int i = 0; i < outCols; i++)
1537 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001538 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001539 Instruction *mov = emitCast(result, i, arg0, 0);
1540 mov->dst.mask = 1 << i;
1541 ASSERT(mov->src[0].swizzle == 0x00);
1542 }
1543 }
1544 else if(arg0->isMatrix())
1545 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001546 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001547
Alexis Hetu2a198552016-09-27 20:50:45 -04001548 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001549 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001550 TIntermTyped *argi = arg[n]->getAsTyped();
1551 const int inCols = argi->getNominalSize();
1552 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001553
Alexis Hetu2a198552016-09-27 20:50:45 -04001554 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001555 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001556 if(i >= inCols || outRows > inRows)
1557 {
1558 // Initialize to identity matrix
1559 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));
1560 emitCast(result, i + n * outCols, &col, 0);
1561 }
1562
1563 if(i < inCols)
1564 {
1565 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1566 mov->dst.mask = 0xF >> (4 - inRows);
1567 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001568 }
1569 }
1570 }
1571 else
1572 {
1573 int column = 0;
1574 int row = 0;
1575
1576 for(size_t i = 0; i < argumentCount; i++)
1577 {
1578 TIntermTyped *argi = arg[i]->getAsTyped();
1579 int size = argi->getNominalSize();
1580 int element = 0;
1581
1582 while(element < size)
1583 {
1584 Instruction *mov = emitCast(result, column, argi, 0);
1585 mov->dst.mask = (0xF << row) & 0xF;
1586 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1587
1588 int end = row + size - element;
1589 column = end >= outRows ? column + 1 : column;
1590 element = element + outRows - row;
1591 row = end >= outRows ? 0 : end;
1592 }
1593 }
1594 }
1595 }
1596 break;
1597 case EOpConstructStruct:
1598 if(visit == PostVisit)
1599 {
1600 int offset = 0;
1601 for(size_t i = 0; i < argumentCount; i++)
1602 {
1603 TIntermTyped *argi = arg[i]->getAsTyped();
1604 int size = argi->totalRegisterCount();
1605
1606 for(int index = 0; index < size; index++)
1607 {
1608 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1609 mov->dst.mask = writeMask(result, offset + index);
1610 }
1611
1612 offset += size;
1613 }
1614 }
1615 break;
1616 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1617 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1618 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1619 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1620 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1621 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1622 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1623 case EOpModf:
1624 if(visit == PostVisit)
1625 {
1626 TIntermTyped* arg1 = arg[1]->getAsTyped();
1627 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1628 assignLvalue(arg1, arg1);
1629 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1630 }
1631 break;
1632 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1633 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1634 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1635 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1636 case EOpClamp:
1637 if(visit == PostVisit)
1638 {
1639 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1640 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1641 }
1642 break;
1643 case EOpMix: if(visit == PostVisit) emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]); break;
1644 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1645 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1646 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1647 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1648 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1649 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1650 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1651 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1652 case EOpMul:
1653 if(visit == PostVisit)
1654 {
1655 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001656 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1657 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001658
1659 int size = arg0->getNominalSize();
1660 for(int i = 0; i < size; i++)
1661 {
1662 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1663 }
1664 }
1665 break;
1666 case EOpOuterProduct:
1667 if(visit == PostVisit)
1668 {
1669 for(int i = 0; i < dim(arg[1]); i++)
1670 {
1671 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1672 mul->src[1].swizzle = 0x55 * i;
1673 }
1674 }
1675 break;
1676 default: UNREACHABLE(node->getOp());
1677 }
1678
1679 return true;
1680 }
1681
1682 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1683 {
1684 if(currentScope != emitScope)
1685 {
1686 return false;
1687 }
1688
1689 TIntermTyped *condition = node->getCondition();
1690 TIntermNode *trueBlock = node->getTrueBlock();
1691 TIntermNode *falseBlock = node->getFalseBlock();
1692 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1693
1694 condition->traverse(this);
1695
1696 if(node->usesTernaryOperator())
1697 {
1698 if(constantCondition)
1699 {
1700 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1701
1702 if(trueCondition)
1703 {
1704 trueBlock->traverse(this);
1705 copy(node, trueBlock);
1706 }
1707 else
1708 {
1709 falseBlock->traverse(this);
1710 copy(node, falseBlock);
1711 }
1712 }
1713 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1714 {
1715 trueBlock->traverse(this);
1716 falseBlock->traverse(this);
1717 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1718 }
1719 else
1720 {
1721 emit(sw::Shader::OPCODE_IF, 0, condition);
1722
1723 if(trueBlock)
1724 {
1725 trueBlock->traverse(this);
1726 copy(node, trueBlock);
1727 }
1728
1729 if(falseBlock)
1730 {
1731 emit(sw::Shader::OPCODE_ELSE);
1732 falseBlock->traverse(this);
1733 copy(node, falseBlock);
1734 }
1735
1736 emit(sw::Shader::OPCODE_ENDIF);
1737 }
1738 }
1739 else // if/else statement
1740 {
1741 if(constantCondition)
1742 {
1743 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1744
1745 if(trueCondition)
1746 {
1747 if(trueBlock)
1748 {
1749 trueBlock->traverse(this);
1750 }
1751 }
1752 else
1753 {
1754 if(falseBlock)
1755 {
1756 falseBlock->traverse(this);
1757 }
1758 }
1759 }
1760 else
1761 {
1762 emit(sw::Shader::OPCODE_IF, 0, condition);
1763
1764 if(trueBlock)
1765 {
1766 trueBlock->traverse(this);
1767 }
1768
1769 if(falseBlock)
1770 {
1771 emit(sw::Shader::OPCODE_ELSE);
1772 falseBlock->traverse(this);
1773 }
1774
1775 emit(sw::Shader::OPCODE_ENDIF);
1776 }
1777 }
1778
1779 return false;
1780 }
1781
1782 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1783 {
1784 if(currentScope != emitScope)
1785 {
1786 return false;
1787 }
1788
1789 unsigned int iterations = loopCount(node);
1790
1791 if(iterations == 0)
1792 {
1793 return false;
1794 }
1795
1796 bool unroll = (iterations <= 4);
1797
1798 if(unroll)
1799 {
1800 LoopUnrollable loopUnrollable;
1801 unroll = loopUnrollable.traverse(node);
1802 }
1803
1804 TIntermNode *init = node->getInit();
1805 TIntermTyped *condition = node->getCondition();
1806 TIntermTyped *expression = node->getExpression();
1807 TIntermNode *body = node->getBody();
1808 Constant True(true);
1809
1810 if(node->getType() == ELoopDoWhile)
1811 {
1812 Temporary iterate(this);
1813 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1814
1815 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1816
1817 if(body)
1818 {
1819 body->traverse(this);
1820 }
1821
1822 emit(sw::Shader::OPCODE_TEST);
1823
1824 condition->traverse(this);
1825 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1826
1827 emit(sw::Shader::OPCODE_ENDWHILE);
1828 }
1829 else
1830 {
1831 if(init)
1832 {
1833 init->traverse(this);
1834 }
1835
1836 if(unroll)
1837 {
1838 for(unsigned int i = 0; i < iterations; i++)
1839 {
1840 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1841
1842 if(body)
1843 {
1844 body->traverse(this);
1845 }
1846
1847 if(expression)
1848 {
1849 expression->traverse(this);
1850 }
1851 }
1852 }
1853 else
1854 {
1855 if(condition)
1856 {
1857 condition->traverse(this);
1858 }
1859 else
1860 {
1861 condition = &True;
1862 }
1863
1864 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1865
1866 if(body)
1867 {
1868 body->traverse(this);
1869 }
1870
1871 emit(sw::Shader::OPCODE_TEST);
1872
1873 if(expression)
1874 {
1875 expression->traverse(this);
1876 }
1877
1878 if(condition)
1879 {
1880 condition->traverse(this);
1881 }
1882
1883 emit(sw::Shader::OPCODE_ENDWHILE);
1884 }
1885 }
1886
1887 return false;
1888 }
1889
1890 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1891 {
1892 if(currentScope != emitScope)
1893 {
1894 return false;
1895 }
1896
1897 switch(node->getFlowOp())
1898 {
1899 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1900 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1901 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1902 case EOpReturn:
1903 if(visit == PostVisit)
1904 {
1905 TIntermTyped *value = node->getExpression();
1906
1907 if(value)
1908 {
1909 copy(functionArray[currentFunction].ret, value);
1910 }
1911
1912 emit(sw::Shader::OPCODE_LEAVE);
1913 }
1914 break;
1915 default: UNREACHABLE(node->getFlowOp());
1916 }
1917
1918 return true;
1919 }
1920
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001921 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1922 {
1923 if(currentScope != emitScope)
1924 {
1925 return false;
1926 }
1927
1928 TIntermTyped* switchValue = node->getInit();
1929 TIntermAggregate* opList = node->getStatementList();
1930
1931 if(!switchValue || !opList)
1932 {
1933 return false;
1934 }
1935
1936 switchValue->traverse(this);
1937
1938 emit(sw::Shader::OPCODE_SWITCH);
1939
1940 TIntermSequence& sequence = opList->getSequence();
1941 TIntermSequence::iterator it = sequence.begin();
1942 TIntermSequence::iterator defaultIt = sequence.end();
1943 int nbCases = 0;
1944 for(; it != sequence.end(); ++it)
1945 {
1946 TIntermCase* currentCase = (*it)->getAsCaseNode();
1947 if(currentCase)
1948 {
1949 TIntermSequence::iterator caseIt = it;
1950
1951 TIntermTyped* condition = currentCase->getCondition();
1952 if(condition) // non default case
1953 {
1954 if(nbCases != 0)
1955 {
1956 emit(sw::Shader::OPCODE_ELSE);
1957 }
1958
1959 condition->traverse(this);
1960 Temporary result(this);
1961 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1962 emit(sw::Shader::OPCODE_IF, 0, &result);
1963 nbCases++;
1964
Nicolas Capens6d123312018-01-08 12:57:52 -05001965 // Emit the code for this case and all subsequent cases until we hit a break statement.
1966 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001967 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1968 {
1969 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05001970
1971 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
1972 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
1973 // Note that this eliminates useless operations but shouldn't affect correctness.
1974 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001975 {
1976 break;
1977 }
1978 }
1979 }
1980 else
1981 {
1982 defaultIt = it; // The default case might not be the last case, keep it for last
1983 }
1984 }
1985 }
1986
1987 // If there's a default case, traverse it here
1988 if(defaultIt != sequence.end())
1989 {
1990 emit(sw::Shader::OPCODE_ELSE);
1991 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
1992 {
1993 (*defaultIt)->traverse(this);
1994 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1995 {
1996 break;
1997 }
1998 }
1999 }
2000
2001 for(int i = 0; i < nbCases; ++i)
2002 {
2003 emit(sw::Shader::OPCODE_ENDIF);
2004 }
2005
2006 emit(sw::Shader::OPCODE_ENDSWITCH);
2007
2008 return false;
2009 }
2010
Nicolas Capens0bac2852016-05-07 06:09:58 -04002011 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2012 {
2013 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2014 }
2015
2016 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2017 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2018 {
2019 Instruction *instruction = new Instruction(op);
2020
2021 if(dst)
2022 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002023 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002024 }
2025
Alexis Hetu929c6b02017-11-07 16:04:25 -05002026 if(src0)
2027 {
2028 TIntermTyped* src = src0->getAsTyped();
2029 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2030 }
2031
Nicolas Capens0530b452017-11-15 16:39:47 -05002032 source(instruction->src[0], src0, index0);
2033 source(instruction->src[1], src1, index1);
2034 source(instruction->src[2], src2, index2);
2035 source(instruction->src[3], src3, index3);
2036 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002037
2038 shader->append(instruction);
2039
2040 return instruction;
2041 }
2042
2043 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2044 {
2045 return emitCast(dst, 0, src, 0);
2046 }
2047
2048 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2049 {
2050 switch(src->getBasicType())
2051 {
2052 case EbtBool:
2053 switch(dst->getBasicType())
2054 {
2055 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2056 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2057 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2058 default: break;
2059 }
2060 break;
2061 case EbtInt:
2062 switch(dst->getBasicType())
2063 {
2064 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2065 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2066 default: break;
2067 }
2068 break;
2069 case EbtUInt:
2070 switch(dst->getBasicType())
2071 {
2072 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2073 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2074 default: break;
2075 }
2076 break;
2077 case EbtFloat:
2078 switch(dst->getBasicType())
2079 {
2080 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2081 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2082 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2083 default: break;
2084 }
2085 break;
2086 default:
2087 break;
2088 }
2089
2090 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2091 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2092 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2093
2094 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2095 }
2096
2097 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2098 {
2099 for(int index = 0; index < dst->elementRegisterCount(); index++)
2100 {
2101 emit(op, dst, index, src0, index, src1, index, src2, index);
2102 }
2103 }
2104
2105 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2106 {
2107 emitBinary(op, result, src0, src1);
2108 assignLvalue(lhs, result);
2109 }
2110
2111 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2112 {
2113 sw::Shader::Opcode opcode;
2114 switch(left->getAsTyped()->getBasicType())
2115 {
2116 case EbtBool:
2117 case EbtInt:
2118 opcode = sw::Shader::OPCODE_ICMP;
2119 break;
2120 case EbtUInt:
2121 opcode = sw::Shader::OPCODE_UCMP;
2122 break;
2123 default:
2124 opcode = sw::Shader::OPCODE_CMP;
2125 break;
2126 }
2127
2128 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2129 cmp->control = cmpOp;
2130 }
2131
2132 int componentCount(const TType &type, int registers)
2133 {
2134 if(registers == 0)
2135 {
2136 return 0;
2137 }
2138
2139 if(type.isArray() && registers >= type.elementRegisterCount())
2140 {
2141 int index = registers / type.elementRegisterCount();
2142 registers -= index * type.elementRegisterCount();
2143 return index * type.getElementSize() + componentCount(type, registers);
2144 }
2145
2146 if(type.isStruct() || type.isInterfaceBlock())
2147 {
2148 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2149 int elements = 0;
2150
Alexis Hetuda163ed2018-01-03 16:36:14 -05002151 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002152 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002153 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002154
2155 if(fieldType.totalRegisterCount() <= registers)
2156 {
2157 registers -= fieldType.totalRegisterCount();
2158 elements += fieldType.getObjectSize();
2159 }
2160 else // Register within this field
2161 {
2162 return elements + componentCount(fieldType, registers);
2163 }
2164 }
2165 }
2166 else if(type.isMatrix())
2167 {
2168 return registers * type.registerSize();
2169 }
2170
2171 UNREACHABLE(0);
2172 return 0;
2173 }
2174
2175 int registerSize(const TType &type, int registers)
2176 {
2177 if(registers == 0)
2178 {
2179 if(type.isStruct())
2180 {
2181 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2182 }
2183 else if(type.isInterfaceBlock())
2184 {
2185 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2186 }
2187
2188 return type.registerSize();
2189 }
2190
2191 if(type.isArray() && registers >= type.elementRegisterCount())
2192 {
2193 int index = registers / type.elementRegisterCount();
2194 registers -= index * type.elementRegisterCount();
2195 return registerSize(type, registers);
2196 }
2197
2198 if(type.isStruct() || type.isInterfaceBlock())
2199 {
2200 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2201 int elements = 0;
2202
Alexis Hetuda163ed2018-01-03 16:36:14 -05002203 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002204 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002205 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002206
2207 if(fieldType.totalRegisterCount() <= registers)
2208 {
2209 registers -= fieldType.totalRegisterCount();
2210 elements += fieldType.getObjectSize();
2211 }
2212 else // Register within this field
2213 {
2214 return registerSize(fieldType, registers);
2215 }
2216 }
2217 }
2218 else if(type.isMatrix())
2219 {
2220 return registerSize(type, 0);
2221 }
2222
2223 UNREACHABLE(0);
2224 return 0;
2225 }
2226
2227 int OutputASM::getBlockId(TIntermTyped *arg)
2228 {
2229 if(arg)
2230 {
2231 const TType &type = arg->getType();
2232 TInterfaceBlock* block = type.getInterfaceBlock();
2233 if(block && (type.getQualifier() == EvqUniform))
2234 {
2235 // Make sure the uniform block is declared
2236 uniformRegister(arg);
2237
2238 const char* blockName = block->name().c_str();
2239
2240 // Fetch uniform block index from array of blocks
2241 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2242 {
2243 if(blockName == it->name)
2244 {
2245 return it->blockId;
2246 }
2247 }
2248
2249 ASSERT(false);
2250 }
2251 }
2252
2253 return -1;
2254 }
2255
2256 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2257 {
2258 const TType &type = arg->getType();
2259 int blockId = getBlockId(arg);
2260 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2261 if(blockId != -1)
2262 {
2263 argumentInfo.bufferIndex = 0;
2264 for(int i = 0; i < blockId; ++i)
2265 {
2266 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2267 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2268 }
2269
2270 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2271
2272 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2273 BlockDefinitionIndexMap::const_iterator it = itEnd;
2274
2275 argumentInfo.clampedIndex = index;
2276 if(type.isInterfaceBlock())
2277 {
2278 // Offset index to the beginning of the selected instance
2279 int blockRegisters = type.elementRegisterCount();
2280 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2281 argumentInfo.bufferIndex += bufferOffset;
2282 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2283 }
2284
2285 int regIndex = registerIndex(arg);
2286 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2287 {
2288 it = blockDefinition.find(i);
2289 if(it != itEnd)
2290 {
2291 argumentInfo.clampedIndex -= (i - regIndex);
2292 break;
2293 }
2294 }
2295 ASSERT(it != itEnd);
2296
2297 argumentInfo.typedMemberInfo = it->second;
2298
2299 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2300 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2301 }
2302 else
2303 {
2304 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2305 }
2306
2307 return argumentInfo;
2308 }
2309
Nicolas Capens0530b452017-11-15 16:39:47 -05002310 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002311 {
2312 if(argument)
2313 {
2314 TIntermTyped *arg = argument->getAsTyped();
2315 Temporary unpackedUniform(this);
2316
2317 const TType& srcType = arg->getType();
2318 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2319 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2320 {
2321 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2322 const TType &memberType = argumentInfo.typedMemberInfo.type;
2323
2324 if(memberType.getBasicType() == EbtBool)
2325 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002326 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002327
2328 // Convert the packed bool, which is currently an int, to a true bool
2329 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2330 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2331 instruction->dst.index = registerIndex(&unpackedUniform);
2332 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2333 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2334 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2335
2336 shader->append(instruction);
2337
2338 arg = &unpackedUniform;
2339 index = 0;
2340 }
2341 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2342 {
2343 int numCols = memberType.getNominalSize();
2344 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002345
Alexis Hetue97a31e2016-11-14 14:10:47 -05002346 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002347
2348 unsigned int dstIndex = registerIndex(&unpackedUniform);
2349 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2350 int arrayIndex = argumentInfo.clampedIndex / numCols;
2351 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2352
2353 for(int j = 0; j < numRows; ++j)
2354 {
2355 // Transpose the row major matrix
2356 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2357 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2358 instruction->dst.index = dstIndex;
2359 instruction->dst.mask = 1 << j;
2360 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2361 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2362 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2363 instruction->src[0].swizzle = srcSwizzle;
2364
2365 shader->append(instruction);
2366 }
2367
2368 arg = &unpackedUniform;
2369 index = 0;
2370 }
2371 }
2372
2373 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2374 const TType &type = argumentInfo.typedMemberInfo.type;
2375
2376 int size = registerSize(type, argumentInfo.clampedIndex);
2377
2378 parameter.type = registerType(arg);
2379 parameter.bufferIndex = argumentInfo.bufferIndex;
2380
2381 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2382 {
2383 int component = componentCount(type, argumentInfo.clampedIndex);
2384 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2385
2386 for(int i = 0; i < 4; i++)
2387 {
2388 if(size == 1) // Replicate
2389 {
2390 parameter.value[i] = constants[component + 0].getAsFloat();
2391 }
2392 else if(i < size)
2393 {
2394 parameter.value[i] = constants[component + i].getAsFloat();
2395 }
2396 else
2397 {
2398 parameter.value[i] = 0.0f;
2399 }
2400 }
2401 }
2402 else
2403 {
2404 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2405
2406 if(parameter.bufferIndex != -1)
2407 {
2408 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2409 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2410 }
2411 }
2412
2413 if(!IsSampler(arg->getBasicType()))
2414 {
2415 parameter.swizzle = readSwizzle(arg, size);
2416 }
2417 }
2418 }
2419
Nicolas Capens0530b452017-11-15 16:39:47 -05002420 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2421 {
2422 parameter.type = registerType(arg);
2423 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002424 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002425 }
2426
Nicolas Capens0bac2852016-05-07 06:09:58 -04002427 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2428 {
2429 for(int index = 0; index < dst->totalRegisterCount(); index++)
2430 {
2431 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002432 }
2433 }
2434
2435 int swizzleElement(int swizzle, int index)
2436 {
2437 return (swizzle >> (index * 2)) & 0x03;
2438 }
2439
2440 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2441 {
2442 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2443 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2444 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2445 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2446 }
2447
2448 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2449 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002450 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2451 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002452 {
2453 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2454 }
2455
2456 TIntermBinary *binary = dst->getAsBinaryNode();
2457
2458 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2459 {
2460 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2461
Nicolas Capens6986b282017-11-16 10:38:19 -05002462 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002463
2464 insert->src[0].type = insert->dst.type;
2465 insert->src[0].index = insert->dst.index;
2466 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002467 source(insert->src[1], src);
2468 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002469
2470 shader->append(insert);
2471 }
2472 else
2473 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002474 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2475
Nicolas Capens6986b282017-11-16 10:38:19 -05002476 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002477
Nicolas Capens0530b452017-11-15 16:39:47 -05002478 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002479 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2480
2481 shader->append(mov1);
2482
2483 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002484 {
2485 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2486
Nicolas Capens84249fd2017-11-09 11:20:51 -05002487 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002488 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002489 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002490
Nicolas Capens0530b452017-11-15 16:39:47 -05002491 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002492
2493 shader->append(mov);
2494 }
2495 }
2496 }
2497
Nicolas Capensd469de22017-11-16 10:42:20 -05002498 void OutputASM::evaluateRvalue(TIntermTyped *node)
2499 {
2500 TIntermBinary *binary = node->getAsBinaryNode();
2501
2502 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2503 {
2504 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2505
2506 destination(insert->dst, node);
2507
2508 Temporary address(this);
2509 unsigned char mask;
2510 TIntermTyped *root = nullptr;
2511 unsigned int offset = 0;
2512 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2513
2514 source(insert->src[0], root, offset);
2515 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2516
2517 source(insert->src[1], binary->getRight());
2518
2519 shader->append(insert);
2520 }
2521 else
2522 {
2523 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2524
2525 destination(mov1->dst, node, 0);
2526
2527 Temporary address(this);
2528 unsigned char mask;
2529 TIntermTyped *root = nullptr;
2530 unsigned int offset = 0;
2531 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2532
2533 source(mov1->src[0], root, offset);
2534 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2535
2536 shader->append(mov1);
2537
2538 for(int i = 1; i < node->totalRegisterCount(); i++)
2539 {
2540 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2541 mov->src[0].rel = mov1->src[0].rel;
2542 }
2543 }
2544 }
2545
Nicolas Capens6986b282017-11-16 10:38:19 -05002546 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002547 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002548 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002549 TIntermTyped *root = nullptr;
2550 unsigned int offset = 0;
2551 unsigned char mask = 0xF;
2552 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2553
2554 dst.type = registerType(root);
2555 dst.index = registerIndex(root) + offset;
2556 dst.mask = mask;
2557
2558 return swizzle;
2559 }
2560
2561 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2562 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002563 TIntermTyped *result = node;
2564 TIntermBinary *binary = node->getAsBinaryNode();
2565 TIntermSymbol *symbol = node->getAsSymbolNode();
2566
2567 if(binary)
2568 {
2569 TIntermTyped *left = binary->getLeft();
2570 TIntermTyped *right = binary->getRight();
2571
Nicolas Capens0530b452017-11-15 16:39:47 -05002572 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002573
2574 switch(binary->getOp())
2575 {
2576 case EOpIndexDirect:
2577 {
2578 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2579
2580 if(left->isRegister())
2581 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002582 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002583
Nicolas Capens0530b452017-11-15 16:39:47 -05002584 mask = 1;
2585 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002586 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002587 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002588 }
2589
2590 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002591 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002592
2593 return element;
2594 }
2595 else if(left->isArray() || left->isMatrix())
2596 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002597 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002598 return 0xE4;
2599 }
2600 else UNREACHABLE(0);
2601 }
2602 break;
2603 case EOpIndexIndirect:
2604 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002605 right->traverse(this);
2606
Nicolas Capens0bac2852016-05-07 06:09:58 -04002607 if(left->isRegister())
2608 {
2609 // Requires INSERT instruction (handled by calling function)
2610 }
2611 else if(left->isArray() || left->isMatrix())
2612 {
2613 int scale = result->totalRegisterCount();
2614
Nicolas Capens0530b452017-11-15 16:39:47 -05002615 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002616 {
2617 if(left->totalRegisterCount() > 1)
2618 {
2619 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002620 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002621
Nicolas Capens0530b452017-11-15 16:39:47 -05002622 rel.index = relativeRegister.index;
2623 rel.type = relativeRegister.type;
2624 rel.scale = scale;
2625 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002626 }
2627 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002628 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002629 {
2630 if(scale == 1)
2631 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002632 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002633 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002634 mad->src[0].index = rel.index;
2635 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002636 }
2637 else
2638 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002639 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002640 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002641 mul->src[0].index = rel.index;
2642 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002643
2644 Constant newScale(scale);
2645 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2646 }
2647
Nicolas Capens0530b452017-11-15 16:39:47 -05002648 rel.type = sw::Shader::PARAMETER_TEMP;
2649 rel.index = registerIndex(&address);
2650 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002651 }
2652 else // Just add the new index to the address register
2653 {
2654 if(scale == 1)
2655 {
2656 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2657 }
2658 else
2659 {
2660 Constant newScale(scale);
2661 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2662 }
2663 }
2664 }
2665 else UNREACHABLE(0);
2666 }
2667 break;
2668 case EOpIndexDirectStruct:
2669 case EOpIndexDirectInterfaceBlock:
2670 {
2671 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2672 left->getType().getStruct()->fields() :
2673 left->getType().getInterfaceBlock()->fields();
2674 int index = right->getAsConstantUnion()->getIConst(0);
2675 int fieldOffset = 0;
2676
2677 for(int i = 0; i < index; i++)
2678 {
2679 fieldOffset += fields[i]->type()->totalRegisterCount();
2680 }
2681
Nicolas Capens0530b452017-11-15 16:39:47 -05002682 offset += fieldOffset;
2683 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002684
2685 return 0xE4;
2686 }
2687 break;
2688 case EOpVectorSwizzle:
2689 {
2690 ASSERT(left->isRegister());
2691
Nicolas Capens0530b452017-11-15 16:39:47 -05002692 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002693
2694 int swizzle = 0;
2695 int rightMask = 0;
2696
2697 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2698
2699 for(unsigned int i = 0; i < sequence.size(); i++)
2700 {
2701 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2702
2703 int element = swizzleElement(leftSwizzle, index);
2704 rightMask = rightMask | (1 << element);
2705 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2706 }
2707
Nicolas Capens0530b452017-11-15 16:39:47 -05002708 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002709
2710 return swizzle;
2711 }
2712 break;
2713 default:
2714 UNREACHABLE(binary->getOp()); // Not an l-value operator
2715 break;
2716 }
2717 }
2718 else if(symbol)
2719 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002720 root = symbol;
2721 offset = 0;
2722 mask = writeMask(symbol);
2723
2724 return 0xE4;
2725 }
2726 else
2727 {
2728 node->traverse(this);
2729
2730 root = node;
2731 offset = 0;
2732 mask = writeMask(node);
2733
Nicolas Capens0bac2852016-05-07 06:09:58 -04002734 return 0xE4;
2735 }
2736
2737 return 0xE4;
2738 }
2739
2740 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2741 {
2742 if(isSamplerRegister(operand))
2743 {
2744 return sw::Shader::PARAMETER_SAMPLER;
2745 }
2746
2747 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002748 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002749 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002750 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2751 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002752 {
2753 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2754 }
2755 outputQualifier = qualifier;
2756 }
2757
2758 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2759 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002760 // Constant arrays are in the constant register file.
2761 if(operand->isArray() && operand->getArraySize() > 1)
2762 {
2763 return sw::Shader::PARAMETER_CONST;
2764 }
2765 else
2766 {
2767 return sw::Shader::PARAMETER_TEMP;
2768 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002769 }
2770
2771 switch(qualifier)
2772 {
2773 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2774 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2775 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2776 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2777 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2778 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2779 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2780 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2781 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2782 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2783 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2784 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2785 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2786 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2787 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2788 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2789 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2790 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2791 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2792 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2793 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2794 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2795 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2796 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2797 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2798 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002799 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002800 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2801 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2802 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2803 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2804 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2805 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2806 default: UNREACHABLE(qualifier);
2807 }
2808
2809 return sw::Shader::PARAMETER_VOID;
2810 }
2811
Alexis Hetu12b00502016-05-20 13:01:11 -04002812 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2813 {
2814 const TQualifier qualifier = operand->getQualifier();
2815 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2816 }
2817
Nicolas Capens0bac2852016-05-07 06:09:58 -04002818 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2819 {
2820 if(isSamplerRegister(operand))
2821 {
2822 return samplerRegister(operand);
2823 }
2824
2825 switch(operand->getQualifier())
2826 {
2827 case EvqTemporary: return temporaryRegister(operand);
2828 case EvqGlobal: return temporaryRegister(operand);
2829 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2830 case EvqAttribute: return attributeRegister(operand);
2831 case EvqVaryingIn: return varyingRegister(operand);
2832 case EvqVaryingOut: return varyingRegister(operand);
2833 case EvqVertexIn: return attributeRegister(operand);
2834 case EvqFragmentOut: return fragmentOutputRegister(operand);
2835 case EvqVertexOut: return varyingRegister(operand);
2836 case EvqFragmentIn: return varyingRegister(operand);
2837 case EvqInvariantVaryingIn: return varyingRegister(operand);
2838 case EvqInvariantVaryingOut: return varyingRegister(operand);
2839 case EvqSmooth: return varyingRegister(operand);
2840 case EvqFlat: return varyingRegister(operand);
2841 case EvqCentroidOut: return varyingRegister(operand);
2842 case EvqSmoothIn: return varyingRegister(operand);
2843 case EvqFlatIn: return varyingRegister(operand);
2844 case EvqCentroidIn: return varyingRegister(operand);
2845 case EvqUniform: return uniformRegister(operand);
2846 case EvqIn: return temporaryRegister(operand);
2847 case EvqOut: return temporaryRegister(operand);
2848 case EvqInOut: return temporaryRegister(operand);
2849 case EvqConstReadOnly: return temporaryRegister(operand);
2850 case EvqPosition: return varyingRegister(operand);
2851 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002852 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2853 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2854 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2855 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002856 case EvqPointCoord: return varyingRegister(operand);
2857 case EvqFragColor: return 0;
2858 case EvqFragData: return fragmentOutputRegister(operand);
2859 case EvqFragDepth: return 0;
2860 default: UNREACHABLE(operand->getQualifier());
2861 }
2862
2863 return 0;
2864 }
2865
2866 int OutputASM::writeMask(TIntermTyped *destination, int index)
2867 {
2868 if(destination->getQualifier() == EvqPointSize)
2869 {
2870 return 0x2; // Point size stored in the y component
2871 }
2872
2873 return 0xF >> (4 - registerSize(destination->getType(), index));
2874 }
2875
2876 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2877 {
2878 if(argument->getQualifier() == EvqPointSize)
2879 {
2880 return 0x55; // Point size stored in the y component
2881 }
2882
2883 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2884
2885 return swizzleSize[size];
2886 }
2887
2888 // Conservatively checks whether an expression is fast to compute and has no side effects
2889 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2890 {
2891 if(!expression->isRegister())
2892 {
2893 return false;
2894 }
2895
2896 return cost(expression, budget) >= 0;
2897 }
2898
2899 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2900 int OutputASM::cost(TIntermNode *expression, int budget)
2901 {
2902 if(budget < 0)
2903 {
2904 return budget;
2905 }
2906
2907 if(expression->getAsSymbolNode())
2908 {
2909 return budget;
2910 }
2911 else if(expression->getAsConstantUnion())
2912 {
2913 return budget;
2914 }
2915 else if(expression->getAsBinaryNode())
2916 {
2917 TIntermBinary *binary = expression->getAsBinaryNode();
2918
2919 switch(binary->getOp())
2920 {
2921 case EOpVectorSwizzle:
2922 case EOpIndexDirect:
2923 case EOpIndexDirectStruct:
2924 case EOpIndexDirectInterfaceBlock:
2925 return cost(binary->getLeft(), budget - 0);
2926 case EOpAdd:
2927 case EOpSub:
2928 case EOpMul:
2929 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2930 default:
2931 return -1;
2932 }
2933 }
2934 else if(expression->getAsUnaryNode())
2935 {
2936 TIntermUnary *unary = expression->getAsUnaryNode();
2937
2938 switch(unary->getOp())
2939 {
2940 case EOpAbs:
2941 case EOpNegative:
2942 return cost(unary->getOperand(), budget - 1);
2943 default:
2944 return -1;
2945 }
2946 }
2947 else if(expression->getAsSelectionNode())
2948 {
2949 TIntermSelection *selection = expression->getAsSelectionNode();
2950
2951 if(selection->usesTernaryOperator())
2952 {
2953 TIntermTyped *condition = selection->getCondition();
2954 TIntermNode *trueBlock = selection->getTrueBlock();
2955 TIntermNode *falseBlock = selection->getFalseBlock();
2956 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2957
2958 if(constantCondition)
2959 {
2960 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2961
2962 if(trueCondition)
2963 {
2964 return cost(trueBlock, budget - 0);
2965 }
2966 else
2967 {
2968 return cost(falseBlock, budget - 0);
2969 }
2970 }
2971 else
2972 {
2973 return cost(trueBlock, cost(falseBlock, budget - 2));
2974 }
2975 }
2976 }
2977
2978 return -1;
2979 }
2980
2981 const Function *OutputASM::findFunction(const TString &name)
2982 {
2983 for(unsigned int f = 0; f < functionArray.size(); f++)
2984 {
2985 if(functionArray[f].name == name)
2986 {
2987 return &functionArray[f];
2988 }
2989 }
2990
2991 return 0;
2992 }
2993
2994 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2995 {
2996 return allocate(temporaries, temporary);
2997 }
2998
Alexis Hetu49351232017-11-02 16:00:32 -04002999 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3000 {
3001 if(type.isStruct())
3002 {
3003 const TFieldList &fields = type.getStruct()->fields();
3004 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003005 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003006 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003007 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003008 setPixelShaderInputs(fieldType, fieldVar, flat);
3009 fieldVar += fieldType.totalRegisterCount();
3010 }
3011 }
3012 else
3013 {
3014 for(int i = 0; i < type.totalRegisterCount(); i++)
3015 {
3016 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3017 }
3018 }
3019 }
3020
Nicolas Capens0bac2852016-05-07 06:09:58 -04003021 int OutputASM::varyingRegister(TIntermTyped *varying)
3022 {
3023 int var = lookup(varyings, varying);
3024
3025 if(var == -1)
3026 {
3027 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003028 int registerCount = varying->totalRegisterCount();
3029
3030 if(pixelShader)
3031 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003032 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003033 {
3034 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3035 return 0;
3036 }
3037
3038 if(varying->getQualifier() == EvqPointCoord)
3039 {
3040 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003041 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003042 }
3043 else
3044 {
Alexis Hetu49351232017-11-02 16:00:32 -04003045 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003046 }
3047 }
3048 else if(vertexShader)
3049 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003050 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003051 {
3052 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3053 return 0;
3054 }
3055
3056 if(varying->getQualifier() == EvqPosition)
3057 {
3058 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003059 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003060 }
3061 else if(varying->getQualifier() == EvqPointSize)
3062 {
3063 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003064 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003065 }
3066 else
3067 {
3068 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3069 }
3070 }
3071 else UNREACHABLE(0);
3072
3073 declareVarying(varying, var);
3074 }
3075
3076 return var;
3077 }
3078
3079 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3080 {
3081 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3082 {
Alexis Hetu49351232017-11-02 16:00:32 -04003083 TIntermSymbol *symbol = varying->getAsSymbolNode();
3084 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3085 }
3086 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003087
Alexis Hetu49351232017-11-02 16:00:32 -04003088 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3089 {
3090 const char *name = varyingName.c_str();
3091 VaryingList &activeVaryings = shaderObject->varyings;
3092
3093 TStructure* structure = type.getStruct();
3094 if(structure)
3095 {
3096 int fieldRegisterIndex = registerIndex;
3097
3098 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003099 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003100 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003101 const TType& fieldType = *(field->type());
3102 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003103 if(fieldRegisterIndex >= 0)
3104 {
3105 fieldRegisterIndex += fieldType.totalRegisterCount();
3106 }
3107 }
3108 }
3109 else
3110 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003111 // Check if this varying has been declared before without having a register assigned
3112 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3113 {
3114 if(v->name == name)
3115 {
Alexis Hetu49351232017-11-02 16:00:32 -04003116 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003117 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003118 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3119 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003120 }
3121
3122 return;
3123 }
3124 }
3125
Alexis Hetu924513c2018-01-05 15:48:12 -05003126 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003127 }
3128 }
3129
3130 int OutputASM::uniformRegister(TIntermTyped *uniform)
3131 {
3132 const TType &type = uniform->getType();
3133 ASSERT(!IsSampler(type.getBasicType()));
3134 TInterfaceBlock *block = type.getAsInterfaceBlock();
3135 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3136 ASSERT(symbol || block);
3137
3138 if(symbol || block)
3139 {
3140 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3141 bool isBlockMember = (!block && parentBlock);
3142 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3143
3144 if(index == -1 || isBlockMember)
3145 {
3146 if(index == -1)
3147 {
3148 index = allocate(uniforms, uniform);
3149 }
3150
3151 // Verify if the current uniform is a member of an already declared block
3152 const TString &name = symbol ? symbol->getSymbol() : block->name();
3153 int blockMemberIndex = blockMemberLookup(type, name, index);
3154 if(blockMemberIndex == -1)
3155 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003156 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003157 }
3158 else
3159 {
3160 index = blockMemberIndex;
3161 }
3162 }
3163
3164 return index;
3165 }
3166
3167 return 0;
3168 }
3169
3170 int OutputASM::attributeRegister(TIntermTyped *attribute)
3171 {
3172 ASSERT(!attribute->isArray());
3173
3174 int index = lookup(attributes, attribute);
3175
3176 if(index == -1)
3177 {
3178 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3179 ASSERT(symbol);
3180
3181 if(symbol)
3182 {
3183 index = allocate(attributes, attribute);
3184 const TType &type = attribute->getType();
3185 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003186 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3187 switch(type.getBasicType())
3188 {
3189 case EbtInt:
3190 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3191 break;
3192 case EbtUInt:
3193 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3194 break;
3195 case EbtFloat:
3196 default:
3197 break;
3198 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003199
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003200 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003201 {
3202 for(int i = 0; i < registerCount; i++)
3203 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003204 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003205 }
3206 }
3207
3208 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3209
3210 const char *name = symbol->getSymbol().c_str();
3211 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3212 }
3213 }
3214
3215 return index;
3216 }
3217
3218 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3219 {
3220 return allocate(fragmentOutputs, fragmentOutput);
3221 }
3222
3223 int OutputASM::samplerRegister(TIntermTyped *sampler)
3224 {
3225 const TType &type = sampler->getType();
3226 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3227
3228 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3229 TIntermBinary *binary = sampler->getAsBinaryNode();
3230
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003231 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003232 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003233 switch(type.getQualifier())
3234 {
3235 case EvqUniform:
3236 return samplerRegister(symbol);
3237 case EvqIn:
3238 case EvqConstReadOnly:
3239 // Function arguments are not (uniform) sampler registers
3240 return -1;
3241 default:
3242 UNREACHABLE(type.getQualifier());
3243 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003244 }
3245 else if(binary)
3246 {
3247 TIntermTyped *left = binary->getLeft();
3248 TIntermTyped *right = binary->getRight();
3249 const TType &leftType = left->getType();
3250 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3251 int offset = 0;
3252
3253 switch(binary->getOp())
3254 {
3255 case EOpIndexDirect:
3256 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003257 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003258 break;
3259 case EOpIndexDirectStruct:
3260 ASSERT(leftType.isStruct());
3261 {
3262 const TFieldList &fields = leftType.getStruct()->fields();
3263
3264 for(int i = 0; i < index; i++)
3265 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003266 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003267 }
3268 }
3269 break;
3270 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3271 return -1;
3272 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3273 default:
3274 UNREACHABLE(binary->getOp());
3275 return -1;
3276 }
3277
3278 int base = samplerRegister(left);
3279
3280 if(base < 0)
3281 {
3282 return -1;
3283 }
3284
3285 return base + offset;
3286 }
3287
3288 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003289 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003290 }
3291
3292 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3293 {
3294 const TType &type = sampler->getType();
3295 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3296
3297 int index = lookup(samplers, sampler);
3298
3299 if(index == -1)
3300 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003301 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003302
3303 if(sampler->getQualifier() == EvqUniform)
3304 {
3305 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003306 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003307 }
3308 }
3309
3310 return index;
3311 }
3312
3313 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3314 {
3315 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3316 }
3317
3318 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3319 {
3320 for(unsigned int i = 0; i < list.size(); i++)
3321 {
3322 if(list[i] == variable)
3323 {
3324 return i; // Pointer match
3325 }
3326 }
3327
3328 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3329 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3330
3331 if(varBlock)
3332 {
3333 for(unsigned int i = 0; i < list.size(); i++)
3334 {
3335 if(list[i])
3336 {
3337 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3338
3339 if(listBlock)
3340 {
3341 if(listBlock->name() == varBlock->name())
3342 {
3343 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3344 ASSERT(listBlock->fields() == varBlock->fields());
3345 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3346 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3347
3348 return i;
3349 }
3350 }
3351 }
3352 }
3353 }
3354 else if(varSymbol)
3355 {
3356 for(unsigned int i = 0; i < list.size(); i++)
3357 {
3358 if(list[i])
3359 {
3360 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3361
3362 if(listSymbol)
3363 {
3364 if(listSymbol->getId() == varSymbol->getId())
3365 {
3366 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3367 ASSERT(listSymbol->getType() == varSymbol->getType());
3368 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3369
3370 return i;
3371 }
3372 }
3373 }
3374 }
3375 }
3376
3377 return -1;
3378 }
3379
3380 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3381 {
3382 for(unsigned int i = 0; i < list.size(); i++)
3383 {
3384 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3385 {
3386 return i; // Pointer match
3387 }
3388 }
3389 return -1;
3390 }
3391
Alexis Hetuda163ed2018-01-03 16:36:14 -05003392 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003393 {
3394 int index = lookup(list, variable);
3395
3396 if(index == -1)
3397 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003398 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003399
3400 for(unsigned int i = 0; i < list.size(); i++)
3401 {
3402 if(list[i] == 0)
3403 {
3404 unsigned int j = 1;
3405 for( ; j < registerCount && (i + j) < list.size(); j++)
3406 {
3407 if(list[i + j] != 0)
3408 {
3409 break;
3410 }
3411 }
3412
3413 if(j == registerCount) // Found free slots
3414 {
3415 for(unsigned int j = 0; j < registerCount; j++)
3416 {
3417 list[i + j] = variable;
3418 }
3419
3420 return i;
3421 }
3422 }
3423 }
3424
3425 index = list.size();
3426
3427 for(unsigned int i = 0; i < registerCount; i++)
3428 {
3429 list.push_back(variable);
3430 }
3431 }
3432
3433 return index;
3434 }
3435
3436 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3437 {
3438 int index = lookup(list, variable);
3439
3440 if(index >= 0)
3441 {
3442 list[index] = 0;
3443 }
3444 }
3445
3446 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3447 {
3448 const TInterfaceBlock *block = type.getInterfaceBlock();
3449
3450 if(block)
3451 {
3452 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3453 const TFieldList& fields = block->fields();
3454 const TString &blockName = block->name();
3455 int fieldRegisterIndex = registerIndex;
3456
3457 if(!type.isInterfaceBlock())
3458 {
3459 // This is a uniform that's part of a block, let's see if the block is already defined
3460 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3461 {
3462 if(activeUniformBlocks[i].name == blockName.c_str())
3463 {
3464 // The block is already defined, find the register for the current uniform and return it
3465 for(size_t j = 0; j < fields.size(); j++)
3466 {
3467 const TString &fieldName = fields[j]->name();
3468 if(fieldName == name)
3469 {
3470 return fieldRegisterIndex;
3471 }
3472
3473 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3474 }
3475
3476 ASSERT(false);
3477 return fieldRegisterIndex;
3478 }
3479 }
3480 }
3481 }
3482
3483 return -1;
3484 }
3485
Alexis Hetuda163ed2018-01-03 16:36:14 -05003486 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003487 {
3488 const TStructure *structure = type.getStruct();
3489 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3490
3491 if(!structure && !block)
3492 {
3493 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3494 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3495 if(blockId >= 0)
3496 {
3497 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3498 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3499 }
3500 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003501 bool isSampler = IsSampler(type.getBasicType());
3502 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003503 {
3504 for(int i = 0; i < type.totalRegisterCount(); i++)
3505 {
3506 shader->declareSampler(fieldRegisterIndex + i);
3507 }
3508 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003509 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003510 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003511 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003512 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003513 }
3514 else if(block)
3515 {
3516 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3517 const TFieldList& fields = block->fields();
3518 const TString &blockName = block->name();
3519 int fieldRegisterIndex = registerIndex;
3520 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3521
3522 blockId = activeUniformBlocks.size();
3523 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3524 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3525 block->blockStorage(), isRowMajor, registerIndex, blockId));
3526 blockDefinitions.push_back(BlockDefinitionIndexMap());
3527
3528 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3529 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003530 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003531 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003532 const TType &fieldType = *(field->type());
3533 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003534 if(isUniformBlockMember && (fieldName == name))
3535 {
3536 registerIndex = fieldRegisterIndex;
3537 }
3538
3539 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3540
Alexis Hetuda163ed2018-01-03 16:36:14 -05003541 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003542 fieldRegisterIndex += fieldType.totalRegisterCount();
3543 }
3544 currentBlockEncoder.exitAggregateType();
3545 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3546 }
3547 else
3548 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003549 // Store struct for program link time validation
3550 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3551
Nicolas Capens0bac2852016-05-07 06:09:58 -04003552 int fieldRegisterIndex = registerIndex;
3553
3554 const TFieldList& fields = structure->fields();
3555 if(type.isArray() && (structure || type.isInterfaceBlock()))
3556 {
3557 for(int i = 0; i < type.getArraySize(); i++)
3558 {
3559 if(encoder)
3560 {
3561 encoder->enterAggregateType();
3562 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003563 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003564 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003565 const TType &fieldType = *(field->type());
3566 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003567 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3568
Alexis Hetuda163ed2018-01-03 16:36:14 -05003569 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3570 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003571 }
3572 if(encoder)
3573 {
3574 encoder->exitAggregateType();
3575 }
3576 }
3577 }
3578 else
3579 {
3580 if(encoder)
3581 {
3582 encoder->enterAggregateType();
3583 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003584 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003585 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003586 const TType &fieldType = *(field->type());
3587 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003588 const TString uniformName = name + "." + fieldName;
3589
Alexis Hetuda163ed2018-01-03 16:36:14 -05003590 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3591 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003592 }
3593 if(encoder)
3594 {
3595 encoder->exitAggregateType();
3596 }
3597 }
3598 }
3599 }
3600
Nicolas Capens0bac2852016-05-07 06:09:58 -04003601 int OutputASM::dim(TIntermNode *v)
3602 {
3603 TIntermTyped *vector = v->getAsTyped();
3604 ASSERT(vector && vector->isRegister());
3605 return vector->getNominalSize();
3606 }
3607
3608 int OutputASM::dim2(TIntermNode *m)
3609 {
3610 TIntermTyped *matrix = m->getAsTyped();
3611 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3612 return matrix->getSecondarySize();
3613 }
3614
3615 // Returns ~0u if no loop count could be determined
3616 unsigned int OutputASM::loopCount(TIntermLoop *node)
3617 {
3618 // Parse loops of the form:
3619 // for(int index = initial; index [comparator] limit; index += increment)
3620 TIntermSymbol *index = 0;
3621 TOperator comparator = EOpNull;
3622 int initial = 0;
3623 int limit = 0;
3624 int increment = 0;
3625
3626 // Parse index name and intial value
3627 if(node->getInit())
3628 {
3629 TIntermAggregate *init = node->getInit()->getAsAggregate();
3630
3631 if(init)
3632 {
3633 TIntermSequence &sequence = init->getSequence();
3634 TIntermTyped *variable = sequence[0]->getAsTyped();
3635
Nicolas Capense3f05552017-05-24 10:45:56 -04003636 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003637 {
3638 TIntermBinary *assign = variable->getAsBinaryNode();
3639
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003640 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003641 {
3642 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3643 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3644
3645 if(symbol && constant)
3646 {
3647 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3648 {
3649 index = symbol;
3650 initial = constant->getUnionArrayPointer()[0].getIConst();
3651 }
3652 }
3653 }
3654 }
3655 }
3656 }
3657
3658 // Parse comparator and limit value
3659 if(index && node->getCondition())
3660 {
3661 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003662 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003663
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003664 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003665 {
3666 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3667
3668 if(constant)
3669 {
3670 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3671 {
3672 comparator = test->getOp();
3673 limit = constant->getUnionArrayPointer()[0].getIConst();
3674 }
3675 }
3676 }
3677 }
3678
3679 // Parse increment
3680 if(index && comparator != EOpNull && node->getExpression())
3681 {
3682 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3683 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3684
3685 if(binaryTerminal)
3686 {
3687 TOperator op = binaryTerminal->getOp();
3688 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3689
3690 if(constant)
3691 {
3692 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3693 {
3694 int value = constant->getUnionArrayPointer()[0].getIConst();
3695
3696 switch(op)
3697 {
3698 case EOpAddAssign: increment = value; break;
3699 case EOpSubAssign: increment = -value; break;
3700 default: UNIMPLEMENTED();
3701 }
3702 }
3703 }
3704 }
3705 else if(unaryTerminal)
3706 {
3707 TOperator op = unaryTerminal->getOp();
3708
3709 switch(op)
3710 {
3711 case EOpPostIncrement: increment = 1; break;
3712 case EOpPostDecrement: increment = -1; break;
3713 case EOpPreIncrement: increment = 1; break;
3714 case EOpPreDecrement: increment = -1; break;
3715 default: UNIMPLEMENTED();
3716 }
3717 }
3718 }
3719
3720 if(index && comparator != EOpNull && increment != 0)
3721 {
3722 if(comparator == EOpLessThanEqual)
3723 {
3724 comparator = EOpLessThan;
3725 limit += 1;
3726 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003727 else if(comparator == EOpGreaterThanEqual)
3728 {
3729 comparator = EOpLessThan;
3730 limit -= 1;
3731 std::swap(initial, limit);
3732 increment = -increment;
3733 }
3734 else if(comparator == EOpGreaterThan)
3735 {
3736 comparator = EOpLessThan;
3737 std::swap(initial, limit);
3738 increment = -increment;
3739 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003740
3741 if(comparator == EOpLessThan)
3742 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003743 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003744 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003745 return 0;
3746 }
3747
3748 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3749
3750 if(iterations < 0)
3751 {
3752 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003753 }
3754
3755 return iterations;
3756 }
3757 else UNIMPLEMENTED(); // Falls through
3758 }
3759
3760 return ~0u;
3761 }
3762
3763 bool LoopUnrollable::traverse(TIntermNode *node)
3764 {
3765 loopDepth = 0;
3766 loopUnrollable = true;
3767
3768 node->traverse(this);
3769
3770 return loopUnrollable;
3771 }
3772
3773 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3774 {
3775 if(visit == PreVisit)
3776 {
3777 loopDepth++;
3778 }
3779 else if(visit == PostVisit)
3780 {
3781 loopDepth++;
3782 }
3783
3784 return true;
3785 }
3786
3787 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3788 {
3789 if(!loopUnrollable)
3790 {
3791 return false;
3792 }
3793
3794 if(!loopDepth)
3795 {
3796 return true;
3797 }
3798
3799 switch(node->getFlowOp())
3800 {
3801 case EOpKill:
3802 case EOpReturn:
3803 break;
3804 case EOpBreak:
3805 case EOpContinue:
3806 loopUnrollable = false;
3807 break;
3808 default: UNREACHABLE(node->getFlowOp());
3809 }
3810
3811 return loopUnrollable;
3812 }
3813
3814 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3815 {
3816 return loopUnrollable;
3817 }
3818}