2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 #include "compiler/OutputHLSL.h"
9 #include "compiler/debug.h"
10 #include "compiler/InfoSink.h"
11 #include "compiler/UnfoldSelect.h"
12 #include "compiler/SearchSymbol.h"
19 // Integer to TString conversion
23 sprintf(buffer, "%d", i);
27 OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
29 mUnfoldSelect = new UnfoldSelect(context, this);
30 mInsideFunction = false;
32 mUsesTexture2D = false;
33 mUsesTexture2D_bias = false;
34 mUsesTexture2DProj = false;
35 mUsesTexture2DProj_bias = false;
36 mUsesTextureCube = false;
37 mUsesTextureCube_bias = false;
38 mUsesDepthRange = false;
39 mUsesFragCoord = false;
40 mUsesPointCoord = false;
41 mUsesFrontFacing = false;
42 mUsesPointSize = false;
48 mUsesFaceforward1 = false;
49 mUsesFaceforward2 = false;
50 mUsesFaceforward3 = false;
51 mUsesFaceforward4 = false;
52 mUsesEqualMat2 = false;
53 mUsesEqualMat3 = false;
54 mUsesEqualMat4 = false;
55 mUsesEqualVec2 = false;
56 mUsesEqualVec3 = false;
57 mUsesEqualVec4 = false;
58 mUsesEqualIVec2 = false;
59 mUsesEqualIVec3 = false;
60 mUsesEqualIVec4 = false;
61 mUsesEqualBVec2 = false;
62 mUsesEqualBVec3 = false;
63 mUsesEqualBVec4 = false;
71 OutputHLSL::~OutputHLSL()
76 void OutputHLSL::output()
78 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
81 mContext.infoSink.obj << mHeader.c_str();
82 mContext.infoSink.obj << mBody.c_str();
85 TInfoSinkBase &OutputHLSL::getBodyStream()
90 int OutputHLSL::vectorSize(const TType &type) const
92 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
93 int arraySize = type.isArray() ? type.getArraySize() : 1;
95 return elementSize * arraySize;
98 void OutputHLSL::header()
100 ShShaderType shaderType = mContext.shaderType;
101 TInfoSinkBase &out = mHeader;
103 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
105 out << *structDeclaration;
108 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
113 if (shaderType == SH_FRAGMENT_SHADER)
118 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
119 int semanticIndex = 0;
121 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
123 const TSymbol *symbol = (*namedSymbol).second;
124 const TString &name = symbol->getName();
126 if (symbol->isVariable())
128 const TVariable *variable = static_cast<const TVariable*>(symbol);
129 const TType &type = variable->getType();
130 TQualifier qualifier = type.getQualifier();
132 if (qualifier == EvqUniform)
134 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
136 uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
139 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
141 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
143 // Program linking depends on this exact format
144 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
146 semanticIndex += type.isArray() ? type.getArraySize() : 1;
149 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
151 // Globals are declared and intialized as an aggregate node
153 else if (qualifier == EvqConst)
155 // Constants are repeated as literals where used
161 out << "// Varyings\n";
164 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
168 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
173 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
176 if (mUsesFrontFacing)
178 out << "static bool gl_FrontFacing = false;\n";
185 out << "uniform float4 dx_Viewport;\n"
186 "uniform float2 dx_Depth;\n";
189 if (mUsesFrontFacing)
191 out << "uniform bool dx_PointsOrLines;\n"
192 "uniform bool dx_FrontCCW;\n";
201 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
203 " return tex2D(s, t);\n"
208 if (mUsesTexture2D_bias)
210 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
212 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
217 if (mUsesTexture2DProj)
219 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
221 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
224 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
226 " return tex2Dproj(s, t);\n"
231 if (mUsesTexture2DProj_bias)
233 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
235 " return tex2Dbias(s, float4(t.x / t.z, t.y / t.z, 0, bias));\n"
238 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
240 " return tex2Dbias(s, float4(t.x / t.w, t.y / t.w, 0, bias));\n"
245 if (mUsesTextureCube)
247 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
249 " return texCUBE(s, t);\n"
254 if (mUsesTextureCube_bias)
256 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
258 " return texCUBEbias(s, float4(t.x, t.y, t.z, bias));\n"
263 else // Vertex shader
269 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
271 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
273 const TSymbol *symbol = (*namedSymbol).second;
274 const TString &name = symbol->getName();
276 if (symbol->isVariable())
278 const TVariable *variable = static_cast<const TVariable*>(symbol);
279 const TType &type = variable->getType();
280 TQualifier qualifier = type.getQualifier();
282 if (qualifier == EvqUniform)
284 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
286 uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
289 else if (qualifier == EvqAttribute)
291 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
293 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
296 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
298 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
300 // Program linking depends on this exact format
301 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
304 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
306 // Globals are declared and intialized as an aggregate node
308 else if (qualifier == EvqConst)
310 // Constants are repeated as literals where used
316 out << "// Attributes\n";
319 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
323 out << "static float gl_PointSize = float(1);\n";
330 "uniform float2 dx_HalfPixelSize;\n"
338 out << "#define GL_USES_FRAG_COORD\n";
343 out << "#define GL_USES_POINT_COORD\n";
346 if (mUsesFrontFacing)
348 out << "#define GL_USES_FRONT_FACING\n";
353 out << "#define GL_USES_POINT_SIZE\n";
358 out << "struct gl_DepthRangeParameters\n"
365 "uniform float3 dx_DepthRange;"
366 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
372 out << "bool xor(bool p, bool q)\n"
374 " return (p || q) && !(p && q);\n"
381 out << "float mod(float x, float y)\n"
383 " return x - y * floor(x / y);\n"
390 out << "float2 mod(float2 x, float y)\n"
392 " return x - y * floor(x / y);\n"
399 out << "float3 mod(float3 x, float y)\n"
401 " return x - y * floor(x / y);\n"
408 out << "float4 mod(float4 x, float y)\n"
410 " return x - y * floor(x / y);\n"
415 if (mUsesFaceforward1)
417 out << "float faceforward(float N, float I, float Nref)\n"
419 " if(dot(Nref, I) >= 0)\n"
431 if (mUsesFaceforward2)
433 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
435 " if(dot(Nref, I) >= 0)\n"
447 if (mUsesFaceforward3)
449 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
451 " if(dot(Nref, I) >= 0)\n"
463 if (mUsesFaceforward4)
465 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
467 " if(dot(Nref, I) >= 0)\n"
481 out << "bool equal(float2x2 m, float2x2 n)\n"
483 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
484 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
490 out << "bool equal(float3x3 m, float3x3 n)\n"
492 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
493 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
494 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
500 out << "bool equal(float4x4 m, float4x4 n)\n"
502 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
503 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
504 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2] && m[2][3] == n[2][3] &&\n"
505 " m[3][0] == n[3][0] && m[3][1] == n[3][1] && m[3][2] == n[3][2] && m[3][3] == n[3][3];\n"
511 out << "bool equal(float2 v, float2 u)\n"
513 " return v.x == u.x && v.y == u.y;\n"
519 out << "bool equal(float3 v, float3 u)\n"
521 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
527 out << "bool equal(float4 v, float4 u)\n"
529 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
535 out << "bool equal(int2 v, int2 u)\n"
537 " return v.x == u.x && v.y == u.y;\n"
543 out << "bool equal(int3 v, int3 u)\n"
545 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
551 out << "bool equal(int4 v, int4 u)\n"
553 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
559 out << "bool equal(bool2 v, bool2 u)\n"
561 " return v.x == u.x && v.y == u.y;\n"
567 out << "bool equal(bool3 v, bool3 u)\n"
569 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
575 out << "bool equal(bool4 v, bool4 u)\n"
577 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
583 out << "float atanyx(float y, float x)\n"
585 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
586 " return atan2(y, x);\n"
591 void OutputHLSL::visitSymbol(TIntermSymbol *node)
593 TInfoSinkBase &out = mBody;
595 TString name = node->getSymbol();
597 if (name == "gl_FragColor")
599 out << "gl_Color[0]";
601 else if (name == "gl_FragData")
605 else if (name == "gl_DepthRange")
607 mUsesDepthRange = true;
610 else if (name == "gl_FragCoord")
612 mUsesFragCoord = true;
615 else if (name == "gl_PointCoord")
617 mUsesPointCoord = true;
620 else if (name == "gl_FrontFacing")
622 mUsesFrontFacing = true;
625 else if (name == "gl_PointSize")
627 mUsesPointSize = true;
632 TQualifier qualifier = node->getQualifier();
634 if (qualifier == EvqUniform)
636 mReferencedUniforms.insert(name.c_str());
638 else if (qualifier == EvqAttribute)
640 mReferencedAttributes.insert(name.c_str());
642 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
644 mReferencedVaryings.insert(name.c_str());
647 out << decorate(name);
651 bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
653 TInfoSinkBase &out = mBody;
655 switch (node->getOp())
657 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
659 if (visit == PreVisit)
661 // GLSL allows to write things like "float x = x;" where a new variable x is defined
662 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
663 // new variable is created before the assignment is evaluated), so we need to convert
664 // this to "float t = x, x = t;".
666 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
667 TIntermTyped *expression = node->getRight();
669 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
670 expression->traverse(&searchSymbol);
671 bool sameSymbol = searchSymbol.foundMatch();
675 // Type already printed
676 out << "t" + str(mUniqueIndex) + " = ";
677 expression->traverse(this);
679 symbolNode->traverse(this);
680 out << " = t" + str(mUniqueIndex);
686 else if (visit == InVisit)
691 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
692 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
693 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
694 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
695 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
696 case EOpVectorTimesMatrixAssign:
697 if (visit == PreVisit)
701 else if (visit == InVisit)
704 node->getLeft()->traverse(this);
705 out << ", transpose(";
712 case EOpMatrixTimesMatrixAssign:
713 if (visit == PreVisit)
717 else if (visit == InVisit)
720 node->getLeft()->traverse(this);
728 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
729 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
730 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
731 case EOpIndexDirectStruct:
732 if (visit == InVisit)
734 out << "." + node->getType().getFieldName();
739 case EOpVectorSwizzle:
740 if (visit == InVisit)
744 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
748 TIntermSequence &sequence = swizzle->getSequence();
750 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
752 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
756 int i = element->getUnionArrayPointer()[0].getIConst();
760 case 0: out << "x"; break;
761 case 1: out << "y"; break;
762 case 2: out << "z"; break;
763 case 3: out << "w"; break;
764 default: UNREACHABLE();
772 return false; // Fully processed
775 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
776 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
777 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
778 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
781 if (node->getLeft()->isScalar())
783 if (node->getOp() == EOpEqual)
785 outputTriplet(visit, "(", " == ", ")");
789 outputTriplet(visit, "(", " != ", ")");
792 else if (node->getLeft()->getBasicType() == EbtStruct)
794 if (node->getOp() == EOpEqual)
803 const TTypeList *fields = node->getLeft()->getType().getStruct();
805 for (size_t i = 0; i < fields->size(); i++)
807 const TType *fieldType = (*fields)[i].type;
809 node->getLeft()->traverse(this);
810 out << "." + fieldType->getFieldName() + " == ";
811 node->getRight()->traverse(this);
812 out << "." + fieldType->getFieldName();
814 if (i < fields->size() - 1)
826 if (node->getLeft()->isMatrix())
828 switch (node->getLeft()->getNominalSize())
830 case 2: mUsesEqualMat2 = true; break;
831 case 3: mUsesEqualMat3 = true; break;
832 case 4: mUsesEqualMat4 = true; break;
833 default: UNREACHABLE();
836 else if (node->getLeft()->isVector())
838 switch (node->getLeft()->getBasicType())
841 switch (node->getLeft()->getNominalSize())
843 case 2: mUsesEqualVec2 = true; break;
844 case 3: mUsesEqualVec3 = true; break;
845 case 4: mUsesEqualVec4 = true; break;
846 default: UNREACHABLE();
850 switch (node->getLeft()->getNominalSize())
852 case 2: mUsesEqualIVec2 = true; break;
853 case 3: mUsesEqualIVec3 = true; break;
854 case 4: mUsesEqualIVec4 = true; break;
855 default: UNREACHABLE();
859 switch (node->getLeft()->getNominalSize())
861 case 2: mUsesEqualBVec2 = true; break;
862 case 3: mUsesEqualBVec3 = true; break;
863 case 4: mUsesEqualBVec4 = true; break;
864 default: UNREACHABLE();
867 default: UNREACHABLE();
872 if (node->getOp() == EOpEqual)
874 outputTriplet(visit, "equal(", ", ", ")");
878 outputTriplet(visit, "!equal(", ", ", ")");
882 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
883 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
884 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
885 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
886 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
887 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
888 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
889 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
890 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
891 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
894 outputTriplet(visit, "xor(", ", ", ")");
896 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
897 default: UNREACHABLE();
903 bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
905 TInfoSinkBase &out = mBody;
907 switch (node->getOp())
909 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
910 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
911 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
912 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
913 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
914 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
915 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
916 case EOpConvIntToBool:
917 case EOpConvFloatToBool:
918 switch (node->getOperand()->getType().getNominalSize())
920 case 1: outputTriplet(visit, "bool(", "", ")"); break;
921 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
922 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
923 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
924 default: UNREACHABLE();
927 case EOpConvBoolToFloat:
928 case EOpConvIntToFloat:
929 switch (node->getOperand()->getType().getNominalSize())
931 case 1: outputTriplet(visit, "float(", "", ")"); break;
932 case 2: outputTriplet(visit, "float2(", "", ")"); break;
933 case 3: outputTriplet(visit, "float3(", "", ")"); break;
934 case 4: outputTriplet(visit, "float4(", "", ")"); break;
935 default: UNREACHABLE();
938 case EOpConvFloatToInt:
939 case EOpConvBoolToInt:
940 switch (node->getOperand()->getType().getNominalSize())
942 case 1: outputTriplet(visit, "int(", "", ")"); break;
943 case 2: outputTriplet(visit, "int2(", "", ")"); break;
944 case 3: outputTriplet(visit, "int3(", "", ")"); break;
945 case 4: outputTriplet(visit, "int4(", "", ")"); break;
946 default: UNREACHABLE();
949 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
950 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
951 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
952 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
953 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
954 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
955 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
956 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
957 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
958 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
959 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
960 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
961 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
962 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
963 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
964 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
965 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
966 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
967 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
968 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
969 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
970 case EOpDFdx: outputTriplet(visit, "ddx(", "", ")"); break;
971 case EOpDFdy: outputTriplet(visit, "ddy(", "", ")"); break;
972 case EOpFwidth: outputTriplet(visit, "fwidth(", "", ")"); break;
973 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
974 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
975 default: UNREACHABLE();
981 bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
983 ShShaderType shaderType = mContext.shaderType;
984 TInfoSinkBase &out = mBody;
986 switch (node->getOp())
996 if (mScopeBracket.size() < mScopeDepth)
998 mScopeBracket.push_back(0); // New scope level
1002 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1006 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1008 if (isSingleStatement(*sit))
1010 mUnfoldSelect->traverse(*sit);
1013 (*sit)->traverse(this);
1018 if (mInsideFunction)
1027 case EOpDeclaration:
1028 if (visit == PreVisit)
1030 TIntermSequence &sequence = node->getSequence();
1031 TIntermTyped *variable = sequence[0]->getAsTyped();
1034 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
1036 if (variable->getType().getStruct())
1038 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
1041 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1043 if (!mInsideFunction)
1048 out << typeString(variable->getType()) + " ";
1050 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1052 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1056 symbol->traverse(this);
1057 out << arrayString(symbol->getType());
1058 out << " = " + initializer(variable->getType());
1062 (*sit)->traverse(this);
1065 if (visit && this->inVisit)
1067 if (*sit != sequence.back())
1069 visit = this->visitAggregate(InVisit, node);
1074 if (visit && this->postVisit)
1076 this->visitAggregate(PostVisit, node);
1079 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1081 // Already added to constructor map
1088 else if (visit == InVisit)
1094 if (visit == PreVisit)
1096 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
1098 TIntermSequence &arguments = node->getSequence();
1100 for (unsigned int i = 0; i < arguments.size(); i++)
1102 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1106 out << argumentString(symbol);
1108 if (i < arguments.size() - 1)
1121 case EOpComma: outputTriplet(visit, "", ", ", ""); break;
1124 TString name = TFunction::unmangleName(node->getName());
1126 if (visit == PreVisit)
1128 out << typeString(node->getType()) << " ";
1136 out << decorate(name) << "(";
1139 TIntermSequence &sequence = node->getSequence();
1140 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1142 for (unsigned int i = 0; i < arguments.size(); i++)
1144 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1148 out << argumentString(symbol);
1150 if (i < arguments.size() - 1)
1158 sequence.erase(sequence.begin());
1163 mInsideFunction = true;
1165 else if (visit == PostVisit)
1169 mInsideFunction = false;
1173 case EOpFunctionCall:
1175 if (visit == PreVisit)
1177 TString name = TFunction::unmangleName(node->getName());
1179 if (node->isUserDefined())
1181 out << decorate(name) << "(";
1185 if (name == "texture2D")
1187 if (node->getSequence().size() == 2)
1189 mUsesTexture2D = true;
1191 else if (node->getSequence().size() == 3)
1193 mUsesTexture2D_bias = true;
1197 out << "gl_texture2D(";
1199 else if (name == "texture2DProj")
1201 if (node->getSequence().size() == 2)
1203 mUsesTexture2DProj = true;
1205 else if (node->getSequence().size() == 3)
1207 mUsesTexture2DProj_bias = true;
1211 out << "gl_texture2DProj(";
1213 else if (name == "textureCube")
1215 if (node->getSequence().size() == 2)
1217 mUsesTextureCube = true;
1219 else if (node->getSequence().size() == 3)
1221 mUsesTextureCube_bias = true;
1225 out << "gl_textureCube(";
1227 else if (name == "texture2DLod")
1229 UNIMPLEMENTED(); // Requires the vertex shader texture sampling extension
1231 else if (name == "texture2DProjLod")
1233 UNIMPLEMENTED(); // Requires the vertex shader texture sampling extension
1235 else if (name == "textureCubeLod")
1237 UNIMPLEMENTED(); // Requires the vertex shader texture sampling extension
1242 else if (visit == InVisit)
1252 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
1253 case EOpConstructFloat:
1254 addConstructor(node->getType(), "vec1", &node->getSequence());
1255 outputTriplet(visit, "vec1(", "", ")");
1257 case EOpConstructVec2:
1258 addConstructor(node->getType(), "vec2", &node->getSequence());
1259 outputTriplet(visit, "vec2(", ", ", ")");
1261 case EOpConstructVec3:
1262 addConstructor(node->getType(), "vec3", &node->getSequence());
1263 outputTriplet(visit, "vec3(", ", ", ")");
1265 case EOpConstructVec4:
1266 addConstructor(node->getType(), "vec4", &node->getSequence());
1267 outputTriplet(visit, "vec4(", ", ", ")");
1269 case EOpConstructBool:
1270 addConstructor(node->getType(), "bvec1", &node->getSequence());
1271 outputTriplet(visit, "bvec1(", "", ")");
1273 case EOpConstructBVec2:
1274 addConstructor(node->getType(), "bvec2", &node->getSequence());
1275 outputTriplet(visit, "bvec2(", ", ", ")");
1277 case EOpConstructBVec3:
1278 addConstructor(node->getType(), "bvec3", &node->getSequence());
1279 outputTriplet(visit, "bvec3(", ", ", ")");
1281 case EOpConstructBVec4:
1282 addConstructor(node->getType(), "bvec4", &node->getSequence());
1283 outputTriplet(visit, "bvec4(", ", ", ")");
1285 case EOpConstructInt:
1286 addConstructor(node->getType(), "ivec1", &node->getSequence());
1287 outputTriplet(visit, "ivec1(", "", ")");
1289 case EOpConstructIVec2:
1290 addConstructor(node->getType(), "ivec2", &node->getSequence());
1291 outputTriplet(visit, "ivec2(", ", ", ")");
1293 case EOpConstructIVec3:
1294 addConstructor(node->getType(), "ivec3", &node->getSequence());
1295 outputTriplet(visit, "ivec3(", ", ", ")");
1297 case EOpConstructIVec4:
1298 addConstructor(node->getType(), "ivec4", &node->getSequence());
1299 outputTriplet(visit, "ivec4(", ", ", ")");
1301 case EOpConstructMat2:
1302 addConstructor(node->getType(), "mat2", &node->getSequence());
1303 outputTriplet(visit, "mat2(", ", ", ")");
1305 case EOpConstructMat3:
1306 addConstructor(node->getType(), "mat3", &node->getSequence());
1307 outputTriplet(visit, "mat3(", ", ", ")");
1309 case EOpConstructMat4:
1310 addConstructor(node->getType(), "mat4", &node->getSequence());
1311 outputTriplet(visit, "mat4(", ", ", ")");
1313 case EOpConstructStruct:
1314 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1315 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
1317 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1318 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1319 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1320 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1321 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1322 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
1325 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
1327 case 1: mUsesMod1 = true; break;
1328 case 2: mUsesMod2 = true; break;
1329 case 3: mUsesMod3 = true; break;
1330 case 4: mUsesMod4 = true; break;
1331 default: UNREACHABLE();
1334 outputTriplet(visit, "mod(", ", ", ")");
1337 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
1339 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
1341 outputTriplet(visit, "atanyx(", ", ", ")");
1343 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1344 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1345 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1346 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1347 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1348 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1349 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1350 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1351 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
1352 case EOpFaceForward:
1354 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
1356 case 1: mUsesFaceforward1 = true; break;
1357 case 2: mUsesFaceforward2 = true; break;
1358 case 3: mUsesFaceforward3 = true; break;
1359 case 4: mUsesFaceforward4 = true; break;
1360 default: UNREACHABLE();
1363 outputTriplet(visit, "faceforward(", ", ", ")");
1366 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1367 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1368 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1369 default: UNREACHABLE();
1375 bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1377 TInfoSinkBase &out = mBody;
1379 if (node->usesTernaryOperator())
1381 out << "t" << mUnfoldSelect->getTemporaryIndex();
1383 else // if/else statement
1385 mUnfoldSelect->traverse(node->getCondition());
1389 node->getCondition()->traverse(this);
1394 if (node->getTrueBlock())
1396 node->getTrueBlock()->traverse(this);
1401 if (node->getFalseBlock())
1406 node->getFalseBlock()->traverse(this);
1415 void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1417 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
1420 bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1422 if (handleExcessiveLoop(node))
1427 TInfoSinkBase &out = mBody;
1429 if (node->getType() == ELoopDoWhile)
1436 if (node->getInit())
1438 mUnfoldSelect->traverse(node->getInit());
1441 if (node->getCondition())
1443 mUnfoldSelect->traverse(node->getCondition());
1446 if (node->getExpression())
1448 mUnfoldSelect->traverse(node->getExpression());
1453 if (node->getInit())
1455 node->getInit()->traverse(this);
1460 if (node->getCondition())
1462 node->getCondition()->traverse(this);
1467 if (node->getExpression())
1469 node->getExpression()->traverse(this);
1476 if (node->getBody())
1478 node->getBody()->traverse(this);
1483 if (node->getType() == ELoopDoWhile)
1487 node->getCondition()->traverse(this);
1497 bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1499 TInfoSinkBase &out = mBody;
1501 switch (node->getFlowOp())
1503 case EOpKill: outputTriplet(visit, "discard", "", ""); break;
1504 case EOpBreak: outputTriplet(visit, "break", "", ""); break;
1505 case EOpContinue: outputTriplet(visit, "continue", "", ""); break;
1507 if (visit == PreVisit)
1509 if (node->getExpression())
1518 else if (visit == PostVisit)
1523 default: UNREACHABLE();
1529 bool OutputHLSL::isSingleStatement(TIntermNode *node)
1531 TIntermAggregate *aggregate = node->getAsAggregate();
1535 if (aggregate->getOp() == EOpSequence)
1541 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1543 if (!isSingleStatement(*sit))
1556 // Handle loops with more than 255 iterations (unsupported by D3D9) by splitting them
1557 bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1559 TInfoSinkBase &out = mBody;
1561 // Parse loops of the form:
1562 // for(int index = initial; index [comparator] limit; index += increment)
1563 TIntermSymbol *index = NULL;
1564 TOperator comparator = EOpNull;
1569 // Parse index name and intial value
1570 if (node->getInit())
1572 TIntermAggregate *init = node->getInit()->getAsAggregate();
1576 TIntermSequence &sequence = init->getSequence();
1577 TIntermTyped *variable = sequence[0]->getAsTyped();
1579 if (variable && variable->getQualifier() == EvqTemporary)
1581 TIntermBinary *assign = variable->getAsBinaryNode();
1583 if (assign->getOp() == EOpInitialize)
1585 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1586 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1588 if (symbol && constant)
1590 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
1593 initial = constant->getUnionArrayPointer()[0].getIConst();
1601 // Parse comparator and limit value
1602 if (index != NULL && node->getCondition())
1604 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
1606 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1608 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1612 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
1614 comparator = test->getOp();
1615 limit = constant->getUnionArrayPointer()[0].getIConst();
1622 if (index != NULL && comparator != EOpNull && node->getExpression())
1624 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
1625 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
1629 TOperator op = binaryTerminal->getOp();
1630 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
1634 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
1636 int value = constant->getUnionArrayPointer()[0].getIConst();
1640 case EOpAddAssign: increment = value; break;
1641 case EOpSubAssign: increment = -value; break;
1642 default: UNIMPLEMENTED();
1647 else if (unaryTerminal)
1649 TOperator op = unaryTerminal->getOp();
1653 case EOpPostIncrement: increment = 1; break;
1654 case EOpPostDecrement: increment = -1; break;
1655 case EOpPreIncrement: increment = 1; break;
1656 case EOpPreDecrement: increment = -1; break;
1657 default: UNIMPLEMENTED();
1662 if (index != NULL && comparator != EOpNull && increment != 0)
1664 if (comparator == EOpLessThanEqual)
1666 comparator = EOpLessThan;
1670 if (comparator == EOpLessThan)
1672 int iterations = (limit - initial + 1) / increment;
1674 if (iterations <= 255)
1676 return false; // Not an excessive loop
1679 while (iterations > 0)
1681 int remainder = (limit - initial + 1) % increment;
1682 int clampedLimit = initial + increment * std::min(255, iterations) - 1 - remainder;
1684 // for(int index = initial; index < clampedLimit; index += increment)
1687 index->traverse(this);
1692 index->traverse(this);
1694 out << clampedLimit;
1697 index->traverse(this);
1703 if (node->getBody())
1705 node->getBody()->traverse(this);
1710 initial += 255 * increment;
1716 else UNIMPLEMENTED();
1719 return false; // Not handled as an excessive loop
1722 void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
1724 TInfoSinkBase &out = mBody;
1726 if (visit == PreVisit)
1730 else if (visit == InVisit)
1734 else if (visit == PostVisit)
1740 TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
1742 TQualifier qualifier = symbol->getQualifier();
1743 const TType &type = symbol->getType();
1744 TString name = symbol->getSymbol();
1746 if (name.empty()) // HLSL demands named arguments, also for prototypes
1748 name = "x" + str(mUniqueIndex++);
1752 name = decorate(name);
1755 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
1758 TString OutputHLSL::qualifierString(TQualifier qualifier)
1762 case EvqIn: return "in";
1763 case EvqOut: return "out";
1764 case EvqInOut: return "inout";
1765 case EvqConstReadOnly: return "const";
1766 default: UNREACHABLE();
1772 TString OutputHLSL::typeString(const TType &type)
1774 if (type.getBasicType() == EbtStruct)
1776 if (type.getTypeName() != "")
1778 return structLookup(type.getTypeName());
1780 else // Nameless structure, define in place
1782 const TTypeList &fields = *type.getStruct();
1784 TString string = "struct\n"
1787 for (unsigned int i = 0; i < fields.size(); i++)
1789 const TType &field = *fields[i].type;
1791 string += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
1799 else if (type.isMatrix())
1801 switch (type.getNominalSize())
1803 case 2: return "float2x2";
1804 case 3: return "float3x3";
1805 case 4: return "float4x4";
1810 switch (type.getBasicType())
1813 switch (type.getNominalSize())
1815 case 1: return "float";
1816 case 2: return "float2";
1817 case 3: return "float3";
1818 case 4: return "float4";
1821 switch (type.getNominalSize())
1823 case 1: return "int";
1824 case 2: return "int2";
1825 case 3: return "int3";
1826 case 4: return "int4";
1829 switch (type.getNominalSize())
1831 case 1: return "bool";
1832 case 2: return "bool2";
1833 case 3: return "bool3";
1834 case 4: return "bool4";
1840 case EbtSamplerCube:
1841 return "samplerCUBE";
1845 UNIMPLEMENTED(); // FIXME
1846 return "<unknown type>";
1849 TString OutputHLSL::arrayString(const TType &type)
1851 if (!type.isArray())
1856 return "[" + str(type.getArraySize()) + "]";
1859 TString OutputHLSL::initializer(const TType &type)
1863 for (int component = 0; component < type.getObjectSize(); component++)
1867 if (component < type.getObjectSize() - 1)
1873 return "{" + string + "}";
1876 void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
1880 return; // Nameless structures don't have constructors
1883 TType ctorType = type;
1884 ctorType.clearArrayness();
1885 ctorType.setPrecision(EbpHigh);
1886 ctorType.setQualifier(EvqTemporary);
1888 TString ctorName = type.getStruct() ? decorate(name) : name;
1890 typedef std::vector<TType> ParameterArray;
1891 ParameterArray ctorParameters;
1895 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
1897 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
1900 else if (type.getStruct())
1902 mStructNames.insert(decorate(name));
1905 structure += "struct " + decorate(name) + "\n"
1908 const TTypeList &fields = *type.getStruct();
1910 for (unsigned int i = 0; i < fields.size(); i++)
1912 const TType &field = *fields[i].type;
1914 structure += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
1917 structure += "};\n";
1919 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
1921 mStructDeclarations.push_back(structure);
1924 for (unsigned int i = 0; i < fields.size(); i++)
1926 ctorParameters.push_back(*fields[i].type);
1931 TString constructor;
1933 if (ctorType.getStruct())
1935 constructor += ctorName + " " + ctorName + "_ctor(";
1937 else // Built-in type
1939 constructor += typeString(ctorType) + " " + ctorName + "(";
1942 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
1944 const TType &type = ctorParameters[parameter];
1946 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
1948 if (parameter < ctorParameters.size() - 1)
1950 constructor += ", ";
1954 constructor += ")\n"
1957 if (ctorType.getStruct())
1959 constructor += " " + ctorName + " structure = {";
1963 constructor += " return " + typeString(ctorType) + "(";
1966 if (ctorType.isMatrix() && ctorParameters.size() == 1)
1968 int dim = ctorType.getNominalSize();
1969 const TType ¶meter = ctorParameters[0];
1971 if (parameter.isScalar())
1973 for (int row = 0; row < dim; row++)
1975 for (int col = 0; col < dim; col++)
1977 constructor += TString((row == col) ? "x0" : "0.0");
1979 if (row < dim - 1 || col < dim - 1)
1981 constructor += ", ";
1986 else if (parameter.isMatrix())
1988 for (int row = 0; row < dim; row++)
1990 for (int col = 0; col < dim; col++)
1992 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
1994 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
1998 constructor += TString((row == col) ? "1.0" : "0.0");
2001 if (row < dim - 1 || col < dim - 1)
2003 constructor += ", ";
2012 int remainingComponents = ctorType.getObjectSize();
2013 int parameterIndex = 0;
2015 while (remainingComponents > 0)
2017 const TType ¶meter = ctorParameters[parameterIndex];
2018 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2020 constructor += "x" + str(parameterIndex);
2022 if (parameter.isScalar())
2024 remainingComponents -= parameter.getObjectSize();
2026 else if (parameter.isVector())
2028 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2030 remainingComponents -= parameter.getObjectSize();
2032 else if (remainingComponents < parameter.getNominalSize())
2034 switch (remainingComponents)
2036 case 1: constructor += ".x"; break;
2037 case 2: constructor += ".xy"; break;
2038 case 3: constructor += ".xyz"; break;
2039 case 4: constructor += ".xyzw"; break;
2040 default: UNREACHABLE();
2043 remainingComponents = 0;
2047 else if (parameter.isMatrix() || parameter.getStruct())
2049 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2051 remainingComponents -= parameter.getObjectSize();
2060 if (remainingComponents)
2062 constructor += ", ";
2067 if (ctorType.getStruct())
2069 constructor += "};\n"
2070 " return structure;\n"
2075 constructor += ");\n"
2079 mConstructors.insert(constructor);
2082 const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2084 TInfoSinkBase &out = mBody;
2086 if (type.getBasicType() == EbtStruct)
2088 out << structLookup(type.getTypeName()) + "_ctor(";
2090 const TTypeList *structure = type.getStruct();
2092 for (size_t i = 0; i < structure->size(); i++)
2094 const TType *fieldType = (*structure)[i].type;
2096 constUnion = writeConstantUnion(*fieldType, constUnion);
2098 if (i != structure->size() - 1)
2108 int size = type.getObjectSize();
2109 bool writeType = size > 1;
2113 out << typeString(type) << "(";
2116 for (int i = 0; i < size; i++, constUnion++)
2118 switch (constUnion->getType())
2120 case EbtFloat: out << constUnion->getFConst(); break;
2121 case EbtInt: out << constUnion->getIConst(); break;
2122 case EbtBool: out << constUnion->getBConst(); break;
2123 default: UNREACHABLE();
2141 TString OutputHLSL::scopeString(unsigned int depthLimit)
2145 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2147 string += "_" + str(i);
2153 TString OutputHLSL::scopedStruct(const TString &typeName)
2160 return typeName + scopeString(mScopeDepth);
2163 TString OutputHLSL::structLookup(const TString &typeName)
2165 for (int depth = mScopeDepth; depth >= 0; depth--)
2167 TString scopedName = decorate(typeName + scopeString(depth));
2169 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2171 if (*structName == scopedName)
2178 UNREACHABLE(); // Should have found a matching constructor
2183 TString OutputHLSL::decorate(const TString &string)
2185 if (string.substr(0, 3) != "gl_" && string.substr(0, 3) != "dx_")
2187 return "_" + string;