blob: 99bcf07a36f6535d45bcaca3e03cc4a188c55d33 [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());
77 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
78 if ((qualifier == EvqInvariantVaryingIn) ||
79 (qualifier == EvqInvariantVaryingOut))
alokp@chromium.org8d47c112012-09-06 16:03:23 +000080 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070081 updateVersion(GLSL_VERSION_120);
82 }
83 break;
84 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040085 case EOpInvariantDeclaration:
86 updateVersion(GLSL_VERSION_120);
87 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070088 case EOpParameters:
89 {
90 const TIntermSequence &params = *(node->getSequence());
91 for (TIntermSequence::const_iterator iter = params.begin();
92 iter != params.end(); ++iter)
93 {
94 const TIntermTyped *param = (*iter)->getAsTyped();
95 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000096 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070097 TQualifier qualifier = param->getQualifier();
98 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
99 {
100 updateVersion(GLSL_VERSION_120);
101 break;
102 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000103 }
104 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700105 // Fully processed. No need to visit children.
106 visitChildren = false;
107 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000108 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000109 case EOpConstructMat2:
110 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700111 case EOpConstructMat4:
112 {
113 const TIntermSequence &sequence = *(node->getSequence());
114 if (sequence.size() == 1)
115 {
116 TIntermTyped *typed = sequence.front()->getAsTyped();
117 if (typed && typed->isMatrix())
118 {
119 updateVersion(GLSL_VERSION_120);
120 }
121 }
122 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000123 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700124 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000125 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000126 }
127
128 return visitChildren;
129}
130
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000131void TVersionGLSL::updateVersion(int version)
132{
133 mVersion = std::max(version, mVersion);
134}
135