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