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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
|
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 T. Zachary Laine
* Copyright © 2010 Eric Anholt
* Copyright © 2010 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is T. Zachary Laine.
*
* Contributor(s):
* Benjamin Otte <otte@gnome.org>
* Eric Anholt <eric@anholt.net>
* T. Zachary Laine <whatwasthataddress@gmail.com>
*/
#include "cairoint.h"
#include "cairo-gl-private.h"
#include "cairo-error-private.h"
#include "cairo-output-stream-private.h"
typedef struct cairo_gl_shader_impl {
void
(*compile_shader) (GLuint *shader, GLenum type, const char *text);
void
(*link_shader) (GLuint *program, GLuint vert, GLuint frag);
void
(*destroy_shader) (GLuint shader);
void
(*destroy_program) (GLuint program);
void
(*bind_float) (cairo_gl_shader_t *shader,
const char *name,
float value);
void
(*bind_vec2) (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1);
void
(*bind_vec3) (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1,
float value2);
void
(*bind_vec4) (cairo_gl_shader_t *shader,
const char *name,
float value0, float value1,
float value2, float value3);
void
(*bind_matrix) (cairo_gl_shader_t *shader,
const char *name,
cairo_matrix_t* m);
void
(*bind_texture) (cairo_gl_shader_t *shader,
const char *name,
GLuint tex_unit);
void
(*use) (cairo_gl_shader_t *shader);
} shader_impl_t;
static cairo_status_t
_cairo_gl_shader_compile (cairo_gl_context_t *ctx,
cairo_gl_shader_t *shader,
cairo_gl_var_type_t src,
cairo_gl_var_type_t mask,
const char *fragment_text);
/* ARB_shader_objects / ARB_vertex_shader / ARB_fragment_shader extensions
API. */
static void
compile_shader_arb (GLuint *shader, GLenum type, const char *text)
{
const char* strings[1] = { text };
GLint gl_status;
*shader = glCreateShaderObjectARB (type);
glShaderSourceARB (*shader, 1, strings, 0);
glCompileShaderARB (*shader);
glGetObjectParameterivARB (*shader, GL_OBJECT_COMPILE_STATUS_ARB, &gl_status);
if (gl_status == GL_FALSE) {
GLint log_size;
glGetObjectParameterivARB (*shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
if (0 < log_size) {
char *log = _cairo_malloc (log_size);
GLint chars;
log[log_size - 1] = '\0';
glGetInfoLogARB (*shader, log_size, &chars, log);
printf ("OpenGL shader compilation failed. Shader:\n"
"%s\n"
"OpenGL compilation log:\n"
"%s\n",
text, log);
free (log);
} else {
printf ("OpenGL shader compilation failed.\n");
}
ASSERT_NOT_REACHED;
}
}
static void
link_shader_arb (GLuint *program, GLuint vert, GLuint frag)
{
GLint gl_status;
*program = glCreateProgramObjectARB ();
glAttachObjectARB (*program, vert);
glAttachObjectARB (*program, frag);
glLinkProgramARB (*program);
glGetObjectParameterivARB (*program, GL_OBJECT_LINK_STATUS_ARB, &gl_status);
if (gl_status == GL_FALSE) {
GLint log_size;
glGetObjectParameterivARB (*program, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
if (0 < log_size) {
char *log = _cairo_malloc (log_size);
GLint chars;
log[log_size - 1] = '\0';
glGetInfoLogARB (*program, log_size, &chars, log);
printf ("OpenGL shader link failed:\n%s\n", log);
free (log);
} else {
printf ("OpenGL shader link failed.\n");
}
ASSERT_NOT_REACHED;
}
}
static void
destroy_shader_arb (GLuint shader)
{
glDeleteObjectARB (shader);
}
static void
destroy_program_arb (GLuint shader)
{
glDeleteObjectARB (shader);
}
static void
bind_float_arb (cairo_gl_shader_t *shader,
const char *name,
float value)
{
GLint location = glGetUniformLocationARB (shader->program, name);
assert (location != -1);
glUniform1fARB (location, value);
}
static void
bind_vec2_arb (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1)
{
GLint location = glGetUniformLocationARB (shader->program, name);
assert (location != -1);
glUniform2fARB (location, value0, value1);
}
static void
bind_vec3_arb (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1,
float value2)
{
GLint location = glGetUniformLocationARB (shader->program, name);
assert (location != -1);
glUniform3fARB (location, value0, value1, value2);
}
static void
bind_vec4_arb (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1,
float value2,
float value3)
{
GLint location = glGetUniformLocationARB (shader->program, name);
assert (location != -1);
glUniform4fARB (location, value0, value1, value2, value3);
}
static void
bind_matrix_arb (cairo_gl_shader_t *shader,
const char *name,
cairo_matrix_t* m)
{
GLint location = glGetUniformLocationARB (shader->program, name);
float gl_m[16] = {
m->xx, m->xy, 0, m->x0,
m->yx, m->yy, 0, m->y0,
0, 0, 1, 0,
0, 0, 0, 1
};
assert (location != -1);
glUniformMatrix4fvARB (location, 1, GL_TRUE, gl_m);
}
static void
bind_texture_arb (cairo_gl_shader_t *shader,
const char *name,
GLuint tex_unit)
{
GLint location = glGetUniformLocationARB (shader->program, name);
assert (location != -1);
glUniform1iARB (location, tex_unit);
}
static void
use_program_arb (cairo_gl_shader_t *shader)
{
if (shader)
glUseProgramObjectARB (shader->program);
else
glUseProgramObjectARB (0);
}
/* OpenGL Core 2.0 API. */
static void
compile_shader_core_2_0 (GLuint *shader, GLenum type, const char *text)
{
const char* strings[1] = { text };
GLint gl_status;
*shader = glCreateShader (type);
glShaderSource (*shader, 1, strings, 0);
glCompileShader (*shader);
glGetShaderiv (*shader, GL_COMPILE_STATUS, &gl_status);
if (gl_status == GL_FALSE) {
GLint log_size;
glGetShaderiv (*shader, GL_INFO_LOG_LENGTH, &log_size);
if (0 < log_size) {
char *log = _cairo_malloc (log_size);
GLint chars;
log[log_size - 1] = '\0';
glGetShaderInfoLog (*shader, log_size, &chars, log);
printf ("OpenGL shader compilation failed. Shader:\n"
"%s\n"
"OpenGL compilation log:\n"
"%s\n",
text, log);
free (log);
} else {
printf ("OpenGL shader compilation failed.\n");
}
ASSERT_NOT_REACHED;
}
}
static void
link_shader_core_2_0 (GLuint *program, GLuint vert, GLuint frag)
{
GLint gl_status;
*program = glCreateProgram ();
glAttachShader (*program, vert);
glAttachShader (*program, frag);
glLinkProgram (*program);
glGetProgramiv (*program, GL_LINK_STATUS, &gl_status);
if (gl_status == GL_FALSE) {
GLint log_size;
glGetProgramiv (*program, GL_INFO_LOG_LENGTH, &log_size);
if (0 < log_size) {
char *log = _cairo_malloc (log_size);
GLint chars;
log[log_size - 1] = '\0';
glGetProgramInfoLog (*program, log_size, &chars, log);
printf ("OpenGL shader link failed:\n%s\n", log);
free (log);
} else {
printf ("OpenGL shader link failed.\n");
}
ASSERT_NOT_REACHED;
}
}
static void
destroy_shader_core_2_0 (GLuint shader)
{
glDeleteShader (shader);
}
static void
destroy_program_core_2_0 (GLuint shader)
{
glDeleteProgram (shader);
}
static void
bind_float_core_2_0 (cairo_gl_shader_t *shader,
const char *name,
float value)
{
GLint location = glGetUniformLocation (shader->program, name);
assert (location != -1);
glUniform1f (location, value);
}
static void
bind_vec2_core_2_0 (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1)
{
GLint location = glGetUniformLocation (shader->program, name);
assert (location != -1);
glUniform2f (location, value0, value1);
}
static void
bind_vec3_core_2_0 (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1,
float value2)
{
GLint location = glGetUniformLocation (shader->program, name);
assert (location != -1);
glUniform3f (location, value0, value1, value2);
}
static void
bind_vec4_core_2_0 (cairo_gl_shader_t *shader,
const char *name,
float value0,
float value1,
float value2,
float value3)
{
GLint location = glGetUniformLocation (shader->program, name);
assert (location != -1);
glUniform4f (location, value0, value1, value2, value3);
}
static void
bind_matrix_core_2_0 (cairo_gl_shader_t *shader, const char *name, cairo_matrix_t* m)
{
GLint location = glGetUniformLocation (shader->program, name);
float gl_m[16] = {
m->xx, m->xy, 0, m->x0,
m->yx, m->yy, 0, m->y0,
0, 0, 1, 0,
0, 0, 0, 1
};
assert (location != -1);
glUniformMatrix4fv (location, 1, GL_TRUE, gl_m);
}
static void
bind_texture_core_2_0 (cairo_gl_shader_t *shader, const char *name, GLuint tex_unit)
{
GLint location = glGetUniformLocation (shader->program, name);
assert (location != -1);
glUniform1i (location, tex_unit);
}
static void
use_program_core_2_0 (cairo_gl_shader_t *shader)
{
if (shader)
glUseProgram (shader->program);
else
glUseProgram (0);
}
static const cairo_gl_shader_impl_t shader_impl_core_2_0 = {
compile_shader_core_2_0,
link_shader_core_2_0,
destroy_shader_core_2_0,
destroy_program_core_2_0,
bind_float_core_2_0,
bind_vec2_core_2_0,
bind_vec3_core_2_0,
bind_vec4_core_2_0,
bind_matrix_core_2_0,
bind_texture_core_2_0,
use_program_core_2_0,
};
static const cairo_gl_shader_impl_t shader_impl_arb = {
compile_shader_arb,
link_shader_arb,
destroy_shader_arb,
destroy_program_arb,
bind_float_arb,
bind_vec2_arb,
bind_vec3_arb,
bind_vec4_arb,
bind_matrix_arb,
bind_texture_arb,
use_program_arb,
};
typedef struct _cairo_shader_cache_entry {
cairo_cache_entry_t base;
cairo_gl_operand_type_t src;
cairo_gl_operand_type_t mask;
cairo_gl_operand_type_t dest;
cairo_gl_shader_in_t in;
cairo_gl_context_t *ctx; /* XXX: needed to destroy the program */
cairo_gl_shader_t shader;
} cairo_shader_cache_entry_t;
static cairo_bool_t
_cairo_gl_shader_cache_equal (const void *key_a, const void *key_b)
{
const cairo_shader_cache_entry_t *a = key_a;
const cairo_shader_cache_entry_t *b = key_b;
return a->src == b->src &&
a->mask == b->mask &&
a->dest == b->dest &&
a->in == b->in;
}
static unsigned long
_cairo_gl_shader_cache_hash (const cairo_shader_cache_entry_t *entry)
{
return (entry->src << 24) | (entry->mask << 16) | (entry->dest << 8) | (entry->in);
}
static void
_cairo_gl_shader_cache_destroy (void *data)
{
cairo_shader_cache_entry_t *entry = data;
_cairo_gl_shader_fini (entry->ctx, &entry->shader);
if (entry->ctx->current_shader == &entry->shader)
entry->ctx->current_shader = NULL;
free (entry);
}
static void
_cairo_gl_shader_init (cairo_gl_shader_t *shader)
{
shader->fragment_shader = 0;
shader->program = 0;
}
cairo_status_t
_cairo_gl_context_init_shaders (cairo_gl_context_t *ctx)
{
static const char *fill_fs_source =
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = color;\n"
"}\n";
cairo_status_t status;
/* XXX multiple device support? */
if (GLEW_VERSION_2_0) {
ctx->shader_impl = &shader_impl_core_2_0;
} else if (GLEW_ARB_shader_objects &&
GLEW_ARB_fragment_shader &&
GLEW_ARB_vertex_program) {
ctx->shader_impl = &shader_impl_arb;
} else {
ctx->shader_impl = NULL;
}
memset (ctx->vertex_shaders, 0, sizeof (ctx->vertex_shaders));
status = _cairo_cache_init (&ctx->shaders,
_cairo_gl_shader_cache_equal,
NULL,
_cairo_gl_shader_cache_destroy,
CAIRO_GL_MAX_SHADERS_PER_CONTEXT);
if (unlikely (status))
return status;
if (ctx->shader_impl != NULL) {
_cairo_gl_shader_init (&ctx->fill_rectangles_shader);
status = _cairo_gl_shader_compile (ctx,
&ctx->fill_rectangles_shader,
CAIRO_GL_VAR_NONE,
CAIRO_GL_VAR_NONE,
fill_fs_source);
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_gl_context_fini_shaders (cairo_gl_context_t *ctx)
{
int i;
for (i = 0; i <= CAIRO_GL_VAR_TYPE_MAX; i++) {
if (ctx->vertex_shaders[i])
ctx->shader_impl->destroy_shader (ctx->vertex_shaders[i]);
}
_cairo_cache_fini (&ctx->shaders);
}
void
_cairo_gl_shader_fini (cairo_gl_context_t *ctx,
cairo_gl_shader_t *shader)
{
if (shader->fragment_shader)
ctx->shader_impl->destroy_shader (shader->fragment_shader);
if (shader->program)
ctx->shader_impl->destroy_program (shader->program);
}
typedef enum cairo_gl_operand_name {
CAIRO_GL_OPERAND_SOURCE,
CAIRO_GL_OPERAND_MASK,
CAIRO_GL_OPERAND_DEST
} cairo_gl_operand_name_t;
static const char *operand_names[] = { "source", "mask", "dest" };
static cairo_gl_var_type_t
cairo_gl_operand_get_var_type (cairo_gl_operand_type_t type)
{
switch (type) {
default:
case CAIRO_GL_OPERAND_COUNT:
ASSERT_NOT_REACHED;
case CAIRO_GL_OPERAND_NONE:
case CAIRO_GL_OPERAND_CONSTANT:
case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
case CAIRO_GL_OPERAND_RADIAL_GRADIENT:
return CAIRO_GL_VAR_NONE;
case CAIRO_GL_OPERAND_TEXTURE:
return CAIRO_GL_VAR_TEXCOORDS;
case CAIRO_GL_OPERAND_SPANS:
return CAIRO_GL_VAR_COVERAGE;
}
}
static void
cairo_gl_shader_emit_variable (cairo_output_stream_t *stream,
cairo_gl_var_type_t type,
cairo_gl_operand_name_t name)
{
switch (type) {
default:
ASSERT_NOT_REACHED;
case CAIRO_GL_VAR_NONE:
break;
case CAIRO_GL_VAR_TEXCOORDS:
_cairo_output_stream_printf (stream,
"varying vec2 %s_texcoords;\n",
operand_names[name]);
break;
case CAIRO_GL_VAR_COVERAGE:
_cairo_output_stream_printf (stream,
"varying float %s_coverage;\n",
operand_names[name]);
break;
}
}
static void
cairo_gl_shader_emit_vertex (cairo_output_stream_t *stream,
cairo_gl_var_type_t type,
cairo_gl_operand_name_t name)
{
switch (type) {
default:
ASSERT_NOT_REACHED;
case CAIRO_GL_VAR_NONE:
break;
case CAIRO_GL_VAR_TEXCOORDS:
_cairo_output_stream_printf (stream,
" %s_texcoords = gl_MultiTexCoord%d.xy;\n",
operand_names[name], name);
break;
case CAIRO_GL_VAR_COVERAGE:
_cairo_output_stream_printf (stream,
" %s_coverage = gl_Color.a;\n",
operand_names[name]);
break;
}
}
static cairo_status_t
cairo_gl_shader_get_vertex_source (cairo_gl_var_type_t src,
cairo_gl_var_type_t mask,
cairo_gl_var_type_t dest,
char **out)
{
cairo_output_stream_t *stream = _cairo_memory_stream_create ();
unsigned char *source;
unsigned int length;
cairo_status_t status;
cairo_gl_shader_emit_variable (stream, src, CAIRO_GL_OPERAND_SOURCE);
cairo_gl_shader_emit_variable (stream, mask, CAIRO_GL_OPERAND_MASK);
cairo_gl_shader_emit_variable (stream, dest, CAIRO_GL_OPERAND_DEST);
_cairo_output_stream_printf (stream,
"void main()\n"
"{\n"
" gl_Position = ftransform();\n");
cairo_gl_shader_emit_vertex (stream, src, CAIRO_GL_OPERAND_SOURCE);
cairo_gl_shader_emit_vertex (stream, mask, CAIRO_GL_OPERAND_MASK);
cairo_gl_shader_emit_vertex (stream, dest, CAIRO_GL_OPERAND_DEST);
_cairo_output_stream_write (stream,
"}\n\0", 3);
status = _cairo_memory_stream_destroy (stream, &source, &length);
if (unlikely (status))
return status;
*out = (char *) source;
return CAIRO_STATUS_SUCCESS;
}
static void
cairo_gl_shader_emit_color (cairo_output_stream_t *stream,
GLuint tex_target,
cairo_gl_operand_type_t type,
cairo_gl_operand_name_t name)
{
const char *namestr = operand_names[name];
const char *rectstr = (tex_target == GL_TEXTURE_RECTANGLE_EXT ? "Rect" : "");
switch (type) {
case CAIRO_GL_OPERAND_COUNT:
default:
ASSERT_NOT_REACHED;
break;
case CAIRO_GL_OPERAND_NONE:
_cairo_output_stream_printf (stream,
"vec4 get_%s()\n"
"{\n"
" return vec4 (0, 0, 0, 1);\n"
"}\n",
namestr);
break;
case CAIRO_GL_OPERAND_CONSTANT:
_cairo_output_stream_printf (stream,
"uniform vec4 %s_constant;\n"
"vec4 get_%s()\n"
"{\n"
" return %s_constant;\n"
"}\n",
namestr, namestr, namestr);
break;
case CAIRO_GL_OPERAND_TEXTURE:
_cairo_output_stream_printf (stream,
"uniform sampler2D%s %s_sampler;\n"
"varying vec2 %s_texcoords;\n"
"vec4 get_%s()\n"
"{\n"
" return texture2D%s(%s_sampler, %s_texcoords);\n"
"}\n",
rectstr, namestr, namestr, namestr, rectstr, namestr, namestr);
break;
case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
_cairo_output_stream_printf (stream,
"uniform sampler1D %s_sampler;\n"
"uniform mat4 %s_matrix;\n"
"uniform vec2 %s_segment;\n"
"\n"
"vec4 get_%s()\n"
"{\n"
" vec2 pos = (%s_matrix * vec4 (gl_FragCoord.xy, 0.0, 1.0)).xy;\n"
" float t = dot (pos, %s_segment) / dot (%s_segment, %s_segment);\n"
" return texture1D (%s_sampler, t);\n"
"}\n",
namestr, namestr, namestr, namestr, namestr,
namestr, namestr, namestr, namestr);
break;
case CAIRO_GL_OPERAND_RADIAL_GRADIENT:
_cairo_output_stream_printf (stream,
"uniform sampler1D %s_sampler;\n"
"uniform mat4 %s_matrix;\n"
"uniform vec2 %s_circle_1;\n"
"uniform float %s_radius_0;\n"
"uniform float %s_radius_1;\n"
"\n"
"vec4 get_%s()\n"
"{\n"
" vec2 pos = (%s_matrix * vec4 (gl_FragCoord.xy, 0.0, 1.0)).xy;\n"
" \n"
" float dr = %s_radius_1 - %s_radius_0;\n"
" float dot_circle_1 = dot (%s_circle_1, %s_circle_1);\n"
" float dot_pos_circle_1 = dot (pos, %s_circle_1);\n"
" \n"
" float A = dot_circle_1 - dr * dr;\n"
" float B = -2.0 * (dot_pos_circle_1 + %s_radius_0 * dr);\n"
" float C = dot (pos, pos) - %s_radius_0 * %s_radius_0;\n"
" float det = B * B - 4.0 * A * C;\n"
" det = max (det, 0.0);\n"
" \n"
" float sqrt_det = sqrt (det);\n"
" sqrt_det *= sign(A);\n"
" \n"
" float t = (-B + sqrt_det) / (2.0 * A);\n"
" return texture1D (%s_sampler, t);\n"
"}\n",
namestr, namestr, namestr, namestr, namestr,
namestr, namestr, namestr, namestr, namestr,
namestr, namestr, namestr, namestr, namestr,
namestr);
break;
case CAIRO_GL_OPERAND_SPANS:
_cairo_output_stream_printf (stream,
"varying float %s_coverage;\n"
"vec4 get_%s()\n"
"{\n"
" return vec4(0, 0, 0, %s_coverage);\n"
"}\n",
namestr, namestr, namestr);
break;
}
}
static cairo_status_t
cairo_gl_shader_get_fragment_source (GLuint tex_target,
cairo_gl_shader_in_t in,
cairo_gl_operand_type_t src,
cairo_gl_operand_type_t mask,
cairo_gl_operand_type_t dest,
char **out)
{
cairo_output_stream_t *stream = _cairo_memory_stream_create ();
unsigned char *source;
unsigned int length;
cairo_status_t status;
cairo_gl_shader_emit_color (stream, tex_target, src, CAIRO_GL_OPERAND_SOURCE);
cairo_gl_shader_emit_color (stream, tex_target, mask, CAIRO_GL_OPERAND_MASK);
if (dest != CAIRO_GL_OPERAND_NONE)
cairo_gl_shader_emit_color (stream, tex_target, dest, CAIRO_GL_OPERAND_DEST);
_cairo_output_stream_printf (stream,
"void main()\n"
"{\n");
switch (in) {
case CAIRO_GL_SHADER_IN_COUNT:
default:
ASSERT_NOT_REACHED;
case CAIRO_GL_SHADER_IN_NORMAL:
_cairo_output_stream_printf (stream,
" gl_FragColor = get_source() * get_mask().a;\n");
break;
case CAIRO_GL_SHADER_IN_CA_SOURCE:
_cairo_output_stream_printf (stream,
" gl_FragColor = get_source() * get_mask();\n");
break;
case CAIRO_GL_SHADER_IN_CA_SOURCE_ALPHA:
_cairo_output_stream_printf (stream,
" gl_FragColor = get_source().a * get_mask();\n");
break;
}
_cairo_output_stream_write (stream,
"}\n\0", 3);
status = _cairo_memory_stream_destroy (stream, &source, &length);
if (unlikely (status))
return status;
*out = (char *) source;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_gl_shader_compile (cairo_gl_context_t *ctx,
cairo_gl_shader_t *shader,
cairo_gl_var_type_t src,
cairo_gl_var_type_t mask,
const char *fragment_text)
{
unsigned int vertex_shader;
cairo_status_t status;
if (ctx->shader_impl == NULL)
return CAIRO_STATUS_SUCCESS;
assert (shader->program == 0);
vertex_shader = cairo_gl_var_type_hash (src, mask, CAIRO_GL_VAR_NONE);
if (ctx->vertex_shaders[vertex_shader] == 0) {
char *source;
status = cairo_gl_shader_get_vertex_source (src,
mask,
CAIRO_GL_VAR_NONE,
&source);
if (unlikely (status))
goto FAILURE;
ctx->shader_impl->compile_shader (&ctx->vertex_shaders[vertex_shader],
GL_VERTEX_SHADER,
source);
free (source);
}
ctx->shader_impl->compile_shader (&shader->fragment_shader,
GL_FRAGMENT_SHADER,
fragment_text);
ctx->shader_impl->link_shader (&shader->program,
ctx->vertex_shaders[vertex_shader],
shader->fragment_shader);
return CAIRO_STATUS_SUCCESS;
FAILURE:
_cairo_gl_shader_fini (ctx, shader);
shader->fragment_shader = 0;
shader->program = 0;
return status;
}
void
_cairo_gl_shader_bind_float (cairo_gl_context_t *ctx,
const char *name,
float value)
{
ctx->shader_impl->bind_float (ctx->current_shader, name, value);
}
void
_cairo_gl_shader_bind_vec2 (cairo_gl_context_t *ctx,
const char *name,
float value0,
float value1)
{
ctx->shader_impl->bind_vec2 (ctx->current_shader, name, value0, value1);
}
void
_cairo_gl_shader_bind_vec3 (cairo_gl_context_t *ctx,
const char *name,
float value0,
float value1,
float value2)
{
ctx->shader_impl->bind_vec3 (ctx->current_shader, name, value0, value1, value2);
}
void
_cairo_gl_shader_bind_vec4 (cairo_gl_context_t *ctx,
const char *name,
float value0, float value1,
float value2, float value3)
{
ctx->shader_impl->bind_vec4 (ctx->current_shader, name, value0, value1, value2, value3);
}
void
_cairo_gl_shader_bind_matrix (cairo_gl_context_t *ctx,
const char *name, cairo_matrix_t* m)
{
ctx->shader_impl->bind_matrix (ctx->current_shader, name, m);
}
void
_cairo_gl_shader_bind_texture (cairo_gl_context_t *ctx,
const char *name, GLuint tex_unit)
{
ctx->shader_impl->bind_texture (ctx->current_shader, name, tex_unit);
}
void
_cairo_gl_set_shader (cairo_gl_context_t *ctx,
cairo_gl_shader_t *shader)
{
if (ctx->shader_impl == NULL)
return;
if (ctx->current_shader == shader)
return;
ctx->shader_impl->use (shader);
ctx->current_shader = shader;
}
cairo_status_t
_cairo_gl_set_shader_by_type (cairo_gl_context_t *ctx,
cairo_gl_operand_type_t source,
cairo_gl_operand_type_t mask,
cairo_gl_shader_in_t in)
{
cairo_shader_cache_entry_t lookup, *entry;
char *fs_source;
cairo_status_t status;
if (ctx->shader_impl == NULL)
return CAIRO_STATUS_SUCCESS;
lookup.src = source;
lookup.mask = mask;
lookup.dest = CAIRO_GL_OPERAND_NONE;
lookup.in = in;
lookup.base.hash = _cairo_gl_shader_cache_hash (&lookup);
lookup.base.size = 1;
entry = _cairo_cache_lookup (&ctx->shaders, &lookup.base);
if (entry) {
assert (entry->shader.program);
_cairo_gl_set_shader (ctx, &entry->shader);
return CAIRO_STATUS_SUCCESS;
}
status = cairo_gl_shader_get_fragment_source (ctx->tex_target,
in,
source,
mask,
CAIRO_GL_OPERAND_NONE,
&fs_source);
if (unlikely (status))
return status;
entry = malloc (sizeof (cairo_shader_cache_entry_t));
if (unlikely (entry == NULL)) {
free (fs_source);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memcpy (entry, &lookup, sizeof (cairo_shader_cache_entry_t));
entry->ctx = ctx;
_cairo_gl_shader_init (&entry->shader);
status = _cairo_gl_shader_compile (ctx,
&entry->shader,
cairo_gl_operand_get_var_type (source),
cairo_gl_operand_get_var_type (mask),
fs_source);
free (fs_source);
if (unlikely (status)) {
free (entry);
return status;
}
status = _cairo_cache_insert (&ctx->shaders, &entry->base);
if (unlikely (status)) {
_cairo_gl_shader_fini (ctx, &entry->shader);
free (entry);
return status;
}
_cairo_gl_set_shader (ctx, &entry->shader);
if (source != CAIRO_GL_OPERAND_CONSTANT &&
source != CAIRO_GL_OPERAND_SPANS &&
source != CAIRO_GL_OPERAND_NONE) {
_cairo_gl_shader_bind_texture (ctx, "source_sampler", 0);
}
if (mask != CAIRO_GL_OPERAND_CONSTANT &&
mask != CAIRO_GL_OPERAND_SPANS &&
mask != CAIRO_GL_OPERAND_NONE) {
_cairo_gl_shader_bind_texture (ctx, "mask_sampler", 1);
}
return CAIRO_STATUS_SUCCESS;
}
|