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