gfx/angle/src/compiler/OutputHLSL.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/compiler/OutputHLSL.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,3094 @@
     1.4 +//
     1.5 +// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
     1.6 +// Use of this source code is governed by a BSD-style license that can be
     1.7 +// found in the LICENSE file.
     1.8 +//
     1.9 +
    1.10 +#include "compiler/OutputHLSL.h"
    1.11 +
    1.12 +#include "common/angleutils.h"
    1.13 +#include "compiler/compiler_debug.h"
    1.14 +#include "compiler/DetectDiscontinuity.h"
    1.15 +#include "compiler/InfoSink.h"
    1.16 +#include "compiler/SearchSymbol.h"
    1.17 +#include "compiler/UnfoldShortCircuit.h"
    1.18 +
    1.19 +#include <algorithm>
    1.20 +#include <cfloat>
    1.21 +#include <stdio.h>
    1.22 +
    1.23 +namespace sh
    1.24 +{
    1.25 +// Integer to TString conversion
    1.26 +TString str(int i)
    1.27 +{
    1.28 +    char buffer[20];
    1.29 +    snprintf(buffer, sizeof(buffer), "%d", i);
    1.30 +    return buffer;
    1.31 +}
    1.32 +
    1.33 +OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
    1.34 +    : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
    1.35 +{
    1.36 +    mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
    1.37 +    mInsideFunction = false;
    1.38 +
    1.39 +    mUsesTexture2D = false;
    1.40 +    mUsesTexture2D_bias = false;
    1.41 +    mUsesTexture2DProj = false;
    1.42 +    mUsesTexture2DProj_bias = false;
    1.43 +    mUsesTexture2DProjLod = false;
    1.44 +    mUsesTexture2DLod = false;
    1.45 +    mUsesTextureCube = false;
    1.46 +    mUsesTextureCube_bias = false;
    1.47 +    mUsesTextureCubeLod = false;
    1.48 +    mUsesTexture2DLod0 = false;
    1.49 +    mUsesTexture2DLod0_bias = false;
    1.50 +    mUsesTexture2DProjLod0 = false;
    1.51 +    mUsesTexture2DProjLod0_bias = false;
    1.52 +    mUsesTextureCubeLod0 = false;
    1.53 +    mUsesTextureCubeLod0_bias = false;
    1.54 +    mUsesFragColor = false;
    1.55 +    mUsesFragData = false;
    1.56 +    mUsesDepthRange = false;
    1.57 +    mUsesFragCoord = false;
    1.58 +    mUsesPointCoord = false;
    1.59 +    mUsesFrontFacing = false;
    1.60 +    mUsesPointSize = false;
    1.61 +    mUsesFragDepth = false;
    1.62 +    mUsesXor = false;
    1.63 +    mUsesMod1 = false;
    1.64 +    mUsesMod2v = false;
    1.65 +    mUsesMod2f = false;
    1.66 +    mUsesMod3v = false;
    1.67 +    mUsesMod3f = false;
    1.68 +    mUsesMod4v = false;
    1.69 +    mUsesMod4f = false;
    1.70 +    mUsesFaceforward1 = false;
    1.71 +    mUsesFaceforward2 = false;
    1.72 +    mUsesFaceforward3 = false;
    1.73 +    mUsesFaceforward4 = false;
    1.74 +    mUsesAtan2_1 = false;
    1.75 +    mUsesAtan2_2 = false;
    1.76 +    mUsesAtan2_3 = false;
    1.77 +    mUsesAtan2_4 = false;
    1.78 +
    1.79 +    mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
    1.80 +
    1.81 +    mScopeDepth = 0;
    1.82 +
    1.83 +    mUniqueIndex = 0;
    1.84 +
    1.85 +    mContainsLoopDiscontinuity = false;
    1.86 +    mOutputLod0Function = false;
    1.87 +    mInsideDiscontinuousLoop = false;
    1.88 +
    1.89 +    mExcessiveLoopIndex = NULL;
    1.90 +
    1.91 +    if (mOutputType == SH_HLSL9_OUTPUT)
    1.92 +    {
    1.93 +        if (mContext.shaderType == SH_FRAGMENT_SHADER)
    1.94 +        {
    1.95 +            mUniformRegister = 3;   // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
    1.96 +        }
    1.97 +        else
    1.98 +        {
    1.99 +            mUniformRegister = 2;   // Reserve registers for dx_DepthRange and dx_ViewAdjust
   1.100 +        }
   1.101 +    }
   1.102 +    else
   1.103 +    {
   1.104 +        mUniformRegister = 0;
   1.105 +    }
   1.106 +
   1.107 +    mSamplerRegister = 0;
   1.108 +}
   1.109 +
   1.110 +OutputHLSL::~OutputHLSL()
   1.111 +{
   1.112 +    delete mUnfoldShortCircuit;
   1.113 +}
   1.114 +
   1.115 +void OutputHLSL::output()
   1.116 +{
   1.117 +    mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
   1.118 +
   1.119 +    mContext.treeRoot->traverse(this);   // Output the body first to determine what has to go in the header
   1.120 +    header();
   1.121 +
   1.122 +    mContext.infoSink().obj << mHeader.c_str();
   1.123 +    mContext.infoSink().obj << mBody.c_str();
   1.124 +}
   1.125 +
   1.126 +TInfoSinkBase &OutputHLSL::getBodyStream()
   1.127 +{
   1.128 +    return mBody;
   1.129 +}
   1.130 +
   1.131 +const ActiveUniforms &OutputHLSL::getUniforms()
   1.132 +{
   1.133 +    return mActiveUniforms;
   1.134 +}
   1.135 +
   1.136 +int OutputHLSL::vectorSize(const TType &type) const
   1.137 +{
   1.138 +    int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
   1.139 +    int arraySize = type.isArray() ? type.getArraySize() : 1;
   1.140 +
   1.141 +    return elementSize * arraySize;
   1.142 +}
   1.143 +
   1.144 +void OutputHLSL::header()
   1.145 +{
   1.146 +    ShShaderType shaderType = mContext.shaderType;
   1.147 +    TInfoSinkBase &out = mHeader;
   1.148 +
   1.149 +    for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
   1.150 +    {
   1.151 +        out << *structDeclaration;
   1.152 +    }
   1.153 +
   1.154 +    for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
   1.155 +    {
   1.156 +        out << *constructor;
   1.157 +    }
   1.158 +
   1.159 +    TString uniforms;
   1.160 +    TString varyings;
   1.161 +    TString attributes;
   1.162 +
   1.163 +    for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
   1.164 +    {
   1.165 +        const TType &type = uniform->second->getType();
   1.166 +        const TString &name = uniform->second->getSymbol();
   1.167 +
   1.168 +        if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))   // Also declare the texture
   1.169 +        {
   1.170 +            int index = samplerRegister(mReferencedUniforms[name]);
   1.171 +
   1.172 +            uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) + 
   1.173 +                        " : register(s" + str(index) + ");\n";
   1.174 +
   1.175 +            uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) + 
   1.176 +                        " : register(t" + str(index) + ");\n";
   1.177 +        }
   1.178 +        else
   1.179 +        {
   1.180 +            uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + 
   1.181 +                        " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
   1.182 +        }
   1.183 +    }
   1.184 +
   1.185 +    for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
   1.186 +    {
   1.187 +        const TType &type = varying->second->getType();
   1.188 +        const TString &name = varying->second->getSymbol();
   1.189 +
   1.190 +        // Program linking depends on this exact format
   1.191 +        varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
   1.192 +    }
   1.193 +
   1.194 +    for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
   1.195 +    {
   1.196 +        const TType &type = attribute->second->getType();
   1.197 +        const TString &name = attribute->second->getSymbol();
   1.198 +
   1.199 +        attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
   1.200 +    }
   1.201 +
   1.202 +    if (shaderType == SH_FRAGMENT_SHADER)
   1.203 +    {
   1.204 +        TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
   1.205 +        const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
   1.206 +
   1.207 +        const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
   1.208 +
   1.209 +        out << "// Varyings\n";
   1.210 +        out <<  varyings;
   1.211 +        out << "\n"
   1.212 +               "static float4 gl_Color[" << numColorValues << "] =\n"
   1.213 +               "{\n";
   1.214 +        for (unsigned int i = 0; i < numColorValues; i++)
   1.215 +        {
   1.216 +            out << "    float4(0, 0, 0, 0)";
   1.217 +            if (i + 1 != numColorValues)
   1.218 +            {
   1.219 +                out << ",";
   1.220 +            }
   1.221 +            out << "\n";
   1.222 +        }
   1.223 +        out << "};\n";
   1.224 +
   1.225 +        if (mUsesFragDepth)
   1.226 +        {
   1.227 +            out << "static float gl_Depth = 0.0;\n";
   1.228 +        }
   1.229 +
   1.230 +        if (mUsesFragCoord)
   1.231 +        {
   1.232 +            out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
   1.233 +        }
   1.234 +
   1.235 +        if (mUsesPointCoord)
   1.236 +        {
   1.237 +            out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
   1.238 +        }
   1.239 +
   1.240 +        if (mUsesFrontFacing)
   1.241 +        {
   1.242 +            out << "static bool gl_FrontFacing = false;\n";
   1.243 +        }
   1.244 +
   1.245 +        out << "\n";
   1.246 +
   1.247 +        if (mUsesDepthRange)
   1.248 +        {
   1.249 +            out << "struct gl_DepthRangeParameters\n"
   1.250 +                   "{\n"
   1.251 +                   "    float near;\n"
   1.252 +                   "    float far;\n"
   1.253 +                   "    float diff;\n"
   1.254 +                   "};\n"
   1.255 +                   "\n";
   1.256 +        }
   1.257 +
   1.258 +        if (mOutputType == SH_HLSL11_OUTPUT)
   1.259 +        {
   1.260 +            out << "cbuffer DriverConstants : register(b1)\n"
   1.261 +                   "{\n";
   1.262 +
   1.263 +            if (mUsesDepthRange)
   1.264 +            {
   1.265 +                out << "    float3 dx_DepthRange : packoffset(c0);\n";
   1.266 +            }
   1.267 +
   1.268 +            if (mUsesFragCoord)
   1.269 +            {
   1.270 +                out << "    float4 dx_ViewCoords : packoffset(c1);\n";
   1.271 +            }
   1.272 +
   1.273 +            if (mUsesFragCoord || mUsesFrontFacing)
   1.274 +            {
   1.275 +                out << "    float3 dx_DepthFront : packoffset(c2);\n";
   1.276 +            }
   1.277 +
   1.278 +            out << "};\n";
   1.279 +        }
   1.280 +        else
   1.281 +        {
   1.282 +            if (mUsesDepthRange)
   1.283 +            {
   1.284 +                out << "uniform float3 dx_DepthRange : register(c0);";
   1.285 +            }
   1.286 +
   1.287 +            if (mUsesFragCoord)
   1.288 +            {
   1.289 +                out << "uniform float4 dx_ViewCoords : register(c1);\n";
   1.290 +            }
   1.291 +
   1.292 +            if (mUsesFragCoord || mUsesFrontFacing)
   1.293 +            {
   1.294 +                out << "uniform float3 dx_DepthFront : register(c2);\n";
   1.295 +            }
   1.296 +        }
   1.297 +
   1.298 +        out << "\n";
   1.299 +
   1.300 +        if (mUsesDepthRange)
   1.301 +        {
   1.302 +            out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
   1.303 +                   "\n";
   1.304 +        }
   1.305 +        
   1.306 +        out <<  uniforms;
   1.307 +        out << "\n";
   1.308 +
   1.309 +        if (mUsesTexture2D)
   1.310 +        {
   1.311 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.312 +            {
   1.313 +                out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
   1.314 +                       "{\n"
   1.315 +                       "    return tex2D(s, t);\n"
   1.316 +                       "}\n"
   1.317 +                       "\n";
   1.318 +            }
   1.319 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.320 +            {
   1.321 +                out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
   1.322 +                       "{\n"
   1.323 +                       "    return t.Sample(s, uv);\n"
   1.324 +                       "}\n"
   1.325 +                       "\n";
   1.326 +            }
   1.327 +            else UNREACHABLE();
   1.328 +        }
   1.329 +
   1.330 +        if (mUsesTexture2D_bias)
   1.331 +        {
   1.332 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.333 +            {
   1.334 +                out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
   1.335 +                       "{\n"
   1.336 +                       "    return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
   1.337 +                       "}\n"
   1.338 +                       "\n";
   1.339 +            }
   1.340 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.341 +            {
   1.342 +                out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv, float bias)\n"
   1.343 +                       "{\n"
   1.344 +                       "    return t.SampleBias(s, uv, bias);\n"
   1.345 +                       "}\n"
   1.346 +                       "\n";
   1.347 +            }
   1.348 +            else UNREACHABLE();
   1.349 +        }
   1.350 +
   1.351 +        if (mUsesTexture2DProj)
   1.352 +        {
   1.353 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.354 +            {
   1.355 +                out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
   1.356 +                       "{\n"
   1.357 +                       "    return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
   1.358 +                       "}\n"
   1.359 +                       "\n"
   1.360 +                       "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
   1.361 +                       "{\n"
   1.362 +                       "    return tex2Dproj(s, t);\n"
   1.363 +                       "}\n"
   1.364 +                       "\n";
   1.365 +            }
   1.366 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.367 +            {
   1.368 +                out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
   1.369 +                       "{\n"
   1.370 +                       "    return t.Sample(s, float2(uvw.x / uvw.z, uvw.y / uvw.z));\n"
   1.371 +                       "}\n"
   1.372 +                       "\n"
   1.373 +                       "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
   1.374 +                       "{\n"
   1.375 +                       "    return t.Sample(s, float2(uvw.x / uvw.w, uvw.y / uvw.w));\n"
   1.376 +                       "}\n"
   1.377 +                       "\n";
   1.378 +            }
   1.379 +            else UNREACHABLE();
   1.380 +        }
   1.381 +
   1.382 +        if (mUsesTexture2DProj_bias)
   1.383 +        {
   1.384 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.385 +            {
   1.386 +                out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
   1.387 +                       "{\n"
   1.388 +                       "    return tex2Dbias(s, float4(t.x / t.z, t.y / t.z, 0, bias));\n"
   1.389 +                       "}\n"
   1.390 +                       "\n"
   1.391 +                       "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
   1.392 +                       "{\n"
   1.393 +                       "    return tex2Dbias(s, float4(t.x / t.w, t.y / t.w, 0, bias));\n"
   1.394 +                       "}\n"
   1.395 +                       "\n";
   1.396 +            }
   1.397 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.398 +            {
   1.399 +                out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
   1.400 +                       "{\n"
   1.401 +                       "    return t.SampleBias(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), bias);\n"
   1.402 +                       "}\n"
   1.403 +                       "\n"
   1.404 +                       "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
   1.405 +                       "{\n"
   1.406 +                       "    return t.SampleBias(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), bias);\n"
   1.407 +                       "}\n"
   1.408 +                       "\n";
   1.409 +            }
   1.410 +            else UNREACHABLE();
   1.411 +        }
   1.412 +
   1.413 +        if (mUsesTextureCube)
   1.414 +        {
   1.415 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.416 +            {
   1.417 +                out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
   1.418 +                       "{\n"
   1.419 +                       "    return texCUBE(s, t);\n"
   1.420 +                       "}\n"
   1.421 +                       "\n";
   1.422 +            }
   1.423 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.424 +            {
   1.425 +                out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
   1.426 +                       "{\n"
   1.427 +                       "    return t.Sample(s, uvw);\n"
   1.428 +                       "}\n"
   1.429 +                       "\n";
   1.430 +            }
   1.431 +            else UNREACHABLE();
   1.432 +        }
   1.433 +
   1.434 +        if (mUsesTextureCube_bias)
   1.435 +        {
   1.436 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.437 +            {
   1.438 +                out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
   1.439 +                       "{\n"
   1.440 +                       "    return texCUBEbias(s, float4(t.x, t.y, t.z, bias));\n"
   1.441 +                       "}\n"
   1.442 +                       "\n";
   1.443 +            }
   1.444 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.445 +            {
   1.446 +                out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
   1.447 +                       "{\n"
   1.448 +                       "    return t.SampleBias(s, uvw, bias);\n"
   1.449 +                       "}\n"
   1.450 +                       "\n";
   1.451 +            }
   1.452 +            else UNREACHABLE();
   1.453 +        }
   1.454 +
   1.455 +        // These *Lod0 intrinsics are not available in GL fragment shaders.
   1.456 +        // They are used to sample using discontinuous texture coordinates.
   1.457 +        if (mUsesTexture2DLod0)
   1.458 +        {
   1.459 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.460 +            {
   1.461 +                out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
   1.462 +                       "{\n"
   1.463 +                       "    return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
   1.464 +                       "}\n"
   1.465 +                       "\n";
   1.466 +            }
   1.467 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.468 +            {
   1.469 +                out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv)\n"
   1.470 +                       "{\n"
   1.471 +                       "    return t.SampleLevel(s, uv, 0);\n"
   1.472 +                       "}\n"
   1.473 +                       "\n";
   1.474 +            }
   1.475 +            else UNREACHABLE();
   1.476 +        }
   1.477 +
   1.478 +        if (mUsesTexture2DLod0_bias)
   1.479 +        {
   1.480 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.481 +            {
   1.482 +                out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
   1.483 +                       "{\n"
   1.484 +                       "    return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
   1.485 +                       "}\n"
   1.486 +                       "\n";
   1.487 +            }
   1.488 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.489 +            {
   1.490 +                out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv, float bias)\n"
   1.491 +                       "{\n"
   1.492 +                       "    return t.SampleLevel(s, uv, 0);\n"
   1.493 +                       "}\n"
   1.494 +                       "\n";
   1.495 +            }
   1.496 +            else UNREACHABLE();
   1.497 +        }
   1.498 +
   1.499 +        if (mUsesTexture2DProjLod0)
   1.500 +        {
   1.501 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.502 +            {
   1.503 +                out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
   1.504 +                       "{\n"
   1.505 +                       "    return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
   1.506 +                       "}\n"
   1.507 +                       "\n"
   1.508 +                       "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
   1.509 +                       "{\n"
   1.510 +                       "    return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
   1.511 +                       "}\n"
   1.512 +                       "\n";
   1.513 +            }
   1.514 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.515 +            {
   1.516 +                out << "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float3 uvw)\n"
   1.517 +                       "{\n"
   1.518 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
   1.519 +                       "}\n"
   1.520 +                       "\n"
   1.521 +                       "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float4 uvw)\n"
   1.522 +                       "{\n"
   1.523 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
   1.524 +                       "}\n"
   1.525 +                       "\n";
   1.526 +            }
   1.527 +            else UNREACHABLE();
   1.528 +        }
   1.529 +
   1.530 +        if (mUsesTexture2DProjLod0_bias)
   1.531 +        {
   1.532 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.533 +            {
   1.534 +                out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
   1.535 +                       "{\n"
   1.536 +                       "    return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
   1.537 +                       "}\n"
   1.538 +                       "\n"
   1.539 +                       "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
   1.540 +                       "{\n"
   1.541 +                       "    return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
   1.542 +                       "}\n"
   1.543 +                       "\n";
   1.544 +            }
   1.545 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.546 +            {
   1.547 +                out << "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
   1.548 +                       "{\n"
   1.549 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
   1.550 +                       "}\n"
   1.551 +                       "\n"
   1.552 +                       "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
   1.553 +                       "{\n"
   1.554 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
   1.555 +                       "}\n"
   1.556 +                       "\n";
   1.557 +            }
   1.558 +            else UNREACHABLE();
   1.559 +        }
   1.560 +
   1.561 +        if (mUsesTextureCubeLod0)
   1.562 +        {
   1.563 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.564 +            {
   1.565 +                out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
   1.566 +                       "{\n"
   1.567 +                       "    return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
   1.568 +                       "}\n"
   1.569 +                       "\n";
   1.570 +            }
   1.571 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.572 +            {
   1.573 +                out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw)\n"
   1.574 +                       "{\n"
   1.575 +                       "    return t.SampleLevel(s, uvw, 0);\n"
   1.576 +                       "}\n"
   1.577 +                       "\n";
   1.578 +            }
   1.579 +            else UNREACHABLE();
   1.580 +        }
   1.581 +
   1.582 +        if (mUsesTextureCubeLod0_bias)
   1.583 +        {
   1.584 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.585 +            {
   1.586 +                out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
   1.587 +                       "{\n"
   1.588 +                       "    return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
   1.589 +                       "}\n"
   1.590 +                       "\n";
   1.591 +            }
   1.592 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.593 +            {
   1.594 +                out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
   1.595 +                       "{\n"
   1.596 +                       "    return t.SampleLevel(s, uvw, 0);\n"
   1.597 +                       "}\n"
   1.598 +                       "\n";
   1.599 +            }
   1.600 +            else UNREACHABLE();
   1.601 +        }
   1.602 +
   1.603 +        if (usingMRTExtension && mNumRenderTargets > 1)
   1.604 +        {
   1.605 +            out << "#define GL_USES_MRT\n";
   1.606 +        }
   1.607 +
   1.608 +        if (mUsesFragColor)
   1.609 +        {
   1.610 +            out << "#define GL_USES_FRAG_COLOR\n";
   1.611 +        }
   1.612 +
   1.613 +        if (mUsesFragData)
   1.614 +        {
   1.615 +            out << "#define GL_USES_FRAG_DATA\n";
   1.616 +        }
   1.617 +    }
   1.618 +    else   // Vertex shader
   1.619 +    {
   1.620 +        out << "// Attributes\n";
   1.621 +        out <<  attributes;
   1.622 +        out << "\n"
   1.623 +               "static float4 gl_Position = float4(0, 0, 0, 0);\n";
   1.624 +        
   1.625 +        if (mUsesPointSize)
   1.626 +        {
   1.627 +            out << "static float gl_PointSize = float(1);\n";
   1.628 +        }
   1.629 +
   1.630 +        out << "\n"
   1.631 +               "// Varyings\n";
   1.632 +        out <<  varyings;
   1.633 +        out << "\n";
   1.634 +
   1.635 +        if (mUsesDepthRange)
   1.636 +        {
   1.637 +            out << "struct gl_DepthRangeParameters\n"
   1.638 +                   "{\n"
   1.639 +                   "    float near;\n"
   1.640 +                   "    float far;\n"
   1.641 +                   "    float diff;\n"
   1.642 +                   "};\n"
   1.643 +                   "\n";
   1.644 +        }
   1.645 +
   1.646 +        if (mOutputType == SH_HLSL11_OUTPUT)
   1.647 +        {
   1.648 +            if (mUsesDepthRange)
   1.649 +            {
   1.650 +                out << "cbuffer DriverConstants : register(b1)\n"
   1.651 +                       "{\n"
   1.652 +                       "    float3 dx_DepthRange : packoffset(c0);\n"
   1.653 +                       "};\n"
   1.654 +                       "\n";
   1.655 +            }
   1.656 +        }
   1.657 +        else
   1.658 +        {
   1.659 +            if (mUsesDepthRange)
   1.660 +            {
   1.661 +                out << "uniform float3 dx_DepthRange : register(c0);\n";
   1.662 +            }
   1.663 +
   1.664 +            out << "uniform float4 dx_ViewAdjust : register(c1);\n"
   1.665 +                   "\n";
   1.666 +        }
   1.667 +
   1.668 +        if (mUsesDepthRange)
   1.669 +        {
   1.670 +            out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
   1.671 +                   "\n";
   1.672 +        }
   1.673 +
   1.674 +        out << uniforms;
   1.675 +        out << "\n";
   1.676 +        
   1.677 +        if (mUsesTexture2D)
   1.678 +        {
   1.679 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.680 +            {
   1.681 +                out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
   1.682 +                       "{\n"
   1.683 +                       "    return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
   1.684 +                       "}\n"
   1.685 +                       "\n";
   1.686 +            }
   1.687 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.688 +            {
   1.689 +                out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
   1.690 +                       "{\n"
   1.691 +                       "    return t.SampleLevel(s, uv, 0);\n"
   1.692 +                       "}\n"
   1.693 +                       "\n";
   1.694 +            }
   1.695 +            else UNREACHABLE();
   1.696 +        }
   1.697 +
   1.698 +        if (mUsesTexture2DLod)
   1.699 +        {
   1.700 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.701 +            {
   1.702 +                out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
   1.703 +                       "{\n"
   1.704 +                       "    return tex2Dlod(s, float4(t.x, t.y, 0, lod));\n"
   1.705 +                       "}\n"
   1.706 +                       "\n";
   1.707 +            }
   1.708 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.709 +            {
   1.710 +                out << "float4 gl_texture2DLod(Texture2D t, SamplerState s, float2 uv, float lod)\n"
   1.711 +                       "{\n"
   1.712 +                       "    return t.SampleLevel(s, uv, lod);\n"
   1.713 +                       "}\n"
   1.714 +                       "\n";
   1.715 +            }
   1.716 +            else UNREACHABLE();
   1.717 +        }
   1.718 +
   1.719 +        if (mUsesTexture2DProj)
   1.720 +        {
   1.721 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.722 +            {
   1.723 +                out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
   1.724 +                       "{\n"
   1.725 +                       "    return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
   1.726 +                       "}\n"
   1.727 +                       "\n"
   1.728 +                       "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
   1.729 +                       "{\n"
   1.730 +                       "    return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
   1.731 +                       "}\n"
   1.732 +                       "\n";
   1.733 +            }
   1.734 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.735 +            {
   1.736 +                out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
   1.737 +                       "{\n"
   1.738 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
   1.739 +                       "}\n"
   1.740 +                       "\n"
   1.741 +                       "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
   1.742 +                       "{\n"
   1.743 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
   1.744 +                       "}\n"
   1.745 +                       "\n";
   1.746 +            }
   1.747 +            else UNREACHABLE();
   1.748 +        }
   1.749 +
   1.750 +        if (mUsesTexture2DProjLod)
   1.751 +        {
   1.752 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.753 +            {
   1.754 +                out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
   1.755 +                       "{\n"
   1.756 +                       "    return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, lod));\n"
   1.757 +                       "}\n"
   1.758 +                       "\n"
   1.759 +                       "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
   1.760 +                       "{\n"
   1.761 +                       "    return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, lod));\n"
   1.762 +                       "}\n"
   1.763 +                       "\n";
   1.764 +            }
   1.765 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.766 +            {
   1.767 +                out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float lod)\n"
   1.768 +                       "{\n"
   1.769 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), lod);\n"
   1.770 +                       "}\n"
   1.771 +                       "\n"
   1.772 +                       "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
   1.773 +                       "{\n"
   1.774 +                       "    return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), lod);\n"
   1.775 +                       "}\n"
   1.776 +                       "\n";
   1.777 +            }
   1.778 +            else UNREACHABLE();
   1.779 +        }
   1.780 +
   1.781 +        if (mUsesTextureCube)
   1.782 +        {
   1.783 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.784 +            {
   1.785 +                out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
   1.786 +                       "{\n"
   1.787 +                       "    return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
   1.788 +                       "}\n"
   1.789 +                       "\n";
   1.790 +            }
   1.791 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.792 +            {
   1.793 +                out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
   1.794 +                       "{\n"
   1.795 +                       "    return t.SampleLevel(s, uvw, 0);\n"
   1.796 +                       "}\n"
   1.797 +                       "\n";
   1.798 +            }
   1.799 +            else UNREACHABLE();
   1.800 +        }
   1.801 +
   1.802 +        if (mUsesTextureCubeLod)
   1.803 +        {
   1.804 +            if (mOutputType == SH_HLSL9_OUTPUT)
   1.805 +            {
   1.806 +                out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
   1.807 +                       "{\n"
   1.808 +                       "    return texCUBElod(s, float4(t.x, t.y, t.z, lod));\n"
   1.809 +                       "}\n"
   1.810 +                       "\n";
   1.811 +            }
   1.812 +            else if (mOutputType == SH_HLSL11_OUTPUT)
   1.813 +            {
   1.814 +                out << "float4 gl_textureCubeLod(TextureCube t, SamplerState s, float3 uvw, float lod)\n"
   1.815 +                       "{\n"
   1.816 +                       "    return t.SampleLevel(s, uvw, lod);\n"
   1.817 +                       "}\n"
   1.818 +                       "\n";
   1.819 +            }
   1.820 +            else UNREACHABLE();
   1.821 +        }
   1.822 +    }
   1.823 +
   1.824 +    if (mUsesFragCoord)
   1.825 +    {
   1.826 +        out << "#define GL_USES_FRAG_COORD\n";
   1.827 +    }
   1.828 +
   1.829 +    if (mUsesPointCoord)
   1.830 +    {
   1.831 +        out << "#define GL_USES_POINT_COORD\n";
   1.832 +    }
   1.833 +
   1.834 +    if (mUsesFrontFacing)
   1.835 +    {
   1.836 +        out << "#define GL_USES_FRONT_FACING\n";
   1.837 +    }
   1.838 +
   1.839 +    if (mUsesPointSize)
   1.840 +    {
   1.841 +        out << "#define GL_USES_POINT_SIZE\n";
   1.842 +    }
   1.843 +
   1.844 +    if (mUsesFragDepth)
   1.845 +    {
   1.846 +        out << "#define GL_USES_FRAG_DEPTH\n";
   1.847 +    }
   1.848 +
   1.849 +    if (mUsesDepthRange)
   1.850 +    {
   1.851 +        out << "#define GL_USES_DEPTH_RANGE\n";
   1.852 +    }
   1.853 +
   1.854 +    if (mUsesXor)
   1.855 +    {
   1.856 +        out << "bool xor(bool p, bool q)\n"
   1.857 +               "{\n"
   1.858 +               "    return (p || q) && !(p && q);\n"
   1.859 +               "}\n"
   1.860 +               "\n";
   1.861 +    }
   1.862 +
   1.863 +    if (mUsesMod1)
   1.864 +    {
   1.865 +        out << "float mod(float x, float y)\n"
   1.866 +               "{\n"
   1.867 +               "    return x - y * floor(x / y);\n"
   1.868 +               "}\n"
   1.869 +               "\n";
   1.870 +    }
   1.871 +
   1.872 +    if (mUsesMod2v)
   1.873 +    {
   1.874 +        out << "float2 mod(float2 x, float2 y)\n"
   1.875 +               "{\n"
   1.876 +               "    return x - y * floor(x / y);\n"
   1.877 +               "}\n"
   1.878 +               "\n";
   1.879 +    }
   1.880 +
   1.881 +    if (mUsesMod2f)
   1.882 +    {
   1.883 +        out << "float2 mod(float2 x, float y)\n"
   1.884 +               "{\n"
   1.885 +               "    return x - y * floor(x / y);\n"
   1.886 +               "}\n"
   1.887 +               "\n";
   1.888 +    }
   1.889 +    
   1.890 +    if (mUsesMod3v)
   1.891 +    {
   1.892 +        out << "float3 mod(float3 x, float3 y)\n"
   1.893 +               "{\n"
   1.894 +               "    return x - y * floor(x / y);\n"
   1.895 +               "}\n"
   1.896 +               "\n";
   1.897 +    }
   1.898 +
   1.899 +    if (mUsesMod3f)
   1.900 +    {
   1.901 +        out << "float3 mod(float3 x, float y)\n"
   1.902 +               "{\n"
   1.903 +               "    return x - y * floor(x / y);\n"
   1.904 +               "}\n"
   1.905 +               "\n";
   1.906 +    }
   1.907 +
   1.908 +    if (mUsesMod4v)
   1.909 +    {
   1.910 +        out << "float4 mod(float4 x, float4 y)\n"
   1.911 +               "{\n"
   1.912 +               "    return x - y * floor(x / y);\n"
   1.913 +               "}\n"
   1.914 +               "\n";
   1.915 +    }
   1.916 +
   1.917 +    if (mUsesMod4f)
   1.918 +    {
   1.919 +        out << "float4 mod(float4 x, float y)\n"
   1.920 +               "{\n"
   1.921 +               "    return x - y * floor(x / y);\n"
   1.922 +               "}\n"
   1.923 +               "\n";
   1.924 +    }
   1.925 +
   1.926 +    if (mUsesFaceforward1)
   1.927 +    {
   1.928 +        out << "float faceforward(float N, float I, float Nref)\n"
   1.929 +               "{\n"
   1.930 +               "    if(dot(Nref, I) >= 0)\n"
   1.931 +               "    {\n"
   1.932 +               "        return -N;\n"
   1.933 +               "    }\n"
   1.934 +               "    else\n"
   1.935 +               "    {\n"
   1.936 +               "        return N;\n"
   1.937 +               "    }\n"
   1.938 +               "}\n"
   1.939 +               "\n";
   1.940 +    }
   1.941 +
   1.942 +    if (mUsesFaceforward2)
   1.943 +    {
   1.944 +        out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
   1.945 +               "{\n"
   1.946 +               "    if(dot(Nref, I) >= 0)\n"
   1.947 +               "    {\n"
   1.948 +               "        return -N;\n"
   1.949 +               "    }\n"
   1.950 +               "    else\n"
   1.951 +               "    {\n"
   1.952 +               "        return N;\n"
   1.953 +               "    }\n"
   1.954 +               "}\n"
   1.955 +               "\n";
   1.956 +    }
   1.957 +
   1.958 +    if (mUsesFaceforward3)
   1.959 +    {
   1.960 +        out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
   1.961 +               "{\n"
   1.962 +               "    if(dot(Nref, I) >= 0)\n"
   1.963 +               "    {\n"
   1.964 +               "        return -N;\n"
   1.965 +               "    }\n"
   1.966 +               "    else\n"
   1.967 +               "    {\n"
   1.968 +               "        return N;\n"
   1.969 +               "    }\n"
   1.970 +               "}\n"
   1.971 +               "\n";
   1.972 +    }
   1.973 +
   1.974 +    if (mUsesFaceforward4)
   1.975 +    {
   1.976 +        out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
   1.977 +               "{\n"
   1.978 +               "    if(dot(Nref, I) >= 0)\n"
   1.979 +               "    {\n"
   1.980 +               "        return -N;\n"
   1.981 +               "    }\n"
   1.982 +               "    else\n"
   1.983 +               "    {\n"
   1.984 +               "        return N;\n"
   1.985 +               "    }\n"
   1.986 +               "}\n"
   1.987 +               "\n";
   1.988 +    }
   1.989 +
   1.990 +    if (mUsesAtan2_1)
   1.991 +    {
   1.992 +        out << "float atanyx(float y, float x)\n"
   1.993 +               "{\n"
   1.994 +               "    if(x == 0 && y == 0) x = 1;\n"   // Avoid producing a NaN
   1.995 +               "    return atan2(y, x);\n"
   1.996 +               "}\n";
   1.997 +    }
   1.998 +
   1.999 +    if (mUsesAtan2_2)
  1.1000 +    {
  1.1001 +        out << "float2 atanyx(float2 y, float2 x)\n"
  1.1002 +               "{\n"
  1.1003 +               "    if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
  1.1004 +               "    if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
  1.1005 +               "    return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
  1.1006 +               "}\n";
  1.1007 +    }
  1.1008 +
  1.1009 +    if (mUsesAtan2_3)
  1.1010 +    {
  1.1011 +        out << "float3 atanyx(float3 y, float3 x)\n"
  1.1012 +               "{\n"
  1.1013 +               "    if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
  1.1014 +               "    if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
  1.1015 +               "    if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
  1.1016 +               "    return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
  1.1017 +               "}\n";
  1.1018 +    }
  1.1019 +
  1.1020 +    if (mUsesAtan2_4)
  1.1021 +    {
  1.1022 +        out << "float4 atanyx(float4 y, float4 x)\n"
  1.1023 +               "{\n"
  1.1024 +               "    if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
  1.1025 +               "    if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
  1.1026 +               "    if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
  1.1027 +               "    if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
  1.1028 +               "    return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
  1.1029 +               "}\n";
  1.1030 +    }
  1.1031 +}
  1.1032 +
  1.1033 +void OutputHLSL::visitSymbol(TIntermSymbol *node)
  1.1034 +{
  1.1035 +    TInfoSinkBase &out = mBody;
  1.1036 +
  1.1037 +    TString name = node->getSymbol();
  1.1038 +
  1.1039 +    if (name == "gl_FragColor")
  1.1040 +    {
  1.1041 +        out << "gl_Color[0]";
  1.1042 +        mUsesFragColor = true;
  1.1043 +    }
  1.1044 +    else if (name == "gl_FragData")
  1.1045 +    {
  1.1046 +        out << "gl_Color";
  1.1047 +        mUsesFragData = true;
  1.1048 +    }
  1.1049 +    else if (name == "gl_DepthRange")
  1.1050 +    {
  1.1051 +        mUsesDepthRange = true;
  1.1052 +        out << name;
  1.1053 +    }
  1.1054 +    else if (name == "gl_FragCoord")
  1.1055 +    {
  1.1056 +        mUsesFragCoord = true;
  1.1057 +        out << name;
  1.1058 +    }
  1.1059 +    else if (name == "gl_PointCoord")
  1.1060 +    {
  1.1061 +        mUsesPointCoord = true;
  1.1062 +        out << name;
  1.1063 +    }
  1.1064 +    else if (name == "gl_FrontFacing")
  1.1065 +    {
  1.1066 +        mUsesFrontFacing = true;
  1.1067 +        out << name;
  1.1068 +    }
  1.1069 +    else if (name == "gl_PointSize")
  1.1070 +    {
  1.1071 +        mUsesPointSize = true;
  1.1072 +        out << name;
  1.1073 +    }
  1.1074 +    else if (name == "gl_FragDepthEXT")
  1.1075 +    {
  1.1076 +        mUsesFragDepth = true;
  1.1077 +        out << "gl_Depth";
  1.1078 +    }
  1.1079 +    else
  1.1080 +    {
  1.1081 +        TQualifier qualifier = node->getQualifier();
  1.1082 +
  1.1083 +        if (qualifier == EvqUniform)
  1.1084 +        {
  1.1085 +            mReferencedUniforms[name] = node;
  1.1086 +            out << decorateUniform(name, node->getType());
  1.1087 +        }
  1.1088 +        else if (qualifier == EvqAttribute)
  1.1089 +        {
  1.1090 +            mReferencedAttributes[name] = node;
  1.1091 +            out << decorate(name);
  1.1092 +        }
  1.1093 +        else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
  1.1094 +        {
  1.1095 +            mReferencedVaryings[name] = node;
  1.1096 +            out << decorate(name);
  1.1097 +        }
  1.1098 +        else
  1.1099 +        {
  1.1100 +            out << decorate(name);
  1.1101 +        }
  1.1102 +    }
  1.1103 +}
  1.1104 +
  1.1105 +bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
  1.1106 +{
  1.1107 +    TInfoSinkBase &out = mBody;
  1.1108 +
  1.1109 +    switch (node->getOp())
  1.1110 +    {
  1.1111 +      case EOpAssign:                  outputTriplet(visit, "(", " = ", ")");           break;
  1.1112 +      case EOpInitialize:
  1.1113 +        if (visit == PreVisit)
  1.1114 +        {
  1.1115 +            // GLSL allows to write things like "float x = x;" where a new variable x is defined
  1.1116 +            // and the value of an existing variable x is assigned. HLSL uses C semantics (the
  1.1117 +            // new variable is created before the assignment is evaluated), so we need to convert
  1.1118 +            // this to "float t = x, x = t;".
  1.1119 +
  1.1120 +            TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
  1.1121 +            TIntermTyped *expression = node->getRight();
  1.1122 +
  1.1123 +            sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
  1.1124 +            expression->traverse(&searchSymbol);
  1.1125 +            bool sameSymbol = searchSymbol.foundMatch();
  1.1126 +
  1.1127 +            if (sameSymbol)
  1.1128 +            {
  1.1129 +                // Type already printed
  1.1130 +                out << "t" + str(mUniqueIndex) + " = ";
  1.1131 +                expression->traverse(this);
  1.1132 +                out << ", ";
  1.1133 +                symbolNode->traverse(this);
  1.1134 +                out << " = t" + str(mUniqueIndex);
  1.1135 +
  1.1136 +                mUniqueIndex++;
  1.1137 +                return false;
  1.1138 +            }
  1.1139 +        }
  1.1140 +        else if (visit == InVisit)
  1.1141 +        {
  1.1142 +            out << " = ";
  1.1143 +        }
  1.1144 +        break;
  1.1145 +      case EOpAddAssign:               outputTriplet(visit, "(", " += ", ")");          break;
  1.1146 +      case EOpSubAssign:               outputTriplet(visit, "(", " -= ", ")");          break;
  1.1147 +      case EOpMulAssign:               outputTriplet(visit, "(", " *= ", ")");          break;
  1.1148 +      case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")");          break;
  1.1149 +      case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")");          break;
  1.1150 +      case EOpVectorTimesMatrixAssign:
  1.1151 +        if (visit == PreVisit)
  1.1152 +        {
  1.1153 +            out << "(";
  1.1154 +        }
  1.1155 +        else if (visit == InVisit)
  1.1156 +        {
  1.1157 +            out << " = mul(";
  1.1158 +            node->getLeft()->traverse(this);
  1.1159 +            out << ", transpose(";   
  1.1160 +        }
  1.1161 +        else
  1.1162 +        {
  1.1163 +            out << ")))";
  1.1164 +        }
  1.1165 +        break;
  1.1166 +      case EOpMatrixTimesMatrixAssign:
  1.1167 +        if (visit == PreVisit)
  1.1168 +        {
  1.1169 +            out << "(";
  1.1170 +        }
  1.1171 +        else if (visit == InVisit)
  1.1172 +        {
  1.1173 +            out << " = mul(";
  1.1174 +            node->getLeft()->traverse(this);
  1.1175 +            out << ", ";   
  1.1176 +        }
  1.1177 +        else
  1.1178 +        {
  1.1179 +            out << "))";
  1.1180 +        }
  1.1181 +        break;
  1.1182 +      case EOpDivAssign:               outputTriplet(visit, "(", " /= ", ")");          break;
  1.1183 +      case EOpIndexDirect:             outputTriplet(visit, "", "[", "]");              break;
  1.1184 +      case EOpIndexIndirect:           outputTriplet(visit, "", "[", "]");              break;
  1.1185 +      case EOpIndexDirectStruct:
  1.1186 +        if (visit == InVisit)
  1.1187 +        {
  1.1188 +            const TStructure* structure = node->getLeft()->getType().getStruct();
  1.1189 +            const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
  1.1190 +            const TField* field = structure->fields()[index->getIConst(0)];
  1.1191 +            out << "." + decorateField(field->name(), node->getLeft()->getType());
  1.1192 +
  1.1193 +            return false;
  1.1194 +        }
  1.1195 +        break;
  1.1196 +      case EOpVectorSwizzle:
  1.1197 +        if (visit == InVisit)
  1.1198 +        {
  1.1199 +            out << ".";
  1.1200 +
  1.1201 +            TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
  1.1202 +
  1.1203 +            if (swizzle)
  1.1204 +            {
  1.1205 +                TIntermSequence &sequence = swizzle->getSequence();
  1.1206 +
  1.1207 +                for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
  1.1208 +                {
  1.1209 +                    TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
  1.1210 +
  1.1211 +                    if (element)
  1.1212 +                    {
  1.1213 +                        int i = element->getIConst(0);
  1.1214 +
  1.1215 +                        switch (i)
  1.1216 +                        {
  1.1217 +                        case 0: out << "x"; break;
  1.1218 +                        case 1: out << "y"; break;
  1.1219 +                        case 2: out << "z"; break;
  1.1220 +                        case 3: out << "w"; break;
  1.1221 +                        default: UNREACHABLE();
  1.1222 +                        }
  1.1223 +                    }
  1.1224 +                    else UNREACHABLE();
  1.1225 +                }
  1.1226 +            }
  1.1227 +            else UNREACHABLE();
  1.1228 +
  1.1229 +            return false;   // Fully processed
  1.1230 +        }
  1.1231 +        break;
  1.1232 +      case EOpAdd:               outputTriplet(visit, "(", " + ", ")"); break;
  1.1233 +      case EOpSub:               outputTriplet(visit, "(", " - ", ")"); break;
  1.1234 +      case EOpMul:               outputTriplet(visit, "(", " * ", ")"); break;
  1.1235 +      case EOpDiv:               outputTriplet(visit, "(", " / ", ")"); break;
  1.1236 +      case EOpEqual:
  1.1237 +      case EOpNotEqual:
  1.1238 +        if (node->getLeft()->isScalar())
  1.1239 +        {
  1.1240 +            if (node->getOp() == EOpEqual)
  1.1241 +            {
  1.1242 +                outputTriplet(visit, "(", " == ", ")");
  1.1243 +            }
  1.1244 +            else
  1.1245 +            {
  1.1246 +                outputTriplet(visit, "(", " != ", ")");
  1.1247 +            }
  1.1248 +        }
  1.1249 +        else if (node->getLeft()->getBasicType() == EbtStruct)
  1.1250 +        {
  1.1251 +            if (node->getOp() == EOpEqual)
  1.1252 +            {
  1.1253 +                out << "(";
  1.1254 +            }
  1.1255 +            else
  1.1256 +            {
  1.1257 +                out << "!(";
  1.1258 +            }
  1.1259 +
  1.1260 +            const TFieldList &fields = node->getLeft()->getType().getStruct()->fields();
  1.1261 +
  1.1262 +            for (size_t i = 0; i < fields.size(); i++)
  1.1263 +            {
  1.1264 +                const TField *field = fields[i];
  1.1265 +
  1.1266 +                node->getLeft()->traverse(this);
  1.1267 +                out << "." + decorateField(field->name(), node->getLeft()->getType()) + " == ";
  1.1268 +                node->getRight()->traverse(this);
  1.1269 +                out << "." + decorateField(field->name(), node->getLeft()->getType());
  1.1270 +
  1.1271 +                if (i < fields.size() - 1)
  1.1272 +                {
  1.1273 +                    out << " && ";
  1.1274 +                }
  1.1275 +            }
  1.1276 +
  1.1277 +            out << ")";
  1.1278 +
  1.1279 +            return false;
  1.1280 +        }
  1.1281 +        else
  1.1282 +        {
  1.1283 +            ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
  1.1284 +
  1.1285 +            if (node->getOp() == EOpEqual)
  1.1286 +            {
  1.1287 +                outputTriplet(visit, "all(", " == ", ")");
  1.1288 +            }
  1.1289 +            else
  1.1290 +            {
  1.1291 +                outputTriplet(visit, "!all(", " == ", ")");
  1.1292 +            }
  1.1293 +        }
  1.1294 +        break;
  1.1295 +      case EOpLessThan:          outputTriplet(visit, "(", " < ", ")");   break;
  1.1296 +      case EOpGreaterThan:       outputTriplet(visit, "(", " > ", ")");   break;
  1.1297 +      case EOpLessThanEqual:     outputTriplet(visit, "(", " <= ", ")");  break;
  1.1298 +      case EOpGreaterThanEqual:  outputTriplet(visit, "(", " >= ", ")");  break;
  1.1299 +      case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")");   break;
  1.1300 +      case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")");   break;
  1.1301 +      case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
  1.1302 +      case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
  1.1303 +      case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
  1.1304 +      case EOpLogicalOr:
  1.1305 +        out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
  1.1306 +        return false;
  1.1307 +      case EOpLogicalXor:
  1.1308 +        mUsesXor = true;
  1.1309 +        outputTriplet(visit, "xor(", ", ", ")");
  1.1310 +        break;
  1.1311 +      case EOpLogicalAnd:
  1.1312 +        out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
  1.1313 +        return false;
  1.1314 +      default: UNREACHABLE();
  1.1315 +    }
  1.1316 +
  1.1317 +    return true;
  1.1318 +}
  1.1319 +
  1.1320 +bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
  1.1321 +{
  1.1322 +    switch (node->getOp())
  1.1323 +    {
  1.1324 +      case EOpNegative:         outputTriplet(visit, "(-", "", ")");  break;
  1.1325 +      case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")");  break;
  1.1326 +      case EOpLogicalNot:       outputTriplet(visit, "(!", "", ")");  break;
  1.1327 +      case EOpPostIncrement:    outputTriplet(visit, "(", "", "++)"); break;
  1.1328 +      case EOpPostDecrement:    outputTriplet(visit, "(", "", "--)"); break;
  1.1329 +      case EOpPreIncrement:     outputTriplet(visit, "(++", "", ")"); break;
  1.1330 +      case EOpPreDecrement:     outputTriplet(visit, "(--", "", ")"); break;
  1.1331 +      case EOpConvIntToBool:
  1.1332 +      case EOpConvFloatToBool:
  1.1333 +        switch (node->getOperand()->getType().getNominalSize())
  1.1334 +        {
  1.1335 +          case 1:    outputTriplet(visit, "bool(", "", ")");  break;
  1.1336 +          case 2:    outputTriplet(visit, "bool2(", "", ")"); break;
  1.1337 +          case 3:    outputTriplet(visit, "bool3(", "", ")"); break;
  1.1338 +          case 4:    outputTriplet(visit, "bool4(", "", ")"); break;
  1.1339 +          default: UNREACHABLE();
  1.1340 +        }
  1.1341 +        break;
  1.1342 +      case EOpConvBoolToFloat:
  1.1343 +      case EOpConvIntToFloat:
  1.1344 +        switch (node->getOperand()->getType().getNominalSize())
  1.1345 +        {
  1.1346 +          case 1:    outputTriplet(visit, "float(", "", ")");  break;
  1.1347 +          case 2:    outputTriplet(visit, "float2(", "", ")"); break;
  1.1348 +          case 3:    outputTriplet(visit, "float3(", "", ")"); break;
  1.1349 +          case 4:    outputTriplet(visit, "float4(", "", ")"); break;
  1.1350 +          default: UNREACHABLE();
  1.1351 +        }
  1.1352 +        break;
  1.1353 +      case EOpConvFloatToInt:
  1.1354 +      case EOpConvBoolToInt:
  1.1355 +        switch (node->getOperand()->getType().getNominalSize())
  1.1356 +        {
  1.1357 +          case 1:    outputTriplet(visit, "int(", "", ")");  break;
  1.1358 +          case 2:    outputTriplet(visit, "int2(", "", ")"); break;
  1.1359 +          case 3:    outputTriplet(visit, "int3(", "", ")"); break;
  1.1360 +          case 4:    outputTriplet(visit, "int4(", "", ")"); break;
  1.1361 +          default: UNREACHABLE();
  1.1362 +        }
  1.1363 +        break;
  1.1364 +      case EOpRadians:          outputTriplet(visit, "radians(", "", ")");   break;
  1.1365 +      case EOpDegrees:          outputTriplet(visit, "degrees(", "", ")");   break;
  1.1366 +      case EOpSin:              outputTriplet(visit, "sin(", "", ")");       break;
  1.1367 +      case EOpCos:              outputTriplet(visit, "cos(", "", ")");       break;
  1.1368 +      case EOpTan:              outputTriplet(visit, "tan(", "", ")");       break;
  1.1369 +      case EOpAsin:             outputTriplet(visit, "asin(", "", ")");      break;
  1.1370 +      case EOpAcos:             outputTriplet(visit, "acos(", "", ")");      break;
  1.1371 +      case EOpAtan:             outputTriplet(visit, "atan(", "", ")");      break;
  1.1372 +      case EOpExp:              outputTriplet(visit, "exp(", "", ")");       break;
  1.1373 +      case EOpLog:              outputTriplet(visit, "log(", "", ")");       break;
  1.1374 +      case EOpExp2:             outputTriplet(visit, "exp2(", "", ")");      break;
  1.1375 +      case EOpLog2:             outputTriplet(visit, "log2(", "", ")");      break;
  1.1376 +      case EOpSqrt:             outputTriplet(visit, "sqrt(", "", ")");      break;
  1.1377 +      case EOpInverseSqrt:      outputTriplet(visit, "rsqrt(", "", ")");     break;
  1.1378 +      case EOpAbs:              outputTriplet(visit, "abs(", "", ")");       break;
  1.1379 +      case EOpSign:             outputTriplet(visit, "sign(", "", ")");      break;
  1.1380 +      case EOpFloor:            outputTriplet(visit, "floor(", "", ")");     break;
  1.1381 +      case EOpCeil:             outputTriplet(visit, "ceil(", "", ")");      break;
  1.1382 +      case EOpFract:            outputTriplet(visit, "frac(", "", ")");      break;
  1.1383 +      case EOpLength:           outputTriplet(visit, "length(", "", ")");    break;
  1.1384 +      case EOpNormalize:        outputTriplet(visit, "normalize(", "", ")"); break;
  1.1385 +      case EOpDFdx:
  1.1386 +        if(mInsideDiscontinuousLoop || mOutputLod0Function)
  1.1387 +        {
  1.1388 +            outputTriplet(visit, "(", "", ", 0.0)");
  1.1389 +        }
  1.1390 +        else
  1.1391 +        {
  1.1392 +            outputTriplet(visit, "ddx(", "", ")");
  1.1393 +        }
  1.1394 +        break;
  1.1395 +      case EOpDFdy:
  1.1396 +        if(mInsideDiscontinuousLoop || mOutputLod0Function)
  1.1397 +        {
  1.1398 +            outputTriplet(visit, "(", "", ", 0.0)");
  1.1399 +        }
  1.1400 +        else
  1.1401 +        {
  1.1402 +           outputTriplet(visit, "ddy(", "", ")");
  1.1403 +        }
  1.1404 +        break;
  1.1405 +      case EOpFwidth:
  1.1406 +        if(mInsideDiscontinuousLoop || mOutputLod0Function)
  1.1407 +        {
  1.1408 +            outputTriplet(visit, "(", "", ", 0.0)");
  1.1409 +        }
  1.1410 +        else
  1.1411 +        {
  1.1412 +            outputTriplet(visit, "fwidth(", "", ")");
  1.1413 +        }
  1.1414 +        break;
  1.1415 +      case EOpAny:              outputTriplet(visit, "any(", "", ")");       break;
  1.1416 +      case EOpAll:              outputTriplet(visit, "all(", "", ")");       break;
  1.1417 +      default: UNREACHABLE();
  1.1418 +    }
  1.1419 +
  1.1420 +    return true;
  1.1421 +}
  1.1422 +
  1.1423 +bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
  1.1424 +{
  1.1425 +    TInfoSinkBase &out = mBody;
  1.1426 +
  1.1427 +    switch (node->getOp())
  1.1428 +    {
  1.1429 +      case EOpSequence:
  1.1430 +        {
  1.1431 +            if (mInsideFunction)
  1.1432 +            {
  1.1433 +                outputLineDirective(node->getLine().first_line);
  1.1434 +                out << "{\n";
  1.1435 +
  1.1436 +                mScopeDepth++;
  1.1437 +
  1.1438 +                if (mScopeBracket.size() < mScopeDepth)
  1.1439 +                {
  1.1440 +                    mScopeBracket.push_back(0);   // New scope level
  1.1441 +                }
  1.1442 +                else
  1.1443 +                {
  1.1444 +                    mScopeBracket[mScopeDepth - 1]++;   // New scope at existing level
  1.1445 +                }
  1.1446 +            }
  1.1447 +
  1.1448 +            for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
  1.1449 +            {
  1.1450 +                outputLineDirective((*sit)->getLine().first_line);
  1.1451 +
  1.1452 +                traverseStatements(*sit);
  1.1453 +
  1.1454 +                out << ";\n";
  1.1455 +            }
  1.1456 +
  1.1457 +            if (mInsideFunction)
  1.1458 +            {
  1.1459 +                outputLineDirective(node->getLine().last_line);
  1.1460 +                out << "}\n";
  1.1461 +
  1.1462 +                mScopeDepth--;
  1.1463 +            }
  1.1464 +
  1.1465 +            return false;
  1.1466 +        }
  1.1467 +      case EOpDeclaration:
  1.1468 +        if (visit == PreVisit)
  1.1469 +        {
  1.1470 +            TIntermSequence &sequence = node->getSequence();
  1.1471 +            TIntermTyped *variable = sequence[0]->getAsTyped();
  1.1472 +
  1.1473 +            if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
  1.1474 +            {
  1.1475 +                if (variable->getType().getStruct())
  1.1476 +                {
  1.1477 +                    addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
  1.1478 +                }
  1.1479 +
  1.1480 +                if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "")   // Variable declaration
  1.1481 +                {
  1.1482 +                    if (!mInsideFunction)
  1.1483 +                    {
  1.1484 +                        out << "static ";
  1.1485 +                    }
  1.1486 +
  1.1487 +                    out << typeString(variable->getType()) + " ";
  1.1488 +
  1.1489 +                    for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
  1.1490 +                    {
  1.1491 +                        TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
  1.1492 +
  1.1493 +                        if (symbol)
  1.1494 +                        {
  1.1495 +                            symbol->traverse(this);
  1.1496 +                            out << arrayString(symbol->getType());
  1.1497 +                            out << " = " + initializer(variable->getType());
  1.1498 +                        }
  1.1499 +                        else
  1.1500 +                        {
  1.1501 +                            (*sit)->traverse(this);
  1.1502 +                        }
  1.1503 +
  1.1504 +                        if (*sit != sequence.back())
  1.1505 +                        {
  1.1506 +                            out << ", ";
  1.1507 +                        }
  1.1508 +                    }
  1.1509 +                }
  1.1510 +                else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "")   // Type (struct) declaration
  1.1511 +                {
  1.1512 +                    // Already added to constructor map
  1.1513 +                }
  1.1514 +                else UNREACHABLE();
  1.1515 +            }
  1.1516 +            else if (variable && (variable->getQualifier() == EvqVaryingOut || variable->getQualifier() == EvqInvariantVaryingOut))
  1.1517 +            {
  1.1518 +                for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
  1.1519 +                {
  1.1520 +                    TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
  1.1521 +
  1.1522 +                    if (symbol)
  1.1523 +                    {
  1.1524 +                        // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
  1.1525 +                        mReferencedVaryings[symbol->getSymbol()] = symbol;
  1.1526 +                    }
  1.1527 +                    else
  1.1528 +                    {
  1.1529 +                        (*sit)->traverse(this);
  1.1530 +                    }
  1.1531 +                }
  1.1532 +            }
  1.1533 +
  1.1534 +            return false;
  1.1535 +        }
  1.1536 +        else if (visit == InVisit)
  1.1537 +        {
  1.1538 +            out << ", ";
  1.1539 +        }
  1.1540 +        break;
  1.1541 +      case EOpPrototype:
  1.1542 +        if (visit == PreVisit)
  1.1543 +        {
  1.1544 +            out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
  1.1545 +
  1.1546 +            TIntermSequence &arguments = node->getSequence();
  1.1547 +
  1.1548 +            for (unsigned int i = 0; i < arguments.size(); i++)
  1.1549 +            {
  1.1550 +                TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
  1.1551 +
  1.1552 +                if (symbol)
  1.1553 +                {
  1.1554 +                    out << argumentString(symbol);
  1.1555 +
  1.1556 +                    if (i < arguments.size() - 1)
  1.1557 +                    {
  1.1558 +                        out << ", ";
  1.1559 +                    }
  1.1560 +                }
  1.1561 +                else UNREACHABLE();
  1.1562 +            }
  1.1563 +
  1.1564 +            out << ");\n";
  1.1565 +
  1.1566 +            // Also prototype the Lod0 variant if needed
  1.1567 +            if (mContainsLoopDiscontinuity && !mOutputLod0Function)
  1.1568 +            {
  1.1569 +                mOutputLod0Function = true;
  1.1570 +                node->traverse(this);
  1.1571 +                mOutputLod0Function = false;
  1.1572 +            }
  1.1573 +
  1.1574 +            return false;
  1.1575 +        }
  1.1576 +        break;
  1.1577 +      case EOpComma:            outputTriplet(visit, "(", ", ", ")");                break;
  1.1578 +      case EOpFunction:
  1.1579 +        {
  1.1580 +            TString name = TFunction::unmangleName(node->getName());
  1.1581 +
  1.1582 +            out << typeString(node->getType()) << " ";
  1.1583 +
  1.1584 +            if (name == "main")
  1.1585 +            {
  1.1586 +                out << "gl_main(";
  1.1587 +            }
  1.1588 +            else
  1.1589 +            {
  1.1590 +                out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
  1.1591 +            }
  1.1592 +
  1.1593 +            TIntermSequence &sequence = node->getSequence();
  1.1594 +            TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
  1.1595 +
  1.1596 +            for (unsigned int i = 0; i < arguments.size(); i++)
  1.1597 +            {
  1.1598 +                TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
  1.1599 +
  1.1600 +                if (symbol)
  1.1601 +                {
  1.1602 +                    if (symbol->getType().getStruct())
  1.1603 +                    {
  1.1604 +                        addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
  1.1605 +                    }
  1.1606 +
  1.1607 +                    out << argumentString(symbol);
  1.1608 +
  1.1609 +                    if (i < arguments.size() - 1)
  1.1610 +                    {
  1.1611 +                        out << ", ";
  1.1612 +                    }
  1.1613 +                }
  1.1614 +                else UNREACHABLE();
  1.1615 +            }
  1.1616 +
  1.1617 +            out << ")\n"
  1.1618 +                "{\n";
  1.1619 +            
  1.1620 +            if (sequence.size() > 1)
  1.1621 +            {
  1.1622 +                mInsideFunction = true;
  1.1623 +                sequence[1]->traverse(this);
  1.1624 +                mInsideFunction = false;
  1.1625 +            }
  1.1626 +            
  1.1627 +            out << "}\n";
  1.1628 +
  1.1629 +            if (mContainsLoopDiscontinuity && !mOutputLod0Function)
  1.1630 +            {
  1.1631 +                if (name != "main")
  1.1632 +                {
  1.1633 +                    mOutputLod0Function = true;
  1.1634 +                    node->traverse(this);
  1.1635 +                    mOutputLod0Function = false;
  1.1636 +                }
  1.1637 +            }
  1.1638 +
  1.1639 +            return false;
  1.1640 +        }
  1.1641 +        break;
  1.1642 +      case EOpFunctionCall:
  1.1643 +        {
  1.1644 +            TString name = TFunction::unmangleName(node->getName());
  1.1645 +            bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
  1.1646 +
  1.1647 +            if (node->isUserDefined())
  1.1648 +            {
  1.1649 +                out << decorate(name) << (lod0 ? "Lod0(" : "(");
  1.1650 +            }
  1.1651 +            else
  1.1652 +            {
  1.1653 +                if (name == "texture2D")
  1.1654 +                {
  1.1655 +                    if (!lod0)
  1.1656 +                    {
  1.1657 +                        if (node->getSequence().size() == 2)
  1.1658 +                        {
  1.1659 +                            mUsesTexture2D = true;
  1.1660 +                        }
  1.1661 +                        else if (node->getSequence().size() == 3)
  1.1662 +                        {
  1.1663 +                            mUsesTexture2D_bias = true;
  1.1664 +                        }
  1.1665 +                        else UNREACHABLE();
  1.1666 +
  1.1667 +                        out << "gl_texture2D(";
  1.1668 +                    }
  1.1669 +                    else
  1.1670 +                    {
  1.1671 +                        if (node->getSequence().size() == 2)
  1.1672 +                        {
  1.1673 +                            mUsesTexture2DLod0 = true;
  1.1674 +                        }
  1.1675 +                        else if (node->getSequence().size() == 3)
  1.1676 +                        {
  1.1677 +                            mUsesTexture2DLod0_bias = true;
  1.1678 +                        }
  1.1679 +                        else UNREACHABLE();
  1.1680 +
  1.1681 +                        out << "gl_texture2DLod0(";
  1.1682 +                    }
  1.1683 +                }
  1.1684 +                else if (name == "texture2DProj")
  1.1685 +                {
  1.1686 +                    if (!lod0)
  1.1687 +                    {
  1.1688 +                        if (node->getSequence().size() == 2)
  1.1689 +                        {
  1.1690 +                            mUsesTexture2DProj = true;
  1.1691 +                        }
  1.1692 +                        else if (node->getSequence().size() == 3)
  1.1693 +                        {
  1.1694 +                            mUsesTexture2DProj_bias = true;
  1.1695 +                        }
  1.1696 +                        else UNREACHABLE();
  1.1697 +
  1.1698 +                        out << "gl_texture2DProj(";
  1.1699 +                    }
  1.1700 +                    else
  1.1701 +                    {
  1.1702 +                        if (node->getSequence().size() == 2)
  1.1703 +                        {
  1.1704 +                            mUsesTexture2DProjLod0 = true;
  1.1705 +                        }
  1.1706 +                        else if (node->getSequence().size() == 3)
  1.1707 +                        {
  1.1708 +                            mUsesTexture2DProjLod0_bias = true;
  1.1709 +                        }
  1.1710 +                        else UNREACHABLE();
  1.1711 +
  1.1712 +                        out << "gl_texture2DProjLod0(";
  1.1713 +                    }
  1.1714 +                }
  1.1715 +                else if (name == "textureCube")
  1.1716 +                {
  1.1717 +                    if (!lod0)
  1.1718 +                    {
  1.1719 +                        if (node->getSequence().size() == 2)
  1.1720 +                        {
  1.1721 +                            mUsesTextureCube = true;
  1.1722 +                        }
  1.1723 +                        else if (node->getSequence().size() == 3)
  1.1724 +                        {
  1.1725 +                            mUsesTextureCube_bias = true;
  1.1726 +                        }
  1.1727 +                        else UNREACHABLE();
  1.1728 +
  1.1729 +                        out << "gl_textureCube(";
  1.1730 +                    }
  1.1731 +                    else
  1.1732 +                    {
  1.1733 +                        if (node->getSequence().size() == 2)
  1.1734 +                        {
  1.1735 +                            mUsesTextureCubeLod0 = true;
  1.1736 +                        }
  1.1737 +                        else if (node->getSequence().size() == 3)
  1.1738 +                        {
  1.1739 +                            mUsesTextureCubeLod0_bias = true;
  1.1740 +                        }
  1.1741 +                        else UNREACHABLE();
  1.1742 +
  1.1743 +                        out << "gl_textureCubeLod0(";
  1.1744 +                    }
  1.1745 +                }
  1.1746 +                else if (name == "texture2DLod")
  1.1747 +                {
  1.1748 +                    if (node->getSequence().size() == 3)
  1.1749 +                    {
  1.1750 +                        mUsesTexture2DLod = true;
  1.1751 +                    }
  1.1752 +                    else UNREACHABLE();
  1.1753 +
  1.1754 +                    out << "gl_texture2DLod(";
  1.1755 +                }
  1.1756 +                else if (name == "texture2DProjLod")
  1.1757 +                {
  1.1758 +                    if (node->getSequence().size() == 3)
  1.1759 +                    {
  1.1760 +                        mUsesTexture2DProjLod = true;
  1.1761 +                    }
  1.1762 +                    else UNREACHABLE();
  1.1763 +
  1.1764 +                    out << "gl_texture2DProjLod(";
  1.1765 +                }
  1.1766 +                else if (name == "textureCubeLod")
  1.1767 +                {
  1.1768 +                    if (node->getSequence().size() == 3)
  1.1769 +                    {
  1.1770 +                        mUsesTextureCubeLod = true;
  1.1771 +                    }
  1.1772 +                    else UNREACHABLE();
  1.1773 +
  1.1774 +                    out << "gl_textureCubeLod(";
  1.1775 +                }
  1.1776 +                else UNREACHABLE();
  1.1777 +            }
  1.1778 +
  1.1779 +            TIntermSequence &arguments = node->getSequence();
  1.1780 +
  1.1781 +            for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
  1.1782 +            {
  1.1783 +                if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
  1.1784 +                {
  1.1785 +                    out << "texture_";
  1.1786 +                    (*arg)->traverse(this);
  1.1787 +                    out << ", sampler_";
  1.1788 +                }
  1.1789 +
  1.1790 +                (*arg)->traverse(this);
  1.1791 +
  1.1792 +                if (arg < arguments.end() - 1)
  1.1793 +                {
  1.1794 +                    out << ", ";
  1.1795 +                }
  1.1796 +            }
  1.1797 +
  1.1798 +            out << ")";
  1.1799 +
  1.1800 +            return false;
  1.1801 +        }
  1.1802 +        break;
  1.1803 +      case EOpParameters:       outputTriplet(visit, "(", ", ", ")\n{\n");             break;
  1.1804 +      case EOpConstructFloat:
  1.1805 +        addConstructor(node->getType(), "vec1", &node->getSequence());
  1.1806 +        outputTriplet(visit, "vec1(", "", ")");
  1.1807 +        break;
  1.1808 +      case EOpConstructVec2:
  1.1809 +        addConstructor(node->getType(), "vec2", &node->getSequence());
  1.1810 +        outputTriplet(visit, "vec2(", ", ", ")");
  1.1811 +        break;
  1.1812 +      case EOpConstructVec3:
  1.1813 +        addConstructor(node->getType(), "vec3", &node->getSequence());
  1.1814 +        outputTriplet(visit, "vec3(", ", ", ")");
  1.1815 +        break;
  1.1816 +      case EOpConstructVec4:
  1.1817 +        addConstructor(node->getType(), "vec4", &node->getSequence());
  1.1818 +        outputTriplet(visit, "vec4(", ", ", ")");
  1.1819 +        break;
  1.1820 +      case EOpConstructBool:
  1.1821 +        addConstructor(node->getType(), "bvec1", &node->getSequence());
  1.1822 +        outputTriplet(visit, "bvec1(", "", ")");
  1.1823 +        break;
  1.1824 +      case EOpConstructBVec2:
  1.1825 +        addConstructor(node->getType(), "bvec2", &node->getSequence());
  1.1826 +        outputTriplet(visit, "bvec2(", ", ", ")");
  1.1827 +        break;
  1.1828 +      case EOpConstructBVec3:
  1.1829 +        addConstructor(node->getType(), "bvec3", &node->getSequence());
  1.1830 +        outputTriplet(visit, "bvec3(", ", ", ")");
  1.1831 +        break;
  1.1832 +      case EOpConstructBVec4:
  1.1833 +        addConstructor(node->getType(), "bvec4", &node->getSequence());
  1.1834 +        outputTriplet(visit, "bvec4(", ", ", ")");
  1.1835 +        break;
  1.1836 +      case EOpConstructInt:
  1.1837 +        addConstructor(node->getType(), "ivec1", &node->getSequence());
  1.1838 +        outputTriplet(visit, "ivec1(", "", ")");
  1.1839 +        break;
  1.1840 +      case EOpConstructIVec2:
  1.1841 +        addConstructor(node->getType(), "ivec2", &node->getSequence());
  1.1842 +        outputTriplet(visit, "ivec2(", ", ", ")");
  1.1843 +        break;
  1.1844 +      case EOpConstructIVec3:
  1.1845 +        addConstructor(node->getType(), "ivec3", &node->getSequence());
  1.1846 +        outputTriplet(visit, "ivec3(", ", ", ")");
  1.1847 +        break;
  1.1848 +      case EOpConstructIVec4:
  1.1849 +        addConstructor(node->getType(), "ivec4", &node->getSequence());
  1.1850 +        outputTriplet(visit, "ivec4(", ", ", ")");
  1.1851 +        break;
  1.1852 +      case EOpConstructMat2:
  1.1853 +        addConstructor(node->getType(), "mat2", &node->getSequence());
  1.1854 +        outputTriplet(visit, "mat2(", ", ", ")");
  1.1855 +        break;
  1.1856 +      case EOpConstructMat3:
  1.1857 +        addConstructor(node->getType(), "mat3", &node->getSequence());
  1.1858 +        outputTriplet(visit, "mat3(", ", ", ")");
  1.1859 +        break;
  1.1860 +      case EOpConstructMat4: 
  1.1861 +        addConstructor(node->getType(), "mat4", &node->getSequence());
  1.1862 +        outputTriplet(visit, "mat4(", ", ", ")");
  1.1863 +        break;
  1.1864 +      case EOpConstructStruct:
  1.1865 +        addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
  1.1866 +        outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
  1.1867 +        break;
  1.1868 +      case EOpLessThan:         outputTriplet(visit, "(", " < ", ")");                 break;
  1.1869 +      case EOpGreaterThan:      outputTriplet(visit, "(", " > ", ")");                 break;
  1.1870 +      case EOpLessThanEqual:    outputTriplet(visit, "(", " <= ", ")");                break;
  1.1871 +      case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")");                break;
  1.1872 +      case EOpVectorEqual:      outputTriplet(visit, "(", " == ", ")");                break;
  1.1873 +      case EOpVectorNotEqual:   outputTriplet(visit, "(", " != ", ")");                break;
  1.1874 +      case EOpMod:
  1.1875 +        {
  1.1876 +            // We need to look at the number of components in both arguments
  1.1877 +            switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
  1.1878 +                     + node->getSequence()[1]->getAsTyped()->getNominalSize())
  1.1879 +            {
  1.1880 +              case 11: mUsesMod1 = true; break;
  1.1881 +              case 22: mUsesMod2v = true; break;
  1.1882 +              case 21: mUsesMod2f = true; break;
  1.1883 +              case 33: mUsesMod3v = true; break;
  1.1884 +              case 31: mUsesMod3f = true; break;
  1.1885 +              case 44: mUsesMod4v = true; break;
  1.1886 +              case 41: mUsesMod4f = true; break;
  1.1887 +              default: UNREACHABLE();
  1.1888 +            }
  1.1889 +
  1.1890 +            outputTriplet(visit, "mod(", ", ", ")");
  1.1891 +        }
  1.1892 +        break;
  1.1893 +      case EOpPow:              outputTriplet(visit, "pow(", ", ", ")");               break;
  1.1894 +      case EOpAtan:
  1.1895 +        ASSERT(node->getSequence().size() == 2);   // atan(x) is a unary operator
  1.1896 +        switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
  1.1897 +        {
  1.1898 +          case 1: mUsesAtan2_1 = true; break;
  1.1899 +          case 2: mUsesAtan2_2 = true; break;
  1.1900 +          case 3: mUsesAtan2_3 = true; break;
  1.1901 +          case 4: mUsesAtan2_4 = true; break;
  1.1902 +          default: UNREACHABLE();
  1.1903 +        }
  1.1904 +        outputTriplet(visit, "atanyx(", ", ", ")");
  1.1905 +        break;
  1.1906 +      case EOpMin:           outputTriplet(visit, "min(", ", ", ")");           break;
  1.1907 +      case EOpMax:           outputTriplet(visit, "max(", ", ", ")");           break;
  1.1908 +      case EOpClamp:         outputTriplet(visit, "clamp(", ", ", ")");         break;
  1.1909 +      case EOpMix:           outputTriplet(visit, "lerp(", ", ", ")");          break;
  1.1910 +      case EOpStep:          outputTriplet(visit, "step(", ", ", ")");          break;
  1.1911 +      case EOpSmoothStep:    outputTriplet(visit, "smoothstep(", ", ", ")");    break;
  1.1912 +      case EOpDistance:      outputTriplet(visit, "distance(", ", ", ")");      break;
  1.1913 +      case EOpDot:           outputTriplet(visit, "dot(", ", ", ")");           break;
  1.1914 +      case EOpCross:         outputTriplet(visit, "cross(", ", ", ")");         break;
  1.1915 +      case EOpFaceForward:
  1.1916 +        {
  1.1917 +            switch (node->getSequence()[0]->getAsTyped()->getNominalSize())   // Number of components in the first argument
  1.1918 +            {
  1.1919 +            case 1: mUsesFaceforward1 = true; break;
  1.1920 +            case 2: mUsesFaceforward2 = true; break;
  1.1921 +            case 3: mUsesFaceforward3 = true; break;
  1.1922 +            case 4: mUsesFaceforward4 = true; break;
  1.1923 +            default: UNREACHABLE();
  1.1924 +            }
  1.1925 +            
  1.1926 +            outputTriplet(visit, "faceforward(", ", ", ")");
  1.1927 +        }
  1.1928 +        break;
  1.1929 +      case EOpReflect:       outputTriplet(visit, "reflect(", ", ", ")");       break;
  1.1930 +      case EOpRefract:       outputTriplet(visit, "refract(", ", ", ")");       break;
  1.1931 +      case EOpMul:           outputTriplet(visit, "(", " * ", ")");             break;
  1.1932 +      default: UNREACHABLE();
  1.1933 +    }
  1.1934 +
  1.1935 +    return true;
  1.1936 +}
  1.1937 +
  1.1938 +bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
  1.1939 +{
  1.1940 +    TInfoSinkBase &out = mBody;
  1.1941 +
  1.1942 +    if (node->usesTernaryOperator())
  1.1943 +    {
  1.1944 +        out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
  1.1945 +    }
  1.1946 +    else  // if/else statement
  1.1947 +    {
  1.1948 +        mUnfoldShortCircuit->traverse(node->getCondition());
  1.1949 +
  1.1950 +        out << "if(";
  1.1951 +
  1.1952 +        node->getCondition()->traverse(this);
  1.1953 +
  1.1954 +        out << ")\n";
  1.1955 +        
  1.1956 +        outputLineDirective(node->getLine().first_line);
  1.1957 +        out << "{\n";
  1.1958 +
  1.1959 +        if (node->getTrueBlock())
  1.1960 +        {
  1.1961 +            traverseStatements(node->getTrueBlock());
  1.1962 +        }
  1.1963 +
  1.1964 +        outputLineDirective(node->getLine().first_line);
  1.1965 +        out << ";\n}\n";
  1.1966 +
  1.1967 +        if (node->getFalseBlock())
  1.1968 +        {
  1.1969 +            out << "else\n";
  1.1970 +
  1.1971 +            outputLineDirective(node->getFalseBlock()->getLine().first_line);
  1.1972 +            out << "{\n";
  1.1973 +
  1.1974 +            outputLineDirective(node->getFalseBlock()->getLine().first_line);
  1.1975 +            traverseStatements(node->getFalseBlock());
  1.1976 +
  1.1977 +            outputLineDirective(node->getFalseBlock()->getLine().first_line);
  1.1978 +            out << ";\n}\n";
  1.1979 +        }
  1.1980 +    }
  1.1981 +
  1.1982 +    return false;
  1.1983 +}
  1.1984 +
  1.1985 +void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
  1.1986 +{
  1.1987 +    writeConstantUnion(node->getType(), node->getUnionArrayPointer());
  1.1988 +}
  1.1989 +
  1.1990 +bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
  1.1991 +{
  1.1992 +    bool wasDiscontinuous = mInsideDiscontinuousLoop;
  1.1993 +
  1.1994 +    if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
  1.1995 +    {
  1.1996 +        mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
  1.1997 +    }
  1.1998 +
  1.1999 +    if (mOutputType == SH_HLSL9_OUTPUT)
  1.2000 +    {
  1.2001 +        if (handleExcessiveLoop(node))
  1.2002 +        {
  1.2003 +            return false;
  1.2004 +        }
  1.2005 +    }
  1.2006 +
  1.2007 +    TInfoSinkBase &out = mBody;
  1.2008 +
  1.2009 +    if (node->getType() == ELoopDoWhile)
  1.2010 +    {
  1.2011 +        out << "{do\n";
  1.2012 +
  1.2013 +        outputLineDirective(node->getLine().first_line);
  1.2014 +        out << "{\n";
  1.2015 +    }
  1.2016 +    else
  1.2017 +    {
  1.2018 +        out << "{for(";
  1.2019 +        
  1.2020 +        if (node->getInit())
  1.2021 +        {
  1.2022 +            node->getInit()->traverse(this);
  1.2023 +        }
  1.2024 +
  1.2025 +        out << "; ";
  1.2026 +
  1.2027 +        if (node->getCondition())
  1.2028 +        {
  1.2029 +            node->getCondition()->traverse(this);
  1.2030 +        }
  1.2031 +
  1.2032 +        out << "; ";
  1.2033 +
  1.2034 +        if (node->getExpression())
  1.2035 +        {
  1.2036 +            node->getExpression()->traverse(this);
  1.2037 +        }
  1.2038 +
  1.2039 +        out << ")\n";
  1.2040 +        
  1.2041 +        outputLineDirective(node->getLine().first_line);
  1.2042 +        out << "{\n";
  1.2043 +    }
  1.2044 +
  1.2045 +    if (node->getBody())
  1.2046 +    {
  1.2047 +        traverseStatements(node->getBody());
  1.2048 +    }
  1.2049 +
  1.2050 +    outputLineDirective(node->getLine().first_line);
  1.2051 +    out << ";}\n";
  1.2052 +
  1.2053 +    if (node->getType() == ELoopDoWhile)
  1.2054 +    {
  1.2055 +        outputLineDirective(node->getCondition()->getLine().first_line);
  1.2056 +        out << "while(\n";
  1.2057 +
  1.2058 +        node->getCondition()->traverse(this);
  1.2059 +
  1.2060 +        out << ");";
  1.2061 +    }
  1.2062 +
  1.2063 +    out << "}\n";
  1.2064 +
  1.2065 +    mInsideDiscontinuousLoop = wasDiscontinuous;
  1.2066 +
  1.2067 +    return false;
  1.2068 +}
  1.2069 +
  1.2070 +bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
  1.2071 +{
  1.2072 +    TInfoSinkBase &out = mBody;
  1.2073 +
  1.2074 +    switch (node->getFlowOp())
  1.2075 +    {
  1.2076 +      case EOpKill:     outputTriplet(visit, "discard;\n", "", "");  break;
  1.2077 +      case EOpBreak:
  1.2078 +        if (visit == PreVisit)
  1.2079 +        {
  1.2080 +            if (mExcessiveLoopIndex)
  1.2081 +            {
  1.2082 +                out << "{Break";
  1.2083 +                mExcessiveLoopIndex->traverse(this);
  1.2084 +                out << " = true; break;}\n";
  1.2085 +            }
  1.2086 +            else
  1.2087 +            {
  1.2088 +                out << "break;\n";
  1.2089 +            }
  1.2090 +        }
  1.2091 +        break;
  1.2092 +      case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
  1.2093 +      case EOpReturn:
  1.2094 +        if (visit == PreVisit)
  1.2095 +        {
  1.2096 +            if (node->getExpression())
  1.2097 +            {
  1.2098 +                out << "return ";
  1.2099 +            }
  1.2100 +            else
  1.2101 +            {
  1.2102 +                out << "return;\n";
  1.2103 +            }
  1.2104 +        }
  1.2105 +        else if (visit == PostVisit)
  1.2106 +        {
  1.2107 +            if (node->getExpression())
  1.2108 +            {
  1.2109 +                out << ";\n";
  1.2110 +            }
  1.2111 +        }
  1.2112 +        break;
  1.2113 +      default: UNREACHABLE();
  1.2114 +    }
  1.2115 +
  1.2116 +    return true;
  1.2117 +}
  1.2118 +
  1.2119 +void OutputHLSL::traverseStatements(TIntermNode *node)
  1.2120 +{
  1.2121 +    if (isSingleStatement(node))
  1.2122 +    {
  1.2123 +        mUnfoldShortCircuit->traverse(node);
  1.2124 +    }
  1.2125 +
  1.2126 +    node->traverse(this);
  1.2127 +}
  1.2128 +
  1.2129 +bool OutputHLSL::isSingleStatement(TIntermNode *node)
  1.2130 +{
  1.2131 +    TIntermAggregate *aggregate = node->getAsAggregate();
  1.2132 +
  1.2133 +    if (aggregate)
  1.2134 +    {
  1.2135 +        if (aggregate->getOp() == EOpSequence)
  1.2136 +        {
  1.2137 +            return false;
  1.2138 +        }
  1.2139 +        else
  1.2140 +        {
  1.2141 +            for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
  1.2142 +            {
  1.2143 +                if (!isSingleStatement(*sit))
  1.2144 +                {
  1.2145 +                    return false;
  1.2146 +                }
  1.2147 +            }
  1.2148 +
  1.2149 +            return true;
  1.2150 +        }
  1.2151 +    }
  1.2152 +
  1.2153 +    return true;
  1.2154 +}
  1.2155 +
  1.2156 +// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
  1.2157 +// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
  1.2158 +bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
  1.2159 +{
  1.2160 +    const int MAX_LOOP_ITERATIONS = 254;
  1.2161 +    TInfoSinkBase &out = mBody;
  1.2162 +
  1.2163 +    // Parse loops of the form:
  1.2164 +    // for(int index = initial; index [comparator] limit; index += increment)
  1.2165 +    TIntermSymbol *index = NULL;
  1.2166 +    TOperator comparator = EOpNull;
  1.2167 +    int initial = 0;
  1.2168 +    int limit = 0;
  1.2169 +    int increment = 0;
  1.2170 +
  1.2171 +    // Parse index name and intial value
  1.2172 +    if (node->getInit())
  1.2173 +    {
  1.2174 +        TIntermAggregate *init = node->getInit()->getAsAggregate();
  1.2175 +
  1.2176 +        if (init)
  1.2177 +        {
  1.2178 +            TIntermSequence &sequence = init->getSequence();
  1.2179 +            TIntermTyped *variable = sequence[0]->getAsTyped();
  1.2180 +
  1.2181 +            if (variable && variable->getQualifier() == EvqTemporary)
  1.2182 +            {
  1.2183 +                TIntermBinary *assign = variable->getAsBinaryNode();
  1.2184 +
  1.2185 +                if (assign->getOp() == EOpInitialize)
  1.2186 +                {
  1.2187 +                    TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
  1.2188 +                    TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
  1.2189 +
  1.2190 +                    if (symbol && constant)
  1.2191 +                    {
  1.2192 +                        if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
  1.2193 +                        {
  1.2194 +                            index = symbol;
  1.2195 +                            initial = constant->getIConst(0);
  1.2196 +                        }
  1.2197 +                    }
  1.2198 +                }
  1.2199 +            }
  1.2200 +        }
  1.2201 +    }
  1.2202 +
  1.2203 +    // Parse comparator and limit value
  1.2204 +    if (index != NULL && node->getCondition())
  1.2205 +    {
  1.2206 +        TIntermBinary *test = node->getCondition()->getAsBinaryNode();
  1.2207 +        
  1.2208 +        if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
  1.2209 +        {
  1.2210 +            TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
  1.2211 +
  1.2212 +            if (constant)
  1.2213 +            {
  1.2214 +                if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
  1.2215 +                {
  1.2216 +                    comparator = test->getOp();
  1.2217 +                    limit = constant->getIConst(0);
  1.2218 +                }
  1.2219 +            }
  1.2220 +        }
  1.2221 +    }
  1.2222 +
  1.2223 +    // Parse increment
  1.2224 +    if (index != NULL && comparator != EOpNull && node->getExpression())
  1.2225 +    {
  1.2226 +        TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
  1.2227 +        TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
  1.2228 +        
  1.2229 +        if (binaryTerminal)
  1.2230 +        {
  1.2231 +            TOperator op = binaryTerminal->getOp();
  1.2232 +            TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
  1.2233 +
  1.2234 +            if (constant)
  1.2235 +            {
  1.2236 +                if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
  1.2237 +                {
  1.2238 +                    int value = constant->getIConst(0);
  1.2239 +
  1.2240 +                    switch (op)
  1.2241 +                    {
  1.2242 +                      case EOpAddAssign: increment = value;  break;
  1.2243 +                      case EOpSubAssign: increment = -value; break;
  1.2244 +                      default: UNIMPLEMENTED();
  1.2245 +                    }
  1.2246 +                }
  1.2247 +            }
  1.2248 +        }
  1.2249 +        else if (unaryTerminal)
  1.2250 +        {
  1.2251 +            TOperator op = unaryTerminal->getOp();
  1.2252 +
  1.2253 +            switch (op)
  1.2254 +            {
  1.2255 +              case EOpPostIncrement: increment = 1;  break;
  1.2256 +              case EOpPostDecrement: increment = -1; break;
  1.2257 +              case EOpPreIncrement:  increment = 1;  break;
  1.2258 +              case EOpPreDecrement:  increment = -1; break;
  1.2259 +              default: UNIMPLEMENTED();
  1.2260 +            }
  1.2261 +        }
  1.2262 +    }
  1.2263 +
  1.2264 +    if (index != NULL && comparator != EOpNull && increment != 0)
  1.2265 +    {
  1.2266 +        if (comparator == EOpLessThanEqual)
  1.2267 +        {
  1.2268 +            comparator = EOpLessThan;
  1.2269 +            limit += 1;
  1.2270 +        }
  1.2271 +
  1.2272 +        if (comparator == EOpLessThan)
  1.2273 +        {
  1.2274 +            int iterations = (limit - initial) / increment;
  1.2275 +
  1.2276 +            if (iterations <= MAX_LOOP_ITERATIONS)
  1.2277 +            {
  1.2278 +                return false;   // Not an excessive loop
  1.2279 +            }
  1.2280 +
  1.2281 +            TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
  1.2282 +            mExcessiveLoopIndex = index;
  1.2283 +
  1.2284 +            out << "{int ";
  1.2285 +            index->traverse(this);
  1.2286 +            out << ";\n"
  1.2287 +                   "bool Break";
  1.2288 +            index->traverse(this);
  1.2289 +            out << " = false;\n";
  1.2290 +
  1.2291 +            bool firstLoopFragment = true;
  1.2292 +
  1.2293 +            while (iterations > 0)
  1.2294 +            {
  1.2295 +                int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
  1.2296 +
  1.2297 +                if (!firstLoopFragment)
  1.2298 +                {
  1.2299 +                    out << "if(!Break";
  1.2300 +                    index->traverse(this);
  1.2301 +                    out << ") {\n";
  1.2302 +                }
  1.2303 +
  1.2304 +                if (iterations <= MAX_LOOP_ITERATIONS)   // Last loop fragment
  1.2305 +                {
  1.2306 +                    mExcessiveLoopIndex = NULL;   // Stops setting the Break flag
  1.2307 +                }
  1.2308 +                
  1.2309 +                // for(int index = initial; index < clampedLimit; index += increment)
  1.2310 +
  1.2311 +                out << "for(";
  1.2312 +                index->traverse(this);
  1.2313 +                out << " = ";
  1.2314 +                out << initial;
  1.2315 +
  1.2316 +                out << "; ";
  1.2317 +                index->traverse(this);
  1.2318 +                out << " < ";
  1.2319 +                out << clampedLimit;
  1.2320 +
  1.2321 +                out << "; ";
  1.2322 +                index->traverse(this);
  1.2323 +                out << " += ";
  1.2324 +                out << increment;
  1.2325 +                out << ")\n";
  1.2326 +                
  1.2327 +                outputLineDirective(node->getLine().first_line);
  1.2328 +                out << "{\n";
  1.2329 +
  1.2330 +                if (node->getBody())
  1.2331 +                {
  1.2332 +                    node->getBody()->traverse(this);
  1.2333 +                }
  1.2334 +
  1.2335 +                outputLineDirective(node->getLine().first_line);
  1.2336 +                out << ";}\n";
  1.2337 +
  1.2338 +                if (!firstLoopFragment)
  1.2339 +                {
  1.2340 +                    out << "}\n";
  1.2341 +                }
  1.2342 +
  1.2343 +                firstLoopFragment = false;
  1.2344 +
  1.2345 +                initial += MAX_LOOP_ITERATIONS * increment;
  1.2346 +                iterations -= MAX_LOOP_ITERATIONS;
  1.2347 +            }
  1.2348 +            
  1.2349 +            out << "}";
  1.2350 +
  1.2351 +            mExcessiveLoopIndex = restoreIndex;
  1.2352 +
  1.2353 +            return true;
  1.2354 +        }
  1.2355 +        else UNIMPLEMENTED();
  1.2356 +    }
  1.2357 +
  1.2358 +    return false;   // Not handled as an excessive loop
  1.2359 +}
  1.2360 +
  1.2361 +void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
  1.2362 +{
  1.2363 +    TInfoSinkBase &out = mBody;
  1.2364 +
  1.2365 +    if (visit == PreVisit)
  1.2366 +    {
  1.2367 +        out << preString;
  1.2368 +    }
  1.2369 +    else if (visit == InVisit)
  1.2370 +    {
  1.2371 +        out << inString;
  1.2372 +    }
  1.2373 +    else if (visit == PostVisit)
  1.2374 +    {
  1.2375 +        out << postString;
  1.2376 +    }
  1.2377 +}
  1.2378 +
  1.2379 +void OutputHLSL::outputLineDirective(int line)
  1.2380 +{
  1.2381 +    if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
  1.2382 +    {
  1.2383 +        mBody << "\n";
  1.2384 +        mBody << "#line " << line;
  1.2385 +
  1.2386 +        if (mContext.sourcePath)
  1.2387 +        {
  1.2388 +            mBody << " \"" << mContext.sourcePath << "\"";
  1.2389 +        }
  1.2390 +        
  1.2391 +        mBody << "\n";
  1.2392 +    }
  1.2393 +}
  1.2394 +
  1.2395 +TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
  1.2396 +{
  1.2397 +    TQualifier qualifier = symbol->getQualifier();
  1.2398 +    const TType &type = symbol->getType();
  1.2399 +    TString name = symbol->getSymbol();
  1.2400 +
  1.2401 +    if (name.empty())   // HLSL demands named arguments, also for prototypes
  1.2402 +    {
  1.2403 +        name = "x" + str(mUniqueIndex++);
  1.2404 +    }
  1.2405 +    else
  1.2406 +    {
  1.2407 +        name = decorate(name);
  1.2408 +    }
  1.2409 +
  1.2410 +    if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
  1.2411 +    {
  1.2412 +       return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
  1.2413 +              qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
  1.2414 +    }
  1.2415 +
  1.2416 +    return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
  1.2417 +}
  1.2418 +
  1.2419 +TString OutputHLSL::qualifierString(TQualifier qualifier)
  1.2420 +{
  1.2421 +    switch(qualifier)
  1.2422 +    {
  1.2423 +      case EvqIn:            return "in";
  1.2424 +      case EvqOut:           return "out";
  1.2425 +      case EvqInOut:         return "inout";
  1.2426 +      case EvqConstReadOnly: return "const";
  1.2427 +      default: UNREACHABLE();
  1.2428 +    }
  1.2429 +
  1.2430 +    return "";
  1.2431 +}
  1.2432 +
  1.2433 +TString OutputHLSL::typeString(const TType &type)
  1.2434 +{
  1.2435 +    if (type.getBasicType() == EbtStruct)
  1.2436 +    {
  1.2437 +        const TString& typeName = type.getStruct()->name();
  1.2438 +        if (typeName != "")
  1.2439 +        {
  1.2440 +            return structLookup(typeName);
  1.2441 +        }
  1.2442 +        else   // Nameless structure, define in place
  1.2443 +        {
  1.2444 +            const TFieldList &fields = type.getStruct()->fields();
  1.2445 +
  1.2446 +            TString string = "struct\n"
  1.2447 +                             "{\n";
  1.2448 +
  1.2449 +            for (unsigned int i = 0; i < fields.size(); i++)
  1.2450 +            {
  1.2451 +                const TField *field = fields[i];
  1.2452 +
  1.2453 +                string += "    " + typeString(*field->type()) + " " + decorate(field->name()) + arrayString(*field->type()) + ";\n";
  1.2454 +            }
  1.2455 +
  1.2456 +            string += "} ";
  1.2457 +
  1.2458 +            return string;
  1.2459 +        }
  1.2460 +    }
  1.2461 +    else if (type.isMatrix())
  1.2462 +    {
  1.2463 +        switch (type.getNominalSize())
  1.2464 +        {
  1.2465 +          case 2: return "float2x2";
  1.2466 +          case 3: return "float3x3";
  1.2467 +          case 4: return "float4x4";
  1.2468 +        }
  1.2469 +    }
  1.2470 +    else
  1.2471 +    {
  1.2472 +        switch (type.getBasicType())
  1.2473 +        {
  1.2474 +          case EbtFloat:
  1.2475 +            switch (type.getNominalSize())
  1.2476 +            {
  1.2477 +              case 1: return "float";
  1.2478 +              case 2: return "float2";
  1.2479 +              case 3: return "float3";
  1.2480 +              case 4: return "float4";
  1.2481 +            }
  1.2482 +          case EbtInt:
  1.2483 +            switch (type.getNominalSize())
  1.2484 +            {
  1.2485 +              case 1: return "int";
  1.2486 +              case 2: return "int2";
  1.2487 +              case 3: return "int3";
  1.2488 +              case 4: return "int4";
  1.2489 +            }
  1.2490 +          case EbtBool:
  1.2491 +            switch (type.getNominalSize())
  1.2492 +            {
  1.2493 +              case 1: return "bool";
  1.2494 +              case 2: return "bool2";
  1.2495 +              case 3: return "bool3";
  1.2496 +              case 4: return "bool4";
  1.2497 +            }
  1.2498 +          case EbtVoid:
  1.2499 +            return "void";
  1.2500 +          case EbtSampler2D:
  1.2501 +            return "sampler2D";
  1.2502 +          case EbtSamplerCube:
  1.2503 +            return "samplerCUBE";
  1.2504 +          case EbtSamplerExternalOES:
  1.2505 +            return "sampler2D";
  1.2506 +          default:
  1.2507 +            break;
  1.2508 +        }
  1.2509 +    }
  1.2510 +
  1.2511 +    UNREACHABLE();
  1.2512 +    return "<unknown type>";
  1.2513 +}
  1.2514 +
  1.2515 +TString OutputHLSL::textureString(const TType &type)
  1.2516 +{
  1.2517 +    switch (type.getBasicType())
  1.2518 +    {
  1.2519 +      case EbtSampler2D:
  1.2520 +        return "Texture2D";
  1.2521 +      case EbtSamplerCube:
  1.2522 +        return "TextureCube";
  1.2523 +      case EbtSamplerExternalOES:
  1.2524 +        return "Texture2D";
  1.2525 +      default:
  1.2526 +        break;
  1.2527 +    }
  1.2528 +
  1.2529 +    UNREACHABLE();
  1.2530 +    return "<unknown texture type>";
  1.2531 +}
  1.2532 +
  1.2533 +TString OutputHLSL::arrayString(const TType &type)
  1.2534 +{
  1.2535 +    if (!type.isArray())
  1.2536 +    {
  1.2537 +        return "";
  1.2538 +    }
  1.2539 +
  1.2540 +    return "[" + str(type.getArraySize()) + "]";
  1.2541 +}
  1.2542 +
  1.2543 +TString OutputHLSL::initializer(const TType &type)
  1.2544 +{
  1.2545 +    TString string;
  1.2546 +
  1.2547 +    size_t size = type.getObjectSize();
  1.2548 +    for (size_t component = 0; component < size; component++)
  1.2549 +    {
  1.2550 +        string += "0";
  1.2551 +
  1.2552 +        if (component + 1 < size)
  1.2553 +        {
  1.2554 +            string += ", ";
  1.2555 +        }
  1.2556 +    }
  1.2557 +
  1.2558 +    return "{" + string + "}";
  1.2559 +}
  1.2560 +
  1.2561 +void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
  1.2562 +{
  1.2563 +    if (name == "")
  1.2564 +    {
  1.2565 +        return;   // Nameless structures don't have constructors
  1.2566 +    }
  1.2567 +
  1.2568 +    if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
  1.2569 +    {
  1.2570 +        return;   // Already added
  1.2571 +    }
  1.2572 +
  1.2573 +    TType ctorType = type;
  1.2574 +    ctorType.clearArrayness();
  1.2575 +    ctorType.setPrecision(EbpHigh);
  1.2576 +    ctorType.setQualifier(EvqTemporary);
  1.2577 +
  1.2578 +    TString ctorName = type.getStruct() ? decorate(name) : name;
  1.2579 +
  1.2580 +    typedef std::vector<TType> ParameterArray;
  1.2581 +    ParameterArray ctorParameters;
  1.2582 +
  1.2583 +    if (type.getStruct())
  1.2584 +    {
  1.2585 +        mStructNames.insert(decorate(name));
  1.2586 +
  1.2587 +        TString structure;
  1.2588 +        structure += "struct " + decorate(name) + "\n"
  1.2589 +                     "{\n";
  1.2590 +
  1.2591 +        const TFieldList &fields = type.getStruct()->fields();
  1.2592 +
  1.2593 +        for (unsigned int i = 0; i < fields.size(); i++)
  1.2594 +        {
  1.2595 +            const TField *field = fields[i];
  1.2596 +
  1.2597 +            structure += "    " + typeString(*field->type()) + " " + decorateField(field->name(), type) + arrayString(*field->type()) + ";\n";
  1.2598 +        }
  1.2599 +
  1.2600 +        structure += "};\n";
  1.2601 +
  1.2602 +        if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
  1.2603 +        {
  1.2604 +            mStructDeclarations.push_back(structure);
  1.2605 +        }
  1.2606 +
  1.2607 +        for (unsigned int i = 0; i < fields.size(); i++)
  1.2608 +        {
  1.2609 +            ctorParameters.push_back(*fields[i]->type());
  1.2610 +        }
  1.2611 +    }
  1.2612 +    else if (parameters)
  1.2613 +    {
  1.2614 +        for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
  1.2615 +        {
  1.2616 +            ctorParameters.push_back((*parameter)->getAsTyped()->getType());
  1.2617 +        }
  1.2618 +    }
  1.2619 +    else UNREACHABLE();
  1.2620 +
  1.2621 +    TString constructor;
  1.2622 +
  1.2623 +    if (ctorType.getStruct())
  1.2624 +    {
  1.2625 +        constructor += ctorName + " " + ctorName + "_ctor(";
  1.2626 +    }
  1.2627 +    else   // Built-in type
  1.2628 +    {
  1.2629 +        constructor += typeString(ctorType) + " " + ctorName + "(";
  1.2630 +    }
  1.2631 +
  1.2632 +    for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
  1.2633 +    {
  1.2634 +        const TType &type = ctorParameters[parameter];
  1.2635 +
  1.2636 +        constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
  1.2637 +
  1.2638 +        if (parameter < ctorParameters.size() - 1)
  1.2639 +        {
  1.2640 +            constructor += ", ";
  1.2641 +        }
  1.2642 +    }
  1.2643 +
  1.2644 +    constructor += ")\n"
  1.2645 +                   "{\n";
  1.2646 +
  1.2647 +    if (ctorType.getStruct())
  1.2648 +    {
  1.2649 +        constructor += "    " + ctorName + " structure = {";
  1.2650 +    }
  1.2651 +    else
  1.2652 +    {
  1.2653 +        constructor += "    return " + typeString(ctorType) + "(";
  1.2654 +    }
  1.2655 +
  1.2656 +    if (ctorType.isMatrix() && ctorParameters.size() == 1)
  1.2657 +    {
  1.2658 +        int dim = ctorType.getNominalSize();
  1.2659 +        const TType &parameter = ctorParameters[0];
  1.2660 +
  1.2661 +        if (parameter.isScalar())
  1.2662 +        {
  1.2663 +            for (int row = 0; row < dim; row++)
  1.2664 +            {
  1.2665 +                for (int col = 0; col < dim; col++)
  1.2666 +                {
  1.2667 +                    constructor += TString((row == col) ? "x0" : "0.0");
  1.2668 +                    
  1.2669 +                    if (row < dim - 1 || col < dim - 1)
  1.2670 +                    {
  1.2671 +                        constructor += ", ";
  1.2672 +                    }
  1.2673 +                }
  1.2674 +            }
  1.2675 +        }
  1.2676 +        else if (parameter.isMatrix())
  1.2677 +        {
  1.2678 +            for (int row = 0; row < dim; row++)
  1.2679 +            {
  1.2680 +                for (int col = 0; col < dim; col++)
  1.2681 +                {
  1.2682 +                    if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
  1.2683 +                    {
  1.2684 +                        constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
  1.2685 +                    }
  1.2686 +                    else
  1.2687 +                    {
  1.2688 +                        constructor += TString((row == col) ? "1.0" : "0.0");
  1.2689 +                    }
  1.2690 +
  1.2691 +                    if (row < dim - 1 || col < dim - 1)
  1.2692 +                    {
  1.2693 +                        constructor += ", ";
  1.2694 +                    }
  1.2695 +                }
  1.2696 +            }
  1.2697 +        }
  1.2698 +        else UNREACHABLE();
  1.2699 +    }
  1.2700 +    else
  1.2701 +    {
  1.2702 +        size_t remainingComponents = ctorType.getObjectSize();
  1.2703 +        size_t parameterIndex = 0;
  1.2704 +
  1.2705 +        while (remainingComponents > 0)
  1.2706 +        {
  1.2707 +            const TType &parameter = ctorParameters[parameterIndex];
  1.2708 +            const size_t parameterSize = parameter.getObjectSize();
  1.2709 +            bool moreParameters = parameterIndex + 1 < ctorParameters.size();
  1.2710 +
  1.2711 +            constructor += "x" + str(parameterIndex);
  1.2712 +
  1.2713 +            if (parameter.isScalar())
  1.2714 +            {
  1.2715 +                ASSERT(parameterSize <= remainingComponents);
  1.2716 +                remainingComponents -= parameterSize;
  1.2717 +            }
  1.2718 +            else if (parameter.isVector())
  1.2719 +            {
  1.2720 +                if (remainingComponents == parameterSize || moreParameters)
  1.2721 +                {
  1.2722 +                    ASSERT(parameterSize <= remainingComponents);
  1.2723 +                    remainingComponents -= parameterSize;
  1.2724 +                }
  1.2725 +                else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
  1.2726 +                {
  1.2727 +                    switch (remainingComponents)
  1.2728 +                    {
  1.2729 +                      case 1: constructor += ".x";    break;
  1.2730 +                      case 2: constructor += ".xy";   break;
  1.2731 +                      case 3: constructor += ".xyz";  break;
  1.2732 +                      case 4: constructor += ".xyzw"; break;
  1.2733 +                      default: UNREACHABLE();
  1.2734 +                    }
  1.2735 +
  1.2736 +                    remainingComponents = 0;
  1.2737 +                }
  1.2738 +                else UNREACHABLE();
  1.2739 +            }
  1.2740 +            else if (parameter.isMatrix() || parameter.getStruct())
  1.2741 +            {
  1.2742 +                ASSERT(remainingComponents == parameterSize || moreParameters);
  1.2743 +                ASSERT(parameterSize <= remainingComponents);
  1.2744 +                
  1.2745 +                remainingComponents -= parameterSize;
  1.2746 +            }
  1.2747 +            else UNREACHABLE();
  1.2748 +
  1.2749 +            if (moreParameters)
  1.2750 +            {
  1.2751 +                parameterIndex++;
  1.2752 +            }
  1.2753 +
  1.2754 +            if (remainingComponents)
  1.2755 +            {
  1.2756 +                constructor += ", ";
  1.2757 +            }
  1.2758 +        }
  1.2759 +    }
  1.2760 +
  1.2761 +    if (ctorType.getStruct())
  1.2762 +    {
  1.2763 +        constructor += "};\n"
  1.2764 +                       "    return structure;\n"
  1.2765 +                       "}\n";
  1.2766 +    }
  1.2767 +    else
  1.2768 +    {
  1.2769 +        constructor += ");\n"
  1.2770 +                       "}\n";
  1.2771 +    }
  1.2772 +
  1.2773 +    mConstructors.insert(constructor);
  1.2774 +}
  1.2775 +
  1.2776 +const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
  1.2777 +{
  1.2778 +    TInfoSinkBase &out = mBody;
  1.2779 +
  1.2780 +    if (type.getBasicType() == EbtStruct)
  1.2781 +    {
  1.2782 +        out << structLookup(type.getStruct()->name()) + "_ctor(";
  1.2783 +        
  1.2784 +        const TFieldList &fields = type.getStruct()->fields();
  1.2785 +
  1.2786 +        for (size_t i = 0; i < fields.size(); i++)
  1.2787 +        {
  1.2788 +            const TType *fieldType = fields[i]->type();
  1.2789 +
  1.2790 +            constUnion = writeConstantUnion(*fieldType, constUnion);
  1.2791 +
  1.2792 +            if (i != fields.size() - 1)
  1.2793 +            {
  1.2794 +                out << ", ";
  1.2795 +            }
  1.2796 +        }
  1.2797 +
  1.2798 +        out << ")";
  1.2799 +    }
  1.2800 +    else
  1.2801 +    {
  1.2802 +        size_t size = type.getObjectSize();
  1.2803 +        bool writeType = size > 1;
  1.2804 +        
  1.2805 +        if (writeType)
  1.2806 +        {
  1.2807 +            out << typeString(type) << "(";
  1.2808 +        }
  1.2809 +
  1.2810 +        for (size_t i = 0; i < size; i++, constUnion++)
  1.2811 +        {
  1.2812 +            switch (constUnion->getType())
  1.2813 +            {
  1.2814 +              case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
  1.2815 +              case EbtInt:   out << constUnion->getIConst(); break;
  1.2816 +              case EbtBool:  out << constUnion->getBConst(); break;
  1.2817 +              default: UNREACHABLE();
  1.2818 +            }
  1.2819 +
  1.2820 +            if (i != size - 1)
  1.2821 +            {
  1.2822 +                out << ", ";
  1.2823 +            }
  1.2824 +        }
  1.2825 +
  1.2826 +        if (writeType)
  1.2827 +        {
  1.2828 +            out << ")";
  1.2829 +        }
  1.2830 +    }
  1.2831 +
  1.2832 +    return constUnion;
  1.2833 +}
  1.2834 +
  1.2835 +TString OutputHLSL::scopeString(unsigned int depthLimit)
  1.2836 +{
  1.2837 +    TString string;
  1.2838 +
  1.2839 +    for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
  1.2840 +    {
  1.2841 +        string += "_" + str(i);
  1.2842 +    }
  1.2843 +
  1.2844 +    return string;
  1.2845 +}
  1.2846 +
  1.2847 +TString OutputHLSL::scopedStruct(const TString &typeName)
  1.2848 +{
  1.2849 +    if (typeName == "")
  1.2850 +    {
  1.2851 +        return typeName;
  1.2852 +    }
  1.2853 +
  1.2854 +    return typeName + scopeString(mScopeDepth);
  1.2855 +}
  1.2856 +
  1.2857 +TString OutputHLSL::structLookup(const TString &typeName)
  1.2858 +{
  1.2859 +    for (int depth = mScopeDepth; depth >= 0; depth--)
  1.2860 +    {
  1.2861 +        TString scopedName = decorate(typeName + scopeString(depth));
  1.2862 +
  1.2863 +        for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
  1.2864 +        {
  1.2865 +            if (*structName == scopedName)
  1.2866 +            {
  1.2867 +                return scopedName;
  1.2868 +            }
  1.2869 +        }
  1.2870 +    }
  1.2871 +
  1.2872 +    UNREACHABLE();   // Should have found a matching constructor
  1.2873 +
  1.2874 +    return typeName;
  1.2875 +}
  1.2876 +
  1.2877 +TString OutputHLSL::decorate(const TString &string)
  1.2878 +{
  1.2879 +    if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
  1.2880 +    {
  1.2881 +        return "_" + string;
  1.2882 +    }
  1.2883 +    
  1.2884 +    return string;
  1.2885 +}
  1.2886 +
  1.2887 +TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
  1.2888 +{
  1.2889 +    if (type.getBasicType() == EbtSamplerExternalOES)
  1.2890 +    {
  1.2891 +        return "ex_" + string;
  1.2892 +    }
  1.2893 +    
  1.2894 +    return decorate(string);
  1.2895 +}
  1.2896 +
  1.2897 +TString OutputHLSL::decorateField(const TString &string, const TType &structure)
  1.2898 +{
  1.2899 +    if (structure.getStruct()->name().compare(0, 3, "gl_") != 0)
  1.2900 +    {
  1.2901 +        return decorate(string);
  1.2902 +    }
  1.2903 +
  1.2904 +    return string;
  1.2905 +}
  1.2906 +
  1.2907 +TString OutputHLSL::registerString(TIntermSymbol *operand)
  1.2908 +{
  1.2909 +    ASSERT(operand->getQualifier() == EvqUniform);
  1.2910 +
  1.2911 +    if (IsSampler(operand->getBasicType()))
  1.2912 +    {
  1.2913 +        return "s" + str(samplerRegister(operand));
  1.2914 +    }
  1.2915 +
  1.2916 +    return "c" + str(uniformRegister(operand));
  1.2917 +}
  1.2918 +
  1.2919 +int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
  1.2920 +{
  1.2921 +    const TType &type = sampler->getType();
  1.2922 +    ASSERT(IsSampler(type.getBasicType()));
  1.2923 +
  1.2924 +    int index = mSamplerRegister;
  1.2925 +    mSamplerRegister += sampler->totalRegisterCount();
  1.2926 +
  1.2927 +    declareUniform(type, sampler->getSymbol(), index);
  1.2928 +
  1.2929 +    return index;
  1.2930 +}
  1.2931 +
  1.2932 +int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
  1.2933 +{
  1.2934 +    const TType &type = uniform->getType();
  1.2935 +    ASSERT(!IsSampler(type.getBasicType()));
  1.2936 +
  1.2937 +    int index = mUniformRegister;
  1.2938 +    mUniformRegister += uniform->totalRegisterCount();
  1.2939 +
  1.2940 +    declareUniform(type, uniform->getSymbol(), index);
  1.2941 +
  1.2942 +    return index;
  1.2943 +}
  1.2944 +
  1.2945 +void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
  1.2946 +{
  1.2947 +    TStructure *structure = type.getStruct();
  1.2948 +
  1.2949 +    if (!structure)
  1.2950 +    {
  1.2951 +        mActiveUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), index));
  1.2952 +    }
  1.2953 +    else
  1.2954 +    {
  1.2955 +        const TFieldList &fields = structure->fields();
  1.2956 +
  1.2957 +        if (type.isArray())
  1.2958 +        {
  1.2959 +            int elementIndex = index;
  1.2960 +
  1.2961 +            for (int i = 0; i < type.getArraySize(); i++)
  1.2962 +            {
  1.2963 +                for (size_t j = 0; j < fields.size(); j++)
  1.2964 +                {
  1.2965 +                    const TType &fieldType = *fields[j]->type();
  1.2966 +                    const TString uniformName = name + "[" + str(i) + "]." + fields[j]->name();
  1.2967 +                    declareUniform(fieldType, uniformName, elementIndex);
  1.2968 +                    elementIndex += fieldType.totalRegisterCount();
  1.2969 +                }
  1.2970 +            }
  1.2971 +        }
  1.2972 +        else
  1.2973 +        {
  1.2974 +            int fieldIndex = index;
  1.2975 +
  1.2976 +            for (size_t i = 0; i < fields.size(); i++)
  1.2977 +            {
  1.2978 +                const TType &fieldType = *fields[i]->type();
  1.2979 +                const TString uniformName = name + "." + fields[i]->name();
  1.2980 +                declareUniform(fieldType, uniformName, fieldIndex);
  1.2981 +                fieldIndex += fieldType.totalRegisterCount();
  1.2982 +            }
  1.2983 +        }
  1.2984 +    }
  1.2985 +}
  1.2986 +
  1.2987 +GLenum OutputHLSL::glVariableType(const TType &type)
  1.2988 +{
  1.2989 +    if (type.getBasicType() == EbtFloat)
  1.2990 +    {
  1.2991 +        if (type.isScalar())
  1.2992 +        {
  1.2993 +            return GL_FLOAT;
  1.2994 +        }
  1.2995 +        else if (type.isVector())
  1.2996 +        {
  1.2997 +            switch(type.getNominalSize())
  1.2998 +            {
  1.2999 +              case 2: return GL_FLOAT_VEC2;
  1.3000 +              case 3: return GL_FLOAT_VEC3;
  1.3001 +              case 4: return GL_FLOAT_VEC4;
  1.3002 +              default: UNREACHABLE();
  1.3003 +            }
  1.3004 +        }
  1.3005 +        else if (type.isMatrix())
  1.3006 +        {
  1.3007 +            switch(type.getNominalSize())
  1.3008 +            {
  1.3009 +              case 2: return GL_FLOAT_MAT2;
  1.3010 +              case 3: return GL_FLOAT_MAT3;
  1.3011 +              case 4: return GL_FLOAT_MAT4;
  1.3012 +              default: UNREACHABLE();
  1.3013 +            }
  1.3014 +        }
  1.3015 +        else UNREACHABLE();
  1.3016 +    }
  1.3017 +    else if (type.getBasicType() == EbtInt)
  1.3018 +    {
  1.3019 +        if (type.isScalar())
  1.3020 +        {
  1.3021 +            return GL_INT;
  1.3022 +        }
  1.3023 +        else if (type.isVector())
  1.3024 +        {
  1.3025 +            switch(type.getNominalSize())
  1.3026 +            {
  1.3027 +              case 2: return GL_INT_VEC2;
  1.3028 +              case 3: return GL_INT_VEC3;
  1.3029 +              case 4: return GL_INT_VEC4;
  1.3030 +              default: UNREACHABLE();
  1.3031 +            }
  1.3032 +        }
  1.3033 +        else UNREACHABLE();
  1.3034 +    }
  1.3035 +    else if (type.getBasicType() == EbtBool)
  1.3036 +    {
  1.3037 +        if (type.isScalar())
  1.3038 +        {
  1.3039 +            return GL_BOOL;
  1.3040 +        }
  1.3041 +        else if (type.isVector())
  1.3042 +        {
  1.3043 +            switch(type.getNominalSize())
  1.3044 +            {
  1.3045 +              case 2: return GL_BOOL_VEC2;
  1.3046 +              case 3: return GL_BOOL_VEC3;
  1.3047 +              case 4: return GL_BOOL_VEC4;
  1.3048 +              default: UNREACHABLE();
  1.3049 +            }
  1.3050 +        }
  1.3051 +        else UNREACHABLE();
  1.3052 +    }
  1.3053 +    else if (type.getBasicType() == EbtSampler2D)
  1.3054 +    {
  1.3055 +        return GL_SAMPLER_2D;
  1.3056 +    }
  1.3057 +    else if (type.getBasicType() == EbtSamplerCube)
  1.3058 +    {
  1.3059 +        return GL_SAMPLER_CUBE;
  1.3060 +    }
  1.3061 +    else UNREACHABLE();
  1.3062 +
  1.3063 +    return GL_NONE;
  1.3064 +}
  1.3065 +
  1.3066 +GLenum OutputHLSL::glVariablePrecision(const TType &type)
  1.3067 +{
  1.3068 +    if (type.getBasicType() == EbtFloat)
  1.3069 +    {
  1.3070 +        switch (type.getPrecision())
  1.3071 +        {
  1.3072 +          case EbpHigh:   return GL_HIGH_FLOAT;
  1.3073 +          case EbpMedium: return GL_MEDIUM_FLOAT;
  1.3074 +          case EbpLow:    return GL_LOW_FLOAT;
  1.3075 +          case EbpUndefined:
  1.3076 +            // Should be defined as the default precision by the parser
  1.3077 +          default: UNREACHABLE();
  1.3078 +        }
  1.3079 +    }
  1.3080 +    else if (type.getBasicType() == EbtInt)
  1.3081 +    {
  1.3082 +        switch (type.getPrecision())
  1.3083 +        {
  1.3084 +          case EbpHigh:   return GL_HIGH_INT;
  1.3085 +          case EbpMedium: return GL_MEDIUM_INT;
  1.3086 +          case EbpLow:    return GL_LOW_INT;
  1.3087 +          case EbpUndefined:
  1.3088 +            // Should be defined as the default precision by the parser
  1.3089 +          default: UNREACHABLE();
  1.3090 +        }
  1.3091 +    }
  1.3092 +
  1.3093 +    // Other types (boolean, sampler) don't have a precision
  1.3094 +    return GL_NONE;
  1.3095 +}
  1.3096 +
  1.3097 +}

mercurial