ce025f0ec3
KTX textures crash the classic OpenGL renderer and the Python viewer with "Number of channels not supported: 1": mjCTexture::LoadKTX stores the file as an opaque single-channel blob for the Filament renderer, and mjr_uploadTexture aborts via mju_error because the classic path only handles 3- or 4-channel textures. Skip textures the classic renderer can't upload (nchannel not 3 or 4) with a warning instead of aborting, so the model still loads and the other textures render. LoadKTX and the Filament path are unchanged. The guard also covers the cube/skybox path, which previously over-read a single-channel blob as RGB. Add a render_test.py regression test that builds a model with a KTX texture and constructs an MjrContext; it aborts before this change and passes after. Addresses #3343.
146 lines
5.1 KiB
Python
146 lines
5.1 KiB
Python
# Copyright 2022 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.
|
|
# ==============================================================================
|
|
"""Tests for MuJoCo Python rendering."""
|
|
|
|
import os
|
|
|
|
from absl.testing import absltest
|
|
import mujoco
|
|
import numpy as np
|
|
|
|
|
|
@absltest.skipUnless(
|
|
hasattr(mujoco, 'GLContext'), 'MuJoCo rendering is disabled'
|
|
)
|
|
class MuJoCoRenderTest(absltest.TestCase):
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.gl = mujoco.GLContext(640, 480)
|
|
self.gl.make_current()
|
|
|
|
def tearDown(self):
|
|
super().tearDown()
|
|
del self.gl
|
|
|
|
def test_can_render(self):
|
|
"""Test that the bindings can successfully render a simple image.
|
|
|
|
This test sets up a basic MuJoCo rendering context similar to the example in
|
|
https://mujoco.readthedocs.io/en/latest/programming#visualization
|
|
It calls `mjr_rectangle` rather than `mjr_render` so that we can assert an
|
|
exact rendered image without needing golden data. The purpose of this test
|
|
is to ensure that the bindings can correctly return pixels in Python, rather
|
|
than to test MuJoCo's rendering pipeline itself.
|
|
"""
|
|
|
|
self.model = mujoco.MjModel.from_xml_string('<mujoco><worldbody/></mujoco>')
|
|
self.data = mujoco.MjData(self.model)
|
|
|
|
scene = mujoco.MjvScene(self.model, maxgeom=0)
|
|
mujoco.mjv_updateScene(
|
|
self.model,
|
|
self.data,
|
|
mujoco.MjvOption(),
|
|
mujoco.MjvPerturb(),
|
|
mujoco.MjvCamera(),
|
|
mujoco.mjtCatBit.mjCAT_ALL,
|
|
scene,
|
|
)
|
|
|
|
context = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
|
|
mujoco.mjr_setBuffer(mujoco.mjtFramebuffer.mjFB_OFFSCREEN, context)
|
|
|
|
# MuJoCo's default render buffer size is 640x480.
|
|
full_rect = mujoco.MjrRect(0, 0, 640, 480)
|
|
mujoco.mjr_rectangle(full_rect, 0, 0, 0, 1)
|
|
|
|
blue_rect = mujoco.MjrRect(56, 67, 234, 123)
|
|
mujoco.mjr_rectangle(blue_rect, 0, 0, 1, 1)
|
|
|
|
expected_upside_down_image = np.zeros((480, 640, 3), dtype=np.uint8)
|
|
expected_upside_down_image[67 : 67 + 123, 56 : 56 + 234, 2] = 255
|
|
|
|
upside_down_image = np.empty((480, 640, 3), dtype=np.uint8)
|
|
mujoco.mjr_readPixels(upside_down_image, None, full_rect, context)
|
|
np.testing.assert_array_equal(upside_down_image, expected_upside_down_image)
|
|
|
|
# Check that mjr_readPixels can accept a flattened array.
|
|
upside_down_image[:] = 0
|
|
mujoco.mjr_readPixels(
|
|
np.reshape(upside_down_image, -1), None, full_rect, context
|
|
)
|
|
np.testing.assert_array_equal(upside_down_image, expected_upside_down_image)
|
|
context.free()
|
|
|
|
def test_safe_to_free_context_twice(self):
|
|
self.model = mujoco.MjModel.from_xml_string('<mujoco><worldbody/></mujoco>')
|
|
self.data = mujoco.MjData(self.model)
|
|
|
|
scene = mujoco.MjvScene(self.model, maxgeom=0)
|
|
mujoco.mjv_updateScene(
|
|
self.model,
|
|
self.data,
|
|
mujoco.MjvOption(),
|
|
None,
|
|
mujoco.MjvCamera(),
|
|
mujoco.mjtCatBit.mjCAT_ALL,
|
|
scene,
|
|
)
|
|
|
|
context = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
|
|
mujoco.mjr_setBuffer(mujoco.mjtFramebuffer.mjFB_OFFSCREEN, context)
|
|
|
|
context.free()
|
|
context.free()
|
|
|
|
def test_ktx_texture_does_not_crash_classic_renderer(self):
|
|
# KTX textures are stored as opaque single-channel blobs for the Filament
|
|
# renderer; mjCTexture::LoadKTX does not decode them. The classic renderer
|
|
# must skip such textures with a warning rather than aborting in
|
|
# mjr_uploadTexture (https://github.com/google-deepmind/mujoco/issues/3343).
|
|
ktx_path = os.path.join(self.create_tempdir().full_path, 'texture.ktx')
|
|
with open(ktx_path, 'wb') as f:
|
|
# LoadKTX stores the raw payload as a 1-channel blob without decoding, so
|
|
# the contents do not matter; start with the KTX 2.0 identifier for realism.
|
|
f.write(b'\xabKTX 20\xbb\r\n\x1a\n' + bytes(64))
|
|
|
|
model = mujoco.MjModel.from_xml_string(f"""
|
|
<mujoco>
|
|
<asset>
|
|
<texture name="ktx" type="2d" file="{ktx_path}"/>
|
|
<material name="mat" texture="ktx"/>
|
|
</asset>
|
|
<worldbody>
|
|
<geom type="plane" size="1 1 .1" material="mat"/>
|
|
</worldbody>
|
|
</mujoco>""")
|
|
self.assertEqual(model.tex_nchannel[0], 1)
|
|
|
|
# Building the render context uploads all textures; before the fix this
|
|
# aborted with "Number of channels not supported: 1". It must not raise now.
|
|
context = mujoco.MjrContext(model, mujoco.mjtFontScale.mjFONTSCALE_150)
|
|
context.free()
|
|
|
|
def test_mjrrect_repr(self):
|
|
rect = mujoco.MjrRect(1, 2, 3, 4)
|
|
rect_repr = repr(rect)
|
|
self.assertIn('MjrRect', rect_repr)
|
|
self.assertIn('left: 1', rect_repr)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
absltest.main()
|