aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/platforms/cl_12/interop/interop_glfw.c
blob: 8f94100552167f954b93880b9554962f1487480d (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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */

//
//
//

#include <glad/glad.h>
#include <glfw/glfw3.h>

//
//
//

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

//
//
//

#include "common/cl/assert_cl.h"

//
//
//

#include "interop.h"

//
//
//

#include "skc_cl.h"
#include "runtime_cl_12.h"

//
//
//

#include "svg2skc/transform_stack.h"

//
//
//

#if 1
#define SKC_IMAGE_FORMAT GL_RGBA8
#else
#define SKC_IMAGE_FORMAT GL_RGBA16F
#endif

//
//
//

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

//
//
//

struct skc_interop
{
  GLFWwindow              * window;

  cl_context                context_cl;

  GLuint                    fbo;
  GLuint                    rbo;

  struct skc_framebuffer_cl fb;

  int                       width;
  int                       height;

  bool                      is_msecs;
  bool                      is_srgb;
  bool                      is_vsync_on;
  bool                      is_fullscreen;
  bool                      is_iconified;
  bool                      is_resized;
  bool                      is_spinning;
  bool                      is_transform;

  skc_float                 scale;
  skc_float2                translate;
  float                     rotate_theta;

  int                       key;
};

//
// INITIALIZE GLFW/GLAD
//

static
void
skc_interop_error_callback(int error, char const * description)
{
  fputs(description,stderr);
}

//
//
//

static
void
skc_interop_iconify_callback(GLFWwindow * window, int iconified)
{
  struct skc_interop * interop = glfwGetWindowUserPointer(window);

  interop->is_iconified = iconified;
}

//
//
//

static
void
skc_interop_key_callback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
  struct skc_interop * interop = glfwGetWindowUserPointer(window);

  if (action == GLFW_RELEASE)
    return;

  switch (key)
    {
    case GLFW_KEY_EQUAL:
      interop->rotate_theta = 0.0f;
      interop->is_transform = true;
      break;

    case GLFW_KEY_M:
      interop->is_msecs ^= true;
      break;

    case GLFW_KEY_R:
      interop->is_spinning ^= true;
      break;

    case GLFW_KEY_S:
      interop->is_srgb ^= true;
      if (interop->is_srgb)
        glEnable(GL_FRAMEBUFFER_SRGB);
      else
        glDisable(GL_FRAMEBUFFER_SRGB);
      break;

    case GLFW_KEY_V:
      interop->is_vsync_on ^= true;
      glfwSwapInterval(interop->is_vsync_on ? 1 : 0);
      break;

    case GLFW_KEY_W:
      glfwSetWindowSize(window,1024,1024);
      break;

    case GLFW_KEY_ESCAPE:
      glfwSetWindowShouldClose(window,GL_TRUE);
      break;

    default:
      interop->key = key;
    }
}

static
void
skc_interop_window_size_callback(GLFWwindow * window, int width, int height)
{
  struct skc_interop * interop = glfwGetWindowUserPointer(window);

  interop->width        = width;
  interop->height       = height;
  interop->is_resized   = true;
  interop->is_transform = true;

#if 0
  skc_render_kernel_set_clip(0,0,width,height);
#endif
}

static
void
skc_interop_scale(struct skc_interop * interop, double const scale_offset)
{
#define SKC_SCALE_FACTOR 1.05

  static double scale_exp = 0.0;

  scale_exp += scale_offset;

  interop->scale = (float)pow(SKC_SCALE_FACTOR,scale_exp);
}

