Skip to content

Commit

Permalink
add pyry's fixed examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Jan 17, 2024
1 parent b607468 commit f897502
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions examples/adapter_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def main():
for f in features:
print(f.name)


t0 = time.time()
main()
dt = time.time() - t0
Expand Down
25 changes: 16 additions & 9 deletions examples/glfw_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def is_wayland():

def get_linux_window(window):
if is_wayland():
return int(glfw.get_wayland_window(window))
return glfw.get_wayland_window(window)
else:
return int(glfw.get_x11_window(window))
return glfw.get_x11_window(window)


def get_linux_display():
Expand All @@ -50,7 +50,7 @@ def get_linux_display():


def get_handles(window):
for (prefix, maker) in WINDOW_GETTERS:
for prefix, maker in WINDOW_GETTERS:
if sys.platform.lower().startswith(prefix):
win_getter, display_getter = maker()
return (win_getter(window), display_getter())
Expand All @@ -61,7 +61,9 @@ class GLFWWindow:
def __init__(self, w: int, h: int, title="xgpu"):
self.width = w
self.height = h
glfw.init()
if is_wayland():
glfw.init_hint(glfw.PLATFORM, glfw.PLATFORM_WAYLAND)
print("GLFW init:", glfw.init())
glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
glfw.window_hint(glfw.RESIZABLE, True)
# see https://github.com/FlorianRhiem/pyGLFW/issues/42
Expand All @@ -70,27 +72,29 @@ def __init__(self, w: int, h: int, title="xgpu"):
glfw.window_hint(glfw.FOCUSED, False) # prevent Wayland focus error
self.window = glfw.create_window(w, h, title, None, None)
(self.window_handle, self.display_id) = get_handles(self.window)
print("window:", self.window_handle)
print("display:", self.display_id)
self._surface = None
self._surf_config = None

def poll(self) -> bool:
glfw.poll_events()
return bool(not glfw.window_should_close(self.window))

def configure_surface(self, device: Device):
def configure_surface(self, device: Device, format=xgpu.TextureFormat.BGRA8Unorm):
print("Configuring surface?")
if self._surface is None:
return
if self._surf_config is None:
self._surf_config = xgpu.surfaceConfiguration(
device=device,
usage=xgpu.TextureUsageFlags([xgpu.TextureUsage.RenderAttachment]),
viewFormats=[xgpu.TextureFormat.RGBA8Unorm],
format=xgpu.TextureFormat.RGBA8Unorm,
viewFormats=[format],
format=format,
alphaMode=xgpu.CompositeAlphaMode.Auto,
width=self.width,
height=self.height,
presentMode=xgpu.PresentMode.Fifo
presentMode=xgpu.PresentMode.Fifo,
)
self._surf_config.width = self.width
self._surf_config.height = self.height
Expand All @@ -102,6 +106,7 @@ def get_surface(self, instance: Instance) -> Surface:
if self._surface is not None:
return self._surface
desc = self.get_surface_descriptor()
print("Got surface descriptor?")
self._surface = instance.createSurfaceFromDesc(desc)
print("Got surface?")
return self._surface
Expand All @@ -113,13 +118,15 @@ def get_surface_descriptor(self) -> SurfaceDescriptor:
hwnd=cast_any_to_void(self.window_handle),
)
elif sys.platform.startswith("linux"): # no-cover
if is_wayland:
if is_wayland():
print("WAYLAND???")
# todo: wayland seems to be broken right now
inner = xgpu.surfaceDescriptorFromWaylandSurface(
display=cast_any_to_void(self.display_id),
surface=cast_any_to_void(self.window_handle),
)
else:
print("XLIB????")
inner = xgpu.surfaceDescriptorFromXlibWindow(
display=cast_any_to_void(self.display_id), window=self.window_handle
)
Expand Down
9 changes: 3 additions & 6 deletions examples/triangle_offscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
}
"""


def main(power_preference=xg.PowerPreference.HighPerformance):
t0 = time.time()
(adapter, _) = get_adapter(instance=None, power=power_preference)
Expand All @@ -67,9 +68,7 @@ def _main(device: xg.Device):
HEIGHT = 1024

shader = device.createShaderModule(
nextInChain=xg.ChainedStruct(
[xg.shaderModuleWGSLDescriptor(code=shader_source)]
),
nextInChain=xg.ChainedStruct([xg.shaderModuleWGSLDescriptor(code=shader_source)]),
hints=[],
)

Expand All @@ -88,9 +87,7 @@ def _main(device: xg.Device):
topology=xg.PrimitiveTopology.TriangleList,
stripIndexFormat=xg.IndexFormat.Undefined,
)
vertex = xg.vertexState(
module=shader, entryPoint="vs_main", constants=[], buffers=[]
)
vertex = xg.vertexState(module=shader, entryPoint="vs_main", constants=[], buffers=[])
color_target = xg.colorTargetState(
format=xg.TextureFormat.RGBA8Unorm,
writeMask=xg.ColorWriteMaskFlags([xg.ColorWriteMask.All]),
Expand Down
21 changes: 11 additions & 10 deletions examples/triangle_windowed.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
}
"""


def main():
WIDTH = 1024
HEIGHT = 1024
Expand All @@ -57,12 +58,14 @@ def main():
(adapter, _) = get_adapter(instance, xg.PowerPreference.HighPerformance, surface)
device = get_device(adapter)

window.configure_surface(device)
window_tex_format = xg.TextureFormat.BGRA8Unorm
# surface.getPreferredFormat(adapter)
print("Window tex format:", window_tex_format.name)

window.configure_surface(device, window_tex_format)

shader = device.createShaderModule(
nextInChain=xg.ChainedStruct(
[xg.shaderModuleWGSLDescriptor(code=shader_source)]
),
nextInChain=xg.ChainedStruct([xg.shaderModuleWGSLDescriptor(code=shader_source)]),
hints=[],
)

Expand All @@ -80,11 +83,9 @@ def main():
frontFace=xg.FrontFace.CCW,
cullMode=xg.CullMode._None,
)
vertex = xg.vertexState(
module=shader, entryPoint="vs_main", constants=[], buffers=[]
)
vertex = xg.vertexState(module=shader, entryPoint="vs_main", constants=[], buffers=[])
color_target = xg.colorTargetState(
format=xg.TextureFormat.RGBA8Unorm,
format=window_tex_format,
blend=xg.blendState(color=REPLACE, alpha=REPLACE),
writeMask=xg.ColorWriteMask.All,
)
Expand All @@ -111,10 +112,10 @@ def main():
print("Tex status?", surf_tex.status.name)

color_view = surf_tex.texture.createView(
format = xg.TextureFormat.RGBA8Unorm,
format=xg.TextureFormat.Undefined,
dimension=xg.TextureViewDimension._2D,
mipLevelCount=1,
arrayLayerCount=1
arrayLayerCount=1,
)

color_attachment = xg.renderPassColorAttachment(
Expand Down

0 comments on commit f897502

Please sign in to comment.