-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatostim.c
More file actions
1539 lines (1098 loc) · 42 KB
/
datostim.c
File metadata and controls
1539 lines (1098 loc) · 42 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) 2025 Cyrille Rossant and contributors. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for details.
* SPDX-License-Identifier: MIT
*/
/*************************************************************************************************/
/* Imports */
/*************************************************************************************************/
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <cglm/cglm.h>
#include <datoviz_app.h>
#include <datoviz_defaults.h>
#include <datoviz_protocol.h>
#include <datoviz_types.h>
#include "datostim.h"
/*************************************************************************************************/
/* Constants */
/*************************************************************************************************/
#define DSTIM_DEFAULT_WIDTH 960
#define DSTIM_DEFAULT_HEIGHT 400
#define DSTIM_DEFAULT_BACKGROUND 127, 127, 127, 255
#define DSTIM_DEFAULT_SQUARE_WIDTH 100
#define DSTIM_DEFAULT_SQUARE_HEIGHT 100
#define DSTIM_MAX_SCREENS 8
#define DSTIM_MAX_LAYERS 16
#define DSTIM_DEFAULT_SQUARE_COLOR 0, 255, 255, 255
#define DSTIM_ALTERNATIVE_SQUARE_COLOR 255, 255, 0, 255
#define SQUARE_VERTEX_COUNT 6
/*************************************************************************************************/
/* Macros */
/*************************************************************************************************/
#define GET_SCREEN \
if (screen_idx >= DSTIM_MAX_SCREENS) \
{ \
log_error("screen_idx must be lower than %d", DSTIM_MAX_SCREENS); \
return; \
} \
stim->screen_count = MAX(stim->screen_count, screen_idx + 1); \
DScreen* screen = &stim->screens[screen_idx];
#define GET_LAYER \
if (layer_idx >= DSTIM_MAX_LAYERS) \
{ \
log_error("layer_idx must be lower than %d", DSTIM_MAX_LAYERS); \
return; \
} \
stim->layer_count = MAX(stim->layer_count, layer_idx + 1); \
DLayer* layer = &stim->layers[layer_idx];
#define TOUCH_LAYER layer->is_dirty = true;
#define TOUCH_LAYER_TEXTURE layer->is_texture_dirty = true;
/*************************************************************************************************/
/* Typedefs */
/*************************************************************************************************/
typedef struct DScreen DScreen;
typedef struct DLayer DLayer;
typedef struct DStim DStim;
typedef struct DStimSquareVertex DStimSquareVertex;
typedef struct DStimVertex DStimVertex;
typedef struct DStimPush DStimPush;
// typedef struct DStimParams DStimParams;
/*************************************************************************************************/
/* Enums */
/*************************************************************************************************/
typedef enum
{
LOG_TRACE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
} LogLevel;
/*************************************************************************************************/
/* Logging */
/*************************************************************************************************/
static const char* level_names[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR"};
static const char* level_colors[] = {"\x1b[90m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m"};
void log_log(LogLevel level, const char* fmt, ...)
{
// Time string
char timebuf[20];
time_t t = time(NULL);
struct tm* lt = localtime(&t);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", lt);
// Variadic args
va_list args;
va_start(args, fmt);
// Print to stdout with color and formatting
fprintf(stderr, "%s[%s] %s: ", level_colors[level], timebuf, level_names[level]);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\x1b[0m\n");
va_end(args);
}
// Shorthand macros
#define log_trace(...) log_log(LOG_TRACE, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __VA_ARGS__)
/*************************************************************************************************/
/* Structs */
/*************************************************************************************************/
struct DScreen
{
uvec2 offset;
uvec2 size;
mat4 projection;
};
struct DLayer
{
mat4 view;
vec2 tex_offset;
vec2 tex_size;
int mask;
cvec4 min_color;
cvec4 max_color;
float tex_angle;
// texture width and height
uint32_t tex_width;
uint32_t tex_height;
// Texture data.
DvzSize tex_nbytes;
uint8_t* rgba;
DvzFormat format;
DStimInterpolation interpolation;
DStimBlend blend;
bool is_periodic;
bool is_visible; // false by default
bool is_blank; // need to prepare the pipeline
bool is_dirty; // need to update the layer's parameters with push constant
bool is_texture_dirty; // need to upload the texture data again
};
struct DStim
{
DvzApp* app;
DvzBatch* batch;
// Window size.
uint32_t width;
uint32_t height;
DvzId canvas_id;
DvzId background_graphics_id;
DvzId background_vertex_id;
DvzId background_params_id;
DvzId square_graphics_id;
DvzId square_vertex_id;
DvzId square_params_id;
// NOTE: for now, 1 graphics pipeline per layer to support multiple fixed states and texture
// bindings (multiple descriptors per pipeline not yet supported by Datoviz Rendering
// Protocol).
DvzId sphere_graphics_ids[DSTIM_MAX_LAYERS];
DvzId sphere_vertex_id;
DvzId sphere_index_id;
// NOTE: 1 texture and sampler per layer (hence, per sphere graphics pipeline).
DvzId texture_ids[DSTIM_MAX_LAYERS];
DvzId sampler_ids[DSTIM_MAX_LAYERS];
mat4 model;
uint32_t sphere_index_count;
uint32_t screen_count;
DScreen screens[DSTIM_MAX_SCREENS];
uint32_t layer_count;
DLayer layers[DSTIM_MAX_LAYERS];
};
/*************************************************************************************************/
/* GPU structs */
/*************************************************************************************************/
struct DStimSquareVertex
{
vec3 pos;
};
struct DStimVertex
{
vec3 vertexPos;
vec2 vertexUV;
};
// NOTE: maxPushConstantsSize needs to be >= 256 on the GPU
struct DStimPush
{
mat4 model;
mat4 view;
mat4 projection;
vec4 min_color;
vec4 max_color;
vec2 tex_offset;
vec2 tex_size;
float tex_angle;
};
/*************************************************************************************************/
/* Utils */
/*************************************************************************************************/
static void* _cpy(DvzSize size, const void* data)
{
if (data == NULL)
return NULL;
void* data_cpy = malloc(size);
memcpy(data_cpy, data, size);
return data_cpy;
}
static void* read_file(const char* filename, DvzSize* size)
{
/* The returned pointer must be freed by the caller. */
void* buffer = NULL;
DvzSize length = 0;
FILE* f = fopen(filename, "rb");
if (!f)
{
printf("Could not find %s.\n", filename);
return NULL;
}
fseek(f, 0, SEEK_END);
length = (DvzSize)ftell(f);
if (size != NULL)
*size = length;
fseek(f, 0, SEEK_SET);
// NOTE: for safety, add a zero byte at the end to ensure a text file is
// loaded into a 0-terminated string.
buffer = (void*)calloc((size_t)length + 1, 1);
fread(buffer, 1, (size_t)length, f);
fclose(f);
return buffer;
}
static void set_shaders_glsl(
DvzBatch* batch, DvzId graphics_id, const char* vertex_filename, const char* fragment_filename)
{
// Vertex shader.
char* vertex_glsl = read_file(vertex_filename, NULL);
DvzRequest req = dvz_create_glsl(batch, DVZ_SHADER_VERTEX, vertex_glsl);
FREE(vertex_glsl);
// Assign the shader to the graphics pipe.
DvzId square_vertex = req.id;
dvz_set_shader(batch, graphics_id, square_vertex);
// Fragment shader.
char* fragment_glsl = read_file(fragment_filename, NULL);
req = dvz_create_glsl(batch, DVZ_SHADER_FRAGMENT, fragment_glsl);
FREE(fragment_glsl);
// Assign the shader to the graphics pipe.
DvzId fragment_id = req.id;
dvz_set_shader(batch, graphics_id, fragment_id);
}
static void set_shaders_spv(
DvzBatch* batch, DvzId graphics_id, const char* vertex_filename, const char* fragment_filename)
{
// Vertex shader.
DvzSize vertex_size = 0;
unsigned char* vertex_spv = read_file(vertex_filename, &vertex_size);
DvzRequest req = dvz_create_spirv(batch, DVZ_SHADER_VERTEX, vertex_size, vertex_spv);
FREE(vertex_spv);
// Assign the shader to the graphics pipe.
DvzId square_vertex = req.id;
dvz_set_shader(batch, graphics_id, square_vertex);
// Fragment shader.
DvzSize fragment_size = 0;
unsigned char* fragment_spv = read_file(fragment_filename, &fragment_size);
req = dvz_create_spirv(batch, DVZ_SHADER_FRAGMENT, fragment_size, fragment_spv);
FREE(fragment_spv);
// Assign the shader to the graphics pipe.
DvzId fragment_id = req.id;
dvz_set_shader(batch, graphics_id, fragment_id);
}
/*************************************************************************************************/
/* Helpers */
/*************************************************************************************************/
static DvzId create_square_pipeline(DvzBatch* batch)
{
// Create a custom graphics.
DvzRequest req = dvz_create_graphics(batch, DVZ_GRAPHICS_CUSTOM, 0);
DvzId graphics_id = req.id;
set_shaders_spv(batch, graphics_id, "shaders/square.vert.spv", "shaders/square.frag.spv");
// Primitive topology.
dvz_set_primitive(batch, graphics_id, DVZ_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
// Polygon mode.
dvz_set_polygon(batch, graphics_id, DVZ_POLYGON_MODE_FILL);
// Vertex binding.
dvz_set_vertex(batch, graphics_id, 0, sizeof(DStimSquareVertex), DVZ_VERTEX_INPUT_RATE_VERTEX);
// Vertex attrs.
dvz_set_attr(
batch, graphics_id, 0, 0, DVZ_FORMAT_R32G32B32_SFLOAT, offsetof(DStimSquareVertex, pos));
// Slots.
dvz_set_slot(batch, graphics_id, 0, DVZ_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
return graphics_id;
}
static DvzId create_sphere_pipeline(DvzBatch* batch)
{
// Create a custom graphics.
DvzRequest req = dvz_create_graphics(batch, DVZ_GRAPHICS_CUSTOM, 0);
DvzId graphics_id = req.id;
set_shaders_spv(batch, graphics_id, "shaders/sphere.vert.spv", "shaders/sphere.frag.spv");
// Primitive topology.
dvz_set_primitive(batch, graphics_id, DVZ_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
// DEBUG
// dvz_set_primitive(batch, graphics_id, DVZ_PRIMITIVE_TOPOLOGY_POINT_LIST);
// dvz_set_cull(batch, graphics_id, DVZ_CULL_MODE_BACK);
dvz_set_front(batch, graphics_id, DVZ_FRONT_FACE_CLOCKWISE);
// Polygon mode.
dvz_set_polygon(batch, graphics_id, DVZ_POLYGON_MODE_FILL);
// Vertex binding.
dvz_set_vertex(batch, graphics_id, 0, sizeof(DStimVertex), DVZ_VERTEX_INPUT_RATE_VERTEX);
// Vertex attrs.
dvz_set_attr(
batch, graphics_id, 0, 0, //
DVZ_FORMAT_R32G32B32_SFLOAT, offsetof(DStimVertex, vertexPos));
dvz_set_attr(
batch, graphics_id, 0, 1, //
DVZ_FORMAT_R32G32_SFLOAT, offsetof(DStimVertex, vertexUV));
// Slots.
dvz_set_slot(batch, graphics_id, 0, DVZ_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
// Push constants.
dvz_set_push(
batch, graphics_id, DVZ_SHADER_VERTEX | DVZ_SHADER_FRAGMENT, 0, sizeof(DStimPush));
return graphics_id;
}
// In normalized device coordinates (whole window = [-1..+1]).
static void upload_rectangle(DvzBatch* batch, DvzId vertex_id, vec2 offset, vec2 shape)
{
ANN(batch);
ASSERT(vertex_id != DVZ_ID_NONE);
float x = offset[0];
float y = offset[1];
float w = shape[0];
float h = shape[1];
DStimSquareVertex data[] = {
// lower triangle
{{x, y, 0}},
{{x + w, y, 0}},
{{x, y + h, 0}}, //
// upper triangle
{{x + w, y + h, 0}},
{{x, y + h, 0}},
{{x + w, y, 0}},
};
DvzRequest req = dvz_upload_dat(batch, vertex_id, 0, sizeof(data), data, 0);
}
static void rectangle_color(
DvzBatch* batch, DvzId params_id, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
ANN(batch);
ASSERT(params_id != DVZ_ID_NONE);
// NOTE: from uint8_t to float [0.0, 1.0] for GPU uniform
vec4 color = {red / 255.0, green / 255.0, blue / 255.0, alpha / 255.0};
DvzRequest req = dvz_upload_dat(batch, params_id, 0, sizeof(vec4), &color, 0);
}
/*************************************************************************************************/
/* DStim helper functions */
/*************************************************************************************************/
static void create_background(DStim* stim)
{
ANN(stim);
DvzBatch* batch = stim->batch;
ANN(batch);
// Create the graphics pipelines.
stim->background_graphics_id = create_square_pipeline(batch);
// Create the vertex buffer dat for the background.
DvzRequest req = dvz_create_dat(
batch, DVZ_BUFFER_TYPE_VERTEX, SQUARE_VERTEX_COUNT * sizeof(DStimSquareVertex),
DVZ_DAT_FLAGS_PERSISTENT_STAGING);
stim->background_vertex_id = req.id;
req = dvz_bind_vertex(batch, stim->background_graphics_id, 0, stim->background_vertex_id, 0);
// UBO.
req = dvz_create_dat(
batch, DVZ_BUFFER_TYPE_UNIFORM, sizeof(vec4), DVZ_DAT_FLAGS_PERSISTENT_STAGING);
stim->background_params_id = req.id;
req = dvz_bind_dat(batch, stim->background_graphics_id, 0, stim->background_params_id, 0);
}
static void create_square(DStim* stim)
{
ANN(stim);
DvzBatch* batch = stim->batch;
ANN(batch);
// Create the graphics pipelines.
stim->square_graphics_id = create_square_pipeline(batch);
// Create the vertex buffer dat for the square.
DvzRequest req = dvz_create_dat(
batch, DVZ_BUFFER_TYPE_VERTEX, SQUARE_VERTEX_COUNT * sizeof(DStimSquareVertex),
DVZ_DAT_FLAGS_PERSISTENT_STAGING);
stim->square_vertex_id = req.id;
req = dvz_bind_vertex(batch, stim->square_graphics_id, 0, stim->square_vertex_id, 0);
// UBO.
req = dvz_create_dat(
batch, DVZ_BUFFER_TYPE_UNIFORM, sizeof(vec4), DVZ_DAT_FLAGS_PERSISTENT_STAGING);
stim->square_params_id = req.id;
req = dvz_bind_dat(batch, stim->square_graphics_id, 0, stim->square_params_id, 0);
}
static void create_sphere_vertex_buffer(DStim* stim, uint32_t sphere_vertex_count)
{
ANN(stim);
DvzBatch* batch = stim->batch;
ANN(batch);
// Create the vertex buffer dat for the sphere.
DvzRequest req = dvz_create_dat(
batch, DVZ_BUFFER_TYPE_VERTEX, sphere_vertex_count * sizeof(DStimVertex), 0);
stim->sphere_vertex_id = req.id;
}
static void bind_sphere_vertex_buffer(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
ASSERT(layer_idx < DSTIM_MAX_LAYERS);
DvzBatch* batch = stim->batch;
ANN(batch);
dvz_bind_vertex(batch, stim->sphere_graphics_ids[layer_idx], 0, stim->sphere_vertex_id, 0);
}
static void create_sphere_index_buffer(DStim* stim, uint32_t sphere_index_count)
{
ANN(stim);
DvzBatch* batch = stim->batch;
ANN(batch);
// Create the vertex buffer dat for the sphere.
DvzRequest req =
dvz_create_dat(batch, DVZ_BUFFER_TYPE_INDEX, sphere_index_count * sizeof(DvzIndex), 0);
stim->sphere_index_id = req.id;
}
static void bind_sphere_index_buffer(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
ASSERT(layer_idx < DSTIM_MAX_LAYERS);
DvzBatch* batch = stim->batch;
ANN(batch);
dvz_bind_index(batch, stim->sphere_graphics_ids[layer_idx], stim->sphere_index_id, 0);
}
static void load_sphere_vertex_data(DStim* stim, uint32_t sphere_vertex_count)
{
// Load vertex data from disk.
DvzSize buffer_size = 0;
DStimVertex* vertices = read_file("data/vertex", &buffer_size);
ASSERT(buffer_size > 0);
ASSERT(buffer_size == sphere_vertex_count * sizeof(DStimVertex));
dstim_vertices(stim, sphere_vertex_count, vertices);
FREE(vertices);
}
static void load_sphere_index_data(DStim* stim, uint32_t sphere_index_count)
{
DvzSize buffer_size = 0;
DvzIndex* indices = read_file("data/index", &buffer_size);
ASSERT(buffer_size > 0);
ASSERT(buffer_size == sphere_index_count * sizeof(DvzIndex));
dstim_indices(stim, sphere_index_count, indices);
FREE(indices);
}
static void
create_texture(DStim* stim, uint32_t layer_idx, DvzFormat format, uint32_t width, uint32_t height)
{
ANN(stim);
GET_LAYER
ASSERT(width > 0);
ASSERT(height > 0);
DvzBatch* batch = stim->batch;
ANN(batch);
// Texture.
DvzRequest req = dvz_create_tex(batch, 2, format, (uvec3){width, height, 1}, 0);
stim->texture_ids[layer_idx] = req.id;
}
static void create_sampler(
DStim* stim, uint32_t layer_idx, DvzFilter filter, DvzSamplerAddressMode address_mode)
{
ANN(stim);
ASSERT(layer_idx < DSTIM_MAX_LAYERS);
DvzBatch* batch = stim->batch;
ANN(batch);
DvzRequest req = dvz_create_sampler(batch, filter, address_mode);
stim->sampler_ids[layer_idx] = req.id;
}
static void bind_texture(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
ASSERT(layer_idx < DSTIM_MAX_LAYERS);
DvzBatch* batch = stim->batch;
ANN(batch);
DvzId tex_id = stim->texture_ids[layer_idx];
ASSERT(tex_id != DVZ_ID_NONE);
dvz_bind_tex(
batch, stim->sphere_graphics_ids[layer_idx], 0, tex_id, stim->sampler_ids[layer_idx],
(uvec3){0, 0, 0});
}
static void upload_texture(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
DvzId tex_id = stim->texture_ids[layer_idx];
ASSERT(tex_id != DVZ_ID_NONE);
uint32_t width = layer->tex_width;
uint32_t height = layer->tex_height;
DvzSize tex_nbytes = layer->tex_nbytes;
uint8_t* rgba = layer->rgba;
ASSERT(width > 0);
ASSERT(height > 0);
ASSERT(tex_nbytes > 0);
ANN(rgba);
dvz_upload_tex(
stim->batch, tex_id, (uvec3){0, 0, 0}, (uvec3){width, height, 1}, tex_nbytes, rgba, 0);
}
static void set_blend(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
DvzBlendType blend = DVZ_BLEND_DISABLE;
if (layer->blend == DSTIM_BLEND_DST)
{
blend = DVZ_BLEND_DESTINATION;
}
dvz_set_blend(batch, stim->sphere_graphics_ids[layer_idx], blend);
}
static void set_mask(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
dvz_set_mask(batch, stim->sphere_graphics_ids[layer_idx], layer->mask);
}
static void prepare_sphere_pipeline(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
stim->sphere_graphics_ids[layer_idx] = create_sphere_pipeline(batch);
// Bind buffers to the new pipeline.
bind_sphere_vertex_buffer(stim, layer_idx);
bind_sphere_index_buffer(stim, layer_idx);
// Create texture.
DvzFormat format = layer->format;
uint32_t width = layer->tex_width;
uint32_t height = layer->tex_height;
// NOTE TODO: cannot change texture size after layer creation. Need to emit texture resize
// request.
create_texture(stim, layer_idx, format, width, height);
// Create sampler.
DvzFilter filter = layer->interpolation == DSTIM_INTERPOLATION_NEAREST ? DVZ_FILTER_NEAREST
: DVZ_FILTER_LINEAR;
DvzSamplerAddressMode address_mode = layer->is_periodic
? DVZ_SAMPLER_ADDRESS_MODE_REPEAT
: DVZ_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
create_sampler(stim, layer_idx, filter, address_mode);
// Bind the texture and sampler to the layer's pipeline.
bind_texture(stim, layer_idx);
// NOTE: address the case where these parameters change afterwards.
set_blend(stim, layer_idx);
set_mask(stim, layer_idx);
}
static void push_sphere_pipeline(DStim* stim, uint32_t layer_idx, DStimPush* push)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
// Send the push constant to the command buffer.
dvz_record_push(
batch, stim->canvas_id, stim->sphere_graphics_ids[layer_idx],
DVZ_SHADER_VERTEX | DVZ_SHADER_FRAGMENT, 0, sizeof(DStimPush), push);
}
static void draw_sphere_pipeline(DStim* stim, uint32_t layer_idx)
{
ANN(stim);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
// Sphere.
dvz_record_draw_indexed(
batch, stim->canvas_id, stim->sphere_graphics_ids[layer_idx], 0, 0,
stim->sphere_index_count, 0, 1);
}
static void fill_push(DStim* stim, uint32_t layer_idx, mat4 projection, DStimPush* push)
{
ANN(stim);
ANN(push);
GET_LAYER
DvzBatch* batch = stim->batch;
ANN(batch);
// Per-screen projection matrix.
glm_mat4_copy(projection, push->projection);
// Per-layer view matrix.
glm_mat4_copy(layer->view, push->view);
// Push constants parameters.
push->min_color[0] = layer->min_color[0] / 255.0;
push->min_color[1] = layer->min_color[1] / 255.0;
push->min_color[2] = layer->min_color[2] / 255.0;
push->min_color[3] = layer->min_color[3] / 255.0;
push->max_color[0] = layer->max_color[0] / 255.0;
push->max_color[1] = layer->max_color[1] / 255.0;
push->max_color[2] = layer->max_color[2] / 255.0;
push->max_color[3] = layer->max_color[3] / 255.0;
push->tex_offset[0] = layer->tex_offset[0];
push->tex_offset[1] = layer->tex_offset[1];
push->tex_size[0] = layer->tex_size[0];
push->tex_size[1] = layer->tex_size[1];
push->tex_angle = layer->tex_angle;
}
/*************************************************************************************************/
/* DStim functions */
/*************************************************************************************************/
DStim* dstim_init(uint32_t width, uint32_t height)
{
if (width == 0)
{
log_error("width cannot be zero");
return NULL;
}
if (height == 0)
{
log_error("height cannot be zero");
return NULL;
}
DStim* stim = (DStim*)calloc(1, sizeof(DStim));
stim->width = width;
stim->height = height;
for (uint32_t i = 0; i < DSTIM_MAX_LAYERS; i++)
{
stim->layers[i].is_blank = true;
}
// App.
// --------------------------------------------------------------------------------------------
DvzApp* app = dvz_app(0);
DvzBatch* batch = dvz_app_batch(app);
stim->app = app;
ANN(stim->app);
stim->batch = batch;
ANN(stim->batch);
DvzRequest req = {0};
// Background.
// --------------------------------------------------------------------------------------------
create_background(stim);
upload_rectangle(batch, stim->background_vertex_id, (vec2){-1, -1}, (vec2){+2, +2});
dstim_background(stim, DSTIM_DEFAULT_BACKGROUND);
// Square.
// --------------------------------------------------------------------------------------------
create_square(stim);
uint32_t sw = DSTIM_DEFAULT_SQUARE_WIDTH;
uint32_t sh = DSTIM_DEFAULT_SQUARE_HEIGHT;
dstim_square_pos(stim, width - sw, height - sh, sw, sh);
dstim_square_color(stim, DSTIM_DEFAULT_SQUARE_COLOR);
// Sphere.
// --------------------------------------------------------------------------------------------
uint32_t sphere_vertex_count = 20706;
// Create the vertex buffer dat for the sphere.
create_sphere_vertex_buffer(stim, sphere_vertex_count);
load_sphere_vertex_data(stim, sphere_vertex_count); // load from disk
// Create the index buffer dat for the sphere.// load from disk
stim->sphere_index_count = 124236;
create_sphere_index_buffer(stim, stim->sphere_index_count);
load_sphere_index_data(stim, stim->sphere_index_count);
// Canvas.
// --------------------------------------------------------------------------------------------
req = dvz_create_canvas(batch, width, height, DVZ_DEFAULT_CLEAR_COLOR, 0);
stim->canvas_id = req.id;
return stim;
}
void dstim_background(DStim* stim, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
ANN(stim);
rectangle_color(stim->batch, stim->background_params_id, red, green, blue, alpha);
}
void dstim_vertices(DStim* stim, uint32_t vertex_count, DStimVertex* vertices)
{
ANN(stim);
ASSERT(vertex_count > 0);
ANN(vertices);
DvzSize buffer_size = vertex_count * sizeof(DStimVertex);
ASSERT(buffer_size > 0);
ASSERT(stim->sphere_vertex_id != DVZ_ID_NONE);
DvzRequest req =
dvz_upload_dat(stim->batch, stim->sphere_vertex_id, 0, buffer_size, vertices, 0);
}
void dstim_indices(DStim* stim, uint32_t index_count, uint32_t* indices)
{
ANN(stim);
ASSERT(index_count > 0);
ANN(indices);
DvzSize buffer_size = index_count * sizeof(DvzIndex);
ASSERT(buffer_size > 0);
ASSERT(stim->sphere_index_id != DVZ_ID_NONE);
DvzRequest req =
dvz_upload_dat(stim->batch, stim->sphere_index_id, 0, buffer_size, indices, 0);
}
void dstim_square_pos(DStim* stim, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{
ANN(stim);
// from pixels to NDC
float xf = -1 + 2.0 * (float)x / (float)stim->width;
float yf = -1 + 2.0 * (float)y / (float)stim->height;
float wf = 2.0 * (float)w / (float)stim->width;
float hf = 2.0 * (float)h / (float)stim->height;
upload_rectangle(stim->batch, stim->square_vertex_id, (vec2){xf, yf}, (vec2){wf, hf});
}
void dstim_square_color(DStim* stim, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
ANN(stim);
rectangle_color(stim->batch, stim->square_params_id, red, green, blue, alpha);
}
void dstim_model(DStim* stim, mat4 model)
{
ANN(stim);
glm_mat4_copy(model, stim->model);
}
void dstim_cleanup(DStim* stim)
{