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; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 69 | |
| 70 | MaxCallStackDepth = UINT_MAX; |
| 71 | } |
| 72 | |
Nicolas Capens | 08ca3c6 | 2015-02-13 16:06:45 -0500 | [diff] [blame] | 73 | TCompiler::TCompiler(GLenum type) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 74 | : shaderType(type), |
| 75 | maxCallStackDepth(UINT_MAX) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 76 | { |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 77 | allocator.push(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 78 | SetGlobalPoolAllocator(&allocator); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TCompiler::~TCompiler() |
| 82 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 83 | SetGlobalPoolAllocator(nullptr); |
| 84 | allocator.popAll(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool TCompiler::Init(const ShBuiltInResources& resources) |
| 88 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 89 | shaderVersion = 100; |
| 90 | maxCallStackDepth = resources.MaxCallStackDepth; |
| 91 | TScopedPoolAllocator scopedAlloc(&allocator, false); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 92 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 93 | // Generate built-in symbol table. |
| 94 | if (!InitBuiltInSymbolTable(resources)) |
| 95 | return false; |
| 96 | InitExtensionBehavior(resources, extensionBehavior); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 97 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 98 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool TCompiler::compile(const char* const shaderStrings[], |
| 102 | const int numStrings, |
| 103 | int compileOptions) |
| 104 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 105 | TScopedPoolAllocator scopedAlloc(&allocator, true); |
| 106 | clearResults(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 107 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 108 | if (numStrings == 0) |
| 109 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 110 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 111 | // First string is path of source file if flag is set. The actual source follows. |
| 112 | const char* sourcePath = nullptr; |
| 113 | int firstSource = 0; |
| 114 | if (compileOptions & SH_SOURCE_PATH) |
| 115 | { |
| 116 | sourcePath = shaderStrings[0]; |
| 117 | ++firstSource; |
| 118 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 119 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 120 | TIntermediate intermediate(infoSink); |
| 121 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
| 122 | shaderType, compileOptions, true, |
| 123 | sourcePath, infoSink); |
| 124 | SetGlobalParseContext(&parseContext); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 125 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 126 | // We preserve symbols at the built-in level from compile-to-compile. |
| 127 | // Start pushing the user-defined symbols at global level. |
| 128 | symbolTable.push(); |
| 129 | if (!symbolTable.atGlobalLevel()) |
| 130 | infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 131 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 132 | // Parse shader. |
| 133 | bool success = |
| 134 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) && |
| 135 | (parseContext.getTreeRoot() != nullptr); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 136 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 137 | shaderVersion = parseContext.getShaderVersion(); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 138 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 139 | if (success) { |
| 140 | TIntermNode* root = parseContext.getTreeRoot(); |
| 141 | success = intermediate.postProcess(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 142 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 143 | if (success) |
| 144 | success = validateCallDepth(root, infoSink); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 145 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 146 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 147 | success = validateLimitations(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 148 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 149 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
| 150 | intermediate.outputTree(root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 151 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 152 | if (success && (compileOptions & SH_OBJECT_CODE)) |
| 153 | success = translate(root); |
| 154 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 155 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 156 | // Ensure symbol table is returned to the built-in level, |
| 157 | // throwing away all but the built-ins. |
| 158 | while (!symbolTable.atBuiltInLevel()) |
| 159 | symbolTable.pop(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 160 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 161 | return success; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 162 | } |
| 163 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 164 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 165 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 166 | assert(symbolTable.isEmpty()); |
| 167 | symbolTable.push(); // COMMON_BUILTINS |
| 168 | symbolTable.push(); // ESSL1_BUILTINS |
| 169 | symbolTable.push(); // ESSL3_BUILTINS |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 170 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 171 | TPublicType integer; |
| 172 | integer.type = EbtInt; |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 173 | integer.primarySize = 1; |
| 174 | integer.secondarySize = 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 175 | integer.array = false; |
| 176 | |
| 177 | TPublicType floatingPoint; |
| 178 | floatingPoint.type = EbtFloat; |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 179 | floatingPoint.primarySize = 1; |
| 180 | floatingPoint.secondarySize = 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 181 | floatingPoint.array = false; |
| 182 | |
| 183 | switch(shaderType) |
| 184 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 185 | case GL_FRAGMENT_SHADER: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 186 | symbolTable.setDefaultPrecision(integer, EbpMedium); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 187 | break; |
| 188 | case GL_VERTEX_SHADER: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 189 | symbolTable.setDefaultPrecision(integer, EbpHigh); |
| 190 | symbolTable.setDefaultPrecision(floatingPoint, EbpHigh); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 191 | break; |
| 192 | default: assert(false && "Language not supported"); |
| 193 | } |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 194 | |
| 195 | InsertBuiltInFunctions(shaderType, resources, symbolTable); |
| 196 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 197 | IdentifyBuiltIns(shaderType, resources, symbolTable); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 198 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 199 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void TCompiler::clearResults() |
| 203 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 204 | infoSink.info.erase(); |
| 205 | infoSink.obj.erase(); |
| 206 | infoSink.debug.erase(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 207 | } |
| 208 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 209 | bool TCompiler::validateCallDepth(TIntermNode *root, TInfoSink &infoSink) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 210 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 211 | AnalyzeCallDepth validator(root); |
| 212 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 213 | unsigned int depth = validator.analyzeCallDepth(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 214 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 215 | if(depth == 0) |
| 216 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 217 | infoSink.info.prefix(EPrefixError); |
| 218 | infoSink.info << "Missing main()"; |
| 219 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 220 | } |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 221 | else if(depth == UINT_MAX) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 222 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 223 | infoSink.info.prefix(EPrefixError); |
| 224 | infoSink.info << "Function recursion detected"; |
| 225 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 226 | } |
| 227 | else if(depth > maxCallStackDepth) |
| 228 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 229 | infoSink.info.prefix(EPrefixError); |
| 230 | infoSink.info << "Function call stack too deep"; |
| 231 | return false; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 234 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | bool TCompiler::validateLimitations(TIntermNode* root) { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 238 | ValidateLimitations validate(shaderType, infoSink.info); |
| 239 | root->traverse(&validate); |
| 240 | return validate.numErrors() == 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 241 | } |
| 242 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 243 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 244 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 245 | return extensionBehavior; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 246 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 247 | |
| 248 | bool InitCompilerGlobals() |
| 249 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 250 | if(!InitializePoolIndex()) |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 251 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 252 | assert(0 && "InitCompilerGlobals(): Failed to initalize global pool"); |
| 253 | return false; |
| 254 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 255 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 256 | if(!InitializeParseContextIndex()) |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 257 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 258 | assert(0 && "InitCompilerGlobals(): Failed to initalize parse context"); |
| 259 | return false; |
| 260 | } |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 261 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 262 | return true; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void FreeCompilerGlobals() |
| 266 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 267 | FreeParseContextIndex(); |
| 268 | FreePoolIndex(); |
Nicolas Capens | b28964b | 2015-02-10 15:23:06 -0500 | [diff] [blame] | 269 | } |