blob: ad795030d19c3b3bc5034a8c4a4bc79ea27176d0 [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
Nicolas Capens0bac2852016-05-07 06:09:58 -040029namespace glsl
30{
31 // Integer to TString conversion
32 TString str(int i)
33 {
34 char buffer[20];
35 sprintf(buffer, "%d", i);
36 return buffer;
37 }
38
39 class Temporary : public TIntermSymbol
40 {
41 public:
42 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
43 {
44 }
45
46 ~Temporary()
47 {
48 assembler->freeTemporary(this);
49 }
50
51 private:
52 OutputASM *const assembler;
53 };
54
55 class Constant : public TIntermConstantUnion
56 {
57 public:
58 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
59 {
60 constants[0].setFConst(x);
61 constants[1].setFConst(y);
62 constants[2].setFConst(z);
63 constants[3].setFConst(w);
64 }
65
66 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
67 {
68 constants[0].setBConst(b);
69 }
70
71 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
72 {
73 constants[0].setIConst(i);
74 }
75
76 ~Constant()
77 {
78 }
79
80 private:
81 ConstantUnion constants[4];
82 };
83
84 Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
85 type(type), precision(precision), name(name), arraySize(arraySize), registerIndex(registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
86 {
87 }
88
89 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
90 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
91 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
92 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
93 {
94 }
95
96 BlockLayoutEncoder::BlockLayoutEncoder(bool rowMajor)
97 : mCurrentOffset(0), isRowMajor(rowMajor)
98 {
99 }
100
101 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
102 {
103 int arrayStride;
104 int matrixStride;
105
106 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
107
108 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
109 static_cast<int>(arrayStride * BytesPerComponent),
110 static_cast<int>(matrixStride * BytesPerComponent),
111 (matrixStride > 0) && isRowMajor);
112
113 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
114
115 return memberInfo;
116 }
117
118 // static
119 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
120 {
121 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
122 }
123
124 // static
125 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
126 {
127 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
128 }
129
130 void BlockLayoutEncoder::nextRegister()
131 {
132 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
133 }
134
135 Std140BlockEncoder::Std140BlockEncoder(bool rowMajor) : BlockLayoutEncoder(rowMajor)
136 {
137 }
138
139 void Std140BlockEncoder::enterAggregateType()
140 {
141 nextRegister();
142 }
143
144 void Std140BlockEncoder::exitAggregateType()
145 {
146 nextRegister();
147 }
148
149 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
150 {
151 size_t baseAlignment = 0;
152 int matrixStride = 0;
153 int arrayStride = 0;
154
155 if(type.isMatrix())
156 {
157 baseAlignment = ComponentsPerRegister;
158 matrixStride = ComponentsPerRegister;
159
160 if(arraySize > 0)
161 {
162 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
163 arrayStride = ComponentsPerRegister * numRegisters;
164 }
165 }
166 else if(arraySize > 0)
167 {
168 baseAlignment = ComponentsPerRegister;
169 arrayStride = ComponentsPerRegister;
170 }
171 else
172 {
173 const size_t numComponents = type.getElementSize();
174 baseAlignment = (numComponents == 3 ? 4u : numComponents);
175 }
176
177 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
178
179 *matrixStrideOut = matrixStride;
180 *arrayStrideOut = arrayStride;
181 }
182
183 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
184 {
185 if(arraySize > 0)
186 {
187 mCurrentOffset += arrayStride * arraySize;
188 }
189 else if(type.isMatrix())
190 {
191 ASSERT(matrixStride == ComponentsPerRegister);
192 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
193 mCurrentOffset += ComponentsPerRegister * numRegisters;
194 }
195 else
196 {
197 mCurrentOffset += type.getElementSize();
198 }
199 }
200
201 Attribute::Attribute()
202 {
203 type = GL_NONE;
204 arraySize = 0;
205 registerIndex = 0;
206 }
207
208 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
209 {
210 this->type = type;
211 this->name = name;
212 this->arraySize = arraySize;
213 this->location = location;
214 this->registerIndex = registerIndex;
215 }
216
217 sw::PixelShader *Shader::getPixelShader() const
218 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500219 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400220 }
221
222 sw::VertexShader *Shader::getVertexShader() const
223 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500224 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400225 }
226
227 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
228 {
229 TString name = TFunction::unmangleName(nodeName);
230
231 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D")
232 {
233 method = IMPLICIT;
234 }
235 else if(name == "texture2DProj" || name == "textureProj")
236 {
237 method = IMPLICIT;
238 proj = true;
239 }
240 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
241 {
242 method = LOD;
243 }
244 else if(name == "texture2DProjLod" || name == "textureProjLod")
245 {
246 method = LOD;
247 proj = true;
248 }
249 else if(name == "textureSize")
250 {
251 method = SIZE;
252 }
253 else if(name == "textureOffset")
254 {
255 method = IMPLICIT;
256 offset = true;
257 }
258 else if(name == "textureProjOffset")
259 {
260 method = IMPLICIT;
261 offset = true;
262 proj = true;
263 }
264 else if(name == "textureLodOffset")
265 {
266 method = LOD;
267 offset = true;
268 }
269 else if(name == "textureProjLodOffset")
270 {
271 method = LOD;
272 proj = true;
273 offset = true;
274 }
275 else if(name == "texelFetch")
276 {
277 method = FETCH;
278 }
279 else if(name == "texelFetchOffset")
280 {
281 method = FETCH;
282 offset = true;
283 }
284 else if(name == "textureGrad")
285 {
286 method = GRAD;
287 }
288 else if(name == "textureGradOffset")
289 {
290 method = GRAD;
291 offset = true;
292 }
293 else if(name == "textureProjGrad")
294 {
295 method = GRAD;
296 proj = true;
297 }
298 else if(name == "textureProjGradOffset")
299 {
300 method = GRAD;
301 proj = true;
302 offset = true;
303 }
304 else UNREACHABLE(0);
305 }
306
307 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
308 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500309 shader = nullptr;
310 pixelShader = nullptr;
311 vertexShader = nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400312
313 if(shaderObject)
314 {
315 shader = shaderObject->getShader();
316 pixelShader = shaderObject->getPixelShader();
317 vertexShader = shaderObject->getVertexShader();
318 }
319
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500320 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
Nicolas Capens0bac2852016-05-07 06:09:58 -0400321 currentFunction = 0;
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500322 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
Nicolas Capens0bac2852016-05-07 06:09:58 -0400323 }
324
325 OutputASM::~OutputASM()
326 {
327 }
328
329 void OutputASM::output()
330 {
331 if(shader)
332 {
333 emitShader(GLOBAL);
334
335 if(functionArray.size() > 1) // Only call main() when there are other functions
336 {
337 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
338 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
339 callMain->dst.index = 0; // main()
340
341 emit(sw::Shader::OPCODE_RET);
342 }
343
344 emitShader(FUNCTION);
345 }
346 }
347
348 void OutputASM::emitShader(Scope scope)
349 {
350 emitScope = scope;
351 currentScope = GLOBAL;
352 mContext.getTreeRoot()->traverse(this);
353 }
354
355 void OutputASM::freeTemporary(Temporary *temporary)
356 {
357 free(temporaries, temporary);
358 }
359
360 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
361 {
362 TBasicType baseType = in->getType().getBasicType();
363
364 switch(op)
365 {
366 case sw::Shader::OPCODE_NEG:
367 switch(baseType)
368 {
369 case EbtInt:
370 case EbtUInt:
371 return sw::Shader::OPCODE_INEG;
372 case EbtFloat:
373 default:
374 return op;
375 }
376 case sw::Shader::OPCODE_ABS:
377 switch(baseType)
378 {
379 case EbtInt:
380 return sw::Shader::OPCODE_IABS;
381 case EbtFloat:
382 default:
383 return op;
384 }
385 case sw::Shader::OPCODE_SGN:
386 switch(baseType)
387 {
388 case EbtInt:
389 return sw::Shader::OPCODE_ISGN;
390 case EbtFloat:
391 default:
392 return op;
393 }
394 case sw::Shader::OPCODE_ADD:
395 switch(baseType)
396 {
397 case EbtInt:
398 case EbtUInt:
399 return sw::Shader::OPCODE_IADD;
400 case EbtFloat:
401 default:
402 return op;
403 }
404 case sw::Shader::OPCODE_SUB:
405 switch(baseType)
406 {
407 case EbtInt:
408 case EbtUInt:
409 return sw::Shader::OPCODE_ISUB;
410 case EbtFloat:
411 default:
412 return op;
413 }
414 case sw::Shader::OPCODE_MUL:
415 switch(baseType)
416 {
417 case EbtInt:
418 case EbtUInt:
419 return sw::Shader::OPCODE_IMUL;
420 case EbtFloat:
421 default:
422 return op;
423 }
424 case sw::Shader::OPCODE_DIV:
425 switch(baseType)
426 {
427 case EbtInt:
428 return sw::Shader::OPCODE_IDIV;
429 case EbtUInt:
430 return sw::Shader::OPCODE_UDIV;
431 case EbtFloat:
432 default:
433 return op;
434 }
435 case sw::Shader::OPCODE_IMOD:
436 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
437 case sw::Shader::OPCODE_ISHR:
438 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
439 case sw::Shader::OPCODE_MIN:
440 switch(baseType)
441 {
442 case EbtInt:
443 return sw::Shader::OPCODE_IMIN;
444 case EbtUInt:
445 return sw::Shader::OPCODE_UMIN;
446 case EbtFloat:
447 default:
448 return op;
449 }
450 case sw::Shader::OPCODE_MAX:
451 switch(baseType)
452 {
453 case EbtInt:
454 return sw::Shader::OPCODE_IMAX;
455 case EbtUInt:
456 return sw::Shader::OPCODE_UMAX;
457 case EbtFloat:
458 default:
459 return op;
460 }
461 default:
462 return op;
463 }
464 }
465
466 void OutputASM::visitSymbol(TIntermSymbol *symbol)
467 {
468 // Vertex varyings don't have to be actively used to successfully link
469 // against pixel shaders that use them. So make sure they're declared.
470 if(symbol->getQualifier() == EvqVaryingOut || symbol->getQualifier() == EvqInvariantVaryingOut || symbol->getQualifier() == EvqVertexOut)
471 {
472 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
473 {
474 declareVarying(symbol, -1);
475 }
476 }
477
478 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
479 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
480 // "All members of a named uniform block declared with a shared or std140 layout qualifier
481 // are considered active, even if they are not referenced in any shader in the program.
482 // The uniform block itself is also considered active, even if no member of the block is referenced."
483 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
484 {
485 uniformRegister(symbol);
486 }
487 }
488
489 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
490 {
491 if(currentScope != emitScope)
492 {
493 return false;
494 }
495
496 TIntermTyped *result = node;
497 TIntermTyped *left = node->getLeft();
498 TIntermTyped *right = node->getRight();
499 const TType &leftType = left->getType();
500 const TType &rightType = right->getType();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400501
502 if(isSamplerRegister(result))
503 {
504 return false; // Don't traverse, the register index is determined statically
505 }
506
507 switch(node->getOp())
508 {
509 case EOpAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500510 assert(visit == PreVisit);
511 right->traverse(this);
512 assignLvalue(left, right);
513 copy(result, right);
514 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400515 case EOpInitialize:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500516 assert(visit == PreVisit);
Nicolas Capens7cbb1de2017-12-22 08:54:18 -0500517 // Constant arrays go into the constant register file.
518 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
519 {
520 for(int i = 0; i < left->totalRegisterCount(); i++)
521 {
522 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
523 }
524 }
525 else
526 {
527 right->traverse(this);
528 copy(left, right);
529 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500530 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400531 case EOpMatrixTimesScalarAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500532 assert(visit == PreVisit);
533 right->traverse(this);
534 for(int i = 0; i < leftType.getNominalSize(); i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400535 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500536 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400537 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500538
539 assignLvalue(left, result);
540 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400541 case EOpVectorTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500542 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400543 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500544 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400545 int size = leftType.getNominalSize();
546
547 for(int i = 0; i < size; i++)
548 {
549 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
550 dot->dst.mask = 1 << i;
551 }
552
553 assignLvalue(left, result);
554 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500555 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400556 case EOpMatrixTimesMatrixAssign:
Nicolas Capens84249fd2017-11-09 11:20:51 -0500557 assert(visit == PreVisit);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400558 {
Nicolas Capens84249fd2017-11-09 11:20:51 -0500559 right->traverse(this);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400560 int dim = leftType.getNominalSize();
561
562 for(int i = 0; i < dim; i++)
563 {
564 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
565 mul->src[1].swizzle = 0x00;
566
567 for(int j = 1; j < dim; j++)
568 {
569 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
570 mad->src[1].swizzle = j * 0x55;
571 }
572 }
573
574 assignLvalue(left, result);
575 }
Nicolas Capens84249fd2017-11-09 11:20:51 -0500576 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400577 case EOpIndexDirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400578 case EOpIndexIndirect:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400579 case EOpIndexDirectStruct:
580 case EOpIndexDirectInterfaceBlock:
Nicolas Capensd469de22017-11-16 10:42:20 -0500581 assert(visit == PreVisit);
582 evaluateRvalue(node);
583 return false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400584 case EOpVectorSwizzle:
585 if(visit == PostVisit)
586 {
587 int swizzle = 0;
588 TIntermAggregate *components = right->getAsAggregate();
589
590 if(components)
591 {
592 TIntermSequence &sequence = components->getSequence();
593 int component = 0;
594
595 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
596 {
597 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
598
599 if(element)
600 {
601 int i = element->getUnionArrayPointer()[0].getIConst();
602 swizzle |= i << (component * 2);
603 component++;
604 }
605 else UNREACHABLE(0);
606 }
607 }
608 else UNREACHABLE(0);
609
610 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
611 mov->src[0].swizzle = swizzle;
612 }
613 break;
614 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
615 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
616 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
617 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
618 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
619 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
620 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
621 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
622 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
623 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
624 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
625 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
626 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
627 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
628 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
629 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
630 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
631 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
632 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
633 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
634 case EOpEqual:
635 if(visit == PostVisit)
636 {
637 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
638
639 for(int index = 1; index < left->totalRegisterCount(); index++)
640 {
641 Temporary equal(this);
642 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
643 emit(sw::Shader::OPCODE_AND, result, result, &equal);
644 }
645 }
646 break;
647 case EOpNotEqual:
648 if(visit == PostVisit)
649 {
650 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
651
652 for(int index = 1; index < left->totalRegisterCount(); index++)
653 {
654 Temporary notEqual(this);
655 emit(sw::Shader::OPCODE_NE, &notEqual, 0, left, index, right, index);
656 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
657 }
658 }
659 break;
660 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
661 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
662 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
663 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
664 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
665 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
666 case EOpMatrixTimesScalar:
667 if(visit == PostVisit)
668 {
669 if(left->isMatrix())
670 {
671 for(int i = 0; i < leftType.getNominalSize(); i++)
672 {
673 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
674 }
675 }
676 else if(right->isMatrix())
677 {
678 for(int i = 0; i < rightType.getNominalSize(); i++)
679 {
680 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
681 }
682 }
683 else UNREACHABLE(0);
684 }
685 break;
686 case EOpVectorTimesMatrix:
687 if(visit == PostVisit)
688 {
689 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
690
691 int size = rightType.getNominalSize();
692 for(int i = 0; i < size; i++)
693 {
694 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
695 dot->dst.mask = 1 << i;
696 }
697 }
698 break;
699 case EOpMatrixTimesVector:
700 if(visit == PostVisit)
701 {
702 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
703 mul->src[1].swizzle = 0x00;
704
705 int size = rightType.getNominalSize();
706 for(int i = 1; i < size; i++)
707 {
708 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
709 mad->src[1].swizzle = i * 0x55;
710 }
711 }
712 break;
713 case EOpMatrixTimesMatrix:
714 if(visit == PostVisit)
715 {
716 int dim = leftType.getNominalSize();
717
718 int size = rightType.getNominalSize();
719 for(int i = 0; i < size; i++)
720 {
721 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
722 mul->src[1].swizzle = 0x00;
723
724 for(int j = 1; j < dim; j++)
725 {
726 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
727 mad->src[1].swizzle = j * 0x55;
728 }
729 }
730 }
731 break;
732 case EOpLogicalOr:
733 if(trivial(right, 6))
734 {
735 if(visit == PostVisit)
736 {
737 emit(sw::Shader::OPCODE_OR, result, left, right);
738 }
739 }
740 else // Short-circuit evaluation
741 {
742 if(visit == InVisit)
743 {
744 emit(sw::Shader::OPCODE_MOV, result, left);
745 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
746 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
747 }
748 else if(visit == PostVisit)
749 {
750 emit(sw::Shader::OPCODE_MOV, result, right);
751 emit(sw::Shader::OPCODE_ENDIF);
752 }
753 }
754 break;
755 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
756 case EOpLogicalAnd:
757 if(trivial(right, 6))
758 {
759 if(visit == PostVisit)
760 {
761 emit(sw::Shader::OPCODE_AND, result, left, right);
762 }
763 }
764 else // Short-circuit evaluation
765 {
766 if(visit == InVisit)
767 {
768 emit(sw::Shader::OPCODE_MOV, result, left);
769 emit(sw::Shader::OPCODE_IF, 0, result);
770 }
771 else if(visit == PostVisit)
772 {
773 emit(sw::Shader::OPCODE_MOV, result, right);
774 emit(sw::Shader::OPCODE_ENDIF);
775 }
776 }
777 break;
778 default: UNREACHABLE(node->getOp());
779 }
780
781 return true;
782 }
783
784 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
785 {
786 switch(size)
787 {
788 case 1: // Used for cofactor computation only
789 {
790 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
791 bool isMov = (row == col);
792 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
793 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
794 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
795 mov->dst.mask = 1 << outRow;
796 }
797 break;
798 case 2:
799 {
800 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
801
802 bool isCofactor = (col >= 0) && (row >= 0);
803 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
804 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
805 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
806
807 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
808 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
809 det->dst.mask = 1 << outRow;
810 }
811 break;
812 case 3:
813 {
814 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
815
816 bool isCofactor = (col >= 0) && (row >= 0);
817 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
818 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
819 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
820 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
821
822 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
823 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
824 det->dst.mask = 1 << outRow;
825 }
826 break;
827 case 4:
828 {
829 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
830 det->dst.mask = 1 << outRow;
831 }
832 break;
833 default:
834 UNREACHABLE(size);
835 break;
836 }
837 }
838
839 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
840 {
841 if(currentScope != emitScope)
842 {
843 return false;
844 }
845
846 TIntermTyped *result = node;
847 TIntermTyped *arg = node->getOperand();
848 TBasicType basicType = arg->getType().getBasicType();
849
850 union
851 {
852 float f;
853 int i;
854 } one_value;
855
856 if(basicType == EbtInt || basicType == EbtUInt)
857 {
858 one_value.i = 1;
859 }
860 else
861 {
862 one_value.f = 1.0f;
863 }
864
865 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
866 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
867 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
868
869 switch(node->getOp())
870 {
871 case EOpNegative:
872 if(visit == PostVisit)
873 {
874 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
875 for(int index = 0; index < arg->totalRegisterCount(); index++)
876 {
877 emit(negOpcode, result, index, arg, index);
878 }
879 }
880 break;
881 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
882 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Alexis Hetu18e2a972017-07-28 13:43:25 -0400883 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400884 case EOpPostIncrement:
885 if(visit == PostVisit)
886 {
887 copy(result, arg);
888
889 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
890 for(int index = 0; index < arg->totalRegisterCount(); index++)
891 {
892 emit(addOpcode, arg, index, arg, index, &one);
893 }
894
895 assignLvalue(arg, arg);
896 }
897 break;
898 case EOpPostDecrement:
899 if(visit == PostVisit)
900 {
901 copy(result, arg);
902
903 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
904 for(int index = 0; index < arg->totalRegisterCount(); index++)
905 {
906 emit(subOpcode, arg, index, arg, index, &one);
907 }
908
909 assignLvalue(arg, arg);
910 }
911 break;
912 case EOpPreIncrement:
913 if(visit == PostVisit)
914 {
915 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
916 for(int index = 0; index < arg->totalRegisterCount(); index++)
917 {
918 emit(addOpcode, result, index, arg, index, &one);
919 }
920
921 assignLvalue(arg, result);
922 }
923 break;
924 case EOpPreDecrement:
925 if(visit == PostVisit)
926 {
927 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
928 for(int index = 0; index < arg->totalRegisterCount(); index++)
929 {
930 emit(subOpcode, result, index, arg, index, &one);
931 }
932
933 assignLvalue(arg, result);
934 }
935 break;
936 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
937 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
938 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
939 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
940 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
941 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
942 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
943 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
944 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
945 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
946 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
947 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
948 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
949 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
950 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
951 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
952 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
953 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
954 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
955 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
956 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
957 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
958 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
959 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
960 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
961 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
962 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
963 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
964 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
965 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
966 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
967 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
968 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
969 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
970 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
971 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
972 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
973 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
974 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
975 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
976 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
977 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
978 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
979 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
980 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
981 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
982 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
983 case EOpTranspose:
984 if(visit == PostVisit)
985 {
986 int numCols = arg->getNominalSize();
987 int numRows = arg->getSecondarySize();
988 for(int i = 0; i < numCols; ++i)
989 {
990 for(int j = 0; j < numRows; ++j)
991 {
992 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
993 mov->src[0].swizzle = 0x55 * j;
994 mov->dst.mask = 1 << i;
995 }
996 }
997 }
998 break;
999 case EOpDeterminant:
1000 if(visit == PostVisit)
1001 {
1002 int size = arg->getNominalSize();
1003 ASSERT(size == arg->getSecondarySize());
1004
1005 emitDeterminant(result, arg, size);
1006 }
1007 break;
1008 case EOpInverse:
1009 if(visit == PostVisit)
1010 {
1011 int size = arg->getNominalSize();
1012 ASSERT(size == arg->getSecondarySize());
1013
1014 // Compute transposed matrix of cofactors
1015 for(int i = 0; i < size; ++i)
1016 {
1017 for(int j = 0; j < size; ++j)
1018 {
1019 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1020 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1021 emitDeterminant(result, arg, size - 1, j, i, i, j);
1022 }
1023 }
1024
1025 // Compute 1 / determinant
1026 Temporary invDet(this);
1027 emitDeterminant(&invDet, arg, size);
1028 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1029 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1030 div->src[1].swizzle = 0x00; // xxxx
1031
1032 // Divide transposed matrix of cofactors by determinant
1033 for(int i = 0; i < size; ++i)
1034 {
1035 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1036 }
1037 }
1038 break;
1039 default: UNREACHABLE(node->getOp());
1040 }
1041
1042 return true;
1043 }
1044
1045 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1046 {
1047 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1048 {
1049 return false;
1050 }
1051
1052 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1053
1054 TIntermTyped *result = node;
1055 const TType &resultType = node->getType();
1056 TIntermSequence &arg = node->getSequence();
1057 size_t argumentCount = arg.size();
1058
1059 switch(node->getOp())
1060 {
1061 case EOpSequence: break;
1062 case EOpDeclaration: break;
1063 case EOpInvariantDeclaration: break;
1064 case EOpPrototype: break;
1065 case EOpComma:
1066 if(visit == PostVisit)
1067 {
1068 copy(result, arg[1]);
1069 }
1070 break;
1071 case EOpFunction:
1072 if(visit == PreVisit)
1073 {
1074 const TString &name = node->getName();
1075
1076 if(emitScope == FUNCTION)
1077 {
1078 if(functionArray.size() > 1) // No need for a label when there's only main()
1079 {
1080 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1081 label->dst.type = sw::Shader::PARAMETER_LABEL;
1082
1083 const Function *function = findFunction(name);
1084 ASSERT(function); // Should have been added during global pass
1085 label->dst.index = function->label;
1086 currentFunction = function->label;
1087 }
1088 }
1089 else if(emitScope == GLOBAL)
1090 {
1091 if(name != "main(")
1092 {
1093 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1094 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1095 }
1096 }
1097 else UNREACHABLE(emitScope);
1098
1099 currentScope = FUNCTION;
1100 }
1101 else if(visit == PostVisit)
1102 {
1103 if(emitScope == FUNCTION)
1104 {
1105 if(functionArray.size() > 1) // No need to return when there's only main()
1106 {
1107 emit(sw::Shader::OPCODE_RET);
1108 }
1109 }
1110
1111 currentScope = GLOBAL;
1112 }
1113 break;
1114 case EOpFunctionCall:
1115 if(visit == PostVisit)
1116 {
1117 if(node->isUserDefined())
1118 {
1119 const TString &name = node->getName();
1120 const Function *function = findFunction(name);
1121
1122 if(!function)
1123 {
1124 mContext.error(node->getLine(), "function definition not found", name.c_str());
1125 return false;
1126 }
1127
1128 TIntermSequence &arguments = *function->arg;
1129
1130 for(size_t i = 0; i < argumentCount; i++)
1131 {
1132 TIntermTyped *in = arguments[i]->getAsTyped();
1133
1134 if(in->getQualifier() == EvqIn ||
1135 in->getQualifier() == EvqInOut ||
1136 in->getQualifier() == EvqConstReadOnly)
1137 {
1138 copy(in, arg[i]);
1139 }
1140 }
1141
1142 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1143 call->dst.type = sw::Shader::PARAMETER_LABEL;
1144 call->dst.index = function->label;
1145
1146 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1147 {
1148 copy(result, function->ret);
1149 }
1150
1151 for(size_t i = 0; i < argumentCount; i++)
1152 {
1153 TIntermTyped *argument = arguments[i]->getAsTyped();
1154 TIntermTyped *out = arg[i]->getAsTyped();
1155
1156 if(argument->getQualifier() == EvqOut ||
1157 argument->getQualifier() == EvqInOut)
1158 {
Nicolas Capens5da2d3f2016-06-11 00:41:49 -04001159 assignLvalue(out, argument);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001160 }
1161 }
1162 }
1163 else
1164 {
1165 const TextureFunction textureFunction(node->getName());
Nicolas Capensa0b57832017-11-07 13:07:53 -05001166 TIntermTyped *s = arg[0]->getAsTyped();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001167 TIntermTyped *t = arg[1]->getAsTyped();
1168
1169 Temporary coord(this);
1170
1171 if(textureFunction.proj)
1172 {
Nicolas Capens0484c792016-06-13 22:02:36 -04001173 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1174 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1175 rcp->dst.mask = 0x7;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001176
Nicolas Capens0484c792016-06-13 22:02:36 -04001177 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1178 mul->dst.mask = 0x7;
Nicolas Capensa0b57832017-11-07 13:07:53 -05001179
1180 if(IsShadowSampler(s->getBasicType()))
1181 {
1182 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1183 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1184 mov->src[0].swizzle = 0xA4;
1185 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001186 }
1187 else
1188 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001189 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1190
1191 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1192 {
1193 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1194 mov->src[0].swizzle = 0xA4;
1195 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001196 }
1197
1198 switch(textureFunction.method)
1199 {
1200 case TextureFunction::IMPLICIT:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001201 if(!textureFunction.offset)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001202 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001203 if(argumentCount == 2)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001204 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001205 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001206 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001207 else if(argumentCount == 3) // Bias
Nicolas Capens0bac2852016-05-07 06:09:58 -04001208 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001209 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1210 }
1211 else UNREACHABLE(argumentCount);
1212 }
1213 else // Offset
1214 {
1215 if(argumentCount == 3)
1216 {
1217 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1218 }
1219 else if(argumentCount == 4) // Bias
1220 {
1221 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001222 }
1223 else UNREACHABLE(argumentCount);
1224 }
1225 break;
1226 case TextureFunction::LOD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001227 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001228 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001229 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001230 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001231 else if(argumentCount == 4) // Offset
1232 {
1233 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1234 }
1235 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001236 break;
1237 case TextureFunction::FETCH:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001238 if(!textureFunction.offset && argumentCount == 3)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001239 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001240 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001241 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001242 else if(argumentCount == 4) // Offset
1243 {
1244 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1245 }
1246 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001247 break;
1248 case TextureFunction::GRAD:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001249 if(!textureFunction.offset && argumentCount == 4)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001250 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001251 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001252 }
Nicolas Capensa0b57832017-11-07 13:07:53 -05001253 else if(argumentCount == 5) // Offset
1254 {
1255 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1256 }
1257 else UNREACHABLE(argumentCount);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001258 break;
1259 case TextureFunction::SIZE:
Nicolas Capensa0b57832017-11-07 13:07:53 -05001260 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001261 break;
1262 default:
1263 UNREACHABLE(textureFunction.method);
1264 }
1265 }
1266 }
1267 break;
1268 case EOpParameters:
1269 break;
1270 case EOpConstructFloat:
1271 case EOpConstructVec2:
1272 case EOpConstructVec3:
1273 case EOpConstructVec4:
1274 case EOpConstructBool:
1275 case EOpConstructBVec2:
1276 case EOpConstructBVec3:
1277 case EOpConstructBVec4:
1278 case EOpConstructInt:
1279 case EOpConstructIVec2:
1280 case EOpConstructIVec3:
1281 case EOpConstructIVec4:
1282 case EOpConstructUInt:
1283 case EOpConstructUVec2:
1284 case EOpConstructUVec3:
1285 case EOpConstructUVec4:
1286 if(visit == PostVisit)
1287 {
1288 int component = 0;
Alexis Hetu2a198552016-09-27 20:50:45 -04001289 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1290 int arrayComponents = result->getType().getElementSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001291 for(size_t i = 0; i < argumentCount; i++)
1292 {
1293 TIntermTyped *argi = arg[i]->getAsTyped();
1294 int size = argi->getNominalSize();
Alexis Hetu2a198552016-09-27 20:50:45 -04001295 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1296 int swizzle = component - (arrayIndex * arrayComponents);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001297
1298 if(!argi->isMatrix())
1299 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001300 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1301 mov->dst.mask = (0xF << swizzle) & 0xF;
1302 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001303
1304 component += size;
1305 }
1306 else // Matrix
1307 {
1308 int column = 0;
1309
1310 while(component < resultType.getNominalSize())
1311 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001312 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1313 mov->dst.mask = (0xF << swizzle) & 0xF;
1314 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001315
1316 column++;
1317 component += size;
1318 }
1319 }
1320 }
1321 }
1322 break;
1323 case EOpConstructMat2:
1324 case EOpConstructMat2x3:
1325 case EOpConstructMat2x4:
1326 case EOpConstructMat3x2:
1327 case EOpConstructMat3:
1328 case EOpConstructMat3x4:
1329 case EOpConstructMat4x2:
1330 case EOpConstructMat4x3:
1331 case EOpConstructMat4:
1332 if(visit == PostVisit)
1333 {
1334 TIntermTyped *arg0 = arg[0]->getAsTyped();
1335 const int outCols = result->getNominalSize();
1336 const int outRows = result->getSecondarySize();
1337
1338 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1339 {
1340 for(int i = 0; i < outCols; i++)
1341 {
Alexis Hetu7208e932016-06-02 11:19:24 -04001342 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001343 Instruction *mov = emitCast(result, i, arg0, 0);
1344 mov->dst.mask = 1 << i;
1345 ASSERT(mov->src[0].swizzle == 0x00);
1346 }
1347 }
1348 else if(arg0->isMatrix())
1349 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001350 int arraySize = result->isArray() ? result->getArraySize() : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001351
Alexis Hetu2a198552016-09-27 20:50:45 -04001352 for(int n = 0; n < arraySize; n++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001353 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001354 TIntermTyped *argi = arg[n]->getAsTyped();
1355 const int inCols = argi->getNominalSize();
1356 const int inRows = argi->getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001357
Alexis Hetu2a198552016-09-27 20:50:45 -04001358 for(int i = 0; i < outCols; i++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001359 {
Alexis Hetu2a198552016-09-27 20:50:45 -04001360 if(i >= inCols || outRows > inRows)
1361 {
1362 // Initialize to identity matrix
1363 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));
1364 emitCast(result, i + n * outCols, &col, 0);
1365 }
1366
1367 if(i < inCols)
1368 {
1369 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1370 mov->dst.mask = 0xF >> (4 - inRows);
1371 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001372 }
1373 }
1374 }
1375 else
1376 {
1377 int column = 0;
1378 int row = 0;
1379
1380 for(size_t i = 0; i < argumentCount; i++)
1381 {
1382 TIntermTyped *argi = arg[i]->getAsTyped();
1383 int size = argi->getNominalSize();
1384 int element = 0;
1385
1386 while(element < size)
1387 {
1388 Instruction *mov = emitCast(result, column, argi, 0);
1389 mov->dst.mask = (0xF << row) & 0xF;
1390 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1391
1392 int end = row + size - element;
1393 column = end >= outRows ? column + 1 : column;
1394 element = element + outRows - row;
1395 row = end >= outRows ? 0 : end;
1396 }
1397 }
1398 }
1399 }
1400 break;
1401 case EOpConstructStruct:
1402 if(visit == PostVisit)
1403 {
1404 int offset = 0;
1405 for(size_t i = 0; i < argumentCount; i++)
1406 {
1407 TIntermTyped *argi = arg[i]->getAsTyped();
1408 int size = argi->totalRegisterCount();
1409
1410 for(int index = 0; index < size; index++)
1411 {
1412 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1413 mov->dst.mask = writeMask(result, offset + index);
1414 }
1415
1416 offset += size;
1417 }
1418 }
1419 break;
1420 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1421 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1422 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1423 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1424 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1425 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1426 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1427 case EOpModf:
1428 if(visit == PostVisit)
1429 {
1430 TIntermTyped* arg1 = arg[1]->getAsTyped();
1431 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1432 assignLvalue(arg1, arg1);
1433 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1434 }
1435 break;
1436 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1437 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1438 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1439 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1440 case EOpClamp:
1441 if(visit == PostVisit)
1442 {
1443 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1444 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1445 }
1446 break;
1447 case EOpMix: if(visit == PostVisit) emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]); break;
1448 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1449 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1450 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1451 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1452 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1453 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1454 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1455 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1456 case EOpMul:
1457 if(visit == PostVisit)
1458 {
1459 TIntermTyped *arg0 = arg[0]->getAsTyped();
Alexis Hetue97a31e2016-11-14 14:10:47 -05001460 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1461 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001462
1463 int size = arg0->getNominalSize();
1464 for(int i = 0; i < size; i++)
1465 {
1466 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1467 }
1468 }
1469 break;
1470 case EOpOuterProduct:
1471 if(visit == PostVisit)
1472 {
1473 for(int i = 0; i < dim(arg[1]); i++)
1474 {
1475 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1476 mul->src[1].swizzle = 0x55 * i;
1477 }
1478 }
1479 break;
1480 default: UNREACHABLE(node->getOp());
1481 }
1482
1483 return true;
1484 }
1485
1486 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1487 {
1488 if(currentScope != emitScope)
1489 {
1490 return false;
1491 }
1492
1493 TIntermTyped *condition = node->getCondition();
1494 TIntermNode *trueBlock = node->getTrueBlock();
1495 TIntermNode *falseBlock = node->getFalseBlock();
1496 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1497
1498 condition->traverse(this);
1499
1500 if(node->usesTernaryOperator())
1501 {
1502 if(constantCondition)
1503 {
1504 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1505
1506 if(trueCondition)
1507 {
1508 trueBlock->traverse(this);
1509 copy(node, trueBlock);
1510 }
1511 else
1512 {
1513 falseBlock->traverse(this);
1514 copy(node, falseBlock);
1515 }
1516 }
1517 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1518 {
1519 trueBlock->traverse(this);
1520 falseBlock->traverse(this);
1521 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1522 }
1523 else
1524 {
1525 emit(sw::Shader::OPCODE_IF, 0, condition);
1526
1527 if(trueBlock)
1528 {
1529 trueBlock->traverse(this);
1530 copy(node, trueBlock);
1531 }
1532
1533 if(falseBlock)
1534 {
1535 emit(sw::Shader::OPCODE_ELSE);
1536 falseBlock->traverse(this);
1537 copy(node, falseBlock);
1538 }
1539
1540 emit(sw::Shader::OPCODE_ENDIF);
1541 }
1542 }
1543 else // if/else statement
1544 {
1545 if(constantCondition)
1546 {
1547 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1548
1549 if(trueCondition)
1550 {
1551 if(trueBlock)
1552 {
1553 trueBlock->traverse(this);
1554 }
1555 }
1556 else
1557 {
1558 if(falseBlock)
1559 {
1560 falseBlock->traverse(this);
1561 }
1562 }
1563 }
1564 else
1565 {
1566 emit(sw::Shader::OPCODE_IF, 0, condition);
1567
1568 if(trueBlock)
1569 {
1570 trueBlock->traverse(this);
1571 }
1572
1573 if(falseBlock)
1574 {
1575 emit(sw::Shader::OPCODE_ELSE);
1576 falseBlock->traverse(this);
1577 }
1578
1579 emit(sw::Shader::OPCODE_ENDIF);
1580 }
1581 }
1582
1583 return false;
1584 }
1585
1586 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1587 {
1588 if(currentScope != emitScope)
1589 {
1590 return false;
1591 }
1592
1593 unsigned int iterations = loopCount(node);
1594
1595 if(iterations == 0)
1596 {
1597 return false;
1598 }
1599
1600 bool unroll = (iterations <= 4);
1601
1602 if(unroll)
1603 {
1604 LoopUnrollable loopUnrollable;
1605 unroll = loopUnrollable.traverse(node);
1606 }
1607
1608 TIntermNode *init = node->getInit();
1609 TIntermTyped *condition = node->getCondition();
1610 TIntermTyped *expression = node->getExpression();
1611 TIntermNode *body = node->getBody();
1612 Constant True(true);
1613
1614 if(node->getType() == ELoopDoWhile)
1615 {
1616 Temporary iterate(this);
1617 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1618
1619 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1620
1621 if(body)
1622 {
1623 body->traverse(this);
1624 }
1625
1626 emit(sw::Shader::OPCODE_TEST);
1627
1628 condition->traverse(this);
1629 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1630
1631 emit(sw::Shader::OPCODE_ENDWHILE);
1632 }
1633 else
1634 {
1635 if(init)
1636 {
1637 init->traverse(this);
1638 }
1639
1640 if(unroll)
1641 {
1642 for(unsigned int i = 0; i < iterations; i++)
1643 {
1644 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1645
1646 if(body)
1647 {
1648 body->traverse(this);
1649 }
1650
1651 if(expression)
1652 {
1653 expression->traverse(this);
1654 }
1655 }
1656 }
1657 else
1658 {
1659 if(condition)
1660 {
1661 condition->traverse(this);
1662 }
1663 else
1664 {
1665 condition = &True;
1666 }
1667
1668 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1669
1670 if(body)
1671 {
1672 body->traverse(this);
1673 }
1674
1675 emit(sw::Shader::OPCODE_TEST);
1676
1677 if(expression)
1678 {
1679 expression->traverse(this);
1680 }
1681
1682 if(condition)
1683 {
1684 condition->traverse(this);
1685 }
1686
1687 emit(sw::Shader::OPCODE_ENDWHILE);
1688 }
1689 }
1690
1691 return false;
1692 }
1693
1694 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1695 {
1696 if(currentScope != emitScope)
1697 {
1698 return false;
1699 }
1700
1701 switch(node->getFlowOp())
1702 {
1703 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1704 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1705 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1706 case EOpReturn:
1707 if(visit == PostVisit)
1708 {
1709 TIntermTyped *value = node->getExpression();
1710
1711 if(value)
1712 {
1713 copy(functionArray[currentFunction].ret, value);
1714 }
1715
1716 emit(sw::Shader::OPCODE_LEAVE);
1717 }
1718 break;
1719 default: UNREACHABLE(node->getFlowOp());
1720 }
1721
1722 return true;
1723 }
1724
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001725 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1726 {
1727 if(currentScope != emitScope)
1728 {
1729 return false;
1730 }
1731
1732 TIntermTyped* switchValue = node->getInit();
1733 TIntermAggregate* opList = node->getStatementList();
1734
1735 if(!switchValue || !opList)
1736 {
1737 return false;
1738 }
1739
1740 switchValue->traverse(this);
1741
1742 emit(sw::Shader::OPCODE_SWITCH);
1743
1744 TIntermSequence& sequence = opList->getSequence();
1745 TIntermSequence::iterator it = sequence.begin();
1746 TIntermSequence::iterator defaultIt = sequence.end();
1747 int nbCases = 0;
1748 for(; it != sequence.end(); ++it)
1749 {
1750 TIntermCase* currentCase = (*it)->getAsCaseNode();
1751 if(currentCase)
1752 {
1753 TIntermSequence::iterator caseIt = it;
1754
1755 TIntermTyped* condition = currentCase->getCondition();
1756 if(condition) // non default case
1757 {
1758 if(nbCases != 0)
1759 {
1760 emit(sw::Shader::OPCODE_ELSE);
1761 }
1762
1763 condition->traverse(this);
1764 Temporary result(this);
1765 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
1766 emit(sw::Shader::OPCODE_IF, 0, &result);
1767 nbCases++;
1768
1769 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1770 {
1771 (*caseIt)->traverse(this);
1772 if((*caseIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1773 {
1774 break;
1775 }
1776 }
1777 }
1778 else
1779 {
1780 defaultIt = it; // The default case might not be the last case, keep it for last
1781 }
1782 }
1783 }
1784
1785 // If there's a default case, traverse it here
1786 if(defaultIt != sequence.end())
1787 {
1788 emit(sw::Shader::OPCODE_ELSE);
1789 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
1790 {
1791 (*defaultIt)->traverse(this);
1792 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1793 {
1794 break;
1795 }
1796 }
1797 }
1798
1799 for(int i = 0; i < nbCases; ++i)
1800 {
1801 emit(sw::Shader::OPCODE_ENDIF);
1802 }
1803
1804 emit(sw::Shader::OPCODE_ENDSWITCH);
1805
1806 return false;
1807 }
1808
Nicolas Capens0bac2852016-05-07 06:09:58 -04001809 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
1810 {
1811 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
1812 }
1813
1814 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
1815 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
1816 {
1817 Instruction *instruction = new Instruction(op);
1818
1819 if(dst)
1820 {
Nicolas Capens0530b452017-11-15 16:39:47 -05001821 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001822 }
1823
Alexis Hetu929c6b02017-11-07 16:04:25 -05001824 if(src0)
1825 {
1826 TIntermTyped* src = src0->getAsTyped();
1827 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
1828 }
1829
Nicolas Capens0530b452017-11-15 16:39:47 -05001830 source(instruction->src[0], src0, index0);
1831 source(instruction->src[1], src1, index1);
1832 source(instruction->src[2], src2, index2);
1833 source(instruction->src[3], src3, index3);
1834 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001835
1836 shader->append(instruction);
1837
1838 return instruction;
1839 }
1840
1841 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
1842 {
1843 return emitCast(dst, 0, src, 0);
1844 }
1845
1846 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
1847 {
1848 switch(src->getBasicType())
1849 {
1850 case EbtBool:
1851 switch(dst->getBasicType())
1852 {
1853 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1854 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1855 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
1856 default: break;
1857 }
1858 break;
1859 case EbtInt:
1860 switch(dst->getBasicType())
1861 {
1862 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1863 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
1864 default: break;
1865 }
1866 break;
1867 case EbtUInt:
1868 switch(dst->getBasicType())
1869 {
1870 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1871 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
1872 default: break;
1873 }
1874 break;
1875 case EbtFloat:
1876 switch(dst->getBasicType())
1877 {
1878 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
1879 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
1880 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
1881 default: break;
1882 }
1883 break;
1884 default:
1885 break;
1886 }
1887
1888 ASSERT((src->getBasicType() == dst->getBasicType()) ||
1889 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
1890 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
1891
1892 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
1893 }
1894
1895 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1896 {
1897 for(int index = 0; index < dst->elementRegisterCount(); index++)
1898 {
1899 emit(op, dst, index, src0, index, src1, index, src2, index);
1900 }
1901 }
1902
1903 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1904 {
1905 emitBinary(op, result, src0, src1);
1906 assignLvalue(lhs, result);
1907 }
1908
1909 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1910 {
1911 sw::Shader::Opcode opcode;
1912 switch(left->getAsTyped()->getBasicType())
1913 {
1914 case EbtBool:
1915 case EbtInt:
1916 opcode = sw::Shader::OPCODE_ICMP;
1917 break;
1918 case EbtUInt:
1919 opcode = sw::Shader::OPCODE_UCMP;
1920 break;
1921 default:
1922 opcode = sw::Shader::OPCODE_CMP;
1923 break;
1924 }
1925
1926 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
1927 cmp->control = cmpOp;
1928 }
1929
1930 int componentCount(const TType &type, int registers)
1931 {
1932 if(registers == 0)
1933 {
1934 return 0;
1935 }
1936
1937 if(type.isArray() && registers >= type.elementRegisterCount())
1938 {
1939 int index = registers / type.elementRegisterCount();
1940 registers -= index * type.elementRegisterCount();
1941 return index * type.getElementSize() + componentCount(type, registers);
1942 }
1943
1944 if(type.isStruct() || type.isInterfaceBlock())
1945 {
1946 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
1947 int elements = 0;
1948
1949 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
1950 {
1951 const TType &fieldType = *((*field)->type());
1952
1953 if(fieldType.totalRegisterCount() <= registers)
1954 {
1955 registers -= fieldType.totalRegisterCount();
1956 elements += fieldType.getObjectSize();
1957 }
1958 else // Register within this field
1959 {
1960 return elements + componentCount(fieldType, registers);
1961 }
1962 }
1963 }
1964 else if(type.isMatrix())
1965 {
1966 return registers * type.registerSize();
1967 }
1968
1969 UNREACHABLE(0);
1970 return 0;
1971 }
1972
1973 int registerSize(const TType &type, int registers)
1974 {
1975 if(registers == 0)
1976 {
1977 if(type.isStruct())
1978 {
1979 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
1980 }
1981 else if(type.isInterfaceBlock())
1982 {
1983 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
1984 }
1985
1986 return type.registerSize();
1987 }
1988
1989 if(type.isArray() && registers >= type.elementRegisterCount())
1990 {
1991 int index = registers / type.elementRegisterCount();
1992 registers -= index * type.elementRegisterCount();
1993 return registerSize(type, registers);
1994 }
1995
1996 if(type.isStruct() || type.isInterfaceBlock())
1997 {
1998 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
1999 int elements = 0;
2000
2001 for(TFieldList::const_iterator field = fields.begin(); field != fields.end(); field++)
2002 {
2003 const TType &fieldType = *((*field)->type());
2004
2005 if(fieldType.totalRegisterCount() <= registers)
2006 {
2007 registers -= fieldType.totalRegisterCount();
2008 elements += fieldType.getObjectSize();
2009 }
2010 else // Register within this field
2011 {
2012 return registerSize(fieldType, registers);
2013 }
2014 }
2015 }
2016 else if(type.isMatrix())
2017 {
2018 return registerSize(type, 0);
2019 }
2020
2021 UNREACHABLE(0);
2022 return 0;
2023 }
2024
2025 int OutputASM::getBlockId(TIntermTyped *arg)
2026 {
2027 if(arg)
2028 {
2029 const TType &type = arg->getType();
2030 TInterfaceBlock* block = type.getInterfaceBlock();
2031 if(block && (type.getQualifier() == EvqUniform))
2032 {
2033 // Make sure the uniform block is declared
2034 uniformRegister(arg);
2035
2036 const char* blockName = block->name().c_str();
2037
2038 // Fetch uniform block index from array of blocks
2039 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2040 {
2041 if(blockName == it->name)
2042 {
2043 return it->blockId;
2044 }
2045 }
2046
2047 ASSERT(false);
2048 }
2049 }
2050
2051 return -1;
2052 }
2053
2054 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2055 {
2056 const TType &type = arg->getType();
2057 int blockId = getBlockId(arg);
2058 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2059 if(blockId != -1)
2060 {
2061 argumentInfo.bufferIndex = 0;
2062 for(int i = 0; i < blockId; ++i)
2063 {
2064 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2065 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2066 }
2067
2068 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2069
2070 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2071 BlockDefinitionIndexMap::const_iterator it = itEnd;
2072
2073 argumentInfo.clampedIndex = index;
2074 if(type.isInterfaceBlock())
2075 {
2076 // Offset index to the beginning of the selected instance
2077 int blockRegisters = type.elementRegisterCount();
2078 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2079 argumentInfo.bufferIndex += bufferOffset;
2080 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2081 }
2082
2083 int regIndex = registerIndex(arg);
2084 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2085 {
2086 it = blockDefinition.find(i);
2087 if(it != itEnd)
2088 {
2089 argumentInfo.clampedIndex -= (i - regIndex);
2090 break;
2091 }
2092 }
2093 ASSERT(it != itEnd);
2094
2095 argumentInfo.typedMemberInfo = it->second;
2096
2097 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2098 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2099 }
2100 else
2101 {
2102 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2103 }
2104
2105 return argumentInfo;
2106 }
2107
Nicolas Capens0530b452017-11-15 16:39:47 -05002108 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002109 {
2110 if(argument)
2111 {
2112 TIntermTyped *arg = argument->getAsTyped();
2113 Temporary unpackedUniform(this);
2114
2115 const TType& srcType = arg->getType();
2116 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2117 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2118 {
2119 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2120 const TType &memberType = argumentInfo.typedMemberInfo.type;
2121
2122 if(memberType.getBasicType() == EbtBool)
2123 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002124 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002125
2126 // Convert the packed bool, which is currently an int, to a true bool
2127 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2128 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2129 instruction->dst.index = registerIndex(&unpackedUniform);
2130 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2131 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2132 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2133
2134 shader->append(instruction);
2135
2136 arg = &unpackedUniform;
2137 index = 0;
2138 }
2139 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2140 {
2141 int numCols = memberType.getNominalSize();
2142 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002143
Alexis Hetue97a31e2016-11-14 14:10:47 -05002144 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002145
2146 unsigned int dstIndex = registerIndex(&unpackedUniform);
2147 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2148 int arrayIndex = argumentInfo.clampedIndex / numCols;
2149 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2150
2151 for(int j = 0; j < numRows; ++j)
2152 {
2153 // Transpose the row major matrix
2154 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2155 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2156 instruction->dst.index = dstIndex;
2157 instruction->dst.mask = 1 << j;
2158 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2159 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2160 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2161 instruction->src[0].swizzle = srcSwizzle;
2162
2163 shader->append(instruction);
2164 }
2165
2166 arg = &unpackedUniform;
2167 index = 0;
2168 }
2169 }
2170
2171 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2172 const TType &type = argumentInfo.typedMemberInfo.type;
2173
2174 int size = registerSize(type, argumentInfo.clampedIndex);
2175
2176 parameter.type = registerType(arg);
2177 parameter.bufferIndex = argumentInfo.bufferIndex;
2178
2179 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2180 {
2181 int component = componentCount(type, argumentInfo.clampedIndex);
2182 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2183
2184 for(int i = 0; i < 4; i++)
2185 {
2186 if(size == 1) // Replicate
2187 {
2188 parameter.value[i] = constants[component + 0].getAsFloat();
2189 }
2190 else if(i < size)
2191 {
2192 parameter.value[i] = constants[component + i].getAsFloat();
2193 }
2194 else
2195 {
2196 parameter.value[i] = 0.0f;
2197 }
2198 }
2199 }
2200 else
2201 {
2202 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2203
2204 if(parameter.bufferIndex != -1)
2205 {
2206 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2207 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2208 }
2209 }
2210
2211 if(!IsSampler(arg->getBasicType()))
2212 {
2213 parameter.swizzle = readSwizzle(arg, size);
2214 }
2215 }
2216 }
2217
Nicolas Capens0530b452017-11-15 16:39:47 -05002218 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2219 {
2220 parameter.type = registerType(arg);
2221 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002222 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002223 }
2224
Nicolas Capens0bac2852016-05-07 06:09:58 -04002225 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2226 {
2227 for(int index = 0; index < dst->totalRegisterCount(); index++)
2228 {
2229 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002230 }
2231 }
2232
2233 int swizzleElement(int swizzle, int index)
2234 {
2235 return (swizzle >> (index * 2)) & 0x03;
2236 }
2237
2238 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2239 {
2240 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2241 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2242 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2243 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2244 }
2245
2246 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2247 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002248 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2249 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002250 {
2251 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2252 }
2253
2254 TIntermBinary *binary = dst->getAsBinaryNode();
2255
2256 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2257 {
2258 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2259
Nicolas Capens6986b282017-11-16 10:38:19 -05002260 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002261
2262 insert->src[0].type = insert->dst.type;
2263 insert->src[0].index = insert->dst.index;
2264 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002265 source(insert->src[1], src);
2266 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002267
2268 shader->append(insert);
2269 }
2270 else
2271 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002272 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2273
Nicolas Capens6986b282017-11-16 10:38:19 -05002274 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002275
Nicolas Capens0530b452017-11-15 16:39:47 -05002276 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002277 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2278
2279 shader->append(mov1);
2280
2281 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002282 {
2283 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2284
Nicolas Capens84249fd2017-11-09 11:20:51 -05002285 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002286 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002287 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002288
Nicolas Capens0530b452017-11-15 16:39:47 -05002289 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002290
2291 shader->append(mov);
2292 }
2293 }
2294 }
2295
Nicolas Capensd469de22017-11-16 10:42:20 -05002296 void OutputASM::evaluateRvalue(TIntermTyped *node)
2297 {
2298 TIntermBinary *binary = node->getAsBinaryNode();
2299
2300 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2301 {
2302 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2303
2304 destination(insert->dst, node);
2305
2306 Temporary address(this);
2307 unsigned char mask;
2308 TIntermTyped *root = nullptr;
2309 unsigned int offset = 0;
2310 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2311
2312 source(insert->src[0], root, offset);
2313 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2314
2315 source(insert->src[1], binary->getRight());
2316
2317 shader->append(insert);
2318 }
2319 else
2320 {
2321 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2322
2323 destination(mov1->dst, node, 0);
2324
2325 Temporary address(this);
2326 unsigned char mask;
2327 TIntermTyped *root = nullptr;
2328 unsigned int offset = 0;
2329 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2330
2331 source(mov1->src[0], root, offset);
2332 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2333
2334 shader->append(mov1);
2335
2336 for(int i = 1; i < node->totalRegisterCount(); i++)
2337 {
2338 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2339 mov->src[0].rel = mov1->src[0].rel;
2340 }
2341 }
2342 }
2343
Nicolas Capens6986b282017-11-16 10:38:19 -05002344 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002345 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002346 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002347 TIntermTyped *root = nullptr;
2348 unsigned int offset = 0;
2349 unsigned char mask = 0xF;
2350 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2351
2352 dst.type = registerType(root);
2353 dst.index = registerIndex(root) + offset;
2354 dst.mask = mask;
2355
2356 return swizzle;
2357 }
2358
2359 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2360 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002361 TIntermTyped *result = node;
2362 TIntermBinary *binary = node->getAsBinaryNode();
2363 TIntermSymbol *symbol = node->getAsSymbolNode();
2364
2365 if(binary)
2366 {
2367 TIntermTyped *left = binary->getLeft();
2368 TIntermTyped *right = binary->getRight();
2369
Nicolas Capens0530b452017-11-15 16:39:47 -05002370 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002371
2372 switch(binary->getOp())
2373 {
2374 case EOpIndexDirect:
2375 {
2376 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2377
2378 if(left->isRegister())
2379 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002380 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002381
Nicolas Capens0530b452017-11-15 16:39:47 -05002382 mask = 1;
2383 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002384 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002385 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002386 }
2387
2388 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002389 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002390
2391 return element;
2392 }
2393 else if(left->isArray() || left->isMatrix())
2394 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002395 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002396 return 0xE4;
2397 }
2398 else UNREACHABLE(0);
2399 }
2400 break;
2401 case EOpIndexIndirect:
2402 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002403 right->traverse(this);
2404
Nicolas Capens0bac2852016-05-07 06:09:58 -04002405 if(left->isRegister())
2406 {
2407 // Requires INSERT instruction (handled by calling function)
2408 }
2409 else if(left->isArray() || left->isMatrix())
2410 {
2411 int scale = result->totalRegisterCount();
2412
Nicolas Capens0530b452017-11-15 16:39:47 -05002413 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002414 {
2415 if(left->totalRegisterCount() > 1)
2416 {
2417 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002418 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002419
Nicolas Capens0530b452017-11-15 16:39:47 -05002420 rel.index = relativeRegister.index;
2421 rel.type = relativeRegister.type;
2422 rel.scale = scale;
2423 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002424 }
2425 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002426 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002427 {
2428 if(scale == 1)
2429 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002430 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002431 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002432 mad->src[0].index = rel.index;
2433 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002434 }
2435 else
2436 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002437 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002438 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002439 mul->src[0].index = rel.index;
2440 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002441
2442 Constant newScale(scale);
2443 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2444 }
2445
Nicolas Capens0530b452017-11-15 16:39:47 -05002446 rel.type = sw::Shader::PARAMETER_TEMP;
2447 rel.index = registerIndex(&address);
2448 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002449 }
2450 else // Just add the new index to the address register
2451 {
2452 if(scale == 1)
2453 {
2454 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2455 }
2456 else
2457 {
2458 Constant newScale(scale);
2459 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2460 }
2461 }
2462 }
2463 else UNREACHABLE(0);
2464 }
2465 break;
2466 case EOpIndexDirectStruct:
2467 case EOpIndexDirectInterfaceBlock:
2468 {
2469 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2470 left->getType().getStruct()->fields() :
2471 left->getType().getInterfaceBlock()->fields();
2472 int index = right->getAsConstantUnion()->getIConst(0);
2473 int fieldOffset = 0;
2474
2475 for(int i = 0; i < index; i++)
2476 {
2477 fieldOffset += fields[i]->type()->totalRegisterCount();
2478 }
2479
Nicolas Capens0530b452017-11-15 16:39:47 -05002480 offset += fieldOffset;
2481 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002482
2483 return 0xE4;
2484 }
2485 break;
2486 case EOpVectorSwizzle:
2487 {
2488 ASSERT(left->isRegister());
2489
Nicolas Capens0530b452017-11-15 16:39:47 -05002490 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002491
2492 int swizzle = 0;
2493 int rightMask = 0;
2494
2495 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2496
2497 for(unsigned int i = 0; i < sequence.size(); i++)
2498 {
2499 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2500
2501 int element = swizzleElement(leftSwizzle, index);
2502 rightMask = rightMask | (1 << element);
2503 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2504 }
2505
Nicolas Capens0530b452017-11-15 16:39:47 -05002506 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002507
2508 return swizzle;
2509 }
2510 break;
2511 default:
2512 UNREACHABLE(binary->getOp()); // Not an l-value operator
2513 break;
2514 }
2515 }
2516 else if(symbol)
2517 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002518 root = symbol;
2519 offset = 0;
2520 mask = writeMask(symbol);
2521
2522 return 0xE4;
2523 }
2524 else
2525 {
2526 node->traverse(this);
2527
2528 root = node;
2529 offset = 0;
2530 mask = writeMask(node);
2531
Nicolas Capens0bac2852016-05-07 06:09:58 -04002532 return 0xE4;
2533 }
2534
2535 return 0xE4;
2536 }
2537
2538 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2539 {
2540 if(isSamplerRegister(operand))
2541 {
2542 return sw::Shader::PARAMETER_SAMPLER;
2543 }
2544
2545 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002546 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002547 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002548 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2549 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002550 {
2551 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2552 }
2553 outputQualifier = qualifier;
2554 }
2555
2556 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2557 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002558 // Constant arrays are in the constant register file.
2559 if(operand->isArray() && operand->getArraySize() > 1)
2560 {
2561 return sw::Shader::PARAMETER_CONST;
2562 }
2563 else
2564 {
2565 return sw::Shader::PARAMETER_TEMP;
2566 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002567 }
2568
2569 switch(qualifier)
2570 {
2571 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2572 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2573 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2574 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2575 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2576 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2577 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2578 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2579 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2580 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2581 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2582 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2583 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2584 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2585 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2586 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2587 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2588 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2589 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2590 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2591 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2592 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2593 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2594 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2595 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2596 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002597 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002598 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2599 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2600 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2601 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2602 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2603 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2604 default: UNREACHABLE(qualifier);
2605 }
2606
2607 return sw::Shader::PARAMETER_VOID;
2608 }
2609
Alexis Hetu12b00502016-05-20 13:01:11 -04002610 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2611 {
2612 const TQualifier qualifier = operand->getQualifier();
2613 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2614 }
2615
Nicolas Capens0bac2852016-05-07 06:09:58 -04002616 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2617 {
2618 if(isSamplerRegister(operand))
2619 {
2620 return samplerRegister(operand);
2621 }
2622
2623 switch(operand->getQualifier())
2624 {
2625 case EvqTemporary: return temporaryRegister(operand);
2626 case EvqGlobal: return temporaryRegister(operand);
2627 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2628 case EvqAttribute: return attributeRegister(operand);
2629 case EvqVaryingIn: return varyingRegister(operand);
2630 case EvqVaryingOut: return varyingRegister(operand);
2631 case EvqVertexIn: return attributeRegister(operand);
2632 case EvqFragmentOut: return fragmentOutputRegister(operand);
2633 case EvqVertexOut: return varyingRegister(operand);
2634 case EvqFragmentIn: return varyingRegister(operand);
2635 case EvqInvariantVaryingIn: return varyingRegister(operand);
2636 case EvqInvariantVaryingOut: return varyingRegister(operand);
2637 case EvqSmooth: return varyingRegister(operand);
2638 case EvqFlat: return varyingRegister(operand);
2639 case EvqCentroidOut: return varyingRegister(operand);
2640 case EvqSmoothIn: return varyingRegister(operand);
2641 case EvqFlatIn: return varyingRegister(operand);
2642 case EvqCentroidIn: return varyingRegister(operand);
2643 case EvqUniform: return uniformRegister(operand);
2644 case EvqIn: return temporaryRegister(operand);
2645 case EvqOut: return temporaryRegister(operand);
2646 case EvqInOut: return temporaryRegister(operand);
2647 case EvqConstReadOnly: return temporaryRegister(operand);
2648 case EvqPosition: return varyingRegister(operand);
2649 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002650 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2651 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2652 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2653 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002654 case EvqPointCoord: return varyingRegister(operand);
2655 case EvqFragColor: return 0;
2656 case EvqFragData: return fragmentOutputRegister(operand);
2657 case EvqFragDepth: return 0;
2658 default: UNREACHABLE(operand->getQualifier());
2659 }
2660
2661 return 0;
2662 }
2663
2664 int OutputASM::writeMask(TIntermTyped *destination, int index)
2665 {
2666 if(destination->getQualifier() == EvqPointSize)
2667 {
2668 return 0x2; // Point size stored in the y component
2669 }
2670
2671 return 0xF >> (4 - registerSize(destination->getType(), index));
2672 }
2673
2674 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2675 {
2676 if(argument->getQualifier() == EvqPointSize)
2677 {
2678 return 0x55; // Point size stored in the y component
2679 }
2680
2681 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2682
2683 return swizzleSize[size];
2684 }
2685
2686 // Conservatively checks whether an expression is fast to compute and has no side effects
2687 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2688 {
2689 if(!expression->isRegister())
2690 {
2691 return false;
2692 }
2693
2694 return cost(expression, budget) >= 0;
2695 }
2696
2697 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2698 int OutputASM::cost(TIntermNode *expression, int budget)
2699 {
2700 if(budget < 0)
2701 {
2702 return budget;
2703 }
2704
2705 if(expression->getAsSymbolNode())
2706 {
2707 return budget;
2708 }
2709 else if(expression->getAsConstantUnion())
2710 {
2711 return budget;
2712 }
2713 else if(expression->getAsBinaryNode())
2714 {
2715 TIntermBinary *binary = expression->getAsBinaryNode();
2716
2717 switch(binary->getOp())
2718 {
2719 case EOpVectorSwizzle:
2720 case EOpIndexDirect:
2721 case EOpIndexDirectStruct:
2722 case EOpIndexDirectInterfaceBlock:
2723 return cost(binary->getLeft(), budget - 0);
2724 case EOpAdd:
2725 case EOpSub:
2726 case EOpMul:
2727 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2728 default:
2729 return -1;
2730 }
2731 }
2732 else if(expression->getAsUnaryNode())
2733 {
2734 TIntermUnary *unary = expression->getAsUnaryNode();
2735
2736 switch(unary->getOp())
2737 {
2738 case EOpAbs:
2739 case EOpNegative:
2740 return cost(unary->getOperand(), budget - 1);
2741 default:
2742 return -1;
2743 }
2744 }
2745 else if(expression->getAsSelectionNode())
2746 {
2747 TIntermSelection *selection = expression->getAsSelectionNode();
2748
2749 if(selection->usesTernaryOperator())
2750 {
2751 TIntermTyped *condition = selection->getCondition();
2752 TIntermNode *trueBlock = selection->getTrueBlock();
2753 TIntermNode *falseBlock = selection->getFalseBlock();
2754 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2755
2756 if(constantCondition)
2757 {
2758 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2759
2760 if(trueCondition)
2761 {
2762 return cost(trueBlock, budget - 0);
2763 }
2764 else
2765 {
2766 return cost(falseBlock, budget - 0);
2767 }
2768 }
2769 else
2770 {
2771 return cost(trueBlock, cost(falseBlock, budget - 2));
2772 }
2773 }
2774 }
2775
2776 return -1;
2777 }
2778
2779 const Function *OutputASM::findFunction(const TString &name)
2780 {
2781 for(unsigned int f = 0; f < functionArray.size(); f++)
2782 {
2783 if(functionArray[f].name == name)
2784 {
2785 return &functionArray[f];
2786 }
2787 }
2788
2789 return 0;
2790 }
2791
2792 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2793 {
2794 return allocate(temporaries, temporary);
2795 }
2796
Alexis Hetu49351232017-11-02 16:00:32 -04002797 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
2798 {
2799 if(type.isStruct())
2800 {
2801 const TFieldList &fields = type.getStruct()->fields();
2802 int fieldVar = var;
2803 for(size_t i = 0; i < fields.size(); i++)
2804 {
2805 const TType& fieldType = *(fields[i]->type());
2806 setPixelShaderInputs(fieldType, fieldVar, flat);
2807 fieldVar += fieldType.totalRegisterCount();
2808 }
2809 }
2810 else
2811 {
2812 for(int i = 0; i < type.totalRegisterCount(); i++)
2813 {
2814 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2815 }
2816 }
2817 }
2818
Nicolas Capens0bac2852016-05-07 06:09:58 -04002819 int OutputASM::varyingRegister(TIntermTyped *varying)
2820 {
2821 int var = lookup(varyings, varying);
2822
2823 if(var == -1)
2824 {
2825 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002826 int registerCount = varying->totalRegisterCount();
2827
2828 if(pixelShader)
2829 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04002830 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002831 {
2832 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2833 return 0;
2834 }
2835
2836 if(varying->getQualifier() == EvqPointCoord)
2837 {
2838 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04002839 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002840 }
2841 else
2842 {
Alexis Hetu49351232017-11-02 16:00:32 -04002843 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002844 }
2845 }
2846 else if(vertexShader)
2847 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04002848 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002849 {
2850 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2851 return 0;
2852 }
2853
2854 if(varying->getQualifier() == EvqPosition)
2855 {
2856 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002857 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002858 }
2859 else if(varying->getQualifier() == EvqPointSize)
2860 {
2861 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002862 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002863 }
2864 else
2865 {
2866 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2867 }
2868 }
2869 else UNREACHABLE(0);
2870
2871 declareVarying(varying, var);
2872 }
2873
2874 return var;
2875 }
2876
2877 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2878 {
2879 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
2880 {
Alexis Hetu49351232017-11-02 16:00:32 -04002881 TIntermSymbol *symbol = varying->getAsSymbolNode();
2882 declareVarying(varying->getType(), symbol->getSymbol(), reg);
2883 }
2884 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002885
Alexis Hetu49351232017-11-02 16:00:32 -04002886 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
2887 {
2888 const char *name = varyingName.c_str();
2889 VaryingList &activeVaryings = shaderObject->varyings;
2890
2891 TStructure* structure = type.getStruct();
2892 if(structure)
2893 {
2894 int fieldRegisterIndex = registerIndex;
2895
2896 const TFieldList &fields = type.getStruct()->fields();
2897 for(size_t i = 0; i < fields.size(); i++)
2898 {
2899 const TType& fieldType = *(fields[i]->type());
2900 declareVarying(fieldType, varyingName + "." + fields[i]->name(), fieldRegisterIndex);
2901 if(fieldRegisterIndex >= 0)
2902 {
2903 fieldRegisterIndex += fieldType.totalRegisterCount();
2904 }
2905 }
2906 }
2907 else
2908 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002909 // Check if this varying has been declared before without having a register assigned
2910 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2911 {
2912 if(v->name == name)
2913 {
Alexis Hetu49351232017-11-02 16:00:32 -04002914 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002915 {
Alexis Hetu49351232017-11-02 16:00:32 -04002916 ASSERT(v->reg < 0 || v->reg == registerIndex);
2917 v->reg = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002918 }
2919
2920 return;
2921 }
2922 }
2923
Alexis Hetu49351232017-11-02 16:00:32 -04002924 activeVaryings.push_back(glsl::Varying(glVariableType(type), name, type.getArraySize(), registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002925 }
2926 }
2927
2928 int OutputASM::uniformRegister(TIntermTyped *uniform)
2929 {
2930 const TType &type = uniform->getType();
2931 ASSERT(!IsSampler(type.getBasicType()));
2932 TInterfaceBlock *block = type.getAsInterfaceBlock();
2933 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2934 ASSERT(symbol || block);
2935
2936 if(symbol || block)
2937 {
2938 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2939 bool isBlockMember = (!block && parentBlock);
2940 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2941
2942 if(index == -1 || isBlockMember)
2943 {
2944 if(index == -1)
2945 {
2946 index = allocate(uniforms, uniform);
2947 }
2948
2949 // Verify if the current uniform is a member of an already declared block
2950 const TString &name = symbol ? symbol->getSymbol() : block->name();
2951 int blockMemberIndex = blockMemberLookup(type, name, index);
2952 if(blockMemberIndex == -1)
2953 {
2954 declareUniform(type, name, index);
2955 }
2956 else
2957 {
2958 index = blockMemberIndex;
2959 }
2960 }
2961
2962 return index;
2963 }
2964
2965 return 0;
2966 }
2967
2968 int OutputASM::attributeRegister(TIntermTyped *attribute)
2969 {
2970 ASSERT(!attribute->isArray());
2971
2972 int index = lookup(attributes, attribute);
2973
2974 if(index == -1)
2975 {
2976 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2977 ASSERT(symbol);
2978
2979 if(symbol)
2980 {
2981 index = allocate(attributes, attribute);
2982 const TType &type = attribute->getType();
2983 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04002984 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2985 switch(type.getBasicType())
2986 {
2987 case EbtInt:
2988 attribType = sw::VertexShader::ATTRIBTYPE_INT;
2989 break;
2990 case EbtUInt:
2991 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
2992 break;
2993 case EbtFloat:
2994 default:
2995 break;
2996 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002997
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04002998 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002999 {
3000 for(int i = 0; i < registerCount; i++)
3001 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003002 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003003 }
3004 }
3005
3006 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3007
3008 const char *name = symbol->getSymbol().c_str();
3009 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3010 }
3011 }
3012
3013 return index;
3014 }
3015
3016 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3017 {
3018 return allocate(fragmentOutputs, fragmentOutput);
3019 }
3020
3021 int OutputASM::samplerRegister(TIntermTyped *sampler)
3022 {
3023 const TType &type = sampler->getType();
3024 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3025
3026 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3027 TIntermBinary *binary = sampler->getAsBinaryNode();
3028
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003029 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003030 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003031 switch(type.getQualifier())
3032 {
3033 case EvqUniform:
3034 return samplerRegister(symbol);
3035 case EvqIn:
3036 case EvqConstReadOnly:
3037 // Function arguments are not (uniform) sampler registers
3038 return -1;
3039 default:
3040 UNREACHABLE(type.getQualifier());
3041 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003042 }
3043 else if(binary)
3044 {
3045 TIntermTyped *left = binary->getLeft();
3046 TIntermTyped *right = binary->getRight();
3047 const TType &leftType = left->getType();
3048 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3049 int offset = 0;
3050
3051 switch(binary->getOp())
3052 {
3053 case EOpIndexDirect:
3054 ASSERT(left->isArray());
3055 offset = index * leftType.elementRegisterCount();
3056 break;
3057 case EOpIndexDirectStruct:
3058 ASSERT(leftType.isStruct());
3059 {
3060 const TFieldList &fields = leftType.getStruct()->fields();
3061
3062 for(int i = 0; i < index; i++)
3063 {
3064 offset += fields[i]->type()->totalRegisterCount();
3065 }
3066 }
3067 break;
3068 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3069 return -1;
3070 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3071 default:
3072 UNREACHABLE(binary->getOp());
3073 return -1;
3074 }
3075
3076 int base = samplerRegister(left);
3077
3078 if(base < 0)
3079 {
3080 return -1;
3081 }
3082
3083 return base + offset;
3084 }
3085
3086 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003087 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003088 }
3089
3090 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3091 {
3092 const TType &type = sampler->getType();
3093 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3094
3095 int index = lookup(samplers, sampler);
3096
3097 if(index == -1)
3098 {
3099 index = allocate(samplers, sampler);
3100
3101 if(sampler->getQualifier() == EvqUniform)
3102 {
3103 const char *name = sampler->getSymbol().c_str();
3104 declareUniform(type, name, index);
3105 }
3106 }
3107
3108 return index;
3109 }
3110
3111 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3112 {
3113 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3114 }
3115
3116 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3117 {
3118 for(unsigned int i = 0; i < list.size(); i++)
3119 {
3120 if(list[i] == variable)
3121 {
3122 return i; // Pointer match
3123 }
3124 }
3125
3126 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3127 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3128
3129 if(varBlock)
3130 {
3131 for(unsigned int i = 0; i < list.size(); i++)
3132 {
3133 if(list[i])
3134 {
3135 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3136
3137 if(listBlock)
3138 {
3139 if(listBlock->name() == varBlock->name())
3140 {
3141 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3142 ASSERT(listBlock->fields() == varBlock->fields());
3143 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3144 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3145
3146 return i;
3147 }
3148 }
3149 }
3150 }
3151 }
3152 else if(varSymbol)
3153 {
3154 for(unsigned int i = 0; i < list.size(); i++)
3155 {
3156 if(list[i])
3157 {
3158 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3159
3160 if(listSymbol)
3161 {
3162 if(listSymbol->getId() == varSymbol->getId())
3163 {
3164 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3165 ASSERT(listSymbol->getType() == varSymbol->getType());
3166 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3167
3168 return i;
3169 }
3170 }
3171 }
3172 }
3173 }
3174
3175 return -1;
3176 }
3177
3178 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3179 {
3180 for(unsigned int i = 0; i < list.size(); i++)
3181 {
3182 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3183 {
3184 return i; // Pointer match
3185 }
3186 }
3187 return -1;
3188 }
3189
3190 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
3191 {
3192 int index = lookup(list, variable);
3193
3194 if(index == -1)
3195 {
3196 unsigned int registerCount = variable->blockRegisterCount();
3197
3198 for(unsigned int i = 0; i < list.size(); i++)
3199 {
3200 if(list[i] == 0)
3201 {
3202 unsigned int j = 1;
3203 for( ; j < registerCount && (i + j) < list.size(); j++)
3204 {
3205 if(list[i + j] != 0)
3206 {
3207 break;
3208 }
3209 }
3210
3211 if(j == registerCount) // Found free slots
3212 {
3213 for(unsigned int j = 0; j < registerCount; j++)
3214 {
3215 list[i + j] = variable;
3216 }
3217
3218 return i;
3219 }
3220 }
3221 }
3222
3223 index = list.size();
3224
3225 for(unsigned int i = 0; i < registerCount; i++)
3226 {
3227 list.push_back(variable);
3228 }
3229 }
3230
3231 return index;
3232 }
3233
3234 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3235 {
3236 int index = lookup(list, variable);
3237
3238 if(index >= 0)
3239 {
3240 list[index] = 0;
3241 }
3242 }
3243
3244 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3245 {
3246 const TInterfaceBlock *block = type.getInterfaceBlock();
3247
3248 if(block)
3249 {
3250 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3251 const TFieldList& fields = block->fields();
3252 const TString &blockName = block->name();
3253 int fieldRegisterIndex = registerIndex;
3254
3255 if(!type.isInterfaceBlock())
3256 {
3257 // This is a uniform that's part of a block, let's see if the block is already defined
3258 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3259 {
3260 if(activeUniformBlocks[i].name == blockName.c_str())
3261 {
3262 // The block is already defined, find the register for the current uniform and return it
3263 for(size_t j = 0; j < fields.size(); j++)
3264 {
3265 const TString &fieldName = fields[j]->name();
3266 if(fieldName == name)
3267 {
3268 return fieldRegisterIndex;
3269 }
3270
3271 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3272 }
3273
3274 ASSERT(false);
3275 return fieldRegisterIndex;
3276 }
3277 }
3278 }
3279 }
3280
3281 return -1;
3282 }
3283
3284 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int blockId, BlockLayoutEncoder* encoder)
3285 {
3286 const TStructure *structure = type.getStruct();
3287 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3288
3289 if(!structure && !block)
3290 {
3291 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3292 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3293 if(blockId >= 0)
3294 {
3295 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3296 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3297 }
3298 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3299 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3300 fieldRegisterIndex, blockId, blockInfo));
3301 if(IsSampler(type.getBasicType()))
3302 {
3303 for(int i = 0; i < type.totalRegisterCount(); i++)
3304 {
3305 shader->declareSampler(fieldRegisterIndex + i);
3306 }
3307 }
3308 }
3309 else if(block)
3310 {
3311 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3312 const TFieldList& fields = block->fields();
3313 const TString &blockName = block->name();
3314 int fieldRegisterIndex = registerIndex;
3315 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3316
3317 blockId = activeUniformBlocks.size();
3318 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3319 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3320 block->blockStorage(), isRowMajor, registerIndex, blockId));
3321 blockDefinitions.push_back(BlockDefinitionIndexMap());
3322
3323 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3324 currentBlockEncoder.enterAggregateType();
3325 for(size_t i = 0; i < fields.size(); i++)
3326 {
3327 const TType &fieldType = *(fields[i]->type());
3328 const TString &fieldName = fields[i]->name();
3329 if(isUniformBlockMember && (fieldName == name))
3330 {
3331 registerIndex = fieldRegisterIndex;
3332 }
3333
3334 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3335
3336 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, &currentBlockEncoder);
3337 fieldRegisterIndex += fieldType.totalRegisterCount();
3338 }
3339 currentBlockEncoder.exitAggregateType();
3340 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3341 }
3342 else
3343 {
3344 int fieldRegisterIndex = registerIndex;
3345
3346 const TFieldList& fields = structure->fields();
3347 if(type.isArray() && (structure || type.isInterfaceBlock()))
3348 {
3349 for(int i = 0; i < type.getArraySize(); i++)
3350 {
3351 if(encoder)
3352 {
3353 encoder->enterAggregateType();
3354 }
3355 for(size_t j = 0; j < fields.size(); j++)
3356 {
3357 const TType &fieldType = *(fields[j]->type());
3358 const TString &fieldName = fields[j]->name();
3359 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3360
3361 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3362 fieldRegisterIndex += fieldType.totalRegisterCount();
3363 }
3364 if(encoder)
3365 {
3366 encoder->exitAggregateType();
3367 }
3368 }
3369 }
3370 else
3371 {
3372 if(encoder)
3373 {
3374 encoder->enterAggregateType();
3375 }
3376 for(size_t i = 0; i < fields.size(); i++)
3377 {
3378 const TType &fieldType = *(fields[i]->type());
3379 const TString &fieldName = fields[i]->name();
3380 const TString uniformName = name + "." + fieldName;
3381
3382 declareUniform(fieldType, uniformName, fieldRegisterIndex, blockId, encoder);
3383 fieldRegisterIndex += fieldType.totalRegisterCount();
3384 }
3385 if(encoder)
3386 {
3387 encoder->exitAggregateType();
3388 }
3389 }
3390 }
3391 }
3392
3393 GLenum OutputASM::glVariableType(const TType &type)
3394 {
3395 switch(type.getBasicType())
3396 {
3397 case EbtFloat:
3398 if(type.isScalar())
3399 {
3400 return GL_FLOAT;
3401 }
3402 else if(type.isVector())
3403 {
3404 switch(type.getNominalSize())
3405 {
3406 case 2: return GL_FLOAT_VEC2;
3407 case 3: return GL_FLOAT_VEC3;
3408 case 4: return GL_FLOAT_VEC4;
3409 default: UNREACHABLE(type.getNominalSize());
3410 }
3411 }
3412 else if(type.isMatrix())
3413 {
3414 switch(type.getNominalSize())
3415 {
3416 case 2:
3417 switch(type.getSecondarySize())
3418 {
3419 case 2: return GL_FLOAT_MAT2;
3420 case 3: return GL_FLOAT_MAT2x3;
3421 case 4: return GL_FLOAT_MAT2x4;
3422 default: UNREACHABLE(type.getSecondarySize());
3423 }
3424 case 3:
3425 switch(type.getSecondarySize())
3426 {
3427 case 2: return GL_FLOAT_MAT3x2;
3428 case 3: return GL_FLOAT_MAT3;
3429 case 4: return GL_FLOAT_MAT3x4;
3430 default: UNREACHABLE(type.getSecondarySize());
3431 }
3432 case 4:
3433 switch(type.getSecondarySize())
3434 {
3435 case 2: return GL_FLOAT_MAT4x2;
3436 case 3: return GL_FLOAT_MAT4x3;
3437 case 4: return GL_FLOAT_MAT4;
3438 default: UNREACHABLE(type.getSecondarySize());
3439 }
3440 default: UNREACHABLE(type.getNominalSize());
3441 }
3442 }
3443 else UNREACHABLE(0);
3444 break;
3445 case EbtInt:
3446 if(type.isScalar())
3447 {
3448 return GL_INT;
3449 }
3450 else if(type.isVector())
3451 {
3452 switch(type.getNominalSize())
3453 {
3454 case 2: return GL_INT_VEC2;
3455 case 3: return GL_INT_VEC3;
3456 case 4: return GL_INT_VEC4;
3457 default: UNREACHABLE(type.getNominalSize());
3458 }
3459 }
3460 else UNREACHABLE(0);
3461 break;
3462 case EbtUInt:
3463 if(type.isScalar())
3464 {
3465 return GL_UNSIGNED_INT;
3466 }
3467 else if(type.isVector())
3468 {
3469 switch(type.getNominalSize())
3470 {
3471 case 2: return GL_UNSIGNED_INT_VEC2;
3472 case 3: return GL_UNSIGNED_INT_VEC3;
3473 case 4: return GL_UNSIGNED_INT_VEC4;
3474 default: UNREACHABLE(type.getNominalSize());
3475 }
3476 }
3477 else UNREACHABLE(0);
3478 break;
3479 case EbtBool:
3480 if(type.isScalar())
3481 {
3482 return GL_BOOL;
3483 }
3484 else if(type.isVector())
3485 {
3486 switch(type.getNominalSize())
3487 {
3488 case 2: return GL_BOOL_VEC2;
3489 case 3: return GL_BOOL_VEC3;
3490 case 4: return GL_BOOL_VEC4;
3491 default: UNREACHABLE(type.getNominalSize());
3492 }
3493 }
3494 else UNREACHABLE(0);
3495 break;
3496 case EbtSampler2D:
3497 return GL_SAMPLER_2D;
3498 case EbtISampler2D:
3499 return GL_INT_SAMPLER_2D;
3500 case EbtUSampler2D:
3501 return GL_UNSIGNED_INT_SAMPLER_2D;
3502 case EbtSamplerCube:
3503 return GL_SAMPLER_CUBE;
3504 case EbtISamplerCube:
3505 return GL_INT_SAMPLER_CUBE;
3506 case EbtUSamplerCube:
3507 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3508 case EbtSamplerExternalOES:
3509 return GL_SAMPLER_EXTERNAL_OES;
3510 case EbtSampler3D:
3511 return GL_SAMPLER_3D_OES;
3512 case EbtISampler3D:
3513 return GL_INT_SAMPLER_3D;
3514 case EbtUSampler3D:
3515 return GL_UNSIGNED_INT_SAMPLER_3D;
3516 case EbtSampler2DArray:
3517 return GL_SAMPLER_2D_ARRAY;
3518 case EbtISampler2DArray:
3519 return GL_INT_SAMPLER_2D_ARRAY;
3520 case EbtUSampler2DArray:
3521 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3522 case EbtSampler2DShadow:
3523 return GL_SAMPLER_2D_SHADOW;
3524 case EbtSamplerCubeShadow:
3525 return GL_SAMPLER_CUBE_SHADOW;
3526 case EbtSampler2DArrayShadow:
3527 return GL_SAMPLER_2D_ARRAY_SHADOW;
3528 default:
3529 UNREACHABLE(type.getBasicType());
3530 break;
3531 }
3532
3533 return GL_NONE;
3534 }
3535
3536 GLenum OutputASM::glVariablePrecision(const TType &type)
3537 {
3538 if(type.getBasicType() == EbtFloat)
3539 {
3540 switch(type.getPrecision())
3541 {
3542 case EbpHigh: return GL_HIGH_FLOAT;
3543 case EbpMedium: return GL_MEDIUM_FLOAT;
3544 case EbpLow: return GL_LOW_FLOAT;
3545 case EbpUndefined:
3546 // Should be defined as the default precision by the parser
3547 default: UNREACHABLE(type.getPrecision());
3548 }
3549 }
3550 else if(type.getBasicType() == EbtInt)
3551 {
3552 switch(type.getPrecision())
3553 {
3554 case EbpHigh: return GL_HIGH_INT;
3555 case EbpMedium: return GL_MEDIUM_INT;
3556 case EbpLow: return GL_LOW_INT;
3557 case EbpUndefined:
3558 // Should be defined as the default precision by the parser
3559 default: UNREACHABLE(type.getPrecision());
3560 }
3561 }
3562
3563 // Other types (boolean, sampler) don't have a precision
3564 return GL_NONE;
3565 }
3566
3567 int OutputASM::dim(TIntermNode *v)
3568 {
3569 TIntermTyped *vector = v->getAsTyped();
3570 ASSERT(vector && vector->isRegister());
3571 return vector->getNominalSize();
3572 }
3573
3574 int OutputASM::dim2(TIntermNode *m)
3575 {
3576 TIntermTyped *matrix = m->getAsTyped();
3577 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3578 return matrix->getSecondarySize();
3579 }
3580
3581 // Returns ~0u if no loop count could be determined
3582 unsigned int OutputASM::loopCount(TIntermLoop *node)
3583 {
3584 // Parse loops of the form:
3585 // for(int index = initial; index [comparator] limit; index += increment)
3586 TIntermSymbol *index = 0;
3587 TOperator comparator = EOpNull;
3588 int initial = 0;
3589 int limit = 0;
3590 int increment = 0;
3591
3592 // Parse index name and intial value
3593 if(node->getInit())
3594 {
3595 TIntermAggregate *init = node->getInit()->getAsAggregate();
3596
3597 if(init)
3598 {
3599 TIntermSequence &sequence = init->getSequence();
3600 TIntermTyped *variable = sequence[0]->getAsTyped();
3601
Nicolas Capense3f05552017-05-24 10:45:56 -04003602 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003603 {
3604 TIntermBinary *assign = variable->getAsBinaryNode();
3605
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003606 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003607 {
3608 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3609 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3610
3611 if(symbol && constant)
3612 {
3613 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3614 {
3615 index = symbol;
3616 initial = constant->getUnionArrayPointer()[0].getIConst();
3617 }
3618 }
3619 }
3620 }
3621 }
3622 }
3623
3624 // Parse comparator and limit value
3625 if(index && node->getCondition())
3626 {
3627 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003628 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003629
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003630 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003631 {
3632 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3633
3634 if(constant)
3635 {
3636 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3637 {
3638 comparator = test->getOp();
3639 limit = constant->getUnionArrayPointer()[0].getIConst();
3640 }
3641 }
3642 }
3643 }
3644
3645 // Parse increment
3646 if(index && comparator != EOpNull && node->getExpression())
3647 {
3648 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3649 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3650
3651 if(binaryTerminal)
3652 {
3653 TOperator op = binaryTerminal->getOp();
3654 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3655
3656 if(constant)
3657 {
3658 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3659 {
3660 int value = constant->getUnionArrayPointer()[0].getIConst();
3661
3662 switch(op)
3663 {
3664 case EOpAddAssign: increment = value; break;
3665 case EOpSubAssign: increment = -value; break;
3666 default: UNIMPLEMENTED();
3667 }
3668 }
3669 }
3670 }
3671 else if(unaryTerminal)
3672 {
3673 TOperator op = unaryTerminal->getOp();
3674
3675 switch(op)
3676 {
3677 case EOpPostIncrement: increment = 1; break;
3678 case EOpPostDecrement: increment = -1; break;
3679 case EOpPreIncrement: increment = 1; break;
3680 case EOpPreDecrement: increment = -1; break;
3681 default: UNIMPLEMENTED();
3682 }
3683 }
3684 }
3685
3686 if(index && comparator != EOpNull && increment != 0)
3687 {
3688 if(comparator == EOpLessThanEqual)
3689 {
3690 comparator = EOpLessThan;
3691 limit += 1;
3692 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003693 else if(comparator == EOpGreaterThanEqual)
3694 {
3695 comparator = EOpLessThan;
3696 limit -= 1;
3697 std::swap(initial, limit);
3698 increment = -increment;
3699 }
3700 else if(comparator == EOpGreaterThan)
3701 {
3702 comparator = EOpLessThan;
3703 std::swap(initial, limit);
3704 increment = -increment;
3705 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003706
3707 if(comparator == EOpLessThan)
3708 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003709 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003710 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003711 return 0;
3712 }
3713
3714 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3715
3716 if(iterations < 0)
3717 {
3718 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003719 }
3720
3721 return iterations;
3722 }
3723 else UNIMPLEMENTED(); // Falls through
3724 }
3725
3726 return ~0u;
3727 }
3728
3729 bool LoopUnrollable::traverse(TIntermNode *node)
3730 {
3731 loopDepth = 0;
3732 loopUnrollable = true;
3733
3734 node->traverse(this);
3735
3736 return loopUnrollable;
3737 }
3738
3739 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3740 {
3741 if(visit == PreVisit)
3742 {
3743 loopDepth++;
3744 }
3745 else if(visit == PostVisit)
3746 {
3747 loopDepth++;
3748 }
3749
3750 return true;
3751 }
3752
3753 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3754 {
3755 if(!loopUnrollable)
3756 {
3757 return false;
3758 }
3759
3760 if(!loopDepth)
3761 {
3762 return true;
3763 }
3764
3765 switch(node->getFlowOp())
3766 {
3767 case EOpKill:
3768 case EOpReturn:
3769 break;
3770 case EOpBreak:
3771 case EOpContinue:
3772 loopUnrollable = false;
3773 break;
3774 default: UNREACHABLE(node->getFlowOp());
3775 }
3776
3777 return loopUnrollable;
3778 }
3779
3780 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3781 {
3782 return loopUnrollable;
3783 }
3784}