summaryrefslogtreecommitdiff
path: root/gsk/ngl/gskngluniformstateprivate.h
blob: 6185087a6898b6b9c2e67b0d5db66beff987dfb6 (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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* gskngluniformstateprivate.h
 *
 * Copyright 2020 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifndef GSK_NGL_UNIFORM_STATE_PRIVATE_H
#define GSK_NGL_UNIFORM_STATE_PRIVATE_H

#include "gskngltypesprivate.h"

G_BEGIN_DECLS

typedef struct { float v0; } Uniform1f;
typedef struct { float v0; float v1; } Uniform2f;
typedef struct { float v0; float v1; float v2; } Uniform3f;
typedef struct { float v0; float v1; float v2; float v3; } Uniform4f;

typedef struct { int v0; } Uniform1i;
typedef struct { int v0; int v1; } Uniform2i;
typedef struct { int v0; int v1; int v2; } Uniform3i;
typedef struct { int v0; int v1; int v2; int v3; } Uniform4i;

typedef struct { guint v0; } Uniform1ui;

#define GSK_NGL_UNIFORM_ARRAY_BITS  5
#define GSK_NGL_UNIFORM_FORMAT_BITS 5
#define GSK_NGL_UNIFORM_OFFSET_BITS 21

typedef struct _GskNglUniformInfo
{
  guint initial     : 1;
  guint format      : GSK_NGL_UNIFORM_FORMAT_BITS;
  guint array_count : GSK_NGL_UNIFORM_ARRAY_BITS;
  guint offset      : GSK_NGL_UNIFORM_OFFSET_BITS;
} GskNglUniformInfo;

G_STATIC_ASSERT (sizeof (GskNglUniformInfo) == 4);

typedef struct _GskNglUniformInfoElement
{
  GskNglUniformInfo info;
  guint stamp;
} GskNglUniformInfoElement;

G_STATIC_ASSERT (sizeof (GskNglUniformInfoElement) == 8);

typedef struct _GskNglUniformProgram
{
  guint program_id;
  guint n_uniforms : 12;
  guint has_attachments : 1;

  /* To avoid walking our 1:1 array of location->uniform slots, we have
   * a sparse index that allows us to skip the empty zones.
   */
  guint *sparse;
  guint n_sparse;

  /* Uniforms are provided inline at the end of structure to avoid
   * an extra dereference.
   */
  GskNglUniformInfoElement uniforms[0];
} GskNglUniformProgram;

typedef struct _GskNglUniformState
{
  GHashTable *programs;
  guint8 *values_buf;
  guint values_pos;
  guint values_len;
  GskNglUniformInfo apply_hash[512];
} GskNglUniformState;

/**
 * GskNglUniformStateCallback:
 * @info: a pointer to the information about the uniform
 * @location: the location of the uniform within the GPU program.
 * @user_data: closure data for the callback
 *
 * This callback can be used to snapshot state of a program which
 * is useful when batching commands so that the state may be compared
 * with future evocations of the program.
 */
typedef void (*GskNglUniformStateCallback) (const GskNglUniformInfo *info,
                                            guint                    location,
                                            gpointer                 user_data);

typedef enum _GskNglUniformKind
{
  GSK_NGL_UNIFORM_FORMAT_1F = 1,
  GSK_NGL_UNIFORM_FORMAT_2F,
  GSK_NGL_UNIFORM_FORMAT_3F,
  GSK_NGL_UNIFORM_FORMAT_4F,

  GSK_NGL_UNIFORM_FORMAT_1FV,
  GSK_NGL_UNIFORM_FORMAT_2FV,
  GSK_NGL_UNIFORM_FORMAT_3FV,
  GSK_NGL_UNIFORM_FORMAT_4FV,

  GSK_NGL_UNIFORM_FORMAT_1I,
  GSK_NGL_UNIFORM_FORMAT_2I,
  GSK_NGL_UNIFORM_FORMAT_3I,
  GSK_NGL_UNIFORM_FORMAT_4I,

  GSK_NGL_UNIFORM_FORMAT_1UI,

  GSK_NGL_UNIFORM_FORMAT_TEXTURE,

  GSK_NGL_UNIFORM_FORMAT_MATRIX,
  GSK_NGL_UNIFORM_FORMAT_ROUNDED_RECT,
  GSK_NGL_UNIFORM_FORMAT_COLOR,

  GSK_NGL_UNIFORM_FORMAT_LAST
} GskNglUniformFormat;

G_STATIC_ASSERT (GSK_NGL_UNIFORM_FORMAT_LAST < (1 << GSK_NGL_UNIFORM_FORMAT_BITS));

GskNglUniformState   *gsk_ngl_uniform_state_new         (void);
GskNglUniformState   *gsk_ngl_uniform_state_ref         (GskNglUniformState        *state);
void                  gsk_ngl_uniform_state_unref       (GskNglUniformState        *state);
GskNglUniformProgram *gsk_ngl_uniform_state_get_program (GskNglUniformState        *state,
                                                         guint                      program,
                                                         guint                      n_uniforms);
void                  gsk_ngl_uniform_state_end_frame   (GskNglUniformState        *state);
gsize                 gsk_ngl_uniform_format_size       (GskNglUniformFormat        format);
gpointer              gsk_ngl_uniform_state_init_value  (GskNglUniformState        *state,
                                                         GskNglUniformProgram      *program,
                                                         GskNglUniformFormat        format,
                                                         guint                      array_count,
                                                         guint                      location,
                                                         GskNglUniformInfoElement **infoptr);

#define GSK_NGL_UNIFORM_VALUE(base, offset) ((gpointer)((base) + ((offset) * 4)))
#define gsk_ngl_uniform_state_get_uniform_data(state,offset) GSK_NGL_UNIFORM_VALUE((state)->values_buf, offset)
#define gsk_ngl_uniform_state_snapshot(state, program_info, callback, user_data) \
  G_STMT_START {                                                                 \
    for (guint z = 0; z < program_info->n_sparse; z++)                           \
      {                                                                          \
        guint location = program_info->sparse[z];                                \
        GskNglUniformInfoElement *info = &program_info->uniforms[location];      \
                                                                                 \
        g_assert (location < GL_MAX_UNIFORM_LOCATIONS);                          \
        g_assert (location < program_info->n_uniforms);                          \
                                                                                 \
        if (info->info.format > 0)                                               \
          callback (&info->info, location, user_data);                           \
      }                                                                          \
  } G_STMT_END

static inline gpointer
gsk_ngl_uniform_state_get_value (GskNglUniformState        *state,
                                 GskNglUniformProgram      *program,
                                 GskNglUniformFormat        format,
                                 guint                      array_count,
                                 guint                      location,
                                 guint                      stamp,
                                 GskNglUniformInfoElement **infoptr)
{
  GskNglUniformInfoElement *info;

  if (location == (guint)-1)
    return NULL;

  /* If the stamp is the same, then we can ignore the request
   * and short-circuit as early as possible. This requires the
   * caller to increment their private stamp when they change
   * internal state.
   *
   * This is generally used for the shared uniforms like projection,
   * modelview, clip, etc to avoid so many comparisons which cost
   * considerable CPU.
   */
  info = &program->uniforms[location];
  if (stamp != 0 && stamp == info->stamp)
    return NULL;

  if G_LIKELY (format == info->info.format && array_count <= info->info.array_count)
    {
      *infoptr = info;
      return GSK_NGL_UNIFORM_VALUE (state->values_buf, info->info.offset);
    }

  return gsk_ngl_uniform_state_init_value (state, program, format, array_count, location, infoptr);
}

static inline guint
gsk_ngl_uniform_state_align (guint current_pos,
                             guint size)
{
  guint align = size > 8 ? 16 : (size > 4 ? 8 : 4);
  guint masked = current_pos & (align - 1);

  g_assert (size > 0);
  g_assert (align == 4 || align == 8 || align == 16);
  g_assert (masked < align);

  return align - masked;
}

static inline gpointer
gsk_ngl_uniform_state_realloc (GskNglUniformState *state,
                               guint               size,
                               guint              *offset)
{
  guint padding = gsk_ngl_uniform_state_align (state->values_pos, size);

  if G_UNLIKELY (state->values_len - padding - size < state->values_pos)
    {
      state->values_len *= 2;
      state->values_buf = g_realloc (state->values_buf, state->values_len);
    }

  /* offsets are in slots of 4 to use fewer bits */
  g_assert ((state->values_pos + padding) % 4 == 0);
  *offset = (state->values_pos + padding) / 4;
  state->values_pos += padding + size;

  return GSK_NGL_UNIFORM_VALUE (state->values_buf, *offset);
}

#define GSK_NGL_UNIFORM_STATE_REPLACE(info, u, type, count)                                \
  G_STMT_START {                                                                           \
    if ((info)->info.initial && count == (info)->info.array_count)                         \
      {                                                                                    \
        u = GSK_NGL_UNIFORM_VALUE (state->values_buf, (info)->info.offset);                \
      }                                                                                    \
    else                                                                                   \
      {                                                                                    \
        guint offset;                                                                      \
        u = gsk_ngl_uniform_state_realloc (state, sizeof(type) * MAX (1, count), &offset); \
        g_assert (offset < (1 << GSK_NGL_UNIFORM_OFFSET_BITS));                            \
        (info)->info.offset = offset;                                                      \
        /* We might have increased array length */                                         \
        (info)->info.array_count = count;                                                  \
      }                                                                                    \
  } G_STMT_END

static inline void
gsk_ngl_uniform_info_changed (GskNglUniformInfoElement *info,
                              guint                     location,
                              guint                     stamp)
{
  info->stamp = stamp;
  info->info.initial = FALSE;
}

static inline void
gsk_ngl_uniform_state_set1f (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             float                 value0)
{
  Uniform1f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != 0);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_1F, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform1f , 1);
      u->v0 = value0;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set2f (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             float                 value0,
                             float                 value1)
{
  Uniform2f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_2F, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform2f, 1);
      u->v0 = value0;
      u->v1 = value1;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set3f (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             float                 value0,
                             float                 value1,
                             float                 value2)
{
  Uniform3f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_3F, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform3f, 1);
      u->v0 = value0;
      u->v1 = value1;
      u->v2 = value2;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set4f (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             float                 value0,
                             float                 value1,
                             float                 value2,
                             float                 value3)
{
  Uniform4f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_4F, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform4f, 1);
      u->v0 = value0;
      u->v1 = value1;
      u->v2 = value2;
      u->v3 = value3;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set1ui (GskNglUniformState   *state,
                              GskNglUniformProgram *program,
                              guint                 location,
                              guint                 stamp,
                              guint                 value0)
{
  Uniform1ui *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_1UI, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform1ui, 1);
      u->v0 = value0;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set1i (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             int                   value0)
{
  Uniform1i *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_1I, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform1i, 1);
      u->v0 = value0;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set2i (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             int                   value0,
                             int                   value1)
{
  Uniform2i *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_2I, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform2i, 1);
      u->v0 = value0;
      u->v1 = value1;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set3i (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             int                   value0,
                             int                   value1,
                             int                   value2)
{
  Uniform3i *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_3I, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform3i, 1);
      u->v0 = value0;
      u->v1 = value1;
      u->v2 = value2;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set4i (GskNglUniformState   *state,
                             GskNglUniformProgram *program,
                             guint                 location,
                             guint                 stamp,
                             int                   value0,
                             int                   value1,
                             int                   value2,
                             int                   value3)
{
  Uniform4i *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_4I, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform4i, 1);
      u->v0 = value0;
      u->v1 = value1;
      u->v2 = value2;
      u->v3 = value3;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set_rounded_rect (GskNglUniformState   *state,
                                        GskNglUniformProgram *program,
                                        guint                 location,
                                        guint                 stamp,
                                        const GskRoundedRect *rounded_rect)
{
  GskRoundedRect *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (rounded_rect != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_ROUNDED_RECT, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, GskRoundedRect, 1);
      memcpy (u, rounded_rect, sizeof *rounded_rect);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set_matrix (GskNglUniformState      *state,
                                  GskNglUniformProgram    *program,
                                  guint                    location,
                                  guint                    stamp,
                                  const graphene_matrix_t *matrix)
{
  graphene_matrix_t *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (matrix != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_MATRIX, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, graphene_matrix_t, 1);
      memcpy (u, matrix, sizeof *matrix);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

/**
 * gsk_ngl_uniform_state_set_texture:
 * @state: a #GskNglUniformState
 * @program: the program id
 * @location: the location of the texture
 * @texture_slot: a texturing slot such as GL_TEXTURE0
 *
 * Sets the uniform expecting a texture to @texture_slot. This API
 * expects a texture slot such as GL_TEXTURE0 to reduce chances of
 * miss-use by the caller.
 *
 * The value stored to the uniform is in the form of 0 for GL_TEXTURE0,
 * 1 for GL_TEXTURE1, and so on.
 */
static inline void
gsk_ngl_uniform_state_set_texture (GskNglUniformState   *state,
                                   GskNglUniformProgram *program,
                                   guint                 location,
                                   guint                 stamp,
                                   guint                 texture_slot)
{
  GskNglUniformInfoElement *info;
  guint *u;

  g_assert (texture_slot >= GL_TEXTURE0);
  g_assert (texture_slot < GL_TEXTURE16);

  texture_slot -= GL_TEXTURE0;

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_TEXTURE, 1, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, guint, 1);
      *u = texture_slot;
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

/**
 * gsk_ngl_uniform_state_set_color:
 * @state: a #GskNglUniformState
 * @program: a program id > 0
 * @location: the uniform location
 * @color: a color to set or %NULL for transparent
 *
 * Sets a uniform to the color described by @color. This is a convenience
 * function to allow callers to avoid having to translate colors to floats
 * in other portions of the renderer.
 */
static inline void
gsk_ngl_uniform_state_set_color (GskNglUniformState   *state,
                                 GskNglUniformProgram *program,
                                 guint                 location,
                                 guint                 stamp,
                                 const GdkRGBA        *color)
{
  static const GdkRGBA transparent = {0};
  GskNglUniformInfoElement *info;
  GdkRGBA *u;

  g_assert (state != NULL);
  g_assert (program != NULL);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_COLOR, 1, location, stamp, &info)))
    {
      if (color == NULL)
        color = &transparent;

      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, GdkRGBA, 1);
      memcpy (u, color, sizeof *color);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set1fv (GskNglUniformState   *state,
                              GskNglUniformProgram *program,
                              guint                 location,
                              guint                 stamp,
                              guint                 count,
                              const float          *value)
{
  Uniform1f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (count > 0);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_1FV, count, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform1f, count);
      memcpy (u, value, sizeof (Uniform1f) * count);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set2fv (GskNglUniformState   *state,
                              GskNglUniformProgram *program,
                              guint                 location,
                              guint                 stamp,
                              guint                 count,
                              const float          *value)
{
  Uniform2f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (count > 0);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_2FV, count, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform2f, count);
      memcpy (u, value, sizeof (Uniform2f) * count);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set3fv (GskNglUniformState   *state,
                              GskNglUniformProgram *program,
                              guint                 location,
                              guint                 stamp,
                              guint                 count,
                              const float          *value)
{
  Uniform3f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (count > 0);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_3FV, count, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform3f, count);
      memcpy (u, value, sizeof (Uniform3f) * count);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline void
