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 | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 1324 | const TType &nodeType = node->getType(); |
| 1325 | const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 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 | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 1336 | ensureStructDefined(nodeType); |
| 1337 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1338 | out << DecorateUniform(name, nodeType); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1339 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1340 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1341 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1342 | mReferencedAttributes[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1343 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1344 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1345 | else if (IsVarying(qualifier)) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1346 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1347 | mReferencedVaryings[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1348 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1349 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1350 | else if (qualifier == EvqFragmentOut) |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1351 | { |
| 1352 | mReferencedOutputVariables[name] = node; |
| 1353 | out << "out_" << name; |
| 1354 | } |
| 1355 | else if (qualifier == EvqFragColor) |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1356 | { |
| 1357 | out << "gl_Color[0]"; |
| 1358 | mUsesFragColor = true; |
| 1359 | } |
| 1360 | else if (qualifier == EvqFragData) |
| 1361 | { |
| 1362 | out << "gl_Color"; |
| 1363 | mUsesFragData = true; |
| 1364 | } |
| 1365 | else if (qualifier == EvqFragCoord) |
| 1366 | { |
| 1367 | mUsesFragCoord = true; |
| 1368 | out << name; |
| 1369 | } |
| 1370 | else if (qualifier == EvqPointCoord) |
| 1371 | { |
| 1372 | mUsesPointCoord = true; |
| 1373 | out << name; |
| 1374 | } |
| 1375 | else if (qualifier == EvqFrontFacing) |
| 1376 | { |
| 1377 | mUsesFrontFacing = true; |
| 1378 | out << name; |
| 1379 | } |
| 1380 | else if (qualifier == EvqPointSize) |
| 1381 | { |
| 1382 | mUsesPointSize = true; |
| 1383 | out << name; |
| 1384 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 1385 | else if (qualifier == EvqInstanceID) |
| 1386 | { |
| 1387 | mUsesInstanceID = true; |
| 1388 | out << name; |
| 1389 | } |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1390 | else if (name == "gl_FragDepthEXT") |
| 1391 | { |
| 1392 | mUsesFragDepth = true; |
| 1393 | out << "gl_Depth"; |
| 1394 | } |
Olli Etuaho | 78174db | 2015-04-21 16:14:00 +0300 | [diff] [blame] | 1395 | else if (node->isInternal()) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 1396 | { |
| 1397 | out << name; |
| 1398 | } |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1399 | else |
| 1400 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1401 | out << Decorate(name); |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1402 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1406 | void OutputHLSL::visitRaw(TIntermRaw *node) |
| 1407 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1408 | getInfoSink() << node->getRawText(); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1409 | } |
| 1410 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1411 | void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out) |
| 1412 | { |
| 1413 | if (type.isScalar() && !type.isArray()) |
| 1414 | { |
| 1415 | if (op == EOpEqual) |
| 1416 | { |
| 1417 | outputTriplet(visit, "(", " == ", ")", out); |
| 1418 | } |
| 1419 | else |
| 1420 | { |
| 1421 | outputTriplet(visit, "(", " != ", ")", out); |
| 1422 | } |
| 1423 | } |
| 1424 | else |
| 1425 | { |
| 1426 | if (visit == PreVisit && op == EOpNotEqual) |
| 1427 | { |
| 1428 | out << "!"; |
| 1429 | } |
| 1430 | |
| 1431 | if (type.isArray()) |
| 1432 | { |
| 1433 | const TString &functionName = addArrayEqualityFunction(type); |
| 1434 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out); |
| 1435 | } |
| 1436 | else if (type.getBasicType() == EbtStruct) |
| 1437 | { |
| 1438 | const TStructure &structure = *type.getStruct(); |
| 1439 | const TString &functionName = addStructEqualityFunction(structure); |
| 1440 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out); |
| 1441 | } |
| 1442 | else |
| 1443 | { |
| 1444 | ASSERT(type.isMatrix() || type.isVector()); |
| 1445 | outputTriplet(visit, "all(", " == ", ")", out); |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1450 | bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) |
| 1451 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1452 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1453 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1454 | // Handle accessing std140 structs by value |
| 1455 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1456 | { |
| 1457 | out << mFlaggedStructMappedNames[node]; |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1461 | switch (node->getOp()) |
| 1462 | { |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1463 | case EOpAssign: |
| 1464 | if (node->getLeft()->isArray()) |
| 1465 | { |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1466 | TIntermAggregate *rightAgg = node->getRight()->getAsAggregate(); |
| 1467 | if (rightAgg != nullptr && rightAgg->isConstructor()) |
| 1468 | { |
| 1469 | const TString &functionName = addArrayConstructIntoFunction(node->getType()); |
| 1470 | out << functionName << "("; |
| 1471 | node->getLeft()->traverse(this); |
| 1472 | TIntermSequence *seq = rightAgg->getSequence(); |
| 1473 | for (auto &arrayElement : *seq) |
| 1474 | { |
| 1475 | out << ", "; |
| 1476 | arrayElement->traverse(this); |
| 1477 | } |
| 1478 | out << ")"; |
| 1479 | return false; |
| 1480 | } |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 1481 | // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned. |
| 1482 | ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall); |
| 1483 | |
| 1484 | const TString &functionName = addArrayAssignmentFunction(node->getType()); |
| 1485 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | outputTriplet(visit, "(", " = ", ")"); |
| 1490 | } |
| 1491 | break; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1492 | case EOpInitialize: |
| 1493 | if (visit == PreVisit) |
| 1494 | { |
| 1495 | // GLSL allows to write things like "float x = x;" where a new variable x is defined |
| 1496 | // and the value of an existing variable x is assigned. HLSL uses C semantics (the |
| 1497 | // new variable is created before the assignment is evaluated), so we need to convert |
| 1498 | // this to "float t = x, x = t;". |
| 1499 | |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1500 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1501 | ASSERT(symbolNode); |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1502 | TIntermTyped *expression = node->getRight(); |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1503 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1504 | // TODO (jmadill): do a 'deep' scan to know if an expression is statically const |
| 1505 | if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst) |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1506 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1507 | // For variables which are not constant, defer their real initialization until |
| 1508 | // after we initialize other globals: uniforms, attributes and varyings. |
| 1509 | mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression)); |
| 1510 | const TString &initString = initializer(node->getType()); |
| 1511 | node->setRight(new TIntermRaw(node->getType(), initString)); |
| 1512 | } |
| 1513 | else if (writeSameSymbolInitializer(out, symbolNode, expression)) |
| 1514 | { |
| 1515 | // Skip initializing the rest of the expression |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | } |
| 1519 | else if (visit == InVisit) |
| 1520 | { |
| 1521 | out << " = "; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1522 | } |
| 1523 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1524 | case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break; |
| 1525 | case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break; |
| 1526 | case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1527 | case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1528 | case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1529 | case EOpVectorTimesMatrixAssign: |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1530 | if (visit == PreVisit) |
| 1531 | { |
| 1532 | out << "("; |
| 1533 | } |
| 1534 | else if (visit == InVisit) |
| 1535 | { |
| 1536 | out << " = mul("; |
| 1537 | node->getLeft()->traverse(this); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1538 | out << ", transpose("; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1539 | } |
| 1540 | else |
| 1541 | { |
daniel@transgaming.com | 3aa7420 | 2010-04-29 03:39:04 +0000 | [diff] [blame] | 1542 | out << ")))"; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1543 | } |
| 1544 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1545 | case EOpMatrixTimesMatrixAssign: |
| 1546 | if (visit == PreVisit) |
| 1547 | { |
| 1548 | out << "("; |
| 1549 | } |
| 1550 | else if (visit == InVisit) |
| 1551 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1552 | out << " = transpose(mul(transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1553 | node->getLeft()->traverse(this); |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1554 | out << "), transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1555 | } |
| 1556 | else |
| 1557 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1558 | out << "))))"; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1559 | } |
| 1560 | break; |
| 1561 | case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1562 | case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1563 | case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break; |
| 1564 | case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break; |
| 1565 | case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break; |
| 1566 | case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break; |
| 1567 | case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break; |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1568 | case EOpIndexDirect: |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1569 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1570 | const TType& leftType = node->getLeft()->getType(); |
| 1571 | if (leftType.isInterfaceBlock()) |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1572 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1573 | if (visit == PreVisit) |
| 1574 | { |
| 1575 | TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock(); |
| 1576 | const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1577 | mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1578 | out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1581 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1582 | else |
| 1583 | { |
| 1584 | outputTriplet(visit, "", "[", "]"); |
| 1585 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1586 | } |
| 1587 | break; |
| 1588 | case EOpIndexIndirect: |
| 1589 | // We do not currently support indirect references to interface blocks |
| 1590 | ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock); |
| 1591 | outputTriplet(visit, "", "[", "]"); |
| 1592 | break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1593 | case EOpIndexDirectStruct: |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1594 | if (visit == InVisit) |
| 1595 | { |
| 1596 | const TStructure* structure = node->getLeft()->getType().getStruct(); |
| 1597 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1598 | const TField* field = structure->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1599 | out << "." + DecorateField(field->name(), *structure); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1600 | |
| 1601 | return false; |
| 1602 | } |
| 1603 | break; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1604 | case EOpIndexDirectInterfaceBlock: |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1605 | if (visit == InVisit) |
| 1606 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1607 | const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock(); |
| 1608 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1609 | const TField* field = interfaceBlock->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1610 | out << "." + Decorate(field->name()); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1611 | |
| 1612 | return false; |
| 1613 | } |
| 1614 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1615 | case EOpVectorSwizzle: |
| 1616 | if (visit == InVisit) |
| 1617 | { |
| 1618 | out << "."; |
| 1619 | |
| 1620 | TIntermAggregate *swizzle = node->getRight()->getAsAggregate(); |
| 1621 | |
| 1622 | if (swizzle) |
| 1623 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1624 | TIntermSequence *sequence = swizzle->getSequence(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1625 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1626 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1627 | { |
| 1628 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 1629 | |
| 1630 | if (element) |
| 1631 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 1632 | int i = element->getIConst(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1633 | |
| 1634 | switch (i) |
| 1635 | { |
| 1636 | case 0: out << "x"; break; |
| 1637 | case 1: out << "y"; break; |
| 1638 | case 2: out << "z"; break; |
| 1639 | case 3: out << "w"; break; |
| 1640 | default: UNREACHABLE(); |
| 1641 | } |
| 1642 | } |
| 1643 | else UNREACHABLE(); |
| 1644 | } |
| 1645 | } |
| 1646 | else UNREACHABLE(); |
| 1647 | |
| 1648 | return false; // Fully processed |
| 1649 | } |
| 1650 | break; |
| 1651 | case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break; |
| 1652 | case EOpSub: outputTriplet(visit, "(", " - ", ")"); break; |
| 1653 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
| 1654 | case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1655 | case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1656 | case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break; |
| 1657 | case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break; |
| 1658 | case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break; |
| 1659 | case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break; |
| 1660 | case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break; |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1661 | case EOpEqual: |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1662 | case EOpNotEqual: |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1663 | outputEqual(visit, node->getLeft()->getType(), node->getOp(), out); |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1664 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1665 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 1666 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 1667 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 1668 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 1669 | case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1670 | case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1671 | case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break; |
| 1672 | case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break; |
daniel@transgaming.com | 69f084b | 2010-04-23 18:34:46 +0000 | [diff] [blame] | 1673 | case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1674 | case EOpLogicalOr: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1675 | if (node->getRight()->hasSideEffects()) |
| 1676 | { |
| 1677 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1678 | return false; |
| 1679 | } |
| 1680 | else |
| 1681 | { |
| 1682 | outputTriplet(visit, "(", " || ", ")"); |
| 1683 | return true; |
| 1684 | } |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1685 | case EOpLogicalXor: |
| 1686 | mUsesXor = true; |
| 1687 | outputTriplet(visit, "xor(", ", ", ")"); |
| 1688 | break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1689 | case EOpLogicalAnd: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1690 | if (node->getRight()->hasSideEffects()) |
| 1691 | { |
| 1692 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1693 | return false; |
| 1694 | } |
| 1695 | else |
| 1696 | { |
| 1697 | outputTriplet(visit, "(", " && ", ")"); |
| 1698 | return true; |
| 1699 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1700 | default: UNREACHABLE(); |
| 1701 | } |
| 1702 | |
| 1703 | return true; |
| 1704 | } |
| 1705 | |
| 1706 | bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) |
| 1707 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1708 | switch (node->getOp()) |
| 1709 | { |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1710 | case EOpNegative: outputTriplet(visit, "(-", "", ")"); break; |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 1711 | case EOpPositive: outputTriplet(visit, "(+", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1712 | case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
| 1713 | case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1714 | case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1715 | case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break; |
| 1716 | case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break; |
| 1717 | case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break; |
| 1718 | case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1719 | case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break; |
| 1720 | case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break; |
| 1721 | case EOpSin: outputTriplet(visit, "sin(", "", ")"); break; |
| 1722 | case EOpCos: outputTriplet(visit, "cos(", "", ")"); break; |
| 1723 | case EOpTan: outputTriplet(visit, "tan(", "", ")"); break; |
| 1724 | case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break; |
| 1725 | case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break; |
| 1726 | case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break; |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 1727 | case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break; |
| 1728 | case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break; |
| 1729 | case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break; |
| 1730 | case EOpAsinh: |
| 1731 | ASSERT(node->getUseEmulatedFunction()); |
| 1732 | writeEmulatedFunctionTriplet(visit, "asinh("); |
| 1733 | break; |
| 1734 | case EOpAcosh: |
| 1735 | ASSERT(node->getUseEmulatedFunction()); |
| 1736 | writeEmulatedFunctionTriplet(visit, "acosh("); |
| 1737 | break; |
| 1738 | case EOpAtanh: |
| 1739 | ASSERT(node->getUseEmulatedFunction()); |
| 1740 | writeEmulatedFunctionTriplet(visit, "atanh("); |
| 1741 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1742 | case EOpExp: outputTriplet(visit, "exp(", "", ")"); break; |
| 1743 | case EOpLog: outputTriplet(visit, "log(", "", ")"); break; |
| 1744 | case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break; |
| 1745 | case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break; |
| 1746 | case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break; |
| 1747 | case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break; |
| 1748 | case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break; |
| 1749 | case EOpSign: outputTriplet(visit, "sign(", "", ")"); break; |
| 1750 | case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break; |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame] | 1751 | case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break; |
| 1752 | case EOpRound: outputTriplet(visit, "round(", "", ")"); break; |
| 1753 | case EOpRoundEven: |
| 1754 | ASSERT(node->getUseEmulatedFunction()); |
| 1755 | writeEmulatedFunctionTriplet(visit, "roundEven("); |
| 1756 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1757 | case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break; |
| 1758 | case EOpFract: outputTriplet(visit, "frac(", "", ")"); break; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 1759 | case EOpIsNan: |
| 1760 | outputTriplet(visit, "isnan(", "", ")"); |
| 1761 | mRequiresIEEEStrictCompiling = true; |
| 1762 | break; |
Arun Patole | 0c1726e | 2015-02-18 14:35:02 +0530 | [diff] [blame] | 1763 | case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break; |
Olli Etuaho | e8d2c07 | 2015-01-08 16:33:54 +0200 | [diff] [blame] | 1764 | case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break; |
| 1765 | case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break; |
| 1766 | case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
| 1767 | case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 1768 | case EOpPackSnorm2x16: |
| 1769 | ASSERT(node->getUseEmulatedFunction()); |
| 1770 | writeEmulatedFunctionTriplet(visit, "packSnorm2x16("); |
| 1771 | break; |
| 1772 | case EOpPackUnorm2x16: |
| 1773 | ASSERT(node->getUseEmulatedFunction()); |
| 1774 | writeEmulatedFunctionTriplet(visit, "packUnorm2x16("); |
| 1775 | break; |
| 1776 | case EOpPackHalf2x16: |
| 1777 | ASSERT(node->getUseEmulatedFunction()); |
| 1778 | writeEmulatedFunctionTriplet(visit, "packHalf2x16("); |
| 1779 | break; |
| 1780 | case EOpUnpackSnorm2x16: |
| 1781 | ASSERT(node->getUseEmulatedFunction()); |
| 1782 | writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16("); |
| 1783 | break; |
| 1784 | case EOpUnpackUnorm2x16: |
| 1785 | ASSERT(node->getUseEmulatedFunction()); |
| 1786 | writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16("); |
| 1787 | break; |
| 1788 | case EOpUnpackHalf2x16: |
| 1789 | ASSERT(node->getUseEmulatedFunction()); |
| 1790 | writeEmulatedFunctionTriplet(visit, "unpackHalf2x16("); |
| 1791 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1792 | case EOpLength: outputTriplet(visit, "length(", "", ")"); break; |
| 1793 | case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break; |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1794 | case EOpDFdx: |
| 1795 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1796 | { |
| 1797 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1798 | } |
| 1799 | else |
| 1800 | { |
| 1801 | outputTriplet(visit, "ddx(", "", ")"); |
| 1802 | } |
| 1803 | break; |
| 1804 | case EOpDFdy: |
| 1805 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1806 | { |
| 1807 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1808 | } |
| 1809 | else |
| 1810 | { |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1811 | outputTriplet(visit, "ddy(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1812 | } |
| 1813 | break; |
| 1814 | case EOpFwidth: |
| 1815 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1816 | { |
| 1817 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1818 | } |
| 1819 | else |
| 1820 | { |
| 1821 | outputTriplet(visit, "fwidth(", "", ")"); |
| 1822 | } |
| 1823 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1824 | case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break; |
| 1825 | case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break; |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 1826 | case EOpInverse: |
| 1827 | ASSERT(node->getUseEmulatedFunction()); |
| 1828 | writeEmulatedFunctionTriplet(visit, "inverse("); |
| 1829 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1830 | |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1831 | case EOpAny: outputTriplet(visit, "any(", "", ")"); break; |
| 1832 | case EOpAll: outputTriplet(visit, "all(", "", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1833 | default: UNREACHABLE(); |
| 1834 | } |
| 1835 | |
| 1836 | return true; |
| 1837 | } |
| 1838 | |
| 1839 | bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) |
| 1840 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1841 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1842 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1843 | switch (node->getOp()) |
| 1844 | { |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1845 | case EOpSequence: |
| 1846 | { |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1847 | if (mInsideFunction) |
| 1848 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1849 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1850 | out << "{\n"; |
| 1851 | } |
| 1852 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1853 | 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] | 1854 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1855 | outputLineDirective((*sit)->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 1856 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 1857 | traverseStatements(*sit); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1858 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 1859 | // Don't output ; after case labels, they're terminated by : |
| 1860 | // This is needed especially since outputting a ; after a case statement would turn empty |
| 1861 | // case statements into non-empty case statements, disallowing fall-through from them. |
| 1862 | if ((*sit)->getAsCaseNode() == nullptr) |
| 1863 | out << ";\n"; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1866 | if (mInsideFunction) |
| 1867 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1868 | outputLineDirective(node->getLine().last_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1869 | out << "}\n"; |
| 1870 | } |
| 1871 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1872 | return false; |
| 1873 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1874 | case EOpDeclaration: |
| 1875 | if (visit == PreVisit) |
| 1876 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1877 | TIntermSequence *sequence = node->getSequence(); |
| 1878 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1879 | |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 1880 | if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1881 | { |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 1882 | ensureStructDefined(variable->getType()); |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1883 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1884 | if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1885 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1886 | for (const auto &seqElement : *sequence) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1887 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1888 | if (isSingleStatement(seqElement)) |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1889 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1890 | mUnfoldShortCircuit->traverse(seqElement); |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1891 | } |
| 1892 | |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1893 | if (!mInsideFunction) |
| 1894 | { |
| 1895 | out << "static "; |
| 1896 | } |
| 1897 | |
| 1898 | out << TypeString(variable->getType()) + " "; |
| 1899 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1900 | TIntermSymbol *symbol = seqElement->getAsSymbolNode(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1901 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1902 | if (symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1903 | { |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1904 | symbol->traverse(this); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1905 | out << ArrayString(symbol->getType()); |
Jamie Madill | 79bb0d9 | 2013-12-09 16:20:28 -0500 | [diff] [blame] | 1906 | out << " = " + initializer(symbol->getType()); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1907 | } |
| 1908 | else |
| 1909 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1910 | seqElement->traverse(this); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1913 | if (seqElement != sequence->back()) |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1914 | { |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1915 | out << ";\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1918 | } |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1919 | else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration |
| 1920 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1921 | // Already added to constructor map |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1922 | } |
| 1923 | else UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1924 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1925 | else if (variable && IsVaryingOut(variable->getQualifier())) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1926 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1927 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1928 | { |
| 1929 | TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); |
| 1930 | |
| 1931 | if (symbol) |
| 1932 | { |
| 1933 | // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking |
| 1934 | mReferencedVaryings[symbol->getSymbol()] = symbol; |
| 1935 | } |
| 1936 | else |
| 1937 | { |
| 1938 | (*sit)->traverse(this); |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1943 | return false; |
| 1944 | } |
| 1945 | else if (visit == InVisit) |
| 1946 | { |
| 1947 | out << ", "; |
| 1948 | } |
| 1949 | break; |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 1950 | case EOpInvariantDeclaration: |
| 1951 | // Do not do any translation |
| 1952 | return false; |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1953 | case EOpPrototype: |
| 1954 | if (visit == PreVisit) |
| 1955 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 1956 | size_t index = mCallDag.findIndex(node); |
| 1957 | // Skip the prototype if it is not implemented (and thus not used) |
| 1958 | if (index == CallDAG::InvalidIndex) |
| 1959 | { |
| 1960 | return false; |
| 1961 | } |
| 1962 | |
Olli Etuaho | 76acee8 | 2014-11-04 13:44:03 +0200 | [diff] [blame] | 1963 | out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1964 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1965 | TIntermSequence *arguments = node->getSequence(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1966 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1967 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1968 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1969 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1970 | |
| 1971 | if (symbol) |
| 1972 | { |
| 1973 | out << argumentString(symbol); |
| 1974 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1975 | if (i < arguments->size() - 1) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1976 | { |
| 1977 | out << ", "; |
| 1978 | } |
| 1979 | } |
| 1980 | else UNREACHABLE(); |
| 1981 | } |
| 1982 | |
| 1983 | out << ");\n"; |
| 1984 | |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 1985 | // Also prototype the Lod0 variant if needed |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 1986 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 1987 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 1988 | { |
| 1989 | mOutputLod0Function = true; |
| 1990 | node->traverse(this); |
| 1991 | mOutputLod0Function = false; |
| 1992 | } |
| 1993 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1994 | return false; |
| 1995 | } |
| 1996 | break; |
daniel@transgaming.com | ed2180d | 2012-03-26 17:08:54 +0000 | [diff] [blame] | 1997 | case EOpComma: outputTriplet(visit, "(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1998 | case EOpFunction: |
| 1999 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2000 | ASSERT(mCurrentFunctionMetadata == nullptr); |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 2001 | TString name = TFunction::unmangleName(node->getName()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2002 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2003 | size_t index = mCallDag.findIndex(node); |
| 2004 | ASSERT(index != CallDAG::InvalidIndex); |
| 2005 | mCurrentFunctionMetadata = &mASTMetadataList[index]; |
| 2006 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2007 | out << TypeString(node->getType()) << " "; |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (name == "main") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2010 | { |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2011 | out << "gl_main("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2012 | } |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2013 | else |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2014 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2015 | out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2016 | } |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 2017 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2018 | TIntermSequence *sequence = node->getSequence(); |
| 2019 | TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2020 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2021 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2022 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2023 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (symbol) |
| 2026 | { |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 2027 | ensureStructDefined(symbol->getType()); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2028 | |
| 2029 | out << argumentString(symbol); |
| 2030 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2031 | if (i < arguments->size() - 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2032 | { |
| 2033 | out << ", "; |
| 2034 | } |
| 2035 | } |
| 2036 | else UNREACHABLE(); |
| 2037 | } |
| 2038 | |
| 2039 | out << ")\n" |
| 2040 | "{\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2041 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2042 | if (sequence->size() > 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2043 | { |
| 2044 | mInsideFunction = true; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2045 | (*sequence)[1]->traverse(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2046 | mInsideFunction = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2047 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2048 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2049 | out << "}\n"; |
| 2050 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2051 | mCurrentFunctionMetadata = nullptr; |
| 2052 | |
| 2053 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2054 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2055 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2056 | ASSERT(name != "main"); |
| 2057 | mOutputLod0Function = true; |
| 2058 | node->traverse(this); |
| 2059 | mOutputLod0Function = false; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2062 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2063 | } |
| 2064 | break; |
| 2065 | case EOpFunctionCall: |
| 2066 | { |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2067 | TString name = TFunction::unmangleName(node->getName()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2068 | TIntermSequence *arguments = node->getSequence(); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2069 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2070 | bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2071 | if (node->isUserDefined()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2072 | { |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2073 | if (node->isArray()) |
| 2074 | { |
| 2075 | UNIMPLEMENTED(); |
| 2076 | } |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2077 | size_t index = mCallDag.findIndex(node); |
| 2078 | ASSERT(index != CallDAG::InvalidIndex); |
| 2079 | lod0 &= mASTMetadataList[index].mNeedsLod0; |
| 2080 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2081 | out << Decorate(name) << (lod0 ? "Lod0(" : "("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2082 | } |
| 2083 | else |
| 2084 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2085 | TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType(); |
shannonwoods@chromium.org | c6ac65f | 2013-05-30 00:02:50 +0000 | [diff] [blame] | 2086 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2087 | TextureFunction textureFunction; |
| 2088 | textureFunction.sampler = samplerType; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2089 | textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize(); |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2090 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2091 | textureFunction.proj = false; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2092 | textureFunction.offset = false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2093 | |
| 2094 | if (name == "texture2D" || name == "textureCube" || name == "texture") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2095 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2096 | textureFunction.method = TextureFunction::IMPLICIT; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2097 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2098 | else if (name == "texture2DProj" || name == "textureProj") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2099 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2100 | textureFunction.method = TextureFunction::IMPLICIT; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2101 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2102 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2103 | else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" || |
| 2104 | name == "texture2DLodEXT" || name == "textureCubeLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2105 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2106 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2107 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2108 | else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2109 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2110 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2111 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2112 | } |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2113 | else if (name == "textureSize") |
| 2114 | { |
| 2115 | textureFunction.method = TextureFunction::SIZE; |
| 2116 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2117 | else if (name == "textureOffset") |
| 2118 | { |
| 2119 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2120 | textureFunction.offset = true; |
| 2121 | } |
Nicolas Capens | df86c6b | 2014-02-14 20:09:17 -0500 | [diff] [blame] | 2122 | else if (name == "textureProjOffset") |
| 2123 | { |
| 2124 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2125 | textureFunction.offset = true; |
| 2126 | textureFunction.proj = true; |
| 2127 | } |
| 2128 | else if (name == "textureLodOffset") |
| 2129 | { |
| 2130 | textureFunction.method = TextureFunction::LOD; |
| 2131 | textureFunction.offset = true; |
| 2132 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 2133 | else if (name == "textureProjLodOffset") |
| 2134 | { |
| 2135 | textureFunction.method = TextureFunction::LOD; |
| 2136 | textureFunction.proj = true; |
| 2137 | textureFunction.offset = true; |
| 2138 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 2139 | else if (name == "texelFetch") |
| 2140 | { |
| 2141 | textureFunction.method = TextureFunction::FETCH; |
| 2142 | } |
| 2143 | else if (name == "texelFetchOffset") |
| 2144 | { |
| 2145 | textureFunction.method = TextureFunction::FETCH; |
| 2146 | textureFunction.offset = true; |
| 2147 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2148 | else if (name == "textureGrad" || name == "texture2DGradEXT") |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 2149 | { |
| 2150 | textureFunction.method = TextureFunction::GRAD; |
| 2151 | } |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 2152 | else if (name == "textureGradOffset") |
| 2153 | { |
| 2154 | textureFunction.method = TextureFunction::GRAD; |
| 2155 | textureFunction.offset = true; |
| 2156 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2157 | else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT") |
Nicolas Capens | f7378e3 | 2014-02-19 17:29:32 -0500 | [diff] [blame] | 2158 | { |
| 2159 | textureFunction.method = TextureFunction::GRAD; |
| 2160 | textureFunction.proj = true; |
| 2161 | } |
| 2162 | else if (name == "textureProjGradOffset") |
| 2163 | { |
| 2164 | textureFunction.method = TextureFunction::GRAD; |
| 2165 | textureFunction.proj = true; |
| 2166 | textureFunction.offset = true; |
| 2167 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2168 | else UNREACHABLE(); |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2169 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2170 | 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] | 2171 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2172 | unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments |
| 2173 | |
| 2174 | if (textureFunction.offset) |
| 2175 | { |
| 2176 | mandatoryArgumentCount++; |
| 2177 | } |
| 2178 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2179 | bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2180 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2181 | if (lod0 || mShaderType == GL_VERTEX_SHADER) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2182 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2183 | if (bias) |
| 2184 | { |
| 2185 | textureFunction.method = TextureFunction::LOD0BIAS; |
| 2186 | } |
| 2187 | else |
| 2188 | { |
| 2189 | textureFunction.method = TextureFunction::LOD0; |
| 2190 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2191 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2192 | else if (bias) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2193 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2194 | textureFunction.method = TextureFunction::BIAS; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | mUsesTexture.insert(textureFunction); |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2199 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2200 | out << textureFunction.name(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2201 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2202 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2203 | for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2204 | { |
| 2205 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType())) |
| 2206 | { |
| 2207 | out << "texture_"; |
| 2208 | (*arg)->traverse(this); |
| 2209 | out << ", sampler_"; |
| 2210 | } |
| 2211 | |
| 2212 | (*arg)->traverse(this); |
| 2213 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2214 | if (arg < arguments->end() - 1) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2215 | { |
| 2216 | out << ", "; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | out << ")"; |
| 2221 | |
| 2222 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2223 | } |
| 2224 | break; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2225 | case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2226 | case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break; |
| 2227 | case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break; |
| 2228 | case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break; |
| 2229 | case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break; |
| 2230 | case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break; |
| 2231 | case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break; |
| 2232 | case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break; |
| 2233 | case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break; |
| 2234 | case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break; |
| 2235 | case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break; |
| 2236 | case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break; |
| 2237 | case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break; |
| 2238 | case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break; |
| 2239 | case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break; |
| 2240 | case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break; |
| 2241 | case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break; |
| 2242 | case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break; |
| 2243 | case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break; |
| 2244 | case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break; |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2245 | case EOpConstructStruct: |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2246 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 2247 | if (node->getType().isArray()) |
| 2248 | { |
| 2249 | UNIMPLEMENTED(); |
| 2250 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2251 | const TString &structName = StructNameString(*node->getType().getStruct()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2252 | mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence()); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2253 | outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")"); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2254 | } |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2255 | break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2256 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 2257 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 2258 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 2259 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 2260 | case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break; |
| 2261 | case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2262 | case EOpMod: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2263 | ASSERT(node->getUseEmulatedFunction()); |
| 2264 | writeEmulatedFunctionTriplet(visit, "mod("); |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2265 | break; |
Olli Etuaho | b6e07a6 | 2015-02-16 12:22:10 +0200 | [diff] [blame] | 2266 | case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2267 | case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2268 | case EOpAtan: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2269 | ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2270 | ASSERT(node->getUseEmulatedFunction()); |
| 2271 | writeEmulatedFunctionTriplet(visit, "atan("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break; |
| 2274 | case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break; |
| 2275 | case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break; |
| 2276 | case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break; |
| 2277 | case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break; |
| 2278 | case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break; |
| 2279 | case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break; |
| 2280 | case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break; |
| 2281 | case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break; |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2282 | case EOpFaceForward: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2283 | ASSERT(node->getUseEmulatedFunction()); |
| 2284 | writeEmulatedFunctionTriplet(visit, "faceforward("); |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2285 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2286 | case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break; |
| 2287 | case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2288 | case EOpOuterProduct: |
| 2289 | ASSERT(node->getUseEmulatedFunction()); |
| 2290 | writeEmulatedFunctionTriplet(visit, "outerProduct("); |
| 2291 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2292 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2293 | default: UNREACHABLE(); |
| 2294 | } |
| 2295 | |
| 2296 | return true; |
| 2297 | } |
| 2298 | |
| 2299 | bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node) |
| 2300 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2301 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2302 | |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2303 | if (node->usesTernaryOperator()) |
| 2304 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2305 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2306 | } |
| 2307 | else // if/else statement |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2308 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2309 | mUnfoldShortCircuit->traverse(node->getCondition()); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2310 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2311 | // D3D errors when there is a gradient operation in a loop in an unflattened if. |
| 2312 | if (mShaderType == GL_FRAGMENT_SHADER |
| 2313 | && mCurrentFunctionMetadata->hasDiscontinuousLoop(node) |
| 2314 | && mCurrentFunctionMetadata->hasGradientInCallGraph(node)) |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2315 | { |
| 2316 | out << "FLATTEN "; |
| 2317 | } |
| 2318 | |
| 2319 | out << "if ("; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2320 | |
| 2321 | node->getCondition()->traverse(this); |
| 2322 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2323 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2324 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2325 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2326 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2327 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2328 | bool discard = false; |
| 2329 | |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2330 | if (node->getTrueBlock()) |
| 2331 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2332 | traverseStatements(node->getTrueBlock()); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2333 | |
| 2334 | // Detect true discard |
| 2335 | discard = (discard || FindDiscard::search(node->getTrueBlock())); |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2336 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2337 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2338 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2339 | out << ";\n}\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2340 | |
| 2341 | if (node->getFalseBlock()) |
| 2342 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2343 | out << "else\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2344 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2345 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2346 | out << "{\n"; |
| 2347 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2348 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2349 | traverseStatements(node->getFalseBlock()); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2350 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2351 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2352 | out << ";\n}\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2353 | |
| 2354 | // Detect false discard |
| 2355 | discard = (discard || FindDiscard::search(node->getFalseBlock())); |
| 2356 | } |
| 2357 | |
| 2358 | // ANGLE issue 486: Detect problematic conditional discard |
| 2359 | if (discard && FindSideEffectRewriting::search(node)) |
| 2360 | { |
| 2361 | mUsesDiscardRewriting = true; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2362 | } |
| 2363 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2364 | |
| 2365 | return false; |
| 2366 | } |
| 2367 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2368 | bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2369 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2370 | if (node->getStatementList()) |
| 2371 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 2372 | node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList())); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2373 | outputTriplet(visit, "switch (", ") ", ""); |
| 2374 | // The curly braces get written when visiting the statementList aggregate |
| 2375 | } |
| 2376 | else |
| 2377 | { |
| 2378 | // No statementList, so it won't output curly braces |
| 2379 | outputTriplet(visit, "switch (", ") {", "}\n"); |
| 2380 | } |
| 2381 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2382 | } |
| 2383 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2384 | bool OutputHLSL::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2385 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2386 | if (node->hasCondition()) |
| 2387 | { |
| 2388 | outputTriplet(visit, "case (", "", "):\n"); |
| 2389 | return true; |
| 2390 | } |
| 2391 | else |
| 2392 | { |
| 2393 | TInfoSinkBase &out = getInfoSink(); |
| 2394 | out << "default:\n"; |
| 2395 | return false; |
| 2396 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2399 | void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) |
| 2400 | { |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2401 | writeConstantUnion(node->getType(), node->getUnionArrayPointer()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) |
| 2405 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2406 | mNestedLoopDepth++; |
| 2407 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2408 | bool wasDiscontinuous = mInsideDiscontinuousLoop; |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2409 | mInsideDiscontinuousLoop = mInsideDiscontinuousLoop || |
Corentin Wallez | a1884f2 | 2015-04-29 10:15:16 -0700 | [diff] [blame] | 2410 | mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2411 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2412 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2413 | { |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2414 | if (handleExcessiveLoop(node)) |
| 2415 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2416 | mInsideDiscontinuousLoop = wasDiscontinuous; |
| 2417 | mNestedLoopDepth--; |
| 2418 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2419 | return false; |
| 2420 | } |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2423 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2424 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2425 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2426 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2427 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2428 | out << "{" << unroll << " do\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2429 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2430 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2431 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2432 | } |
| 2433 | else |
| 2434 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2435 | out << "{" << unroll << " for("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2436 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2437 | if (node->getInit()) |
| 2438 | { |
| 2439 | node->getInit()->traverse(this); |
| 2440 | } |
| 2441 | |
| 2442 | out << "; "; |
| 2443 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2444 | if (node->getCondition()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2445 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2446 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | out << "; "; |
| 2450 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2451 | if (node->getExpression()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2452 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2453 | node->getExpression()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2456 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2457 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2458 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2459 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | if (node->getBody()) |
| 2463 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2464 | traverseStatements(node->getBody()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2467 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 7fb81e8 | 2011-09-23 18:20:46 +0000 | [diff] [blame] | 2468 | out << ";}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2469 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2470 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2471 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2472 | outputLineDirective(node->getCondition()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2473 | out << "while(\n"; |
| 2474 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2475 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2476 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2477 | out << ");"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2480 | out << "}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2481 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2482 | mInsideDiscontinuousLoop = wasDiscontinuous; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2483 | mNestedLoopDepth--; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2484 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2485 | return false; |
| 2486 | } |
| 2487 | |
| 2488 | bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) |
| 2489 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2490 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2491 | |
| 2492 | switch (node->getFlowOp()) |
| 2493 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2494 | case EOpKill: |
| 2495 | outputTriplet(visit, "discard;\n", "", ""); |
| 2496 | break; |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2497 | case EOpBreak: |
| 2498 | if (visit == PreVisit) |
| 2499 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2500 | if (mNestedLoopDepth > 1) |
| 2501 | { |
| 2502 | mUsesNestedBreak = true; |
| 2503 | } |
| 2504 | |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2505 | if (mExcessiveLoopIndex) |
| 2506 | { |
| 2507 | out << "{Break"; |
| 2508 | mExcessiveLoopIndex->traverse(this); |
| 2509 | out << " = true; break;}\n"; |
| 2510 | } |
| 2511 | else |
| 2512 | { |
| 2513 | out << "break;\n"; |
| 2514 | } |
| 2515 | } |
| 2516 | break; |
apatrick@chromium.org | 05a5d8e | 2011-02-16 19:07:20 +0000 | [diff] [blame] | 2517 | case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2518 | case EOpReturn: |
| 2519 | if (visit == PreVisit) |
| 2520 | { |
| 2521 | if (node->getExpression()) |
| 2522 | { |
| 2523 | out << "return "; |
| 2524 | } |
| 2525 | else |
| 2526 | { |
| 2527 | out << "return;\n"; |
| 2528 | } |
| 2529 | } |
| 2530 | else if (visit == PostVisit) |
| 2531 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2532 | if (node->getExpression()) |
| 2533 | { |
| 2534 | out << ";\n"; |
| 2535 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2536 | } |
| 2537 | break; |
| 2538 | default: UNREACHABLE(); |
| 2539 | } |
| 2540 | |
| 2541 | return true; |
| 2542 | } |
| 2543 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2544 | void OutputHLSL::traverseStatements(TIntermNode *node) |
| 2545 | { |
| 2546 | if (isSingleStatement(node)) |
| 2547 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2548 | mUnfoldShortCircuit->traverse(node); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | node->traverse(this); |
| 2552 | } |
| 2553 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2554 | bool OutputHLSL::isSingleStatement(TIntermNode *node) |
| 2555 | { |
| 2556 | TIntermAggregate *aggregate = node->getAsAggregate(); |
| 2557 | |
| 2558 | if (aggregate) |
| 2559 | { |
| 2560 | if (aggregate->getOp() == EOpSequence) |
| 2561 | { |
| 2562 | return false; |
| 2563 | } |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 2564 | else if (aggregate->getOp() == EOpDeclaration) |
| 2565 | { |
| 2566 | // Declaring multiple comma-separated variables must be considered multiple statements |
| 2567 | // because each individual declaration has side effects which are visible in the next. |
| 2568 | return false; |
| 2569 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2570 | else |
| 2571 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2572 | 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] | 2573 | { |
| 2574 | if (!isSingleStatement(*sit)) |
| 2575 | { |
| 2576 | return false; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | return true; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | return true; |
| 2585 | } |
| 2586 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2587 | // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them |
| 2588 | // (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] | 2589 | bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node) |
| 2590 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2591 | const int MAX_LOOP_ITERATIONS = 254; |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2592 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2593 | |
| 2594 | // Parse loops of the form: |
| 2595 | // for(int index = initial; index [comparator] limit; index += increment) |
| 2596 | TIntermSymbol *index = NULL; |
| 2597 | TOperator comparator = EOpNull; |
| 2598 | int initial = 0; |
| 2599 | int limit = 0; |
| 2600 | int increment = 0; |
| 2601 | |
| 2602 | // Parse index name and intial value |
| 2603 | if (node->getInit()) |
| 2604 | { |
| 2605 | TIntermAggregate *init = node->getInit()->getAsAggregate(); |
| 2606 | |
| 2607 | if (init) |
| 2608 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2609 | TIntermSequence *sequence = init->getSequence(); |
| 2610 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (variable && variable->getQualifier() == EvqTemporary) |
| 2613 | { |
| 2614 | TIntermBinary *assign = variable->getAsBinaryNode(); |
| 2615 | |
| 2616 | if (assign->getOp() == EOpInitialize) |
| 2617 | { |
| 2618 | TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); |
| 2619 | TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); |
| 2620 | |
| 2621 | if (symbol && constant) |
| 2622 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2623 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2624 | { |
| 2625 | index = symbol; |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2626 | initial = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | // Parse comparator and limit value |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2635 | if (index != NULL && node->getCondition()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2636 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2637 | TIntermBinary *test = node->getCondition()->getAsBinaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2638 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2639 | if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId()) |
| 2640 | { |
| 2641 | TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); |
| 2642 | |
| 2643 | if (constant) |
| 2644 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2645 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2646 | { |
| 2647 | comparator = test->getOp(); |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2648 | limit = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | // Parse increment |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2655 | if (index != NULL && comparator != EOpNull && node->getExpression()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2656 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2657 | TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); |
| 2658 | TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2659 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2660 | if (binaryTerminal) |
| 2661 | { |
| 2662 | TOperator op = binaryTerminal->getOp(); |
| 2663 | TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); |
| 2664 | |
| 2665 | if (constant) |
| 2666 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2667 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2668 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2669 | int value = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2670 | |
| 2671 | switch (op) |
| 2672 | { |
| 2673 | case EOpAddAssign: increment = value; break; |
| 2674 | case EOpSubAssign: increment = -value; break; |
| 2675 | default: UNIMPLEMENTED(); |
| 2676 | } |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | else if (unaryTerminal) |
| 2681 | { |
| 2682 | TOperator op = unaryTerminal->getOp(); |
| 2683 | |
| 2684 | switch (op) |
| 2685 | { |
| 2686 | case EOpPostIncrement: increment = 1; break; |
| 2687 | case EOpPostDecrement: increment = -1; break; |
| 2688 | case EOpPreIncrement: increment = 1; break; |
| 2689 | case EOpPreDecrement: increment = -1; break; |
| 2690 | default: UNIMPLEMENTED(); |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | if (index != NULL && comparator != EOpNull && increment != 0) |
| 2696 | { |
| 2697 | if (comparator == EOpLessThanEqual) |
| 2698 | { |
| 2699 | comparator = EOpLessThan; |
| 2700 | limit += 1; |
| 2701 | } |
| 2702 | |
| 2703 | if (comparator == EOpLessThan) |
| 2704 | { |
daniel@transgaming.com | f1f538e | 2011-02-09 16:30:01 +0000 | [diff] [blame] | 2705 | int iterations = (limit - initial) / increment; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2706 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2707 | if (iterations <= MAX_LOOP_ITERATIONS) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2708 | { |
| 2709 | return false; // Not an excessive loop |
| 2710 | } |
| 2711 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2712 | TIntermSymbol *restoreIndex = mExcessiveLoopIndex; |
| 2713 | mExcessiveLoopIndex = index; |
| 2714 | |
daniel@transgaming.com | 0933b0c | 2012-07-11 20:37:28 +0000 | [diff] [blame] | 2715 | out << "{int "; |
| 2716 | index->traverse(this); |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2717 | out << ";\n" |
| 2718 | "bool Break"; |
| 2719 | index->traverse(this); |
| 2720 | out << " = false;\n"; |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2721 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2722 | bool firstLoopFragment = true; |
| 2723 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2724 | while (iterations > 0) |
| 2725 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2726 | int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2727 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2728 | if (!firstLoopFragment) |
| 2729 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2730 | out << "if (!Break"; |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2731 | index->traverse(this); |
| 2732 | out << ") {\n"; |
| 2733 | } |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2734 | |
| 2735 | if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment |
| 2736 | { |
| 2737 | mExcessiveLoopIndex = NULL; // Stops setting the Break flag |
| 2738 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2739 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2740 | // for(int index = initial; index < clampedLimit; index += increment) |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2741 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2742 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2743 | out << unroll << " for("; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2744 | index->traverse(this); |
| 2745 | out << " = "; |
| 2746 | out << initial; |
| 2747 | |
| 2748 | out << "; "; |
| 2749 | index->traverse(this); |
| 2750 | out << " < "; |
| 2751 | out << clampedLimit; |
| 2752 | |
| 2753 | out << "; "; |
| 2754 | index->traverse(this); |
| 2755 | out << " += "; |
| 2756 | out << increment; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2757 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2758 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2759 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2760 | out << "{\n"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2761 | |
| 2762 | if (node->getBody()) |
| 2763 | { |
| 2764 | node->getBody()->traverse(this); |
| 2765 | } |
| 2766 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2767 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2768 | out << ";}\n"; |
| 2769 | |
| 2770 | if (!firstLoopFragment) |
| 2771 | { |
| 2772 | out << "}\n"; |
| 2773 | } |
| 2774 | |
| 2775 | firstLoopFragment = false; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2776 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2777 | initial += MAX_LOOP_ITERATIONS * increment; |
| 2778 | iterations -= MAX_LOOP_ITERATIONS; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2779 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2780 | |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2781 | out << "}"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2782 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2783 | mExcessiveLoopIndex = restoreIndex; |
| 2784 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2785 | return true; |
| 2786 | } |
| 2787 | else UNIMPLEMENTED(); |
| 2788 | } |
| 2789 | |
| 2790 | return false; // Not handled as an excessive loop |
| 2791 | } |
| 2792 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 2793 | 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] | 2794 | { |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2795 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2796 | { |
| 2797 | out << preString; |
| 2798 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2799 | else if (visit == InVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2800 | { |
| 2801 | out << inString; |
| 2802 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2803 | else if (visit == PostVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2804 | { |
| 2805 | out << postString; |
| 2806 | } |
| 2807 | } |
| 2808 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 2809 | void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString) |
| 2810 | { |
| 2811 | outputTriplet(visit, preString, inString, postString, getInfoSink()); |
| 2812 | } |
| 2813 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2814 | void OutputHLSL::outputLineDirective(int line) |
| 2815 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2816 | if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2817 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2818 | TInfoSinkBase &out = getInfoSink(); |
| 2819 | |
| 2820 | out << "\n"; |
| 2821 | out << "#line " << line; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2822 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2823 | if (mSourcePath) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2824 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2825 | out << " \"" << mSourcePath << "\""; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2826 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2827 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2828 | out << "\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2832 | TString OutputHLSL::argumentString(const TIntermSymbol *symbol) |
| 2833 | { |
| 2834 | TQualifier qualifier = symbol->getQualifier(); |
| 2835 | const TType &type = symbol->getType(); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2836 | TString name = symbol->getSymbol(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2837 | |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2838 | if (name.empty()) // HLSL demands named arguments, also for prototypes |
| 2839 | { |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 2840 | name = "x" + str(mUniqueIndex++); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2841 | } |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2842 | else if (!symbol->isInternal()) |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2843 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2844 | name = Decorate(name); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2847 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) |
| 2848 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2849 | return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " + |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2850 | QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2853 | return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | TString OutputHLSL::initializer(const TType &type) |
| 2857 | { |
| 2858 | TString string; |
| 2859 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2860 | size_t size = type.getObjectSize(); |
| 2861 | for (size_t component = 0; component < size; component++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2862 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2863 | string += "0"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2864 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2865 | if (component + 1 < size) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2866 | { |
| 2867 | string += ", "; |
| 2868 | } |
| 2869 | } |
| 2870 | |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2871 | return "{" + string + "}"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2872 | } |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 2873 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2874 | 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] | 2875 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 2876 | if (type.isArray()) |
| 2877 | { |
| 2878 | UNIMPLEMENTED(); |
| 2879 | } |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2880 | TInfoSinkBase &out = getInfoSink(); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2881 | |
| 2882 | if (visit == PreVisit) |
| 2883 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 2884 | mStructureHLSL->addConstructor(type, name, parameters); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2885 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2886 | out << name << "("; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2887 | } |
| 2888 | else if (visit == InVisit) |
| 2889 | { |
| 2890 | out << ", "; |
| 2891 | } |
| 2892 | else if (visit == PostVisit) |
| 2893 | { |
| 2894 | out << ")"; |
| 2895 | } |
| 2896 | } |
| 2897 | |
Jamie Madill | 6ba6ead | 2015-05-04 14:21:21 -0400 | [diff] [blame^] | 2898 | const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2899 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2900 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2901 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2902 | const TStructure* structure = type.getStruct(); |
| 2903 | if (structure) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2904 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2905 | out << StructNameString(*structure) + "_ctor("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2906 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2907 | const TFieldList& fields = structure->fields(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2908 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2909 | for (size_t i = 0; i < fields.size(); i++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2910 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2911 | const TType *fieldType = fields[i]->type(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2912 | constUnion = writeConstantUnion(*fieldType, constUnion); |
| 2913 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2914 | if (i != fields.size() - 1) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2915 | { |
| 2916 | out << ", "; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | out << ")"; |
| 2921 | } |
| 2922 | else |
| 2923 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2924 | size_t size = type.getObjectSize(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2925 | bool writeType = size > 1; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2926 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2927 | if (writeType) |
| 2928 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2929 | out << TypeString(type) << "("; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2932 | for (size_t i = 0; i < size; i++, constUnion++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2933 | { |
| 2934 | switch (constUnion->getType()) |
| 2935 | { |
daniel@transgaming.com | 6c1203f | 2013-01-11 04:12:43 +0000 | [diff] [blame] | 2936 | 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] | 2937 | case EbtInt: out << constUnion->getIConst(); break; |
Nicolas Capens | c0f7c61 | 2013-06-05 11:46:09 -0400 | [diff] [blame] | 2938 | case EbtUInt: out << constUnion->getUConst(); break; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 2939 | case EbtBool: out << constUnion->getBConst(); break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2940 | default: UNREACHABLE(); |
| 2941 | } |
| 2942 | |
| 2943 | if (i != size - 1) |
| 2944 | { |
| 2945 | out << ", "; |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | if (writeType) |
| 2950 | { |
| 2951 | out << ")"; |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | return constUnion; |
| 2956 | } |
| 2957 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2958 | void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr) |
| 2959 | { |
| 2960 | TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr); |
| 2961 | outputTriplet(visit, preString.c_str(), ", ", ")"); |
| 2962 | } |
| 2963 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 2964 | bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression) |
| 2965 | { |
| 2966 | sh::SearchSymbol searchSymbol(symbolNode->getSymbol()); |
| 2967 | expression->traverse(&searchSymbol); |
| 2968 | |
| 2969 | if (searchSymbol.foundMatch()) |
| 2970 | { |
| 2971 | // Type already printed |
| 2972 | out << "t" + str(mUniqueIndex) + " = "; |
| 2973 | expression->traverse(this); |
| 2974 | out << ", "; |
| 2975 | symbolNode->traverse(this); |
| 2976 | out << " = t" + str(mUniqueIndex); |
| 2977 | |
| 2978 | mUniqueIndex++; |
| 2979 | return true; |
| 2980 | } |
| 2981 | |
| 2982 | return false; |
| 2983 | } |
| 2984 | |
| 2985 | void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out) |
| 2986 | { |
| 2987 | out << "#define ANGLE_USES_DEFERRED_INIT\n" |
| 2988 | << "\n" |
| 2989 | << "void initializeDeferredGlobals()\n" |
| 2990 | << "{\n"; |
| 2991 | |
| 2992 | for (const auto &deferredGlobal : mDeferredGlobalInitializers) |
| 2993 | { |
| 2994 | TIntermSymbol *symbol = deferredGlobal.first; |
| 2995 | TIntermTyped *expression = deferredGlobal.second; |
| 2996 | ASSERT(symbol); |
| 2997 | ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst); |
| 2998 | |
| 2999 | out << " " << Decorate(symbol->getSymbol()) << " = "; |
| 3000 | |
| 3001 | if (!writeSameSymbolInitializer(out, symbol, expression)) |
| 3002 | { |
| 3003 | ASSERT(mInfoSinkStack.top() == &out); |
| 3004 | expression->traverse(this); |
| 3005 | } |
| 3006 | |
| 3007 | out << ";\n"; |
| 3008 | } |
| 3009 | |
| 3010 | out << "}\n" |
| 3011 | << "\n"; |
| 3012 | } |
| 3013 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3014 | TString OutputHLSL::addStructEqualityFunction(const TStructure &structure) |
| 3015 | { |
| 3016 | const TFieldList &fields = structure.fields(); |
| 3017 | |
| 3018 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 3019 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3020 | if (eqFunction->structure == &structure) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3021 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3022 | return eqFunction->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | const TString &structNameString = StructNameString(structure); |
| 3027 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3028 | StructEqualityFunction *function = new StructEqualityFunction(); |
| 3029 | function->structure = &structure; |
| 3030 | function->functionName = "angle_eq_" + structNameString; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3031 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3032 | TInfoSinkBase fnOut; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3033 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3034 | fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n" |
| 3035 | << "{\n" |
| 3036 | " return "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3037 | |
| 3038 | for (size_t i = 0; i < fields.size(); i++) |
| 3039 | { |
| 3040 | const TField *field = fields[i]; |
| 3041 | const TType *fieldType = field->type(); |
| 3042 | |
| 3043 | const TString &fieldNameA = "a." + Decorate(field->name()); |
| 3044 | const TString &fieldNameB = "b." + Decorate(field->name()); |
| 3045 | |
| 3046 | if (i > 0) |
| 3047 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3048 | fnOut << " && "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3049 | } |
| 3050 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3051 | fnOut << "("; |
| 3052 | outputEqual(PreVisit, *fieldType, EOpEqual, fnOut); |
| 3053 | fnOut << fieldNameA; |
| 3054 | outputEqual(InVisit, *fieldType, EOpEqual, fnOut); |
| 3055 | fnOut << fieldNameB; |
| 3056 | outputEqual(PostVisit, *fieldType, EOpEqual, fnOut); |
| 3057 | fnOut << ")"; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3058 | } |
| 3059 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3060 | fnOut << ";\n" << "}\n"; |
| 3061 | |
| 3062 | function->functionDefinition = fnOut.c_str(); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3063 | |
| 3064 | mStructEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3065 | mEqualityFunctions.push_back(function); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3066 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3067 | return function->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3068 | } |
| 3069 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3070 | TString OutputHLSL::addArrayEqualityFunction(const TType& type) |
| 3071 | { |
| 3072 | for (const auto &eqFunction : mArrayEqualityFunctions) |
| 3073 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3074 | if (eqFunction->type == type) |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3075 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3076 | return eqFunction->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | const TString &typeName = TypeString(type); |
| 3081 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3082 | ArrayHelperFunction *function = new ArrayHelperFunction(); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3083 | function->type = type; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3084 | |
| 3085 | TInfoSinkBase fnNameOut; |
| 3086 | fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3087 | function->functionName = fnNameOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3088 | |
| 3089 | TType nonArrayType = type; |
| 3090 | nonArrayType.clearArrayness(); |
| 3091 | |
| 3092 | TInfoSinkBase fnOut; |
| 3093 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3094 | fnOut << "bool " << function->functionName << "(" |
Olli Etuaho | fc7cfd1 | 2015-03-31 14:46:18 +0300 | [diff] [blame] | 3095 | << typeName << " a[" << type.getArraySize() << "], " |
| 3096 | << typeName << " b[" << type.getArraySize() << "])\n" |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3097 | << "{\n" |
| 3098 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3099 | " {\n" |
| 3100 | " if ("; |
| 3101 | |
| 3102 | outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3103 | fnOut << "a[i]"; |
| 3104 | outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3105 | fnOut << "b[i]"; |
| 3106 | outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3107 | |
| 3108 | fnOut << ") { return false; }\n" |
| 3109 | " }\n" |
| 3110 | " return true;\n" |
| 3111 | "}\n"; |
| 3112 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3113 | function->functionDefinition = fnOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3114 | |
| 3115 | mArrayEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3116 | mEqualityFunctions.push_back(function); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3117 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3118 | return function->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3119 | } |
| 3120 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3121 | TString OutputHLSL::addArrayAssignmentFunction(const TType& type) |
| 3122 | { |
| 3123 | for (const auto &assignFunction : mArrayAssignmentFunctions) |
| 3124 | { |
| 3125 | if (assignFunction.type == type) |
| 3126 | { |
| 3127 | return assignFunction.functionName; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | const TString &typeName = TypeString(type); |
| 3132 | |
| 3133 | ArrayHelperFunction function; |
| 3134 | function.type = type; |
| 3135 | |
| 3136 | TInfoSinkBase fnNameOut; |
| 3137 | fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName; |
| 3138 | function.functionName = fnNameOut.c_str(); |
| 3139 | |
| 3140 | TInfoSinkBase fnOut; |
| 3141 | |
| 3142 | fnOut << "void " << function.functionName << "(out " |
| 3143 | << typeName << " a[" << type.getArraySize() << "], " |
| 3144 | << typeName << " b[" << type.getArraySize() << "])\n" |
| 3145 | << "{\n" |
| 3146 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3147 | " {\n" |
| 3148 | " a[i] = b[i];\n" |
| 3149 | " }\n" |
| 3150 | "}\n"; |
| 3151 | |
| 3152 | function.functionDefinition = fnOut.c_str(); |
| 3153 | |
| 3154 | mArrayAssignmentFunctions.push_back(function); |
| 3155 | |
| 3156 | return function.functionName; |
| 3157 | } |
| 3158 | |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3159 | TString OutputHLSL::addArrayConstructIntoFunction(const TType& type) |
| 3160 | { |
| 3161 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 3162 | { |
| 3163 | if (constructIntoFunction.type == type) |
| 3164 | { |
| 3165 | return constructIntoFunction.functionName; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | const TString &typeName = TypeString(type); |
| 3170 | |
| 3171 | ArrayHelperFunction function; |
| 3172 | function.type = type; |
| 3173 | |
| 3174 | TInfoSinkBase fnNameOut; |
| 3175 | fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName; |
| 3176 | function.functionName = fnNameOut.c_str(); |
| 3177 | |
| 3178 | TInfoSinkBase fnOut; |
| 3179 | |
| 3180 | fnOut << "void " << function.functionName << "(out " |
| 3181 | << typeName << " a[" << type.getArraySize() << "]"; |
| 3182 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3183 | { |
| 3184 | fnOut << ", " << typeName << " b" << i; |
| 3185 | } |
| 3186 | fnOut << ")\n" |
| 3187 | "{\n"; |
| 3188 | |
| 3189 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3190 | { |
| 3191 | fnOut << " a[" << i << "] = b" << i << ";\n"; |
| 3192 | } |
| 3193 | fnOut << "}\n"; |
| 3194 | |
| 3195 | function.functionDefinition = fnOut.c_str(); |
| 3196 | |
| 3197 | mArrayConstructIntoFunctions.push_back(function); |
| 3198 | |
| 3199 | return function.functionName; |
| 3200 | } |
| 3201 | |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 3202 | void OutputHLSL::ensureStructDefined(const TType &type) |
| 3203 | { |
| 3204 | TStructure *structure = type.getStruct(); |
| 3205 | |
| 3206 | if (structure) |
| 3207 | { |
| 3208 | mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr); |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3213 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3214 | } |