Headless Rendering not working by default, and not using GPU
Intro
Hi!
I am a Engineering researcher at Allen Institute for AI, we use MuJoCo for embodied AI projects.
My setup
Latest code on main, commit d0cf2a3ca536ce15767231a0edfc5891db6158ad
Compiling with flags
-DMUJOCO_USE_FILAMENT=ON
-DMUJOCO_USE_FILAMENT_VULKAN=ON
What's happening? What did you expect?
When I try running mujoco with filament on Linux, with Vulkan backend, when no display is present (headless rendering) it does not work.
With the code as is I get an error mjr_maxViewport not implemented. when getting the screen dimensions. After adding this fix to src/experimental/filament/stubs.cc; replace mjrRect mjr_maxViewport(const mjrContext* con) definition with:
mjrRect mjr_maxViewport(const mjrContext* con) {
mjrRect res = {0, 0, con->offWidth, con->offHeight};
return res;
}
Rendering works, but it does not use the GPU and renders with virtual device llvmpipe which is is very slow.
Creating scene and rendering...
- Model: 2116 geoms
- Data created
Using 'soft' CircularBuffer (12288 KiB)
FEngine (64 bits) created at 0x28c5c150 (threading is enabled)
FEngine resolved backend: Vulkan
Vulkan device driver: llvmpipe Mesa 25.0.7-0ubuntu0.24.04.2 (LLVM 20.1.2)
Selected physical device 'llvmpipe (LLVM 20.1.2, 256 bits)' from 1 physical devices. (vendor 0x10005, device 0x0, driver 0x1, api 1.4)
Steps for reproduction
- Build the library with Filament and Vulkan on Linux
- Add the fix to
src/experimental/filament/stubs.cc(without fix there is an error so still a bug) Replace:
mjrRect mjr_maxViewport(const mjrContext* con) {
mju_error("mjr_maxViewport not implemented.");
return mjrRect{};
}
->
mjrRect mjr_maxViewport(const mjrContext* con) {
mjrRect res = {0, 0, con->offWidth, con->offHeight};
return res;
}
- Have the filament assets in the current dirrectory of the running example
filament/assets/data/compiled by material compiler, and defaultibl.ktx:
ibl.ktx phong_2d_uv_fade.filamat phong_cube.filamat
pbr.filamat phong_2d_uv.filamat unlit_depth.filamat
pbr_packed.filamat phong_color_fade.filamat unlit_line.filamat
phong_2d_fade.filamat phong_color.filamat unlit_segmentation.filamat
phong_2d.filamat phong_cube_fade.filamat unlit_ui.filamat
You can generate them by running:
cd src/experimental/filament/assets && for f in *.mat; do echo "Compiling $f..." && /build/bin/matc --api vulkan --api opengl --output c_test/filament/assets/data/${f/.mat/.filamat} $f 2>&1 | grep -E "(error|Error|Unable|Compiling|Success)" || echo "Success"; done
And copy ibl.ktx from /build/bin/assets/ibl.ktx to same directory.
- Compile and run below C++ test (link any needed libraries including filament, ktxreader etc.)
Minimal model for reproduction
Any simple model works to reproduce it:
minimal XML
<mujoco>
<worldbody>
<light pos="0 0 1"/>
<geom type="sphere" size="1" rgba="1 0 0 1"/>
</worldbody>
</mujoco>
Code required for reproduction
#include <mujoco/mujoco.h>
#include <mujoco/mjrender.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include "experimental/filament/render_context_filament.h"
int main(int argc, char** argv) {
if (argc != 2) {
printf("Usage: %s <model.xml>\n", argv[0]);
return 1;
}
char error[1000];
mjModel* m = mj_loadXML(argv[1], 0, error, 1000);
if (!m) {
printf("Failed to load model: %s\n", error);
return 1;
}
mjData* d = mj_makeData(m);
// create context
mjrContext con;
mjvCamera cam;
mjvOption opt;
mjvScene scn;
mjv_defaultCamera(&cam);
mjv_defaultOption(&opt);
// for floorplan1_physics.xml
cam.azimuth = 165;
cam.elevation = -30;
cam.distance = 3.2;
cam.lookat[0] = 0;
cam.lookat[1] = 0;
cam.lookat[2] = 0;
mjv_makeScene(m, &scn, 2000);
mjr_defaultContext(&con);
// Filament to use vulkan
mjrFilamentConfig filament_config;
mjr_defaultFilamentConfig(&filament_config);
filament_config.graphics_api = mjGFX_VULKAN; // or mjGFX_OPENGL
filament_config.enable_gui = false; // Headless mode
// create Filament context
mjr_makeFilamentContext(m, &con, &filament_config);
// set offscreen buffer dimensions
con.offWidth = 1024;
con.offHeight = 768;
// set offscreen buffer
mjr_setBuffer(mjFB_OFFSCREEN, &con);
// update positions
mj_forward(m, d);
// update scene
mjv_updateScene(m, d, &opt, NULL, &cam, mjCAT_ALL, &scn);
printf("Scene has %d geoms, %d lights\n", scn.ngeom, scn.nlight);
printf("Offscreen buffer: %dx%d\n", con.offWidth, con.offHeight);
// print geom info
for (int i = 0; i < scn.ngeom; i++) {
const mjvGeom* g = scn.geoms + i;
printf("Geom %d: type=%d, size=[%.3f %.3f %.3f], rgba=[%.2f %.2f %.2f %.2f]\n",
i, g->type, g->size[0], g->size[1], g->size[2],
g->rgba[0], g->rgba[1], g->rgba[2], g->rgba[3]);
}
// setup viewport
mjrRect viewport = {0, 0, 1024, 768};
// render
printf("Rendering frame...\n");
mjr_render(viewport, &scn, &con);
// read pixels, render actually happens here
unsigned char* rgb = (unsigned char*)malloc(1024 * 768 * 3);
mjr_readPixels(rgb, NULL, viewport, &con);
printf("Read %d bytes from renderer\n", 1024 * 768 * 3);
// save as PGM
FILE* fp = fopen("/tmp/frame.pgm", "wb");
fprintf(fp, "P6\n1024 768\n255\n");
fwrite(rgb, 1, 1024 * 768 * 3, fp);
fclose(fp);
printf("Saved to /tmp/frame.pgm\n");
free(rgb);
mjv_freeScene(&scn);
mjr_freeContext(&con);
mj_deleteData(d);
mj_deleteModel(m);
return 0;
}
compile script:
#!/bin/bash
# Compile filament_headless.c with MuJoCo and Filament support
BUILD_DIR="./build"
SRC_FILE="c_test/filament_headless.c" # Adjust as needed
OUTPUT="c_test/filament_headless"
# Include directories
INCLUDES=(
"-I./include" # MuJoCo headers
"-I./src" # MuJoCo source headers (for experimental)
"-I${BUILD_DIR}/_deps/filament-src/filament/include" # Filament headers
"-I${BUILD_DIR}/_deps/filament-src/libs/filabridge/include" # Filabridge headers
)
# Library directories
LIBDIRS=(
"-L${BUILD_DIR}/lib"
)
# Libraries to link (in order)
LIBS=(
"-lmujoco_filament" # MuJoCo Filament integration
"-lmujoco" # MuJoCo core library
"-lfilament" # Filament rendering library
"-lbackend" # Filament backend
"-lbluevk" # Vulkan backend
"-lbluegl" # OpenGL backend
"-lfilaflat" # Filament flatbuffers
"-lfilabridge" # Filament bridge
"-lktxreader" # KTX texture reader
"-limage" # Image library
"-lmath" # Math library
"-lutils" # Utils library
"-lsmol-v" # Smol-V compression
"-lzstd" # Zstd compression
"-ldl" # Dynamic linking
"-lpthread" # Threading
"-lm" # Math library
"-lrt" # Real-time library
)
# Get absolute path for rpath
BUILD_DIR_ABS=$(cd "${BUILD_DIR}" && pwd)
# Compile (use g++ since we need C++ standard library for Filament)
g++ -g -std=c++17 \
"${INCLUDES[@]}" \
"${LIBDIRS[@]}" \
-o "${OUTPUT}" \
"${SRC_FILE}" \
"${LIBS[@]}" \
-lstdc++ \
-Wl,-rpath,"${BUILD_DIR_ABS}/lib"
if [ $? -eq 0 ]; then
echo "Compilation successful! Output: ${OUTPUT}"
else
echo "Compilation failed!"
exit 1
fi
Confirmations
- [x] I searched the latest documentation thoroughly before posting.
- [x] I searched previous Issues and Discussions, I am certain this has not been raised before.
CC @haroonq
Could you please try running with VK_LOADER_DEBUG=all?
If you can't get your vulkan GPU drivers to work, you might have better luck using opengl. Just use the default setting: MUJOCO_USE_FILAMENT_VULKAN=OFF.
The visual output should be identical between filament OpenGL and Vulkan.
Thanks for the hint, after investigating the debug output, found a problem with the installed drivers, reinstalled recommended version 535.274.02 and now GPU is selected, output:
>c_test/filament_headless ~/mujoco_latest_2/test_scenes/FloorPlan1_physics.xml
Using 'soft' CircularBuffer (12288 KiB)
FEngine (64 bits) created at 0x593c132e7df0 (threading is enabled)
FEngine resolved backend: Vulkan
Vulkan device driver: NVIDIA 535.274.02
Selected physical device 'NVIDIA TITAN Xp' from 3 physical devices. (vendor 0x10de, device 0x1b02, driver 0x85c48080, api 1.3)
Backend feature level: 3
FEngine feature level: 1
Requested linear format but KTX contains a sRGB format.
Scene has 234 geoms, 2 lights
Offscreen buffer: 1024x768
Thanks!