blob: acb46dc165fabc9e7d8924f8b4dd01126326a534 [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)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030035 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000036{
Qingqing Dengad0d0792015-04-08 14:25:06 -070037 if (output == SH_GLSL_130_OUTPUT)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080038 {
Qingqing Dengad0d0792015-04-08 14:25:06 -070039 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 Mo05b6b7f2015-03-02 17:08:09 -080048 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070049 else
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080050 {
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.org9ecf3952010-10-13 19:28:25 +000057}
58
Zhenyao Moe40d1e92014-07-16 17:40:36 -070059void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000060{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000061 if (node->getSymbol() == "gl_PointCoord")
62 updateVersion(GLSL_VERSION_120);
63}
64
Zhenyao Moe40d1e92014-07-16 17:40:36 -070065bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000066{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000067 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000068
Zhenyao Moe40d1e92014-07-16 17:40:36 -070069 switch (node->getOp())
70 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000071 case EOpSequence:
72 // We need to visit sequence children to get to global or inner scope.
73 visitChildren = true;
74 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070075 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000076 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070077 const TIntermSequence &sequence = *(node->getSequence());
Olli Etuaho214c2d82015-04-27 14:49:13 +030078 if (sequence.front()->getAsTyped()->getType().isInvariant())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000079 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070080 updateVersion(GLSL_VERSION_120);
81 }
82 break;
83 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040084 case EOpInvariantDeclaration:
85 updateVersion(GLSL_VERSION_120);
86 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070087 case EOpParameters:
88 {
89 const TIntermSequence &params = *(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.org8d47c112012-09-06 16:03:23 +000095 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070096 TQualifier qualifier = param->getQualifier();
97 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
98 {
99 updateVersion(GLSL_VERSION_120);
100 break;
101 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000102 }
103 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700104 // Fully processed. No need to visit children.
105 visitChildren = false;
106 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000107 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000108 case EOpConstructMat2:
109 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700110 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.orge26cb5e2011-01-18 21:27:02 +0000122 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700123 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000124 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000125 }
126
127 return visitChildren;
128}
129
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000130void TVersionGLSL::updateVersion(int version)
131{
132 mVersion = std::max(version, mVersion);
133}
134