1 //
2 // Copyright 2002 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
11 namespace sh
12 {
13
TOutputGLSL(TInfoSinkBase & objSink,ShHashFunction64 hashFunction,NameMap & nameMap,TSymbolTable * symbolTable,sh::GLenum shaderType,int shaderVersion,ShShaderOutput output,ShCompileOptions compileOptions)14 TOutputGLSL::TOutputGLSL(TInfoSinkBase &objSink,
15 ShHashFunction64 hashFunction,
16 NameMap &nameMap,
17 TSymbolTable *symbolTable,
18 sh::GLenum shaderType,
19 int shaderVersion,
20 ShShaderOutput output,
21 ShCompileOptions compileOptions)
22 : TOutputGLSLBase(objSink,
23 hashFunction,
24 nameMap,
25 symbolTable,
26 shaderType,
27 shaderVersion,
28 output,
29 compileOptions)
30 {}
31
writeVariablePrecision(TPrecision)32 bool TOutputGLSL::writeVariablePrecision(TPrecision)
33 {
34 return false;
35 }
36
visitSymbol(TIntermSymbol * node)37 void TOutputGLSL::visitSymbol(TIntermSymbol *node)
38 {
39 TInfoSinkBase &out = objSink();
40
41 // All the special cases are built-ins, so if it's not a built-in we can return early.
42 if (node->variable().symbolType() != SymbolType::BuiltIn)
43 {
44 TOutputGLSLBase::visitSymbol(node);
45 return;
46 }
47
48 // Some built-ins get a special translation.
49 const ImmutableString &name = node->getName();
50 if (name == "gl_FragDepthEXT")
51 {
52 out << "gl_FragDepth";
53 }
54 else if (name == "gl_FragColor" && sh::IsGLSL130OrNewer(getShaderOutput()))
55 {
56 out << "webgl_FragColor";
57 }
58 else if (name == "gl_FragData" && sh::IsGLSL130OrNewer(getShaderOutput()))
59 {
60 out << "webgl_FragData";
61 }
62 else if (name == "gl_SecondaryFragColorEXT")
63 {
64 out << "webgl_SecondaryFragColor";
65 }
66 else if (name == "gl_SecondaryFragDataEXT")
67 {
68 out << "webgl_SecondaryFragData";
69 }
70 else
71 {
72 TOutputGLSLBase::visitSymbol(node);
73 }
74 }
75
translateTextureFunction(const ImmutableString & name,const ShCompileOptions & option)76 ImmutableString TOutputGLSL::translateTextureFunction(const ImmutableString &name,
77 const ShCompileOptions &option)
78 {
79 // Check WEBGL_video_texture invocation first.
80 if (name == "textureVideoWEBGL")
81 {
82 if (option & SH_TAKE_VIDEO_TEXTURE_AS_EXTERNAL_OES)
83 {
84 // TODO(http://anglebug.com/3889): Implement external image situation.
85 UNIMPLEMENTED();
86 return ImmutableString("");
87 }
88 else
89 {
90 // Default translating textureVideoWEBGL to texture2D.
91 return ImmutableString("texture2D");
92 }
93 }
94
95 static const char *simpleRename[] = {"texture2DLodEXT",
96 "texture2DLod",
97 "texture2DProjLodEXT",
98 "texture2DProjLod",
99 "textureCubeLodEXT",
100 "textureCubeLod",
101 "texture2DGradEXT",
102 "texture2DGradARB",
103 "texture2DProjGradEXT",
104 "texture2DProjGradARB",
105 "textureCubeGradEXT",
106 "textureCubeGradARB",
107 nullptr,
108 nullptr};
109 static const char *legacyToCoreRename[] = {
110 "texture2D", "texture", "texture2DProj", "textureProj", "texture2DLod", "textureLod",
111 "texture2DProjLod", "textureProjLod", "texture2DRect", "texture", "texture2DRectProj",
112 "textureProj", "textureCube", "texture", "textureCubeLod", "textureLod",
113 // Extensions
114 "texture2DLodEXT", "textureLod", "texture2DProjLodEXT", "textureProjLod",
115 "textureCubeLodEXT", "textureLod", "texture2DGradEXT", "textureGrad",
116 "texture2DProjGradEXT", "textureProjGrad", "textureCubeGradEXT", "textureGrad", "texture3D",
117 "texture", "texture3DProj", "textureProj", "texture3DLod", "textureLod", "texture3DProjLod",
118 "textureProjLod", "shadow2DEXT", "texture", "shadow2DProjEXT", "textureProj", nullptr,
119 nullptr};
120 const char **mapping =
121 (sh::IsGLSL130OrNewer(getShaderOutput())) ? legacyToCoreRename : simpleRename;
122
123 for (int i = 0; mapping[i] != nullptr; i += 2)
124 {
125 if (name == mapping[i])
126 {
127 return ImmutableString(mapping[i + 1]);
128 }
129 }
130
131 return name;
132 }
133
134 } // namespace sh
135