1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // frame_capture_utils.cpp:
7 // ANGLE Frame capture common classes.
8 //
9
10 #include "common/frame_capture_utils.h"
11
12 namespace angle
13 {
14 namespace
15 {
16 // Keep the simplest nullptr string for easy C parsing.
17 constexpr char kNullPointerString[] = "0";
18 } // anonymous namespace
19
ParamCapture()20 ParamCapture::ParamCapture() : type(ParamType::TGLenum), enumGroup(gl::GLESEnum::AllEnums) {}
21
ParamCapture(const char * nameIn,ParamType typeIn)22 ParamCapture::ParamCapture(const char *nameIn, ParamType typeIn)
23 : name(nameIn),
24 type(typeIn),
25 enumGroup(gl::GLESEnum::AllEnums),
26 bigGLEnum(gl::BigGLEnum::AllEnums)
27 {}
28
29 ParamCapture::~ParamCapture() = default;
30
ParamCapture(ParamCapture && other)31 ParamCapture::ParamCapture(ParamCapture &&other)
32 : type(ParamType::TGLenum),
33 enumGroup(gl::GLESEnum::AllEnums),
34 bigGLEnum(gl::BigGLEnum::AllEnums)
35 {
36 *this = std::move(other);
37 }
38
operator =(ParamCapture && other)39 ParamCapture &ParamCapture::operator=(ParamCapture &&other)
40 {
41 std::swap(name, other.name);
42 std::swap(type, other.type);
43 std::swap(value, other.value);
44 std::swap(enumGroup, other.enumGroup);
45 std::swap(bigGLEnum, other.bigGLEnum);
46 std::swap(data, other.data);
47 std::swap(arrayClientPointerIndex, other.arrayClientPointerIndex);
48 std::swap(readBufferSizeBytes, other.readBufferSizeBytes);
49 std::swap(dataNElements, other.dataNElements);
50 return *this;
51 }
52
ParamBuffer()53 ParamBuffer::ParamBuffer() {}
54
55 ParamBuffer::~ParamBuffer() = default;
56
ParamBuffer(ParamBuffer && other)57 ParamBuffer::ParamBuffer(ParamBuffer &&other)
58 {
59 *this = std::move(other);
60 }
61
operator =(ParamBuffer && other)62 ParamBuffer &ParamBuffer::operator=(ParamBuffer &&other)
63 {
64 std::swap(mParamCaptures, other.mParamCaptures);
65 std::swap(mClientArrayDataParam, other.mClientArrayDataParam);
66 std::swap(mReadBufferSize, other.mReadBufferSize);
67 std::swap(mReturnValueCapture, other.mReturnValueCapture);
68 return *this;
69 }
70
getParam(const char * paramName,ParamType paramType,int index)71 ParamCapture &ParamBuffer::getParam(const char *paramName, ParamType paramType, int index)
72 {
73 ParamCapture &capture = mParamCaptures[index];
74 ASSERT(capture.name == paramName);
75 ASSERT(capture.type == paramType);
76 return capture;
77 }
78
getParam(const char * paramName,ParamType paramType,int index) const79 const ParamCapture &ParamBuffer::getParam(const char *paramName,
80 ParamType paramType,
81 int index) const
82 {
83 return const_cast<ParamBuffer *>(this)->getParam(paramName, paramType, index);
84 }
85
getParamFlexName(const char * paramName1,const char * paramName2,ParamType paramType,int index)86 ParamCapture &ParamBuffer::getParamFlexName(const char *paramName1,
87 const char *paramName2,
88 ParamType paramType,
89 int index)
90 {
91 ParamCapture &capture = mParamCaptures[index];
92 ASSERT(capture.name == paramName1 || capture.name == paramName2);
93 ASSERT(capture.type == paramType);
94 return capture;
95 }
96
getParamFlexName(const char * paramName1,const char * paramName2,ParamType paramType,int index) const97 const ParamCapture &ParamBuffer::getParamFlexName(const char *paramName1,
98 const char *paramName2,
99 ParamType paramType,
100 int index) const
101 {
102 return const_cast<ParamBuffer *>(this)->getParamFlexName(paramName1, paramName2, paramType,
103 index);
104 }
105
addParam(ParamCapture && param)106 void ParamBuffer::addParam(ParamCapture &¶m)
107 {
108 if (param.arrayClientPointerIndex != -1)
109 {
110 ASSERT(mClientArrayDataParam == -1);
111 mClientArrayDataParam = static_cast<int>(mParamCaptures.size());
112 }
113
114 mReadBufferSize = std::max(param.readBufferSizeBytes, mReadBufferSize);
115 mParamCaptures.emplace_back(std::move(param));
116 }
117
addReturnValue(ParamCapture && returnValue)118 void ParamBuffer::addReturnValue(ParamCapture &&returnValue)
119 {
120 mReturnValueCapture = std::move(returnValue);
121 }
122
getNextParamName()123 const char *ParamBuffer::getNextParamName()
124 {
125 static const char *kParamNames[] = {"p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
126 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
127 "p16", "p17", "p18", "p19", "p20", "p21", "p22"};
128 ASSERT(mParamCaptures.size() < ArraySize(kParamNames));
129 return kParamNames[mParamCaptures.size()];
130 }
131
getClientArrayPointerParameter()132 ParamCapture &ParamBuffer::getClientArrayPointerParameter()
133 {
134 ASSERT(hasClientArrayData());
135 return mParamCaptures[mClientArrayDataParam];
136 }
137
CallCapture(EntryPoint entryPointIn,ParamBuffer && paramsIn)138 CallCapture::CallCapture(EntryPoint entryPointIn, ParamBuffer &¶msIn)
139 : entryPoint(entryPointIn), params(std::move(paramsIn))
140 {}
141
CallCapture(const std::string & customFunctionNameIn,ParamBuffer && paramsIn)142 CallCapture::CallCapture(const std::string &customFunctionNameIn, ParamBuffer &¶msIn)
143 : entryPoint(EntryPoint::Invalid),
144 customFunctionName(customFunctionNameIn),
145 params(std::move(paramsIn))
146 {}
147
148 CallCapture::~CallCapture() = default;
149
CallCapture(CallCapture && other)150 CallCapture::CallCapture(CallCapture &&other)
151 {
152 *this = std::move(other);
153 }
154
operator =(CallCapture && other)155 CallCapture &CallCapture::operator=(CallCapture &&other)
156 {
157 std::swap(entryPoint, other.entryPoint);
158 std::swap(customFunctionName, other.customFunctionName);
159 std::swap(params, other.params);
160 std::swap(isActive, other.isActive);
161 std::swap(contextID, other.contextID);
162 std::swap(isSyncPoint, other.isSyncPoint);
163 return *this;
164 }
165
name() const166 const char *CallCapture::name() const
167 {
168 if (customFunctionName.empty())
169 {
170 ASSERT(entryPoint != EntryPoint::Invalid);
171 return angle::GetEntryPointName(entryPoint);
172 }
173 else
174 {
175 return customFunctionName.c_str();
176 }
177 }
178
179 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLboolean value)180 void WriteParamValueReplay<ParamType::TGLboolean>(std::ostream &os,
181 const CallCapture &call,
182 GLboolean value)
183 {
184 switch (value)
185 {
186 case GL_TRUE:
187 os << "GL_TRUE";
188 break;
189 case GL_FALSE:
190 os << "GL_FALSE";
191 break;
192 default:
193 os << "0x" << std::hex << std::uppercase << GLint(value);
194 }
195 }
196
197 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLboolean * value)198 void WriteParamValueReplay<ParamType::TGLbooleanPointer>(std::ostream &os,
199 const CallCapture &call,
200 GLboolean *value)
201 {
202 if (value == 0)
203 {
204 os << kNullPointerString;
205 }
206 else
207 {
208 os << "(GLboolean *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
209 }
210 }
211
212 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const void * value)213 void WriteParamValueReplay<ParamType::TvoidConstPointer>(std::ostream &os,
214 const CallCapture &call,
215 const void *value)
216 {
217 if (value == 0)
218 {
219 os << kNullPointerString;
220 }
221 else
222 {
223 os << "(const void *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
224 }
225 }
226
227 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,void * value)228 void WriteParamValueReplay<ParamType::TvoidPointer>(std::ostream &os,
229 const CallCapture &call,
230 void *value)
231 {
232 if (value == 0)
233 {
234 os << kNullPointerString;
235 }
236 else
237 {
238 os << "(void *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
239 }
240 }
241
242 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const GLfloat * value)243 void WriteParamValueReplay<ParamType::TGLfloatConstPointer>(std::ostream &os,
244 const CallCapture &call,
245 const GLfloat *value)
246 {
247 if (value == 0)
248 {
249 os << kNullPointerString;
250 }
251 else
252 {
253 os << "(const GLfloat *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
254 }
255 }
256
257 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const GLint * value)258 void WriteParamValueReplay<ParamType::TGLintConstPointer>(std::ostream &os,
259 const CallCapture &call,
260 const GLint *value)
261 {
262 if (value == 0)
263 {
264 os << kNullPointerString;
265 }
266 else
267 {
268 os << "(const GLint *)" << static_cast<int>(reinterpret_cast<intptr_t>(value));
269 }
270 }
271
272 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLsizei * value)273 void WriteParamValueReplay<ParamType::TGLsizeiPointer>(std::ostream &os,
274 const CallCapture &call,
275 GLsizei *value)
276 {
277 if (value == 0)
278 {
279 os << kNullPointerString;
280 }
281 else
282 {
283 os << "(GLsizei *)" << static_cast<int>(reinterpret_cast<intptr_t>(value));
284 }
285 }
286
287 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const GLuint * value)288 void WriteParamValueReplay<ParamType::TGLuintConstPointer>(std::ostream &os,
289 const CallCapture &call,
290 const GLuint *value)
291 {
292 if (value == 0)
293 {
294 os << kNullPointerString;
295 }
296 else
297 {
298 os << "(const GLuint *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
299 }
300 }
301
302 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLDEBUGPROCKHR value)303 void WriteParamValueReplay<ParamType::TGLDEBUGPROCKHR>(std::ostream &os,
304 const CallCapture &call,
305 GLDEBUGPROCKHR value)
306 {}
307
308 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLDEBUGPROC value)309 void WriteParamValueReplay<ParamType::TGLDEBUGPROC>(std::ostream &os,
310 const CallCapture &call,
311 GLDEBUGPROC value)
312 {}
313
314 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::BufferID value)315 void WriteParamValueReplay<ParamType::TBufferID>(std::ostream &os,
316 const CallCapture &call,
317 gl::BufferID value)
318 {
319 os << "gBufferMap[" << value.value << "]";
320 }
321
322 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::FenceNVID value)323 void WriteParamValueReplay<ParamType::TFenceNVID>(std::ostream &os,
324 const CallCapture &call,
325 gl::FenceNVID value)
326 {
327 os << "gFenceNVMap[" << value.value << "]";
328 }
329
330 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::FramebufferID value)331 void WriteParamValueReplay<ParamType::TFramebufferID>(std::ostream &os,
332 const CallCapture &call,
333 gl::FramebufferID value)
334 {
335 os << "gFramebufferMap[" << value.value << "]";
336 }
337
338 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::MemoryObjectID value)339 void WriteParamValueReplay<ParamType::TMemoryObjectID>(std::ostream &os,
340 const CallCapture &call,
341 gl::MemoryObjectID value)
342 {
343 os << "gMemoryObjectMap[" << value.value << "]";
344 }
345
346 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::ProgramPipelineID value)347 void WriteParamValueReplay<ParamType::TProgramPipelineID>(std::ostream &os,
348 const CallCapture &call,
349 gl::ProgramPipelineID value)
350 {
351 os << "gProgramPipelineMap[" << value.value << "]";
352 }
353
354 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::QueryID value)355 void WriteParamValueReplay<ParamType::TQueryID>(std::ostream &os,
356 const CallCapture &call,
357 gl::QueryID value)
358 {
359 os << "gQueryMap[" << value.value << "]";
360 }
361
362 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::RenderbufferID value)363 void WriteParamValueReplay<ParamType::TRenderbufferID>(std::ostream &os,
364 const CallCapture &call,
365 gl::RenderbufferID value)
366 {
367 os << "gRenderbufferMap[" << value.value << "]";
368 }
369
370 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::SamplerID value)371 void WriteParamValueReplay<ParamType::TSamplerID>(std::ostream &os,
372 const CallCapture &call,
373 gl::SamplerID value)
374 {
375 os << "gSamplerMap[" << value.value << "]";
376 }
377
378 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::SemaphoreID value)379 void WriteParamValueReplay<ParamType::TSemaphoreID>(std::ostream &os,
380 const CallCapture &call,
381 gl::SemaphoreID value)
382 {
383 os << "gSemaphoreMap[" << value.value << "]";
384 }
385
386 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::ShaderProgramID value)387 void WriteParamValueReplay<ParamType::TShaderProgramID>(std::ostream &os,
388 const CallCapture &call,
389 gl::ShaderProgramID value)
390 {
391 os << "gShaderProgramMap[" << value.value << "]";
392 }
393
394 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::SyncID value)395 void WriteParamValueReplay<ParamType::TSyncID>(std::ostream &os,
396 const CallCapture &call,
397 gl::SyncID value)
398 {
399 os << "gSyncMap2[" << value.value << "]";
400 }
401
402 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::TextureID value)403 void WriteParamValueReplay<ParamType::TTextureID>(std::ostream &os,
404 const CallCapture &call,
405 gl::TextureID value)
406 {
407 os << "gTextureMap[" << value.value << "]";
408 }
409
410 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::TransformFeedbackID value)411 void WriteParamValueReplay<ParamType::TTransformFeedbackID>(std::ostream &os,
412 const CallCapture &call,
413 gl::TransformFeedbackID value)
414 {
415 os << "gTransformFeedbackMap[" << value.value << "]";
416 }
417
418 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::VertexArrayID value)419 void WriteParamValueReplay<ParamType::TVertexArrayID>(std::ostream &os,
420 const CallCapture &call,
421 gl::VertexArrayID value)
422 {
423 os << "gVertexArrayMap[" << value.value << "]";
424 }
425
426 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::UniformLocation value)427 void WriteParamValueReplay<ParamType::TUniformLocation>(std::ostream &os,
428 const CallCapture &call,
429 gl::UniformLocation value)
430 {
431 if (value.value == -1)
432 {
433 os << "-1";
434 return;
435 }
436
437 os << "gUniformLocations[";
438
439 // Find the program from the call parameters.
440 std::vector<gl::ShaderProgramID> shaderProgramIDs;
441 if (FindResourceIDsInCall<gl::ShaderProgramID>(call, shaderProgramIDs))
442 {
443 ASSERT(shaderProgramIDs.size() == 1);
444 os << shaderProgramIDs[0].value;
445 }
446 else
447 {
448 os << "gCurrentProgram";
449 }
450
451 os << "][" << value.value << "]";
452 }
453
454 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::UniformBlockIndex value)455 void WriteParamValueReplay<ParamType::TUniformBlockIndex>(std::ostream &os,
456 const CallCapture &call,
457 gl::UniformBlockIndex value)
458 {
459 // We do not support directly using uniform block indexes due to their multiple indirections.
460 // Use CaptureCustomUniformBlockBinding if you end up hitting this assertion.
461 UNREACHABLE();
462 }
463
464 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,GLubyte value)465 void WriteParamValueReplay<ParamType::TGLubyte>(std::ostream &os,
466 const CallCapture &call,
467 GLubyte value)
468 {
469 const int v = value;
470 os << v;
471 }
472
473 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLDEBUGPROCKHR value)474 void WriteParamValueReplay<ParamType::TEGLDEBUGPROCKHR>(std::ostream &os,
475 const CallCapture &call,
476 EGLDEBUGPROCKHR value)
477 {
478 // It's not necessary to implement correct capture for these types.
479 os << "0";
480 }
481
482 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLGetBlobFuncANDROID value)483 void WriteParamValueReplay<ParamType::TEGLGetBlobFuncANDROID>(std::ostream &os,
484 const CallCapture &call,
485 EGLGetBlobFuncANDROID value)
486 {
487 // It's not necessary to implement correct capture for these types.
488 os << "0";
489 }
490
491 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLSetBlobFuncANDROID value)492 void WriteParamValueReplay<ParamType::TEGLSetBlobFuncANDROID>(std::ostream &os,
493 const CallCapture &call,
494 EGLSetBlobFuncANDROID value)
495 {
496 // It's not necessary to implement correct capture for these types.
497 os << "0";
498 }
499
500 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,egl::Config * value)501 void WriteParamValueReplay<ParamType::Tegl_ConfigPointer>(std::ostream &os,
502 const CallCapture &call,
503 egl::Config *value)
504 {
505 os << "EGL_NO_CONFIG_KHR";
506 }
507
508 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,egl::SurfaceID value)509 void WriteParamValueReplay<ParamType::TSurfaceID>(std::ostream &os,
510 const CallCapture &call,
511 egl::SurfaceID value)
512 {
513 os << "gSurfaceMap2[" << value.value << "]";
514 }
515
516 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,gl::ContextID value)517 void WriteParamValueReplay<ParamType::TContextID>(std::ostream &os,
518 const CallCapture &call,
519 gl::ContextID value)
520 {
521 os << "gContextMap2[" << value.value << "]";
522 }
523
524 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,egl::Display * value)525 void WriteParamValueReplay<ParamType::Tegl_DisplayPointer>(std::ostream &os,
526 const CallCapture &call,
527 egl::Display *value)
528 {
529 os << "gEGLDisplay";
530 }
531
532 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,egl::ImageID value)533 void WriteParamValueReplay<ParamType::TImageID>(std::ostream &os,
534 const CallCapture &call,
535 egl::ImageID value)
536 {
537 os << "gEGLImageMap2[" << value.value << "]";
538 }
539
540 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLClientBuffer value)541 void WriteParamValueReplay<ParamType::TEGLClientBuffer>(std::ostream &os,
542 const CallCapture &call,
543 EGLClientBuffer value)
544 {
545 os << value;
546 }
547
548 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,egl::SyncID value)549 void WriteParamValueReplay<ParamType::Tegl_SyncID>(std::ostream &os,
550 const CallCapture &call,
551 egl::SyncID value)
552 {
553 os << "gEGLSyncMap[" << value.value << "]";
554 }
555
556 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const EGLAttrib * value)557 void WriteParamValueReplay<ParamType::TEGLAttribPointer>(std::ostream &os,
558 const CallCapture &call,
559 const EGLAttrib *value)
560 {
561 if (value == 0)
562 {
563 os << kNullPointerString;
564 }
565 else
566 {
567 os << "(const EGLAttrib *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
568 }
569 }
570
571 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,const EGLint * value)572 void WriteParamValueReplay<ParamType::TEGLintConstPointer>(std::ostream &os,
573 const CallCapture &call,
574 const EGLint *value)
575 {
576 if (value == 0)
577 {
578 os << kNullPointerString;
579 }
580 else
581 {
582 os << "(const EGLint *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
583 }
584 }
585
586 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLint * value)587 void WriteParamValueReplay<ParamType::TEGLintPointer>(std::ostream &os,
588 const CallCapture &call,
589 EGLint *value)
590 {
591 if (value == 0)
592 {
593 os << kNullPointerString;
594 }
595 else
596 {
597 os << "(const EGLint *)" << static_cast<int>(reinterpret_cast<uintptr_t>(value));
598 }
599 }
600
601 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLTime value)602 void WriteParamValueReplay<ParamType::TEGLTime>(std::ostream &os,
603 const CallCapture &call,
604 EGLTime value)
605 {
606 os << value << "ul";
607 }
608
609 template <>
WriteParamValueReplay(std::ostream & os,const CallCapture & call,EGLTimeKHR value)610 void WriteParamValueReplay<ParamType::TEGLTimeKHR>(std::ostream &os,
611 const CallCapture &call,
612 EGLTimeKHR value)
613 {
614 os << value << "ul";
615 }
616
617 template <typename ParamValueType>
FindResourceIDsInCall(const CallCapture & call,std::vector<ParamValueType> & idsOut)618 bool FindResourceIDsInCall(const CallCapture &call, std::vector<ParamValueType> &idsOut)
619 {
620 const ParamType paramType = ParamValueTrait<ParamValueType>::typeID;
621 for (const ParamCapture ¶m : call.params.getParamCaptures())
622 {
623 if (param.type == paramType)
624 {
625 const ParamValueType id = AccessParamValue<ParamValueType>(paramType, param.value);
626 idsOut.push_back(id);
627 }
628 }
629
630 return !idsOut.empty();
631 }
632
633 // Explicit instantiation
634 template bool FindResourceIDsInCall<gl::TextureID>(const CallCapture &call,
635 std::vector<gl::TextureID> &idsOut);
636 template bool FindResourceIDsInCall<gl::ShaderProgramID>(const CallCapture &call,
637 std::vector<gl::ShaderProgramID> &idsOut);
638 } // namespace angle
639