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