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")
428 else if(name == "texture2DProj" || name == "textureProj")
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")
497 else if(name == "texture2DRect")
501 else if(name == "texture2DRectProj")
509 OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
512 pixelShader = nullptr;
513 vertexShader = nullptr;
517 shader = shaderObject->getShader();
518 pixelShader = shaderObject->getPixelShader();
519 vertexShader = shaderObject->getVertexShader();
522 functionArray.push_back(Function(0, "main(", nullptr, nullptr));
524 outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
527 OutputASM::~OutputASM()
531 void OutputASM::output()
537 if(functionArray.size() > 1) // Only call main() when there are other functions
539 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
540 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
541 callMain->dst.index = 0; // main()
543 emit(sw::Shader::OPCODE_RET);
546 emitShader(FUNCTION);
550 void OutputASM::emitShader(Scope scope)
553 currentScope = GLOBAL;
554 mContext.getTreeRoot()->traverse(this);
557 void OutputASM::freeTemporary(Temporary *temporary)
559 free(temporaries, temporary);
562 sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
564 TBasicType baseType = in->getType().getBasicType();
568 case sw::Shader::OPCODE_NEG:
573 return sw::Shader::OPCODE_INEG;
578 case sw::Shader::OPCODE_ABS:
582 return sw::Shader::OPCODE_IABS;
587 case sw::Shader::OPCODE_SGN:
591 return sw::Shader::OPCODE_ISGN;
596 case sw::Shader::OPCODE_ADD:
601 return sw::Shader::OPCODE_IADD;
606 case sw::Shader::OPCODE_SUB:
611 return sw::Shader::OPCODE_ISUB;
616 case sw::Shader::OPCODE_MUL:
621 return sw::Shader::OPCODE_IMUL;
626 case sw::Shader::OPCODE_DIV:
630 return sw::Shader::OPCODE_IDIV;
632 return sw::Shader::OPCODE_UDIV;
637 case sw::Shader::OPCODE_IMOD:
638 return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
639 case sw::Shader::OPCODE_ISHR:
640 return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
641 case sw::Shader::OPCODE_MIN:
645 return sw::Shader::OPCODE_IMIN;
647 return sw::Shader::OPCODE_UMIN;
652 case sw::Shader::OPCODE_MAX:
656 return sw::Shader::OPCODE_IMAX;
658 return sw::Shader::OPCODE_UMAX;
668 void OutputASM::visitSymbol(TIntermSymbol *symbol)
670 // The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
671 // so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
672 switch(symbol->getQualifier())
676 case EvqInvariantVaryingIn:
677 case EvqInvariantVaryingOut:
680 if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
682 declareVarying(symbol, -1);
686 declareFragmentOutput(symbol);
692 TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
693 // OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
694 // "All members of a named uniform block declared with a shared or std140 layout qualifier
695 // are considered active, even if they are not referenced in any shader in the program.
696 // The uniform block itself is also considered active, even if no member of the block is referenced."
697 if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
699 uniformRegister(symbol);
703 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
705 if(currentScope != emitScope)
710 TIntermTyped *result = node;
711 TIntermTyped *left = node->getLeft();
712 TIntermTyped *right = node->getRight();
713 const TType &leftType = left->getType();
714 const TType &rightType = right->getType();
716 if(isSamplerRegister(result))
718 return false; // Don't traverse, the register index is determined statically
721 switch(node->getOp())
724 assert(visit == PreVisit);
725 right->traverse(this);
726 assignLvalue(left, right);
730 assert(visit == PreVisit);
731 // Constant arrays go into the constant register file.
732 if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
734 for(int i = 0; i < left->totalRegisterCount(); i++)
736 emit(sw::Shader::OPCODE_DEF, left, i, right, i);
741 right->traverse(this);
745 case EOpMatrixTimesScalarAssign:
746 assert(visit == PreVisit);
747 right->traverse(this);
748 for(int i = 0; i < leftType.getNominalSize(); i++)
750 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
753 assignLvalue(left, result);
755 case EOpVectorTimesMatrixAssign:
756 assert(visit == PreVisit);
758 right->traverse(this);
759 int size = leftType.getNominalSize();
761 for(int i = 0; i < size; i++)
763 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
764 dot->dst.mask = 1 << i;
767 assignLvalue(left, result);
770 case EOpMatrixTimesMatrixAssign:
771 assert(visit == PreVisit);
773 right->traverse(this);
774 int dim = leftType.getNominalSize();
776 for(int i = 0; i < dim; i++)
778 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
779 mul->src[1].swizzle = 0x00;
781 for(int j = 1; j < dim; j++)
783 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
784 mad->src[1].swizzle = j * 0x55;
788 assignLvalue(left, result);
792 case EOpIndexIndirect:
793 case EOpIndexDirectStruct:
794 case EOpIndexDirectInterfaceBlock:
795 assert(visit == PreVisit);
796 evaluateRvalue(node);
798 case EOpVectorSwizzle:
799 if(visit == PostVisit)
802 TIntermAggregate *components = right->getAsAggregate();
806 TIntermSequence &sequence = components->getSequence();
809 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
811 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
815 int i = element->getUnionArrayPointer()[0].getIConst();
816 swizzle |= i << (component * 2);
824 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
825 mov->src[0].swizzle = swizzle;
828 case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
829 case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
830 case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
831 case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
832 case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
833 case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
834 case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
835 case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
836 case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
837 case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
838 case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
839 case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
840 case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
841 case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
842 case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
843 case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
844 case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
845 case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
846 case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
847 case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
849 if(visit == PostVisit)
851 emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
853 for(int index = 1; index < left->totalRegisterCount(); index++)
855 Temporary equal(this);
856 emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
857 emit(sw::Shader::OPCODE_AND, result, result, &equal);
862 if(visit == PostVisit)
864 emitBinary(sw::Shader::OPCODE_NE, result, left, right);
866 for(int index = 1; index < left->totalRegisterCount(); index++)
868 Temporary notEqual(this);
869 emit(sw::Shader::OPCODE_NE, ¬Equal, 0, left, index, right, index);
870 emit(sw::Shader::OPCODE_OR, result, result, ¬Equal);
874 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
875 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
876 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
877 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
878 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
879 case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
880 case EOpMatrixTimesScalar:
881 if(visit == PostVisit)
885 for(int i = 0; i < leftType.getNominalSize(); i++)
887 emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
890 else if(right->isMatrix())
892 for(int i = 0; i < rightType.getNominalSize(); i++)
894 emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
900 case EOpVectorTimesMatrix:
901 if(visit == PostVisit)
903 sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
905 int size = rightType.getNominalSize();
906 for(int i = 0; i < size; i++)
908 Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
909 dot->dst.mask = 1 << i;
913 case EOpMatrixTimesVector:
914 if(visit == PostVisit)
916 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
917 mul->src[1].swizzle = 0x00;
919 int size = rightType.getNominalSize();
920 for(int i = 1; i < size; i++)
922 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
923 mad->src[1].swizzle = i * 0x55;
927 case EOpMatrixTimesMatrix:
928 if(visit == PostVisit)
930 int dim = leftType.getNominalSize();
932 int size = rightType.getNominalSize();
933 for(int i = 0; i < size; i++)
935 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
936 mul->src[1].swizzle = 0x00;
938 for(int j = 1; j < dim; j++)
940 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
941 mad->src[1].swizzle = j * 0x55;
947 if(trivial(right, 6))
949 if(visit == PostVisit)
951 emit(sw::Shader::OPCODE_OR, result, left, right);
954 else // Short-circuit evaluation
958 emit(sw::Shader::OPCODE_MOV, result, left);
959 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
960 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
962 else if(visit == PostVisit)
964 emit(sw::Shader::OPCODE_MOV, result, right);
965 emit(sw::Shader::OPCODE_ENDIF);
969 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
971 if(trivial(right, 6))
973 if(visit == PostVisit)
975 emit(sw::Shader::OPCODE_AND, result, left, right);
978 else // Short-circuit evaluation
982 emit(sw::Shader::OPCODE_MOV, result, left);
983 emit(sw::Shader::OPCODE_IF, 0, result);
985 else if(visit == PostVisit)
987 emit(sw::Shader::OPCODE_MOV, result, right);
988 emit(sw::Shader::OPCODE_ENDIF);
992 default: UNREACHABLE(node->getOp());
998 void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
1002 case 1: // Used for cofactor computation only
1004 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1005 bool isMov = (row == col);
1006 sw::Shader::Opcode op = isMov ? sw::Shader::OPCODE_MOV : sw::Shader::OPCODE_NEG;
1007 Instruction *mov = emit(op, result, outCol, arg, isMov ? 1 - row : row);
1008 mov->src[0].swizzle = 0x55 * (isMov ? 1 - col : col);
1009 mov->dst.mask = 1 << outRow;
1014 static const unsigned int swizzle[3] = { 0x99, 0x88, 0x44 }; // xy?? : yzyz, xzxz, xyxy
1016 bool isCofactor = (col >= 0) && (row >= 0);
1017 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1018 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1019 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1021 Instruction *det = emit(sw::Shader::OPCODE_DET2, result, outCol, arg, negate ? col1 : col0, arg, negate ? col0 : col1);
1022 det->src[0].swizzle = det->src[1].swizzle = swizzle[isCofactor ? row : 2];
1023 det->dst.mask = 1 << outRow;
1028 static const unsigned int swizzle[4] = { 0xF9, 0xF8, 0xF4, 0xE4 }; // xyz? : yzww, xzww, xyww, xyzw
1030 bool isCofactor = (col >= 0) && (row >= 0);
1031 int col0 = (isCofactor && (col <= 0)) ? 1 : 0;
1032 int col1 = (isCofactor && (col <= 1)) ? 2 : 1;
1033 int col2 = (isCofactor && (col <= 2)) ? 3 : 2;
1034 bool negate = isCofactor && ((col & 0x01) ^ (row & 0x01));
1036 Instruction *det = emit(sw::Shader::OPCODE_DET3, result, outCol, arg, col0, arg, negate ? col2 : col1, arg, negate ? col1 : col2);
1037 det->src[0].swizzle = det->src[1].swizzle = det->src[2].swizzle = swizzle[isCofactor ? row : 3];
1038 det->dst.mask = 1 << outRow;
1043 Instruction *det = emit(sw::Shader::OPCODE_DET4, result, outCol, arg, 0, arg, 1, arg, 2, arg, 3);
1044 det->dst.mask = 1 << outRow;
1053 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
1055 if(currentScope != emitScope)
1060 TIntermTyped *result = node;
1061 TIntermTyped *arg = node->getOperand();
1062 TBasicType basicType = arg->getType().getBasicType();
1070 if(basicType == EbtInt || basicType == EbtUInt)
1079 Constant one(one_value.f, one_value.f, one_value.f, one_value.f);
1080 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
1081 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
1083 switch(node->getOp())
1086 if(visit == PostVisit)
1088 sw::Shader::Opcode negOpcode = getOpcode(sw::Shader::OPCODE_NEG, arg);
1089 for(int index = 0; index < arg->totalRegisterCount(); index++)
1091 emit(negOpcode, result, index, arg, index);
1095 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1096 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1097 case EOpBitwiseNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
1098 case EOpPostIncrement:
1099 if(visit == PostVisit)
1103 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1104 for(int index = 0; index < arg->totalRegisterCount(); index++)
1106 emit(addOpcode, arg, index, arg, index, &one);
1109 assignLvalue(arg, arg);
1112 case EOpPostDecrement:
1113 if(visit == PostVisit)
1117 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1118 for(int index = 0; index < arg->totalRegisterCount(); index++)
1120 emit(subOpcode, arg, index, arg, index, &one);
1123 assignLvalue(arg, arg);
1126 case EOpPreIncrement:
1127 if(visit == PostVisit)
1129 sw::Shader::Opcode addOpcode = getOpcode(sw::Shader::OPCODE_ADD, arg);
1130 for(int index = 0; index < arg->totalRegisterCount(); index++)
1132 emit(addOpcode, result, index, arg, index, &one);
1135 assignLvalue(arg, result);
1138 case EOpPreDecrement:
1139 if(visit == PostVisit)
1141 sw::Shader::Opcode subOpcode = getOpcode(sw::Shader::OPCODE_SUB, arg);
1142 for(int index = 0; index < arg->totalRegisterCount(); index++)
1144 emit(subOpcode, result, index, arg, index, &one);
1147 assignLvalue(arg, result);
1150 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
1151 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, °); break;
1152 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
1153 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
1154 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
1155 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
1156 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
1157 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
1158 case EOpSinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_SINH, result, arg); break;
1159 case EOpCosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_COSH, result, arg); break;
1160 case EOpTanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_TANH, result, arg); break;
1161 case EOpAsinh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASINH, result, arg); break;
1162 case EOpAcosh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOSH, result, arg); break;
1163 case EOpAtanh: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATANH, result, arg); break;
1164 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
1165 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
1166 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
1167 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
1168 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
1169 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
1170 case EOpAbs: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_ABS, result), result, arg); break;
1171 case EOpSign: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_SGN, result), result, arg); break;
1172 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
1173 case EOpTrunc: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break;
1174 case EOpRound: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUND, result, arg); break;
1175 case EOpRoundEven: if(visit == PostVisit) emit(sw::Shader::OPCODE_ROUNDEVEN, result, arg); break;
1176 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
1177 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
1178 case EOpIsNan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISNAN, result, arg); break;
1179 case EOpIsInf: if(visit == PostVisit) emit(sw::Shader::OPCODE_ISINF, result, arg); break;
1180 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
1181 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
1182 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
1183 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
1184 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
1185 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
1186 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
1187 case EOpFloatBitsToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOINT, result, arg); break;
1188 case EOpFloatBitsToUint: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOATBITSTOUINT, result, arg); break;
1189 case EOpIntBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_INTBITSTOFLOAT, result, arg); break;
1190 case EOpUintBitsToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_UINTBITSTOFLOAT, result, arg); break;
1191 case EOpPackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKSNORM2x16, result, arg); break;
1192 case EOpPackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKUNORM2x16, result, arg); break;
1193 case EOpPackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_PACKHALF2x16, result, arg); break;
1194 case EOpUnpackSnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKSNORM2x16, result, arg); break;
1195 case EOpUnpackUnorm2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKUNORM2x16, result, arg); break;
1196 case EOpUnpackHalf2x16: if(visit == PostVisit) emit(sw::Shader::OPCODE_UNPACKHALF2x16, result, arg); break;
1198 if(visit == PostVisit)
1200 int numCols = arg->getNominalSize();
1201 int numRows = arg->getSecondarySize();
1202 for(int i = 0; i < numCols; ++i)
1204 for(int j = 0; j < numRows; ++j)
1206 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, j, arg, i);
1207 mov->src[0].swizzle = 0x55 * j;
1208 mov->dst.mask = 1 << i;
1213 case EOpDeterminant:
1214 if(visit == PostVisit)
1216 int size = arg->getNominalSize();
1217 ASSERT(size == arg->getSecondarySize());
1219 emitDeterminant(result, arg, size);
1223 if(visit == PostVisit)
1225 int size = arg->getNominalSize();
1226 ASSERT(size == arg->getSecondarySize());
1228 // Compute transposed matrix of cofactors
1229 for(int i = 0; i < size; ++i)
1231 for(int j = 0; j < size; ++j)
1233 // For a 2x2 matrix, the cofactor is simply a transposed move or negate
1234 // For a 3x3 or 4x4 matrix, the cofactor is a transposed determinant
1235 emitDeterminant(result, arg, size - 1, j, i, i, j);
1239 // Compute 1 / determinant
1240 Temporary invDet(this);
1241 emitDeterminant(&invDet, arg, size);
1242 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
1243 Instruction *div = emit(sw::Shader::OPCODE_DIV, &invDet, &one, &invDet);
1244 div->src[1].swizzle = 0x00; // xxxx
1246 // Divide transposed matrix of cofactors by determinant
1247 for(int i = 0; i < size; ++i)
1249 emit(sw::Shader::OPCODE_MUL, result, i, result, i, &invDet);
1253 default: UNREACHABLE(node->getOp());
1259 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
1261 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
1266 Constant zero(0.0f, 0.0f, 0.0f, 0.0f);
1268 TIntermTyped *result = node;
1269 const TType &resultType = node->getType();
1270 TIntermSequence &arg = node->getSequence();
1271 size_t argumentCount = arg.size();
1273 switch(node->getOp())
1275 case EOpSequence: break;
1276 case EOpDeclaration: break;
1277 case EOpInvariantDeclaration: break;
1278 case EOpPrototype: break;
1280 if(visit == PostVisit)
1282 copy(result, arg[1]);
1286 if(visit == PreVisit)
1288 const TString &name = node->getName();
1290 if(emitScope == FUNCTION)
1292 if(functionArray.size() > 1) // No need for a label when there's only main()
1294 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
1295 label->dst.type = sw::Shader::PARAMETER_LABEL;
1297 const Function *function = findFunction(name);
1298 ASSERT(function); // Should have been added during global pass
1299 label->dst.index = function->label;
1300 currentFunction = function->label;
1303 else if(emitScope == GLOBAL)
1307 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
1308 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
1311 else UNREACHABLE(emitScope);
1313 currentScope = FUNCTION;
1315 else if(visit == PostVisit)
1317 if(emitScope == FUNCTION)
1319 if(functionArray.size() > 1) // No need to return when there's only main()
1321 emit(sw::Shader::OPCODE_RET);
1325 currentScope = GLOBAL;
1328 case EOpFunctionCall:
1329 if(visit == PostVisit)
1331 if(node->isUserDefined())
1333 const TString &name = node->getName();
1334 const Function *function = findFunction(name);
1338 mContext.error(node->getLine(), "function definition not found", name.c_str());
1342 TIntermSequence &arguments = *function->arg;
1344 for(size_t i = 0; i < argumentCount; i++)
1346 TIntermTyped *in = arguments[i]->getAsTyped();
1348 if(in->getQualifier() == EvqIn ||
1349 in->getQualifier() == EvqInOut ||
1350 in->getQualifier() == EvqConstReadOnly)
1356 Instruction *call = emit(sw::Shader::OPCODE_CALL);
1357 call->dst.type = sw::Shader::PARAMETER_LABEL;
1358 call->dst.index = function->label;
1360 if(function->ret && function->ret->getType().getBasicType() != EbtVoid)
1362 copy(result, function->ret);
1365 for(size_t i = 0; i < argumentCount; i++)
1367 TIntermTyped *argument = arguments[i]->getAsTyped();
1368 TIntermTyped *out = arg[i]->getAsTyped();
1370 if(argument->getQualifier() == EvqOut ||
1371 argument->getQualifier() == EvqInOut)
1373 assignLvalue(out, argument);
1379 const TextureFunction textureFunction(node->getName());
1380 TIntermTyped *s = arg[0]->getAsTyped();
1381 TIntermTyped *t = arg[1]->getAsTyped();
1383 Temporary coord(this);
1385 if(textureFunction.proj)
1387 Instruction *rcp = emit(sw::Shader::OPCODE_RCPX, &coord, arg[1]);
1388 rcp->src[0].swizzle = 0x55 * (t->getNominalSize() - 1);
1389 rcp->dst.mask = 0x7;
1391 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &coord, arg[1], &coord);
1392 mul->dst.mask = 0x7;
1394 if(IsShadowSampler(s->getBasicType()))
1396 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1397 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, &coord);
1398 mov->src[0].swizzle = 0xA4;
1403 Instruction *mov = emit(sw::Shader::OPCODE_MOV, &coord, arg[1]);
1405 if(IsShadowSampler(s->getBasicType()) && t->getNominalSize() == 3)
1407 ASSERT(s->getBasicType() == EbtSampler2DShadow);
1408 mov->src[0].swizzle = 0xA4;
1412 switch(textureFunction.method)
1414 case TextureFunction::IMPLICIT:
1415 if(!textureFunction.offset)
1417 if(argumentCount == 2)
1419 emit(sw::Shader::OPCODE_TEX, result, &coord, s);
1421 else if(argumentCount == 3) // Bias
1423 emit(sw::Shader::OPCODE_TEXBIAS, result, &coord, s, arg[2]);
1425 else UNREACHABLE(argumentCount);
1429 if(argumentCount == 3)
1431 emit(sw::Shader::OPCODE_TEXOFFSET, result, &coord, s, arg[2]);
1433 else if(argumentCount == 4) // Bias
1435 emit(sw::Shader::OPCODE_TEXOFFSETBIAS, result, &coord, s, arg[2], arg[3]);
1437 else UNREACHABLE(argumentCount);
1440 case TextureFunction::LOD:
1441 if(!textureFunction.offset && argumentCount == 3)
1443 emit(sw::Shader::OPCODE_TEXLOD, result, &coord, s, arg[2]);
1445 else if(argumentCount == 4) // Offset
1447 emit(sw::Shader::OPCODE_TEXLODOFFSET, result, &coord, s, arg[3], arg[2]);
1449 else UNREACHABLE(argumentCount);
1451 case TextureFunction::FETCH:
1452 if(!textureFunction.offset && argumentCount == 3)
1454 emit(sw::Shader::OPCODE_TEXELFETCH, result, &coord, s, arg[2]);
1456 else if(argumentCount == 4) // Offset
1458 emit(sw::Shader::OPCODE_TEXELFETCHOFFSET, result, &coord, s, arg[3], arg[2]);
1460 else UNREACHABLE(argumentCount);
1462 case TextureFunction::GRAD:
1463 if(!textureFunction.offset && argumentCount == 4)
1465 emit(sw::Shader::OPCODE_TEXGRAD, result, &coord, s, arg[2], arg[3]);
1467 else if(argumentCount == 5) // Offset
1469 emit(sw::Shader::OPCODE_TEXGRADOFFSET, result, &coord, s, arg[2], arg[3], arg[4]);
1471 else UNREACHABLE(argumentCount);
1473 case TextureFunction::RECT:
1474 emit(sw::Shader::OPCODE_TEXRECT, result, &coord, s);
1476 case TextureFunction::SIZE:
1477 emit(sw::Shader::OPCODE_TEXSIZE, result, arg[1], s);
1480 UNREACHABLE(textureFunction.method);
1487 case EOpConstructFloat:
1488 case EOpConstructVec2:
1489 case EOpConstructVec3:
1490 case EOpConstructVec4:
1491 case EOpConstructBool:
1492 case EOpConstructBVec2:
1493 case EOpConstructBVec3:
1494 case EOpConstructBVec4:
1495 case EOpConstructInt:
1496 case EOpConstructIVec2:
1497 case EOpConstructIVec3:
1498 case EOpConstructIVec4:
1499 case EOpConstructUInt:
1500 case EOpConstructUVec2:
1501 case EOpConstructUVec3:
1502 case EOpConstructUVec4:
1503 if(visit == PostVisit)
1506 int arrayMaxIndex = result->isArray() ? result->getArraySize() - 1 : 0;
1507 int arrayComponents = result->getType().getElementSize();
1508 for(size_t i = 0; i < argumentCount; i++)
1510 TIntermTyped *argi = arg[i]->getAsTyped();
1511 int size = argi->getNominalSize();
1512 int arrayIndex = std::min(component / arrayComponents, arrayMaxIndex);
1513 int swizzle = component - (arrayIndex * arrayComponents);
1515 if(!argi->isMatrix())
1517 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1518 mov->dst.mask = (0xF << swizzle) & 0xF;
1519 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1523 else if(!result->isMatrix()) // Construct a non matrix from a matrix
1525 Instruction *mov = emitCast(result, arrayIndex, argi, 0);
1526 mov->dst.mask = (0xF << swizzle) & 0xF;
1527 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1529 // At most one more instruction when constructing a vec3 from a mat2 or a vec4 from a mat2/mat3
1530 if(result->getNominalSize() > size)
1532 Instruction *mov = emitCast(result, arrayIndex, argi, 1);
1533 mov->dst.mask = (0xF << (swizzle + size)) & 0xF;
1534 // mat2: xxxy (0x40), mat3: xxxx (0x00)
1535 mov->src[0].swizzle = ((size == 2) ? 0x40 : 0x00) << (swizzle * 2);
1544 while(component < resultType.getNominalSize())
1546 Instruction *mov = emitCast(result, arrayIndex, argi, column);
1547 mov->dst.mask = (0xF << swizzle) & 0xF;
1548 mov->src[0].swizzle = readSwizzle(argi, size) << (swizzle * 2);
1557 case EOpConstructMat2:
1558 case EOpConstructMat2x3:
1559 case EOpConstructMat2x4:
1560 case EOpConstructMat3x2:
1561 case EOpConstructMat3:
1562 case EOpConstructMat3x4:
1563 case EOpConstructMat4x2:
1564 case EOpConstructMat4x3:
1565 case EOpConstructMat4:
1566 if(visit == PostVisit)
1568 TIntermTyped *arg0 = arg[0]->getAsTyped();
1569 const int outCols = result->getNominalSize();
1570 const int outRows = result->getSecondarySize();
1572 if(arg0->isScalar() && arg.size() == 1) // Construct scale matrix
1574 for(int i = 0; i < outCols; i++)
1576 emit(sw::Shader::OPCODE_MOV, result, i, &zero);
1577 Instruction *mov = emitCast(result, i, arg0, 0);
1578 mov->dst.mask = 1 << i;
1579 ASSERT(mov->src[0].swizzle == 0x00);
1582 else if(arg0->isMatrix())
1584 int arraySize = result->isArray() ? result->getArraySize() : 1;
1586 for(int n = 0; n < arraySize; n++)
1588 TIntermTyped *argi = arg[n]->getAsTyped();
1589 const int inCols = argi->getNominalSize();
1590 const int inRows = argi->getSecondarySize();
1592 for(int i = 0; i < outCols; i++)
1594 if(i >= inCols || outRows > inRows)
1596 // Initialize to identity matrix
1597 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));
1598 emitCast(result, i + n * outCols, &col, 0);
1603 Instruction *mov = emitCast(result, i + n * outCols, argi, i);
1604 mov->dst.mask = 0xF >> (4 - inRows);
1614 for(size_t i = 0; i < argumentCount; i++)
1616 TIntermTyped *argi = arg[i]->getAsTyped();
1617 int size = argi->getNominalSize();
1620 while(element < size)
1622 Instruction *mov = emitCast(result, column, argi, 0);
1623 mov->dst.mask = (0xF << row) & 0xF;
1624 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
1626 int end = row + size - element;
1627 column = end >= outRows ? column + 1 : column;
1628 element = element + outRows - row;
1629 row = end >= outRows ? 0 : end;
1635 case EOpConstructStruct:
1636 if(visit == PostVisit)
1639 for(size_t i = 0; i < argumentCount; i++)
1641 TIntermTyped *argi = arg[i]->getAsTyped();
1642 int size = argi->totalRegisterCount();
1644 for(int index = 0; index < size; index++)
1646 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, index + offset, argi, index);
1647 mov->dst.mask = writeMask(result, offset + index);
1654 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
1655 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
1656 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
1657 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
1658 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
1659 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
1660 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
1662 if(visit == PostVisit)
1664 TIntermTyped* arg1 = arg[1]->getAsTyped();
1665 emit(sw::Shader::OPCODE_TRUNC, arg1, arg[0]);
1666 assignLvalue(arg1, arg1);
1667 emitBinary(sw::Shader::OPCODE_SUB, result, arg[0], arg1);
1670 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
1671 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
1672 case EOpMin: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, arg[0], arg[1]); break;
1673 case EOpMax: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]); break;
1675 if(visit == PostVisit)
1677 emit(getOpcode(sw::Shader::OPCODE_MAX, result), result, arg[0], arg[1]);
1678 emit(getOpcode(sw::Shader::OPCODE_MIN, result), result, result, arg[2]);
1682 if(visit == PostVisit)
1684 if(arg[2]->getAsTyped()->getBasicType() == EbtBool)
1686 emit(sw::Shader::OPCODE_SELECT, result, arg[2], arg[1], arg[0]);
1690 emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]);
1694 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
1695 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
1696 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
1697 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
1698 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
1699 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1700 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
1701 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
1703 if(visit == PostVisit)
1705 TIntermTyped *arg0 = arg[0]->getAsTyped();
1706 ASSERT((arg0->getNominalSize() == arg[1]->getAsTyped()->getNominalSize()) &&
1707 (arg0->getSecondarySize() == arg[1]->getAsTyped()->getSecondarySize()));
1709 int size = arg0->getNominalSize();
1710 for(int i = 0; i < size; i++)
1712 emit(sw::Shader::OPCODE_MUL, result, i, arg[0], i, arg[1], i);
1716 case EOpOuterProduct:
1717 if(visit == PostVisit)
1719 for(int i = 0; i < dim(arg[1]); i++)
1721 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, arg[0], 0, arg[1]);
1722 mul->src[1].swizzle = 0x55 * i;
1726 default: UNREACHABLE(node->getOp());
1732 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1734 if(currentScope != emitScope)
1739 TIntermTyped *condition = node->getCondition();
1740 TIntermNode *trueBlock = node->getTrueBlock();
1741 TIntermNode *falseBlock = node->getFalseBlock();
1742 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1744 condition->traverse(this);
1746 if(node->usesTernaryOperator())
1748 if(constantCondition)
1750 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1754 trueBlock->traverse(this);
1755 copy(node, trueBlock);
1759 falseBlock->traverse(this);
1760 copy(node, falseBlock);
1763 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1765 trueBlock->traverse(this);
1766 falseBlock->traverse(this);
1767 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1771 emit(sw::Shader::OPCODE_IF, 0, condition);
1775 trueBlock->traverse(this);
1776 copy(node, trueBlock);
1781 emit(sw::Shader::OPCODE_ELSE);
1782 falseBlock->traverse(this);
1783 copy(node, falseBlock);
1786 emit(sw::Shader::OPCODE_ENDIF);
1789 else // if/else statement
1791 if(constantCondition)
1793 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1799 trueBlock->traverse(this);
1806 falseBlock->traverse(this);
1812 emit(sw::Shader::OPCODE_IF, 0, condition);
1816 trueBlock->traverse(this);
1821 emit(sw::Shader::OPCODE_ELSE);
1822 falseBlock->traverse(this);
1825 emit(sw::Shader::OPCODE_ENDIF);
1832 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1834 if(currentScope != emitScope)
1839 unsigned int iterations = loopCount(node);
1846 bool unroll = (iterations <= 4);
1850 LoopUnrollable loopUnrollable;
1851 unroll = loopUnrollable.traverse(node);
1854 TIntermNode *init = node->getInit();
1855 TIntermTyped *condition = node->getCondition();
1856 TIntermTyped *expression = node->getExpression();
1857 TIntermNode *body = node->getBody();
1858 Constant True(true);
1860 if(node->getType() == ELoopDoWhile)
1862 Temporary iterate(this);
1863 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1865 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1869 body->traverse(this);
1872 emit(sw::Shader::OPCODE_TEST);
1874 condition->traverse(this);
1875 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1877 emit(sw::Shader::OPCODE_ENDWHILE);
1883 init->traverse(this);
1888 for(unsigned int i = 0; i < iterations; i++)
1890 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1894 body->traverse(this);
1899 expression->traverse(this);
1907 condition->traverse(this);
1914 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1918 body->traverse(this);
1921 emit(sw::Shader::OPCODE_TEST);
1925 expression->traverse(this);
1930 condition->traverse(this);
1933 emit(sw::Shader::OPCODE_ENDWHILE);
1940 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1942 if(currentScope != emitScope)
1947 switch(node->getFlowOp())
1949 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1950 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1951 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1953 if(visit == PostVisit)
1955 TIntermTyped *value = node->getExpression();
1959 copy(functionArray[currentFunction].ret, value);
1962 emit(sw::Shader::OPCODE_LEAVE);
1965 default: UNREACHABLE(node->getFlowOp());
1971 bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
1973 if(currentScope != emitScope)
1978 TIntermTyped* switchValue = node->getInit();
1979 TIntermAggregate* opList = node->getStatementList();
1981 if(!switchValue || !opList)
1986 switchValue->traverse(this);
1988 emit(sw::Shader::OPCODE_SWITCH);
1990 TIntermSequence& sequence = opList->getSequence();
1991 TIntermSequence::iterator it = sequence.begin();
1992 TIntermSequence::iterator defaultIt = sequence.end();
1994 for(; it != sequence.end(); ++it)
1996 TIntermCase* currentCase = (*it)->getAsCaseNode();
1999 TIntermSequence::iterator caseIt = it;
2001 TIntermTyped* condition = currentCase->getCondition();
2002 if(condition) // non default case
2006 emit(sw::Shader::OPCODE_ELSE);
2009 condition->traverse(this);
2010 Temporary result(this);
2011 emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
2012 emit(sw::Shader::OPCODE_IF, 0, &result);
2015 // Emit the code for this case and all subsequent cases until we hit a break statement.
2016 // TODO: This can repeat a lot of code for switches with many fall-through cases.
2017 for(++caseIt; caseIt != sequence.end(); ++caseIt)
2019 (*caseIt)->traverse(this);
2021 // Stop if we encounter an unconditional branch (break, continue, return, or kill).
2022 // TODO: This doesn't work if the statement is at a deeper scope level (e.g. {break;}).
2023 // Note that this eliminates useless operations but shouldn't affect correctness.
2024 if((*caseIt)->getAsBranchNode())
2032 defaultIt = it; // The default case might not be the last case, keep it for last
2037 // If there's a default case, traverse it here
2038 if(defaultIt != sequence.end())
2040 emit(sw::Shader::OPCODE_ELSE);
2041 for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
2043 (*defaultIt)->traverse(this);
2044 if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
2051 for(int i = 0; i < nbCases; ++i)
2053 emit(sw::Shader::OPCODE_ENDIF);
2056 emit(sw::Shader::OPCODE_ENDSWITCH);
2061 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
2063 return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);
2066 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, int dstIndex, TIntermNode *src0, int index0, TIntermNode *src1, int index1,
2067 TIntermNode *src2, int index2, TIntermNode *src3, int index3, TIntermNode *src4, int index4)
2069 Instruction *instruction = new Instruction(op);
2073 destination(instruction->dst, dst, dstIndex);
2078 TIntermTyped* src = src0->getAsTyped();
2079 instruction->dst.partialPrecision = src && (src->getPrecision() <= EbpLow);
2082 source(instruction->src[0], src0, index0);
2083 source(instruction->src[1], src1, index1);
2084 source(instruction->src[2], src2, index2);
2085 source(instruction->src[3], src3, index3);
2086 source(instruction->src[4], src4, index4);
2088 shader->append(instruction);
2093 Instruction *OutputASM::emitCast(TIntermTyped *dst, TIntermTyped *src)
2095 return emitCast(dst, 0, src, 0);
2098 Instruction *OutputASM::emitCast(TIntermTyped *dst, int dstIndex, TIntermTyped *src, int srcIndex)
2100 switch(src->getBasicType())
2103 switch(dst->getBasicType())
2105 case EbtInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2106 case EbtUInt: return emit(sw::Shader::OPCODE_B2I, dst, dstIndex, src, srcIndex);
2107 case EbtFloat: return emit(sw::Shader::OPCODE_B2F, dst, dstIndex, src, srcIndex);
2112 switch(dst->getBasicType())
2114 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2115 case EbtFloat: return emit(sw::Shader::OPCODE_I2F, dst, dstIndex, src, srcIndex);
2120 switch(dst->getBasicType())
2122 case EbtBool: return emit(sw::Shader::OPCODE_I2B, dst, dstIndex, src, srcIndex);
2123 case EbtFloat: return emit(sw::Shader::OPCODE_U2F, dst, dstIndex, src, srcIndex);
2128 switch(dst->getBasicType())
2130 case EbtBool: return emit(sw::Shader::OPCODE_F2B, dst, dstIndex, src, srcIndex);
2131 case EbtInt: return emit(sw::Shader::OPCODE_F2I, dst, dstIndex, src, srcIndex);
2132 case EbtUInt: return emit(sw::Shader::OPCODE_F2U, dst, dstIndex, src, srcIndex);
2140 ASSERT((src->getBasicType() == dst->getBasicType()) ||
2141 ((src->getBasicType() == EbtInt) && (dst->getBasicType() == EbtUInt)) ||
2142 ((src->getBasicType() == EbtUInt) && (dst->getBasicType() == EbtInt)));
2144 return emit(sw::Shader::OPCODE_MOV, dst, dstIndex, src, srcIndex);
2147 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
2149 for(int index = 0; index < dst->elementRegisterCount(); index++)
2151 emit(op, dst, index, src0, index, src1, index, src2, index);
2155 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
2157 emitBinary(op, result, src0, src1);
2158 assignLvalue(lhs, result);
2161 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
2163 sw::Shader::Opcode opcode;
2164 switch(left->getAsTyped()->getBasicType())
2168 opcode = sw::Shader::OPCODE_ICMP;
2171 opcode = sw::Shader::OPCODE_UCMP;
2174 opcode = sw::Shader::OPCODE_CMP;
2178 Instruction *cmp = emit(opcode, dst, 0, left, index, right, index);
2179 cmp->control = cmpOp;
2182 int componentCount(const TType &type, int registers)
2189 if(type.isArray() && registers >= type.elementRegisterCount())
2191 int index = registers / type.elementRegisterCount();
2192 registers -= index * type.elementRegisterCount();
2193 return index * type.getElementSize() + componentCount(type, registers);
2196 if(type.isStruct() || type.isInterfaceBlock())
2198 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2201 for(const auto &field : fields)
2203 const TType &fieldType = *(field->type());
2205 if(fieldType.totalRegisterCount() <= registers)
2207 registers -= fieldType.totalRegisterCount();
2208 elements += fieldType.getObjectSize();
2210 else // Register within this field
2212 return elements + componentCount(fieldType, registers);
2216 else if(type.isMatrix())
2218 return registers * type.registerSize();
2225 int registerSize(const TType &type, int registers)
2231 return registerSize(*((*(type.getStruct()->fields().begin()))->type()), 0);
2233 else if(type.isInterfaceBlock())
2235 return registerSize(*((*(type.getInterfaceBlock()->fields().begin()))->type()), 0);
2238 return type.registerSize();
2241 if(type.isArray() && registers >= type.elementRegisterCount())
2243 int index = registers / type.elementRegisterCount();
2244 registers -= index * type.elementRegisterCount();
2245 return registerSize(type, registers);
2248 if(type.isStruct() || type.isInterfaceBlock())
2250 const TFieldList& fields = type.getStruct() ? type.getStruct()->fields() : type.getInterfaceBlock()->fields();
2253 for(const auto &field : fields)
2255 const TType &fieldType = *(field->type());
2257 if(fieldType.totalRegisterCount() <= registers)
2259 registers -= fieldType.totalRegisterCount();
2260 elements += fieldType.getObjectSize();
2262 else // Register within this field
2264 return registerSize(fieldType, registers);
2268 else if(type.isMatrix())
2270 return registerSize(type, 0);
2277 int OutputASM::getBlockId(TIntermTyped *arg)
2281 const TType &type = arg->getType();
2282 TInterfaceBlock* block = type.getInterfaceBlock();
2283 if(block && (type.getQualifier() == EvqUniform))
2285 // Make sure the uniform block is declared
2286 uniformRegister(arg);
2288 const char* blockName = block->name().c_str();
2290 // Fetch uniform block index from array of blocks
2291 for(ActiveUniformBlocks::const_iterator it = shaderObject->activeUniformBlocks.begin(); it != shaderObject->activeUniformBlocks.end(); ++it)
2293 if(blockName == it->name)
2306 OutputASM::ArgumentInfo OutputASM::getArgumentInfo(TIntermTyped *arg, int index)
2308 const TType &type = arg->getType();
2309 int blockId = getBlockId(arg);
2310 ArgumentInfo argumentInfo(BlockMemberInfo::getDefaultBlockInfo(), type, -1, -1);
2313 argumentInfo.bufferIndex = 0;
2314 for(int i = 0; i < blockId; ++i)
2316 int blockArraySize = shaderObject->activeUniformBlocks[i].arraySize;
2317 argumentInfo.bufferIndex += blockArraySize > 0 ? blockArraySize : 1;
2320 const BlockDefinitionIndexMap& blockDefinition = blockDefinitions[blockId];
2322 BlockDefinitionIndexMap::const_iterator itEnd = blockDefinition.end();
2323 BlockDefinitionIndexMap::const_iterator it = itEnd;
2325 argumentInfo.clampedIndex = index;
2326 if(type.isInterfaceBlock())
2328 // Offset index to the beginning of the selected instance
2329 int blockRegisters = type.elementRegisterCount();
2330 int bufferOffset = argumentInfo.clampedIndex / blockRegisters;
2331 argumentInfo.bufferIndex += bufferOffset;
2332 argumentInfo.clampedIndex -= bufferOffset * blockRegisters;
2335 int regIndex = registerIndex(arg);
2336 for(int i = regIndex + argumentInfo.clampedIndex; i >= regIndex; --i)
2338 it = blockDefinition.find(i);
2341 argumentInfo.clampedIndex -= (i - regIndex);
2345 ASSERT(it != itEnd);
2347 argumentInfo.typedMemberInfo = it->second;
2349 int registerCount = argumentInfo.typedMemberInfo.type.totalRegisterCount();
2350 argumentInfo.clampedIndex = (argumentInfo.clampedIndex >= registerCount) ? registerCount - 1 : argumentInfo.clampedIndex;
2354 argumentInfo.clampedIndex = (index >= arg->totalRegisterCount()) ? arg->totalRegisterCount() - 1 : index;
2357 return argumentInfo;
2360 void OutputASM::source(sw::Shader::SourceParameter ¶meter, TIntermNode *argument, int index)
2364 TIntermTyped *arg = argument->getAsTyped();
2365 Temporary unpackedUniform(this);
2367 const TType& srcType = arg->getType();
2368 TInterfaceBlock* srcBlock = srcType.getInterfaceBlock();
2369 if(srcBlock && (srcType.getQualifier() == EvqUniform))
2371 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2372 const TType &memberType = argumentInfo.typedMemberInfo.type;
2374 if(memberType.getBasicType() == EbtBool)
2376 ASSERT(argumentInfo.clampedIndex < (memberType.isArray() ? memberType.getArraySize() : 1)); // index < arraySize
2378 // Convert the packed bool, which is currently an int, to a true bool
2379 Instruction *instruction = new Instruction(sw::Shader::OPCODE_I2B);
2380 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2381 instruction->dst.index = registerIndex(&unpackedUniform);
2382 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2383 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2384 instruction->src[0].index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * argumentInfo.typedMemberInfo.arrayStride;
2386 shader->append(instruction);
2388 arg = &unpackedUniform;
2391 else if((memberType.getLayoutQualifier().matrixPacking == EmpRowMajor) && memberType.isMatrix())
2393 int numCols = memberType.getNominalSize();
2394 int numRows = memberType.getSecondarySize();
2396 ASSERT(argumentInfo.clampedIndex < (numCols * (memberType.isArray() ? memberType.getArraySize() : 1))); // index < cols * arraySize
2398 unsigned int dstIndex = registerIndex(&unpackedUniform);
2399 unsigned int srcSwizzle = (argumentInfo.clampedIndex % numCols) * 0x55;
2400 int arrayIndex = argumentInfo.clampedIndex / numCols;
2401 int matrixStartOffset = argumentInfo.typedMemberInfo.offset + arrayIndex * argumentInfo.typedMemberInfo.arrayStride;
2403 for(int j = 0; j < numRows; ++j)
2405 // Transpose the row major matrix
2406 Instruction *instruction = new Instruction(sw::Shader::OPCODE_MOV);
2407 instruction->dst.type = sw::Shader::PARAMETER_TEMP;
2408 instruction->dst.index = dstIndex;
2409 instruction->dst.mask = 1 << j;
2410 instruction->src[0].type = sw::Shader::PARAMETER_CONST;
2411 instruction->src[0].bufferIndex = argumentInfo.bufferIndex;
2412 instruction->src[0].index = matrixStartOffset + j * argumentInfo.typedMemberInfo.matrixStride;
2413 instruction->src[0].swizzle = srcSwizzle;
2415 shader->append(instruction);
2418 arg = &unpackedUniform;
2423 const ArgumentInfo argumentInfo = getArgumentInfo(arg, index);
2424 const TType &type = argumentInfo.typedMemberInfo.type;
2426 int size = registerSize(type, argumentInfo.clampedIndex);
2428 parameter.type = registerType(arg);
2429 parameter.bufferIndex = argumentInfo.bufferIndex;
2431 if(arg->getAsConstantUnion() && arg->getAsConstantUnion()->getUnionArrayPointer())
2433 int component = componentCount(type, argumentInfo.clampedIndex);
2434 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
2436 for(int i = 0; i < 4; i++)
2438 if(size == 1) // Replicate
2440 parameter.value[i] = constants[component + 0].getAsFloat();
2444 parameter.value[i] = constants[component + i].getAsFloat();
2448 parameter.value[i] = 0.0f;
2454 parameter.index = registerIndex(arg) + argumentInfo.clampedIndex;
2456 if(parameter.bufferIndex != -1)
2458 int stride = (argumentInfo.typedMemberInfo.matrixStride > 0) ? argumentInfo.typedMemberInfo.matrixStride : argumentInfo.typedMemberInfo.arrayStride;
2459 parameter.index = argumentInfo.typedMemberInfo.offset + argumentInfo.clampedIndex * stride;
2463 if(!IsSampler(arg->getBasicType()))
2465 parameter.swizzle = readSwizzle(arg, size);
2470 void OutputASM::destination(sw::Shader::DestinationParameter ¶meter, TIntermTyped *arg, int index)
2472 parameter.type = registerType(arg);
2473 parameter.index = registerIndex(arg) + index;
2474 parameter.mask = writeMask(arg, index);
2477 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
2479 for(int index = 0; index < dst->totalRegisterCount(); index++)
2481 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, index, src, offset + index);
2485 int swizzleElement(int swizzle, int index)
2487 return (swizzle >> (index * 2)) & 0x03;
2490 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
2492 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
2493 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
2494 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
2495 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
2498 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermTyped *src)
2500 if((src->isVector() && (!dst->isVector() || (src->getNominalSize() != dst->getNominalSize()))) ||
2501 (src->isMatrix() && (!dst->isMatrix() || (src->getNominalSize() != dst->getNominalSize()) || (src->getSecondarySize() != dst->getSecondarySize()))))
2503 return mContext.error(src->getLine(), "Result type should match the l-value type in compound assignment", src->isVector() ? "vector" : "matrix");
2506 TIntermBinary *binary = dst->getAsBinaryNode();
2508 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && dst->isScalar())
2510 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
2512 lvalue(insert->dst, dst);
2514 insert->src[0].type = insert->dst.type;
2515 insert->src[0].index = insert->dst.index;
2516 insert->src[0].rel = insert->dst.rel;
2517 source(insert->src[1], src);
2518 source(insert->src[2], binary->getRight());
2520 shader->append(insert);
2524 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2526 int swizzle = lvalue(mov1->dst, dst);
2528 source(mov1->src[0], src);
2529 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2531 shader->append(mov1);
2533 for(int offset = 1; offset < dst->totalRegisterCount(); offset++)
2535 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
2537 mov->dst = mov1->dst;
2538 mov->dst.index += offset;
2539 mov->dst.mask = writeMask(dst, offset);
2541 source(mov->src[0], src, offset);
2543 shader->append(mov);
2548 void OutputASM::evaluateRvalue(TIntermTyped *node)
2550 TIntermBinary *binary = node->getAsBinaryNode();
2552 if(binary && binary->getOp() == EOpIndexIndirect && binary->getLeft()->isVector() && node->isScalar())
2554 Instruction *insert = new Instruction(sw::Shader::OPCODE_EXTRACT);
2556 destination(insert->dst, node);
2558 Temporary address(this);
2560 TIntermTyped *root = nullptr;
2561 unsigned int offset = 0;
2562 int swizzle = lvalue(root, offset, insert->src[0].rel, mask, address, node);
2564 source(insert->src[0], root, offset);
2565 insert->src[0].swizzle = swizzleSwizzle(insert->src[0].swizzle, swizzle);
2567 source(insert->src[1], binary->getRight());
2569 shader->append(insert);
2573 Instruction *mov1 = new Instruction(sw::Shader::OPCODE_MOV);
2575 destination(mov1->dst, node, 0);
2577 Temporary address(this);
2579 TIntermTyped *root = nullptr;
2580 unsigned int offset = 0;
2581 int swizzle = lvalue(root, offset, mov1->src[0].rel, mask, address, node);
2583 source(mov1->src[0], root, offset);
2584 mov1->src[0].swizzle = swizzleSwizzle(mov1->src[0].swizzle, swizzle);
2586 shader->append(mov1);
2588 for(int i = 1; i < node->totalRegisterCount(); i++)
2590 Instruction *mov = emit(sw::Shader::OPCODE_MOV, node, i, root, offset + i);
2591 mov->src[0].rel = mov1->src[0].rel;
2596 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, TIntermTyped *node)
2598 Temporary address(this);
2599 TIntermTyped *root = nullptr;
2600 unsigned int offset = 0;
2601 unsigned char mask = 0xF;
2602 int swizzle = lvalue(root, offset, dst.rel, mask, address, node);
2604 dst.type = registerType(root);
2605 dst.index = registerIndex(root) + offset;
2611 int OutputASM::lvalue(TIntermTyped *&root, unsigned int &offset, sw::Shader::Relative &rel, unsigned char &mask, Temporary &address, TIntermTyped *node)
2613 TIntermTyped *result = node;
2614 TIntermBinary *binary = node->getAsBinaryNode();
2615 TIntermSymbol *symbol = node->getAsSymbolNode();
2619 TIntermTyped *left = binary->getLeft();
2620 TIntermTyped *right = binary->getRight();
2622 int leftSwizzle = lvalue(root, offset, rel, mask, address, left); // Resolve the l-value of the left side
2624 switch(binary->getOp())
2626 case EOpIndexDirect:
2628 int rightIndex = right->getAsConstantUnion()->getIConst(0);
2630 if(left->isRegister())
2632 int leftMask = mask;
2635 while((leftMask & mask) == 0)
2640 int element = swizzleElement(leftSwizzle, rightIndex);
2641 mask = 1 << element;
2645 else if(left->isArray() || left->isMatrix())
2647 offset += rightIndex * result->totalRegisterCount();
2650 else UNREACHABLE(0);
2653 case EOpIndexIndirect:
2655 right->traverse(this);
2657 if(left->isRegister())
2659 // Requires INSERT instruction (handled by calling function)
2661 else if(left->isArray() || left->isMatrix())
2663 int scale = result->totalRegisterCount();
2665 if(rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
2667 if(left->totalRegisterCount() > 1)
2669 sw::Shader::SourceParameter relativeRegister;
2670 source(relativeRegister, right);
2672 rel.index = relativeRegister.index;
2673 rel.type = relativeRegister.type;
2675 rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
2678 else if(rel.index != registerIndex(&address)) // Move the previous index register to the address register
2682 Constant oldScale((int)rel.scale);
2683 Instruction *mad = emit(sw::Shader::OPCODE_IMAD, &address, &address, &oldScale, right);
2684 mad->src[0].index = rel.index;
2685 mad->src[0].type = rel.type;
2689 Constant oldScale((int)rel.scale);
2690 Instruction *mul = emit(sw::Shader::OPCODE_IMUL, &address, &address, &oldScale);
2691 mul->src[0].index = rel.index;
2692 mul->src[0].type = rel.type;
2694 Constant newScale(scale);
2695 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2698 rel.type = sw::Shader::PARAMETER_TEMP;
2699 rel.index = registerIndex(&address);
2702 else // Just add the new index to the address register
2706 emit(sw::Shader::OPCODE_IADD, &address, &address, right);
2710 Constant newScale(scale);
2711 emit(sw::Shader::OPCODE_IMAD, &address, right, &newScale, &address);
2715 else UNREACHABLE(0);
2718 case EOpIndexDirectStruct:
2719 case EOpIndexDirectInterfaceBlock:
2721 const TFieldList& fields = (binary->getOp() == EOpIndexDirectStruct) ?
2722 left->getType().getStruct()->fields() :
2723 left->getType().getInterfaceBlock()->fields();
2724 int index = right->getAsConstantUnion()->getIConst(0);
2725 int fieldOffset = 0;
2727 for(int i = 0; i < index; i++)
2729 fieldOffset += fields[i]->type()->totalRegisterCount();
2732 offset += fieldOffset;
2733 mask = writeMask(result);
2738 case EOpVectorSwizzle:
2740 ASSERT(left->isRegister());
2742 int leftMask = mask;
2747 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
2749 for(unsigned int i = 0; i < sequence.size(); i++)
2751 int index = sequence[i]->getAsConstantUnion()->getIConst(0);
2753 int element = swizzleElement(leftSwizzle, index);
2754 rightMask = rightMask | (1 << element);
2755 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
2758 mask = leftMask & rightMask;
2764 UNREACHABLE(binary->getOp()); // Not an l-value operator
2772 mask = writeMask(symbol);
2778 node->traverse(this);
2782 mask = writeMask(node);
2790 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
2792 if(isSamplerRegister(operand))
2794 return sw::Shader::PARAMETER_SAMPLER;
2797 const TQualifier qualifier = operand->getQualifier();
2798 if((qualifier == EvqFragColor) || (qualifier == EvqFragData))
2800 if(((qualifier == EvqFragData) && (outputQualifier == EvqFragColor)) ||
2801 ((qualifier == EvqFragColor) && (outputQualifier == EvqFragData)))
2803 mContext.error(operand->getLine(), "static assignment to both gl_FragData and gl_FragColor", "");
2805 outputQualifier = qualifier;
2808 if(qualifier == EvqConstExpr && (!operand->getAsConstantUnion() || !operand->getAsConstantUnion()->getUnionArrayPointer()))
2810 // Constant arrays are in the constant register file.
2811 if(operand->isArray() && operand->getArraySize() > 1)
2813 return sw::Shader::PARAMETER_CONST;
2817 return sw::Shader::PARAMETER_TEMP;
2823 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
2824 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
2825 case EvqConstExpr: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
2826 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
2827 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
2828 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
2829 case EvqVertexIn: return sw::Shader::PARAMETER_INPUT;
2830 case EvqFragmentOut: return sw::Shader::PARAMETER_COLOROUT;
2831 case EvqVertexOut: return sw::Shader::PARAMETER_OUTPUT;
2832 case EvqFragmentIn: return sw::Shader::PARAMETER_INPUT;
2833 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
2834 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
2835 case EvqSmooth: return sw::Shader::PARAMETER_OUTPUT;
2836 case EvqFlat: return sw::Shader::PARAMETER_OUTPUT;
2837 case EvqCentroidOut: return sw::Shader::PARAMETER_OUTPUT;
2838 case EvqSmoothIn: return sw::Shader::PARAMETER_INPUT;
2839 case EvqFlatIn: return sw::Shader::PARAMETER_INPUT;
2840 case EvqCentroidIn: return sw::Shader::PARAMETER_INPUT;
2841 case EvqUniform: return sw::Shader::PARAMETER_CONST;
2842 case EvqIn: return sw::Shader::PARAMETER_TEMP;
2843 case EvqOut: return sw::Shader::PARAMETER_TEMP;
2844 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
2845 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
2846 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
2847 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
2848 case EvqInstanceID: return sw::Shader::PARAMETER_MISCTYPE;
2849 case EvqVertexID: return sw::Shader::PARAMETER_MISCTYPE;
2850 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
2851 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
2852 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
2853 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
2854 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
2855 case EvqFragDepth: return sw::Shader::PARAMETER_DEPTHOUT;
2856 default: UNREACHABLE(qualifier);
2859 return sw::Shader::PARAMETER_VOID;
2862 bool OutputASM::hasFlatQualifier(TIntermTyped *operand)
2864 const TQualifier qualifier = operand->getQualifier();
2865 return qualifier == EvqFlat || qualifier == EvqFlatOut || qualifier == EvqFlatIn;
2868 unsigned int OutputASM::registerIndex(TIntermTyped *operand)
2870 if(isSamplerRegister(operand))
2872 return samplerRegister(operand);
2875 switch(operand->getQualifier())
2877 case EvqTemporary: return temporaryRegister(operand);
2878 case EvqGlobal: return temporaryRegister(operand);
2879 case EvqConstExpr: return temporaryRegister(operand); // Unevaluated constant expression
2880 case EvqAttribute: return attributeRegister(operand);
2881 case EvqVaryingIn: return varyingRegister(operand);
2882 case EvqVaryingOut: return varyingRegister(operand);
2883 case EvqVertexIn: return attributeRegister(operand);
2884 case EvqFragmentOut: return fragmentOutputRegister(operand);
2885 case EvqVertexOut: return varyingRegister(operand);
2886 case EvqFragmentIn: return varyingRegister(operand);
2887 case EvqInvariantVaryingIn: return varyingRegister(operand);
2888 case EvqInvariantVaryingOut: return varyingRegister(operand);
2889 case EvqSmooth: return varyingRegister(operand);
2890 case EvqFlat: return varyingRegister(operand);
2891 case EvqCentroidOut: return varyingRegister(operand);
2892 case EvqSmoothIn: return varyingRegister(operand);
2893 case EvqFlatIn: return varyingRegister(operand);
2894 case EvqCentroidIn: return varyingRegister(operand);
2895 case EvqUniform: return uniformRegister(operand);
2896 case EvqIn: return temporaryRegister(operand);
2897 case EvqOut: return temporaryRegister(operand);
2898 case EvqInOut: return temporaryRegister(operand);
2899 case EvqConstReadOnly: return temporaryRegister(operand);
2900 case EvqPosition: return varyingRegister(operand);
2901 case EvqPointSize: return varyingRegister(operand);
2902 case EvqInstanceID: vertexShader->declareInstanceId(); return sw::Shader::InstanceIDIndex;
2903 case EvqVertexID: vertexShader->declareVertexId(); return sw::Shader::VertexIDIndex;
2904 case EvqFragCoord: pixelShader->declareVPos(); return sw::Shader::VPosIndex;
2905 case EvqFrontFacing: pixelShader->declareVFace(); return sw::Shader::VFaceIndex;
2906 case EvqPointCoord: return varyingRegister(operand);
2907 case EvqFragColor: return 0;
2908 case EvqFragData: return fragmentOutputRegister(operand);
2909 case EvqFragDepth: return 0;
2910 default: UNREACHABLE(operand->getQualifier());
2916 int OutputASM::writeMask(TIntermTyped *destination, int index)
2918 if(destination->getQualifier() == EvqPointSize)
2920 return 0x2; // Point size stored in the y component
2923 return 0xF >> (4 - registerSize(destination->getType(), index));
2926 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
2928 if(argument->getQualifier() == EvqPointSize)
2930 return 0x55; // Point size stored in the y component
2933 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
2935 return swizzleSize[size];
2938 // Conservatively checks whether an expression is fast to compute and has no side effects
2939 bool OutputASM::trivial(TIntermTyped *expression, int budget)
2941 if(!expression->isRegister())
2946 return cost(expression, budget) >= 0;
2949 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
2950 int OutputASM::cost(TIntermNode *expression, int budget)
2957 if(expression->getAsSymbolNode())
2961 else if(expression->getAsConstantUnion())
2965 else if(expression->getAsBinaryNode())
2967 TIntermBinary *binary = expression->getAsBinaryNode();
2969 switch(binary->getOp())
2971 case EOpVectorSwizzle:
2972 case EOpIndexDirect:
2973 case EOpIndexDirectStruct:
2974 case EOpIndexDirectInterfaceBlock:
2975 return cost(binary->getLeft(), budget - 0);
2979 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
2984 else if(expression->getAsUnaryNode())
2986 TIntermUnary *unary = expression->getAsUnaryNode();
2988 switch(unary->getOp())
2992 return cost(unary->getOperand(), budget - 1);
2997 else if(expression->getAsSelectionNode())
2999 TIntermSelection *selection = expression->getAsSelectionNode();
3001 if(selection->usesTernaryOperator())
3003 TIntermTyped *condition = selection->getCondition();
3004 TIntermNode *trueBlock = selection->getTrueBlock();
3005 TIntermNode *falseBlock = selection->getFalseBlock();
3006 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
3008 if(constantCondition)
3010 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
3014 return cost(trueBlock, budget - 0);
3018 return cost(falseBlock, budget - 0);
3023 return cost(trueBlock, cost(falseBlock, budget - 2));
3031 const Function *OutputASM::findFunction(const TString &name)
3033 for(unsigned int f = 0; f < functionArray.size(); f++)
3035 if(functionArray[f].name == name)
3037 return &functionArray[f];
3044 int OutputASM::temporaryRegister(TIntermTyped *temporary)
3046 return allocate(temporaries, temporary);
3049 void OutputASM::setPixelShaderInputs(const TType& type, int var, bool flat)
3053 const TFieldList &fields = type.getStruct()->fields();
3055 for(const auto &field : fields)
3057 const TType& fieldType = *(field->type());
3058 setPixelShaderInputs(fieldType, fieldVar, flat);
3059 fieldVar += fieldType.totalRegisterCount();
3064 for(int i = 0; i < type.totalRegisterCount(); i++)
3066 pixelShader->setInput(var + i, type.registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i, flat));
3071 int OutputASM::varyingRegister(TIntermTyped *varying)
3073 int var = lookup(varyings, varying);
3077 var = allocate(varyings, varying);
3078 int registerCount = varying->totalRegisterCount();
3082 if((var + registerCount) > sw::MAX_FRAGMENT_INPUTS)
3084 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "fragment shader");
3088 if(varying->getQualifier() == EvqPointCoord)
3090 ASSERT(varying->isRegister());
3091 pixelShader->setInput(var, varying->registerSize(), sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var));
3095 setPixelShaderInputs(varying->getType(), var, hasFlatQualifier(varying));
3098 else if(vertexShader)
3100 if((var + registerCount) > sw::MAX_VERTEX_OUTPUTS)
3102 mContext.error(varying->getLine(), "Varyings packing failed: Too many varyings", "vertex shader");
3106 if(varying->getQualifier() == EvqPosition)
3108 ASSERT(varying->isRegister());
3109 vertexShader->setPositionRegister(var);
3111 else if(varying->getQualifier() == EvqPointSize)
3113 ASSERT(varying->isRegister());
3114 vertexShader->setPointSizeRegister(var);
3118 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
3121 else UNREACHABLE(0);
3123 declareVarying(varying, var);
3129 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
3131 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
3133 TIntermSymbol *symbol = varying->getAsSymbolNode();
3134 declareVarying(varying->getType(), symbol->getSymbol(), reg);
3138 void OutputASM::declareVarying(const TType &type, const TString &varyingName, int registerIndex)
3140 const char *name = varyingName.c_str();
3141 VaryingList &activeVaryings = shaderObject->varyings;
3143 TStructure* structure = type.getStruct();
3146 int fieldRegisterIndex = registerIndex;
3148 const TFieldList &fields = type.getStruct()->fields();
3149 for(const auto &field : fields)
3151 const TType& fieldType = *(field->type());
3152 declareVarying(fieldType, varyingName + "." + field->name(), fieldRegisterIndex);
3153 if(fieldRegisterIndex >= 0)
3155 fieldRegisterIndex += fieldType.totalRegisterCount();
3161 // Check if this varying has been declared before without having a register assigned
3162 for(VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
3166 if(registerIndex >= 0)
3168 ASSERT(v->registerIndex < 0 || v->registerIndex == registerIndex);
3169 v->registerIndex = registerIndex;
3176 activeVaryings.push_back(glsl::Varying(type, name, registerIndex, 0));
3180 void OutputASM::declareFragmentOutput(TIntermTyped *fragmentOutput)
3182 int requestedLocation = fragmentOutput->getType().getLayoutQualifier().location;
3183 if((requestedLocation >= 0) && (requestedLocation < sw::RENDERTARGETS))
3185 if(fragmentOutputs.size() <= requestedLocation)
3187 while(fragmentOutputs.size() < requestedLocation)
3189 fragmentOutputs.push_back(nullptr);
3191 fragmentOutputs.push_back(fragmentOutput);
3193 else if(!fragmentOutputs[requestedLocation])
3195 fragmentOutputs[requestedLocation] = fragmentOutput;
3199 mContext.error(fragmentOutput->getLine(), "Fragment output location aliasing", "fragment shader");
3202 else if(requestedLocation >= sw::RENDERTARGETS)
3204 mContext.error(fragmentOutput->getLine(), "Fragment output location larger or equal to MAX_DRAW_BUFFERS", "fragment shader");
3208 int OutputASM::uniformRegister(TIntermTyped *uniform)
3210 const TType &type = uniform->getType();
3211 ASSERT(!IsSampler(type.getBasicType()));
3212 TInterfaceBlock *block = type.getAsInterfaceBlock();
3213 TIntermSymbol *symbol = uniform->getAsSymbolNode();
3214 ASSERT(symbol || block);
3218 TInterfaceBlock* parentBlock = type.getInterfaceBlock();
3219 bool isBlockMember = (!block && parentBlock);
3220 int index = isBlockMember ? lookup(uniforms, parentBlock) : lookup(uniforms, uniform);
3222 if(index == -1 || isBlockMember)
3226 index = allocate(uniforms, uniform);
3229 // Verify if the current uniform is a member of an already declared block
3230 const TString &name = symbol ? symbol->getSymbol() : block->name();
3231 int blockMemberIndex = blockMemberLookup(type, name, index);
3232 if(blockMemberIndex == -1)
3234 declareUniform(type, name, index, false);
3238 index = blockMemberIndex;
3248 int OutputASM::attributeRegister(TIntermTyped *attribute)
3250 ASSERT(!attribute->isArray());
3252 int index = lookup(attributes, attribute);
3256 TIntermSymbol *symbol = attribute->getAsSymbolNode();
3261 index = allocate(attributes, attribute);
3262 const TType &type = attribute->getType();
3263 int registerCount = attribute->totalRegisterCount();
3264 sw::VertexShader::AttribType attribType = sw::VertexShader::ATTRIBTYPE_FLOAT;
3265 switch(type.getBasicType())
3268 attribType = sw::VertexShader::ATTRIBTYPE_INT;
3271 attribType = sw::VertexShader::ATTRIBTYPE_UINT;
3278 if(vertexShader && (index + registerCount) <= sw::MAX_VERTEX_INPUTS)
3280 for(int i = 0; i < registerCount; i++)
3282 vertexShader->setInput(index + i, sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i, false), attribType);
3286 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
3288 const char *name = symbol->getSymbol().c_str();
3289 activeAttributes.push_back(Attribute(glVariableType(type), name, type.getArraySize(), type.getLayoutQualifier().location, index));
3296 int OutputASM::fragmentOutputRegister(TIntermTyped *fragmentOutput)
3298 return allocate(fragmentOutputs, fragmentOutput);
3301 int OutputASM::samplerRegister(TIntermTyped *sampler)
3303 const TType &type = sampler->getType();
3304 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3306 TIntermSymbol *symbol = sampler->getAsSymbolNode();
3307 TIntermBinary *binary = sampler->getAsBinaryNode();
3311 switch(type.getQualifier())
3314 return samplerRegister(symbol);
3316 case EvqConstReadOnly:
3317 // Function arguments are not (uniform) sampler registers
3320 UNREACHABLE(type.getQualifier());
3325 TIntermTyped *left = binary->getLeft();
3326 TIntermTyped *right = binary->getRight();
3327 const TType &leftType = left->getType();
3328 int index = right->getAsConstantUnion() ? right->getAsConstantUnion()->getIConst(0) : 0;
3331 switch(binary->getOp())
3333 case EOpIndexDirect:
3334 ASSERT(left->isArray());
3335 offset = index * leftType.samplerRegisterCount();
3337 case EOpIndexDirectStruct:
3338 ASSERT(leftType.isStruct());
3340 const TFieldList &fields = leftType.getStruct()->fields();
3342 for(int i = 0; i < index; i++)
3344 offset += fields[i]->type()->totalSamplerRegisterCount();
3348 case EOpIndexIndirect: // Indirect indexing produces a temporary, not a sampler register
3350 case EOpIndexDirectInterfaceBlock: // Interface blocks can't contain samplers
3352 UNREACHABLE(binary->getOp());
3356 int base = samplerRegister(left);
3363 return base + offset;
3367 return -1; // Not a (uniform) sampler register
3370 int OutputASM::samplerRegister(TIntermSymbol *sampler)
3372 const TType &type = sampler->getType();
3373 ASSERT(IsSampler(type.getBasicType()) || type.isStruct()); // Structures can contain samplers
3375 int index = lookup(samplers, sampler);
3379 index = allocate(samplers, sampler, true);
3381 if(sampler->getQualifier() == EvqUniform)
3383 const char *name = sampler->getSymbol().c_str();
3384 declareUniform(type, name, index, true);
3391 bool OutputASM::isSamplerRegister(TIntermTyped *operand)
3393 return operand && IsSampler(operand->getBasicType()) && samplerRegister(operand) >= 0;
3396 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
3398 for(unsigned int i = 0; i < list.size(); i++)
3400 if(list[i] == variable)
3402 return i; // Pointer match
3406 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
3407 TInterfaceBlock *varBlock = variable->getType().getAsInterfaceBlock();
3411 for(unsigned int i = 0; i < list.size(); i++)
3415 TInterfaceBlock *listBlock = list[i]->getType().getAsInterfaceBlock();
3419 if(listBlock->name() == varBlock->name())
3421 ASSERT(listBlock->arraySize() == varBlock->arraySize());
3422 ASSERT(listBlock->fields() == varBlock->fields());
3423 ASSERT(listBlock->blockStorage() == varBlock->blockStorage());
3424 ASSERT(listBlock->matrixPacking() == varBlock->matrixPacking());
3434 for(unsigned int i = 0; i < list.size(); i++)
3438 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
3442 if(listSymbol->getId() == varSymbol->getId())
3444 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
3445 ASSERT(listSymbol->getType() == varSymbol->getType());
3446 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
3458 int OutputASM::lookup(VariableArray &list, TInterfaceBlock *block)
3460 for(unsigned int i = 0; i < list.size(); i++)
3462 if(list[i] && (list[i]->getType().getInterfaceBlock() == block))
3464 return i; // Pointer match
3470 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable, bool samplersOnly)
3472 int index = lookup(list, variable);
3476 unsigned int registerCount = variable->blockRegisterCount(samplersOnly);
3478 for(unsigned int i = 0; i < list.size(); i++)
3483 for( ; j < registerCount && (i + j) < list.size(); j++)
3485 if(list[i + j] != 0)
3491 if(j == registerCount) // Found free slots
3493 for(unsigned int j = 0; j < registerCount; j++)
3495 list[i + j] = variable;
3503 index = list.size();
3505 for(unsigned int i = 0; i < registerCount; i++)
3507 list.push_back(variable);
3514 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
3516 int index = lookup(list, variable);
3524 int OutputASM::blockMemberLookup(const TType &type, const TString &name, int registerIndex)
3526 const TInterfaceBlock *block = type.getInterfaceBlock();
3530 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3531 const TFieldList& fields = block->fields();
3532 const TString &blockName = block->name();
3533 int fieldRegisterIndex = registerIndex;
3535 if(!type.isInterfaceBlock())
3537 // This is a uniform that's part of a block, let's see if the block is already defined
3538 for(size_t i = 0; i < activeUniformBlocks.size(); ++i)
3540 if(activeUniformBlocks[i].name == blockName.c_str())
3542 // The block is already defined, find the register for the current uniform and return it
3543 for(size_t j = 0; j < fields.size(); j++)
3545 const TString &fieldName = fields[j]->name();
3546 if(fieldName == name)
3548 return fieldRegisterIndex;
3551 fieldRegisterIndex += fields[j]->type()->totalRegisterCount();
3555 return fieldRegisterIndex;
3564 void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, bool samplersOnly, int blockId, BlockLayoutEncoder* encoder)
3566 const TStructure *structure = type.getStruct();
3567 const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;
3569 if(!structure && !block)
3571 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
3572 const BlockMemberInfo blockInfo = encoder ? encoder->encodeType(type) : BlockMemberInfo::getDefaultBlockInfo();
3575 blockDefinitions[blockId][registerIndex] = TypedMemberInfo(blockInfo, type);
3576 shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());
3578 int fieldRegisterIndex = encoder ? shaderObject->activeUniformBlocks[blockId].registerIndex + BlockLayoutEncoder::getBlockRegister(blockInfo) : registerIndex;
3579 bool isSampler = IsSampler(type.getBasicType());
3580 if(isSampler && samplersOnly)
3582 for(int i = 0; i < type.totalRegisterCount(); i++)
3584 shader->declareSampler(fieldRegisterIndex + i);
3587 if(isSampler == samplersOnly)
3589 activeUniforms.push_back(Uniform(type, name.c_str(), fieldRegisterIndex, blockId, blockInfo));
3594 ActiveUniformBlocks &activeUniformBlocks = shaderObject->activeUniformBlocks;
3595 const TFieldList& fields = block->fields();
3596 const TString &blockName = block->name();
3597 int fieldRegisterIndex = registerIndex;
3598 bool isUniformBlockMember = !type.isInterfaceBlock() && (blockId == -1);
3600 blockId = activeUniformBlocks.size();
3601 bool isRowMajor = block->matrixPacking() == EmpRowMajor;
3602 activeUniformBlocks.push_back(UniformBlock(blockName.c_str(), 0, block->arraySize(),
3603 block->blockStorage(), isRowMajor, registerIndex, blockId));
3604 blockDefinitions.push_back(BlockDefinitionIndexMap());
3606 Std140BlockEncoder currentBlockEncoder;
3607 currentBlockEncoder.enterAggregateType();
3608 for(const auto &field : fields)
3610 const TType &fieldType = *(field->type());
3611 const TString &fieldName = field->name();
3612 if(isUniformBlockMember && (fieldName == name))
3614 registerIndex = fieldRegisterIndex;
3617 const TString uniformName = block->hasInstanceName() ? blockName + "." + fieldName : fieldName;
3619 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, ¤tBlockEncoder);
3620 fieldRegisterIndex += fieldType.totalRegisterCount();
3622 currentBlockEncoder.exitAggregateType();
3623 activeUniformBlocks[blockId].dataSize = currentBlockEncoder.getBlockSize();
3627 // Store struct for program link time validation
3628 shaderObject->activeUniformStructs.push_back(Uniform(type, name.c_str(), registerIndex, -1, BlockMemberInfo::getDefaultBlockInfo()));
3630 int fieldRegisterIndex = registerIndex;
3632 const TFieldList& fields = structure->fields();
3633 if(type.isArray() && (structure || type.isInterfaceBlock()))
3635 for(int i = 0; i < type.getArraySize(); i++)
3639 encoder->enterAggregateType();
3641 for(const auto &field : fields)
3643 const TType &fieldType = *(field->type());
3644 const TString &fieldName = field->name();
3645 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
3647 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3648 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
3652 encoder->exitAggregateType();
3660 encoder->enterAggregateType();
3662 for(const auto &field : fields)
3664 const TType &fieldType = *(field->type());
3665 const TString &fieldName = field->name();
3666 const TString uniformName = name + "." + fieldName;
3668 declareUniform(fieldType, uniformName, fieldRegisterIndex, samplersOnly, blockId, encoder);
3669 fieldRegisterIndex += samplersOnly ? fieldType.totalSamplerRegisterCount() : fieldType.totalRegisterCount();
3673 encoder->exitAggregateType();
3679 int OutputASM::dim(TIntermNode *v)
3681 TIntermTyped *vector = v->getAsTyped();
3682 ASSERT(vector && vector->isRegister());
3683 return vector->getNominalSize();
3686 int OutputASM::dim2(TIntermNode *m)
3688 TIntermTyped *matrix = m->getAsTyped();
3689 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
3690 return matrix->getSecondarySize();
3693 // Returns ~0u if no loop count could be determined
3694 unsigned int OutputASM::loopCount(TIntermLoop *node)
3696 // Parse loops of the form:
3697 // for(int index = initial; index [comparator] limit; index += increment)
3698 TIntermSymbol *index = 0;
3699 TOperator comparator = EOpNull;
3704 // Parse index name and intial value
3707 TIntermAggregate *init = node->getInit()->getAsAggregate();
3711 TIntermSequence &sequence = init->getSequence();
3712 TIntermTyped *variable = sequence[0]->getAsTyped();
3714 if(variable && variable->getQualifier() == EvqTemporary && variable->getBasicType() == EbtInt)
3716 TIntermBinary *assign = variable->getAsBinaryNode();
3718 if(assign && assign->getOp() == EOpInitialize)
3720 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3721 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3723 if(symbol && constant)
3725 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3728 initial = constant->getUnionArrayPointer()[0].getIConst();
3736 // Parse comparator and limit value
3737 if(index && node->getCondition())
3739 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
3740 TIntermSymbol *left = test ? test->getLeft()->getAsSymbolNode() : nullptr;
3742 if(left && (left->getId() == index->getId()))
3744 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3748 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3750 comparator = test->getOp();
3751 limit = constant->getUnionArrayPointer()[0].getIConst();
3758 if(index && comparator != EOpNull && node->getExpression())
3760 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3761 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
3765 TOperator op = binaryTerminal->getOp();
3766 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3770 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
3772 int value = constant->getUnionArrayPointer()[0].getIConst();
3776 case EOpAddAssign: increment = value; break;
3777 case EOpSubAssign: increment = -value; break;
3778 default: UNIMPLEMENTED();
3783 else if(unaryTerminal)
3785 TOperator op = unaryTerminal->getOp();
3789 case EOpPostIncrement: increment = 1; break;
3790 case EOpPostDecrement: increment = -1; break;
3791 case EOpPreIncrement: increment = 1; break;
3792 case EOpPreDecrement: increment = -1; break;
3793 default: UNIMPLEMENTED();
3798 if(index && comparator != EOpNull && increment != 0)
3800 if(comparator == EOpLessThanEqual)
3802 comparator = EOpLessThan;
3805 else if(comparator == EOpGreaterThanEqual)
3807 comparator = EOpLessThan;
3809 std::swap(initial, limit);
3810 increment = -increment;
3812 else if(comparator == EOpGreaterThan)
3814 comparator = EOpLessThan;
3815 std::swap(initial, limit);
3816 increment = -increment;
3819 if(comparator == EOpLessThan)
3821 if(!(initial < limit)) // Never loops
3826 int iterations = (limit - initial + abs(increment) - 1) / increment; // Ceiling division
3835 else UNIMPLEMENTED(); // Falls through
3841 bool LoopUnrollable::traverse(TIntermNode *node)
3844 loopUnrollable = true;
3846 node->traverse(this);
3848 return loopUnrollable;
3851 bool LoopUnrollable::visitLoop(Visit visit, TIntermLoop *loop)
3853 if(visit == PreVisit)
3857 else if(visit == PostVisit)
3865 bool LoopUnrollable::visitBranch(Visit visit, TIntermBranch *node)
3877 switch(node->getFlowOp())
3884 loopUnrollable = false;
3886 default: UNREACHABLE(node->getFlowOp());
3889 return loopUnrollable;
3892 bool LoopUnrollable::visitAggregate(Visit visit, TIntermAggregate *node)
3894 return loopUnrollable;