alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #include "compiler/Initialize.h" |
| 8 | #include "compiler/ParseHelper.h" |
| 9 | #include "compiler/ShHandle.h" |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 10 | #include "compiler/ValidateLimitations.h" |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 11 | #include "compiler/MapLongVariableNames.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 12 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 13 | namespace { |
| 14 | bool InitializeSymbolTable( |
| 15 | const TBuiltInStrings& builtInStrings, |
| 16 | ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources, |
| 17 | TInfoSink& infoSink, TSymbolTable& symbolTable) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 18 | { |
| 19 | TIntermediate intermediate(infoSink); |
| 20 | TExtensionBehavior extBehavior; |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 21 | // The builtins deliberately don't specify precisions for the function |
| 22 | // arguments and return types. For that reason we don't try to check them. |
| 23 | TParseContext parseContext(symbolTable, extBehavior, intermediate, type, spec, 0, false, NULL, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 24 | |
| 25 | GlobalParseContext = &parseContext; |
| 26 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 27 | assert(symbolTable.isEmpty()); |
| 28 | // |
| 29 | // Parse the built-ins. This should only happen once per |
| 30 | // language symbol table. |
| 31 | // |
| 32 | // Push the symbol table to give it an initial scope. This |
| 33 | // push should not have a corresponding pop, so that built-ins |
| 34 | // are preserved, and the test for an empty table fails. |
| 35 | // |
| 36 | symbolTable.push(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 37 | |
| 38 | for (TBuiltInStrings::const_iterator i = builtInStrings.begin(); i != builtInStrings.end(); ++i) |
| 39 | { |
alokp@chromium.org | 570bfc7 | 2010-09-24 17:19:25 +0000 | [diff] [blame] | 40 | const char* builtInShaders = i->c_str(); |
| 41 | int builtInLengths = static_cast<int>(i->size()); |
| 42 | if (builtInLengths <= 0) |
| 43 | continue; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 44 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 45 | if (PaParseStrings(1, &builtInShaders, &builtInLengths, &parseContext) != 0) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 46 | { |
| 47 | infoSink.info.message(EPrefixInternalError, "Unable to parse built-ins"); |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 52 | IdentifyBuiltIns(type, spec, resources, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 53 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 57 | class TScopedPoolAllocator { |
| 58 | public: |
| 59 | TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop) |
| 60 | : mAllocator(allocator), mPushPopAllocator(pushPop) { |
| 61 | if (mPushPopAllocator) mAllocator->push(); |
| 62 | SetGlobalPoolAllocator(mAllocator); |
| 63 | } |
| 64 | ~TScopedPoolAllocator() { |
| 65 | SetGlobalPoolAllocator(NULL); |
| 66 | if (mPushPopAllocator) mAllocator->pop(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | TPoolAllocator* mAllocator; |
| 71 | bool mPushPopAllocator; |
| 72 | }; |
| 73 | } // namespace |
| 74 | |
| 75 | TShHandleBase::TShHandleBase() { |
| 76 | allocator.push(); |
| 77 | SetGlobalPoolAllocator(&allocator); |
| 78 | } |
| 79 | |
| 80 | TShHandleBase::~TShHandleBase() { |
| 81 | SetGlobalPoolAllocator(NULL); |
| 82 | allocator.popAll(); |
| 83 | } |
| 84 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 85 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 86 | : shaderType(type), |
| 87 | shaderSpec(spec) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | TCompiler::~TCompiler() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | bool TCompiler::Init(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 96 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 97 | TScopedPoolAllocator scopedAlloc(&allocator, false); |
| 98 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 99 | // Generate built-in symbol table. |
| 100 | if (!InitBuiltInSymbolTable(resources)) |
| 101 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 102 | InitExtensionBehavior(resources, extensionBehavior); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 103 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool TCompiler::compile(const char* const shaderStrings[], |
| 108 | const int numStrings, |
| 109 | int compileOptions) |
| 110 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 111 | TScopedPoolAllocator scopedAlloc(&allocator, true); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 112 | clearResults(); |
| 113 | |
| 114 | if (numStrings == 0) |
| 115 | return true; |
| 116 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 117 | // If compiling for WebGL, validate loop and indexing as well. |
| 118 | if (shaderSpec == SH_WEBGL_SPEC) |
| 119 | compileOptions |= SH_VALIDATE_LOOP_INDEXING; |
alokp@chromium.org | 1f29954 | 2010-11-12 15:50:23 +0000 | [diff] [blame] | 120 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 121 | // First string is path of source file if flag is set. The actual source follows. |
| 122 | const char* sourcePath = NULL; |
| 123 | int firstSource = 0; |
| 124 | if (compileOptions & SH_SOURCE_PATH) |
| 125 | { |
| 126 | sourcePath = shaderStrings[0]; |
| 127 | ++firstSource; |
| 128 | } |
| 129 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 130 | TIntermediate intermediate(infoSink); |
| 131 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 132 | shaderType, shaderSpec, compileOptions, true, |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 133 | sourcePath, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 134 | GlobalParseContext = &parseContext; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 135 | |
| 136 | // We preserve symbols at the built-in level from compile-to-compile. |
| 137 | // Start pushing the user-defined symbols at global level. |
| 138 | symbolTable.push(); |
| 139 | if (!symbolTable.atGlobalLevel()) |
| 140 | infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); |
| 141 | |
| 142 | // Parse shader. |
| 143 | bool success = |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 144 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) && |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 145 | (parseContext.treeRoot != NULL); |
| 146 | if (success) { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 147 | TIntermNode* root = parseContext.treeRoot; |
| 148 | success = intermediate.postProcess(root); |
| 149 | |
| 150 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 151 | success = validateLimitations(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 152 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 153 | // Call mapLongVariableNames() before collectAttribsUniforms() so in |
| 154 | // collectAttribsUniforms() we already have the mapped symbol names and |
| 155 | // we could composite mapped and original variable names. |
| 156 | if (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) |
| 157 | mapLongVariableNames(root); |
| 158 | |
| 159 | if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) |
| 160 | collectAttribsUniforms(root); |
| 161 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 162 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 163 | intermediate.outputTree(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 164 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 165 | if (success && (compileOptions & SH_OBJECT_CODE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 166 | translate(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Cleanup memory. |
| 170 | intermediate.remove(parseContext.treeRoot); |
| 171 | // Ensure symbol table is returned to the built-in level, |
| 172 | // throwing away all but the built-ins. |
| 173 | while (!symbolTable.atBuiltInLevel()) |
| 174 | symbolTable.pop(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 175 | |
| 176 | return success; |
| 177 | } |
| 178 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 179 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 180 | { |
| 181 | TBuiltIns builtIns; |
| 182 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 183 | builtIns.initialize(shaderType, shaderSpec, resources); |
| 184 | return InitializeSymbolTable(builtIns.getBuiltInStrings(), |
| 185 | shaderType, shaderSpec, resources, infoSink, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void TCompiler::clearResults() |
| 189 | { |
| 190 | infoSink.info.erase(); |
| 191 | infoSink.obj.erase(); |
| 192 | infoSink.debug.erase(); |
| 193 | |
| 194 | attribs.clear(); |
| 195 | uniforms.clear(); |
| 196 | } |
| 197 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 198 | bool TCompiler::validateLimitations(TIntermNode* root) { |
| 199 | ValidateLimitations validate(shaderType, infoSink.info); |
| 200 | root->traverse(&validate); |
| 201 | return validate.numErrors() == 0; |
| 202 | } |
| 203 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 204 | void TCompiler::collectAttribsUniforms(TIntermNode* root) |
| 205 | { |
| 206 | CollectAttribsUniforms collect(attribs, uniforms); |
| 207 | root->traverse(&collect); |
| 208 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 209 | |
| 210 | void TCompiler::mapLongVariableNames(TIntermNode* root) |
| 211 | { |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 212 | MapLongVariableNames map(varyingLongNameMap); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 213 | root->traverse(&map); |
| 214 | } |
| 215 | |
| 216 | int TCompiler::getMappedNameMaxLength() const |
| 217 | { |
| 218 | return MAX_IDENTIFIER_NAME_SIZE + 1; |
| 219 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 220 | |
| 221 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 222 | { |
| 223 | return extensionBehavior; |
| 224 | } |