Skip to content

Commit 9fe7777

Browse files
authored
Add util to get glfw present_info (#767)
* Add util to get glfw present_info * fix * update examples * typo * laxy imoirt
1 parent 148eebb commit 9fe7777

File tree

4 files changed

+60
-42
lines changed

4 files changed

+60
-42
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# Load wgpu so autodoc can query docstrings
2626
import wgpu # noqa: E402
2727
import wgpu.utils.compute # noqa: E402
28+
import wgpu.utils.glfw_present_info # noqa: E402
2829

2930

3031
# -- Tests -------------------------------------------------------------------

docs/utils.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ Compute with buffers
6666
6767
.. autofunction:: wgpu.utils.compute.compute_with_buffers
6868

69+
70+
71+
Helper for using glfw directly (not via rendercanvas)
72+
-----------------------------------------------------
73+
74+
.. code-block:: py
75+
76+
from wgpu.utils.glfw_present_info import get_glfw_present_info
77+
78+
.. autofunction:: wgpu.utils.glfw_present_info.get_glfw_present_info

examples/gui_direct.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,16 @@
77

88
# run_example = false
99

10-
import os
11-
import sys
1210
import time
1311
import atexit
1412

1513
import glfw
1614
import wgpu
15+
from wgpu.utils.glfw_present_info import get_glfw_present_info
1716

1817
# from triangle import setup_drawing_sync
1918
from cube import setup_drawing_sync
2019

21-
22-
system_is_wayland = "wayland" in os.getenv("XDG_SESSION_TYPE", "").lower()
23-
api_is_wayland = False
24-
if sys.platform.startswith("linux") and system_is_wayland:
25-
if not hasattr(glfw, "get_x11_window"):
26-
api_is_wayland = True
27-
28-
29-
def get_glfw_present_info(window):
30-
if sys.platform.startswith("win"):
31-
return {
32-
"platform": "windows",
33-
"window": int(glfw.get_win32_window(window)),
34-
"vsync": True,
35-
}
36-
elif sys.platform.startswith("darwin"):
37-
return {
38-
"platform": "cocoa",
39-
"window": int(glfw.get_cocoa_window(window)),
40-
"vsync": True,
41-
}
42-
elif sys.platform.startswith("linux"):
43-
if api_is_wayland:
44-
return {
45-
"platform": "wayland",
46-
"window": int(glfw.get_wayland_window(window)),
47-
"display": int(glfw.get_wayland_display()),
48-
"vsync": True,
49-
}
50-
else:
51-
return {
52-
"platform": "x11",
53-
"window": int(glfw.get_x11_window(window)),
54-
"display": int(glfw.get_x11_display()),
55-
"vsync": True,
56-
}
57-
else:
58-
raise RuntimeError(f"Cannot get GLFW surface info on {sys.platform}.")
59-
60-
6120
# Setup glfw
6221
glfw.init()
6322
atexit.register(glfw.terminate)

wgpu/utils/glfw_present_info.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import sys
3+
4+
5+
def get_glfw_present_info(window, vsync=True) -> dict:
6+
"""Get the ``present_info`` dict required to instantiate a ``GPUCanvasContext``.
7+
8+
Given a glfw window handle, return a dict that can be passed to ``wgpu.gpu.get_canvas_context()`` to create a ``GPUCanvasContext``.
9+
"""
10+
11+
# Lazy import, so the docs can be build without needing glfw
12+
import glfw
13+
14+
system_is_wayland = "wayland" in os.getenv("XDG_SESSION_TYPE", "").lower()
15+
api_is_wayland = False
16+
if sys.platform.startswith("linux") and system_is_wayland:
17+
if not hasattr(glfw, "get_x11_window"):
18+
api_is_wayland = True
19+
20+
if sys.platform.startswith("win"):
21+
return {
22+
"platform": "windows",
23+
"window": int(glfw.get_win32_window(window)),
24+
"vsync": vsync,
25+
}
26+
elif sys.platform.startswith("darwin"):
27+
return {
28+
"platform": "cocoa",
29+
"window": int(glfw.get_cocoa_window(window)),
30+
"vsync": vsync,
31+
}
32+
elif sys.platform.startswith("linux"):
33+
if api_is_wayland:
34+
return {
35+
"platform": "wayland",
36+
"window": int(glfw.get_wayland_window(window)),
37+
"display": int(glfw.get_wayland_display()),
38+
"vsync": vsync,
39+
}
40+
else:
41+
return {
42+
"platform": "x11",
43+
"window": int(glfw.get_x11_window(window)),
44+
"display": int(glfw.get_x11_display()),
45+
"vsync": vsync,
46+
}
47+
else:
48+
raise RuntimeError(f"Cannot get GLFW surface info on {sys.platform}.")

0 commit comments

Comments
 (0)