blob: 5e58920c878d3c6717e58a76c5458dbcfdb22b91 [file] [log] [blame]
alokp@chromium.org76b82082010-03-24 17:59:39 +00001//
Jamie Madill02f20dd2013-09-12 12:07:42 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org76b82082010-03-24 17:59:39 +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/OutputGLSL.h"
alokp@chromium.org76b82082010-03-24 17:59:39 +00008
Olli Etuahocccf2b02017-07-05 14:50:54 +03009#include "compiler/translator/Compiler.h"
10
Jamie Madill45bcc782016-11-07 13:58:48 -050011namespace sh
12{
13
Qiankun Miao89dd8f32016-11-09 12:59:30 +000014TOutputGLSL::TOutputGLSL(TInfoSinkBase &objSink,
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +000015 ShArrayIndexClampingStrategy clampingStrategy,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000016 ShHashFunction64 hashFunction,
Qiankun Miao89dd8f32016-11-09 12:59:30 +000017 NameMap &nameMap,
Olli Etuahoa5e693a2017-07-13 16:07:26 +030018 TSymbolTable *symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +000019 sh::GLenum shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080020 int shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +080021 ShShaderOutput output,
22 ShCompileOptions compileOptions)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080023 : TOutputGLSLBase(objSink,
24 clampingStrategy,
25 hashFunction,
26 nameMap,
27 symbolTable,
Qiankun Miao89dd8f32016-11-09 12:59:30 +000028 shaderType,
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080029 shaderVersion,
Qiankun Miao705a9192016-08-29 10:05:27 +080030 output,
31 compileOptions)
alokp@chromium.org76b82082010-03-24 17:59:39 +000032{
33}
34
zmo@google.com5601ea02011-06-10 18:23:25 +000035bool TOutputGLSL::writeVariablePrecision(TPrecision)
alokp@chromium.org76b82082010-03-24 17:59:39 +000036{
alokp@chromium.org76b82082010-03-24 17:59:39 +000037 return false;
38}
Jamie Madill2aeb26a2013-07-08 14:02:55 -040039
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080040void TOutputGLSL::visitSymbol(TIntermSymbol *node)
Jamie Madill2aeb26a2013-07-08 14:02:55 -040041{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050042 TInfoSinkBase &out = objSink();
Jamie Madill2aeb26a2013-07-08 14:02:55 -040043
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020044 // All the special cases are built-ins, so if it's not a built-in we can return early.
45 if (node->variable().symbolType() != SymbolType::BuiltIn)
46 {
47 TOutputGLSLBase::visitSymbol(node);
48 return;
49 }
50
51 // Some built-ins get a special translation.
52 const TString &name = node->getName();
53 if (name == "gl_FragDepthEXT")
Jamie Madill2aeb26a2013-07-08 14:02:55 -040054 {
55 out << "gl_FragDepth";
56 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020057 else if (name == "gl_FragColor" && sh::IsGLSL130OrNewer(getShaderOutput()))
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080058 {
59 out << "webgl_FragColor";
60 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020061 else if (name == "gl_FragData" && sh::IsGLSL130OrNewer(getShaderOutput()))
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080062 {
63 out << "webgl_FragData";
64 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020065 else if (name == "gl_SecondaryFragColorEXT")
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030066 {
67 out << "angle_SecondaryFragColor";
68 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020069 else if (name == "gl_SecondaryFragDataEXT")
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030070 {
71 out << "angle_SecondaryFragData";
72 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -040073 else
74 {
75 TOutputGLSLBase::visitSymbol(node);
76 }
77}
Nicolas Capens46485082014-04-15 13:12:50 -040078
Olli Etuahoec9232b2017-03-27 17:01:37 +030079TString TOutputGLSL::translateTextureFunction(const TString &name)
Nicolas Capens46485082014-04-15 13:12:50 -040080{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050081 static const char *simpleRename[] = {"texture2DLodEXT",
82 "texture2DLod",
83 "texture2DProjLodEXT",
84 "texture2DProjLod",
85 "textureCubeLodEXT",
86 "textureCubeLod",
87 "texture2DGradEXT",
88 "texture2DGradARB",
89 "texture2DProjGradEXT",
90 "texture2DProjGradARB",
91 "textureCubeGradEXT",
92 "textureCubeGradARB",
Yunchao Hef81ce4a2017-04-24 10:49:17 +080093 nullptr,
94 nullptr};
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080095 static const char *legacyToCoreRename[] = {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050096 "texture2D", "texture", "texture2DProj", "textureProj", "texture2DLod", "textureLod",
97 "texture2DProjLod", "textureProjLod", "texture2DRect", "texture", "textureCube", "texture",
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -080098 "textureCubeLod", "textureLod",
99 // Extensions
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500100 "texture2DLodEXT", "textureLod", "texture2DProjLodEXT", "textureProjLod",
101 "textureCubeLodEXT", "textureLod", "texture2DGradEXT", "textureGrad",
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800102 "texture2DProjGradEXT", "textureProjGrad", "textureCubeGradEXT", "textureGrad", nullptr,
103 nullptr};
Jamie Madillacb4b812016-11-07 13:50:29 -0500104 const char **mapping =
105 (sh::IsGLSL130OrNewer(getShaderOutput())) ? legacyToCoreRename : simpleRename;
Nicolas Capens46485082014-04-15 13:12:50 -0400106
Yunchao He4f285442017-04-21 12:15:49 +0800107 for (int i = 0; mapping[i] != nullptr; i += 2)
Zhenyao Mo05b6b7f2015-03-02 17:08:09 -0800108 {
109 if (name == mapping[i])
110 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500111 return mapping[i + 1];
Nicolas Capens46485082014-04-15 13:12:50 -0400112 }
113 }
114
115 return name;
116}
Jamie Madill45bcc782016-11-07 13:58:48 -0500117
118} // namespace sh