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