blob: 05b111a7a743a8db76d88f0caaa08922ccdb0fc2 [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;
11
alokp@chromium.org8d47c112012-09-06 16:03:23 +000012// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000013// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
14// but only at the global scope.
15// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
16// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000017// 3. Call to a matrix constructor with another matrix as argument.
18// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000019// 4. Arrays as "out" function parameters.
20// GLSL spec section 6.1.1: "When calling a function, expressions that do
21// not evaluate to l-values cannot be passed to parameters declared as
22// out or inout."
23// GLSL 1.1 section 5.8: "Other binary or unary expressions,
24// non-dereferenced arrays, function names, swizzles with repeated fields,
25// and constants cannot be l-values."
26// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
27// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000028//
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070029TVersionGLSL::TVersionGLSL(sh::GLenum type, const TPragma &pragma)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000030{
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070031 if (pragma.stdgl.invariantAll)
32 mVersion = GLSL_VERSION_120;
33 else
34 mVersion = GLSL_VERSION_110;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000035}
36
Zhenyao Moe40d1e92014-07-16 17:40:36 -070037void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000038{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000039 if (node->getSymbol() == "gl_PointCoord")
40 updateVersion(GLSL_VERSION_120);
41}
42
Zhenyao Moe40d1e92014-07-16 17:40:36 -070043bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000044{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000045 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000046
Zhenyao Moe40d1e92014-07-16 17:40:36 -070047 switch (node->getOp())
48 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000049 case EOpSequence:
50 // We need to visit sequence children to get to global or inner scope.
51 visitChildren = true;
52 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070053 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000054 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070055 const TIntermSequence &sequence = *(node->getSequence());
56 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
57 if ((qualifier == EvqInvariantVaryingIn) ||
58 (qualifier == EvqInvariantVaryingOut))
alokp@chromium.org8d47c112012-09-06 16:03:23 +000059 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070060 updateVersion(GLSL_VERSION_120);
61 }
62 break;
63 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040064 case EOpInvariantDeclaration:
65 updateVersion(GLSL_VERSION_120);
66 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070067 case EOpParameters:
68 {
69 const TIntermSequence &params = *(node->getSequence());
70 for (TIntermSequence::const_iterator iter = params.begin();
71 iter != params.end(); ++iter)
72 {
73 const TIntermTyped *param = (*iter)->getAsTyped();
74 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000075 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070076 TQualifier qualifier = param->getQualifier();
77 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
78 {
79 updateVersion(GLSL_VERSION_120);
80 break;
81 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +000082 }
83 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -070084 // Fully processed. No need to visit children.
85 visitChildren = false;
86 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +000087 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000088 case EOpConstructMat2:
89 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -070090 case EOpConstructMat4:
91 {
92 const TIntermSequence &sequence = *(node->getSequence());
93 if (sequence.size() == 1)
94 {
95 TIntermTyped *typed = sequence.front()->getAsTyped();
96 if (typed && typed->isMatrix())
97 {
98 updateVersion(GLSL_VERSION_120);
99 }
100 }
101 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000102 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700103 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000104 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000105 }
106
107 return visitChildren;
108}
109
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000110void TVersionGLSL::updateVersion(int version)
111{
112 mVersion = std::max(version, mVersion);
113}
114