blob: 81688765b8488baa75fe2265db634815a566879f [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
Olli Etuahobf4e1b72016-12-09 11:30:15 +000095bool TVersionGLSL::visitInvariantDeclaration(Visit, TIntermInvariantDeclaration *node)
96{
97 ensureVersionIsAtLeast(GLSL_VERSION_120);
98 return true;
99}
100
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000101bool TVersionGLSL::visitFunctionPrototype(Visit, TIntermFunctionPrototype *node)
102{
103 const TIntermSequence &params = *(node->getSequence());
104 for (TIntermSequence::const_iterator iter = params.begin(); iter != params.end(); ++iter)
105 {
106 const TIntermTyped *param = (*iter)->getAsTyped();
107 if (param->isArray())
108 {
109 TQualifier qualifier = param->getQualifier();
110 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
111 {
112 ensureVersionIsAtLeast(GLSL_VERSION_120);
113 break;
114 }
115 }
116 }
117 // Fully processed. No need to visit children.
118 return false;
119}
120
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700121bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000122{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300123 if (node->getOp() == EOpConstruct && node->getType().isMatrix())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700124 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300125 const TIntermSequence &sequence = *(node->getSequence());
126 if (sequence.size() == 1)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700127 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300128 TIntermTyped *typed = sequence.front()->getAsTyped();
129 if (typed && typed->isMatrix())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700130 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300131 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700132 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000133 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000134 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000135 return true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000136}
137
Geoff Lang7b9b2842015-06-15 11:02:49 -0700138void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000139{
140 mVersion = std::max(version, mVersion);
141}
142
Jamie Madill45bcc782016-11-07 13:58:48 -0500143} // namespace sh