gsk_ngl_uniform_state_set4fv (GskNglUniformState   *state,
                              GskNglUniformProgram *program,
                              guint                 location,
                              guint                 stamp,
                              guint                 count,
                              const float          *value)
{
  Uniform4f *u;
  GskNglUniformInfoElement *info;

  g_assert (state != NULL);
  g_assert (program != NULL);
  g_assert (count > 0);

  if ((u = gsk_ngl_uniform_state_get_value (state, program, GSK_NGL_UNIFORM_FORMAT_4FV, count, location, stamp, &info)))
    {
      GSK_NGL_UNIFORM_STATE_REPLACE (info, u, Uniform4f, count);
      memcpy (u, value, sizeof (Uniform4f) * count);
      gsk_ngl_uniform_info_changed (info, location, stamp);
    }
}

static inline guint
gsk_ngl_uniform_state_fmix (guint program,
                            guint location)
{
  guint h = (program << 16) | location;

   h ^= h >> 16;
   h *= 0x85ebca6b;
   h ^= h >> 13;
   h *= 0xc2b2ae35;
   h ^= h >> 16;

   return h;
}

/*
 * gsk_ngl_uniform_state_apply:
 * @state: the uniform state
 * @program: the program id
 * @location: the location of the uniform
 * @offset: the offset of the data within the buffer
 * @info: the uniform info
 *
 * This function can be used to apply state that was previously recorded
 * by the #GskNglUniformState.
 *
 * It is specifically useful from the GskNglCommandQueue to execute uniform
 * changes but only when they have changed from the current value.
 */
