alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 1 | // |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [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 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/VersionGLSL.h" |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 8 | |
| 9 | static const int GLSL_VERSION_110 = 110; |
| 10 | static const int GLSL_VERSION_120 = 120; |
| 11 | |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 12 | // We need to scan for the following: |
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.) |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 19 | // 4. Arrays as "out" function parameters. |
| 20 | // GLSL spec section 6.1.1: "When calling a function, expressions that do |
| 21 | // not evaluate to l-values cannot be passed to parameters declared as |
| 22 | // out or inout." |
| 23 | // GLSL 1.1 section 5.8: "Other binary or unary expressions, |
| 24 | // non-dereferenced arrays, function names, swizzles with repeated fields, |
| 25 | // and constants cannot be l-values." |
| 26 | // GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that |
| 27 | // are built-in types, entire structures or arrays... are all l-values." |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 28 | // |
| 29 | // TODO(alokp): The following two cases of invariant decalaration get lost |
| 30 | // during parsing - they do not get carried over to the intermediate tree. |
| 31 | // Handle these cases: |
| 32 | // 1. When a pragma is used to force all output variables to be invariant: |
| 33 | // - #pragma STDGL invariant(all) |
| 34 | // 2. When a previously decalared or built-in variable is marked invariant: |
| 35 | // - invariant gl_Position; |
| 36 | // - varying vec3 color; invariant color; |
| 37 | // |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame^] | 38 | TVersionGLSL::TVersionGLSL(sh::GLenum type) |
Shannon Woods | f26ecc8 | 2014-06-16 13:36:28 -0400 | [diff] [blame] | 39 | : mVersion(GLSL_VERSION_110) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | void TVersionGLSL::visitSymbol(TIntermSymbol* node) |
| 44 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 45 | if (node->getSymbol() == "gl_PointCoord") |
| 46 | updateVersion(GLSL_VERSION_120); |
| 47 | } |
| 48 | |
| 49 | void TVersionGLSL::visitConstantUnion(TIntermConstantUnion*) |
| 50 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool TVersionGLSL::visitBinary(Visit, TIntermBinary*) |
| 54 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool TVersionGLSL::visitUnary(Visit, TIntermUnary*) |
| 59 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool TVersionGLSL::visitSelection(Visit, TIntermSelection*) |
| 64 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate* node) |
| 69 | { |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 70 | bool visitChildren = true; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 71 | |
| 72 | switch (node->getOp()) { |
| 73 | case EOpSequence: |
| 74 | // We need to visit sequence children to get to global or inner scope. |
| 75 | visitChildren = true; |
| 76 | break; |
| 77 | case EOpDeclaration: { |
| 78 | const TIntermSequence& sequence = node->getSequence(); |
| 79 | TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier(); |
| 80 | if ((qualifier == EvqInvariantVaryingIn) || |
| 81 | (qualifier == EvqInvariantVaryingOut)) { |
| 82 | updateVersion(GLSL_VERSION_120); |
| 83 | } |
| 84 | break; |
| 85 | } |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 86 | case EOpParameters: { |
| 87 | const TIntermSequence& params = node->getSequence(); |
| 88 | for (TIntermSequence::const_iterator iter = params.begin(); |
| 89 | iter != params.end(); ++iter) |
| 90 | { |
| 91 | const TIntermTyped* param = (*iter)->getAsTyped(); |
| 92 | if (param->isArray()) |
| 93 | { |
| 94 | TQualifier qualifier = param->getQualifier(); |
| 95 | if ((qualifier == EvqOut) || (qualifier == EvqInOut)) |
| 96 | { |
| 97 | updateVersion(GLSL_VERSION_120); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | // Fully processed. No need to visit children. |
| 103 | visitChildren = false; |
| 104 | break; |
| 105 | } |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 106 | case EOpConstructMat2: |
| 107 | case EOpConstructMat3: |
| 108 | case EOpConstructMat4: { |
| 109 | const TIntermSequence& sequence = node->getSequence(); |
| 110 | if (sequence.size() == 1) { |
| 111 | TIntermTyped* typed = sequence.front()->getAsTyped(); |
| 112 | if (typed && typed->isMatrix()) { |
| 113 | updateVersion(GLSL_VERSION_120); |
| 114 | } |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 119 | default: break; |
| 120 | } |
| 121 | |
| 122 | return visitChildren; |
| 123 | } |
| 124 | |
| 125 | bool TVersionGLSL::visitLoop(Visit, TIntermLoop*) |
| 126 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool TVersionGLSL::visitBranch(Visit, TIntermBranch*) |
| 131 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | void TVersionGLSL::updateVersion(int version) |
| 136 | { |
| 137 | mVersion = std::max(version, mVersion); |
| 138 | } |
| 139 | |