blob: 4364c2eb8b235a7e7bba246812c5de6387a3d80b [file] [log] [blame]
alokp@chromium.org9ecf3952010-10-13 19:28:25 +00001//
alokp@chromium.org8d47c112012-09-06 16:03:23 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org9ecf3952010-10-13 19:28:25 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/VersionGLSL.h"
alokp@chromium.org9ecf3952010-10-13 19:28:25 +00008
Geoff Lang7b9b2842015-06-15 11:02:49 -07009int ShaderOutputTypeToGLSLVersion(ShShaderOutput output)
10{
11 switch (output)
12 {
13 case SH_GLSL_130_OUTPUT: return GLSL_VERSION_130;
14 case SH_GLSL_410_CORE_OUTPUT: return GLSL_VERSION_410;
15 case SH_GLSL_420_CORE_OUTPUT: return GLSL_VERSION_420;
16 case SH_GLSL_COMPATIBILITY_OUTPUT: return GLSL_VERSION_110;
17 default: UNREACHABLE(); return 0;
18 }
19}
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000020
alokp@chromium.org8d47c112012-09-06 16:03:23 +000021// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000022// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
23// but only at the global scope.
24// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
25// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000026// 3. Call to a matrix constructor with another matrix as argument.
27// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000028// 4. Arrays as "out" function parameters.
29// GLSL spec section 6.1.1: "When calling a function, expressions that do
30// not evaluate to l-values cannot be passed to parameters declared as
31// out or inout."
32// GLSL 1.1 section 5.8: "Other binary or unary expressions,
33// non-dereferenced arrays, function names, swizzles with repeated fields,
34// and constants cannot be l-values."
35// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
36// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000037//
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080038TVersionGLSL::TVersionGLSL(sh::GLenum type,
39 const TPragma &pragma,
40 ShShaderOutput output)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030041 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000042{
Geoff Lang7b9b2842015-06-15 11:02:49 -070043 mVersion = ShaderOutputTypeToGLSLVersion(output);
44 if (pragma.stdgl.invariantAll)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080045 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070046 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080047 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000048}
49
Zhenyao Moe40d1e92014-07-16 17:40:36 -070050void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000051{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000052 if (node->getSymbol() == "gl_PointCoord")
Geoff Lang7b9b2842015-06-15 11:02:49 -070053 {
54 ensureVersionIsAtLeast(GLSL_VERSION_120);
55 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000056}
57
Zhenyao Moe40d1e92014-07-16 17:40:36 -070058bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000059{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000060 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000061
Zhenyao Moe40d1e92014-07-16 17:40:36 -070062 switch (node->getOp())
63 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000064 case EOpSequence:
65 // We need to visit sequence children to get to global or inner scope.
66 visitChildren = true;
67 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070068 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000069 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070070 const TIntermSequence &sequence = *(node->getSequence());
Olli Etuaho214c2d82015-04-27 14:49:13 +030071 if (sequence.front()->getAsTyped()->getType().isInvariant())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000072 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070073 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070074 }
75 break;
76 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040077 case EOpInvariantDeclaration:
Geoff Lang7b9b2842015-06-15 11:02:49 -070078 ensureVersionIsAtLeast(GLSL_VERSION_120);
Jamie Madill3b5c2da2014-08-19 15:23:32 -040079 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070080 case EOpParameters:
81 {
82 const TIntermSequence &params = *(node->getSequence());
83 for (TIntermSequence::const_iterator iter = params.begin();
84 iter != params.end(); ++iter)
85 {
86 const TIntermTyped *param = (*iter)->getAsTyped();
87 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000088 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070089 TQualifier qualifier = param->getQualifier();
90 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
91 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070092 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070093 break;
94 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +000095 }
96 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -070097 // Fully processed. No need to visit children.
98 visitChildren = false;
99 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000100 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000101 case EOpConstructMat2:
102 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700103 case EOpConstructMat4:
104 {
105 const TIntermSequence &sequence = *(node->getSequence());
106 if (sequence.size() == 1)
107 {
108 TIntermTyped *typed = sequence.front()->getAsTyped();
109 if (typed && typed->isMatrix())
110 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700111 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700112 }
113 }
114 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000115 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700116 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000117 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000118 }
119
120 return visitChildren;
121}
122
Geoff Lang7b9b2842015-06-15 11:02:49 -0700123void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000124{
125 mVersion = std::max(version, mVersion);
126}
127