src/glfw_surface.c
 1#include "alba.h"
 2#include "internal.h"
 3
 4#include <stdio.h>
 5#include <stdlib.h>
 6
 7#include "webgpu.h"
 8#include "GLFW/glfw3.h"
 9
10#ifdef GLFW_X11
11#   define GLFW_EXPOSE_NATIVE_X11
12#endif
13#ifdef GLFW_WAYLAND
14#   define GLFW_EXPOSE_NATIVE_WAYLAND
15#endif
16#ifdef GLFW_COCOA
17#   define GLFW_EXPOSE_NATIVE_COCOA
18#endif
19#ifdef GLFW_WIN32
20#   define GLFW_EXPOSE_NATIVE_WIN32
21#endif
22
23#include <GLFW/glfw3native.h>
24
25
26WGPUSurface get_window_surface(const WGPUInstance instance, AlbaWindow* window)
27{
28    WGPUSurfaceDescriptor surfaceDescriptor = {0};
29    switch (glfwGetPlatform())
30    {
31#ifdef GLFW_X11
32    case GLFW_PLATFORM_X11:
33        Display* x11_display = glfwGetX11Display();
34        const Window x11_window = glfwGetX11Window(window->glfw_window);
35
36        WGPUSurfaceDescriptorFromXlibWindow fromXlibWindow = {0};
37        fromXlibWindow.chain.sType = WGPUSType_SurfaceDescriptorFromXlibWindow;
38        fromXlibWindow.display = x11_display;
39        fromXlibWindow.window = x11_window;
40
41        surfaceDescriptor.nextInChain = &fromXlibWindow.chain;
42        return wgpuInstanceCreateSurface(instance, &surfaceDescriptor);
43#endif
44
45#ifdef GLFW_WAYLAND
46    case GLFW_PLATFORM_WAYLAND:
47        struct wl_display* wayland_display = glfwGetWaylandDisplay();
48        struct wl_surface* wayland_surface = glfwGetWaylandWindow(window->glfw_window);
49
50        WGPUSurfaceDescriptorFromWaylandSurface fromWaylandSurface = {0};
51        fromWaylandSurface.chain.sType = WGPUSType_SurfaceDescriptorFromWaylandSurface;
52        fromWaylandSurface.display = wayland_display;
53        fromWaylandSurface.surface = wayland_surface;
54
55        surfaceDescriptor.nextInChain = &fromWaylandSurface.chain;
56        return wgpuInstanceCreateSurface(instance, &surfaceDescriptor);
57#endif
58
59#ifdef GLFW_COCOA
60    case GLFW_PLATFORM_COCOA:
61        id metal_layer = [CAMetalLayer layer];
62        NSWindow* ns_window = glfwGetCocoaWindow(window->glfw_window);
63        [ns_window.contentView setWantsLayer : YES] ;
64        [ns_window.contentView setLayer : metal_layer] ;
65
66        WGPUSurfaceDescriptorFromMetalLayer fromMetalLayer = {0};
67        fromMetalLayer.chain.sType = WGPUSType_SurfaceDescriptorFromMetalLayer;
68        fromMetalLayer.layer = metal_layer;
69
70        surfaceDescriptor.nextInChain = &fromMetalLayer.chain;
71        return wgpuInstanceCreateSurface(instance, &surfaceDescriptor);
72#endif
73
74#ifdef GLFW_WIN32
75    case GLFW_PLATFORM_WIN32:
76    HWND hwnd = glfwGetWin32Window(window->glfw_window);
77    HINSTANCE hinstance = GetModuleHandle(NULL);
78
79    WGPUSurfaceDescriptorFromWindowsHWND fromWindowsHWND = {0]};
80    fromWindowsHWND.chain.sType = WGPUSType_SurfaceDescriptorFromWindowsHWND;
81    fromWindowsHWND.hinstance = hinstance;
82    fromWindowsHWND.hwnd = hwnd;
83
84    surfaceDescriptor.nextInChain = &fromWindowsHWND.chain;
85    return wgpuInstanceCreateSurface(instance, &surfaceDescriptor);
86#endif
87
88    default:
89        fprintf(stderr, "error: unsuppoerted platform: %d\n", glfwGetPlatform());
90        exit(1);
91    }
92}