blob: 5c0c51914fda819541e31e54e277235558b1bcce [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 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070071 case EOpDeclaration:
alokp@chromium.org8d47c112012-09-06 16:03:23 +000072 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070073 const TIntermSequence &sequence = *(node->getSequence());
Olli Etuaho214c2d82015-04-27 14:49:13 +030074 if (sequence.front()->getAsTyped()->getType().isInvariant())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000075 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070076 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070077 }
78 break;
79 }
Jamie Madill3b5c2da2014-08-19 15:23:32 -040080 case EOpInvariantDeclaration:
Geoff Lang7b9b2842015-06-15 11:02:49 -070081 ensureVersionIsAtLeast(GLSL_VERSION_120);
Jamie Madill3b5c2da2014-08-19 15:23:32 -040082 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070083 case EOpParameters:
84 {
85 const TIntermSequence &params = *(node->getSequence());
86 for (TIntermSequence::const_iterator iter = params.begin();
87 iter != params.end(); ++iter)
88 {
89 const TIntermTyped *param = (*iter)->getAsTyped();
90 if (param->isArray())
alokp@chromium.org8d47c112012-09-06 16:03:23 +000091 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070092 TQualifier qualifier = param->getQualifier();
93 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
94 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070095 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070096 break;
97 }
alokp@chromium.org8d47c112012-09-06 16:03:23 +000098 }
99 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700100 // Fully processed. No need to visit children.
101 visitChildren = false;
102 break;
alokp@chromium.org8d47c112012-09-06 16:03:23 +0000103 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000104 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400105 case EOpConstructMat2x3:
106 case EOpConstructMat2x4:
107 case EOpConstructMat3x2:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000108 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400109 case EOpConstructMat3x4:
110 case EOpConstructMat4x2:
111 case EOpConstructMat4x3:
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700112 case EOpConstructMat4:
113 {
114 const TIntermSequence &sequence = *(node->getSequence());
115 if (sequence.size() == 1)
116 {
117 TIntermTyped *typed = sequence.front()->getAsTyped();
118 if (typed && typed->isMatrix())
119 {
Geoff Lang7b9b2842015-06-15 11:02:49 -0700120 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700121 }
122 }
123 break;
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000124 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700125 default:
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000126 break;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000127 }
128
129 return visitChildren;
130}
131
Geoff Lang7b9b2842015-06-15 11:02:49 -0700132void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000133{
134 mVersion = std::max(version, mVersion);
135}
136