summaryrefslogtreecommitdiffstats
path: root/demos/window_glfw.c
blob: 610027894d7c2c5d25c1ff8172c8e05ee90cae50 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// License: CC0 / Public Domain

#if defined(USE_GL) + defined(USE_VK) + defined(USE_D3D11) != 1
#error Specify exactly one of -DUSE_GL, -DUSE_VK or -DUSE_D3D11 when compiling!
#endif

#include <string.h>
#include <math.h>

#include "common.h"
#include "window.h"

#ifdef USE_VK
#define VK_NO_PROTOTYPES
#include <libplacebo/vulkan.h>
#define GLFW_INCLUDE_VULKAN
#define IMPL win_impl_glfw_vk
#define IMPL_NAME "GLFW (vulkan)"
#define IMPL_TAG "glfw-vk"
#endif

#ifdef USE_GL
#include <libplacebo/opengl.h>
#define IMPL win_impl_glfw_gl
#define IMPL_NAME "GLFW (opengl)"
#define IMPL_TAG "glfw-gl"
#endif

#ifdef USE_D3D11
#include <libplacebo/d3d11.h>
#define IMPL win_impl_glfw_d3d11
#define IMPL_NAME "GLFW (D3D11)"
#define IMPL_TAG "glfw-d3d11"
#endif

#include <GLFW/glfw3.h>

#if defined(USE_GL) && defined(HAVE_EGL)
#define GLFW_EXPOSE_NATIVE_EGL
#include <GLFW/glfw3native.h>
#endif

#ifdef USE_D3D11
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#endif

#ifdef _WIN32
#define strdup _strdup
#endif

#ifdef NDEBUG
#define DEBUG false
#else
#define DEBUG true
#endif

#define PL_ARRAY_SIZE(s) (sizeof(s) / sizeof((s)[0]))

const struct window_impl IMPL;

struct window_pos {
    int x;
    int y;
    int w;
    int h;
};

struct priv {
    struct window w;
    GLFWwindow *win;

#ifdef USE_VK
    VkSurfaceKHR surf;
    pl_vulkan vk;
    pl_vk_inst vk_inst;
#endif

#ifdef USE_GL
    pl_opengl gl;
#endif

#ifdef USE_D3D11
    pl_d3d11 d3d11;
#endif

    float scroll_dx, scroll_dy;
    char **files;
    size_t files_num;
    size_t files_size;
    bool file_seen;

    struct window_pos windowed_pos;
};

static void err_cb(int code, const char *desc)
{
    fprintf(stderr, "GLFW err %d: %s\n", code, desc);
}

static void close_cb(GLFWwindow *win)
{
    struct priv *p = glfwGetWindowUserPointer(win);
    p->w.window_lost = true;
}

static void resize_cb(GLFWwindow *win, int width, int height)
{
    struct priv *p = glfwGetWindowUserPointer(win);
    if (!pl_swapchain_resize(p->w.swapchain, &width, &height)) {
        fprintf(stderr, "libplacebo: Failed resizing swapchain? Exiting...\n");
        p->w.window_lost = true;
    }
}

static void scroll_cb(GLFWwindow *win, double dx, double dy)
{
    struct priv *p = glfwGetWindowUserPointer(win);
    p->scroll_dx += dx;
    p->scroll_dy += dy;
}

static void drop_cb(GLFWwindow *win, int num, const char *files[])
{
    struct priv *p = glfwGetWindowUserPointer(win);

    for (int i = 0; i < num; i++) {
        if (p->files_num == p->files_size) {
            size_t new_size = p->files_size ? p->files_size * 2 : 16;
            char **new_files = realloc(p->files, new_size * sizeof(char *));
            if (!new_files)
                return;
            p->files = new_files;
            p->files_size = new_size;
        }

        char *file = strdup(files[i]);
        if (!file)
            return;

        p->files[p->files_num++] = file;
    }
}

#ifdef USE_GL
static bool make_current(void *priv)
{
    GLFWwindow *win = priv;
    glfwMakeContextCurrent(win);
    return true;
}

static void release_current(void *priv)
{
    glfwMakeContextCurrent(NULL);
}
#endif

