Skip to content

Commit 28f63f4

Browse files
authored
Make test_cuda_gl_register_image_smoketest() more robust. (NVIDIA#175)
* Add `pyglet.options["headless"] = True` and remove all code expecting failures * qa/ctk-next.drawio.svg line ending fix (overlooked before) * headless on Windows requires EGL: avoid headless and handle cudaErrorOperatingSystem instead * Adopt ChatGPT suggestion of test_cuda_gl_register_image_smoketest(), with simplifications * Simply use `if ctypes.util.find_library("EGL") is None`
1 parent 2c6daba commit 28f63f4

1 file changed

Lines changed: 90 additions & 19 deletions

File tree

‎cuda_bindings/tests/test_graphics_apis.py‎

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,104 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

4+
import contextlib
5+
import ctypes
6+
import ctypes.util
47
import os
8+
import sys
59

610
import pytest
711
from cuda.bindings import runtime as cudart
812

913

10-
def test_graphics_api_smoketest():
14+
@contextlib.contextmanager
15+
def _gl_context():
16+
"""
17+
Yield a (tex_id, tex_target) with a current GL context.
18+
Tries:
19+
1) Windows: hidden WGL window (no EGL)
20+
2) Linux with DISPLAY/wayland: hidden window
21+
3) Linux headless: EGL headless if available
22+
Skips if none work.
23+
"""
1124
pyglet = pytest.importorskip("pyglet")
25+
26+
# Prefer non-headless when a display is available; it's more portable and avoids EGL.
27+
if sys.platform.startswith("linux") and not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
28+
if ctypes.util.find_library("EGL") is None:
29+
pytest.skip("No DISPLAY and no EGL runtime available for headless context.")
30+
pyglet.options["headless"] = True
31+
32+
# Create a minimal offscreen/hidden context
33+
win = None
1234
try:
13-
tex = pyglet.image.Texture.create(512, 512)
14-
except ImportError:
15-
pytest.skip("pyglet lazy ImportError")
16-
except AttributeError:
17-
pytest.skip("pyglet lazy import AttributeError")
18-
except pyglet.display.xlib.NoSuchDisplayException:
19-
pytest.skip("pyglet.display.xlib.NoSuchDisplayException")
20-
21-
err, gfx_resource = cudart.cudaGraphicsGLRegisterImage(
22-
tex.id, tex.target, cudart.cudaGraphicsRegisterFlags.cudaGraphicsRegisterFlagsWriteDiscard
23-
)
24-
error_name = cudart.cudaGetErrorName(err)[1].decode()
25-
if error_name == "cudaSuccess":
26-
assert int(gfx_resource) != 0
27-
elif os.environ.get("DISPLAY") is None:
28-
assert error_name == "cudaErrorOperatingSystem"
29-
else:
30-
assert error_name in ("cudaErrorInvalidValue", "cudaErrorUnknown")
35+
if not pyglet.options.get("headless"):
36+
# Hidden window path (WGL on Windows, GLX/WLS on Linux)
37+
from pyglet import gl
38+
39+
config = gl.Config(double_buffer=False)
40+
win = pyglet.window.Window(visible=False, config=config)
41+
win.switch_to()
42+
else:
43+
# Headless EGL path; pyglet will arrange a pbuffer-like headless context
44+
from pyglet.gl import headless # noqa: F401 (import side-effect creates context)
45+
46+
# Make a tiny texture so we have a real GL object to register
47+
from pyglet.gl import gl as _gl
48+
49+
tex_id = _gl.GLuint(0)
50+
_gl.glGenTextures(1, ctypes.byref(tex_id))
51+
target = _gl.GL_TEXTURE_2D
52+
_gl.glBindTexture(target, tex_id.value)
53+
_gl.glTexParameteri(target, _gl.GL_TEXTURE_MIN_FILTER, _gl.GL_NEAREST)
54+
_gl.glTexParameteri(target, _gl.GL_TEXTURE_MAG_FILTER, _gl.GL_NEAREST)
55+
width, height = 16, 16
56+
_gl.glTexImage2D(target, 0, _gl.GL_RGBA8, width, height, 0, _gl.GL_RGBA, _gl.GL_UNSIGNED_BYTE, None)
57+
58+
yield int(tex_id.value), int(target)
59+
60+
except Exception as e:
61+
# Convert any pyglet/GL creation failure into a clean skip
62+
pytest.skip(f"Could not create GL context/texture: {type(e).__name__}: {e}")
63+
finally:
64+
# Best-effort cleanup
65+
try:
66+
from pyglet.gl import gl as _gl
67+
68+
if tex_id.value:
69+
_gl.glDeleteTextures(1, ctypes.byref(tex_id))
70+
except Exception: # noqa: S110
71+
pass
72+
try:
73+
if win is not None:
74+
win.close()
75+
except Exception: # noqa: S110
76+
pass
77+
78+
79+
@pytest.mark.parametrize(
80+
"flags",
81+
[
82+
cudart.cudaGraphicsRegisterFlags.cudaGraphicsRegisterFlagsNone,
83+
cudart.cudaGraphicsRegisterFlags.cudaGraphicsRegisterFlagsWriteDiscard,
84+
],
85+
)
86+
def test_cuda_gl_register_image_smoketest(flags):
87+
with _gl_context() as (tex_id, tex_target):
88+
# Register
89+
err, resource = cudart.cudaGraphicsGLRegisterImage(tex_id, tex_target, flags)
90+
name = cudart.cudaGetErrorName(err)[1].decode()
91+
92+
# Map error expectations by environment:
93+
# - success: we actually exercised the API
94+
# - operating-system: typical when the driver/runtime refuses interop (e.g., no GPU/driver in CI container)
95+
acceptable = {"cudaSuccess", "cudaErrorOperatingSystem"}
96+
97+
assert name in acceptable, f"cudaGraphicsGLRegisterImage returned {name}"
98+
if name == "cudaSuccess":
99+
assert int(resource) != 0
100+
# Unregister to be tidy
101+
cudart.cudaGraphicsUnregisterResource(resource)
31102

32103

33104
def test_cuda_register_image_invalid():

0 commit comments

Comments
 (0)