blob: 7e6d4cbbab4fadc65734866b09f0086c1700a9e3 [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
9static const int GLSL_VERSION_110 = 110;
10static const int GLSL_VERSION_120 = 120;
Qingqing Dengad0d0792015-04-08 14:25:06 -070011static const int GLSL_VERSION_130 = 130;
12static const int GLSL_VERSION_410 = 410;
13static const int GLSL_VERSION_420 = 420;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000014
alokp@chromium.org8d47c112012-09-06 16:03:23 +000015// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000016// 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.orge26cb5e2011-01-18 21:27:02 +000020// 3. Call to a matrix constructor with another matrix as argument.
21// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000022// 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.org9ecf3952010-10-13 19:28:25 +000031//
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080032TVersionGLSL::TVersionGLSL(sh::GLenum type,
33 const TPragma &pragma,
34 ShShaderOutput output)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000035{
Qingqing Dengad0d0792015-04-08 14:25:06 -070036 if (output == SH_GLSL_130_OUTPUT)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080037 {
Qingqing Dengad0d0792015-04-08 14:25:06 -070038 mVersion = GLSL_VERSION_130;
39 }
40 else if (output == SH_GLSL_410_CORE_OUTPUT)
41 {
42 mVersion = GLSL_VERSION_410;
43 }
44 else if (output == SH_GLSL_420_CORE_OUTPUT)
45 {
46 mVersion = GLSL_VERSION_420;
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080047 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070048 else
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080049 {
50 ASSERT(output == SH_GLSL_COMPATIBILITY_OUTPUT);
51 if (pragma.stdgl.invariantAll)
52 mVersion = GLSL_VERSION_120;
53 else
54 mVersion = GLSL_VERSION_110;
55 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000056}
57
Zhenyao Moe40d1e92014-07-16 17:40:36 -070058void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000059{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000060 if (node->getSymbol() == "gl_PointCoord")
61 updateVersion(GLSL_VERSION_120);
62}
63
Zhenyao Moe40d1e92014-07-16 17:40:36 -070064bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000065{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000066 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000067
Zhenyao Moe40d1e92014-07-16 17:40:36 -070068 switch (node->getOp())
69 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000070 case EOpSequence:
71 // We need to visit sequence children to get to global or inner scope.
72 visitChildren = true;
73 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070074 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000075 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070076 const TIntermSequence &sequence = *(node->getSequence());
Olli Etuaho214c2d82015-04-27 14:49:13 +030077 if (sequence.front()->getAsTyped()->getType().isInvariant())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000078 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070079 updateVersion(GLSL_VERSION_120);
80 }
81 break;
82 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040083 case EOpInvariantDeclaration:
84 updateVersion(GLSL_VERSION_120);
85 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070086 case EOpParameters:
87 {
88 const TIntermSequence &params = *(node->getSequence());
89 for (TIntermSequence::const_iterator iter = params.begin();
90 iter != params.end(); ++iter)
91 {
92 const TIntermTyped *param = (*iter)->getAsTyped();
93 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000094 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070095 TQualifier qualifier = param->getQualifier();
96 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
97 {
98 updateVersion(GLSL_VERSION_120);
99 break;
100 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000101 }
102 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700103 // Fully processed. No need to visit children.
104 visitChildren = false;
105 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000106 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000107 case EOpConstructMat2:
108 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700109 case EOpConstructMat4:
110 {
111 const TIntermSequence &sequence = *(node->getSequence());
112 if (sequence.size() == 1)
113 {
114 TIntermTyped *typed = sequence.front()->getAsTyped();
115 if (typed && typed->isMatrix())
116 {
117 updateVersion(GLSL_VERSION_120);
118 }
119 }
120 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000121 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700122 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000123 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000124 }
125
126 return visitChildren;
127}
128
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000129void TVersionGLSL::updateVersion(int version)
130{
131 mVersion = std::max(version, mVersion);
132}
133