static inline void
gsk_ngl_uniform_state_apply (GskNglUniformState *state,
                             guint               program,
                             guint               location,
                             GskNglUniformInfo   info)
{
  guint index = gsk_ngl_uniform_state_fmix (program, location) % G_N_ELEMENTS (state->apply_hash);
  gconstpointer dataptr = GSK_NGL_UNIFORM_VALUE (state->values_buf, info.offset);

  /* aligned, can treat as unsigned */
  if (*(guint *)&info == *(guint *)&state->apply_hash[index])
    return;

  state->apply_hash[index] = info;

  /* TODO: We could do additional comparisons here to make sure we are
   *       changing state.
   */

  switch (info.format)
    {
    case GSK_NGL_UNIFORM_FORMAT_1F:
      glUniform1fv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_2F:
      glUniform2fv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_3F:
      glUniform3fv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_4F:
      glUniform4fv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_1FV:
      glUniform1fv (location, info.array_count, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_2FV:
      glUniform2fv (location, info.array_count, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_3FV:
      glUniform3fv (location, info.array_count, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_4FV:
      glUniform4fv (location, info.array_count, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_1I:
    case GSK_NGL_UNIFORM_FORMAT_TEXTURE:
      glUniform1iv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_2I:
      glUniform2iv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_3I:
      glUniform3iv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_4I:
      glUniform4iv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_1UI:
      glUniform1uiv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_MATRIX: {
      float mat[16];
      graphene_matrix_to_float (dataptr, mat);
      glUniformMatrix4fv (location, 1, GL_FALSE, mat);
#if 0
      /* TODO: If Graphene can give us a peek here on platforms
       * where the format is float[16] (most/all x86_64?) then
       * We can avoid the SIMD operation to convert the format.
       */
      G_STATIC_ASSERT (sizeof (graphene_matrix_t) == 16*4);
      glUniformMatrix4fv (location, 1, GL_FALSE, dataptr);
#endif
    }
    break;

    case GSK_NGL_UNIFORM_FORMAT_COLOR:
      glUniform4fv (location, 1, dataptr);
    break;

    case GSK_NGL_UNIFORM_FORMAT_ROUNDED_RECT:
      glUniform4fv (location, 3, dataptr);
    break;

    default:
      g_assert_not_reached ();
    }
}

G_END_DECLS

#endif /* GSK_NGL_UNIFORM_STATE_PRIVATE_H */