blob: 8edbd009b069749f800d942163cf5d90283ea97d [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//
29// TODO(alokp): The following two cases of invariant decalaration get lost
30// during parsing - they do not get carried over to the intermediate tree.
31// Handle these cases:
32// 1. When a pragma is used to force all output variables to be invariant:
33// - #pragma STDGL invariant(all)
34// 2. When a previously decalared or built-in variable is marked invariant:
35// - invariant gl_Position;
36// - varying vec3 color; invariant color;
37//
Jamie Madill183bde52014-07-02 15:31:19 -040038TVersionGLSL::TVersionGLSL(sh::GLenum type)
Shannon Woodsf26ecc82014-06-16 13:36:28 -040039 : mVersion(GLSL_VERSION_110)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000040{
41}
42
Zhenyao Moe40d1e92014-07-16 17:40:36 -070043void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000044{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000045 if (node->getSymbol() == "gl_PointCoord")
46 updateVersion(GLSL_VERSION_120);
47}
48
Zhenyao Moe40d1e92014-07-16 17:40:36 -070049bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000050{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000051 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000052
Zhenyao Moe40d1e92014-07-16 17:40:36 -070053 switch (node->getOp())
54 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000055 case EOpSequence:
56 // We need to visit sequence children to get to global or inner scope.
57 visitChildren = true;
58 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070059 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000060 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070061 const TIntermSequence &sequence = *(node->getSequence());
62 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
63 if ((qualifier == EvqInvariantVaryingIn) ||
64 (qualifier == EvqInvariantVaryingOut))
alokp@chromium.org8d47c112012-09-06 16:03:23 +000065 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070066 updateVersion(GLSL_VERSION_120);
67 }
68 break;
69 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040070 case EOpInvariantDeclaration:
71 updateVersion(GLSL_VERSION_120);
72 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070073 case EOpParameters:
74 {
75 const TIntermSequence &params = *(node->getSequence());
76 for (TIntermSequence::const_iterator iter = params.begin();
77 iter != params.end(); ++iter)
78 {
79 const TIntermTyped *param = (*iter)->getAsTyped();
80 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000081 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070082 TQualifier qualifier = param->getQualifier();
83 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
84 {
85 updateVersion(GLSL_VERSION_120);
86 break;
87 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +000088 }
89 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -070090 // Fully processed. No need to visit children.
91 visitChildren = false;
92 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +000093 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000094 case EOpConstructMat2:
95 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -070096 case EOpConstructMat4:
97 {
98 const TIntermSequence &sequence = *(node->getSequence());
99 if (sequence.size() == 1)
100 {
101 TIntermTyped *typed = sequence.front()->getAsTyped();
102 if (typed && typed->isMatrix())
103 {
104 updateVersion(GLSL_VERSION_120);
105 }
106 }
107 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000108 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700109 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000110 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000111 }
112
113 return visitChildren;
114}
115
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000116void TVersionGLSL::updateVersion(int version)
117{
118 mVersion = std::max(version, mVersion);
119}
120