michael@0: // michael@0: // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: #include "compiler/OutputHLSL.h" michael@0: michael@0: #include "common/angleutils.h" michael@0: #include "compiler/compiler_debug.h" michael@0: #include "compiler/DetectDiscontinuity.h" michael@0: #include "compiler/InfoSink.h" michael@0: #include "compiler/SearchSymbol.h" michael@0: #include "compiler/UnfoldShortCircuit.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace sh michael@0: { michael@0: // Integer to TString conversion michael@0: TString str(int i) michael@0: { michael@0: char buffer[20]; michael@0: snprintf(buffer, sizeof(buffer), "%d", i); michael@0: return buffer; michael@0: } michael@0: michael@0: OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType) michael@0: : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType) michael@0: { michael@0: mUnfoldShortCircuit = new UnfoldShortCircuit(context, this); michael@0: mInsideFunction = false; michael@0: michael@0: mUsesTexture2D = false; michael@0: mUsesTexture2D_bias = false; michael@0: mUsesTexture2DProj = false; michael@0: mUsesTexture2DProj_bias = false; michael@0: mUsesTexture2DProjLod = false; michael@0: mUsesTexture2DLod = false; michael@0: mUsesTextureCube = false; michael@0: mUsesTextureCube_bias = false; michael@0: mUsesTextureCubeLod = false; michael@0: mUsesTexture2DLod0 = false; michael@0: mUsesTexture2DLod0_bias = false; michael@0: mUsesTexture2DProjLod0 = false; michael@0: mUsesTexture2DProjLod0_bias = false; michael@0: mUsesTextureCubeLod0 = false; michael@0: mUsesTextureCubeLod0_bias = false; michael@0: mUsesFragColor = false; michael@0: mUsesFragData = false; michael@0: mUsesDepthRange = false; michael@0: mUsesFragCoord = false; michael@0: mUsesPointCoord = false; michael@0: mUsesFrontFacing = false; michael@0: mUsesPointSize = false; michael@0: mUsesFragDepth = false; michael@0: mUsesXor = false; michael@0: mUsesMod1 = false; michael@0: mUsesMod2v = false; michael@0: mUsesMod2f = false; michael@0: mUsesMod3v = false; michael@0: mUsesMod3f = false; michael@0: mUsesMod4v = false; michael@0: mUsesMod4f = false; michael@0: mUsesFaceforward1 = false; michael@0: mUsesFaceforward2 = false; michael@0: mUsesFaceforward3 = false; michael@0: mUsesFaceforward4 = false; michael@0: mUsesAtan2_1 = false; michael@0: mUsesAtan2_2 = false; michael@0: mUsesAtan2_3 = false; michael@0: mUsesAtan2_4 = false; michael@0: michael@0: mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1; michael@0: michael@0: mScopeDepth = 0; michael@0: michael@0: mUniqueIndex = 0; michael@0: michael@0: mContainsLoopDiscontinuity = false; michael@0: mOutputLod0Function = false; michael@0: mInsideDiscontinuousLoop = false; michael@0: michael@0: mExcessiveLoopIndex = NULL; michael@0: michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: if (mContext.shaderType == SH_FRAGMENT_SHADER) michael@0: { michael@0: mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront michael@0: } michael@0: else michael@0: { michael@0: mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mUniformRegister = 0; michael@0: } michael@0: michael@0: mSamplerRegister = 0; michael@0: } michael@0: michael@0: OutputHLSL::~OutputHLSL() michael@0: { michael@0: delete mUnfoldShortCircuit; michael@0: } michael@0: michael@0: void OutputHLSL::output() michael@0: { michael@0: mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot); michael@0: michael@0: mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header michael@0: header(); michael@0: michael@0: mContext.infoSink().obj << mHeader.c_str(); michael@0: mContext.infoSink().obj << mBody.c_str(); michael@0: } michael@0: michael@0: TInfoSinkBase &OutputHLSL::getBodyStream() michael@0: { michael@0: return mBody; michael@0: } michael@0: michael@0: const ActiveUniforms &OutputHLSL::getUniforms() michael@0: { michael@0: return mActiveUniforms; michael@0: } michael@0: michael@0: int OutputHLSL::vectorSize(const TType &type) const michael@0: { michael@0: int elementSize = type.isMatrix() ? type.getNominalSize() : 1; michael@0: int arraySize = type.isArray() ? type.getArraySize() : 1; michael@0: michael@0: return elementSize * arraySize; michael@0: } michael@0: michael@0: void OutputHLSL::header() michael@0: { michael@0: ShShaderType shaderType = mContext.shaderType; michael@0: TInfoSinkBase &out = mHeader; michael@0: michael@0: for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++) michael@0: { michael@0: out << *structDeclaration; michael@0: } michael@0: michael@0: for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++) michael@0: { michael@0: out << *constructor; michael@0: } michael@0: michael@0: TString uniforms; michael@0: TString varyings; michael@0: TString attributes; michael@0: michael@0: for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++) michael@0: { michael@0: const TType &type = uniform->second->getType(); michael@0: const TString &name = uniform->second->getSymbol(); michael@0: michael@0: if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture michael@0: { michael@0: int index = samplerRegister(mReferencedUniforms[name]); michael@0: michael@0: uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) + michael@0: " : register(s" + str(index) + ");\n"; michael@0: michael@0: uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) + michael@0: " : register(t" + str(index) + ");\n"; michael@0: } michael@0: else michael@0: { michael@0: uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + michael@0: " : register(" + registerString(mReferencedUniforms[name]) + ");\n"; michael@0: } michael@0: } michael@0: michael@0: for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++) michael@0: { michael@0: const TType &type = varying->second->getType(); michael@0: const TString &name = varying->second->getSymbol(); michael@0: michael@0: // Program linking depends on this exact format michael@0: varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n"; michael@0: } michael@0: michael@0: for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++) michael@0: { michael@0: const TType &type = attribute->second->getType(); michael@0: const TString &name = attribute->second->getSymbol(); michael@0: michael@0: attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n"; michael@0: } michael@0: michael@0: if (shaderType == SH_FRAGMENT_SHADER) michael@0: { michael@0: TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers"); michael@0: const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire)); michael@0: michael@0: const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1; michael@0: michael@0: out << "// Varyings\n"; michael@0: out << varyings; michael@0: out << "\n" michael@0: "static float4 gl_Color[" << numColorValues << "] =\n" michael@0: "{\n"; michael@0: for (unsigned int i = 0; i < numColorValues; i++) michael@0: { michael@0: out << " float4(0, 0, 0, 0)"; michael@0: if (i + 1 != numColorValues) michael@0: { michael@0: out << ","; michael@0: } michael@0: out << "\n"; michael@0: } michael@0: out << "};\n"; michael@0: michael@0: if (mUsesFragDepth) michael@0: { michael@0: out << "static float gl_Depth = 0.0;\n"; michael@0: } michael@0: michael@0: if (mUsesFragCoord) michael@0: { michael@0: out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"; michael@0: } michael@0: michael@0: if (mUsesPointCoord) michael@0: { michael@0: out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n"; michael@0: } michael@0: michael@0: if (mUsesFrontFacing) michael@0: { michael@0: out << "static bool gl_FrontFacing = false;\n"; michael@0: } michael@0: michael@0: out << "\n"; michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "struct gl_DepthRangeParameters\n" michael@0: "{\n" michael@0: " float near;\n" michael@0: " float far;\n" michael@0: " float diff;\n" michael@0: "};\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "cbuffer DriverConstants : register(b1)\n" michael@0: "{\n"; michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << " float3 dx_DepthRange : packoffset(c0);\n"; michael@0: } michael@0: michael@0: if (mUsesFragCoord) michael@0: { michael@0: out << " float4 dx_ViewCoords : packoffset(c1);\n"; michael@0: } michael@0: michael@0: if (mUsesFragCoord || mUsesFrontFacing) michael@0: { michael@0: out << " float3 dx_DepthFront : packoffset(c2);\n"; michael@0: } michael@0: michael@0: out << "};\n"; michael@0: } michael@0: else michael@0: { michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "uniform float3 dx_DepthRange : register(c0);"; michael@0: } michael@0: michael@0: if (mUsesFragCoord) michael@0: { michael@0: out << "uniform float4 dx_ViewCoords : register(c1);\n"; michael@0: } michael@0: michael@0: if (mUsesFragCoord || mUsesFrontFacing) michael@0: { michael@0: out << "uniform float3 dx_DepthFront : register(c2);\n"; michael@0: } michael@0: } michael@0: michael@0: out << "\n"; michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" michael@0: "\n"; michael@0: } michael@0: michael@0: out << uniforms; michael@0: out << "\n"; michael@0: michael@0: if (mUsesTexture2D) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(sampler2D s, float2 t)\n" michael@0: "{\n" michael@0: " return tex2D(s, t);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n" michael@0: "{\n" michael@0: " return t.Sample(s, uv);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2D_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleBias(s, uv, bias);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProj) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n" michael@0: "{\n" michael@0: " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(sampler2D s, float4 t)\n" michael@0: "{\n" michael@0: " return tex2Dproj(s, t);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.Sample(s, float2(uvw.x / uvw.z, uvw.y / uvw.z));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n" michael@0: "{\n" michael@0: " return t.Sample(s, float2(uvw.x / uvw.w, uvw.y / uvw.w));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProj_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dbias(s, float4(t.x / t.z, t.y / t.z, 0, bias));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dbias(s, float4(t.x / t.w, t.y / t.w, 0, bias));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleBias(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), bias);\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleBias(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), bias);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCube) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n" michael@0: "{\n" michael@0: " return texCUBE(s, t);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.Sample(s, uvw);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCube_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n" michael@0: "{\n" michael@0: " return texCUBEbias(s, float4(t.x, t.y, t.z, bias));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleBias(s, uvw, bias);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: // These *Lod0 intrinsics are not available in GL fragment shaders. michael@0: // They are used to sample using discontinuous texture coordinates. michael@0: if (mUsesTexture2DLod0) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uv, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DLod0_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uv, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProjLod0) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float4 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProjLod0_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float3 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float4 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCubeLod0) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n" michael@0: "{\n" michael@0: " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uvw, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCubeLod0_bias) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n" michael@0: "{\n" michael@0: " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw, float bias)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uvw, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (usingMRTExtension && mNumRenderTargets > 1) michael@0: { michael@0: out << "#define GL_USES_MRT\n"; michael@0: } michael@0: michael@0: if (mUsesFragColor) michael@0: { michael@0: out << "#define GL_USES_FRAG_COLOR\n"; michael@0: } michael@0: michael@0: if (mUsesFragData) michael@0: { michael@0: out << "#define GL_USES_FRAG_DATA\n"; michael@0: } michael@0: } michael@0: else // Vertex shader michael@0: { michael@0: out << "// Attributes\n"; michael@0: out << attributes; michael@0: out << "\n" michael@0: "static float4 gl_Position = float4(0, 0, 0, 0);\n"; michael@0: michael@0: if (mUsesPointSize) michael@0: { michael@0: out << "static float gl_PointSize = float(1);\n"; michael@0: } michael@0: michael@0: out << "\n" michael@0: "// Varyings\n"; michael@0: out << varyings; michael@0: out << "\n"; michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "struct gl_DepthRangeParameters\n" michael@0: "{\n" michael@0: " float near;\n" michael@0: " float far;\n" michael@0: " float diff;\n" michael@0: "};\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "cbuffer DriverConstants : register(b1)\n" michael@0: "{\n" michael@0: " float3 dx_DepthRange : packoffset(c0);\n" michael@0: "};\n" michael@0: "\n"; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "uniform float3 dx_DepthRange : register(c0);\n"; michael@0: } michael@0: michael@0: out << "uniform float4 dx_ViewAdjust : register(c1);\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" michael@0: "\n"; michael@0: } michael@0: michael@0: out << uniforms; michael@0: out << "\n"; michael@0: michael@0: if (mUsesTexture2D) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(sampler2D s, float2 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uv, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DLod) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x, t.y, 0, lod));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DLod(Texture2D t, SamplerState s, float2 uv, float lod)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uv, lod);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProj) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(sampler2D s, float4 t)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTexture2DProjLod) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, lod));\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n" michael@0: "{\n" michael@0: " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, lod));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float lod)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), lod);\n" michael@0: "}\n" michael@0: "\n" michael@0: "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), lod);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCube) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n" michael@0: "{\n" michael@0: " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uvw, 0);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: if (mUsesTextureCubeLod) michael@0: { michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n" michael@0: "{\n" michael@0: " return texCUBElod(s, float4(t.x, t.y, t.z, lod));\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else if (mOutputType == SH_HLSL11_OUTPUT) michael@0: { michael@0: out << "float4 gl_textureCubeLod(TextureCube t, SamplerState s, float3 uvw, float lod)\n" michael@0: "{\n" michael@0: " return t.SampleLevel(s, uvw, lod);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: } michael@0: michael@0: if (mUsesFragCoord) michael@0: { michael@0: out << "#define GL_USES_FRAG_COORD\n"; michael@0: } michael@0: michael@0: if (mUsesPointCoord) michael@0: { michael@0: out << "#define GL_USES_POINT_COORD\n"; michael@0: } michael@0: michael@0: if (mUsesFrontFacing) michael@0: { michael@0: out << "#define GL_USES_FRONT_FACING\n"; michael@0: } michael@0: michael@0: if (mUsesPointSize) michael@0: { michael@0: out << "#define GL_USES_POINT_SIZE\n"; michael@0: } michael@0: michael@0: if (mUsesFragDepth) michael@0: { michael@0: out << "#define GL_USES_FRAG_DEPTH\n"; michael@0: } michael@0: michael@0: if (mUsesDepthRange) michael@0: { michael@0: out << "#define GL_USES_DEPTH_RANGE\n"; michael@0: } michael@0: michael@0: if (mUsesXor) michael@0: { michael@0: out << "bool xor(bool p, bool q)\n" michael@0: "{\n" michael@0: " return (p || q) && !(p && q);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod1) michael@0: { michael@0: out << "float mod(float x, float y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod2v) michael@0: { michael@0: out << "float2 mod(float2 x, float2 y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod2f) michael@0: { michael@0: out << "float2 mod(float2 x, float y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod3v) michael@0: { michael@0: out << "float3 mod(float3 x, float3 y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod3f) michael@0: { michael@0: out << "float3 mod(float3 x, float y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod4v) michael@0: { michael@0: out << "float4 mod(float4 x, float4 y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesMod4f) michael@0: { michael@0: out << "float4 mod(float4 x, float y)\n" michael@0: "{\n" michael@0: " return x - y * floor(x / y);\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesFaceforward1) michael@0: { michael@0: out << "float faceforward(float N, float I, float Nref)\n" michael@0: "{\n" michael@0: " if(dot(Nref, I) >= 0)\n" michael@0: " {\n" michael@0: " return -N;\n" michael@0: " }\n" michael@0: " else\n" michael@0: " {\n" michael@0: " return N;\n" michael@0: " }\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesFaceforward2) michael@0: { michael@0: out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n" michael@0: "{\n" michael@0: " if(dot(Nref, I) >= 0)\n" michael@0: " {\n" michael@0: " return -N;\n" michael@0: " }\n" michael@0: " else\n" michael@0: " {\n" michael@0: " return N;\n" michael@0: " }\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesFaceforward3) michael@0: { michael@0: out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n" michael@0: "{\n" michael@0: " if(dot(Nref, I) >= 0)\n" michael@0: " {\n" michael@0: " return -N;\n" michael@0: " }\n" michael@0: " else\n" michael@0: " {\n" michael@0: " return N;\n" michael@0: " }\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesFaceforward4) michael@0: { michael@0: out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n" michael@0: "{\n" michael@0: " if(dot(Nref, I) >= 0)\n" michael@0: " {\n" michael@0: " return -N;\n" michael@0: " }\n" michael@0: " else\n" michael@0: " {\n" michael@0: " return N;\n" michael@0: " }\n" michael@0: "}\n" michael@0: "\n"; michael@0: } michael@0: michael@0: if (mUsesAtan2_1) michael@0: { michael@0: out << "float atanyx(float y, float x)\n" michael@0: "{\n" michael@0: " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN michael@0: " return atan2(y, x);\n" michael@0: "}\n"; michael@0: } michael@0: michael@0: if (mUsesAtan2_2) michael@0: { michael@0: out << "float2 atanyx(float2 y, float2 x)\n" michael@0: "{\n" michael@0: " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n" michael@0: " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n" michael@0: " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n" michael@0: "}\n"; michael@0: } michael@0: michael@0: if (mUsesAtan2_3) michael@0: { michael@0: out << "float3 atanyx(float3 y, float3 x)\n" michael@0: "{\n" michael@0: " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n" michael@0: " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n" michael@0: " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n" michael@0: " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n" michael@0: "}\n"; michael@0: } michael@0: michael@0: if (mUsesAtan2_4) michael@0: { michael@0: out << "float4 atanyx(float4 y, float4 x)\n" michael@0: "{\n" michael@0: " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n" michael@0: " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n" michael@0: " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n" michael@0: " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n" michael@0: " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n" michael@0: "}\n"; michael@0: } michael@0: } michael@0: michael@0: void OutputHLSL::visitSymbol(TIntermSymbol *node) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: TString name = node->getSymbol(); michael@0: michael@0: if (name == "gl_FragColor") michael@0: { michael@0: out << "gl_Color[0]"; michael@0: mUsesFragColor = true; michael@0: } michael@0: else if (name == "gl_FragData") michael@0: { michael@0: out << "gl_Color"; michael@0: mUsesFragData = true; michael@0: } michael@0: else if (name == "gl_DepthRange") michael@0: { michael@0: mUsesDepthRange = true; michael@0: out << name; michael@0: } michael@0: else if (name == "gl_FragCoord") michael@0: { michael@0: mUsesFragCoord = true; michael@0: out << name; michael@0: } michael@0: else if (name == "gl_PointCoord") michael@0: { michael@0: mUsesPointCoord = true; michael@0: out << name; michael@0: } michael@0: else if (name == "gl_FrontFacing") michael@0: { michael@0: mUsesFrontFacing = true; michael@0: out << name; michael@0: } michael@0: else if (name == "gl_PointSize") michael@0: { michael@0: mUsesPointSize = true; michael@0: out << name; michael@0: } michael@0: else if (name == "gl_FragDepthEXT") michael@0: { michael@0: mUsesFragDepth = true; michael@0: out << "gl_Depth"; michael@0: } michael@0: else michael@0: { michael@0: TQualifier qualifier = node->getQualifier(); michael@0: michael@0: if (qualifier == EvqUniform) michael@0: { michael@0: mReferencedUniforms[name] = node; michael@0: out << decorateUniform(name, node->getType()); michael@0: } michael@0: else if (qualifier == EvqAttribute) michael@0: { michael@0: mReferencedAttributes[name] = node; michael@0: out << decorate(name); michael@0: } michael@0: else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn) michael@0: { michael@0: mReferencedVaryings[name] = node; michael@0: out << decorate(name); michael@0: } michael@0: else michael@0: { michael@0: out << decorate(name); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break; michael@0: case EOpInitialize: michael@0: if (visit == PreVisit) michael@0: { michael@0: // GLSL allows to write things like "float x = x;" where a new variable x is defined michael@0: // and the value of an existing variable x is assigned. HLSL uses C semantics (the michael@0: // new variable is created before the assignment is evaluated), so we need to convert michael@0: // this to "float t = x, x = t;". michael@0: michael@0: TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); michael@0: TIntermTyped *expression = node->getRight(); michael@0: michael@0: sh::SearchSymbol searchSymbol(symbolNode->getSymbol()); michael@0: expression->traverse(&searchSymbol); michael@0: bool sameSymbol = searchSymbol.foundMatch(); michael@0: michael@0: if (sameSymbol) michael@0: { michael@0: // Type already printed michael@0: out << "t" + str(mUniqueIndex) + " = "; michael@0: expression->traverse(this); michael@0: out << ", "; michael@0: symbolNode->traverse(this); michael@0: out << " = t" + str(mUniqueIndex); michael@0: michael@0: mUniqueIndex++; michael@0: return false; michael@0: } michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << " = "; michael@0: } michael@0: break; michael@0: case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break; michael@0: case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break; michael@0: case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break; michael@0: case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; michael@0: case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; michael@0: case EOpVectorTimesMatrixAssign: michael@0: if (visit == PreVisit) michael@0: { michael@0: out << "("; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << " = mul("; michael@0: node->getLeft()->traverse(this); michael@0: out << ", transpose("; michael@0: } michael@0: else michael@0: { michael@0: out << ")))"; michael@0: } michael@0: break; michael@0: case EOpMatrixTimesMatrixAssign: michael@0: if (visit == PreVisit) michael@0: { michael@0: out << "("; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << " = mul("; michael@0: node->getLeft()->traverse(this); michael@0: out << ", "; michael@0: } michael@0: else michael@0: { michael@0: out << "))"; michael@0: } michael@0: break; michael@0: case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break; michael@0: case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break; michael@0: case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break; michael@0: case EOpIndexDirectStruct: michael@0: if (visit == InVisit) michael@0: { michael@0: const TStructure* structure = node->getLeft()->getType().getStruct(); michael@0: const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); michael@0: const TField* field = structure->fields()[index->getIConst(0)]; michael@0: out << "." + decorateField(field->name(), node->getLeft()->getType()); michael@0: michael@0: return false; michael@0: } michael@0: break; michael@0: case EOpVectorSwizzle: michael@0: if (visit == InVisit) michael@0: { michael@0: out << "."; michael@0: michael@0: TIntermAggregate *swizzle = node->getRight()->getAsAggregate(); michael@0: michael@0: if (swizzle) michael@0: { michael@0: TIntermSequence &sequence = swizzle->getSequence(); michael@0: michael@0: for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) michael@0: { michael@0: TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); michael@0: michael@0: if (element) michael@0: { michael@0: int i = element->getIConst(0); michael@0: michael@0: switch (i) michael@0: { michael@0: case 0: out << "x"; break; michael@0: case 1: out << "y"; break; michael@0: case 2: out << "z"; break; michael@0: case 3: out << "w"; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: return false; // Fully processed michael@0: } michael@0: break; michael@0: case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break; michael@0: case EOpSub: outputTriplet(visit, "(", " - ", ")"); break; michael@0: case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; michael@0: case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break; michael@0: case EOpEqual: michael@0: case EOpNotEqual: michael@0: if (node->getLeft()->isScalar()) michael@0: { michael@0: if (node->getOp() == EOpEqual) michael@0: { michael@0: outputTriplet(visit, "(", " == ", ")"); michael@0: } michael@0: else michael@0: { michael@0: outputTriplet(visit, "(", " != ", ")"); michael@0: } michael@0: } michael@0: else if (node->getLeft()->getBasicType() == EbtStruct) michael@0: { michael@0: if (node->getOp() == EOpEqual) michael@0: { michael@0: out << "("; michael@0: } michael@0: else michael@0: { michael@0: out << "!("; michael@0: } michael@0: michael@0: const TFieldList &fields = node->getLeft()->getType().getStruct()->fields(); michael@0: michael@0: for (size_t i = 0; i < fields.size(); i++) michael@0: { michael@0: const TField *field = fields[i]; michael@0: michael@0: node->getLeft()->traverse(this); michael@0: out << "." + decorateField(field->name(), node->getLeft()->getType()) + " == "; michael@0: node->getRight()->traverse(this); michael@0: out << "." + decorateField(field->name(), node->getLeft()->getType()); michael@0: michael@0: if (i < fields.size() - 1) michael@0: { michael@0: out << " && "; michael@0: } michael@0: } michael@0: michael@0: out << ")"; michael@0: michael@0: return false; michael@0: } michael@0: else michael@0: { michael@0: ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector()); michael@0: michael@0: if (node->getOp() == EOpEqual) michael@0: { michael@0: outputTriplet(visit, "all(", " == ", ")"); michael@0: } michael@0: else michael@0: { michael@0: outputTriplet(visit, "!all(", " == ", ")"); michael@0: } michael@0: } michael@0: break; michael@0: case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; michael@0: case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; michael@0: case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; michael@0: case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; michael@0: case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; michael@0: case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; michael@0: case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break; michael@0: case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break; michael@0: case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break; michael@0: case EOpLogicalOr: michael@0: out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); michael@0: return false; michael@0: case EOpLogicalXor: michael@0: mUsesXor = true; michael@0: outputTriplet(visit, "xor(", ", ", ")"); michael@0: break; michael@0: case EOpLogicalAnd: michael@0: out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); michael@0: return false; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) michael@0: { michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpNegative: outputTriplet(visit, "(-", "", ")"); break; michael@0: case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break; michael@0: case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break; michael@0: case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break; michael@0: case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break; michael@0: case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break; michael@0: case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break; michael@0: case EOpConvIntToBool: michael@0: case EOpConvFloatToBool: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: outputTriplet(visit, "bool(", "", ")"); break; michael@0: case 2: outputTriplet(visit, "bool2(", "", ")"); break; michael@0: case 3: outputTriplet(visit, "bool3(", "", ")"); break; michael@0: case 4: outputTriplet(visit, "bool4(", "", ")"); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case EOpConvBoolToFloat: michael@0: case EOpConvIntToFloat: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: outputTriplet(visit, "float(", "", ")"); break; michael@0: case 2: outputTriplet(visit, "float2(", "", ")"); break; michael@0: case 3: outputTriplet(visit, "float3(", "", ")"); break; michael@0: case 4: outputTriplet(visit, "float4(", "", ")"); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case EOpConvFloatToInt: michael@0: case EOpConvBoolToInt: michael@0: switch (node->getOperand()->getType().getNominalSize()) michael@0: { michael@0: case 1: outputTriplet(visit, "int(", "", ")"); break; michael@0: case 2: outputTriplet(visit, "int2(", "", ")"); break; michael@0: case 3: outputTriplet(visit, "int3(", "", ")"); break; michael@0: case 4: outputTriplet(visit, "int4(", "", ")"); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break; michael@0: case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break; michael@0: case EOpSin: outputTriplet(visit, "sin(", "", ")"); break; michael@0: case EOpCos: outputTriplet(visit, "cos(", "", ")"); break; michael@0: case EOpTan: outputTriplet(visit, "tan(", "", ")"); break; michael@0: case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break; michael@0: case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break; michael@0: case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break; michael@0: case EOpExp: outputTriplet(visit, "exp(", "", ")"); break; michael@0: case EOpLog: outputTriplet(visit, "log(", "", ")"); break; michael@0: case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break; michael@0: case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break; michael@0: case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break; michael@0: case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break; michael@0: case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break; michael@0: case EOpSign: outputTriplet(visit, "sign(", "", ")"); break; michael@0: case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break; michael@0: case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break; michael@0: case EOpFract: outputTriplet(visit, "frac(", "", ")"); break; michael@0: case EOpLength: outputTriplet(visit, "length(", "", ")"); break; michael@0: case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break; michael@0: case EOpDFdx: michael@0: if(mInsideDiscontinuousLoop || mOutputLod0Function) michael@0: { michael@0: outputTriplet(visit, "(", "", ", 0.0)"); michael@0: } michael@0: else michael@0: { michael@0: outputTriplet(visit, "ddx(", "", ")"); michael@0: } michael@0: break; michael@0: case EOpDFdy: michael@0: if(mInsideDiscontinuousLoop || mOutputLod0Function) michael@0: { michael@0: outputTriplet(visit, "(", "", ", 0.0)"); michael@0: } michael@0: else michael@0: { michael@0: outputTriplet(visit, "ddy(", "", ")"); michael@0: } michael@0: break; michael@0: case EOpFwidth: michael@0: if(mInsideDiscontinuousLoop || mOutputLod0Function) michael@0: { michael@0: outputTriplet(visit, "(", "", ", 0.0)"); michael@0: } michael@0: else michael@0: { michael@0: outputTriplet(visit, "fwidth(", "", ")"); michael@0: } michael@0: break; michael@0: case EOpAny: outputTriplet(visit, "any(", "", ")"); break; michael@0: case EOpAll: outputTriplet(visit, "all(", "", ")"); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: switch (node->getOp()) michael@0: { michael@0: case EOpSequence: michael@0: { michael@0: if (mInsideFunction) michael@0: { michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << "{\n"; michael@0: michael@0: mScopeDepth++; michael@0: michael@0: if (mScopeBracket.size() < mScopeDepth) michael@0: { michael@0: mScopeBracket.push_back(0); // New scope level michael@0: } michael@0: else michael@0: { michael@0: mScopeBracket[mScopeDepth - 1]++; // New scope at existing level michael@0: } michael@0: } michael@0: michael@0: for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++) michael@0: { michael@0: outputLineDirective((*sit)->getLine().first_line); michael@0: michael@0: traverseStatements(*sit); michael@0: michael@0: out << ";\n"; michael@0: } michael@0: michael@0: if (mInsideFunction) michael@0: { michael@0: outputLineDirective(node->getLine().last_line); michael@0: out << "}\n"; michael@0: michael@0: mScopeDepth--; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: case EOpDeclaration: michael@0: if (visit == PreVisit) michael@0: { michael@0: TIntermSequence &sequence = node->getSequence(); michael@0: TIntermTyped *variable = sequence[0]->getAsTyped(); michael@0: michael@0: if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal)) michael@0: { michael@0: if (variable->getType().getStruct()) michael@0: { michael@0: addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL); michael@0: } michael@0: michael@0: if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration michael@0: { michael@0: if (!mInsideFunction) michael@0: { michael@0: out << "static "; michael@0: } michael@0: michael@0: out << typeString(variable->getType()) + " "; michael@0: michael@0: for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) michael@0: { michael@0: TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); michael@0: michael@0: if (symbol) michael@0: { michael@0: symbol->traverse(this); michael@0: out << arrayString(symbol->getType()); michael@0: out << " = " + initializer(variable->getType()); michael@0: } michael@0: else michael@0: { michael@0: (*sit)->traverse(this); michael@0: } michael@0: michael@0: if (*sit != sequence.back()) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: } michael@0: else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration michael@0: { michael@0: // Already added to constructor map michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else if (variable && (variable->getQualifier() == EvqVaryingOut || variable->getQualifier() == EvqInvariantVaryingOut)) michael@0: { michael@0: for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) michael@0: { michael@0: TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); michael@0: michael@0: if (symbol) michael@0: { michael@0: // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking michael@0: mReferencedVaryings[symbol->getSymbol()] = symbol; michael@0: } michael@0: else michael@0: { michael@0: (*sit)->traverse(this); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << ", "; michael@0: } michael@0: break; michael@0: case EOpPrototype: michael@0: if (visit == PreVisit) michael@0: { michael@0: out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "("); michael@0: michael@0: TIntermSequence &arguments = node->getSequence(); michael@0: michael@0: for (unsigned int i = 0; i < arguments.size(); i++) michael@0: { michael@0: TIntermSymbol *symbol = arguments[i]->getAsSymbolNode(); michael@0: michael@0: if (symbol) michael@0: { michael@0: out << argumentString(symbol); michael@0: michael@0: if (i < arguments.size() - 1) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: out << ");\n"; michael@0: michael@0: // Also prototype the Lod0 variant if needed michael@0: if (mContainsLoopDiscontinuity && !mOutputLod0Function) michael@0: { michael@0: mOutputLod0Function = true; michael@0: node->traverse(this); michael@0: mOutputLod0Function = false; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: break; michael@0: case EOpComma: outputTriplet(visit, "(", ", ", ")"); break; michael@0: case EOpFunction: michael@0: { michael@0: TString name = TFunction::unmangleName(node->getName()); michael@0: michael@0: out << typeString(node->getType()) << " "; michael@0: michael@0: if (name == "main") michael@0: { michael@0: out << "gl_main("; michael@0: } michael@0: else michael@0: { michael@0: out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "("); michael@0: } michael@0: michael@0: TIntermSequence &sequence = node->getSequence(); michael@0: TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence(); michael@0: michael@0: for (unsigned int i = 0; i < arguments.size(); i++) michael@0: { michael@0: TIntermSymbol *symbol = arguments[i]->getAsSymbolNode(); michael@0: michael@0: if (symbol) michael@0: { michael@0: if (symbol->getType().getStruct()) michael@0: { michael@0: addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL); michael@0: } michael@0: michael@0: out << argumentString(symbol); michael@0: michael@0: if (i < arguments.size() - 1) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: out << ")\n" michael@0: "{\n"; michael@0: michael@0: if (sequence.size() > 1) michael@0: { michael@0: mInsideFunction = true; michael@0: sequence[1]->traverse(this); michael@0: mInsideFunction = false; michael@0: } michael@0: michael@0: out << "}\n"; michael@0: michael@0: if (mContainsLoopDiscontinuity && !mOutputLod0Function) michael@0: { michael@0: if (name != "main") michael@0: { michael@0: mOutputLod0Function = true; michael@0: node->traverse(this); michael@0: mOutputLod0Function = false; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: break; michael@0: case EOpFunctionCall: michael@0: { michael@0: TString name = TFunction::unmangleName(node->getName()); michael@0: bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; michael@0: michael@0: if (node->isUserDefined()) michael@0: { michael@0: out << decorate(name) << (lod0 ? "Lod0(" : "("); michael@0: } michael@0: else michael@0: { michael@0: if (name == "texture2D") michael@0: { michael@0: if (!lod0) michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTexture2D = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2D_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2D("; michael@0: } michael@0: else michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTexture2DLod0 = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2DLod0_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2DLod0("; michael@0: } michael@0: } michael@0: else if (name == "texture2DProj") michael@0: { michael@0: if (!lod0) michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTexture2DProj = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2DProj_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2DProj("; michael@0: } michael@0: else michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTexture2DProjLod0 = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2DProjLod0_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2DProjLod0("; michael@0: } michael@0: } michael@0: else if (name == "textureCube") michael@0: { michael@0: if (!lod0) michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTextureCube = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTextureCube_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_textureCube("; michael@0: } michael@0: else michael@0: { michael@0: if (node->getSequence().size() == 2) michael@0: { michael@0: mUsesTextureCubeLod0 = true; michael@0: } michael@0: else if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTextureCubeLod0_bias = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_textureCubeLod0("; michael@0: } michael@0: } michael@0: else if (name == "texture2DLod") michael@0: { michael@0: if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2DLod = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2DLod("; michael@0: } michael@0: else if (name == "texture2DProjLod") michael@0: { michael@0: if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTexture2DProjLod = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_texture2DProjLod("; michael@0: } michael@0: else if (name == "textureCubeLod") michael@0: { michael@0: if (node->getSequence().size() == 3) michael@0: { michael@0: mUsesTextureCubeLod = true; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: out << "gl_textureCubeLod("; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: TIntermSequence &arguments = node->getSequence(); michael@0: michael@0: for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++) michael@0: { michael@0: if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType())) michael@0: { michael@0: out << "texture_"; michael@0: (*arg)->traverse(this); michael@0: out << ", sampler_"; michael@0: } michael@0: michael@0: (*arg)->traverse(this); michael@0: michael@0: if (arg < arguments.end() - 1) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: michael@0: out << ")"; michael@0: michael@0: return false; michael@0: } michael@0: break; michael@0: case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break; michael@0: case EOpConstructFloat: michael@0: addConstructor(node->getType(), "vec1", &node->getSequence()); michael@0: outputTriplet(visit, "vec1(", "", ")"); michael@0: break; michael@0: case EOpConstructVec2: michael@0: addConstructor(node->getType(), "vec2", &node->getSequence()); michael@0: outputTriplet(visit, "vec2(", ", ", ")"); michael@0: break; michael@0: case EOpConstructVec3: michael@0: addConstructor(node->getType(), "vec3", &node->getSequence()); michael@0: outputTriplet(visit, "vec3(", ", ", ")"); michael@0: break; michael@0: case EOpConstructVec4: michael@0: addConstructor(node->getType(), "vec4", &node->getSequence()); michael@0: outputTriplet(visit, "vec4(", ", ", ")"); michael@0: break; michael@0: case EOpConstructBool: michael@0: addConstructor(node->getType(), "bvec1", &node->getSequence()); michael@0: outputTriplet(visit, "bvec1(", "", ")"); michael@0: break; michael@0: case EOpConstructBVec2: michael@0: addConstructor(node->getType(), "bvec2", &node->getSequence()); michael@0: outputTriplet(visit, "bvec2(", ", ", ")"); michael@0: break; michael@0: case EOpConstructBVec3: michael@0: addConstructor(node->getType(), "bvec3", &node->getSequence()); michael@0: outputTriplet(visit, "bvec3(", ", ", ")"); michael@0: break; michael@0: case EOpConstructBVec4: michael@0: addConstructor(node->getType(), "bvec4", &node->getSequence()); michael@0: outputTriplet(visit, "bvec4(", ", ", ")"); michael@0: break; michael@0: case EOpConstructInt: michael@0: addConstructor(node->getType(), "ivec1", &node->getSequence()); michael@0: outputTriplet(visit, "ivec1(", "", ")"); michael@0: break; michael@0: case EOpConstructIVec2: michael@0: addConstructor(node->getType(), "ivec2", &node->getSequence()); michael@0: outputTriplet(visit, "ivec2(", ", ", ")"); michael@0: break; michael@0: case EOpConstructIVec3: michael@0: addConstructor(node->getType(), "ivec3", &node->getSequence()); michael@0: outputTriplet(visit, "ivec3(", ", ", ")"); michael@0: break; michael@0: case EOpConstructIVec4: michael@0: addConstructor(node->getType(), "ivec4", &node->getSequence()); michael@0: outputTriplet(visit, "ivec4(", ", ", ")"); michael@0: break; michael@0: case EOpConstructMat2: michael@0: addConstructor(node->getType(), "mat2", &node->getSequence()); michael@0: outputTriplet(visit, "mat2(", ", ", ")"); michael@0: break; michael@0: case EOpConstructMat3: michael@0: addConstructor(node->getType(), "mat3", &node->getSequence()); michael@0: outputTriplet(visit, "mat3(", ", ", ")"); michael@0: break; michael@0: case EOpConstructMat4: michael@0: addConstructor(node->getType(), "mat4", &node->getSequence()); michael@0: outputTriplet(visit, "mat4(", ", ", ")"); michael@0: break; michael@0: case EOpConstructStruct: michael@0: addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence()); michael@0: outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")"); michael@0: break; michael@0: case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; michael@0: case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; michael@0: case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; michael@0: case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; michael@0: case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break; michael@0: case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break; michael@0: case EOpMod: michael@0: { michael@0: // We need to look at the number of components in both arguments michael@0: switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10 michael@0: + node->getSequence()[1]->getAsTyped()->getNominalSize()) michael@0: { michael@0: case 11: mUsesMod1 = true; break; michael@0: case 22: mUsesMod2v = true; break; michael@0: case 21: mUsesMod2f = true; break; michael@0: case 33: mUsesMod3v = true; break; michael@0: case 31: mUsesMod3f = true; break; michael@0: case 44: mUsesMod4v = true; break; michael@0: case 41: mUsesMod4f = true; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: outputTriplet(visit, "mod(", ", ", ")"); michael@0: } michael@0: break; michael@0: case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break; michael@0: case EOpAtan: michael@0: ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator michael@0: switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) michael@0: { michael@0: case 1: mUsesAtan2_1 = true; break; michael@0: case 2: mUsesAtan2_2 = true; break; michael@0: case 3: mUsesAtan2_3 = true; break; michael@0: case 4: mUsesAtan2_4 = true; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: outputTriplet(visit, "atanyx(", ", ", ")"); michael@0: break; michael@0: case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break; michael@0: case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break; michael@0: case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break; michael@0: case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break; michael@0: case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break; michael@0: case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break; michael@0: case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break; michael@0: case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break; michael@0: case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break; michael@0: case EOpFaceForward: michael@0: { michael@0: switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument michael@0: { michael@0: case 1: mUsesFaceforward1 = true; break; michael@0: case 2: mUsesFaceforward2 = true; break; michael@0: case 3: mUsesFaceforward3 = true; break; michael@0: case 4: mUsesFaceforward4 = true; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: outputTriplet(visit, "faceforward(", ", ", ")"); michael@0: } michael@0: break; michael@0: case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break; michael@0: case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break; michael@0: case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: if (node->usesTernaryOperator()) michael@0: { michael@0: out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); michael@0: } michael@0: else // if/else statement michael@0: { michael@0: mUnfoldShortCircuit->traverse(node->getCondition()); michael@0: michael@0: out << "if("; michael@0: michael@0: node->getCondition()->traverse(this); michael@0: michael@0: out << ")\n"; michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << "{\n"; michael@0: michael@0: if (node->getTrueBlock()) michael@0: { michael@0: traverseStatements(node->getTrueBlock()); michael@0: } michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << ";\n}\n"; michael@0: michael@0: if (node->getFalseBlock()) michael@0: { michael@0: out << "else\n"; michael@0: michael@0: outputLineDirective(node->getFalseBlock()->getLine().first_line); michael@0: out << "{\n"; michael@0: michael@0: outputLineDirective(node->getFalseBlock()->getLine().first_line); michael@0: traverseStatements(node->getFalseBlock()); michael@0: michael@0: outputLineDirective(node->getFalseBlock()->getLine().first_line); michael@0: out << ";\n}\n"; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) michael@0: { michael@0: writeConstantUnion(node->getType(), node->getUnionArrayPointer()); michael@0: } michael@0: michael@0: bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) michael@0: { michael@0: bool wasDiscontinuous = mInsideDiscontinuousLoop; michael@0: michael@0: if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop) michael@0: { michael@0: mInsideDiscontinuousLoop = containsLoopDiscontinuity(node); michael@0: } michael@0: michael@0: if (mOutputType == SH_HLSL9_OUTPUT) michael@0: { michael@0: if (handleExcessiveLoop(node)) michael@0: { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: if (node->getType() == ELoopDoWhile) michael@0: { michael@0: out << "{do\n"; michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << "{\n"; michael@0: } michael@0: else michael@0: { michael@0: out << "{for("; michael@0: michael@0: if (node->getInit()) michael@0: { michael@0: node->getInit()->traverse(this); michael@0: } michael@0: michael@0: out << "; "; michael@0: michael@0: if (node->getCondition()) michael@0: { michael@0: node->getCondition()->traverse(this); michael@0: } michael@0: michael@0: out << "; "; michael@0: michael@0: if (node->getExpression()) michael@0: { michael@0: node->getExpression()->traverse(this); michael@0: } michael@0: michael@0: out << ")\n"; michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << "{\n"; michael@0: } michael@0: michael@0: if (node->getBody()) michael@0: { michael@0: traverseStatements(node->getBody()); michael@0: } michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << ";}\n"; michael@0: michael@0: if (node->getType() == ELoopDoWhile) michael@0: { michael@0: outputLineDirective(node->getCondition()->getLine().first_line); michael@0: out << "while(\n"; michael@0: michael@0: node->getCondition()->traverse(this); michael@0: michael@0: out << ");"; michael@0: } michael@0: michael@0: out << "}\n"; michael@0: michael@0: mInsideDiscontinuousLoop = wasDiscontinuous; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: switch (node->getFlowOp()) michael@0: { michael@0: case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break; michael@0: case EOpBreak: michael@0: if (visit == PreVisit) michael@0: { michael@0: if (mExcessiveLoopIndex) michael@0: { michael@0: out << "{Break"; michael@0: mExcessiveLoopIndex->traverse(this); michael@0: out << " = true; break;}\n"; michael@0: } michael@0: else michael@0: { michael@0: out << "break;\n"; michael@0: } michael@0: } michael@0: break; michael@0: case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break; michael@0: case EOpReturn: michael@0: if (visit == PreVisit) michael@0: { michael@0: if (node->getExpression()) michael@0: { michael@0: out << "return "; michael@0: } michael@0: else michael@0: { michael@0: out << "return;\n"; michael@0: } michael@0: } michael@0: else if (visit == PostVisit) michael@0: { michael@0: if (node->getExpression()) michael@0: { michael@0: out << ";\n"; michael@0: } michael@0: } michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void OutputHLSL::traverseStatements(TIntermNode *node) michael@0: { michael@0: if (isSingleStatement(node)) michael@0: { michael@0: mUnfoldShortCircuit->traverse(node); michael@0: } michael@0: michael@0: node->traverse(this); michael@0: } michael@0: michael@0: bool OutputHLSL::isSingleStatement(TIntermNode *node) michael@0: { michael@0: TIntermAggregate *aggregate = node->getAsAggregate(); michael@0: michael@0: if (aggregate) michael@0: { michael@0: if (aggregate->getOp() == EOpSequence) michael@0: { michael@0: return false; michael@0: } michael@0: else michael@0: { michael@0: for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++) michael@0: { michael@0: if (!isSingleStatement(*sit)) michael@0: { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them michael@0: // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254). michael@0: bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node) michael@0: { michael@0: const int MAX_LOOP_ITERATIONS = 254; michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: // Parse loops of the form: michael@0: // for(int index = initial; index [comparator] limit; index += increment) michael@0: TIntermSymbol *index = NULL; michael@0: TOperator comparator = EOpNull; michael@0: int initial = 0; michael@0: int limit = 0; michael@0: int increment = 0; michael@0: michael@0: // Parse index name and intial value michael@0: if (node->getInit()) michael@0: { michael@0: TIntermAggregate *init = node->getInit()->getAsAggregate(); michael@0: michael@0: if (init) michael@0: { michael@0: TIntermSequence &sequence = init->getSequence(); michael@0: TIntermTyped *variable = sequence[0]->getAsTyped(); michael@0: michael@0: if (variable && variable->getQualifier() == EvqTemporary) michael@0: { michael@0: TIntermBinary *assign = variable->getAsBinaryNode(); michael@0: michael@0: if (assign->getOp() == EOpInitialize) michael@0: { michael@0: TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); michael@0: TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); michael@0: michael@0: if (symbol && constant) michael@0: { michael@0: if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1) michael@0: { michael@0: index = symbol; michael@0: initial = constant->getIConst(0); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Parse comparator and limit value michael@0: if (index != NULL && node->getCondition()) michael@0: { michael@0: TIntermBinary *test = node->getCondition()->getAsBinaryNode(); michael@0: michael@0: if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId()) michael@0: { michael@0: TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); michael@0: michael@0: if (constant) michael@0: { michael@0: if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1) michael@0: { michael@0: comparator = test->getOp(); michael@0: limit = constant->getIConst(0); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Parse increment michael@0: if (index != NULL && comparator != EOpNull && node->getExpression()) michael@0: { michael@0: TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); michael@0: TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); michael@0: michael@0: if (binaryTerminal) michael@0: { michael@0: TOperator op = binaryTerminal->getOp(); michael@0: TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); michael@0: michael@0: if (constant) michael@0: { michael@0: if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1) michael@0: { michael@0: int value = constant->getIConst(0); michael@0: michael@0: switch (op) michael@0: { michael@0: case EOpAddAssign: increment = value; break; michael@0: case EOpSubAssign: increment = -value; break; michael@0: default: UNIMPLEMENTED(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else if (unaryTerminal) michael@0: { michael@0: TOperator op = unaryTerminal->getOp(); michael@0: michael@0: switch (op) michael@0: { michael@0: case EOpPostIncrement: increment = 1; break; michael@0: case EOpPostDecrement: increment = -1; break; michael@0: case EOpPreIncrement: increment = 1; break; michael@0: case EOpPreDecrement: increment = -1; break; michael@0: default: UNIMPLEMENTED(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (index != NULL && comparator != EOpNull && increment != 0) michael@0: { michael@0: if (comparator == EOpLessThanEqual) michael@0: { michael@0: comparator = EOpLessThan; michael@0: limit += 1; michael@0: } michael@0: michael@0: if (comparator == EOpLessThan) michael@0: { michael@0: int iterations = (limit - initial) / increment; michael@0: michael@0: if (iterations <= MAX_LOOP_ITERATIONS) michael@0: { michael@0: return false; // Not an excessive loop michael@0: } michael@0: michael@0: TIntermSymbol *restoreIndex = mExcessiveLoopIndex; michael@0: mExcessiveLoopIndex = index; michael@0: michael@0: out << "{int "; michael@0: index->traverse(this); michael@0: out << ";\n" michael@0: "bool Break"; michael@0: index->traverse(this); michael@0: out << " = false;\n"; michael@0: michael@0: bool firstLoopFragment = true; michael@0: michael@0: while (iterations > 0) michael@0: { michael@0: int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); michael@0: michael@0: if (!firstLoopFragment) michael@0: { michael@0: out << "if(!Break"; michael@0: index->traverse(this); michael@0: out << ") {\n"; michael@0: } michael@0: michael@0: if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment michael@0: { michael@0: mExcessiveLoopIndex = NULL; // Stops setting the Break flag michael@0: } michael@0: michael@0: // for(int index = initial; index < clampedLimit; index += increment) michael@0: michael@0: out << "for("; michael@0: index->traverse(this); michael@0: out << " = "; michael@0: out << initial; michael@0: michael@0: out << "; "; michael@0: index->traverse(this); michael@0: out << " < "; michael@0: out << clampedLimit; michael@0: michael@0: out << "; "; michael@0: index->traverse(this); michael@0: out << " += "; michael@0: out << increment; michael@0: out << ")\n"; michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << "{\n"; michael@0: michael@0: if (node->getBody()) michael@0: { michael@0: node->getBody()->traverse(this); michael@0: } michael@0: michael@0: outputLineDirective(node->getLine().first_line); michael@0: out << ";}\n"; michael@0: michael@0: if (!firstLoopFragment) michael@0: { michael@0: out << "}\n"; michael@0: } michael@0: michael@0: firstLoopFragment = false; michael@0: michael@0: initial += MAX_LOOP_ITERATIONS * increment; michael@0: iterations -= MAX_LOOP_ITERATIONS; michael@0: } michael@0: michael@0: out << "}"; michael@0: michael@0: mExcessiveLoopIndex = restoreIndex; michael@0: michael@0: return true; michael@0: } michael@0: else UNIMPLEMENTED(); michael@0: } michael@0: michael@0: return false; // Not handled as an excessive loop michael@0: } michael@0: michael@0: void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: if (visit == PreVisit) michael@0: { michael@0: out << preString; michael@0: } michael@0: else if (visit == InVisit) michael@0: { michael@0: out << inString; michael@0: } michael@0: else if (visit == PostVisit) michael@0: { michael@0: out << postString; michael@0: } michael@0: } michael@0: michael@0: void OutputHLSL::outputLineDirective(int line) michael@0: { michael@0: if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0)) michael@0: { michael@0: mBody << "\n"; michael@0: mBody << "#line " << line; michael@0: michael@0: if (mContext.sourcePath) michael@0: { michael@0: mBody << " \"" << mContext.sourcePath << "\""; michael@0: } michael@0: michael@0: mBody << "\n"; michael@0: } michael@0: } michael@0: michael@0: TString OutputHLSL::argumentString(const TIntermSymbol *symbol) michael@0: { michael@0: TQualifier qualifier = symbol->getQualifier(); michael@0: const TType &type = symbol->getType(); michael@0: TString name = symbol->getSymbol(); michael@0: michael@0: if (name.empty()) // HLSL demands named arguments, also for prototypes michael@0: { michael@0: name = "x" + str(mUniqueIndex++); michael@0: } michael@0: else michael@0: { michael@0: name = decorate(name); michael@0: } michael@0: michael@0: if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) michael@0: { michael@0: return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " + michael@0: qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type); michael@0: } michael@0: michael@0: return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type); michael@0: } michael@0: michael@0: TString OutputHLSL::qualifierString(TQualifier qualifier) michael@0: { michael@0: switch(qualifier) michael@0: { michael@0: case EvqIn: return "in"; michael@0: case EvqOut: return "out"; michael@0: case EvqInOut: return "inout"; michael@0: case EvqConstReadOnly: return "const"; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return ""; michael@0: } michael@0: michael@0: TString OutputHLSL::typeString(const TType &type) michael@0: { michael@0: if (type.getBasicType() == EbtStruct) michael@0: { michael@0: const TString& typeName = type.getStruct()->name(); michael@0: if (typeName != "") michael@0: { michael@0: return structLookup(typeName); michael@0: } michael@0: else // Nameless structure, define in place michael@0: { michael@0: const TFieldList &fields = type.getStruct()->fields(); michael@0: michael@0: TString string = "struct\n" michael@0: "{\n"; michael@0: michael@0: for (unsigned int i = 0; i < fields.size(); i++) michael@0: { michael@0: const TField *field = fields[i]; michael@0: michael@0: string += " " + typeString(*field->type()) + " " + decorate(field->name()) + arrayString(*field->type()) + ";\n"; michael@0: } michael@0: michael@0: string += "} "; michael@0: michael@0: return string; michael@0: } michael@0: } michael@0: else if (type.isMatrix()) michael@0: { michael@0: switch (type.getNominalSize()) michael@0: { michael@0: case 2: return "float2x2"; michael@0: case 3: return "float3x3"; michael@0: case 4: return "float4x4"; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: switch (type.getBasicType()) michael@0: { michael@0: case EbtFloat: michael@0: switch (type.getNominalSize()) michael@0: { michael@0: case 1: return "float"; michael@0: case 2: return "float2"; michael@0: case 3: return "float3"; michael@0: case 4: return "float4"; michael@0: } michael@0: case EbtInt: michael@0: switch (type.getNominalSize()) michael@0: { michael@0: case 1: return "int"; michael@0: case 2: return "int2"; michael@0: case 3: return "int3"; michael@0: case 4: return "int4"; michael@0: } michael@0: case EbtBool: michael@0: switch (type.getNominalSize()) michael@0: { michael@0: case 1: return "bool"; michael@0: case 2: return "bool2"; michael@0: case 3: return "bool3"; michael@0: case 4: return "bool4"; michael@0: } michael@0: case EbtVoid: michael@0: return "void"; michael@0: case EbtSampler2D: michael@0: return "sampler2D"; michael@0: case EbtSamplerCube: michael@0: return "samplerCUBE"; michael@0: case EbtSamplerExternalOES: michael@0: return "sampler2D"; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: UNREACHABLE(); michael@0: return ""; michael@0: } michael@0: michael@0: TString OutputHLSL::textureString(const TType &type) michael@0: { michael@0: switch (type.getBasicType()) michael@0: { michael@0: case EbtSampler2D: michael@0: return "Texture2D"; michael@0: case EbtSamplerCube: michael@0: return "TextureCube"; michael@0: case EbtSamplerExternalOES: michael@0: return "Texture2D"; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: UNREACHABLE(); michael@0: return ""; michael@0: } michael@0: michael@0: TString OutputHLSL::arrayString(const TType &type) michael@0: { michael@0: if (!type.isArray()) michael@0: { michael@0: return ""; michael@0: } michael@0: michael@0: return "[" + str(type.getArraySize()) + "]"; michael@0: } michael@0: michael@0: TString OutputHLSL::initializer(const TType &type) michael@0: { michael@0: TString string; michael@0: michael@0: size_t size = type.getObjectSize(); michael@0: for (size_t component = 0; component < size; component++) michael@0: { michael@0: string += "0"; michael@0: michael@0: if (component + 1 < size) michael@0: { michael@0: string += ", "; michael@0: } michael@0: } michael@0: michael@0: return "{" + string + "}"; michael@0: } michael@0: michael@0: void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters) michael@0: { michael@0: if (name == "") michael@0: { michael@0: return; // Nameless structures don't have constructors michael@0: } michael@0: michael@0: if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end()) michael@0: { michael@0: return; // Already added michael@0: } michael@0: michael@0: TType ctorType = type; michael@0: ctorType.clearArrayness(); michael@0: ctorType.setPrecision(EbpHigh); michael@0: ctorType.setQualifier(EvqTemporary); michael@0: michael@0: TString ctorName = type.getStruct() ? decorate(name) : name; michael@0: michael@0: typedef std::vector ParameterArray; michael@0: ParameterArray ctorParameters; michael@0: michael@0: if (type.getStruct()) michael@0: { michael@0: mStructNames.insert(decorate(name)); michael@0: michael@0: TString structure; michael@0: structure += "struct " + decorate(name) + "\n" michael@0: "{\n"; michael@0: michael@0: const TFieldList &fields = type.getStruct()->fields(); michael@0: michael@0: for (unsigned int i = 0; i < fields.size(); i++) michael@0: { michael@0: const TField *field = fields[i]; michael@0: michael@0: structure += " " + typeString(*field->type()) + " " + decorateField(field->name(), type) + arrayString(*field->type()) + ";\n"; michael@0: } michael@0: michael@0: structure += "};\n"; michael@0: michael@0: if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end()) michael@0: { michael@0: mStructDeclarations.push_back(structure); michael@0: } michael@0: michael@0: for (unsigned int i = 0; i < fields.size(); i++) michael@0: { michael@0: ctorParameters.push_back(*fields[i]->type()); michael@0: } michael@0: } michael@0: else if (parameters) michael@0: { michael@0: for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++) michael@0: { michael@0: ctorParameters.push_back((*parameter)->getAsTyped()->getType()); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: TString constructor; michael@0: michael@0: if (ctorType.getStruct()) michael@0: { michael@0: constructor += ctorName + " " + ctorName + "_ctor("; michael@0: } michael@0: else // Built-in type michael@0: { michael@0: constructor += typeString(ctorType) + " " + ctorName + "("; michael@0: } michael@0: michael@0: for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++) michael@0: { michael@0: const TType &type = ctorParameters[parameter]; michael@0: michael@0: constructor += typeString(type) + " x" + str(parameter) + arrayString(type); michael@0: michael@0: if (parameter < ctorParameters.size() - 1) michael@0: { michael@0: constructor += ", "; michael@0: } michael@0: } michael@0: michael@0: constructor += ")\n" michael@0: "{\n"; michael@0: michael@0: if (ctorType.getStruct()) michael@0: { michael@0: constructor += " " + ctorName + " structure = {"; michael@0: } michael@0: else michael@0: { michael@0: constructor += " return " + typeString(ctorType) + "("; michael@0: } michael@0: michael@0: if (ctorType.isMatrix() && ctorParameters.size() == 1) michael@0: { michael@0: int dim = ctorType.getNominalSize(); michael@0: const TType ¶meter = ctorParameters[0]; michael@0: michael@0: if (parameter.isScalar()) michael@0: { michael@0: for (int row = 0; row < dim; row++) michael@0: { michael@0: for (int col = 0; col < dim; col++) michael@0: { michael@0: constructor += TString((row == col) ? "x0" : "0.0"); michael@0: michael@0: if (row < dim - 1 || col < dim - 1) michael@0: { michael@0: constructor += ", "; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else if (parameter.isMatrix()) michael@0: { michael@0: for (int row = 0; row < dim; row++) michael@0: { michael@0: for (int col = 0; col < dim; col++) michael@0: { michael@0: if (row < parameter.getNominalSize() && col < parameter.getNominalSize()) michael@0: { michael@0: constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]"; michael@0: } michael@0: else michael@0: { michael@0: constructor += TString((row == col) ? "1.0" : "0.0"); michael@0: } michael@0: michael@0: if (row < dim - 1 || col < dim - 1) michael@0: { michael@0: constructor += ", "; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else michael@0: { michael@0: size_t remainingComponents = ctorType.getObjectSize(); michael@0: size_t parameterIndex = 0; michael@0: michael@0: while (remainingComponents > 0) michael@0: { michael@0: const TType ¶meter = ctorParameters[parameterIndex]; michael@0: const size_t parameterSize = parameter.getObjectSize(); michael@0: bool moreParameters = parameterIndex + 1 < ctorParameters.size(); michael@0: michael@0: constructor += "x" + str(parameterIndex); michael@0: michael@0: if (parameter.isScalar()) michael@0: { michael@0: ASSERT(parameterSize <= remainingComponents); michael@0: remainingComponents -= parameterSize; michael@0: } michael@0: else if (parameter.isVector()) michael@0: { michael@0: if (remainingComponents == parameterSize || moreParameters) michael@0: { michael@0: ASSERT(parameterSize <= remainingComponents); michael@0: remainingComponents -= parameterSize; michael@0: } michael@0: else if (remainingComponents < static_cast(parameter.getNominalSize())) michael@0: { michael@0: switch (remainingComponents) michael@0: { michael@0: case 1: constructor += ".x"; break; michael@0: case 2: constructor += ".xy"; break; michael@0: case 3: constructor += ".xyz"; break; michael@0: case 4: constructor += ".xyzw"; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: remainingComponents = 0; michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else if (parameter.isMatrix() || parameter.getStruct()) michael@0: { michael@0: ASSERT(remainingComponents == parameterSize || moreParameters); michael@0: ASSERT(parameterSize <= remainingComponents); michael@0: michael@0: remainingComponents -= parameterSize; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: if (moreParameters) michael@0: { michael@0: parameterIndex++; michael@0: } michael@0: michael@0: if (remainingComponents) michael@0: { michael@0: constructor += ", "; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (ctorType.getStruct()) michael@0: { michael@0: constructor += "};\n" michael@0: " return structure;\n" michael@0: "}\n"; michael@0: } michael@0: else michael@0: { michael@0: constructor += ");\n" michael@0: "}\n"; michael@0: } michael@0: michael@0: mConstructors.insert(constructor); michael@0: } michael@0: michael@0: const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion) michael@0: { michael@0: TInfoSinkBase &out = mBody; michael@0: michael@0: if (type.getBasicType() == EbtStruct) michael@0: { michael@0: out << structLookup(type.getStruct()->name()) + "_ctor("; michael@0: michael@0: const TFieldList &fields = type.getStruct()->fields(); michael@0: michael@0: for (size_t i = 0; i < fields.size(); i++) michael@0: { michael@0: const TType *fieldType = fields[i]->type(); michael@0: michael@0: constUnion = writeConstantUnion(*fieldType, constUnion); michael@0: michael@0: if (i != fields.size() - 1) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: michael@0: out << ")"; michael@0: } michael@0: else michael@0: { michael@0: size_t size = type.getObjectSize(); michael@0: bool writeType = size > 1; michael@0: michael@0: if (writeType) michael@0: { michael@0: out << typeString(type) << "("; michael@0: } michael@0: michael@0: for (size_t i = 0; i < size; i++, constUnion++) michael@0: { michael@0: switch (constUnion->getType()) michael@0: { michael@0: case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break; michael@0: case EbtInt: out << constUnion->getIConst(); break; michael@0: case EbtBool: out << constUnion->getBConst(); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: if (i != size - 1) michael@0: { michael@0: out << ", "; michael@0: } michael@0: } michael@0: michael@0: if (writeType) michael@0: { michael@0: out << ")"; michael@0: } michael@0: } michael@0: michael@0: return constUnion; michael@0: } michael@0: michael@0: TString OutputHLSL::scopeString(unsigned int depthLimit) michael@0: { michael@0: TString string; michael@0: michael@0: for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++) michael@0: { michael@0: string += "_" + str(i); michael@0: } michael@0: michael@0: return string; michael@0: } michael@0: michael@0: TString OutputHLSL::scopedStruct(const TString &typeName) michael@0: { michael@0: if (typeName == "") michael@0: { michael@0: return typeName; michael@0: } michael@0: michael@0: return typeName + scopeString(mScopeDepth); michael@0: } michael@0: michael@0: TString OutputHLSL::structLookup(const TString &typeName) michael@0: { michael@0: for (int depth = mScopeDepth; depth >= 0; depth--) michael@0: { michael@0: TString scopedName = decorate(typeName + scopeString(depth)); michael@0: michael@0: for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++) michael@0: { michael@0: if (*structName == scopedName) michael@0: { michael@0: return scopedName; michael@0: } michael@0: } michael@0: } michael@0: michael@0: UNREACHABLE(); // Should have found a matching constructor michael@0: michael@0: return typeName; michael@0: } michael@0: michael@0: TString OutputHLSL::decorate(const TString &string) michael@0: { michael@0: if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0) michael@0: { michael@0: return "_" + string; michael@0: } michael@0: michael@0: return string; michael@0: } michael@0: michael@0: TString OutputHLSL::decorateUniform(const TString &string, const TType &type) michael@0: { michael@0: if (type.getBasicType() == EbtSamplerExternalOES) michael@0: { michael@0: return "ex_" + string; michael@0: } michael@0: michael@0: return decorate(string); michael@0: } michael@0: michael@0: TString OutputHLSL::decorateField(const TString &string, const TType &structure) michael@0: { michael@0: if (structure.getStruct()->name().compare(0, 3, "gl_") != 0) michael@0: { michael@0: return decorate(string); michael@0: } michael@0: michael@0: return string; michael@0: } michael@0: michael@0: TString OutputHLSL::registerString(TIntermSymbol *operand) michael@0: { michael@0: ASSERT(operand->getQualifier() == EvqUniform); michael@0: michael@0: if (IsSampler(operand->getBasicType())) michael@0: { michael@0: return "s" + str(samplerRegister(operand)); michael@0: } michael@0: michael@0: return "c" + str(uniformRegister(operand)); michael@0: } michael@0: michael@0: int OutputHLSL::samplerRegister(TIntermSymbol *sampler) michael@0: { michael@0: const TType &type = sampler->getType(); michael@0: ASSERT(IsSampler(type.getBasicType())); michael@0: michael@0: int index = mSamplerRegister; michael@0: mSamplerRegister += sampler->totalRegisterCount(); michael@0: michael@0: declareUniform(type, sampler->getSymbol(), index); michael@0: michael@0: return index; michael@0: } michael@0: michael@0: int OutputHLSL::uniformRegister(TIntermSymbol *uniform) michael@0: { michael@0: const TType &type = uniform->getType(); michael@0: ASSERT(!IsSampler(type.getBasicType())); michael@0: michael@0: int index = mUniformRegister; michael@0: mUniformRegister += uniform->totalRegisterCount(); michael@0: michael@0: declareUniform(type, uniform->getSymbol(), index); michael@0: michael@0: return index; michael@0: } michael@0: michael@0: void OutputHLSL::declareUniform(const TType &type, const TString &name, int index) michael@0: { michael@0: TStructure *structure = type.getStruct(); michael@0: michael@0: if (!structure) michael@0: { michael@0: mActiveUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), index)); michael@0: } michael@0: else michael@0: { michael@0: const TFieldList &fields = structure->fields(); michael@0: michael@0: if (type.isArray()) michael@0: { michael@0: int elementIndex = index; michael@0: michael@0: for (int i = 0; i < type.getArraySize(); i++) michael@0: { michael@0: for (size_t j = 0; j < fields.size(); j++) michael@0: { michael@0: const TType &fieldType = *fields[j]->type(); michael@0: const TString uniformName = name + "[" + str(i) + "]." + fields[j]->name(); michael@0: declareUniform(fieldType, uniformName, elementIndex); michael@0: elementIndex += fieldType.totalRegisterCount(); michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: int fieldIndex = index; michael@0: michael@0: for (size_t i = 0; i < fields.size(); i++) michael@0: { michael@0: const TType &fieldType = *fields[i]->type(); michael@0: const TString uniformName = name + "." + fields[i]->name(); michael@0: declareUniform(fieldType, uniformName, fieldIndex); michael@0: fieldIndex += fieldType.totalRegisterCount(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: GLenum OutputHLSL::glVariableType(const TType &type) michael@0: { michael@0: if (type.getBasicType() == EbtFloat) michael@0: { michael@0: if (type.isScalar()) michael@0: { michael@0: return GL_FLOAT; michael@0: } michael@0: else if (type.isVector()) michael@0: { michael@0: switch(type.getNominalSize()) michael@0: { michael@0: case 2: return GL_FLOAT_VEC2; michael@0: case 3: return GL_FLOAT_VEC3; michael@0: case 4: return GL_FLOAT_VEC4; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else if (type.isMatrix()) michael@0: { michael@0: switch(type.getNominalSize()) michael@0: { michael@0: case 2: return GL_FLOAT_MAT2; michael@0: case 3: return GL_FLOAT_MAT3; michael@0: case 4: return GL_FLOAT_MAT4; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else if (type.getBasicType() == EbtInt) michael@0: { michael@0: if (type.isScalar()) michael@0: { michael@0: return GL_INT; michael@0: } michael@0: else if (type.isVector()) michael@0: { michael@0: switch(type.getNominalSize()) michael@0: { michael@0: case 2: return GL_INT_VEC2; michael@0: case 3: return GL_INT_VEC3; michael@0: case 4: return GL_INT_VEC4; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else if (type.getBasicType() == EbtBool) michael@0: { michael@0: if (type.isScalar()) michael@0: { michael@0: return GL_BOOL; michael@0: } michael@0: else if (type.isVector()) michael@0: { michael@0: switch(type.getNominalSize()) michael@0: { michael@0: case 2: return GL_BOOL_VEC2; michael@0: case 3: return GL_BOOL_VEC3; michael@0: case 4: return GL_BOOL_VEC4; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: else if (type.getBasicType() == EbtSampler2D) michael@0: { michael@0: return GL_SAMPLER_2D; michael@0: } michael@0: else if (type.getBasicType() == EbtSamplerCube) michael@0: { michael@0: return GL_SAMPLER_CUBE; michael@0: } michael@0: else UNREACHABLE(); michael@0: michael@0: return GL_NONE; michael@0: } michael@0: michael@0: GLenum OutputHLSL::glVariablePrecision(const TType &type) michael@0: { michael@0: if (type.getBasicType() == EbtFloat) michael@0: { michael@0: switch (type.getPrecision()) michael@0: { michael@0: case EbpHigh: return GL_HIGH_FLOAT; michael@0: case EbpMedium: return GL_MEDIUM_FLOAT; michael@0: case EbpLow: return GL_LOW_FLOAT; michael@0: case EbpUndefined: michael@0: // Should be defined as the default precision by the parser michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: else if (type.getBasicType() == EbtInt) michael@0: { michael@0: switch (type.getPrecision()) michael@0: { michael@0: case EbpHigh: return GL_HIGH_INT; michael@0: case EbpMedium: return GL_MEDIUM_INT; michael@0: case EbpLow: return GL_LOW_INT; michael@0: case EbpUndefined: michael@0: // Should be defined as the default precision by the parser michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: michael@0: // Other types (boolean, sampler) don't have a precision michael@0: return GL_NONE; michael@0: } michael@0: michael@0: }