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