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