Flutter Impeller
proc_table_gles.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
7 
8 #include <functional>
9 #include <string>
10 
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/mapping.h"
16 
17 /// Enable to allow GLES to push/pop labels for usage in GPU traces
18 #define IP_ENABLE_GLES_LABELING false
19 
20 namespace impeller {
21 
22 const char* GLErrorToString(GLenum value);
23 bool GLErrorIsFatal(GLenum value);
24 
26  const PFNGLGETERRORPROC error_fn;
27 
28  // TODO(135922) Change to string_view.
29  const char* name;
30 
31  AutoErrorCheck(PFNGLGETERRORPROC error, const char* name)
32  : error_fn(error), name(name) {}
33 
35  if (error_fn) {
36  auto error = error_fn();
37  if (error == GL_NO_ERROR) {
38  return;
39  }
40  if (GLErrorIsFatal(error)) {
41  FML_LOG(FATAL) << "Fatal GL Error " << GLErrorToString(error) << "("
42  << error << ")" << " encountered on call to " << name;
43  } else {
44  FML_LOG(ERROR) << "GL Error " << GLErrorToString(error) << "(" << error
45  << ")" << " encountered on call to " << name;
46  }
47  }
48  }
49 };
50 
51 template <class Type>
52 void BuildGLArgumentsStream(std::stringstream& stream, Type arg) {
53  stream << arg;
54 }
55 
56 constexpr void BuildGLArgumentsStream(std::stringstream& stream) {}
57 
58 template <class Type, class... Rest>
59 void BuildGLArgumentsStream(std::stringstream& stream,
60  Type arg,
61  Rest... other_args) {
62  BuildGLArgumentsStream(stream, arg);
63  stream << ", ";
64  BuildGLArgumentsStream(stream, other_args...);
65 }
66 
67 template <class... Type>
68 [[nodiscard]] std::string BuildGLArguments(Type... args) {
69  std::stringstream stream;
70  stream << "(";
71  BuildGLArgumentsStream(stream, args...);
72  stream << ")";
73  return stream.str();
74 }
75 
76 template <class T>
77 struct GLProc {
78  using GLFunctionType = T;
79 
80  //----------------------------------------------------------------------------
81  /// The name of the GL function.
82  ///
83  // TODO(135922) Change to string_view.
84  const char* name = nullptr;
85 
86  //----------------------------------------------------------------------------
87  /// The pointer to the GL function.
88  ///
89  GLFunctionType* function = nullptr;
90 
91  //----------------------------------------------------------------------------
92  /// An optional error function. If present, all calls will be followed by an
93  /// error check.
94  ///
95  PFNGLGETERRORPROC error_fn = nullptr;
96 
97  //----------------------------------------------------------------------------
98  /// Whether the OpenGL call and its arguments should be logged.
99  ///
100  /// Only works in IMPELLER_DEBUG and for environments where traditional
101  /// tracing is hard. Expect log spam and only use during development.
102  ///
103  bool log_calls = false;
104 
105  //----------------------------------------------------------------------------
106  /// @brief Call the GL function with the appropriate parameters. Lookup
107  /// the documentation for the GL function being called to
108  /// understand the arguments and return types. The arguments
109  /// types must match and will be type checked.
110  ///
111  template <class... Args>
112  auto operator()(Args&&... args) const {
113 #if defined(IMPELLER_DEBUG) && !defined(NDEBUG)
114  AutoErrorCheck error(error_fn, name);
115  // We check for the existence of extensions, and reset the function pointer
116  // but it's still called unconditionally below, and will segfault. This
117  // validation log will at least give us a hint as to what's going on.
118  FML_CHECK(IsAvailable()) << "GL function " << name << " is not available. "
119  << "This is likely due to a missing extension.";
120  if (log_calls) {
121  FML_LOG(IMPORTANT) << name << BuildGLArguments(args...);
122  }
123 #endif // defined(IMPELLER_DEBUG) && !defined(NDEBUG)
124  return function(std::forward<Args>(args)...);
125  }
126 
127  constexpr bool IsAvailable() const { return function != nullptr; }
128 
129  void Reset() {
130  function = nullptr;
131  error_fn = nullptr;
132  }
133 };
134 
135 #define FOR_EACH_IMPELLER_PROC(PROC) \
136  PROC(ActiveTexture); \
137  PROC(AttachShader); \
138  PROC(BindAttribLocation); \
139  PROC(BindBuffer); \
140  PROC(BindFramebuffer); \
141  PROC(BindRenderbuffer); \
142  PROC(BindTexture); \
143  PROC(BindVertexArray); \
144  PROC(BlendEquationSeparate); \
145  PROC(BlendFuncSeparate); \
146  PROC(BufferData); \
147  PROC(BufferSubData); \
148  PROC(CheckFramebufferStatus); \
149  PROC(Clear); \
150  PROC(ClearColor); \
151  PROC(ClearStencil); \
152  PROC(ColorMask); \
153  PROC(CompileShader); \
154  PROC(CreateProgram); \
155  PROC(CreateShader); \
156  PROC(CullFace); \
157  PROC(DeleteBuffers); \
158  PROC(DeleteFramebuffers); \
159  PROC(DeleteProgram); \
160  PROC(DeleteRenderbuffers); \
161  PROC(DeleteShader); \
162  PROC(DeleteTextures); \
163  PROC(DeleteVertexArrays); \
164  PROC(DepthFunc); \
165  PROC(DepthMask); \
166  PROC(DetachShader); \
167  PROC(Disable); \
168  PROC(DisableVertexAttribArray); \
169  PROC(DrawArrays); \
170  PROC(DrawElements); \
171  PROC(Enable); \
172  PROC(EnableVertexAttribArray); \
173  PROC(Finish); \
174  PROC(Flush); \
175  PROC(FramebufferRenderbuffer); \
176  PROC(FramebufferTexture2D); \
177  PROC(FrontFace); \
178  PROC(GenBuffers); \
179  PROC(GenerateMipmap); \
180  PROC(GenFramebuffers); \
181  PROC(GenRenderbuffers); \
182  PROC(GenTextures); \
183  PROC(GenVertexArrays); \
184  PROC(GetActiveUniform); \
185  PROC(GetBooleanv); \
186  PROC(GetFloatv); \
187  PROC(GetFramebufferAttachmentParameteriv); \
188  PROC(GetIntegerv); \
189  PROC(GetProgramInfoLog); \
190  PROC(GetProgramiv); \
191  PROC(GetShaderInfoLog); \
192  PROC(GetShaderiv); \
193  PROC(GetString); \
194  PROC(GetStringi); \
195  PROC(GetUniformLocation); \
196  PROC(IsBuffer); \
197  PROC(IsFramebuffer); \
198  PROC(IsProgram); \
199  PROC(IsRenderbuffer); \
200  PROC(IsShader); \
201  PROC(IsTexture); \
202  PROC(LinkProgram); \
203  PROC(PixelStorei); \
204  PROC(RenderbufferStorage); \
205  PROC(Scissor); \
206  PROC(ShaderBinary); \
207  PROC(ShaderSource); \
208  PROC(StencilFuncSeparate); \
209  PROC(StencilMaskSeparate); \
210  PROC(StencilOpSeparate); \
211  PROC(TexImage2D); \
212  PROC(TexSubImage2D); \
213  PROC(TexParameteri); \
214  PROC(TexParameterfv); \
215  PROC(Uniform1fv); \
216  PROC(Uniform1i); \
217  PROC(Uniform2fv); \
218  PROC(Uniform3fv); \
219  PROC(Uniform4fv); \
220  PROC(UniformMatrix4fv); \
221  PROC(UseProgram); \
222  PROC(VertexAttribPointer); \
223  PROC(Viewport); \
224  PROC(GetShaderSource); \
225  PROC(ReadPixels);
226 
227 // Calls specific to OpenGLES.
228 void(glClearDepthf)(GLfloat depth);
229 void(glDepthRangef)(GLfloat n, GLfloat f);
230 
231 #define FOR_EACH_IMPELLER_ES_ONLY_PROC(PROC) \
232  PROC(ClearDepthf); \
233  PROC(DepthRangef);
234 
235 // Calls specific to desktop GL.
236 void(glClearDepth)(GLdouble depth);
237 void(glDepthRange)(GLdouble n, GLdouble f);
238 
239 #define FOR_EACH_IMPELLER_DESKTOP_ONLY_PROC(PROC) \
240  PROC(ClearDepth); \
241  PROC(DepthRange);
242 
243 #define FOR_EACH_IMPELLER_GLES3_PROC(PROC) \
244  PROC(FenceSync); \
245  PROC(DeleteSync); \
246  PROC(GetActiveUniformBlockiv); \
247  PROC(GetActiveUniformBlockName); \
248  PROC(GetUniformBlockIndex); \
249  PROC(UniformBlockBinding); \
250  PROC(BindBufferRange); \
251  PROC(WaitSync); \
252  PROC(RenderbufferStorageMultisample) \
253  PROC(BlitFramebuffer);
254 
255 #define FOR_EACH_IMPELLER_EXT_PROC(PROC) \
256  PROC(DebugMessageControlKHR); \
257  PROC(DiscardFramebufferEXT); \
258  PROC(FramebufferTexture2DMultisampleEXT); \
259  PROC(PushDebugGroupKHR); \
260  PROC(PopDebugGroupKHR); \
261  PROC(ObjectLabelKHR); \
262  PROC(RenderbufferStorageMultisampleEXT); \
263  PROC(GenQueriesEXT); \
264  PROC(DeleteQueriesEXT); \
265  PROC(GetQueryObjectui64vEXT); \
266  PROC(BeginQueryEXT); \
267  PROC(EndQueryEXT); \
268  PROC(GetQueryObjectuivEXT); \
269  PROC(BlitFramebufferANGLE);
270 
271 enum class DebugResourceType {
272  kTexture,
273  kBuffer,
274  kProgram,
275  kShader,
277  kFrameBuffer,
278  kFence,
279 };
280 
282  public:
283  using Resolver = std::function<void*(const char* function_name)>;
284  explicit ProcTableGLES(Resolver resolver);
285  ProcTableGLES(ProcTableGLES&& other) = default;
286 
288 
289 #define IMPELLER_PROC(name) \
290  GLProc<decltype(gl##name)> name = {"gl" #name, nullptr};
291 
297 
298 #undef IMPELLER_PROC
299 
300  bool IsValid() const;
301 
302  /// @brief Set the source for the attached [shader].
303  ///
304  /// Optionally, [defines] may contain a string value that will be
305  /// append to the shader source after the version marker. This can be used to
306  /// support static specialization. For example, setting "#define Foo 1".
307  void ShaderSourceMapping(GLuint shader,
308  const fml::Mapping& mapping,
309  const std::vector<Scalar>& defines = {}) const;
310 
311  const DescriptionGLES* GetDescription() const;
312 
313  const std::shared_ptr<const CapabilitiesGLES>& GetCapabilities() const;
314 
315  std::string DescribeCurrentFramebuffer() const;
316 
317  std::string GetProgramInfoLogString(GLuint program) const;
318 
319  bool IsCurrentFramebufferComplete() const;
320 
321  bool SupportsDebugLabels() const;
322 
324  GLint name,
325  std::string_view label) const;
326 
327  void PushDebugGroup(const std::string& string) const;
328 
329  void PopDebugGroup() const;
330 
331  // Visible For testing.
332  std::optional<std::string> ComputeShaderWithDefines(
333  const fml::Mapping& mapping,
334  const std::vector<Scalar>& defines) const;
335 
336  private:
337  bool is_valid_ = false;
338  std::unique_ptr<DescriptionGLES> description_;
339  std::shared_ptr<const CapabilitiesGLES> capabilities_;
340  GLint debug_label_max_length_ = 0;
341 
342  ProcTableGLES(const ProcTableGLES&) = delete;
343 
344  ProcTableGLES& operator=(const ProcTableGLES&) = delete;
345 };
346 
347 } // namespace impeller
348 
349 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
GLenum type
FOR_EACH_IMPELLER_ES_ONLY_PROC(IMPELLER_PROC)
ProcTableGLES(ProcTableGLES &&other)=default
std::optional< std::string > ComputeShaderWithDefines(const fml::Mapping &mapping, const std::vector< Scalar > &defines) const
bool SetDebugLabel(DebugResourceType type, GLint name, std::string_view label) const
FOR_EACH_IMPELLER_GLES3_PROC(IMPELLER_PROC)
std::function< void *(const char *function_name)> Resolver
void ShaderSourceMapping(GLuint shader, const fml::Mapping &mapping, const std::vector< Scalar > &defines={}) const
Set the source for the attached [shader].
FOR_EACH_IMPELLER_DESKTOP_ONLY_PROC(IMPELLER_PROC)
std::string GetProgramInfoLogString(GLuint program) const
bool SupportsDebugLabels() const
std::string DescribeCurrentFramebuffer() const
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
bool IsCurrentFramebufferComplete() const
ProcTableGLES(Resolver resolver)
FOR_EACH_IMPELLER_PROC(IMPELLER_PROC)
FOR_EACH_IMPELLER_EXT_PROC(IMPELLER_PROC)
void PushDebugGroup(const std::string &string) const
const DescriptionGLES * GetDescription() const
int32_t value
void() glClearDepth(GLdouble depth)
bool GLErrorIsFatal(GLenum value)
void() glDepthRangef(GLfloat n, GLfloat f)
const char * GLErrorToString(GLenum value)
void BuildGLArgumentsStream(std::stringstream &stream, Type arg)
void() glDepthRange(GLdouble n, GLdouble f)
void() glClearDepthf(GLfloat depth)
std::string BuildGLArguments(Type... args)
#define IMPELLER_PROC(name)
AutoErrorCheck(PFNGLGETERRORPROC error, const char *name)
const PFNGLGETERRORPROC error_fn
auto operator()(Args &&... args) const
Call the GL function with the appropriate parameters. Lookup the documentation for the GL function be...
const char * name
constexpr bool IsAvailable() const
PFNGLGETERRORPROC error_fn