// Copyright 2025 DeepMind Technologies Limited // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "experimental/filament/render_context_filament.h" #include #include #include #include #include #include #include "experimental/filament/compat/mjr_filament_renderer.h" #if defined(TLS_FILAMENT_CONTEXT) static thread_local mujoco::MjrFilamentRenderer* g_filament_context = nullptr; #else static mujoco::MjrFilamentRenderer* g_filament_context = nullptr; #endif static void CheckFilamentContext() { if (g_filament_context == nullptr) { mju_error("Missing context; did you call mjrf_makeFilamentContext?"); } } extern "C" { void mjrf_defaultFilamentConfig(mjrFilamentConfig* config) { memset(config, 0, sizeof(mjrFilamentConfig)); } void mjrf_makeFilamentContext(const mjModel* m, mjrContext* con, const mjrFilamentConfig* config) { // TODO: Support multiple contexts and multiple threads. For now, we'll just // assume a single, global context. if (g_filament_context != nullptr) { mju_error("Context already exists!"); } g_filament_context = new mujoco::MjrFilamentRenderer(config); g_filament_context->Init(m); } void mjrf_defaultContext(mjrContext* con) { memset(con, 0, sizeof(mjrContext)); } void mjrf_makeContext(const mjModel* m, mjrContext* con, int fontscale) { mjrf_freeContext(con); mjrFilamentConfig cfg; mjrf_defaultFilamentConfig(&cfg); cfg.width = m->vis.global.offwidth; cfg.height = m->vis.global.offheight; mjrf_makeFilamentContext(m, con, &cfg); } void mjrf_freeContext(mjrContext* con) { // mjr_freeContext may be called multiple times. if (g_filament_context) { delete g_filament_context; g_filament_context = nullptr; } mjrf_defaultContext(con); } void mjrf_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) { CheckFilamentContext(); g_filament_context->Render(viewport, scn); } void mjrf_uploadMesh(const mjModel* m, const mjrContext* con, int meshid) { CheckFilamentContext(); g_filament_context->UploadMesh(m, meshid); } void mjrf_uploadTexture(const mjModel* m, const mjrContext* con, int texid) { CheckFilamentContext(); g_filament_context->UploadTexture(m, texid); } void mjrf_uploadHField(const mjModel* m, const mjrContext* con, int hfieldid) { CheckFilamentContext(); g_filament_context->UploadHeightField(m, hfieldid); } void mjrf_setBuffer(int framebuffer, mjrContext* con) { CheckFilamentContext(); g_filament_context->SetFrameBuffer(framebuffer); } void mjrf_readPixels(unsigned char* rgb, float* depth, mjrRect viewport, const mjrContext* con) { CheckFilamentContext(); g_filament_context->ReadPixels(viewport, rgb, depth); } uintptr_t mjrf_uploadGuiImage(uintptr_t tex_id, const unsigned char* pixels, int width, int height, int bpp, const mjrContext* con) { CheckFilamentContext(); return g_filament_context->UploadGuiImage(tex_id, pixels, width, height, bpp); } double mjrf_getFrameRate(const mjrContext* con) { CheckFilamentContext(); return g_filament_context->GetFrameRate(); } void mjrf_updateGui(const mjrContext* con) { if (g_filament_context != nullptr) { g_filament_context->UpdateGui(); } } } // extern "C"