Add mjrfCallback type.

PiperOrigin-RevId: 928640487
Change-Id: Iebf470caafe924a976e4a1b39b0c47727e22195b
This commit is contained in:
Haroon Qureshi
2026-06-08 10:25:57 -07:00
committed by Copybara-Service
parent 74d1459a4a
commit 982e3be9ef
8 changed files with 18 additions and 15 deletions
@@ -63,7 +63,7 @@ static UniquePtr<mjrfTexture> CreateFallbackIndirectLightTexture(
mjrf_defaultTextureData(&payload); mjrf_defaultTextureData(&payload);
payload.bytes = bytes; payload.bytes = bytes;
payload.nbytes = nbytes; payload.nbytes = nbytes;
payload.release_callback = +[](void* user_data) { payload.release = +[](void* user_data) {
mju_closeResource((mjResource*)user_data); mju_closeResource((mjResource*)user_data);
}; };
payload.user_data = resource; payload.user_data = resource;
@@ -414,7 +414,7 @@ static void UpdateMeshData(mjrfMeshData* data, const mjModel* model, int id,
MeshBuilder* builder = new MeshBuilder(num_vertices); MeshBuilder* builder = new MeshBuilder(num_vertices);
data->user_data = builder; data->user_data = builder;
data->release_callback = [](void* user_data) { data->release = [](void* user_data) {
delete static_cast<MeshBuilder*>(user_data); delete static_cast<MeshBuilder*>(user_data);
}; };
@@ -485,7 +485,7 @@ void UpdateSkinFlexMeshData(mjrfMeshData* data, const mjModel* model,
data->index_type = mjINDEX_TYPE_U32; data->index_type = mjINDEX_TYPE_U32;
data->primitive_type = mjMESH_PRIMITIVE_TYPE_TRIANGLES; data->primitive_type = mjMESH_PRIMITIVE_TYPE_TRIANGLES;
data->compute_bounds = true; data->compute_bounds = true;
data->release_callback = nullptr; data->release = nullptr;
data->user_data = nullptr; data->user_data = nullptr;
} }
@@ -572,7 +572,7 @@ void ModelObjects::UploadTexture(const mjModel* model, int id) {
model->tex_width[id] * model->tex_height[id] * model->tex_nchannel[id]; model->tex_width[id] * model->tex_height[id] * model->tex_nchannel[id];
// We assume that the model has the same lifetime as the engine. // We assume that the model has the same lifetime as the engine.
payload.user_data = nullptr; payload.user_data = nullptr;
payload.release_callback = nullptr; payload.release = nullptr;
auto texture = CreateTexture(ctx_, config); auto texture = CreateTexture(ctx_, config);
mjrf_setTextureData(texture.get(), &payload); mjrf_setTextureData(texture.get(), &payload);
@@ -69,7 +69,7 @@ class BuiltinBuilder : public mjrfMeshData {
Args&&... args) { Args&&... args) {
auto builder = new T(std::forward<Args>(args)...); auto builder = new T(std::forward<Args>(args)...);
mjrfMeshData* mesh_data = builder->PrepareMeshData(); mjrfMeshData* mesh_data = builder->PrepareMeshData();
mesh_data->release_callback = +[](void* user_data) { mesh_data->release = +[](void* user_data) {
delete static_cast<BuiltinBuilder*>(user_data); delete static_cast<BuiltinBuilder*>(user_data);
}; };
mesh_data->user_data = builder; mesh_data->user_data = builder;
@@ -165,8 +165,8 @@ mjrfFrameHandle FilamentContext::Render(
if (!read_requests.empty()) { if (!read_requests.empty()) {
engine_->flushAndWait(); engine_->flushAndWait();
if (read_requests[0].read_completed_callback) { if (read_requests[0].read_completed) {
read_requests[0].read_completed_callback(read_requests[0].user_data); read_requests[0].read_completed(read_requests[0].user_data);
} }
} }
+2 -2
View File
@@ -110,9 +110,9 @@ Mesh::Mesh(filament::Engine* engine, const mjrfMeshData& data)
// If the user has provided a release callback, then we need to ensure we // If the user has provided a release callback, then we need to ensure we
// call is when filament is done with the mesh data. // call is when filament is done with the mesh data.
if (data.release_callback) { if (data.release) {
shared_state_->callbacks.push_back([=]() { shared_state_->callbacks.push_back([=]() {
data.release_callback(data.user_data); data.release(data.user_data);
}); });
} }
@@ -162,7 +162,7 @@ Texture::~Texture() {
void Texture::Upload(const mjrfTextureData& data) { void Texture::Upload(const mjrfTextureData& data) {
user_data_ = data.user_data; user_data_ = data.user_data;
release_callback_ = data.release_callback; release_callback_ = data.release;
if (data.bytes == nullptr || data.nbytes == 0) { if (data.bytes == nullptr || data.nbytes == 0) {
ReleaseData(); ReleaseData();
@@ -67,6 +67,9 @@ struct mjrfLight {};
struct mjrfRenderable {}; struct mjrfRenderable {};
struct mjrfRenderTarget {}; struct mjrfRenderTarget {};
// Callback function type for rendering operations.
typedef void (*mjrfCallback)(void* user_data);
// ## Rendering Context (mjrfContext) // ## Rendering Context (mjrfContext)
// //
// The Context is the main entry point for the library. It manages all the // The Context is the main entry point for the library. It manages all the
@@ -194,7 +197,7 @@ struct mjrfReadPixelsRequest {
// Callback when the read pixels operation is complete. This function can // Callback when the read pixels operation is complete. This function can
// optionally be used to free the output buffer if needed. // optionally be used to free the output buffer if needed.
void (*read_completed_callback)(void* user_data); mjrfCallback read_completed;
// User data to pass to the completion callback. // User data to pass to the completion callback.
void* user_data; void* user_data;
@@ -310,7 +313,7 @@ struct mjrfTextureData {
// Because rendering may be multithreaded, we cannot make assumptions about // Because rendering may be multithreaded, we cannot make assumptions about
// when the image data will finish uploading to the GPU. As such, we will use // when the image data will finish uploading to the GPU. As such, we will use
// this callback to notify callers when it is safe to free the image data. // this callback to notify callers when it is safe to free the image data.
void (*release_callback)(void* user_data); mjrfCallback release;
// User data to pass to the release callback. // User data to pass to the release callback.
void* user_data; void* user_data;
@@ -441,7 +444,7 @@ struct mjrfMeshData {
// Because rendering may be multithreaded, we cannot make assumptions about // Because rendering may be multithreaded, we cannot make assumptions about
// when the mesh data will finish uploading to the GPU. As such, we will use // when the mesh data will finish uploading to the GPU. As such, we will use
// this callback to notify callers when it is safe to free the mesh data. // this callback to notify callers when it is safe to free the mesh data.
void (*release_callback)(void* user_data); mjrfCallback release;
// User data to pass to the release callback. // User data to pass to the release callback.
void* user_data; void* user_data;
+2 -2
View File
@@ -101,7 +101,7 @@ uintptr_t ImguiBridge::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
texture_data.bytes = bytes; texture_data.bytes = bytes;
texture_data.nbytes = num_bytes; texture_data.nbytes = num_bytes;
texture_data.user_data = bytes; texture_data.user_data = bytes;
texture_data.release_callback = callback; texture_data.release = callback;
std::memcpy(bytes, pixels, num_bytes); std::memcpy(bytes, pixels, num_bytes);
mjrf_setTextureData(texture, &texture_data); mjrf_setTextureData(texture, &texture_data);
@@ -138,7 +138,7 @@ void ImguiBridge::UpdateTexture(ImTextureData* data) {
texture_data.bytes = data->GetPixels(); texture_data.bytes = data->GetPixels();
texture_data.nbytes = data->Width * data->Height * 4; texture_data.nbytes = data->Width * data->Height * 4;
texture_data.user_data = nullptr; texture_data.user_data = nullptr;
texture_data.release_callback = nullptr; texture_data.release = nullptr;
mjrf_setTextureData(iter->second.get(), &texture_data); mjrf_setTextureData(iter->second.get(), &texture_data);
data->SetStatus(ImTextureStatus_OK); data->SetStatus(ImTextureStatus_OK);
} }