1//
2// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "compiler/translator/OutputGLSL.h"
8
9#include "compiler/translator/Compiler.h"
10
11namespace sh
12{
13
14TOutputGLSL::TOutputGLSL(TInfoSinkBase &objSink,
15 ShArrayIndexClampingStrategy clampingStrategy,
16 ShHashFunction64 hashFunction,
17 NameMap &nameMap,
18 TSymbolTable *symbolTable,
19 sh::GLenum shaderType,
20 int shaderVersion,
21 ShShaderOutput output,
22 ShCompileOptions compileOptions)
23 : TOutputGLSLBase(objSink,
24 clampingStrategy,
25 hashFunction,
26 nameMap,
27 symbolTable,
28 shaderType,
29 shaderVersion,
30 output,
31 compileOptions)
32{}
33
34bool TOutputGLSL::writeVariablePrecision(TPrecision)
35{
36 return false;
37}
38
39void TOutputGLSL::visitSymbol(TIntermSymbol *node)
40{
41 TInfoSinkBase &out = objSink();
42
43 // All the special cases are built-ins, so if it's not a built-in we can return early.
44 if (node->variable().symbolType() != SymbolType::BuiltIn)
45 {
46 TOutputGLSLBase::visitSymbol(node);
47 return;
48 }
49
50 // Some built-ins get a special translation.
51 const ImmutableString &name = node->getName();
52 if (name == "gl_FragDepthEXT")
53 {
54 out << "gl_FragDepth";
55 }
56 else if (name == "gl_FragColor" && sh::IsGLSL130OrNewer(getShaderOutput()))
57 {
58 out << "webgl_FragColor";
59 }
60 else if (name == "gl_FragData" && sh::IsGLSL130OrNewer(getShaderOutput()))
61 {
62 out << "webgl_FragData";
63 }
64 else if (name == "gl_SecondaryFragColorEXT")
65 {
66 out << "angle_SecondaryFragColor";
67 }
68 else if (name == "gl_SecondaryFragDataEXT")
69 {
70 out << "angle_SecondaryFragData";
71 }
72 else
73 {
74 TOutputGLSLBase::visitSymbol(node);
75 }
76}
77
78ImmutableString TOutputGLSL::translateTextureFunction(const ImmutableString &name)
79{
80 static const char *simpleRename[] = {"texture2DLodEXT",
81 "texture2DLod",
82 "texture2DProjLodEXT",
83 "texture2DProjLod",
84 "textureCubeLodEXT",
85 "textureCubeLod",
86 "texture2DGradEXT",
87 "texture2DGradARB",
88 "texture2DProjGradEXT",
89 "texture2DProjGradARB",
90 "textureCubeGradEXT",
91 "textureCubeGradARB",
92 nullptr,
93 nullptr};
94 static const char *legacyToCoreRename[] = {
95 "texture2D", "texture", "texture2DProj", "textureProj", "texture2DLod", "textureLod",
96 "texture2DProjLod", "textureProjLod", "texture2DRect", "texture", "texture2DRectProj",
97 "textureProj", "textureCube", "texture", "textureCubeLod", "textureLod",
98 // Extensions
99 "texture2DLodEXT", "textureLod", "texture2DProjLodEXT", "textureProjLod",
100 "textureCubeLodEXT", "textureLod", "texture2DGradEXT", "textureGrad",
101 "texture2DProjGradEXT", "textureProjGrad", "textureCubeGradEXT", "textureGrad", nullptr,
102 nullptr};
103 const char **mapping =
104 (sh::IsGLSL130OrNewer(getShaderOutput())) ? legacyToCoreRename : simpleRename;
105
106 for (int i = 0; mapping[i] != nullptr; i += 2)
107 {
108 if (name == mapping[i])
109 {
110 return ImmutableString(mapping[i + 1]);
111 }
112 }
113
114 return name;
115}
116
117} // namespace sh
118