blob: e9d654c104bb66844e292ca30a157aaebf0afdd4 [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"
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020010#include "compiler/translator/Symbol.h"
Olli Etuaho8c04d072016-12-09 15:30:48 +000011
Jamie Madill45bcc782016-11-07 13:58:48 -050012namespace sh
13{
14
Geoff Lang7b9b2842015-06-15 11:02:49 -070015int ShaderOutputTypeToGLSLVersion(ShShaderOutput output)
16{
17 switch (output)
18 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050019 case SH_GLSL_130_OUTPUT:
20 return GLSL_VERSION_130;
21 case SH_GLSL_140_OUTPUT:
22 return GLSL_VERSION_140;
23 case SH_GLSL_150_CORE_OUTPUT:
24 return GLSL_VERSION_150;
25 case SH_GLSL_330_CORE_OUTPUT:
26 return GLSL_VERSION_330;
27 case SH_GLSL_400_CORE_OUTPUT:
28 return GLSL_VERSION_400;
29 case SH_GLSL_410_CORE_OUTPUT:
30 return GLSL_VERSION_410;
31 case SH_GLSL_420_CORE_OUTPUT:
32 return GLSL_VERSION_420;
33 case SH_GLSL_430_CORE_OUTPUT:
34 return GLSL_VERSION_430;
35 case SH_GLSL_440_CORE_OUTPUT:
36 return GLSL_VERSION_440;
37 case SH_GLSL_450_CORE_OUTPUT:
38 return GLSL_VERSION_450;
39 case SH_GLSL_COMPATIBILITY_OUTPUT:
40 return GLSL_VERSION_110;
41 default:
42 UNREACHABLE();
43 return 0;
Geoff Lang7b9b2842015-06-15 11:02:49 -070044 }
45}
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000046
alokp@chromium.org8d47c112012-09-06 16:03:23 +000047// We need to scan for the following:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000048// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
49// but only at the global scope.
50// 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader
51// but inside any scope.
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +000052// 3. Call to a matrix constructor with another matrix as argument.
53// (These constructors were reserved in GLSL version 1.10.)
alokp@chromium.org8d47c112012-09-06 16:03:23 +000054// 4. Arrays as "out" function parameters.
55// GLSL spec section 6.1.1: "When calling a function, expressions that do
56// not evaluate to l-values cannot be passed to parameters declared as
57// out or inout."
58// GLSL 1.1 section 5.8: "Other binary or unary expressions,
59// non-dereferenced arrays, function names, swizzles with repeated fields,
60// and constants cannot be l-values."
61// GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that
62// are built-in types, entire structures or arrays... are all l-values."
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000063//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050064TVersionGLSL::TVersionGLSL(sh::GLenum type, const TPragma &pragma, ShShaderOutput output)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030065 : TIntermTraverser(true, false, false)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000066{
Geoff Lang7b9b2842015-06-15 11:02:49 -070067 mVersion = ShaderOutputTypeToGLSLVersion(output);
68 if (pragma.stdgl.invariantAll)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080069 {
Geoff Lang7b9b2842015-06-15 11:02:49 -070070 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080071 }
Olli Etuaho8c04d072016-12-09 15:30:48 +000072 if (type == GL_COMPUTE_SHADER)
73 {
74 ensureVersionIsAtLeast(GLSL_VERSION_430);
75 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000076}
77
Zhenyao Moe40d1e92014-07-16 17:40:36 -070078void TVersionGLSL::visitSymbol(TIntermSymbol *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000079{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020080 if (node->variable().symbolType() == SymbolType::BuiltIn && node->getName() == "gl_PointCoord")
Geoff Lang7b9b2842015-06-15 11:02:49 -070081 {
82 ensureVersionIsAtLeast(GLSL_VERSION_120);
83 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000084}
85
Olli Etuaho13389b62016-10-16 11:48:18 +010086bool TVersionGLSL::visitDeclaration(Visit, TIntermDeclaration *node)
87{
88 const TIntermSequence &sequence = *(node->getSequence());
89 if (sequence.front()->getAsTyped()->getType().isInvariant())
90 {
91 ensureVersionIsAtLeast(GLSL_VERSION_120);
92 }
93 return true;
94}
95
Olli Etuahobf4e1b72016-12-09 11:30:15 +000096bool TVersionGLSL::visitInvariantDeclaration(Visit, TIntermInvariantDeclaration *node)
97{
98 ensureVersionIsAtLeast(GLSL_VERSION_120);
99 return true;
100}
101
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000102bool TVersionGLSL::visitFunctionPrototype(Visit, TIntermFunctionPrototype *node)
103{
104 const TIntermSequence &params = *(node->getSequence());
105 for (TIntermSequence::const_iterator iter = params.begin(); iter != params.end(); ++iter)
106 {
107 const TIntermTyped *param = (*iter)->getAsTyped();
108 if (param->isArray())
109 {
110 TQualifier qualifier = param->getQualifier();
111 if ((qualifier == EvqOut) || (qualifier == EvqInOut))
112 {
113 ensureVersionIsAtLeast(GLSL_VERSION_120);
114 break;
115 }
116 }
117 }
118 // Fully processed. No need to visit children.
119 return false;
120}
121
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700122bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000123{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300124 if (node->getOp() == EOpConstruct && node->getType().isMatrix())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700125 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300126 const TIntermSequence &sequence = *(node->getSequence());
127 if (sequence.size() == 1)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700128 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300129 TIntermTyped *typed = sequence.front()->getAsTyped();
130 if (typed && typed->isMatrix())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700131 {
Olli Etuaho8fab3202017-05-08 18:22:22 +0300132 ensureVersionIsAtLeast(GLSL_VERSION_120);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700133 }
kbr@chromium.orge26cb5e2011-01-18 21:27:02 +0000134 }
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000135 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000136 return true;
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000137}
138
Geoff Lang7b9b2842015-06-15 11:02:49 -0700139void TVersionGLSL::ensureVersionIsAtLeast(int version)
alokp@chromium.org9ecf3952010-10-13 19:28:25 +0000140{
141 mVersion = std::max(version, mVersion);
142}
143
Jamie Madill45bcc782016-11-07 13:58:48 -0500144} // namespace sh