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