2 // Copyright (c) 2002-2013 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/translator/OutputHLSL.h"
9 #include "common/angleutils.h"
10 #include "compiler/translator/compilerdebug.h"
11 #include "compiler/translator/DetectDiscontinuity.h"
12 #include "compiler/translator/InfoSink.h"
13 #include "compiler/translator/SearchSymbol.h"
14 #include "compiler/translator/UnfoldShortCircuit.h"
15 #include "compiler/translator/NodeSearch.h"
16 #include "compiler/translator/RewriteElseBlocks.h"
25 OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
26 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
28 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
29 mInsideFunction = false;
31 mUsesTexture2D = false;
32 mUsesTexture2D_bias = false;
33 mUsesTexture2DProj = false;
34 mUsesTexture2DProj_bias = false;
35 mUsesTexture2DProjLod = false;
36 mUsesTexture2DLod = false;
37 mUsesTextureCube = false;
38 mUsesTextureCube_bias = false;
39 mUsesTextureCubeLod = false;
40 mUsesTexture2DLod0 = false;
41 mUsesTexture2DLod0_bias = false;
42 mUsesTexture2DProjLod0 = false;
43 mUsesTexture2DProjLod0_bias = false;
44 mUsesTextureCubeLod0 = false;
45 mUsesTextureCubeLod0_bias = false;
46 mUsesFragColor = false;
47 mUsesFragData = false;
48 mUsesDepthRange = false;
49 mUsesFragCoord = false;
50 mUsesPointCoord = false;
51 mUsesFrontFacing = false;
52 mUsesPointSize = false;
53 mUsesFragDepth = false;
62 mUsesFaceforward1 = false;
63 mUsesFaceforward2 = false;
64 mUsesFaceforward3 = false;
65 mUsesFaceforward4 = false;
70 mUsesDiscardRewriting = false;
72 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
78 mContainsLoopDiscontinuity = false;
79 mOutputLod0Function = false;
80 mInsideDiscontinuousLoop = false;
82 mExcessiveLoopIndex = NULL;
84 if (mOutputType == SH_HLSL9_OUTPUT)
86 if (mContext.shaderType == SH_FRAGMENT_SHADER)
88 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
92 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
100 mSamplerRegister = 0;
103 OutputHLSL::~OutputHLSL()
105 delete mUnfoldShortCircuit;
108 void OutputHLSL::output()
110 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
112 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
113 // use a vertex attribute as a condition, and some related computation in the else block.
114 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
116 RewriteElseBlocks(mContext.treeRoot);
119 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
122 mContext.infoSink().obj << mHeader.c_str();
123 mContext.infoSink().obj << mBody.c_str();
126 TInfoSinkBase &OutputHLSL::getBodyStream()
131 const ActiveUniforms &OutputHLSL::getUniforms()
133 return mActiveUniforms;
136 int OutputHLSL::vectorSize(const TType &type) const
138 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
139 int arraySize = type.isArray() ? type.getArraySize() : 1;
141 return elementSize * arraySize;
144 void OutputHLSL::header()
146 ShShaderType shaderType = mContext.shaderType;
147 TInfoSinkBase &out = mHeader;
149 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
151 out << *structDeclaration;
154 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
163 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
165 const TType &type = uniform->second->getType();
166 const TString &name = uniform->second->getSymbol();
168 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
170 int index = samplerRegister(mReferencedUniforms[name]);
172 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
173 " : register(s" + str(index) + ");\n";
175 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
176 " : register(t" + str(index) + ");\n";
180 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
181 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
185 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
187 const TType &type = varying->second->getType();
188 const TString &name = varying->second->getSymbol();
190 // Program linking depends on this exact format
191 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
194 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
196 const TType &type = attribute->second->getType();
197 const TString &name = attribute->second->getSymbol();
199 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
202 if (mUsesDiscardRewriting)
204 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
207 if (shaderType == SH_FRAGMENT_SHADER)
209 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
210 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
212 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
214 out << "// Varyings\n";
217 "static float4 gl_Color[" << numColorValues << "] =\n"
219 for (unsigned int i = 0; i < numColorValues; i++)
221 out << " float4(0, 0, 0, 0)";
222 if (i + 1 != numColorValues)
232 out << "static float gl_Depth = 0.0;\n";
237 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
242 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
245 if (mUsesFrontFacing)
247 out << "static bool gl_FrontFacing = false;\n";
254 out << "struct gl_DepthRangeParameters\n"
263 if (mOutputType == SH_HLSL11_OUTPUT)
265 out << "cbuffer DriverConstants : register(b1)\n"
270 out << " float3 dx_DepthRange : packoffset(c0);\n";
275 out << " float4 dx_ViewCoords : packoffset(c1);\n";
278 if (mUsesFragCoord || mUsesFrontFacing)
280 out << " float3 dx_DepthFront : packoffset(c2);\n";
289 out << "uniform float3 dx_DepthRange : register(c0);";
294 out << "uniform float4 dx_ViewCoords : register(c1);\n";
297 if (mUsesFragCoord || mUsesFrontFacing)
299 out << "uniform float3 dx_DepthFront : register(c2);\n";
307 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
316 if (mOutputType == SH_HLSL9_OUTPUT)
318 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
320 " return tex2D(s, t);\n"
324 else if (mOutputType == SH_HLSL11_OUTPUT)
326 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
328 " return t.Sample(s, uv);\n"
335 if (mUsesTexture2D_bias)
337 if (mOutputType == SH_HLSL9_OUTPUT)
339 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
341 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
345 else if (mOutputType == SH_HLSL11_OUTPUT)
347 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv, float bias)\n"
349 " return t.SampleBias(s, uv, bias);\n"
356 if (mUsesTexture2DProj)
358 if (mOutputType == SH_HLSL9_OUTPUT)
360 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
362 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
365 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
367 " return tex2Dproj(s, t);\n"
371 else if (mOutputType == SH_HLSL11_OUTPUT)
373 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
375 " return t.Sample(s, float2(uvw.x / uvw.z, uvw.y / uvw.z));\n"
378 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
380 " return t.Sample(s, float2(uvw.x / uvw.w, uvw.y / uvw.w));\n"
387 if (mUsesTexture2DProj_bias)
389 if (mOutputType == SH_HLSL9_OUTPUT)
391 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
393 " return tex2Dbias(s, float4(t.x / t.z, t.y / t.z, 0, bias));\n"
396 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
398 " return tex2Dbias(s, float4(t.x / t.w, t.y / t.w, 0, bias));\n"
402 else if (mOutputType == SH_HLSL11_OUTPUT)
404 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
406 " return t.SampleBias(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), bias);\n"
409 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
411 " return t.SampleBias(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), bias);\n"
418 if (mUsesTextureCube)
420 if (mOutputType == SH_HLSL9_OUTPUT)
422 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
424 " return texCUBE(s, t);\n"
428 else if (mOutputType == SH_HLSL11_OUTPUT)
430 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
432 " return t.Sample(s, uvw);\n"
439 if (mUsesTextureCube_bias)
441 if (mOutputType == SH_HLSL9_OUTPUT)
443 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
445 " return texCUBEbias(s, float4(t.x, t.y, t.z, bias));\n"
449 else if (mOutputType == SH_HLSL11_OUTPUT)
451 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
453 " return t.SampleBias(s, uvw, bias);\n"
460 // These *Lod0 intrinsics are not available in GL fragment shaders.
461 // They are used to sample using discontinuous texture coordinates.
462 if (mUsesTexture2DLod0)
464 if (mOutputType == SH_HLSL9_OUTPUT)
466 out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
468 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
472 else if (mOutputType == SH_HLSL11_OUTPUT)
474 out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv)\n"
476 " return t.SampleLevel(s, uv, 0);\n"
483 if (mUsesTexture2DLod0_bias)
485 if (mOutputType == SH_HLSL9_OUTPUT)
487 out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
489 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
493 else if (mOutputType == SH_HLSL11_OUTPUT)
495 out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv, float bias)\n"
497 " return t.SampleLevel(s, uv, 0);\n"
504 if (mUsesTexture2DProjLod0)
506 if (mOutputType == SH_HLSL9_OUTPUT)
508 out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
510 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
513 "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
515 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
519 else if (mOutputType == SH_HLSL11_OUTPUT)
521 out << "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float3 uvw)\n"
523 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
526 "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float4 uvw)\n"
528 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
535 if (mUsesTexture2DProjLod0_bias)
537 if (mOutputType == SH_HLSL9_OUTPUT)
539 out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
541 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
544 "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
546 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
550 else if (mOutputType == SH_HLSL11_OUTPUT)
552 out << "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
554 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
557 "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
559 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
566 if (mUsesTextureCubeLod0)
568 if (mOutputType == SH_HLSL9_OUTPUT)
570 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
572 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
576 else if (mOutputType == SH_HLSL11_OUTPUT)
578 out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw)\n"
580 " return t.SampleLevel(s, uvw, 0);\n"
587 if (mUsesTextureCubeLod0_bias)
589 if (mOutputType == SH_HLSL9_OUTPUT)
591 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
593 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
597 else if (mOutputType == SH_HLSL11_OUTPUT)
599 out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
601 " return t.SampleLevel(s, uvw, 0);\n"
608 if (usingMRTExtension && mNumRenderTargets > 1)
610 out << "#define GL_USES_MRT\n";
615 out << "#define GL_USES_FRAG_COLOR\n";
620 out << "#define GL_USES_FRAG_DATA\n";
623 else // Vertex shader
625 out << "// Attributes\n";
628 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
632 out << "static float gl_PointSize = float(1);\n";
642 out << "struct gl_DepthRangeParameters\n"
651 if (mOutputType == SH_HLSL11_OUTPUT)
655 out << "cbuffer DriverConstants : register(b1)\n"
657 " float3 dx_DepthRange : packoffset(c0);\n"
666 out << "uniform float3 dx_DepthRange : register(c0);\n";
669 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
675 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
684 if (mOutputType == SH_HLSL9_OUTPUT)
686 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
688 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
692 else if (mOutputType == SH_HLSL11_OUTPUT)
694 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
696 " return t.SampleLevel(s, uv, 0);\n"
703 if (mUsesTexture2DLod)
705 if (mOutputType == SH_HLSL9_OUTPUT)
707 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
709 " return tex2Dlod(s, float4(t.x, t.y, 0, lod));\n"
713 else if (mOutputType == SH_HLSL11_OUTPUT)
715 out << "float4 gl_texture2DLod(Texture2D t, SamplerState s, float2 uv, float lod)\n"
717 " return t.SampleLevel(s, uv, lod);\n"
724 if (mUsesTexture2DProj)
726 if (mOutputType == SH_HLSL9_OUTPUT)
728 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
730 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
733 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
735 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
739 else if (mOutputType == SH_HLSL11_OUTPUT)
741 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
743 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
746 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
748 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
755 if (mUsesTexture2DProjLod)
757 if (mOutputType == SH_HLSL9_OUTPUT)
759 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
761 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, lod));\n"
764 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
766 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, lod));\n"
770 else if (mOutputType == SH_HLSL11_OUTPUT)
772 out << "float4 gl_texture2DProjLod(Texture2D t, SamplerState s, float3 uvw, float lod)\n"
774 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), lod);\n"
777 "float4 gl_texture2DProjLod(Texture2D t, SamplerState s, float4 uvw, float lod)\n"
779 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), lod);\n"
786 if (mUsesTextureCube)
788 if (mOutputType == SH_HLSL9_OUTPUT)
790 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
792 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
796 else if (mOutputType == SH_HLSL11_OUTPUT)
798 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
800 " return t.SampleLevel(s, uvw, 0);\n"
807 if (mUsesTextureCubeLod)
809 if (mOutputType == SH_HLSL9_OUTPUT)
811 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
813 " return texCUBElod(s, float4(t.x, t.y, t.z, lod));\n"
817 else if (mOutputType == SH_HLSL11_OUTPUT)
819 out << "float4 gl_textureCubeLod(TextureCube t, SamplerState s, float3 uvw, float lod)\n"
821 " return t.SampleLevel(s, uvw, lod);\n"
831 out << "#define GL_USES_FRAG_COORD\n";
836 out << "#define GL_USES_POINT_COORD\n";
839 if (mUsesFrontFacing)
841 out << "#define GL_USES_FRONT_FACING\n";
846 out << "#define GL_USES_POINT_SIZE\n";
851 out << "#define GL_USES_FRAG_DEPTH\n";
856 out << "#define GL_USES_DEPTH_RANGE\n";
861 out << "bool xor(bool p, bool q)\n"
863 " return (p || q) && !(p && q);\n"
870 out << "float mod(float x, float y)\n"
872 " return x - y * floor(x / y);\n"
879 out << "float2 mod(float2 x, float2 y)\n"
881 " return x - y * floor(x / y);\n"
888 out << "float2 mod(float2 x, float y)\n"
890 " return x - y * floor(x / y);\n"
897 out << "float3 mod(float3 x, float3 y)\n"
899 " return x - y * floor(x / y);\n"
906 out << "float3 mod(float3 x, float y)\n"
908 " return x - y * floor(x / y);\n"
915 out << "float4 mod(float4 x, float4 y)\n"
917 " return x - y * floor(x / y);\n"
924 out << "float4 mod(float4 x, float y)\n"
926 " return x - y * floor(x / y);\n"
931 if (mUsesFaceforward1)
933 out << "float faceforward(float N, float I, float Nref)\n"
935 " if(dot(Nref, I) >= 0)\n"
947 if (mUsesFaceforward2)
949 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
951 " if(dot(Nref, I) >= 0)\n"
963 if (mUsesFaceforward3)
965 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
967 " if(dot(Nref, I) >= 0)\n"
979 if (mUsesFaceforward4)
981 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
983 " if(dot(Nref, I) >= 0)\n"
997 out << "float atanyx(float y, float x)\n"
999 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1000 " return atan2(y, x);\n"
1006 out << "float2 atanyx(float2 y, float2 x)\n"
1008 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1009 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1010 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1016 out << "float3 atanyx(float3 y, float3 x)\n"
1018 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1019 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1020 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1021 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1027 out << "float4 atanyx(float4 y, float4 x)\n"
1029 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1030 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1031 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1032 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1033 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1038 void OutputHLSL::visitSymbol(TIntermSymbol *node)
1040 TInfoSinkBase &out = mBody;
1042 TString name = node->getSymbol();
1044 if (name == "gl_FragColor")
1046 out << "gl_Color[0]";
1047 mUsesFragColor = true;
1049 else if (name == "gl_FragData")
1052 mUsesFragData = true;
1054 else if (name == "gl_DepthRange")
1056 mUsesDepthRange = true;
1059 else if (name == "gl_FragCoord")
1061 mUsesFragCoord = true;
1064 else if (name == "gl_PointCoord")
1066 mUsesPointCoord = true;
1069 else if (name == "gl_FrontFacing")
1071 mUsesFrontFacing = true;
1074 else if (name == "gl_PointSize")
1076 mUsesPointSize = true;
1079 else if (name == "gl_FragDepthEXT")
1081 mUsesFragDepth = true;
1086 TQualifier qualifier = node->getQualifier();
1088 if (qualifier == EvqUniform)
1090 mReferencedUniforms[name] = node;
1091 out << decorateUniform(name, node->getType());
1093 else if (qualifier == EvqAttribute)
1095 mReferencedAttributes[name] = node;
1096 out << decorate(name);
1098 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
1100 mReferencedVaryings[name] = node;
1101 out << decorate(name);
1103 else if (qualifier == EvqInternal)
1109 out << decorate(name);
1114 void OutputHLSL::visitRaw(TIntermRaw *node)
1116 mBody << node->getRawText();
1119 bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1121 TInfoSinkBase &out = mBody;
1123 switch (node->getOp())
1125 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
1127 if (visit == PreVisit)
1129 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1130 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1131 // new variable is created before the assignment is evaluated), so we need to convert
1132 // this to "float t = x, x = t;".
1134 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1135 TIntermTyped *expression = node->getRight();
1137 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1138 expression->traverse(&searchSymbol);
1139 bool sameSymbol = searchSymbol.foundMatch();
1143 // Type already printed
1144 out << "t" + str(mUniqueIndex) + " = ";
1145 expression->traverse(this);
1147 symbolNode->traverse(this);
1148 out << " = t" + str(mUniqueIndex);
1154 else if (visit == InVisit)
1159 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1160 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1161 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1162 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1163 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1164 case EOpVectorTimesMatrixAssign:
1165 if (visit == PreVisit)
1169 else if (visit == InVisit)
1172 node->getLeft()->traverse(this);
1173 out << ", transpose(";
1180 case EOpMatrixTimesMatrixAssign:
1181 if (visit == PreVisit)
1185 else if (visit == InVisit)
1188 node->getLeft()->traverse(this);
1196 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
1197 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
1198 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
1199 case EOpIndexDirectStruct:
1200 if (visit == InVisit)
1202 const TStructure* structure = node->getLeft()->getType().getStruct();
1203 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1204 const TField* field = structure->fields()[index->getIConst(0)];
1205 out << "." + decorateField(field->name(), node->getLeft()->getType());
1210 case EOpVectorSwizzle:
1211 if (visit == InVisit)
1215 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1219 TIntermSequence &sequence = swizzle->getSequence();
1221 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1223 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1227 int i = element->getIConst(0);
1231 case 0: out << "x"; break;
1232 case 1: out << "y"; break;
1233 case 2: out << "z"; break;
1234 case 3: out << "w"; break;
1235 default: UNREACHABLE();
1243 return false; // Fully processed
1246 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1247 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1248 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1249 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
1252 if (node->getLeft()->isScalar())
1254 if (node->getOp() == EOpEqual)
1256 outputTriplet(visit, "(", " == ", ")");
1260 outputTriplet(visit, "(", " != ", ")");
1263 else if (node->getLeft()->getBasicType() == EbtStruct)
1265 if (node->getOp() == EOpEqual)
1274 const TFieldList &fields = node->getLeft()->getType().getStruct()->fields();
1276 for (size_t i = 0; i < fields.size(); i++)
1278 const TField *field = fields[i];
1280 node->getLeft()->traverse(this);
1281 out << "." + decorateField(field->name(), node->getLeft()->getType()) + " == ";
1282 node->getRight()->traverse(this);
1283 out << "." + decorateField(field->name(), node->getLeft()->getType());
1285 if (i < fields.size() - 1)
1297 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1299 if (node->getOp() == EOpEqual)
1301 outputTriplet(visit, "all(", " == ", ")");
1305 outputTriplet(visit, "!all(", " == ", ")");
1309 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1310 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1311 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1312 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1313 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
1314 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
1315 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1316 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
1317 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
1319 if (node->getRight()->hasSideEffects())
1321 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1326 outputTriplet(visit, "(", " || ", ")");
1331 outputTriplet(visit, "xor(", ", ", ")");
1334 if (node->getRight()->hasSideEffects())
1336 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1341 outputTriplet(visit, "(", " && ", ")");
1344 default: UNREACHABLE();
1350 bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1352 switch (node->getOp())
1354 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1355 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1356 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1357 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1358 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1359 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1360 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
1361 case EOpConvIntToBool:
1362 case EOpConvFloatToBool:
1363 switch (node->getOperand()->getType().getNominalSize())
1365 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1366 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1367 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1368 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
1369 default: UNREACHABLE();
1372 case EOpConvBoolToFloat:
1373 case EOpConvIntToFloat:
1374 switch (node->getOperand()->getType().getNominalSize())
1376 case 1: outputTriplet(visit, "float(", "", ")"); break;
1377 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1378 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1379 case 4: outputTriplet(visit, "float4(", "", ")"); break;
1380 default: UNREACHABLE();
1383 case EOpConvFloatToInt:
1384 case EOpConvBoolToInt:
1385 switch (node->getOperand()->getType().getNominalSize())
1387 case 1: outputTriplet(visit, "int(", "", ")"); break;
1388 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1389 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1390 case 4: outputTriplet(visit, "int4(", "", ")"); break;
1391 default: UNREACHABLE();
1394 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1395 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1396 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1397 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1398 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1399 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1400 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1401 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1402 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1403 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1404 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1405 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1406 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1407 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1408 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1409 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1410 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1411 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1412 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1413 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1414 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
1416 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1418 outputTriplet(visit, "(", "", ", 0.0)");
1422 outputTriplet(visit, "ddx(", "", ")");
1426 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1428 outputTriplet(visit, "(", "", ", 0.0)");
1432 outputTriplet(visit, "ddy(", "", ")");
1436 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1438 outputTriplet(visit, "(", "", ", 0.0)");
1442 outputTriplet(visit, "fwidth(", "", ")");
1445 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1446 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
1447 default: UNREACHABLE();
1453 bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1455 TInfoSinkBase &out = mBody;
1457 switch (node->getOp())
1461 if (mInsideFunction)
1463 outputLineDirective(node->getLine().first_line);
1468 if (mScopeBracket.size() < mScopeDepth)
1470 mScopeBracket.push_back(0); // New scope level
1474 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1478 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1480 outputLineDirective((*sit)->getLine().first_line);
1482 traverseStatements(*sit);
1487 if (mInsideFunction)
1489 outputLineDirective(node->getLine().last_line);
1497 case EOpDeclaration:
1498 if (visit == PreVisit)
1500 TIntermSequence &sequence = node->getSequence();
1501 TIntermTyped *variable = sequence[0]->getAsTyped();
1503 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
1505 if (variable->getType().getStruct())
1507 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
1510 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1512 if (!mInsideFunction)
1517 out << typeString(variable->getType()) + " ";
1519 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1521 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1525 symbol->traverse(this);
1526 out << arrayString(symbol->getType());
1527 out << " = " + initializer(symbol->getType());
1531 (*sit)->traverse(this);
1534 if (*sit != sequence.back())
1540 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1542 // Already added to constructor map
1546 else if (variable && (variable->getQualifier() == EvqVaryingOut || variable->getQualifier() == EvqInvariantVaryingOut))
1548 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1550 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1554 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1555 mReferencedVaryings[symbol->getSymbol()] = symbol;
1559 (*sit)->traverse(this);
1566 else if (visit == InVisit)
1572 if (visit == PreVisit)
1574 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
1576 TIntermSequence &arguments = node->getSequence();
1578 for (unsigned int i = 0; i < arguments.size(); i++)
1580 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1584 out << argumentString(symbol);
1586 if (i < arguments.size() - 1)
1596 // Also prototype the Lod0 variant if needed
1597 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1599 mOutputLod0Function = true;
1600 node->traverse(this);
1601 mOutputLod0Function = false;
1607 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
1610 TString name = TFunction::unmangleName(node->getName());
1612 out << typeString(node->getType()) << " ";
1620 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
1623 TIntermSequence &sequence = node->getSequence();
1624 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1626 for (unsigned int i = 0; i < arguments.size(); i++)
1628 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1632 if (symbol->getType().getStruct())
1634 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
1637 out << argumentString(symbol);
1639 if (i < arguments.size() - 1)
1650 if (sequence.size() > 1)
1652 mInsideFunction = true;
1653 sequence[1]->traverse(this);
1654 mInsideFunction = false;
1659 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1663 mOutputLod0Function = true;
1664 node->traverse(this);
1665 mOutputLod0Function = false;
1672 case EOpFunctionCall:
1674 TString name = TFunction::unmangleName(node->getName());
1675 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
1677 if (node->isUserDefined())
1679 out << decorate(name) << (lod0 ? "Lod0(" : "(");
1683 if (name == "texture2D")
1687 if (node->getSequence().size() == 2)
1689 mUsesTexture2D = true;
1691 else if (node->getSequence().size() == 3)
1693 mUsesTexture2D_bias = true;
1697 out << "gl_texture2D(";
1701 if (node->getSequence().size() == 2)
1703 mUsesTexture2DLod0 = true;
1705 else if (node->getSequence().size() == 3)
1707 mUsesTexture2DLod0_bias = true;
1711 out << "gl_texture2DLod0(";
1714 else if (name == "texture2DProj")
1718 if (node->getSequence().size() == 2)
1720 mUsesTexture2DProj = true;
1722 else if (node->getSequence().size() == 3)
1724 mUsesTexture2DProj_bias = true;
1728 out << "gl_texture2DProj(";
1732 if (node->getSequence().size() == 2)
1734 mUsesTexture2DProjLod0 = true;
1736 else if (node->getSequence().size() == 3)
1738 mUsesTexture2DProjLod0_bias = true;
1742 out << "gl_texture2DProjLod0(";
1745 else if (name == "textureCube")
1749 if (node->getSequence().size() == 2)
1751 mUsesTextureCube = true;
1753 else if (node->getSequence().size() == 3)
1755 mUsesTextureCube_bias = true;
1759 out << "gl_textureCube(";
1763 if (node->getSequence().size() == 2)
1765 mUsesTextureCubeLod0 = true;
1767 else if (node->getSequence().size() == 3)
1769 mUsesTextureCubeLod0_bias = true;
1773 out << "gl_textureCubeLod0(";
1776 else if (name == "texture2DLod")
1778 if (node->getSequence().size() == 3)
1780 mUsesTexture2DLod = true;
1784 out << "gl_texture2DLod(";
1786 else if (name == "texture2DProjLod")
1788 if (node->getSequence().size() == 3)
1790 mUsesTexture2DProjLod = true;
1794 out << "gl_texture2DProjLod(";
1796 else if (name == "textureCubeLod")
1798 if (node->getSequence().size() == 3)
1800 mUsesTextureCubeLod = true;
1804 out << "gl_textureCubeLod(";
1809 TIntermSequence &arguments = node->getSequence();
1811 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
1813 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
1816 (*arg)->traverse(this);
1817 out << ", sampler_";
1820 (*arg)->traverse(this);
1822 if (arg < arguments.end() - 1)
1833 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
1834 case EOpConstructFloat:
1835 addConstructor(node->getType(), "vec1", &node->getSequence());
1836 outputTriplet(visit, "vec1(", "", ")");
1838 case EOpConstructVec2:
1839 addConstructor(node->getType(), "vec2", &node->getSequence());
1840 outputTriplet(visit, "vec2(", ", ", ")");
1842 case EOpConstructVec3:
1843 addConstructor(node->getType(), "vec3", &node->getSequence());
1844 outputTriplet(visit, "vec3(", ", ", ")");
1846 case EOpConstructVec4:
1847 addConstructor(node->getType(), "vec4", &node->getSequence());
1848 outputTriplet(visit, "vec4(", ", ", ")");
1850 case EOpConstructBool:
1851 addConstructor(node->getType(), "bvec1", &node->getSequence());
1852 outputTriplet(visit, "bvec1(", "", ")");
1854 case EOpConstructBVec2:
1855 addConstructor(node->getType(), "bvec2", &node->getSequence());
1856 outputTriplet(visit, "bvec2(", ", ", ")");
1858 case EOpConstructBVec3:
1859 addConstructor(node->getType(), "bvec3", &node->getSequence());
1860 outputTriplet(visit, "bvec3(", ", ", ")");
1862 case EOpConstructBVec4:
1863 addConstructor(node->getType(), "bvec4", &node->getSequence());
1864 outputTriplet(visit, "bvec4(", ", ", ")");
1866 case EOpConstructInt:
1867 addConstructor(node->getType(), "ivec1", &node->getSequence());
1868 outputTriplet(visit, "ivec1(", "", ")");
1870 case EOpConstructIVec2:
1871 addConstructor(node->getType(), "ivec2", &node->getSequence());
1872 outputTriplet(visit, "ivec2(", ", ", ")");
1874 case EOpConstructIVec3:
1875 addConstructor(node->getType(), "ivec3", &node->getSequence());
1876 outputTriplet(visit, "ivec3(", ", ", ")");
1878 case EOpConstructIVec4:
1879 addConstructor(node->getType(), "ivec4", &node->getSequence());
1880 outputTriplet(visit, "ivec4(", ", ", ")");
1882 case EOpConstructMat2:
1883 addConstructor(node->getType(), "mat2", &node->getSequence());
1884 outputTriplet(visit, "mat2(", ", ", ")");
1886 case EOpConstructMat3:
1887 addConstructor(node->getType(), "mat3", &node->getSequence());
1888 outputTriplet(visit, "mat3(", ", ", ")");
1890 case EOpConstructMat4:
1891 addConstructor(node->getType(), "mat4", &node->getSequence());
1892 outputTriplet(visit, "mat4(", ", ", ")");
1894 case EOpConstructStruct:
1895 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
1896 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
1898 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1899 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1900 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1901 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1902 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1903 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
1906 // We need to look at the number of components in both arguments
1907 switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
1908 + node->getSequence()[1]->getAsTyped()->getNominalSize())
1910 case 11: mUsesMod1 = true; break;
1911 case 22: mUsesMod2v = true; break;
1912 case 21: mUsesMod2f = true; break;
1913 case 33: mUsesMod3v = true; break;
1914 case 31: mUsesMod3f = true; break;
1915 case 44: mUsesMod4v = true; break;
1916 case 41: mUsesMod4f = true; break;
1917 default: UNREACHABLE();
1920 outputTriplet(visit, "mod(", ", ", ")");
1923 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
1925 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
1926 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
1928 case 1: mUsesAtan2_1 = true; break;
1929 case 2: mUsesAtan2_2 = true; break;
1930 case 3: mUsesAtan2_3 = true; break;
1931 case 4: mUsesAtan2_4 = true; break;
1932 default: UNREACHABLE();
1934 outputTriplet(visit, "atanyx(", ", ", ")");
1936 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1937 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1938 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1939 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1940 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1941 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1942 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1943 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1944 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
1945 case EOpFaceForward:
1947 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
1949 case 1: mUsesFaceforward1 = true; break;
1950 case 2: mUsesFaceforward2 = true; break;
1951 case 3: mUsesFaceforward3 = true; break;
1952 case 4: mUsesFaceforward4 = true; break;
1953 default: UNREACHABLE();
1956 outputTriplet(visit, "faceforward(", ", ", ")");
1959 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1960 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1961 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1962 default: UNREACHABLE();
1968 bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1970 TInfoSinkBase &out = mBody;
1972 if (node->usesTernaryOperator())
1974 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1976 else // if/else statement
1978 mUnfoldShortCircuit->traverse(node->getCondition());
1982 node->getCondition()->traverse(this);
1986 outputLineDirective(node->getLine().first_line);
1989 bool discard = false;
1991 if (node->getTrueBlock())
1993 traverseStatements(node->getTrueBlock());
1995 // Detect true discard
1996 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1999 outputLineDirective(node->getLine().first_line);
2002 if (node->getFalseBlock())
2006 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2009 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2010 traverseStatements(node->getFalseBlock());
2012 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2015 // Detect false discard
2016 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2019 // ANGLE issue 486: Detect problematic conditional discard
2020 if (discard && FindSideEffectRewriting::search(node))
2022 mUsesDiscardRewriting = true;
2029 void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2031 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
2034 bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2036 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2038 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
2040 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2043 if (mOutputType == SH_HLSL9_OUTPUT)
2045 if (handleExcessiveLoop(node))
2051 TInfoSinkBase &out = mBody;
2053 if (node->getType() == ELoopDoWhile)
2057 outputLineDirective(node->getLine().first_line);
2064 if (node->getInit())
2066 node->getInit()->traverse(this);
2071 if (node->getCondition())
2073 node->getCondition()->traverse(this);
2078 if (node->getExpression())
2080 node->getExpression()->traverse(this);
2085 outputLineDirective(node->getLine().first_line);
2089 if (node->getBody())
2091 traverseStatements(node->getBody());
2094 outputLineDirective(node->getLine().first_line);
2097 if (node->getType() == ELoopDoWhile)
2099 outputLineDirective(node->getCondition()->getLine().first_line);
2102 node->getCondition()->traverse(this);
2109 mInsideDiscontinuousLoop = wasDiscontinuous;
2114 bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2116 TInfoSinkBase &out = mBody;
2118 switch (node->getFlowOp())
2121 outputTriplet(visit, "discard;\n", "", "");
2124 if (visit == PreVisit)
2126 if (mExcessiveLoopIndex)
2129 mExcessiveLoopIndex->traverse(this);
2130 out << " = true; break;}\n";
2138 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
2140 if (visit == PreVisit)
2142 if (node->getExpression())
2151 else if (visit == PostVisit)
2153 if (node->getExpression())
2159 default: UNREACHABLE();
2165 void OutputHLSL::traverseStatements(TIntermNode *node)
2167 if (isSingleStatement(node))
2169 mUnfoldShortCircuit->traverse(node);
2172 node->traverse(this);
2175 bool OutputHLSL::isSingleStatement(TIntermNode *node)
2177 TIntermAggregate *aggregate = node->getAsAggregate();
2181 if (aggregate->getOp() == EOpSequence)
2187 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2189 if (!isSingleStatement(*sit))
2202 // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2203 // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
2204 bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2206 const int MAX_LOOP_ITERATIONS = 254;
2207 TInfoSinkBase &out = mBody;
2209 // Parse loops of the form:
2210 // for(int index = initial; index [comparator] limit; index += increment)
2211 TIntermSymbol *index = NULL;
2212 TOperator comparator = EOpNull;
2217 // Parse index name and intial value
2218 if (node->getInit())
2220 TIntermAggregate *init = node->getInit()->getAsAggregate();
2224 TIntermSequence &sequence = init->getSequence();
2225 TIntermTyped *variable = sequence[0]->getAsTyped();
2227 if (variable && variable->getQualifier() == EvqTemporary)
2229 TIntermBinary *assign = variable->getAsBinaryNode();
2231 if (assign->getOp() == EOpInitialize)
2233 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2234 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2236 if (symbol && constant)
2238 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2241 initial = constant->getIConst(0);
2249 // Parse comparator and limit value
2250 if (index != NULL && node->getCondition())
2252 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
2254 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2256 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2260 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2262 comparator = test->getOp();
2263 limit = constant->getIConst(0);
2270 if (index != NULL && comparator != EOpNull && node->getExpression())
2272 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2273 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
2277 TOperator op = binaryTerminal->getOp();
2278 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2282 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2284 int value = constant->getIConst(0);
2288 case EOpAddAssign: increment = value; break;
2289 case EOpSubAssign: increment = -value; break;
2290 default: UNIMPLEMENTED();
2295 else if (unaryTerminal)
2297 TOperator op = unaryTerminal->getOp();
2301 case EOpPostIncrement: increment = 1; break;
2302 case EOpPostDecrement: increment = -1; break;
2303 case EOpPreIncrement: increment = 1; break;
2304 case EOpPreDecrement: increment = -1; break;
2305 default: UNIMPLEMENTED();
2310 if (index != NULL && comparator != EOpNull && increment != 0)
2312 if (comparator == EOpLessThanEqual)
2314 comparator = EOpLessThan;
2318 if (comparator == EOpLessThan)
2320 int iterations = (limit - initial) / increment;
2322 if (iterations <= MAX_LOOP_ITERATIONS)
2324 return false; // Not an excessive loop
2327 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2328 mExcessiveLoopIndex = index;
2331 index->traverse(this);
2334 index->traverse(this);
2335 out << " = false;\n";
2337 bool firstLoopFragment = true;
2339 while (iterations > 0)
2341 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
2343 if (!firstLoopFragment)
2345 out << "if (!Break";
2346 index->traverse(this);
2350 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2352 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2355 // for(int index = initial; index < clampedLimit; index += increment)
2358 index->traverse(this);
2363 index->traverse(this);
2365 out << clampedLimit;
2368 index->traverse(this);
2373 outputLineDirective(node->getLine().first_line);
2376 if (node->getBody())
2378 node->getBody()->traverse(this);
2381 outputLineDirective(node->getLine().first_line);
2384 if (!firstLoopFragment)
2389 firstLoopFragment = false;
2391 initial += MAX_LOOP_ITERATIONS * increment;
2392 iterations -= MAX_LOOP_ITERATIONS;
2397 mExcessiveLoopIndex = restoreIndex;
2401 else UNIMPLEMENTED();
2404 return false; // Not handled as an excessive loop
2407 void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
2409 TInfoSinkBase &out = mBody;
2411 if (visit == PreVisit)
2415 else if (visit == InVisit)
2419 else if (visit == PostVisit)
2425 void OutputHLSL::outputLineDirective(int line)
2427 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2430 mBody << "#line " << line;
2432 if (mContext.sourcePath)
2434 mBody << " \"" << mContext.sourcePath << "\"";
2441 TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2443 TQualifier qualifier = symbol->getQualifier();
2444 const TType &type = symbol->getType();
2445 TString name = symbol->getSymbol();
2447 if (name.empty()) // HLSL demands named arguments, also for prototypes
2449 name = "x" + str(mUniqueIndex++);
2453 name = decorate(name);
2456 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2458 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2459 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2462 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
2465 TString OutputHLSL::qualifierString(TQualifier qualifier)
2469 case EvqIn: return "in";
2470 case EvqOut: return "out";
2471 case EvqInOut: return "inout";
2472 case EvqConstReadOnly: return "const";
2473 default: UNREACHABLE();
2479 TString OutputHLSL::typeString(const TType &type)
2481 if (type.getBasicType() == EbtStruct)
2483 const TString& typeName = type.getStruct()->name();
2486 return structLookup(typeName);
2488 else // Nameless structure, define in place
2490 const TFieldList &fields = type.getStruct()->fields();
2492 TString string = "struct\n"
2495 for (unsigned int i = 0; i < fields.size(); i++)
2497 const TField *field = fields[i];
2499 string += " " + typeString(*field->type()) + " " + decorate(field->name()) + arrayString(*field->type()) + ";\n";
2507 else if (type.isMatrix())
2509 switch (type.getNominalSize())
2511 case 2: return "float2x2";
2512 case 3: return "float3x3";
2513 case 4: return "float4x4";
2518 switch (type.getBasicType())
2521 switch (type.getNominalSize())
2523 case 1: return "float";
2524 case 2: return "float2";
2525 case 3: return "float3";
2526 case 4: return "float4";
2529 switch (type.getNominalSize())
2531 case 1: return "int";
2532 case 2: return "int2";
2533 case 3: return "int3";
2534 case 4: return "int4";
2537 switch (type.getNominalSize())
2539 case 1: return "bool";
2540 case 2: return "bool2";
2541 case 3: return "bool3";
2542 case 4: return "bool4";
2548 case EbtSamplerCube:
2549 return "samplerCUBE";
2550 case EbtSamplerExternalOES:
2558 return "<unknown type>";
2561 TString OutputHLSL::textureString(const TType &type)
2563 switch (type.getBasicType())
2567 case EbtSamplerCube:
2568 return "TextureCube";
2569 case EbtSamplerExternalOES:
2576 return "<unknown texture type>";
2579 TString OutputHLSL::arrayString(const TType &type)
2581 if (!type.isArray())
2586 return "[" + str(type.getArraySize()) + "]";
2589 TString OutputHLSL::initializer(const TType &type)
2593 size_t size = type.getObjectSize();
2594 for (size_t component = 0; component < size; component++)
2598 if (component + 1 < size)
2604 return "{" + string + "}";
2607 void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
2611 return; // Nameless structures don't have constructors
2614 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2616 return; // Already added
2619 TType ctorType = type;
2620 ctorType.clearArrayness();
2621 ctorType.setPrecision(EbpHigh);
2622 ctorType.setQualifier(EvqTemporary);
2624 TString ctorName = type.getStruct() ? decorate(name) : name;
2626 typedef std::vector<TType> ParameterArray;
2627 ParameterArray ctorParameters;
2629 if (type.getStruct())
2631 mStructNames.insert(decorate(name));
2634 structure += "struct " + decorate(name) + "\n"
2637 const TFieldList &fields = type.getStruct()->fields();
2639 for (unsigned int i = 0; i < fields.size(); i++)
2641 const TField *field = fields[i];
2643 structure += " " + typeString(*field->type()) + " " + decorateField(field->name(), type) + arrayString(*field->type()) + ";\n";
2646 structure += "};\n";
2648 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
2650 mStructDeclarations.push_back(structure);
2653 for (unsigned int i = 0; i < fields.size(); i++)
2655 ctorParameters.push_back(*fields[i]->type());
2658 else if (parameters)
2660 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2662 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
2667 TString constructor;
2669 if (ctorType.getStruct())
2671 constructor += ctorName + " " + ctorName + "_ctor(";
2673 else // Built-in type
2675 constructor += typeString(ctorType) + " " + ctorName + "(";
2678 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2680 const TType &type = ctorParameters[parameter];
2682 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2684 if (parameter < ctorParameters.size() - 1)
2686 constructor += ", ";
2690 constructor += ")\n"
2693 if (ctorType.getStruct())
2695 constructor += " " + ctorName + " structure = {";
2699 constructor += " return " + typeString(ctorType) + "(";
2702 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2704 int dim = ctorType.getNominalSize();
2705 const TType ¶meter = ctorParameters[0];
2707 if (parameter.isScalar())
2709 for (int row = 0; row < dim; row++)
2711 for (int col = 0; col < dim; col++)
2713 constructor += TString((row == col) ? "x0" : "0.0");
2715 if (row < dim - 1 || col < dim - 1)
2717 constructor += ", ";
2722 else if (parameter.isMatrix())
2724 for (int row = 0; row < dim; row++)
2726 for (int col = 0; col < dim; col++)
2728 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2730 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2734 constructor += TString((row == col) ? "1.0" : "0.0");
2737 if (row < dim - 1 || col < dim - 1)
2739 constructor += ", ";
2748 size_t remainingComponents = ctorType.getObjectSize();
2749 size_t parameterIndex = 0;
2751 while (remainingComponents > 0)
2753 const TType ¶meter = ctorParameters[parameterIndex];
2754 const size_t parameterSize = parameter.getObjectSize();
2755 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
2757 constructor += "x" + str(parameterIndex);
2759 if (parameter.isScalar())
2761 ASSERT(parameterSize <= remainingComponents);
2762 remainingComponents -= parameterSize;
2764 else if (parameter.isVector())
2766 if (remainingComponents == parameterSize || moreParameters)
2768 ASSERT(parameterSize <= remainingComponents);
2769 remainingComponents -= parameterSize;
2771 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
2773 switch (remainingComponents)
2775 case 1: constructor += ".x"; break;
2776 case 2: constructor += ".xy"; break;
2777 case 3: constructor += ".xyz"; break;
2778 case 4: constructor += ".xyzw"; break;
2779 default: UNREACHABLE();
2782 remainingComponents = 0;
2786 else if (parameter.isMatrix() || parameter.getStruct())
2788 ASSERT(remainingComponents == parameterSize || moreParameters);
2789 ASSERT(parameterSize <= remainingComponents);
2791 remainingComponents -= parameterSize;
2800 if (remainingComponents)
2802 constructor += ", ";
2807 if (ctorType.getStruct())
2809 constructor += "};\n"
2810 " return structure;\n"
2815 constructor += ");\n"
2819 mConstructors.insert(constructor);
2822 const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2824 TInfoSinkBase &out = mBody;
2826 if (type.getBasicType() == EbtStruct)
2828 out << structLookup(type.getStruct()->name()) + "_ctor(";
2830 const TFieldList &fields = type.getStruct()->fields();
2832 for (size_t i = 0; i < fields.size(); i++)
2834 const TType *fieldType = fields[i]->type();
2836 constUnion = writeConstantUnion(*fieldType, constUnion);
2838 if (i != fields.size() - 1)
2848 size_t size = type.getObjectSize();
2849 bool writeType = size > 1;
2853 out << typeString(type) << "(";
2856 for (size_t i = 0; i < size; i++, constUnion++)
2858 switch (constUnion->getType())
2860 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
2861 case EbtInt: out << constUnion->getIConst(); break;
2862 case EbtBool: out << constUnion->getBConst(); break;
2863 default: UNREACHABLE();
2881 TString OutputHLSL::scopeString(unsigned int depthLimit)
2885 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2887 string += "_" + str(i);
2893 TString OutputHLSL::scopedStruct(const TString &typeName)
2900 return typeName + scopeString(mScopeDepth);
2903 TString OutputHLSL::structLookup(const TString &typeName)
2905 for (int depth = mScopeDepth; depth >= 0; depth--)
2907 TString scopedName = decorate(typeName + scopeString(depth));
2909 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2911 if (*structName == scopedName)
2918 UNREACHABLE(); // Should have found a matching constructor
2923 TString OutputHLSL::decorate(const TString &string)
2925 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
2927 return "_" + string;
2933 TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
2935 if (type.getBasicType() == EbtSamplerExternalOES)
2937 return "ex_" + string;
2940 return decorate(string);
2943 TString OutputHLSL::decorateField(const TString &string, const TType &structure)
2945 if (structure.getStruct()->name().compare(0, 3, "gl_") != 0)
2947 return decorate(string);
2953 TString OutputHLSL::registerString(TIntermSymbol *operand)
2955 ASSERT(operand->getQualifier() == EvqUniform);
2957 if (IsSampler(operand->getBasicType()))
2959 return "s" + str(samplerRegister(operand));
2962 return "c" + str(uniformRegister(operand));
2965 int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
2967 const TType &type = sampler->getType();
2968 ASSERT(IsSampler(type.getBasicType()));
2970 int index = mSamplerRegister;
2971 mSamplerRegister += sampler->totalRegisterCount();
2973 declareUniform(type, sampler->getSymbol(), index);
2978 int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
2980 const TType &type = uniform->getType();
2981 ASSERT(!IsSampler(type.getBasicType()));
2983 int index = mUniformRegister;
2984 mUniformRegister += uniform->totalRegisterCount();
2986 declareUniform(type, uniform->getSymbol(), index);
2991 void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
2993 TStructure *structure = type.getStruct();
2997 mActiveUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), index));
3001 const TFieldList &fields = structure->fields();
3005 int elementIndex = index;
3007 for (int i = 0; i < type.getArraySize(); i++)
3009 for (size_t j = 0; j < fields.size(); j++)
3011 const TType &fieldType = *fields[j]->type();
3012 const TString uniformName = name + "[" + str(i) + "]." + fields[j]->name();
3013 declareUniform(fieldType, uniformName, elementIndex);
3014 elementIndex += fieldType.totalRegisterCount();
3020 int fieldIndex = index;
3022 for (size_t i = 0; i < fields.size(); i++)
3024 const TType &fieldType = *fields[i]->type();
3025 const TString uniformName = name + "." + fields[i]->name();
3026 declareUniform(fieldType, uniformName, fieldIndex);
3027 fieldIndex += fieldType.totalRegisterCount();
3033 GLenum OutputHLSL::glVariableType(const TType &type)
3035 if (type.getBasicType() == EbtFloat)
3037 if (type.isScalar())
3041 else if (type.isVector())
3043 switch(type.getNominalSize())
3045 case 2: return GL_FLOAT_VEC2;
3046 case 3: return GL_FLOAT_VEC3;
3047 case 4: return GL_FLOAT_VEC4;
3048 default: UNREACHABLE();
3051 else if (type.isMatrix())
3053 switch(type.getNominalSize())
3055 case 2: return GL_FLOAT_MAT2;
3056 case 3: return GL_FLOAT_MAT3;
3057 case 4: return GL_FLOAT_MAT4;
3058 default: UNREACHABLE();
3063 else if (type.getBasicType() == EbtInt)
3065 if (type.isScalar())
3069 else if (type.isVector())
3071 switch(type.getNominalSize())
3073 case 2: return GL_INT_VEC2;
3074 case 3: return GL_INT_VEC3;
3075 case 4: return GL_INT_VEC4;
3076 default: UNREACHABLE();
3081 else if (type.getBasicType() == EbtBool)
3083 if (type.isScalar())
3087 else if (type.isVector())
3089 switch(type.getNominalSize())
3091 case 2: return GL_BOOL_VEC2;
3092 case 3: return GL_BOOL_VEC3;
3093 case 4: return GL_BOOL_VEC4;
3094 default: UNREACHABLE();
3099 else if (type.getBasicType() == EbtSampler2D)
3101 return GL_SAMPLER_2D;
3103 else if (type.getBasicType() == EbtSamplerCube)
3105 return GL_SAMPLER_CUBE;
3112 GLenum OutputHLSL::glVariablePrecision(const TType &type)
3114 if (type.getBasicType() == EbtFloat)
3116 switch (type.getPrecision())
3118 case EbpHigh: return GL_HIGH_FLOAT;
3119 case EbpMedium: return GL_MEDIUM_FLOAT;
3120 case EbpLow: return GL_LOW_FLOAT;
3122 // Should be defined as the default precision by the parser
3123 default: UNREACHABLE();
3126 else if (type.getBasicType() == EbtInt)
3128 switch (type.getPrecision())
3130 case EbpHigh: return GL_HIGH_INT;
3131 case EbpMedium: return GL_MEDIUM_INT;
3132 case EbpLow: return GL_LOW_INT;
3134 // Should be defined as the default precision by the parser
3135 default: UNREACHABLE();
3139 // Other types (boolean, sampler) don't have a precision