-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics_context.c
2967 lines (2445 loc) · 134 KB
/
graphics_context.c
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
// TODO(jerry): Both _push_batch_ functions do not handle proper overflow! This can be bad!
// I trust STB to not be buggy
// NOTE(jerry): There are quite a lot of holes in this implementation... Mostly buffer overruns, which
// I have to checkout later.
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic push
#endif
#define STB_IMAGE_IMPLEMENTATION
#include <bundled_external/stb_image.h>
#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#include <bundled_external/stb_truetype.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// prototype for the file watcher.
void _graphics_context_reload_resource(char* file_name, void* user_data);
/////////////////// BEGIN Graphics Abstraction
// This is used to centralize most of the non-significant graphics API code.
// small fry like glViewport, glScissor or one liners are left out, but objects are mostly kept here
// uniforms are also placed here.
// These are redundant but allow me to trace them if I want to.
// These are less efficient since they rebind without caching.
// Hopefully the framerate doesn't get hit.
void _opengl_shader_uniform_set_float1(GLuint shader_program, char* uniform_name, float v0) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform1f(uniform_location, v0);
}
void _opengl_shader_uniform_set_float2(GLuint shader_program, char* uniform_name, float v0, float v1) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform2f(uniform_location, v0, v1);
}
void _opengl_shader_uniform_set_float3(GLuint shader_program, char* uniform_name, float v0, float v1, float v2) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform3f(uniform_location, v0, v1, v2);
}
void _opengl_shader_uniform_set_float4(GLuint shader_program, char* uniform_name, float v0, float v1, float v2, float v3) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform4f(uniform_location, v0, v1, v2, v3);
}
void _opengl_shader_uniform_set_integer1(GLuint shader_program, char* uniform_name, int v0) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform1i(uniform_location, v0);
}
void _opengl_shader_uniform_set_integer2(GLuint shader_program, char* uniform_name, int v0, int v1) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform2i(uniform_location, v0, v1);
}
void _opengl_shader_uniform_set_integer3(GLuint shader_program, char* uniform_name, int v0, int v1, int v2) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform3i(uniform_location, v0, v1, v2);
}
void _opengl_shader_uniform_set_integer4(GLuint shader_program, char* uniform_name, int v0, int v1, int v2, int v3) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniform4i(uniform_location, v0, v1, v2, v3);
}
void _opengl_shader_uniform_set_matrix4x4(GLuint shader_program, char* uniform_name, size_t count, float* matrix) {
glUseProgram(shader_program);
GLuint uniform_location = glGetUniformLocation(shader_program, uniform_name);
glUniformMatrix4fv(uniform_location, count, GL_FALSE, matrix);
}
#if __EMSCRIPTEN__
static char* DEFAULT_VERTEX_SOURCE =
"#version 300 es\n"
"precision highp float;\n"
"layout(location=0) in vec2 vertex_attribute_position;\n"
"layout(location=1) in vec2 vertex_attribute_texcoord;\n"
"layout(location=2) in vec4 vertex_attribute_color;\n"
"out vec2 vertex_position;\n"
"out vec2 vertex_texcoord;\n"
"out vec4 vertex_color;\n"
"uniform mat4 view_matrix;\n"
"uniform mat4 projection_matrix;\n"
"void main(){\n"
// TODO(jerry): enforce row matrices! Although they are less SIMD friendly, but I don't really care about performance right now.
// Semantically, it's easier to understand.
// (because of layout reasons...)
// Personally I use row matrices, but to lessen my work and avoid too much thinking just use column matrix until I get this setup more. (left multiply)
"gl_Position = projection_matrix * view_matrix * vec4(vertex_attribute_position.x, vertex_attribute_position.y, 0.0, 1.0);\n"
"vertex_position = vertex_attribute_position;\n"
"vertex_texcoord = vertex_attribute_texcoord;\n"
"vertex_color = vertex_attribute_color;\n"
"}\n"
;
static char* DEFAULT_FRAGMENT_SOURCE =
"#version 300 es\n"
"precision highp float;\n"
"in vec2 vertex_position;\n"
"in vec2 vertex_texcoord;\n"
"in vec4 vertex_color;\n"
"uniform sampler2D sampler_texture;\n"
"uniform float using_texture;\n"
"out vec4 output_color;\n"
"void main(){\n"
// branchless shader to do texture sampling.
"output_color = mix(vertex_color, texture(sampler_texture, vertex_texcoord) * vertex_color, using_texture);\n"
"}\n"
;
static char* TEXT_FRAGMENT_SOURCE =
"#version 300 es\n"
"precision highp float;\n"
"in vec2 vertex_position;\n"
"in vec2 vertex_texcoord;\n"
"in vec4 vertex_color;\n"
"uniform sampler2D sampler_texture;\n"
"out vec4 output_color;\n"
"void main(){\n"
"float text_alpha = texture(sampler_texture, vertex_texcoord).a;\n"
"output_color = vec4(vertex_color.rgb, text_alpha * vertex_color.a);\n"
"}\n"
;
#else
static char* DEFAULT_VERTEX_SOURCE =
"#version 330\n"
"layout(location=0) in vec2 vertex_attribute_position;\n"
"layout(location=1) in vec2 vertex_attribute_texcoord;\n"
"layout(location=2) in vec4 vertex_attribute_color;\n"
"out vec2 vertex_position;\n"
"out vec2 vertex_texcoord;\n"
"out vec4 vertex_color;\n"
"uniform mat4 view_matrix;\n"
"uniform mat4 projection_matrix;\n"
"void main(){\n"
// TODO(jerry): enforce row matrices! Although they are less SIMD friendly, but I don't really care about performance right now.
// Semantically, it's easier to understand.
// (because of layout reasons...)
// Personally I use row matrices, but to lessen my work and avoid too much thinking just use column matrix until I get this setup more. (left multiply)
"gl_Position = projection_matrix * view_matrix * vec4(vertex_attribute_position.x, vertex_attribute_position.y, 0.0, 1.0);\n"
"vertex_position = vertex_attribute_position;\n"
"vertex_texcoord = vertex_attribute_texcoord;\n"
"vertex_color = vertex_attribute_color;\n"
"}\n"
;
static char* DEFAULT_FRAGMENT_SOURCE =
"#version 330\n"
"in vec2 vertex_position;\n"
"in vec2 vertex_texcoord;\n"
"in vec4 vertex_color;\n"
"uniform sampler2D sampler_texture;\n"
"uniform float using_texture = 0;"
"out vec4 output_color;\n"
"void main(){\n"
// branchless shader to do texture sampling.
"output_color = mix(vertex_color, texture(sampler_texture, vertex_texcoord) * vertex_color, using_texture);\n"
"}\n"
;
static char* TEXT_FRAGMENT_SOURCE =
"#version 330\n"
"in vec2 vertex_position;\n"
"in vec2 vertex_texcoord;\n"
"in vec4 vertex_color;\n"
"uniform sampler2D sampler_texture;\n"
"out vec4 output_color;\n"
"void main(){\n"
"float text_alpha = texture(sampler_texture, vec2(vertex_texcoord.x, vertex_texcoord.y)).r;\n"
"output_color = vec4(vertex_color.rgb, text_alpha * vertex_color.a);\n"
"}\n"
;
#endif
// I really shouldn't assume these are zero-terminated C strings
// I swear this is going to bite me in the ass later.
struct graphics_context_shader_description {
char* vertex_source;
char* fragment_source;
};
#define GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH 8192
struct graphics_context_shader_result {
bool error;
GLuint program;
char vertex_error_log[GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH];
char fragment_error_log[GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH];
char linkage_error_log[GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH];
};
struct graphics_context_shader_result _opengl_create_shader_program(struct graphics_context_shader_description description) {
struct graphics_context_shader_result result = {};
if (description.vertex_source && description.fragment_source) {
GLuint shader_program = glCreateProgram();
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
{
glShaderSource(vertex_shader, 1, (const GLchar* const*)&description.vertex_source, 0);
glCompileShader(vertex_shader);
GLint compilation_status;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compilation_status);
if (compilation_status == GL_FALSE) {
result.error = true;
glGetShaderInfoLog(vertex_shader, GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH, 0, result.vertex_error_log);
}
}
{
glShaderSource(fragment_shader, 1, (const GLchar* const*)&description.fragment_source, 0);
glCompileShader(fragment_shader);
GLint compilation_status;
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &compilation_status);
if (compilation_status == GL_FALSE) {
result.error = true;
glGetShaderInfoLog(fragment_shader, GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH, 0, result.fragment_error_log);
}
}
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
{
GLint link_status;
glGetProgramiv(shader_program, GL_LINK_STATUS, &link_status);
if (link_status == GL_FALSE) {
result.error = true;
glGetProgramInfoLog(shader_program, GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH, 0, result.linkage_error_log);
}
}
glDetachShader(shader_program, vertex_shader);
glDetachShader(shader_program, fragment_shader);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
result.program = shader_program;
} else {
result.error = true;
if (!description.vertex_source) {
snprintf(result.vertex_error_log, GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH, "no vertex shader provided\n");
}
if (!description.fragment_source) {
snprintf(result.fragment_error_log, GRAPHICS_CONTEXT_MAX_SHADER_LOG_LENGTH, "no fragment shader provided\n");
}
}
return result;
}
enum {
// according to https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml
GRAPHICS_CONTEXT_DEFAULT_MIPMAP_LEVEL = 1000,
};
// set to sane defaults... Mostly.
enum graphics_context_texture_filter_type {
GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_DEFAULT, // will probably be nearest
GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_NEAREST,
GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_LINEAR,
};
GLint _map_native_texture_filter_enum_into_opengl(GLint input) {
switch (input) {
case GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_NEAREST: return GL_NEAREST;
case GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_LINEAR: return GL_LINEAR;
}
return input;
}
enum graphics_context_texture_wrap_type {
GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_REPEAT,
GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_CLAMP_TO_EDGE,
GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_CLAMP_TO_BORDER,
GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_MIRROR_REPEAT,
GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_MIRROR_CLAMP_TO_EDGE,
};
GLint _map_native_texture_wrap_enum_into_opengl(GLint input) {
switch (input) {
case GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_REPEAT: return GL_REPEAT;
case GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
case GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
case GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_MIRROR_REPEAT: return GL_MIRRORED_REPEAT;
case GRAPHICS_CONTEXT_TEXTURE_WRAP_TYPE_MIRROR_CLAMP_TO_EDGE: return GL_MIRROR_CLAMP_TO_EDGE;
}
return input;
}
// directly maps to OpenGL
enum graphics_context_texture_format {
GRAPHICS_CONTEXT_TEXTURE_FORMAT_RGBA32,
GRAPHICS_CONTEXT_TEXTURE_FORMAT_RGB24,
GRAPHICS_CONTEXT_TEXTURE_FORMAT_R8,
GRAPHICS_CONTEXT_TEXTURE_FORMAT_RG16,
GRAPHICS_CONTEXT_TEXTURE_FORMAT_BGR24,
GRAPHICS_CONTEXT_TEXTURE_FORMAT_BGRA32,
};
GLint _map_native_texture_format_enum_into_opengl(GLint input) {
switch (input) {
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_RGBA32: return GL_RGBA;
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_RGB24: return GL_RGB;
#if __EMSCRIPTEN__
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_R8: return GL_ALPHA;
#else
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_R8: return GL_RED;
#endif
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_RG16: return GL_RG;
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_BGR24: return GL_BGR;
case GRAPHICS_CONTEXT_TEXTURE_FORMAT_BGRA32: return GL_BGRA;
};
return input;
}
struct graphics_context_texture_description {
GLint base_level;
GLint mipmap_level;
GLint min_filter;
GLint mag_filter;
GLint wrap_t;
GLint wrap_s;
GLint internal_format;
GLint format;
GLsizei unpack_alignment;
};
#define OPENGL_DEFAULT_UNPACK_ALIGNMENT (4)
// A combined gen textures and glTexParameters with image2D.
GLuint _opengl_create_new_texture2d(struct graphics_context_texture_description description, uint32_t image_width, uint32_t image_height, uint8_t* image_pixels) {
if (description.min_filter == GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_DEFAULT) {
description.min_filter = GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_NEAREST;
}
if (description.mag_filter == GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_DEFAULT) {
description.mag_filter = GRAPHICS_CONTEXT_TEXTURE_FILTER_TYPE_NEAREST;
}
if (description.unpack_alignment == 0) {
description.unpack_alignment = OPENGL_DEFAULT_UNPACK_ALIGNMENT;
}
GLuint gl_texture_handle;
glGenTextures(1, &gl_texture_handle);
glBindTexture(GL_TEXTURE_2D, gl_texture_handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, description.base_level);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, description.mipmap_level);
if (description.mipmap_level > 0) {
glGenerateMipmap(GL_TEXTURE_2D);
}
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _map_native_texture_filter_enum_into_opengl(description.min_filter));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _map_native_texture_filter_enum_into_opengl(description.mag_filter));
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _map_native_texture_wrap_enum_into_opengl(description.wrap_t));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _map_native_texture_wrap_enum_into_opengl(description.wrap_s));
glPixelStorei(GL_UNPACK_ALIGNMENT, description.unpack_alignment);
glTexImage2D(GL_TEXTURE_2D,
0,
_map_native_texture_format_enum_into_opengl(description.internal_format),
image_width, image_height, 0,
_map_native_texture_format_enum_into_opengl(description.format),
GL_UNSIGNED_BYTE, image_pixels);
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, OPENGL_DEFAULT_UNPACK_ALIGNMENT);
return gl_texture_handle;
}
GLint _map_native_vertex_attribute_enum_into_opengl(uint8_t input) {
switch (input) {
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_FLOAT: return GL_FLOAT;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_DOUBLE: return GL_DOUBLE;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_INT32: return GL_INT;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_INT16: return GL_SHORT;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_INT8: return GL_BYTE;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT32: return GL_UNSIGNED_INT;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT16: return GL_UNSIGNED_SHORT;
case GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT8: return GL_UNSIGNED_BYTE;
}
return input;
}
size_t _map_index_buffer_index_type_enum_into_element_size(uint8_t input) {
switch (input) {
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT8: return 1;
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT16: return 2;
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT32: return 4;
}
return 0;
}
GLint _map_index_buffer_index_type_enum_into_opengl(uint8_t input) {
switch (input) {
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT8: return GL_UNSIGNED_BYTE;
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT16: return GL_UNSIGNED_SHORT;
case GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT32: return GL_UNSIGNED_INT;
}
return input;
}
GLint _map_native_vertex_buffer_usage_enum_into_opengl(uint8_t input) {
switch (input) {
case GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_STATIC: return GL_STATIC_DRAW;
case GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DYNAMIC: return GL_DYNAMIC_DRAW;
}
return input;
}
struct graphics_context_vertex_buffer _opengl_create_vertex_buffer(struct graphics_context_vertex_buffer_description buffer_description) {
GLuint vertex_buffer_object = 0;
GLuint vertex_array_object = 0;
GLuint index_buffer_object = 0;
glGenBuffers(1, &vertex_buffer_object);
glGenVertexArrays(1, &vertex_array_object);
// initialize buffer.
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object);
{
size_t buffer_size = buffer_description.vertex_size * buffer_description.buffer_count;
if (buffer_description.usage == GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DEFAULT) {
buffer_description.usage = GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_STATIC;
}
glBufferData(GL_ARRAY_BUFFER, buffer_size, buffer_description.buffer_data, _map_native_vertex_buffer_usage_enum_into_opengl(buffer_description.usage));
}
// setup the index buffer.
// if we should
{
// buffer_count != 0 should be sane...
if (buffer_description.index_buffer.buffer_count > 0) {
glGenBuffers(1, &index_buffer_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_object);
size_t element_size = _map_index_buffer_index_type_enum_into_element_size(buffer_description.index_buffer.index_type);
size_t buffer_size = buffer_description.index_buffer.buffer_count * element_size;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer_size, buffer_description.index_buffer.buffer_data, _map_native_vertex_buffer_usage_enum_into_opengl(buffer_description.index_buffer.usage));
}
}
// setup opengl vertex attribute state with the vertex array.
glBindVertexArray(vertex_array_object);
{
for (size_t vertex_attribute_index = 0; vertex_attribute_index < GRAPHICS_CONTEXT_VERTEX_FORMAT_MAX_ATTRIBUTE_COUNT; ++vertex_attribute_index) {
struct graphics_context_vertex_attribute* current_vertex_attribute = &buffer_description.format.attributes[vertex_attribute_index];
bool empty_attribute = true;
// Check for any "non-zero" member.
// Excluding the name which is technically optional here.
{
if (empty_attribute && current_vertex_attribute->element_count) {
empty_attribute = false;
} else if (empty_attribute && current_vertex_attribute->offset) {
empty_attribute = false;
} else if (empty_attribute && current_vertex_attribute->type) {
empty_attribute = false;
}
}
if (!empty_attribute) {
glEnableVertexAttribArray(vertex_attribute_index);
glVertexAttribPointer(vertex_attribute_index,
current_vertex_attribute->element_count,
_map_native_vertex_attribute_enum_into_opengl(current_vertex_attribute->type),
current_vertex_attribute->normalized,
buffer_description.vertex_size,
(void*)current_vertex_attribute->offset);
}
}
}
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return (struct graphics_context_vertex_buffer) {
.description = buffer_description,
.vertex_array_object = vertex_array_object,
.vertex_buffer_object = vertex_buffer_object,
.index_buffer_object = index_buffer_object,
};
}
////////////////////// END of Graphics API Abstractions
struct graphics_context_viewport_rectangle {
float x;
float y;
float w;
float h;
float scale_factor;
};
static bool _should_pillarbox(float width, float height, float virtual_width, float virtual_height) {
float physical_aspect_ratio = width / height;
float logical_aspect_ratio = virtual_width / virtual_height;
return (physical_aspect_ratio > logical_aspect_ratio);
}
// NOTE(jerry):
// I implicitly specced the API according to graphics_context_api to
// have virtual resolution == 0 == real resolution
// but none of this code actually does that so I'm doing the special casing here.
// There's going to be a huge change of stuff anyways, so most of this should be kind of gone.
static float _optimal_aspect_ratio_scale_factor(float width, float height, float virtual_width, float virtual_height) {
if (_should_pillarbox(width, height, virtual_width, virtual_height)) {
return height / virtual_height;
} else {
return width / virtual_width;
}
}
static struct graphics_context_viewport_rectangle _viewport_rectangle_for_virtual_resolution(float width, float height, float virtual_width, float virtual_height) {
if (virtual_height == 0 && virtual_width == 0) {
virtual_height = height;
virtual_width = width;
}
struct graphics_context_viewport_rectangle viewport = {};
// from virtual to real.
float aspect_ratio_scale_factor = _optimal_aspect_ratio_scale_factor(width, height, virtual_width, virtual_height);
viewport.w = (virtual_width * aspect_ratio_scale_factor);
viewport.h = (virtual_height * aspect_ratio_scale_factor);
viewport.x = (width - viewport.w) / 2.0f;
viewport.y = (height - viewport.h) / 2.0f;
viewport.scale_factor = aspect_ratio_scale_factor;
return viewport;
}
static void _graphics_context_memory_usage_printout(struct graphics_context* graphics_context) {
#if 0
{
size_t c = graphics_context->memory_capacity;
console_printf("%llu B (%llu KB), %llu MB capacity\n", c, c / 1024, c / (1024*1024));
size_t u = graphics_context->memory_used + graphics_context->memory_used_top;
if (u > c) {
size_t o = u - c;
console_printf("FATAL: You have overrun memory by: %llu B (%llu KB) %llu MB!\n", o, o/1024, o/(1024*1024));
}
console_printf("%llu B, %llu KB, %llu MB\n", u, u / 1024, u / (1024*1024));
size_t l = c - u;
console_printf("%llu B, %llu KB, %llu MB left\n", l, l / 1024, l / (1024*1024));
}
#endif
}
static void* _graphics_context_push_buffer_unaligned_alloc(struct graphics_context* graphics_context, size_t amount) {
void* result = graphics_context->memory + graphics_context->memory_used;
graphics_context->memory_used += amount;
size_t total_used = graphics_context->memory_used + graphics_context->memory_used_top;
// This assertion will fire until we start asking for more memory blocks I suppose.
_graphics_context_memory_usage_printout(graphics_context);
assertion(total_used < graphics_context->memory_capacity && "(forward alloc)Run out of graphics_context memory!");
return result;
}
static void* _graphics_context_push_buffer_unaligned_alloc_top(struct graphics_context* graphics_context, size_t amount) {
graphics_context->memory_used_top += amount;
void* result = (graphics_context->memory + graphics_context->memory_capacity) - graphics_context->memory_used_top;
size_t total_used = graphics_context->memory_used + graphics_context->memory_used_top;
_graphics_context_memory_usage_printout(graphics_context);
assertion(total_used < graphics_context->memory_capacity && "(top alloc)Run out of graphics_context memory!");
return result;
}
static void* _graphics_context_read_file_into_buffer(struct graphics_context* graphics_context, char* file_location) {
size_t file_buffer_length = get_file_size(file_location);
void* new_file_buffer = _graphics_context_push_buffer_unaligned_alloc_top(graphics_context, file_buffer_length);
read_file_into_buffer(file_location, new_file_buffer, file_buffer_length);
_graphics_context_memory_usage_printout(graphics_context);
return new_file_buffer;
}
// NOTE(jerry): A macro can easily be used to make this more ergonomic, however I don't do that here.
struct graphics_context_allocation_result graphics_context_push_buffer_allocate_forward(struct graphics_context* graphics_context, size_t amount) {
size_t marker_location = graphics_context->memory_used;
void* memory = _graphics_context_push_buffer_unaligned_alloc(graphics_context, amount);
struct graphics_context_allocation_result result = (struct graphics_context_allocation_result) {
.memory = memory,
.restoration_marker = (graphics_context_push_buffer_marker) {
.region_type = GRAPHICS_CONTEXT_PUSH_BUFFER_REGION_TYPE_FORWARD,
.marker = marker_location
}
};
#ifdef DEBUG_BUILD
if (amount > 0) {
graphics_context->top_markers[graphics_context->top_marker_count++] = result.restoration_marker;
}
#endif
return result;
}
struct graphics_context_allocation_result graphics_context_push_buffer_allocate_top(struct graphics_context* graphics_context, size_t amount) {
size_t marker_location = graphics_context->memory_used_top;
void* memory = _graphics_context_push_buffer_unaligned_alloc_top(graphics_context, amount);
struct graphics_context_allocation_result result = (struct graphics_context_allocation_result) {
.memory = memory,
.restoration_marker = (graphics_context_push_buffer_marker) {
.region_type = GRAPHICS_CONTEXT_PUSH_BUFFER_REGION_TYPE_TOP,
.marker = marker_location
}
};
#ifdef DEBUG_BUILD
if (amount > 0) {
graphics_context->forward_markers[graphics_context->forward_marker_count++] = result.restoration_marker;
}
#endif
return result;
}
void graphics_context_push_buffer_restore(struct graphics_context* graphics_context, graphics_context_push_buffer_marker marker) {
switch (marker.region_type) {
case GRAPHICS_CONTEXT_PUSH_BUFFER_REGION_TYPE_TOP: {
#ifdef DEBUG_BUILD
if (graphics_context->top_marker_count > 0) {
graphics_context_push_buffer_marker* most_recent_marker = &graphics_context->top_markers[graphics_context->top_marker_count-1];
assert(most_recent_marker->marker == marker.marker && "You are freeing push buffer allocations out of order!");
}
#endif
graphics_context->memory_used_top = marker.marker;
} break;
case GRAPHICS_CONTEXT_PUSH_BUFFER_REGION_TYPE_FORWARD: {
#ifdef DEBUG_BUILD
if (graphics_context->forward_marker_count > 0) {
graphics_context_push_buffer_marker* most_recent_marker = &graphics_context->forward_markers[graphics_context->forward_marker_count-1];
assert(most_recent_marker->marker == marker.marker && "You are freeing push buffer allocations out of order!");
}
#endif
graphics_context->memory_used = marker.marker;
} break;
}
}
enum graphics_context_resource_type {
GRAPHICS_CONTEXT_RESOURCE_TYPE_TEXTURE,
GRAPHICS_CONTEXT_RESOURCE_TYPE_SHADER,
/* GRAPHICS_CONTEXT_RESOURCE_TYPE_FONT, */
};
struct graphics_context_file_watcher_user_data_packet {
struct graphics_context* context;
uint8_t resource_type;
union {
graphics_context_texture_handle texture;
graphics_context_shader_handle shader;
};
};
void graphics_context_initialize(struct graphics_context* graphics_context, struct graphics_context_limits limits) {
graphics_context->memory_capacity = limits.memory_capacity;
graphics_context->memory_used = 0;
graphics_context->memory_used_top = 0;
graphics_context->memory = system_memory_allocate(graphics_context->memory_capacity);
// Ensure these are always a POW2
graphics_context->shader_capacity = 4096; // TODO(jerry)
graphics_context->texture_capacity = limits.textures;
graphics_context->font_capacity = limits.fonts;
graphics_context->spare_glyph_cache_capacity = limits.glyph_cache;
graphics_context->quad_batch_capacity = limits.batch_quads;
graphics_context->immediate_vertices_count = 0;
graphics_context->immediate_indices_count = 0;
graphics_context->quad_batch_count = 0;
graphics_context->font_count = 0;
graphics_context->texture_count = 0;
graphics_context->shaders = _graphics_context_push_buffer_unaligned_alloc(graphics_context, graphics_context->shader_capacity * sizeof(*graphics_context->shaders));
graphics_context->textures = _graphics_context_push_buffer_unaligned_alloc(graphics_context, graphics_context->texture_capacity * sizeof(*graphics_context->textures));
graphics_context->fonts = _graphics_context_push_buffer_unaligned_alloc(graphics_context, graphics_context->font_capacity * sizeof(*graphics_context->fonts));
graphics_context->glyph_owners = _graphics_context_push_buffer_unaligned_alloc(graphics_context, graphics_context->spare_glyph_cache_capacity * sizeof(*graphics_context->glyph_owners));
graphics_context->spare_glyph_cache = _graphics_context_push_buffer_unaligned_alloc(graphics_context, graphics_context->spare_glyph_cache_capacity * sizeof(*graphics_context->spare_glyph_cache));
// NOTE(jerry): This uses... Way more memory.
graphics_context->batch_vertices = _graphics_context_push_buffer_unaligned_alloc(graphics_context, (graphics_context->quad_batch_capacity*4 + SPACE_FOR_GENERIC_SHAPE_VERTICES) * sizeof(*graphics_context->batch_vertices));
graphics_context->batch_indices = _graphics_context_push_buffer_unaligned_alloc(graphics_context, (graphics_context->quad_batch_capacity*6 + SPACE_FOR_GENERIC_SHAPE_VERTICES) * sizeof(*graphics_context->batch_indices));
// modern opengl means... Shaders
graphics_context->batch_quads_shader_program = graphics_context_load_shader_from_source(graphics_context, DEFAULT_VERTEX_SOURCE, DEFAULT_FRAGMENT_SOURCE);
graphics_context->text_shader_program = graphics_context_load_shader_from_source(graphics_context, DEFAULT_VERTEX_SOURCE, TEXT_FRAGMENT_SOURCE);
// This assumes you always interleave your data, and I will work with that
// assumption always, since it's simplest and doesn't really seem to break performance.
{
struct graphics_context_vertex_buffer buffer = _opengl_create_vertex_buffer(
(struct graphics_context_vertex_buffer_description) {
.vertex_size = sizeof(struct graphics_context_default_vertex_format),
.buffer_count = 4 * graphics_context->quad_batch_capacity + SPACE_FOR_GENERIC_SHAPE_VERTICES,
.buffer_data = NULL,
.usage = GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DYNAMIC,
.index_buffer = {
.index_type = GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT16,
.buffer_count = 6 * graphics_context->quad_batch_capacity + SPACE_FOR_GENERIC_SHAPE_VERTICES,
.buffer_data = NULL,
.usage = GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DYNAMIC,
},
.format = {
.attributes[0] = { .name = "vertex_position", .element_count = 2, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_FLOAT, },
.attributes[1] = { .name = "vertex_texcoord", .element_count = 2, .normalized = true, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_INT16, .offset = offsetof(struct graphics_context_default_vertex_format, texture_coordinate_u)},
.attributes[2] = { .name = "vertex_colors" , .element_count = 4, .normalized = true, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT8, .offset = offsetof(struct graphics_context_default_vertex_format, r)},
// These extras are meant to be used as flags.
/* .attributes[3] = { .name = "extra_set1", .element_count = 4, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT8, .offset = offsetof(struct graphics_context_default_vertex_format, extra0)}, */
}
}
);
graphics_context->batch_vertex_buffer = buffer;
}
// text vertex buffer
{
struct graphics_context_vertex_buffer buffer = _opengl_create_vertex_buffer(
(struct graphics_context_vertex_buffer_description) {
.vertex_size = sizeof(struct graphics_context_default_vertex_format),
.buffer_count = 4 ,
.buffer_data = NULL,
.usage = GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DYNAMIC,
.index_buffer = {
.index_type = GRAPHICS_CONTEXT_INDEX_BUFFER_INDEX_TYPE_UINT16,
.buffer_count = 6,
.buffer_data = NULL,
.usage = GRAPHICS_CONTEXT_VERTEX_BUFFER_USAGE_TYPE_DYNAMIC,
},
.format = {
.attributes[0] = { .name = "vertex_position", .element_count = 2, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_FLOAT, },
.attributes[1] = { .name = "vertex_texcoord", .element_count = 2, .normalized = true, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_INT16, .offset = offsetof(struct graphics_context_default_vertex_format, texture_coordinate_u)},
.attributes[2] = { .name = "vertex_colors" , .element_count = 4, .normalized = true, .type = GRAPHICS_CONTEXT_VERTEX_ATTRIBUTE_TYPE_UINT8, .offset = offsetof(struct graphics_context_default_vertex_format, r)},
}
}
);
graphics_context->text_vertex_buffer = buffer;
}
_graphics_context_memory_usage_printout(graphics_context);
}
void graphics_context_update_shader_uniform(struct graphics_context* graphics_context, graphics_context_shader_handle shader, char* uniform_name, struct graphics_context_shader_uniform_parameter uniform_data) {
if (!uniform_data.count) {
return;
}
struct graphics_context_shader* shader_resource = &graphics_context->shaders[shader.id];
GLuint shader_program_object = shader_resource->program;
switch (uniform_data.type) {
case GRAPHICS_CONTEXT_SHADER_UNIFORM_PARAMETER_TYPE_FLOAT: {
switch (uniform_data.count) {
case 1: {
_opengl_shader_uniform_set_float1(shader_program_object, uniform_name, uniform_data.floating_point.values[0]);
} break;
case 2: {
_opengl_shader_uniform_set_float2(shader_program_object, uniform_name, uniform_data.floating_point.values[0], uniform_data.floating_point.values[1]);
} break;
case 3: {
_opengl_shader_uniform_set_float3(shader_program_object, uniform_name, uniform_data.floating_point.values[0], uniform_data.floating_point.values[1], uniform_data.floating_point.values[2]);
} break;
case 4: {
_opengl_shader_uniform_set_float4(shader_program_object, uniform_name, uniform_data.floating_point.values[0], uniform_data.floating_point.values[1], uniform_data.floating_point.values[2], uniform_data.floating_point.values[3]);
} break;
}
} break;
case GRAPHICS_CONTEXT_SHADER_UNIFORM_PARAMETER_TYPE_INTEGER: {
switch (uniform_data.count) {
case 1: {
_opengl_shader_uniform_set_integer1(shader_program_object, uniform_name, uniform_data.integer.values[0]);
} break;
case 2: {
_opengl_shader_uniform_set_integer2(shader_program_object, uniform_name, uniform_data.integer.values[0], uniform_data.integer.values[1]);
} break;
case 3: {
_opengl_shader_uniform_set_integer3(shader_program_object, uniform_name, uniform_data.integer.values[0], uniform_data.integer.values[1], uniform_data.integer.values[2]);
} break;
case 4: {
_opengl_shader_uniform_set_integer4(shader_program_object, uniform_name, uniform_data.integer.values[0], uniform_data.integer.values[1], uniform_data.integer.values[2], uniform_data.integer.values[3]);
} break;
}
} break;
case GRAPHICS_CONTEXT_SHADER_UNIFORM_PARAMETER_TYPE_MATRIX: {
_opengl_shader_uniform_set_matrix4x4(shader_program_object, uniform_name, uniform_data.count, uniform_data.matrix.data);
} break;
}
}
static void _graphics_context_upload_matrix_uniform_to_all_shader_programs(struct graphics_context* graphics_context, char* name, float* matrix) {
for (size_t shader_index = 1; shader_index <= graphics_context->shader_count; ++shader_index) {
_opengl_shader_uniform_set_matrix4x4(graphics_context->shaders[shader_index].program, name, 1, matrix);
}
}
void graphics_context_set_orthographic_projection(struct graphics_context* graphics_context, float left, float top, float right, float bottom, float near, float far) {
float scale_x = (2.0f / (right - left));
float scale_y = (2.0f / (top - bottom));
float scale_z = (-2.0f / (far - near));
float translation_x = -((right + left) / (right - left));
float translation_y = -((top + bottom) / (top - bottom));
float translation_z = -((far + near) / (far - near));
float orthographic_matrix[] = {
scale_x, 0, 0, 0,
0, scale_y, 0, 0,
0, 0, scale_z, 0,
translation_x, translation_y, translation_z, 1
};
memcpy(graphics_context->matrices.projection, orthographic_matrix, sizeof(orthographic_matrix));
_graphics_context_upload_matrix_uniform_to_all_shader_programs(graphics_context, "projection_matrix", graphics_context->matrices.projection);
}
static uint16_t _graphics_context_search_for_empty_texture_slot(struct graphics_context* graphics_context) {
uint16_t slot_index = 1;
assertion(graphics_context->texture_count < graphics_context->texture_capacity && "Somehow we tried to prune for texture slots even though we should have no more textures!");
for(; slot_index < graphics_context->texture_capacity; ++slot_index) {
struct graphics_context_texture* texture_resource = &graphics_context->textures[slot_index];
if (texture_resource->status == GRAPHICS_CONTEXT_RESOURCE_STATUS_READY || texture_resource->is_render_target) {
continue;
} else {
return slot_index;
}
}
return 0;
}
static uint16_t _graphics_context_search_for_unloaded_texture_with_pathname_hashed(struct graphics_context* graphics_context, char* file_location) {
uint64_t hash_key = fnv1a_hash(file_location, cstring_length(file_location));
uint16_t resource_hash_index = hash_key & (graphics_context->texture_capacity - 1);
struct graphics_context_texture* texture_resource = &graphics_context->textures[resource_hash_index];
if (strncmp(texture_resource->file_path, file_location, BLACKIRON_MAX_PLATFORM_PATHNAME_LENGTH-1) == 0) {
return resource_hash_index;
}
// I literally have no idea how or why this can possibly happen, but I'll
// thank myself if this ever happens.
assertion(graphics_context->texture_count < graphics_context->texture_capacity && "Somehow we tried to prune for texture slots even though we should have no more textures!");
{
while (texture_resource->status == GRAPHICS_CONTEXT_RESOURCE_STATUS_READY || texture_resource->is_render_target) {
resource_hash_index++;
if (resource_hash_index >= graphics_context->texture_capacity) {
resource_hash_index = 0;
}
texture_resource = &graphics_context->textures[resource_hash_index];
}
}
return resource_hash_index;
}
// this is technically not optimal since I think this one will waste the
// most space, but it should guarantee a fit.
int64_t _graphics_optimal_power_of_two_image_size(int width, int height) {
if (width > height) {
return next_power_of_two(width);
} else {
return next_power_of_two(height);
}
}
// temporary memory
// move this to common or something and make it take an allocator instead.
enum resample_bitmap_flags {
RESAMPLE_BITMAP_FLIP_H = BIT(0),
RESAMPLE_BITMAP_FLIP_V = BIT(1),
};
// This is mostly inlined and unrolled to one level because it would likely be faster in debug
// this was not profiled, and it's not really super inconvenient so eh.
// This image resampler will take a non power of two image, and pad it from it's extents to get a power of two
// image. This is done to support older hardware and webGL or anything that doesn't allow NPOT natively.
// This is done for fonts(their texture glyphs specifically.) or textures when used as resources,
// should not be used on render targets.
static uint8_t* _resample_bitmap_to_next_power_of_two_ext(struct graphics_context* graphics_context, uint32_t square_size, uint8_t* original_buffer, uint32_t original_width, uint32_t original_height, uint8_t components, uint8_t flags) {
size_t allocation_size = square_size*square_size * components;
struct graphics_context_allocation_result allocation = graphics_context_push_buffer_allocate_forward(graphics_context, allocation_size);
// make sure to zero out memory to make sure nothing weird shows up.
uint8_t* pixel_buffer = allocation.memory;
memset(pixel_buffer, 0, allocation_size);
bool flip_h = (flags & RESAMPLE_BITMAP_FLIP_H);
bool flip_v = (flags & RESAMPLE_BITMAP_FLIP_V);
int sample_y;
int sample_x;
switch (components) {
case 1: {
for (int y = 0; y < original_height; ++y) {
sample_y = y;
if (flip_v) {
sample_y = (original_height - y);
}
for (int x = 0; x < original_width; ++x) {
sample_x = x;
if (flip_h) {
sample_x = (original_width - x);
}
pixel_buffer[y * square_size + x] = original_buffer[sample_y * original_width + sample_x];
}
}
} break;
case 3: {
for (int y = 0; y < original_height; ++y) {
sample_y = y;
if (flip_v) {
sample_y = (original_height - y);
}
for (int x = 0; x < original_width; ++x) {
sample_x = x;
if (flip_h) {
sample_x = (original_width - x);
}
pixel_buffer[y * square_size*3 + x*3 + 0] = original_buffer[sample_y * original_width*3 + sample_x*3 + 0];
pixel_buffer[y * square_size*3 + x*3 + 1] = original_buffer[sample_y * original_width*3 + sample_x*3 + 1];
pixel_buffer[y * square_size*3 + x*3 + 2] = original_buffer[sample_y * original_width*3 + sample_x*3 + 2];
}
}
} break;
case 4: {
for (int y = 0; y < original_height; ++y) {
sample_y = y;
if (flip_v) {
sample_y = (original_height - y);
}
for (int x = 0; x < original_width; ++x) {
sample_x = x;
if (flip_h) {
sample_x = (original_width - x);
}
pixel_buffer[y * square_size*4 + x*4 + 0] = original_buffer[sample_y * original_width*4 + sample_x*4 + 0];
pixel_buffer[y * square_size*4 + x*4 + 1] = original_buffer[sample_y * original_width*4 + sample_x*4 + 1];
pixel_buffer[y * square_size*4 + x*4 + 2] = original_buffer[sample_y * original_width*4 + sample_x*4 + 2];
pixel_buffer[y * square_size*4 + x*4 + 3] = original_buffer[sample_y * original_width*4 + sample_x*4 + 3];
}
}
} break;
}
// As long as we don't write into the pointer this is safe to just "free".
// TODO(jerry): to be more robust, I may actually want to return this marker and just free it later.