Hash :
3bd10b19
Author :
Date :
2019-07-08T13:23:37
Capture/Replay: Return CallCapture from capture funcs. This moves more shared code into a simple templated helper function. It will also allow us to call the parameter capture methods more easily for mid-execution capture. Refactoring change only. Bug: angleproject:3611 Change-Id: I8d95a6230922dfa0403ba5c328df78735c765519 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1688508 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// FrameCapture.cpp:
// ANGLE Frame capture implementation.
//
#include "libANGLE/FrameCapture.h"
#include <string>
#include "libANGLE/Context.h"
#include "libANGLE/VertexArray.h"
namespace angle
{
namespace
{
std::string GetCaptureFileName(size_t frameIndex, const char *suffix)
{
std::stringstream fnameStream;
fnameStream << "angle_capture_frame" << std::setfill('0') << std::setw(3) << frameIndex
<< suffix;
return fnameStream.str();
}
void WriteParamStaticVarName(const CallCapture &call,
const ParamCapture ¶m,
int counter,
std::ostream &out)
{
out << call.name << "_" << param.name << "_" << counter;
}
template <typename T, typename CastT = T>
void WriteInlineData(const std::vector<uint8_t> &vec, std::ostream &out)
{
const T *data = reinterpret_cast<const T *>(vec.data());
size_t count = vec.size() / sizeof(T);
out << static_cast<CastT>(data[0]);
for (size_t dataIndex = 1; dataIndex < count; ++dataIndex)
{
out << ", " << static_cast<CastT>(data[dataIndex]);
}
}
constexpr size_t kInlineDataThreshold = 128;
} // anonymous namespace
ParamCapture::ParamCapture() : type(ParamType::TGLenum) {}
ParamCapture::ParamCapture(const char *nameIn, ParamType typeIn) : name(nameIn), type(typeIn) {}
ParamCapture::~ParamCapture() = default;
ParamCapture::ParamCapture(ParamCapture &&other) : type(ParamType::TGLenum)
{
*this = std::move(other);
}
ParamCapture &ParamCapture::operator=(ParamCapture &&other)
{
std::swap(name, other.name);
std::swap(type, other.type);
std::swap(value, other.value);
std::swap(data, other.data);
std::swap(arrayClientPointerIndex, other.arrayClientPointerIndex);
std::swap(readBufferSize, other.readBufferSize);
return *this;
}
ParamBuffer::ParamBuffer() {}
ParamBuffer::~ParamBuffer() = default;
ParamBuffer::ParamBuffer(ParamBuffer &&other)
{
*this = std::move(other);
}
ParamBuffer &ParamBuffer::operator=(ParamBuffer &&other)
{
std::swap(mParamCaptures, other.mParamCaptures);
std::swap(mClientArrayDataParam, other.mClientArrayDataParam);
std::swap(mReadBufferSize, other.mReadBufferSize);
return *this;
}
ParamCapture &ParamBuffer::getParam(const char *paramName, ParamType paramType, int index)
{
ParamCapture &capture = mParamCaptures[index];
ASSERT(capture.name == paramName);
ASSERT(capture.type == paramType);
return capture;
}
void ParamBuffer::addParam(ParamCapture &¶m)
{
if (param.arrayClientPointerIndex != -1)
{
ASSERT(mClientArrayDataParam == -1);
mClientArrayDataParam = static_cast<int>(mParamCaptures.size());
}
mReadBufferSize = std::max(param.readBufferSize, mReadBufferSize);
mParamCaptures.emplace_back(std::move(param));
}
ParamCapture &ParamBuffer::getClientArrayPointerParameter()
{
ASSERT(hasClientArrayData());
return mParamCaptures[mClientArrayDataParam];
}
CallCapture::CallCapture(const char *nameIn, ParamBuffer &¶msIn)
: name(nameIn), params(std::move(paramsIn))
{}
CallCapture::~CallCapture() = default;
CallCapture::CallCapture(CallCapture &&other)
{
*this = std::move(other);
}
CallCapture &CallCapture::operator=(CallCapture &&other)
{
std::swap(name, other.name);
std::swap(params, other.params);
return *this;
}
FrameCapture::FrameCapture() : mFrameIndex(0), mReadBufferSize(0)
{
reset();
}
FrameCapture::~FrameCapture() = default;
void FrameCapture::captureCall(const gl::Context *context, CallCapture &&call)
{
if (call.name == "glVertexAttribPointer")
{
// Get array location
GLuint index = call.params.getParam("index", ParamType::TGLuint, 0).value.GLuintVal;
if (call.params.hasClientArrayData())
{
mClientVertexArrayMap[index] = mCalls.size();
}
else
{
mClientVertexArrayMap[index] = -1;
}
}
else if (call.name == "glDrawArrays")
{
if (context->getStateCache().hasAnyActiveClientAttrib())
{
// Get counts from paramBuffer.
GLint startVertex = call.params.getParam("start", ParamType::TGLint, 1).value.GLintVal;
GLsizei drawCount =
call.params.getParam("count", ParamType::TGLsizei, 2).value.GLsizeiVal;
captureClientArraySnapshot(context, startVertex + drawCount, 1);
}
}
else if (call.name == "glDrawElements")
{
if (context->getStateCache().hasAnyActiveClientAttrib())
{
GLsizei count = call.params.getParam("count", ParamType::TGLsizei, 1).value.GLsizeiVal;
gl::DrawElementsType drawElementsType =
call.params.getParam("typePacked", ParamType::TDrawElementsType, 2)
.value.DrawElementsTypeVal;
const void *indices = call.params.getParam("indices", ParamType::TvoidConstPointer, 3)
.value.voidConstPointerVal;
gl::IndexRange indexRange;
bool restart = context->getState().isPrimitiveRestartEnabled();
gl::Buffer *elementArrayBuffer =
context->getState().getVertexArray()->getElementArrayBuffer();
if (elementArrayBuffer)
{
size_t offset = reinterpret_cast<size_t>(indices);
(void)elementArrayBuffer->getIndexRange(context, drawElementsType, offset, count,
restart, &indexRange);
}
else
{
indexRange = gl::ComputeIndexRange(drawElementsType, indices, count, restart);
}
captureClientArraySnapshot(context, indexRange.end, 1);
}
}
mReadBufferSize = std::max(mReadBufferSize, call.params.getReadBufferSize());
mCalls.emplace_back(std::move(call));
}
void FrameCapture::captureClientArraySnapshot(const gl::Context *context,
size_t vertexCount,
size_t instanceCount)
{
const gl::VertexArray *vao = context->getState().getVertexArray();
// Capture client array data.
for (size_t attribIndex : context->getStateCache().getActiveClientAttribsMask())
{
const gl::VertexAttribute &attrib = vao->getVertexAttribute(attribIndex);
const gl::VertexBinding &binding = vao->getVertexBinding(attrib.bindingIndex);
int callIndex = mClientVertexArrayMap[attribIndex];
if (callIndex != -1)
{
size_t count = vertexCount;
if (binding.getDivisor() > 0)
{
count = rx::UnsignedCeilDivide(instanceCount, binding.getDivisor());
}
// The last capture element doesn't take up the full stride.
size_t bytesToCapture = (count - 1) * binding.getStride() + attrib.format->pixelBytes;
CallCapture &call = mCalls[callIndex];
ParamCapture ¶m = call.params.getClientArrayPointerParameter();
ASSERT(param.type == ParamType::TvoidConstPointer);
ParamBuffer updateParamBuffer;
updateParamBuffer.addValueParam<GLint>("arrayIndex", ParamType::TGLint, attribIndex);
ParamCapture updateMemory("pointer", ParamType::TvoidConstPointer);
CaptureMemory(param.value.voidConstPointerVal, bytesToCapture, &updateMemory);
updateParamBuffer.addParam(std::move(updateMemory));
mCalls.emplace_back("UpdateClientArrayPointer", std::move(updateParamBuffer));
mClientArraySizes[attribIndex] =
std::max(mClientArraySizes[attribIndex], bytesToCapture);
}
}
}
bool FrameCapture::anyClientArray() const
{
for (size_t size : mClientArraySizes)
{
if (size > 0)
return true;
}
return false;
}
void FrameCapture::onEndFrame()
{
if (!mCalls.empty())
{
saveCapturedFrameAsCpp();
}
reset();
mFrameIndex++;
}
void FrameCapture::saveCapturedFrameAsCpp()
{
bool useClientArrays = anyClientArray();
std::stringstream out;
std::stringstream header;
std::vector<uint8_t> binaryData;
header << "#include \"util/gles_loader_autogen.h\"\n";
header << "\n";
header << "#include <cstdio>\n";
header << "#include <vector>\n";
header << "\n";
header << "namespace\n";
header << "{\n";
if (mReadBufferSize > 0)
{
header << "std::vector<uint8_t> gReadBuffer;\n";
}
if (useClientArrays)
{
header << "std::vector<uint8_t> gClientArrays[" << gl::MAX_VERTEX_ATTRIBS << "];\n";
header << "template <size_t N>\n";
header << "void UpdateClientArrayPointer(int arrayIndex, const uint8_t (&data)[N])\n";
header << "{\n";
header << " memcpy(gClientArrays[arrayIndex].data(), data, N);\n";
header << "}\n";
}
out << "void ReplayFrame" << mFrameIndex << "()\n";
out << "{\n";
out << " LoadBinaryData();\n";
for (size_t arrayIndex = 0; arrayIndex < mClientArraySizes.size(); ++arrayIndex)
{
if (mClientArraySizes[arrayIndex] > 0)
{
out << " gClientArrays[" << arrayIndex << "].resize("
<< mClientArraySizes[arrayIndex] << ");\n";
}
}
if (mReadBufferSize > 0)
{
out << " gReadBuffer.resize(" << mReadBufferSize << ");\n";
}
for (const CallCapture &call : mCalls)
{
out << " ";
writeCallReplay(call, out, header, &binaryData);
out << ";\n";
}
if (!binaryData.empty())
{
std::string fname = GetCaptureFileName(mFrameIndex, ".angledata");
FILE *fp = fopen(fname.c_str(), "wb");
fwrite(binaryData.data(), 1, binaryData.size(), fp);
fclose(fp);
header << "std::vector<uint8_t> gBinaryData;\n";
header << "void LoadBinaryData()\n";
header << "{\n";
header << " gBinaryData.resize(" << static_cast<int>(binaryData.size()) << ");\n";
header << " FILE *fp = fopen(\"" << fname << "\", \"rb\");\n";
header << " fread(gBinaryData.data(), 1, " << static_cast<int>(binaryData.size())
<< ", fp);\n";
header << " fclose(fp);\n";
header << "}\n";
}
else
{
header << "// No binary data.\n";
header << "void LoadBinaryData() {}\n";
}
out << "}\n";
header << "} // anonymous namespace\n";
std::string outString = out.str();
std::string headerString = header.str();
std::string fname = GetCaptureFileName(mFrameIndex, ".cpp");
FILE *fp = fopen(fname.c_str(), "w");
fprintf(fp, "%s\n\n%s", headerString.c_str(), outString.c_str());
fclose(fp);
printf("Saved '%s'.\n", fname.c_str());
}
int FrameCapture::getAndIncrementCounter(const std::string &callName, const std::string ¶mName)
{
auto counterKey = std::tie(callName, paramName);
return mDataCounters[counterKey]++;
}
void FrameCapture::writeCallReplay(const CallCapture &call,
std::ostream &out,
std::ostream &header,
std::vector<uint8_t> *binaryData)
{
out << call.name << "(";
bool first = true;
for (const ParamCapture ¶m : call.params.getParamCaptures())
{
if (!first)
{
out << ", ";
}
if (param.arrayClientPointerIndex != -1)
{
out << "gClientArrays[" << param.arrayClientPointerIndex << "].data()";
}
else if (param.readBufferSize > 0)
{
out << "reinterpret_cast<" << ParamTypeToString(param.type) << ">(gReadBuffer.data())";
}
else if (param.data.empty())
{
out << param;
}
else
{
if (param.type == ParamType::TGLcharConstPointer)
{
const std::vector<uint8_t> &data = param.data[0];
std::string str(data.begin(), data.end());
out << "\"" << str << "\"";
}
else if (param.type == ParamType::TGLcharConstPointerPointer)
{
int counter = getAndIncrementCounter(call.name, param.name);
header << "const char *";
WriteParamStaticVarName(call, param, counter, header);
header << "[] = { \n";
for (const std::vector<uint8_t> &data : param.data)
{
std::string str(data.begin(), data.end());
header << " R\"(" << str << ")\",\n";
}
header << " };\n";
WriteParamStaticVarName(call, param, counter, out);
}
else
{
int counter = getAndIncrementCounter(call.name, param.name);
ASSERT(param.data.size() == 1);
const std::vector<uint8_t> &data = param.data[0];
if (data.size() > kInlineDataThreshold)
{
size_t offset = binaryData->size();
binaryData->resize(offset + data.size());
memcpy(binaryData->data() + offset, data.data(), data.size());
out << "reinterpret_cast<" << ParamTypeToString(param.type) << ">(&gBinaryData["
<< offset << "])";
}
else
{
ParamType overrideType = param.type;
if (param.type == ParamType::TGLvoidConstPointer ||
param.type == ParamType::TvoidConstPointer)
{
overrideType = ParamType::TGLubyteConstPointer;
}
std::string paramTypeString = ParamTypeToString(overrideType);
header << paramTypeString.substr(0, paramTypeString.length() - 1);
WriteParamStaticVarName(call, param, counter, header);
header << "[] = { ";
switch (overrideType)
{
case ParamType::TGLintConstPointer:
WriteInlineData<GLint>(data, header);
break;
case ParamType::TGLshortConstPointer:
WriteInlineData<GLshort>(data, header);
break;
case ParamType::TGLfloatConstPointer:
WriteInlineData<GLfloat>(data, header);
break;
case ParamType::TGLubyteConstPointer:
WriteInlineData<GLubyte, int>(data, header);
break;
case ParamType::TGLuintConstPointer:
case ParamType::TGLenumConstPointer:
WriteInlineData<GLuint>(data, header);
break;
default:
UNIMPLEMENTED();
break;
}
header << " };\n";
WriteParamStaticVarName(call, param, counter, out);
}
}
}
first = false;
}
out << ")";
}
bool FrameCapture::enabled() const
{
return mFrameIndex < 100;
}
void FrameCapture::reset()
{
mCalls.clear();
mClientVertexArrayMap.fill(-1);
mClientArraySizes.fill(0);
mDataCounters.clear();
mReadBufferSize = 0;
}
std::ostream &operator<<(std::ostream &os, const ParamCapture &capture)
{
WriteParamTypeToStream(os, capture.type, capture.value);
return os;
}
void CaptureMemory(const void *source, size_t size, ParamCapture *paramCapture)
{
std::vector<uint8_t> data(size);
memcpy(data.data(), source, size);
paramCapture->data.emplace_back(std::move(data));
}
void CaptureString(const GLchar *str, ParamCapture *paramCapture)
{
CaptureMemory(str, strlen(str), paramCapture);
}
template <>
void WriteParamValueToStream<ParamType::TGLboolean>(std::ostream &os, GLboolean value)
{
switch (value)
{
case GL_TRUE:
os << "GL_TRUE";
break;
case GL_FALSE:
os << "GL_FALSE";
break;
default:
os << "GL_INVALID_ENUM";
}
}
template <>
void WriteParamValueToStream<ParamType::TvoidConstPointer>(std::ostream &os, const void *value)
{
if (value == 0)
{
os << "nullptr";
}
else
{
os << "reinterpret_cast<const void *>("
<< static_cast<int>(reinterpret_cast<uintptr_t>(value)) << ")";
}
}
template <>
void WriteParamValueToStream<ParamType::TGLDEBUGPROCKHR>(std::ostream &os, GLDEBUGPROCKHR value)
{}
} // namespace angle