static
void
skc_interop_scroll_callback(GLFWwindow * window, double xoffset, double yoffset)
{
  bool const ctrl =
    (glfwGetKey(window,GLFW_KEY_LEFT_CONTROL)  == GLFW_PRESS) ||
    (glfwGetKey(window,GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS);

  if (!ctrl)
    return;

  struct skc_interop * interop = glfwGetWindowUserPointer(window);

  skc_interop_scale(interop,yoffset);

  interop->is_transform = true;
}

static
void
skc_interop_translate(struct skc_interop * interop, float const dx, float const dy)
{
  float const dx_scaled = dx / interop->scale;
  float const dy_scaled = dy / interop->scale;

  float const cos_theta = cosf(interop->rotate_theta); // replace with cospi if available
  float const sin_theta = sinf(interop->rotate_theta); // replace with sinpi if available

  interop->translate.x += dx_scaled*cos_theta + dy_scaled*sin_theta;
  interop->translate.y += dy_scaled*cos_theta - dx_scaled*sin_theta;
}

static
void
skc_interop_cursor_position_callback(GLFWwindow * window, double x, double y)
{

  int const state = glfwGetMouseButton(window,GLFW_MOUSE_BUTTON_LEFT);

  static bool  is_mouse_dragging = false;
  static float x_prev=0.0, y_prev=0.0;

  float const mx = (float)x;
  float const my = (float)y;

  if (state == GLFW_PRESS)
    {
      struct skc_interop * interop = glfwGetWindowUserPointer(window);

      if (is_mouse_dragging)
        {
          const bool ctrl =
            (glfwGetKey(window,GLFW_KEY_LEFT_CONTROL)  == GLFW_PRESS) ||
            (glfwGetKey(window,GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS);

          if (ctrl)
            {
              float const cx  = 0.5f * interop->width;
              float const cy  = 0.5f * interop->height;

              // find angle between mouse and center
              float const vx  = x_prev - cx;
              float const vy  = y_prev - cy;

              float const wx  = mx - cx;
              float const wy  = my - cy;

              float const len = sqrtf((vx*vx + vy*vy) * (wx*wx + wy*wy));

              if (len > 0.0f)
                {
                  float const dot = vx*wx + vy*wy;
                  float const da  = acosf(dot / len);

                  if (vx*wy - vy*wx >= 0.0f)
                    interop->rotate_theta += da;
                  else
                    interop->rotate_theta -= da;

                  interop->rotate_theta = fmodf(interop->rotate_theta,(float)(M_PI*2.0));
                }
            }
          else
            {
              skc_interop_translate(interop,
                                    mx - x_prev,
                                    my - y_prev);
            }

          interop->is_transform = true;
        }
      else
        {
          is_mouse_dragging = true;
        }

      x_prev = mx;
      y_prev = my;
    }
  else
    {
      is_mouse_dragging = false;
    }
}

//
//
//

static
void
skc_interop_acquire(struct skc_interop * interop)
{
  // frame buffer object
  glCreateFramebuffers(1,&interop->fbo);

  // render buffer object w/a color buffer
  glCreateRenderbuffers(1,&interop->rbo);

  // size rbo
  glNamedRenderbufferStorage(interop->rbo,
                             SKC_IMAGE_FORMAT,
                             interop->width,
                             interop->height);

  // attach rbo to fbo
  glNamedFramebufferRenderbuffer(interop->fbo,
                                 GL_COLOR_ATTACHMENT0,
                                 GL_RENDERBUFFER,
                                 interop->rbo);
}

//
//
//

struct skc_interop *
skc_interop_create()
{
  struct skc_interop * interop = malloc(sizeof(*interop));

  *interop = (struct skc_interop)
    {
     .fb            = { .type        = SKC_FRAMEBUFFER_CL_GL_RENDERBUFFER,
                        .mem         = NULL,
                        .interop     = interop,
                        .post_render = skc_interop_blit },

     .is_msecs      = true,
     .is_srgb       = true,
     .is_vsync_on   = false,
     .is_fullscreen = false,
     .is_iconified  = false,
     .is_resized    = true,
     .is_spinning   = false,
     .is_transform  = true,

     .scale         = 1.0f,
     .translate     = { 0.0f, 0.0f },
     .rotate_theta  = 0.0f,

     .key           = 0
    };

  //
  // INITIALIZE GLFW/GLAD
  //
  glfwSetErrorCallback(skc_interop_error_callback);

  if (!glfwInit())
    exit(EXIT_FAILURE);

  GLFWmonitor       * const primary = glfwGetPrimaryMonitor();
  GLFWvidmode const * const mode    = glfwGetVideoMode(primary);

  if (interop->is_fullscreen)
    {
      interop->width  = mode->width;
      interop->height = mode->height;
    }
  else
    {
      interop->width  = 1600;
      interop->height = 1600;
    }

  glfwWindowHint(GLFW_ALPHA_BITS,            0);
  glfwWindowHint(GLFW_DEPTH_BITS,            0);
  glfwWindowHint(GLFW_STENCIL_BITS,          0);

  glfwWindowHint(GLFW_SRGB_CAPABLE,          GL_TRUE);

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);

  glfwWindowHint(GLFW_OPENGL_PROFILE,        GLFW_OPENGL_CORE_PROFILE);

  interop->window = glfwCreateWindow(interop->width,
                                     interop->height,
                                     "Skia Compute",
                                     interop->is_fullscreen ? primary : NULL,
                                     NULL);

  if (interop->window == NULL)
    {
      glfwTerminate();
      exit(EXIT_FAILURE);
    }

  // save back pointer
  glfwSetWindowUserPointer(interop->window,interop);

  glfwMakeContextCurrent(interop->window);

  // set up GLAD
  gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

  // ignore vsync for now
  glfwSwapInterval(interop->is_vsync_on ? 1 : 0);

  // only copy r/g/b
  glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_FALSE);

  // enable SRGB, disable scissor
  glEnable(GL_FRAMEBUFFER_SRGB);
  glDisable(GL_SCISSOR_TEST);

  //
  // SET USER POINTER AND CALLBACKS
  //
  glfwSetKeyCallback            (interop->window,skc_interop_key_callback);
  glfwSetFramebufferSizeCallback(interop->window,skc_interop_window_size_callback);
  glfwSetScrollCallback         (interop->window,skc_interop_scroll_callback);
  glfwSetCursorPosCallback      (interop->window,skc_interop_cursor_position_callback);
  glfwSetWindowIconifyCallback  (interop->window,skc_interop_iconify_callback);

  //
  //
  //
  fprintf(stderr,
          "GL_VENDOR   : %s\n"
          "GL_RENDERER : %s\n",
          glGetString(GL_VENDOR),
          glGetString(GL_RENDERER));

  //
  // acquire an FBO/RBO
  //
  skc_interop_acquire(interop);

  return interop;
}

