1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "OutputASM.h"
16 #include "Common/Math.hpp"
18 #include "common/debug.h"
21 #include "libGLESv2/Shader.h"
23 #include <GLES2/gl2.h>
24 #include <GLES2/gl2ext.h>
25 #include <GLES3/gl3.h>
31 GLenum glVariableType(const TType &type)
33 switch(type.getBasicType())
40 else if(type.isVector())
42 switch(type.getNominalSize())
44 case 2: return GL_FLOAT_VEC2;
45 case 3: return GL_FLOAT_VEC3;
46 case 4: return GL_FLOAT_VEC4;
47 default: UNREACHABLE(type.getNominalSize());
50 else if(type.isMatrix())
52 switch(type.getNominalSize())
55 switch(type.getSecondarySize())
57 case 2: return GL_FLOAT_MAT2;
58 case 3: return GL_FLOAT_MAT2x3;
59 case 4: return GL_FLOAT_MAT2x4;
60 default: UNREACHABLE(type.getSecondarySize());
63 switch(type.getSecondarySize())
65 case 2: return GL_FLOAT_MAT3x2;
66 case 3: return GL_FLOAT_MAT3;
67 case 4: return GL_FLOAT_MAT3x4;
68 default: UNREACHABLE(type.getSecondarySize());
71 switch(type.getSecondarySize())
73 case 2: return GL_FLOAT_MAT4x2;
74 case 3: return GL_FLOAT_MAT4x3;
75 case 4: return GL_FLOAT_MAT4;
76 default: UNREACHABLE(type.getSecondarySize());
78 default: UNREACHABLE(type.getNominalSize());
88 else if(type.isVector())
90 switch(type.getNominalSize())
92 case 2: return GL_INT_VEC2;
93 case 3: return GL_INT_VEC3;
94 case 4: return GL_INT_VEC4;
95 default: UNREACHABLE(type.getNominalSize());
103 return GL_UNSIGNED_INT;
105 else if(type.isVector())
107 switch(type.getNominalSize())
109 case 2: return GL_UNSIGNED_INT_VEC2;
110 case 3: return GL_UNSIGNED_INT_VEC3;
111 case 4: return GL_UNSIGNED_INT_VEC4;
112 default: UNREACHABLE(type.getNominalSize());
122 else if(type.isVector())
124 switch(type.getNominalSize())
126 case 2: return GL_BOOL_VEC2;
127 case 3: return GL_BOOL_VEC3;
128 case 4: return GL_BOOL_VEC4;
129 default: UNREACHABLE(type.getNominalSize());
135 return GL_SAMPLER_2D;
137 return GL_INT_SAMPLER_2D;
139 return GL_UNSIGNED_INT_SAMPLER_2D;
141 return GL_SAMPLER_CUBE;
142 case EbtSampler2DRect:
143 return GL_SAMPLER_2D_RECT_ARB;
144 case EbtISamplerCube:
145 return GL_INT_SAMPLER_CUBE;
146 case EbtUSamplerCube:
147 return GL_UNSIGNED_INT_SAMPLER_CUBE;
148 case EbtSamplerExternalOES:
149 return GL_SAMPLER_EXTERNAL_OES;
151 return GL_SAMPLER_3D_OES;
153 return GL_INT_SAMPLER_3D;
155 return GL_UNSIGNED_INT_SAMPLER_3D;
156 case EbtSampler2DArray:
157 return GL_SAMPLER_2D_ARRAY;
158 case EbtISampler2DArray:
159 return GL_INT_SAMPLER_2D_ARRAY;
160 case EbtUSampler2DArray:
161 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
162 case EbtSampler2DShadow:
163 return GL_SAMPLER_2D_SHADOW;
164 case EbtSamplerCubeShadow:
165 return GL_SAMPLER_CUBE_SHADOW;
166 case EbtSampler2DArrayShadow:
167 return GL_SAMPLER_2D_ARRAY_SHADOW;
169 UNREACHABLE(type.getBasicType());
176 GLenum glVariablePrecision(const TType &type)
178 if(type.getBasicType() == EbtFloat)
180 switch(type.getPrecision())
182 case EbpHigh: return GL_HIGH_FLOAT;
183 case EbpMedium: return GL_MEDIUM_FLOAT;
184 case EbpLow: return GL_LOW_FLOAT;
186 // Should be defined as the default precision by the parser
187 default: UNREACHABLE(type.getPrecision());
190 else if(type.getBasicType() == EbtInt)
192 switch(type.getPrecision())
194 case EbpHigh: return GL_HIGH_INT;
195 case EbpMedium: return GL_MEDIUM_INT;
196 case EbpLow: return GL_LOW_INT;
198 // Should be defined as the default precision by the parser
199 default: UNREACHABLE(type.getPrecision());
203 // Other types (boolean, sampler) don't have a precision
210 // Integer to TString conversion
214 sprintf(buffer, "%d", i);
218 class Temporary : public TIntermSymbol
221 Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
227 assembler->freeTemporary(this);
231 OutputASM *const assembler;
234 class Constant : public TIntermConstantUnion
237 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
239 constants[0].setFConst(x);
240 constants[1].setFConst(y);
241 constants[2].setFConst(z);
242 constants[3].setFConst(w);
245 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
247 constants[0].setBConst(b);
250 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
252 constants[0].setIConst(i);
260 ConstantUnion constants[4];
263 ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
264 type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
265 name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
269 for(const auto& field : type.getStruct()->fields())
271 fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
276 Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
277 ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
281 UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
282 TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
283 name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
284 isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
288 BlockLayoutEncoder::BlockLayoutEncoder()
293 BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
298 bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
299 getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
301 const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
302 static_cast<int>(arrayStride * BytesPerComponent),
303 static_cast<int>(matrixStride * BytesPerComponent),
304 (matrixStride > 0) && isRowMajor);
306 advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
312 size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
314 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
318 size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
320 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
323 void BlockLayoutEncoder::nextRegister()
325 mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
328 Std140BlockEncoder::Std140BlockEncoder() : BlockLayoutEncoder()
332 void Std140BlockEncoder::enterAggregateType()
337 void Std140BlockEncoder::exitAggregateType()
342 void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
344 size_t baseAlignment = 0;
345 int matrixStride = 0;
350 baseAlignment = ComponentsPerRegister;
351 matrixStride = ComponentsPerRegister;
355 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
356 arrayStride = ComponentsPerRegister * numRegisters;
359 else if(arraySize > 0)
361 baseAlignment = ComponentsPerRegister;
362 arrayStride = ComponentsPerRegister;
366 const size_t numComponents = type.getElementSize();
367 baseAlignment = (numComponents == 3 ? 4u : numComponents);
370 mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
372 *matrixStrideOut = matrixStride;
373 *arrayStrideOut = arrayStride;
376 void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
380 mCurrentOffset += arrayStride * arraySize;
382 else if(type.isMatrix())
384 ASSERT(matrixStride == ComponentsPerRegister);
385 const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
386 mCurrentOffset += ComponentsPerRegister * numRegisters;
390 mCurrentOffset += type.getElementSize();
394 Attribute::Attribute()
401 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex)
405 this->arraySize = arraySize;
406 this->location = location;
407 this->registerIndex = registerIndex;
410 sw::PixelShader *Shader::getPixelShader() const
415 sw::VertexShader *Shader::getVertexShader() const
420 OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
422 TString name = TFunction::unmangleName(nodeName);
424 if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D" || name == "texture2DRect")
428 else if(name == "texture2DProj" || name == "textureProj" || name == "texture2DRectProj")
433 else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
437 else if(name == "texture2DProjLod" || name == "textureProjLod")
442 else if(name == "textureSize")
446 else if(name == "textureOffset")
451 else if(name == "textureProjOffset")
457 else if(name == "textureLodOffset")
462 else if(name == "textureProjLodOffset")
468 else if(name == "texelFetch")
472 else if(name == "texelFetchOffset")
477 else if(name == "textureGrad")
481 else if(name == "textureGradOffset")
486 else if(name == "textureProjGrad")
491 else if(name == "textureProjGradOffset")
500 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
503 pixelShader = nullptr;
504 vertexShader = nullptr;
508 shader = shaderObject->getShader();
509 pixelShader = shaderObject->getPixelShader();
510 vertexShader = shaderObject->getVertexShader();
513 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
515 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
518 OutputASM::~OutputASM()
522 void OutputASM::output()
528 if(functionArray.size() > 1) // Only call main() when there are other functions
530 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
531 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
532 callMain->dst.index = 0; // main()
534 emit(sw::Shader::OPCODE_RET);
537 emitShader(FUNCTION);
541 void OutputASM::emitShader(Scope scope)
544 currentScope = GLOBAL;
545 mContext.getTreeRoot()->traverse(this);
548 void OutputASM::freeTemporary(Temporary *temporary)
550 free(temporaries, temporary);
553 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
555 TBasicType baseType = in->getType().getBasicType();
559 case sw::Shader::OPCODE_NEG:
564 return sw::Shader::OPCODE_INEG;
569 case sw::Shader::OPCODE_ABS:
573 return sw::Shader::OPCODE_IABS;
578 case sw::Shader::OPCODE_SGN:
582 return sw::Shader::OPCODE_ISGN;
587 case sw::Shader::OPCODE_ADD:
592 return sw::Shader::OPCODE_IADD;
597 case sw::Shader::OPCODE_SUB:
602 return sw::Shader::OPCODE_ISUB;
607 case sw::Shader::OPCODE_MUL:
612 return sw::Shader::OPCODE_IMUL;
617 case sw::Shader::OPCODE_DIV:
621 return sw::Shader::OPCODE_IDIV;
623 return sw::Shader::OPCODE_UDIV;
628 case sw::Shader::OPCODE_IMOD:
629 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
630 case sw::Shader::OPCODE_ISHR:
631 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
632 case sw::Shader::OPCODE_MIN:
636 return sw::Shader::OPCODE_IMIN;
638 return sw::Shader::OPCODE_UMIN;
643 case sw::Shader::OPCODE_MAX:
647 return sw::Shader::OPCODE_IMAX;
649 return sw::Shader::OPCODE_UMAX;
659 void OutputASM::visitSymbol(TIntermSymbol *symbol)
661 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
662 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
663 switch(symbol->getQualifier())
667 case EvqInvariantVaryingIn:
668 case EvqInvariantVaryingOut:
671 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
673 declareVarying(symbol, -1);
677 declareFragmentOutput(symbol);
683 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
684 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
685 // "All members of a named uniform block declared with a shared or std140 layout qualifier
686 // are considered active, even if they are not referenced in any shader in the program.
687 // The uniform block itself is also considered active, even if no member of the block is referenced."
688 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
690 uniformRegister(symbol);
694 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
696 if(currentScope != emitScope)
701 TIntermTyped *result = node;
702 TIntermTyped *left = node->getLeft();
703 TIntermTyped *right = node->getRight();
704 const TType &leftType = left->getType();
705 const TType &rightType = right->getType();
707 if(isSamplerRegister(result))
709 return false; // Don't traverse, the register index is determined statically
712 switch(node->getOp())
715 assert(visit == PreVisit);
716 right->traverse(this);
717 assignLvalue(left, right);
721 assert(visit == PreVisit);
722 // Constant arrays go into the constant register file.
723 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
725 for(int i = 0; i < left->totalRegisterCount(); i++)
727 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
732 right->traverse(this);
736 case EOpMatrixTimesScalarAssign:
737 assert(visit == PreVisit);
738 right->traverse(this);
739 for(int i = 0; i < leftType.getNominalSize(); i++)
741 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
744 assignLvalue(left, result);
746 case EOpVectorTimesMatrixAssign:
747 assert(visit == PreVisit);
749 right->traverse(this);
750 int size = leftType.getNominalSize();
752 for(int i = 0; i < size; i++)
754 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
755 dot->dst.mask = 1 << i;
758 assignLvalue(left, result);
761 case EOpMatrixTimesMatrixAssign:
762 assert(visit == PreVisit);
764 right->traverse(this);
765 int dim = leftType.getNominalSize();
767 for(int i = 0; i < dim; i++)
769 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
770 mul->src[1].swizzle = 0x00;
772 for(int j = 1; j < dim; j++)
774 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
775 mad->src[1].swizzle = j * 0x55;
779 assignLvalue(left, result);
783 case EOpIndexIndirect:
784 case EOpIndexDirectStruct:
785 case EOpIndexDirectInterfaceBlock:
786 assert(visit == PreVisit);
787 evaluateRvalue(node);
789 case EOpVectorSwizzle:
790 if(visit == PostVisit)
793 TIntermAggregate *components = right->getAsAggregate();
797 TIntermSequence &sequence = components->getSequence();
800 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
802 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
806 int i = element->getUnionArrayPointer()[0].getIConst();
807 swizzle |= i << (component * 2);
815 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
816 mov->src[0].swizzle = swizzle;
819 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
820 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
821 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
822 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
823 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
824 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
825 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
826 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
827 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
828 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
829 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
830 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
831 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
832 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
833 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
834 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
835 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
836 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
837 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
838 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
840 if(visit == PostVisit)
842 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
844 for(int index = 1; index < left->totalRegisterCount(); index++)
846 Temporary equal(this);
847 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
848 emit(sw::Shader::OPCODE_AND, result, result, &equal);
853 if(visit == PostVisit)
855 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
857 for(int index = 1; index < left->totalRegisterCount(); index++)
859 Temporary notEqual(this);
860 emit(sw::Shader::OPCODE_NE, ¬Equal, 0, left, index, right, index);
861 emit(sw::Shader::OPCODE_OR, result, result, ¬Equal);
865 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
866 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
867 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
868 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
869 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
870 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
871 case EOpMatrixTimesScalar:
872 if(visit == PostVisit)
876 for(int i = 0; i < leftType.getNominalSize(); i++)
878 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
881 else if(right->isMatrix())
883 for(int i = 0; i < rightType.getNominalSize(); i++)
885 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
891 case EOpVectorTimesMatrix:
892 if(visit == PostVisit)
894 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
896 int size = rightType.getNominalSize();
897 for(int i = 0; i < size; i++)
899 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
900 dot->dst.mask = 1 << i;
904 case EOpMatrixTimesVector:
905 if(visit == PostVisit)
907 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
908 mul->src[1].swizzle = 0x00;
910 int size = rightType.getNominalSize();
911 for(int i = 1; i < size; i++)
913 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
914 mad->src[1].swizzle = i * 0x55;
918 case EOpMatrixTimesMatrix:
919 if(visit == PostVisit)
921 int dim = leftType.getNominalSize();
923 int size = rightType.getNominalSize();
924 for(int i = 0; i < size; i++)
926 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
927 mul->src[1].swizzle = 0x00;
929 for(int j = 1; j < dim; j++)
931 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
932 mad->src[1].swizzle = j * 0x55;
938 if(trivial(right, 6))
940 if(visit == PostVisit)
942 emit(sw::Shader::OPCODE_OR, result, left, right);
945 else // Short-circuit evaluation
949 emit(sw::Shader::OPCODE_MOV, result, left);
950 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
951 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
953 else if(visit == PostVisit)
955 emit(sw::Shader::OPCODE_MOV, result, right);
956 emit(sw::Shader::OPCODE_ENDIF);
960 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
962 if(trivial(right, 6))
964 if(visit == PostVisit)
966 emit(sw::Shader::OPCODE_AND, result, left, right);
969 else // Short-circuit evaluation
973 emit(sw::Shader::OPCODE_MOV, result, left);
974 emit(sw::Shader::OPCODE_IF, 0, result);
976 else if(visit == PostVisit)
978 emit(sw::Shader::OPCODE_MOV, result, right);
979 emit(sw::Shader::OPCODE_ENDIF);
983 default: UNREACHABLE(node->getOp());
989 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
993 case 1: // Used for cofactor computation only
995 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
996 bool isMov = (row == col);
997 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
998 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
999 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1000 mov->dst.mask = 1 << outRow;
1005 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1007 bool isCofactor = (col >= 0) && (row >= 0);
1008 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1009 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1010 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1012 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1013 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1014 det->dst.mask = 1 << outRow;
1019 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1021 bool isCofactor = (col >= 0) && (row >= 0);
1022 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1023 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1024 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1025 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1027 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1028 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1029 det->dst.mask = 1 << outRow;
1034 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1035 det->dst.mask = 1 << outRow;
1044 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1046 if(currentScope != emitScope)
1051 TIntermTyped *result = node;
1052 TIntermTyped *arg = node->getOperand();
1053 TBasicType basicType = arg->getType().getBasicType();
1061 if(basicType == EbtInt || basicType == EbtUInt)
1070 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1071 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1072 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1074 switch(node->getOp())
1077 if(visit == PostVisit)
1079 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1080 for(int index = 0; index < arg->totalRegisterCount(); index++)
1082 emit(negOpcode, result, index, arg, index);
1086 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1087 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1088 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1089 case EOpPostIncrement:
1090 if(visit == PostVisit)
1094 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1095 for(int index = 0; index < arg->totalRegisterCount(); index++)
1097 emit(addOpcode, arg, index, arg, index, &one);
1100 assignLvalue(arg, arg);
1103 case EOpPostDecrement:
1104 if(visit == PostVisit)
1108 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1109 for(int index = 0; index < arg->totalRegisterCount(); index++)
1111 emit(subOpcode, arg, index, arg, index, &one);
1114 assignLvalue(arg, arg);
1117 case EOpPreIncrement:
1118 if(visit == PostVisit)
1120 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1121 for(int index = 0; index < arg->totalRegisterCount(); index++)
1123 emit(addOpcode, result, index, arg, index, &one);
1126 assignLvalue(arg, result);
1129 case EOpPreDecrement:
1130 if(visit == PostVisit)
1132 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1133 for(int index = 0; index < arg->totalRegisterCount(); index++)
1135 emit(subOpcode, result, index, arg, index, &one);
1138 assignLvalue(arg, result);
1141 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1142 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, °); break;
1143 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1144 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1145 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1146 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1147 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1148 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1149 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1150 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1151 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1152 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1153 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1154 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1155 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1156 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1157 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1158 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1159 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1160 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1161 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1162 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1163 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1164 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1165 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1166 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1167 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1168 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1169 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1170 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1171 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1172 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1173 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1174 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1175 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1176 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1177 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1178 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1179 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1180 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1181 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1182 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1183 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1184 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1185 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1186 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1187 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1189 if(visit == PostVisit)
1191 int numCols = arg->getNominalSize();
1192 int numRows = arg->getSecondarySize();
1193 for(int i = 0; i < numCols; ++i)
1195 for(int j = 0; j < numRows; ++j)
1197 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1198 mov->src[0].swizzle = 0x55 * j;
1199 mov->dst.mask = 1 << i;
1204 case EOpDeterminant:
1205 if(visit == PostVisit)
1207 int size = arg->getNominalSize();
1208 ASSERT(size == arg->getSecondarySize());
1210 emitDeterminant(result, arg, size);
1214 if(visit == PostVisit)
1216 int size = arg->getNominalSize();
1217 ASSERT(size == arg->getSecondarySize());
1219 // Compute transposed matrix of cofactors
1220 for(int i = 0; i < size; ++i)
1222 for(int j = 0; j < size; ++j)
1224 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1225 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1226 emitDeterminant(result, arg, size - 1, j, i, i, j);
1230 // Compute 1 / determinant
1231 Temporary invDet(this);
1232 emitDeterminant(&invDet, arg, size);
1233 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1234 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1235 div->src[1].swizzle = 0x00; // xxxx
1237 // Divide transposed matrix of cofactors by determinant
1238 for(int i = 0; i < size; ++i)
1240 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1244 default: UNREACHABLE(node->getOp());
1250 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1252 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1257 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1259 TIntermTyped *result = node;
1260 const TType &resultType = node->getType();
1261 TIntermSequence &arg = node->getSequence();
1262 size_t argumentCount = arg.size();
1264 switch(node->getOp())
1266 case EOpSequence: break;
1267 case EOpDeclaration: break;
1268 case EOpInvariantDeclaration: break;
1269 case EOpPrototype: break;
1271 if(visit == PostVisit)
1273 copy(result, arg[1]);
1277 if(visit == PreVisit)
1279 const TString &name = node->getName();
1281 if(emitScope == FUNCTION)
1283 if(functionArray.size() > 1) // No need for a label when there's only main()
1285 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1286 label->dst.type = sw::Shader::PARAMETER_LABEL;
1288 const Function *function = findFunction(name);
1289 ASSERT(function); // Should have been added during global pass
1290 label->dst.index = function->label;
1291 currentFunction = function->label;
1294 else if(emitScope == GLOBAL)
1298 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1299 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1302 else UNREACHABLE(emitScope);
1304 currentScope = FUNCTION;
1306 else if(visit == PostVisit)
1308 if(emitScope == FUNCTION)
1310 if(functionArray.size() > 1) // No need to return when there's only main()
1312 emit(sw::Shader::OPCODE_RET);
1316 currentScope = GLOBAL;
1319 case EOpFunctionCall:
1320 if(visit == PostVisit)
1322 if(node->isUserDefined())
1324 const TString &name = node->getName();
1325 const Function *function = findFunction(name);
1329 mContext.error(node->getLine(), "function definition not found", name.c_str());
1333 TIntermSequence &arguments = *function->arg;
1335 for(size_t i = 0; i < argumentCount; i++)
1337 TIntermTyped *in = arguments[i]->getAsTyped();
1339 if(in->getQualifier() == EvqIn ||
1340 in->getQualifier() == EvqInOut ||
1341 in->getQualifier() == EvqConstReadOnly)
1347 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1348 call->dst.type = sw::Shader::PARAMETER_LABEL;
1349 call->dst.index = function->label;
1351 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1353 copy(result, function->ret);
1356 for(size_t i = 0; i < argumentCount; i++)
1358 TIntermTyped *argument = arguments[i]->getAsTyped();
1359 TIntermTyped *out = arg[i]->getAsTyped();
1361 if(argument->getQualifier() == EvqOut ||
1362 argument->getQualifier() == EvqInOut)
1364 assignLvalue(out, argument);
1370 const TextureFunction textureFunction(node->getName());
1371 TIntermTyped *s = arg[0]->getAsTyped();
1372 TIntermTyped *t = arg[1]->getAsTyped();
1374 Temporary coord(this);
1376 if(textureFunction.proj)
1378 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1379 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1380 rcp->dst.mask = 0x7;
1382 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1383 mul->dst.mask = 0x7;
1385 if(IsShadowSampler(s->getBasicType()))
1387 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1388 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1389 mov->src[0].swizzle = 0xA4;
1394 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1396 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1398 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1399 mov->src[0].swizzle = 0xA4;
1403 switch(textureFunction.method)
1405 case TextureFunction::IMPLICIT:
1406 if(!textureFunction.offset)
1408 if(argumentCount == 2)
1410 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
1412 else if(argumentCount == 3) // Bias
1414 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1416 else UNREACHABLE(argumentCount);
1420 if(argumentCount == 3)
1422 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1424 else if(argumentCount == 4) // Bias
1426 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
1428 else UNREACHABLE(argumentCount);
1431 case TextureFunction::LOD:
1432 if(!textureFunction.offset && argumentCount == 3)
1434 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
1436 else if(argumentCount == 4) // Offset
1438 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1440 else UNREACHABLE(argumentCount);
1442 case TextureFunction::FETCH:
1443 if(!textureFunction.offset && argumentCount == 3)
1445 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
1447 else if(argumentCount == 4) // Offset
1449 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1451 else UNREACHABLE(argumentCount);
1453 case TextureFunction::GRAD:
1454 if(!textureFunction.offset && argumentCount == 4)
1456 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
1458 else if(argumentCount == 5) // Offset
1460 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1462 else UNREACHABLE(argumentCount);
1464 case TextureFunction::SIZE:
1465 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
1468 UNREACHABLE(textureFunction.method);
1475 case EOpConstructFloat:
1476 case EOpConstructVec2:
1477 case EOpConstructVec3:
1478 case EOpConstructVec4:
1479 case EOpConstructBool:
1480 case EOpConstructBVec2:
1481 case EOpConstructBVec3:
1482 case EOpConstructBVec4:
1483 case EOpConstructInt:
1484 case EOpConstructIVec2:
1485 case EOpConstructIVec3:
1486 case EOpConstructIVec4:
1487 case EOpConstructUInt:
1488 case EOpConstructUVec2:
1489 case EOpConstructUVec3:
1490 case EOpConstructUVec4:
1491 if(visit == PostVisit)
1494 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1495 int arrayComponents = result->getType().getElementSize();
1496 for(size_t i = 0; i < argumentCount; i++)
1498 TIntermTyped *argi = arg[i]->getAsTyped();
1499 int size = argi->getNominalSize();
1500 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1501 int swizzle = component - (arrayIndex * arrayComponents);
1503 if(!argi->isMatrix())
1505 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1506 mov->dst.mask = (0xF << swizzle) & 0xF;
1507 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1511 else if(!result->isMatrix()) // Construct a non matrix from a matrix
1513 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1514 mov->dst.mask = (0xF << swizzle) & 0xF;
1515 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1517 // At most one more instruction when constructing a vec3 from a mat2 or a vec4 from a mat2/mat3
1518 if(result->getNominalSize() > size)
1520 Instruction *mov = emitCast(result, arrayIndex, argi, 1);
1521 mov->dst.mask = (0xF << (swizzle + size)) & 0xF;
1522 // mat2: xxxy (0x40), mat3: xxxx (0x00)
1523 mov->src[0].swizzle = ((size == 2) ? 0x40 : 0x00) << (swizzle * 2);
1532 while(component < resultType.getNominalSize())
1534 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1535 mov->dst.mask = (0xF << swizzle) & 0xF;
1536 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1545 case EOpConstructMat2:
1546 case EOpConstructMat2x3:
1547 case EOpConstructMat2x4:
1548 case EOpConstructMat3x2:
1549 case EOpConstructMat3:
1550 case EOpConstructMat3x4:
1551 case EOpConstructMat4x2:
1552 case EOpConstructMat4x3:
1553 case EOpConstructMat4:
1554 if(visit == PostVisit)
1556 TIntermTyped *arg0 = arg[0]->getAsTyped();
1557 const int outCols = result->getNominalSize();
1558 const int outRows = result->getSecondarySize();
1560 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1562 for(int i = 0; i < outCols; i++)
1564 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
1565 Instruction *mov = emitCast(result, i, arg0, 0);
1566 mov->dst.mask = 1 << i;
1567 ASSERT(mov->src[0].swizzle == 0x00);
1570 else if(arg0->isMatrix())
1572 int arraySize = result->isArray() ? result->getArraySize() : 1;
1574 for(int n = 0; n < arraySize; n++)
1576 TIntermTyped *argi = arg[n]->getAsTyped();
1577 const int inCols = argi->getNominalSize();
1578 const int inRows = argi->getSecondarySize();
1580 for(int i = 0; i < outCols; i++)
1582 if(i >= inCols || outRows > inRows)
1584 // Initialize to identity matrix
1585 Constant col((i == 0 ? 1.0f : 0.0f), (i == 1 ? 1.0f : 0.0f), (i == 2 ? 1.0f : 0.0f), (i == 3 ? 1.0f : 0.0f));
1586 emitCast(result, i + n * outCols, &col, 0);
1591 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1592 mov->dst.mask = 0xF >> (4 - inRows);
1602 for(size_t i = 0; i < argumentCount; i++)
1604 TIntermTyped *argi = arg[i]->getAsTyped();
1605 int size = argi->getNominalSize();
1608 while(element < size)
1610 Instruction *mov = emitCast(result, column, argi, 0);
1611 mov->dst.mask = (0xF << row) & 0xF;
1612 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1614 int end = row + size - element;
1615 column = end >= outRows ? column + 1 : column;
1616 element = element + outRows - row;
1617 row = end >= outRows ? 0 : end;
1623 case EOpConstructStruct:
1624 if(visit == PostVisit)
1627 for(size_t i = 0; i < argumentCount; i++)
1629 TIntermTyped *argi = arg[i]->getAsTyped();
1630 int size = argi->totalRegisterCount();
1632 for(int index = 0; index < size; index++)
1634 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1635 mov->dst.mask = writeMask(result, offset + index);
1642 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1643 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1644 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1645 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1646 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1647 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1648 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1650 if(visit == PostVisit)
1652 TIntermTyped* arg1 = arg[1]->getAsTyped();
1653 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1654 assignLvalue(arg1, arg1);
1655 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1658 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1659 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1660 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1661 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1663 if(visit == PostVisit)
1665 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1666 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1670 if(visit == PostVisit)
1672 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1674 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1678 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1682 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1683 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1684 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1685 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1686 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1687 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1688 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1689 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1691 if(visit == PostVisit)
1693 TIntermTyped *arg0 = arg[0]->getAsTyped();
1694 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1695 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
1697 int size = arg0->getNominalSize();
1698 for(int i = 0; i < size; i++)
1700 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1704 case EOpOuterProduct:
1705 if(visit == PostVisit)
1707 for(int i = 0; i < dim(arg[1]); i++)
1709 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1710 mul->src[1].swizzle = 0x55 * i;
1714 default: UNREACHABLE(node->getOp());
1720 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1722 if(currentScope != emitScope)
1727 TIntermTyped *condition = node->getCondition();
1728 TIntermNode *trueBlock = node->getTrueBlock();
1729 TIntermNode *falseBlock = node->getFalseBlock();
1730 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1732 condition->traverse(this);
1734 if(node->usesTernaryOperator())
1736 if(constantCondition)
1738 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1742 trueBlock->traverse(this);
1743 copy(node, trueBlock);
1747 falseBlock->traverse(this);
1748 copy(node, falseBlock);
1751 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1753 trueBlock->traverse(this);
1754 falseBlock->traverse(this);
1755 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1759 emit(sw::Shader::OPCODE_IF, 0, condition);
1763 trueBlock->traverse(this);
1764 copy(node, trueBlock);
1769 emit(sw::Shader::OPCODE_ELSE);
1770 falseBlock->traverse(this);
1771 copy(node, falseBlock);
1774 emit(sw::Shader::OPCODE_ENDIF);
1777 else // if/else statement
1779 if(constantCondition)
1781 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1787 trueBlock->traverse(this);
1794 falseBlock->traverse(this);
1800 emit(sw::Shader::OPCODE_IF, 0, condition);
1804 trueBlock->traverse(this);
1809 emit(sw::Shader::OPCODE_ELSE);
1810 falseBlock->traverse(this);
1813 emit(sw::Shader::OPCODE_ENDIF);
1820 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1822 if(currentScope != emitScope)
1827 unsigned int iterations = loopCount(node);
1834 bool unroll = (iterations <= 4);
1838 LoopUnrollable loopUnrollable;
1839 unroll = loopUnrollable.traverse(node);
1842 TIntermNode *init = node->getInit();
1843 TIntermTyped *condition = node->getCondition();
1844 TIntermTyped *expression = node->getExpression();
1845 TIntermNode *body = node->getBody();
1846 Constant True(true);
1848 if(node->getType() == ELoopDoWhile)
1850 Temporary iterate(this);
1851 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1853 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1857 body->traverse(this);
1860 emit(sw::Shader::OPCODE_TEST);
1862 condition->traverse(this);
1863 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1865 emit(sw::Shader::OPCODE_ENDWHILE);
1871 init->traverse(this);
1876 for(unsigned int i = 0; i < iterations; i++)
1878 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1882 body->traverse(this);
1887 expression->traverse(this);
1895 condition->traverse(this);
1902 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1906 body->traverse(this);
1909 emit(sw::Shader::OPCODE_TEST);
1913 expression->traverse(this);
1918 condition->traverse(this);
1921 emit(sw::Shader::OPCODE_ENDWHILE);
1928 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1930 if(currentScope != emitScope)
1935 switch(node->getFlowOp())
1937 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1938 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1939 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1941 if(visit == PostVisit)
1943 TIntermTyped *value = node->getExpression();
1947 copy(functionArray[currentFunction].ret, value);
1950 emit(sw::Shader::OPCODE_LEAVE);
1953 default: UNREACHABLE(node->getFlowOp());
1959 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1961 if(currentScope != emitScope)
1966 TIntermTyped* switchValue = node->getInit();
1967 TIntermAggregate* opList = node->getStatementList();
1969 if(!switchValue || !opList)
1974 switchValue->traverse(this);
1976 emit(sw::Shader::OPCODE_SWITCH);
1978 TIntermSequence& sequence = opList->getSequence();
1979 TIntermSequence::iterator it = sequence.begin();
1980 TIntermSequence::iterator defaultIt = sequence.end();
1982 for(; it != sequence.end(); ++it)
1984 TIntermCase* currentCase = (*it)->getAsCaseNode();
1987 TIntermSequence::iterator caseIt = it;
1989 TIntermTyped* condition = currentCase->getCondition();
1990 if(condition) // non default case
1994 emit(sw::Shader::OPCODE_ELSE);
1997 condition->traverse(this);
1998 Temporary result(this);
1999 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
2000 emit(sw::Shader::OPCODE_IF, 0, &result);
2003 // Emit the code for this case and all subsequent cases until we hit a break statement.
2004 // TODO: This can repeat a lot of code for switches with many fall-through cases.
2005 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2007 (*caseIt)->traverse(this);
2009 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2010 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2011 // Note that this eliminates useless operations but shouldn't affect correctness.
2012 if((*caseIt)->getAsBranchNode())
2020 defaultIt = it; // The default case might not be the last case, keep it for last
2025 // If there's a default case, traverse it here
2026 if(defaultIt != sequence.end())
2028 emit(sw::Shader::OPCODE_ELSE);
2029 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2031 (*defaultIt)->traverse(this);
2032 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2039 for(int i = 0; i < nbCases; ++i)
2041 emit(sw::Shader::OPCODE_ENDIF);
2044 emit(sw::Shader::OPCODE_ENDSWITCH);
2049 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2051 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2054 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2055 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2057 Instruction *instruction = new Instruction(op);
2061 destination(instruction->dst, dst, dstIndex);
2066 TIntermTyped* src = src0->getAsTyped();
2067 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2070 source(instruction->src[0], src0, index0);
2071 source(instruction->src[1], src1, index1);
2072 source(instruction->src[2], src2, index2);
2073 source(instruction->src[3], src3, index3);
2074 source(instruction->src[4], src4, index4);
2076 shader->append(instruction);
2081 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2083 return emitCast(dst, 0, src, 0);
2086 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2088 switch(src->getBasicType())
2091 switch(dst->getBasicType())
2093 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2094 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2095 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2100 switch(dst->getBasicType())
2102 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2103 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2108 switch(dst->getBasicType())
2110 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2111 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2116 switch(dst->getBasicType())
2118 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2119 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2120 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2128 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2129 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2130 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2132 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2135 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2137 for(int index = 0; index < dst->elementRegisterCount(); index++)
2139 emit(op, dst, index, src0, index, src1, index, src2, index);
2143 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2145 emitBinary(op, result, src0, src1);
2146 assignLvalue(lhs, result);
2149 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2151 sw::Shader::Opcode opcode;
2152 switch(left->getAsTyped()->getBasicType())
2156 opcode = sw::Shader::OPCODE_ICMP;
2159 opcode = sw::Shader::OPCODE_UCMP;
2162 opcode = sw::Shader::OPCODE_CMP;
2166 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2167 cmp->control = cmpOp;
2170 int componentCount(const TType &type, int registers)
2177 if(type.isArray() && registers >= type.elementRegisterCount())
2179 int index = registers / type.elementRegisterCount();
2180 registers -= index * type.elementRegisterCount();
2181 return index * type.getElementSize() + componentCount(type, registers);
2184 if(type.isStruct() || type.isInterfaceBlock())
2186 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2189 for(const auto &field : fields)
2191 const TType &fieldType = *(field->type());
2193 if(fieldType.totalRegisterCount() <= registers)
2195 registers -= fieldType.totalRegisterCount();
2196 elements += fieldType.getObjectSize();
2198 else // Register within this field
2200 return elements + componentCount(fieldType, registers);
2204 else if(type.isMatrix())
2206 return registers * type.registerSize();
2213 int registerSize(const TType &type, int registers)
2219 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2221 else if(type.isInterfaceBlock())
2223 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2226 return type.registerSize();
2229 if(type.isArray() && registers >= type.elementRegisterCount())
2231 int index = registers / type.elementRegisterCount();
2232 registers -= index * type.elementRegisterCount();
2233 return registerSize(type, registers);
2236 if(type.isStruct() || type.isInterfaceBlock())
2238 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2241 for(const auto &field : fields)
2243 const TType &fieldType = *(field->type());
2245 if(fieldType.totalRegisterCount() <= registers)
2247 registers -= fieldType.totalRegisterCount();
2248 elements += fieldType.getObjectSize();
2250 else // Register within this field
2252 return registerSize(fieldType, registers);
2256 else if(type.isMatrix())
2258 return registerSize(type, 0);
2265 int OutputASM::getBlockId(TIntermTyped *arg)
2269 const TType &type = arg->getType();
2270 TInterfaceBlock* block = type.getInterfaceBlock();
2271 if(block && (type.getQualifier() == EvqUniform))
2273 // Make sure the uniform block is declared
2274 uniformRegister(arg);
2276 const char* blockName = block->name().c_str();
2278 // Fetch uniform block index from array of blocks
2279 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2281 if(blockName == it->name)
2294 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2296 const TType &type = arg->getType();
2297 int blockId = getBlockId(arg);
2298 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2301 argumentInfo.bufferIndex = 0;
2302 for(int i = 0; i < blockId; ++i)
2304 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2305 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2308 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2310 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2311 BlockDefinitionIndexMap::const_iterator it = itEnd;
2313 argumentInfo.clampedIndex = index;
2314 if(type.isInterfaceBlock())
2316 // Offset index to the beginning of the selected instance
2317 int blockRegisters = type.elementRegisterCount();
2318 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2319 argumentInfo.bufferIndex += bufferOffset;
2320 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2323 int regIndex = registerIndex(arg);
2324 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2326 it = blockDefinition.find(i);
2329 argumentInfo.clampedIndex -= (i - regIndex);
2333 ASSERT(it != itEnd);
2335 argumentInfo.typedMemberInfo = it->second;
2337 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2338 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2342 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2345 return argumentInfo;
2348 void OutputASM::source(sw::Shader::SourceParameter ¶meter, TIntermNode *argument, int index)
2352 TIntermTyped *arg = argument->getAsTyped();
2353 Temporary unpackedUniform(this);
2355 const TType& srcType = arg->getType();
2356 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2357 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2359 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2360 const TType &memberType = argumentInfo.typedMemberInfo.type;
2362 if(memberType.getBasicType() == EbtBool)
2364 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
2366 // Convert the packed bool, which is currently an int, to a true bool
2367 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2368 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2369 instruction->dst.index = registerIndex(&unpackedUniform);
2370 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2371 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2372 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2374 shader->append(instruction);
2376 arg = &unpackedUniform;
2379 else if((memberType.getLayoutQualifier().matrixPacking == EmpRowMajor) && memberType.isMatrix())
2381 int numCols = memberType.getNominalSize();
2382 int numRows = memberType.getSecondarySize();
2384 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
2386 unsigned int dstIndex = registerIndex(&unpackedUniform);
2387 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2388 int arrayIndex = argumentInfo.clampedIndex / numCols;
2389 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2391 for(int j = 0; j < numRows; ++j)
2393 // Transpose the row major matrix
2394 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2395 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2396 instruction->dst.index = dstIndex;
2397 instruction->dst.mask = 1 << j;
2398 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2399 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2400 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2401 instruction->src[0].swizzle = srcSwizzle;
2403 shader->append(instruction);
2406 arg = &unpackedUniform;
2411 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2412 const TType &type = argumentInfo.typedMemberInfo.type;
2414 int size = registerSize(type, argumentInfo.clampedIndex);
2416 parameter.type = registerType(arg);
2417 parameter.bufferIndex = argumentInfo.bufferIndex;
2419 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2421 int component = componentCount(type, argumentInfo.clampedIndex);
2422 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2424 for(int i = 0; i < 4; i++)
2426 if(size == 1) // Replicate
2428 parameter.value[i] = constants[component + 0].getAsFloat();
2432 parameter.value[i] = constants[component + i].getAsFloat();
2436 parameter.value[i] = 0.0f;
2442 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2444 if(parameter.bufferIndex != -1)
2446 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2447 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2451 if(!IsSampler(arg->getBasicType()))
2453 parameter.swizzle = readSwizzle(arg, size);
2458 void OutputASM::destination(sw::Shader::DestinationParameter ¶meter, TIntermTyped *arg, int index)
2460 parameter.type = registerType(arg);
2461 parameter.index = registerIndex(arg) + index;
2462 parameter.mask = writeMask(arg, index);
2465 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2467 for(int index = 0; index < dst->totalRegisterCount(); index++)
2469 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
2473 int swizzleElement(int swizzle, int index)
2475 return (swizzle >> (index * 2)) & 0x03;
2478 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2480 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2481 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2482 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2483 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2486 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2488 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2489 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
2491 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2494 TIntermBinary *binary = dst->getAsBinaryNode();
2496 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2498 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2500 lvalue(insert->dst, dst);
2502 insert->src[0].type = insert->dst.type;
2503 insert->src[0].index = insert->dst.index;
2504 insert->src[0].rel = insert->dst.rel;
2505 source(insert->src[1], src);
2506 source(insert->src[2], binary->getRight());
2508 shader->append(insert);
2512 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2514 int swizzle = lvalue(mov1->dst, dst);
2516 source(mov1->src[0], src);
2517 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2519 shader->append(mov1);
2521 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
2523 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2525 mov->dst = mov1->dst;
2526 mov->dst.index += offset;
2527 mov->dst.mask = writeMask(dst, offset);
2529 source(mov->src[0], src, offset);
2531 shader->append(mov);
2536 void OutputASM::evaluateRvalue(TIntermTyped *node)
2538 TIntermBinary *binary = node->getAsBinaryNode();
2540 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2542 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2544 destination(insert->dst, node);
2546 Temporary address(this);
2548 TIntermTyped *root = nullptr;
2549 unsigned int offset = 0;
2550 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2552 source(insert->src[0], root, offset);
2553 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2555 source(insert->src[1], binary->getRight());
2557 shader->append(insert);
2561 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2563 destination(mov1->dst, node, 0);
2565 Temporary address(this);
2567 TIntermTyped *root = nullptr;
2568 unsigned int offset = 0;
2569 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2571 source(mov1->src[0], root, offset);
2572 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2574 shader->append(mov1);
2576 for(int i = 1; i < node->totalRegisterCount(); i++)
2578 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2579 mov->src[0].rel = mov1->src[0].rel;
2584 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
2586 Temporary address(this);
2587 TIntermTyped *root = nullptr;
2588 unsigned int offset = 0;
2589 unsigned char mask = 0xF;
2590 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2592 dst.type = registerType(root);
2593 dst.index = registerIndex(root) + offset;
2599 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2601 TIntermTyped *result = node;
2602 TIntermBinary *binary = node->getAsBinaryNode();
2603 TIntermSymbol *symbol = node->getAsSymbolNode();
2607 TIntermTyped *left = binary->getLeft();
2608 TIntermTyped *right = binary->getRight();
2610 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
2612 switch(binary->getOp())
2614 case EOpIndexDirect:
2616 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2618 if(left->isRegister())
2620 int leftMask = mask;
2623 while((leftMask & mask) == 0)
2628 int element = swizzleElement(leftSwizzle, rightIndex);
2629 mask = 1 << element;
2633 else if(left->isArray() || left->isMatrix())
2635 offset += rightIndex * result->totalRegisterCount();
2638 else UNREACHABLE(0);
2641 case EOpIndexIndirect:
2643 right->traverse(this);
2645 if(left->isRegister())
2647 // Requires INSERT instruction (handled by calling function)
2649 else if(left->isArray() || left->isMatrix())
2651 int scale = result->totalRegisterCount();
2653 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
2655 if(left->totalRegisterCount() > 1)
2657 sw::Shader::SourceParameter relativeRegister;
2658 source(relativeRegister, right);
2660 rel.index = relativeRegister.index;
2661 rel.type = relativeRegister.type;
2663 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
2666 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
2670 Constant oldScale((int)rel.scale);
2671 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
2672 mad->src[0].index = rel.index;
2673 mad->src[0].type = rel.type;
2677 Constant oldScale((int)rel.scale);
2678 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
2679 mul->src[0].index = rel.index;
2680 mul->src[0].type = rel.type;
2682 Constant newScale(scale);
2683 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2686 rel.type = sw::Shader::PARAMETER_TEMP;
2687 rel.index = registerIndex(&address);
2690 else // Just add the new index to the address register
2694 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2698 Constant newScale(scale);
2699 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2703 else UNREACHABLE(0);
2706 case EOpIndexDirectStruct:
2707 case EOpIndexDirectInterfaceBlock:
2709 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2710 left->getType().getStruct()->fields() :
2711 left->getType().getInterfaceBlock()->fields();
2712 int index = right->getAsConstantUnion()->getIConst(0);
2713 int fieldOffset = 0;
2715 for(int i = 0; i < index; i++)
2717 fieldOffset += fields[i]->type()->totalRegisterCount();
2720 offset += fieldOffset;
2721 mask = writeMask(result);
2726 case EOpVectorSwizzle:
2728 ASSERT(left->isRegister());
2730 int leftMask = mask;
2735 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2737 for(unsigned int i = 0; i < sequence.size(); i++)
2739 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2741 int element = swizzleElement(leftSwizzle, index);
2742 rightMask = rightMask | (1 << element);
2743 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2746 mask = leftMask & rightMask;
2752 UNREACHABLE(binary->getOp()); // Not an l-value operator
2760 mask = writeMask(symbol);
2766 node->traverse(this);
2770 mask = writeMask(node);
2778 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2780 if(isSamplerRegister(operand))
2782 return sw::Shader::PARAMETER_SAMPLER;
2785 const TQualifier qualifier = operand->getQualifier();
2786 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
2788 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2789 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
2791 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2793 outputQualifier = qualifier;
2796 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2798 // Constant arrays are in the constant register file.
2799 if(operand->isArray() && operand->getArraySize() > 1)
2801 return sw::Shader::PARAMETER_CONST;
2805 return sw::Shader::PARAMETER_TEMP;
2811 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2812 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2813 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2814 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2815 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2816 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2817 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2818 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2819 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2820 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2821 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2822 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2823 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2824 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2825 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2826 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2827 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2828 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2829 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2830 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2831 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2832 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2833 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2834 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2835 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2836 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
2837 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
2838 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2839 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2840 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2841 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2842 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2843 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2844 default: UNREACHABLE(qualifier);
2847 return sw::Shader::PARAMETER_VOID;
2850 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2852 const TQualifier qualifier = operand->getQualifier();
2853 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2856 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2858 if(isSamplerRegister(operand))
2860 return samplerRegister(operand);
2862 else if(operand->getType().totalSamplerRegisterCount() > 0) // Struct containing a sampler
2864 samplerRegister(operand); // Make sure the sampler is declared
2867 switch(operand->getQualifier())
2869 case EvqTemporary: return temporaryRegister(operand);
2870 case EvqGlobal: return temporaryRegister(operand);
2871 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2872 case EvqAttribute: return attributeRegister(operand);
2873 case EvqVaryingIn: return varyingRegister(operand);
2874 case EvqVaryingOut: return varyingRegister(operand);
2875 case EvqVertexIn: return attributeRegister(operand);
2876 case EvqFragmentOut: return fragmentOutputRegister(operand);
2877 case EvqVertexOut: return varyingRegister(operand);
2878 case EvqFragmentIn: return varyingRegister(operand);
2879 case EvqInvariantVaryingIn: return varyingRegister(operand);
2880 case EvqInvariantVaryingOut: return varyingRegister(operand);
2881 case EvqSmooth: return varyingRegister(operand);
2882 case EvqFlat: return varyingRegister(operand);
2883 case EvqCentroidOut: return varyingRegister(operand);
2884 case EvqSmoothIn: return varyingRegister(operand);
2885 case EvqFlatIn: return varyingRegister(operand);
2886 case EvqCentroidIn: return varyingRegister(operand);
2887 case EvqUniform: return uniformRegister(operand);
2888 case EvqIn: return temporaryRegister(operand);
2889 case EvqOut: return temporaryRegister(operand);
2890 case EvqInOut: return temporaryRegister(operand);
2891 case EvqConstReadOnly: return temporaryRegister(operand);
2892 case EvqPosition: return varyingRegister(operand);
2893 case EvqPointSize: return varyingRegister(operand);
2894 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2895 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2896 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2897 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
2898 case EvqPointCoord: return varyingRegister(operand);
2899 case EvqFragColor: return 0;
2900 case EvqFragData: return fragmentOutputRegister(operand);
2901 case EvqFragDepth: return 0;
2902 default: UNREACHABLE(operand->getQualifier());
2908 int OutputASM::writeMask(TIntermTyped *destination, int index)
2910 if(destination->getQualifier() == EvqPointSize)
2912 return 0x2; // Point size stored in the y component
2915 return 0xF >> (4 - registerSize(destination->getType(), index));
2918 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2920 if(argument->getQualifier() == EvqPointSize)
2922 return 0x55; // Point size stored in the y component
2925 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2927 return swizzleSize[size];
2930 // Conservatively checks whether an expression is fast to compute and has no side effects
2931 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2933 if(!expression->isRegister())
2938 return cost(expression, budget) >= 0;
2941 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2942 int OutputASM::cost(TIntermNode *expression, int budget)
2949 if(expression->getAsSymbolNode())
2953 else if(expression->getAsConstantUnion())
2957 else if(expression->getAsBinaryNode())
2959 TIntermBinary *binary = expression->getAsBinaryNode();
2961 switch(binary->getOp())
2963 case EOpVectorSwizzle:
2964 case EOpIndexDirect:
2965 case EOpIndexDirectStruct:
2966 case EOpIndexDirectInterfaceBlock:
2967 return cost(binary->getLeft(), budget - 0);
2971 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2976 else if(expression->getAsUnaryNode())
2978 TIntermUnary *unary = expression->getAsUnaryNode();
2980 switch(unary->getOp())
2984 return cost(unary->getOperand(), budget - 1);
2989 else if(expression->getAsSelectionNode())
2991 TIntermSelection *selection = expression->getAsSelectionNode();
2993 if(selection->usesTernaryOperator())
2995 TIntermTyped *condition = selection->getCondition();
2996 TIntermNode *trueBlock = selection->getTrueBlock();
2997 TIntermNode *falseBlock = selection->getFalseBlock();
2998 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
3000 if(constantCondition)
3002 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
3006 return cost(trueBlock, budget - 0);
3010 return cost(falseBlock, budget - 0);
3015 return cost(trueBlock, cost(falseBlock, budget - 2));
3023 const Function *OutputASM::findFunction(const TString &name)
3025 for(unsigned int f = 0; f < functionArray.size(); f++)
3027 if(functionArray[f].name == name)
3029 return &functionArray[f];
3036 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3038 int index = allocate(temporaries, temporary);
3039 if(index >= sw::NUM_TEMPORARY_REGISTERS)
3041 mContext.error(temporary->getLine(),
3042 "Too many temporary registers required to compile shader",
3043 pixelShader ? "pixel shader" : "vertex shader");
3048 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3052 const TFieldList &fields = type.getStruct()->fields();
3054 for(const auto &field : fields)
3056 const TType& fieldType = *(field->type());
3057 setPixelShaderInputs(fieldType, fieldVar, flat);
3058 fieldVar += fieldType.totalRegisterCount();
3063 for(int i = 0; i < type.totalRegisterCount(); i++)
3065 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3070 int OutputASM::varyingRegister(TIntermTyped *varying)
3072 int var = lookup(varyings, varying);
3076 var = allocate(varyings, varying);
3077 int registerCount = varying->totalRegisterCount();
3081 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
3083 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3087 if(varying->getQualifier() == EvqPointCoord)
3089 ASSERT(varying->isRegister());
3090 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
3094 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
3097 else if(vertexShader)
3099 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
3101 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3105 if(varying->getQualifier() == EvqPosition)
3107 ASSERT(varying->isRegister());
3108 vertexShader->setPositionRegister(var);
3110 else if(varying->getQualifier() == EvqPointSize)
3112 ASSERT(varying->isRegister());
3113 vertexShader->setPointSizeRegister(var);
3117 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3120 else UNREACHABLE(0);
3122 declareVarying(varying, var);
3128 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3130 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3132 TIntermSymbol *symbol = varying->getAsSymbolNode();
3133 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3137 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3139 const char *name = varyingName.c_str();
3140 VaryingList &activeVaryings = shaderObject->varyings;
3142 TStructure* structure = type.getStruct();
3145 int fieldRegisterIndex = registerIndex;
3147 const TFieldList &fields = type.getStruct()->fields();
3148 for(const auto &field : fields)
3150 const TType& fieldType = *(field->type());
3151 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
3152 if(fieldRegisterIndex >= 0)
3154 fieldRegisterIndex += fieldType.totalRegisterCount();
3160 // Check if this varying has been declared before without having a register assigned
3161 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3165 if(registerIndex >= 0)
3167 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3168 v->registerIndex = registerIndex;
3175 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
3179 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3181 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
3182 int registerCount = fragmentOutput->totalRegisterCount();
3183 if(requestedLocation < 0)
3185 ASSERT(requestedLocation == -1); // All other negative values would have been prevented in TParseContext::parseLayoutQualifier
3186 return; // No requested location
3188 else if((requestedLocation + registerCount) > sw::RENDERTARGETS)
3190 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3194 int currentIndex = lookup(fragmentOutputs, fragmentOutput);
3195 if(requestedLocation != currentIndex)
3197 if(currentIndex != -1)
3199 mContext.error(fragmentOutput->getLine(), "Multiple locations for fragment output", "fragment shader");
3203 if(fragmentOutputs.size() <= (size_t)requestedLocation)
3205 while(fragmentOutputs.size() < (size_t)requestedLocation)
3207 fragmentOutputs.push_back(nullptr);
3209 for(int i = 0; i < registerCount; i++)
3211 fragmentOutputs.push_back(fragmentOutput);
3216 for(int i = 0; i < registerCount; i++)
3218 if(!fragmentOutputs[requestedLocation + i])
3220 fragmentOutputs[requestedLocation + i] = fragmentOutput;
3224 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3234 int OutputASM::uniformRegister(TIntermTyped *uniform)
3236 const TType &type = uniform->getType();
3237 ASSERT(!IsSampler(type.getBasicType()));
3238 TInterfaceBlock *block = type.getAsInterfaceBlock();
3239 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3240 ASSERT(symbol || block);
3244 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3245 bool isBlockMember = (!block && parentBlock);
3246 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3248 if(index == -1 || isBlockMember)
3252 index = allocate(uniforms, uniform);
3255 // Verify if the current uniform is a member of an already declared block
3256 const TString &name = symbol ? symbol->getSymbol() : block->name();
3257 int blockMemberIndex = blockMemberLookup(type, name, index);
3258 if(blockMemberIndex == -1)
3260 declareUniform(type, name, index, false);
3264 index = blockMemberIndex;
3274 int OutputASM::attributeRegister(TIntermTyped *attribute)
3276 ASSERT(!attribute->isArray());
3278 int index = lookup(attributes, attribute);
3282 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3287 index = allocate(attributes, attribute);
3288 const TType &type = attribute->getType();
3289 int registerCount = attribute->totalRegisterCount();
3290 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3291 switch(type.getBasicType())
3294 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3297 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3304 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
3306 for(int i = 0; i < registerCount; i++)
3308 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
3312 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3314 const char *name = symbol->getSymbol().c_str();
3315 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3322 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3324 return allocate(fragmentOutputs, fragmentOutput);
3327 int OutputASM::samplerRegister(TIntermTyped *sampler)
3329 const TType &type = sampler->getType();
3330 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3332 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3333 TIntermBinary *binary = sampler->getAsBinaryNode();
3337 switch(type.getQualifier())
3340 return samplerRegister(symbol);
3342 case EvqConstReadOnly:
3343 // Function arguments are not (uniform) sampler registers
3346 UNREACHABLE(type.getQualifier());
3351 TIntermTyped *left = binary->getLeft();
3352 TIntermTyped *right = binary->getRight();
3353 const TType &leftType = left->getType();
3354 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3357 switch(binary->getOp())
3359 case EOpIndexDirect:
3360 ASSERT(left->isArray());
3361 offset = index * leftType.samplerRegisterCount();
3363 case EOpIndexDirectStruct:
3364 ASSERT(leftType.isStruct());
3366 const TFieldList &fields = leftType.getStruct()->fields();
3368 for(int i = 0; i < index; i++)
3370 offset += fields[i]->type()->totalSamplerRegisterCount();
3374 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3376 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3378 UNREACHABLE(binary->getOp());
3382 int base = samplerRegister(left);
3389 return base + offset;
3393 return -1; // Not a (uniform) sampler register
3396 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3398 const TType &type = sampler->getType();
3399 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3401 int index = lookup(samplers, sampler);
3405 index = allocate(samplers, sampler, true);
3407 if(sampler->getQualifier() == EvqUniform)
3409 const char *name = sampler->getSymbol().c_str();
3410 declareUniform(type, name, index, true);
3417 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3419 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3422 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3424 for(unsigned int i = 0; i < list.size(); i++)
3426 if(list[i] == variable)
3428 return i; // Pointer match
3432 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3433 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3437 for(unsigned int i = 0; i < list.size(); i++)
3441 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3445 if(listBlock->name() == varBlock->name())
3447 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3448 ASSERT(listBlock->fields() == varBlock->fields());
3449 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3450 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3460 for(unsigned int i = 0; i < list.size(); i++)
3464 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3468 if(listSymbol->getId() == varSymbol->getId())
3470 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3471 ASSERT(listSymbol->getType() == varSymbol->getType());
3472 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3484 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3486 for(unsigned int i = 0; i < list.size(); i++)
3488 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3490 return i; // Pointer match
3496 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
3498 int index = lookup(list, variable);
3502 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
3504 for(unsigned int i = 0; i < list.size(); i++)
3509 for( ; j < registerCount && (i + j) < list.size(); j++)
3511 if(list[i + j] != 0)
3517 if(j == registerCount) // Found free slots
3519 for(unsigned int j = 0; j < registerCount; j++)
3521 list[i + j] = variable;
3529 index = list.size();
3531 for(unsigned int i = 0; i < registerCount; i++)
3533 list.push_back(variable);
3540 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3542 int index = lookup(list, variable);
3550 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3552 const TInterfaceBlock *block = type.getInterfaceBlock();
3556 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3557 const TFieldList& fields = block->fields();
3558 const TString &blockName = block->name();
3559 int fieldRegisterIndex = registerIndex;
3561 if(!type.isInterfaceBlock())
3563 // This is a uniform that's part of a block, let's see if the block is already defined
3564 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3566 if(activeUniformBlocks[i].name == blockName.c_str())
3568 // The block is already defined, find the register for the current uniform and return it
3569 for(size_t j = 0; j < fields.size(); j++)
3571 const TString &fieldName = fields[j]->name();
3572 if(fieldName == name)
3574 return fieldRegisterIndex;
3577 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3581 return fieldRegisterIndex;
3590 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
3592 const TStructure *structure = type.getStruct();
3593 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3595 if(!structure && !block)
3597 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3598 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3601 blockDefinitions[blockId].insert(BlockDefinitionIndexMap::value_type(registerIndex, TypedMemberInfo(blockInfo, type)));
3602 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3604 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3605 bool isSampler = IsSampler(type.getBasicType());
3606 if(isSampler && samplersOnly)
3608 for(int i = 0; i < type.totalRegisterCount(); i++)
3610 shader->declareSampler(fieldRegisterIndex + i);
3613 if(isSampler == samplersOnly)
3615 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
3620 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3621 const TFieldList& fields = block->fields();
3622 const TString &blockName = block->name();
3623 int fieldRegisterIndex = registerIndex;
3624 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3626 blockId = activeUniformBlocks.size();
3627 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3628 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3629 block->blockStorage(), isRowMajor, registerIndex, blockId));
3630 blockDefinitions.push_back(BlockDefinitionIndexMap());
3632 Std140BlockEncoder currentBlockEncoder;
3633 currentBlockEncoder.enterAggregateType();
3634 for(const auto &field : fields)
3636 const TType &fieldType = *(field->type());
3637 const TString &fieldName = field->name();
3638 if(isUniformBlockMember && (fieldName == name))
3640 registerIndex = fieldRegisterIndex;
3643 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3645 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, ¤tBlockEncoder);
3646 fieldRegisterIndex += fieldType.totalRegisterCount();
3648 currentBlockEncoder.exitAggregateType();
3649 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3653 // Store struct for program link time validation
3654 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3656 int fieldRegisterIndex = registerIndex;
3658 const TFieldList& fields = structure->fields();
3659 if(type.isArray() && (structure || type.isInterfaceBlock()))
3661 for(int i = 0; i < type.getArraySize(); i++)
3665 encoder->enterAggregateType();
3667 for(const auto &field : fields)
3669 const TType &fieldType = *(field->type());
3670 const TString &fieldName = field->name();
3671 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3673 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3674 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
3678 encoder->exitAggregateType();
3686 encoder->enterAggregateType();
3688 for(const auto &field : fields)
3690 const TType &fieldType = *(field->type());
3691 const TString &fieldName = field->name();
3692 const TString uniformName = name + "." + fieldName;
3694 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3695 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
3699 encoder->exitAggregateType();
3705 int OutputASM::dim(TIntermNode *v)
3707 TIntermTyped *vector = v->getAsTyped();
3708 ASSERT(vector && vector->isRegister());
3709 return vector->getNominalSize();
3712 int OutputASM::dim2(TIntermNode *m)
3714 TIntermTyped *matrix = m->getAsTyped();
3715 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3716 return matrix->getSecondarySize();
3719 // Returns ~0u if no loop count could be determined
3720 unsigned int OutputASM::loopCount(TIntermLoop *node)
3722 // Parse loops of the form:
3723 // for(int index = initial; index [comparator] limit; index += increment)
3724 TIntermSymbol *index = 0;
3725 TOperator comparator = EOpNull;
3730 // Parse index name and intial value
3733 TIntermAggregate *init = node->getInit()->getAsAggregate();
3737 TIntermSequence &sequence = init->getSequence();
3738 TIntermTyped *variable = sequence[0]->getAsTyped();
3740 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
3742 TIntermBinary *assign = variable->getAsBinaryNode();
3744 if(assign && assign->getOp() == EOpInitialize)
3746 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3747 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3749 if(symbol && constant)
3751 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3754 initial = constant->getUnionArrayPointer()[0].getIConst();
3762 // Parse comparator and limit value
3763 if(index && node->getCondition())
3765 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
3766 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
3768 if(left && (left->getId() == index->getId()))
3770 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3774 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3776 comparator = test->getOp();
3777 limit = constant->getUnionArrayPointer()[0].getIConst();
3784 if(index && comparator != EOpNull && node->getExpression())
3786 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3787 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3791 TOperator op = binaryTerminal->getOp();
3792 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3796 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3798 int value = constant->getUnionArrayPointer()[0].getIConst();
3802 case EOpAddAssign: increment = value; break;
3803 case EOpSubAssign: increment = -value; break;
3804 default: UNIMPLEMENTED();
3809 else if(unaryTerminal)
3811 TOperator op = unaryTerminal->getOp();
3815 case EOpPostIncrement: increment = 1; break;
3816 case EOpPostDecrement: increment = -1; break;
3817 case EOpPreIncrement: increment = 1; break;
3818 case EOpPreDecrement: increment = -1; break;
3819 default: UNIMPLEMENTED();
3824 if(index && comparator != EOpNull && increment != 0)
3826 if(comparator == EOpLessThanEqual)
3828 comparator = EOpLessThan;
3831 else if(comparator == EOpGreaterThanEqual)
3833 comparator = EOpLessThan;
3835 std::swap(initial, limit);
3836 increment = -increment;
3838 else if(comparator == EOpGreaterThan)
3840 comparator = EOpLessThan;
3841 std::swap(initial, limit);
3842 increment = -increment;
3845 if(comparator == EOpLessThan)
3847 if(!(initial < limit)) // Never loops
3852 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3861 else UNIMPLEMENTED(); // Falls through
3867 bool LoopUnrollable::traverse(TIntermNode *node)
3870 loopUnrollable = true;
3872 node->traverse(this);
3874 return loopUnrollable;
3877 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3879 if(visit == PreVisit)
3883 else if(visit == PostVisit)
3891 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3903 switch(node->getFlowOp())
3910 loopUnrollable = false;
3912 default: UNREACHABLE(node->getFlowOp());
3915 return loopUnrollable;
3918 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3920 return loopUnrollable;