Repository navigation
feat(sim): filament renderer integration and mujoco 4.0 bump - #344
Merged
Merged
Conversation
- sim config flag that switches between renderers - pystudio viewer when filament renderer is active - fr3 cart example uses filament renderer
Filament moved out of mujoco.experimental in 4.0 and some mjrf bindings were renamed (ModelObjects -> ModelResources, ReadPixelsRequest -> ReadRequest, force_software_rendering -> software_rendering). Studio's ViewerApp became mujoco.studio.app.App. Importing studio.window to register Filament assets is no longer needed. The Filament tests now only skip on mujoco < 4 and fail if the import breaks, so a moved module can no longer silently skip them.
MuJoCo 4.0 renders Filament's depth draw mode without post processing, so reading it into an R32F target gives reversed-z values that convert to metric depth like the classic depth buffer. This replaces the extra classic OpenGL depth pass of FilamentSimCameraSet and removes the now unused _ClassicDepthRenderer.
Filament's OpenGL backend uses GLX on Linux: without an X display it prints "Failed to open X display. (exiting)" and then hangs. Default to Vulkan on Linux without DISPLAY and raise instead of hanging when OpenGL is requested explicitly there. Destroying a Filament Vulkan context on NVIDIA drivers calls eglTerminate() on the default EGL display, which invalidates the classic renderer's EGL context (eglMakeCurrent fails with EGL_NOT_INITIALIZED). Filament contexts are therefore pooled per thread and reused instead of destroyed. Pooled contexts are kept alive at exit, as destroying them on the main thread aborts for contexts created by other threads.
All cameras share one mjvScene, and mjv_updateScene positions infinite planes such as the floor relative to scene.camera before it updates the camera, i.e. relative to the previously rendered camera. This shifted the floor (and its shadows) by grid cells depending on which camera was rendered before, so images differed between calls on the same state and between single- and multi-camera setups. Update the camera before the scene.
GPUs generally do not support 3 channel 8 bit render targets (Vulkan's R8G8B8_UNORM has no image features on NVIDIA), so Filament emulates RGB8 with a slow readback. Render into RGBA8 and drop alpha with cv2.cvtColor, which is ~100x faster than a numpy slice copy. Images are identical, 3 cameras at 1280x720 go from ~8.5 to ~5.3 ms on Vulkan.
Sim.register_state_capture records the simulation state (mj_getState) at a fixed rate of simulation time into a ring buffer, e.g. at the exposure times of a camera, so that frames can later be rendered from the state a real camera would have recorded. Captures follow a fixed schedule (no drift), happen at the physics step closest to each capture time after integration, and restart on reset or when time goes back.
SimCameraSet now renders in Python with either MuJoCo's classic OpenGL renderer or Filament, from a separate render-only MjData whose kinematics are recomputed from the state, so images, time stamps and extrinsics always belong together. With render_on_demand=True frames show the state at request time; with render_on_demand=False they show the state captured at each camera's frame rate (the last exposure of a real camera) and are rendered from it on request. This removes the C++ camera implementation (SimCameraSet, FrameSet, Renderer and the rendering callbacks that ran in every physics step), the C++ EGL context handoff and the EGL headers dependency. Rendering from Python is faster: frames are read into numpy arrays directly instead of being copied through Eigen and pybind11.
Callbacks were called when more than seconds_between_calls had passed since their last call, so each period was rounded up to the next physics step and the calls drifted (e.g. 98 instead of 100 calls in 10 s at a 0.1 s interval and a 2 ms timestep). They also ran between mj_step1 and mj_step2, i.e. on a state that was not yet integrated. All callbacks now share the scheduling of the state captures: the k-th call is due at start + k * period and happens at the closest physics step, after integration. The schedule restarts on reset and whenever the simulation time goes backwards, which previously stopped callbacks until the time caught up again. The state capture is now a regular callback with a restart hook instead of a separate mechanism, and since nothing runs between the two step halves anymore, step uses mj_step.
The number of buffered framesets was max(len + 1, max_buffer_frames) instead of min(...), so it was at least the buffer size after the first frameset. Until the buffer was full once, get_timestamp_frames then searched empty slots and failed an assertion.
Sim cameras could be free, tracking, fixed or default_free cameras, but only fixed cameras were usable: SimCameraConfig had no parameters for the pose of free or the target of tracking cameras, and the intrinsics and extrinsics of default_free cameras silently came from the last camera of the model. All env configs use fixed cameras, and moving cameras, e.g. for videos, are cameras attached to a body or with a tracking mode in MJCF. SimCameraSet now always renders the camera of the model named by the identifier and raises if it does not exist. CameraType, the type field and src/sim/camera.h are removed, SimCameraConfig is kept as an alias of BaseCameraConfig. BREAKING CHANGE: rcs._core.sim.CameraType and the type argument and field of SimCameraConfig are removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.