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" |
| 10 | |
| 11 | static bool InitializeSymbolTable( |
| 12 | const TBuiltInStrings& builtInStrings, |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 13 | ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources, |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 14 | TInfoSink& infoSink, TSymbolTable& symbolTable) |
| 15 | { |
| 16 | TIntermediate intermediate(infoSink); |
| 17 | TExtensionBehavior extBehavior; |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 18 | TParseContext parseContext(symbolTable, extBehavior, intermediate, type, spec, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 19 | |
| 20 | GlobalParseContext = &parseContext; |
| 21 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 22 | assert(symbolTable.isEmpty()); |
| 23 | // |
| 24 | // Parse the built-ins. This should only happen once per |
| 25 | // language symbol table. |
| 26 | // |
| 27 | // Push the symbol table to give it an initial scope. This |
| 28 | // push should not have a corresponding pop, so that built-ins |
| 29 | // are preserved, and the test for an empty table fails. |
| 30 | // |
| 31 | symbolTable.push(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 32 | |
| 33 | for (TBuiltInStrings::const_iterator i = builtInStrings.begin(); i != builtInStrings.end(); ++i) |
| 34 | { |
alokp@chromium.org | 570bfc7 | 2010-09-24 17:19:25 +0000 | [diff] [blame] | 35 | const char* builtInShaders = i->c_str(); |
| 36 | int builtInLengths = static_cast<int>(i->size()); |
| 37 | if (builtInLengths <= 0) |
| 38 | continue; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 39 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 40 | if (PaParseStrings(1, &builtInShaders, &builtInLengths, &parseContext) != 0) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 41 | { |
| 42 | infoSink.info.message(EPrefixInternalError, "Unable to parse built-ins"); |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 47 | IdentifyBuiltIns(type, spec, resources, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 48 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 52 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 53 | : shaderType(type), |
| 54 | shaderSpec(spec) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | TCompiler::~TCompiler() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | bool TCompiler::Init(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 63 | { |
| 64 | // Generate built-in symbol table. |
| 65 | if (!InitBuiltInSymbolTable(resources)) |
| 66 | return false; |
| 67 | |
| 68 | InitExtensionBehavior(resources, extensionBehavior); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool TCompiler::compile(const char* const shaderStrings[], |
| 73 | const int numStrings, |
| 74 | int compileOptions) |
| 75 | { |
| 76 | clearResults(); |
| 77 | |
| 78 | if (numStrings == 0) |
| 79 | return true; |
| 80 | |
alokp@chromium.org | 1f29954 | 2010-11-12 15:50:23 +0000 | [diff] [blame^] | 81 | // If compiling for WebGL, validate control-flow and indexing as well. |
| 82 | if (shaderSpec == SH_WEBGL_SPEC) { |
| 83 | compileOptions |= SH_VALIDATE_CONTROL_FLOW | SH_VALIDATE_INDEXING; |
| 84 | } |
| 85 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 86 | TIntermediate intermediate(infoSink); |
| 87 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 88 | shaderType, shaderSpec, infoSink); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 89 | GlobalParseContext = &parseContext; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 90 | |
| 91 | // We preserve symbols at the built-in level from compile-to-compile. |
| 92 | // Start pushing the user-defined symbols at global level. |
| 93 | symbolTable.push(); |
| 94 | if (!symbolTable.atGlobalLevel()) |
| 95 | infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); |
| 96 | |
| 97 | // Parse shader. |
| 98 | bool success = |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 99 | (PaParseStrings(numStrings, shaderStrings, NULL, &parseContext) == 0) && |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 100 | (parseContext.treeRoot != NULL); |
| 101 | if (success) { |
| 102 | success = intermediate.postProcess(parseContext.treeRoot); |
| 103 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 104 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 105 | intermediate.outputTree(parseContext.treeRoot); |
| 106 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 107 | if (success && (compileOptions & SH_OBJECT_CODE)) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 108 | translate(parseContext.treeRoot); |
| 109 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 110 | if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 111 | collectAttribsUniforms(parseContext.treeRoot); |
| 112 | } |
| 113 | |
| 114 | // Cleanup memory. |
| 115 | intermediate.remove(parseContext.treeRoot); |
| 116 | // Ensure symbol table is returned to the built-in level, |
| 117 | // throwing away all but the built-ins. |
| 118 | while (!symbolTable.atBuiltInLevel()) |
| 119 | symbolTable.pop(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 120 | |
| 121 | return success; |
| 122 | } |
| 123 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 124 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 125 | { |
| 126 | TBuiltIns builtIns; |
| 127 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 128 | builtIns.initialize(shaderType, shaderSpec, resources); |
| 129 | return InitializeSymbolTable(builtIns.getBuiltInStrings(), |
| 130 | shaderType, shaderSpec, resources, infoSink, symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void TCompiler::clearResults() |
| 134 | { |
| 135 | infoSink.info.erase(); |
| 136 | infoSink.obj.erase(); |
| 137 | infoSink.debug.erase(); |
| 138 | |
| 139 | attribs.clear(); |
| 140 | uniforms.clear(); |
| 141 | } |
| 142 | |
| 143 | void TCompiler::collectAttribsUniforms(TIntermNode* root) |
| 144 | { |
| 145 | CollectAttribsUniforms collect(attribs, uniforms); |
| 146 | root->traverse(&collect); |
| 147 | } |
| 148 | |