Skip to content

Commit a467cf0

Browse files
committed
examples/wayland: Update Wayland example.
* Port to xdg_shell protocol in favor of rarely available wl_shell. * Update pywayland import names, they now have prefixes. * Resolve variable name collision between wl_surface and VkSurface. * Use VK_LAYER_KHRONOS_validation * Resolve synchronization validation by calling vkQueueWaitIdle. * Block during display.dispatch * Pass only first element of pipeline array to vkCmdBindPipeline.
1 parent 3dff490 commit a467cf0

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

example/contribs/example_wayland.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,49 @@
88
import time
99
from vulkan import *
1010
from pywayland.client import Display
11-
from pywayland.protocol.wayland import Compositor, Shell, Shm
11+
from pywayland.protocol.wayland import WlCompositor, WlShm
12+
from pywayland.protocol.xdg_shell.xdg_wm_base import XdgWmBase, XdgWmBaseProxy
13+
from pywayland.protocol.xdg_shell.xdg_toplevel import XdgToplevelProxy
14+
from pywayland.protocol.xdg_shell.xdg_surface import XdgSurfaceProxy
1215

1316

1417
WIDTH = 400
1518
HEIGHT = 400
1619

1720

18-
def shm_format_handler(shm, format_):
19-
pass
21+
def shm_format_handler(shm, format_int):
22+
format_enum = WlShm.format(format_int)
23+
print(f"Available format: {format_enum.name}")
24+
25+
def wm_base_ping_handler(xdg_wm_base: XdgWmBaseProxy, serial: int):
26+
xdg_wm_base.pong(serial)
2027

2128
def registry_global_handler(registry, id_, interface, version):
2229
window = registry.user_data
2330
if interface == 'wl_compositor':
24-
window['compositor'] = registry.bind(id_, Compositor, version)
25-
elif interface == 'wl_shell':
26-
window['shell'] = registry.bind(id_, Shell, version)
31+
window['compositor'] = registry.bind(id_, WlCompositor, version)
2732
elif interface == 'wl_shm':
28-
window['shm'] = registry.bind(id_, Shm, version)
33+
window['shm'] = registry.bind(id_, WlShm, version)
2934
window['shm'].dispatcher['format'] = shm_format_handler
35+
elif interface == "xdg_wm_base":
36+
window["xdg_wm_base"] = registry.bind(id_, XdgWmBase, version)
37+
window["xdg_wm_base"].dispatcher["ping"] = wm_base_ping_handler
3038

3139
def registry_global_remover(registry, id_):
3240
print("got a registry losing event for {}".format(id))
3341

34-
def shell_surface_ping_handler(shell_surface, serial):
35-
shell_surface.pong(serial)
42+
def xdg_surface_configure_handler(xdg_surface: XdgSurfaceProxy, serial: int):
43+
xdg_surface.ack_configure(serial)
44+
45+
def xdg_toplevel_configure_handler(toplevel: XdgToplevelProxy, width: int, height: int, states: bytes):
46+
print(f"Configuring {width}x{height}")
47+
48+
def xdg_toplevel_close_handler():
49+
print("Window closed")
3650

3751
window = {
3852
'compositor': None,
39-
'shell': None,
53+
'xdg_wm_base': None,
4054
'shm': None
4155
}
4256

@@ -48,21 +62,28 @@ def shell_surface_ping_handler(shell_surface, serial):
4862
registry.dispatcher['global_remove'] = registry_global_remover
4963
registry.user_data = window
5064

51-
display.dispatch()
65+
display.dispatch(block=True)
5266
display.roundtrip()
5367

5468
assert window['compositor']
55-
assert window['shell']
69+
assert window['xdg_wm_base']
5670
assert window['shm']
5771

58-
surface = window['compositor'].create_surface()
59-
shell_surface = window['shell'].get_shell_surface(surface)
60-
shell_surface.set_toplevel()
61-
shell_surface.dispatcher['ping'] = shell_surface_ping_handler
72+
wl_surface = window['compositor'].create_surface()
73+
74+
xdg_surface = window["xdg_wm_base"].get_xdg_surface(wl_surface)
75+
xdg_surface.dispatcher["configure"] = xdg_surface_configure_handler
76+
77+
toplevel = xdg_surface.get_toplevel()
78+
toplevel.dispatcher["configure"] = xdg_toplevel_configure_handler
79+
toplevel.dispatcher["close"] = xdg_toplevel_close_handler
80+
81+
toplevel.set_app_id("python_vulkan_wayland_example")
82+
toplevel.set_title("Python Vulkan Wayland Example")
6283

6384
region = window['compositor'].create_region()
6485
region.add(0, 0, WIDTH, HEIGHT)
65-
surface.set_opaque_region(region)
86+
wl_surface.set_opaque_region(region)
6687

6788

6889
# ----------
@@ -83,7 +104,7 @@ def shell_surface_ping_handler(shell_surface, serial):
83104
layers = [l.layerName for l in layers]
84105
print("availables layers: %s\n" % layers)
85106

86-
layers = ['VK_LAYER_LUNARG_standard_validation']
107+
layers = ['VK_LAYER_KHRONOS_validation']
87108
extensions = ['VK_KHR_surface', 'VK_EXT_debug_report', 'VK_KHR_wayland_surface']
88109

89110
createInfo = VkInstanceCreateInfo(
@@ -127,7 +148,7 @@ def surface_wayland():
127148
surface_create = VkWaylandSurfaceCreateInfoKHR(
128149
sType=VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
129150
display=ffi.cast('void*', display._ptr),
130-
surface=ffi.cast('void*', surface._ptr),
151+
surface=ffi.cast('void*', wl_surface._ptr),
131152
flags=0)
132153
return vkCreateWaylandSurfaceKHR(instance, surface_create, None)
133154

@@ -596,7 +617,7 @@ def get_swap_extent(capabilities):
596617
vkCmdBeginRenderPass(command_buffer, render_pass_begin_create, VK_SUBPASS_CONTENTS_INLINE)
597618

598619
# Bing pipeline
599-
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline)
620+
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline[0])
600621

601622
# Draw
602623
vkCmdDraw(command_buffer, 3, 1, 0, 0)
@@ -658,6 +679,7 @@ def draw_frame():
658679
present_create.pImageIndices[0] = image_index
659680
vkQueuePresentKHR(presentation_queue, present_create)
660681

682+
vkQueueWaitIdle(presentation_queue)
661683

662684
# Main loop
663685
running = True

0 commit comments

Comments
 (0)