blob: fb9141b60536f5f7d48f9420272752ec5c07e014 [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
Geoff Lang7b9b2842015-06-15 11:02:49 -07009int ShaderOutputTypeToGLSLVersion(ShShaderOutput output)
10{
11 switch (output)
12 {
13 case SH_GLSL_130_OUTPUT: return GLSL_VERSION_130;
Geoff Lang8273e002015-06-15 13:40:19 -070014 case SH_GLSL_140_OUTPUT: return GLSL_VERSION_140;
15 case SH_GLSL_150_CORE_OUTPUT: return GLSL_VERSION_150;
16 case SH_GLSL_330_CORE_OUTPUT: return GLSL_VERSION_330;
17 case SH_GLSL_400_CORE_OUTPUT: return GLSL_VERSION_400;
Geoff Lang7b9b2842015-06-15 11:02:49 -070018 case SH_GLSL_410_CORE_OUTPUT: return GLSL_VERSION_410;
19 case SH_GLSL_420_CORE_OUTPUT: return GLSL_VERSION_420;
Geoff Lang8273e002015-06-15 13:40:19 -070020 case SH_GLSL_430_CORE_OUTPUT: return GLSL_VERSION_430;
21 case SH_GLSL_440_CORE_OUTPUT: return GLSL_VERSION_440;
22 case SH_GLSL_450_CORE_OUTPUT: return GLSL_VERSION_450;
Geoff Lang7b9b2842015-06-15 11:02:49 -070023 case SH_GLSL_COMPATIBILITY_OUTPUT: return GLSL_VERSION_110;
24 default: UNREACHABLE(); return 0;
25 }
26}
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000027
alokp@chromium.org8d47c112012-09-06 16:03:23 +000028// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000029// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
30// but only at the global scope.
31// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
32// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000033// 3. Call to a matrix constructor with another matrix as argument.
34// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000035// 4. Arrays as "out" function parameters.
36// GLSL spec section 6.1.1: "When calling a function, expressions that do
37// not evaluate to l-values cannot be passed to parameters declared as
38// out or inout."
39// GLSL 1.1 section 5.8: "Other binary or unary expressions,
40// non-dereferenced arrays, function names, swizzles with repeated fields,
41// and constants cannot be l-values."
42// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
43// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000044//
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080045TVersionGLSL::TVersionGLSL(sh::GLenum type,
46 const TPragma &pragma,
47 ShShaderOutput output)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030048 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000049{
Geoff Lang7b9b2842015-06-15 11:02:49 -070050 mVersion = ShaderOutputTypeToGLSLVersion(output);
51 if (pragma.stdgl.invariantAll)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080052 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070053 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080054 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000055}
56
Zhenyao Moe40d1e92014-07-16 17:40:36 -070057void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000058{
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000059 if (node->getSymbol() == "gl_PointCoord")
Geoff Lang7b9b2842015-06-15 11:02:49 -070060 {
61 ensureVersionIsAtLeast(GLSL_VERSION_120);
62 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000063}
64
Zhenyao Moe40d1e92014-07-16 17:40:36 -070065bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000066{
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000067 bool visitChildren = true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000068
Zhenyao Moe40d1e92014-07-16 17:40:36 -070069 switch (node->getOp())
70 {
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000071 case EOpSequence:
72 // We need to visit sequence children to get to global or inner scope.
73 visitChildren = true;
74 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070075 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000076 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070077 const TIntermSequence &sequence = *(node->getSequence());
Olli Etuaho214c2d82015-04-27 14:49:13 +030078 if (sequence.front()->getAsTyped()->getType().isInvariant())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000079 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070080 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070081 }
82 break;
83 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040084 case EOpInvariantDeclaration:
Geoff Lang7b9b2842015-06-15 11:02:49 -070085 ensureVersionIsAtLeast(GLSL_VERSION_120);
Jamie Madill3b5c2da2014-08-19 15:23:32 -040086 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070087 case EOpParameters:
88 {
89 const TIntermSequence &params = *(node->getSequence());
90 for (TIntermSequence::const_iterator iter = params.begin();
91 iter != params.end(); ++iter)
92 {
93 const TIntermTyped *param = (*iter)->getAsTyped();
94 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000095 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070096 TQualifier qualifier = param->getQualifier();
97 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
98 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070099 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700100 break;
101 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000102 }
103 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700104 // Fully processed. No need to visit children.
105 visitChildren = false;
106 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000107 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000108 case EOpConstructMat2:
109 case EOpConstructMat3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700110 case EOpConstructMat4:
111 {
112 const TIntermSequence &sequence = *(node->getSequence());
113 if (sequence.size() == 1)
114 {
115 TIntermTyped *typed = sequence.front()->getAsTyped();
116 if (typed && typed->isMatrix())
117 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700118 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700119 }
120 }
121 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000122 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700123 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000124 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000125 }
126
127 return visitChildren;
128}
129
Geoff Lang7b9b2842015-06-15 11:02:49 -0700130void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000131{
132 mVersion = std::max(version, mVersion);
133}
134