alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +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 | |
Corentin Wallez | 8b28a8b | 2016-09-15 19:47:56 -0400 | [diff] [blame] | 7 | #include "compiler/translator/Compiler.h" |
| 8 | |
| 9 | #include <sstream> |
| 10 | |
| 11 | #include "angle_gl.h" |
| 12 | #include "common/utilities.h" |
Qiankun Miao | 09cfac6 | 2016-09-06 17:25:16 +0800 | [diff] [blame] | 13 | #include "compiler/translator/AddAndTrueToLoopCondition.h" |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 14 | #include "compiler/translator/Cache.h" |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 15 | #include "compiler/translator/CallDAG.h" |
Olli Etuaho | ab91882 | 2017-07-14 17:03:42 +0300 | [diff] [blame] | 16 | #include "compiler/translator/ClampPointSize.h" |
Olli Etuaho | 78ed6cd | 2017-08-09 16:19:00 +0300 | [diff] [blame] | 17 | #include "compiler/translator/CollectVariables.h" |
Martin Radev | 69056a1 | 2017-05-18 11:14:50 +0300 | [diff] [blame] | 18 | #include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 19 | #include "compiler/translator/DeferGlobalInitializers.h" |
Zhenyao Mo | 4e94fea | 2016-08-09 14:31:37 -0700 | [diff] [blame] | 20 | #include "compiler/translator/EmulateGLFragColorBroadcast.h" |
Jamie Madill | d569619 | 2016-10-06 11:09:24 -0400 | [diff] [blame] | 21 | #include "compiler/translator/EmulatePrecision.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 22 | #include "compiler/translator/Initialize.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 23 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 24 | #include "compiler/translator/IntermNodePatternMatcher.h" |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 25 | #include "compiler/translator/IsASTDepthBelowLimit.h" |
| 26 | #include "compiler/translator/OutputTree.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 27 | #include "compiler/translator/ParseContext.h" |
Olli Etuaho | 87c3588 | 2017-10-19 15:19:38 +0300 | [diff] [blame] | 28 | #include "compiler/translator/PruneNoOps.h" |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 29 | #include "compiler/translator/RegenerateStructNames.h" |
Olli Etuaho | bb5a7e2 | 2017-08-30 13:03:12 +0300 | [diff] [blame] | 30 | #include "compiler/translator/RemoveArrayLengthMethod.h" |
Olli Etuaho | c1f14fb | 2017-10-28 19:17:23 +0300 | [diff] [blame] | 31 | #include "compiler/translator/RemoveEmptySwitchStatements.h" |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 32 | #include "compiler/translator/RemoveInvariantDeclaration.h" |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame] | 33 | #include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h" |
Olli Etuaho | 5c407bb | 2015-06-01 12:20:39 +0300 | [diff] [blame] | 34 | #include "compiler/translator/RemovePow.h" |
Olli Etuaho | 3d70ca9 | 2017-11-10 16:53:26 +0200 | [diff] [blame] | 35 | #include "compiler/translator/RemoveUnreferencedVariables.h" |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 36 | #include "compiler/translator/RewriteDoWhile.h" |
Zhenyao Mo | cd68fe7 | 2014-07-11 10:45:44 -0700 | [diff] [blame] | 37 | #include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h" |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 38 | #include "compiler/translator/SeparateDeclarations.h" |
| 39 | #include "compiler/translator/SimplifyLoopConditions.h" |
Olli Etuaho | bb5a7e2 | 2017-08-30 13:03:12 +0300 | [diff] [blame] | 40 | #include "compiler/translator/SplitSequenceOperator.h" |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 41 | #include "compiler/translator/UnfoldShortCircuitAST.h" |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 42 | #include "compiler/translator/UseInterfaceBlockFields.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 43 | #include "compiler/translator/ValidateLimitations.h" |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 44 | #include "compiler/translator/ValidateMaxParameters.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 45 | #include "compiler/translator/ValidateOutputs.h" |
Jiawei Shao | 4cc89e2 | 2017-08-31 14:25:54 +0800 | [diff] [blame] | 46 | #include "compiler/translator/ValidateVaryingLocations.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 47 | #include "compiler/translator/VariablePacker.h" |
Olli Etuaho | 661fc48 | 2017-10-16 12:17:05 +0300 | [diff] [blame] | 48 | #include "compiler/translator/VectorizeVectorScalarArithmetic.h" |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 49 | #include "compiler/translator/util.h" |
shannon.woods@transgaming.com | da1ed36 | 2013-01-25 21:54:57 +0000 | [diff] [blame] | 50 | #include "third_party/compiler/ArrayBoundsClamper.h" |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 51 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 52 | namespace sh |
| 53 | { |
| 54 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 55 | namespace |
| 56 | { |
| 57 | |
| 58 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 59 | void DumpFuzzerCase(char const *const *shaderStrings, |
| 60 | size_t numStrings, |
| 61 | uint32_t type, |
| 62 | uint32_t spec, |
| 63 | uint32_t output, |
| 64 | uint64_t options) |
| 65 | { |
| 66 | static int fileIndex = 0; |
| 67 | |
| 68 | std::ostringstream o; |
| 69 | o << "corpus/" << fileIndex++ << ".sample"; |
| 70 | std::string s = o.str(); |
| 71 | |
| 72 | // Must match the input format of the fuzzer |
| 73 | FILE *f = fopen(s.c_str(), "w"); |
| 74 | fwrite(&type, sizeof(type), 1, f); |
| 75 | fwrite(&spec, sizeof(spec), 1, f); |
| 76 | fwrite(&output, sizeof(output), 1, f); |
| 77 | fwrite(&options, sizeof(options), 1, f); |
| 78 | |
| 79 | char zero[128 - 20] = {0}; |
| 80 | fwrite(&zero, 128 - 20, 1, f); |
| 81 | |
| 82 | for (size_t i = 0; i < numStrings; i++) |
| 83 | { |
| 84 | fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f); |
| 85 | } |
| 86 | fwrite(&zero, 1, 1, f); |
| 87 | |
| 88 | fclose(f); |
| 89 | } |
| 90 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 91 | } // anonymous namespace |
| 92 | |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 93 | bool IsWebGLBasedSpec(ShShaderSpec spec) |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 94 | { |
Qiankun Miao | c2c5fc4 | 2016-08-31 15:24:22 +0800 | [diff] [blame] | 95 | return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC); |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 98 | bool IsGLSL130OrNewer(ShShaderOutput output) |
| 99 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 100 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 101 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 102 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT || |
| 103 | output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT || |
| 104 | output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT); |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 107 | bool IsGLSL420OrNewer(ShShaderOutput output) |
| 108 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 109 | return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT || |
| 110 | output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT); |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Zhenyao Mo | b7bf742 | 2016-11-08 14:44:05 -0800 | [diff] [blame] | 113 | bool IsGLSL410OrOlder(ShShaderOutput output) |
| 114 | { |
| 115 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 116 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 117 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT); |
| 118 | } |
| 119 | |
Qiankun Miao | 89dd8f3 | 2016-11-09 12:59:30 +0000 | [diff] [blame] | 120 | bool RemoveInvariant(sh::GLenum shaderType, |
| 121 | int shaderVersion, |
| 122 | ShShaderOutput outputType, |
| 123 | ShCompileOptions compileOptions) |
| 124 | { |
| 125 | if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 && |
| 126 | shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType)) |
| 127 | return true; |
| 128 | |
| 129 | if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 && |
Qiankun Miao | 41f9f67 | 2016-11-16 17:04:36 +0800 | [diff] [blame] | 130 | shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER) |
Qiankun Miao | 89dd8f3 | 2016-11-09 12:59:30 +0000 | [diff] [blame] | 131 | return true; |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |
Zhenyao Mo | 7faf1a1 | 2014-04-25 18:03:56 -0700 | [diff] [blame] | 136 | size_t GetGlobalMaxTokenSize(ShShaderSpec spec) |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 137 | { |
He Yunchao | 29ab9ff | 2015-08-06 16:58:30 +0800 | [diff] [blame] | 138 | // WebGL defines a max token length of 256, while ES2 leaves max token |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 139 | // size undefined. ES3 defines a max size of 1024 characters. |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 140 | switch (spec) |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 141 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 142 | case SH_WEBGL_SPEC: |
| 143 | return 256; |
| 144 | default: |
| 145 | return 1024; |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 149 | int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources) |
| 150 | { |
| 151 | switch (shaderType) |
| 152 | { |
| 153 | case GL_VERTEX_SHADER: |
| 154 | return resources.MaxVertexUniformVectors; |
| 155 | case GL_FRAGMENT_SHADER: |
| 156 | return resources.MaxFragmentUniformVectors; |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 157 | |
| 158 | // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 159 | case GL_COMPUTE_SHADER: |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 160 | return resources.MaxComputeUniformComponents / 4; |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 161 | case GL_GEOMETRY_SHADER_OES: |
| 162 | return resources.MaxGeometryUniformComponents / 4; |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 163 | default: |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 164 | UNREACHABLE(); |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 169 | namespace |
| 170 | { |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 171 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 172 | class TScopedPoolAllocator |
| 173 | { |
| 174 | public: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 175 | TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 176 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 177 | mAllocator->push(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 178 | SetGlobalPoolAllocator(mAllocator); |
| 179 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 180 | ~TScopedPoolAllocator() |
| 181 | { |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 182 | SetGlobalPoolAllocator(nullptr); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 183 | mAllocator->pop(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 186 | private: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 187 | TPoolAllocator *mAllocator; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 188 | }; |
| 189 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 190 | class TScopedSymbolTableLevel |
| 191 | { |
| 192 | public: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 193 | TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 194 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 195 | ASSERT(mTable->atBuiltInLevel()); |
| 196 | mTable->push(); |
| 197 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 198 | ~TScopedSymbolTableLevel() |
| 199 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 200 | while (!mTable->atBuiltInLevel()) |
| 201 | mTable->pop(); |
| 202 | } |
| 203 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 204 | private: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 205 | TSymbolTable *mTable; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 206 | }; |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 207 | |
| 208 | int MapSpecToShaderVersion(ShShaderSpec spec) |
| 209 | { |
| 210 | switch (spec) |
| 211 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 212 | case SH_GLES2_SPEC: |
| 213 | case SH_WEBGL_SPEC: |
| 214 | return 100; |
| 215 | case SH_GLES3_SPEC: |
| 216 | case SH_WEBGL2_SPEC: |
| 217 | return 300; |
| 218 | case SH_GLES3_1_SPEC: |
| 219 | case SH_WEBGL3_SPEC: |
| 220 | return 310; |
| 221 | default: |
| 222 | UNREACHABLE(); |
| 223 | return 0; |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 227 | } // namespace |
| 228 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 229 | TShHandleBase::TShHandleBase() |
| 230 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 231 | allocator.push(); |
| 232 | SetGlobalPoolAllocator(&allocator); |
| 233 | } |
| 234 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 235 | TShHandleBase::~TShHandleBase() |
| 236 | { |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 237 | SetGlobalPoolAllocator(nullptr); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 238 | allocator.popAll(); |
| 239 | } |
| 240 | |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 241 | TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output) |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 242 | : variablesCollected(false), |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 243 | mGLPositionInitialized(false), |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 244 | shaderType(type), |
zmo@google.com | f420c42 | 2011-09-12 18:27:59 +0000 | [diff] [blame] | 245 | shaderSpec(spec), |
Jamie Madill | 68fe74a | 2014-05-27 12:56:01 -0400 | [diff] [blame] | 246 | outputType(output), |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 247 | maxUniformVectors(0), |
| 248 | maxExpressionComplexity(0), |
| 249 | maxCallStackDepth(0), |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 250 | maxFunctionParameters(0), |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 251 | fragmentPrecisionHigh(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 252 | clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC), |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 253 | builtInFunctionEmulator(), |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 254 | mDiagnostics(infoSink.info), |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 255 | mSourcePath(nullptr), |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 256 | mComputeShaderLocalSizeDeclared(false), |
Jamie Madill | 2f294c9 | 2017-11-20 14:47:26 -0500 | [diff] [blame] | 257 | mComputeShaderLocalSize(1), |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 258 | mGeometryShaderMaxVertices(-1), |
| 259 | mGeometryShaderInvocations(0), |
| 260 | mGeometryShaderInputPrimitiveType(EptUndefined), |
| 261 | mGeometryShaderOutputPrimitiveType(EptUndefined) |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 262 | { |
| 263 | } |
| 264 | |
| 265 | TCompiler::~TCompiler() |
| 266 | { |
| 267 | } |
| 268 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 269 | bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const |
Olli Etuaho | 5d91dda | 2015-06-18 15:47:46 +0300 | [diff] [blame] | 270 | { |
| 271 | // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API, |
| 272 | // validate loop and indexing as well (to verify that the shader only uses minimal functionality |
| 273 | // of ESSL 1.00 as in Appendix A of the spec). |
| 274 | return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) || |
| 275 | (compileOptions & SH_VALIDATE_LOOP_INDEXING); |
| 276 | } |
| 277 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 278 | bool TCompiler::Init(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 279 | { |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 280 | shaderVersion = 100; |
| 281 | |
| 282 | maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources); |
| 283 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 284 | maxExpressionComplexity = resources.MaxExpressionComplexity; |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 285 | maxCallStackDepth = resources.MaxCallStackDepth; |
| 286 | maxFunctionParameters = resources.MaxFunctionParameters; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 287 | |
| 288 | SetGlobalPoolAllocator(&allocator); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 289 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 290 | // Generate built-in symbol table. |
| 291 | if (!InitBuiltInSymbolTable(resources)) |
| 292 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 293 | InitExtensionBehavior(resources, extensionBehavior); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 294 | fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 295 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 296 | arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy); |
| 297 | clampingStrategy = resources.ArrayIndexClampingStrategy; |
| 298 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 299 | hashFunction = resources.HashFunction; |
| 300 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 304 | TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[], |
| 305 | size_t numStrings, |
| 306 | ShCompileOptions compileOptions) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 307 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 308 | return compileTreeImpl(shaderStrings, numStrings, compileOptions); |
| 309 | } |
| 310 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 311 | TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[], |
| 312 | size_t numStrings, |
| 313 | const ShCompileOptions compileOptions) |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 314 | { |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 315 | clearResults(); |
| 316 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 317 | ASSERT(numStrings > 0); |
| 318 | ASSERT(GetGlobalPoolAllocator()); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 319 | |
David Yen | 0fbd128 | 2015-02-02 14:46:09 -0800 | [diff] [blame] | 320 | // Reset the extension behavior for each compilation unit. |
| 321 | ResetExtensionBehavior(extensionBehavior); |
| 322 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 323 | // First string is path of source file if flag is set. The actual source follows. |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 324 | size_t firstSource = 0; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 325 | if (compileOptions & SH_SOURCE_PATH) |
| 326 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 327 | mSourcePath = shaderStrings[0]; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 328 | ++firstSource; |
| 329 | } |
| 330 | |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame] | 331 | TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec, |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 332 | compileOptions, true, &mDiagnostics, getResources()); |
Olli Etuaho | 853dc1a | 2014-11-06 17:25:48 +0200 | [diff] [blame] | 333 | |
Olli Etuaho | a699668 | 2015-10-12 14:32:30 +0300 | [diff] [blame] | 334 | parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 335 | |
| 336 | // We preserve symbols at the built-in level from compile-to-compile. |
| 337 | // Start pushing the user-defined symbols at global level. |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 338 | TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 339 | |
| 340 | // Parse shader. |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 341 | if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, |
| 342 | &parseContext) != 0) |
| 343 | { |
| 344 | return nullptr; |
| 345 | } |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 346 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 347 | if (parseContext.getTreeRoot() == nullptr) |
| 348 | { |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
| 352 | setASTMetadata(parseContext); |
| 353 | |
| 354 | if (MapSpecToShaderVersion(shaderSpec) < shaderVersion) |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 355 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 356 | mDiagnostics.globalError("unsupported shader version"); |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 357 | return nullptr; |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 358 | } |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 359 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 360 | TIntermBlock *root = parseContext.getTreeRoot(); |
| 361 | if (!checkAndSimplifyAST(root, parseContext, compileOptions)) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 362 | { |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 363 | return nullptr; |
| 364 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 365 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 366 | return root; |
| 367 | } |
Martin Radev | 802abe0 | 2016-08-04 17:48:32 +0300 | [diff] [blame] | 368 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 369 | void TCompiler::setASTMetadata(const TParseContext &parseContext) |
| 370 | { |
| 371 | shaderVersion = parseContext.getShaderVersion(); |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 372 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 373 | mPragma = parseContext.pragma(); |
| 374 | symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 375 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 376 | mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared(); |
| 377 | mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize(); |
Olli Etuaho | a699668 | 2015-10-12 14:32:30 +0300 | [diff] [blame] | 378 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 379 | mNumViews = parseContext.getNumViews(); |
| 380 | |
| 381 | // Highp might have been auto-enabled based on shader version |
| 382 | fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh(); |
| 383 | |
| 384 | if (shaderType == GL_GEOMETRY_SHADER_OES) |
| 385 | { |
| 386 | mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType(); |
| 387 | mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType(); |
| 388 | mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices(); |
| 389 | mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | bool TCompiler::checkAndSimplifyAST(TIntermBlock *root, |
| 394 | const TParseContext &parseContext, |
| 395 | ShCompileOptions compileOptions) |
| 396 | { |
| 397 | // Disallow expressions deemed too complex. |
| 398 | if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root)) |
| 399 | { |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | // We prune no-ops to work around driver bugs and to keep AST processing and output simple. |
| 404 | // The following kinds of no-ops are pruned: |
| 405 | // 1. Empty declarations "int;". |
| 406 | // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision |
| 407 | // for float, so float literal statements would end up with no precision which is |
| 408 | // invalid ESSL. |
| 409 | // After this empty declarations are not allowed in the AST. |
| 410 | PruneNoOps(root); |
| 411 | |
| 412 | // In case the last case inside a switch statement is a certain type of no-op, GLSL |
| 413 | // compilers in drivers may not accept it. In this case we clean up the dead code from the |
| 414 | // end of switch statements. This is also required because PruneNoOps may have left switch |
| 415 | // statements that only contained an empty declaration inside the final case in an invalid |
| 416 | // state. Relies on that PruneNoOps has already been run. |
| 417 | RemoveNoOpCasesFromEndOfSwitchStatements(root, &symbolTable); |
| 418 | |
| 419 | // Remove empty switch statements - this makes output simpler. |
| 420 | RemoveEmptySwitchStatements(root); |
| 421 | |
| 422 | // Create the function DAG and check there is no recursion |
| 423 | if (!initCallDag(root)) |
| 424 | { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth()) |
| 429 | { |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | // Checks which functions are used and if "main" exists |
| 434 | functionMetadata.clear(); |
| 435 | functionMetadata.resize(mCallDag.size()); |
| 436 | if (!tagUsedFunctions()) |
| 437 | { |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS)) |
| 442 | { |
| 443 | pruneUnusedFunctions(root); |
| 444 | } |
| 445 | |
| 446 | if (shaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, shaderType)) |
| 447 | { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | if (shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER && |
| 452 | !ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers, |
| 453 | &mDiagnostics)) |
| 454 | { |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | if (shouldRunLoopAndIndexingValidation(compileOptions) && |
| 459 | !ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics)) |
| 460 | { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // Fail compilation if precision emulation not supported. |
| 465 | if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision && |
| 466 | !EmulatePrecision::SupportedInLanguage(outputType)) |
| 467 | { |
| 468 | mDiagnostics.globalError("Precision emulation not supported for this output type."); |
| 469 | return false; |
| 470 | } |
| 471 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 472 | // Clamping uniform array bounds needs to happen after validateLimitations pass. |
| 473 | if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS) |
| 474 | { |
| 475 | arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); |
| 476 | } |
| 477 | |
| 478 | if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) && |
| 479 | parseContext.isExtensionEnabled(TExtension::OVR_multiview) && |
| 480 | getShaderType() != GL_COMPUTE_SHADER) |
| 481 | { |
| 482 | DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions, |
| 483 | outputType, &symbolTable); |
| 484 | } |
| 485 | |
| 486 | // This pass might emit short circuits so keep it before the short circuit unfolding |
| 487 | if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS) |
| 488 | RewriteDoWhile(root, &symbolTable); |
| 489 | |
| 490 | if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION) |
| 491 | sh::AddAndTrueToLoopCondition(root); |
| 492 | |
| 493 | if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT) |
| 494 | { |
| 495 | UnfoldShortCircuitAST unfoldShortCircuit; |
| 496 | root->traverse(&unfoldShortCircuit); |
| 497 | unfoldShortCircuit.updateTree(); |
| 498 | } |
| 499 | |
| 500 | if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT) |
| 501 | { |
| 502 | RemovePow(root); |
| 503 | } |
| 504 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 505 | if (compileOptions & SH_REGENERATE_STRUCT_NAMES) |
| 506 | { |
| 507 | RegenerateStructNames gen(&symbolTable, shaderVersion); |
| 508 | root->traverse(&gen); |
| 509 | } |
| 510 | |
| 511 | if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 && |
| 512 | compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 && |
| 513 | IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers)) |
| 514 | { |
| 515 | EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables, |
| 516 | &symbolTable, shaderVersion); |
| 517 | } |
| 518 | |
Olli Etuaho | ae04e1e | 2017-11-27 16:00:39 +0200 | [diff] [blame] | 519 | int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS) |
| 520 | ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor |
| 521 | : 0; |
| 522 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 523 | // Split multi declarations and remove calls to array length(). |
| 524 | // Note that SimplifyLoopConditions needs to be run before any other AST transformations |
| 525 | // that may need to generate new statements from loop conditions or loop expressions. |
Olli Etuaho | ae04e1e | 2017-11-27 16:00:39 +0200 | [diff] [blame] | 526 | SimplifyLoopConditions(root, |
| 527 | IntermNodePatternMatcher::kMultiDeclaration | |
| 528 | IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized, |
| 529 | &getSymbolTable(), getShaderVersion()); |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 530 | |
| 531 | // Note that separate declarations need to be run before other AST transformations that |
| 532 | // generate new statements from expressions. |
| 533 | SeparateDeclarations(root); |
| 534 | |
Olli Etuaho | ae04e1e | 2017-11-27 16:00:39 +0200 | [diff] [blame] | 535 | SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized, |
| 536 | &getSymbolTable(), getShaderVersion()); |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 537 | |
| 538 | RemoveArrayLengthMethod(root); |
| 539 | |
Olli Etuaho | 3d70ca9 | 2017-11-10 16:53:26 +0200 | [diff] [blame] | 540 | RemoveUnreferencedVariables(root, &symbolTable); |
| 541 | |
| 542 | // Built-in function emulation needs to happen after validateLimitations pass. |
| 543 | // TODO(jmadill): Remove global pool allocator. |
| 544 | GetGlobalPoolAllocator()->lock(); |
| 545 | initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions); |
| 546 | GetGlobalPoolAllocator()->unlock(); |
| 547 | builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root); |
| 548 | |
| 549 | if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS) |
| 550 | { |
| 551 | ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable); |
| 552 | } |
| 553 | |
| 554 | if (shouldCollectVariables(compileOptions)) |
| 555 | { |
| 556 | ASSERT(!variablesCollected); |
| 557 | CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings, |
| 558 | &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks, |
| 559 | hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior); |
| 560 | collectInterfaceBlocks(); |
| 561 | variablesCollected = true; |
| 562 | if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS) |
| 563 | { |
| 564 | useAllMembersInUnusedStandardAndSharedBlocks(root); |
| 565 | } |
| 566 | if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) |
| 567 | { |
| 568 | // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec |
| 569 | // Appendix A, section 7, the shader does not use too many uniforms. |
| 570 | if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms)) |
| 571 | { |
| 572 | mDiagnostics.globalError("too many uniforms"); |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | if (compileOptions & SH_INIT_OUTPUT_VARIABLES) |
| 577 | { |
| 578 | initializeOutputVariables(root); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Removing invariant declarations must be done after collecting variables. |
| 583 | // Otherwise, built-in invariant declarations don't apply. |
| 584 | if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions)) |
| 585 | { |
| 586 | RemoveInvariantDeclaration(root); |
| 587 | } |
| 588 | |
| 589 | // gl_Position is always written in compatibility output mode. |
| 590 | // It may have been already initialized among other output variables, in that case we don't |
| 591 | // need to initialize it twice. |
| 592 | if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized && |
| 593 | ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT))) |
| 594 | { |
| 595 | initializeGLPosition(root); |
| 596 | mGLPositionInitialized = true; |
| 597 | } |
| 598 | |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 599 | // DeferGlobalInitializers needs to be run before other AST transformations that generate new |
| 600 | // statements from expressions. But it's fine to run DeferGlobalInitializers after the above |
| 601 | // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST |
| 602 | // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00. |
| 603 | bool initializeLocalsAndGlobals = |
| 604 | (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType()); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 605 | bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES); |
| 606 | DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize, &symbolTable); |
| 607 | |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 608 | if (initializeLocalsAndGlobals) |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 609 | { |
| 610 | // Initialize uninitialized local variables. |
| 611 | // In some cases initializing can generate extra statements in the parent block, such as |
| 612 | // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case |
| 613 | // we need to first simplify loop conditions. We've already separated declarations |
| 614 | // earlier, which is also required. If we don't follow the Appendix A limitations, loop |
| 615 | // init statements can declare arrays or nameless structs and have multiple |
| 616 | // declarations. |
| 617 | |
| 618 | if (!shouldRunLoopAndIndexingValidation(compileOptions)) |
| 619 | { |
| 620 | SimplifyLoopConditions(root, |
| 621 | IntermNodePatternMatcher::kArrayDeclaration | |
| 622 | IntermNodePatternMatcher::kNamelessStructDeclaration, |
| 623 | &getSymbolTable(), getShaderVersion()); |
| 624 | } |
Olli Etuaho | 661fc48 | 2017-10-16 12:17:05 +0300 | [diff] [blame] | 625 | |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 626 | InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize, |
| 627 | &getSymbolTable()); |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE)) |
| 631 | { |
| 632 | ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable()); |
| 633 | } |
| 634 | |
Olli Etuaho | 661fc48 | 2017-10-16 12:17:05 +0300 | [diff] [blame] | 635 | if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC) |
| 636 | { |
| 637 | VectorizeVectorScalarArithmetic(root, &getSymbolTable()); |
| 638 | } |
| 639 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 640 | return true; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 643 | bool TCompiler::compile(const char *const shaderStrings[], |
| 644 | size_t numStrings, |
| 645 | ShCompileOptions compileOptionsIn) |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 646 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 647 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 648 | DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn); |
| 649 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 650 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 651 | if (numStrings == 0) |
| 652 | return true; |
| 653 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 654 | ShCompileOptions compileOptions = compileOptionsIn; |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 655 | |
| 656 | // Apply key workarounds. |
| 657 | if (shouldFlattenPragmaStdglInvariantAll()) |
| 658 | { |
| 659 | // This should be harmless to do in all cases, but for the moment, do it only conditionally. |
| 660 | compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL; |
| 661 | } |
| 662 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 663 | TScopedPoolAllocator scopedAlloc(&allocator); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 664 | TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 665 | |
| 666 | if (root) |
| 667 | { |
| 668 | if (compileOptions & SH_INTERMEDIATE_TREE) |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 669 | OutputTree(root, infoSink.info); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 670 | |
| 671 | if (compileOptions & SH_OBJECT_CODE) |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 672 | { |
| 673 | PerformanceDiagnostics perfDiagnostics(&mDiagnostics); |
| 674 | translate(root, compileOptions, &perfDiagnostics); |
| 675 | } |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 676 | |
| 677 | // The IntermNode tree doesn't need to be deleted here, since the |
| 678 | // memory will be freed in a big chunk by the PoolAllocator. |
| 679 | return true; |
| 680 | } |
| 681 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 684 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 685 | { |
Olli Etuaho | 28cb036 | 2016-11-22 15:42:37 +0000 | [diff] [blame] | 686 | if (resources.MaxDrawBuffers < 1) |
| 687 | { |
| 688 | return false; |
| 689 | } |
| 690 | if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1) |
| 691 | { |
| 692 | return false; |
| 693 | } |
| 694 | |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 695 | compileResources = resources; |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 696 | setResourceString(); |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 697 | |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 698 | ASSERT(symbolTable.isEmpty()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 699 | symbolTable.push(); // COMMON_BUILTINS |
| 700 | symbolTable.push(); // ESSL1_BUILTINS |
| 701 | symbolTable.push(); // ESSL3_BUILTINS |
| 702 | symbolTable.push(); // ESSL3_1_BUILTINS |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 703 | symbolTable.push(); // GLSL_BUILTINS |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 704 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 705 | switch (shaderType) |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 706 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 707 | case GL_FRAGMENT_SHADER: |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 708 | symbolTable.setDefaultPrecision(EbtInt, EbpMedium); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 709 | break; |
| 710 | case GL_VERTEX_SHADER: |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 711 | case GL_COMPUTE_SHADER: |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 712 | case GL_GEOMETRY_SHADER_OES: |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 713 | symbolTable.setDefaultPrecision(EbtInt, EbpHigh); |
| 714 | symbolTable.setDefaultPrecision(EbtFloat, EbpHigh); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 715 | break; |
| 716 | default: |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 717 | UNREACHABLE(); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 718 | } |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 719 | // Set defaults for sampler types that have default precision, even those that are |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 720 | // only available if an extension exists. |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 721 | // New sampler types in ESSL3 don't have default precision. ESSL1 types do. |
| 722 | initSamplerDefaultPrecision(EbtSampler2D); |
| 723 | initSamplerDefaultPrecision(EbtSamplerCube); |
| 724 | // SamplerExternalOES is specified in the extension to have default precision. |
| 725 | initSamplerDefaultPrecision(EbtSamplerExternalOES); |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 726 | // SamplerExternal2DY2YEXT is specified in the extension to have default precision. |
| 727 | initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT); |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 728 | // It isn't specified whether Sampler2DRect has default precision. |
| 729 | initSamplerDefaultPrecision(EbtSampler2DRect); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 730 | |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 731 | symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh); |
jchen10 | 4cdac9e | 2017-05-08 11:01:20 +0800 | [diff] [blame] | 732 | |
Jamie Madill | 1b45214 | 2013-07-12 14:51:11 -0400 | [diff] [blame] | 733 | InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 734 | |
| 735 | IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable); |
| 736 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 737 | symbolTable.markBuiltInInitializationFinished(); |
| 738 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 739 | return true; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 742 | void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType) |
| 743 | { |
| 744 | ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd); |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 745 | symbolTable.setDefaultPrecision(samplerType, EbpLow); |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 748 | void TCompiler::setResourceString() |
| 749 | { |
| 750 | std::ostringstream strstream; |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 751 | |
| 752 | // clang-format off |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 753 | strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 754 | << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors |
| 755 | << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors |
| 756 | << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits |
| 757 | << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits |
| 758 | << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits |
| 759 | << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors |
| 760 | << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers |
| 761 | << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives |
| 762 | << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external |
| 763 | << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3 |
| 764 | << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external |
| 765 | << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle |
| 766 | << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers |
| 767 | << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh |
| 768 | << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity |
| 769 | << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth |
| 770 | << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters |
| 771 | << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended |
| 772 | << ":EXT_frag_depth:" << compileResources.EXT_frag_depth |
| 773 | << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod |
| 774 | << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch |
| 775 | << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch |
| 776 | << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch |
Martin Radev | 318f9aa | 2017-05-17 17:47:28 +0300 | [diff] [blame] | 777 | << ":OVR_multiview:" << compileResources.OVR_multiview |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 778 | << ":EXT_YUV_target:" << compileResources.EXT_YUV_target |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 779 | << ":OES_geometry_shader:" << compileResources.OES_geometry_shader |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 780 | << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors |
| 781 | << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors |
| 782 | << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset |
| 783 | << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset |
| 784 | << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers |
Martin Radev | 318f9aa | 2017-05-17 17:47:28 +0300 | [diff] [blame] | 785 | << ":MaxViewsOVR:" << compileResources.MaxViewsOVR |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 786 | << ":NV_draw_buffers:" << compileResources.NV_draw_buffers |
| 787 | << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision |
Martin Radev | 84aa2dc | 2017-09-11 15:51:02 +0300 | [diff] [blame] | 788 | << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset |
| 789 | << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 790 | << ":MaxImageUnits:" << compileResources.MaxImageUnits |
| 791 | << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms |
| 792 | << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms |
| 793 | << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms |
| 794 | << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms |
| 795 | << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources |
| 796 | << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0] |
| 797 | << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1] |
| 798 | << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2] |
| 799 | << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0] |
| 800 | << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1] |
| 801 | << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2] |
| 802 | << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents |
| 803 | << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits |
| 804 | << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters |
| 805 | << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers |
| 806 | << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters |
| 807 | << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters |
| 808 | << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters |
| 809 | << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings |
| 810 | << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers |
| 811 | << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers |
| 812 | << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 813 | << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 814 | << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents |
Jiawei Shao | d27f5c8 | 2017-08-23 09:38:08 +0800 | [diff] [blame] | 815 | << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks |
| 816 | << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents |
| 817 | << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents |
| 818 | << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices |
| 819 | << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents |
| 820 | << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits |
| 821 | << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers |
| 822 | << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters |
| 823 | << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks |
| 824 | << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations |
| 825 | << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms; |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 826 | // clang-format on |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 827 | |
| 828 | builtInResourcesString = strstream.str(); |
| 829 | } |
| 830 | |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 831 | void TCompiler::collectInterfaceBlocks() |
| 832 | { |
| 833 | ASSERT(interfaceBlocks.empty()); |
Jiawei Shao | d8105a0 | 2017-08-08 09:54:36 +0800 | [diff] [blame] | 834 | interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size()); |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 835 | interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end()); |
| 836 | interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(), |
| 837 | shaderStorageBlocks.end()); |
Jiawei Shao | d8105a0 | 2017-08-08 09:54:36 +0800 | [diff] [blame] | 838 | interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end()); |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 839 | } |
| 840 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 841 | void TCompiler::clearResults() |
| 842 | { |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 843 | arrayBoundsClamper.Cleanup(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 844 | infoSink.info.erase(); |
| 845 | infoSink.obj.erase(); |
| 846 | infoSink.debug.erase(); |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 847 | mDiagnostics.resetErrorCount(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 848 | |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 849 | attributes.clear(); |
| 850 | outputVariables.clear(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 851 | uniforms.clear(); |
Jiawei-Shao | df7d7c9 | 2017-07-31 09:34:04 +0800 | [diff] [blame] | 852 | inputVaryings.clear(); |
| 853 | outputVaryings.clear(); |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 854 | interfaceBlocks.clear(); |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 855 | uniformBlocks.clear(); |
| 856 | shaderStorageBlocks.clear(); |
Jiawei Shao | d8105a0 | 2017-08-08 09:54:36 +0800 | [diff] [blame] | 857 | inBlocks.clear(); |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 858 | variablesCollected = false; |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 859 | mGLPositionInitialized = false; |
zmo@google.com | a3b4ab4 | 2011-09-16 00:53:26 +0000 | [diff] [blame] | 860 | |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 861 | mNumViews = -1; |
| 862 | |
Shao | b5cc119 | 2017-07-06 10:47:20 +0800 | [diff] [blame] | 863 | mGeometryShaderInputPrimitiveType = EptUndefined; |
| 864 | mGeometryShaderOutputPrimitiveType = EptUndefined; |
| 865 | mGeometryShaderInvocations = 0; |
| 866 | mGeometryShaderMaxVertices = -1; |
| 867 | |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 868 | builtInFunctionEmulator.cleanup(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 869 | |
| 870 | nameMap.clear(); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 871 | |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 872 | mSourcePath = nullptr; |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 873 | |
| 874 | symbolTable.clearCompilationResults(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 877 | bool TCompiler::initCallDag(TIntermNode *root) |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 878 | { |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 879 | mCallDag.clear(); |
| 880 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 881 | switch (mCallDag.init(root, &mDiagnostics)) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 882 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 883 | case CallDAG::INITDAG_SUCCESS: |
| 884 | return true; |
| 885 | case CallDAG::INITDAG_RECURSION: |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 886 | case CallDAG::INITDAG_UNDEFINED: |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 887 | // Error message has already been written out. |
| 888 | ASSERT(mDiagnostics.numErrors() > 0); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 889 | return false; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | UNREACHABLE(); |
| 893 | return true; |
| 894 | } |
| 895 | |
| 896 | bool TCompiler::checkCallDepth() |
| 897 | { |
| 898 | std::vector<int> depths(mCallDag.size()); |
| 899 | |
| 900 | for (size_t i = 0; i < mCallDag.size(); i++) |
| 901 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 902 | int depth = 0; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 903 | auto &record = mCallDag.getRecordFromIndex(i); |
| 904 | |
| 905 | for (auto &calleeIndex : record.callees) |
| 906 | { |
| 907 | depth = std::max(depth, depths[calleeIndex] + 1); |
| 908 | } |
| 909 | |
| 910 | depths[i] = depth; |
| 911 | |
| 912 | if (depth >= maxCallStackDepth) |
| 913 | { |
| 914 | // Trace back the function chain to have a meaningful info log. |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 915 | std::stringstream errorStream; |
| 916 | errorStream << "Call stack too deep (larger than " << maxCallStackDepth |
| 917 | << ") with the following call chain: " << record.name; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 918 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 919 | int currentFunction = static_cast<int>(i); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 920 | int currentDepth = depth; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 921 | |
| 922 | while (currentFunction != -1) |
| 923 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 924 | errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 925 | |
| 926 | int nextFunction = -1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 927 | for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 928 | { |
| 929 | if (depths[calleeIndex] == currentDepth - 1) |
| 930 | { |
| 931 | currentDepth--; |
| 932 | nextFunction = calleeIndex; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | currentFunction = nextFunction; |
| 937 | } |
| 938 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 939 | std::string errorStr = errorStream.str(); |
| 940 | mDiagnostics.globalError(errorStr.c_str()); |
| 941 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 942 | return false; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return true; |
| 947 | } |
| 948 | |
| 949 | bool TCompiler::tagUsedFunctions() |
| 950 | { |
| 951 | // Search from main, starting from the end of the DAG as it usually is the root. |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 952 | for (size_t i = mCallDag.size(); i-- > 0;) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 953 | { |
Olli Etuaho | ec9232b | 2017-03-27 17:01:37 +0300 | [diff] [blame] | 954 | if (mCallDag.getRecordFromIndex(i).name == "main") |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 955 | { |
| 956 | internalTagUsedFunction(i); |
| 957 | return true; |
| 958 | } |
| 959 | } |
| 960 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 961 | mDiagnostics.globalError("Missing main()"); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
| 965 | void TCompiler::internalTagUsedFunction(size_t index) |
| 966 | { |
| 967 | if (functionMetadata[index].used) |
| 968 | { |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | functionMetadata[index].used = true; |
| 973 | |
| 974 | for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees) |
| 975 | { |
| 976 | internalTagUsedFunction(calleeIndex); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 980 | // A predicate for the stl that returns if a top-level node is unused |
| 981 | class TCompiler::UnusedPredicate |
| 982 | { |
| 983 | public: |
| 984 | UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas) |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 985 | : mCallDag(callDag), mMetadatas(metadatas) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 986 | { |
| 987 | } |
| 988 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 989 | bool operator()(TIntermNode *node) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 990 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 991 | const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode(); |
| 992 | const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition(); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 993 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 994 | const TFunctionSymbolInfo *functionInfo = nullptr; |
| 995 | |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 996 | if (asFunctionDefinition) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 997 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 998 | functionInfo = asFunctionDefinition->getFunctionSymbolInfo(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 999 | } |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 1000 | else if (asFunctionPrototype) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1001 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 1002 | functionInfo = asFunctionPrototype->getFunctionSymbolInfo(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1003 | } |
| 1004 | if (functionInfo == nullptr) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1005 | { |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1009 | size_t callDagIndex = mCallDag->findIndex(functionInfo); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1010 | if (callDagIndex == CallDAG::InvalidIndex) |
| 1011 | { |
| 1012 | // This happens only for unimplemented prototypes which are thus unused |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 1013 | ASSERT(asFunctionPrototype); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | ASSERT(callDagIndex < mMetadatas->size()); |
| 1018 | return !(*mMetadatas)[callDagIndex].used; |
| 1019 | } |
| 1020 | |
| 1021 | private: |
| 1022 | const CallDAG *mCallDag; |
| 1023 | const std::vector<FunctionMetadata> *mMetadatas; |
| 1024 | }; |
| 1025 | |
Olli Etuaho | d10cf69 | 2017-11-02 11:06:14 +0200 | [diff] [blame] | 1026 | void TCompiler::pruneUnusedFunctions(TIntermBlock *root) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1027 | { |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1028 | UnusedPredicate isUnused(&mCallDag, &functionMetadata); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1029 | TIntermSequence *sequence = root->getSequence(); |
Corentin Wallez | b081e78 | 2015-07-20 05:40:04 -0700 | [diff] [blame] | 1030 | |
| 1031 | if (!sequence->empty()) |
| 1032 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1033 | sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), |
| 1034 | sequence->end()); |
Corentin Wallez | b081e78 | 2015-07-20 05:40:04 -0700 | [diff] [blame] | 1035 | } |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Olli Etuaho | b427920 | 2017-05-18 15:08:24 +0300 | [diff] [blame] | 1038 | bool TCompiler::limitExpressionComplexity(TIntermBlock *root) |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 1039 | { |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 1040 | if (!IsASTDepthBelowLimit(root, maxExpressionComplexity)) |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 1041 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 1042 | mDiagnostics.globalError("Expression too complex."); |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
| 1045 | |
Olli Etuaho | b427920 | 2017-05-18 15:08:24 +0300 | [diff] [blame] | 1046 | if (!ValidateMaxParameters(root, maxFunctionParameters)) |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 1047 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 1048 | mDiagnostics.globalError("Function has too many parameters."); |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 1049 | return false; |
| 1050 | } |
| 1051 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 1052 | return true; |
| 1053 | } |
| 1054 | |
Corentin Wallez | 1df1602 | 2016-10-27 08:16:56 -0400 | [diff] [blame] | 1055 | bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions) |
| 1056 | { |
| 1057 | return (compileOptions & SH_VARIABLES) != 0; |
| 1058 | } |
| 1059 | |
| 1060 | bool TCompiler::wereVariablesCollected() const |
| 1061 | { |
| 1062 | return variablesCollected; |
| 1063 | } |
| 1064 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 1065 | void TCompiler::initializeGLPosition(TIntermBlock *root) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1066 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 1067 | InitVariableList list; |
Olli Etuaho | e7c2857 | 2017-10-23 16:29:33 +0300 | [diff] [blame] | 1068 | sh::ShaderVariable var(GL_FLOAT_VEC4); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 1069 | var.name = "gl_Position"; |
| 1070 | list.push_back(var); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 1071 | InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 1074 | void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root) |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 1075 | { |
| 1076 | sh::InterfaceBlockList list; |
| 1077 | |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 1078 | for (auto block : uniformBlocks) |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 1079 | { |
| 1080 | if (!block.staticUse && |
Qin Jiajia | ca68d98 | 2017-09-18 16:41:56 +0800 | [diff] [blame] | 1081 | (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED)) |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 1082 | { |
| 1083 | list.push_back(block); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 1087 | sh::UseInterfaceBlockFields(root, list, symbolTable); |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 1090 | void TCompiler::initializeOutputVariables(TIntermBlock *root) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1091 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 1092 | InitVariableList list; |
Jiawei Shao | d27f5c8 | 2017-08-23 09:38:08 +0800 | [diff] [blame] | 1093 | if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1094 | { |
Jiawei-Shao | df7d7c9 | 2017-07-31 09:34:04 +0800 | [diff] [blame] | 1095 | for (auto var : outputVaryings) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1096 | { |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 1097 | list.push_back(var); |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 1098 | if (var.name == "gl_Position") |
| 1099 | { |
| 1100 | ASSERT(!mGLPositionInitialized); |
| 1101 | mGLPositionInitialized = true; |
| 1102 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1103 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1104 | } |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 1105 | else |
| 1106 | { |
| 1107 | ASSERT(shaderType == GL_FRAGMENT_SHADER); |
| 1108 | for (auto var : outputVariables) |
| 1109 | { |
| 1110 | list.push_back(var); |
| 1111 | } |
| 1112 | } |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 1113 | InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1116 | const TExtensionBehavior &TCompiler::getExtensionBehavior() const |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 1117 | { |
| 1118 | return extensionBehavior; |
| 1119 | } |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 1120 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 1121 | const char *TCompiler::getSourcePath() const |
| 1122 | { |
| 1123 | return mSourcePath; |
| 1124 | } |
| 1125 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1126 | const ShBuiltInResources &TCompiler::getResources() const |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 1127 | { |
| 1128 | return compileResources; |
| 1129 | } |
| 1130 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1131 | const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 1132 | { |
| 1133 | return arrayBoundsClamper; |
| 1134 | } |
| 1135 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 1136 | ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const |
| 1137 | { |
| 1138 | return clampingStrategy; |
| 1139 | } |
| 1140 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1141 | const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 1142 | { |
| 1143 | return builtInFunctionEmulator; |
| 1144 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1145 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 1146 | void TCompiler::writePragma(ShCompileOptions compileOptions) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1147 | { |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 1148 | if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL)) |
| 1149 | { |
| 1150 | TInfoSinkBase &sink = infoSink.obj; |
| 1151 | if (mPragma.stdgl.invariantAll) |
| 1152 | sink << "#pragma STDGL invariant(all)\n"; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | bool TCompiler::isVaryingDefined(const char *varyingName) |
| 1157 | { |
| 1158 | ASSERT(variablesCollected); |
Jiawei-Shao | df7d7c9 | 2017-07-31 09:34:04 +0800 | [diff] [blame] | 1159 | for (size_t ii = 0; ii < inputVaryings.size(); ++ii) |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 1160 | { |
Jiawei-Shao | df7d7c9 | 2017-07-31 09:34:04 +0800 | [diff] [blame] | 1161 | if (inputVaryings[ii].name == varyingName) |
| 1162 | { |
| 1163 | return true; |
| 1164 | } |
| 1165 | } |
| 1166 | for (size_t ii = 0; ii < outputVaryings.size(); ++ii) |
| 1167 | { |
| 1168 | if (outputVaryings[ii].name == varyingName) |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 1169 | { |
| 1170 | return true; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return false; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1175 | } |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 1176 | |
| 1177 | } // namespace sh |