wgpu: add sdl3 glue

This commit is contained in:
Slashscreen
2025-02-12 14:27:16 -08:00
committed by GitHub
parent 435f77b16b
commit 02b19b115b
4 changed files with 97 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
#+build !linux
#+build !windows
#+build !darwin
package wgpu_sdl3_glue
#panic("package wgpu/sdl3glue is not supported on the current target")
+23
View File
@@ -0,0 +1,23 @@
package wgpu_sdl3_glue
import "vendor:sdl3"
import "vendor:wgpu"
import CA "vendor:darwin/QuartzCore"
import NS "core:sys/darwin/Foundation"
GetSurface :: proc(instance: wgpu.Instance, window: ^sdl3.Window) -> wgpu.Surface {
ns_window := cast(^NS.Window)sdl3.GetPointerProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_COCOA_WINDOW_POINTER, nil)
metal_layer := CA.MetalLayer_layer()
ns_window->contentView()->setLayer(metal_layer)
return wgpu.InstanceCreateSurface(
instance,
&wgpu.SurfaceDescriptor{
nextInChain = &wgpu.SurfaceDescriptorFromMetalLayer{
chain = wgpu.ChainedStruct{
sType = .SurfaceDescriptorFromMetalLayer,
},
layer = rawptr(metal_layer),
},
},
)
}
+45
View File
@@ -0,0 +1,45 @@
package wgpu_sdl3_glue
import "vendor:sdl3"
import "vendor:wgpu"
@(private="file")
DRIVER_X11: cstring = "x11"
@(private="file")
DRIVER_WAYLAND: cstring = "wayland"
GetSurface :: proc(instance: wgpu.Instance, window: ^sdl3.Window) -> wgpu.Surface {
if sdl3.strcmp(sdl3.GetCurrentVideoDriver(), DRIVER_WAYLAND) == 0 {
display := sdl3.GetPointerProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_X11_DISPLAY_POINTER, nil)
surface := sdl3.GetNumberProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_X11_WINDOW_NUMBER, 0)
return wgpu.InstanceCreateSurface(
instance,
&wgpu.SurfaceDescriptor{
nextInChain = &wgpu.SurfaceDescriptorFromWaylandSurface{
chain = {
sType = .SurfaceDescriptorFromWaylandSurface,
},
display = display,
surface = surface,
},
},
)
} else if sdl3.strcmp(sdl3.GetCurrentVideoDriver(), DRIVER_X11) == 0 {
display := sdl3.GetPointerProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_WAYLAND_DISPLAY_POINTER, nil)
surface := sdl3.GetPointerProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_WAYLAND_SURFACE_POINTER, 0)
return wgpu.InstanceCreateSurface(
instance,
&wgpu.SurfaceDescriptor{
nextInChain = &wgpu.SurfaceDescriptorFromXlibWindow{
chain = {
sType = .SurfaceDescriptorFromXlibWindow,
},
display = display,
window = u64(window),
},
},
)
} else {
panic("wgpu sdl3 glue: unsupported platform, expected Wayland or X11")
}
}
+23
View File
@@ -0,0 +1,23 @@
package wgpu_sdl3_glue
import win "core:sys/windows"
import "vendor:sdl3"
import "vendor:wgpu"
GetSurface :: proc(instance: wgpu.Instance, window: ^sdl3.Window) -> wgpu.Surface {
hwnd := sdl3.GetPointerProperty(sdl3.GetWindowProperties(window), sdl3.PROP_WINDOW_WIN32_HWND_POINTER, nil)
hinstance := win.GetModuleHandleW(nil)
return wgpu.InstanceCreateSurface(
instance,
&wgpu.SurfaceDescriptor{
nextInChain = &wgpu.SurfaceDescriptorFromWindowsHWND{
chain = wgpu.ChainedStruct{
sType = .SurfaceDescriptorFromWindowsHWND,
},
hinstance = rawptr(hinstance),
hwnd = rawptr(hwnd),
},
},
)
}