Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 14 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 15 | #include "Compiler.h" |
| 16 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 17 | #include "AnalyzeCallDepth.h" |
| 18 | #include "Initialize.h" |
| 19 | #include "InitializeParseContext.h" |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 20 | #include "InitializeGlobals.h" |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 21 | #include "ParseHelper.h" |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 22 | #include "ValidateLimitations.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 23 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 24 | namespace |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 25 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 26 | class TScopedPoolAllocator { |
| 27 | public: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 28 | TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop) |
| 29 | : mAllocator(allocator), mPushPopAllocator(pushPop) |
| 30 | { |
| 31 | if (mPushPopAllocator) mAllocator->push(); |
| 32 | SetGlobalPoolAllocator(mAllocator); |
| 33 | } |
| 34 | ~TScopedPoolAllocator() |
| 35 | { |
| 36 | SetGlobalPoolAllocator(nullptr); |
| 37 | if (mPushPopAllocator) mAllocator->pop(); |
| 38 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 39 | |
| 40 | private: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 41 | TPoolAllocator* mAllocator; |
| 42 | bool mPushPopAllocator; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 43 | }; |
| 44 | } // namespace |
| 45 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 46 | // |
| 47 | // Initialize built-in resources with minimum expected values. |
| 48 | // |
| 49 | ShBuiltInResources::ShBuiltInResources() |
| 50 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 51 | // Constants. |
| 52 | MaxVertexAttribs = 8; |
| 53 | MaxVertexUniformVectors = 128; |
| 54 | MaxVaryingVectors = 8; |
| 55 | MaxVertexTextureImageUnits = 0; |
| 56 | MaxCombinedTextureImageUnits = 8; |
| 57 | MaxTextureImageUnits = 8; |
| 58 | MaxFragmentUniformVectors = 16; |
| 59 | MaxDrawBuffers = 1; |
| 60 | MaxVertexOutputVectors = 16; |
| 61 | MaxFragmentInputVectors = 15; |
| 62 | MinProgramTexelOffset = -8; |
| 63 | MaxProgramTexelOffset = 7; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 64 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 65 | // Extensions. |
| 66 | OES_standard_derivatives = 0; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 67 | OES_fragment_precision_high = 0; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 68 | OES_EGL_image_external = 0; |
Lingfeng Yang | 4806a9b | 2019-01-19 14:40:08 -0800 | [diff] [blame^] | 69 | OES_EGL_image_external_essl3 = 0; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 70 | |
| 71 | MaxCallStackDepth = UINT_MAX; |
| 72 | } |
| 73 | |
Nicolas Capens | 08ca3c6 | 2015-02-13 16:06:45 -0500 | [diff] [blame] | 74 | TCompiler::TCompiler(GLenum type) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 75 | : shaderType(type), |
| 76 | maxCallStackDepth(UINT_MAX) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 77 | { |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 78 | allocator.push(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 79 | SetGlobalPoolAllocator(&allocator); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TCompiler::~TCompiler() |
| 83 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 84 | SetGlobalPoolAllocator(nullptr); |
| 85 | allocator.popAll(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool TCompiler::Init(const ShBuiltInResources& resources) |
| 89 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 90 | shaderVersion = 100; |
| 91 | maxCallStackDepth = resources.MaxCallStackDepth; |
| 92 | TScopedPoolAllocator scopedAlloc(&allocator, false); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 93 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 94 | // Generate built-in symbol table. |
| 95 | if (!InitBuiltInSymbolTable(resources)) |
| 96 | return false; |
| 97 | InitExtensionBehavior(resources, extensionBehavior); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 98 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 99 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool TCompiler::compile(const char* const shaderStrings[], |
| 103 | const int numStrings, |
| 104 | int compileOptions) |
| 105 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 106 | TScopedPoolAllocator scopedAlloc(&allocator, true); |
| 107 | clearResults(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 108 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 109 | if (numStrings == 0) |
| 110 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 111 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 112 | // First string is path of source file if flag is set. The actual source follows. |
| 113 | const char* sourcePath = nullptr; |
| 114 | int firstSource = 0; |
| 115 | if (compileOptions & SH_SOURCE_PATH) |
| 116 | { |
| 117 | sourcePath = shaderStrings[0]; |
| 118 | ++firstSource; |
| 119 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 120 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 121 | TIntermediate intermediate(infoSink); |
| 122 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
| 123 | shaderType, compileOptions, true, |
| 124 | sourcePath, infoSink); |
| 125 | SetGlobalParseContext(&parseContext); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 126 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 127 | // We preserve symbols at the built-in level from compile-to-compile. |
| 128 | // Start pushing the user-defined symbols at global level. |
| 129 | symbolTable.push(); |
| 130 | if (!symbolTable.atGlobalLevel()) |
| 131 | infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 132 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 133 | // Parse shader. |
| 134 | bool success = |
| 135 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) && |
| 136 | (parseContext.getTreeRoot() != nullptr); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 137 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 138 | shaderVersion = parseContext.getShaderVersion(); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 139 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 140 | if (success) { |
| 141 | TIntermNode* root = parseContext.getTreeRoot(); |
| 142 | success = intermediate.postProcess(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 143 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 144 | if (success) |
| 145 | success = validateCallDepth(root, infoSink); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 146 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 147 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 148 | success = validateLimitations(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 149 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 150 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
| 151 | intermediate.outputTree(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 152 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 153 | if (success && (compileOptions & SH_OBJECT_CODE)) |
| 154 | success = translate(root); |
| 155 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 156 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 157 | // Ensure symbol table is returned to the built-in level, |
| 158 | // throwing away all but the built-ins. |
| 159 | while (!symbolTable.atBuiltInLevel()) |
| 160 | symbolTable.pop(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 161 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 162 | return success; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 163 | } |
| 164 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 165 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 166 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 167 | assert(symbolTable.isEmpty()); |
| 168 | symbolTable.push(); // COMMON_BUILTINS |
| 169 | symbolTable.push(); // ESSL1_BUILTINS |
| 170 | symbolTable.push(); // ESSL3_BUILTINS |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 171 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 172 | TPublicType integer; |
| 173 | integer.type = EbtInt; |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 174 | integer.primarySize = 1; |
| 175 | integer.secondarySize = 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 176 | integer.array = false; |
| 177 | |
| 178 | TPublicType floatingPoint; |
| 179 | floatingPoint.type = EbtFloat; |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 180 | floatingPoint.primarySize = 1; |
| 181 | floatingPoint.secondarySize = 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 182 | floatingPoint.array = false; |
| 183 | |
| 184 | switch(shaderType) |
| 185 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 186 | case GL_FRAGMENT_SHADER: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 187 | symbolTable.setDefaultPrecision(integer, EbpMedium); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 188 | break; |
| 189 | case GL_VERTEX_SHADER: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 190 | symbolTable.setDefaultPrecision(integer, EbpHigh); |
| 191 | symbolTable.setDefaultPrecision(floatingPoint, EbpHigh); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 192 | break; |
| 193 | default: assert(false && "Language not supported"); |
| 194 | } |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 195 | |
| 196 | InsertBuiltInFunctions(shaderType, resources, symbolTable); |
| 197 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 198 | IdentifyBuiltIns(shaderType, resources, symbolTable); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 199 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 200 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void TCompiler::clearResults() |
| 204 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 205 | infoSink.info.erase(); |
| 206 | infoSink.obj.erase(); |
| 207 | infoSink.debug.erase(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 208 | } |
| 209 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 210 | bool TCompiler::validateCallDepth(TIntermNode *root, TInfoSink &infoSink) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 211 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 212 | AnalyzeCallDepth validator(root); |
| 213 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 214 | unsigned int depth = validator.analyzeCallDepth(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 215 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 216 | if(depth == 0) |
| 217 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 218 | infoSink.info.prefix(EPrefixError); |
| 219 | infoSink.info << "Missing main()"; |
| 220 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 221 | } |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 222 | else if(depth == UINT_MAX) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 223 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 224 | infoSink.info.prefix(EPrefixError); |
| 225 | infoSink.info << "Function recursion detected"; |
| 226 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 227 | } |
| 228 | else if(depth > maxCallStackDepth) |
| 229 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 230 | infoSink.info.prefix(EPrefixError); |
Alexis Hetu | 2e4c069 | 2018-01-15 16:16:27 -0500 | [diff] [blame] | 231 | infoSink.info << "Function call stack too deep (depth was "; |
| 232 | infoSink.info << depth; |
| 233 | infoSink.info << " while maximum call stack depth is "; |
| 234 | infoSink.info << maxCallStackDepth; |
| 235 | infoSink.info << ")"; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 236 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 239 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool TCompiler::validateLimitations(TIntermNode* root) { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 243 | ValidateLimitations validate(shaderType, infoSink.info); |
| 244 | root->traverse(&validate); |
| 245 | return validate.numErrors() == 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 246 | } |
| 247 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 248 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 249 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 250 | return extensionBehavior; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 251 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 252 | |
| 253 | bool InitCompilerGlobals() |
| 254 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 255 | if(!InitializePoolIndex()) |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 256 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 257 | assert(0 && "InitCompilerGlobals(): Failed to initalize global pool"); |
| 258 | return false; |
| 259 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 260 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 261 | if(!InitializeParseContextIndex()) |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 262 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 263 | assert(0 && "InitCompilerGlobals(): Failed to initalize parse context"); |
| 264 | return false; |
| 265 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 266 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 267 | return true; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void FreeCompilerGlobals() |
| 271 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 272 | FreeParseContextIndex(); |
| 273 | FreePoolIndex(); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 274 | } |