blob: 4ef41be8495e568f9883bb53947e1b34005f49ce [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;
Alexis Hetu46768622018-01-16 22:09:28 -0500142 case EbtSampler2DRect:
143 return GL_SAMPLER_2D_RECT_ARB;
Alexis Hetu924513c2018-01-05 15:48:12 -0500144 case EbtISamplerCube:
145 return GL_INT_SAMPLER_CUBE;
146 case EbtUSamplerCube:
147 return GL_UNSIGNED_INT_SAMPLER_CUBE;
148 case EbtSamplerExternalOES:
149 return GL_SAMPLER_EXTERNAL_OES;
150 case EbtSampler3D:
151 return GL_SAMPLER_3D_OES;
152 case EbtISampler3D:
153 return GL_INT_SAMPLER_3D;
154 case EbtUSampler3D:
155 return GL_UNSIGNED_INT_SAMPLER_3D;
156 case EbtSampler2DArray:
157 return GL_SAMPLER_2D_ARRAY;
158 case EbtISampler2DArray:
159 return GL_INT_SAMPLER_2D_ARRAY;
160 case EbtUSampler2DArray:
161 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
162 case EbtSampler2DShadow:
163 return GL_SAMPLER_2D_SHADOW;
164 case EbtSamplerCubeShadow:
165 return GL_SAMPLER_CUBE_SHADOW;
166 case EbtSampler2DArrayShadow:
167 return GL_SAMPLER_2D_ARRAY_SHADOW;
168 default:
169 UNREACHABLE(type.getBasicType());
170 break;
171 }
172
173 return GL_NONE;
174 }
175
176 GLenum glVariablePrecision(const TType &type)
177 {
178 if(type.getBasicType() == EbtFloat)
179 {
180 switch(type.getPrecision())
181 {
182 case EbpHigh: return GL_HIGH_FLOAT;
183 case EbpMedium: return GL_MEDIUM_FLOAT;
184 case EbpLow: return GL_LOW_FLOAT;
185 case EbpUndefined:
186 // Should be defined as the default precision by the parser
187 default: UNREACHABLE(type.getPrecision());
188 }
189 }
190 else if(type.getBasicType() == EbtInt)
191 {
192 switch(type.getPrecision())
193 {
194 case EbpHigh: return GL_HIGH_INT;
195 case EbpMedium: return GL_MEDIUM_INT;
196 case EbpLow: return GL_LOW_INT;
197 case EbpUndefined:
198 // Should be defined as the default precision by the parser
199 default: UNREACHABLE(type.getPrecision());
200 }
201 }
202
203 // Other types (boolean, sampler) don't have a precision
204 return GL_NONE;
205 }
206}
207
Nicolas Capens0bac2852016-05-07 06:09:58 -0400208namespace glsl
209{
210 // Integer to TString conversion
211 TString str(int i)
212 {
213 char buffer[20];
214 sprintf(buffer, "%d", i);
215 return buffer;
216 }
217
218 class Temporary : public TIntermSymbol
219 {
220 public:
221 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
222 {
223 }
224
225 ~Temporary()
226 {
227 assembler->freeTemporary(this);
228 }
229
230 private:
231 OutputASM *const assembler;
232 };
233
234 class Constant : public TIntermConstantUnion
235 {
236 public:
237 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
238 {
239 constants[0].setFConst(x);
240 constants[1].setFConst(y);
241 constants[2].setFConst(z);
242 constants[3].setFConst(w);
243 }
244
245 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
246 {
247 constants[0].setBConst(b);
248 }
249
250 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
251 {
252 constants[0].setIConst(i);
253 }
254
255 ~Constant()
256 {
257 }
258
259 private:
260 ConstantUnion constants[4];
261 };
262
Alexis Hetu924513c2018-01-05 15:48:12 -0500263 ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
264 type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
265 name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
266 {
267 if(type.isStruct())
268 {
269 for(const auto& field : type.getStruct()->fields())
270 {
271 fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
272 }
273 }
274 }
275
276 Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
277 ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400278 {
279 }
280
281 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
282 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
283 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
284 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
285 {
286 }
287
Alexis Hetud2742532018-01-23 16:53:41 -0500288 BlockLayoutEncoder::BlockLayoutEncoder()
289 : mCurrentOffset(0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400290 {
291 }
292
293 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
294 {
295 int arrayStride;
296 int matrixStride;
297
Alexis Hetud2742532018-01-23 16:53:41 -0500298 bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
299 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400300
301 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
302 static_cast<int>(arrayStride * BytesPerComponent),
303 static_cast<int>(matrixStride * BytesPerComponent),
Alexis Hetud2742532018-01-23 16:53:41 -0500304 (matrixStride > 0) && isRowMajor);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400305
Alexis Hetud2742532018-01-23 16:53:41 -0500306 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400307
308 return memberInfo;
309 }
310
311 // static
312 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
313 {
314 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
315 }
316
317 // static
318 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
319 {
320 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
321 }
322
323 void BlockLayoutEncoder::nextRegister()
324 {
325 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
326 }
327
Alexis Hetud2742532018-01-23 16:53:41 -0500328 Std140BlockEncoder::Std140BlockEncoder() : BlockLayoutEncoder()
Nicolas Capens0bac2852016-05-07 06:09:58 -0400329 {
330 }
331
332 void Std140BlockEncoder::enterAggregateType()
333 {
334 nextRegister();
335 }
336
337 void Std140BlockEncoder::exitAggregateType()
338 {
339 nextRegister();
340 }
341
342 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
343 {
344 size_t baseAlignment = 0;
345 int matrixStride = 0;
346 int arrayStride = 0;
347
348 if(type.isMatrix())
349 {
350 baseAlignment = ComponentsPerRegister;
351 matrixStride = ComponentsPerRegister;
352
353 if(arraySize > 0)
354 {
355 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
356 arrayStride = ComponentsPerRegister * numRegisters;
357 }
358 }
359 else if(arraySize > 0)
360 {
361 baseAlignment = ComponentsPerRegister;
362 arrayStride = ComponentsPerRegister;
363 }
364 else
365 {
366 const size_t numComponents = type.getElementSize();
367 baseAlignment = (numComponents == 3 ? 4u : numComponents);
368 }
369
370 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
371
372 *matrixStrideOut = matrixStride;
373 *arrayStrideOut = arrayStride;
374 }
375
376 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
377 {
378 if(arraySize > 0)
379 {
380 mCurrentOffset += arrayStride * arraySize;
381 }
382 else if(type.isMatrix())
383 {
384 ASSERT(matrixStride == ComponentsPerRegister);
385 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
386 mCurrentOffset += ComponentsPerRegister * numRegisters;
387 }
388 else
389 {
390 mCurrentOffset += type.getElementSize();
391 }
392 }
393
394 Attribute::Attribute()
395 {
396 type = GL_NONE;
397 arraySize = 0;
398 registerIndex = 0;
399 }
400
401 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
402 {
403 this->type = type;
404 this->name = name;
405 this->arraySize = arraySize;
406 this->location = location;
407 this->registerIndex = registerIndex;
408 }
409
410 sw::PixelShader *Shader::getPixelShader() const
411 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500412 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400413 }
414
415 sw::VertexShader *Shader::getVertexShader() const
416 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500417 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400418 }
419
420 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
421 {
422 TString name = TFunction::unmangleName(nodeName);
423
424 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D")
425 {
426 method = IMPLICIT;
427 }
428 else if(name == "texture2DProj" || name == "textureProj")
429 {
430 method = IMPLICIT;
431 proj = true;
432 }
433 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
434 {
435 method = LOD;
436 }
437 else if(name == "texture2DProjLod" || name == "textureProjLod")
438 {
439 method = LOD;
440 proj = true;
441 }
442 else if(name == "textureSize")
443 {
444 method = SIZE;
445 }
446 else if(name == "textureOffset")
447 {
448 method = IMPLICIT;
449 offset = true;
450 }
451 else if(name == "textureProjOffset")
452 {
453 method = IMPLICIT;
454 offset = true;
455 proj = true;
456 }
457 else if(name == "textureLodOffset")
458 {
459 method = LOD;
460 offset = true;
461 }
462 else if(name == "textureProjLodOffset")
463 {
464 method = LOD;
465 proj = true;
466 offset = true;
467 }
468 else if(name == "texelFetch")
469 {
470 method = FETCH;
471 }
472 else if(name == "texelFetchOffset")
473 {
474 method = FETCH;
475 offset = true;
476 }
477 else if(name == "textureGrad")
478 {
479 method = GRAD;
480 }
481 else if(name == "textureGradOffset")
482 {
483 method = GRAD;
484 offset = true;
485 }
486 else if(name == "textureProjGrad")
487 {
488 method = GRAD;
489 proj = true;
490 }
491 else if(name == "textureProjGradOffset")
492 {
493 method = GRAD;
494 proj = true;
495 offset = true;
496 }
Alexis Hetu46768622018-01-16 22:09:28 -0500497 else if(name == "texture2DRect")
498 {
499 method = RECT;
500 }
501 else if(name == "texture2DRectProj")
502 {
503 method = RECT;
504 proj = true;
505 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400506 else UNREACHABLE(0);
507 }
508
509 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
510 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500511 shader = nullptr;
512 pixelShader = nullptr;
513 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400514
515 if(shaderObject)
516 {
517 shader = shaderObject->getShader();
518 pixelShader = shaderObject->getPixelShader();
519 vertexShader = shaderObject->getVertexShader();
520 }
521
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500522 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400523 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500524 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400525 }
526
527 OutputASM::~OutputASM()
528 {
529 }
530
531 void OutputASM::output()
532 {
533 if(shader)
534 {
535 emitShader(GLOBAL);
536
537 if(functionArray.size() > 1) // Only call main() when there are other functions
538 {
539 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
540 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
541 callMain->dst.index = 0; // main()
542
543 emit(sw::Shader::OPCODE_RET);
544 }
545
546 emitShader(FUNCTION);
547 }
548 }
549
550 void OutputASM::emitShader(Scope scope)
551 {
552 emitScope = scope;
553 currentScope = GLOBAL;
554 mContext.getTreeRoot()->traverse(this);
555 }
556
557 void OutputASM::freeTemporary(Temporary *temporary)
558 {
559 free(temporaries, temporary);
560 }
561
562 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
563 {
564 TBasicType baseType = in->getType().getBasicType();
565
566 switch(op)
567 {
568 case sw::Shader::OPCODE_NEG:
569 switch(baseType)
570 {
571 case EbtInt:
572 case EbtUInt:
573 return sw::Shader::OPCODE_INEG;
574 case EbtFloat:
575 default:
576 return op;
577 }
578 case sw::Shader::OPCODE_ABS:
579 switch(baseType)
580 {
581 case EbtInt:
582 return sw::Shader::OPCODE_IABS;
583 case EbtFloat:
584 default:
585 return op;
586 }
587 case sw::Shader::OPCODE_SGN:
588 switch(baseType)
589 {
590 case EbtInt:
591 return sw::Shader::OPCODE_ISGN;
592 case EbtFloat:
593 default:
594 return op;
595 }
596 case sw::Shader::OPCODE_ADD:
597 switch(baseType)
598 {
599 case EbtInt:
600 case EbtUInt:
601 return sw::Shader::OPCODE_IADD;
602 case EbtFloat:
603 default:
604 return op;
605 }
606 case sw::Shader::OPCODE_SUB:
607 switch(baseType)
608 {
609 case EbtInt:
610 case EbtUInt:
611 return sw::Shader::OPCODE_ISUB;
612 case EbtFloat:
613 default:
614 return op;
615 }
616 case sw::Shader::OPCODE_MUL:
617 switch(baseType)
618 {
619 case EbtInt:
620 case EbtUInt:
621 return sw::Shader::OPCODE_IMUL;
622 case EbtFloat:
623 default:
624 return op;
625 }
626 case sw::Shader::OPCODE_DIV:
627 switch(baseType)
628 {
629 case EbtInt:
630 return sw::Shader::OPCODE_IDIV;
631 case EbtUInt:
632 return sw::Shader::OPCODE_UDIV;
633 case EbtFloat:
634 default:
635 return op;
636 }
637 case sw::Shader::OPCODE_IMOD:
638 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
639 case sw::Shader::OPCODE_ISHR:
640 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
641 case sw::Shader::OPCODE_MIN:
642 switch(baseType)
643 {
644 case EbtInt:
645 return sw::Shader::OPCODE_IMIN;
646 case EbtUInt:
647 return sw::Shader::OPCODE_UMIN;
648 case EbtFloat:
649 default:
650 return op;
651 }
652 case sw::Shader::OPCODE_MAX:
653 switch(baseType)
654 {
655 case EbtInt:
656 return sw::Shader::OPCODE_IMAX;
657 case EbtUInt:
658 return sw::Shader::OPCODE_UMAX;
659 case EbtFloat:
660 default:
661 return op;
662 }
663 default:
664 return op;
665 }
666 }
667
668 void OutputASM::visitSymbol(TIntermSymbol *symbol)
669 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500670 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
671 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
672 switch(symbol->getQualifier())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400673 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500674 case EvqVaryingIn:
675 case EvqVaryingOut:
676 case EvqInvariantVaryingIn:
677 case EvqInvariantVaryingOut:
678 case EvqVertexOut:
679 case EvqFragmentIn:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400680 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
681 {
682 declareVarying(symbol, -1);
683 }
Nicolas Capens6896e352018-01-10 12:46:52 -0500684 break;
Alexis Hetu930df972018-01-30 16:54:13 -0500685 case EvqFragmentOut:
686 declareFragmentOutput(symbol);
687 break;
Nicolas Capens6896e352018-01-10 12:46:52 -0500688 default:
689 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400690 }
691
692 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
693 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
694 // "All members of a named uniform block declared with a shared or std140 layout qualifier
695 // are considered active, even if they are not referenced in any shader in the program.
696 // The uniform block itself is also considered active, even if no member of the block is referenced."
697 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
698 {
699 uniformRegister(symbol);
700 }
701 }
702
703 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
704 {
705 if(currentScope != emitScope)
706 {
707 return false;
708 }
709
710 TIntermTyped *result = node;
711 TIntermTyped *left = node->getLeft();
712 TIntermTyped *right = node->getRight();
713 const TType &leftType = left->getType();
714 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400715
716 if(isSamplerRegister(result))
717 {
718 return false; // Don't traverse, the register index is determined statically
719 }
720
721 switch(node->getOp())
722 {
723 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500724 assert(visit == PreVisit);
725 right->traverse(this);
726 assignLvalue(left, right);
727 copy(result, right);
728 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400729 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500730 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500731 // Constant arrays go into the constant register file.
732 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
733 {
734 for(int i = 0; i < left->totalRegisterCount(); i++)
735 {
736 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
737 }
738 }
739 else
740 {
741 right->traverse(this);
742 copy(left, right);
743 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500744 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400745 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500746 assert(visit == PreVisit);
747 right->traverse(this);
748 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400749 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500750 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400751 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500752
753 assignLvalue(left, result);
754 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400755 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500756 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400757 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500758 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400759 int size = leftType.getNominalSize();
760
761 for(int i = 0; i < size; i++)
762 {
763 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
764 dot->dst.mask = 1 << i;
765 }
766
767 assignLvalue(left, result);
768 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500769 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400770 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500771 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400772 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500773 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400774 int dim = leftType.getNominalSize();
775
776 for(int i = 0; i < dim; i++)
777 {
778 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
779 mul->src[1].swizzle = 0x00;
780
781 for(int j = 1; j < dim; j++)
782 {
783 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
784 mad->src[1].swizzle = j * 0x55;
785 }
786 }
787
788 assignLvalue(left, result);
789 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500790 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400791 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400792 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400793 case EOpIndexDirectStruct:
794 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500795 assert(visit == PreVisit);
796 evaluateRvalue(node);
797 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400798 case EOpVectorSwizzle:
799 if(visit == PostVisit)
800 {
801 int swizzle = 0;
802 TIntermAggregate *components = right->getAsAggregate();
803
804 if(components)
805 {
806 TIntermSequence &sequence = components->getSequence();
807 int component = 0;
808
809 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
810 {
811 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
812
813 if(element)
814 {
815 int i = element->getUnionArrayPointer()[0].getIConst();
816 swizzle |= i << (component * 2);
817 component++;
818 }
819 else UNREACHABLE(0);
820 }
821 }
822 else UNREACHABLE(0);
823
824 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
825 mov->src[0].swizzle = swizzle;
826 }
827 break;
828 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
829 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
830 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
831 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
832 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
833 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
834 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
835 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
836 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
837 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
838 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
839 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
840 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
841 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
842 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
843 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
844 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
845 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
846 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
847 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
848 case EOpEqual:
849 if(visit == PostVisit)
850 {
851 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
852
853 for(int index = 1; index < left->totalRegisterCount(); index++)
854 {
855 Temporary equal(this);
856 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
857 emit(sw::Shader::OPCODE_AND, result, result, &equal);
858 }
859 }
860 break;
861 case EOpNotEqual:
862 if(visit == PostVisit)
863 {
864 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
865
866 for(int index = 1; index < left->totalRegisterCount(); index++)
867 {
868 Temporary notEqual(this);
869 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
870 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
871 }
872 }
873 break;
874 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
875 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
876 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
877 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
878 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
879 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
880 case EOpMatrixTimesScalar:
881 if(visit == PostVisit)
882 {
883 if(left->isMatrix())
884 {
885 for(int i = 0; i < leftType.getNominalSize(); i++)
886 {
887 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
888 }
889 }
890 else if(right->isMatrix())
891 {
892 for(int i = 0; i < rightType.getNominalSize(); i++)
893 {
894 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
895 }
896 }
897 else UNREACHABLE(0);
898 }
899 break;
900 case EOpVectorTimesMatrix:
901 if(visit == PostVisit)
902 {
903 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
904
905 int size = rightType.getNominalSize();
906 for(int i = 0; i < size; i++)
907 {
908 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
909 dot->dst.mask = 1 << i;
910 }
911 }
912 break;
913 case EOpMatrixTimesVector:
914 if(visit == PostVisit)
915 {
916 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
917 mul->src[1].swizzle = 0x00;
918
919 int size = rightType.getNominalSize();
920 for(int i = 1; i < size; i++)
921 {
922 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
923 mad->src[1].swizzle = i * 0x55;
924 }
925 }
926 break;
927 case EOpMatrixTimesMatrix:
928 if(visit == PostVisit)
929 {
930 int dim = leftType.getNominalSize();
931
932 int size = rightType.getNominalSize();
933 for(int i = 0; i < size; i++)
934 {
935 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
936 mul->src[1].swizzle = 0x00;
937
938 for(int j = 1; j < dim; j++)
939 {
940 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
941 mad->src[1].swizzle = j * 0x55;
942 }
943 }
944 }
945 break;
946 case EOpLogicalOr:
947 if(trivial(right, 6))
948 {
949 if(visit == PostVisit)
950 {
951 emit(sw::Shader::OPCODE_OR, result, left, right);
952 }
953 }
954 else // Short-circuit evaluation
955 {
956 if(visit == InVisit)
957 {
958 emit(sw::Shader::OPCODE_MOV, result, left);
959 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
960 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
961 }
962 else if(visit == PostVisit)
963 {
964 emit(sw::Shader::OPCODE_MOV, result, right);
965 emit(sw::Shader::OPCODE_ENDIF);
966 }
967 }
968 break;
969 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
970 case EOpLogicalAnd:
971 if(trivial(right, 6))
972 {
973 if(visit == PostVisit)
974 {
975 emit(sw::Shader::OPCODE_AND, result, left, right);
976 }
977 }
978 else // Short-circuit evaluation
979 {
980 if(visit == InVisit)
981 {
982 emit(sw::Shader::OPCODE_MOV, result, left);
983 emit(sw::Shader::OPCODE_IF, 0, result);
984 }
985 else if(visit == PostVisit)
986 {
987 emit(sw::Shader::OPCODE_MOV, result, right);
988 emit(sw::Shader::OPCODE_ENDIF);
989 }
990 }
991 break;
992 default: UNREACHABLE(node->getOp());
993 }
994
995 return true;
996 }
997
998 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
999 {
1000 switch(size)
1001 {
1002 case 1: // Used for cofactor computation only
1003 {
1004 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1005 bool isMov = (row == col);
1006 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
1007 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
1008 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1009 mov->dst.mask = 1 << outRow;
1010 }
1011 break;
1012 case 2:
1013 {
1014 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1015
1016 bool isCofactor = (col >= 0) && (row >= 0);
1017 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1018 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1019 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1020
1021 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1022 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1023 det->dst.mask = 1 << outRow;
1024 }
1025 break;
1026 case 3:
1027 {
1028 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1029
1030 bool isCofactor = (col >= 0) && (row >= 0);
1031 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1032 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1033 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1034 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1035
1036 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1037 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1038 det->dst.mask = 1 << outRow;
1039 }
1040 break;
1041 case 4:
1042 {
1043 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1044 det->dst.mask = 1 << outRow;
1045 }
1046 break;
1047 default:
1048 UNREACHABLE(size);
1049 break;
1050 }
1051 }
1052
1053 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1054 {
1055 if(currentScope != emitScope)
1056 {
1057 return false;
1058 }
1059
1060 TIntermTyped *result = node;
1061 TIntermTyped *arg = node->getOperand();
1062 TBasicType basicType = arg->getType().getBasicType();
1063
1064 union
1065 {
1066 float f;
1067 int i;
1068 } one_value;
1069
1070 if(basicType == EbtInt || basicType == EbtUInt)
1071 {
1072 one_value.i = 1;
1073 }
1074 else
1075 {
1076 one_value.f = 1.0f;
1077 }
1078
1079 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1080 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1081 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1082
1083 switch(node->getOp())
1084 {
1085 case EOpNegative:
1086 if(visit == PostVisit)
1087 {
1088 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1089 for(int index = 0; index < arg->totalRegisterCount(); index++)
1090 {
1091 emit(negOpcode, result, index, arg, index);
1092 }
1093 }
1094 break;
1095 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1096 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001097 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001098 case EOpPostIncrement:
1099 if(visit == PostVisit)
1100 {
1101 copy(result, arg);
1102
1103 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1104 for(int index = 0; index < arg->totalRegisterCount(); index++)
1105 {
1106 emit(addOpcode, arg, index, arg, index, &one);
1107 }
1108
1109 assignLvalue(arg, arg);
1110 }
1111 break;
1112 case EOpPostDecrement:
1113 if(visit == PostVisit)
1114 {
1115 copy(result, arg);
1116
1117 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1118 for(int index = 0; index < arg->totalRegisterCount(); index++)
1119 {
1120 emit(subOpcode, arg, index, arg, index, &one);
1121 }
1122
1123 assignLvalue(arg, arg);
1124 }
1125 break;
1126 case EOpPreIncrement:
1127 if(visit == PostVisit)
1128 {
1129 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1130 for(int index = 0; index < arg->totalRegisterCount(); index++)
1131 {
1132 emit(addOpcode, result, index, arg, index, &one);
1133 }
1134
1135 assignLvalue(arg, result);
1136 }
1137 break;
1138 case EOpPreDecrement:
1139 if(visit == PostVisit)
1140 {
1141 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1142 for(int index = 0; index < arg->totalRegisterCount(); index++)
1143 {
1144 emit(subOpcode, result, index, arg, index, &one);
1145 }
1146
1147 assignLvalue(arg, result);
1148 }
1149 break;
1150 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1151 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1152 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1153 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1154 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1155 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1156 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1157 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1158 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1159 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1160 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1161 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1162 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1163 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1164 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1165 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1166 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1167 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1168 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1169 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1170 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1171 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1172 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1173 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1174 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1175 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1176 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1177 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1178 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1179 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1180 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1181 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1182 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1183 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1184 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1185 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1186 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1187 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1188 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1189 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1190 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1191 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1192 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1193 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1194 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1195 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1196 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1197 case EOpTranspose:
1198 if(visit == PostVisit)
1199 {
1200 int numCols = arg->getNominalSize();
1201 int numRows = arg->getSecondarySize();
1202 for(int i = 0; i < numCols; ++i)
1203 {
1204 for(int j = 0; j < numRows; ++j)
1205 {
1206 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1207 mov->src[0].swizzle = 0x55 * j;
1208 mov->dst.mask = 1 << i;
1209 }
1210 }
1211 }
1212 break;
1213 case EOpDeterminant:
1214 if(visit == PostVisit)
1215 {
1216 int size = arg->getNominalSize();
1217 ASSERT(size == arg->getSecondarySize());
1218
1219 emitDeterminant(result, arg, size);
1220 }
1221 break;
1222 case EOpInverse:
1223 if(visit == PostVisit)
1224 {
1225 int size = arg->getNominalSize();
1226 ASSERT(size == arg->getSecondarySize());
1227
1228 // Compute transposed matrix of cofactors
1229 for(int i = 0; i < size; ++i)
1230 {
1231 for(int j = 0; j < size; ++j)
1232 {
1233 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1234 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1235 emitDeterminant(result, arg, size - 1, j, i, i, j);
1236 }
1237 }
1238
1239 // Compute 1 / determinant
1240 Temporary invDet(this);
1241 emitDeterminant(&invDet, arg, size);
1242 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1243 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1244 div->src[1].swizzle = 0x00; // xxxx
1245
1246 // Divide transposed matrix of cofactors by determinant
1247 for(int i = 0; i < size; ++i)
1248 {
1249 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1250 }
1251 }
1252 break;
1253 default: UNREACHABLE(node->getOp());
1254 }
1255
1256 return true;
1257 }
1258
1259 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1260 {
1261 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1262 {
1263 return false;
1264 }
1265
1266 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1267
1268 TIntermTyped *result = node;
1269 const TType &resultType = node->getType();
1270 TIntermSequence &arg = node->getSequence();
1271 size_t argumentCount = arg.size();
1272
1273 switch(node->getOp())
1274 {
1275 case EOpSequence: break;
1276 case EOpDeclaration: break;
1277 case EOpInvariantDeclaration: break;
1278 case EOpPrototype: break;
1279 case EOpComma:
1280 if(visit == PostVisit)
1281 {
1282 copy(result, arg[1]);
1283 }
1284 break;
1285 case EOpFunction:
1286 if(visit == PreVisit)
1287 {
1288 const TString &name = node->getName();
1289
1290 if(emitScope == FUNCTION)
1291 {
1292 if(functionArray.size() > 1) // No need for a label when there's only main()
1293 {
1294 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1295 label->dst.type = sw::Shader::PARAMETER_LABEL;
1296
1297 const Function *function = findFunction(name);
1298 ASSERT(function); // Should have been added during global pass
1299 label->dst.index = function->label;
1300 currentFunction = function->label;
1301 }
1302 }
1303 else if(emitScope == GLOBAL)
1304 {
1305 if(name != "main(")
1306 {
1307 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1308 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1309 }
1310 }
1311 else UNREACHABLE(emitScope);
1312
1313 currentScope = FUNCTION;
1314 }
1315 else if(visit == PostVisit)
1316 {
1317 if(emitScope == FUNCTION)
1318 {
1319 if(functionArray.size() > 1) // No need to return when there's only main()
1320 {
1321 emit(sw::Shader::OPCODE_RET);
1322 }
1323 }
1324
1325 currentScope = GLOBAL;
1326 }
1327 break;
1328 case EOpFunctionCall:
1329 if(visit == PostVisit)
1330 {
1331 if(node->isUserDefined())
1332 {
1333 const TString &name = node->getName();
1334 const Function *function = findFunction(name);
1335
1336 if(!function)
1337 {
1338 mContext.error(node->getLine(), "function definition not found", name.c_str());
1339 return false;
1340 }
1341
1342 TIntermSequence &arguments = *function->arg;
1343
1344 for(size_t i = 0; i < argumentCount; i++)
1345 {
1346 TIntermTyped *in = arguments[i]->getAsTyped();
1347
1348 if(in->getQualifier() == EvqIn ||
1349 in->getQualifier() == EvqInOut ||
1350 in->getQualifier() == EvqConstReadOnly)
1351 {
1352 copy(in, arg[i]);
1353 }
1354 }
1355
1356 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1357 call->dst.type = sw::Shader::PARAMETER_LABEL;
1358 call->dst.index = function->label;
1359
1360 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1361 {
1362 copy(result, function->ret);
1363 }
1364
1365 for(size_t i = 0; i < argumentCount; i++)
1366 {
1367 TIntermTyped *argument = arguments[i]->getAsTyped();
1368 TIntermTyped *out = arg[i]->getAsTyped();
1369
1370 if(argument->getQualifier() == EvqOut ||
1371 argument->getQualifier() == EvqInOut)
1372 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001373 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001374 }
1375 }
1376 }
1377 else
1378 {
1379 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001380 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001381 TIntermTyped *t = arg[1]->getAsTyped();
1382
1383 Temporary coord(this);
1384
1385 if(textureFunction.proj)
1386 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001387 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1388 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1389 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001390
Nicolas Capens0484c792016-06-13 22:02:36 -04001391 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1392 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001393
1394 if(IsShadowSampler(s->getBasicType()))
1395 {
1396 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1397 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1398 mov->src[0].swizzle = 0xA4;
1399 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001400 }
1401 else
1402 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001403 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1404
1405 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1406 {
1407 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1408 mov->src[0].swizzle = 0xA4;
1409 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001410 }
1411
1412 switch(textureFunction.method)
1413 {
1414 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001415 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001416 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001417 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001418 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001419 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001420 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001421 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001422 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001423 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1424 }
1425 else UNREACHABLE(argumentCount);
1426 }
1427 else // Offset
1428 {
1429 if(argumentCount == 3)
1430 {
1431 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1432 }
1433 else if(argumentCount == 4) // Bias
1434 {
1435 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001436 }
1437 else UNREACHABLE(argumentCount);
1438 }
1439 break;
1440 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001441 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001442 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001443 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001444 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001445 else if(argumentCount == 4) // Offset
1446 {
1447 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1448 }
1449 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001450 break;
1451 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001452 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001453 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001454 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001455 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001456 else if(argumentCount == 4) // Offset
1457 {
1458 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1459 }
1460 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001461 break;
1462 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001463 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001464 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001465 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001466 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001467 else if(argumentCount == 5) // Offset
1468 {
1469 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1470 }
1471 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001472 break;
Alexis Hetu46768622018-01-16 22:09:28 -05001473 case TextureFunction::RECT:
1474 emit(sw::Shader::OPCODE_TEXRECT, result, &coord, s);
1475 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001476 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001477 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001478 break;
1479 default:
1480 UNREACHABLE(textureFunction.method);
1481 }
1482 }
1483 }
1484 break;
1485 case EOpParameters:
1486 break;
1487 case EOpConstructFloat:
1488 case EOpConstructVec2:
1489 case EOpConstructVec3:
1490 case EOpConstructVec4:
1491 case EOpConstructBool:
1492 case EOpConstructBVec2:
1493 case EOpConstructBVec3:
1494 case EOpConstructBVec4:
1495 case EOpConstructInt:
1496 case EOpConstructIVec2:
1497 case EOpConstructIVec3:
1498 case EOpConstructIVec4:
1499 case EOpConstructUInt:
1500 case EOpConstructUVec2:
1501 case EOpConstructUVec3:
1502 case EOpConstructUVec4:
1503 if(visit == PostVisit)
1504 {
1505 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001506 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1507 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001508 for(size_t i = 0; i < argumentCount; i++)
1509 {
1510 TIntermTyped *argi = arg[i]->getAsTyped();
1511 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001512 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1513 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001514
1515 if(!argi->isMatrix())
1516 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001517 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
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 component += size;
1522 }
1523 else // Matrix
1524 {
1525 int column = 0;
1526
1527 while(component < resultType.getNominalSize())
1528 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001529 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1530 mov->dst.mask = (0xF << swizzle) & 0xF;
1531 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001532
1533 column++;
1534 component += size;
1535 }
1536 }
1537 }
1538 }
1539 break;
1540 case EOpConstructMat2:
1541 case EOpConstructMat2x3:
1542 case EOpConstructMat2x4:
1543 case EOpConstructMat3x2:
1544 case EOpConstructMat3:
1545 case EOpConstructMat3x4:
1546 case EOpConstructMat4x2:
1547 case EOpConstructMat4x3:
1548 case EOpConstructMat4:
1549 if(visit == PostVisit)
1550 {
1551 TIntermTyped *arg0 = arg[0]->getAsTyped();
1552 const int outCols = result->getNominalSize();
1553 const int outRows = result->getSecondarySize();
1554
1555 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1556 {
1557 for(int i = 0; i < outCols; i++)
1558 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001559 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001560 Instruction *mov = emitCast(result, i, arg0, 0);
1561 mov->dst.mask = 1 << i;
1562 ASSERT(mov->src[0].swizzle == 0x00);
1563 }
1564 }
1565 else if(arg0->isMatrix())
1566 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001567 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001568
Alexis Hetu2a198552016-09-27 20:50:45 -04001569 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001570 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001571 TIntermTyped *argi = arg[n]->getAsTyped();
1572 const int inCols = argi->getNominalSize();
1573 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001574
Alexis Hetu2a198552016-09-27 20:50:45 -04001575 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001576 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001577 if(i >= inCols || outRows > inRows)
1578 {
1579 // Initialize to identity matrix
1580 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));
1581 emitCast(result, i + n * outCols, &col, 0);
1582 }
1583
1584 if(i < inCols)
1585 {
1586 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1587 mov->dst.mask = 0xF >> (4 - inRows);
1588 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001589 }
1590 }
1591 }
1592 else
1593 {
1594 int column = 0;
1595 int row = 0;
1596
1597 for(size_t i = 0; i < argumentCount; i++)
1598 {
1599 TIntermTyped *argi = arg[i]->getAsTyped();
1600 int size = argi->getNominalSize();
1601 int element = 0;
1602
1603 while(element < size)
1604 {
1605 Instruction *mov = emitCast(result, column, argi, 0);
1606 mov->dst.mask = (0xF << row) & 0xF;
1607 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1608
1609 int end = row + size - element;
1610 column = end >= outRows ? column + 1 : column;
1611 element = element + outRows - row;
1612 row = end >= outRows ? 0 : end;
1613 }
1614 }
1615 }
1616 }
1617 break;
1618 case EOpConstructStruct:
1619 if(visit == PostVisit)
1620 {
1621 int offset = 0;
1622 for(size_t i = 0; i < argumentCount; i++)
1623 {
1624 TIntermTyped *argi = arg[i]->getAsTyped();
1625 int size = argi->totalRegisterCount();
1626
1627 for(int index = 0; index < size; index++)
1628 {
1629 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1630 mov->dst.mask = writeMask(result, offset + index);
1631 }
1632
1633 offset += size;
1634 }
1635 }
1636 break;
1637 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1638 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1639 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1640 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1641 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1642 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1643 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1644 case EOpModf:
1645 if(visit == PostVisit)
1646 {
1647 TIntermTyped* arg1 = arg[1]->getAsTyped();
1648 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1649 assignLvalue(arg1, arg1);
1650 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1651 }
1652 break;
1653 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1654 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1655 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1656 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1657 case EOpClamp:
1658 if(visit == PostVisit)
1659 {
1660 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1661 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1662 }
1663 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001664 case EOpMix:
1665 if(visit == PostVisit)
1666 {
1667 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1668 {
1669 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1670 }
1671 else
1672 {
1673 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1674 }
1675 }
1676 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001677 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1678 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1679 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1680 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1681 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1682 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1683 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1684 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1685 case EOpMul:
1686 if(visit == PostVisit)
1687 {
1688 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001689 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1690 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001691
1692 int size = arg0->getNominalSize();
1693 for(int i = 0; i < size; i++)
1694 {
1695 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1696 }
1697 }
1698 break;
1699 case EOpOuterProduct:
1700 if(visit == PostVisit)
1701 {
1702 for(int i = 0; i < dim(arg[1]); i++)
1703 {
1704 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1705 mul->src[1].swizzle = 0x55 * i;
1706 }
1707 }
1708 break;
1709 default: UNREACHABLE(node->getOp());
1710 }
1711
1712 return true;
1713 }
1714
1715 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1716 {
1717 if(currentScope != emitScope)
1718 {
1719 return false;
1720 }
1721
1722 TIntermTyped *condition = node->getCondition();
1723 TIntermNode *trueBlock = node->getTrueBlock();
1724 TIntermNode *falseBlock = node->getFalseBlock();
1725 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1726
1727 condition->traverse(this);
1728
1729 if(node->usesTernaryOperator())
1730 {
1731 if(constantCondition)
1732 {
1733 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1734
1735 if(trueCondition)
1736 {
1737 trueBlock->traverse(this);
1738 copy(node, trueBlock);
1739 }
1740 else
1741 {
1742 falseBlock->traverse(this);
1743 copy(node, falseBlock);
1744 }
1745 }
1746 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1747 {
1748 trueBlock->traverse(this);
1749 falseBlock->traverse(this);
1750 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1751 }
1752 else
1753 {
1754 emit(sw::Shader::OPCODE_IF, 0, condition);
1755
1756 if(trueBlock)
1757 {
1758 trueBlock->traverse(this);
1759 copy(node, trueBlock);
1760 }
1761
1762 if(falseBlock)
1763 {
1764 emit(sw::Shader::OPCODE_ELSE);
1765 falseBlock->traverse(this);
1766 copy(node, falseBlock);
1767 }
1768
1769 emit(sw::Shader::OPCODE_ENDIF);
1770 }
1771 }
1772 else // if/else statement
1773 {
1774 if(constantCondition)
1775 {
1776 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1777
1778 if(trueCondition)
1779 {
1780 if(trueBlock)
1781 {
1782 trueBlock->traverse(this);
1783 }
1784 }
1785 else
1786 {
1787 if(falseBlock)
1788 {
1789 falseBlock->traverse(this);
1790 }
1791 }
1792 }
1793 else
1794 {
1795 emit(sw::Shader::OPCODE_IF, 0, condition);
1796
1797 if(trueBlock)
1798 {
1799 trueBlock->traverse(this);
1800 }
1801
1802 if(falseBlock)
1803 {
1804 emit(sw::Shader::OPCODE_ELSE);
1805 falseBlock->traverse(this);
1806 }
1807
1808 emit(sw::Shader::OPCODE_ENDIF);
1809 }
1810 }
1811
1812 return false;
1813 }
1814
1815 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1816 {
1817 if(currentScope != emitScope)
1818 {
1819 return false;
1820 }
1821
1822 unsigned int iterations = loopCount(node);
1823
1824 if(iterations == 0)
1825 {
1826 return false;
1827 }
1828
1829 bool unroll = (iterations <= 4);
1830
1831 if(unroll)
1832 {
1833 LoopUnrollable loopUnrollable;
1834 unroll = loopUnrollable.traverse(node);
1835 }
1836
1837 TIntermNode *init = node->getInit();
1838 TIntermTyped *condition = node->getCondition();
1839 TIntermTyped *expression = node->getExpression();
1840 TIntermNode *body = node->getBody();
1841 Constant True(true);
1842
1843 if(node->getType() == ELoopDoWhile)
1844 {
1845 Temporary iterate(this);
1846 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1847
1848 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1849
1850 if(body)
1851 {
1852 body->traverse(this);
1853 }
1854
1855 emit(sw::Shader::OPCODE_TEST);
1856
1857 condition->traverse(this);
1858 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1859
1860 emit(sw::Shader::OPCODE_ENDWHILE);
1861 }
1862 else
1863 {
1864 if(init)
1865 {
1866 init->traverse(this);
1867 }
1868
1869 if(unroll)
1870 {
1871 for(unsigned int i = 0; i < iterations; i++)
1872 {
1873 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1874
1875 if(body)
1876 {
1877 body->traverse(this);
1878 }
1879
1880 if(expression)
1881 {
1882 expression->traverse(this);
1883 }
1884 }
1885 }
1886 else
1887 {
1888 if(condition)
1889 {
1890 condition->traverse(this);
1891 }
1892 else
1893 {
1894 condition = &True;
1895 }
1896
1897 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1898
1899 if(body)
1900 {
1901 body->traverse(this);
1902 }
1903
1904 emit(sw::Shader::OPCODE_TEST);
1905
1906 if(expression)
1907 {
1908 expression->traverse(this);
1909 }
1910
1911 if(condition)
1912 {
1913 condition->traverse(this);
1914 }
1915
1916 emit(sw::Shader::OPCODE_ENDWHILE);
1917 }
1918 }
1919
1920 return false;
1921 }
1922
1923 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1924 {
1925 if(currentScope != emitScope)
1926 {
1927 return false;
1928 }
1929
1930 switch(node->getFlowOp())
1931 {
1932 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1933 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1934 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1935 case EOpReturn:
1936 if(visit == PostVisit)
1937 {
1938 TIntermTyped *value = node->getExpression();
1939
1940 if(value)
1941 {
1942 copy(functionArray[currentFunction].ret, value);
1943 }
1944
1945 emit(sw::Shader::OPCODE_LEAVE);
1946 }
1947 break;
1948 default: UNREACHABLE(node->getFlowOp());
1949 }
1950
1951 return true;
1952 }
1953
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001954 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1955 {
1956 if(currentScope != emitScope)
1957 {
1958 return false;
1959 }
1960
1961 TIntermTyped* switchValue = node->getInit();
1962 TIntermAggregate* opList = node->getStatementList();
1963
1964 if(!switchValue || !opList)
1965 {
1966 return false;
1967 }
1968
1969 switchValue->traverse(this);
1970
1971 emit(sw::Shader::OPCODE_SWITCH);
1972
1973 TIntermSequence& sequence = opList->getSequence();
1974 TIntermSequence::iterator it = sequence.begin();
1975 TIntermSequence::iterator defaultIt = sequence.end();
1976 int nbCases = 0;
1977 for(; it != sequence.end(); ++it)
1978 {
1979 TIntermCase* currentCase = (*it)->getAsCaseNode();
1980 if(currentCase)
1981 {
1982 TIntermSequence::iterator caseIt = it;
1983
1984 TIntermTyped* condition = currentCase->getCondition();
1985 if(condition) // non default case
1986 {
1987 if(nbCases != 0)
1988 {
1989 emit(sw::Shader::OPCODE_ELSE);
1990 }
1991
1992 condition->traverse(this);
1993 Temporary result(this);
1994 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1995 emit(sw::Shader::OPCODE_IF, 0, &result);
1996 nbCases++;
1997
Nicolas Capens6d123312018-01-08 12:57:52 -05001998 // Emit the code for this case and all subsequent cases until we hit a break statement.
1999 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002000 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2001 {
2002 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05002003
2004 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2005 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2006 // Note that this eliminates useless operations but shouldn't affect correctness.
2007 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002008 {
2009 break;
2010 }
2011 }
2012 }
2013 else
2014 {
2015 defaultIt = it; // The default case might not be the last case, keep it for last
2016 }
2017 }
2018 }
2019
2020 // If there's a default case, traverse it here
2021 if(defaultIt != sequence.end())
2022 {
2023 emit(sw::Shader::OPCODE_ELSE);
2024 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2025 {
2026 (*defaultIt)->traverse(this);
2027 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2028 {
2029 break;
2030 }
2031 }
2032 }
2033
2034 for(int i = 0; i < nbCases; ++i)
2035 {
2036 emit(sw::Shader::OPCODE_ENDIF);
2037 }
2038
2039 emit(sw::Shader::OPCODE_ENDSWITCH);
2040
2041 return false;
2042 }
2043
Nicolas Capens0bac2852016-05-07 06:09:58 -04002044 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2045 {
2046 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2047 }
2048
2049 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2050 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2051 {
2052 Instruction *instruction = new Instruction(op);
2053
2054 if(dst)
2055 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002056 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002057 }
2058
Alexis Hetu929c6b02017-11-07 16:04:25 -05002059 if(src0)
2060 {
2061 TIntermTyped* src = src0->getAsTyped();
2062 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2063 }
2064
Nicolas Capens0530b452017-11-15 16:39:47 -05002065 source(instruction->src[0], src0, index0);
2066 source(instruction->src[1], src1, index1);
2067 source(instruction->src[2], src2, index2);
2068 source(instruction->src[3], src3, index3);
2069 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002070
2071 shader->append(instruction);
2072
2073 return instruction;
2074 }
2075
2076 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2077 {
2078 return emitCast(dst, 0, src, 0);
2079 }
2080
2081 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2082 {
2083 switch(src->getBasicType())
2084 {
2085 case EbtBool:
2086 switch(dst->getBasicType())
2087 {
2088 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2089 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2090 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2091 default: break;
2092 }
2093 break;
2094 case EbtInt:
2095 switch(dst->getBasicType())
2096 {
2097 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2098 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2099 default: break;
2100 }
2101 break;
2102 case EbtUInt:
2103 switch(dst->getBasicType())
2104 {
2105 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2106 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2107 default: break;
2108 }
2109 break;
2110 case EbtFloat:
2111 switch(dst->getBasicType())
2112 {
2113 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2114 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2115 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2116 default: break;
2117 }
2118 break;
2119 default:
2120 break;
2121 }
2122
2123 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2124 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2125 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2126
2127 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2128 }
2129
2130 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2131 {
2132 for(int index = 0; index < dst->elementRegisterCount(); index++)
2133 {
2134 emit(op, dst, index, src0, index, src1, index, src2, index);
2135 }
2136 }
2137
2138 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2139 {
2140 emitBinary(op, result, src0, src1);
2141 assignLvalue(lhs, result);
2142 }
2143
2144 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2145 {
2146 sw::Shader::Opcode opcode;
2147 switch(left->getAsTyped()->getBasicType())
2148 {
2149 case EbtBool:
2150 case EbtInt:
2151 opcode = sw::Shader::OPCODE_ICMP;
2152 break;
2153 case EbtUInt:
2154 opcode = sw::Shader::OPCODE_UCMP;
2155 break;
2156 default:
2157 opcode = sw::Shader::OPCODE_CMP;
2158 break;
2159 }
2160
2161 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2162 cmp->control = cmpOp;
2163 }
2164
2165 int componentCount(const TType &type, int registers)
2166 {
2167 if(registers == 0)
2168 {
2169 return 0;
2170 }
2171
2172 if(type.isArray() && registers >= type.elementRegisterCount())
2173 {
2174 int index = registers / type.elementRegisterCount();
2175 registers -= index * type.elementRegisterCount();
2176 return index * type.getElementSize() + componentCount(type, registers);
2177 }
2178
2179 if(type.isStruct() || type.isInterfaceBlock())
2180 {
2181 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2182 int elements = 0;
2183
Alexis Hetuda163ed2018-01-03 16:36:14 -05002184 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002185 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002186 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002187
2188 if(fieldType.totalRegisterCount() <= registers)
2189 {
2190 registers -= fieldType.totalRegisterCount();
2191 elements += fieldType.getObjectSize();
2192 }
2193 else // Register within this field
2194 {
2195 return elements + componentCount(fieldType, registers);
2196 }
2197 }
2198 }
2199 else if(type.isMatrix())
2200 {
2201 return registers * type.registerSize();
2202 }
2203
2204 UNREACHABLE(0);
2205 return 0;
2206 }
2207
2208 int registerSize(const TType &type, int registers)
2209 {
2210 if(registers == 0)
2211 {
2212 if(type.isStruct())
2213 {
2214 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2215 }
2216 else if(type.isInterfaceBlock())
2217 {
2218 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2219 }
2220
2221 return type.registerSize();
2222 }
2223
2224 if(type.isArray() && registers >= type.elementRegisterCount())
2225 {
2226 int index = registers / type.elementRegisterCount();
2227 registers -= index * type.elementRegisterCount();
2228 return registerSize(type, registers);
2229 }
2230
2231 if(type.isStruct() || type.isInterfaceBlock())
2232 {
2233 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2234 int elements = 0;
2235
Alexis Hetuda163ed2018-01-03 16:36:14 -05002236 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002237 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002238 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002239
2240 if(fieldType.totalRegisterCount() <= registers)
2241 {
2242 registers -= fieldType.totalRegisterCount();
2243 elements += fieldType.getObjectSize();
2244 }
2245 else // Register within this field
2246 {
2247 return registerSize(fieldType, registers);
2248 }
2249 }
2250 }
2251 else if(type.isMatrix())
2252 {
2253 return registerSize(type, 0);
2254 }
2255
2256 UNREACHABLE(0);
2257 return 0;
2258 }
2259
2260 int OutputASM::getBlockId(TIntermTyped *arg)
2261 {
2262 if(arg)
2263 {
2264 const TType &type = arg->getType();
2265 TInterfaceBlock* block = type.getInterfaceBlock();
2266 if(block && (type.getQualifier() == EvqUniform))
2267 {
2268 // Make sure the uniform block is declared
2269 uniformRegister(arg);
2270
2271 const char* blockName = block->name().c_str();
2272
2273 // Fetch uniform block index from array of blocks
2274 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2275 {
2276 if(blockName == it->name)
2277 {
2278 return it->blockId;
2279 }
2280 }
2281
2282 ASSERT(false);
2283 }
2284 }
2285
2286 return -1;
2287 }
2288
2289 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2290 {
2291 const TType &type = arg->getType();
2292 int blockId = getBlockId(arg);
2293 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2294 if(blockId != -1)
2295 {
2296 argumentInfo.bufferIndex = 0;
2297 for(int i = 0; i < blockId; ++i)
2298 {
2299 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2300 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2301 }
2302
2303 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2304
2305 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2306 BlockDefinitionIndexMap::const_iterator it = itEnd;
2307
2308 argumentInfo.clampedIndex = index;
2309 if(type.isInterfaceBlock())
2310 {
2311 // Offset index to the beginning of the selected instance
2312 int blockRegisters = type.elementRegisterCount();
2313 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2314 argumentInfo.bufferIndex += bufferOffset;
2315 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2316 }
2317
2318 int regIndex = registerIndex(arg);
2319 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2320 {
2321 it = blockDefinition.find(i);
2322 if(it != itEnd)
2323 {
2324 argumentInfo.clampedIndex -= (i - regIndex);
2325 break;
2326 }
2327 }
2328 ASSERT(it != itEnd);
2329
2330 argumentInfo.typedMemberInfo = it->second;
2331
2332 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2333 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2334 }
2335 else
2336 {
2337 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2338 }
2339
2340 return argumentInfo;
2341 }
2342
Nicolas Capens0530b452017-11-15 16:39:47 -05002343 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002344 {
2345 if(argument)
2346 {
2347 TIntermTyped *arg = argument->getAsTyped();
2348 Temporary unpackedUniform(this);
2349
2350 const TType& srcType = arg->getType();
2351 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2352 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2353 {
2354 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2355 const TType &memberType = argumentInfo.typedMemberInfo.type;
2356
2357 if(memberType.getBasicType() == EbtBool)
2358 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002359 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002360
2361 // Convert the packed bool, which is currently an int, to a true bool
2362 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2363 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2364 instruction->dst.index = registerIndex(&unpackedUniform);
2365 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2366 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2367 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2368
2369 shader->append(instruction);
2370
2371 arg = &unpackedUniform;
2372 index = 0;
2373 }
Alexis Hetud2742532018-01-23 16:53:41 -05002374 else if((memberType.getLayoutQualifier().matrixPacking == EmpRowMajor) && memberType.isMatrix())
Nicolas Capens0bac2852016-05-07 06:09:58 -04002375 {
2376 int numCols = memberType.getNominalSize();
2377 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002378
Alexis Hetue97a31e2016-11-14 14:10:47 -05002379 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002380
2381 unsigned int dstIndex = registerIndex(&unpackedUniform);
2382 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2383 int arrayIndex = argumentInfo.clampedIndex / numCols;
2384 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2385
2386 for(int j = 0; j < numRows; ++j)
2387 {
2388 // Transpose the row major matrix
2389 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2390 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2391 instruction->dst.index = dstIndex;
2392 instruction->dst.mask = 1 << j;
2393 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2394 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2395 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2396 instruction->src[0].swizzle = srcSwizzle;
2397
2398 shader->append(instruction);
2399 }
2400
2401 arg = &unpackedUniform;
2402 index = 0;
2403 }
2404 }
2405
2406 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2407 const TType &type = argumentInfo.typedMemberInfo.type;
2408
2409 int size = registerSize(type, argumentInfo.clampedIndex);
2410
2411 parameter.type = registerType(arg);
2412 parameter.bufferIndex = argumentInfo.bufferIndex;
2413
2414 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2415 {
2416 int component = componentCount(type, argumentInfo.clampedIndex);
2417 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2418
2419 for(int i = 0; i < 4; i++)
2420 {
2421 if(size == 1) // Replicate
2422 {
2423 parameter.value[i] = constants[component + 0].getAsFloat();
2424 }
2425 else if(i < size)
2426 {
2427 parameter.value[i] = constants[component + i].getAsFloat();
2428 }
2429 else
2430 {
2431 parameter.value[i] = 0.0f;
2432 }
2433 }
2434 }
2435 else
2436 {
2437 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2438
2439 if(parameter.bufferIndex != -1)
2440 {
2441 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2442 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2443 }
2444 }
2445
2446 if(!IsSampler(arg->getBasicType()))
2447 {
2448 parameter.swizzle = readSwizzle(arg, size);
2449 }
2450 }
2451 }
2452
Nicolas Capens0530b452017-11-15 16:39:47 -05002453 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2454 {
2455 parameter.type = registerType(arg);
2456 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002457 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002458 }
2459
Nicolas Capens0bac2852016-05-07 06:09:58 -04002460 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2461 {
2462 for(int index = 0; index < dst->totalRegisterCount(); index++)
2463 {
2464 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002465 }
2466 }
2467
2468 int swizzleElement(int swizzle, int index)
2469 {
2470 return (swizzle >> (index * 2)) & 0x03;
2471 }
2472
2473 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2474 {
2475 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2476 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2477 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2478 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2479 }
2480
2481 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2482 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002483 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2484 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002485 {
2486 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2487 }
2488
2489 TIntermBinary *binary = dst->getAsBinaryNode();
2490
2491 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2492 {
2493 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2494
Nicolas Capens6986b282017-11-16 10:38:19 -05002495 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002496
2497 insert->src[0].type = insert->dst.type;
2498 insert->src[0].index = insert->dst.index;
2499 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002500 source(insert->src[1], src);
2501 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002502
2503 shader->append(insert);
2504 }
2505 else
2506 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002507 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2508
Nicolas Capens6986b282017-11-16 10:38:19 -05002509 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002510
Nicolas Capens0530b452017-11-15 16:39:47 -05002511 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002512 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2513
2514 shader->append(mov1);
2515
2516 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002517 {
2518 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2519
Nicolas Capens84249fd2017-11-09 11:20:51 -05002520 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002521 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002522 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002523
Nicolas Capens0530b452017-11-15 16:39:47 -05002524 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002525
2526 shader->append(mov);
2527 }
2528 }
2529 }
2530
Nicolas Capensd469de22017-11-16 10:42:20 -05002531 void OutputASM::evaluateRvalue(TIntermTyped *node)
2532 {
2533 TIntermBinary *binary = node->getAsBinaryNode();
2534
2535 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2536 {
2537 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2538
2539 destination(insert->dst, node);
2540
2541 Temporary address(this);
2542 unsigned char mask;
2543 TIntermTyped *root = nullptr;
2544 unsigned int offset = 0;
2545 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2546
2547 source(insert->src[0], root, offset);
2548 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2549
2550 source(insert->src[1], binary->getRight());
2551
2552 shader->append(insert);
2553 }
2554 else
2555 {
2556 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2557
2558 destination(mov1->dst, node, 0);
2559
2560 Temporary address(this);
2561 unsigned char mask;
2562 TIntermTyped *root = nullptr;
2563 unsigned int offset = 0;
2564 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2565
2566 source(mov1->src[0], root, offset);
2567 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2568
2569 shader->append(mov1);
2570
2571 for(int i = 1; i < node->totalRegisterCount(); i++)
2572 {
2573 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2574 mov->src[0].rel = mov1->src[0].rel;
2575 }
2576 }
2577 }
2578
Nicolas Capens6986b282017-11-16 10:38:19 -05002579 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002580 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002581 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002582 TIntermTyped *root = nullptr;
2583 unsigned int offset = 0;
2584 unsigned char mask = 0xF;
2585 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2586
2587 dst.type = registerType(root);
2588 dst.index = registerIndex(root) + offset;
2589 dst.mask = mask;
2590
2591 return swizzle;
2592 }
2593
2594 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2595 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002596 TIntermTyped *result = node;
2597 TIntermBinary *binary = node->getAsBinaryNode();
2598 TIntermSymbol *symbol = node->getAsSymbolNode();
2599
2600 if(binary)
2601 {
2602 TIntermTyped *left = binary->getLeft();
2603 TIntermTyped *right = binary->getRight();
2604
Nicolas Capens0530b452017-11-15 16:39:47 -05002605 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002606
2607 switch(binary->getOp())
2608 {
2609 case EOpIndexDirect:
2610 {
2611 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2612
2613 if(left->isRegister())
2614 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002615 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002616
Nicolas Capens0530b452017-11-15 16:39:47 -05002617 mask = 1;
2618 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002619 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002620 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002621 }
2622
2623 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002624 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002625
2626 return element;
2627 }
2628 else if(left->isArray() || left->isMatrix())
2629 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002630 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002631 return 0xE4;
2632 }
2633 else UNREACHABLE(0);
2634 }
2635 break;
2636 case EOpIndexIndirect:
2637 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002638 right->traverse(this);
2639
Nicolas Capens0bac2852016-05-07 06:09:58 -04002640 if(left->isRegister())
2641 {
2642 // Requires INSERT instruction (handled by calling function)
2643 }
2644 else if(left->isArray() || left->isMatrix())
2645 {
2646 int scale = result->totalRegisterCount();
2647
Nicolas Capens0530b452017-11-15 16:39:47 -05002648 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002649 {
2650 if(left->totalRegisterCount() > 1)
2651 {
2652 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002653 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002654
Nicolas Capens0530b452017-11-15 16:39:47 -05002655 rel.index = relativeRegister.index;
2656 rel.type = relativeRegister.type;
2657 rel.scale = scale;
2658 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002659 }
2660 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002661 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002662 {
2663 if(scale == 1)
2664 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002665 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002666 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002667 mad->src[0].index = rel.index;
2668 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002669 }
2670 else
2671 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002672 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002673 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002674 mul->src[0].index = rel.index;
2675 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002676
2677 Constant newScale(scale);
2678 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2679 }
2680
Nicolas Capens0530b452017-11-15 16:39:47 -05002681 rel.type = sw::Shader::PARAMETER_TEMP;
2682 rel.index = registerIndex(&address);
2683 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002684 }
2685 else // Just add the new index to the address register
2686 {
2687 if(scale == 1)
2688 {
2689 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2690 }
2691 else
2692 {
2693 Constant newScale(scale);
2694 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2695 }
2696 }
2697 }
2698 else UNREACHABLE(0);
2699 }
2700 break;
2701 case EOpIndexDirectStruct:
2702 case EOpIndexDirectInterfaceBlock:
2703 {
2704 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2705 left->getType().getStruct()->fields() :
2706 left->getType().getInterfaceBlock()->fields();
2707 int index = right->getAsConstantUnion()->getIConst(0);
2708 int fieldOffset = 0;
2709
2710 for(int i = 0; i < index; i++)
2711 {
2712 fieldOffset += fields[i]->type()->totalRegisterCount();
2713 }
2714
Nicolas Capens0530b452017-11-15 16:39:47 -05002715 offset += fieldOffset;
2716 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002717
2718 return 0xE4;
2719 }
2720 break;
2721 case EOpVectorSwizzle:
2722 {
2723 ASSERT(left->isRegister());
2724
Nicolas Capens0530b452017-11-15 16:39:47 -05002725 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002726
2727 int swizzle = 0;
2728 int rightMask = 0;
2729
2730 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2731
2732 for(unsigned int i = 0; i < sequence.size(); i++)
2733 {
2734 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2735
2736 int element = swizzleElement(leftSwizzle, index);
2737 rightMask = rightMask | (1 << element);
2738 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2739 }
2740
Nicolas Capens0530b452017-11-15 16:39:47 -05002741 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002742
2743 return swizzle;
2744 }
2745 break;
2746 default:
2747 UNREACHABLE(binary->getOp()); // Not an l-value operator
2748 break;
2749 }
2750 }
2751 else if(symbol)
2752 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002753 root = symbol;
2754 offset = 0;
2755 mask = writeMask(symbol);
2756
2757 return 0xE4;
2758 }
2759 else
2760 {
2761 node->traverse(this);
2762
2763 root = node;
2764 offset = 0;
2765 mask = writeMask(node);
2766
Nicolas Capens0bac2852016-05-07 06:09:58 -04002767 return 0xE4;
2768 }
2769
2770 return 0xE4;
2771 }
2772
2773 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2774 {
2775 if(isSamplerRegister(operand))
2776 {
2777 return sw::Shader::PARAMETER_SAMPLER;
2778 }
2779
2780 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002781 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002782 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002783 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2784 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002785 {
2786 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2787 }
2788 outputQualifier = qualifier;
2789 }
2790
2791 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2792 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002793 // Constant arrays are in the constant register file.
2794 if(operand->isArray() && operand->getArraySize() > 1)
2795 {
2796 return sw::Shader::PARAMETER_CONST;
2797 }
2798 else
2799 {
2800 return sw::Shader::PARAMETER_TEMP;
2801 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002802 }
2803
2804 switch(qualifier)
2805 {
2806 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2807 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2808 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2809 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2810 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2811 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2812 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2813 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2814 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2815 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2816 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2817 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2818 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2819 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2820 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2821 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2822 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2823 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2824 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2825 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2826 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2827 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2828 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2829 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2830 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2831 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002832 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002833 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2834 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2835 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2836 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2837 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2838 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2839 default: UNREACHABLE(qualifier);
2840 }
2841
2842 return sw::Shader::PARAMETER_VOID;
2843 }
2844
Alexis Hetu12b00502016-05-20 13:01:11 -04002845 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2846 {
2847 const TQualifier qualifier = operand->getQualifier();
2848 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2849 }
2850
Nicolas Capens0bac2852016-05-07 06:09:58 -04002851 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2852 {
2853 if(isSamplerRegister(operand))
2854 {
2855 return samplerRegister(operand);
2856 }
2857
2858 switch(operand->getQualifier())
2859 {
2860 case EvqTemporary: return temporaryRegister(operand);
2861 case EvqGlobal: return temporaryRegister(operand);
2862 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2863 case EvqAttribute: return attributeRegister(operand);
2864 case EvqVaryingIn: return varyingRegister(operand);
2865 case EvqVaryingOut: return varyingRegister(operand);
2866 case EvqVertexIn: return attributeRegister(operand);
2867 case EvqFragmentOut: return fragmentOutputRegister(operand);
2868 case EvqVertexOut: return varyingRegister(operand);
2869 case EvqFragmentIn: return varyingRegister(operand);
2870 case EvqInvariantVaryingIn: return varyingRegister(operand);
2871 case EvqInvariantVaryingOut: return varyingRegister(operand);
2872 case EvqSmooth: return varyingRegister(operand);
2873 case EvqFlat: return varyingRegister(operand);
2874 case EvqCentroidOut: return varyingRegister(operand);
2875 case EvqSmoothIn: return varyingRegister(operand);
2876 case EvqFlatIn: return varyingRegister(operand);
2877 case EvqCentroidIn: return varyingRegister(operand);
2878 case EvqUniform: return uniformRegister(operand);
2879 case EvqIn: return temporaryRegister(operand);
2880 case EvqOut: return temporaryRegister(operand);
2881 case EvqInOut: return temporaryRegister(operand);
2882 case EvqConstReadOnly: return temporaryRegister(operand);
2883 case EvqPosition: return varyingRegister(operand);
2884 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002885 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2886 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2887 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2888 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002889 case EvqPointCoord: return varyingRegister(operand);
2890 case EvqFragColor: return 0;
2891 case EvqFragData: return fragmentOutputRegister(operand);
2892 case EvqFragDepth: return 0;
2893 default: UNREACHABLE(operand->getQualifier());
2894 }
2895
2896 return 0;
2897 }
2898
2899 int OutputASM::writeMask(TIntermTyped *destination, int index)
2900 {
2901 if(destination->getQualifier() == EvqPointSize)
2902 {
2903 return 0x2; // Point size stored in the y component
2904 }
2905
2906 return 0xF >> (4 - registerSize(destination->getType(), index));
2907 }
2908
2909 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2910 {
2911 if(argument->getQualifier() == EvqPointSize)
2912 {
2913 return 0x55; // Point size stored in the y component
2914 }
2915
2916 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2917
2918 return swizzleSize[size];
2919 }
2920
2921 // Conservatively checks whether an expression is fast to compute and has no side effects
2922 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2923 {
2924 if(!expression->isRegister())
2925 {
2926 return false;
2927 }
2928
2929 return cost(expression, budget) >= 0;
2930 }
2931
2932 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2933 int OutputASM::cost(TIntermNode *expression, int budget)
2934 {
2935 if(budget < 0)
2936 {
2937 return budget;
2938 }
2939
2940 if(expression->getAsSymbolNode())
2941 {
2942 return budget;
2943 }
2944 else if(expression->getAsConstantUnion())
2945 {
2946 return budget;
2947 }
2948 else if(expression->getAsBinaryNode())
2949 {
2950 TIntermBinary *binary = expression->getAsBinaryNode();
2951
2952 switch(binary->getOp())
2953 {
2954 case EOpVectorSwizzle:
2955 case EOpIndexDirect:
2956 case EOpIndexDirectStruct:
2957 case EOpIndexDirectInterfaceBlock:
2958 return cost(binary->getLeft(), budget - 0);
2959 case EOpAdd:
2960 case EOpSub:
2961 case EOpMul:
2962 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2963 default:
2964 return -1;
2965 }
2966 }
2967 else if(expression->getAsUnaryNode())
2968 {
2969 TIntermUnary *unary = expression->getAsUnaryNode();
2970
2971 switch(unary->getOp())
2972 {
2973 case EOpAbs:
2974 case EOpNegative:
2975 return cost(unary->getOperand(), budget - 1);
2976 default:
2977 return -1;
2978 }
2979 }
2980 else if(expression->getAsSelectionNode())
2981 {
2982 TIntermSelection *selection = expression->getAsSelectionNode();
2983
2984 if(selection->usesTernaryOperator())
2985 {
2986 TIntermTyped *condition = selection->getCondition();
2987 TIntermNode *trueBlock = selection->getTrueBlock();
2988 TIntermNode *falseBlock = selection->getFalseBlock();
2989 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2990
2991 if(constantCondition)
2992 {
2993 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2994
2995 if(trueCondition)
2996 {
2997 return cost(trueBlock, budget - 0);
2998 }
2999 else
3000 {
3001 return cost(falseBlock, budget - 0);
3002 }
3003 }
3004 else
3005 {
3006 return cost(trueBlock, cost(falseBlock, budget - 2));
3007 }
3008 }
3009 }
3010
3011 return -1;
3012 }
3013
3014 const Function *OutputASM::findFunction(const TString &name)
3015 {
3016 for(unsigned int f = 0; f < functionArray.size(); f++)
3017 {
3018 if(functionArray[f].name == name)
3019 {
3020 return &functionArray[f];
3021 }
3022 }
3023
3024 return 0;
3025 }
3026
3027 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3028 {
3029 return allocate(temporaries, temporary);
3030 }
3031
Alexis Hetu49351232017-11-02 16:00:32 -04003032 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3033 {
3034 if(type.isStruct())
3035 {
3036 const TFieldList &fields = type.getStruct()->fields();
3037 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003038 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003039 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003040 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003041 setPixelShaderInputs(fieldType, fieldVar, flat);
3042 fieldVar += fieldType.totalRegisterCount();
3043 }
3044 }
3045 else
3046 {
3047 for(int i = 0; i < type.totalRegisterCount(); i++)
3048 {
3049 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3050 }
3051 }
3052 }
3053
Nicolas Capens0bac2852016-05-07 06:09:58 -04003054 int OutputASM::varyingRegister(TIntermTyped *varying)
3055 {
3056 int var = lookup(varyings, varying);
3057
3058 if(var == -1)
3059 {
3060 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003061 int registerCount = varying->totalRegisterCount();
3062
3063 if(pixelShader)
3064 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003065 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003066 {
3067 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3068 return 0;
3069 }
3070
3071 if(varying->getQualifier() == EvqPointCoord)
3072 {
3073 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003074 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003075 }
3076 else
3077 {
Alexis Hetu49351232017-11-02 16:00:32 -04003078 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003079 }
3080 }
3081 else if(vertexShader)
3082 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003083 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003084 {
3085 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3086 return 0;
3087 }
3088
3089 if(varying->getQualifier() == EvqPosition)
3090 {
3091 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003092 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003093 }
3094 else if(varying->getQualifier() == EvqPointSize)
3095 {
3096 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003097 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003098 }
3099 else
3100 {
3101 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3102 }
3103 }
3104 else UNREACHABLE(0);
3105
3106 declareVarying(varying, var);
3107 }
3108
3109 return var;
3110 }
3111
3112 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3113 {
3114 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3115 {
Alexis Hetu49351232017-11-02 16:00:32 -04003116 TIntermSymbol *symbol = varying->getAsSymbolNode();
3117 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3118 }
3119 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003120
Alexis Hetu49351232017-11-02 16:00:32 -04003121 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3122 {
3123 const char *name = varyingName.c_str();
3124 VaryingList &activeVaryings = shaderObject->varyings;
3125
3126 TStructure* structure = type.getStruct();
3127 if(structure)
3128 {
3129 int fieldRegisterIndex = registerIndex;
3130
3131 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003132 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003133 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003134 const TType& fieldType = *(field->type());
3135 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003136 if(fieldRegisterIndex >= 0)
3137 {
3138 fieldRegisterIndex += fieldType.totalRegisterCount();
3139 }
3140 }
3141 }
3142 else
3143 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003144 // Check if this varying has been declared before without having a register assigned
3145 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3146 {
3147 if(v->name == name)
3148 {
Alexis Hetu49351232017-11-02 16:00:32 -04003149 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003150 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003151 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3152 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003153 }
3154
3155 return;
3156 }
3157 }
3158
Alexis Hetu924513c2018-01-05 15:48:12 -05003159 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003160 }
3161 }
3162
Alexis Hetu930df972018-01-30 16:54:13 -05003163 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3164 {
3165 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
3166 if((requestedLocation >= 0) && (requestedLocation < sw::RENDERTARGETS))
3167 {
3168 if(fragmentOutputs.size() <= requestedLocation)
3169 {
3170 while(fragmentOutputs.size() < requestedLocation)
3171 {
3172 fragmentOutputs.push_back(nullptr);
3173 }
3174 fragmentOutputs.push_back(fragmentOutput);
3175 }
3176 else if(!fragmentOutputs[requestedLocation])
3177 {
3178 fragmentOutputs[requestedLocation] = fragmentOutput;
3179 }
3180 else
3181 {
3182 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3183 }
3184 }
3185 else if(requestedLocation >= sw::RENDERTARGETS)
3186 {
3187 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3188 }
3189 }
3190
Nicolas Capens0bac2852016-05-07 06:09:58 -04003191 int OutputASM::uniformRegister(TIntermTyped *uniform)
3192 {
3193 const TType &type = uniform->getType();
3194 ASSERT(!IsSampler(type.getBasicType()));
3195 TInterfaceBlock *block = type.getAsInterfaceBlock();
3196 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3197 ASSERT(symbol || block);
3198
3199 if(symbol || block)
3200 {
3201 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3202 bool isBlockMember = (!block && parentBlock);
3203 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3204
3205 if(index == -1 || isBlockMember)
3206 {
3207 if(index == -1)
3208 {
3209 index = allocate(uniforms, uniform);
3210 }
3211
3212 // Verify if the current uniform is a member of an already declared block
3213 const TString &name = symbol ? symbol->getSymbol() : block->name();
3214 int blockMemberIndex = blockMemberLookup(type, name, index);
3215 if(blockMemberIndex == -1)
3216 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003217 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003218 }
3219 else
3220 {
3221 index = blockMemberIndex;
3222 }
3223 }
3224
3225 return index;
3226 }
3227
3228 return 0;
3229 }
3230
3231 int OutputASM::attributeRegister(TIntermTyped *attribute)
3232 {
3233 ASSERT(!attribute->isArray());
3234
3235 int index = lookup(attributes, attribute);
3236
3237 if(index == -1)
3238 {
3239 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3240 ASSERT(symbol);
3241
3242 if(symbol)
3243 {
3244 index = allocate(attributes, attribute);
3245 const TType &type = attribute->getType();
3246 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003247 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3248 switch(type.getBasicType())
3249 {
3250 case EbtInt:
3251 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3252 break;
3253 case EbtUInt:
3254 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3255 break;
3256 case EbtFloat:
3257 default:
3258 break;
3259 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003260
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003261 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003262 {
3263 for(int i = 0; i < registerCount; i++)
3264 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003265 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003266 }
3267 }
3268
3269 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3270
3271 const char *name = symbol->getSymbol().c_str();
3272 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3273 }
3274 }
3275
3276 return index;
3277 }
3278
3279 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3280 {
3281 return allocate(fragmentOutputs, fragmentOutput);
3282 }
3283
3284 int OutputASM::samplerRegister(TIntermTyped *sampler)
3285 {
3286 const TType &type = sampler->getType();
3287 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3288
3289 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3290 TIntermBinary *binary = sampler->getAsBinaryNode();
3291
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003292 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003293 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003294 switch(type.getQualifier())
3295 {
3296 case EvqUniform:
3297 return samplerRegister(symbol);
3298 case EvqIn:
3299 case EvqConstReadOnly:
3300 // Function arguments are not (uniform) sampler registers
3301 return -1;
3302 default:
3303 UNREACHABLE(type.getQualifier());
3304 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003305 }
3306 else if(binary)
3307 {
3308 TIntermTyped *left = binary->getLeft();
3309 TIntermTyped *right = binary->getRight();
3310 const TType &leftType = left->getType();
3311 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3312 int offset = 0;
3313
3314 switch(binary->getOp())
3315 {
3316 case EOpIndexDirect:
3317 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003318 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003319 break;
3320 case EOpIndexDirectStruct:
3321 ASSERT(leftType.isStruct());
3322 {
3323 const TFieldList &fields = leftType.getStruct()->fields();
3324
3325 for(int i = 0; i < index; i++)
3326 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003327 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003328 }
3329 }
3330 break;
3331 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3332 return -1;
3333 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3334 default:
3335 UNREACHABLE(binary->getOp());
3336 return -1;
3337 }
3338
3339 int base = samplerRegister(left);
3340
3341 if(base < 0)
3342 {
3343 return -1;
3344 }
3345
3346 return base + offset;
3347 }
3348
3349 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003350 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003351 }
3352
3353 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3354 {
3355 const TType &type = sampler->getType();
3356 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3357
3358 int index = lookup(samplers, sampler);
3359
3360 if(index == -1)
3361 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003362 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003363
3364 if(sampler->getQualifier() == EvqUniform)
3365 {
3366 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003367 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003368 }
3369 }
3370
3371 return index;
3372 }
3373
3374 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3375 {
3376 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3377 }
3378
3379 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3380 {
3381 for(unsigned int i = 0; i < list.size(); i++)
3382 {
3383 if(list[i] == variable)
3384 {
3385 return i; // Pointer match
3386 }
3387 }
3388
3389 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3390 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3391
3392 if(varBlock)
3393 {
3394 for(unsigned int i = 0; i < list.size(); i++)
3395 {
3396 if(list[i])
3397 {
3398 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3399
3400 if(listBlock)
3401 {
3402 if(listBlock->name() == varBlock->name())
3403 {
3404 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3405 ASSERT(listBlock->fields() == varBlock->fields());
3406 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3407 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3408
3409 return i;
3410 }
3411 }
3412 }
3413 }
3414 }
3415 else if(varSymbol)
3416 {
3417 for(unsigned int i = 0; i < list.size(); i++)
3418 {
3419 if(list[i])
3420 {
3421 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3422
3423 if(listSymbol)
3424 {
3425 if(listSymbol->getId() == varSymbol->getId())
3426 {
3427 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3428 ASSERT(listSymbol->getType() == varSymbol->getType());
3429 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3430
3431 return i;
3432 }
3433 }
3434 }
3435 }
3436 }
3437
3438 return -1;
3439 }
3440
3441 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3442 {
3443 for(unsigned int i = 0; i < list.size(); i++)
3444 {
3445 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3446 {
3447 return i; // Pointer match
3448 }
3449 }
3450 return -1;
3451 }
3452
Alexis Hetuda163ed2018-01-03 16:36:14 -05003453 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003454 {
3455 int index = lookup(list, variable);
3456
3457 if(index == -1)
3458 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003459 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003460
3461 for(unsigned int i = 0; i < list.size(); i++)
3462 {
3463 if(list[i] == 0)
3464 {
3465 unsigned int j = 1;
3466 for( ; j < registerCount && (i + j) < list.size(); j++)
3467 {
3468 if(list[i + j] != 0)
3469 {
3470 break;
3471 }
3472 }
3473
3474 if(j == registerCount) // Found free slots
3475 {
3476 for(unsigned int j = 0; j < registerCount; j++)
3477 {
3478 list[i + j] = variable;
3479 }
3480
3481 return i;
3482 }
3483 }
3484 }
3485
3486 index = list.size();
3487
3488 for(unsigned int i = 0; i < registerCount; i++)
3489 {
3490 list.push_back(variable);
3491 }
3492 }
3493
3494 return index;
3495 }
3496
3497 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3498 {
3499 int index = lookup(list, variable);
3500
3501 if(index >= 0)
3502 {
3503 list[index] = 0;
3504 }
3505 }
3506
3507 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3508 {
3509 const TInterfaceBlock *block = type.getInterfaceBlock();
3510
3511 if(block)
3512 {
3513 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3514 const TFieldList& fields = block->fields();
3515 const TString &blockName = block->name();
3516 int fieldRegisterIndex = registerIndex;
3517
3518 if(!type.isInterfaceBlock())
3519 {
3520 // This is a uniform that's part of a block, let's see if the block is already defined
3521 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3522 {
3523 if(activeUniformBlocks[i].name == blockName.c_str())
3524 {
3525 // The block is already defined, find the register for the current uniform and return it
3526 for(size_t j = 0; j < fields.size(); j++)
3527 {
3528 const TString &fieldName = fields[j]->name();
3529 if(fieldName == name)
3530 {
3531 return fieldRegisterIndex;
3532 }
3533
3534 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3535 }
3536
3537 ASSERT(false);
3538 return fieldRegisterIndex;
3539 }
3540 }
3541 }
3542 }
3543
3544 return -1;
3545 }
3546
Alexis Hetuda163ed2018-01-03 16:36:14 -05003547 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003548 {
3549 const TStructure *structure = type.getStruct();
3550 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3551
3552 if(!structure && !block)
3553 {
3554 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3555 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3556 if(blockId >= 0)
3557 {
3558 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3559 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3560 }
3561 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003562 bool isSampler = IsSampler(type.getBasicType());
3563 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003564 {
3565 for(int i = 0; i < type.totalRegisterCount(); i++)
3566 {
3567 shader->declareSampler(fieldRegisterIndex + i);
3568 }
3569 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003570 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003571 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003572 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003573 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003574 }
3575 else if(block)
3576 {
3577 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3578 const TFieldList& fields = block->fields();
3579 const TString &blockName = block->name();
3580 int fieldRegisterIndex = registerIndex;
3581 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3582
3583 blockId = activeUniformBlocks.size();
3584 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3585 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3586 block->blockStorage(), isRowMajor, registerIndex, blockId));
3587 blockDefinitions.push_back(BlockDefinitionIndexMap());
3588
Alexis Hetud2742532018-01-23 16:53:41 -05003589 Std140BlockEncoder currentBlockEncoder;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003590 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003591 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003592 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003593 const TType &fieldType = *(field->type());
3594 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003595 if(isUniformBlockMember && (fieldName == name))
3596 {
3597 registerIndex = fieldRegisterIndex;
3598 }
3599
3600 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3601
Alexis Hetuda163ed2018-01-03 16:36:14 -05003602 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003603 fieldRegisterIndex += fieldType.totalRegisterCount();
3604 }
3605 currentBlockEncoder.exitAggregateType();
3606 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3607 }
3608 else
3609 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003610 // Store struct for program link time validation
3611 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3612
Nicolas Capens0bac2852016-05-07 06:09:58 -04003613 int fieldRegisterIndex = registerIndex;
3614
3615 const TFieldList& fields = structure->fields();
3616 if(type.isArray() && (structure || type.isInterfaceBlock()))
3617 {
3618 for(int i = 0; i < type.getArraySize(); i++)
3619 {
3620 if(encoder)
3621 {
3622 encoder->enterAggregateType();
3623 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003624 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003625 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003626 const TType &fieldType = *(field->type());
3627 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003628 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3629
Alexis Hetuda163ed2018-01-03 16:36:14 -05003630 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3631 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003632 }
3633 if(encoder)
3634 {
3635 encoder->exitAggregateType();
3636 }
3637 }
3638 }
3639 else
3640 {
3641 if(encoder)
3642 {
3643 encoder->enterAggregateType();
3644 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003645 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003646 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003647 const TType &fieldType = *(field->type());
3648 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003649 const TString uniformName = name + "." + fieldName;
3650
Alexis Hetuda163ed2018-01-03 16:36:14 -05003651 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3652 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003653 }
3654 if(encoder)
3655 {
3656 encoder->exitAggregateType();
3657 }
3658 }
3659 }
3660 }
3661
Nicolas Capens0bac2852016-05-07 06:09:58 -04003662 int OutputASM::dim(TIntermNode *v)
3663 {
3664 TIntermTyped *vector = v->getAsTyped();
3665 ASSERT(vector && vector->isRegister());
3666 return vector->getNominalSize();
3667 }
3668
3669 int OutputASM::dim2(TIntermNode *m)
3670 {
3671 TIntermTyped *matrix = m->getAsTyped();
3672 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3673 return matrix->getSecondarySize();
3674 }
3675
3676 // Returns ~0u if no loop count could be determined
3677 unsigned int OutputASM::loopCount(TIntermLoop *node)
3678 {
3679 // Parse loops of the form:
3680 // for(int index = initial; index [comparator] limit; index += increment)
3681 TIntermSymbol *index = 0;
3682 TOperator comparator = EOpNull;
3683 int initial = 0;
3684 int limit = 0;
3685 int increment = 0;
3686
3687 // Parse index name and intial value
3688 if(node->getInit())
3689 {
3690 TIntermAggregate *init = node->getInit()->getAsAggregate();
3691
3692 if(init)
3693 {
3694 TIntermSequence &sequence = init->getSequence();
3695 TIntermTyped *variable = sequence[0]->getAsTyped();
3696
Nicolas Capense3f05552017-05-24 10:45:56 -04003697 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003698 {
3699 TIntermBinary *assign = variable->getAsBinaryNode();
3700
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003701 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003702 {
3703 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3704 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3705
3706 if(symbol && constant)
3707 {
3708 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3709 {
3710 index = symbol;
3711 initial = constant->getUnionArrayPointer()[0].getIConst();
3712 }
3713 }
3714 }
3715 }
3716 }
3717 }
3718
3719 // Parse comparator and limit value
3720 if(index && node->getCondition())
3721 {
3722 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003723 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003724
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003725 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003726 {
3727 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3728
3729 if(constant)
3730 {
3731 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3732 {
3733 comparator = test->getOp();
3734 limit = constant->getUnionArrayPointer()[0].getIConst();
3735 }
3736 }
3737 }
3738 }
3739
3740 // Parse increment
3741 if(index && comparator != EOpNull && node->getExpression())
3742 {
3743 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3744 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3745
3746 if(binaryTerminal)
3747 {
3748 TOperator op = binaryTerminal->getOp();
3749 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3750
3751 if(constant)
3752 {
3753 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3754 {
3755 int value = constant->getUnionArrayPointer()[0].getIConst();
3756
3757 switch(op)
3758 {
3759 case EOpAddAssign: increment = value; break;
3760 case EOpSubAssign: increment = -value; break;
3761 default: UNIMPLEMENTED();
3762 }
3763 }
3764 }
3765 }
3766 else if(unaryTerminal)
3767 {
3768 TOperator op = unaryTerminal->getOp();
3769
3770 switch(op)
3771 {
3772 case EOpPostIncrement: increment = 1; break;
3773 case EOpPostDecrement: increment = -1; break;
3774 case EOpPreIncrement: increment = 1; break;
3775 case EOpPreDecrement: increment = -1; break;
3776 default: UNIMPLEMENTED();
3777 }
3778 }
3779 }
3780
3781 if(index && comparator != EOpNull && increment != 0)
3782 {
3783 if(comparator == EOpLessThanEqual)
3784 {
3785 comparator = EOpLessThan;
3786 limit += 1;
3787 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003788 else if(comparator == EOpGreaterThanEqual)
3789 {
3790 comparator = EOpLessThan;
3791 limit -= 1;
3792 std::swap(initial, limit);
3793 increment = -increment;
3794 }
3795 else if(comparator == EOpGreaterThan)
3796 {
3797 comparator = EOpLessThan;
3798 std::swap(initial, limit);
3799 increment = -increment;
3800 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003801
3802 if(comparator == EOpLessThan)
3803 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003804 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003805 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003806 return 0;
3807 }
3808
3809 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3810
3811 if(iterations < 0)
3812 {
3813 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003814 }
3815
3816 return iterations;
3817 }
3818 else UNIMPLEMENTED(); // Falls through
3819 }
3820
3821 return ~0u;
3822 }
3823
3824 bool LoopUnrollable::traverse(TIntermNode *node)
3825 {
3826 loopDepth = 0;
3827 loopUnrollable = true;
3828
3829 node->traverse(this);
3830
3831 return loopUnrollable;
3832 }
3833
3834 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3835 {
3836 if(visit == PreVisit)
3837 {
3838 loopDepth++;
3839 }
3840 else if(visit == PostVisit)
3841 {
3842 loopDepth++;
3843 }
3844
3845 return true;
3846 }
3847
3848 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3849 {
3850 if(!loopUnrollable)
3851 {
3852 return false;
3853 }
3854
3855 if(!loopDepth)
3856 {
3857 return true;
3858 }
3859
3860 switch(node->getFlowOp())
3861 {
3862 case EOpKill:
3863 case EOpReturn:
3864 break;
3865 case EOpBreak:
3866 case EOpContinue:
3867 loopUnrollable = false;
3868 break;
3869 default: UNREACHABLE(node->getFlowOp());
3870 }
3871
3872 return loopUnrollable;
3873 }
3874
3875 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3876 {
3877 return loopUnrollable;
3878 }
3879}