//
//
//

void
skc_interop_destroy(struct skc_interop * interop)
{
  glfwDestroyWindow(interop->window);
  glfwTerminate();

  free(interop);
}

//
//
//

void
skc_interop_set_cl_context(struct skc_interop * interop,
                           cl_context           context_cl)
{
  interop->context_cl = context_cl;
}

//
//
//

cl_context_properties
skc_interop_get_wgl_context()
{
  return (cl_context_properties)wglGetCurrentContext();
}

cl_context_properties
skc_interop_get_wgl_dc()
{
  return (cl_context_properties)wglGetCurrentDC();
}

//
//
//

#define SKC_ROTATE_STEP ((float)(M_PI / 180.0))

void
skc_interop_transform(struct skc_interop         * interop,
                      struct skc_transform_stack * ts)
{
  // OpenGL'ism
  skc_transform_stack_push_affine(ts,
                                  1.0f, 0.0f,0.0f,
                                  0.0f,-1.0f,(float)interop->height);
  // multiply
  skc_transform_stack_concat(ts);

  // spinner...
  if (interop->is_spinning)
    interop->rotate_theta = fmodf(interop->rotate_theta + SKC_ROTATE_STEP,(float)(M_PI*2.0));

  // always rotate and scale around surface center point
  skc_transform_stack_push_rotate_scale_xy(ts,
                                           interop->rotate_theta,
                                           interop->scale,
                                           interop->scale,
                                           0.5f*interop->width,
                                           0.5f*interop->height);
  skc_transform_stack_concat(ts);

  // where did the mouse take us?
  skc_transform_stack_push_translate(ts,
                                     interop->translate.x,
                                     interop->translate.y);
  skc_transform_stack_concat(ts);
}

//
//
//

