[Chore] (MG_Backend, MG_Backend/DirectGLES, MG_Backend/DirectVulkan, MG_Backend/MGPipe, MG_Backend/Record, MG_Benchmark/Buffer, MG_Benchmark/Container, MG_Benchmark/Driver, MG_Benchmark/Program, MG_Benchmark/ShaderCache, MG_Benchmark/Transpile, MG_Gbm, MG_Impl, MG_Impl/CGLImpl, MG_Impl/DyldInterpose, MG_Impl/EGLImpl, MG_Impl/GLImpl, MG_Impl/GLXImpl, MG_Impl/NSOpenGLImpl, MG_Impl/Pipe, MG_Impl/WGLImpl, MG_IntegrationTest, MG_IntegrationTest/Harness, MG_IntegrationTest/Scenarios, MG_Pipe, MG_Remote, MG_Remote/Client, MG_Remote/Protocol, MG_Remote/Server, MG_Remote/Transport, MG_Remote/Wire, MG_State/EGLState, MG_State/GLState, MG_Test, MG_Test/Backend, MG_Test/BackendLoader, MG_Test/Buffer, MG_Test/EGLState, MG_Test/Framebuffer, MG_Test/Gbm, MG_Test/Pipe, MG_Test/Pipeline, MG_Test/Program, MG_Test/Query, MG_Test/SelfTest, MG_Test/ShaderTranspiler, MG_Test/State, MG_Test/Swapchain, MG_Test/Texture, MG_Test/Util, MG_Test/VertexArray, MG_Test/Wire, MG_Util/Async, MG_Util/BackendLoaders, MG_Util/Converters, MG_Util/Damage, MG_Util/Debug, MG_Util/Math, MG_Util/Metrics, MG_Util/SelfTest, MG_Util/ShaderTranspiler, MG_Util/Texture, MG_Util/X11, MobileGL, scripts/data, tools/cts, tools/device_bench, tools/spikes, tools/trace_replay, tools/wgl-smoke): clang-format every C and C++ source

