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 | |
Olli Etuaho | 8c04d07 | 2016-12-09 15:30:48 +0000 | [diff] [blame] | 9 | #include "angle_gl.h" |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame^] | 10 | #include "compiler/translator/Symbol.h" |
Olli Etuaho | 8c04d07 | 2016-12-09 15:30:48 +0000 | [diff] [blame] | 11 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 12 | namespace sh |
| 13 | { |
| 14 | |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 15 | int ShaderOutputTypeToGLSLVersion(ShShaderOutput output) |
| 16 | { |
| 17 | switch (output) |
| 18 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 19 | case SH_GLSL_130_OUTPUT: |
| 20 | return GLSL_VERSION_130; |
| 21 | case SH_GLSL_140_OUTPUT: |
| 22 | return GLSL_VERSION_140; |
| 23 | case SH_GLSL_150_CORE_OUTPUT: |
| 24 | return GLSL_VERSION_150; |
| 25 | case SH_GLSL_330_CORE_OUTPUT: |
| 26 | return GLSL_VERSION_330; |
| 27 | case SH_GLSL_400_CORE_OUTPUT: |
| 28 | return GLSL_VERSION_400; |
| 29 | case SH_GLSL_410_CORE_OUTPUT: |
| 30 | return GLSL_VERSION_410; |
| 31 | case SH_GLSL_420_CORE_OUTPUT: |
| 32 | return GLSL_VERSION_420; |
| 33 | case SH_GLSL_430_CORE_OUTPUT: |
| 34 | return GLSL_VERSION_430; |
| 35 | case SH_GLSL_440_CORE_OUTPUT: |
| 36 | return GLSL_VERSION_440; |
| 37 | case SH_GLSL_450_CORE_OUTPUT: |
| 38 | return GLSL_VERSION_450; |
| 39 | case SH_GLSL_COMPATIBILITY_OUTPUT: |
| 40 | return GLSL_VERSION_110; |
| 41 | default: |
| 42 | UNREACHABLE(); |
| 43 | return 0; |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 46 | |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 47 | // We need to scan for the following: |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 48 | // 1. "invariant" keyword: This can occur in both - vertex and fragment shaders |
| 49 | // but only at the global scope. |
| 50 | // 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader |
| 51 | // but inside any scope. |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 52 | // 3. Call to a matrix constructor with another matrix as argument. |
| 53 | // (These constructors were reserved in GLSL version 1.10.) |
alokp@chromium.org | 8d47c11 | 2012-09-06 16:03:23 +0000 | [diff] [blame] | 54 | // 4. Arrays as "out" function parameters. |
| 55 | // GLSL spec section 6.1.1: "When calling a function, expressions that do |
| 56 | // not evaluate to l-values cannot be passed to parameters declared as |
| 57 | // out or inout." |
| 58 | // GLSL 1.1 section 5.8: "Other binary or unary expressions, |
| 59 | // non-dereferenced arrays, function names, swizzles with repeated fields, |
| 60 | // and constants cannot be l-values." |
| 61 | // GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that |
| 62 | // 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] | 63 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 64 | TVersionGLSL::TVersionGLSL(sh::GLenum type, const TPragma &pragma, ShShaderOutput output) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame] | 65 | : TIntermTraverser(true, false, false) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 66 | { |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 67 | mVersion = ShaderOutputTypeToGLSLVersion(output); |
| 68 | if (pragma.stdgl.invariantAll) |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 69 | { |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 70 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
Zhenyao Mo | 05b6b7f | 2015-03-02 17:08:09 -0800 | [diff] [blame] | 71 | } |
Olli Etuaho | 8c04d07 | 2016-12-09 15:30:48 +0000 | [diff] [blame] | 72 | if (type == GL_COMPUTE_SHADER) |
| 73 | { |
| 74 | ensureVersionIsAtLeast(GLSL_VERSION_430); |
| 75 | } |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 78 | void TVersionGLSL::visitSymbol(TIntermSymbol *node) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 79 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame^] | 80 | if (node->variable().symbolType() == SymbolType::BuiltIn && node->getName() == "gl_PointCoord") |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 81 | { |
| 82 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
| 83 | } |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 86 | bool TVersionGLSL::visitDeclaration(Visit, TIntermDeclaration *node) |
| 87 | { |
| 88 | const TIntermSequence &sequence = *(node->getSequence()); |
| 89 | if (sequence.front()->getAsTyped()->getType().isInvariant()) |
| 90 | { |
| 91 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
Olli Etuaho | bf4e1b7 | 2016-12-09 11:30:15 +0000 | [diff] [blame] | 96 | bool TVersionGLSL::visitInvariantDeclaration(Visit, TIntermInvariantDeclaration *node) |
| 97 | { |
| 98 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
| 99 | return true; |
| 100 | } |
| 101 | |
Olli Etuaho | 8ad9e75 | 2017-01-16 19:55:20 +0000 | [diff] [blame] | 102 | bool TVersionGLSL::visitFunctionPrototype(Visit, TIntermFunctionPrototype *node) |
| 103 | { |
| 104 | const TIntermSequence ¶ms = *(node->getSequence()); |
| 105 | for (TIntermSequence::const_iterator iter = params.begin(); iter != params.end(); ++iter) |
| 106 | { |
| 107 | const TIntermTyped *param = (*iter)->getAsTyped(); |
| 108 | if (param->isArray()) |
| 109 | { |
| 110 | TQualifier qualifier = param->getQualifier(); |
| 111 | if ((qualifier == EvqOut) || (qualifier == EvqInOut)) |
| 112 | { |
| 113 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | // Fully processed. No need to visit children. |
| 119 | return false; |
| 120 | } |
| 121 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 122 | bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 123 | { |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 124 | if (node->getOp() == EOpConstruct && node->getType().isMatrix()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 125 | { |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 126 | const TIntermSequence &sequence = *(node->getSequence()); |
| 127 | if (sequence.size() == 1) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 128 | { |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 129 | TIntermTyped *typed = sequence.front()->getAsTyped(); |
| 130 | if (typed && typed->isMatrix()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 131 | { |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 132 | ensureVersionIsAtLeast(GLSL_VERSION_120); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 133 | } |
kbr@chromium.org | e26cb5e | 2011-01-18 21:27:02 +0000 | [diff] [blame] | 134 | } |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 135 | } |
Olli Etuaho | 8ad9e75 | 2017-01-16 19:55:20 +0000 | [diff] [blame] | 136 | return true; |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Geoff Lang | 7b9b284 | 2015-06-15 11:02:49 -0700 | [diff] [blame] | 139 | void TVersionGLSL::ensureVersionIsAtLeast(int version) |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 140 | { |
| 141 | mVersion = std::max(version, mVersion); |
| 142 | } |
| 143 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 144 | } // namespace sh |