forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgraphics.c
More file actions
3323 lines (2538 loc) · 75.5 KB
/
graphics.c
File metadata and controls
3323 lines (2538 loc) · 75.5 KB
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
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <assert.h>
#include "../util/base.h"
#include "../util/bmem.h"
#include "../util/platform.h"
#include "graphics-internal.h"
#include "vec2.h"
#include "vec3.h"
#include "quat.h"
#include "axisang.h"
#include "effect-parser.h"
#include "effect.h"
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
static THREAD_LOCAL graphics_t *thread_graphics = NULL;
static inline bool gs_obj_valid(const void *obj, const char *f,
const char *name)
{
if (!obj) {
blog(LOG_DEBUG, "%s: Null '%s' parameter", f, name);
return false;
}
return true;
}
static inline bool gs_valid(const char *f)
{
if (!thread_graphics) {
blog(LOG_DEBUG, "%s: called while not in a graphics context",
f);
return false;
}
return true;
}
#define ptr_valid(ptr, func) gs_obj_valid(ptr, func, #ptr)
#define gs_valid_p(func, param1) (gs_valid(func) && ptr_valid(param1, func))
#define gs_valid_p2(func, param1, param2) \
(gs_valid(func) && ptr_valid(param1, func) && ptr_valid(param2, func))
#define gs_valid_p3(func, param1, param2, param3) \
(gs_valid(func) && ptr_valid(param1, func) && \
ptr_valid(param2, func) && ptr_valid(param3, func))
#define IMMEDIATE_COUNT 512
void gs_enum_adapters(bool (*callback)(void *param, const char *name,
uint32_t id),
void *param)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid_p("gs_enum_adapters", callback))
return;
if (graphics->exports.device_enum_adapters) {
if (graphics->exports.device_enum_adapters(callback, param)) {
return;
}
}
/* If the subsystem does not currently support device enumeration of
* adapters or fails to enumerate adapters, just set it to one adapter
* named "Default" */
callback(param, "Default", 0);
}
extern void gs_init_image_deps(void);
extern void gs_free_image_deps(void);
bool load_graphics_imports(struct gs_exports *exports, void *module,
const char *module_name);
static bool graphics_init_immediate_vb(struct graphics_subsystem *graphics)
{
struct gs_vb_data *vbd;
vbd = gs_vbdata_create();
vbd->num = IMMEDIATE_COUNT;
vbd->points = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
vbd->normals = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
vbd->colors = bmalloc(sizeof(uint32_t) * IMMEDIATE_COUNT);
vbd->num_tex = 1;
vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
vbd->tvarray[0].width = 2;
vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);
graphics->immediate_vertbuffer =
graphics->exports.device_vertexbuffer_create(graphics->device,
vbd, GS_DYNAMIC);
if (!graphics->immediate_vertbuffer)
return false;
return true;
}
static bool graphics_init_sprite_vb(struct graphics_subsystem *graphics)
{
struct gs_vb_data *vbd;
vbd = gs_vbdata_create();
vbd->num = 4;
vbd->points = bmalloc(sizeof(struct vec3) * 4);
vbd->num_tex = 1;
vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
vbd->tvarray[0].width = 2;
vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);
memset(vbd->points, 0, sizeof(struct vec3) * 4);
memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);
graphics->sprite_buffer = graphics->exports.device_vertexbuffer_create(
graphics->device, vbd, GS_DYNAMIC);
if (!graphics->sprite_buffer)
return false;
return true;
}
static bool graphics_init(struct graphics_subsystem *graphics)
{
struct matrix4 top_mat;
matrix4_identity(&top_mat);
da_push_back(graphics->matrix_stack, &top_mat);
graphics->exports.device_enter_context(graphics->device);
if (!graphics_init_immediate_vb(graphics))
return false;
if (!graphics_init_sprite_vb(graphics))
return false;
if (pthread_mutex_init(&graphics->mutex, NULL) != 0)
return false;
if (pthread_mutex_init(&graphics->effect_mutex, NULL) != 0)
return false;
graphics->exports.device_blend_function_separate(
graphics->device, GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA,
GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
graphics->cur_blend_state.enabled = true;
graphics->cur_blend_state.src_c = GS_BLEND_SRCALPHA;
graphics->cur_blend_state.dest_c = GS_BLEND_INVSRCALPHA;
graphics->cur_blend_state.src_a = GS_BLEND_ONE;
graphics->cur_blend_state.dest_a = GS_BLEND_INVSRCALPHA;
graphics->cur_blend_state.op = GS_BLEND_OP_ADD;
graphics->exports.device_blend_op(graphics->device,
graphics->cur_blend_state.op);
graphics->exports.device_leave_context(graphics->device);
gs_init_image_deps();
return true;
}
int gs_create(graphics_t **pgraphics, const char *module, uint32_t adapter)
{
int errcode = GS_ERROR_FAIL;
graphics_t *graphics = bzalloc(sizeof(struct graphics_subsystem));
pthread_mutex_init_value(&graphics->mutex);
pthread_mutex_init_value(&graphics->effect_mutex);
graphics->module = os_dlopen(module);
if (!graphics->module) {
errcode = GS_ERROR_MODULE_NOT_FOUND;
goto error;
}
if (!load_graphics_imports(&graphics->exports, graphics->module,
module))
goto error;
errcode = graphics->exports.device_create(&graphics->device, adapter);
if (errcode != GS_SUCCESS)
goto error;
if (!graphics_init(graphics)) {
errcode = GS_ERROR_FAIL;
goto error;
}
*pgraphics = graphics;
return errcode;
error:
gs_destroy(graphics);
return errcode;
}
extern void gs_effect_actually_destroy(gs_effect_t *effect);
void gs_destroy(graphics_t *graphics)
{
if (!ptr_valid(graphics, "gs_destroy"))
return;
while (thread_graphics)
gs_leave_context();
if (graphics->device) {
struct gs_effect *effect = graphics->first_effect;
thread_graphics = graphics;
graphics->exports.device_enter_context(graphics->device);
while (effect) {
struct gs_effect *next = effect->next;
gs_effect_actually_destroy(effect);
effect = next;
}
graphics->exports.gs_vertexbuffer_destroy(
graphics->sprite_buffer);
graphics->exports.gs_vertexbuffer_destroy(
graphics->immediate_vertbuffer);
graphics->exports.device_destroy(graphics->device);
thread_graphics = NULL;
}
pthread_mutex_destroy(&graphics->mutex);
pthread_mutex_destroy(&graphics->effect_mutex);
da_free(graphics->matrix_stack);
da_free(graphics->viewport_stack);
da_free(graphics->blend_state_stack);
if (graphics->module)
os_dlclose(graphics->module);
bfree(graphics);
gs_free_image_deps();
}
void gs_enter_context(graphics_t *graphics)
{
if (!ptr_valid(graphics, "gs_enter_context"))
return;
bool is_current = thread_graphics == graphics;
if (thread_graphics && !is_current) {
while (thread_graphics)
gs_leave_context();
}
if (!is_current) {
pthread_mutex_lock(&graphics->mutex);
graphics->exports.device_enter_context(graphics->device);
thread_graphics = graphics;
}
os_atomic_inc_long(&graphics->ref);
}
void gs_leave_context(void)
{
if (gs_valid("gs_leave_context")) {
if (!os_atomic_dec_long(&thread_graphics->ref)) {
graphics_t *graphics = thread_graphics;
graphics->exports.device_leave_context(
graphics->device);
pthread_mutex_unlock(&graphics->mutex);
thread_graphics = NULL;
}
}
}
graphics_t *gs_get_context(void)
{
return thread_graphics;
}
void *gs_get_device_obj(void)
{
if (!gs_valid("gs_get_device_obj"))
return NULL;
return thread_graphics->exports.device_get_device_obj(
thread_graphics->device);
}
const char *gs_get_device_name(void)
{
return gs_valid("gs_get_device_name")
? thread_graphics->exports.device_get_name()
: NULL;
}
int gs_get_device_type(void)
{
return gs_valid("gs_get_device_type")
? thread_graphics->exports.device_get_type()
: -1;
}
static inline struct matrix4 *top_matrix(graphics_t *graphics)
{
return graphics->matrix_stack.array + graphics->cur_matrix;
}
void gs_matrix_push(void)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_matrix_push"))
return;
struct matrix4 mat, *top_mat = top_matrix(graphics);
memcpy(&mat, top_mat, sizeof(struct matrix4));
da_push_back(graphics->matrix_stack, &mat);
graphics->cur_matrix++;
}
void gs_matrix_pop(void)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_matrix_pop"))
return;
if (graphics->cur_matrix == 0) {
blog(LOG_ERROR, "Tried to pop last matrix on stack");
return;
}
da_erase(graphics->matrix_stack, graphics->cur_matrix);
graphics->cur_matrix--;
}
void gs_matrix_identity(void)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_identity"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_identity(top_mat);
}
void gs_matrix_transpose(void)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_transpose"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_transpose(top_mat, top_mat);
}
void gs_matrix_set(const struct matrix4 *matrix)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_set"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_copy(top_mat, matrix);
}
void gs_matrix_get(struct matrix4 *dst)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_get"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_copy(dst, top_mat);
}
void gs_matrix_mul(const struct matrix4 *matrix)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_mul"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_mul(top_mat, matrix, top_mat);
}
void gs_matrix_rotquat(const struct quat *rot)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_rotquat"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_rotate_i(top_mat, rot, top_mat);
}
void gs_matrix_rotaa(const struct axisang *rot)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_rotaa"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_rotate_aa_i(top_mat, rot, top_mat);
}
void gs_matrix_translate(const struct vec3 *pos)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_translate"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_translate3v_i(top_mat, pos, top_mat);
}
void gs_matrix_scale(const struct vec3 *scale)
{
struct matrix4 *top_mat;
if (!gs_valid("gs_matrix_scale"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat)
matrix4_scale_i(top_mat, scale, top_mat);
}
void gs_matrix_rotaa4f(float x, float y, float z, float angle)
{
struct matrix4 *top_mat;
struct axisang aa;
if (!gs_valid("gs_matrix_rotaa4f"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat) {
axisang_set(&aa, x, y, z, angle);
matrix4_rotate_aa_i(top_mat, &aa, top_mat);
}
}
void gs_matrix_translate3f(float x, float y, float z)
{
struct matrix4 *top_mat;
struct vec3 p;
if (!gs_valid("gs_matrix_translate3f"))
return;
top_mat = top_matrix(thread_graphics);
if (top_mat) {
vec3_set(&p, x, y, z);
matrix4_translate3v_i(top_mat, &p, top_mat);
}
}
void gs_matrix_scale3f(float x, float y, float z)
{
struct matrix4 *top_mat = top_matrix(thread_graphics);
struct vec3 p;
if (top_mat) {
vec3_set(&p, x, y, z);
matrix4_scale_i(top_mat, &p, top_mat);
}
}
static inline void reset_immediate_arrays(graphics_t *graphics)
{
da_init(graphics->verts);
da_init(graphics->norms);
da_init(graphics->colors);
for (size_t i = 0; i < 16; i++)
da_init(graphics->texverts[i]);
}
void gs_render_start(bool b_new)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_render_start"))
return;
graphics->using_immediate = !b_new;
reset_immediate_arrays(graphics);
if (b_new) {
graphics->vbd = gs_vbdata_create();
} else {
graphics->vbd = gs_vertexbuffer_get_data(
graphics->immediate_vertbuffer);
memset(graphics->vbd->colors, 0xFF,
sizeof(uint32_t) * IMMEDIATE_COUNT);
graphics->verts.array = graphics->vbd->points;
graphics->norms.array = graphics->vbd->normals;
graphics->colors.array = graphics->vbd->colors;
graphics->texverts[0].array = graphics->vbd->tvarray[0].array;
graphics->verts.capacity = IMMEDIATE_COUNT;
graphics->norms.capacity = IMMEDIATE_COUNT;
graphics->colors.capacity = IMMEDIATE_COUNT;
graphics->texverts[0].capacity = IMMEDIATE_COUNT;
}
}
static inline size_t min_size(const size_t a, const size_t b)
{
return (a < b) ? a : b;
}
void gs_render_stop(enum gs_draw_mode mode)
{
graphics_t *graphics = thread_graphics;
size_t i, num;
if (!gs_valid("gs_render_stop"))
return;
num = graphics->verts.num;
if (!num) {
if (!graphics->using_immediate) {
da_free(graphics->verts);
da_free(graphics->norms);
da_free(graphics->colors);
for (i = 0; i < 16; i++)
da_free(graphics->texverts[i]);
gs_vbdata_destroy(graphics->vbd);
}
return;
}
if (graphics->norms.num &&
(graphics->norms.num != graphics->verts.num)) {
blog(LOG_ERROR, "gs_render_stop: normal count does "
"not match vertex count");
num = min_size(num, graphics->norms.num);
}
if (graphics->colors.num &&
(graphics->colors.num != graphics->verts.num)) {
blog(LOG_ERROR, "gs_render_stop: color count does "
"not match vertex count");
num = min_size(num, graphics->colors.num);
}
if (graphics->texverts[0].num &&
(graphics->texverts[0].num != graphics->verts.num)) {
blog(LOG_ERROR, "gs_render_stop: texture vertex count does "
"not match vertex count");
num = min_size(num, graphics->texverts[0].num);
}
if (graphics->using_immediate) {
gs_vertexbuffer_flush(graphics->immediate_vertbuffer);
gs_load_vertexbuffer(graphics->immediate_vertbuffer);
gs_load_indexbuffer(NULL);
gs_draw(mode, 0, (uint32_t)num);
reset_immediate_arrays(graphics);
} else {
gs_vertbuffer_t *vb = gs_render_save();
gs_load_vertexbuffer(vb);
gs_load_indexbuffer(NULL);
gs_draw(mode, 0, 0);
gs_vertexbuffer_destroy(vb);
}
graphics->vbd = NULL;
}
gs_vertbuffer_t *gs_render_save(void)
{
graphics_t *graphics = thread_graphics;
size_t num_tex, i;
if (!gs_valid("gs_render_save"))
return NULL;
if (graphics->using_immediate)
return NULL;
if (!graphics->verts.num) {
gs_vbdata_destroy(graphics->vbd);
return NULL;
}
for (num_tex = 0; num_tex < 16; num_tex++)
if (!graphics->texverts[num_tex].num)
break;
graphics->vbd->points = graphics->verts.array;
graphics->vbd->normals = graphics->norms.array;
graphics->vbd->colors = graphics->colors.array;
graphics->vbd->num = graphics->verts.num;
graphics->vbd->num_tex = num_tex;
if (graphics->vbd->num_tex) {
graphics->vbd->tvarray =
bmalloc(sizeof(struct gs_tvertarray) * num_tex);
for (i = 0; i < num_tex; i++) {
graphics->vbd->tvarray[i].width = 2;
graphics->vbd->tvarray[i].array =
graphics->texverts[i].array;
}
}
reset_immediate_arrays(graphics);
return gs_vertexbuffer_create(graphics->vbd, 0);
}
void gs_vertex2f(float x, float y)
{
struct vec3 v3;
if (!gs_valid("gs_verte"))
return;
vec3_set(&v3, x, y, 0.0f);
gs_vertex3v(&v3);
}
void gs_vertex3f(float x, float y, float z)
{
struct vec3 v3;
if (!gs_valid("gs_vertex3f"))
return;
vec3_set(&v3, x, y, z);
gs_vertex3v(&v3);
}
void gs_normal3f(float x, float y, float z)
{
struct vec3 v3;
if (!gs_valid("gs_normal3f"))
return;
vec3_set(&v3, x, y, z);
gs_normal3v(&v3);
}
static inline bool validvertsize(graphics_t *graphics, size_t num,
const char *name)
{
if (graphics->using_immediate && num == IMMEDIATE_COUNT) {
blog(LOG_ERROR,
"%s: tried to use over %u "
"for immediate rendering",
name, IMMEDIATE_COUNT);
return false;
}
return true;
}
void gs_color(uint32_t color)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_color"))
return;
if (!validvertsize(graphics, graphics->colors.num, "gs_color"))
return;
da_push_back(graphics->colors, &color);
}
void gs_texcoord(float x, float y, int unit)
{
struct vec2 v2;
if (!gs_valid("gs_texcoord"))
return;
vec2_set(&v2, x, y);
gs_texcoord2v(&v2, unit);
}
void gs_vertex2v(const struct vec2 *v)
{
struct vec3 v3;
if (!gs_valid("gs_vertex2v"))
return;
vec3_set(&v3, v->x, v->y, 0.0f);
gs_vertex3v(&v3);
}
void gs_vertex3v(const struct vec3 *v)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_vertex3v"))
return;
if (!validvertsize(graphics, graphics->verts.num, "gs_vertex"))
return;
da_push_back(graphics->verts, v);
}
void gs_normal3v(const struct vec3 *v)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_normal3v"))
return;
if (!validvertsize(graphics, graphics->norms.num, "gs_normal"))
return;
da_push_back(graphics->norms, v);
}
void gs_color4v(const struct vec4 *v)
{
/* TODO */
UNUSED_PARAMETER(v);
}
void gs_texcoord2v(const struct vec2 *v, int unit)
{
graphics_t *graphics = thread_graphics;
if (!gs_valid("gs_texcoord2v"))
return;
if (!validvertsize(graphics, graphics->texverts[unit].num,
"gs_texcoord"))
return;
da_push_back(graphics->texverts[unit], v);
}
input_t *gs_get_input(void)
{
/* TODO */
return NULL;
}
gs_effect_t *gs_get_effect(void)
{
if (!gs_valid("gs_get_effect"))
return NULL;
return thread_graphics ? thread_graphics->cur_effect : NULL;
}
static inline struct gs_effect *find_cached_effect(const char *filename)
{
struct gs_effect *effect = thread_graphics->first_effect;
while (effect) {
if (strcmp(effect->effect_path, filename) == 0)
break;
effect = effect->next;
}
return effect;
}
gs_effect_t *gs_effect_create_from_file(const char *file, char **error_string)
{
char *file_string;
gs_effect_t *effect = NULL;
if (!gs_valid_p("gs_effect_create_from_file", file))
return NULL;
effect = find_cached_effect(file);
if (effect)
return effect;
file_string = os_quick_read_utf8_file(file);
if (!file_string) {
blog(LOG_ERROR, "Could not load effect file '%s'", file);
return NULL;
}
effect = gs_effect_create(file_string, file, error_string);
bfree(file_string);
return effect;
}
gs_effect_t *gs_effect_create(const char *effect_string, const char *filename,
char **error_string)
{
if (!gs_valid_p("gs_effect_create", effect_string))
return NULL;
struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
struct effect_parser parser;
bool success;
effect->graphics = thread_graphics;
effect->effect_path = bstrdup(filename);
ep_init(&parser);
success = ep_parse(&parser, effect, effect_string, filename);
if (!success) {
if (error_string)
*error_string =
error_data_buildstring(&parser.cfp.error_list);
gs_effect_destroy(effect);
effect = NULL;
}
if (effect) {
pthread_mutex_lock(&thread_graphics->effect_mutex);
if (effect->effect_path) {
effect->cached = true;
effect->next = thread_graphics->first_effect;
thread_graphics->first_effect = effect;
}
pthread_mutex_unlock(&thread_graphics->effect_mutex);
}
ep_free(&parser);
return effect;
}
gs_shader_t *gs_vertexshader_create_from_file(const char *file,
char **error_string)
{
if (!gs_valid_p("gs_vertexshader_create_from_file", file))
return NULL;
char *file_string;
gs_shader_t *shader = NULL;
file_string = os_quick_read_utf8_file(file);
if (!file_string) {
blog(LOG_ERROR, "Could not load vertex shader file '%s'", file);
return NULL;
}
shader = gs_vertexshader_create(file_string, file, error_string);
bfree(file_string);
return shader;
}
gs_shader_t *gs_pixelshader_create_from_file(const char *file,
char **error_string)
{
char *file_string;
gs_shader_t *shader = NULL;
if (!gs_valid_p("gs_pixelshader_create_from_file", file))
return NULL;
file_string = os_quick_read_utf8_file(file);
if (!file_string) {
blog(LOG_ERROR, "Could not load pixel shader file '%s'", file);
return NULL;
}
shader = gs_pixelshader_create(file_string, file, error_string);
bfree(file_string);
return shader;
}
gs_texture_t *gs_texture_create_from_file(const char *file)
{
enum gs_color_format format;
uint32_t cx;
uint32_t cy;
uint8_t *data = gs_create_texture_file_data(file, &format, &cx, &cy);
gs_texture_t *tex = NULL;
if (data) {
tex = gs_texture_create(cx, cy, format, 1,
(const uint8_t **)&data, 0);
bfree(data);
}
return tex;
}
static inline void assign_sprite_rect(float *start, float *end, float size,
bool flip)
{
if (!flip) {
*start = 0.0f;
*end = size;
} else {
*start = size;
*end = 0.0f;
}
}
static inline void assign_sprite_uv(float *start, float *end, bool flip)
{
if (!flip) {
*start = 0.0f;
*end = 1.0f;
} else {
*start = 1.0f;
*end = 0.0f;
}
}
static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
float start_u, float end_u, float start_v, float end_v)
{
struct vec2 *tvarray = data->tvarray[0].array;
vec3_zero(data->points);
vec3_set(data->points + 1, fcx, 0.0f, 0.0f);
vec3_set(data->points + 2, 0.0f, fcy, 0.0f);
vec3_set(data->points + 3, fcx, fcy, 0.0f);
vec2_set(tvarray, start_u, start_v);
vec2_set(tvarray + 1, end_u, start_v);
vec2_set(tvarray + 2, start_u, end_v);
vec2_set(tvarray + 3, end_u, end_v);
}
static inline void build_sprite_norm(struct gs_vb_data *data, float fcx,
float fcy, uint32_t flip)
{
float start_u, end_u;
float start_v, end_v;
assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
}
static inline void build_subsprite_norm(struct gs_vb_data *data, float fsub_x,
float fsub_y, float fsub_cx,
float fsub_cy, float fcx, float fcy,
uint32_t flip)
{
float start_u, end_u;
float start_v, end_v;
if ((flip & GS_FLIP_U) == 0) {
start_u = fsub_x / fcx;
end_u = (fsub_x + fsub_cx) / fcx;
} else {
start_u = (fsub_x + fsub_cx) / fcx;
end_u = fsub_x / fcx;
}
if ((flip & GS_FLIP_V) == 0) {
start_v = fsub_y / fcy;
end_v = (fsub_y + fsub_cy) / fcy;
} else {
start_v = (fsub_y + fsub_cy) / fcy;