Skip to content

Commit

Permalink
Allow both 'auto' and None as texture_format when choosing VisPy node (
Browse files Browse the repository at this point in the history
…#6652)

# Description

In #6411, we missed these lines:

https://github.com/napari/napari/blob/4f4c063ae5dd79d6d188e201d44b8d57eba71909/napari/_vispy/layers/image.py#L25-L32

These were added in #3990 because 'auto' has a slightly different
meaning in VisPy than *really* fully auto: it chooses the default
texture dtype for the input NumPy array's dtype. For float arrays, this
is float. However, if your OpenGL implementation doesn't support float
textures, VisPy will raise.

Instead, passing `texture_format=None` when creating the ImageNode tells
VisPy to transform the data to whatever texture format it sees fit.

In #6411, when getting the VisPy node for a given dtype, we only check
for `texture_format != 'auto'` as the "catch-all" texture format. But,
in fact, if we are on a machine that doesn't support float32 textures,
by this point in the code the format has been changed to None,
incorrectly triggering these lines:

https://github.com/napari/napari/blob/89f8194d3fa4eef620755804806ac69ef684df63/napari/_vispy/layers/image.py#L59-L73

and causing a ValueError.

This PR fixes that by also checking for None in that same clause.

This PR also adds a test by monkeypatching the function that checks for
float32 texture support.

# References

#3988
#3990

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 20, 2024
1 parent 565ced2 commit d4fc586
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions napari/_vispy/_tests/test_vispy_image_layer.py
Expand Up @@ -70,3 +70,15 @@ def test_3d_slice_of_4d_image_with_order(order):

scene_size = vispy_image_scene_size(vispy_image)
np.testing.assert_array_equal((16, 16, 16), scene_size)


def test_no_float32_texture_support(monkeypatch):
"""Ensure Image node can be created if OpenGL driver lacks float textures.
See #3988, #3990, #6652.
"""
monkeypatch.setattr(
"napari._vispy.layers.image.get_gl_extensions", lambda: ""
)
image = Image(np.zeros((16, 8, 4, 2), dtype="uint8"), scale=(1, 2, 4, 8))
VispyImageLayer(image)
2 changes: 1 addition & 1 deletion napari/_vispy/layers/image.py
Expand Up @@ -52,7 +52,7 @@ def get_node(
# Return Image or Volume node based on 2D or 3D.
res = self._image_node if ndisplay == 2 else self._volume_node
if (
res.texture_format != "auto"
res.texture_format not in {"auto", None}
and dtype is not None
and _VISPY_FORMAT_TO_DTYPE[res.texture_format] != dtype
):
Expand Down

0 comments on commit d4fc586

Please sign in to comment.