#ifdef USE_VK
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_vk_proc_addr(VkInstance instance, const char* pName)
{
    return (PFN_vkVoidFunction) glfwGetInstanceProcAddress(instance, pName);
}
#endif

static struct window *glfw_create(pl_log log, const struct window_params *params)
{
    struct priv *p = calloc(1, sizeof(struct priv));
    if (!p)
        return NULL;

    p->w.impl = &IMPL;
    if (!glfwInit()) {
        fprintf(stderr, "GLFW: Failed initializing?\n");
        goto error;
    }

    glfwSetErrorCallback(&err_cb);

#ifdef USE_VK
    if (!glfwVulkanSupported()) {
        fprintf(stderr, "GLFW: No vulkan support! Perhaps recompile with -DUSE_GL\n");
        goto error;
    }

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
#endif // USE_VK

#ifdef USE_D3D11
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
#endif // USE_D3D11

#ifdef USE_GL
    struct {
        int api;
        int major, minor;
        int glsl_ver;
        int profile;
    } gl_vers[] = {
        { GLFW_OPENGL_API,    4, 6, 460, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_API,    4, 5, 450, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_API,    4, 4, 440, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_API,    4, 0, 400, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_API,    3, 3, 330, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_API,    3, 2, 150, GLFW_OPENGL_CORE_PROFILE },
        { GLFW_OPENGL_ES_API, 3, 2, 320, },
        { GLFW_OPENGL_API,    3, 1, 140, },
        { GLFW_OPENGL_ES_API, 3, 1, 310, },
        { GLFW_OPENGL_API,    3, 0, 130, },
        { GLFW_OPENGL_ES_API, 3, 0, 300, },
        { GLFW_OPENGL_ES_API, 2, 0, 100, },
        { GLFW_OPENGL_API,    2, 1, 120, },
        { GLFW_OPENGL_API,    2, 0, 110, },
    };

    for (int i = 0; i < PL_ARRAY_SIZE(gl_vers); i++) {
        glfwWindowHint(GLFW_CLIENT_API, gl_vers[i].api);
#ifdef HAVE_EGL
        glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#endif
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, gl_vers[i].major);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, gl_vers[i].minor);
        glfwWindowHint(GLFW_OPENGL_PROFILE, gl_vers[i].profile);
#ifdef __APPLE__
        if (gl_vers[i].profile == GLFW_OPENGL_CORE_PROFILE)
            glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

#endif // USE_GL

        if (params->alpha)
            glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);

        printf("Creating %dx%d window%s...\n", params->width, params->height,
               params->alpha ? " (with alpha)" : "");

        p->win = glfwCreateWindow(params->width, params->height, params->title, NULL, NULL);

#ifdef USE_GL
        if (p->win)
            break;
    }
#endif // USE_GL

    if (!p->win) {
        fprintf(stderr, "GLFW: Failed creating window\n");
        goto error;
    }

    // Set up GLFW event callbacks
    glfwSetWindowUserPointer(p->win, p);
    glfwSetFramebufferSizeCallback(p->win, resize_cb);
    glfwSetWindowCloseCallback(p->win, close_cb);
    glfwSetScrollCallback(p->win, scroll_cb);
    glfwSetDropCallback(p->win, drop_cb);

#ifdef USE_VK
    VkResult err;

    uint32_t num;
    p->vk_inst = pl_vk_inst_create(log, pl_vk_inst_params(
        .get_proc_addr = get_vk_proc_addr,
        .debug = DEBUG,
        .extensions = glfwGetRequiredInstanceExtensions(&num),
        .num_extensions = num,
    ));

    if (!p->vk_inst) {
        fprintf(stderr, "libplacebo: Failed creating vulkan instance\n");
        goto error;
    }

    err = glfwCreateWindowSurface(p->vk_inst->instance, p->win, NULL, &p->surf);
    if (err != VK_SUCCESS) {
        fprintf(stderr, "GLFW: Failed creating vulkan surface\n");
        goto error;
    }

    p->vk = pl_vulkan_create(log, pl_vulkan_params(
        .instance = p->vk_inst->instance,
        .get_proc_addr = p->vk_inst->get_proc_addr,
        .surface = p->surf,
        .allow_software = true,
    ));
    if (!p->vk) {
        fprintf(stderr, "libplacebo: Failed creating vulkan device\n");
        goto error;
    }

    p->w.swapchain = pl_vulkan_create_swapchain(p->vk, pl_vulkan_swapchain_params(
        .surface = p->surf,
        .present_mode = VK_PRESENT_MODE_FIFO_KHR,
    ));

    if (!p->w.swapchain) {
        fprintf(stderr, "libplacebo: Failed creating vulkan swapchain\n");
        goto error;
    }

    p->w.gpu = p->vk->gpu;
