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