daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/OutputHLSL.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 9 | #include <algorithm> |
shannon.woods@transgaming.com | fff89b3 | 2013-02-28 23:20:15 +0000 | [diff] [blame] | 10 | #include <cfloat> |
| 11 | #include <stdio.h> |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 12 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 13 | #include "common/angleutils.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 14 | #include "common/debug.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 15 | #include "common/utilities.h" |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 16 | #include "compiler/translator/BuiltInFunctionEmulator.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 17 | #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 18 | #include "compiler/translator/FlagStd140Structs.h" |
| 19 | #include "compiler/translator/InfoSink.h" |
| 20 | #include "compiler/translator/NodeSearch.h" |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 21 | #include "compiler/translator/RemoveSwitchFallThrough.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 22 | #include "compiler/translator/RewriteElseBlocks.h" |
| 23 | #include "compiler/translator/SearchSymbol.h" |
| 24 | #include "compiler/translator/StructureHLSL.h" |
| 25 | #include "compiler/translator/TranslatorHLSL.h" |
| 26 | #include "compiler/translator/UnfoldShortCircuit.h" |
| 27 | #include "compiler/translator/UniformHLSL.h" |
| 28 | #include "compiler/translator/UtilsHLSL.h" |
| 29 | #include "compiler/translator/blocklayout.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 30 | #include "compiler/translator/util.h" |
| 31 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | namespace sh |
| 33 | { |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 34 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 35 | TString OutputHLSL::TextureFunction::name() const |
| 36 | { |
| 37 | TString name = "gl_texture"; |
| 38 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 39 | if (IsSampler2D(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 40 | { |
| 41 | name += "2D"; |
| 42 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 43 | else if (IsSampler3D(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 44 | { |
| 45 | name += "3D"; |
| 46 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 47 | else if (IsSamplerCube(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 48 | { |
| 49 | name += "Cube"; |
| 50 | } |
| 51 | else UNREACHABLE(); |
| 52 | |
| 53 | if (proj) |
| 54 | { |
| 55 | name += "Proj"; |
| 56 | } |
| 57 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 58 | if (offset) |
| 59 | { |
| 60 | name += "Offset"; |
| 61 | } |
| 62 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 63 | switch(method) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 64 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 65 | case IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 66 | case BIAS: break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 67 | case LOD: name += "Lod"; break; |
| 68 | case LOD0: name += "Lod0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 69 | case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 70 | case SIZE: name += "Size"; break; |
| 71 | case FETCH: name += "Fetch"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 72 | case GRAD: name += "Grad"; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 73 | default: UNREACHABLE(); |
| 74 | } |
| 75 | |
| 76 | return name + "("; |
| 77 | } |
| 78 | |
| 79 | bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const |
| 80 | { |
| 81 | if (sampler < rhs.sampler) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 82 | if (sampler > rhs.sampler) return false; |
| 83 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 84 | if (coords < rhs.coords) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 85 | if (coords > rhs.coords) return false; |
| 86 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 87 | if (!proj && rhs.proj) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 88 | if (proj && !rhs.proj) return false; |
| 89 | |
| 90 | if (!offset && rhs.offset) return true; |
| 91 | if (offset && !rhs.offset) return false; |
| 92 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 93 | if (method < rhs.method) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 94 | if (method > rhs.method) return false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 99 | OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion, |
| 100 | const TExtensionBehavior &extensionBehavior, |
| 101 | const char *sourcePath, ShShaderOutput outputType, |
| 102 | int numRenderTargets, const std::vector<Uniform> &uniforms, |
| 103 | int compileOptions) |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 104 | : TIntermTraverser(true, true, true), |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 105 | mShaderType(shaderType), |
| 106 | mShaderVersion(shaderVersion), |
| 107 | mExtensionBehavior(extensionBehavior), |
| 108 | mSourcePath(sourcePath), |
| 109 | mOutputType(outputType), |
| 110 | mNumRenderTargets(numRenderTargets), |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 111 | mCompileOptions(compileOptions), |
| 112 | mCurrentFunctionMetadata(nullptr) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 114 | mUnfoldShortCircuit = new UnfoldShortCircuit(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 115 | mInsideFunction = false; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 116 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 117 | mUsesFragColor = false; |
| 118 | mUsesFragData = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 119 | mUsesDepthRange = false; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 120 | mUsesFragCoord = false; |
| 121 | mUsesPointCoord = false; |
| 122 | mUsesFrontFacing = false; |
| 123 | mUsesPointSize = false; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 124 | mUsesInstanceID = false; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 125 | mUsesFragDepth = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 126 | mUsesXor = false; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 127 | mUsesDiscardRewriting = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 128 | mUsesNestedBreak = false; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 129 | mRequiresIEEEStrictCompiling = false; |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 130 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 131 | mUniqueIndex = 0; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 132 | |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 133 | mOutputLod0Function = false; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 134 | mInsideDiscontinuousLoop = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 135 | mNestedLoopDepth = 0; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 136 | |
| 137 | mExcessiveLoopIndex = NULL; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 138 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 139 | mStructureHLSL = new StructureHLSL; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 140 | mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms); |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 141 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 142 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 143 | { |
Arun Patole | 6341939 | 2015-03-13 11:51:07 +0530 | [diff] [blame] | 144 | // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront. |
| 145 | // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust. |
| 146 | // In both cases total 3 uniform registers need to be reserved. |
| 147 | mUniformHLSL->reserveUniformRegisters(3); |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 148 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 149 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 150 | // Reserve registers for the default uniform block and driver constants |
| 151 | mUniformHLSL->reserveInterfaceBlockRegisters(2); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 154 | OutputHLSL::~OutputHLSL() |
| 155 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 156 | SafeDelete(mUnfoldShortCircuit); |
| 157 | SafeDelete(mStructureHLSL); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 158 | SafeDelete(mUniformHLSL); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 159 | for (auto &eqFunction : mStructEqualityFunctions) |
| 160 | { |
| 161 | SafeDelete(eqFunction); |
| 162 | } |
| 163 | for (auto &eqFunction : mArrayEqualityFunctions) |
| 164 | { |
| 165 | SafeDelete(eqFunction); |
| 166 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 169 | void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink) |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 170 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 171 | const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 172 | makeFlaggedStructMaps(flaggedStructs); |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 173 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 174 | // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which |
| 175 | // use a vertex attribute as a condition, and some related computation in the else block. |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 176 | if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 177 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 178 | RewriteElseBlocks(treeRoot); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 181 | BuiltInFunctionEmulator builtInFunctionEmulator; |
| 182 | InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 183 | builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 184 | |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 185 | // Now that we are done changing the AST, do the analyses need for HLSL generation |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 186 | CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink); |
| 187 | ASSERT(success == CallDAG::INITDAG_SUCCESS); |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 188 | UNUSED_ASSERTION_VARIABLE(success); |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 189 | mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 190 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 191 | // Output the body and footer first to determine what has to go in the header |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 192 | mInfoSinkStack.push(&mBody); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 193 | treeRoot->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 194 | mInfoSinkStack.pop(); |
| 195 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 196 | mInfoSinkStack.push(&mFooter); |
| 197 | if (!mDeferredGlobalInitializers.empty()) |
| 198 | { |
| 199 | writeDeferredGlobalInitializers(mFooter); |
| 200 | } |
| 201 | mInfoSinkStack.pop(); |
| 202 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 203 | mInfoSinkStack.push(&mHeader); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 204 | header(&builtInFunctionEmulator); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 205 | mInfoSinkStack.pop(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 206 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 207 | objSink << mHeader.c_str(); |
| 208 | objSink << mBody.c_str(); |
| 209 | objSink << mFooter.c_str(); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 210 | |
| 211 | builtInFunctionEmulator.Cleanup(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 214 | void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs) |
| 215 | { |
| 216 | for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++) |
| 217 | { |
| 218 | TIntermTyped *flaggedNode = flaggedStructs[structIndex]; |
| 219 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 220 | TInfoSinkBase structInfoSink; |
| 221 | mInfoSinkStack.push(&structInfoSink); |
| 222 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 223 | // This will mark the necessary block elements as referenced |
| 224 | flaggedNode->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 225 | |
| 226 | TString structName(structInfoSink.c_str()); |
| 227 | mInfoSinkStack.pop(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 228 | |
| 229 | mFlaggedStructOriginalNames[flaggedNode] = structName; |
| 230 | |
| 231 | for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.')) |
| 232 | { |
| 233 | structName.erase(pos, 1); |
| 234 | } |
| 235 | |
| 236 | mFlaggedStructMappedNames[flaggedNode] = "map" + structName; |
| 237 | } |
| 238 | } |
| 239 | |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 240 | const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const |
| 241 | { |
| 242 | return mUniformHLSL->getInterfaceBlockRegisterMap(); |
| 243 | } |
| 244 | |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 245 | const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const |
| 246 | { |
| 247 | return mUniformHLSL->getUniformRegisterMap(); |
| 248 | } |
| 249 | |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 250 | int OutputHLSL::vectorSize(const TType &type) const |
| 251 | { |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 252 | int elementSize = type.isMatrix() ? type.getCols() : 1; |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 253 | int arraySize = type.isArray() ? type.getArraySize() : 1; |
| 254 | |
| 255 | return elementSize * arraySize; |
| 256 | } |
| 257 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 258 | TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 259 | { |
| 260 | TString init; |
| 261 | |
| 262 | TString preIndentString; |
| 263 | TString fullIndentString; |
| 264 | |
| 265 | for (int spaces = 0; spaces < (indent * 4); spaces++) |
| 266 | { |
| 267 | preIndentString += ' '; |
| 268 | } |
| 269 | |
| 270 | for (int spaces = 0; spaces < ((indent+1) * 4); spaces++) |
| 271 | { |
| 272 | fullIndentString += ' '; |
| 273 | } |
| 274 | |
| 275 | init += preIndentString + "{\n"; |
| 276 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 277 | const TFieldList &fields = structure.fields(); |
| 278 | for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 279 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 280 | const TField &field = *fields[fieldIndex]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 281 | const TString &fieldName = rhsStructName + "." + Decorate(field.name()); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 282 | const TType &fieldType = *field.type(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 283 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 284 | if (fieldType.getStruct()) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 285 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 286 | init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 290 | init += fullIndentString + fieldName + ",\n"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n"; |
| 295 | |
| 296 | return init; |
| 297 | } |
| 298 | |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 299 | void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 300 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 301 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 302 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 303 | TString varyings; |
| 304 | TString attributes; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 305 | TString flaggedStructs; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 306 | |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 307 | for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 308 | { |
| 309 | TIntermTyped *structNode = flaggedStructIt->first; |
| 310 | const TString &mappedName = flaggedStructIt->second; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 311 | const TStructure &structure = *structNode->getType().getStruct(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 312 | const TString &originalName = mFlaggedStructOriginalNames[structNode]; |
| 313 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 314 | flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n"; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 315 | flaggedStructs += structInitializerString(0, structure, originalName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 316 | flaggedStructs += "\n"; |
| 317 | } |
| 318 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 319 | for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++) |
| 320 | { |
| 321 | const TType &type = varying->second->getType(); |
| 322 | const TString &name = varying->second->getSymbol(); |
| 323 | |
| 324 | // Program linking depends on this exact format |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 325 | varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " + |
| 326 | Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++) |
| 330 | { |
| 331 | const TType &type = attribute->second->getType(); |
| 332 | const TString &name = attribute->second->getSymbol(); |
| 333 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 334 | attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 337 | out << mStructureHLSL->structsHeader(); |
Jamie Madill | 529077d | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 338 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 339 | out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms); |
| 340 | out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks); |
| 341 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 342 | if (!mEqualityFunctions.empty()) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 343 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 344 | out << "\n// Equality functions\n\n"; |
| 345 | for (const auto &eqFunction : mEqualityFunctions) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 346 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 347 | out << eqFunction->functionDefinition << "\n"; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 348 | } |
| 349 | } |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 350 | if (!mArrayAssignmentFunctions.empty()) |
| 351 | { |
| 352 | out << "\n// Assignment functions\n\n"; |
| 353 | for (const auto &assignmentFunction : mArrayAssignmentFunctions) |
| 354 | { |
| 355 | out << assignmentFunction.functionDefinition << "\n"; |
| 356 | } |
| 357 | } |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 358 | if (!mArrayConstructIntoFunctions.empty()) |
| 359 | { |
| 360 | out << "\n// Array constructor functions\n\n"; |
| 361 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 362 | { |
| 363 | out << constructIntoFunction.functionDefinition << "\n"; |
| 364 | } |
| 365 | } |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 366 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 367 | if (mUsesDiscardRewriting) |
| 368 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 369 | out << "#define ANGLE_USES_DISCARD_REWRITING\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 372 | if (mUsesNestedBreak) |
| 373 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 374 | out << "#define ANGLE_USES_NESTED_BREAK\n"; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 377 | if (mRequiresIEEEStrictCompiling) |
| 378 | { |
| 379 | out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n"; |
| 380 | } |
| 381 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 382 | out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n" |
| 383 | "#define LOOP [loop]\n" |
| 384 | "#define FLATTEN [flatten]\n" |
| 385 | "#else\n" |
| 386 | "#define LOOP\n" |
| 387 | "#define FLATTEN\n" |
| 388 | "#endif\n"; |
| 389 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 390 | if (mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 391 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 392 | TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers"); |
| 393 | const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire)); |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 394 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 395 | out << "// Varyings\n"; |
| 396 | out << varyings; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 397 | out << "\n"; |
| 398 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 399 | if (mShaderVersion >= 300) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 400 | { |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 401 | for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 402 | { |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 403 | const TString &variableName = outputVariableIt->first; |
| 404 | const TType &variableType = outputVariableIt->second->getType(); |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 405 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 406 | out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) + |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 407 | " = " + initializer(variableType) + ";\n"; |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 408 | } |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 409 | } |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 410 | else |
| 411 | { |
| 412 | const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1; |
| 413 | |
| 414 | out << "static float4 gl_Color[" << numColorValues << "] =\n" |
| 415 | "{\n"; |
| 416 | for (unsigned int i = 0; i < numColorValues; i++) |
| 417 | { |
| 418 | out << " float4(0, 0, 0, 0)"; |
| 419 | if (i + 1 != numColorValues) |
| 420 | { |
| 421 | out << ","; |
| 422 | } |
| 423 | out << "\n"; |
| 424 | } |
| 425 | |
| 426 | out << "};\n"; |
| 427 | } |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 428 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 429 | if (mUsesFragDepth) |
| 430 | { |
| 431 | out << "static float gl_Depth = 0.0;\n"; |
| 432 | } |
| 433 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 434 | if (mUsesFragCoord) |
| 435 | { |
| 436 | out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"; |
| 437 | } |
| 438 | |
| 439 | if (mUsesPointCoord) |
| 440 | { |
| 441 | out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n"; |
| 442 | } |
| 443 | |
| 444 | if (mUsesFrontFacing) |
| 445 | { |
| 446 | out << "static bool gl_FrontFacing = false;\n"; |
| 447 | } |
| 448 | |
| 449 | out << "\n"; |
| 450 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 451 | if (mUsesDepthRange) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 452 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 453 | out << "struct gl_DepthRangeParameters\n" |
| 454 | "{\n" |
| 455 | " float near;\n" |
| 456 | " float far;\n" |
| 457 | " float diff;\n" |
| 458 | "};\n" |
| 459 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 460 | } |
| 461 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 462 | if (mOutputType == SH_HLSL11_OUTPUT) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 463 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 464 | out << "cbuffer DriverConstants : register(b1)\n" |
| 465 | "{\n"; |
| 466 | |
| 467 | if (mUsesDepthRange) |
| 468 | { |
| 469 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
| 470 | } |
| 471 | |
| 472 | if (mUsesFragCoord) |
| 473 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 474 | out << " float4 dx_ViewCoords : packoffset(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if (mUsesFragCoord || mUsesFrontFacing) |
| 478 | { |
| 479 | out << " float3 dx_DepthFront : packoffset(c2);\n"; |
| 480 | } |
| 481 | |
| 482 | out << "};\n"; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | if (mUsesDepthRange) |
| 487 | { |
| 488 | out << "uniform float3 dx_DepthRange : register(c0);"; |
| 489 | } |
| 490 | |
| 491 | if (mUsesFragCoord) |
| 492 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 493 | out << "uniform float4 dx_ViewCoords : register(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (mUsesFragCoord || mUsesFrontFacing) |
| 497 | { |
| 498 | out << "uniform float3 dx_DepthFront : register(c2);\n"; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | out << "\n"; |
| 503 | |
| 504 | if (mUsesDepthRange) |
| 505 | { |
| 506 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 507 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 508 | } |
daniel@transgaming.com | 5024cc4 | 2010-04-20 18:52:04 +0000 | [diff] [blame] | 509 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 510 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 511 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 512 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 513 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 514 | out << flaggedStructs; |
| 515 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 516 | } |
| 517 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 518 | if (usingMRTExtension && mNumRenderTargets > 1) |
| 519 | { |
| 520 | out << "#define GL_USES_MRT\n"; |
| 521 | } |
| 522 | |
| 523 | if (mUsesFragColor) |
| 524 | { |
| 525 | out << "#define GL_USES_FRAG_COLOR\n"; |
| 526 | } |
| 527 | |
| 528 | if (mUsesFragData) |
| 529 | { |
| 530 | out << "#define GL_USES_FRAG_DATA\n"; |
| 531 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 532 | } |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 533 | else // Vertex shader |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 534 | { |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 535 | out << "// Attributes\n"; |
| 536 | out << attributes; |
| 537 | out << "\n" |
| 538 | "static float4 gl_Position = float4(0, 0, 0, 0);\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 539 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 540 | if (mUsesPointSize) |
| 541 | { |
| 542 | out << "static float gl_PointSize = float(1);\n"; |
| 543 | } |
| 544 | |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 545 | if (mUsesInstanceID) |
| 546 | { |
| 547 | out << "static int gl_InstanceID;"; |
| 548 | } |
| 549 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 550 | out << "\n" |
| 551 | "// Varyings\n"; |
| 552 | out << varyings; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 553 | out << "\n"; |
| 554 | |
| 555 | if (mUsesDepthRange) |
| 556 | { |
| 557 | out << "struct gl_DepthRangeParameters\n" |
| 558 | "{\n" |
| 559 | " float near;\n" |
| 560 | " float far;\n" |
| 561 | " float diff;\n" |
| 562 | "};\n" |
| 563 | "\n"; |
| 564 | } |
| 565 | |
| 566 | if (mOutputType == SH_HLSL11_OUTPUT) |
| 567 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 568 | out << "cbuffer DriverConstants : register(b1)\n" |
| 569 | "{\n"; |
| 570 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 571 | if (mUsesDepthRange) |
| 572 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 573 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 574 | } |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 575 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 576 | // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders. |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 577 | // However, we declare it for all shaders (including Feature Level 10+). |
| 578 | // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused. |
| 579 | out << " float4 dx_ViewAdjust : packoffset(c1);\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 580 | out << " float2 dx_ViewCoords : packoffset(c2);\n"; |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 581 | |
| 582 | out << "};\n" |
| 583 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 584 | } |
| 585 | else |
| 586 | { |
| 587 | if (mUsesDepthRange) |
| 588 | { |
| 589 | out << "uniform float3 dx_DepthRange : register(c0);\n"; |
| 590 | } |
| 591 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 592 | out << "uniform float4 dx_ViewAdjust : register(c1);\n"; |
| 593 | out << "uniform float2 dx_ViewCoords : register(c2);\n" |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 594 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 595 | } |
| 596 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 597 | if (mUsesDepthRange) |
| 598 | { |
| 599 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 600 | "\n"; |
| 601 | } |
| 602 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 603 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 604 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 605 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 606 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 607 | out << flaggedStructs; |
| 608 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 609 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 610 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 611 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 612 | for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++) |
| 613 | { |
| 614 | // Return type |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 615 | if (textureFunction->method == TextureFunction::SIZE) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 616 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 617 | switch(textureFunction->sampler) |
| 618 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 619 | case EbtSampler2D: out << "int2 "; break; |
| 620 | case EbtSampler3D: out << "int3 "; break; |
| 621 | case EbtSamplerCube: out << "int2 "; break; |
| 622 | case EbtSampler2DArray: out << "int3 "; break; |
| 623 | case EbtISampler2D: out << "int2 "; break; |
| 624 | case EbtISampler3D: out << "int3 "; break; |
| 625 | case EbtISamplerCube: out << "int2 "; break; |
| 626 | case EbtISampler2DArray: out << "int3 "; break; |
| 627 | case EbtUSampler2D: out << "int2 "; break; |
| 628 | case EbtUSampler3D: out << "int3 "; break; |
| 629 | case EbtUSamplerCube: out << "int2 "; break; |
| 630 | case EbtUSampler2DArray: out << "int3 "; break; |
| 631 | case EbtSampler2DShadow: out << "int2 "; break; |
| 632 | case EbtSamplerCubeShadow: out << "int2 "; break; |
| 633 | case EbtSampler2DArrayShadow: out << "int3 "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 634 | default: UNREACHABLE(); |
| 635 | } |
| 636 | } |
| 637 | else // Sampling function |
| 638 | { |
| 639 | switch(textureFunction->sampler) |
| 640 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 641 | case EbtSampler2D: out << "float4 "; break; |
| 642 | case EbtSampler3D: out << "float4 "; break; |
| 643 | case EbtSamplerCube: out << "float4 "; break; |
| 644 | case EbtSampler2DArray: out << "float4 "; break; |
| 645 | case EbtISampler2D: out << "int4 "; break; |
| 646 | case EbtISampler3D: out << "int4 "; break; |
| 647 | case EbtISamplerCube: out << "int4 "; break; |
| 648 | case EbtISampler2DArray: out << "int4 "; break; |
| 649 | case EbtUSampler2D: out << "uint4 "; break; |
| 650 | case EbtUSampler3D: out << "uint4 "; break; |
| 651 | case EbtUSamplerCube: out << "uint4 "; break; |
| 652 | case EbtUSampler2DArray: out << "uint4 "; break; |
| 653 | case EbtSampler2DShadow: out << "float "; break; |
| 654 | case EbtSamplerCubeShadow: out << "float "; break; |
| 655 | case EbtSampler2DArrayShadow: out << "float "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 656 | default: UNREACHABLE(); |
| 657 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 660 | // Function name |
| 661 | out << textureFunction->name(); |
| 662 | |
| 663 | // Argument list |
| 664 | int hlslCoords = 4; |
| 665 | |
| 666 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 667 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 668 | switch(textureFunction->sampler) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 669 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 670 | case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break; |
| 671 | case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break; |
| 672 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 673 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 674 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 675 | switch(textureFunction->method) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 676 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 677 | case TextureFunction::IMPLICIT: break; |
| 678 | case TextureFunction::BIAS: hlslCoords = 4; break; |
| 679 | case TextureFunction::LOD: hlslCoords = 4; break; |
| 680 | case TextureFunction::LOD0: hlslCoords = 4; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 681 | case TextureFunction::LOD0BIAS: hlslCoords = 4; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 682 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 683 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 684 | } |
| 685 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 686 | { |
| 687 | switch(textureFunction->sampler) |
| 688 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 689 | case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break; |
| 690 | case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break; |
| 691 | case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break; |
| 692 | case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break; |
| 693 | case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break; |
| 694 | case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 695 | case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 696 | case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break; |
| 697 | case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break; |
| 698 | case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 699 | case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 700 | case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break; |
| 701 | case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break; |
| 702 | case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break; |
| 703 | case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 704 | default: UNREACHABLE(); |
| 705 | } |
| 706 | } |
| 707 | else UNREACHABLE(); |
| 708 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 709 | if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 710 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 711 | switch(textureFunction->coords) |
| 712 | { |
| 713 | case 2: out << ", int2 t"; break; |
| 714 | case 3: out << ", int3 t"; break; |
| 715 | default: UNREACHABLE(); |
| 716 | } |
| 717 | } |
| 718 | else // Floating-point coordinates (except textureSize) |
| 719 | { |
| 720 | switch(textureFunction->coords) |
| 721 | { |
| 722 | case 1: out << ", int lod"; break; // textureSize() |
| 723 | case 2: out << ", float2 t"; break; |
| 724 | case 3: out << ", float3 t"; break; |
| 725 | case 4: out << ", float4 t"; break; |
| 726 | default: UNREACHABLE(); |
| 727 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 730 | if (textureFunction->method == TextureFunction::GRAD) |
| 731 | { |
| 732 | switch(textureFunction->sampler) |
| 733 | { |
| 734 | case EbtSampler2D: |
| 735 | case EbtISampler2D: |
| 736 | case EbtUSampler2D: |
| 737 | case EbtSampler2DArray: |
| 738 | case EbtISampler2DArray: |
| 739 | case EbtUSampler2DArray: |
| 740 | case EbtSampler2DShadow: |
| 741 | case EbtSampler2DArrayShadow: |
| 742 | out << ", float2 ddx, float2 ddy"; |
| 743 | break; |
| 744 | case EbtSampler3D: |
| 745 | case EbtISampler3D: |
| 746 | case EbtUSampler3D: |
| 747 | case EbtSamplerCube: |
| 748 | case EbtISamplerCube: |
| 749 | case EbtUSamplerCube: |
| 750 | case EbtSamplerCubeShadow: |
| 751 | out << ", float3 ddx, float3 ddy"; |
| 752 | break; |
| 753 | default: UNREACHABLE(); |
| 754 | } |
| 755 | } |
| 756 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 757 | switch(textureFunction->method) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 758 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 759 | case TextureFunction::IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 760 | case TextureFunction::BIAS: break; // Comes after the offset parameter |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 761 | case TextureFunction::LOD: out << ", float lod"; break; |
| 762 | case TextureFunction::LOD0: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 763 | case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 764 | case TextureFunction::SIZE: break; |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 765 | case TextureFunction::FETCH: out << ", int mip"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 766 | case TextureFunction::GRAD: break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 767 | default: UNREACHABLE(); |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 770 | if (textureFunction->offset) |
| 771 | { |
| 772 | switch(textureFunction->sampler) |
| 773 | { |
| 774 | case EbtSampler2D: out << ", int2 offset"; break; |
| 775 | case EbtSampler3D: out << ", int3 offset"; break; |
| 776 | case EbtSampler2DArray: out << ", int2 offset"; break; |
| 777 | case EbtISampler2D: out << ", int2 offset"; break; |
| 778 | case EbtISampler3D: out << ", int3 offset"; break; |
| 779 | case EbtISampler2DArray: out << ", int2 offset"; break; |
| 780 | case EbtUSampler2D: out << ", int2 offset"; break; |
| 781 | case EbtUSampler3D: out << ", int3 offset"; break; |
| 782 | case EbtUSampler2DArray: out << ", int2 offset"; break; |
| 783 | case EbtSampler2DShadow: out << ", int2 offset"; break; |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 784 | case EbtSampler2DArrayShadow: out << ", int2 offset"; break; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 785 | default: UNREACHABLE(); |
| 786 | } |
| 787 | } |
| 788 | |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 789 | if (textureFunction->method == TextureFunction::BIAS || |
| 790 | textureFunction->method == TextureFunction::LOD0BIAS) |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 791 | { |
| 792 | out << ", float bias"; |
| 793 | } |
| 794 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 795 | out << ")\n" |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 796 | "{\n"; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 797 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 798 | if (textureFunction->method == TextureFunction::SIZE) |
| 799 | { |
| 800 | if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler)) |
| 801 | { |
| 802 | if (IsSamplerArray(textureFunction->sampler)) |
| 803 | { |
| 804 | out << " uint width; uint height; uint layers; uint numberOfLevels;\n" |
| 805 | " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n"; |
| 806 | } |
| 807 | else |
| 808 | { |
| 809 | out << " uint width; uint height; uint numberOfLevels;\n" |
| 810 | " x.GetDimensions(lod, width, height, numberOfLevels);\n"; |
| 811 | } |
| 812 | } |
| 813 | else if (IsSampler3D(textureFunction->sampler)) |
| 814 | { |
| 815 | out << " uint width; uint height; uint depth; uint numberOfLevels;\n" |
| 816 | " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n"; |
| 817 | } |
| 818 | else UNREACHABLE(); |
| 819 | |
| 820 | switch(textureFunction->sampler) |
| 821 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 822 | case EbtSampler2D: out << " return int2(width, height);"; break; |
| 823 | case EbtSampler3D: out << " return int3(width, height, depth);"; break; |
| 824 | case EbtSamplerCube: out << " return int2(width, height);"; break; |
| 825 | case EbtSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 826 | case EbtISampler2D: out << " return int2(width, height);"; break; |
| 827 | case EbtISampler3D: out << " return int3(width, height, depth);"; break; |
| 828 | case EbtISamplerCube: out << " return int2(width, height);"; break; |
| 829 | case EbtISampler2DArray: out << " return int3(width, height, layers);"; break; |
| 830 | case EbtUSampler2D: out << " return int2(width, height);"; break; |
| 831 | case EbtUSampler3D: out << " return int3(width, height, depth);"; break; |
| 832 | case EbtUSamplerCube: out << " return int2(width, height);"; break; |
| 833 | case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 834 | case EbtSampler2DShadow: out << " return int2(width, height);"; break; |
| 835 | case EbtSamplerCubeShadow: out << " return int2(width, height);"; break; |
| 836 | case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 837 | default: UNREACHABLE(); |
| 838 | } |
| 839 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 840 | else |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 841 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 842 | if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler)) |
| 843 | { |
| 844 | out << " float width; float height; float layers; float levels;\n"; |
| 845 | |
| 846 | out << " uint mip = 0;\n"; |
| 847 | |
| 848 | out << " x.GetDimensions(mip, width, height, layers, levels);\n"; |
| 849 | |
| 850 | out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n"; |
| 851 | out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n"; |
| 852 | out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n"; |
| 853 | out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n"; |
| 854 | |
| 855 | // FACE_POSITIVE_X = 000b |
| 856 | // FACE_NEGATIVE_X = 001b |
| 857 | // FACE_POSITIVE_Y = 010b |
| 858 | // FACE_NEGATIVE_Y = 011b |
| 859 | // FACE_POSITIVE_Z = 100b |
| 860 | // FACE_NEGATIVE_Z = 101b |
| 861 | out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n"; |
| 862 | |
| 863 | out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n"; |
| 864 | out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n"; |
| 865 | out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n"; |
| 866 | |
| 867 | out << " t.x = (u * 0.5f / m) + 0.5f;\n"; |
| 868 | out << " t.y = (v * 0.5f / m) + 0.5f;\n"; |
| 869 | } |
| 870 | else if (IsIntegerSampler(textureFunction->sampler) && |
| 871 | textureFunction->method != TextureFunction::FETCH) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 872 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 873 | if (IsSampler2D(textureFunction->sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 874 | { |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 875 | if (IsSamplerArray(textureFunction->sampler)) |
| 876 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 877 | out << " float width; float height; float layers; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 878 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 879 | if (textureFunction->method == TextureFunction::LOD0) |
| 880 | { |
| 881 | out << " uint mip = 0;\n"; |
| 882 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 883 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 884 | { |
| 885 | out << " uint mip = bias;\n"; |
| 886 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 887 | else |
| 888 | { |
| 889 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 890 | textureFunction->method == TextureFunction::BIAS) |
| 891 | { |
| 892 | out << " x.GetDimensions(0, width, height, layers, levels);\n" |
| 893 | " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 894 | " float dx = length(ddx(tSized));\n" |
| 895 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 896 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 897 | |
| 898 | if (textureFunction->method == TextureFunction::BIAS) |
| 899 | { |
| 900 | out << " lod += bias;\n"; |
| 901 | } |
| 902 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 903 | else if (textureFunction->method == TextureFunction::GRAD) |
| 904 | { |
| 905 | out << " x.GetDimensions(0, width, height, layers, levels);\n" |
| 906 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 907 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 908 | |
| 909 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 910 | } |
| 911 | |
| 912 | out << " x.GetDimensions(mip, width, height, layers, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 913 | } |
| 914 | else |
| 915 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 916 | out << " float width; float height; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 917 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 918 | if (textureFunction->method == TextureFunction::LOD0) |
| 919 | { |
| 920 | out << " uint mip = 0;\n"; |
| 921 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 922 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 923 | { |
| 924 | out << " uint mip = bias;\n"; |
| 925 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 926 | else |
| 927 | { |
| 928 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 929 | textureFunction->method == TextureFunction::BIAS) |
| 930 | { |
| 931 | out << " x.GetDimensions(0, width, height, levels);\n" |
| 932 | " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 933 | " float dx = length(ddx(tSized));\n" |
| 934 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 935 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 936 | |
| 937 | if (textureFunction->method == TextureFunction::BIAS) |
| 938 | { |
| 939 | out << " lod += bias;\n"; |
| 940 | } |
| 941 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 942 | else if (textureFunction->method == TextureFunction::LOD) |
| 943 | { |
| 944 | out << " x.GetDimensions(0, width, height, levels);\n"; |
| 945 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 946 | else if (textureFunction->method == TextureFunction::GRAD) |
| 947 | { |
| 948 | out << " x.GetDimensions(0, width, height, levels);\n" |
| 949 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 950 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 951 | |
| 952 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 953 | } |
| 954 | |
| 955 | out << " x.GetDimensions(mip, width, height, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 956 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 957 | } |
| 958 | else if (IsSampler3D(textureFunction->sampler)) |
| 959 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 960 | out << " float width; float height; float depth; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 961 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 962 | if (textureFunction->method == TextureFunction::LOD0) |
| 963 | { |
| 964 | out << " uint mip = 0;\n"; |
| 965 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 966 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 967 | { |
| 968 | out << " uint mip = bias;\n"; |
| 969 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 970 | else |
| 971 | { |
| 972 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 973 | textureFunction->method == TextureFunction::BIAS) |
| 974 | { |
| 975 | out << " x.GetDimensions(0, width, height, depth, levels);\n" |
| 976 | " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n" |
| 977 | " float dx = length(ddx(tSized));\n" |
| 978 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 979 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 980 | |
| 981 | if (textureFunction->method == TextureFunction::BIAS) |
| 982 | { |
| 983 | out << " lod += bias;\n"; |
| 984 | } |
| 985 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 986 | else if (textureFunction->method == TextureFunction::GRAD) |
| 987 | { |
| 988 | out << " x.GetDimensions(0, width, height, depth, levels);\n" |
| 989 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 990 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 991 | |
| 992 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 993 | } |
| 994 | |
| 995 | out << " x.GetDimensions(mip, width, height, depth, levels);\n"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 996 | } |
| 997 | else UNREACHABLE(); |
| 998 | } |
| 999 | |
| 1000 | out << " return "; |
| 1001 | |
| 1002 | // HLSL intrinsic |
| 1003 | if (mOutputType == SH_HLSL9_OUTPUT) |
| 1004 | { |
| 1005 | switch(textureFunction->sampler) |
| 1006 | { |
| 1007 | case EbtSampler2D: out << "tex2D"; break; |
| 1008 | case EbtSamplerCube: out << "texCUBE"; break; |
| 1009 | default: UNREACHABLE(); |
| 1010 | } |
| 1011 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1012 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1013 | { |
| 1014 | case TextureFunction::IMPLICIT: out << "(s, "; break; |
| 1015 | case TextureFunction::BIAS: out << "bias(s, "; break; |
| 1016 | case TextureFunction::LOD: out << "lod(s, "; break; |
| 1017 | case TextureFunction::LOD0: out << "lod(s, "; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1018 | case TextureFunction::LOD0BIAS: out << "lod(s, "; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1019 | default: UNREACHABLE(); |
| 1020 | } |
| 1021 | } |
| 1022 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 1023 | { |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1024 | if (textureFunction->method == TextureFunction::GRAD) |
| 1025 | { |
| 1026 | if (IsIntegerSampler(textureFunction->sampler)) |
| 1027 | { |
| 1028 | out << "x.Load("; |
| 1029 | } |
| 1030 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1031 | { |
| 1032 | out << "x.SampleCmpLevelZero(s, "; |
| 1033 | } |
| 1034 | else |
| 1035 | { |
| 1036 | out << "x.SampleGrad(s, "; |
| 1037 | } |
| 1038 | } |
| 1039 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1040 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1041 | { |
| 1042 | out << "x.Load("; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1043 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1044 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1045 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1046 | switch(textureFunction->method) |
| 1047 | { |
| 1048 | case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break; |
| 1049 | case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break; |
| 1050 | case TextureFunction::LOD: out << "x.SampleCmp(s, "; break; |
| 1051 | case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break; |
| 1052 | case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break; |
| 1053 | default: UNREACHABLE(); |
| 1054 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1055 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1056 | else |
| 1057 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1058 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1059 | { |
| 1060 | case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break; |
| 1061 | case TextureFunction::BIAS: out << "x.SampleBias(s, "; break; |
| 1062 | case TextureFunction::LOD: out << "x.SampleLevel(s, "; break; |
| 1063 | case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1064 | case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1065 | default: UNREACHABLE(); |
| 1066 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1067 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1068 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1069 | else UNREACHABLE(); |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1070 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1071 | // Integer sampling requires integer addresses |
| 1072 | TString addressx = ""; |
| 1073 | TString addressy = ""; |
| 1074 | TString addressz = ""; |
| 1075 | TString close = ""; |
| 1076 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1077 | if (IsIntegerSampler(textureFunction->sampler) || |
| 1078 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1079 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1080 | switch(hlslCoords) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1081 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1082 | case 2: out << "int3("; break; |
| 1083 | case 3: out << "int4("; break; |
| 1084 | default: UNREACHABLE(); |
| 1085 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1086 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1087 | // Convert from normalized floating-point to integer |
| 1088 | if (textureFunction->method != TextureFunction::FETCH) |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1089 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1090 | addressx = "int(floor(width * frac(("; |
| 1091 | addressy = "int(floor(height * frac(("; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1092 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1093 | if (IsSamplerArray(textureFunction->sampler)) |
| 1094 | { |
| 1095 | addressz = "int(max(0, min(layers - 1, floor(0.5 + "; |
| 1096 | } |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1097 | else if (IsSamplerCube(textureFunction->sampler)) |
| 1098 | { |
| 1099 | addressz = "(((("; |
| 1100 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1101 | else |
| 1102 | { |
| 1103 | addressz = "int(floor(depth * frac(("; |
| 1104 | } |
| 1105 | |
| 1106 | close = "))))"; |
| 1107 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | switch(hlslCoords) |
| 1112 | { |
| 1113 | case 2: out << "float2("; break; |
| 1114 | case 3: out << "float3("; break; |
| 1115 | case 4: out << "float4("; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1116 | default: UNREACHABLE(); |
| 1117 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1118 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1119 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1120 | TString proj = ""; // Only used for projected textures |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1121 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1122 | if (textureFunction->proj) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1123 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1124 | switch(textureFunction->coords) |
| 1125 | { |
| 1126 | case 3: proj = " / t.z"; break; |
| 1127 | case 4: proj = " / t.w"; break; |
| 1128 | default: UNREACHABLE(); |
| 1129 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1130 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 1131 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1132 | out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1133 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1134 | if (mOutputType == SH_HLSL9_OUTPUT) |
| 1135 | { |
| 1136 | if (hlslCoords >= 3) |
| 1137 | { |
| 1138 | if (textureFunction->coords < 3) |
| 1139 | { |
| 1140 | out << ", 0"; |
| 1141 | } |
| 1142 | else |
| 1143 | { |
| 1144 | out << ", t.z" + proj; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | if (hlslCoords == 4) |
| 1149 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1150 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1151 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1152 | case TextureFunction::BIAS: out << ", bias"; break; |
| 1153 | case TextureFunction::LOD: out << ", lod"; break; |
| 1154 | case TextureFunction::LOD0: out << ", 0"; break; |
| 1155 | case TextureFunction::LOD0BIAS: out << ", bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1156 | default: UNREACHABLE(); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | out << "));\n"; |
| 1161 | } |
| 1162 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 1163 | { |
| 1164 | if (hlslCoords >= 3) |
| 1165 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1166 | if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler)) |
| 1167 | { |
| 1168 | out << ", face"; |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | out << ", " + addressz + ("t.z" + proj) + close; |
| 1173 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1174 | } |
| 1175 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1176 | if (textureFunction->method == TextureFunction::GRAD) |
| 1177 | { |
| 1178 | if (IsIntegerSampler(textureFunction->sampler)) |
| 1179 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1180 | out << ", mip)"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1181 | } |
| 1182 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1183 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1184 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1185 | if (textureFunction->proj) |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1186 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1187 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1188 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1189 | out << "), t.z" << proj; |
| 1190 | } |
| 1191 | else |
| 1192 | { |
| 1193 | switch(textureFunction->coords) |
| 1194 | { |
| 1195 | case 3: out << "), t.z"; break; |
| 1196 | case 4: out << "), t.w"; break; |
| 1197 | default: UNREACHABLE(); |
| 1198 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | else |
| 1202 | { |
| 1203 | out << "), ddx, ddy"; |
| 1204 | } |
| 1205 | } |
| 1206 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1207 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1208 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1209 | out << ", mip)"; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1210 | } |
| 1211 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1212 | { |
| 1213 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1214 | if (textureFunction->proj) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1215 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1216 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1217 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1218 | out << "), t.z" << proj; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | switch(textureFunction->coords) |
| 1223 | { |
| 1224 | case 3: out << "), t.z"; break; |
| 1225 | case 4: out << "), t.w"; break; |
| 1226 | default: UNREACHABLE(); |
| 1227 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | else |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1231 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1232 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1233 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1234 | case TextureFunction::IMPLICIT: out << ")"; break; |
| 1235 | case TextureFunction::BIAS: out << "), bias"; break; |
| 1236 | case TextureFunction::LOD: out << "), lod"; break; |
| 1237 | case TextureFunction::LOD0: out << "), 0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1238 | case TextureFunction::LOD0BIAS: out << "), bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1239 | default: UNREACHABLE(); |
| 1240 | } |
| 1241 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1242 | |
| 1243 | if (textureFunction->offset) |
| 1244 | { |
| 1245 | out << ", offset"; |
| 1246 | } |
| 1247 | |
| 1248 | out << ");"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1249 | } |
| 1250 | else UNREACHABLE(); |
| 1251 | } |
| 1252 | |
| 1253 | out << "\n" |
| 1254 | "}\n" |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1255 | "\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 1258 | if (mUsesFragCoord) |
| 1259 | { |
| 1260 | out << "#define GL_USES_FRAG_COORD\n"; |
| 1261 | } |
| 1262 | |
| 1263 | if (mUsesPointCoord) |
| 1264 | { |
| 1265 | out << "#define GL_USES_POINT_COORD\n"; |
| 1266 | } |
| 1267 | |
| 1268 | if (mUsesFrontFacing) |
| 1269 | { |
| 1270 | out << "#define GL_USES_FRONT_FACING\n"; |
| 1271 | } |
| 1272 | |
| 1273 | if (mUsesPointSize) |
| 1274 | { |
| 1275 | out << "#define GL_USES_POINT_SIZE\n"; |
| 1276 | } |
| 1277 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1278 | if (mUsesFragDepth) |
| 1279 | { |
| 1280 | out << "#define GL_USES_FRAG_DEPTH\n"; |
| 1281 | } |
| 1282 | |
shannonwoods@chromium.org | 0329988 | 2013-05-30 00:05:26 +0000 | [diff] [blame] | 1283 | if (mUsesDepthRange) |
| 1284 | { |
| 1285 | out << "#define GL_USES_DEPTH_RANGE\n"; |
| 1286 | } |
| 1287 | |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1288 | if (mUsesXor) |
| 1289 | { |
| 1290 | out << "bool xor(bool p, bool q)\n" |
| 1291 | "{\n" |
| 1292 | " return (p || q) && !(p && q);\n" |
| 1293 | "}\n" |
| 1294 | "\n"; |
| 1295 | } |
| 1296 | |
Olli Etuaho | 95cd3c6 | 2015-03-03 16:45:32 +0200 | [diff] [blame] | 1297 | builtInFunctionEmulator->OutputEmulatedFunctions(out); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | void OutputHLSL::visitSymbol(TIntermSymbol *node) |
| 1301 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1302 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1303 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1304 | // Handle accessing std140 structs by value |
| 1305 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1306 | { |
| 1307 | out << mFlaggedStructMappedNames[node]; |
| 1308 | return; |
| 1309 | } |
| 1310 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1311 | TString name = node->getSymbol(); |
| 1312 | |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1313 | if (name == "gl_DepthRange") |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1314 | { |
| 1315 | mUsesDepthRange = true; |
| 1316 | out << name; |
| 1317 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1318 | else |
| 1319 | { |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1320 | TQualifier qualifier = node->getQualifier(); |
| 1321 | |
| 1322 | if (qualifier == EvqUniform) |
| 1323 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1324 | const TType& nodeType = node->getType(); |
| 1325 | const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock(); |
| 1326 | |
| 1327 | if (interfaceBlock) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1328 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1329 | mReferencedInterfaceBlocks[interfaceBlock->name()] = node; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1330 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1331 | else |
| 1332 | { |
| 1333 | mReferencedUniforms[name] = node; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1334 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1335 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1336 | out << DecorateUniform(name, nodeType); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1337 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1338 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1339 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1340 | mReferencedAttributes[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1341 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1342 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1343 | else if (IsVarying(qualifier)) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1344 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1345 | mReferencedVaryings[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1346 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1347 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1348 | else if (qualifier == EvqFragmentOut) |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1349 | { |
| 1350 | mReferencedOutputVariables[name] = node; |
| 1351 | out << "out_" << name; |
| 1352 | } |
| 1353 | else if (qualifier == EvqFragColor) |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1354 | { |
| 1355 | out << "gl_Color[0]"; |
| 1356 | mUsesFragColor = true; |
| 1357 | } |
| 1358 | else if (qualifier == EvqFragData) |
| 1359 | { |
| 1360 | out << "gl_Color"; |
| 1361 | mUsesFragData = true; |
| 1362 | } |
| 1363 | else if (qualifier == EvqFragCoord) |
| 1364 | { |
| 1365 | mUsesFragCoord = true; |
| 1366 | out << name; |
| 1367 | } |
| 1368 | else if (qualifier == EvqPointCoord) |
| 1369 | { |
| 1370 | mUsesPointCoord = true; |
| 1371 | out << name; |
| 1372 | } |
| 1373 | else if (qualifier == EvqFrontFacing) |
| 1374 | { |
| 1375 | mUsesFrontFacing = true; |
| 1376 | out << name; |
| 1377 | } |
| 1378 | else if (qualifier == EvqPointSize) |
| 1379 | { |
| 1380 | mUsesPointSize = true; |
| 1381 | out << name; |
| 1382 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 1383 | else if (qualifier == EvqInstanceID) |
| 1384 | { |
| 1385 | mUsesInstanceID = true; |
| 1386 | out << name; |
| 1387 | } |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1388 | else if (name == "gl_FragDepthEXT") |
| 1389 | { |
| 1390 | mUsesFragDepth = true; |
| 1391 | out << "gl_Depth"; |
| 1392 | } |
Olli Etuaho | 78174db | 2015-04-21 16:14:00 +0300 | [diff] [blame] | 1393 | else if (node->isInternal()) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 1394 | { |
| 1395 | out << name; |
| 1396 | } |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1397 | else |
| 1398 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1399 | out << Decorate(name); |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1400 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1404 | void OutputHLSL::visitRaw(TIntermRaw *node) |
| 1405 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1406 | getInfoSink() << node->getRawText(); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1409 | void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out) |
| 1410 | { |
| 1411 | if (type.isScalar() && !type.isArray()) |
| 1412 | { |
| 1413 | if (op == EOpEqual) |
| 1414 | { |
| 1415 | outputTriplet(visit, "(", " == ", ")", out); |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | outputTriplet(visit, "(", " != ", ")", out); |
| 1420 | } |
| 1421 | } |
| 1422 | else |
| 1423 | { |
| 1424 | if (visit == PreVisit && op == EOpNotEqual) |
| 1425 | { |
| 1426 | out << "!"; |
| 1427 | } |
| 1428 | |
| 1429 | if (type.isArray()) |
| 1430 | { |
| 1431 | const TString &functionName = addArrayEqualityFunction(type); |
| 1432 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out); |
| 1433 | } |
| 1434 | else if (type.getBasicType() == EbtStruct) |
| 1435 | { |
| 1436 | const TStructure &structure = *type.getStruct(); |
| 1437 | const TString &functionName = addStructEqualityFunction(structure); |
| 1438 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out); |
| 1439 | } |
| 1440 | else |
| 1441 | { |
| 1442 | ASSERT(type.isMatrix() || type.isVector()); |
| 1443 | outputTriplet(visit, "all(", " == ", ")", out); |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1448 | bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) |
| 1449 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1450 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1451 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1452 | // Handle accessing std140 structs by value |
| 1453 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1454 | { |
| 1455 | out << mFlaggedStructMappedNames[node]; |
| 1456 | return false; |
| 1457 | } |
| 1458 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1459 | switch (node->getOp()) |
| 1460 | { |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1461 | case EOpAssign: |
| 1462 | if (node->getLeft()->isArray()) |
| 1463 | { |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1464 | TIntermAggregate *rightAgg = node->getRight()->getAsAggregate(); |
| 1465 | if (rightAgg != nullptr && rightAgg->isConstructor()) |
| 1466 | { |
| 1467 | const TString &functionName = addArrayConstructIntoFunction(node->getType()); |
| 1468 | out << functionName << "("; |
| 1469 | node->getLeft()->traverse(this); |
| 1470 | TIntermSequence *seq = rightAgg->getSequence(); |
| 1471 | for (auto &arrayElement : *seq) |
| 1472 | { |
| 1473 | out << ", "; |
| 1474 | arrayElement->traverse(this); |
| 1475 | } |
| 1476 | out << ")"; |
| 1477 | return false; |
| 1478 | } |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 1479 | // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned. |
| 1480 | ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall); |
| 1481 | |
| 1482 | const TString &functionName = addArrayAssignmentFunction(node->getType()); |
| 1483 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1484 | } |
| 1485 | else |
| 1486 | { |
| 1487 | outputTriplet(visit, "(", " = ", ")"); |
| 1488 | } |
| 1489 | break; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1490 | case EOpInitialize: |
| 1491 | if (visit == PreVisit) |
| 1492 | { |
| 1493 | // GLSL allows to write things like "float x = x;" where a new variable x is defined |
| 1494 | // and the value of an existing variable x is assigned. HLSL uses C semantics (the |
| 1495 | // new variable is created before the assignment is evaluated), so we need to convert |
| 1496 | // this to "float t = x, x = t;". |
| 1497 | |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1498 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1499 | ASSERT(symbolNode); |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1500 | TIntermTyped *expression = node->getRight(); |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1501 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1502 | // TODO (jmadill): do a 'deep' scan to know if an expression is statically const |
| 1503 | if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst) |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1504 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1505 | // For variables which are not constant, defer their real initialization until |
| 1506 | // after we initialize other globals: uniforms, attributes and varyings. |
| 1507 | mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression)); |
| 1508 | const TString &initString = initializer(node->getType()); |
| 1509 | node->setRight(new TIntermRaw(node->getType(), initString)); |
| 1510 | } |
| 1511 | else if (writeSameSymbolInitializer(out, symbolNode, expression)) |
| 1512 | { |
| 1513 | // Skip initializing the rest of the expression |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1514 | return false; |
| 1515 | } |
| 1516 | } |
| 1517 | else if (visit == InVisit) |
| 1518 | { |
| 1519 | out << " = "; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1520 | } |
| 1521 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1522 | case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break; |
| 1523 | case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break; |
| 1524 | case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1525 | case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1526 | case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1527 | case EOpVectorTimesMatrixAssign: |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1528 | if (visit == PreVisit) |
| 1529 | { |
| 1530 | out << "("; |
| 1531 | } |
| 1532 | else if (visit == InVisit) |
| 1533 | { |
| 1534 | out << " = mul("; |
| 1535 | node->getLeft()->traverse(this); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1536 | out << ", transpose("; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1537 | } |
| 1538 | else |
| 1539 | { |
daniel@transgaming.com | 3aa7420 | 2010-04-29 03:39:04 +0000 | [diff] [blame] | 1540 | out << ")))"; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1541 | } |
| 1542 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1543 | case EOpMatrixTimesMatrixAssign: |
| 1544 | if (visit == PreVisit) |
| 1545 | { |
| 1546 | out << "("; |
| 1547 | } |
| 1548 | else if (visit == InVisit) |
| 1549 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1550 | out << " = transpose(mul(transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1551 | node->getLeft()->traverse(this); |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1552 | out << "), transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1553 | } |
| 1554 | else |
| 1555 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1556 | out << "))))"; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1557 | } |
| 1558 | break; |
| 1559 | case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1560 | case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1561 | case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break; |
| 1562 | case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break; |
| 1563 | case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break; |
| 1564 | case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break; |
| 1565 | case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break; |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1566 | case EOpIndexDirect: |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1567 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1568 | const TType& leftType = node->getLeft()->getType(); |
| 1569 | if (leftType.isInterfaceBlock()) |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1570 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1571 | if (visit == PreVisit) |
| 1572 | { |
| 1573 | TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock(); |
| 1574 | const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1575 | mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1576 | out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1577 | return false; |
| 1578 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1579 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1580 | else |
| 1581 | { |
| 1582 | outputTriplet(visit, "", "[", "]"); |
| 1583 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1584 | } |
| 1585 | break; |
| 1586 | case EOpIndexIndirect: |
| 1587 | // We do not currently support indirect references to interface blocks |
| 1588 | ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock); |
| 1589 | outputTriplet(visit, "", "[", "]"); |
| 1590 | break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1591 | case EOpIndexDirectStruct: |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1592 | if (visit == InVisit) |
| 1593 | { |
| 1594 | const TStructure* structure = node->getLeft()->getType().getStruct(); |
| 1595 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1596 | const TField* field = structure->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1597 | out << "." + DecorateField(field->name(), *structure); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1598 | |
| 1599 | return false; |
| 1600 | } |
| 1601 | break; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1602 | case EOpIndexDirectInterfaceBlock: |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1603 | if (visit == InVisit) |
| 1604 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1605 | const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock(); |
| 1606 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1607 | const TField* field = interfaceBlock->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1608 | out << "." + Decorate(field->name()); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1609 | |
| 1610 | return false; |
| 1611 | } |
| 1612 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1613 | case EOpVectorSwizzle: |
| 1614 | if (visit == InVisit) |
| 1615 | { |
| 1616 | out << "."; |
| 1617 | |
| 1618 | TIntermAggregate *swizzle = node->getRight()->getAsAggregate(); |
| 1619 | |
| 1620 | if (swizzle) |
| 1621 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1622 | TIntermSequence *sequence = swizzle->getSequence(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1623 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1624 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1625 | { |
| 1626 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 1627 | |
| 1628 | if (element) |
| 1629 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 1630 | int i = element->getIConst(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1631 | |
| 1632 | switch (i) |
| 1633 | { |
| 1634 | case 0: out << "x"; break; |
| 1635 | case 1: out << "y"; break; |
| 1636 | case 2: out << "z"; break; |
| 1637 | case 3: out << "w"; break; |
| 1638 | default: UNREACHABLE(); |
| 1639 | } |
| 1640 | } |
| 1641 | else UNREACHABLE(); |
| 1642 | } |
| 1643 | } |
| 1644 | else UNREACHABLE(); |
| 1645 | |
| 1646 | return false; // Fully processed |
| 1647 | } |
| 1648 | break; |
| 1649 | case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break; |
| 1650 | case EOpSub: outputTriplet(visit, "(", " - ", ")"); break; |
| 1651 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
| 1652 | case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1653 | case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1654 | case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break; |
| 1655 | case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break; |
| 1656 | case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break; |
| 1657 | case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break; |
| 1658 | case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break; |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1659 | case EOpEqual: |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1660 | case EOpNotEqual: |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1661 | outputEqual(visit, node->getLeft()->getType(), node->getOp(), out); |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1662 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1663 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 1664 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 1665 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 1666 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 1667 | case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1668 | case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1669 | case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break; |
| 1670 | case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break; |
daniel@transgaming.com | 69f084b | 2010-04-23 18:34:46 +0000 | [diff] [blame] | 1671 | case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1672 | case EOpLogicalOr: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1673 | if (node->getRight()->hasSideEffects()) |
| 1674 | { |
| 1675 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1676 | return false; |
| 1677 | } |
| 1678 | else |
| 1679 | { |
| 1680 | outputTriplet(visit, "(", " || ", ")"); |
| 1681 | return true; |
| 1682 | } |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1683 | case EOpLogicalXor: |
| 1684 | mUsesXor = true; |
| 1685 | outputTriplet(visit, "xor(", ", ", ")"); |
| 1686 | break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1687 | case EOpLogicalAnd: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1688 | if (node->getRight()->hasSideEffects()) |
| 1689 | { |
| 1690 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1691 | return false; |
| 1692 | } |
| 1693 | else |
| 1694 | { |
| 1695 | outputTriplet(visit, "(", " && ", ")"); |
| 1696 | return true; |
| 1697 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1698 | default: UNREACHABLE(); |
| 1699 | } |
| 1700 | |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
| 1704 | bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) |
| 1705 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1706 | switch (node->getOp()) |
| 1707 | { |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1708 | case EOpNegative: outputTriplet(visit, "(-", "", ")"); break; |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 1709 | case EOpPositive: outputTriplet(visit, "(+", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1710 | case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
| 1711 | case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1712 | case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1713 | case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break; |
| 1714 | case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break; |
| 1715 | case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break; |
| 1716 | case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1717 | case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break; |
| 1718 | case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break; |
| 1719 | case EOpSin: outputTriplet(visit, "sin(", "", ")"); break; |
| 1720 | case EOpCos: outputTriplet(visit, "cos(", "", ")"); break; |
| 1721 | case EOpTan: outputTriplet(visit, "tan(", "", ")"); break; |
| 1722 | case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break; |
| 1723 | case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break; |
| 1724 | case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break; |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 1725 | case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break; |
| 1726 | case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break; |
| 1727 | case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break; |
| 1728 | case EOpAsinh: |
| 1729 | ASSERT(node->getUseEmulatedFunction()); |
| 1730 | writeEmulatedFunctionTriplet(visit, "asinh("); |
| 1731 | break; |
| 1732 | case EOpAcosh: |
| 1733 | ASSERT(node->getUseEmulatedFunction()); |
| 1734 | writeEmulatedFunctionTriplet(visit, "acosh("); |
| 1735 | break; |
| 1736 | case EOpAtanh: |
| 1737 | ASSERT(node->getUseEmulatedFunction()); |
| 1738 | writeEmulatedFunctionTriplet(visit, "atanh("); |
| 1739 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1740 | case EOpExp: outputTriplet(visit, "exp(", "", ")"); break; |
| 1741 | case EOpLog: outputTriplet(visit, "log(", "", ")"); break; |
| 1742 | case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break; |
| 1743 | case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break; |
| 1744 | case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break; |
| 1745 | case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break; |
| 1746 | case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break; |
| 1747 | case EOpSign: outputTriplet(visit, "sign(", "", ")"); break; |
| 1748 | case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break; |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame] | 1749 | case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break; |
| 1750 | case EOpRound: outputTriplet(visit, "round(", "", ")"); break; |
| 1751 | case EOpRoundEven: |
| 1752 | ASSERT(node->getUseEmulatedFunction()); |
| 1753 | writeEmulatedFunctionTriplet(visit, "roundEven("); |
| 1754 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1755 | case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break; |
| 1756 | case EOpFract: outputTriplet(visit, "frac(", "", ")"); break; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 1757 | case EOpIsNan: |
| 1758 | outputTriplet(visit, "isnan(", "", ")"); |
| 1759 | mRequiresIEEEStrictCompiling = true; |
| 1760 | break; |
Arun Patole | 0c1726e | 2015-02-18 14:35:02 +0530 | [diff] [blame] | 1761 | case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break; |
Olli Etuaho | e8d2c07 | 2015-01-08 16:33:54 +0200 | [diff] [blame] | 1762 | case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break; |
| 1763 | case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break; |
| 1764 | case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
| 1765 | case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 1766 | case EOpPackSnorm2x16: |
| 1767 | ASSERT(node->getUseEmulatedFunction()); |
| 1768 | writeEmulatedFunctionTriplet(visit, "packSnorm2x16("); |
| 1769 | break; |
| 1770 | case EOpPackUnorm2x16: |
| 1771 | ASSERT(node->getUseEmulatedFunction()); |
| 1772 | writeEmulatedFunctionTriplet(visit, "packUnorm2x16("); |
| 1773 | break; |
| 1774 | case EOpPackHalf2x16: |
| 1775 | ASSERT(node->getUseEmulatedFunction()); |
| 1776 | writeEmulatedFunctionTriplet(visit, "packHalf2x16("); |
| 1777 | break; |
| 1778 | case EOpUnpackSnorm2x16: |
| 1779 | ASSERT(node->getUseEmulatedFunction()); |
| 1780 | writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16("); |
| 1781 | break; |
| 1782 | case EOpUnpackUnorm2x16: |
| 1783 | ASSERT(node->getUseEmulatedFunction()); |
| 1784 | writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16("); |
| 1785 | break; |
| 1786 | case EOpUnpackHalf2x16: |
| 1787 | ASSERT(node->getUseEmulatedFunction()); |
| 1788 | writeEmulatedFunctionTriplet(visit, "unpackHalf2x16("); |
| 1789 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1790 | case EOpLength: outputTriplet(visit, "length(", "", ")"); break; |
| 1791 | case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break; |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1792 | case EOpDFdx: |
| 1793 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1794 | { |
| 1795 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1796 | } |
| 1797 | else |
| 1798 | { |
| 1799 | outputTriplet(visit, "ddx(", "", ")"); |
| 1800 | } |
| 1801 | break; |
| 1802 | case EOpDFdy: |
| 1803 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1804 | { |
| 1805 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1806 | } |
| 1807 | else |
| 1808 | { |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1809 | outputTriplet(visit, "ddy(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1810 | } |
| 1811 | break; |
| 1812 | case EOpFwidth: |
| 1813 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1814 | { |
| 1815 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1816 | } |
| 1817 | else |
| 1818 | { |
| 1819 | outputTriplet(visit, "fwidth(", "", ")"); |
| 1820 | } |
| 1821 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1822 | case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break; |
| 1823 | case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break; |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 1824 | case EOpInverse: |
| 1825 | ASSERT(node->getUseEmulatedFunction()); |
| 1826 | writeEmulatedFunctionTriplet(visit, "inverse("); |
| 1827 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1828 | |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1829 | case EOpAny: outputTriplet(visit, "any(", "", ")"); break; |
| 1830 | case EOpAll: outputTriplet(visit, "all(", "", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1831 | default: UNREACHABLE(); |
| 1832 | } |
| 1833 | |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) |
| 1838 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1839 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1840 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1841 | switch (node->getOp()) |
| 1842 | { |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1843 | case EOpSequence: |
| 1844 | { |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1845 | if (mInsideFunction) |
| 1846 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1847 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1848 | out << "{\n"; |
| 1849 | } |
| 1850 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1851 | for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++) |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1852 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1853 | outputLineDirective((*sit)->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 1854 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 1855 | traverseStatements(*sit); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1856 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 1857 | // Don't output ; after case labels, they're terminated by : |
| 1858 | // This is needed especially since outputting a ; after a case statement would turn empty |
| 1859 | // case statements into non-empty case statements, disallowing fall-through from them. |
| 1860 | if ((*sit)->getAsCaseNode() == nullptr) |
| 1861 | out << ";\n"; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1864 | if (mInsideFunction) |
| 1865 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1866 | outputLineDirective(node->getLine().last_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1867 | out << "}\n"; |
| 1868 | } |
| 1869 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1872 | case EOpDeclaration: |
| 1873 | if (visit == PreVisit) |
| 1874 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1875 | TIntermSequence *sequence = node->getSequence(); |
| 1876 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1877 | |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 1878 | if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1879 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1880 | TStructure *structure = variable->getType().getStruct(); |
| 1881 | |
| 1882 | if (structure) |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1883 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1884 | mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL); |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1887 | if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1888 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1889 | for (const auto &seqElement : *sequence) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1890 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1891 | if (isSingleStatement(seqElement)) |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1892 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1893 | mUnfoldShortCircuit->traverse(seqElement); |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1896 | if (!mInsideFunction) |
| 1897 | { |
| 1898 | out << "static "; |
| 1899 | } |
| 1900 | |
| 1901 | out << TypeString(variable->getType()) + " "; |
| 1902 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1903 | TIntermSymbol *symbol = seqElement->getAsSymbolNode(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1904 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1905 | if (symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1906 | { |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1907 | symbol->traverse(this); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1908 | out << ArrayString(symbol->getType()); |
Jamie Madill | 79bb0d9 | 2013-12-09 16:20:28 -0500 | [diff] [blame] | 1909 | out << " = " + initializer(symbol->getType()); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1910 | } |
| 1911 | else |
| 1912 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1913 | seqElement->traverse(this); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1916 | if (seqElement != sequence->back()) |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1917 | { |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1918 | out << ";\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1919 | } |
| 1920 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1921 | } |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1922 | else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration |
| 1923 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1924 | // Already added to constructor map |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1925 | } |
| 1926 | else UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1927 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1928 | else if (variable && IsVaryingOut(variable->getQualifier())) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1929 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1930 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1931 | { |
| 1932 | TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); |
| 1933 | |
| 1934 | if (symbol) |
| 1935 | { |
| 1936 | // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking |
| 1937 | mReferencedVaryings[symbol->getSymbol()] = symbol; |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | (*sit)->traverse(this); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1946 | return false; |
| 1947 | } |
| 1948 | else if (visit == InVisit) |
| 1949 | { |
| 1950 | out << ", "; |
| 1951 | } |
| 1952 | break; |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 1953 | case EOpInvariantDeclaration: |
| 1954 | // Do not do any translation |
| 1955 | return false; |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1956 | case EOpPrototype: |
| 1957 | if (visit == PreVisit) |
| 1958 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 1959 | size_t index = mCallDag.findIndex(node); |
| 1960 | // Skip the prototype if it is not implemented (and thus not used) |
| 1961 | if (index == CallDAG::InvalidIndex) |
| 1962 | { |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
Olli Etuaho | 76acee8 | 2014-11-04 13:44:03 +0200 | [diff] [blame] | 1966 | out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1967 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1968 | TIntermSequence *arguments = node->getSequence(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1969 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1970 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1971 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1972 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1973 | |
| 1974 | if (symbol) |
| 1975 | { |
| 1976 | out << argumentString(symbol); |
| 1977 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1978 | if (i < arguments->size() - 1) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1979 | { |
| 1980 | out << ", "; |
| 1981 | } |
| 1982 | } |
| 1983 | else UNREACHABLE(); |
| 1984 | } |
| 1985 | |
| 1986 | out << ");\n"; |
| 1987 | |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 1988 | // Also prototype the Lod0 variant if needed |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 1989 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 1990 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 1991 | { |
| 1992 | mOutputLod0Function = true; |
| 1993 | node->traverse(this); |
| 1994 | mOutputLod0Function = false; |
| 1995 | } |
| 1996 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1997 | return false; |
| 1998 | } |
| 1999 | break; |
daniel@transgaming.com | ed2180d | 2012-03-26 17:08:54 +0000 | [diff] [blame] | 2000 | case EOpComma: outputTriplet(visit, "(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2001 | case EOpFunction: |
| 2002 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2003 | ASSERT(mCurrentFunctionMetadata == nullptr); |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 2004 | TString name = TFunction::unmangleName(node->getName()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2005 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2006 | size_t index = mCallDag.findIndex(node); |
| 2007 | ASSERT(index != CallDAG::InvalidIndex); |
| 2008 | mCurrentFunctionMetadata = &mASTMetadataList[index]; |
| 2009 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2010 | out << TypeString(node->getType()) << " "; |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2011 | |
| 2012 | if (name == "main") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2013 | { |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2014 | out << "gl_main("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2015 | } |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2016 | else |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2017 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2018 | out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2019 | } |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 2020 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2021 | TIntermSequence *sequence = node->getSequence(); |
| 2022 | TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2023 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2024 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2025 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2026 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2027 | |
| 2028 | if (symbol) |
| 2029 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 2030 | TStructure *structure = symbol->getType().getStruct(); |
| 2031 | |
| 2032 | if (structure) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2033 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 2034 | mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | out << argumentString(symbol); |
| 2038 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2039 | if (i < arguments->size() - 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2040 | { |
| 2041 | out << ", "; |
| 2042 | } |
| 2043 | } |
| 2044 | else UNREACHABLE(); |
| 2045 | } |
| 2046 | |
| 2047 | out << ")\n" |
| 2048 | "{\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2049 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2050 | if (sequence->size() > 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2051 | { |
| 2052 | mInsideFunction = true; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2053 | (*sequence)[1]->traverse(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2054 | mInsideFunction = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2055 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2056 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2057 | out << "}\n"; |
| 2058 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2059 | mCurrentFunctionMetadata = nullptr; |
| 2060 | |
| 2061 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2062 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2063 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2064 | ASSERT(name != "main"); |
| 2065 | mOutputLod0Function = true; |
| 2066 | node->traverse(this); |
| 2067 | mOutputLod0Function = false; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2070 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2071 | } |
| 2072 | break; |
| 2073 | case EOpFunctionCall: |
| 2074 | { |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2075 | TString name = TFunction::unmangleName(node->getName()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2076 | TIntermSequence *arguments = node->getSequence(); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2077 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2078 | bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2079 | if (node->isUserDefined()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2080 | { |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2081 | if (node->isArray()) |
| 2082 | { |
| 2083 | UNIMPLEMENTED(); |
| 2084 | } |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2085 | size_t index = mCallDag.findIndex(node); |
| 2086 | ASSERT(index != CallDAG::InvalidIndex); |
| 2087 | lod0 &= mASTMetadataList[index].mNeedsLod0; |
| 2088 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2089 | out << Decorate(name) << (lod0 ? "Lod0(" : "("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2090 | } |
| 2091 | else |
| 2092 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2093 | TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType(); |
shannonwoods@chromium.org | c6ac65f | 2013-05-30 00:02:50 +0000 | [diff] [blame] | 2094 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2095 | TextureFunction textureFunction; |
| 2096 | textureFunction.sampler = samplerType; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2097 | textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize(); |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2098 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2099 | textureFunction.proj = false; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2100 | textureFunction.offset = false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2101 | |
| 2102 | if (name == "texture2D" || name == "textureCube" || name == "texture") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2103 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2104 | textureFunction.method = TextureFunction::IMPLICIT; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2105 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2106 | else if (name == "texture2DProj" || name == "textureProj") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2107 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2108 | textureFunction.method = TextureFunction::IMPLICIT; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2109 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2110 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2111 | else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" || |
| 2112 | name == "texture2DLodEXT" || name == "textureCubeLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2113 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2114 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2115 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2116 | else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2117 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2118 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2119 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2120 | } |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2121 | else if (name == "textureSize") |
| 2122 | { |
| 2123 | textureFunction.method = TextureFunction::SIZE; |
| 2124 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2125 | else if (name == "textureOffset") |
| 2126 | { |
| 2127 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2128 | textureFunction.offset = true; |
| 2129 | } |
Nicolas Capens | df86c6b | 2014-02-14 20:09:17 -0500 | [diff] [blame] | 2130 | else if (name == "textureProjOffset") |
| 2131 | { |
| 2132 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2133 | textureFunction.offset = true; |
| 2134 | textureFunction.proj = true; |
| 2135 | } |
| 2136 | else if (name == "textureLodOffset") |
| 2137 | { |
| 2138 | textureFunction.method = TextureFunction::LOD; |
| 2139 | textureFunction.offset = true; |
| 2140 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 2141 | else if (name == "textureProjLodOffset") |
| 2142 | { |
| 2143 | textureFunction.method = TextureFunction::LOD; |
| 2144 | textureFunction.proj = true; |
| 2145 | textureFunction.offset = true; |
| 2146 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 2147 | else if (name == "texelFetch") |
| 2148 | { |
| 2149 | textureFunction.method = TextureFunction::FETCH; |
| 2150 | } |
| 2151 | else if (name == "texelFetchOffset") |
| 2152 | { |
| 2153 | textureFunction.method = TextureFunction::FETCH; |
| 2154 | textureFunction.offset = true; |
| 2155 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2156 | else if (name == "textureGrad" || name == "texture2DGradEXT") |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 2157 | { |
| 2158 | textureFunction.method = TextureFunction::GRAD; |
| 2159 | } |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 2160 | else if (name == "textureGradOffset") |
| 2161 | { |
| 2162 | textureFunction.method = TextureFunction::GRAD; |
| 2163 | textureFunction.offset = true; |
| 2164 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2165 | else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT") |
Nicolas Capens | f7378e3 | 2014-02-19 17:29:32 -0500 | [diff] [blame] | 2166 | { |
| 2167 | textureFunction.method = TextureFunction::GRAD; |
| 2168 | textureFunction.proj = true; |
| 2169 | } |
| 2170 | else if (name == "textureProjGradOffset") |
| 2171 | { |
| 2172 | textureFunction.method = TextureFunction::GRAD; |
| 2173 | textureFunction.proj = true; |
| 2174 | textureFunction.offset = true; |
| 2175 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2176 | else UNREACHABLE(); |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2177 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2178 | if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2179 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2180 | unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments |
| 2181 | |
| 2182 | if (textureFunction.offset) |
| 2183 | { |
| 2184 | mandatoryArgumentCount++; |
| 2185 | } |
| 2186 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2187 | bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2188 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2189 | if (lod0 || mShaderType == GL_VERTEX_SHADER) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2190 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2191 | if (bias) |
| 2192 | { |
| 2193 | textureFunction.method = TextureFunction::LOD0BIAS; |
| 2194 | } |
| 2195 | else |
| 2196 | { |
| 2197 | textureFunction.method = TextureFunction::LOD0; |
| 2198 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2199 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2200 | else if (bias) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2201 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2202 | textureFunction.method = TextureFunction::BIAS; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | mUsesTexture.insert(textureFunction); |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2207 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2208 | out << textureFunction.name(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2209 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2210 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2211 | for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2212 | { |
| 2213 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType())) |
| 2214 | { |
| 2215 | out << "texture_"; |
| 2216 | (*arg)->traverse(this); |
| 2217 | out << ", sampler_"; |
| 2218 | } |
| 2219 | |
| 2220 | (*arg)->traverse(this); |
| 2221 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2222 | if (arg < arguments->end() - 1) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2223 | { |
| 2224 | out << ", "; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | out << ")"; |
| 2229 | |
| 2230 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2231 | } |
| 2232 | break; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2233 | case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2234 | case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break; |
| 2235 | case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break; |
| 2236 | case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break; |
| 2237 | case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break; |
| 2238 | case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break; |
| 2239 | case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break; |
| 2240 | case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break; |
| 2241 | case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break; |
| 2242 | case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break; |
| 2243 | case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break; |
| 2244 | case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break; |
| 2245 | case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break; |
| 2246 | case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break; |
| 2247 | case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break; |
| 2248 | case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break; |
| 2249 | case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break; |
| 2250 | case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break; |
| 2251 | case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break; |
| 2252 | case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break; |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2253 | case EOpConstructStruct: |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2254 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 2255 | if (node->getType().isArray()) |
| 2256 | { |
| 2257 | UNIMPLEMENTED(); |
| 2258 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2259 | const TString &structName = StructNameString(*node->getType().getStruct()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2260 | mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence()); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2261 | outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")"); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2262 | } |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2263 | break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2264 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 2265 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 2266 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 2267 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 2268 | case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break; |
| 2269 | case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2270 | case EOpMod: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2271 | ASSERT(node->getUseEmulatedFunction()); |
| 2272 | writeEmulatedFunctionTriplet(visit, "mod("); |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2273 | break; |
Olli Etuaho | b6e07a6 | 2015-02-16 12:22:10 +0200 | [diff] [blame] | 2274 | case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2275 | case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2276 | case EOpAtan: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2277 | ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2278 | ASSERT(node->getUseEmulatedFunction()); |
| 2279 | writeEmulatedFunctionTriplet(visit, "atan("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break; |
| 2282 | case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break; |
| 2283 | case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break; |
| 2284 | case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break; |
| 2285 | case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break; |
| 2286 | case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break; |
| 2287 | case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break; |
| 2288 | case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break; |
| 2289 | case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break; |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2290 | case EOpFaceForward: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2291 | ASSERT(node->getUseEmulatedFunction()); |
| 2292 | writeEmulatedFunctionTriplet(visit, "faceforward("); |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2293 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2294 | case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break; |
| 2295 | case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2296 | case EOpOuterProduct: |
| 2297 | ASSERT(node->getUseEmulatedFunction()); |
| 2298 | writeEmulatedFunctionTriplet(visit, "outerProduct("); |
| 2299 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2300 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2301 | default: UNREACHABLE(); |
| 2302 | } |
| 2303 | |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
| 2307 | bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node) |
| 2308 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2309 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2310 | |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2311 | if (node->usesTernaryOperator()) |
| 2312 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2313 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2314 | } |
| 2315 | else // if/else statement |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2316 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2317 | mUnfoldShortCircuit->traverse(node->getCondition()); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2318 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2319 | // D3D errors when there is a gradient operation in a loop in an unflattened if. |
| 2320 | if (mShaderType == GL_FRAGMENT_SHADER |
| 2321 | && mCurrentFunctionMetadata->hasDiscontinuousLoop(node) |
| 2322 | && mCurrentFunctionMetadata->hasGradientInCallGraph(node)) |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2323 | { |
| 2324 | out << "FLATTEN "; |
| 2325 | } |
| 2326 | |
| 2327 | out << "if ("; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2328 | |
| 2329 | node->getCondition()->traverse(this); |
| 2330 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2331 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2332 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2333 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2334 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2335 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2336 | bool discard = false; |
| 2337 | |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2338 | if (node->getTrueBlock()) |
| 2339 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2340 | traverseStatements(node->getTrueBlock()); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2341 | |
| 2342 | // Detect true discard |
| 2343 | discard = (discard || FindDiscard::search(node->getTrueBlock())); |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2344 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2345 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2346 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2347 | out << ";\n}\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2348 | |
| 2349 | if (node->getFalseBlock()) |
| 2350 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2351 | out << "else\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2352 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2353 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2354 | out << "{\n"; |
| 2355 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2356 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2357 | traverseStatements(node->getFalseBlock()); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2358 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2359 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2360 | out << ";\n}\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2361 | |
| 2362 | // Detect false discard |
| 2363 | discard = (discard || FindDiscard::search(node->getFalseBlock())); |
| 2364 | } |
| 2365 | |
| 2366 | // ANGLE issue 486: Detect problematic conditional discard |
| 2367 | if (discard && FindSideEffectRewriting::search(node)) |
| 2368 | { |
| 2369 | mUsesDiscardRewriting = true; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2372 | |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2376 | bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2377 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2378 | if (node->getStatementList()) |
| 2379 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 2380 | node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList())); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2381 | outputTriplet(visit, "switch (", ") ", ""); |
| 2382 | // The curly braces get written when visiting the statementList aggregate |
| 2383 | } |
| 2384 | else |
| 2385 | { |
| 2386 | // No statementList, so it won't output curly braces |
| 2387 | outputTriplet(visit, "switch (", ") {", "}\n"); |
| 2388 | } |
| 2389 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2390 | } |
| 2391 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2392 | bool OutputHLSL::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2393 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2394 | if (node->hasCondition()) |
| 2395 | { |
| 2396 | outputTriplet(visit, "case (", "", "):\n"); |
| 2397 | return true; |
| 2398 | } |
| 2399 | else |
| 2400 | { |
| 2401 | TInfoSinkBase &out = getInfoSink(); |
| 2402 | out << "default:\n"; |
| 2403 | return false; |
| 2404 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2407 | void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) |
| 2408 | { |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2409 | writeConstantUnion(node->getType(), node->getUnionArrayPointer()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) |
| 2413 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2414 | mNestedLoopDepth++; |
| 2415 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2416 | bool wasDiscontinuous = mInsideDiscontinuousLoop; |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2417 | mInsideDiscontinuousLoop = mInsideDiscontinuousLoop || |
Corentin Wallez | a1884f2 | 2015-04-29 10:15:16 -0700 | [diff] [blame^] | 2418 | mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2419 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2420 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2421 | { |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2422 | if (handleExcessiveLoop(node)) |
| 2423 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2424 | mInsideDiscontinuousLoop = wasDiscontinuous; |
| 2425 | mNestedLoopDepth--; |
| 2426 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2427 | return false; |
| 2428 | } |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2431 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2432 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2433 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2434 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2435 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2436 | out << "{" << unroll << " do\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2437 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2438 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2439 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2440 | } |
| 2441 | else |
| 2442 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2443 | out << "{" << unroll << " for("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2444 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2445 | if (node->getInit()) |
| 2446 | { |
| 2447 | node->getInit()->traverse(this); |
| 2448 | } |
| 2449 | |
| 2450 | out << "; "; |
| 2451 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2452 | if (node->getCondition()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2453 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2454 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | out << "; "; |
| 2458 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2459 | if (node->getExpression()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2460 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2461 | node->getExpression()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2464 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2465 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2466 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2467 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | if (node->getBody()) |
| 2471 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2472 | traverseStatements(node->getBody()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2475 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 7fb81e8 | 2011-09-23 18:20:46 +0000 | [diff] [blame] | 2476 | out << ";}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2477 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2478 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2479 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2480 | outputLineDirective(node->getCondition()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2481 | out << "while(\n"; |
| 2482 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2483 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2484 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2485 | out << ");"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2488 | out << "}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2489 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2490 | mInsideDiscontinuousLoop = wasDiscontinuous; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2491 | mNestedLoopDepth--; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2492 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2493 | return false; |
| 2494 | } |
| 2495 | |
| 2496 | bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) |
| 2497 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2498 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2499 | |
| 2500 | switch (node->getFlowOp()) |
| 2501 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2502 | case EOpKill: |
| 2503 | outputTriplet(visit, "discard;\n", "", ""); |
| 2504 | break; |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2505 | case EOpBreak: |
| 2506 | if (visit == PreVisit) |
| 2507 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2508 | if (mNestedLoopDepth > 1) |
| 2509 | { |
| 2510 | mUsesNestedBreak = true; |
| 2511 | } |
| 2512 | |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2513 | if (mExcessiveLoopIndex) |
| 2514 | { |
| 2515 | out << "{Break"; |
| 2516 | mExcessiveLoopIndex->traverse(this); |
| 2517 | out << " = true; break;}\n"; |
| 2518 | } |
| 2519 | else |
| 2520 | { |
| 2521 | out << "break;\n"; |
| 2522 | } |
| 2523 | } |
| 2524 | break; |
apatrick@chromium.org | 05a5d8e | 2011-02-16 19:07:20 +0000 | [diff] [blame] | 2525 | case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2526 | case EOpReturn: |
| 2527 | if (visit == PreVisit) |
| 2528 | { |
| 2529 | if (node->getExpression()) |
| 2530 | { |
| 2531 | out << "return "; |
| 2532 | } |
| 2533 | else |
| 2534 | { |
| 2535 | out << "return;\n"; |
| 2536 | } |
| 2537 | } |
| 2538 | else if (visit == PostVisit) |
| 2539 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2540 | if (node->getExpression()) |
| 2541 | { |
| 2542 | out << ";\n"; |
| 2543 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2544 | } |
| 2545 | break; |
| 2546 | default: UNREACHABLE(); |
| 2547 | } |
| 2548 | |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2552 | void OutputHLSL::traverseStatements(TIntermNode *node) |
| 2553 | { |
| 2554 | if (isSingleStatement(node)) |
| 2555 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2556 | mUnfoldShortCircuit->traverse(node); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | node->traverse(this); |
| 2560 | } |
| 2561 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2562 | bool OutputHLSL::isSingleStatement(TIntermNode *node) |
| 2563 | { |
| 2564 | TIntermAggregate *aggregate = node->getAsAggregate(); |
| 2565 | |
| 2566 | if (aggregate) |
| 2567 | { |
| 2568 | if (aggregate->getOp() == EOpSequence) |
| 2569 | { |
| 2570 | return false; |
| 2571 | } |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 2572 | else if (aggregate->getOp() == EOpDeclaration) |
| 2573 | { |
| 2574 | // Declaring multiple comma-separated variables must be considered multiple statements |
| 2575 | // because each individual declaration has side effects which are visible in the next. |
| 2576 | return false; |
| 2577 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2578 | else |
| 2579 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2580 | for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++) |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2581 | { |
| 2582 | if (!isSingleStatement(*sit)) |
| 2583 | { |
| 2584 | return false; |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | return true; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | return true; |
| 2593 | } |
| 2594 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2595 | // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them |
| 2596 | // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254). |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2597 | bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node) |
| 2598 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2599 | const int MAX_LOOP_ITERATIONS = 254; |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2600 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2601 | |
| 2602 | // Parse loops of the form: |
| 2603 | // for(int index = initial; index [comparator] limit; index += increment) |
| 2604 | TIntermSymbol *index = NULL; |
| 2605 | TOperator comparator = EOpNull; |
| 2606 | int initial = 0; |
| 2607 | int limit = 0; |
| 2608 | int increment = 0; |
| 2609 | |
| 2610 | // Parse index name and intial value |
| 2611 | if (node->getInit()) |
| 2612 | { |
| 2613 | TIntermAggregate *init = node->getInit()->getAsAggregate(); |
| 2614 | |
| 2615 | if (init) |
| 2616 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2617 | TIntermSequence *sequence = init->getSequence(); |
| 2618 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2619 | |
| 2620 | if (variable && variable->getQualifier() == EvqTemporary) |
| 2621 | { |
| 2622 | TIntermBinary *assign = variable->getAsBinaryNode(); |
| 2623 | |
| 2624 | if (assign->getOp() == EOpInitialize) |
| 2625 | { |
| 2626 | TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); |
| 2627 | TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); |
| 2628 | |
| 2629 | if (symbol && constant) |
| 2630 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2631 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2632 | { |
| 2633 | index = symbol; |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2634 | initial = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | // Parse comparator and limit value |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2643 | if (index != NULL && node->getCondition()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2644 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2645 | TIntermBinary *test = node->getCondition()->getAsBinaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2646 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2647 | if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId()) |
| 2648 | { |
| 2649 | TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); |
| 2650 | |
| 2651 | if (constant) |
| 2652 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2653 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2654 | { |
| 2655 | comparator = test->getOp(); |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2656 | limit = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | // Parse increment |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2663 | if (index != NULL && comparator != EOpNull && node->getExpression()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2664 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2665 | TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); |
| 2666 | TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2667 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2668 | if (binaryTerminal) |
| 2669 | { |
| 2670 | TOperator op = binaryTerminal->getOp(); |
| 2671 | TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); |
| 2672 | |
| 2673 | if (constant) |
| 2674 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2675 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2676 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2677 | int value = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2678 | |
| 2679 | switch (op) |
| 2680 | { |
| 2681 | case EOpAddAssign: increment = value; break; |
| 2682 | case EOpSubAssign: increment = -value; break; |
| 2683 | default: UNIMPLEMENTED(); |
| 2684 | } |
| 2685 | } |
| 2686 | } |
| 2687 | } |
| 2688 | else if (unaryTerminal) |
| 2689 | { |
| 2690 | TOperator op = unaryTerminal->getOp(); |
| 2691 | |
| 2692 | switch (op) |
| 2693 | { |
| 2694 | case EOpPostIncrement: increment = 1; break; |
| 2695 | case EOpPostDecrement: increment = -1; break; |
| 2696 | case EOpPreIncrement: increment = 1; break; |
| 2697 | case EOpPreDecrement: increment = -1; break; |
| 2698 | default: UNIMPLEMENTED(); |
| 2699 | } |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | if (index != NULL && comparator != EOpNull && increment != 0) |
| 2704 | { |
| 2705 | if (comparator == EOpLessThanEqual) |
| 2706 | { |
| 2707 | comparator = EOpLessThan; |
| 2708 | limit += 1; |
| 2709 | } |
| 2710 | |
| 2711 | if (comparator == EOpLessThan) |
| 2712 | { |
daniel@transgaming.com | f1f538e | 2011-02-09 16:30:01 +0000 | [diff] [blame] | 2713 | int iterations = (limit - initial) / increment; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2714 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2715 | if (iterations <= MAX_LOOP_ITERATIONS) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2716 | { |
| 2717 | return false; // Not an excessive loop |
| 2718 | } |
| 2719 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2720 | TIntermSymbol *restoreIndex = mExcessiveLoopIndex; |
| 2721 | mExcessiveLoopIndex = index; |
| 2722 | |
daniel@transgaming.com | 0933b0c | 2012-07-11 20:37:28 +0000 | [diff] [blame] | 2723 | out << "{int "; |
| 2724 | index->traverse(this); |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2725 | out << ";\n" |
| 2726 | "bool Break"; |
| 2727 | index->traverse(this); |
| 2728 | out << " = false;\n"; |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2729 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2730 | bool firstLoopFragment = true; |
| 2731 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2732 | while (iterations > 0) |
| 2733 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2734 | int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2735 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2736 | if (!firstLoopFragment) |
| 2737 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2738 | out << "if (!Break"; |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2739 | index->traverse(this); |
| 2740 | out << ") {\n"; |
| 2741 | } |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2742 | |
| 2743 | if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment |
| 2744 | { |
| 2745 | mExcessiveLoopIndex = NULL; // Stops setting the Break flag |
| 2746 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2747 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2748 | // for(int index = initial; index < clampedLimit; index += increment) |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2749 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2750 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2751 | out << unroll << " for("; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2752 | index->traverse(this); |
| 2753 | out << " = "; |
| 2754 | out << initial; |
| 2755 | |
| 2756 | out << "; "; |
| 2757 | index->traverse(this); |
| 2758 | out << " < "; |
| 2759 | out << clampedLimit; |
| 2760 | |
| 2761 | out << "; "; |
| 2762 | index->traverse(this); |
| 2763 | out << " += "; |
| 2764 | out << increment; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2765 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2766 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2767 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2768 | out << "{\n"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2769 | |
| 2770 | if (node->getBody()) |
| 2771 | { |
| 2772 | node->getBody()->traverse(this); |
| 2773 | } |
| 2774 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2775 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2776 | out << ";}\n"; |
| 2777 | |
| 2778 | if (!firstLoopFragment) |
| 2779 | { |
| 2780 | out << "}\n"; |
| 2781 | } |
| 2782 | |
| 2783 | firstLoopFragment = false; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2784 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2785 | initial += MAX_LOOP_ITERATIONS * increment; |
| 2786 | iterations -= MAX_LOOP_ITERATIONS; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2787 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2788 | |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2789 | out << "}"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2790 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2791 | mExcessiveLoopIndex = restoreIndex; |
| 2792 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2793 | return true; |
| 2794 | } |
| 2795 | else UNIMPLEMENTED(); |
| 2796 | } |
| 2797 | |
| 2798 | return false; // Not handled as an excessive loop |
| 2799 | } |
| 2800 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 2801 | void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2802 | { |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2803 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2804 | { |
| 2805 | out << preString; |
| 2806 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2807 | else if (visit == InVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2808 | { |
| 2809 | out << inString; |
| 2810 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2811 | else if (visit == PostVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2812 | { |
| 2813 | out << postString; |
| 2814 | } |
| 2815 | } |
| 2816 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 2817 | void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString) |
| 2818 | { |
| 2819 | outputTriplet(visit, preString, inString, postString, getInfoSink()); |
| 2820 | } |
| 2821 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2822 | void OutputHLSL::outputLineDirective(int line) |
| 2823 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2824 | if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2825 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2826 | TInfoSinkBase &out = getInfoSink(); |
| 2827 | |
| 2828 | out << "\n"; |
| 2829 | out << "#line " << line; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2830 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2831 | if (mSourcePath) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2832 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2833 | out << " \"" << mSourcePath << "\""; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2834 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2835 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2836 | out << "\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2840 | TString OutputHLSL::argumentString(const TIntermSymbol *symbol) |
| 2841 | { |
| 2842 | TQualifier qualifier = symbol->getQualifier(); |
| 2843 | const TType &type = symbol->getType(); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2844 | TString name = symbol->getSymbol(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2845 | |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2846 | if (name.empty()) // HLSL demands named arguments, also for prototypes |
| 2847 | { |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 2848 | name = "x" + str(mUniqueIndex++); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2849 | } |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2850 | else if (!symbol->isInternal()) |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2851 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2852 | name = Decorate(name); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2855 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) |
| 2856 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2857 | return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " + |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2858 | QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2861 | return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | TString OutputHLSL::initializer(const TType &type) |
| 2865 | { |
| 2866 | TString string; |
| 2867 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2868 | size_t size = type.getObjectSize(); |
| 2869 | for (size_t component = 0; component < size; component++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2870 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2871 | string += "0"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2872 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2873 | if (component + 1 < size) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2874 | { |
| 2875 | string += ", "; |
| 2876 | } |
| 2877 | } |
| 2878 | |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2879 | return "{" + string + "}"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2880 | } |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 2881 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2882 | void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters) |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2883 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 2884 | if (type.isArray()) |
| 2885 | { |
| 2886 | UNIMPLEMENTED(); |
| 2887 | } |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2888 | TInfoSinkBase &out = getInfoSink(); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2889 | |
| 2890 | if (visit == PreVisit) |
| 2891 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 2892 | mStructureHLSL->addConstructor(type, name, parameters); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2893 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2894 | out << name << "("; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2895 | } |
| 2896 | else if (visit == InVisit) |
| 2897 | { |
| 2898 | out << ", "; |
| 2899 | } |
| 2900 | else if (visit == PostVisit) |
| 2901 | { |
| 2902 | out << ")"; |
| 2903 | } |
| 2904 | } |
| 2905 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2906 | const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion) |
| 2907 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2908 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2909 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2910 | const TStructure* structure = type.getStruct(); |
| 2911 | if (structure) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2912 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2913 | out << StructNameString(*structure) + "_ctor("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2914 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2915 | const TFieldList& fields = structure->fields(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2916 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2917 | for (size_t i = 0; i < fields.size(); i++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2918 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2919 | const TType *fieldType = fields[i]->type(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2920 | constUnion = writeConstantUnion(*fieldType, constUnion); |
| 2921 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2922 | if (i != fields.size() - 1) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2923 | { |
| 2924 | out << ", "; |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | out << ")"; |
| 2929 | } |
| 2930 | else |
| 2931 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2932 | size_t size = type.getObjectSize(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2933 | bool writeType = size > 1; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2934 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2935 | if (writeType) |
| 2936 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2937 | out << TypeString(type) << "("; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2940 | for (size_t i = 0; i < size; i++, constUnion++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2941 | { |
| 2942 | switch (constUnion->getType()) |
| 2943 | { |
daniel@transgaming.com | 6c1203f | 2013-01-11 04:12:43 +0000 | [diff] [blame] | 2944 | case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2945 | case EbtInt: out << constUnion->getIConst(); break; |
Nicolas Capens | c0f7c61 | 2013-06-05 11:46:09 -0400 | [diff] [blame] | 2946 | case EbtUInt: out << constUnion->getUConst(); break; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 2947 | case EbtBool: out << constUnion->getBConst(); break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2948 | default: UNREACHABLE(); |
| 2949 | } |
| 2950 | |
| 2951 | if (i != size - 1) |
| 2952 | { |
| 2953 | out << ", "; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | if (writeType) |
| 2958 | { |
| 2959 | out << ")"; |
| 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | return constUnion; |
| 2964 | } |
| 2965 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2966 | void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr) |
| 2967 | { |
| 2968 | TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr); |
| 2969 | outputTriplet(visit, preString.c_str(), ", ", ")"); |
| 2970 | } |
| 2971 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 2972 | bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression) |
| 2973 | { |
| 2974 | sh::SearchSymbol searchSymbol(symbolNode->getSymbol()); |
| 2975 | expression->traverse(&searchSymbol); |
| 2976 | |
| 2977 | if (searchSymbol.foundMatch()) |
| 2978 | { |
| 2979 | // Type already printed |
| 2980 | out << "t" + str(mUniqueIndex) + " = "; |
| 2981 | expression->traverse(this); |
| 2982 | out << ", "; |
| 2983 | symbolNode->traverse(this); |
| 2984 | out << " = t" + str(mUniqueIndex); |
| 2985 | |
| 2986 | mUniqueIndex++; |
| 2987 | return true; |
| 2988 | } |
| 2989 | |
| 2990 | return false; |
| 2991 | } |
| 2992 | |
| 2993 | void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out) |
| 2994 | { |
| 2995 | out << "#define ANGLE_USES_DEFERRED_INIT\n" |
| 2996 | << "\n" |
| 2997 | << "void initializeDeferredGlobals()\n" |
| 2998 | << "{\n"; |
| 2999 | |
| 3000 | for (const auto &deferredGlobal : mDeferredGlobalInitializers) |
| 3001 | { |
| 3002 | TIntermSymbol *symbol = deferredGlobal.first; |
| 3003 | TIntermTyped *expression = deferredGlobal.second; |
| 3004 | ASSERT(symbol); |
| 3005 | ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst); |
| 3006 | |
| 3007 | out << " " << Decorate(symbol->getSymbol()) << " = "; |
| 3008 | |
| 3009 | if (!writeSameSymbolInitializer(out, symbol, expression)) |
| 3010 | { |
| 3011 | ASSERT(mInfoSinkStack.top() == &out); |
| 3012 | expression->traverse(this); |
| 3013 | } |
| 3014 | |
| 3015 | out << ";\n"; |
| 3016 | } |
| 3017 | |
| 3018 | out << "}\n" |
| 3019 | << "\n"; |
| 3020 | } |
| 3021 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3022 | TString OutputHLSL::addStructEqualityFunction(const TStructure &structure) |
| 3023 | { |
| 3024 | const TFieldList &fields = structure.fields(); |
| 3025 | |
| 3026 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 3027 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3028 | if (eqFunction->structure == &structure) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3029 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3030 | return eqFunction->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | const TString &structNameString = StructNameString(structure); |
| 3035 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3036 | StructEqualityFunction *function = new StructEqualityFunction(); |
| 3037 | function->structure = &structure; |
| 3038 | function->functionName = "angle_eq_" + structNameString; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3039 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3040 | TInfoSinkBase fnOut; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3041 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3042 | fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n" |
| 3043 | << "{\n" |
| 3044 | " return "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3045 | |
| 3046 | for (size_t i = 0; i < fields.size(); i++) |
| 3047 | { |
| 3048 | const TField *field = fields[i]; |
| 3049 | const TType *fieldType = field->type(); |
| 3050 | |
| 3051 | const TString &fieldNameA = "a." + Decorate(field->name()); |
| 3052 | const TString &fieldNameB = "b." + Decorate(field->name()); |
| 3053 | |
| 3054 | if (i > 0) |
| 3055 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3056 | fnOut << " && "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3057 | } |
| 3058 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3059 | fnOut << "("; |
| 3060 | outputEqual(PreVisit, *fieldType, EOpEqual, fnOut); |
| 3061 | fnOut << fieldNameA; |
| 3062 | outputEqual(InVisit, *fieldType, EOpEqual, fnOut); |
| 3063 | fnOut << fieldNameB; |
| 3064 | outputEqual(PostVisit, *fieldType, EOpEqual, fnOut); |
| 3065 | fnOut << ")"; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3068 | fnOut << ";\n" << "}\n"; |
| 3069 | |
| 3070 | function->functionDefinition = fnOut.c_str(); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3071 | |
| 3072 | mStructEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3073 | mEqualityFunctions.push_back(function); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3074 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3075 | return function->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3076 | } |
| 3077 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3078 | TString OutputHLSL::addArrayEqualityFunction(const TType& type) |
| 3079 | { |
| 3080 | for (const auto &eqFunction : mArrayEqualityFunctions) |
| 3081 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3082 | if (eqFunction->type == type) |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3083 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3084 | return eqFunction->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | const TString &typeName = TypeString(type); |
| 3089 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3090 | ArrayHelperFunction *function = new ArrayHelperFunction(); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3091 | function->type = type; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3092 | |
| 3093 | TInfoSinkBase fnNameOut; |
| 3094 | fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3095 | function->functionName = fnNameOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3096 | |
| 3097 | TType nonArrayType = type; |
| 3098 | nonArrayType.clearArrayness(); |
| 3099 | |
| 3100 | TInfoSinkBase fnOut; |
| 3101 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3102 | fnOut << "bool " << function->functionName << "(" |
Olli Etuaho | fc7cfd1 | 2015-03-31 14:46:18 +0300 | [diff] [blame] | 3103 | << typeName << " a[" << type.getArraySize() << "], " |
| 3104 | << typeName << " b[" << type.getArraySize() << "])\n" |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3105 | << "{\n" |
| 3106 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3107 | " {\n" |
| 3108 | " if ("; |
| 3109 | |
| 3110 | outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3111 | fnOut << "a[i]"; |
| 3112 | outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3113 | fnOut << "b[i]"; |
| 3114 | outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3115 | |
| 3116 | fnOut << ") { return false; }\n" |
| 3117 | " }\n" |
| 3118 | " return true;\n" |
| 3119 | "}\n"; |
| 3120 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3121 | function->functionDefinition = fnOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3122 | |
| 3123 | mArrayEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3124 | mEqualityFunctions.push_back(function); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3125 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3126 | return function->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3127 | } |
| 3128 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3129 | TString OutputHLSL::addArrayAssignmentFunction(const TType& type) |
| 3130 | { |
| 3131 | for (const auto &assignFunction : mArrayAssignmentFunctions) |
| 3132 | { |
| 3133 | if (assignFunction.type == type) |
| 3134 | { |
| 3135 | return assignFunction.functionName; |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | const TString &typeName = TypeString(type); |
| 3140 | |
| 3141 | ArrayHelperFunction function; |
| 3142 | function.type = type; |
| 3143 | |
| 3144 | TInfoSinkBase fnNameOut; |
| 3145 | fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName; |
| 3146 | function.functionName = fnNameOut.c_str(); |
| 3147 | |
| 3148 | TInfoSinkBase fnOut; |
| 3149 | |
| 3150 | fnOut << "void " << function.functionName << "(out " |
| 3151 | << typeName << " a[" << type.getArraySize() << "], " |
| 3152 | << typeName << " b[" << type.getArraySize() << "])\n" |
| 3153 | << "{\n" |
| 3154 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3155 | " {\n" |
| 3156 | " a[i] = b[i];\n" |
| 3157 | " }\n" |
| 3158 | "}\n"; |
| 3159 | |
| 3160 | function.functionDefinition = fnOut.c_str(); |
| 3161 | |
| 3162 | mArrayAssignmentFunctions.push_back(function); |
| 3163 | |
| 3164 | return function.functionName; |
| 3165 | } |
| 3166 | |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3167 | TString OutputHLSL::addArrayConstructIntoFunction(const TType& type) |
| 3168 | { |
| 3169 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 3170 | { |
| 3171 | if (constructIntoFunction.type == type) |
| 3172 | { |
| 3173 | return constructIntoFunction.functionName; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | const TString &typeName = TypeString(type); |
| 3178 | |
| 3179 | ArrayHelperFunction function; |
| 3180 | function.type = type; |
| 3181 | |
| 3182 | TInfoSinkBase fnNameOut; |
| 3183 | fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName; |
| 3184 | function.functionName = fnNameOut.c_str(); |
| 3185 | |
| 3186 | TInfoSinkBase fnOut; |
| 3187 | |
| 3188 | fnOut << "void " << function.functionName << "(out " |
| 3189 | << typeName << " a[" << type.getArraySize() << "]"; |
| 3190 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3191 | { |
| 3192 | fnOut << ", " << typeName << " b" << i; |
| 3193 | } |
| 3194 | fnOut << ")\n" |
| 3195 | "{\n"; |
| 3196 | |
| 3197 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3198 | { |
| 3199 | fnOut << " a[" << i << "] = b" << i << ";\n"; |
| 3200 | } |
| 3201 | fnOut << "}\n"; |
| 3202 | |
| 3203 | function.functionDefinition = fnOut.c_str(); |
| 3204 | |
| 3205 | mArrayConstructIntoFunctions.push_back(function); |
| 3206 | |
| 3207 | return function.functionName; |
| 3208 | } |
| 3209 | |
| 3210 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3211 | } |