This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_surface.c
121 lines (115 loc) · 3.82 KB
/
create_surface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "webgpu.h"
#include "wgpu.h"
#define WGPU_TARGET_MACOS 1
#define WGPU_TARGET_LINUX_X11 2
#define WGPU_TARGET_WINDOWS 3
#define WGPU_TARGET_LINUX_WAYLAND 4
#if WGPU_TARGET == WGPU_TARGET_MACOS
#include <Foundation/Foundation.h>
#include <QuartzCore/CAMetalLayer.h>
#endif
#include <GLFW/glfw3.h>
#if WGPU_TARGET == WGPU_TARGET_MACOS
#define GLFW_EXPOSE_NATIVE_COCOA
#elif WGPU_TARGET == WGPU_TARGET_LINUX_X11
#define GLFW_EXPOSE_NATIVE_X11
#elif WGPU_TARGET == WGPU_TARGET_LINUX_WAYLAND
#define GLFW_EXPOSE_NATIVE_WAYLAND
#elif WGPU_TARGET == WGPU_TARGET_WINDOWS
#define GLFW_EXPOSE_NATIVE_WIN32
#endif
#include <GLFW/glfw3native.h>
WGPUSurface create_surface(WGPUInstance instance, GLFWwindow* window) {
WGPUSurface surface;
#if WGPU_TARGET == WGPU_TARGET_MACOS
{
id metal_layer = NULL;
NSWindow *ns_window = glfwGetCocoaWindow(window);
[ns_window.contentView setWantsLayer:YES];
metal_layer = [CAMetalLayer layer];
[ns_window.contentView setLayer:metal_layer];
surface = wgpuInstanceCreateSurface(
instance,
&(WGPUSurfaceDescriptor){
.label = NULL,
.nextInChain =
(const WGPUChainedStruct *)&(
WGPUSurfaceDescriptorFromMetalLayer){
.chain =
(WGPUChainedStruct){
.next = NULL,
.sType = WGPUSType_SurfaceDescriptorFromMetalLayer,
},
.layer = metal_layer,
},
});
}
#elif WGPU_TARGET == WGPU_TARGET_LINUX_X11
{
Display *x11_display = glfwGetX11Display();
Window x11_window = glfwGetX11Window(window);
surface = wgpuInstanceCreateSurface(
instance,
&(WGPUSurfaceDescriptor){
.label = NULL,
.nextInChain =
(const WGPUChainedStruct *)&(
WGPUSurfaceDescriptorFromXlibWindow){
.chain =
(WGPUChainedStruct){
.next = NULL,
.sType = WGPUSType_SurfaceDescriptorFromXlibWindow,
},
.display = x11_display,
.window = x11_window,
},
});
}
#elif WGPU_TARGET == WGPU_TARGET_LINUX_WAYLAND
{
struct wl_display *wayland_display = glfwGetWaylandDisplay();
struct wl_surface *wayland_surface = glfwGetWaylandWindow(window);
surface = wgpuInstanceCreateSurface(
instance,
&(WGPUSurfaceDescriptor){
.label = NULL,
.nextInChain =
(const WGPUChainedStruct *)&(
WGPUSurfaceDescriptorFromWaylandSurface){
.chain =
(WGPUChainedStruct){
.next = NULL,
.sType =
WGPUSType_SurfaceDescriptorFromWaylandSurface,
},
.display = wayland_display,
.surface = wayland_surface,
},
});
}
#elif WGPU_TARGET == WGPU_TARGET_WINDOWS
{
HWND hwnd = glfwGetWin32Window(window);
HINSTANCE hinstance = GetModuleHandle(NULL);
surface = wgpuInstanceCreateSurface(
instance,
&(WGPUSurfaceDescriptor){
.label = NULL,
.nextInChain =
(const WGPUChainedStruct *)&(
WGPUSurfaceDescriptorFromWindowsHWND){
.chain =
(WGPUChainedStruct){
.next = NULL,
.sType = WGPUSType_SurfaceDescriptorFromWindowsHWND,
},
.hinstance = hinstance,
.hwnd = hwnd,
},
});
}
#else
#error "Unsupported WGPU_TARGET"
#endif
return surface;
}