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,ShArrayIndexClampingStrategy clampingStrategy,ShHashFunction64 hashFunction,NameMap & nameMap,TSymbolTable * symbolTable,sh::GLenum shaderType,int shaderVersion,ShShaderOutput output,ShCompileOptions compileOptions)14 TOutputGLSL::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
writeVariablePrecision(TPrecision)34 bool TOutputGLSL::writeVariablePrecision(TPrecision)
35 {
36 return false;
37 }
38
visitSymbol(TIntermSymbol * node)39 void 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
translateTextureFunction(const ImmutableString & name,const ShCompileOptions & option)78 ImmutableString TOutputGLSL::translateTextureFunction(const ImmutableString &name,
79 const ShCompileOptions &option)
80 {
81 // Check WEBGL_video_texture invocation first.
82 if (name == "textureVideoWEBGL")
83 {
84 if (option & SH_TAKE_VIDEO_TEXTURE_AS_EXTERNAL_OES)
85 {
86 // TODO(http://anglebug.com/3889): Implement external image situation.
87 UNIMPLEMENTED();
88 return ImmutableString("");
89 }
90 else
91 {
92 // Default translating textureVideoWEBGL to texture2D.
93 return ImmutableString("texture2D");
94 }
95 }
96
97 static const char *simpleRename[] = {"texture2DLodEXT",
98 "texture2DLod",
99 "texture2DProjLodEXT",
100 "texture2DProjLod",
101 "textureCubeLodEXT",
102 "textureCubeLod",
103 "texture2DGradEXT",
104 "texture2DGradARB",
105 "texture2DProjGradEXT",
106 "texture2DProjGradARB",
107 "textureCubeGradEXT",
108 "textureCubeGradARB",
109 nullptr,
110 nullptr};
111 static const char *legacyToCoreRename[] = {
112 "texture2D", "texture", "texture2DProj", "textureProj", "texture2DLod", "textureLod",
113 "texture2DProjLod", "textureProjLod", "texture2DRect", "texture", "texture2DRectProj",
114 "textureProj", "textureCube", "texture", "textureCubeLod", "textureLod",
115 // Extensions
116 "texture2DLodEXT", "textureLod", "texture2DProjLodEXT", "textureProjLod",
117 "textureCubeLodEXT", "textureLod", "texture2DGradEXT", "textureGrad",
118 "texture2DProjGradEXT", "textureProjGrad", "textureCubeGradEXT", "textureGrad", "texture3D",
119 "texture", "texture3DProj", "textureProj", "texture3DLod", "textureLod", "texture3DProjLod",
120 "textureProjLod", nullptr, nullptr};
121 const char **mapping =
122 (sh::IsGLSL130OrNewer(getShaderOutput())) ? legacyToCoreRename : simpleRename;
123
124 for (int i = 0; mapping[i] != nullptr; i += 2)
125 {
126 if (name == mapping[i])
127 {
128 return ImmutableString(mapping[i + 1]);
129 }
130 }
131
132 return name;
133 }
134
135 } // namespace sh
136