static
void
skc_interop_resize(struct skc_interop * interop)
{
  interop->is_resized = false;

  // release the image2d
  if (interop->fb.mem != NULL)
    cl(ReleaseMemObject(interop->fb.mem));

  // resize rbo
  glNamedRenderbufferStorage(interop->rbo,
                             SKC_IMAGE_FORMAT,
                             interop->width,
                             interop->height);

  // attach rbo to fbo
  glNamedFramebufferRenderbuffer(interop->fbo,
                                 GL_COLOR_ATTACHMENT0,
                                 GL_RENDERBUFFER,
                                 interop->rbo);
  //
  //
  //
  cl_int cl_err;

  interop->fb.mem = clCreateFromGLRenderbuffer(interop->context_cl,
                                               CL_MEM_WRITE_ONLY,
                                               interop->rbo,
                                               &cl_err); cl_ok(cl_err);
  //
  // for debugging porpoises!
  //
#if 0
  cl_image_format format;

  cl(GetImageInfo(interop->fb.mem,
                  CL_IMAGE_FORMAT,
                  sizeof(format),
                  &format,
                  NULL));
#endif
}

//
// FPS COUNTER FROM HERE:
//
// http://antongerdelan.net/opengl/glcontext2.html
//

static
void
skc_interop_fps(struct skc_interop * interop)
{
  if (interop->is_fullscreen)
    return;

  // static fps counters
  static double stamp_prev  = 0.0;
  static int    frame_count = 0;

  // locals
  double const  stamp_curr  = glfwGetTime();
  double const  elapsed     = stamp_curr - stamp_prev;

  if (elapsed >= 0.5)
    {
      stamp_prev = stamp_curr;

      char tmp[64];

      if (interop->is_msecs)
        {
          double const msecs = min(elapsed * 1000 / frame_count,9999.9);

          sprintf_s(tmp,64,"%5.1f MSECS - (%d x %d) - VSync %s - sRGB %s",
                    msecs,
                    interop->width,interop->height,
                    interop->is_vsync_on ? "ON"      : "OFF",
                    interop->is_srgb     ? "ENABLED" : "DISABLED");
        }
      else
        {
          double const fps = min((double)frame_count / elapsed,9999.9);

          sprintf_s(tmp,64,"%5.1f FPS - (%d x %d) - VSync %s - sRGB %s",
                    fps,
                    interop->width,interop->height,
                    interop->is_vsync_on ? "ON"      : "OFF",
                    interop->is_srgb     ? "ENABLED" : "DISABLED");
        }

      glfwSetWindowTitle(interop->window,tmp);

      frame_count = 0;
    }

  frame_count++;
}

//
//
//

bool
skc_interop_poll(struct skc_interop * interop, int * key)
{
  // wait until uniconified
  while (interop->is_iconified)
    {
      glfwWaitEvents();
      continue;
    }

  // what's happended?
  glfwPollEvents();

  // resize?
  if (interop->is_resized)
    skc_interop_resize(interop);

  // monitor fps
  skc_interop_fps(interop);

  if (key != NULL)
    {
      *key         = interop->key;
      interop->key = 0;
    }

  bool const is_transform = interop->is_transform || interop->is_spinning;

  interop->is_transform = false;

  return is_transform;
}

//
//
//

void
skc_interop_blit(struct skc_interop * interop)
{
  // blit skc rbo
  glBlitNamedFramebuffer(interop->fbo,0,
                         0,0,interop->width,interop->height,
                         0,0,interop->width,interop->height,
                         GL_COLOR_BUFFER_BIT,
                         GL_NEAREST);

  // swap buffers
  glfwSwapBuffers(interop->window);

#if 0
  //
  // FIXME -- this clear does nothing!
  //
  // As a hack we're clearing the interop'd RBO with a
  // clEnqueueFillImage().
  //
  GLenum const attachments[] = { GL_COLOR_ATTACHMENT0 };
  glInvalidateNamedFramebufferData(interop->fbo,1,attachments);
  float  const rgba[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  glClearNamedFramebufferfv(interop->fbo,GL_COLOR,0,rgba);
#endif
}

//
//
//

skc_framebuffer_t
skc_interop_get_framebuffer(struct skc_interop * interop)
{
  // glFlush();
  glFinish();

  return &interop->fb;
}

//
//
//

bool
skc_interop_should_exit(struct skc_interop * interop)
{
  return glfwWindowShouldClose(interop->window);
}

//
//
//

void
skc_interop_get_size(struct skc_interop * interop,
                     uint32_t           * width,
                     uint32_t           * height)
{
  *width  = interop->width;
  *height = interop->height;
}

//
//
//