blob: 8dfd3cb70d4bf5b99a4c4ab115fb03fabf5182d4 [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
Olli Etuaho8c04d072016-12-09 15:30:48 +00009#include "angle_gl.h"
10
Jamie Madill45bcc782016-11-07 13:58:48 -050011namespace sh
12{
13
Geoff Lang7b9b2842015-06-15 11:02:49 -070014int ShaderOutputTypeToGLSLVersion(ShShaderOutput output)
15{
16 switch (output)
17 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050018 case SH_GLSL_130_OUTPUT:
19 return GLSL_VERSION_130;
20 case SH_GLSL_140_OUTPUT:
21 return GLSL_VERSION_140;
22 case SH_GLSL_150_CORE_OUTPUT:
23 return GLSL_VERSION_150;
24 case SH_GLSL_330_CORE_OUTPUT:
25 return GLSL_VERSION_330;
26 case SH_GLSL_400_CORE_OUTPUT:
27 return GLSL_VERSION_400;
28 case SH_GLSL_410_CORE_OUTPUT:
29 return GLSL_VERSION_410;
30 case SH_GLSL_420_CORE_OUTPUT:
31 return GLSL_VERSION_420;
32 case SH_GLSL_430_CORE_OUTPUT:
33 return GLSL_VERSION_430;
34 case SH_GLSL_440_CORE_OUTPUT:
35 return GLSL_VERSION_440;
36 case SH_GLSL_450_CORE_OUTPUT:
37 return GLSL_VERSION_450;
38 case SH_GLSL_COMPATIBILITY_OUTPUT:
39 return GLSL_VERSION_110;
40 default:
41 UNREACHABLE();
42 return 0;
Geoff Lang7b9b2842015-06-15 11:02:49 -070043 }
44}
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000045
alokp@chromium.org8d47c112012-09-06 16:03:23 +000046// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000047// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
48// but only at the global scope.
49// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
50// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000051// 3. Call to a matrix constructor with another matrix as argument.
52// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000053// 4. Arrays as "out" function parameters.
54// GLSL spec section 6.1.1: "When calling a function, expressions that do
55// not evaluate to l-values cannot be passed to parameters declared as
56// out or inout."
57// GLSL 1.1 section 5.8: "Other binary or unary expressions,
58// non-dereferenced arrays, function names, swizzles with repeated fields,
59// and constants cannot be l-values."
60// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
61// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000062//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050063TVersionGLSL::TVersionGLSL(sh::GLenum type, const TPragma &pragma, ShShaderOutput output)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030064 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000065{
Geoff Lang7b9b2842015-06-15 11:02:49 -070066 mVersion = ShaderOutputTypeToGLSLVersion(output);
67 if (pragma.stdgl.invariantAll)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080068 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070069 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080070 }
Olli Etuaho8c04d072016-12-09 15:30:48 +000071 if (type == GL_COMPUTE_SHADER)
72 {
73 ensureVersionIsAtLeast(GLSL_VERSION_430);
74 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000075}
76
Zhenyao Moe40d1e92014-07-16 17:40:36 -070077void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000078{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000079 if (node->getSymbol() == "gl_PointCoord")
Geoff Lang7b9b2842015-06-15 11:02:49 -070080 {
81 ensureVersionIsAtLeast(GLSL_VERSION_120);
82 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000083}
84
Olli Etuaho13389b62016-10-16 11:48:18 +010085bool TVersionGLSL::visitDeclaration(Visit, TIntermDeclaration *node)
86{
87 const TIntermSequence &sequence = *(node->getSequence());
88 if (sequence.front()->getAsTyped()->getType().isInvariant())
89 {
90 ensureVersionIsAtLeast(GLSL_VERSION_120);
91 }
92 return true;
93}
94
Zhenyao Moe40d1e92014-07-16 17:40:36 -070095bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000096{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000097 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000098
Zhenyao Moe40d1e92014-07-16 17:40:36 -070099 switch (node->getOp())
100 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500101 case EOpInvariantDeclaration:
102 ensureVersionIsAtLeast(GLSL_VERSION_120);
103 break;
104 case EOpParameters:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700105 {
106 const TIntermSequence &params = *(node->getSequence());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500107 for (TIntermSequence::const_iterator iter = params.begin(); iter != params.end();
108 ++iter)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700109 {
110 const TIntermTyped *param = (*iter)->getAsTyped();
111 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000112 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700113 TQualifier qualifier = param->getQualifier();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500114 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700115 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700116 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700117 break;
118 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000119 }
120 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700121 // Fully processed. No need to visit children.
122 visitChildren = false;
123 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000124 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500125 case EOpConstructMat2:
126 case EOpConstructMat2x3:
127 case EOpConstructMat2x4:
128 case EOpConstructMat3x2:
129 case EOpConstructMat3:
130 case EOpConstructMat3x4:
131 case EOpConstructMat4x2:
132 case EOpConstructMat4x3:
133 case EOpConstructMat4:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700134 {
135 const TIntermSequence &sequence = *(node->getSequence());
136 if (sequence.size() == 1)
137 {
138 TIntermTyped *typed = sequence.front()->getAsTyped();
139 if (typed && typed->isMatrix())
140 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700141 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700142 }
143 }
144 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000145 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500146 default:
147 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000148 }
149
150 return visitChildren;
151}
152
Geoff Lang7b9b2842015-06-15 11:02:49 -0700153void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000154{
155 mVersion = std::max(version, mVersion);
156}
157
Jamie Madill45bcc782016-11-07 13:58:48 -0500158} // namespace sh