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