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