aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/interop.c
blob: 6697bb7e83039a0667c6064835e9776182208dec (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
/*
 * 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 "types.h"

//
//
//

#include "interop.h"
#include "context.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_fb
{
  cl_context context;

  GLuint     fbo;
  GLuint     rbo;

  cl_mem     mem;

  int        width;
  int        height;

  bool       is_srgb;
  bool       is_vsync_on;
  bool       is_fullscreen;
  bool       is_iconified;
  bool       is_resized;
  bool       is_spinning;
  bool       is_info;

  skc_float  scale;
  skc_float2 translate;
  float      rotate_theta;
};

static struct skc_interop_fb fb =
  {
    .mem           = NULL,

    .is_srgb       = true,
    .is_vsync_on   = false,
    .is_fullscreen = false,
    .is_iconified  = false,
    .is_resized    = true,
    .is_spinning   = false,
    .is_info       = false,

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

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

static
void
skc_interop_fps(GLFWwindow * window)
{
  if (fb.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;

      double const fps = (double)frame_count / elapsed;

      char tmp[64];

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

      glfwSetWindowTitle(window,tmp);

      frame_count = 0;
    }

  frame_count++;
}

//
// 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)
{
  fb.is_iconified = iconified;
}

//
//
//

static
void
skc_interop_key_callback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
  if (action == GLFW_RELEASE)
    return;

  switch (key)
    {
    case GLFW_KEY_EQUAL:
      fb.rotate_theta = 0.0f;
      break;

    case GLFW_KEY_I:
      fb.is_info = true;
      break;

    case GLFW_KEY_R:
      fb.is_spinning ^= true;
      break;

    case GLFW_KEY_S:
      fb.is_srgb ^= true;
      if (fb.is_srgb)
        glEnable(GL_FRAMEBUFFER_SRGB);
      else
        glDisable(GL_FRAMEBUFFER_SRGB);
      break;

    case GLFW_KEY_V:
      fb.is_vsync_on ^= true;
      glfwSwapInterval(fb.is_vsync_on ? 1 : 0);
      break;

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

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

static
void
skc_interop_window_size_callback(GLFWwindow * window, int width, int height)
{
  fb.width      = width;
  fb.height     = height;
  fb.is_resized = true;

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

static
void
skc_interop_scale(double const scale_offset)
{
#define SKC_SCALE_FACTOR 1.05

  static double scale_exp = 0.0;

  scale_exp += scale_offset;
  fb.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;

  skc_interop_scale(yoffset);
}

static
void
skc_interop_translate(float const dx, float const dy)
{
  float const dx_scaled = dx / fb.scale;
  float const dy_scaled = dy / fb.scale;

  float const cos_theta = cosf(fb.rotate_theta); // replace with cospi if available
  float const sin_theta = sinf(fb.rotate_theta); // replace with sinpi if available

  fb.translate.x += dx_scaled*cos_theta + dy_scaled*sin_theta;
  fb.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)
    {
      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 * fb.width;
              float const cy  = 0.5f * fb.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)
                    fb.rotate_theta += da;
                  else
                    fb.rotate_theta -= da;

                  fb.rotate_theta = fmodf(fb.rotate_theta,(float)(M_PI*2.0));
                }
            }
          else
            {
              skc_interop_translate(mx - x_prev,
                                    my - y_prev);
            }
        }
      else
        {
          is_mouse_dragging = true;
        }

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

//
//
//

static
void
skc_interop_resize()
{
  fb.is_resized = false;

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

  // resize rbo
  glNamedRenderbufferStorage(fb.rbo,
                             SKC_IMAGE_FORMAT,
                             fb.width,
                             fb.height);

  // attach rbo to fbo
  glNamedFramebufferRenderbuffer(fb.fbo,
                                 GL_COLOR_ATTACHMENT0,
                                 GL_RENDERBUFFER,
                                 fb.rbo);
  //
  //
  //
  cl_int cl_err;

  fb.mem = clCreateFromGLRenderbuffer(fb.context,
                                      CL_MEM_WRITE_ONLY,
                                      fb.rbo,
                                      &cl_err); cl_ok(cl_err);
  //
  // for debugging porpoises!
  //
  cl_image_format format;

  cl(GetImageInfo(fb.mem,
                  CL_IMAGE_FORMAT,
                  sizeof(format),
                  &format,
                  NULL));
}

//
//
//

static
void
skc_interop_acquire()
{
  // frame buffer object
  glCreateFramebuffers(1,&fb.fbo);

  // render buffer object w/a color buffer
  glCreateRenderbuffers(1,&fb.rbo);

  // size rbo
  glNamedRenderbufferStorage(fb.rbo,
                             SKC_IMAGE_FORMAT,
                             fb.width,
                             fb.height);

  // attach rbo to fbo
  glNamedFramebufferRenderbuffer(fb.fbo,
                                 GL_COLOR_ATTACHMENT0,
                                 GL_RENDERBUFFER,
                                 fb.rbo);
}

void
skc_interop_register(skc_context_t context)
{
  fb.context = context->runtime->cl.context;
}

//
//
//

void
skc_interop_init(GLFWwindow * * window)
{
  //
  // INITIALIZE GLFW/GLAD
  //
  glfwSetErrorCallback(skc_interop_error_callback);

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

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

  if (fb.is_fullscreen)
    {
      fb.width  = mode->width;
      fb.height = mode->height;
    }
  else
    {
      fb.width  = 1600;
      fb.height = 1024;
    }

  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);

  *window = glfwCreateWindow(fb.width,fb.height,
                             "Skia Compute",
                             fb.is_fullscreen ? primary : NULL,
                             NULL);

  if (*window == NULL)
    {
      glfwTerminate();
      exit(EXIT_FAILURE);
    }

  glfwMakeContextCurrent(*window);

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

  // ignore vsync for now
  glfwSwapInterval(fb.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            (*window,skc_interop_key_callback);
  glfwSetFramebufferSizeCallback(*window,skc_interop_window_size_callback);
  glfwSetScrollCallback         (*window,skc_interop_scroll_callback);
  glfwSetCursorPosCallback      (*window,skc_interop_cursor_position_callback);
  glfwSetWindowIconifyCallback  (*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();
}

//
//
//

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

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

  // spinner...
  if (fb.is_spinning)
    fb.rotate_theta = fmodf(fb.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,
                                           fb.rotate_theta,
                                           fb.scale,fb.scale,
                                           0.5f*fb.width,0.5f*fb.height);
  skc_transform_stack_concat(ts);

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


void
skc_interop_poll(GLFWwindow                 * window,
                 struct skc_transform_stack * ts)
{
  // wait until uniconified
  while (fb.is_iconified)
    {
      glfwWaitEvents();
      continue;
    }

  // what's happended?
  glfwPollEvents();

  // resize?
  if (fb.is_resized)
    skc_interop_resize();

  // monitor fps
  skc_interop_fps(window);

  skc_interop_transform(ts);
}

//
//
//

void
skc_interop_blit(GLFWwindow * window)
{
  // blit skc rbo
  glBlitNamedFramebuffer(fb.fbo,0,
                         0,0,fb.width,fb.height,
                         0,0,fb.width,fb.height,
                         GL_COLOR_BUFFER_BIT,
                         GL_NEAREST);

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

  // swap buffers
  glfwSwapBuffers(window);
}

//
//
//

void *
skc_interop_get_fb(GLFWwindow * window)
{
  glFlush();

  return fb.mem;
}

//
//
//

void
skc_interop_get_dim(uint32_t dim[2])
{
  dim[0] = fb.width;
  dim[1] = fb.height;
}

//
//
//