blob: 37e8ce1e789d9a0643f87d6caf2fc1008d0c7529 [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 {
18 case SH_GLSL_130_OUTPUT: return GLSL_VERSION_130;
Geoff Lang8273e002015-06-15 13:40:19 -070019 case SH_GLSL_140_OUTPUT: return GLSL_VERSION_140;
20 case SH_GLSL_150_CORE_OUTPUT: return GLSL_VERSION_150;
21 case SH_GLSL_330_CORE_OUTPUT: return GLSL_VERSION_330;
22 case SH_GLSL_400_CORE_OUTPUT: return GLSL_VERSION_400;
Geoff Lang7b9b2842015-06-15 11:02:49 -070023 case SH_GLSL_410_CORE_OUTPUT: return GLSL_VERSION_410;
24 case SH_GLSL_420_CORE_OUTPUT: return GLSL_VERSION_420;
Geoff Lang8273e002015-06-15 13:40:19 -070025 case SH_GLSL_430_CORE_OUTPUT: return GLSL_VERSION_430;
26 case SH_GLSL_440_CORE_OUTPUT: return GLSL_VERSION_440;
27 case SH_GLSL_450_CORE_OUTPUT: return GLSL_VERSION_450;
Geoff Lang7b9b2842015-06-15 11:02:49 -070028 case SH_GLSL_COMPATIBILITY_OUTPUT: return GLSL_VERSION_110;
29 default: UNREACHABLE(); return 0;
30 }
31}
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000032
alokp@chromium.org8d47c112012-09-06 16:03:23 +000033// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000034// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
35// but only at the global scope.
36// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
37// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000038// 3. Call to a matrix constructor with another matrix as argument.
39// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000040// 4. Arrays as "out" function parameters.
41// GLSL spec section 6.1.1: "When calling a function, expressions that do
42// not evaluate to l-values cannot be passed to parameters declared as
43// out or inout."
44// GLSL 1.1 section 5.8: "Other binary or unary expressions,
45// non-dereferenced arrays, function names, swizzles with repeated fields,
46// and constants cannot be l-values."
47// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
48// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000049//
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080050TVersionGLSL::TVersionGLSL(sh::GLenum type,
51 const TPragma &pragma,
52 ShShaderOutput output)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030053 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000054{
Geoff Lang7b9b2842015-06-15 11:02:49 -070055 mVersion = ShaderOutputTypeToGLSLVersion(output);
56 if (pragma.stdgl.invariantAll)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080057 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070058 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080059 }
Olli Etuaho8c04d072016-12-09 15:30:48 +000060 if (type == GL_COMPUTE_SHADER)
61 {
62 ensureVersionIsAtLeast(GLSL_VERSION_430);
63 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000064}
65
Zhenyao Moe40d1e92014-07-16 17:40:36 -070066void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000067{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000068 if (node->getSymbol() == "gl_PointCoord")
Geoff Lang7b9b2842015-06-15 11:02:49 -070069 {
70 ensureVersionIsAtLeast(GLSL_VERSION_120);
71 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000072}
73
Olli Etuaho13389b62016-10-16 11:48:18 +010074bool TVersionGLSL::visitDeclaration(Visit, TIntermDeclaration *node)
75{
76 const TIntermSequence &sequence = *(node->getSequence());
77 if (sequence.front()->getAsTyped()->getType().isInvariant())
78 {
79 ensureVersionIsAtLeast(GLSL_VERSION_120);
80 }
81 return true;
82}
83
Zhenyao Moe40d1e92014-07-16 17:40:36 -070084bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000085{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000086 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000087
Zhenyao Moe40d1e92014-07-16 17:40:36 -070088 switch (node->getOp())
89 {
Jamie Madill3b5c2da2014-08-19 15:23:32 -040090 case EOpInvariantDeclaration:
Geoff Lang7b9b2842015-06-15 11:02:49 -070091 ensureVersionIsAtLeast(GLSL_VERSION_120);
Jamie Madill3b5c2da2014-08-19 15:23:32 -040092 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070093 case EOpParameters:
94 {
95 const TIntermSequence &params = *(node->getSequence());
96 for (TIntermSequence::const_iterator iter = params.begin();
97 iter != params.end(); ++iter)
98 {
99 const TIntermTyped *param = (*iter)->getAsTyped();
100 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000101 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700102 TQualifier qualifier = param->getQualifier();
103 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
104 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700105 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700106 break;
107 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000108 }
109 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700110 // Fully processed. No need to visit children.
111 visitChildren = false;
112 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000113 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000114 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400115 case EOpConstructMat2x3:
116 case EOpConstructMat2x4:
117 case EOpConstructMat3x2:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000118 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400119 case EOpConstructMat3x4:
120 case EOpConstructMat4x2:
121 case EOpConstructMat4x3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700122 case EOpConstructMat4:
123 {
124 const TIntermSequence &sequence = *(node->getSequence());
125 if (sequence.size() == 1)
126 {
127 TIntermTyped *typed = sequence.front()->getAsTyped();
128 if (typed && typed->isMatrix())
129 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700130 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700131 }
132 }
133 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000134 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700135 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000136 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000137 }
138
139 return visitChildren;
140}
141
Geoff Lang7b9b2842015-06-15 11:02:49 -0700142void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000143{
144 mVersion = std::max(version, mVersion);
145}
146
Jamie Madill45bcc782016-11-07 13:58:48 -0500147} // namespace sh