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