blob: 2eb8329de6fbb56b88ba227ec5bf7d244b049e91 [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
1775 for(++caseIt; caseIt != sequence.end(); ++caseIt)
1776 {
1777 (*caseIt)->traverse(this);
1778 if((*caseIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1779 {
1780 break;
1781 }
1782 }
1783 }
1784 else
1785 {
1786 defaultIt = it; // The default case might not be the last case, keep it for last
1787 }
1788 }
1789 }
1790
1791 // If there's a default case, traverse it here
1792 if(defaultIt != sequence.end())
1793 {
1794 emit(sw::Shader::OPCODE_ELSE);
1795 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
1796 {
1797 (*defaultIt)->traverse(this);
1798 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
1799 {
1800 break;
1801 }
1802 }
1803 }
1804
1805 for(int i = 0; i < nbCases; ++i)
1806 {
1807 emit(sw::Shader::OPCODE_ENDIF);
1808 }
1809
1810 emit(sw::Shader::OPCODE_ENDSWITCH);
1811
1812 return false;
1813 }
1814
Nicolas Capens0bac2852016-05-07 06:09:58 -04001815 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
1816 {
1817 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
1818 }
1819
1820 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
1821 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
1822 {
1823 Instruction *instruction = new Instruction(op);
1824
1825 if(dst)
1826 {
Nicolas Capens0530b452017-11-15 16:39:47 -05001827 destination(instruction->dst, dst, dstIndex);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001828 }
1829
Alexis Hetu929c6b02017-11-07 16:04:25 -05001830 if(src0)
1831 {
1832 TIntermTyped* src = src0->getAsTyped();
1833 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
1834 }
1835
Nicolas Capens0530b452017-11-15 16:39:47 -05001836 source(instruction->src[0], src0, index0);
1837 source(instruction->src[1], src1, index1);
1838 source(instruction->src[2], src2, index2);
1839 source(instruction->src[3], src3, index3);
1840 source(instruction->src[4], src4, index4);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001841
1842 shader->append(instruction);
1843
1844 return instruction;
1845 }
1846
1847 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
1848 {
1849 return emitCast(dst, 0, src, 0);
1850 }
1851
1852 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
1853 {
1854 switch(src->getBasicType())
1855 {
1856 case EbtBool:
1857 switch(dst->getBasicType())
1858 {
1859 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1860 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
1861 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
1862 default: break;
1863 }
1864 break;
1865 case EbtInt:
1866 switch(dst->getBasicType())
1867 {
1868 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1869 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
1870 default: break;
1871 }
1872 break;
1873 case EbtUInt:
1874 switch(dst->getBasicType())
1875 {
1876 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
1877 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
1878 default: break;
1879 }
1880 break;
1881 case EbtFloat:
1882 switch(dst->getBasicType())
1883 {
1884 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
1885 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
1886 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
1887 default: break;
1888 }
1889 break;
1890 default:
1891 break;
1892 }
1893
1894 ASSERT((src->getBasicType() == dst->getBasicType()) ||
1895 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
1896 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
1897
1898 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
1899 }
1900
1901 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1902 {
1903 for(int index = 0; index < dst->elementRegisterCount(); index++)
1904 {
1905 emit(op, dst, index, src0, index, src1, index, src2, index);
1906 }
1907 }
1908
1909 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1910 {
1911 emitBinary(op, result, src0, src1);
1912 assignLvalue(lhs, result);
1913 }
1914
1915 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1916 {
1917 sw::Shader::Opcode opcode;
1918 switch(left->getAsTyped()->getBasicType())
1919 {
1920 case EbtBool:
1921 case EbtInt:
1922 opcode = sw::Shader::OPCODE_ICMP;
1923 break;
1924 case EbtUInt:
1925 opcode = sw::Shader::OPCODE_UCMP;
1926 break;
1927 default:
1928 opcode = sw::Shader::OPCODE_CMP;
1929 break;
1930 }
1931
1932 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
1933 cmp->control = cmpOp;
1934 }
1935
1936 int componentCount(const TType &type, int registers)
1937 {
1938 if(registers == 0)
1939 {
1940 return 0;
1941 }
1942
1943 if(type.isArray() && registers >= type.elementRegisterCount())
1944 {
1945 int index = registers / type.elementRegisterCount();
1946 registers -= index * type.elementRegisterCount();
1947 return index * type.getElementSize() + componentCount(type, registers);
1948 }
1949
1950 if(type.isStruct() || type.isInterfaceBlock())
1951 {
1952 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
1953 int elements = 0;
1954
Alexis Hetuda163ed2018-01-03 16:36:14 -05001955 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001956 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05001957 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04001958
1959 if(fieldType.totalRegisterCount() <= registers)
1960 {
1961 registers -= fieldType.totalRegisterCount();
1962 elements += fieldType.getObjectSize();
1963 }
1964 else // Register within this field
1965 {
1966 return elements + componentCount(fieldType, registers);
1967 }
1968 }
1969 }
1970 else if(type.isMatrix())
1971 {
1972 return registers * type.registerSize();
1973 }
1974
1975 UNREACHABLE(0);
1976 return 0;
1977 }
1978
1979 int registerSize(const TType &type, int registers)
1980 {
1981 if(registers == 0)
1982 {
1983 if(type.isStruct())
1984 {
1985 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
1986 }
1987 else if(type.isInterfaceBlock())
1988 {
1989 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
1990 }
1991
1992 return type.registerSize();
1993 }
1994
1995 if(type.isArray() && registers >= type.elementRegisterCount())
1996 {
1997 int index = registers / type.elementRegisterCount();
1998 registers -= index * type.elementRegisterCount();
1999 return registerSize(type, registers);
2000 }
2001
2002 if(type.isStruct() || type.isInterfaceBlock())
2003 {
2004 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2005 int elements = 0;
2006
Alexis Hetuda163ed2018-01-03 16:36:14 -05002007 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002008 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002009 const TType &fieldType = *(field->type());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002010
2011 if(fieldType.totalRegisterCount() <= registers)
2012 {
2013 registers -= fieldType.totalRegisterCount();
2014 elements += fieldType.getObjectSize();
2015 }
2016 else // Register within this field
2017 {
2018 return registerSize(fieldType, registers);
2019 }
2020 }
2021 }
2022 else if(type.isMatrix())
2023 {
2024 return registerSize(type, 0);
2025 }
2026
2027 UNREACHABLE(0);
2028 return 0;
2029 }
2030
2031 int OutputASM::getBlockId(TIntermTyped *arg)
2032 {
2033 if(arg)
2034 {
2035 const TType &type = arg->getType();
2036 TInterfaceBlock* block = type.getInterfaceBlock();
2037 if(block && (type.getQualifier() == EvqUniform))
2038 {
2039 // Make sure the uniform block is declared
2040 uniformRegister(arg);
2041
2042 const char* blockName = block->name().c_str();
2043
2044 // Fetch uniform block index from array of blocks
2045 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2046 {
2047 if(blockName == it->name)
2048 {
2049 return it->blockId;
2050 }
2051 }
2052
2053 ASSERT(false);
2054 }
2055 }
2056
2057 return -1;
2058 }
2059
2060 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2061 {
2062 const TType &type = arg->getType();
2063 int blockId = getBlockId(arg);
2064 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2065 if(blockId != -1)
2066 {
2067 argumentInfo.bufferIndex = 0;
2068 for(int i = 0; i < blockId; ++i)
2069 {
2070 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2071 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2072 }
2073
2074 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2075
2076 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2077 BlockDefinitionIndexMap::const_iterator it = itEnd;
2078
2079 argumentInfo.clampedIndex = index;
2080 if(type.isInterfaceBlock())
2081 {
2082 // Offset index to the beginning of the selected instance
2083 int blockRegisters = type.elementRegisterCount();
2084 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2085 argumentInfo.bufferIndex += bufferOffset;
2086 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2087 }
2088
2089 int regIndex = registerIndex(arg);
2090 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2091 {
2092 it = blockDefinition.find(i);
2093 if(it != itEnd)
2094 {
2095 argumentInfo.clampedIndex -= (i - regIndex);
2096 break;
2097 }
2098 }
2099 ASSERT(it != itEnd);
2100
2101 argumentInfo.typedMemberInfo = it->second;
2102
2103 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2104 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2105 }
2106 else
2107 {
2108 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2109 }
2110
2111 return argumentInfo;
2112 }
2113
Nicolas Capens0530b452017-11-15 16:39:47 -05002114 void OutputASM::source(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002115 {
2116 if(argument)
2117 {
2118 TIntermTyped *arg = argument->getAsTyped();
2119 Temporary unpackedUniform(this);
2120
2121 const TType& srcType = arg->getType();
2122 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2123 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2124 {
2125 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2126 const TType &memberType = argumentInfo.typedMemberInfo.type;
2127
2128 if(memberType.getBasicType() == EbtBool)
2129 {
Alexis Hetue97a31e2016-11-14 14:10:47 -05002130 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002131
2132 // Convert the packed bool, which is currently an int, to a true bool
2133 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2134 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2135 instruction->dst.index = registerIndex(&unpackedUniform);
2136 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2137 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2138 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2139
2140 shader->append(instruction);
2141
2142 arg = &unpackedUniform;
2143 index = 0;
2144 }
2145 else if((srcBlock->matrixPacking() == EmpRowMajor) && memberType.isMatrix())
2146 {
2147 int numCols = memberType.getNominalSize();
2148 int numRows = memberType.getSecondarySize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002149
Alexis Hetue97a31e2016-11-14 14:10:47 -05002150 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
Nicolas Capens0bac2852016-05-07 06:09:58 -04002151
2152 unsigned int dstIndex = registerIndex(&unpackedUniform);
2153 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2154 int arrayIndex = argumentInfo.clampedIndex / numCols;
2155 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2156
2157 for(int j = 0; j < numRows; ++j)
2158 {
2159 // Transpose the row major matrix
2160 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2161 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2162 instruction->dst.index = dstIndex;
2163 instruction->dst.mask = 1 << j;
2164 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2165 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2166 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2167 instruction->src[0].swizzle = srcSwizzle;
2168
2169 shader->append(instruction);
2170 }
2171
2172 arg = &unpackedUniform;
2173 index = 0;
2174 }
2175 }
2176
2177 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2178 const TType &type = argumentInfo.typedMemberInfo.type;
2179
2180 int size = registerSize(type, argumentInfo.clampedIndex);
2181
2182 parameter.type = registerType(arg);
2183 parameter.bufferIndex = argumentInfo.bufferIndex;
2184
2185 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2186 {
2187 int component = componentCount(type, argumentInfo.clampedIndex);
2188 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2189
2190 for(int i = 0; i < 4; i++)
2191 {
2192 if(size == 1) // Replicate
2193 {
2194 parameter.value[i] = constants[component + 0].getAsFloat();
2195 }
2196 else if(i < size)
2197 {
2198 parameter.value[i] = constants[component + i].getAsFloat();
2199 }
2200 else
2201 {
2202 parameter.value[i] = 0.0f;
2203 }
2204 }
2205 }
2206 else
2207 {
2208 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2209
2210 if(parameter.bufferIndex != -1)
2211 {
2212 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2213 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2214 }
2215 }
2216
2217 if(!IsSampler(arg->getBasicType()))
2218 {
2219 parameter.swizzle = readSwizzle(arg, size);
2220 }
2221 }
2222 }
2223
Nicolas Capens0530b452017-11-15 16:39:47 -05002224 void OutputASM::destination(sw::Shader::DestinationParameter &parameter, TIntermTyped *arg, int index)
2225 {
2226 parameter.type = registerType(arg);
2227 parameter.index = registerIndex(arg) + index;
Nicolas Capens3ae571e2017-11-16 15:28:14 -05002228 parameter.mask = writeMask(arg, index);
Nicolas Capens0530b452017-11-15 16:39:47 -05002229 }
2230
Nicolas Capens0bac2852016-05-07 06:09:58 -04002231 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2232 {
2233 for(int index = 0; index < dst->totalRegisterCount(); index++)
2234 {
2235 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002236 }
2237 }
2238
2239 int swizzleElement(int swizzle, int index)
2240 {
2241 return (swizzle >> (index * 2)) & 0x03;
2242 }
2243
2244 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2245 {
2246 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2247 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2248 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2249 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2250 }
2251
2252 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2253 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002254 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2255 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002256 {
2257 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2258 }
2259
2260 TIntermBinary *binary = dst->getAsBinaryNode();
2261
2262 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2263 {
2264 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2265
Nicolas Capens6986b282017-11-16 10:38:19 -05002266 lvalue(insert->dst, dst);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002267
2268 insert->src[0].type = insert->dst.type;
2269 insert->src[0].index = insert->dst.index;
2270 insert->src[0].rel = insert->dst.rel;
Nicolas Capens0530b452017-11-15 16:39:47 -05002271 source(insert->src[1], src);
2272 source(insert->src[2], binary->getRight());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002273
2274 shader->append(insert);
2275 }
2276 else
2277 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002278 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2279
Nicolas Capens6986b282017-11-16 10:38:19 -05002280 int swizzle = lvalue(mov1->dst, dst);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002281
Nicolas Capens0530b452017-11-15 16:39:47 -05002282 source(mov1->src[0], src);
Nicolas Capens84249fd2017-11-09 11:20:51 -05002283 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2284
2285 shader->append(mov1);
2286
2287 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002288 {
2289 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2290
Nicolas Capens84249fd2017-11-09 11:20:51 -05002291 mov->dst = mov1->dst;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002292 mov->dst.index += offset;
Nicolas Capens84249fd2017-11-09 11:20:51 -05002293 mov->dst.mask = writeMask(dst, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002294
Nicolas Capens0530b452017-11-15 16:39:47 -05002295 source(mov->src[0], src, offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002296
2297 shader->append(mov);
2298 }
2299 }
2300 }
2301
Nicolas Capensd469de22017-11-16 10:42:20 -05002302 void OutputASM::evaluateRvalue(TIntermTyped *node)
2303 {
2304 TIntermBinary *binary = node->getAsBinaryNode();
2305
2306 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2307 {
2308 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2309
2310 destination(insert->dst, node);
2311
2312 Temporary address(this);
2313 unsigned char mask;
2314 TIntermTyped *root = nullptr;
2315 unsigned int offset = 0;
2316 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2317
2318 source(insert->src[0], root, offset);
2319 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2320
2321 source(insert->src[1], binary->getRight());
2322
2323 shader->append(insert);
2324 }
2325 else
2326 {
2327 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2328
2329 destination(mov1->dst, node, 0);
2330
2331 Temporary address(this);
2332 unsigned char mask;
2333 TIntermTyped *root = nullptr;
2334 unsigned int offset = 0;
2335 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2336
2337 source(mov1->src[0], root, offset);
2338 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2339
2340 shader->append(mov1);
2341
2342 for(int i = 1; i < node->totalRegisterCount(); i++)
2343 {
2344 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2345 mov->src[0].rel = mov1->src[0].rel;
2346 }
2347 }
2348 }
2349
Nicolas Capens6986b282017-11-16 10:38:19 -05002350 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002351 {
Nicolas Capens6986b282017-11-16 10:38:19 -05002352 Temporary address(this);
Nicolas Capens0530b452017-11-15 16:39:47 -05002353 TIntermTyped *root = nullptr;
2354 unsigned int offset = 0;
2355 unsigned char mask = 0xF;
2356 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2357
2358 dst.type = registerType(root);
2359 dst.index = registerIndex(root) + offset;
2360 dst.mask = mask;
2361
2362 return swizzle;
2363 }
2364
2365 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2366 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002367 TIntermTyped *result = node;
2368 TIntermBinary *binary = node->getAsBinaryNode();
2369 TIntermSymbol *symbol = node->getAsSymbolNode();
2370
2371 if(binary)
2372 {
2373 TIntermTyped *left = binary->getLeft();
2374 TIntermTyped *right = binary->getRight();
2375
Nicolas Capens0530b452017-11-15 16:39:47 -05002376 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
Nicolas Capens0bac2852016-05-07 06:09:58 -04002377
2378 switch(binary->getOp())
2379 {
2380 case EOpIndexDirect:
2381 {
2382 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2383
2384 if(left->isRegister())
2385 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002386 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002387
Nicolas Capens0530b452017-11-15 16:39:47 -05002388 mask = 1;
2389 while((leftMask & mask) == 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002390 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002391 mask = mask << 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002392 }
2393
2394 int element = swizzleElement(leftSwizzle, rightIndex);
Nicolas Capens0530b452017-11-15 16:39:47 -05002395 mask = 1 << element;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002396
2397 return element;
2398 }
2399 else if(left->isArray() || left->isMatrix())
2400 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002401 offset += rightIndex * result->totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002402 return 0xE4;
2403 }
2404 else UNREACHABLE(0);
2405 }
2406 break;
2407 case EOpIndexIndirect:
2408 {
Nicolas Capens84249fd2017-11-09 11:20:51 -05002409 right->traverse(this);
2410
Nicolas Capens0bac2852016-05-07 06:09:58 -04002411 if(left->isRegister())
2412 {
2413 // Requires INSERT instruction (handled by calling function)
2414 }
2415 else if(left->isArray() || left->isMatrix())
2416 {
2417 int scale = result->totalRegisterCount();
2418
Nicolas Capens0530b452017-11-15 16:39:47 -05002419 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
Nicolas Capens0bac2852016-05-07 06:09:58 -04002420 {
2421 if(left->totalRegisterCount() > 1)
2422 {
2423 sw::Shader::SourceParameter relativeRegister;
Nicolas Capens0530b452017-11-15 16:39:47 -05002424 source(relativeRegister, right);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002425
Nicolas Capens0530b452017-11-15 16:39:47 -05002426 rel.index = relativeRegister.index;
2427 rel.type = relativeRegister.type;
2428 rel.scale = scale;
2429 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002430 }
2431 }
Nicolas Capens0530b452017-11-15 16:39:47 -05002432 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
Nicolas Capens0bac2852016-05-07 06:09:58 -04002433 {
2434 if(scale == 1)
2435 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002436 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002437 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
Nicolas Capens0530b452017-11-15 16:39:47 -05002438 mad->src[0].index = rel.index;
2439 mad->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002440 }
2441 else
2442 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002443 Constant oldScale((int)rel.scale);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002444 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
Nicolas Capens0530b452017-11-15 16:39:47 -05002445 mul->src[0].index = rel.index;
2446 mul->src[0].type = rel.type;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002447
2448 Constant newScale(scale);
2449 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2450 }
2451
Nicolas Capens0530b452017-11-15 16:39:47 -05002452 rel.type = sw::Shader::PARAMETER_TEMP;
2453 rel.index = registerIndex(&address);
2454 rel.scale = 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002455 }
2456 else // Just add the new index to the address register
2457 {
2458 if(scale == 1)
2459 {
2460 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2461 }
2462 else
2463 {
2464 Constant newScale(scale);
2465 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2466 }
2467 }
2468 }
2469 else UNREACHABLE(0);
2470 }
2471 break;
2472 case EOpIndexDirectStruct:
2473 case EOpIndexDirectInterfaceBlock:
2474 {
2475 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2476 left->getType().getStruct()->fields() :
2477 left->getType().getInterfaceBlock()->fields();
2478 int index = right->getAsConstantUnion()->getIConst(0);
2479 int fieldOffset = 0;
2480
2481 for(int i = 0; i < index; i++)
2482 {
2483 fieldOffset += fields[i]->type()->totalRegisterCount();
2484 }
2485
Nicolas Capens0530b452017-11-15 16:39:47 -05002486 offset += fieldOffset;
2487 mask = writeMask(result);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002488
2489 return 0xE4;
2490 }
2491 break;
2492 case EOpVectorSwizzle:
2493 {
2494 ASSERT(left->isRegister());
2495
Nicolas Capens0530b452017-11-15 16:39:47 -05002496 int leftMask = mask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002497
2498 int swizzle = 0;
2499 int rightMask = 0;
2500
2501 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2502
2503 for(unsigned int i = 0; i < sequence.size(); i++)
2504 {
2505 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2506
2507 int element = swizzleElement(leftSwizzle, index);
2508 rightMask = rightMask | (1 << element);
2509 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2510 }
2511
Nicolas Capens0530b452017-11-15 16:39:47 -05002512 mask = leftMask & rightMask;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002513
2514 return swizzle;
2515 }
2516 break;
2517 default:
2518 UNREACHABLE(binary->getOp()); // Not an l-value operator
2519 break;
2520 }
2521 }
2522 else if(symbol)
2523 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002524 root = symbol;
2525 offset = 0;
2526 mask = writeMask(symbol);
2527
2528 return 0xE4;
2529 }
2530 else
2531 {
2532 node->traverse(this);
2533
2534 root = node;
2535 offset = 0;
2536 mask = writeMask(node);
2537
Nicolas Capens0bac2852016-05-07 06:09:58 -04002538 return 0xE4;
2539 }
2540
2541 return 0xE4;
2542 }
2543
2544 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2545 {
2546 if(isSamplerRegister(operand))
2547 {
2548 return sw::Shader::PARAMETER_SAMPLER;
2549 }
2550
2551 const TQualifier qualifier = operand->getQualifier();
Nicolas Capens0530b452017-11-15 16:39:47 -05002552 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002553 {
Nicolas Capens0530b452017-11-15 16:39:47 -05002554 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2555 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
Nicolas Capens0bac2852016-05-07 06:09:58 -04002556 {
2557 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2558 }
2559 outputQualifier = qualifier;
2560 }
2561
2562 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2563 {
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05002564 // Constant arrays are in the constant register file.
2565 if(operand->isArray() && operand->getArraySize() > 1)
2566 {
2567 return sw::Shader::PARAMETER_CONST;
2568 }
2569 else
2570 {
2571 return sw::Shader::PARAMETER_TEMP;
2572 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002573 }
2574
2575 switch(qualifier)
2576 {
2577 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2578 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2579 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2580 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2581 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2582 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2583 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2584 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2585 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2586 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2587 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2588 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2589 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2590 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2591 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2592 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2593 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2594 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2595 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2596 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2597 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2598 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2599 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2600 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2601 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2602 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002603 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002604 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2605 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2606 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2607 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2608 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2609 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2610 default: UNREACHABLE(qualifier);
2611 }
2612
2613 return sw::Shader::PARAMETER_VOID;
2614 }
2615
Alexis Hetu12b00502016-05-20 13:01:11 -04002616 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2617 {
2618 const TQualifier qualifier = operand->getQualifier();
2619 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2620 }
2621
Nicolas Capens0bac2852016-05-07 06:09:58 -04002622 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2623 {
2624 if(isSamplerRegister(operand))
2625 {
2626 return samplerRegister(operand);
2627 }
2628
2629 switch(operand->getQualifier())
2630 {
2631 case EvqTemporary: return temporaryRegister(operand);
2632 case EvqGlobal: return temporaryRegister(operand);
2633 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2634 case EvqAttribute: return attributeRegister(operand);
2635 case EvqVaryingIn: return varyingRegister(operand);
2636 case EvqVaryingOut: return varyingRegister(operand);
2637 case EvqVertexIn: return attributeRegister(operand);
2638 case EvqFragmentOut: return fragmentOutputRegister(operand);
2639 case EvqVertexOut: return varyingRegister(operand);
2640 case EvqFragmentIn: return varyingRegister(operand);
2641 case EvqInvariantVaryingIn: return varyingRegister(operand);
2642 case EvqInvariantVaryingOut: return varyingRegister(operand);
2643 case EvqSmooth: return varyingRegister(operand);
2644 case EvqFlat: return varyingRegister(operand);
2645 case EvqCentroidOut: return varyingRegister(operand);
2646 case EvqSmoothIn: return varyingRegister(operand);
2647 case EvqFlatIn: return varyingRegister(operand);
2648 case EvqCentroidIn: return varyingRegister(operand);
2649 case EvqUniform: return uniformRegister(operand);
2650 case EvqIn: return temporaryRegister(operand);
2651 case EvqOut: return temporaryRegister(operand);
2652 case EvqInOut: return temporaryRegister(operand);
2653 case EvqConstReadOnly: return temporaryRegister(operand);
2654 case EvqPosition: return varyingRegister(operand);
2655 case EvqPointSize: return varyingRegister(operand);
Alexis Hetu877ddfc2017-07-25 17:48:00 -04002656 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2657 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2658 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2659 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002660 case EvqPointCoord: return varyingRegister(operand);
2661 case EvqFragColor: return 0;
2662 case EvqFragData: return fragmentOutputRegister(operand);
2663 case EvqFragDepth: return 0;
2664 default: UNREACHABLE(operand->getQualifier());
2665 }
2666
2667 return 0;
2668 }
2669
2670 int OutputASM::writeMask(TIntermTyped *destination, int index)
2671 {
2672 if(destination->getQualifier() == EvqPointSize)
2673 {
2674 return 0x2; // Point size stored in the y component
2675 }
2676
2677 return 0xF >> (4 - registerSize(destination->getType(), index));
2678 }
2679
2680 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2681 {
2682 if(argument->getQualifier() == EvqPointSize)
2683 {
2684 return 0x55; // Point size stored in the y component
2685 }
2686
2687 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2688
2689 return swizzleSize[size];
2690 }
2691
2692 // Conservatively checks whether an expression is fast to compute and has no side effects
2693 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2694 {
2695 if(!expression->isRegister())
2696 {
2697 return false;
2698 }
2699
2700 return cost(expression, budget) >= 0;
2701 }
2702
2703 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2704 int OutputASM::cost(TIntermNode *expression, int budget)
2705 {
2706 if(budget < 0)
2707 {
2708 return budget;
2709 }
2710
2711 if(expression->getAsSymbolNode())
2712 {
2713 return budget;
2714 }
2715 else if(expression->getAsConstantUnion())
2716 {
2717 return budget;
2718 }
2719 else if(expression->getAsBinaryNode())
2720 {
2721 TIntermBinary *binary = expression->getAsBinaryNode();
2722
2723 switch(binary->getOp())
2724 {
2725 case EOpVectorSwizzle:
2726 case EOpIndexDirect:
2727 case EOpIndexDirectStruct:
2728 case EOpIndexDirectInterfaceBlock:
2729 return cost(binary->getLeft(), budget - 0);
2730 case EOpAdd:
2731 case EOpSub:
2732 case EOpMul:
2733 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2734 default:
2735 return -1;
2736 }
2737 }
2738 else if(expression->getAsUnaryNode())
2739 {
2740 TIntermUnary *unary = expression->getAsUnaryNode();
2741
2742 switch(unary->getOp())
2743 {
2744 case EOpAbs:
2745 case EOpNegative:
2746 return cost(unary->getOperand(), budget - 1);
2747 default:
2748 return -1;
2749 }
2750 }
2751 else if(expression->getAsSelectionNode())
2752 {
2753 TIntermSelection *selection = expression->getAsSelectionNode();
2754
2755 if(selection->usesTernaryOperator())
2756 {
2757 TIntermTyped *condition = selection->getCondition();
2758 TIntermNode *trueBlock = selection->getTrueBlock();
2759 TIntermNode *falseBlock = selection->getFalseBlock();
2760 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
2761
2762 if(constantCondition)
2763 {
2764 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
2765
2766 if(trueCondition)
2767 {
2768 return cost(trueBlock, budget - 0);
2769 }
2770 else
2771 {
2772 return cost(falseBlock, budget - 0);
2773 }
2774 }
2775 else
2776 {
2777 return cost(trueBlock, cost(falseBlock, budget - 2));
2778 }
2779 }
2780 }
2781
2782 return -1;
2783 }
2784
2785 const Function *OutputASM::findFunction(const TString &name)
2786 {
2787 for(unsigned int f = 0; f < functionArray.size(); f++)
2788 {
2789 if(functionArray[f].name == name)
2790 {
2791 return &functionArray[f];
2792 }
2793 }
2794
2795 return 0;
2796 }
2797
2798 int OutputASM::temporaryRegister(TIntermTyped *temporary)
2799 {
2800 return allocate(temporaries, temporary);
2801 }
2802
Alexis Hetu49351232017-11-02 16:00:32 -04002803 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
2804 {
2805 if(type.isStruct())
2806 {
2807 const TFieldList &fields = type.getStruct()->fields();
2808 int fieldVar = var;
Alexis Hetuda163ed2018-01-03 16:36:14 -05002809 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04002810 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002811 const TType& fieldType = *(field->type());
Alexis Hetu49351232017-11-02 16:00:32 -04002812 setPixelShaderInputs(fieldType, fieldVar, flat);
2813 fieldVar += fieldType.totalRegisterCount();
2814 }
2815 }
2816 else
2817 {
2818 for(int i = 0; i < type.totalRegisterCount(); i++)
2819 {
2820 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
2821 }
2822 }
2823 }
2824
Nicolas Capens0bac2852016-05-07 06:09:58 -04002825 int OutputASM::varyingRegister(TIntermTyped *varying)
2826 {
2827 int var = lookup(varyings, varying);
2828
2829 if(var == -1)
2830 {
2831 var = allocate(varyings, varying);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002832 int registerCount = varying->totalRegisterCount();
2833
2834 if(pixelShader)
2835 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04002836 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002837 {
2838 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
2839 return 0;
2840 }
2841
2842 if(varying->getQualifier() == EvqPointCoord)
2843 {
2844 ASSERT(varying->isRegister());
Alexis Hetu49351232017-11-02 16:00:32 -04002845 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002846 }
2847 else
2848 {
Alexis Hetu49351232017-11-02 16:00:32 -04002849 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002850 }
2851 }
2852 else if(vertexShader)
2853 {
Nicolas Capensec0936c2016-05-18 12:32:02 -04002854 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002855 {
2856 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
2857 return 0;
2858 }
2859
2860 if(varying->getQualifier() == EvqPosition)
2861 {
2862 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002863 vertexShader->setPositionRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002864 }
2865 else if(varying->getQualifier() == EvqPointSize)
2866 {
2867 ASSERT(varying->isRegister());
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04002868 vertexShader->setPointSizeRegister(var);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002869 }
2870 else
2871 {
2872 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
2873 }
2874 }
2875 else UNREACHABLE(0);
2876
2877 declareVarying(varying, var);
2878 }
2879
2880 return var;
2881 }
2882
2883 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
2884 {
2885 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
2886 {
Alexis Hetu49351232017-11-02 16:00:32 -04002887 TIntermSymbol *symbol = varying->getAsSymbolNode();
2888 declareVarying(varying->getType(), symbol->getSymbol(), reg);
2889 }
2890 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04002891
Alexis Hetu49351232017-11-02 16:00:32 -04002892 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
2893 {
2894 const char *name = varyingName.c_str();
2895 VaryingList &activeVaryings = shaderObject->varyings;
2896
2897 TStructure* structure = type.getStruct();
2898 if(structure)
2899 {
2900 int fieldRegisterIndex = registerIndex;
2901
2902 const TFieldList &fields = type.getStruct()->fields();
Alexis Hetuda163ed2018-01-03 16:36:14 -05002903 for(const auto &field : fields)
Alexis Hetu49351232017-11-02 16:00:32 -04002904 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002905 const TType& fieldType = *(field->type());
2906 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
Alexis Hetu49351232017-11-02 16:00:32 -04002907 if(fieldRegisterIndex >= 0)
2908 {
2909 fieldRegisterIndex += fieldType.totalRegisterCount();
2910 }
2911 }
2912 }
2913 else
2914 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002915 // Check if this varying has been declared before without having a register assigned
2916 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
2917 {
2918 if(v->name == name)
2919 {
Alexis Hetu49351232017-11-02 16:00:32 -04002920 if(registerIndex >= 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002921 {
Alexis Hetu49351232017-11-02 16:00:32 -04002922 ASSERT(v->reg < 0 || v->reg == registerIndex);
2923 v->reg = registerIndex;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002924 }
2925
2926 return;
2927 }
2928 }
2929
Alexis Hetu743913c2018-01-04 11:54:55 -05002930 activeVaryings.push_back(glsl::Varying(glVariableType(type), name, type.getArraySize(), type.getQualifier(), registerIndex, 0));
Nicolas Capens0bac2852016-05-07 06:09:58 -04002931 }
2932 }
2933
2934 int OutputASM::uniformRegister(TIntermTyped *uniform)
2935 {
2936 const TType &type = uniform->getType();
2937 ASSERT(!IsSampler(type.getBasicType()));
2938 TInterfaceBlock *block = type.getAsInterfaceBlock();
2939 TIntermSymbol *symbol = uniform->getAsSymbolNode();
2940 ASSERT(symbol || block);
2941
2942 if(symbol || block)
2943 {
2944 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
2945 bool isBlockMember = (!block && parentBlock);
2946 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
2947
2948 if(index == -1 || isBlockMember)
2949 {
2950 if(index == -1)
2951 {
2952 index = allocate(uniforms, uniform);
2953 }
2954
2955 // Verify if the current uniform is a member of an already declared block
2956 const TString &name = symbol ? symbol->getSymbol() : block->name();
2957 int blockMemberIndex = blockMemberLookup(type, name, index);
2958 if(blockMemberIndex == -1)
2959 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05002960 declareUniform(type, name, index, false);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002961 }
2962 else
2963 {
2964 index = blockMemberIndex;
2965 }
2966 }
2967
2968 return index;
2969 }
2970
2971 return 0;
2972 }
2973
2974 int OutputASM::attributeRegister(TIntermTyped *attribute)
2975 {
2976 ASSERT(!attribute->isArray());
2977
2978 int index = lookup(attributes, attribute);
2979
2980 if(index == -1)
2981 {
2982 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2983 ASSERT(symbol);
2984
2985 if(symbol)
2986 {
2987 index = allocate(attributes, attribute);
2988 const TType &type = attribute->getType();
2989 int registerCount = attribute->totalRegisterCount();
Alexis Hetub7508b82016-09-22 15:36:45 -04002990 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
2991 switch(type.getBasicType())
2992 {
2993 case EbtInt:
2994 attribType = sw::VertexShader::ATTRIBTYPE_INT;
2995 break;
2996 case EbtUInt:
2997 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
2998 break;
2999 case EbtFloat:
3000 default:
3001 break;
3002 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003003
Nicolas Capensf0aef1a2016-05-18 14:44:21 -04003004 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003005 {
3006 for(int i = 0; i < registerCount; i++)
3007 {
Alexis Hetub7508b82016-09-22 15:36:45 -04003008 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003009 }
3010 }
3011
3012 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3013
3014 const char *name = symbol->getSymbol().c_str();
3015 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3016 }
3017 }
3018
3019 return index;
3020 }
3021
3022 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3023 {
3024 return allocate(fragmentOutputs, fragmentOutput);
3025 }
3026
3027 int OutputASM::samplerRegister(TIntermTyped *sampler)
3028 {
3029 const TType &type = sampler->getType();
3030 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3031
3032 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3033 TIntermBinary *binary = sampler->getAsBinaryNode();
3034
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003035 if(symbol)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003036 {
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003037 switch(type.getQualifier())
3038 {
3039 case EvqUniform:
3040 return samplerRegister(symbol);
3041 case EvqIn:
3042 case EvqConstReadOnly:
3043 // Function arguments are not (uniform) sampler registers
3044 return -1;
3045 default:
3046 UNREACHABLE(type.getQualifier());
3047 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003048 }
3049 else if(binary)
3050 {
3051 TIntermTyped *left = binary->getLeft();
3052 TIntermTyped *right = binary->getRight();
3053 const TType &leftType = left->getType();
3054 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3055 int offset = 0;
3056
3057 switch(binary->getOp())
3058 {
3059 case EOpIndexDirect:
3060 ASSERT(left->isArray());
Alexis Hetuda163ed2018-01-03 16:36:14 -05003061 offset = index * leftType.samplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003062 break;
3063 case EOpIndexDirectStruct:
3064 ASSERT(leftType.isStruct());
3065 {
3066 const TFieldList &fields = leftType.getStruct()->fields();
3067
3068 for(int i = 0; i < index; i++)
3069 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003070 offset += fields[i]->type()->totalSamplerRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003071 }
3072 }
3073 break;
3074 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3075 return -1;
3076 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3077 default:
3078 UNREACHABLE(binary->getOp());
3079 return -1;
3080 }
3081
3082 int base = samplerRegister(left);
3083
3084 if(base < 0)
3085 {
3086 return -1;
3087 }
3088
3089 return base + offset;
3090 }
3091
3092 UNREACHABLE(0);
Nicolas Capensfcb70fd2017-05-17 15:16:51 -04003093 return -1; // Not a (uniform) sampler register
Nicolas Capens0bac2852016-05-07 06:09:58 -04003094 }
3095
3096 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3097 {
3098 const TType &type = sampler->getType();
3099 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3100
3101 int index = lookup(samplers, sampler);
3102
3103 if(index == -1)
3104 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003105 index = allocate(samplers, sampler, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003106
3107 if(sampler->getQualifier() == EvqUniform)
3108 {
3109 const char *name = sampler->getSymbol().c_str();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003110 declareUniform(type, name, index, true);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003111 }
3112 }
3113
3114 return index;
3115 }
3116
3117 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3118 {
3119 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3120 }
3121
3122 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3123 {
3124 for(unsigned int i = 0; i < list.size(); i++)
3125 {
3126 if(list[i] == variable)
3127 {
3128 return i; // Pointer match
3129 }
3130 }
3131
3132 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3133 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3134
3135 if(varBlock)
3136 {
3137 for(unsigned int i = 0; i < list.size(); i++)
3138 {
3139 if(list[i])
3140 {
3141 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3142
3143 if(listBlock)
3144 {
3145 if(listBlock->name() == varBlock->name())
3146 {
3147 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3148 ASSERT(listBlock->fields() == varBlock->fields());
3149 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3150 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3151
3152 return i;
3153 }
3154 }
3155 }
3156 }
3157 }
3158 else if(varSymbol)
3159 {
3160 for(unsigned int i = 0; i < list.size(); i++)
3161 {
3162 if(list[i])
3163 {
3164 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3165
3166 if(listSymbol)
3167 {
3168 if(listSymbol->getId() == varSymbol->getId())
3169 {
3170 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3171 ASSERT(listSymbol->getType() == varSymbol->getType());
3172 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3173
3174 return i;
3175 }
3176 }
3177 }
3178 }
3179 }
3180
3181 return -1;
3182 }
3183
3184 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3185 {
3186 for(unsigned int i = 0; i < list.size(); i++)
3187 {
3188 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3189 {
3190 return i; // Pointer match
3191 }
3192 }
3193 return -1;
3194 }
3195
Alexis Hetuda163ed2018-01-03 16:36:14 -05003196 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003197 {
3198 int index = lookup(list, variable);
3199
3200 if(index == -1)
3201 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003202 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003203
3204 for(unsigned int i = 0; i < list.size(); i++)
3205 {
3206 if(list[i] == 0)
3207 {
3208 unsigned int j = 1;
3209 for( ; j < registerCount && (i + j) < list.size(); j++)
3210 {
3211 if(list[i + j] != 0)
3212 {
3213 break;
3214 }
3215 }
3216
3217 if(j == registerCount) // Found free slots
3218 {
3219 for(unsigned int j = 0; j < registerCount; j++)
3220 {
3221 list[i + j] = variable;
3222 }
3223
3224 return i;
3225 }
3226 }
3227 }
3228
3229 index = list.size();
3230
3231 for(unsigned int i = 0; i < registerCount; i++)
3232 {
3233 list.push_back(variable);
3234 }
3235 }
3236
3237 return index;
3238 }
3239
3240 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3241 {
3242 int index = lookup(list, variable);
3243
3244 if(index >= 0)
3245 {
3246 list[index] = 0;
3247 }
3248 }
3249
3250 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3251 {
3252 const TInterfaceBlock *block = type.getInterfaceBlock();
3253
3254 if(block)
3255 {
3256 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3257 const TFieldList& fields = block->fields();
3258 const TString &blockName = block->name();
3259 int fieldRegisterIndex = registerIndex;
3260
3261 if(!type.isInterfaceBlock())
3262 {
3263 // This is a uniform that's part of a block, let's see if the block is already defined
3264 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3265 {
3266 if(activeUniformBlocks[i].name == blockName.c_str())
3267 {
3268 // The block is already defined, find the register for the current uniform and return it
3269 for(size_t j = 0; j < fields.size(); j++)
3270 {
3271 const TString &fieldName = fields[j]->name();
3272 if(fieldName == name)
3273 {
3274 return fieldRegisterIndex;
3275 }
3276
3277 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3278 }
3279
3280 ASSERT(false);
3281 return fieldRegisterIndex;
3282 }
3283 }
3284 }
3285 }
3286
3287 return -1;
3288 }
3289
Alexis Hetuda163ed2018-01-03 16:36:14 -05003290 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003291 {
3292 const TStructure *structure = type.getStruct();
3293 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3294
3295 if(!structure && !block)
3296 {
3297 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3298 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3299 if(blockId >= 0)
3300 {
3301 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3302 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3303 }
3304 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
Alexis Hetuda163ed2018-01-03 16:36:14 -05003305 bool isSampler = IsSampler(type.getBasicType());
3306 if(isSampler && samplersOnly)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003307 {
3308 for(int i = 0; i < type.totalRegisterCount(); i++)
3309 {
3310 shader->declareSampler(fieldRegisterIndex + i);
3311 }
3312 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003313 if(!isSampler || samplersOnly)
3314 {
3315 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(),
3316 fieldRegisterIndex, blockId, blockInfo));
3317 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003318 }
3319 else if(block)
3320 {
3321 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3322 const TFieldList& fields = block->fields();
3323 const TString &blockName = block->name();
3324 int fieldRegisterIndex = registerIndex;
3325 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3326
3327 blockId = activeUniformBlocks.size();
3328 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3329 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3330 block->blockStorage(), isRowMajor, registerIndex, blockId));
3331 blockDefinitions.push_back(BlockDefinitionIndexMap());
3332
3333 Std140BlockEncoder currentBlockEncoder(isRowMajor);
3334 currentBlockEncoder.enterAggregateType();
Alexis Hetuda163ed2018-01-03 16:36:14 -05003335 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003336 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003337 const TType &fieldType = *(field->type());
3338 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003339 if(isUniformBlockMember && (fieldName == name))
3340 {
3341 registerIndex = fieldRegisterIndex;
3342 }
3343
3344 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3345
Alexis Hetuda163ed2018-01-03 16:36:14 -05003346 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, &currentBlockEncoder);
Nicolas Capens0bac2852016-05-07 06:09:58 -04003347 fieldRegisterIndex += fieldType.totalRegisterCount();
3348 }
3349 currentBlockEncoder.exitAggregateType();
3350 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3351 }
3352 else
3353 {
3354 int fieldRegisterIndex = registerIndex;
3355
3356 const TFieldList& fields = structure->fields();
3357 if(type.isArray() && (structure || type.isInterfaceBlock()))
3358 {
3359 for(int i = 0; i < type.getArraySize(); i++)
3360 {
3361 if(encoder)
3362 {
3363 encoder->enterAggregateType();
3364 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003365 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003366 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003367 const TType &fieldType = *(field->type());
3368 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003369 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3370
Alexis Hetuda163ed2018-01-03 16:36:14 -05003371 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3372 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003373 }
3374 if(encoder)
3375 {
3376 encoder->exitAggregateType();
3377 }
3378 }
3379 }
3380 else
3381 {
3382 if(encoder)
3383 {
3384 encoder->enterAggregateType();
3385 }
Alexis Hetuda163ed2018-01-03 16:36:14 -05003386 for(const auto &field : fields)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003387 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05003388 const TType &fieldType = *(field->type());
3389 const TString &fieldName = field->name();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003390 const TString uniformName = name + "." + fieldName;
3391
Alexis Hetuda163ed2018-01-03 16:36:14 -05003392 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3393 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
Nicolas Capens0bac2852016-05-07 06:09:58 -04003394 }
3395 if(encoder)
3396 {
3397 encoder->exitAggregateType();
3398 }
3399 }
3400 }
3401 }
3402
3403 GLenum OutputASM::glVariableType(const TType &type)
3404 {
3405 switch(type.getBasicType())
3406 {
3407 case EbtFloat:
3408 if(type.isScalar())
3409 {
3410 return GL_FLOAT;
3411 }
3412 else if(type.isVector())
3413 {
3414 switch(type.getNominalSize())
3415 {
3416 case 2: return GL_FLOAT_VEC2;
3417 case 3: return GL_FLOAT_VEC3;
3418 case 4: return GL_FLOAT_VEC4;
3419 default: UNREACHABLE(type.getNominalSize());
3420 }
3421 }
3422 else if(type.isMatrix())
3423 {
3424 switch(type.getNominalSize())
3425 {
3426 case 2:
3427 switch(type.getSecondarySize())
3428 {
3429 case 2: return GL_FLOAT_MAT2;
3430 case 3: return GL_FLOAT_MAT2x3;
3431 case 4: return GL_FLOAT_MAT2x4;
3432 default: UNREACHABLE(type.getSecondarySize());
3433 }
3434 case 3:
3435 switch(type.getSecondarySize())
3436 {
3437 case 2: return GL_FLOAT_MAT3x2;
3438 case 3: return GL_FLOAT_MAT3;
3439 case 4: return GL_FLOAT_MAT3x4;
3440 default: UNREACHABLE(type.getSecondarySize());
3441 }
3442 case 4:
3443 switch(type.getSecondarySize())
3444 {
3445 case 2: return GL_FLOAT_MAT4x2;
3446 case 3: return GL_FLOAT_MAT4x3;
3447 case 4: return GL_FLOAT_MAT4;
3448 default: UNREACHABLE(type.getSecondarySize());
3449 }
3450 default: UNREACHABLE(type.getNominalSize());
3451 }
3452 }
3453 else UNREACHABLE(0);
3454 break;
3455 case EbtInt:
3456 if(type.isScalar())
3457 {
3458 return GL_INT;
3459 }
3460 else if(type.isVector())
3461 {
3462 switch(type.getNominalSize())
3463 {
3464 case 2: return GL_INT_VEC2;
3465 case 3: return GL_INT_VEC3;
3466 case 4: return GL_INT_VEC4;
3467 default: UNREACHABLE(type.getNominalSize());
3468 }
3469 }
3470 else UNREACHABLE(0);
3471 break;
3472 case EbtUInt:
3473 if(type.isScalar())
3474 {
3475 return GL_UNSIGNED_INT;
3476 }
3477 else if(type.isVector())
3478 {
3479 switch(type.getNominalSize())
3480 {
3481 case 2: return GL_UNSIGNED_INT_VEC2;
3482 case 3: return GL_UNSIGNED_INT_VEC3;
3483 case 4: return GL_UNSIGNED_INT_VEC4;
3484 default: UNREACHABLE(type.getNominalSize());
3485 }
3486 }
3487 else UNREACHABLE(0);
3488 break;
3489 case EbtBool:
3490 if(type.isScalar())
3491 {
3492 return GL_BOOL;
3493 }
3494 else if(type.isVector())
3495 {
3496 switch(type.getNominalSize())
3497 {
3498 case 2: return GL_BOOL_VEC2;
3499 case 3: return GL_BOOL_VEC3;
3500 case 4: return GL_BOOL_VEC4;
3501 default: UNREACHABLE(type.getNominalSize());
3502 }
3503 }
3504 else UNREACHABLE(0);
3505 break;
3506 case EbtSampler2D:
3507 return GL_SAMPLER_2D;
3508 case EbtISampler2D:
3509 return GL_INT_SAMPLER_2D;
3510 case EbtUSampler2D:
3511 return GL_UNSIGNED_INT_SAMPLER_2D;
3512 case EbtSamplerCube:
3513 return GL_SAMPLER_CUBE;
3514 case EbtISamplerCube:
3515 return GL_INT_SAMPLER_CUBE;
3516 case EbtUSamplerCube:
3517 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3518 case EbtSamplerExternalOES:
3519 return GL_SAMPLER_EXTERNAL_OES;
3520 case EbtSampler3D:
3521 return GL_SAMPLER_3D_OES;
3522 case EbtISampler3D:
3523 return GL_INT_SAMPLER_3D;
3524 case EbtUSampler3D:
3525 return GL_UNSIGNED_INT_SAMPLER_3D;
3526 case EbtSampler2DArray:
3527 return GL_SAMPLER_2D_ARRAY;
3528 case EbtISampler2DArray:
3529 return GL_INT_SAMPLER_2D_ARRAY;
3530 case EbtUSampler2DArray:
3531 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3532 case EbtSampler2DShadow:
3533 return GL_SAMPLER_2D_SHADOW;
3534 case EbtSamplerCubeShadow:
3535 return GL_SAMPLER_CUBE_SHADOW;
3536 case EbtSampler2DArrayShadow:
3537 return GL_SAMPLER_2D_ARRAY_SHADOW;
3538 default:
3539 UNREACHABLE(type.getBasicType());
3540 break;
3541 }
3542
3543 return GL_NONE;
3544 }
3545
3546 GLenum OutputASM::glVariablePrecision(const TType &type)
3547 {
3548 if(type.getBasicType() == EbtFloat)
3549 {
3550 switch(type.getPrecision())
3551 {
3552 case EbpHigh: return GL_HIGH_FLOAT;
3553 case EbpMedium: return GL_MEDIUM_FLOAT;
3554 case EbpLow: return GL_LOW_FLOAT;
3555 case EbpUndefined:
3556 // Should be defined as the default precision by the parser
3557 default: UNREACHABLE(type.getPrecision());
3558 }
3559 }
3560 else if(type.getBasicType() == EbtInt)
3561 {
3562 switch(type.getPrecision())
3563 {
3564 case EbpHigh: return GL_HIGH_INT;
3565 case EbpMedium: return GL_MEDIUM_INT;
3566 case EbpLow: return GL_LOW_INT;
3567 case EbpUndefined:
3568 // Should be defined as the default precision by the parser
3569 default: UNREACHABLE(type.getPrecision());
3570 }
3571 }
3572
3573 // Other types (boolean, sampler) don't have a precision
3574 return GL_NONE;
3575 }
3576
3577 int OutputASM::dim(TIntermNode *v)
3578 {
3579 TIntermTyped *vector = v->getAsTyped();
3580 ASSERT(vector && vector->isRegister());
3581 return vector->getNominalSize();
3582 }
3583
3584 int OutputASM::dim2(TIntermNode *m)
3585 {
3586 TIntermTyped *matrix = m->getAsTyped();
3587 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3588 return matrix->getSecondarySize();
3589 }
3590
3591 // Returns ~0u if no loop count could be determined
3592 unsigned int OutputASM::loopCount(TIntermLoop *node)
3593 {
3594 // Parse loops of the form:
3595 // for(int index = initial; index [comparator] limit; index += increment)
3596 TIntermSymbol *index = 0;
3597 TOperator comparator = EOpNull;
3598 int initial = 0;
3599 int limit = 0;
3600 int increment = 0;
3601
3602 // Parse index name and intial value
3603 if(node->getInit())
3604 {
3605 TIntermAggregate *init = node->getInit()->getAsAggregate();
3606
3607 if(init)
3608 {
3609 TIntermSequence &sequence = init->getSequence();
3610 TIntermTyped *variable = sequence[0]->getAsTyped();
3611
Nicolas Capense3f05552017-05-24 10:45:56 -04003612 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003613 {
3614 TIntermBinary *assign = variable->getAsBinaryNode();
3615
Nicolas Capensd0bfd912017-05-24 10:20:24 -04003616 if(assign && assign->getOp() == EOpInitialize)
Nicolas Capens0bac2852016-05-07 06:09:58 -04003617 {
3618 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3619 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3620
3621 if(symbol && constant)
3622 {
3623 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3624 {
3625 index = symbol;
3626 initial = constant->getUnionArrayPointer()[0].getIConst();
3627 }
3628 }
3629 }
3630 }
3631 }
3632 }
3633
3634 // Parse comparator and limit value
3635 if(index && node->getCondition())
3636 {
3637 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003638 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003639
Alexis Hetu7be70cf2016-05-11 10:56:43 -04003640 if(left && (left->getId() == index->getId()))
Nicolas Capens0bac2852016-05-07 06:09:58 -04003641 {
3642 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3643
3644 if(constant)
3645 {
3646 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3647 {
3648 comparator = test->getOp();
3649 limit = constant->getUnionArrayPointer()[0].getIConst();
3650 }
3651 }
3652 }
3653 }
3654
3655 // Parse increment
3656 if(index && comparator != EOpNull && node->getExpression())
3657 {
3658 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3659 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3660
3661 if(binaryTerminal)
3662 {
3663 TOperator op = binaryTerminal->getOp();
3664 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3665
3666 if(constant)
3667 {
3668 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3669 {
3670 int value = constant->getUnionArrayPointer()[0].getIConst();
3671
3672 switch(op)
3673 {
3674 case EOpAddAssign: increment = value; break;
3675 case EOpSubAssign: increment = -value; break;
3676 default: UNIMPLEMENTED();
3677 }
3678 }
3679 }
3680 }
3681 else if(unaryTerminal)
3682 {
3683 TOperator op = unaryTerminal->getOp();
3684
3685 switch(op)
3686 {
3687 case EOpPostIncrement: increment = 1; break;
3688 case EOpPostDecrement: increment = -1; break;
3689 case EOpPreIncrement: increment = 1; break;
3690 case EOpPreDecrement: increment = -1; break;
3691 default: UNIMPLEMENTED();
3692 }
3693 }
3694 }
3695
3696 if(index && comparator != EOpNull && increment != 0)
3697 {
3698 if(comparator == EOpLessThanEqual)
3699 {
3700 comparator = EOpLessThan;
3701 limit += 1;
3702 }
Nicolas Capense3f05552017-05-24 10:45:56 -04003703 else if(comparator == EOpGreaterThanEqual)
3704 {
3705 comparator = EOpLessThan;
3706 limit -= 1;
3707 std::swap(initial, limit);
3708 increment = -increment;
3709 }
3710 else if(comparator == EOpGreaterThan)
3711 {
3712 comparator = EOpLessThan;
3713 std::swap(initial, limit);
3714 increment = -increment;
3715 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04003716
3717 if(comparator == EOpLessThan)
3718 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003719 if(!(initial < limit)) // Never loops
Nicolas Capens0bac2852016-05-07 06:09:58 -04003720 {
Nicolas Capens930b7002017-01-06 17:22:13 -05003721 return 0;
3722 }
3723
3724 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3725
3726 if(iterations < 0)
3727 {
3728 return ~0u;
Nicolas Capens0bac2852016-05-07 06:09:58 -04003729 }
3730
3731 return iterations;
3732 }
3733 else UNIMPLEMENTED(); // Falls through
3734 }
3735
3736 return ~0u;
3737 }
3738
3739 bool LoopUnrollable::traverse(TIntermNode *node)
3740 {
3741 loopDepth = 0;
3742 loopUnrollable = true;
3743
3744 node->traverse(this);
3745
3746 return loopUnrollable;
3747 }
3748
3749 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3750 {
3751 if(visit == PreVisit)
3752 {
3753 loopDepth++;
3754 }
3755 else if(visit == PostVisit)
3756 {
3757 loopDepth++;
3758 }
3759
3760 return true;
3761 }
3762
3763 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3764 {
3765 if(!loopUnrollable)
3766 {
3767 return false;
3768 }
3769
3770 if(!loopDepth)
3771 {
3772 return true;
3773 }
3774
3775 switch(node->getFlowOp())
3776 {
3777 case EOpKill:
3778 case EOpReturn:
3779 break;
3780 case EOpBreak:
3781 case EOpContinue:
3782 loopUnrollable = false;
3783 break;
3784 default: UNREACHABLE(node->getFlowOp());
3785 }
3786
3787 return loopUnrollable;
3788 }
3789
3790 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3791 {
3792 return loopUnrollable;
3793 }
3794}