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; |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 11 | static const int GLSL_VERSION_130 = 130; |
| 12 | static const int GLSL_VERSION_410 = 410; |
| 13 | static const int GLSL_VERSION_420 = 420; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 14 | |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 15 | // We need to scan for the following: |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 16 | // 1. "invariant" keyword: This can occur in both - vertex and fragment shaders |
| 17 | // but only at the global scope. |
| 18 | // 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader |
| 19 | // but inside any scope. |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 20 | // 3. Call to a matrix constructor with another matrix as argument. |
| 21 | // (These constructors were reserved in GLSL version 1.10.) |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 22 | // 4. Arrays as "out" function parameters. |
| 23 | // GLSL spec section 6.1.1: "When calling a function, expressions that do |
| 24 | // not evaluate to l-values cannot be passed to parameters declared as |
| 25 | // out or inout." |
| 26 | // GLSL 1.1 section 5.8: "Other binary or unary expressions, |
| 27 | // non-dereferenced arrays, function names, swizzles with repeated fields, |
| 28 | // and constants cannot be l-values." |
| 29 | // GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that |
| 30 | // 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] | 31 | // |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 32 | TVersionGLSL::TVersionGLSL(sh::GLenum type, |
| 33 | const TPragma &pragma, |
| 34 | ShShaderOutput output) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame^] | 35 | : TIntermTraverser(true, false, false) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 36 | { |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 37 | if (output == SH_GLSL_130_OUTPUT) |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 38 | { |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 39 | mVersion = GLSL_VERSION_130; |
| 40 | } |
| 41 | else if (output == SH_GLSL_410_CORE_OUTPUT) |
| 42 | { |
| 43 | mVersion = GLSL_VERSION_410; |
| 44 | } |
| 45 | else if (output == SH_GLSL_420_CORE_OUTPUT) |
| 46 | { |
| 47 | mVersion = GLSL_VERSION_420; |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 48 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 49 | else |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 50 | { |
| 51 | ASSERT(output == SH_GLSL_COMPATIBILITY_OUTPUT); |
| 52 | if (pragma.stdgl.invariantAll) |
| 53 | mVersion = GLSL_VERSION_120; |
| 54 | else |
| 55 | mVersion = GLSL_VERSION_110; |
| 56 | } |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 59 | void TVersionGLSL::visitSymbol(TIntermSymbol *node) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 60 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 61 | if (node->getSymbol() == "gl_PointCoord") |
| 62 | updateVersion(GLSL_VERSION_120); |
| 63 | } |
| 64 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 65 | bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 66 | { |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 67 | bool visitChildren = true; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 68 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 69 | switch (node->getOp()) |
| 70 | { |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 71 | case EOpSequence: |
| 72 | // We need to visit sequence children to get to global or inner scope. |
| 73 | visitChildren = true; |
| 74 | break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 75 | case EOpDeclaration: |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 76 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 77 | const TIntermSequence &sequence = *(node->getSequence()); |
Olli Etuaho | 214c2d8 | 2015-04-27 14:49:13 +0300 | [diff] [blame] | 78 | if (sequence.front()->getAsTyped()->getType().isInvariant()) |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 79 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 80 | updateVersion(GLSL_VERSION_120); |
| 81 | } |
| 82 | break; |
| 83 | } |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 84 | case EOpInvariantDeclaration: |
| 85 | updateVersion(GLSL_VERSION_120); |
| 86 | break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 87 | case EOpParameters: |
| 88 | { |
| 89 | const TIntermSequence ¶ms = *(node->getSequence()); |
| 90 | for (TIntermSequence::const_iterator iter = params.begin(); |
| 91 | iter != params.end(); ++iter) |
| 92 | { |
| 93 | const TIntermTyped *param = (*iter)->getAsTyped(); |
| 94 | if (param->isArray()) |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 95 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 96 | TQualifier qualifier = param->getQualifier(); |
| 97 | if ((qualifier == EvqOut) || (qualifier == EvqInOut)) |
| 98 | { |
| 99 | updateVersion(GLSL_VERSION_120); |
| 100 | break; |
| 101 | } |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 104 | // Fully processed. No need to visit children. |
| 105 | visitChildren = false; |
| 106 | break; |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 107 | } |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 108 | case EOpConstructMat2: |
| 109 | case EOpConstructMat3: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 110 | case EOpConstructMat4: |
| 111 | { |
| 112 | const TIntermSequence &sequence = *(node->getSequence()); |
| 113 | if (sequence.size() == 1) |
| 114 | { |
| 115 | TIntermTyped *typed = sequence.front()->getAsTyped(); |
| 116 | if (typed && typed->isMatrix()) |
| 117 | { |
| 118 | updateVersion(GLSL_VERSION_120); |
| 119 | } |
| 120 | } |
| 121 | break; |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 122 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 123 | default: |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 124 | break; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return visitChildren; |
| 128 | } |
| 129 | |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 130 | void TVersionGLSL::updateVersion(int version) |
| 131 | { |
| 132 | mVersion = std::max(version, mVersion); |
| 133 | } |
| 134 | |