blob: 26cee052707e450b159160c10652a604f96c052b [file] [log] [blame]
alokp@chromium.org9ecf3952010-10-13 19:28:25 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "compiler/VersionGLSL.h"
8
9static const int GLSL_VERSION_110 = 110;
10static const int GLSL_VERSION_120 = 120;
11
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000012// We need to scan for three things:
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.)
19//
20// If it weren't for (3) then we would only need to scan the global
21// scope of the vertex shader. However, we need to scan the entire
22// shader in both cases.
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000023//
24// TODO(alokp): The following two cases of invariant decalaration get lost
25// during parsing - they do not get carried over to the intermediate tree.
26// Handle these cases:
27// 1. When a pragma is used to force all output variables to be invariant:
28// - #pragma STDGL invariant(all)
29// 2. When a previously decalared or built-in variable is marked invariant:
30// - invariant gl_Position;
31// - varying vec3 color; invariant color;
32//
33TVersionGLSL::TVersionGLSL(ShShaderType type)
34 : mShaderType(type),
35 mVersion(GLSL_VERSION_110)
36{
37}
38
39void TVersionGLSL::visitSymbol(TIntermSymbol* node)
40{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000041 if (node->getSymbol() == "gl_PointCoord")
42 updateVersion(GLSL_VERSION_120);
43}
44
45void TVersionGLSL::visitConstantUnion(TIntermConstantUnion*)
46{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000047}
48
49bool TVersionGLSL::visitBinary(Visit, TIntermBinary*)
50{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000051 return true;
52}
53
54bool TVersionGLSL::visitUnary(Visit, TIntermUnary*)
55{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000056 return true;
57}
58
59bool TVersionGLSL::visitSelection(Visit, TIntermSelection*)
60{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000061 return true;
62}
63
64bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate* node)
65{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000066 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000067
68 switch (node->getOp()) {
69 case EOpSequence:
70 // We need to visit sequence children to get to global or inner scope.
71 visitChildren = true;
72 break;
73 case EOpDeclaration: {
74 const TIntermSequence& sequence = node->getSequence();
75 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
76 if ((qualifier == EvqInvariantVaryingIn) ||
77 (qualifier == EvqInvariantVaryingOut)) {
78 updateVersion(GLSL_VERSION_120);
79 }
80 break;
81 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000082 case EOpConstructMat2:
83 case EOpConstructMat3:
84 case EOpConstructMat4: {
85 const TIntermSequence& sequence = node->getSequence();
86 if (sequence.size() == 1) {
87 TIntermTyped* typed = sequence.front()->getAsTyped();
88 if (typed && typed->isMatrix()) {
89 updateVersion(GLSL_VERSION_120);
90 }
91 }
92 break;
93 }
94
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000095 default: break;
96 }
97
98 return visitChildren;
99}
100
101bool TVersionGLSL::visitLoop(Visit, TIntermLoop*)
102{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000103 return true;
104}
105
106bool TVersionGLSL::visitBranch(Visit, TIntermBranch*)
107{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000108 return true;
109}
110
111void TVersionGLSL::updateVersion(int version)
112{
113 mVersion = std::max(version, mVersion);
114}
115