blob: 76a177af76cdbf2ad31ee14e16f6d731fab4b95a [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
288 BlockLayoutEncoder::BlockLayoutEncoder(bool rowMajor)
289 : mCurrentOffset(0), isRowMajor(rowMajor)
290 {
291 }
292
293 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
294 {
295 int arrayStride;
296 int matrixStride;
297
Alexis Hetubc648b92018-01-04 17:39:15 -0500298 bool isVariableRowMajor = isRowMajor;
299 TLayoutMatrixPacking matrixPacking = type.getLayoutQualifier().matrixPacking;
300 if(matrixPacking != EmpUnspecified)
301 {
302 isVariableRowMajor = (matrixPacking == EmpRowMajor);
303 }
304 getBlockLayoutInfo(type, type.getArraySize(), isVariableRowMajor, &arrayStride, &matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400305
306 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
307 static_cast<int>(arrayStride * BytesPerComponent),
308 static_cast<int>(matrixStride * BytesPerComponent),
Alexis Hetubc648b92018-01-04 17:39:15 -0500309 (matrixStride > 0) && isVariableRowMajor);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400310
Alexis Hetubc648b92018-01-04 17:39:15 -0500311 advanceOffset(type, type.getArraySize(), isVariableRowMajor, arrayStride, matrixStride);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400312
313 return memberInfo;
314 }
315
316 // static
317 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
318 {
319 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
320 }
321
322 // static
323 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
324 {
325 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
326 }
327
328 void BlockLayoutEncoder::nextRegister()
329 {
330 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
331 }
332
333 Std140BlockEncoder::Std140BlockEncoder(bool rowMajor) : BlockLayoutEncoder(rowMajor)
334 {
335 }
336
337 void Std140BlockEncoder::enterAggregateType()
338 {
339 nextRegister();
340 }
341
342 void Std140BlockEncoder::exitAggregateType()
343 {
344 nextRegister();
345 }
346
347 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
348 {
349 size_t baseAlignment = 0;
350 int matrixStride = 0;
351 int arrayStride = 0;
352
353 if(type.isMatrix())
354 {
355 baseAlignment = ComponentsPerRegister;
356 matrixStride = ComponentsPerRegister;
357
358 if(arraySize > 0)
359 {
360 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
361 arrayStride = ComponentsPerRegister * numRegisters;
362 }
363 }
364 else if(arraySize > 0)
365 {
366 baseAlignment = ComponentsPerRegister;
367 arrayStride = ComponentsPerRegister;
368 }
369 else
370 {
371 const size_t numComponents = type.getElementSize();
372 baseAlignment = (numComponents == 3 ? 4u : numComponents);
373 }
374
375 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
376
377 *matrixStrideOut = matrixStride;
378 *arrayStrideOut = arrayStride;
379 }
380
381 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
382 {
383 if(arraySize > 0)
384 {
385 mCurrentOffset += arrayStride * arraySize;
386 }
387 else if(type.isMatrix())
388 {
389 ASSERT(matrixStride == ComponentsPerRegister);
390 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
391 mCurrentOffset += ComponentsPerRegister * numRegisters;
392 }
393 else
394 {
395 mCurrentOffset += type.getElementSize();
396 }
397 }
398
399 Attribute::Attribute()
400 {
401 type = GL_NONE;
402 arraySize = 0;
403 registerIndex = 0;
404 }
405
406 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
407 {
408 this->type = type;
409 this->name = name;
410 this->arraySize = arraySize;
411 this->location = location;
412 this->registerIndex = registerIndex;
413 }
414
415 sw::PixelShader *Shader::getPixelShader() const
416 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500417 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400418 }
419
420 sw::VertexShader *Shader::getVertexShader() const
421 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500422 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400423 }
424
425 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
426 {
427 TString name = TFunction::unmangleName(nodeName);
428
429 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D")
430 {
431 method = IMPLICIT;
432 }
433 else if(name == "texture2DProj" || name == "textureProj")
434 {
435 method = IMPLICIT;
436 proj = true;
437 }
438 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
439 {
440 method = LOD;
441 }
442 else if(name == "texture2DProjLod" || name == "textureProjLod")
443 {
444 method = LOD;
445 proj = true;
446 }
447 else if(name == "textureSize")
448 {
449 method = SIZE;
450 }
451 else if(name == "textureOffset")
452 {
453 method = IMPLICIT;
454 offset = true;
455 }
456 else if(name == "textureProjOffset")
457 {
458 method = IMPLICIT;
459 offset = true;
460 proj = true;
461 }
462 else if(name == "textureLodOffset")
463 {
464 method = LOD;
465 offset = true;
466 }
467 else if(name == "textureProjLodOffset")
468 {
469 method = LOD;
470 proj = true;
471 offset = true;
472 }
473 else if(name == "texelFetch")
474 {
475 method = FETCH;
476 }
477 else if(name == "texelFetchOffset")
478 {
479 method = FETCH;
480 offset = true;
481 }
482 else if(name == "textureGrad")
483 {
484 method = GRAD;
485 }
486 else if(name == "textureGradOffset")
487 {
488 method = GRAD;
489 offset = true;
490 }
491 else if(name == "textureProjGrad")
492 {
493 method = GRAD;
494 proj = true;
495 }
496 else if(name == "textureProjGradOffset")
497 {
498 method = GRAD;
499 proj = true;
500 offset = true;
501 }
Alexis Hetu46768622018-01-16 22:09:28 -0500502 else if(name == "texture2DRect")
503 {
504 method = RECT;
505 }
506 else if(name == "texture2DRectProj")
507 {
508 method = RECT;
509 proj = true;
510 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400511 else UNREACHABLE(0);
512 }
513
514 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
515 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500516 shader = nullptr;
517 pixelShader = nullptr;
518 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400519
520 if(shaderObject)
521 {
522 shader = shaderObject->getShader();
523 pixelShader = shaderObject->getPixelShader();
524 vertexShader = shaderObject->getVertexShader();
525 }
526
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500527 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400528 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500529 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400530 }
531
532 OutputASM::~OutputASM()
533 {
534 }
535
536 void OutputASM::output()
537 {
538 if(shader)
539 {
540 emitShader(GLOBAL);
541
542 if(functionArray.size() > 1) // Only call main() when there are other functions
543 {
544 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
545 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
546 callMain->dst.index = 0; // main()
547
548 emit(sw::Shader::OPCODE_RET);
549 }
550
551 emitShader(FUNCTION);
552 }
553 }
554
555 void OutputASM::emitShader(Scope scope)
556 {
557 emitScope = scope;
558 currentScope = GLOBAL;
559 mContext.getTreeRoot()->traverse(this);
560 }
561
562 void OutputASM::freeTemporary(Temporary *temporary)
563 {
564 free(temporaries, temporary);
565 }
566
567 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
568 {
569 TBasicType baseType = in->getType().getBasicType();
570
571 switch(op)
572 {
573 case sw::Shader::OPCODE_NEG:
574 switch(baseType)
575 {
576 case EbtInt:
577 case EbtUInt:
578 return sw::Shader::OPCODE_INEG;
579 case EbtFloat:
580 default:
581 return op;
582 }
583 case sw::Shader::OPCODE_ABS:
584 switch(baseType)
585 {
586 case EbtInt:
587 return sw::Shader::OPCODE_IABS;
588 case EbtFloat:
589 default:
590 return op;
591 }
592 case sw::Shader::OPCODE_SGN:
593 switch(baseType)
594 {
595 case EbtInt:
596 return sw::Shader::OPCODE_ISGN;
597 case EbtFloat:
598 default:
599 return op;
600 }
601 case sw::Shader::OPCODE_ADD:
602 switch(baseType)
603 {
604 case EbtInt:
605 case EbtUInt:
606 return sw::Shader::OPCODE_IADD;
607 case EbtFloat:
608 default:
609 return op;
610 }
611 case sw::Shader::OPCODE_SUB:
612 switch(baseType)
613 {
614 case EbtInt:
615 case EbtUInt:
616 return sw::Shader::OPCODE_ISUB;
617 case EbtFloat:
618 default:
619 return op;
620 }
621 case sw::Shader::OPCODE_MUL:
622 switch(baseType)
623 {
624 case EbtInt:
625 case EbtUInt:
626 return sw::Shader::OPCODE_IMUL;
627 case EbtFloat:
628 default:
629 return op;
630 }
631 case sw::Shader::OPCODE_DIV:
632 switch(baseType)
633 {
634 case EbtInt:
635 return sw::Shader::OPCODE_IDIV;
636 case EbtUInt:
637 return sw::Shader::OPCODE_UDIV;
638 case EbtFloat:
639 default:
640 return op;
641 }
642 case sw::Shader::OPCODE_IMOD:
643 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
644 case sw::Shader::OPCODE_ISHR:
645 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
646 case sw::Shader::OPCODE_MIN:
647 switch(baseType)
648 {
649 case EbtInt:
650 return sw::Shader::OPCODE_IMIN;
651 case EbtUInt:
652 return sw::Shader::OPCODE_UMIN;
653 case EbtFloat:
654 default:
655 return op;
656 }
657 case sw::Shader::OPCODE_MAX:
658 switch(baseType)
659 {
660 case EbtInt:
661 return sw::Shader::OPCODE_IMAX;
662 case EbtUInt:
663 return sw::Shader::OPCODE_UMAX;
664 case EbtFloat:
665 default:
666 return op;
667 }
668 default:
669 return op;
670 }
671 }
672
673 void OutputASM::visitSymbol(TIntermSymbol *symbol)
674 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500675 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
676 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
677 switch(symbol->getQualifier())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400678 {
Nicolas Capens6896e352018-01-10 12:46:52 -0500679 case EvqVaryingIn:
680 case EvqVaryingOut:
681 case EvqInvariantVaryingIn:
682 case EvqInvariantVaryingOut:
683 case EvqVertexOut:
684 case EvqFragmentIn:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400685 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
686 {
687 declareVarying(symbol, -1);
688 }
Nicolas Capens6896e352018-01-10 12:46:52 -0500689 break;
Alexis Hetu930df972018-01-30 16:54:13 -0500690 case EvqFragmentOut:
691 declareFragmentOutput(symbol);
692 break;
Nicolas Capens6896e352018-01-10 12:46:52 -0500693 default:
694 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400695 }
696
697 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
698 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
699 // "All members of a named uniform block declared with a shared or std140 layout qualifier
700 // are considered active, even if they are not referenced in any shader in the program.
701 // The uniform block itself is also considered active, even if no member of the block is referenced."
702 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
703 {
704 uniformRegister(symbol);
705 }
706 }
707
708 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
709 {
710 if(currentScope != emitScope)
711 {
712 return false;
713 }
714
715 TIntermTyped *result = node;
716 TIntermTyped *left = node->getLeft();
717 TIntermTyped *right = node->getRight();
718 const TType &leftType = left->getType();
719 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720
721 if(isSamplerRegister(result))
722 {
723 return false; // Don't traverse, the register index is determined statically
724 }
725
726 switch(node->getOp())
727 {
728 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500729 assert(visit == PreVisit);
730 right->traverse(this);
731 assignLvalue(left, right);
732 copy(result, right);
733 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400734 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500735 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500736 // Constant arrays go into the constant register file.
737 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
738 {
739 for(int i = 0; i < left->totalRegisterCount(); i++)
740 {
741 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
742 }
743 }
744 else
745 {
746 right->traverse(this);
747 copy(left, right);
748 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500749 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400750 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500751 assert(visit == PreVisit);
752 right->traverse(this);
753 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400754 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500755 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400756 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500757
758 assignLvalue(left, result);
759 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400760 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500761 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400762 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500763 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400764 int size = leftType.getNominalSize();
765
766 for(int i = 0; i < size; i++)
767 {
768 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
769 dot->dst.mask = 1 << i;
770 }
771
772 assignLvalue(left, result);
773 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500774 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400775 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500776 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400777 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500778 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400779 int dim = leftType.getNominalSize();
780
781 for(int i = 0; i < dim; i++)
782 {
783 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
784 mul->src[1].swizzle = 0x00;
785
786 for(int j = 1; j < dim; j++)
787 {
788 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
789 mad->src[1].swizzle = j * 0x55;
790 }
791 }
792
793 assignLvalue(left, result);
794 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500795 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400796 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400797 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400798 case EOpIndexDirectStruct:
799 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500800 assert(visit == PreVisit);
801 evaluateRvalue(node);
802 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400803 case EOpVectorSwizzle:
804 if(visit == PostVisit)
805 {
806 int swizzle = 0;
807 TIntermAggregate *components = right->getAsAggregate();
808
809 if(components)
810 {
811 TIntermSequence &sequence = components->getSequence();
812 int component = 0;
813
814 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
815 {
816 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
817
818 if(element)
819 {
820 int i = element->getUnionArrayPointer()[0].getIConst();
821 swizzle |= i << (component * 2);
822 component++;
823 }
824 else UNREACHABLE(0);
825 }
826 }
827 else UNREACHABLE(0);
828
829 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
830 mov->src[0].swizzle = swizzle;
831 }
832 break;
833 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
834 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
835 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
836 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
837 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
838 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
839 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
840 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
841 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
842 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
843 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
844 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
845 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
846 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
847 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
848 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
849 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
850 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
851 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
852 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
853 case EOpEqual:
854 if(visit == PostVisit)
855 {
856 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
857
858 for(int index = 1; index < left->totalRegisterCount(); index++)
859 {
860 Temporary equal(this);
861 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
862 emit(sw::Shader::OPCODE_AND, result, result, &equal);
863 }
864 }
865 break;
866 case EOpNotEqual:
867 if(visit == PostVisit)
868 {
869 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
870
871 for(int index = 1; index < left->totalRegisterCount(); index++)
872 {
873 Temporary notEqual(this);
874 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
875 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
876 }
877 }
878 break;
879 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
880 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
881 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
882 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
883 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
884 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
885 case EOpMatrixTimesScalar:
886 if(visit == PostVisit)
887 {
888 if(left->isMatrix())
889 {
890 for(int i = 0; i < leftType.getNominalSize(); i++)
891 {
892 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
893 }
894 }
895 else if(right->isMatrix())
896 {
897 for(int i = 0; i < rightType.getNominalSize(); i++)
898 {
899 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
900 }
901 }
902 else UNREACHABLE(0);
903 }
904 break;
905 case EOpVectorTimesMatrix:
906 if(visit == PostVisit)
907 {
908 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
909
910 int size = rightType.getNominalSize();
911 for(int i = 0; i < size; i++)
912 {
913 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
914 dot->dst.mask = 1 << i;
915 }
916 }
917 break;
918 case EOpMatrixTimesVector:
919 if(visit == PostVisit)
920 {
921 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
922 mul->src[1].swizzle = 0x00;
923
924 int size = rightType.getNominalSize();
925 for(int i = 1; i < size; i++)
926 {
927 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
928 mad->src[1].swizzle = i * 0x55;
929 }
930 }
931 break;
932 case EOpMatrixTimesMatrix:
933 if(visit == PostVisit)
934 {
935 int dim = leftType.getNominalSize();
936
937 int size = rightType.getNominalSize();
938 for(int i = 0; i < size; i++)
939 {
940 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
941 mul->src[1].swizzle = 0x00;
942
943 for(int j = 1; j < dim; j++)
944 {
945 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
946 mad->src[1].swizzle = j * 0x55;
947 }
948 }
949 }
950 break;
951 case EOpLogicalOr:
952 if(trivial(right, 6))
953 {
954 if(visit == PostVisit)
955 {
956 emit(sw::Shader::OPCODE_OR, result, left, right);
957 }
958 }
959 else // Short-circuit evaluation
960 {
961 if(visit == InVisit)
962 {
963 emit(sw::Shader::OPCODE_MOV, result, left);
964 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
965 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
966 }
967 else if(visit == PostVisit)
968 {
969 emit(sw::Shader::OPCODE_MOV, result, right);
970 emit(sw::Shader::OPCODE_ENDIF);
971 }
972 }
973 break;
974 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
975 case EOpLogicalAnd:
976 if(trivial(right, 6))
977 {
978 if(visit == PostVisit)
979 {
980 emit(sw::Shader::OPCODE_AND, result, left, right);
981 }
982 }
983 else // Short-circuit evaluation
984 {
985 if(visit == InVisit)
986 {
987 emit(sw::Shader::OPCODE_MOV, result, left);
988 emit(sw::Shader::OPCODE_IF, 0, result);
989 }
990 else if(visit == PostVisit)
991 {
992 emit(sw::Shader::OPCODE_MOV, result, right);
993 emit(sw::Shader::OPCODE_ENDIF);
994 }
995 }
996 break;
997 default: UNREACHABLE(node->getOp());
998 }
999
1000 return true;
1001 }
1002
1003 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
1004 {
1005 switch(size)
1006 {
1007 case 1: // Used for cofactor computation only
1008 {
1009 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1010 bool isMov = (row == col);
1011 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
1012 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
1013 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1014 mov->dst.mask = 1 << outRow;
1015 }
1016 break;
1017 case 2:
1018 {
1019 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1020
1021 bool isCofactor = (col >= 0) && (row >= 0);
1022 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1023 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1024 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1025
1026 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1027 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1028 det->dst.mask = 1 << outRow;
1029 }
1030 break;
1031 case 3:
1032 {
1033 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1034
1035 bool isCofactor = (col >= 0) && (row >= 0);
1036 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1037 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1038 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1039 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1040
1041 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1042 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1043 det->dst.mask = 1 << outRow;
1044 }
1045 break;
1046 case 4:
1047 {
1048 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1049 det->dst.mask = 1 << outRow;
1050 }
1051 break;
1052 default:
1053 UNREACHABLE(size);
1054 break;
1055 }
1056 }
1057
1058 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1059 {
1060 if(currentScope != emitScope)
1061 {
1062 return false;
1063 }
1064
1065 TIntermTyped *result = node;
1066 TIntermTyped *arg = node->getOperand();
1067 TBasicType basicType = arg->getType().getBasicType();
1068
1069 union
1070 {
1071 float f;
1072 int i;
1073 } one_value;
1074
1075 if(basicType == EbtInt || basicType == EbtUInt)
1076 {
1077 one_value.i = 1;
1078 }
1079 else
1080 {
1081 one_value.f = 1.0f;
1082 }
1083
1084 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1085 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1086 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1087
1088 switch(node->getOp())
1089 {
1090 case EOpNegative:
1091 if(visit == PostVisit)
1092 {
1093 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1094 for(int index = 0; index < arg->totalRegisterCount(); index++)
1095 {
1096 emit(negOpcode, result, index, arg, index);
1097 }
1098 }
1099 break;
1100 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1101 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -04001102 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001103 case EOpPostIncrement:
1104 if(visit == PostVisit)
1105 {
1106 copy(result, arg);
1107
1108 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1109 for(int index = 0; index < arg->totalRegisterCount(); index++)
1110 {
1111 emit(addOpcode, arg, index, arg, index, &one);
1112 }
1113
1114 assignLvalue(arg, arg);
1115 }
1116 break;
1117 case EOpPostDecrement:
1118 if(visit == PostVisit)
1119 {
1120 copy(result, arg);
1121
1122 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1123 for(int index = 0; index < arg->totalRegisterCount(); index++)
1124 {
1125 emit(subOpcode, arg, index, arg, index, &one);
1126 }
1127
1128 assignLvalue(arg, arg);
1129 }
1130 break;
1131 case EOpPreIncrement:
1132 if(visit == PostVisit)
1133 {
1134 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1135 for(int index = 0; index < arg->totalRegisterCount(); index++)
1136 {
1137 emit(addOpcode, result, index, arg, index, &one);
1138 }
1139
1140 assignLvalue(arg, result);
1141 }
1142 break;
1143 case EOpPreDecrement:
1144 if(visit == PostVisit)
1145 {
1146 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1147 for(int index = 0; index < arg->totalRegisterCount(); index++)
1148 {
1149 emit(subOpcode, result, index, arg, index, &one);
1150 }
1151
1152 assignLvalue(arg, result);
1153 }
1154 break;
1155 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1156 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
1157 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1158 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1159 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1160 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1161 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1162 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1163 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1164 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1165 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1166 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1167 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1168 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1169 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1170 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1171 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1172 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1173 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1174 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1175 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1176 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1177 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1178 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1179 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1180 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1181 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1182 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1183 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1184 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1185 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1186 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1187 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1188 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1189 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1190 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1191 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1192 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1193 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1194 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1195 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1196 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1197 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1198 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1199 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1200 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1201 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1202 case EOpTranspose:
1203 if(visit == PostVisit)
1204 {
1205 int numCols = arg->getNominalSize();
1206 int numRows = arg->getSecondarySize();
1207 for(int i = 0; i < numCols; ++i)
1208 {
1209 for(int j = 0; j < numRows; ++j)
1210 {
1211 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1212 mov->src[0].swizzle = 0x55 * j;
1213 mov->dst.mask = 1 << i;
1214 }
1215 }
1216 }
1217 break;
1218 case EOpDeterminant:
1219 if(visit == PostVisit)
1220 {
1221 int size = arg->getNominalSize();
1222 ASSERT(size == arg->getSecondarySize());
1223
1224 emitDeterminant(result, arg, size);
1225 }
1226 break;
1227 case EOpInverse:
1228 if(visit == PostVisit)
1229 {
1230 int size = arg->getNominalSize();
1231 ASSERT(size == arg->getSecondarySize());
1232
1233 // Compute transposed matrix of cofactors
1234 for(int i = 0; i < size; ++i)
1235 {
1236 for(int j = 0; j < size; ++j)
1237 {
1238 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1239 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1240 emitDeterminant(result, arg, size - 1, j, i, i, j);
1241 }
1242 }
1243
1244 // Compute 1 / determinant
1245 Temporary invDet(this);
1246 emitDeterminant(&invDet, arg, size);
1247 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1248 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1249 div->src[1].swizzle = 0x00; // xxxx
1250
1251 // Divide transposed matrix of cofactors by determinant
1252 for(int i = 0; i < size; ++i)
1253 {
1254 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1255 }
1256 }
1257 break;
1258 default: UNREACHABLE(node->getOp());
1259 }
1260
1261 return true;
1262 }
1263
1264 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1265 {
1266 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1267 {
1268 return false;
1269 }
1270
1271 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1272
1273 TIntermTyped *result = node;
1274 const TType &resultType = node->getType();
1275 TIntermSequence &arg = node->getSequence();
1276 size_t argumentCount = arg.size();
1277
1278 switch(node->getOp())
1279 {
1280 case EOpSequence: break;
1281 case EOpDeclaration: break;
1282 case EOpInvariantDeclaration: break;
1283 case EOpPrototype: break;
1284 case EOpComma:
1285 if(visit == PostVisit)
1286 {
1287 copy(result, arg[1]);
1288 }
1289 break;
1290 case EOpFunction:
1291 if(visit == PreVisit)
1292 {
1293 const TString &name = node->getName();
1294
1295 if(emitScope == FUNCTION)
1296 {
1297 if(functionArray.size() > 1) // No need for a label when there's only main()
1298 {
1299 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1300 label->dst.type = sw::Shader::PARAMETER_LABEL;
1301
1302 const Function *function = findFunction(name);
1303 ASSERT(function); // Should have been added during global pass
1304 label->dst.index = function->label;
1305 currentFunction = function->label;
1306 }
1307 }
1308 else if(emitScope == GLOBAL)
1309 {
1310 if(name != "main(")
1311 {
1312 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1313 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1314 }
1315 }
1316 else UNREACHABLE(emitScope);
1317
1318 currentScope = FUNCTION;
1319 }
1320 else if(visit == PostVisit)
1321 {
1322 if(emitScope == FUNCTION)
1323 {
1324 if(functionArray.size() > 1) // No need to return when there's only main()
1325 {
1326 emit(sw::Shader::OPCODE_RET);
1327 }
1328 }
1329
1330 currentScope = GLOBAL;
1331 }
1332 break;
1333 case EOpFunctionCall:
1334 if(visit == PostVisit)
1335 {
1336 if(node->isUserDefined())
1337 {
1338 const TString &name = node->getName();
1339 const Function *function = findFunction(name);
1340
1341 if(!function)
1342 {
1343 mContext.error(node->getLine(), "function definition not found", name.c_str());
1344 return false;
1345 }
1346
1347 TIntermSequence &arguments = *function->arg;
1348
1349 for(size_t i = 0; i < argumentCount; i++)
1350 {
1351 TIntermTyped *in = arguments[i]->getAsTyped();
1352
1353 if(in->getQualifier() == EvqIn ||
1354 in->getQualifier() == EvqInOut ||
1355 in->getQualifier() == EvqConstReadOnly)
1356 {
1357 copy(in, arg[i]);
1358 }
1359 }
1360
1361 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1362 call->dst.type = sw::Shader::PARAMETER_LABEL;
1363 call->dst.index = function->label;
1364
1365 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1366 {
1367 copy(result, function->ret);
1368 }
1369
1370 for(size_t i = 0; i < argumentCount; i++)
1371 {
1372 TIntermTyped *argument = arguments[i]->getAsTyped();
1373 TIntermTyped *out = arg[i]->getAsTyped();
1374
1375 if(argument->getQualifier() == EvqOut ||
1376 argument->getQualifier() == EvqInOut)
1377 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001378 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001379 }
1380 }
1381 }
1382 else
1383 {
1384 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001385 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001386 TIntermTyped *t = arg[1]->getAsTyped();
1387
1388 Temporary coord(this);
1389
1390 if(textureFunction.proj)
1391 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001392 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1393 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1394 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001395
Nicolas Capens0484c792016-06-13 22:02:36 -04001396 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1397 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001398
1399 if(IsShadowSampler(s->getBasicType()))
1400 {
1401 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1402 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1403 mov->src[0].swizzle = 0xA4;
1404 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001405 }
1406 else
1407 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001408 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1409
1410 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1411 {
1412 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1413 mov->src[0].swizzle = 0xA4;
1414 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001415 }
1416
1417 switch(textureFunction.method)
1418 {
1419 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001420 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001421 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001422 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001423 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001424 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001425 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001426 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001427 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001428 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1429 }
1430 else UNREACHABLE(argumentCount);
1431 }
1432 else // Offset
1433 {
1434 if(argumentCount == 3)
1435 {
1436 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1437 }
1438 else if(argumentCount == 4) // Bias
1439 {
1440 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001441 }
1442 else UNREACHABLE(argumentCount);
1443 }
1444 break;
1445 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001446 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001447 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001448 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001449 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001450 else if(argumentCount == 4) // Offset
1451 {
1452 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1453 }
1454 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001455 break;
1456 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001457 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001458 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001459 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001460 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001461 else if(argumentCount == 4) // Offset
1462 {
1463 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1464 }
1465 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001466 break;
1467 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001468 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001469 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001470 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001471 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001472 else if(argumentCount == 5) // Offset
1473 {
1474 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1475 }
1476 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001477 break;
Alexis Hetu46768622018-01-16 22:09:28 -05001478 case TextureFunction::RECT:
1479 emit(sw::Shader::OPCODE_TEXRECT, result, &coord, s);
1480 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001481 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001482 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001483 break;
1484 default:
1485 UNREACHABLE(textureFunction.method);
1486 }
1487 }
1488 }
1489 break;
1490 case EOpParameters:
1491 break;
1492 case EOpConstructFloat:
1493 case EOpConstructVec2:
1494 case EOpConstructVec3:
1495 case EOpConstructVec4:
1496 case EOpConstructBool:
1497 case EOpConstructBVec2:
1498 case EOpConstructBVec3:
1499 case EOpConstructBVec4:
1500 case EOpConstructInt:
1501 case EOpConstructIVec2:
1502 case EOpConstructIVec3:
1503 case EOpConstructIVec4:
1504 case EOpConstructUInt:
1505 case EOpConstructUVec2:
1506 case EOpConstructUVec3:
1507 case EOpConstructUVec4:
1508 if(visit == PostVisit)
1509 {
1510 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001511 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1512 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001513 for(size_t i = 0; i < argumentCount; i++)
1514 {
1515 TIntermTyped *argi = arg[i]->getAsTyped();
1516 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001517 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1518 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001519
1520 if(!argi->isMatrix())
1521 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001522 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1523 mov->dst.mask = (0xF << swizzle) & 0xF;
1524 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001525
1526 component += size;
1527 }
1528 else // Matrix
1529 {
1530 int column = 0;
1531
1532 while(component < resultType.getNominalSize())
1533 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001534 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1535 mov->dst.mask = (0xF << swizzle) & 0xF;
1536 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001537
1538 column++;
1539 component += size;
1540 }
1541 }
1542 }
1543 }
1544 break;
1545 case EOpConstructMat2:
1546 case EOpConstructMat2x3:
1547 case EOpConstructMat2x4:
1548 case EOpConstructMat3x2:
1549 case EOpConstructMat3:
1550 case EOpConstructMat3x4:
1551 case EOpConstructMat4x2:
1552 case EOpConstructMat4x3:
1553 case EOpConstructMat4:
1554 if(visit == PostVisit)
1555 {
1556 TIntermTyped *arg0 = arg[0]->getAsTyped();
1557 const int outCols = result->getNominalSize();
1558 const int outRows = result->getSecondarySize();
1559
1560 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1561 {
1562 for(int i = 0; i < outCols; i++)
1563 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001564 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001565 Instruction *mov = emitCast(result, i, arg0, 0);
1566 mov->dst.mask = 1 << i;
1567 ASSERT(mov->src[0].swizzle == 0x00);
1568 }
1569 }
1570 else if(arg0->isMatrix())
1571 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001572 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001573
Alexis Hetu2a198552016-09-27 20:50:45 -04001574 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001575 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001576 TIntermTyped *argi = arg[n]->getAsTyped();
1577 const int inCols = argi->getNominalSize();
1578 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001579
Alexis Hetu2a198552016-09-27 20:50:45 -04001580 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001581 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001582 if(i >= inCols || outRows > inRows)
1583 {
1584 // Initialize to identity matrix
1585 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));
1586 emitCast(result, i + n * outCols, &col, 0);
1587 }
1588
1589 if(i < inCols)
1590 {
1591 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1592 mov->dst.mask = 0xF >> (4 - inRows);
1593 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001594 }
1595 }
1596 }
1597 else
1598 {
1599 int column = 0;
1600 int row = 0;
1601
1602 for(size_t i = 0; i < argumentCount; i++)
1603 {
1604 TIntermTyped *argi = arg[i]->getAsTyped();
1605 int size = argi->getNominalSize();
1606 int element = 0;
1607
1608 while(element < size)
1609 {
1610 Instruction *mov = emitCast(result, column, argi, 0);
1611 mov->dst.mask = (0xF << row) & 0xF;
1612 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1613
1614 int end = row + size - element;
1615 column = end >= outRows ? column + 1 : column;
1616 element = element + outRows - row;
1617 row = end >= outRows ? 0 : end;
1618 }
1619 }
1620 }
1621 }
1622 break;
1623 case EOpConstructStruct:
1624 if(visit == PostVisit)
1625 {
1626 int offset = 0;
1627 for(size_t i = 0; i < argumentCount; i++)
1628 {
1629 TIntermTyped *argi = arg[i]->getAsTyped();
1630 int size = argi->totalRegisterCount();
1631
1632 for(int index = 0; index < size; index++)
1633 {
1634 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1635 mov->dst.mask = writeMask(result, offset + index);
1636 }
1637
1638 offset += size;
1639 }
1640 }
1641 break;
1642 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1643 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1644 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1645 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1646 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1647 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1648 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1649 case EOpModf:
1650 if(visit == PostVisit)
1651 {
1652 TIntermTyped* arg1 = arg[1]->getAsTyped();
1653 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1654 assignLvalue(arg1, arg1);
1655 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1656 }
1657 break;
1658 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1659 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1660 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1661 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1662 case EOpClamp:
1663 if(visit == PostVisit)
1664 {
1665 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1666 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1667 }
1668 break;
Alexis Hetuc4711fa2018-01-12 11:59:35 -05001669 case EOpMix:
1670 if(visit == PostVisit)
1671 {
1672 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1673 {
1674 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1675 }
1676 else
1677 {
1678 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1679 }
1680 }
1681 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001682 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1683 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1684 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1685 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1686 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1687 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1688 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1689 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1690 case EOpMul:
1691 if(visit == PostVisit)
1692 {
1693 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001694 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1695 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001696
1697 int size = arg0->getNominalSize();
1698 for(int i = 0; i < size; i++)
1699 {
1700 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1701 }
1702 }
1703 break;
1704 case EOpOuterProduct:
1705 if(visit == PostVisit)
1706 {
1707 for(int i = 0; i < dim(arg[1]); i++)
1708 {
1709 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1710 mul->src[1].swizzle = 0x55 * i;
1711 }
1712 }
1713 break;
1714 default: UNREACHABLE(node->getOp());
1715 }
1716
1717 return true;
1718 }
1719
1720 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1721 {
1722 if(currentScope != emitScope)
1723 {
1724 return false;
1725 }
1726
1727 TIntermTyped *condition = node->getCondition();
1728 TIntermNode *trueBlock = node->getTrueBlock();
1729 TIntermNode *falseBlock = node->getFalseBlock();
1730 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1731
1732 condition->traverse(this);
1733
1734 if(node->usesTernaryOperator())
1735 {
1736 if(constantCondition)
1737 {
1738 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1739
1740 if(trueCondition)
1741 {
1742 trueBlock->traverse(this);
1743 copy(node, trueBlock);
1744 }
1745 else
1746 {
1747 falseBlock->traverse(this);
1748 copy(node, falseBlock);
1749 }
1750 }
1751 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1752 {
1753 trueBlock->traverse(this);
1754 falseBlock->traverse(this);
1755 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1756 }
1757 else
1758 {
1759 emit(sw::Shader::OPCODE_IF, 0, condition);
1760
1761 if(trueBlock)
1762 {
1763 trueBlock->traverse(this);
1764 copy(node, trueBlock);
1765 }
1766
1767 if(falseBlock)
1768 {
1769 emit(sw::Shader::OPCODE_ELSE);
1770 falseBlock->traverse(this);
1771 copy(node, falseBlock);
1772 }
1773
1774 emit(sw::Shader::OPCODE_ENDIF);
1775 }
1776 }
1777 else // if/else statement
1778 {
1779 if(constantCondition)
1780 {
1781 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1782
1783 if(trueCondition)
1784 {
1785 if(trueBlock)
1786 {
1787 trueBlock->traverse(this);
1788 }
1789 }
1790 else
1791 {
1792 if(falseBlock)
1793 {
1794 falseBlock->traverse(this);
1795 }
1796 }
1797 }
1798 else
1799 {
1800 emit(sw::Shader::OPCODE_IF, 0, condition);
1801
1802 if(trueBlock)
1803 {
1804 trueBlock->traverse(this);
1805 }
1806
1807 if(falseBlock)
1808 {
1809 emit(sw::Shader::OPCODE_ELSE);
1810 falseBlock->traverse(this);
1811 }
1812
1813 emit(sw::Shader::OPCODE_ENDIF);
1814 }
1815 }
1816
1817 return false;
1818 }
1819
1820 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1821 {
1822 if(currentScope != emitScope)
1823 {
1824 return false;
1825 }
1826
1827 unsigned int iterations = loopCount(node);
1828
1829 if(iterations == 0)
1830 {
1831 return false;
1832 }
1833
1834 bool unroll = (iterations <= 4);
1835
1836 if(unroll)
1837 {
1838 LoopUnrollable loopUnrollable;
1839 unroll = loopUnrollable.traverse(node);
1840 }
1841
1842 TIntermNode *init = node->getInit();
1843 TIntermTyped *condition = node->getCondition();
1844 TIntermTyped *expression = node->getExpression();
1845 TIntermNode *body = node->getBody();
1846 Constant True(true);
1847
1848 if(node->getType() == ELoopDoWhile)
1849 {
1850 Temporary iterate(this);
1851 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1852
1853 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1854
1855 if(body)
1856 {
1857 body->traverse(this);
1858 }
1859
1860 emit(sw::Shader::OPCODE_TEST);
1861
1862 condition->traverse(this);
1863 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1864
1865 emit(sw::Shader::OPCODE_ENDWHILE);
1866 }
1867 else
1868 {
1869 if(init)
1870 {
1871 init->traverse(this);
1872 }
1873
1874 if(unroll)
1875 {
1876 for(unsigned int i = 0; i < iterations; i++)
1877 {
1878 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1879
1880 if(body)
1881 {
1882 body->traverse(this);
1883 }
1884
1885 if(expression)
1886 {
1887 expression->traverse(this);
1888 }
1889 }
1890 }
1891 else
1892 {
1893 if(condition)
1894 {
1895 condition->traverse(this);
1896 }
1897 else
1898 {
1899 condition = &True;
1900 }
1901
1902 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1903
1904 if(body)
1905 {
1906 body->traverse(this);
1907 }
1908
1909 emit(sw::Shader::OPCODE_TEST);
1910
1911 if(expression)
1912 {
1913 expression->traverse(this);
1914 }
1915
1916 if(condition)
1917 {
1918 condition->traverse(this);
1919 }
1920
1921 emit(sw::Shader::OPCODE_ENDWHILE);
1922 }
1923 }
1924
1925 return false;
1926 }
1927
1928 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1929 {
1930 if(currentScope != emitScope)
1931 {
1932 return false;
1933 }
1934
1935 switch(node->getFlowOp())
1936 {
1937 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1938 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1939 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1940 case EOpReturn:
1941 if(visit == PostVisit)
1942 {
1943 TIntermTyped *value = node->getExpression();
1944
1945 if(value)
1946 {
1947 copy(functionArray[currentFunction].ret, value);
1948 }
1949
1950 emit(sw::Shader::OPCODE_LEAVE);
1951 }
1952 break;
1953 default: UNREACHABLE(node->getFlowOp());
1954 }
1955
1956 return true;
1957 }
1958
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001959 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1960 {
1961 if(currentScope != emitScope)
1962 {
1963 return false;
1964 }
1965
1966 TIntermTyped* switchValue = node->getInit();
1967 TIntermAggregate* opList = node->getStatementList();
1968
1969 if(!switchValue || !opList)
1970 {
1971 return false;
1972 }
1973
1974 switchValue->traverse(this);
1975
1976 emit(sw::Shader::OPCODE_SWITCH);
1977
1978 TIntermSequence& sequence = opList->getSequence();
1979 TIntermSequence::iterator it = sequence.begin();
1980 TIntermSequence::iterator defaultIt = sequence.end();
1981 int nbCases = 0;
1982 for(; it != sequence.end(); ++it)
1983 {
1984 TIntermCase* currentCase = (*it)->getAsCaseNode();
1985 if(currentCase)
1986 {
1987 TIntermSequence::iterator caseIt = it;
1988
1989 TIntermTyped* condition = currentCase->getCondition();
1990 if(condition) // non default case
1991 {
1992 if(nbCases != 0)
1993 {
1994 emit(sw::Shader::OPCODE_ELSE);
1995 }
1996
1997 condition->traverse(this);
1998 Temporary result(this);
1999 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
2000 emit(sw::Shader::OPCODE_IF, 0, &result);
2001 nbCases++;
2002
Nicolas Capens6d123312018-01-08 12:57:52 -05002003 // Emit the code for this case and all subsequent cases until we hit a break statement.
2004 // TODO: This can repeat a lot of code for switches with many fall-through cases.
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002005 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2006 {
2007 (*caseIt)->traverse(this);
Nicolas Capens6d123312018-01-08 12:57:52 -05002008
2009 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2010 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2011 // Note that this eliminates useless operations but shouldn't affect correctness.
2012 if((*caseIt)->getAsBranchNode())
Alexis Hetu9aa83a92016-05-02 17:34:46 -04002013 {
2014 break;
2015 }
2016 }
2017 }
2018 else
2019 {
2020 defaultIt = it; // The default case might not be the last case, keep it for last
2021 }
2022 }
2023 }
2024
2025 // If there's a default case, traverse it here
2026 if(defaultIt != sequence.end())
2027 {
2028 emit(sw::Shader::OPCODE_ELSE);
2029 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2030 {
2031 (*defaultIt)->traverse(this);
2032 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2033 {
2034 break;
2035 }
2036 }
2037 }
2038
2039 for(int i = 0; i < nbCases; ++i)
2040 {
2041 emit(sw::Shader::OPCODE_ENDIF);
2042 }
2043
2044 emit(sw::Shader::OPCODE_ENDSWITCH);
2045
2046 return false;
2047 }
2048
Nicolas Capens0bac2852016-05-07 06:09:58 -04002049 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2050 {
2051 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2052 }
2053
2054 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2055 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2056 {
2057 Instruction *instruction = new Instruction(op);
2058
2059 if(dst)
2060 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002061 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002062 }
2063
Alexis Hetu929c6b02017-11-07 16:04:25 -05002064 if(src0)
2065 {
2066 TIntermTyped* src = src0->getAsTyped();
2067 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2068 }
2069
Nicolas Capens0530b452017-11-15 16:39:47 -05002070 source(instruction->src[0], src0, index0);
2071 source(instruction->src[1], src1, index1);
2072 source(instruction->src[2], src2, index2);
2073 source(instruction->src[3], src3, index3);
2074 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002075
2076 shader->append(instruction);
2077
2078 return instruction;
2079 }
2080
2081 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2082 {
2083 return emitCast(dst, 0, src, 0);
2084 }
2085
2086 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2087 {
2088 switch(src->getBasicType())
2089 {
2090 case EbtBool:
2091 switch(dst->getBasicType())
2092 {
2093 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2094 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2095 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2096 default: break;
2097 }
2098 break;
2099 case EbtInt:
2100 switch(dst->getBasicType())
2101 {
2102 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2103 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2104 default: break;
2105 }
2106 break;
2107 case EbtUInt:
2108 switch(dst->getBasicType())
2109 {
2110 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2111 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2112 default: break;
2113 }
2114 break;
2115 case EbtFloat:
2116 switch(dst->getBasicType())
2117 {
2118 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2119 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2120 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2121 default: break;
2122 }
2123 break;
2124 default:
2125 break;
2126 }
2127
2128 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2129 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2130 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2131
2132 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2133 }
2134
2135 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2136 {
2137 for(int index = 0; index < dst->elementRegisterCount(); index++)
2138 {
2139 emit(op, dst, index, src0, index, src1, index, src2, index);
2140 }
2141 }
2142
2143 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2144 {
2145 emitBinary(op, result, src0, src1);
2146 assignLvalue(lhs, result);
2147 }
2148
2149 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2150 {
2151 sw::Shader::Opcode opcode;
2152 switch(left->getAsTyped()->getBasicType())
2153 {
2154 case EbtBool:
2155 case EbtInt:
2156 opcode = sw::Shader::OPCODE_ICMP;
2157 break;
2158 case EbtUInt:
2159 opcode = sw::Shader::OPCODE_UCMP;
2160 break;
2161 default:
2162 opcode = sw::Shader::OPCODE_CMP;
2163 break;
2164 }
2165
2166 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2167 cmp->control = cmpOp;
2168 }
2169
2170 int componentCount(const TType &type, int registers)
2171 {
2172 if(registers == 0)
2173 {
2174 return 0;
2175 }
2176
2177 if(type.isArray() && registers >= type.elementRegisterCount())
2178 {
2179 int index = registers / type.elementRegisterCount();
2180 registers -= index * type.elementRegisterCount();
2181 return index * type.getElementSize() + componentCount(type, registers);
2182 }
2183
2184 if(type.isStruct() || type.isInterfaceBlock())
2185 {
2186 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2187 int elements = 0;
2188
Alexis Hetuda163ed2018-01-03 16:36:14 -05002189 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002190 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002191 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002192
2193 if(fieldType.totalRegisterCount() <= registers)
2194 {
2195 registers -= fieldType.totalRegisterCount();
2196 elements += fieldType.getObjectSize();
2197 }
2198 else // Register within this field
2199 {
2200 return elements + componentCount(fieldType, registers);
2201 }
2202 }
2203 }
2204 else if(type.isMatrix())
2205 {
2206 return registers * type.registerSize();
2207 }
2208
2209 UNREACHABLE(0);
2210 return 0;
2211 }
2212
2213 int registerSize(const TType &type, int registers)
2214 {
2215 if(registers == 0)
2216 {
2217 if(type.isStruct())
2218 {
2219 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2220 }
2221 else if(type.isInterfaceBlock())
2222 {
2223 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2224 }
2225
2226 return type.registerSize();
2227 }
2228
2229 if(type.isArray() && registers >= type.elementRegisterCount())
2230 {
2231 int index = registers / type.elementRegisterCount();
2232 registers -= index * type.elementRegisterCount();
2233 return registerSize(type, registers);
2234 }
2235
2236 if(type.isStruct() || type.isInterfaceBlock())
2237 {
2238 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2239 int elements = 0;
2240
Alexis Hetuda163ed2018-01-03 16:36:14 -05002241 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002242 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002243 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002244
2245 if(fieldType.totalRegisterCount() <= registers)
2246 {
2247 registers -= fieldType.totalRegisterCount();
2248 elements += fieldType.getObjectSize();
2249 }
2250 else // Register within this field
2251 {
2252 return registerSize(fieldType, registers);
2253 }
2254 }
2255 }
2256 else if(type.isMatrix())
2257 {
2258 return registerSize(type, 0);
2259 }
2260
2261 UNREACHABLE(0);
2262 return 0;
2263 }
2264
2265 int OutputASM::getBlockId(TIntermTyped *arg)
2266 {
2267 if(arg)
2268 {
2269 const TType &type = arg->getType();
2270 TInterfaceBlock* block = type.getInterfaceBlock();
2271 if(block && (type.getQualifier() == EvqUniform))
2272 {
2273 // Make sure the uniform block is declared
2274 uniformRegister(arg);
2275
2276 const char* blockName = block->name().c_str();
2277
2278 // Fetch uniform block index from array of blocks
2279 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2280 {
2281 if(blockName == it->name)
2282 {
2283 return it->blockId;
2284 }
2285 }
2286
2287 ASSERT(false);
2288 }
2289 }
2290
2291 return -1;
2292 }
2293
2294 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2295 {
2296 const TType &type = arg->getType();
2297 int blockId = getBlockId(arg);
2298 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2299 if(blockId != -1)
2300 {
2301 argumentInfo.bufferIndex = 0;
2302 for(int i = 0; i < blockId; ++i)
2303 {
2304 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2305 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2306 }
2307
2308 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2309
2310 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2311 BlockDefinitionIndexMap::const_iterator it = itEnd;
2312
2313 argumentInfo.clampedIndex = index;
2314 if(type.isInterfaceBlock())
2315 {
2316 // Offset index to the beginning of the selected instance
2317 int blockRegisters = type.elementRegisterCount();
2318 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2319 argumentInfo.bufferIndex += bufferOffset;
2320 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2321 }
2322
2323 int regIndex = registerIndex(arg);
2324 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2325 {
2326 it = blockDefinition.find(i);
2327 if(it != itEnd)
2328 {
2329 argumentInfo.clampedIndex -= (i - regIndex);
2330 break;
2331 }
2332 }
2333 ASSERT(it != itEnd);
2334
2335 argumentInfo.typedMemberInfo = it->second;
2336
2337 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2338 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2339 }
2340 else
2341 {
2342 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2343 }
2344
2345 return argumentInfo;
2346 }
2347
Nicolas Capens0530b452017-11-15 16:39:47 -05002348 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002349 {
2350 if(argument)
2351 {
2352 TIntermTyped *arg = argument->getAsTyped();
2353 Temporary unpackedUniform(this);
2354
2355 const TType& srcType = arg->getType();
2356 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2357 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2358 {
2359 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2360 const TType &memberType = argumentInfo.typedMemberInfo.type;
2361
2362 if(memberType.getBasicType() == EbtBool)
2363 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002364 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002365
2366 // Convert the packed bool, which is currently an int, to a true bool
2367 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2368 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2369 instruction->dst.index = registerIndex(&unpackedUniform);
2370 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2371 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2372 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2373
2374 shader->append(instruction);
2375
2376 arg = &unpackedUniform;
2377 index = 0;
2378 }
2379 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2380 {
2381 int numCols = memberType.getNominalSize();
2382 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002383
Alexis Hetue97a31e2016-11-14 14:10:47 -05002384 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002385
2386 unsigned int dstIndex = registerIndex(&unpackedUniform);
2387 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2388 int arrayIndex = argumentInfo.clampedIndex / numCols;
2389 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2390
2391 for(int j = 0; j < numRows; ++j)
2392 {
2393 // Transpose the row major matrix
2394 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2395 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2396 instruction->dst.index = dstIndex;
2397 instruction->dst.mask = 1 << j;
2398 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2399 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2400 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2401 instruction->src[0].swizzle = srcSwizzle;
2402
2403 shader->append(instruction);
2404 }
2405
2406 arg = &unpackedUniform;
2407 index = 0;
2408 }
2409 }
2410
2411 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2412 const TType &type = argumentInfo.typedMemberInfo.type;
2413
2414 int size = registerSize(type, argumentInfo.clampedIndex);
2415
2416 parameter.type = registerType(arg);
2417 parameter.bufferIndex = argumentInfo.bufferIndex;
2418
2419 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2420 {
2421 int component = componentCount(type, argumentInfo.clampedIndex);
2422 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2423
2424 for(int i = 0; i < 4; i++)
2425 {
2426 if(size == 1) // Replicate
2427 {
2428 parameter.value[i] = constants[component + 0].getAsFloat();
2429 }
2430 else if(i < size)
2431 {
2432 parameter.value[i] = constants[component + i].getAsFloat();
2433 }
2434 else
2435 {
2436 parameter.value[i] = 0.0f;
2437 }
2438 }
2439 }
2440 else
2441 {
2442 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2443
2444 if(parameter.bufferIndex != -1)
2445 {
2446 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2447 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2448 }
2449 }
2450
2451 if(!IsSampler(arg->getBasicType()))
2452 {
2453 parameter.swizzle = readSwizzle(arg, size);
2454 }
2455 }
2456 }
2457
Nicolas Capens0530b452017-11-15 16:39:47 -05002458 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2459 {
2460 parameter.type = registerType(arg);
2461 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002462 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002463 }
2464
Nicolas Capens0bac2852016-05-07 06:09:58 -04002465 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2466 {
2467 for(int index = 0; index < dst->totalRegisterCount(); index++)
2468 {
2469 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002470 }
2471 }
2472
2473 int swizzleElement(int swizzle, int index)
2474 {
2475 return (swizzle >> (index * 2)) & 0x03;
2476 }
2477
2478 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2479 {
2480 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2481 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2482 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2483 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2484 }
2485
2486 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2487 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002488 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2489 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002490 {
2491 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2492 }
2493
2494 TIntermBinary *binary = dst->getAsBinaryNode();
2495
2496 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2497 {
2498 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2499
Nicolas Capens6986b282017-11-16 10:38:19 -05002500 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002501
2502 insert->src[0].type = insert->dst.type;
2503 insert->src[0].index = insert->dst.index;
2504 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002505 source(insert->src[1], src);
2506 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002507
2508 shader->append(insert);
2509 }
2510 else
2511 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002512 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2513
Nicolas Capens6986b282017-11-16 10:38:19 -05002514 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002515
Nicolas Capens0530b452017-11-15 16:39:47 -05002516 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002517 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2518
2519 shader->append(mov1);
2520
2521 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002522 {
2523 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2524
Nicolas Capens84249fd2017-11-09 11:20:51 -05002525 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002526 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002527 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002528
Nicolas Capens0530b452017-11-15 16:39:47 -05002529 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002530
2531 shader->append(mov);
2532 }
2533 }
2534 }
2535
Nicolas Capensd469de22017-11-16 10:42:20 -05002536 void OutputASM::evaluateRvalue(TIntermTyped *node)
2537 {
2538 TIntermBinary *binary = node->getAsBinaryNode();
2539
2540 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2541 {
2542 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2543
2544 destination(insert->dst, node);
2545
2546 Temporary address(this);
2547 unsigned char mask;
2548 TIntermTyped *root = nullptr;
2549 unsigned int offset = 0;
2550 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2551
2552 source(insert->src[0], root, offset);
2553 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2554
2555 source(insert->src[1], binary->getRight());
2556
2557 shader->append(insert);
2558 }
2559 else
2560 {
2561 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2562
2563 destination(mov1->dst, node, 0);
2564
2565 Temporary address(this);
2566 unsigned char mask;
2567 TIntermTyped *root = nullptr;
2568 unsigned int offset = 0;
2569 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2570
2571 source(mov1->src[0], root, offset);
2572 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2573
2574 shader->append(mov1);
2575
2576 for(int i = 1; i < node->totalRegisterCount(); i++)
2577 {
2578 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2579 mov->src[0].rel = mov1->src[0].rel;
2580 }
2581 }
2582 }
2583
Nicolas Capens6986b282017-11-16 10:38:19 -05002584 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002585 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002586 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002587 TIntermTyped *root = nullptr;
2588 unsigned int offset = 0;
2589 unsigned char mask = 0xF;
2590 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2591
2592 dst.type = registerType(root);
2593 dst.index = registerIndex(root) + offset;
2594 dst.mask = mask;
2595
2596 return swizzle;
2597 }
2598
2599 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2600 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002601 TIntermTyped *result = node;
2602 TIntermBinary *binary = node->getAsBinaryNode();
2603 TIntermSymbol *symbol = node->getAsSymbolNode();
2604
2605 if(binary)
2606 {
2607 TIntermTyped *left = binary->getLeft();
2608 TIntermTyped *right = binary->getRight();
2609
Nicolas Capens0530b452017-11-15 16:39:47 -05002610 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002611
2612 switch(binary->getOp())
2613 {
2614 case EOpIndexDirect:
2615 {
2616 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2617
2618 if(left->isRegister())
2619 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002620 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002621
Nicolas Capens0530b452017-11-15 16:39:47 -05002622 mask = 1;
2623 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002624 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002625 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002626 }
2627
2628 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002629 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002630
2631 return element;
2632 }
2633 else if(left->isArray() || left->isMatrix())
2634 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002635 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002636 return 0xE4;
2637 }
2638 else UNREACHABLE(0);
2639 }
2640 break;
2641 case EOpIndexIndirect:
2642 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002643 right->traverse(this);
2644
Nicolas Capens0bac2852016-05-07 06:09:58 -04002645 if(left->isRegister())
2646 {
2647 // Requires INSERT instruction (handled by calling function)
2648 }
2649 else if(left->isArray() || left->isMatrix())
2650 {
2651 int scale = result->totalRegisterCount();
2652
Nicolas Capens0530b452017-11-15 16:39:47 -05002653 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002654 {
2655 if(left->totalRegisterCount() > 1)
2656 {
2657 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002658 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002659
Nicolas Capens0530b452017-11-15 16:39:47 -05002660 rel.index = relativeRegister.index;
2661 rel.type = relativeRegister.type;
2662 rel.scale = scale;
2663 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002664 }
2665 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002666 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002667 {
2668 if(scale == 1)
2669 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002670 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002671 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002672 mad->src[0].index = rel.index;
2673 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002674 }
2675 else
2676 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002677 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002678 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002679 mul->src[0].index = rel.index;
2680 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002681
2682 Constant newScale(scale);
2683 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2684 }
2685
Nicolas Capens0530b452017-11-15 16:39:47 -05002686 rel.type = sw::Shader::PARAMETER_TEMP;
2687 rel.index = registerIndex(&address);
2688 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002689 }
2690 else // Just add the new index to the address register
2691 {
2692 if(scale == 1)
2693 {
2694 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2695 }
2696 else
2697 {
2698 Constant newScale(scale);
2699 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2700 }
2701 }
2702 }
2703 else UNREACHABLE(0);
2704 }
2705 break;
2706 case EOpIndexDirectStruct:
2707 case EOpIndexDirectInterfaceBlock:
2708 {
2709 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2710 left->getType().getStruct()->fields() :
2711 left->getType().getInterfaceBlock()->fields();
2712 int index = right->getAsConstantUnion()->getIConst(0);
2713 int fieldOffset = 0;
2714
2715 for(int i = 0; i < index; i++)
2716 {
2717 fieldOffset += fields[i]->type()->totalRegisterCount();
2718 }
2719
Nicolas Capens0530b452017-11-15 16:39:47 -05002720 offset += fieldOffset;
2721 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002722
2723 return 0xE4;
2724 }
2725 break;
2726 case EOpVectorSwizzle:
2727 {
2728 ASSERT(left->isRegister());
2729
Nicolas Capens0530b452017-11-15 16:39:47 -05002730 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002731
2732 int swizzle = 0;
2733 int rightMask = 0;
2734
2735 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2736
2737 for(unsigned int i = 0; i < sequence.size(); i++)
2738 {
2739 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2740
2741 int element = swizzleElement(leftSwizzle, index);
2742 rightMask = rightMask | (1 << element);
2743 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2744 }
2745
Nicolas Capens0530b452017-11-15 16:39:47 -05002746 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002747
2748 return swizzle;
2749 }
2750 break;
2751 default:
2752 UNREACHABLE(binary->getOp()); // Not an l-value operator
2753 break;
2754 }
2755 }
2756 else if(symbol)
2757 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002758 root = symbol;
2759 offset = 0;
2760 mask = writeMask(symbol);
2761
2762 return 0xE4;
2763 }
2764 else
2765 {
2766 node->traverse(this);
2767
2768 root = node;
2769 offset = 0;
2770 mask = writeMask(node);
2771
Nicolas Capens0bac2852016-05-07 06:09:58 -04002772 return 0xE4;
2773 }
2774
2775 return 0xE4;
2776 }
2777
2778 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2779 {
2780 if(isSamplerRegister(operand))
2781 {
2782 return sw::Shader::PARAMETER_SAMPLER;
2783 }
2784
2785 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002786 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002787 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002788 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2789 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002790 {
2791 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2792 }
2793 outputQualifier = qualifier;
2794 }
2795
2796 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2797 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002798 // Constant arrays are in the constant register file.
2799 if(operand->isArray() && operand->getArraySize() > 1)
2800 {
2801 return sw::Shader::PARAMETER_CONST;
2802 }
2803 else
2804 {
2805 return sw::Shader::PARAMETER_TEMP;
2806 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002807 }
2808
2809 switch(qualifier)
2810 {
2811 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2812 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2813 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2814 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2815 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2816 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2817 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2818 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2819 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2820 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2821 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2822 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2823 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2824 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2825 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2826 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2827 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2828 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2829 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2830 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2831 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2832 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2833 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2834 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2835 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2836 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002837 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002838 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2839 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2840 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2841 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2842 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2843 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2844 default: UNREACHABLE(qualifier);
2845 }
2846
2847 return sw::Shader::PARAMETER_VOID;
2848 }
2849
Alexis Hetu12b00502016-05-20 13:01:11 -04002850 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2851 {
2852 const TQualifier qualifier = operand->getQualifier();
2853 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2854 }
2855
Nicolas Capens0bac2852016-05-07 06:09:58 -04002856 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2857 {
2858 if(isSamplerRegister(operand))
2859 {
2860 return samplerRegister(operand);
2861 }
2862
2863 switch(operand->getQualifier())
2864 {
2865 case EvqTemporary: return temporaryRegister(operand);
2866 case EvqGlobal: return temporaryRegister(operand);
2867 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2868 case EvqAttribute: return attributeRegister(operand);
2869 case EvqVaryingIn: return varyingRegister(operand);
2870 case EvqVaryingOut: return varyingRegister(operand);
2871 case EvqVertexIn: return attributeRegister(operand);
2872 case EvqFragmentOut: return fragmentOutputRegister(operand);
2873 case EvqVertexOut: return varyingRegister(operand);
2874 case EvqFragmentIn: return varyingRegister(operand);
2875 case EvqInvariantVaryingIn: return varyingRegister(operand);
2876 case EvqInvariantVaryingOut: return varyingRegister(operand);
2877 case EvqSmooth: return varyingRegister(operand);
2878 case EvqFlat: return varyingRegister(operand);
2879 case EvqCentroidOut: return varyingRegister(operand);
2880 case EvqSmoothIn: return varyingRegister(operand);
2881 case EvqFlatIn: return varyingRegister(operand);
2882 case EvqCentroidIn: return varyingRegister(operand);
2883 case EvqUniform: return uniformRegister(operand);
2884 case EvqIn: return temporaryRegister(operand);
2885 case EvqOut: return temporaryRegister(operand);
2886 case EvqInOut: return temporaryRegister(operand);
2887 case EvqConstReadOnly: return temporaryRegister(operand);
2888 case EvqPosition: return varyingRegister(operand);
2889 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002890 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2891 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2892 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2893 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002894 case EvqPointCoord: return varyingRegister(operand);
2895 case EvqFragColor: return 0;
2896 case EvqFragData: return fragmentOutputRegister(operand);
2897 case EvqFragDepth: return 0;
2898 default: UNREACHABLE(operand->getQualifier());
2899 }
2900
2901 return 0;
2902 }
2903
2904 int OutputASM::writeMask(TIntermTyped *destination, int index)
2905 {
2906 if(destination->getQualifier() == EvqPointSize)
2907 {
2908 return 0x2; // Point size stored in the y component
2909 }
2910
2911 return 0xF >> (4 - registerSize(destination->getType(), index));
2912 }
2913
2914 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2915 {
2916 if(argument->getQualifier() == EvqPointSize)
2917 {
2918 return 0x55; // Point size stored in the y component
2919 }
2920
2921 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2922
2923 return swizzleSize[size];
2924 }
2925
2926 // Conservatively checks whether an expression is fast to compute and has no side effects
2927 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2928 {
2929 if(!expression->isRegister())
2930 {
2931 return false;
2932 }
2933
2934 return cost(expression, budget) >= 0;
2935 }
2936
2937 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2938 int OutputASM::cost(TIntermNode *expression, int budget)
2939 {
2940 if(budget < 0)
2941 {
2942 return budget;
2943 }
2944
2945 if(expression->getAsSymbolNode())
2946 {
2947 return budget;
2948 }
2949 else if(expression->getAsConstantUnion())
2950 {
2951 return budget;
2952 }
2953 else if(expression->getAsBinaryNode())
2954 {
2955 TIntermBinary *binary = expression->getAsBinaryNode();
2956
2957 switch(binary->getOp())
2958 {
2959 case EOpVectorSwizzle:
2960 case EOpIndexDirect:
2961 case EOpIndexDirectStruct:
2962 case EOpIndexDirectInterfaceBlock:
2963 return cost(binary->getLeft(), budget - 0);
2964 case EOpAdd:
2965 case EOpSub:
2966 case EOpMul:
2967 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2968 default:
2969 return -1;
2970 }
2971 }
2972 else if(expression->getAsUnaryNode())
2973 {
2974 TIntermUnary *unary = expression->getAsUnaryNode();
2975
2976 switch(unary->getOp())
2977 {
2978 case EOpAbs:
2979 case EOpNegative:
2980 return cost(unary->getOperand(), budget - 1);
2981 default:
2982 return -1;
2983 }
2984 }
2985 else if(expression->getAsSelectionNode())
2986 {
2987 TIntermSelection *selection = expression->getAsSelectionNode();
2988
2989 if(selection->usesTernaryOperator())
2990 {
2991 TIntermTyped *condition = selection->getCondition();
2992 TIntermNode *trueBlock = selection->getTrueBlock();
2993 TIntermNode *falseBlock = selection->getFalseBlock();
2994 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2995
2996 if(constantCondition)
2997 {
2998 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2999
3000 if(trueCondition)
3001 {
3002 return cost(trueBlock, budget - 0);
3003 }
3004 else
3005 {
3006 return cost(falseBlock, budget - 0);
3007 }
3008 }
3009 else
3010 {
3011 return cost(trueBlock, cost(falseBlock, budget - 2));
3012 }
3013 }
3014 }
3015
3016 return -1;
3017 }
3018
3019 const Function *OutputASM::findFunction(const TString &name)
3020 {
3021 for(unsigned int f = 0; f < functionArray.size(); f++)
3022 {
3023 if(functionArray[f].name == name)
3024 {
3025 return &functionArray[f];
3026 }
3027 }
3028
3029 return 0;
3030 }
3031
3032 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3033 {
3034 return allocate(temporaries, temporary);
3035 }
3036
Alexis Hetu49351232017-11-02 16:00:32 -04003037 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3038 {
3039 if(type.isStruct())
3040 {
3041 const TFieldList &fields = type.getStruct()->fields();
3042 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003043 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003044 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003045 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04003046 setPixelShaderInputs(fieldType, fieldVar, flat);
3047 fieldVar += fieldType.totalRegisterCount();
3048 }
3049 }
3050 else
3051 {
3052 for(int i = 0; i < type.totalRegisterCount(); i++)
3053 {
3054 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3055 }
3056 }
3057 }
3058
Nicolas Capens0bac2852016-05-07 06:09:58 -04003059 int OutputASM::varyingRegister(TIntermTyped *varying)
3060 {
3061 int var = lookup(varyings, varying);
3062
3063 if(var == -1)
3064 {
3065 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003066 int registerCount = varying->totalRegisterCount();
3067
3068 if(pixelShader)
3069 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04003070 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003071 {
3072 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3073 return 0;
3074 }
3075
3076 if(varying->getQualifier() == EvqPointCoord)
3077 {
3078 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04003079 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003080 }
3081 else
3082 {
Alexis Hetu49351232017-11-02 16:00:32 -04003083 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003084 }
3085 }
3086 else if(vertexShader)
3087 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04003088 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003089 {
3090 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3091 return 0;
3092 }
3093
3094 if(varying->getQualifier() == EvqPosition)
3095 {
3096 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003097 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003098 }
3099 else if(varying->getQualifier() == EvqPointSize)
3100 {
3101 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04003102 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003103 }
3104 else
3105 {
3106 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3107 }
3108 }
3109 else UNREACHABLE(0);
3110
3111 declareVarying(varying, var);
3112 }
3113
3114 return var;
3115 }
3116
3117 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3118 {
3119 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3120 {
Alexis Hetu49351232017-11-02 16:00:32 -04003121 TIntermSymbol *symbol = varying->getAsSymbolNode();
3122 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3123 }
3124 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003125
Alexis Hetu49351232017-11-02 16:00:32 -04003126 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3127 {
3128 const char *name = varyingName.c_str();
3129 VaryingList &activeVaryings = shaderObject->varyings;
3130
3131 TStructure* structure = type.getStruct();
3132 if(structure)
3133 {
3134 int fieldRegisterIndex = registerIndex;
3135
3136 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003137 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04003138 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003139 const TType& fieldType = *(field->type());
3140 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04003141 if(fieldRegisterIndex >= 0)
3142 {
3143 fieldRegisterIndex += fieldType.totalRegisterCount();
3144 }
3145 }
3146 }
3147 else
3148 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04003149 // Check if this varying has been declared before without having a register assigned
3150 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3151 {
3152 if(v->name == name)
3153 {
Alexis Hetu49351232017-11-02 16:00:32 -04003154 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003155 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003156 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3157 v->registerIndex = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003158 }
3159
3160 return;
3161 }
3162 }
3163
Alexis Hetu924513c2018-01-05 15:48:12 -05003164 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04003165 }
3166 }
3167
Alexis Hetu930df972018-01-30 16:54:13 -05003168 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3169 {
3170 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
3171 if((requestedLocation >= 0) && (requestedLocation < sw::RENDERTARGETS))
3172 {
3173 if(fragmentOutputs.size() <= requestedLocation)
3174 {
3175 while(fragmentOutputs.size() < requestedLocation)
3176 {
3177 fragmentOutputs.push_back(nullptr);
3178 }
3179 fragmentOutputs.push_back(fragmentOutput);
3180 }
3181 else if(!fragmentOutputs[requestedLocation])
3182 {
3183 fragmentOutputs[requestedLocation] = fragmentOutput;
3184 }
3185 else
3186 {
3187 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3188 }
3189 }
3190 else if(requestedLocation >= sw::RENDERTARGETS)
3191 {
3192 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3193 }
3194 }
3195
Nicolas Capens0bac2852016-05-07 06:09:58 -04003196 int OutputASM::uniformRegister(TIntermTyped *uniform)
3197 {
3198 const TType &type = uniform->getType();
3199 ASSERT(!IsSampler(type.getBasicType()));
3200 TInterfaceBlock *block = type.getAsInterfaceBlock();
3201 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3202 ASSERT(symbol || block);
3203
3204 if(symbol || block)
3205 {
3206 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3207 bool isBlockMember = (!block && parentBlock);
3208 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3209
3210 if(index == -1 || isBlockMember)
3211 {
3212 if(index == -1)
3213 {
3214 index = allocate(uniforms, uniform);
3215 }
3216
3217 // Verify if the current uniform is a member of an already declared block
3218 const TString &name = symbol ? symbol->getSymbol() : block->name();
3219 int blockMemberIndex = blockMemberLookup(type, name, index);
3220 if(blockMemberIndex == -1)
3221 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003222 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003223 }
3224 else
3225 {
3226 index = blockMemberIndex;
3227 }
3228 }
3229
3230 return index;
3231 }
3232
3233 return 0;
3234 }
3235
3236 int OutputASM::attributeRegister(TIntermTyped *attribute)
3237 {
3238 ASSERT(!attribute->isArray());
3239
3240 int index = lookup(attributes, attribute);
3241
3242 if(index == -1)
3243 {
3244 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3245 ASSERT(symbol);
3246
3247 if(symbol)
3248 {
3249 index = allocate(attributes, attribute);
3250 const TType &type = attribute->getType();
3251 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04003252 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3253 switch(type.getBasicType())
3254 {
3255 case EbtInt:
3256 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3257 break;
3258 case EbtUInt:
3259 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3260 break;
3261 case EbtFloat:
3262 default:
3263 break;
3264 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003265
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003266 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003267 {
3268 for(int i = 0; i < registerCount; i++)
3269 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003270 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003271 }
3272 }
3273
3274 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3275
3276 const char *name = symbol->getSymbol().c_str();
3277 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3278 }
3279 }
3280
3281 return index;
3282 }
3283
3284 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3285 {
3286 return allocate(fragmentOutputs, fragmentOutput);
3287 }
3288
3289 int OutputASM::samplerRegister(TIntermTyped *sampler)
3290 {
3291 const TType &type = sampler->getType();
3292 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3293
3294 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3295 TIntermBinary *binary = sampler->getAsBinaryNode();
3296
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003297 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003298 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003299 switch(type.getQualifier())
3300 {
3301 case EvqUniform:
3302 return samplerRegister(symbol);
3303 case EvqIn:
3304 case EvqConstReadOnly:
3305 // Function arguments are not (uniform) sampler registers
3306 return -1;
3307 default:
3308 UNREACHABLE(type.getQualifier());
3309 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003310 }
3311 else if(binary)
3312 {
3313 TIntermTyped *left = binary->getLeft();
3314 TIntermTyped *right = binary->getRight();
3315 const TType &leftType = left->getType();
3316 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3317 int offset = 0;
3318
3319 switch(binary->getOp())
3320 {
3321 case EOpIndexDirect:
3322 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003323 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003324 break;
3325 case EOpIndexDirectStruct:
3326 ASSERT(leftType.isStruct());
3327 {
3328 const TFieldList &fields = leftType.getStruct()->fields();
3329
3330 for(int i = 0; i < index; i++)
3331 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003332 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003333 }
3334 }
3335 break;
3336 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3337 return -1;
3338 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3339 default:
3340 UNREACHABLE(binary->getOp());
3341 return -1;
3342 }
3343
3344 int base = samplerRegister(left);
3345
3346 if(base < 0)
3347 {
3348 return -1;
3349 }
3350
3351 return base + offset;
3352 }
3353
3354 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003355 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003356 }
3357
3358 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3359 {
3360 const TType &type = sampler->getType();
3361 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3362
3363 int index = lookup(samplers, sampler);
3364
3365 if(index == -1)
3366 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003367 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003368
3369 if(sampler->getQualifier() == EvqUniform)
3370 {
3371 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003372 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003373 }
3374 }
3375
3376 return index;
3377 }
3378
3379 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3380 {
3381 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3382 }
3383
3384 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3385 {
3386 for(unsigned int i = 0; i < list.size(); i++)
3387 {
3388 if(list[i] == variable)
3389 {
3390 return i; // Pointer match
3391 }
3392 }
3393
3394 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3395 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3396
3397 if(varBlock)
3398 {
3399 for(unsigned int i = 0; i < list.size(); i++)
3400 {
3401 if(list[i])
3402 {
3403 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3404
3405 if(listBlock)
3406 {
3407 if(listBlock->name() == varBlock->name())
3408 {
3409 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3410 ASSERT(listBlock->fields() == varBlock->fields());
3411 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3412 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3413
3414 return i;
3415 }
3416 }
3417 }
3418 }
3419 }
3420 else if(varSymbol)
3421 {
3422 for(unsigned int i = 0; i < list.size(); i++)
3423 {
3424 if(list[i])
3425 {
3426 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3427
3428 if(listSymbol)
3429 {
3430 if(listSymbol->getId() == varSymbol->getId())
3431 {
3432 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3433 ASSERT(listSymbol->getType() == varSymbol->getType());
3434 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3435
3436 return i;
3437 }
3438 }
3439 }
3440 }
3441 }
3442
3443 return -1;
3444 }
3445
3446 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3447 {
3448 for(unsigned int i = 0; i < list.size(); i++)
3449 {
3450 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3451 {
3452 return i; // Pointer match
3453 }
3454 }
3455 return -1;
3456 }
3457
Alexis Hetuda163ed2018-01-03 16:36:14 -05003458 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003459 {
3460 int index = lookup(list, variable);
3461
3462 if(index == -1)
3463 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003464 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003465
3466 for(unsigned int i = 0; i < list.size(); i++)
3467 {
3468 if(list[i] == 0)
3469 {
3470 unsigned int j = 1;
3471 for( ; j < registerCount && (i + j) < list.size(); j++)
3472 {
3473 if(list[i + j] != 0)
3474 {
3475 break;
3476 }
3477 }
3478
3479 if(j == registerCount) // Found free slots
3480 {
3481 for(unsigned int j = 0; j < registerCount; j++)
3482 {
3483 list[i + j] = variable;
3484 }
3485
3486 return i;
3487 }
3488 }
3489 }
3490
3491 index = list.size();
3492
3493 for(unsigned int i = 0; i < registerCount; i++)
3494 {
3495 list.push_back(variable);
3496 }
3497 }
3498
3499 return index;
3500 }
3501
3502 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3503 {
3504 int index = lookup(list, variable);
3505
3506 if(index >= 0)
3507 {
3508 list[index] = 0;
3509 }
3510 }
3511
3512 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3513 {
3514 const TInterfaceBlock *block = type.getInterfaceBlock();
3515
3516 if(block)
3517 {
3518 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3519 const TFieldList& fields = block->fields();
3520 const TString &blockName = block->name();
3521 int fieldRegisterIndex = registerIndex;
3522
3523 if(!type.isInterfaceBlock())
3524 {
3525 // This is a uniform that's part of a block, let's see if the block is already defined
3526 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3527 {
3528 if(activeUniformBlocks[i].name == blockName.c_str())
3529 {
3530 // The block is already defined, find the register for the current uniform and return it
3531 for(size_t j = 0; j < fields.size(); j++)
3532 {
3533 const TString &fieldName = fields[j]->name();
3534 if(fieldName == name)
3535 {
3536 return fieldRegisterIndex;
3537 }
3538
3539 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3540 }
3541
3542 ASSERT(false);
3543 return fieldRegisterIndex;
3544 }
3545 }
3546 }
3547 }
3548
3549 return -1;
3550 }
3551
Alexis Hetuda163ed2018-01-03 16:36:14 -05003552 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003553 {
3554 const TStructure *structure = type.getStruct();
3555 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3556
3557 if(!structure && !block)
3558 {
3559 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3560 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3561 if(blockId >= 0)
3562 {
3563 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3564 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3565 }
3566 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003567 bool isSampler = IsSampler(type.getBasicType());
3568 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003569 {
3570 for(int i = 0; i < type.totalRegisterCount(); i++)
3571 {
3572 shader->declareSampler(fieldRegisterIndex + i);
3573 }
3574 }
Alexis Hetu924513c2018-01-05 15:48:12 -05003575 if(isSampler == samplersOnly)
Alexis Hetuda163ed2018-01-03 16:36:14 -05003576 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003577 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
Alexis Hetuda163ed2018-01-03 16:36:14 -05003578 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003579 }
3580 else if(block)
3581 {
3582 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3583 const TFieldList& fields = block->fields();
3584 const TString &blockName = block->name();
3585 int fieldRegisterIndex = registerIndex;
3586 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3587
3588 blockId = activeUniformBlocks.size();
3589 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3590 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3591 block->blockStorage(), isRowMajor, registerIndex, blockId));
3592 blockDefinitions.push_back(BlockDefinitionIndexMap());
3593
3594 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3595 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003596 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003597 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003598 const TType &fieldType = *(field->type());
3599 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003600 if(isUniformBlockMember && (fieldName == name))
3601 {
3602 registerIndex = fieldRegisterIndex;
3603 }
3604
3605 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3606
Alexis Hetuda163ed2018-01-03 16:36:14 -05003607 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003608 fieldRegisterIndex += fieldType.totalRegisterCount();
3609 }
3610 currentBlockEncoder.exitAggregateType();
3611 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3612 }
3613 else
3614 {
Alexis Hetu924513c2018-01-05 15:48:12 -05003615 // Store struct for program link time validation
3616 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3617
Nicolas Capens0bac2852016-05-07 06:09:58 -04003618 int fieldRegisterIndex = registerIndex;
3619
3620 const TFieldList& fields = structure->fields();
3621 if(type.isArray() && (structure || type.isInterfaceBlock()))
3622 {
3623 for(int i = 0; i < type.getArraySize(); i++)
3624 {
3625 if(encoder)
3626 {
3627 encoder->enterAggregateType();
3628 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003629 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003630 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003631 const TType &fieldType = *(field->type());
3632 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003633 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3634
Alexis Hetuda163ed2018-01-03 16:36:14 -05003635 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3636 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003637 }
3638 if(encoder)
3639 {
3640 encoder->exitAggregateType();
3641 }
3642 }
3643 }
3644 else
3645 {
3646 if(encoder)
3647 {
3648 encoder->enterAggregateType();
3649 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003650 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003651 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003652 const TType &fieldType = *(field->type());
3653 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003654 const TString uniformName = name + "." + fieldName;
3655
Alexis Hetuda163ed2018-01-03 16:36:14 -05003656 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3657 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003658 }
3659 if(encoder)
3660 {
3661 encoder->exitAggregateType();
3662 }
3663 }
3664 }
3665 }
3666
Nicolas Capens0bac2852016-05-07 06:09:58 -04003667 int OutputASM::dim(TIntermNode *v)
3668 {
3669 TIntermTyped *vector = v->getAsTyped();
3670 ASSERT(vector && vector->isRegister());
3671 return vector->getNominalSize();
3672 }
3673
3674 int OutputASM::dim2(TIntermNode *m)
3675 {
3676 TIntermTyped *matrix = m->getAsTyped();
3677 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3678 return matrix->getSecondarySize();
3679 }
3680
3681 // Returns ~0u if no loop count could be determined
3682 unsigned int OutputASM::loopCount(TIntermLoop *node)
3683 {
3684 // Parse loops of the form:
3685 // for(int index = initial; index [comparator] limit; index += increment)
3686 TIntermSymbol *index = 0;
3687 TOperator comparator = EOpNull;
3688 int initial = 0;
3689 int limit = 0;
3690 int increment = 0;
3691
3692 // Parse index name and intial value
3693 if(node->getInit())
3694 {
3695 TIntermAggregate *init = node->getInit()->getAsAggregate();
3696
3697 if(init)
3698 {
3699 TIntermSequence &sequence = init->getSequence();
3700 TIntermTyped *variable = sequence[0]->getAsTyped();
3701
Nicolas Capense3f05552017-05-24 10:45:56 -04003702 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003703 {
3704 TIntermBinary *assign = variable->getAsBinaryNode();
3705
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003706 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003707 {
3708 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3709 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3710
3711 if(symbol && constant)
3712 {
3713 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3714 {
3715 index = symbol;
3716 initial = constant->getUnionArrayPointer()[0].getIConst();
3717 }
3718 }
3719 }
3720 }
3721 }
3722 }
3723
3724 // Parse comparator and limit value
3725 if(index && node->getCondition())
3726 {
3727 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003728 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003729
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003730 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003731 {
3732 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3733
3734 if(constant)
3735 {
3736 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3737 {
3738 comparator = test->getOp();
3739 limit = constant->getUnionArrayPointer()[0].getIConst();
3740 }
3741 }
3742 }
3743 }
3744
3745 // Parse increment
3746 if(index && comparator != EOpNull && node->getExpression())
3747 {
3748 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3749 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3750
3751 if(binaryTerminal)
3752 {
3753 TOperator op = binaryTerminal->getOp();
3754 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3755
3756 if(constant)
3757 {
3758 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3759 {
3760 int value = constant->getUnionArrayPointer()[0].getIConst();
3761
3762 switch(op)
3763 {
3764 case EOpAddAssign: increment = value; break;
3765 case EOpSubAssign: increment = -value; break;
3766 default: UNIMPLEMENTED();
3767 }
3768 }
3769 }
3770 }
3771 else if(unaryTerminal)
3772 {
3773 TOperator op = unaryTerminal->getOp();
3774
3775 switch(op)
3776 {
3777 case EOpPostIncrement: increment = 1; break;
3778 case EOpPostDecrement: increment = -1; break;
3779 case EOpPreIncrement: increment = 1; break;
3780 case EOpPreDecrement: increment = -1; break;
3781 default: UNIMPLEMENTED();
3782 }
3783 }
3784 }
3785
3786 if(index && comparator != EOpNull && increment != 0)
3787 {
3788 if(comparator == EOpLessThanEqual)
3789 {
3790 comparator = EOpLessThan;
3791 limit += 1;
3792 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003793 else if(comparator == EOpGreaterThanEqual)
3794 {
3795 comparator = EOpLessThan;
3796 limit -= 1;
3797 std::swap(initial, limit);
3798 increment = -increment;
3799 }
3800 else if(comparator == EOpGreaterThan)
3801 {
3802 comparator = EOpLessThan;
3803 std::swap(initial, limit);
3804 increment = -increment;
3805 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003806
3807 if(comparator == EOpLessThan)
3808 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003809 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003810 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003811 return 0;
3812 }
3813
3814 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3815
3816 if(iterations < 0)
3817 {
3818 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003819 }
3820
3821 return iterations;
3822 }
3823 else UNIMPLEMENTED(); // Falls through
3824 }
3825
3826 return ~0u;
3827 }
3828
3829 bool LoopUnrollable::traverse(TIntermNode *node)
3830 {
3831 loopDepth = 0;
3832 loopUnrollable = true;
3833
3834 node->traverse(this);
3835
3836 return loopUnrollable;
3837 }
3838
3839 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3840 {
3841 if(visit == PreVisit)
3842 {
3843 loopDepth++;
3844 }
3845 else if(visit == PostVisit)
3846 {
3847 loopDepth++;
3848 }
3849
3850 return true;
3851 }
3852
3853 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3854 {
3855 if(!loopUnrollable)
3856 {
3857 return false;
3858 }
3859
3860 if(!loopDepth)
3861 {
3862 return true;
3863 }
3864
3865 switch(node->getFlowOp())
3866 {
3867 case EOpKill:
3868 case EOpReturn:
3869 break;
3870 case EOpBreak:
3871 case EOpContinue:
3872 loopUnrollable = false;
3873 break;
3874 default: UNREACHABLE(node->getFlowOp());
3875 }
3876
3877 return loopUnrollable;
3878 }
3879
3880 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3881 {
3882 return loopUnrollable;
3883 }
3884}