alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +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/VersionGLSL.h" |
| 8 | |
| 9 | static const int GLSL_VERSION_110 = 110; |
| 10 | static const int GLSL_VERSION_120 = 120; |
| 11 | |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame^] | 12 | // We need to scan for three things: |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 13 | // 1. "invariant" keyword: This can occur in both - vertex and fragment shaders |
| 14 | // but only at the global scope. |
| 15 | // 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader |
| 16 | // but inside any scope. |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame^] | 17 | // 3. Call to a matrix constructor with another matrix as argument. |
| 18 | // (These constructors were reserved in GLSL version 1.10.) |
| 19 | // |
| 20 | // If it weren't for (3) then we would only need to scan the global |
| 21 | // scope of the vertex shader. However, we need to scan the entire |
| 22 | // shader in both cases. |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 23 | // |
| 24 | // TODO(alokp): The following two cases of invariant decalaration get lost |
| 25 | // during parsing - they do not get carried over to the intermediate tree. |
| 26 | // Handle these cases: |
| 27 | // 1. When a pragma is used to force all output variables to be invariant: |
| 28 | // - #pragma STDGL invariant(all) |
| 29 | // 2. When a previously decalared or built-in variable is marked invariant: |
| 30 | // - invariant gl_Position; |
| 31 | // - varying vec3 color; invariant color; |
| 32 | // |
| 33 | TVersionGLSL::TVersionGLSL(ShShaderType type) |
| 34 | : mShaderType(type), |
| 35 | mVersion(GLSL_VERSION_110) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void TVersionGLSL::visitSymbol(TIntermSymbol* node) |
| 40 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 41 | if (node->getSymbol() == "gl_PointCoord") |
| 42 | updateVersion(GLSL_VERSION_120); |
| 43 | } |
| 44 | |
| 45 | void TVersionGLSL::visitConstantUnion(TIntermConstantUnion*) |
| 46 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool TVersionGLSL::visitBinary(Visit, TIntermBinary*) |
| 50 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool TVersionGLSL::visitUnary(Visit, TIntermUnary*) |
| 55 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool TVersionGLSL::visitSelection(Visit, TIntermSelection*) |
| 60 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate* node) |
| 65 | { |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame^] | 66 | bool visitChildren = true; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 67 | |
| 68 | switch (node->getOp()) { |
| 69 | case EOpSequence: |
| 70 | // We need to visit sequence children to get to global or inner scope. |
| 71 | visitChildren = true; |
| 72 | break; |
| 73 | case EOpDeclaration: { |
| 74 | const TIntermSequence& sequence = node->getSequence(); |
| 75 | TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier(); |
| 76 | if ((qualifier == EvqInvariantVaryingIn) || |
| 77 | (qualifier == EvqInvariantVaryingOut)) { |
| 78 | updateVersion(GLSL_VERSION_120); |
| 79 | } |
| 80 | break; |
| 81 | } |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame^] | 82 | case EOpConstructMat2: |
| 83 | case EOpConstructMat3: |
| 84 | case EOpConstructMat4: { |
| 85 | const TIntermSequence& sequence = node->getSequence(); |
| 86 | if (sequence.size() == 1) { |
| 87 | TIntermTyped* typed = sequence.front()->getAsTyped(); |
| 88 | if (typed && typed->isMatrix()) { |
| 89 | updateVersion(GLSL_VERSION_120); |
| 90 | } |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 95 | default: break; |
| 96 | } |
| 97 | |
| 98 | return visitChildren; |
| 99 | } |
| 100 | |
| 101 | bool TVersionGLSL::visitLoop(Visit, TIntermLoop*) |
| 102 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool TVersionGLSL::visitBranch(Visit, TIntermBranch*) |
| 107 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void TVersionGLSL::updateVersion(int version) |
| 112 | { |
| 113 | mVersion = std::max(version, mVersion); |
| 114 | } |
| 115 | |