• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 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 #ifndef COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_H_
8 #define COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_H_
9 
10 #include "compiler/translator/Compiler.h"
11 
12 namespace sh
13 {
14 
15 constexpr const char kUniformsVar[]               = "angleUniforms";
16 constexpr const char kViewport[]                  = "viewport";
17 constexpr const char kHalfRenderArea[]            = "halfRenderArea";
18 constexpr const char kFlipXY[]                    = "flipXY";
19 constexpr const char kNegFlipXY[]                 = "negFlipXY";
20 constexpr const char kClipDistancesEnabled[]      = "clipDistancesEnabled";
21 constexpr const char kXfbActiveUnpaused[]         = "xfbActiveUnpaused";
22 constexpr const char kXfbVerticesPerDraw[]        = "xfbVerticesPerDraw";
23 constexpr const char kXfbBufferOffsets[]          = "xfbBufferOffsets";
24 constexpr const char kAcbBufferOffsets[]          = "acbBufferOffsets";
25 constexpr const char kDepthRange[]                = "depthRange";
26 constexpr const char kPreRotation[]               = "preRotation";
27 constexpr const char kFragRotation[]              = "fragRotation";
28 constexpr const char kUnassignedAttributeString[] = " __unassigned_attribute__";
29 
30 class DriverUniform;
31 class SpecConst;
32 class TOutputMSL;
33 
34 typedef std::unordered_map<size_t, std::string> originalNamesMap;
35 typedef std::unordered_map<std::string, size_t> samplerBindingMap;
36 typedef std::unordered_map<std::string, size_t> textureBindingMap;
37 typedef std::unordered_map<std::string, size_t> userUniformBufferBindingMap;
38 typedef std::pair<size_t, size_t> uboBindingInfo;
39 struct UBOBindingInfo
40 {
41     size_t bindIndex = 0;
42     size_t arraySize = 0;
43 };
44 typedef std::unordered_map<std::string, UBOBindingInfo> uniformBufferBindingMap;
45 
46 class TranslatorMetalReflection
47 {
48   public:
TranslatorMetalReflection()49     TranslatorMetalReflection() : hasUBOs(false), hasFlatInput(false) {}
~TranslatorMetalReflection()50     ~TranslatorMetalReflection() {}
51 
addOriginalName(const size_t id,const std::string & name)52     void addOriginalName(const size_t id, const std::string &name)
53     {
54         originalNames.insert({id, name});
55     }
addSamplerBinding(const std::string & name,size_t samplerBinding)56     void addSamplerBinding(const std::string &name, size_t samplerBinding)
57     {
58         samplerBindings.insert({name, samplerBinding});
59     }
addTextureBinding(const std::string & name,size_t textureBinding)60     void addTextureBinding(const std::string &name, size_t textureBinding)
61     {
62         textureBindings.insert({name, textureBinding});
63     }
addUserUniformBufferBinding(const std::string & name,size_t userUniformBufferBinding)64     void addUserUniformBufferBinding(const std::string &name, size_t userUniformBufferBinding)
65     {
66         userUniformBufferBindings.insert({name, userUniformBufferBinding});
67     }
addUniformBufferBinding(const std::string & name,UBOBindingInfo bindingInfo)68     void addUniformBufferBinding(const std::string &name, UBOBindingInfo bindingInfo)
69     {
70         uniformBufferBindings.insert({name, bindingInfo});
71     }
getOriginalName(const size_t id)72     std::string getOriginalName(const size_t id) { return originalNames.at(id); }
getSamplerBindings()73     samplerBindingMap getSamplerBindings() const { return samplerBindings; }
getTextureBindings()74     textureBindingMap getTextureBindings() const { return textureBindings; }
getUserUniformBufferBindings()75     userUniformBufferBindingMap getUserUniformBufferBindings() const
76     {
77         return userUniformBufferBindings;
78     }
getUniformBufferBindings()79     uniformBufferBindingMap getUniformBufferBindings() const { return uniformBufferBindings; }
getSamplerBinding(const std::string & name)80     size_t getSamplerBinding(const std::string &name) const
81     {
82         auto it = samplerBindings.find(name);
83         if (it != samplerBindings.end())
84         {
85             return it->second;
86         }
87         // If we can't find a matching sampler, assert out on Debug, and return an invalid value on
88         // release.
89         ASSERT(0);
90         return std::numeric_limits<size_t>::max();
91     }
getTextureBinding(const std::string & name)92     size_t getTextureBinding(const std::string &name) const
93     {
94         auto it = textureBindings.find(name);
95         if (it != textureBindings.end())
96         {
97             return it->second;
98         }
99         // If we can't find a matching texture, assert out on Debug, and return an invalid value on
100         // release.
101         ASSERT(0);
102         return std::numeric_limits<size_t>::max();
103     }
getUserUniformBufferBinding(const std::string & name)104     size_t getUserUniformBufferBinding(const std::string &name) const
105     {
106         auto it = userUniformBufferBindings.find(name);
107         if (it != userUniformBufferBindings.end())
108         {
109             return it->second;
110         }
111         // If we can't find a matching Uniform binding, assert out on Debug, and return an invalid
112         // value.
113         ASSERT(0);
114         return std::numeric_limits<size_t>::max();
115     }
getUniformBufferBinding(const std::string & name)116     UBOBindingInfo getUniformBufferBinding(const std::string &name) const
117     {
118         auto it = uniformBufferBindings.find(name);
119         if (it != uniformBufferBindings.end())
120         {
121             return it->second;
122         }
123         // If we can't find a matching UBO binding by name, assert out on Debug, and return an
124         // invalid value.
125         ASSERT(0);
126         return {.bindIndex = std::numeric_limits<size_t>::max(),
127                 .arraySize = std::numeric_limits<size_t>::max()};
128     }
reset()129     void reset()
130     {
131         hasUBOs      = false;
132         hasFlatInput = false;
133         originalNames.clear();
134         samplerBindings.clear();
135         textureBindings.clear();
136         userUniformBufferBindings.clear();
137         uniformBufferBindings.clear();
138     }
139 
140     bool hasUBOs      = false;
141     bool hasFlatInput = false;
142 
143   private:
144     originalNamesMap originalNames;
145     samplerBindingMap samplerBindings;
146     textureBindingMap textureBindings;
147     userUniformBufferBindingMap userUniformBufferBindings;
148     uniformBufferBindingMap uniformBufferBindings;
149 };
150 
151 class TranslatorMetalDirect : public TCompiler
152 {
153   public:
154     TranslatorMetalDirect(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
155 
156 #ifdef ANGLE_ENABLE_METAL
getAsTranslatorMetalDirect()157     TranslatorMetalDirect *getAsTranslatorMetalDirect() override { return this; }
158 #endif
159 
enableEmulatedInstanceID(bool e)160     void enableEmulatedInstanceID(bool e) { mEmulatedInstanceID = e; }
getTranslatorMetalReflection()161     TranslatorMetalReflection *getTranslatorMetalReflection() { return &translatorMetalReflection; }
162 
163   protected:
164     bool translate(TIntermBlock *root,
165                    ShCompileOptions compileOptions,
166                    PerformanceDiagnostics *perfDiagnostics) override;
167 
168     ANGLE_NO_DISCARD bool translateImpl(TInfoSinkBase &sink,
169                                         TIntermBlock *root,
170                                         ShCompileOptions compileOptions,
171                                         PerformanceDiagnostics *perfDiagnostics,
172                                         SpecConst *specConst,
173                                         DriverUniform *driverUniforms);
174 
175     ANGLE_NO_DISCARD bool shouldFlattenPragmaStdglInvariantAll() override;
176 
177     ANGLE_NO_DISCARD bool transformDepthBeforeCorrection(TIntermBlock *root,
178                                                          const DriverUniform *driverUniforms);
179 
180     ANGLE_NO_DISCARD bool insertSampleMaskWritingLogic(TIntermBlock &root,
181                                                        DriverUniform &driverUniforms);
182     ANGLE_NO_DISCARD bool insertRasterizationDiscardLogic(TIntermBlock &root);
183 
184     ANGLE_NO_DISCARD TIntermSwizzle *getDriverUniformNegFlipYRef(
185         DriverUniform &driverUniforms) const;
186 
187     bool mEmulatedInstanceID = false;
188 
189     TranslatorMetalReflection translatorMetalReflection = {};
190 };
191 
192 }  // namespace sh
193 
194 #endif  // COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_H_
195