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 namespace impeller {
18 
19 const char* GLErrorToString(GLenum value);
20 bool GLErrorIsFatal(GLenum value);
21 
23  const PFNGLGETERRORPROC error_fn;
24 
25  // TODO(matanlurey) Change to string_view.
26  // https://github.com/flutter/flutter/issues/135922
27  const char* name;
28 
29  AutoErrorCheck(PFNGLGETERRORPROC error, const char* name)
30  : error_fn(error), name(name) {}
31 
33  if (error_fn) {
34  auto error = error_fn();
35  if (error == GL_NO_ERROR) {
36  return;
37  }
38  if (GLErrorIsFatal(error)) {
39  FML_LOG(FATAL) << "Fatal GL Error " << GLErrorToString(error) << "("
40  << error << ")" << " encountered on call to " << name;
41  } else {
42  FML_LOG(ERROR) << "GL Error " << GLErrorToString(error) << "(" << error
43  << ")" << " encountered on call to " << name;
44  }
45  }
46  }
47 };
48 
49 template <class T>
50 struct GLProc {
51  using GLFunctionType = T;
52 
53  // TODO(matanlurey) Change to string_view.
54  // https://github.com/flutter/flutter/issues/135922
55 
56  //----------------------------------------------------------------------------
57  /// The name of the GL function.
58  ///
59  const char* name = nullptr;
60 
61  //----------------------------------------------------------------------------
62  /// The pointer to the GL function.
63  ///
64  GLFunctionType* function = nullptr;
65 
66  //----------------------------------------------------------------------------
67  /// An optional error function. If present, all calls will be followed by an
68  /// error check.
69  ///
70  PFNGLGETERRORPROC error_fn = nullptr;
71 
72  //----------------------------------------------------------------------------
73  /// @brief Call the GL function with the appropriate parameters. Lookup
74  /// the documentation for the GL function being called to
75  /// understand the arguments and return types. The arguments
76  /// types must match and will be type checked.
77  ///
78  template <class... Args>
79  auto operator()(Args&&... args) const {
80 #ifdef IMPELLER_DEBUG
82  // We check for the existence of extensions, and reset the function pointer
83  // but it's still called unconditionally below, and will segfault. This
84  // validation log will at least give us a hint as to what's going on.
85  FML_CHECK(IsAvailable()) << "GL function " << name << " is not available. "
86  << "This is likely due to a missing extension.";
87 #endif // IMPELLER_DEBUG
88  return function(std::forward<Args>(args)...);
89  }
90 
91  constexpr bool IsAvailable() const { return function != nullptr; }
92 
93  void Reset() {
94  function = nullptr;
95  error_fn = nullptr;
96  }
97 };
98 
99 #define FOR_EACH_IMPELLER_PROC(PROC) \
100  PROC(ActiveTexture); \
101  PROC(AttachShader); \
102  PROC(BindAttribLocation); \
103  PROC(BindBuffer); \
104  PROC(BindFramebuffer); \
105  PROC(BindRenderbuffer); \
106  PROC(BindTexture); \
107  PROC(BlendEquationSeparate); \
108  PROC(BlendFuncSeparate); \
109  PROC(BufferData); \
110  PROC(CheckFramebufferStatus); \
111  PROC(Clear); \
112  PROC(ClearColor); \
113  PROC(ClearStencil); \
114  PROC(ColorMask); \
115  PROC(CompileShader); \
116  PROC(CreateProgram); \
117  PROC(CreateShader); \
118  PROC(CullFace); \
119  PROC(DeleteBuffers); \
120  PROC(DeleteFramebuffers); \
121  PROC(DeleteProgram); \
122  PROC(DeleteRenderbuffers); \
123  PROC(DeleteShader); \
124  PROC(DeleteTextures); \
125  PROC(DepthFunc); \
126  PROC(DepthMask); \
127  PROC(DetachShader); \
128  PROC(Disable); \
129  PROC(DisableVertexAttribArray); \
130  PROC(DrawArrays); \
131  PROC(DrawElements); \
132  PROC(Enable); \
133  PROC(EnableVertexAttribArray); \
134  PROC(Flush); \
135  PROC(FramebufferRenderbuffer); \
136  PROC(FramebufferTexture2D); \
137  PROC(FrontFace); \
138  PROC(GenBuffers); \
139  PROC(GenerateMipmap); \
140  PROC(GenFramebuffers); \
141  PROC(GenRenderbuffers); \
142  PROC(GenTextures); \
143  PROC(GetActiveUniform); \
144  PROC(GetBooleanv); \
145  PROC(GetFloatv); \
146  PROC(GetFramebufferAttachmentParameteriv); \
147  PROC(GetIntegerv); \
148  PROC(GetProgramInfoLog); \
149  PROC(GetProgramiv); \
150  PROC(GetShaderInfoLog); \
151  PROC(GetShaderiv); \
152  PROC(GetString); \
153  PROC(GetStringi); \
154  PROC(GetUniformLocation); \
155  PROC(IsBuffer); \
156  PROC(IsFramebuffer); \
157  PROC(IsProgram); \
158  PROC(IsRenderbuffer); \
159  PROC(IsShader); \
160  PROC(IsTexture); \
161  PROC(LinkProgram); \
162  PROC(PixelStorei); \
163  PROC(RenderbufferStorage); \
164  PROC(Scissor); \
165  PROC(ShaderBinary); \
166  PROC(ShaderSource); \
167  PROC(StencilFuncSeparate); \
168  PROC(StencilMaskSeparate); \
169  PROC(StencilOpSeparate); \
170  PROC(TexImage2D); \
171  PROC(TexSubImage2D); \
172  PROC(TexParameteri); \
173  PROC(TexParameterfv); \
174  PROC(Uniform1fv); \
175  PROC(Uniform1i); \
176  PROC(Uniform2fv); \
177  PROC(Uniform3fv); \
178  PROC(Uniform4fv); \
179  PROC(UniformMatrix4fv); \
180  PROC(UseProgram); \
181  PROC(VertexAttribPointer); \
182  PROC(Viewport); \
183  PROC(GetShaderSource); \
184  PROC(ReadPixels);
185 
186 // Calls specific to OpenGLES.
187 void(glClearDepthf)(GLfloat depth);
188 void(glDepthRangef)(GLfloat n, GLfloat f);
189 
190 #define FOR_EACH_IMPELLER_ES_ONLY_PROC(PROC) \
191  PROC(ClearDepthf); \
192  PROC(DepthRangef);
193 
194 // Calls specific to desktop GL.
195 void(glClearDepth)(GLdouble depth);
196 void(glDepthRange)(GLdouble n, GLdouble f);
197 
198 #define FOR_EACH_IMPELLER_DESKTOP_ONLY_PROC(PROC) \
199  PROC(ClearDepth); \
200  PROC(DepthRange);
201 
202 #define FOR_EACH_IMPELLER_GLES3_PROC(PROC) PROC(BlitFramebuffer);
203 
204 #define FOR_EACH_IMPELLER_EXT_PROC(PROC) \
205  PROC(DebugMessageControlKHR); \
206  PROC(DiscardFramebufferEXT); \
207  PROC(FramebufferTexture2DMultisampleEXT); \
208  PROC(PushDebugGroupKHR); \
209  PROC(PopDebugGroupKHR); \
210  PROC(ObjectLabelKHR); \
211  PROC(RenderbufferStorageMultisampleEXT); \
212  PROC(GenQueriesEXT); \
213  PROC(DeleteQueriesEXT); \
214  PROC(GetQueryObjectui64vEXT); \
215  PROC(BeginQueryEXT); \
216  PROC(EndQueryEXT); \
217  PROC(GetQueryObjectuivEXT);
218 
219 enum class DebugResourceType {
220  kTexture,
221  kBuffer,
222  kProgram,
223  kShader,
225  kFrameBuffer,
226 };
227 
229  public:
230  using Resolver = std::function<void*(const char* function_name)>;
231  explicit ProcTableGLES(Resolver resolver);
232  ProcTableGLES(ProcTableGLES&& other) = default;
233 
234  ~ProcTableGLES();
235 
236 #define IMPELLER_PROC(name) \
237  GLProc<decltype(gl##name)> name = {"gl" #name, nullptr};
238 
244 
245 #undef IMPELLER_PROC
246 
247  bool IsValid() const;
248 
249  /// @brief Set the source for the attached [shader].
250  ///
251  /// Optionally, [defines] may contain a string value that will be
252  /// append to the shader source after the version marker. This can be used to
253  /// support static specialization. For example, setting "#define Foo 1".
254  void ShaderSourceMapping(GLuint shader,
255  const fml::Mapping& mapping,
256  const std::vector<Scalar>& defines = {}) const;
257 
258  const DescriptionGLES* GetDescription() const;
259 
260  const std::shared_ptr<const CapabilitiesGLES>& GetCapabilities() const;
261 
262  std::string DescribeCurrentFramebuffer() const;
263 
264  std::string GetProgramInfoLogString(GLuint program) const;
265 
266  bool IsCurrentFramebufferComplete() const;
267 
269  GLint name,
270  const std::string& label) const;
271 
272  void PushDebugGroup(const std::string& string) const;
273 
274  void PopDebugGroup() const;
275 
276  // Visible For testing.
277  std::optional<std::string> ComputeShaderWithDefines(
278  const fml::Mapping& mapping,
279  const std::vector<Scalar>& defines) const;
280 
281  private:
282  bool is_valid_ = false;
283  std::unique_ptr<DescriptionGLES> description_;
284  std::shared_ptr<const CapabilitiesGLES> capabilities_;
285  GLint debug_label_max_length_ = 0;
286 
287  ProcTableGLES(const ProcTableGLES&) = delete;
288 
289  ProcTableGLES& operator=(const ProcTableGLES&) = delete;
290 };
291 
292 } // namespace impeller
293 
294 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
impeller::ProcTableGLES::ShaderSourceMapping
void ShaderSourceMapping(GLuint shader, const fml::Mapping &mapping, const std::vector< Scalar > &defines={}) const
Set the source for the attached [shader].
Definition: proc_table_gles.cc:151
impeller::glDepthRangef
void() glDepthRangef(GLfloat n, GLfloat f)
impeller::glClearDepthf
void() glClearDepthf(GLfloat depth)
impeller::ProcTableGLES::FOR_EACH_IMPELLER_PROC
FOR_EACH_IMPELLER_PROC(IMPELLER_PROC)
impeller::ProcTableGLES::ProcTableGLES
ProcTableGLES(Resolver resolver)
Definition: proc_table_gles.cc:74
IMPELLER_PROC
#define IMPELLER_PROC(name)
Definition: proc_table_gles.h:236
impeller::ProcTableGLES::FOR_EACH_IMPELLER_GLES3_PROC
FOR_EACH_IMPELLER_GLES3_PROC(IMPELLER_PROC)
impeller::ProcTableGLES::PushDebugGroup
void PushDebugGroup(const std::string &string) const
Definition: proc_table_gles.cc:374
impeller::AutoErrorCheck::AutoErrorCheck
AutoErrorCheck(PFNGLGETERRORPROC error, const char *name)
Definition: proc_table_gles.h:29
impeller::glClearDepth
void() glClearDepth(GLdouble depth)
impeller::AutoErrorCheck::name
const char * name
Definition: proc_table_gles.h:27
impeller::GLProc::IsAvailable
constexpr bool IsAvailable() const
Definition: proc_table_gles.h:91
impeller::DebugResourceType::kBuffer
@ kBuffer
impeller::ProcTableGLES::SetDebugLabel
bool SetDebugLabel(DebugResourceType type, GLint name, const std::string &label) const
Definition: proc_table_gles.cc:348
impeller::ProcTableGLES::IsValid
bool IsValid() const
Definition: proc_table_gles.cc:147
impeller::DebugResourceType::kProgram
@ kProgram
impeller::ProcTableGLES::FOR_EACH_IMPELLER_EXT_PROC
FOR_EACH_IMPELLER_EXT_PROC(IMPELLER_PROC)
impeller::AutoErrorCheck
Definition: proc_table_gles.h:22
impeller::GLProc
Definition: proc_table_gles.h:50
impeller::ProcTableGLES::FOR_EACH_IMPELLER_ES_ONLY_PROC
FOR_EACH_IMPELLER_ES_ONLY_PROC(IMPELLER_PROC)
impeller::ProcTableGLES::Resolver
std::function< void *(const char *function_name)> Resolver
Definition: proc_table_gles.h:230
impeller::DebugResourceType::kTexture
@ kTexture
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::ProcTableGLES::GetCapabilities
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
Definition: proc_table_gles.cc:203
impeller::ProcTableGLES
Definition: proc_table_gles.h:228
impeller::ProcTableGLES::ComputeShaderWithDefines
std::optional< std::string > ComputeShaderWithDefines(const fml::Mapping &mapping, const std::vector< Scalar > &defines) const
Definition: proc_table_gles.cc:175
impeller::GLProc::error_fn
PFNGLGETERRORPROC error_fn
Definition: proc_table_gles.h:70
impeller::ProcTableGLES::GetProgramInfoLogString
std::string GetProgramInfoLogString(GLuint program) const
Definition: proc_table_gles.cc:401
impeller::DebugResourceType::kShader
@ kShader
impeller::DebugResourceType::kRenderBuffer
@ kRenderBuffer
gles.h
description_gles.h
impeller::ProcTableGLES::DescribeCurrentFramebuffer
std::string DescribeCurrentFramebuffer() const
Definition: proc_table_gles.cc:268
impeller::ProcTableGLES::FOR_EACH_IMPELLER_DESKTOP_ONLY_PROC
FOR_EACH_IMPELLER_DESKTOP_ONLY_PROC(IMPELLER_PROC)
impeller::GLProc::GLFunctionType
T GLFunctionType
Definition: proc_table_gles.h:51
impeller::DebugResourceType
DebugResourceType
Definition: proc_table_gles.h:219
impeller::GLProc::operator()
auto operator()(Args &&... args) const
Call the GL function with the appropriate parameters. Lookup the documentation for the GL function be...
Definition: proc_table_gles.h:79
impeller::glDepthRange
void() glDepthRange(GLdouble n, GLdouble f)
impeller::ProcTableGLES::IsCurrentFramebufferComplete
bool IsCurrentFramebufferComplete() const
Definition: proc_table_gles.cc:299
impeller::DebugResourceType::kFrameBuffer
@ kFrameBuffer
impeller::GLErrorToString
const char * GLErrorToString(GLenum value)
Definition: proc_table_gles.cc:18
impeller::AutoErrorCheck::~AutoErrorCheck
~AutoErrorCheck()
Definition: proc_table_gles.h:32
impeller::AutoErrorCheck::error_fn
const PFNGLGETERRORPROC error_fn
Definition: proc_table_gles.h:23
capabilities_gles.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::GLProc::name
const char * name
Definition: proc_table_gles.h:59
impeller::ProcTableGLES::PopDebugGroup
void PopDebugGroup() const
Definition: proc_table_gles.cc:391
impeller::GLErrorIsFatal
bool GLErrorIsFatal(GLenum value)
Definition: proc_table_gles.cc:38
impeller::GLProc::Reset
void Reset()
Definition: proc_table_gles.h:93
impeller::ProcTableGLES::GetDescription
const DescriptionGLES * GetDescription() const
Definition: proc_table_gles.cc:199
impeller::ProcTableGLES::~ProcTableGLES
~ProcTableGLES()