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