#endif // USE_VK

#ifdef USE_GL
    p->gl = pl_opengl_create(log, pl_opengl_params(
        .allow_software = true,
        .debug = DEBUG,
#ifdef HAVE_EGL
        .egl_display = glfwGetEGLDisplay(),
        .egl_context = glfwGetEGLContext(p->win),
#endif
        .make_current = make_current,
        .release_current = release_current,
        .get_proc_addr = glfwGetProcAddress,
        .priv = p->win,
    ));
    if (!p->gl) {
        fprintf(stderr, "libplacebo: Failed creating opengl device\n");
        goto error;
    }

    p->w.swapchain = pl_opengl_create_swapchain(p->gl, pl_opengl_swapchain_params(
        .swap_buffers = (void (*)(void *)) glfwSwapBuffers,
        .priv = p->win,
    ));

    if (!p->w.swapchain) {
        fprintf(stderr, "libplacebo: Failed creating opengl swapchain\n");
        goto error;
    }

    p->w.gpu = p->gl->gpu;
#endif // USE_GL

#ifdef USE_D3D11
    p->d3d11 = pl_d3d11_create(log, pl_d3d11_params( .debug = DEBUG ));
    if (!p->d3d11) {
        fprintf(stderr, "libplacebo: Failed creating D3D11 device\n");
        goto error;
    }

    p->w.swapchain = pl_d3d11_create_swapchain(p->d3d11, pl_d3d11_swapchain_params(
        .window = glfwGetWin32Window(p->win),
    ));
    if (!p->w.swapchain) {
        fprintf(stderr, "libplacebo: Failed creating D3D11 swapchain\n");
        goto error;
    }

    p->w.gpu = p->d3d11->gpu;
#endif // USE_D3D11

    glfwGetWindowSize(p->win, &p->windowed_pos.w, &p->windowed_pos.h);
    glfwGetWindowPos(p->win, &p->windowed_pos.x, &p->windowed_pos.y);

    int w, h;
    glfwGetFramebufferSize(p->win, &w, &h);
    pl_swapchain_colorspace_hint(p->w.swapchain, &params->colors);
    if (!pl_swapchain_resize(p->w.swapchain, &w, &h)) {
        fprintf(stderr, "libplacebo: Failed initializing swapchain\n");
        goto error;
    }

    return &p->w;

error:
    window_destroy((struct window **) &p);
    return NULL;
}

static void glfw_destroy(struct window **window)
{
    struct priv *p = (struct priv *) *window;
    if (!p)
        return;

    pl_swapchain_destroy(&p->w.swapchain);

#ifdef USE_VK
    pl_vulkan_destroy(&p->vk);
    if (p->surf) {
        PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)
            p->vk_inst->get_proc_addr(p->vk_inst->instance, "vkDestroySurfaceKHR");
        vkDestroySurfaceKHR(p->vk_inst->instance, p->surf, NULL);
    }
    pl_vk_inst_destroy(&p->vk_inst);
#endif

#ifdef USE_GL
    pl_opengl_destroy(&p->gl);
#endif

#ifdef USE_D3D11
    pl_d3d11_destroy(&p->d3d11);
#endif

    for (int i = 0; i < p->files_num; i++)
        free(p->files[i]);
    free(p->files);

    glfwTerminate();
    free(p);
    *window = NULL;
}

static void glfw_poll(struct window *window, bool block)
{
    if (block) {
        glfwWaitEvents();
    } else {
        glfwPollEvents();
    }
}