- Layout only: token streams match up to split literals, continuations and using order
- The size totals fall, so the structure baseline is lowered
This commit is contained in:
2026-10-16 06:30:38 -04:00
parent 76b10cc35b
commit f4994ee36e
747 changed files with 35842 additions and 36261 deletions
File diff suppressed because it is too large Load Diff
+5 -5
View File
@@ -2,10 +2,10 @@
namespace mobilegl_trace_dump {
// Installs the opt-in framebuffer-attachment and named-2D-texture dump hooks. The respective
// environments are MOBILEGL_TRACE_DUMP_FBO_ATTACHMENTS and MOBILEGL_TRACE_DUMP_TEXTURE_2D.
// Safe and cheap to call on every makeCurrent: the environment is consulted once and the hook is
// installed at most once.
void InstallIfRequested();
// Installs the opt-in framebuffer-attachment and named-2D-texture dump hooks. The respective
// environments are MOBILEGL_TRACE_DUMP_FBO_ATTACHMENTS and MOBILEGL_TRACE_DUMP_TEXTURE_2D.
// Safe and cheap to call on every makeCurrent: the environment is consulted once and the hook is
// installed at most once.
void InstallIfRequested();
} // namespace mobilegl_trace_dump
+99 -99
View File
@@ -16,136 +16,136 @@
#include <cstring>
#include <dlfcn.h>
void *_libGlHandle = nullptr;
void* _libGlHandle = nullptr;
namespace {
void *LookupSymbol(const char *procName);
void* LookupSymbol(const char* procName);
void *GetMobileGlHandle() {
if (_libGlHandle != nullptr) {
void* GetMobileGlHandle() {
if (_libGlHandle != nullptr) {
return _libGlHandle;
}
const char* library = std::getenv("MOBILEGL_TRACE_LIBRARY");
if (library != nullptr && library[0] != '\0') {
_libGlHandle = dlopen(library, RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD);
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen(library, RTLD_NOW | RTLD_GLOBAL);
}
}
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen("libMobileGL.so", RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD);
}
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen("libMobileGL.so", RTLD_NOW | RTLD_GLOBAL);
}
return _libGlHandle;
}
const char *library = std::getenv("MOBILEGL_TRACE_LIBRARY");
if (library != nullptr && library[0] != '\0') {
_libGlHandle = dlopen(library, RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD);
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen(library, RTLD_NOW | RTLD_GLOBAL);
using GlReadBuffer = void (*)(GLenum mode);
using GlReadPixels = void (*)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
void* pixels);
using GlGetIntegerv = void (*)(GLenum pname, GLint* data);
using GlGetError = GLenum (*)();
GlReadBuffer gRealGlReadBuffer = nullptr;
GlReadPixels gRealGlReadPixels = nullptr;
GlGetIntegerv gRealGlGetIntegerv = nullptr;
GlGetError gRealGlGetError = nullptr;
int gSuppressInvalidEnumCount = 0;
void MobileGLTraceReadBuffer(GLenum mode) {
if (mode == GL_BACK) {
++gSuppressInvalidEnumCount;
return;
}
}
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen("libMobileGL.so", RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD);
}
if (_libGlHandle == nullptr) {
_libGlHandle = dlopen("libMobileGL.so", RTLD_NOW | RTLD_GLOBAL);
}
return _libGlHandle;
}
using GlReadBuffer = void (*)(GLenum mode);
using GlReadPixels = void (*)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
void *pixels);
using GlGetIntegerv = void (*)(GLenum pname, GLint *data);
using GlGetError = GLenum (*)();
GlReadBuffer gRealGlReadBuffer = nullptr;
GlReadPixels gRealGlReadPixels = nullptr;
GlGetIntegerv gRealGlGetIntegerv = nullptr;
GlGetError gRealGlGetError = nullptr;
int gSuppressInvalidEnumCount = 0;
void MobileGLTraceReadBuffer(GLenum mode) {
if (mode == GL_BACK) {
++gSuppressInvalidEnumCount;
return;
}
if (gRealGlReadBuffer == nullptr) {
gRealGlReadBuffer = reinterpret_cast<GlReadBuffer>(LookupSymbol("glReadBuffer"));
}
if (gRealGlReadBuffer != nullptr) {
gRealGlReadBuffer(mode);
}
}
void MobileGLTraceReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
void *pixels) {
if (gRealGlReadPixels == nullptr) {
gRealGlReadPixels = reinterpret_cast<GlReadPixels>(LookupSymbol("glReadPixels"));
}
if (gRealGlReadPixels != nullptr) {
gRealGlReadPixels(x, y, width, height, format, type, pixels);
}
}
void MobileGLTraceGetIntegerv(GLenum pname, GLint *data) {
if (pname == GL_READ_BUFFER) {
if (data != nullptr) {
*data = GL_BACK;
if (gRealGlReadBuffer == nullptr) {
gRealGlReadBuffer = reinterpret_cast<GlReadBuffer>(LookupSymbol("glReadBuffer"));
}
++gSuppressInvalidEnumCount;
return;
}
if (gRealGlGetIntegerv == nullptr) {
gRealGlGetIntegerv = reinterpret_cast<GlGetIntegerv>(LookupSymbol("glGetIntegerv"));
}
if (gRealGlGetIntegerv != nullptr) {
gRealGlGetIntegerv(pname, data);
}
}
GLenum MobileGLTraceGetError() {
if (gRealGlGetError == nullptr) {
gRealGlGetError = reinterpret_cast<GlGetError>(LookupSymbol("glGetError"));
}
if (gRealGlGetError == nullptr) {
return GL_NO_ERROR;
}
GLenum error = gRealGlGetError();
if (error == GL_INVALID_ENUM && gSuppressInvalidEnumCount > 0) {
--gSuppressInvalidEnumCount;
return GL_NO_ERROR;
}
return error;
}
void *LookupSymbol(const char *procName) {
void *mobileGl = GetMobileGlHandle();
if (mobileGl != nullptr) {
void *proc = dlsym(mobileGl, procName);
if (proc != nullptr) {
return proc;
if (gRealGlReadBuffer != nullptr) {
gRealGlReadBuffer(mode);
}
}
return dlsym(RTLD_DEFAULT, procName);
}
void MobileGLTraceReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
void* pixels) {
if (gRealGlReadPixels == nullptr) {
gRealGlReadPixels = reinterpret_cast<GlReadPixels>(LookupSymbol("glReadPixels"));
}
if (gRealGlReadPixels != nullptr) {
gRealGlReadPixels(x, y, width, height, format, type, pixels);
}
}
void MobileGLTraceGetIntegerv(GLenum pname, GLint* data) {
if (pname == GL_READ_BUFFER) {
if (data != nullptr) {
*data = GL_BACK;
}
++gSuppressInvalidEnumCount;
return;
}
if (gRealGlGetIntegerv == nullptr) {
gRealGlGetIntegerv = reinterpret_cast<GlGetIntegerv>(LookupSymbol("glGetIntegerv"));
}
if (gRealGlGetIntegerv != nullptr) {
gRealGlGetIntegerv(pname, data);
}
}
GLenum MobileGLTraceGetError() {
if (gRealGlGetError == nullptr) {
gRealGlGetError = reinterpret_cast<GlGetError>(LookupSymbol("glGetError"));
}
if (gRealGlGetError == nullptr) {
return GL_NO_ERROR;
}
GLenum error = gRealGlGetError();
if (error == GL_INVALID_ENUM && gSuppressInvalidEnumCount > 0) {
--gSuppressInvalidEnumCount;
return GL_NO_ERROR;
}
return error;
}
void* LookupSymbol(const char* procName) {
void* mobileGl = GetMobileGlHandle();
if (mobileGl != nullptr) {
void* proc = dlsym(mobileGl, procName);
if (proc != nullptr) {
return proc;
}
}
return dlsym(RTLD_DEFAULT, procName);
}
} // namespace
void *_getPublicProcAddress(const char *procName) {
void* _getPublicProcAddress(const char* procName) {
if (strcmp(procName, "glReadPixels") == 0) {
return reinterpret_cast<void *>(&MobileGLTraceReadPixels);
return reinterpret_cast<void*>(&MobileGLTraceReadPixels);
}
if (strcmp(procName, "glReadBuffer") == 0) {
return reinterpret_cast<void *>(&MobileGLTraceReadBuffer);
return reinterpret_cast<void*>(&MobileGLTraceReadBuffer);
}
if (strcmp(procName, "glGetIntegerv") == 0) {
return reinterpret_cast<void *>(&MobileGLTraceGetIntegerv);
return reinterpret_cast<void*>(&MobileGLTraceGetIntegerv);
}
if (strcmp(procName, "glGetError") == 0) {
return reinterpret_cast<void *>(&MobileGLTraceGetError);
return reinterpret_cast<void*>(&MobileGLTraceGetError);
}
return LookupSymbol(procName);
}
void *_getPrivateProcAddress(const char *procName) {
void *proc = LookupSymbol(procName);
void* _getPrivateProcAddress(const char* procName) {
void* proc = LookupSymbol(procName);
if (proc != nullptr) {
return proc;
}
if (_eglGetProcAddress != nullptr) {
return reinterpret_cast<void *>(_eglGetProcAddress(procName));
return reinterpret_cast<void*>(_eglGetProcAddress(procName));
}
return nullptr;
File diff suppressed because it is too large Load Diff
@@ -46,383 +46,401 @@
namespace {
constexpr unsigned kUniformBuffer = 0x8A11;
constexpr unsigned kShaderStorageBuffer = 0x90D2;
constexpr unsigned kDynamicStorageBit = 0x0100;
constexpr unsigned kRelocBufferName = 1000000;
constexpr unsigned kUniformBuffer = 0x8A11;
constexpr unsigned kShaderStorageBuffer = 0x90D2;
constexpr unsigned kDynamicStorageBit = 0x0100;
constexpr unsigned kRelocBufferName = 1000000;
[[noreturn]] void Fail(const trace::Call *call, const std::string &what) {
std::fprintf(stderr, "error: call %u %s: %s\n", call ? call->no : 0u, call ? call->name() : "-", what.c_str());
std::exit(1);
}
bool Is(const trace::Call *call, const char *name) { return std::strcmp(call->name(), name) == 0; }
bool StartsWith(const char *s, const char *prefix) { return std::strncmp(s, prefix, std::strlen(prefix)) == 0; }
bool ReadsIndexedBuffers(const trace::Call *call) {
const char *n = call->name();
return StartsWith(n, "glDraw") || StartsWith(n, "glMultiDraw") || StartsWith(n, "glDispatchCompute");
}
bool IsRangeTarget(unsigned target) { return target == kUniformBuffer || target == kShaderStorageBuffer; }
struct MapRegion {
unsigned buffer;
uint64_t offset;
uint64_t length;
uint64_t ptr;
};
struct Reloc {
unsigned src = 0;
uint64_t offset = 0;
uint64_t size = 0;
bool dirty = true;
std::vector<char> last;
const trace::EnumSig *targetSig = nullptr;
};
// Signatures for the calls this tool inserts; ids are allocated past the trace's own.
struct Sigs {
const char *createNames[2] = {"n", "buffers"};
const char *storageNames[4] = {"buffer", "size", "data", "flags"};
const char *subDataNames[4] = {"buffer", "offset", "size", "data"};
const char *bindRangeNames[5] = {"target", "index", "buffer", "offset", "size"};
const char *bindNames[2] = {"target", "buffer"};
trace::FunctionSig create{}, storage{}, subData{}, bindRange{}, bind{};
void Init(trace::Id firstId) {
create = {firstId + 0, "glCreateBuffers", 2, createNames};
storage = {firstId + 1, "glNamedBufferStorage", 4, storageNames};
subData = {firstId + 2, "glNamedBufferSubData", 4, subDataNames};
bindRange = {firstId + 3, "glBindBufferRange", 5, bindRangeNames};
bind = {firstId + 4, "glBindBuffer", 2, bindNames};
}
};
class Rewriter {
public:
uint64_t align = 1024;
std::set<unsigned> mapCalls;
int Run(const char *in, const char *out) {
Scan(in);
if (relocSrc_.empty()) {
std::fprintf(stderr, "nothing to realign at %llu bytes\n", (unsigned long long)align);
return 2;
}
sigs_.Init(maxSigId_ + 1);
stride_ = (maxRelocSize_ + align - 1) / align * align;
Pass(in, nullptr); // dry run: count the slots
slotCap_ = slotsUsed_;
Pass(in, out);
std::fprintf(stderr,
"realigned %llu binds of %zu source buffer(s) to %llu bytes: %llu uploads, relocation buffer %llu "
"bytes, %llu calls inserted\n",
(unsigned long long)relocBinds_, relocSrc_.size(), (unsigned long long)align,
(unsigned long long)slotsUsed_, (unsigned long long)(slotCap_ * stride_),
(unsigned long long)inserted_);
for (const auto &m : callMap_) {
std::printf("call %u -> %llu\n", m.first, (unsigned long long)m.second);
}
return 0;
[[noreturn]] void Fail(const trace::Call* call, const std::string& what) {
std::fprintf(stderr, "error: call %u %s: %s\n", call ? call->no : 0u, call ? call->name() : "-", what.c_str());
std::exit(1);
}
private:
// Pass 0: which buffers need relocating, and the sizes involved.
void Scan(const char *in) {
trace::Parser p;
if (!p.open(in)) Fail(nullptr, std::string("cannot open ") + in);
trace::Call *call;
while ((call = p.parse_call())) {
maxSigId_ = std::max(maxSigId_, call->sig->id);
if (Is(call, "glBindBufferRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
uint64_t offset = uint64_t(call->arg(3).toSInt());
if (IsRangeTarget(target) && offset % align != 0) {
relocSrc_.insert(unsigned(call->arg(2).toUInt()));
maxRelocSize_ = std::max(maxRelocSize_, uint64_t(call->arg(4).toSInt()));
}
} else if (Is(call, "glBindBuffersRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
const trace::Array *offsets = call->arg(4).toArray();
if (IsRangeTarget(target) && offsets) {
for (const trace::Value *v : offsets->values) {
if (uint64_t(v->toSInt()) % align != 0) Fail(call, "misaligned glBindBuffersRange is not handled");
bool Is(const trace::Call* call, const char* name) {
return std::strcmp(call->name(), name) == 0;
}
bool StartsWith(const char* s, const char* prefix) {
return std::strncmp(s, prefix, std::strlen(prefix)) == 0;
}
bool ReadsIndexedBuffers(const trace::Call* call) {
const char* n = call->name();
return StartsWith(n, "glDraw") || StartsWith(n, "glMultiDraw") || StartsWith(n, "glDispatchCompute");
}
bool IsRangeTarget(unsigned target) {
return target == kUniformBuffer || target == kShaderStorageBuffer;
}
struct MapRegion {
unsigned buffer;
uint64_t offset;
uint64_t length;
uint64_t ptr;
};
struct Reloc {
unsigned src = 0;
uint64_t offset = 0;
uint64_t size = 0;
bool dirty = true;
std::vector<char> last;
const trace::EnumSig* targetSig = nullptr;
};
// Signatures for the calls this tool inserts; ids are allocated past the trace's own.
struct Sigs {
const char* createNames[2] = {"n", "buffers"};
const char* storageNames[4] = {"buffer", "size", "data", "flags"};
const char* subDataNames[4] = {"buffer", "offset", "size", "data"};
const char* bindRangeNames[5] = {"target", "index", "buffer", "offset", "size"};
const char* bindNames[2] = {"target", "buffer"};
trace::FunctionSig create{}, storage{}, subData{}, bindRange{}, bind{};
void Init(trace::Id firstId) {
create = {firstId + 0, "glCreateBuffers", 2, createNames};
storage = {firstId + 1, "glNamedBufferStorage", 4, storageNames};
subData = {firstId + 2, "glNamedBufferSubData", 4, subDataNames};
bindRange = {firstId + 3, "glBindBufferRange", 5, bindRangeNames};
bind = {firstId + 4, "glBindBuffer", 2, bindNames};
}
};
class Rewriter {
public:
uint64_t align = 1024;
std::set<unsigned> mapCalls;
int Run(const char* in, const char* out) {
Scan(in);
if (relocSrc_.empty()) {
std::fprintf(stderr, "nothing to realign at %llu bytes\n", (unsigned long long)align);
return 2;
}
sigs_.Init(maxSigId_ + 1);
stride_ = (maxRelocSize_ + align - 1) / align * align;
Pass(in, nullptr); // dry run: count the slots
slotCap_ = slotsUsed_;
Pass(in, out);
std::fprintf(
stderr,
"realigned %llu binds of %zu source buffer(s) to %llu bytes: %llu uploads, relocation buffer %llu "
"bytes, %llu calls inserted\n",
(unsigned long long)relocBinds_, relocSrc_.size(), (unsigned long long)align,
(unsigned long long)slotsUsed_, (unsigned long long)(slotCap_ * stride_),
(unsigned long long)inserted_);
for (const auto& m : callMap_) {
std::printf("call %u -> %llu\n", m.first, (unsigned long long)m.second);
}
return 0;
}
private:
// Pass 0: which buffers need relocating, and the sizes involved.
void Scan(const char* in) {
trace::Parser p;
if (!p.open(in)) Fail(nullptr, std::string("cannot open ") + in);
trace::Call* call;
while ((call = p.parse_call())) {
maxSigId_ = std::max(maxSigId_, call->sig->id);
if (Is(call, "glBindBufferRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
uint64_t offset = uint64_t(call->arg(3).toSInt());
if (IsRangeTarget(target) && offset % align != 0) {
relocSrc_.insert(unsigned(call->arg(2).toUInt()));
maxRelocSize_ = std::max(maxRelocSize_, uint64_t(call->arg(4).toSInt()));
}
} else if (Is(call, "glBindBuffersRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
const trace::Array* offsets = call->arg(4).toArray();
if (IsRangeTarget(target) && offsets) {
for (const trace::Value* v : offsets->values) {
if (uint64_t(v->toSInt()) % align != 0)
Fail(call, "misaligned glBindBuffersRange is not handled");
}
}
}
delete call;
}
delete call;
}
}
void Pass(const char *in, const char *out) {
trace::Parser p;
if (!p.open(in)) Fail(nullptr, std::string("cannot open ") + in);
trace::Writer writer;
writer_ = nullptr;
if (out) {
if (!writer.open(out, p.getVersion(), p.getProperties())) Fail(nullptr, std::string("cannot create ") + out);
writer_ = &writer;
void Pass(const char* in, const char* out) {
trace::Parser p;
if (!p.open(in)) Fail(nullptr, std::string("cannot open ") + in);
trace::Writer writer;
writer_ = nullptr;
if (out) {
if (!writer.open(out, p.getVersion(), p.getProperties()))
Fail(nullptr, std::string("cannot create ") + out);
writer_ = &writer;
}
shadow_.clear();
maps_.clear();
generic_.clear();
relocs_.clear();
slotsUsed_ = 0;
relocBinds_ = 0;
inserted_ = 0;
created_ = false;
trace::Call* call;
while ((call = p.parse_call())) {
Process(call);
delete call;
}
if (writer_) writer.close();
writer_ = nullptr;
}
shadow_.clear();
maps_.clear();
generic_.clear();
relocs_.clear();
slotsUsed_ = 0;
relocBinds_ = 0;
inserted_ = 0;
created_ = false;
trace::Call *call;
while ((call = p.parse_call())) {
Process(call);
delete call;
unsigned BufferArg(trace::Call* call, unsigned index) { return unsigned(call->arg(index).toUInt()); }
unsigned Generic(unsigned target) {
auto it = generic_.find(target);
return it == generic_.end() ? 0 : it->second;
}
if (writer_) writer.close();
writer_ = nullptr;
}
unsigned BufferArg(trace::Call *call, unsigned index) { return unsigned(call->arg(index).toUInt()); }
unsigned Generic(unsigned target) {
auto it = generic_.find(target);
return it == generic_.end() ? 0 : it->second;
}
std::vector<char> *Shadow(unsigned buffer) {
auto it = shadow_.find(buffer);
return it == shadow_.end() ? nullptr : &it->second;
}
void Specify(trace::Call *call, unsigned buffer, uint64_t size, trace::Value *data) {
if (!relocSrc_.count(buffer)) return;
std::vector<char> &s = shadow_[buffer];
s.assign(size, 0);
const trace::Blob *blob = data ? data->toBlob() : nullptr;
if (blob) std::memcpy(s.data(), blob->buf, std::min<uint64_t>(size, blob->size));
for (auto &r : relocs_) {
if (r.second.src == buffer) r.second.dirty = true;
std::vector<char>* Shadow(unsigned buffer) {
auto it = shadow_.find(buffer);
return it == shadow_.end() ? nullptr : &it->second;
}
(void)call;
}
void Write(trace::Call *call, unsigned buffer, uint64_t offset, const char *bytes, uint64_t size) {
std::vector<char> *s = Shadow(buffer);
if (!s) return;
if (offset + size > s->size()) Fail(call, "write past the end of a tracked buffer");
std::memcpy(s->data() + offset, bytes, size);
}
void Specify(trace::Call* call, unsigned buffer, uint64_t size, trace::Value* data) {
if (!relocSrc_.count(buffer)) return;
std::vector<char>& s = shadow_[buffer];
s.assign(size, 0);
const trace::Blob* blob = data ? data->toBlob() : nullptr;
if (blob) std::memcpy(s.data(), blob->buf, std::min<uint64_t>(size, blob->size));
for (auto& r : relocs_) {
if (r.second.src == buffer) r.second.dirty = true;
}
(void)call;
}
void Refuse(trace::Call *call, unsigned buffer) {
if (relocSrc_.count(buffer)) Fail(call, "writes a relocated source buffer in a way the trace does not carry the bytes of");
}
void Write(trace::Call* call, unsigned buffer, uint64_t offset, const char* bytes, uint64_t size) {
std::vector<char>* s = Shadow(buffer);
if (!s) return;
if (offset + size > s->size()) Fail(call, "write past the end of a tracked buffer");
std::memcpy(s->data() + offset, bytes, size);
}
void Emit(trace::Call *call) {
if (!writer_) return;
if (mapCalls.count(call->no)) callMap_[call->no] = writtenCalls_;
writer_->writeCall(call);
++writtenCalls_;
}
void Refuse(trace::Call* call, unsigned buffer) {
if (relocSrc_.count(buffer))
Fail(call, "writes a relocated source buffer in a way the trace does not carry the bytes of");
}
void EmitNew(trace::Call *call) {
++inserted_;
if (writer_) {
void Emit(trace::Call* call) {
if (!writer_) return;
if (mapCalls.count(call->no)) callMap_[call->no] = writtenCalls_;
writer_->writeCall(call);
++writtenCalls_;
}
delete call;
}
trace::Call *NewCall(const trace::FunctionSig *sig, unsigned thread) {
return new trace::Call(sig, trace::CallFlags(0), thread);
}
void EmitBind(unsigned thread, const trace::EnumSig *targetSig, unsigned target, unsigned buffer) {
trace::Call *c = NewCall(&sigs_.bind, thread);
c->args[0].value = targetSig ? static_cast<trace::Value *>(new trace::Enum(targetSig, target))
: static_cast<trace::Value *>(new trace::SInt(target));
c->args[1].value = new trace::UInt(buffer);
EmitNew(c);
}
void CreateRelocBuffer(unsigned thread) {
trace::Call *c = NewCall(&sigs_.create, thread);
c->args[0].value = new trace::SInt(1);
trace::Array *names = new trace::Array(1);
names->values[0] = new trace::UInt(kRelocBufferName);
c->args[1].value = names;
EmitNew(c);
c = NewCall(&sigs_.storage, thread);
c->args[0].value = new trace::UInt(kRelocBufferName);
c->args[1].value = new trace::SInt(int64_t(slotCap_ * stride_));
c->args[2].value = new trace::Null();
c->args[3].value = new trace::UInt(kDynamicStorageBit);
EmitNew(c);
created_ = true;
}
void Upload(trace::Call *before, unsigned target, unsigned index, Reloc &r) {
std::vector<char> *s = Shadow(r.src);
if (!s) Fail(before, "a relocated range reads a buffer whose storage the trace never specified");
if (r.offset + r.size > s->size()) Fail(before, "a relocated range runs past its buffer");
std::vector<char> bytes(s->begin() + r.offset, s->begin() + r.offset + r.size);
if (!r.dirty && bytes == r.last) return;
unsigned thread = before->thread_id;
if (writer_ && slotsUsed_ >= slotCap_) Fail(before, "relocation slots exceeded the dry run");
uint64_t slotOffset = slotsUsed_++ * stride_;
if (!created_) CreateRelocBuffer(thread);
trace::Call *c = NewCall(&sigs_.subData, thread);
c->args[0].value = new trace::UInt(kRelocBufferName);
c->args[1].value = new trace::SInt(int64_t(slotOffset));
c->args[2].value = new trace::SInt(int64_t(r.size));
trace::Blob *blob = new trace::Blob(r.size);
std::memcpy(blob->buf, bytes.data(), r.size);
c->args[3].value = blob;
EmitNew(c);
c = NewCall(&sigs_.bindRange, thread);
c->args[0].value = r.targetSig ? static_cast<trace::Value *>(new trace::Enum(r.targetSig, target))
: static_cast<trace::Value *>(new trace::SInt(target));
c->args[1].value = new trace::UInt(index);
c->args[2].value = new trace::UInt(kRelocBufferName);
c->args[3].value = new trace::SInt(int64_t(slotOffset));
c->args[4].value = new trace::SInt(int64_t(r.size));
EmitNew(c);
// glBindBufferRange also moved the generic binding; put back the app's.
EmitBind(thread, r.targetSig, target, Generic(target));
r.last = std::move(bytes);
r.dirty = false;
}
void Process(trace::Call *call) {
const char *n = call->name();
if (Is(call, "glBindBuffer")) {
generic_[unsigned(call->arg(0).toSInt())] = BufferArg(call, 1);
} else if (Is(call, "glBindBufferBase")) {
unsigned target = unsigned(call->arg(0).toSInt());
generic_[target] = BufferArg(call, 2);
relocs_.erase({target, unsigned(call->arg(1).toUInt())});
} else if (Is(call, "glBindBufferRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
unsigned index = unsigned(call->arg(1).toUInt());
unsigned buffer = BufferArg(call, 2);
uint64_t offset = uint64_t(call->arg(3).toSInt());
generic_[target] = buffer;
if (IsRangeTarget(target) && offset % align != 0) {
Reloc r;
r.src = buffer;
r.offset = offset;
r.size = uint64_t(call->arg(4).toSInt());
trace::Value &t = call->arg(0);
trace::Enum *e = nullptr;
// Enum values print by name in dumps; keep the trace's own enum signature when it has one.
struct EnumProbe : trace::Visitor {
trace::Enum *found = nullptr;
void visit(trace::Enum *node) override { found = node; }
} probe;
t.visit(probe);
e = probe.found;
r.targetSig = e ? e->sig : nullptr;
relocs_[{target, index}] = std::move(r);
++relocBinds_;
if (mapCalls.count(call->no) && writer_) callMap_[call->no] = writtenCalls_;
// Only the generic binding survives at this point; the indexed one is bound before its first read.
EmitBind(call->thread_id, e ? e->sig : nullptr, target, buffer);
--inserted_; // a replacement, not an insertion
return;
void EmitNew(trace::Call* call) {
++inserted_;
if (writer_) {
writer_->writeCall(call);
++writtenCalls_;
}
relocs_.erase({target, index});
} else if (Is(call, "glBindBuffersBase") || Is(call, "glBindBuffersRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
unsigned first = unsigned(call->arg(1).toUInt());
unsigned count = unsigned(call->arg(2).toSInt());
for (unsigned i = first; i < first + count; ++i) relocs_.erase({target, i});
} else if (Is(call, "glNamedBufferStorage") || Is(call, "glNamedBufferData")) {
Specify(call, BufferArg(call, 0), uint64_t(call->arg(1).toSInt()), &call->arg(2));
} else if (Is(call, "glBufferStorage") || Is(call, "glBufferData")) {
Specify(call, Generic(unsigned(call->arg(0).toSInt())), uint64_t(call->arg(1).toSInt()), &call->arg(2));
} else if (Is(call, "glNamedBufferSubData") || Is(call, "glBufferSubData")) {
unsigned buffer = Is(call, "glNamedBufferSubData") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
if (relocSrc_.count(buffer)) {
const trace::Blob *blob = call->arg(3).toBlob();
if (!blob) Fail(call, "subdata without bytes");
Write(call, buffer, uint64_t(call->arg(1).toSInt()), blob->buf,
std::min<uint64_t>(uint64_t(call->arg(2).toSInt()), blob->size));
}
} else if (Is(call, "glCopyNamedBufferSubData")) {
Refuse(call, BufferArg(call, 1));
} else if (Is(call, "glCopyBufferSubData")) {
Refuse(call, Generic(unsigned(call->arg(1).toSInt())));
} else if (StartsWith(n, "glClearNamedBuffer")) {
Refuse(call, BufferArg(call, 0));
} else if (StartsWith(n, "glClearBuffer") && (StartsWith(n, "glClearBufferData") || StartsWith(n, "glClearBufferSubData"))) {
Refuse(call, Generic(unsigned(call->arg(0).toSInt())));
} else if (Is(call, "glMapNamedBufferRange") || Is(call, "glMapBufferRange")) {
unsigned buffer = Is(call, "glMapNamedBufferRange") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
if (relocSrc_.count(buffer) && call->ret) {
maps_.push_back({buffer, uint64_t(call->arg(1).toSInt()), uint64_t(call->arg(2).toSInt()),
call->ret->toUIntPtr()});
}
} else if (Is(call, "glMapNamedBuffer") || Is(call, "glMapBuffer")) {
unsigned buffer = Is(call, "glMapNamedBuffer") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
std::vector<char> *s = Shadow(buffer);
if (s && call->ret) maps_.push_back({buffer, 0, s->size(), call->ret->toUIntPtr()});
} else if (Is(call, "glUnmapNamedBuffer") || Is(call, "glUnmapBuffer")) {
unsigned buffer = Is(call, "glUnmapNamedBuffer") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
maps_.erase(std::remove_if(maps_.begin(), maps_.end(), [&](const MapRegion &m) { return m.buffer == buffer; }),
maps_.end());
} else if (Is(call, "memcpy")) {
uint64_t dest = call->arg(0).toUIntPtr();
const trace::Blob *blob = call->arg(1).toBlob();
uint64_t size = call->arg(2).toUInt();
if (blob) {
size = std::min<uint64_t>(size, blob->size);
for (const MapRegion &m : maps_) {
uint64_t lo = std::max(dest, m.ptr);
uint64_t hi = std::min(dest + size, m.ptr + m.length);
if (lo >= hi) continue;
Write(call, m.buffer, m.offset + (lo - m.ptr), blob->buf + (lo - dest), hi - lo);
}
}
} else if (ReadsIndexedBuffers(call)) {
for (auto &r : relocs_) Upload(call, r.first.first, r.first.second, r.second);
delete call;
}
Emit(call);
}
trace::Call* NewCall(const trace::FunctionSig* sig, unsigned thread) {
return new trace::Call(sig, trace::CallFlags(0), thread);
}
Sigs sigs_;
trace::Id maxSigId_ = 0;
std::set<unsigned> relocSrc_;
uint64_t maxRelocSize_ = 0;
uint64_t stride_ = 0;
uint64_t slotCap_ = 0;
uint64_t slotsUsed_ = 0;
uint64_t relocBinds_ = 0;
uint64_t inserted_ = 0;
uint64_t writtenCalls_ = 0;
bool created_ = false;
trace::Writer *writer_ = nullptr;
std::unordered_map<unsigned, std::vector<char>> shadow_;
std::vector<MapRegion> maps_;
std::unordered_map<unsigned, unsigned> generic_;
std::map<std::pair<unsigned, unsigned>, Reloc> relocs_;
std::map<unsigned, uint64_t> callMap_;
};
void EmitBind(unsigned thread, const trace::EnumSig* targetSig, unsigned target, unsigned buffer) {
trace::Call* c = NewCall(&sigs_.bind, thread);
c->args[0].value = targetSig ? static_cast<trace::Value*>(new trace::Enum(targetSig, target))
: static_cast<trace::Value*>(new trace::SInt(target));
c->args[1].value = new trace::UInt(buffer);
EmitNew(c);
}
} // namespace
void CreateRelocBuffer(unsigned thread) {
trace::Call* c = NewCall(&sigs_.create, thread);
c->args[0].value = new trace::SInt(1);
trace::Array* names = new trace::Array(1);
names->values[0] = new trace::UInt(kRelocBufferName);
c->args[1].value = names;
EmitNew(c);
int main(int argc, char **argv) {
c = NewCall(&sigs_.storage, thread);
c->args[0].value = new trace::UInt(kRelocBufferName);
c->args[1].value = new trace::SInt(int64_t(slotCap_ * stride_));
c->args[2].value = new trace::Null();
c->args[3].value = new trace::UInt(kDynamicStorageBit);
EmitNew(c);
created_ = true;
}
void Upload(trace::Call* before, unsigned target, unsigned index, Reloc& r) {
std::vector<char>* s = Shadow(r.src);
if (!s) Fail(before, "a relocated range reads a buffer whose storage the trace never specified");
if (r.offset + r.size > s->size()) Fail(before, "a relocated range runs past its buffer");
std::vector<char> bytes(s->begin() + r.offset, s->begin() + r.offset + r.size);
if (!r.dirty && bytes == r.last) return;
unsigned thread = before->thread_id;
if (writer_ && slotsUsed_ >= slotCap_) Fail(before, "relocation slots exceeded the dry run");
uint64_t slotOffset = slotsUsed_++ * stride_;
if (!created_) CreateRelocBuffer(thread);
trace::Call* c = NewCall(&sigs_.subData, thread);
c->args[0].value = new trace::UInt(kRelocBufferName);
c->args[1].value = new trace::SInt(int64_t(slotOffset));
c->args[2].value = new trace::SInt(int64_t(r.size));
trace::Blob* blob = new trace::Blob(r.size);
std::memcpy(blob->buf, bytes.data(), r.size);
c->args[3].value = blob;
EmitNew(c);
c = NewCall(&sigs_.bindRange, thread);
c->args[0].value = r.targetSig ? static_cast<trace::Value*>(new trace::Enum(r.targetSig, target))
: static_cast<trace::Value*>(new trace::SInt(target));
c->args[1].value = new trace::UInt(index);
c->args[2].value = new trace::UInt(kRelocBufferName);
c->args[3].value = new trace::SInt(int64_t(slotOffset));
c->args[4].value = new trace::SInt(int64_t(r.size));
EmitNew(c);
// glBindBufferRange also moved the generic binding; put back the app's.
EmitBind(thread, r.targetSig, target, Generic(target));
r.last = std::move(bytes);
r.dirty = false;
}
void Process(trace::Call* call) {
const char* n = call->name();
if (Is(call, "glBindBuffer")) {
generic_[unsigned(call->arg(0).toSInt())] = BufferArg(call, 1);
} else if (Is(call, "glBindBufferBase")) {
unsigned target = unsigned(call->arg(0).toSInt());
generic_[target] = BufferArg(call, 2);
relocs_.erase({target, unsigned(call->arg(1).toUInt())});
} else if (Is(call, "glBindBufferRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
unsigned index = unsigned(call->arg(1).toUInt());
unsigned buffer = BufferArg(call, 2);
uint64_t offset = uint64_t(call->arg(3).toSInt());
generic_[target] = buffer;
if (IsRangeTarget(target) && offset % align != 0) {
Reloc r;
r.src = buffer;
r.offset = offset;
r.size = uint64_t(call->arg(4).toSInt());
trace::Value& t = call->arg(0);
trace::Enum* e = nullptr;
// Enum values print by name in dumps; keep the trace's own enum signature when it has one.
struct EnumProbe : trace::Visitor {
trace::Enum* found = nullptr;
void visit(trace::Enum* node) override { found = node; }
} probe;
t.visit(probe);
e = probe.found;
r.targetSig = e ? e->sig : nullptr;
relocs_[{target, index}] = std::move(r);
++relocBinds_;
if (mapCalls.count(call->no) && writer_) callMap_[call->no] = writtenCalls_;
// Only the generic binding survives at this point; the indexed one is bound before its first read.
EmitBind(call->thread_id, e ? e->sig : nullptr, target, buffer);
--inserted_; // a replacement, not an insertion
return;
}
relocs_.erase({target, index});
} else if (Is(call, "glBindBuffersBase") || Is(call, "glBindBuffersRange")) {
unsigned target = unsigned(call->arg(0).toSInt());
unsigned first = unsigned(call->arg(1).toUInt());
unsigned count = unsigned(call->arg(2).toSInt());
for (unsigned i = first; i < first + count; ++i)
relocs_.erase({target, i});
} else if (Is(call, "glNamedBufferStorage") || Is(call, "glNamedBufferData")) {
Specify(call, BufferArg(call, 0), uint64_t(call->arg(1).toSInt()), &call->arg(2));
} else if (Is(call, "glBufferStorage") || Is(call, "glBufferData")) {
Specify(call, Generic(unsigned(call->arg(0).toSInt())), uint64_t(call->arg(1).toSInt()), &call->arg(2));
} else if (Is(call, "glNamedBufferSubData") || Is(call, "glBufferSubData")) {
unsigned buffer =
Is(call, "glNamedBufferSubData") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
if (relocSrc_.count(buffer)) {
const trace::Blob* blob = call->arg(3).toBlob();
if (!blob) Fail(call, "subdata without bytes");
Write(call, buffer, uint64_t(call->arg(1).toSInt()), blob->buf,
std::min<uint64_t>(uint64_t(call->arg(2).toSInt()), blob->size));
}
} else if (Is(call, "glCopyNamedBufferSubData")) {
Refuse(call, BufferArg(call, 1));
} else if (Is(call, "glCopyBufferSubData")) {
Refuse(call, Generic(unsigned(call->arg(1).toSInt())));
} else if (StartsWith(n, "glClearNamedBuffer")) {
Refuse(call, BufferArg(call, 0));
} else if (StartsWith(n, "glClearBuffer") &&
(StartsWith(n, "glClearBufferData") || StartsWith(n, "glClearBufferSubData"))) {
Refuse(call, Generic(unsigned(call->arg(0).toSInt())));
} else if (Is(call, "glMapNamedBufferRange") || Is(call, "glMapBufferRange")) {
unsigned buffer =
Is(call, "glMapNamedBufferRange") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
if (relocSrc_.count(buffer) && call->ret) {
maps_.push_back({buffer, uint64_t(call->arg(1).toSInt()), uint64_t(call->arg(2).toSInt()),
call->ret->toUIntPtr()});
}
} else if (Is(call, "glMapNamedBuffer") || Is(call, "glMapBuffer")) {
unsigned buffer =
Is(call, "glMapNamedBuffer") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
std::vector<char>* s = Shadow(buffer);
if (s && call->ret) maps_.push_back({buffer, 0, s->size(), call->ret->toUIntPtr()});
} else if (Is(call, "glUnmapNamedBuffer") || Is(call, "glUnmapBuffer")) {
unsigned buffer =
Is(call, "glUnmapNamedBuffer") ? BufferArg(call, 0) : Generic(unsigned(call->arg(0).toSInt()));
maps_.erase(
std::remove_if(maps_.begin(), maps_.end(), [&](const MapRegion& m) { return m.buffer == buffer; }),
maps_.end());
} else if (Is(call, "memcpy")) {
uint64_t dest = call->arg(0).toUIntPtr();
const trace::Blob* blob = call->arg(1).toBlob();
uint64_t size = call->arg(2).toUInt();
if (blob) {
size = std::min<uint64_t>(size, blob->size);
for (const MapRegion& m : maps_) {
uint64_t lo = std::max(dest, m.ptr);
uint64_t hi = std::min(dest + size, m.ptr + m.length);
if (lo >= hi) continue;
Write(call, m.buffer, m.offset + (lo - m.ptr), blob->buf + (lo - dest), hi - lo);
}
}
} else if (ReadsIndexedBuffers(call)) {
for (auto& r : relocs_)
Upload(call, r.first.first, r.first.second, r.second);
}
Emit(call);
}
Sigs sigs_;
trace::Id maxSigId_ = 0;
std::set<unsigned> relocSrc_;
uint64_t maxRelocSize_ = 0;
uint64_t stride_ = 0;
uint64_t slotCap_ = 0;
uint64_t slotsUsed_ = 0;
uint64_t relocBinds_ = 0;
uint64_t inserted_ = 0;
uint64_t writtenCalls_ = 0;
bool created_ = false;
trace::Writer* writer_ = nullptr;
std::unordered_map<unsigned, std::vector<char>> shadow_;
std::vector<MapRegion> maps_;
std::unordered_map<unsigned, unsigned> generic_;
std::map<std::pair<unsigned, unsigned>, Reloc> relocs_;
std::map<unsigned, uint64_t> callMap_;
};
} // namespace
int main(int argc, char** argv) {
Rewriter rw;
std::vector<const char *> files;
std::vector<const char*> files;
for (int i = 1; i < argc; ++i) {
if (!std::strcmp(argv[i], "--align") && i + 1 < argc) {
rw.align = std::strtoull(argv[++i], nullptr, 0);
+30 -34
View File
@@ -26,50 +26,47 @@
namespace {
int gFailures = 0;
int gFailures = 0;
void ExpectSplit(const std::string& input, const std::vector<std::string>& expected) {
const std::vector<std::string> actual = mobilegl_trace::SplitSemicolonList(input);
if (actual == expected) {
return;
void ExpectSplit(const std::string& input, const std::vector<std::string>& expected) {
const std::vector<std::string> actual = mobilegl_trace::SplitSemicolonList(input);
if (actual == expected) {
return;
}
++gFailures;
std::cerr << "SplitSemicolonList(\"" << input << "\") gave " << actual.size() << " entries, expected "
<< expected.size() << ":";
for (const std::string& entry : actual) {
std::cerr << " [" << entry << "]";
}
std::cerr << "\n";
}
++gFailures;
std::cerr << "SplitSemicolonList(\"" << input << "\") gave " << actual.size()
<< " entries, expected " << expected.size() << ":";
for (const std::string& entry : actual) {
std::cerr << " [" << entry << "]";
}
std::cerr << "\n";
}
const char* ActionName(mobilegl_trace::EnvOverrideAction action) {
switch (action) {
const char* ActionName(mobilegl_trace::EnvOverrideAction action) {
switch (action) {
case mobilegl_trace::EnvOverrideAction::Ignore:
return "Ignore";
case mobilegl_trace::EnvOverrideAction::Set:
return "Set";
case mobilegl_trace::EnvOverrideAction::Unset:
return "Unset";
}
return "?";
}
return "?";
}
void ExpectParse(const std::string& entry,
mobilegl_trace::EnvOverrideAction expectedAction,
const std::string& expectedKey,
const std::string& expectedValue) {
std::string key = "<untouched>";
std::string value = "<untouched>";
const mobilegl_trace::EnvOverrideAction action =
mobilegl_trace::ParseEnvOverride(entry, &key, &value);
if (action == expectedAction && key == expectedKey && value == expectedValue) {
return;
void ExpectParse(const std::string& entry, mobilegl_trace::EnvOverrideAction expectedAction,
const std::string& expectedKey, const std::string& expectedValue) {
std::string key = "<untouched>";
std::string value = "<untouched>";
const mobilegl_trace::EnvOverrideAction action = mobilegl_trace::ParseEnvOverride(entry, &key, &value);
if (action == expectedAction && key == expectedKey && value == expectedValue) {
return;
}
++gFailures;
std::cerr << "ParseEnvOverride(\"" << entry << "\") gave " << ActionName(action) << " key=[" << key
<< "] value=[" << value << "], expected " << ActionName(expectedAction) << " key=[" << expectedKey
<< "] value=[" << expectedValue << "]\n";
}
++gFailures;
std::cerr << "ParseEnvOverride(\"" << entry << "\") gave " << ActionName(action) << " key=["
<< key << "] value=[" << value << "], expected " << ActionName(expectedAction)
<< " key=[" << expectedKey << "] value=[" << expectedValue << "]\n";
}
} // namespace
@@ -79,8 +76,7 @@ int main() {
// Splitting.
ExpectSplit("", {});
ExpectSplit("MOBILEGL_PIPE_PUSH=1", {"MOBILEGL_PIPE_PUSH=1"});
ExpectSplit("MOBILEGL_PIPE_PUSH=1;MOBILEGL_PIPE_VERIFY=1",
{"MOBILEGL_PIPE_PUSH=1", "MOBILEGL_PIPE_VERIFY=1"});
ExpectSplit("MOBILEGL_PIPE_PUSH=1;MOBILEGL_PIPE_VERIFY=1", {"MOBILEGL_PIPE_PUSH=1", "MOBILEGL_PIPE_VERIFY=1"});
// A trailing ';' is what a caller that joins a list gets for free, and an empty entry
// must not be turned into an unsetenv("") - the whole passthrough would then depend on
// how carefully the shell script trimmed its own string.
+188 -188
View File
@@ -17,206 +17,206 @@ extern "C" void mobilegl_trace_set_requested_size(int width, int height);
namespace {
void PrintUsage(const char *argv0) {
std::cerr
<< "Usage: " << argv0 << " --trace PATH --target-call N --output DIR [options]\n"
<< "\n"
<< "Options:\n"
<< " --golden PATH Golden PNG to compare against\n"
<< " --alternate-golden PATH Additional acceptable golden PNG\n"
<< " --diff PATH Difference PNG output path\n"
<< " --backend NAME DirectGLES or DirectVulkan (default: DirectGLES)\n"
<< " --mobilegl-library PATH libMobileGL.so path (default: libMobileGL.so)\n"
<< " --width N Replay surface width override\n"
<< " --height N Replay surface height override\n"
<< " --window-surface Replay to a native window surface\n"
<< " --pbuffer-surface Replay to an EGL pbuffer surface (default)\n"
<< " --hold-ms N Keep the replay process alive after retrace (default: 0)\n"
<< " --ssim-threshold N Minimum SSIM required to pass (default: 0.99)\n"
<< " --crop-x N Compare crop x\n"
<< " --crop-y N Compare crop y\n"
<< " --crop-width N Compare crop width\n"
<< " --crop-height N Compare crop height\n"
<< " --coherent-as-flush Set MOBILEGL_COHERENT_AS_FLUSH=1 for the replay\n"
<< " --benchmark Frame-timing mode: replay the whole trace, record a\n"
<< " wall-clock timestamp at every frame boundary, and skip\n"
<< " the snapshot and the golden comparison. --target-call is\n"
<< " not required in this mode.\n"
<< " --benchmark-tail-frames=N Frames at the end of the run the mean/median/p95 are\n"
<< " computed over (default: 200, clamped to the frame count)\n"
<< " --benchmark-finish=0/1 glFinish at every frame boundary so a frame time covers\n"
<< " GPU completion and not just CPU submission (default: 1)\n"
<< " --benchmark-result=PATH Timing JSON output (default: OUTPUT/benchmark.json)\n"
<< " --dump-fbo-attachments CALL:DIR[:FBO,FBO,...]\n"
<< " At CALL, write every colour attachment and the depth\n"
<< " attachment of every live framebuffer object into DIR as\n"
<< " fbo<N>-att<M>.png / fbo<N>-depth.png, plus a manifest.txt\n"
<< " of formats and per-channel statistics. Repeatable.\n";
}
bool ReadValue(int argc, char **argv, int &index, std::string &out) {
if (index + 1 >= argc) {
std::cerr << "Missing value for " << argv[index] << "\n";
return false;
}
out = argv[++index];
return true;
}
bool ReadInt(int argc, char **argv, int &index, int &out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::atoi(value.c_str());
return true;
}
bool ReadLongLong(int argc, char **argv, int &index, long long &out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::strtoll(value.c_str(), nullptr, 10);
return true;
}
bool ReadDouble(int argc, char **argv, int &index, double &out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::strtod(value.c_str(), nullptr);
return true;
}
// The benchmark options are documented as --name=VALUE. The space-separated spelling every
// other option here uses is accepted too, so --benchmark-tail-frames=300 and
// --benchmark-tail-frames 300 both work; hasInlineValue says which one was given.
bool MatchOption(const std::string &arg, const char *name, std::string &inlineValue, bool &hasInlineValue) {
if (arg == name) {
hasInlineValue = false;
return true;
}
const std::string prefix = std::string(name) + "=";
if (arg.compare(0, prefix.size(), prefix) == 0) {
inlineValue = arg.substr(prefix.size());
hasInlineValue = true;
return true;
}
return false;
}
bool ParseArgs(int argc, char **argv, mobilegl_trace::Request &request) {
request.backend = "DirectGLES";
// MOBILEGL_ITRP_FIX is a tri-state (Auto/ForceOn/ForceOff); the request only carries ForceOn.
if (const char *itrpFix = std::getenv("MOBILEGL_ITRP_FIX")) {
std::string value = itrpFix;
for (char &ch : value) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
request.iterationRPFix = value == "1" || value == "true" || value == "on" || value == "forceon";
void PrintUsage(const char* argv0) {
std::cerr << "Usage: " << argv0 << " --trace PATH --target-call N --output DIR [options]\n"
<< "\n"
<< "Options:\n"
<< " --golden PATH Golden PNG to compare against\n"
<< " --alternate-golden PATH Additional acceptable golden PNG\n"
<< " --diff PATH Difference PNG output path\n"
<< " --backend NAME DirectGLES or DirectVulkan (default: DirectGLES)\n"
<< " --mobilegl-library PATH libMobileGL.so path (default: libMobileGL.so)\n"
<< " --width N Replay surface width override\n"
<< " --height N Replay surface height override\n"
<< " --window-surface Replay to a native window surface\n"
<< " --pbuffer-surface Replay to an EGL pbuffer surface (default)\n"
<< " --hold-ms N Keep the replay process alive after retrace (default: 0)\n"
<< " --ssim-threshold N Minimum SSIM required to pass (default: 0.99)\n"
<< " --crop-x N Compare crop x\n"
<< " --crop-y N Compare crop y\n"
<< " --crop-width N Compare crop width\n"
<< " --crop-height N Compare crop height\n"
<< " --coherent-as-flush Set MOBILEGL_COHERENT_AS_FLUSH=1 for the replay\n"
<< " --benchmark Frame-timing mode: replay the whole trace, record a\n"
<< " wall-clock timestamp at every frame boundary, and skip\n"
<< " the snapshot and the golden comparison. --target-call is\n"
<< " not required in this mode.\n"
<< " --benchmark-tail-frames=N Frames at the end of the run the mean/median/p95 are\n"
<< " computed over (default: 200, clamped to the frame count)\n"
<< " --benchmark-finish=0/1 glFinish at every frame boundary so a frame time covers\n"
<< " GPU completion and not just CPU submission (default: 1)\n"
<< " --benchmark-result=PATH Timing JSON output (default: OUTPUT/benchmark.json)\n"
<< " --dump-fbo-attachments CALL:DIR[:FBO,FBO,...]\n"
<< " At CALL, write every colour attachment and the depth\n"
<< " attachment of every live framebuffer object into DIR as\n"
<< " fbo<N>-att<M>.png / fbo<N>-depth.png, plus a manifest.txt\n"
<< " of formats and per-channel statistics. Repeatable.\n";
}
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
std::string optionValue;
bool optionHasValue = false;
if (arg == "--trace") {
if (!ReadValue(argc, argv, i, request.tracePath)) return false;
} else if (arg == "--golden") {
if (!ReadValue(argc, argv, i, request.goldenPath)) return false;
} else if (arg == "--alternate-golden") {
std::string alternateGolden;
if (!ReadValue(argc, argv, i, alternateGolden)) return false;
if (!alternateGolden.empty()) {
request.alternateGoldenPaths.push_back(alternateGolden);
}
} else if (arg == "--diff") {
if (!ReadValue(argc, argv, i, request.diffPath)) return false;
} else if (arg == "--output") {
if (!ReadValue(argc, argv, i, request.outputDir)) return false;
} else if (arg == "--backend") {
if (!ReadValue(argc, argv, i, request.backend)) return false;
} else if (arg == "--mobilegl-library") {
if (!ReadValue(argc, argv, i, request.mobileGlLibrary)) return false;
} else if (arg == "--target-frame") {
if (!ReadInt(argc, argv, i, request.targetFrame)) return false;
} else if (arg == "--target-call") {
if (!ReadLongLong(argc, argv, i, request.targetCall)) return false;
} else if (arg == "--width") {
if (!ReadInt(argc, argv, i, request.width)) return false;
} else if (arg == "--height") {
if (!ReadInt(argc, argv, i, request.height)) return false;
} else if (arg == "--window-surface") {
request.usePbuffer = false;
} else if (arg == "--pbuffer-surface") {
request.usePbuffer = true;
} else if (arg == "--hold-ms") {
if (!ReadInt(argc, argv, i, request.holdMs)) return false;
} else if (arg == "--ssim-threshold") {
if (!ReadDouble(argc, argv, i, request.ssimThreshold)) return false;
} else if (arg == "--crop-x") {
if (!ReadInt(argc, argv, i, request.cropX)) return false;
} else if (arg == "--crop-y") {
if (!ReadInt(argc, argv, i, request.cropY)) return false;
} else if (arg == "--crop-width") {
if (!ReadInt(argc, argv, i, request.cropWidth)) return false;
} else if (arg == "--crop-height") {
if (!ReadInt(argc, argv, i, request.cropHeight)) return false;
} else if (arg == "--coherent-as-flush") {
request.coherentAsFlush = true;
} else if (arg == "--benchmark") {
request.benchmark = true;
} else if (MatchOption(arg, "--benchmark-tail-frames", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkTailFrames = std::atoi(optionValue.c_str());
} else if (MatchOption(arg, "--benchmark-finish", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkFinish = optionValue != "0";
} else if (MatchOption(arg, "--benchmark-result", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkResultPath = optionValue;
} else if (arg == "--dump-fbo-attachments") {
std::string dumpPoint;
if (!ReadValue(argc, argv, i, dumpPoint)) return false;
if (dumpPoint.find(':') == std::string::npos) {
std::cerr << "--dump-fbo-attachments expects CALL:DIR[:FBO,FBO,...]\n";
return false;
}
request.fboAttachmentDumps.push_back(dumpPoint);
} else if (arg == "--help" || arg == "-h") {
return false;
} else {
std::cerr << "Unknown argument: " << arg << "\n";
bool ReadValue(int argc, char** argv, int& index, std::string& out) {
if (index + 1 >= argc) {
std::cerr << "Missing value for " << argv[index] << "\n";
return false;
}
out = argv[++index];
return true;
}
if (request.tracePath.empty()) {
std::cerr << "--trace is required\n";
bool ReadInt(int argc, char** argv, int& index, int& out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::atoi(value.c_str());
return true;
}
bool ReadLongLong(int argc, char** argv, int& index, long long& out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::strtoll(value.c_str(), nullptr, 10);
return true;
}
bool ReadDouble(int argc, char** argv, int& index, double& out) {
std::string value;
if (!ReadValue(argc, argv, index, value)) {
return false;
}
out = std::strtod(value.c_str(), nullptr);
return true;
}
// The benchmark options are documented as --name=VALUE. The space-separated spelling every
// other option here uses is accepted too, so --benchmark-tail-frames=300 and
// --benchmark-tail-frames 300 both work; hasInlineValue says which one was given.
bool MatchOption(const std::string& arg, const char* name, std::string& inlineValue, bool& hasInlineValue) {
if (arg == name) {
hasInlineValue = false;
return true;
}
const std::string prefix = std::string(name) + "=";
if (arg.compare(0, prefix.size(), prefix) == 0) {
inlineValue = arg.substr(prefix.size());
hasInlineValue = true;
return true;
}
return false;
}
if (request.outputDir.empty()) {
std::cerr << "--output is required\n";
return false;
bool ParseArgs(int argc, char** argv, mobilegl_trace::Request& request) {
request.backend = "DirectGLES";
// MOBILEGL_ITRP_FIX is a tri-state (Auto/ForceOn/ForceOff); the request only carries ForceOn.
if (const char* itrpFix = std::getenv("MOBILEGL_ITRP_FIX")) {
std::string value = itrpFix;
for (char& ch : value)
ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
request.iterationRPFix = value == "1" || value == "true" || value == "on" || value == "forceon";
}
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
std::string optionValue;
bool optionHasValue = false;
if (arg == "--trace") {
if (!ReadValue(argc, argv, i, request.tracePath)) return false;
} else if (arg == "--golden") {
if (!ReadValue(argc, argv, i, request.goldenPath)) return false;
} else if (arg == "--alternate-golden") {
std::string alternateGolden;
if (!ReadValue(argc, argv, i, alternateGolden)) return false;
if (!alternateGolden.empty()) {
request.alternateGoldenPaths.push_back(alternateGolden);
}
} else if (arg == "--diff") {
if (!ReadValue(argc, argv, i, request.diffPath)) return false;
} else if (arg == "--output") {
if (!ReadValue(argc, argv, i, request.outputDir)) return false;
} else if (arg == "--backend") {
if (!ReadValue(argc, argv, i, request.backend)) return false;
} else if (arg == "--mobilegl-library") {
if (!ReadValue(argc, argv, i, request.mobileGlLibrary)) return false;
} else if (arg == "--target-frame") {
if (!ReadInt(argc, argv, i, request.targetFrame)) return false;
} else if (arg == "--target-call") {
if (!ReadLongLong(argc, argv, i, request.targetCall)) return false;
} else if (arg == "--width") {
if (!ReadInt(argc, argv, i, request.width)) return false;
} else if (arg == "--height") {
if (!ReadInt(argc, argv, i, request.height)) return false;
} else if (arg == "--window-surface") {
request.usePbuffer = false;
} else if (arg == "--pbuffer-surface") {
request.usePbuffer = true;
} else if (arg == "--hold-ms") {
if (!ReadInt(argc, argv, i, request.holdMs)) return false;
} else if (arg == "--ssim-threshold") {
if (!ReadDouble(argc, argv, i, request.ssimThreshold)) return false;
} else if (arg == "--crop-x") {
if (!ReadInt(argc, argv, i, request.cropX)) return false;
} else if (arg == "--crop-y") {
if (!ReadInt(argc, argv, i, request.cropY)) return false;
} else if (arg == "--crop-width") {
if (!ReadInt(argc, argv, i, request.cropWidth)) return false;
} else if (arg == "--crop-height") {
if (!ReadInt(argc, argv, i, request.cropHeight)) return false;
} else if (arg == "--coherent-as-flush") {
request.coherentAsFlush = true;
} else if (arg == "--benchmark") {
request.benchmark = true;
} else if (MatchOption(arg, "--benchmark-tail-frames", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkTailFrames = std::atoi(optionValue.c_str());
} else if (MatchOption(arg, "--benchmark-finish", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkFinish = optionValue != "0";
} else if (MatchOption(arg, "--benchmark-result", optionValue, optionHasValue)) {
if (!optionHasValue && !ReadValue(argc, argv, i, optionValue)) return false;
request.benchmarkResultPath = optionValue;
} else if (arg == "--dump-fbo-attachments") {
std::string dumpPoint;
if (!ReadValue(argc, argv, i, dumpPoint)) return false;
if (dumpPoint.find(':') == std::string::npos) {
std::cerr << "--dump-fbo-attachments expects CALL:DIR[:FBO,FBO,...]\n";
return false;
}
request.fboAttachmentDumps.push_back(dumpPoint);
} else if (arg == "--help" || arg == "-h") {
return false;
} else {
std::cerr << "Unknown argument: " << arg << "\n";
return false;
}
}
if (request.tracePath.empty()) {
std::cerr << "--trace is required\n";
return false;
}
if (request.outputDir.empty()) {
std::cerr << "--output is required\n";
return false;
}
if (request.targetCall < 0 && !request.benchmark) {
std::cerr << "--target-call is required\n";
return false;
}
if (request.holdMs < 0) {
std::cerr << "--hold-ms must be non-negative\n";
return false;
}
if (request.benchmarkTailFrames <= 0) {
std::cerr << "--benchmark-tail-frames must be positive\n";
return false;
}
return true;
}
if (request.targetCall < 0 && !request.benchmark) {
std::cerr << "--target-call is required\n";
return false;
}
if (request.holdMs < 0) {
std::cerr << "--hold-ms must be non-negative\n";
return false;
}
if (request.benchmarkTailFrames <= 0) {
std::cerr << "--benchmark-tail-frames must be positive\n";
return false;
}
return true;
}
} // namespace
int main(int argc, char **argv) {
int main(int argc, char** argv) {
mobilegl_trace::Request request;
if (!ParseArgs(argc, argv, request)) {
PrintUsage(argv[0]);