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