nish 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/docs/AI.md +3 -1
- package/package.json +5 -5
- package/runtime/gfx/README.md +55 -0
- package/runtime/gfx/nish-gfx.c +1063 -0
- package/runtime/gfx/nish-gfx.h +221 -0
- package/runtime/gfx/nish-gfx.overrides.json +14 -0
- package/runtime/nish.d.ts +22 -5
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-net.c +53 -27
- package/runtime/runtime-uring.c +39 -7
- package/runtime/sqlite/README.md +17 -0
- package/runtime/sqlite/nish-sqlite.c +97 -0
- package/scripts/build.sh +88 -3
- package/std/compress/deflate.ts +1371 -704
- package/std/net/handler.ts +24 -6
- package/std/net/http-server.ts +543 -156
- package/std/net/http1-server.ts +29 -2
- package/std/net/http2-cleartext.ts +30 -1
- package/std/raster.ts +35 -21
- package/std/sync/shm.ts +6 -3
- package/std/synth.ts +9 -1
|
@@ -0,0 +1,1063 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* nish-gfx.c: the shim nish-gfx.h declares, over wgpu-native and SDL3.
|
|
3
|
+
*
|
|
4
|
+
* Every WebGPU request that answers through a callback is made synchronous
|
|
5
|
+
* here: the callback runs in WGPUCallbackMode_AllowProcessEvents mode and
|
|
6
|
+
* writes into a struct on the caller's stack, and a bounded loop of
|
|
7
|
+
* wgpuInstanceProcessEvents() waits for it. The shim never calls
|
|
8
|
+
* wgpuInstanceWaitAny() and never waits on a clock. Buffer readback maps with
|
|
9
|
+
* wgpuBufferMapAsync() and blocks in wgpuDevicePoll(wait = true), which
|
|
10
|
+
* returns once the queue is idle.
|
|
11
|
+
*
|
|
12
|
+
* Errors are copied, never handed to Nish code as they happen: the
|
|
13
|
+
* device-lost and uncaptured-error callbacks (always installed) and the
|
|
14
|
+
* shim's own failures put the kind and at most NISHGFX_ERROR_MESSAGE_MAX
|
|
15
|
+
* bytes of the message into a fixed ring of NISHGFX_ERROR_RING entries under
|
|
16
|
+
* the ring's mutex. A report that finds the ring full is counted, not kept.
|
|
17
|
+
*/
|
|
18
|
+
#include "nish-gfx.h"
|
|
19
|
+
|
|
20
|
+
#include <pthread.h>
|
|
21
|
+
#include <stdio.h>
|
|
22
|
+
#include <stdlib.h>
|
|
23
|
+
#include <string.h>
|
|
24
|
+
|
|
25
|
+
#include <SDL3/SDL.h>
|
|
26
|
+
#include <webgpu/webgpu.h>
|
|
27
|
+
#include <webgpu/wgpu.h>
|
|
28
|
+
|
|
29
|
+
/* How many wgpuInstanceProcessEvents() rounds an adapter or device request
|
|
30
|
+
* gets before it is given up as never answered. wgpu-native answers both
|
|
31
|
+
* within the first round on every backend it has. */
|
|
32
|
+
#define NISHGFX_PROCESS_ROUNDS 1000
|
|
33
|
+
|
|
34
|
+
#define NISHGFX_MAX_ATTRIBUTES 16
|
|
35
|
+
#define NISHGFX_MAX_BINDINGS 16
|
|
36
|
+
|
|
37
|
+
/* ---- the error ring ---------------------------------------------------- */
|
|
38
|
+
|
|
39
|
+
typedef struct NishGfxError {
|
|
40
|
+
int32_t kind;
|
|
41
|
+
char message[NISHGFX_ERROR_MESSAGE_MAX + 1];
|
|
42
|
+
} NishGfxError;
|
|
43
|
+
|
|
44
|
+
typedef struct NishGfxRing {
|
|
45
|
+
pthread_mutex_t lock;
|
|
46
|
+
NishGfxError entries[NISHGFX_ERROR_RING];
|
|
47
|
+
uint32_t head; /* the oldest entry */
|
|
48
|
+
uint32_t count; /* entries queued */
|
|
49
|
+
uint32_t dropped;
|
|
50
|
+
} NishGfxRing;
|
|
51
|
+
|
|
52
|
+
static void ring_init(NishGfxRing *ring) {
|
|
53
|
+
memset(ring, 0, sizeof *ring);
|
|
54
|
+
pthread_mutex_init(&ring->lock, NULL);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static void ring_destroy(NishGfxRing *ring) { pthread_mutex_destroy(&ring->lock); }
|
|
58
|
+
|
|
59
|
+
/* Queue one report. `len` may be WGPU_STRLEN for a NUL-terminated message;
|
|
60
|
+
* the copy keeps at most NISHGFX_ERROR_MESSAGE_MAX bytes and always ends in
|
|
61
|
+
* a NUL. */
|
|
62
|
+
static void ring_push(NishGfxRing *ring, int32_t kind, const char *msg, size_t len) {
|
|
63
|
+
if (msg == NULL) len = 0;
|
|
64
|
+
else if (len == WGPU_STRLEN) len = strlen(msg);
|
|
65
|
+
if (len > NISHGFX_ERROR_MESSAGE_MAX) len = NISHGFX_ERROR_MESSAGE_MAX;
|
|
66
|
+
pthread_mutex_lock(&ring->lock);
|
|
67
|
+
if (ring->count == NISHGFX_ERROR_RING) {
|
|
68
|
+
if (ring->dropped != UINT32_MAX) ring->dropped++;
|
|
69
|
+
} else {
|
|
70
|
+
NishGfxError *e = &ring->entries[(ring->head + ring->count) % NISHGFX_ERROR_RING];
|
|
71
|
+
e->kind = kind;
|
|
72
|
+
if (len > 0) memcpy(e->message, msg, len);
|
|
73
|
+
e->message[len] = '\0';
|
|
74
|
+
ring->count++;
|
|
75
|
+
}
|
|
76
|
+
pthread_mutex_unlock(&ring->lock);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static int32_t ring_pop(NishGfxRing *ring, void *msg, size_t cap) {
|
|
80
|
+
int32_t kind = NISHGFX_ERROR_NONE;
|
|
81
|
+
pthread_mutex_lock(&ring->lock);
|
|
82
|
+
if (ring->count > 0) {
|
|
83
|
+
NishGfxError *e = &ring->entries[ring->head];
|
|
84
|
+
kind = e->kind;
|
|
85
|
+
if (msg != NULL && cap > 0) {
|
|
86
|
+
size_t len = strlen(e->message);
|
|
87
|
+
if (len > cap - 1) len = cap - 1;
|
|
88
|
+
memcpy(msg, e->message, len);
|
|
89
|
+
((char *)msg)[len] = '\0';
|
|
90
|
+
}
|
|
91
|
+
ring->head = (ring->head + 1) % NISHGFX_ERROR_RING;
|
|
92
|
+
ring->count--;
|
|
93
|
+
}
|
|
94
|
+
pthread_mutex_unlock(&ring->lock);
|
|
95
|
+
return kind;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* ---- the handles ------------------------------------------------------- */
|
|
99
|
+
|
|
100
|
+
struct NishGfxInstance {
|
|
101
|
+
WGPUInstance instance;
|
|
102
|
+
NishGfxRing errors;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
struct NishGfxWindow {
|
|
106
|
+
SDL_Window *window;
|
|
107
|
+
NishGfxEvent events[NISHGFX_EVENT_RING];
|
|
108
|
+
uint32_t head, count, dropped;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
struct NishGfxSurface {
|
|
112
|
+
NishGfxInstance *instance;
|
|
113
|
+
SDL_Window *window; /* for its pixel size when the surface is reconfigured */
|
|
114
|
+
SDL_MetalView metal_view;
|
|
115
|
+
WGPUSurface surface;
|
|
116
|
+
WGPUSurfaceConfiguration config;
|
|
117
|
+
WGPUDevice device; /* the device configure saw, for its error ring */
|
|
118
|
+
NishGfxDevice *owner;
|
|
119
|
+
WGPUTexture texture;
|
|
120
|
+
NishGfxView *view;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
struct NishGfxAdapter {
|
|
124
|
+
WGPUAdapter adapter;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
struct NishGfxDevice {
|
|
128
|
+
NishGfxInstance *instance;
|
|
129
|
+
WGPUDevice device;
|
|
130
|
+
WGPUQueue queue;
|
|
131
|
+
NishGfxRing errors;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
struct NishGfxBuffer {
|
|
135
|
+
WGPUBuffer buffer;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
struct NishGfxShader {
|
|
139
|
+
WGPUShaderModule module;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
struct NishGfxPipelineDesc {
|
|
143
|
+
WGPUShaderModule module;
|
|
144
|
+
char *vs_entry, *fs_entry;
|
|
145
|
+
WGPUTextureFormat color_format;
|
|
146
|
+
uint64_t stride;
|
|
147
|
+
size_t attribute_count;
|
|
148
|
+
WGPUVertexAttribute attributes[NISHGFX_MAX_ATTRIBUTES];
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
struct NishGfxPipeline {
|
|
152
|
+
WGPURenderPipeline pipeline;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
struct NishGfxBindGroupDesc {
|
|
156
|
+
WGPUBindGroupLayout layout;
|
|
157
|
+
size_t entry_count;
|
|
158
|
+
WGPUBindGroupEntry entries[NISHGFX_MAX_BINDINGS];
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
struct NishGfxBindGroup {
|
|
162
|
+
WGPUBindGroup group;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
struct NishGfxEncoder {
|
|
166
|
+
WGPUCommandEncoder encoder;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
struct NishGfxPass {
|
|
170
|
+
WGPURenderPassEncoder pass;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
struct NishGfxView {
|
|
174
|
+
WGPUTextureView view;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
struct NishGfxOffscreen {
|
|
178
|
+
NishGfxDevice *device;
|
|
179
|
+
uint32_t width, height;
|
|
180
|
+
uint32_t padded_row; /* bytesPerRow, width * 4 rounded up to 256 */
|
|
181
|
+
WGPUTexture texture;
|
|
182
|
+
NishGfxView view;
|
|
183
|
+
WGPUBuffer readback;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
static WGPUStringView string_view(const char *s) {
|
|
187
|
+
WGPUStringView v = WGPU_STRING_VIEW_INIT;
|
|
188
|
+
if (s != NULL) {
|
|
189
|
+
v.data = s;
|
|
190
|
+
v.length = strlen(s);
|
|
191
|
+
}
|
|
192
|
+
return v;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static char *copy_string(const char *s) {
|
|
196
|
+
if (s == NULL) return NULL;
|
|
197
|
+
size_t n = strlen(s) + 1;
|
|
198
|
+
char *out = malloc(n);
|
|
199
|
+
if (out != NULL) memcpy(out, s, n);
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* ---- instance ---------------------------------------------------------- */
|
|
204
|
+
|
|
205
|
+
NishGfxInstance *nishgfx_instance_create(void) {
|
|
206
|
+
NishGfxInstance *inst = calloc(1, sizeof *inst);
|
|
207
|
+
if (inst == NULL) return NULL;
|
|
208
|
+
inst->instance = wgpuCreateInstance(NULL);
|
|
209
|
+
if (inst->instance == NULL) {
|
|
210
|
+
free(inst);
|
|
211
|
+
return NULL;
|
|
212
|
+
}
|
|
213
|
+
ring_init(&inst->errors);
|
|
214
|
+
return inst;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
void nishgfx_instance_release(NishGfxInstance *inst) {
|
|
218
|
+
if (inst == NULL) return;
|
|
219
|
+
wgpuInstanceRelease(inst->instance);
|
|
220
|
+
ring_destroy(&inst->errors);
|
|
221
|
+
free(inst);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
int32_t nishgfx_instance_error_next(NishGfxInstance *inst, void *msg, size_t cap) {
|
|
225
|
+
return inst == NULL ? NISHGFX_ERROR_NONE : ring_pop(&inst->errors, msg, cap);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/* ---- window and events ------------------------------------------------- */
|
|
229
|
+
|
|
230
|
+
NishGfxWindow *nishgfx_window_create(const char *title, uint32_t width, uint32_t height) {
|
|
231
|
+
if (width == 0 || height == 0 || width > INT32_MAX || height > INT32_MAX) return NULL;
|
|
232
|
+
if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) return NULL;
|
|
233
|
+
NishGfxWindow *w = calloc(1, sizeof *w);
|
|
234
|
+
if (w == NULL) {
|
|
235
|
+
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
236
|
+
return NULL;
|
|
237
|
+
}
|
|
238
|
+
SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
|
|
239
|
+
#ifdef __APPLE__
|
|
240
|
+
/* Only cocoa makes Metal views: SDL refuses a METAL window under any other
|
|
241
|
+
* driver (the offscreen one included) instead of ignoring the flag. */
|
|
242
|
+
const char *driver = SDL_GetCurrentVideoDriver();
|
|
243
|
+
if (driver != NULL && strcmp(driver, "cocoa") == 0) flags |= SDL_WINDOW_METAL;
|
|
244
|
+
#endif
|
|
245
|
+
/* wgpu-native draws the window, so it holds no GL context. Without this
|
|
246
|
+
* property SDL adds OpenGL to a macOS window that asks for no backend, and
|
|
247
|
+
* the offscreen driver fails loading GL ("That operation is not supported"). */
|
|
248
|
+
SDL_PropertiesID props = SDL_CreateProperties();
|
|
249
|
+
if (props == 0) {
|
|
250
|
+
free(w);
|
|
251
|
+
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
252
|
+
return NULL;
|
|
253
|
+
}
|
|
254
|
+
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title != NULL ? title : "");
|
|
255
|
+
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, (Sint64)width);
|
|
256
|
+
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, (Sint64)height);
|
|
257
|
+
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, (Sint64)flags);
|
|
258
|
+
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_EXTERNAL_GRAPHICS_CONTEXT_BOOLEAN, true);
|
|
259
|
+
w->window = SDL_CreateWindowWithProperties(props);
|
|
260
|
+
SDL_DestroyProperties(props);
|
|
261
|
+
if (w->window == NULL) {
|
|
262
|
+
free(w);
|
|
263
|
+
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
264
|
+
return NULL;
|
|
265
|
+
}
|
|
266
|
+
return w;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
void nishgfx_window_release(NishGfxWindow *w) {
|
|
270
|
+
if (w == NULL) return;
|
|
271
|
+
SDL_DestroyWindow(w->window);
|
|
272
|
+
free(w);
|
|
273
|
+
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
static void event_push(NishGfxWindow *w, const NishGfxEvent *e) {
|
|
277
|
+
if (w->count == NISHGFX_EVENT_RING) {
|
|
278
|
+
if (w->dropped != UINT32_MAX) w->dropped++;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
w->events[(w->head + w->count) % NISHGFX_EVENT_RING] = *e;
|
|
282
|
+
w->count++;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
uint32_t nishgfx_events_poll(NishGfxWindow *w) {
|
|
286
|
+
if (w == NULL) return 0;
|
|
287
|
+
uint32_t queued = 0;
|
|
288
|
+
SDL_Event ev;
|
|
289
|
+
while (SDL_PollEvent(&ev)) {
|
|
290
|
+
NishGfxEvent e;
|
|
291
|
+
memset(&e, 0, sizeof e);
|
|
292
|
+
switch (ev.type) {
|
|
293
|
+
case SDL_EVENT_QUIT:
|
|
294
|
+
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
|
295
|
+
e.kind = NISHGFX_EVENT_QUIT;
|
|
296
|
+
break;
|
|
297
|
+
case SDL_EVENT_KEY_DOWN:
|
|
298
|
+
case SDL_EVENT_KEY_UP:
|
|
299
|
+
e.kind = ev.type == SDL_EVENT_KEY_DOWN ? NISHGFX_EVENT_KEY_DOWN : NISHGFX_EVENT_KEY_UP;
|
|
300
|
+
e.key = ev.key.key;
|
|
301
|
+
e.modifiers = ev.key.mod;
|
|
302
|
+
break;
|
|
303
|
+
case SDL_EVENT_MOUSE_MOTION:
|
|
304
|
+
e.kind = NISHGFX_EVENT_MOUSE_MOVE;
|
|
305
|
+
e.x = ev.motion.x;
|
|
306
|
+
e.y = ev.motion.y;
|
|
307
|
+
e.dx = ev.motion.xrel;
|
|
308
|
+
e.dy = ev.motion.yrel;
|
|
309
|
+
break;
|
|
310
|
+
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
311
|
+
case SDL_EVENT_MOUSE_BUTTON_UP:
|
|
312
|
+
e.kind = ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN ? NISHGFX_EVENT_MOUSE_DOWN : NISHGFX_EVENT_MOUSE_UP;
|
|
313
|
+
e.button = ev.button.button;
|
|
314
|
+
e.x = ev.button.x;
|
|
315
|
+
e.y = ev.button.y;
|
|
316
|
+
break;
|
|
317
|
+
case SDL_EVENT_MOUSE_WHEEL:
|
|
318
|
+
e.kind = NISHGFX_EVENT_WHEEL;
|
|
319
|
+
e.x = ev.wheel.mouse_x;
|
|
320
|
+
e.y = ev.wheel.mouse_y;
|
|
321
|
+
e.dx = ev.wheel.x;
|
|
322
|
+
e.dy = ev.wheel.y;
|
|
323
|
+
break;
|
|
324
|
+
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
|
|
325
|
+
e.kind = NISHGFX_EVENT_RESIZED;
|
|
326
|
+
e.width = ev.window.data1 > 0 ? (uint32_t)ev.window.data1 : 0;
|
|
327
|
+
e.height = ev.window.data2 > 0 ? (uint32_t)ev.window.data2 : 0;
|
|
328
|
+
break;
|
|
329
|
+
default:
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
event_push(w, &e);
|
|
333
|
+
queued++;
|
|
334
|
+
}
|
|
335
|
+
return queued;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
uint32_t nishgfx_event_next(NishGfxWindow *w, NishGfxEvent *out) {
|
|
339
|
+
if (w == NULL || out == NULL) return NISHGFX_EVENT_NONE;
|
|
340
|
+
if (w->count == 0) {
|
|
341
|
+
memset(out, 0, sizeof *out);
|
|
342
|
+
return NISHGFX_EVENT_NONE;
|
|
343
|
+
}
|
|
344
|
+
*out = w->events[w->head];
|
|
345
|
+
w->head = (w->head + 1) % NISHGFX_EVENT_RING;
|
|
346
|
+
w->count--;
|
|
347
|
+
return out->kind;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
uint32_t nishgfx_events_dropped(NishGfxWindow *w) { return w == NULL ? 0 : w->dropped; }
|
|
351
|
+
|
|
352
|
+
/* ---- surface ----------------------------------------------------------- */
|
|
353
|
+
|
|
354
|
+
NishGfxSurface *nishgfx_surface_create(NishGfxInstance *inst, NishGfxWindow *w) {
|
|
355
|
+
if (inst == NULL || w == NULL) return NULL;
|
|
356
|
+
NishGfxSurface *s = calloc(1, sizeof *s);
|
|
357
|
+
if (s == NULL) return NULL;
|
|
358
|
+
s->instance = inst;
|
|
359
|
+
s->window = w->window;
|
|
360
|
+
WGPUSurfaceDescriptor desc = WGPU_SURFACE_DESCRIPTOR_INIT;
|
|
361
|
+
const char *driver = SDL_GetCurrentVideoDriver();
|
|
362
|
+
if (driver == NULL) driver = "";
|
|
363
|
+
#ifdef __APPLE__
|
|
364
|
+
WGPUSurfaceSourceMetalLayer metal = WGPU_SURFACE_SOURCE_METAL_LAYER_INIT;
|
|
365
|
+
if (strcmp(driver, "cocoa") == 0) {
|
|
366
|
+
s->metal_view = SDL_Metal_CreateView(w->window);
|
|
367
|
+
metal.layer = s->metal_view != NULL ? SDL_Metal_GetLayer(s->metal_view) : NULL;
|
|
368
|
+
if (metal.layer != NULL) desc.nextInChain = &metal.chain;
|
|
369
|
+
}
|
|
370
|
+
#else
|
|
371
|
+
SDL_PropertiesID props = SDL_GetWindowProperties(w->window);
|
|
372
|
+
WGPUSurfaceSourceWaylandSurface wayland = WGPU_SURFACE_SOURCE_WAYLAND_SURFACE_INIT;
|
|
373
|
+
WGPUSurfaceSourceXlibWindow xlib = WGPU_SURFACE_SOURCE_XLIB_WINDOW_INIT;
|
|
374
|
+
if (strcmp(driver, "wayland") == 0) {
|
|
375
|
+
wayland.display = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL);
|
|
376
|
+
wayland.surface = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL);
|
|
377
|
+
if (wayland.display != NULL && wayland.surface != NULL) desc.nextInChain = &wayland.chain;
|
|
378
|
+
} else if (strcmp(driver, "x11") == 0) {
|
|
379
|
+
xlib.display = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL);
|
|
380
|
+
xlib.window = (uint64_t)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
|
|
381
|
+
if (xlib.display != NULL && xlib.window != 0) desc.nextInChain = &xlib.chain;
|
|
382
|
+
}
|
|
383
|
+
#endif
|
|
384
|
+
if (desc.nextInChain == NULL) {
|
|
385
|
+
char why[NISHGFX_ERROR_MESSAGE_MAX + 1];
|
|
386
|
+
snprintf(why, sizeof why, "no WebGPU surface for SDL video driver \"%s\"", driver);
|
|
387
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL, why, WGPU_STRLEN);
|
|
388
|
+
nishgfx_surface_release(s);
|
|
389
|
+
return NULL;
|
|
390
|
+
}
|
|
391
|
+
s->surface = wgpuInstanceCreateSurface(inst->instance, &desc);
|
|
392
|
+
if (s->surface == NULL) {
|
|
393
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL, "wgpuInstanceCreateSurface answered null", WGPU_STRLEN);
|
|
394
|
+
nishgfx_surface_release(s);
|
|
395
|
+
return NULL;
|
|
396
|
+
}
|
|
397
|
+
return s;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
static void surface_drop_frame(NishGfxSurface *s) {
|
|
401
|
+
if (s->view != NULL) {
|
|
402
|
+
wgpuTextureViewRelease(s->view->view);
|
|
403
|
+
free(s->view);
|
|
404
|
+
s->view = NULL;
|
|
405
|
+
}
|
|
406
|
+
if (s->texture != NULL) {
|
|
407
|
+
wgpuTextureRelease(s->texture);
|
|
408
|
+
s->texture = NULL;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
void nishgfx_surface_release(NishGfxSurface *s) {
|
|
413
|
+
if (s == NULL) return;
|
|
414
|
+
surface_drop_frame(s);
|
|
415
|
+
if (s->surface != NULL) {
|
|
416
|
+
if (s->config.device != NULL) wgpuSurfaceUnconfigure(s->surface);
|
|
417
|
+
wgpuSurfaceRelease(s->surface);
|
|
418
|
+
}
|
|
419
|
+
#ifdef __APPLE__
|
|
420
|
+
if (s->metal_view != NULL) SDL_Metal_DestroyView(s->metal_view);
|
|
421
|
+
#endif
|
|
422
|
+
free(s);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static int has_mode(const WGPUSurfaceCapabilities *caps, WGPUPresentMode mode) {
|
|
426
|
+
for (size_t i = 0; i < caps->presentModeCount; i++)
|
|
427
|
+
if (caps->presentModes[i] == mode) return 1;
|
|
428
|
+
return 0;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
int32_t nishgfx_surface_configure(NishGfxSurface *s, NishGfxAdapter *adapter, NishGfxDevice *device,
|
|
432
|
+
uint32_t width, uint32_t height, uint32_t vsync) {
|
|
433
|
+
if (s == NULL || adapter == NULL || device == NULL) return -1;
|
|
434
|
+
if (width == 0 || height == 0) {
|
|
435
|
+
ring_push(&device->errors, NISHGFX_ERROR_VALIDATION, "a surface needs a width and height above 0", WGPU_STRLEN);
|
|
436
|
+
return -1;
|
|
437
|
+
}
|
|
438
|
+
WGPUSurfaceCapabilities caps = WGPU_SURFACE_CAPABILITIES_INIT;
|
|
439
|
+
if (wgpuSurfaceGetCapabilities(s->surface, adapter->adapter, &caps) != WGPUStatus_Success ||
|
|
440
|
+
caps.formatCount == 0) {
|
|
441
|
+
wgpuSurfaceCapabilitiesFreeMembers(caps);
|
|
442
|
+
ring_push(&device->errors, NISHGFX_ERROR_INTERNAL, "the surface offers no texture format", WGPU_STRLEN);
|
|
443
|
+
return -1;
|
|
444
|
+
}
|
|
445
|
+
WGPUPresentMode mode = WGPUPresentMode_Fifo;
|
|
446
|
+
if (!vsync) {
|
|
447
|
+
if (has_mode(&caps, WGPUPresentMode_Mailbox)) mode = WGPUPresentMode_Mailbox;
|
|
448
|
+
else if (has_mode(&caps, WGPUPresentMode_Immediate)) mode = WGPUPresentMode_Immediate;
|
|
449
|
+
}
|
|
450
|
+
WGPUSurfaceConfiguration config = WGPU_SURFACE_CONFIGURATION_INIT;
|
|
451
|
+
config.device = device->device;
|
|
452
|
+
config.format = caps.formats[0];
|
|
453
|
+
config.usage = WGPUTextureUsage_RenderAttachment;
|
|
454
|
+
config.width = width;
|
|
455
|
+
config.height = height;
|
|
456
|
+
config.alphaMode = caps.alphaModeCount > 0 ? caps.alphaModes[0] : WGPUCompositeAlphaMode_Auto;
|
|
457
|
+
config.presentMode = mode;
|
|
458
|
+
wgpuSurfaceCapabilitiesFreeMembers(caps);
|
|
459
|
+
surface_drop_frame(s);
|
|
460
|
+
wgpuSurfaceConfigure(s->surface, &config);
|
|
461
|
+
s->config = config;
|
|
462
|
+
s->owner = device;
|
|
463
|
+
return 0;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
uint32_t nishgfx_surface_format(NishGfxSurface *s) { return s == NULL ? 0 : (uint32_t)s->config.format; }
|
|
467
|
+
|
|
468
|
+
NishGfxView *nishgfx_surface_acquire(NishGfxSurface *s) {
|
|
469
|
+
if (s == NULL || s->config.device == NULL) return NULL;
|
|
470
|
+
surface_drop_frame(s);
|
|
471
|
+
WGPUSurfaceTexture frame = WGPU_SURFACE_TEXTURE_INIT;
|
|
472
|
+
wgpuSurfaceGetCurrentTexture(s->surface, &frame);
|
|
473
|
+
switch (frame.status) {
|
|
474
|
+
case WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal:
|
|
475
|
+
case WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal:
|
|
476
|
+
break;
|
|
477
|
+
case WGPUSurfaceGetCurrentTextureStatus_Timeout:
|
|
478
|
+
case WGPUSurfaceGetCurrentTextureStatus_Outdated:
|
|
479
|
+
case WGPUSurfaceGetCurrentTextureStatus_Lost:
|
|
480
|
+
if (frame.texture != NULL) wgpuTextureRelease(frame.texture);
|
|
481
|
+
/* At the window's size now; a minimised window's 0 waits for a frame
|
|
482
|
+
* where it has one. */
|
|
483
|
+
int pw = 0, ph = 0;
|
|
484
|
+
if (!SDL_GetWindowSizeInPixels(s->window, &pw, &ph) || pw <= 0 || ph <= 0) return NULL;
|
|
485
|
+
s->config.width = (uint32_t)pw;
|
|
486
|
+
s->config.height = (uint32_t)ph;
|
|
487
|
+
wgpuSurfaceConfigure(s->surface, &s->config);
|
|
488
|
+
return NULL;
|
|
489
|
+
default:
|
|
490
|
+
if (frame.texture != NULL) wgpuTextureRelease(frame.texture);
|
|
491
|
+
ring_push(&s->owner->errors, NISHGFX_ERROR_INTERNAL, "the surface could not give a frame", WGPU_STRLEN);
|
|
492
|
+
return NULL;
|
|
493
|
+
}
|
|
494
|
+
s->texture = frame.texture;
|
|
495
|
+
s->view = calloc(1, sizeof *s->view);
|
|
496
|
+
if (s->view == NULL) {
|
|
497
|
+
surface_drop_frame(s);
|
|
498
|
+
return NULL;
|
|
499
|
+
}
|
|
500
|
+
s->view->view = wgpuTextureCreateView(frame.texture, NULL);
|
|
501
|
+
return s->view;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
void nishgfx_surface_present(NishGfxSurface *s) {
|
|
505
|
+
if (s == NULL || s->texture == NULL) return;
|
|
506
|
+
wgpuSurfacePresent(s->surface);
|
|
507
|
+
surface_drop_frame(s);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/* ---- adapter and device ------------------------------------------------ */
|
|
511
|
+
|
|
512
|
+
typedef struct AdapterAnswer {
|
|
513
|
+
int done;
|
|
514
|
+
WGPURequestAdapterStatus status;
|
|
515
|
+
WGPUAdapter adapter;
|
|
516
|
+
char message[NISHGFX_ERROR_MESSAGE_MAX + 1];
|
|
517
|
+
} AdapterAnswer;
|
|
518
|
+
|
|
519
|
+
static void copy_view(char *dst, WGPUStringView v) {
|
|
520
|
+
size_t len = v.data == NULL ? 0 : v.length == WGPU_STRLEN ? strlen(v.data) : v.length;
|
|
521
|
+
if (len > NISHGFX_ERROR_MESSAGE_MAX) len = NISHGFX_ERROR_MESSAGE_MAX;
|
|
522
|
+
if (len > 0) memcpy(dst, v.data, len);
|
|
523
|
+
dst[len] = '\0';
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
static void on_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message,
|
|
527
|
+
void *userdata1, void *userdata2) {
|
|
528
|
+
(void)userdata2;
|
|
529
|
+
AdapterAnswer *a = userdata1;
|
|
530
|
+
a->status = status;
|
|
531
|
+
a->adapter = adapter;
|
|
532
|
+
copy_view(a->message, message);
|
|
533
|
+
a->done = 1;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
NishGfxAdapter *nishgfx_adapter_request(NishGfxInstance *inst, NishGfxSurface *surface, uint32_t power) {
|
|
537
|
+
if (inst == NULL) return NULL;
|
|
538
|
+
WGPURequestAdapterOptions options = WGPU_REQUEST_ADAPTER_OPTIONS_INIT;
|
|
539
|
+
options.powerPreference = (WGPUPowerPreference)power;
|
|
540
|
+
options.compatibleSurface = surface != NULL ? surface->surface : NULL;
|
|
541
|
+
AdapterAnswer answer;
|
|
542
|
+
memset(&answer, 0, sizeof answer);
|
|
543
|
+
WGPURequestAdapterCallbackInfo info = WGPU_REQUEST_ADAPTER_CALLBACK_INFO_INIT;
|
|
544
|
+
info.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
545
|
+
info.callback = on_adapter;
|
|
546
|
+
info.userdata1 = &answer;
|
|
547
|
+
wgpuInstanceRequestAdapter(inst->instance, &options, info);
|
|
548
|
+
for (int i = 0; !answer.done && i < NISHGFX_PROCESS_ROUNDS; i++) wgpuInstanceProcessEvents(inst->instance);
|
|
549
|
+
if (!answer.done) {
|
|
550
|
+
/* The callback still points at `answer`, which is about to go away: a
|
|
551
|
+
* request wgpu-native never answers is a bug the shim cannot recover
|
|
552
|
+
* from safely, so it stops the process rather than return into it. */
|
|
553
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL, "the adapter request was never answered", WGPU_STRLEN);
|
|
554
|
+
abort();
|
|
555
|
+
}
|
|
556
|
+
if (answer.status != WGPURequestAdapterStatus_Success || answer.adapter == NULL) {
|
|
557
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL,
|
|
558
|
+
answer.message[0] != '\0' ? answer.message : "no adapter", WGPU_STRLEN);
|
|
559
|
+
if (answer.adapter != NULL) wgpuAdapterRelease(answer.adapter);
|
|
560
|
+
return NULL;
|
|
561
|
+
}
|
|
562
|
+
NishGfxAdapter *a = calloc(1, sizeof *a);
|
|
563
|
+
if (a == NULL) {
|
|
564
|
+
wgpuAdapterRelease(answer.adapter);
|
|
565
|
+
return NULL;
|
|
566
|
+
}
|
|
567
|
+
a->adapter = answer.adapter;
|
|
568
|
+
return a;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
void nishgfx_adapter_release(NishGfxAdapter *a) {
|
|
572
|
+
if (a == NULL) return;
|
|
573
|
+
wgpuAdapterRelease(a->adapter);
|
|
574
|
+
free(a);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
typedef struct DeviceAnswer {
|
|
578
|
+
int done;
|
|
579
|
+
WGPURequestDeviceStatus status;
|
|
580
|
+
WGPUDevice device;
|
|
581
|
+
char message[NISHGFX_ERROR_MESSAGE_MAX + 1];
|
|
582
|
+
} DeviceAnswer;
|
|
583
|
+
|
|
584
|
+
static void on_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message,
|
|
585
|
+
void *userdata1, void *userdata2) {
|
|
586
|
+
(void)userdata2;
|
|
587
|
+
DeviceAnswer *d = userdata1;
|
|
588
|
+
d->status = status;
|
|
589
|
+
d->device = device;
|
|
590
|
+
copy_view(d->message, message);
|
|
591
|
+
d->done = 1;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
static void push_view(NishGfxRing *ring, int32_t kind, WGPUStringView v) {
|
|
595
|
+
ring_push(ring, kind, v.data, v.data == NULL ? 0 : v.length);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/* The device's ring outlives every callback wgpu-native can still make, as
|
|
599
|
+
* long as the program releases the device's objects before the device:
|
|
600
|
+
* nishgfx_device_release() releases the device, which fires device-lost with
|
|
601
|
+
* Destroyed or CallbackCancelled, before it frees the struct. An object used
|
|
602
|
+
* after that can report into freed memory (GFX-5 in docs/security/gfx.md). */
|
|
603
|
+
static void on_lost(WGPUDevice const *device, WGPUDeviceLostReason reason, WGPUStringView message,
|
|
604
|
+
void *userdata1, void *userdata2) {
|
|
605
|
+
(void)device;
|
|
606
|
+
(void)userdata2;
|
|
607
|
+
if (reason == WGPUDeviceLostReason_Destroyed || reason == WGPUDeviceLostReason_CallbackCancelled) return;
|
|
608
|
+
push_view(&((NishGfxDevice *)userdata1)->errors, NISHGFX_ERROR_DEVICE_LOST, message);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
static void on_error(WGPUDevice const *device, WGPUErrorType type, WGPUStringView message, void *userdata1,
|
|
612
|
+
void *userdata2) {
|
|
613
|
+
(void)device;
|
|
614
|
+
(void)userdata2;
|
|
615
|
+
int32_t kind = type == WGPUErrorType_Validation ? NISHGFX_ERROR_VALIDATION
|
|
616
|
+
: type == WGPUErrorType_OutOfMemory ? NISHGFX_ERROR_OUT_OF_MEMORY
|
|
617
|
+
: NISHGFX_ERROR_INTERNAL;
|
|
618
|
+
push_view(&((NishGfxDevice *)userdata1)->errors, kind, message);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
NishGfxDevice *nishgfx_device_request(NishGfxInstance *inst, NishGfxAdapter *adapter) {
|
|
622
|
+
if (inst == NULL || adapter == NULL) return NULL;
|
|
623
|
+
NishGfxDevice *dev = calloc(1, sizeof *dev);
|
|
624
|
+
if (dev == NULL) return NULL;
|
|
625
|
+
dev->instance = inst;
|
|
626
|
+
ring_init(&dev->errors);
|
|
627
|
+
WGPUDeviceDescriptor desc = WGPU_DEVICE_DESCRIPTOR_INIT;
|
|
628
|
+
desc.deviceLostCallbackInfo.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
629
|
+
desc.deviceLostCallbackInfo.callback = on_lost;
|
|
630
|
+
desc.deviceLostCallbackInfo.userdata1 = dev;
|
|
631
|
+
desc.uncapturedErrorCallbackInfo.callback = on_error;
|
|
632
|
+
desc.uncapturedErrorCallbackInfo.userdata1 = dev;
|
|
633
|
+
DeviceAnswer answer;
|
|
634
|
+
memset(&answer, 0, sizeof answer);
|
|
635
|
+
WGPURequestDeviceCallbackInfo info = WGPU_REQUEST_DEVICE_CALLBACK_INFO_INIT;
|
|
636
|
+
info.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
637
|
+
info.callback = on_device;
|
|
638
|
+
info.userdata1 = &answer;
|
|
639
|
+
wgpuAdapterRequestDevice(adapter->adapter, &desc, info);
|
|
640
|
+
for (int i = 0; !answer.done && i < NISHGFX_PROCESS_ROUNDS; i++) wgpuInstanceProcessEvents(inst->instance);
|
|
641
|
+
if (!answer.done) {
|
|
642
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL, "the device request was never answered", WGPU_STRLEN);
|
|
643
|
+
abort(); /* as in nishgfx_adapter_request */
|
|
644
|
+
}
|
|
645
|
+
if (answer.status != WGPURequestDeviceStatus_Success || answer.device == NULL) {
|
|
646
|
+
ring_push(&inst->errors, NISHGFX_ERROR_INTERNAL,
|
|
647
|
+
answer.message[0] != '\0' ? answer.message : "no device", WGPU_STRLEN);
|
|
648
|
+
if (answer.device != NULL) wgpuDeviceRelease(answer.device);
|
|
649
|
+
ring_destroy(&dev->errors);
|
|
650
|
+
free(dev);
|
|
651
|
+
return NULL;
|
|
652
|
+
}
|
|
653
|
+
dev->device = answer.device;
|
|
654
|
+
dev->queue = wgpuDeviceGetQueue(dev->device);
|
|
655
|
+
return dev;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
void nishgfx_device_release(NishGfxDevice *dev) {
|
|
659
|
+
if (dev == NULL) return;
|
|
660
|
+
wgpuQueueRelease(dev->queue);
|
|
661
|
+
wgpuDeviceRelease(dev->device);
|
|
662
|
+
wgpuInstanceProcessEvents(dev->instance->instance);
|
|
663
|
+
ring_destroy(&dev->errors);
|
|
664
|
+
free(dev);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
int32_t nishgfx_error_next(NishGfxDevice *dev, void *msg, size_t cap) {
|
|
668
|
+
return dev == NULL ? NISHGFX_ERROR_NONE : ring_pop(&dev->errors, msg, cap);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
uint32_t nishgfx_errors_dropped(NishGfxDevice *dev) {
|
|
672
|
+
if (dev == NULL) return 0;
|
|
673
|
+
pthread_mutex_lock(&dev->errors.lock);
|
|
674
|
+
uint32_t n = dev->errors.dropped;
|
|
675
|
+
pthread_mutex_unlock(&dev->errors.lock);
|
|
676
|
+
return n;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/* ---- resources --------------------------------------------------------- */
|
|
680
|
+
|
|
681
|
+
NishGfxBuffer *nishgfx_buffer_create(NishGfxDevice *dev, const NishGfxBufferDesc *d, const char *label) {
|
|
682
|
+
if (dev == NULL || d == NULL) return NULL;
|
|
683
|
+
WGPUBufferDescriptor desc = WGPU_BUFFER_DESCRIPTOR_INIT;
|
|
684
|
+
desc.label = string_view(label);
|
|
685
|
+
desc.size = d->size;
|
|
686
|
+
desc.usage = d->usage;
|
|
687
|
+
desc.mappedAtCreation = d->mapped_at_creation != 0;
|
|
688
|
+
WGPUBuffer buffer = wgpuDeviceCreateBuffer(dev->device, &desc);
|
|
689
|
+
if (buffer == NULL) return NULL;
|
|
690
|
+
NishGfxBuffer *b = calloc(1, sizeof *b);
|
|
691
|
+
if (b == NULL) {
|
|
692
|
+
wgpuBufferRelease(buffer);
|
|
693
|
+
return NULL;
|
|
694
|
+
}
|
|
695
|
+
b->buffer = buffer;
|
|
696
|
+
return b;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
void nishgfx_buffer_release(NishGfxBuffer *b) {
|
|
700
|
+
if (b == NULL) return;
|
|
701
|
+
wgpuBufferRelease(b->buffer);
|
|
702
|
+
free(b);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
void nishgfx_queue_write(NishGfxDevice *dev, NishGfxBuffer *b, uint64_t offset, const void *data, size_t len) {
|
|
706
|
+
if (dev == NULL || b == NULL || (data == NULL && len > 0)) return;
|
|
707
|
+
wgpuQueueWriteBuffer(dev->queue, b->buffer, offset, data, len);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
NishGfxShader *nishgfx_shader_create(NishGfxDevice *dev, const char *wgsl) {
|
|
711
|
+
if (dev == NULL || wgsl == NULL) return NULL;
|
|
712
|
+
WGPUShaderSourceWGSL source = WGPU_SHADER_SOURCE_WGSL_INIT;
|
|
713
|
+
source.code = string_view(wgsl);
|
|
714
|
+
WGPUShaderModuleDescriptor desc = WGPU_SHADER_MODULE_DESCRIPTOR_INIT;
|
|
715
|
+
desc.nextInChain = &source.chain;
|
|
716
|
+
WGPUShaderModule module = wgpuDeviceCreateShaderModule(dev->device, &desc);
|
|
717
|
+
if (module == NULL) return NULL;
|
|
718
|
+
NishGfxShader *s = calloc(1, sizeof *s);
|
|
719
|
+
if (s == NULL) {
|
|
720
|
+
wgpuShaderModuleRelease(module);
|
|
721
|
+
return NULL;
|
|
722
|
+
}
|
|
723
|
+
s->module = module;
|
|
724
|
+
return s;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
void nishgfx_shader_release(NishGfxShader *s) {
|
|
728
|
+
if (s == NULL) return;
|
|
729
|
+
wgpuShaderModuleRelease(s->module);
|
|
730
|
+
free(s);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/* ---- pipelines --------------------------------------------------------- */
|
|
734
|
+
|
|
735
|
+
NishGfxPipelineDesc *nishgfx_pipeline_desc_new(NishGfxShader *shader, const char *vs_entry, const char *fs_entry,
|
|
736
|
+
uint32_t color_format, uint64_t stride) {
|
|
737
|
+
if (shader == NULL || vs_entry == NULL || fs_entry == NULL) return NULL;
|
|
738
|
+
NishGfxPipelineDesc *d = calloc(1, sizeof *d);
|
|
739
|
+
if (d == NULL) return NULL;
|
|
740
|
+
d->vs_entry = copy_string(vs_entry);
|
|
741
|
+
d->fs_entry = copy_string(fs_entry);
|
|
742
|
+
if (d->vs_entry == NULL || d->fs_entry == NULL) {
|
|
743
|
+
free(d->vs_entry);
|
|
744
|
+
free(d->fs_entry);
|
|
745
|
+
free(d);
|
|
746
|
+
return NULL;
|
|
747
|
+
}
|
|
748
|
+
wgpuShaderModuleAddRef(shader->module);
|
|
749
|
+
d->module = shader->module;
|
|
750
|
+
d->color_format = (WGPUTextureFormat)color_format;
|
|
751
|
+
d->stride = stride;
|
|
752
|
+
return d;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
int32_t nishgfx_pipeline_desc_attribute(NishGfxPipelineDesc *d, uint32_t format, uint64_t offset,
|
|
756
|
+
uint32_t location) {
|
|
757
|
+
if (d == NULL || d->attribute_count == NISHGFX_MAX_ATTRIBUTES) return -1;
|
|
758
|
+
WGPUVertexAttribute *a = &d->attributes[d->attribute_count++];
|
|
759
|
+
WGPUVertexAttribute init = WGPU_VERTEX_ATTRIBUTE_INIT;
|
|
760
|
+
*a = init;
|
|
761
|
+
a->format = (WGPUVertexFormat)format;
|
|
762
|
+
a->offset = offset;
|
|
763
|
+
a->shaderLocation = location;
|
|
764
|
+
return 0;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
static void pipeline_desc_free(NishGfxPipelineDesc *d) {
|
|
768
|
+
wgpuShaderModuleRelease(d->module);
|
|
769
|
+
free(d->vs_entry);
|
|
770
|
+
free(d->fs_entry);
|
|
771
|
+
free(d);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
NishGfxPipeline *nishgfx_pipeline_create(NishGfxDevice *dev, NishGfxPipelineDesc *d) {
|
|
775
|
+
if (d == NULL) return NULL;
|
|
776
|
+
if (dev == NULL) {
|
|
777
|
+
pipeline_desc_free(d);
|
|
778
|
+
return NULL;
|
|
779
|
+
}
|
|
780
|
+
WGPUVertexBufferLayout layout = WGPU_VERTEX_BUFFER_LAYOUT_INIT;
|
|
781
|
+
layout.stepMode = WGPUVertexStepMode_Vertex;
|
|
782
|
+
layout.arrayStride = d->stride;
|
|
783
|
+
layout.attributeCount = d->attribute_count;
|
|
784
|
+
layout.attributes = d->attributes;
|
|
785
|
+
WGPUColorTargetState target = WGPU_COLOR_TARGET_STATE_INIT;
|
|
786
|
+
target.format = d->color_format;
|
|
787
|
+
target.writeMask = WGPUColorWriteMask_All;
|
|
788
|
+
WGPUFragmentState fragment = WGPU_FRAGMENT_STATE_INIT;
|
|
789
|
+
fragment.module = d->module;
|
|
790
|
+
fragment.entryPoint = string_view(d->fs_entry);
|
|
791
|
+
fragment.targetCount = 1;
|
|
792
|
+
fragment.targets = ⌖
|
|
793
|
+
WGPURenderPipelineDescriptor desc = WGPU_RENDER_PIPELINE_DESCRIPTOR_INIT;
|
|
794
|
+
desc.vertex.module = d->module;
|
|
795
|
+
desc.vertex.entryPoint = string_view(d->vs_entry);
|
|
796
|
+
desc.vertex.bufferCount = d->stride > 0 ? 1 : 0;
|
|
797
|
+
desc.vertex.buffers = d->stride > 0 ? &layout : NULL;
|
|
798
|
+
desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
|
|
799
|
+
desc.fragment = &fragment;
|
|
800
|
+
WGPURenderPipeline pipeline = wgpuDeviceCreateRenderPipeline(dev->device, &desc);
|
|
801
|
+
pipeline_desc_free(d);
|
|
802
|
+
if (pipeline == NULL) return NULL;
|
|
803
|
+
NishGfxPipeline *p = calloc(1, sizeof *p);
|
|
804
|
+
if (p == NULL) {
|
|
805
|
+
wgpuRenderPipelineRelease(pipeline);
|
|
806
|
+
return NULL;
|
|
807
|
+
}
|
|
808
|
+
p->pipeline = pipeline;
|
|
809
|
+
return p;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
void nishgfx_pipeline_release(NishGfxPipeline *p) {
|
|
813
|
+
if (p == NULL) return;
|
|
814
|
+
wgpuRenderPipelineRelease(p->pipeline);
|
|
815
|
+
free(p);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/* ---- bind groups ------------------------------------------------------- */
|
|
819
|
+
|
|
820
|
+
NishGfxBindGroupDesc *nishgfx_bind_group_desc_new(NishGfxPipeline *p, uint32_t index) {
|
|
821
|
+
if (p == NULL) return NULL;
|
|
822
|
+
NishGfxBindGroupDesc *d = calloc(1, sizeof *d);
|
|
823
|
+
if (d == NULL) return NULL;
|
|
824
|
+
d->layout = wgpuRenderPipelineGetBindGroupLayout(p->pipeline, index);
|
|
825
|
+
return d;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
int32_t nishgfx_bind_group_desc_buffer(NishGfxBindGroupDesc *d, uint32_t binding, NishGfxBuffer *b,
|
|
829
|
+
uint64_t offset, uint64_t size) {
|
|
830
|
+
if (d == NULL || b == NULL || d->entry_count == NISHGFX_MAX_BINDINGS) return -1;
|
|
831
|
+
WGPUBindGroupEntry *e = &d->entries[d->entry_count++];
|
|
832
|
+
WGPUBindGroupEntry init = WGPU_BIND_GROUP_ENTRY_INIT;
|
|
833
|
+
*e = init;
|
|
834
|
+
e->binding = binding;
|
|
835
|
+
e->buffer = b->buffer;
|
|
836
|
+
e->offset = offset;
|
|
837
|
+
/* wgpu-native panics, and so aborts, on an entry of size 0; 0 means the rest
|
|
838
|
+
* of the buffer here, as in nishgfx_pass_set_vertex_buffer. */
|
|
839
|
+
e->size = size == 0 ? WGPU_WHOLE_SIZE : size;
|
|
840
|
+
return 0;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
NishGfxBindGroup *nishgfx_bind_group_create(NishGfxDevice *dev, NishGfxBindGroupDesc *d) {
|
|
844
|
+
if (d == NULL) return NULL;
|
|
845
|
+
WGPUBindGroup group = NULL;
|
|
846
|
+
if (dev != NULL && d->layout != NULL) {
|
|
847
|
+
WGPUBindGroupDescriptor desc = WGPU_BIND_GROUP_DESCRIPTOR_INIT;
|
|
848
|
+
desc.layout = d->layout;
|
|
849
|
+
desc.entryCount = d->entry_count;
|
|
850
|
+
desc.entries = d->entries;
|
|
851
|
+
group = wgpuDeviceCreateBindGroup(dev->device, &desc);
|
|
852
|
+
}
|
|
853
|
+
if (d->layout != NULL) wgpuBindGroupLayoutRelease(d->layout);
|
|
854
|
+
free(d);
|
|
855
|
+
if (group == NULL) return NULL;
|
|
856
|
+
NishGfxBindGroup *g = calloc(1, sizeof *g);
|
|
857
|
+
if (g == NULL) {
|
|
858
|
+
wgpuBindGroupRelease(group);
|
|
859
|
+
return NULL;
|
|
860
|
+
}
|
|
861
|
+
g->group = group;
|
|
862
|
+
return g;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
void nishgfx_bind_group_release(NishGfxBindGroup *g) {
|
|
866
|
+
if (g == NULL) return;
|
|
867
|
+
wgpuBindGroupRelease(g->group);
|
|
868
|
+
free(g);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/* ---- encoding ---------------------------------------------------------- */
|
|
872
|
+
|
|
873
|
+
NishGfxEncoder *nishgfx_encoder_create(NishGfxDevice *dev) {
|
|
874
|
+
if (dev == NULL) return NULL;
|
|
875
|
+
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(dev->device, NULL);
|
|
876
|
+
if (encoder == NULL) return NULL;
|
|
877
|
+
NishGfxEncoder *e = calloc(1, sizeof *e);
|
|
878
|
+
if (e == NULL) {
|
|
879
|
+
wgpuCommandEncoderRelease(encoder);
|
|
880
|
+
return NULL;
|
|
881
|
+
}
|
|
882
|
+
e->encoder = encoder;
|
|
883
|
+
return e;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
void nishgfx_encoder_submit(NishGfxDevice *dev, NishGfxEncoder *e) {
|
|
887
|
+
if (e == NULL) return;
|
|
888
|
+
if (dev != NULL) {
|
|
889
|
+
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(e->encoder, NULL);
|
|
890
|
+
if (commands != NULL) {
|
|
891
|
+
wgpuQueueSubmit(dev->queue, 1, &commands);
|
|
892
|
+
wgpuCommandBufferRelease(commands);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
wgpuCommandEncoderRelease(e->encoder);
|
|
896
|
+
free(e);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
NishGfxPass *nishgfx_pass_begin(NishGfxEncoder *e, NishGfxView *view, uint32_t load, double r, double g,
|
|
900
|
+
double b, double a) {
|
|
901
|
+
if (e == NULL || view == NULL) return NULL;
|
|
902
|
+
WGPURenderPassColorAttachment color = WGPU_RENDER_PASS_COLOR_ATTACHMENT_INIT;
|
|
903
|
+
color.view = view->view;
|
|
904
|
+
color.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
|
905
|
+
color.loadOp = load ? WGPULoadOp_Load : WGPULoadOp_Clear;
|
|
906
|
+
color.storeOp = WGPUStoreOp_Store;
|
|
907
|
+
color.clearValue.r = r;
|
|
908
|
+
color.clearValue.g = g;
|
|
909
|
+
color.clearValue.b = b;
|
|
910
|
+
color.clearValue.a = a;
|
|
911
|
+
WGPURenderPassDescriptor desc = WGPU_RENDER_PASS_DESCRIPTOR_INIT;
|
|
912
|
+
desc.colorAttachmentCount = 1;
|
|
913
|
+
desc.colorAttachments = &color;
|
|
914
|
+
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(e->encoder, &desc);
|
|
915
|
+
if (pass == NULL) return NULL;
|
|
916
|
+
NishGfxPass *p = calloc(1, sizeof *p);
|
|
917
|
+
if (p == NULL) {
|
|
918
|
+
wgpuRenderPassEncoderEnd(pass);
|
|
919
|
+
wgpuRenderPassEncoderRelease(pass);
|
|
920
|
+
return NULL;
|
|
921
|
+
}
|
|
922
|
+
p->pass = pass;
|
|
923
|
+
return p;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
void nishgfx_pass_set_pipeline(NishGfxPass *p, NishGfxPipeline *pipeline) {
|
|
927
|
+
if (p != NULL && pipeline != NULL) wgpuRenderPassEncoderSetPipeline(p->pass, pipeline->pipeline);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/* A null group or buffer is ignored, not passed on: wgpu-native's handle
|
|
931
|
+
* conversion for either is not one this shim has seen report an error. */
|
|
932
|
+
void nishgfx_pass_set_bind_group(NishGfxPass *p, uint32_t index, NishGfxBindGroup *g) {
|
|
933
|
+
if (p != NULL && g != NULL && g->group != NULL) wgpuRenderPassEncoderSetBindGroup(p->pass, index, g->group, 0, NULL);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
void nishgfx_pass_set_vertex_buffer(NishGfxPass *p, uint32_t slot, NishGfxBuffer *b, uint64_t offset,
|
|
937
|
+
uint64_t size) {
|
|
938
|
+
if (p == NULL || b == NULL || b->buffer == NULL) return;
|
|
939
|
+
wgpuRenderPassEncoderSetVertexBuffer(p->pass, slot, b->buffer, offset,
|
|
940
|
+
size == 0 ? WGPU_WHOLE_SIZE : size);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
void nishgfx_pass_draw(NishGfxPass *p, uint32_t vertices, uint32_t instances, uint32_t first_vertex,
|
|
944
|
+
uint32_t first_instance) {
|
|
945
|
+
if (p != NULL) wgpuRenderPassEncoderDraw(p->pass, vertices, instances, first_vertex, first_instance);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
void nishgfx_pass_end(NishGfxPass *p) {
|
|
949
|
+
if (p == NULL) return;
|
|
950
|
+
wgpuRenderPassEncoderEnd(p->pass);
|
|
951
|
+
wgpuRenderPassEncoderRelease(p->pass);
|
|
952
|
+
free(p);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/* ---- offscreen --------------------------------------------------------- */
|
|
956
|
+
|
|
957
|
+
NishGfxOffscreen *nishgfx_offscreen_create(NishGfxDevice *dev, uint32_t width, uint32_t height) {
|
|
958
|
+
/* The shim's cap (WebGPU's default limit is 8192, a device reports past it):
|
|
959
|
+
* width * 4 rounded to 256 and its product with height fit 32 and 64 bits. */
|
|
960
|
+
if (dev == NULL || width == 0 || height == 0 || width > 16384 || height > 16384) return NULL;
|
|
961
|
+
NishGfxOffscreen *o = calloc(1, sizeof *o);
|
|
962
|
+
if (o == NULL) return NULL;
|
|
963
|
+
o->device = dev;
|
|
964
|
+
o->width = width;
|
|
965
|
+
o->height = height;
|
|
966
|
+
o->padded_row = (width * 4 + 255) / 256 * 256;
|
|
967
|
+
WGPUTextureDescriptor tex = WGPU_TEXTURE_DESCRIPTOR_INIT;
|
|
968
|
+
tex.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
|
|
969
|
+
tex.dimension = WGPUTextureDimension_2D;
|
|
970
|
+
tex.size.width = width;
|
|
971
|
+
tex.size.height = height;
|
|
972
|
+
tex.size.depthOrArrayLayers = 1;
|
|
973
|
+
tex.format = WGPUTextureFormat_RGBA8Unorm;
|
|
974
|
+
o->texture = wgpuDeviceCreateTexture(dev->device, &tex);
|
|
975
|
+
WGPUBufferDescriptor buf = WGPU_BUFFER_DESCRIPTOR_INIT;
|
|
976
|
+
buf.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_MapRead;
|
|
977
|
+
buf.size = (uint64_t)o->padded_row * height;
|
|
978
|
+
o->readback = wgpuDeviceCreateBuffer(dev->device, &buf);
|
|
979
|
+
o->view.view = o->texture != NULL ? wgpuTextureCreateView(o->texture, NULL) : NULL;
|
|
980
|
+
if (o->texture == NULL || o->readback == NULL || o->view.view == NULL) {
|
|
981
|
+
nishgfx_offscreen_release(o);
|
|
982
|
+
return NULL;
|
|
983
|
+
}
|
|
984
|
+
return o;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
void nishgfx_offscreen_release(NishGfxOffscreen *o) {
|
|
988
|
+
if (o == NULL) return;
|
|
989
|
+
if (o->view.view != NULL) wgpuTextureViewRelease(o->view.view);
|
|
990
|
+
if (o->texture != NULL) wgpuTextureRelease(o->texture);
|
|
991
|
+
if (o->readback != NULL) wgpuBufferRelease(o->readback);
|
|
992
|
+
free(o);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
NishGfxView *nishgfx_offscreen_view(NishGfxOffscreen *o) { return o == NULL ? NULL : &o->view; }
|
|
996
|
+
|
|
997
|
+
typedef struct MapAnswer {
|
|
998
|
+
int done;
|
|
999
|
+
WGPUMapAsyncStatus status;
|
|
1000
|
+
char message[NISHGFX_ERROR_MESSAGE_MAX + 1];
|
|
1001
|
+
} MapAnswer;
|
|
1002
|
+
|
|
1003
|
+
static void on_map(WGPUMapAsyncStatus status, WGPUStringView message, void *userdata1, void *userdata2) {
|
|
1004
|
+
(void)userdata2;
|
|
1005
|
+
MapAnswer *m = userdata1;
|
|
1006
|
+
m->status = status;
|
|
1007
|
+
copy_view(m->message, message);
|
|
1008
|
+
m->done = 1;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
int32_t nishgfx_offscreen_read(NishGfxOffscreen *o, void *dst, size_t cap) {
|
|
1012
|
+
if (o == NULL || dst == NULL) return -1;
|
|
1013
|
+
size_t row = (size_t)o->width * 4;
|
|
1014
|
+
size_t need = row * o->height;
|
|
1015
|
+
if (cap < need) return -1;
|
|
1016
|
+
NishGfxDevice *dev = o->device;
|
|
1017
|
+
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(dev->device, NULL);
|
|
1018
|
+
WGPUTexelCopyTextureInfo src = WGPU_TEXEL_COPY_TEXTURE_INFO_INIT;
|
|
1019
|
+
src.texture = o->texture;
|
|
1020
|
+
WGPUTexelCopyBufferInfo to = WGPU_TEXEL_COPY_BUFFER_INFO_INIT;
|
|
1021
|
+
to.buffer = o->readback;
|
|
1022
|
+
to.layout.bytesPerRow = o->padded_row;
|
|
1023
|
+
to.layout.rowsPerImage = o->height;
|
|
1024
|
+
WGPUExtent3D size = {o->width, o->height, 1};
|
|
1025
|
+
wgpuCommandEncoderCopyTextureToBuffer(encoder, &src, &to, &size);
|
|
1026
|
+
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, NULL);
|
|
1027
|
+
wgpuQueueSubmit(dev->queue, 1, &commands);
|
|
1028
|
+
wgpuCommandBufferRelease(commands);
|
|
1029
|
+
wgpuCommandEncoderRelease(encoder);
|
|
1030
|
+
|
|
1031
|
+
size_t mapped = (size_t)o->padded_row * o->height;
|
|
1032
|
+
MapAnswer answer;
|
|
1033
|
+
memset(&answer, 0, sizeof answer);
|
|
1034
|
+
WGPUBufferMapCallbackInfo info = WGPU_BUFFER_MAP_CALLBACK_INFO_INIT;
|
|
1035
|
+
info.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
1036
|
+
info.callback = on_map;
|
|
1037
|
+
info.userdata1 = &answer;
|
|
1038
|
+
wgpuBufferMapAsync(o->readback, WGPUMapMode_Read, 0, mapped, info);
|
|
1039
|
+
wgpuDevicePoll(dev->device, 1, NULL);
|
|
1040
|
+
for (int i = 0; !answer.done && i < NISHGFX_PROCESS_ROUNDS; i++) wgpuInstanceProcessEvents(dev->instance->instance);
|
|
1041
|
+
if (!answer.done) {
|
|
1042
|
+
ring_push(&dev->errors, NISHGFX_ERROR_INTERNAL, "the readback map was never answered", WGPU_STRLEN);
|
|
1043
|
+
abort(); /* as in nishgfx_adapter_request */
|
|
1044
|
+
}
|
|
1045
|
+
if (answer.status != WGPUMapAsyncStatus_Success) {
|
|
1046
|
+
ring_push(&dev->errors, NISHGFX_ERROR_INTERNAL,
|
|
1047
|
+
answer.message[0] != '\0' ? answer.message : "the readback map failed", WGPU_STRLEN);
|
|
1048
|
+
return -2;
|
|
1049
|
+
}
|
|
1050
|
+
const uint8_t *pixels = wgpuBufferGetConstMappedRange(o->readback, 0, mapped);
|
|
1051
|
+
if (pixels == NULL) {
|
|
1052
|
+
wgpuBufferUnmap(o->readback);
|
|
1053
|
+
ring_push(&dev->errors, NISHGFX_ERROR_INTERNAL, "the readback range is not mapped", WGPU_STRLEN);
|
|
1054
|
+
return -2;
|
|
1055
|
+
}
|
|
1056
|
+
/* Each copy reads row bytes at y * padded_row, which ends at most at
|
|
1057
|
+
* (height - 1) * padded_row + row <= mapped, and writes them at y * row,
|
|
1058
|
+
* which ends at need <= cap. */
|
|
1059
|
+
for (uint32_t y = 0; y < o->height; y++)
|
|
1060
|
+
memcpy((uint8_t *)dst + (size_t)y * row, pixels + (size_t)y * o->padded_row, row);
|
|
1061
|
+
wgpuBufferUnmap(o->readback);
|
|
1062
|
+
return 0;
|
|
1063
|
+
}
|