static void glfw_get_cursor(const struct window *window, int *x, int *y)
{
    struct priv *p = (struct priv *) window;
    double dx, dy;
    int fw, fh, ww, wh;
    glfwGetCursorPos(p->win, &dx, &dy);
    glfwGetFramebufferSize(p->win, &fw, &fh);
    glfwGetWindowSize(p->win, &ww, &wh);
    *x = floor(dx * fw / ww);
    *y = floor(dy * fh / wh);
}

static bool glfw_get_button(const struct window *window, enum button btn)
{
    static const int button_map[] = {
        [BTN_LEFT] = GLFW_MOUSE_BUTTON_LEFT,
        [BTN_RIGHT] = GLFW_MOUSE_BUTTON_RIGHT,
        [BTN_MIDDLE] = GLFW_MOUSE_BUTTON_MIDDLE,
    };

    struct priv *p = (struct priv *) window;
    return glfwGetMouseButton(p->win, button_map[btn]) == GLFW_PRESS;
}

static bool glfw_get_key(const struct window *window, enum key key)
{
    static const int key_map[] = {
        [KEY_ESC] = GLFW_KEY_ESCAPE,
    };

    struct priv *p = (struct priv *) window;
    return glfwGetKey(p->win, key_map[key]) == GLFW_PRESS;
}

static void glfw_get_scroll(const struct window *window, float *dx, float *dy)
{
    struct priv *p = (struct priv *) window;
    *dx = p->scroll_dx;
    *dy = p->scroll_dy;
    p->scroll_dx = p->scroll_dy = 0.0;
}

static char *glfw_get_file(const struct window *window)
{
    struct priv *p = (struct priv *) window;
    if (p->file_seen) {
        assert(p->files_num);
        free(p->files[0]);
        memmove(&p->files[0], &p->files[1], --p->files_num * sizeof(char *));
        p->file_seen = false;
    }

    if (!p->files_num)
        return NULL;

    p->file_seen = true;
    return p->files[0];
}

static bool glfw_is_fullscreen(const struct window *window) {
    const struct priv *p = (const struct priv *) window;
    return glfwGetWindowMonitor(p->win);
}

static bool glfw_toggle_fullscreen(const struct window *window, bool fullscreen)
{
    struct priv *p = (struct priv *) window;
    bool window_fullscreen = glfw_is_fullscreen(window);

    if (window_fullscreen == fullscreen)
        return true;

    if (window_fullscreen) {
        glfwSetWindowMonitor(p->win, NULL, p->windowed_pos.x, p->windowed_pos.y,
                             p->windowed_pos.w, p->windowed_pos.h, GLFW_DONT_CARE);
        return true;
    }

    // For simplicity sake use primary monitor
    GLFWmonitor *monitor = glfwGetPrimaryMonitor();
    if (!monitor)
        return false;

    const GLFWvidmode *mode = glfwGetVideoMode(monitor);
    if (!mode)
        return false;

    glfwGetWindowPos(p->win, &p->windowed_pos.x, &p->windowed_pos.y);
    glfwGetWindowSize(p->win, &p->windowed_pos.w, &p->windowed_pos.h);
    glfwSetWindowMonitor(p->win, monitor, 0, 0, mode->width, mode->height,
                         mode->refreshRate);

    return true;
}

static const char *glfw_get_clipboard(const struct window *window)
{
    struct priv *p = (struct priv *) window;
    return glfwGetClipboardString(p->win);
}

static void glfw_set_clipboard(const struct window *window, const char *text)
{
    struct priv *p = (struct priv *) window;
    glfwSetClipboardString(p->win, text);
}

const struct window_impl IMPL = {
    .name = IMPL_NAME,
    .tag = IMPL_TAG,
    .create = glfw_create,
    .destroy = glfw_destroy,
    .poll = glfw_poll,
    .get_cursor = glfw_get_cursor,
    .get_button = glfw_get_button,
    .get_key = glfw_get_key,
    .get_scroll = glfw_get_scroll,
    .get_file = glfw_get_file,
    .toggle_fullscreen = glfw_toggle_fullscreen,
    .is_fullscreen = glfw_is_fullscreen,
    .get_clipboard = glfw_get_clipboard,
    .set_clipboard = glfw_set_clipboard,
};