forked from cubzh/cubzh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertextbuffer.c
1530 lines (1292 loc) · 53.5 KB
/
vertextbuffer.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
// -------------------------------------------------------------
// Cubzh Core
// vertextbuffer.c
// Created by Adrien Duermael on July 7, 2017.
// -------------------------------------------------------------
#include "vertextbuffer.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "cclog.h"
#include "chunk.h"
#include "config.h"
#include "filo_list_uint32.h"
#ifdef DEBUG
#define VERTEX_BUFFER_DEBUG 1
#else
#define VERTEX_BUFFER_DEBUG 0
#endif
// takes the 4 low bits of a and casts into uint8_t
#define TO_UINT4(a) (uint8_t)((a) & 0x0F)
// Vertex buffers are used from outside Cubzh Core
// when implementing renderers (like Swift/Metal renderer)
// Giving each vertex buffer a proper ID is useful to know when
// they need to be referenced/unreferenced
static uint32_t vertex_buffer_next_id = 0;
static FiloListUInt32 *vertex_buffer_destroyed_ids = NULL;
static uint32_t vertex_buffer_get_new_id(void) {
uint32_t i = vertex_buffer_next_id;
vertex_buffer_next_id++;
return i;
}
static void vertex_buffer_add_destroyed_id(uint32_t id) {
if (vertex_buffer_destroyed_ids == NULL) {
vertex_buffer_destroyed_ids = filo_list_uint32_new();
}
filo_list_uint32_push(vertex_buffer_destroyed_ids, id);
}
bool vertex_buffer_pop_destroyed_id(uint32_t *id) {
return filo_list_uint32_pop(vertex_buffer_destroyed_ids, id);
}
struct _VertexBufferMemArea {
// where to start writing bytes
VertexAttributes *start; /* 8 bytes */
// vertex buffer that owns the mem area
VertexBuffer *vb; /* 8 bytes */
// allows to merge consecutive gaps
// chunk == NULL means the mem area is a gap
Chunk *chunk; /* 8 bytes */
VertexBufferMemArea *_globalListNext; /* 8 bytes */
VertexBufferMemArea *_globalListPrevious; /* 8 bytes */
// next area that belongs to the same group
// (same chunk or gaps)
VertexBufferMemArea *_groupListNext; /* 8 bytes */
// previous area that belongs to the same group
// (same chunk or gaps)
VertexBufferMemArea *_groupListPrevious; /* 8 bytes */
// indexing within owner buffer
uint32_t startIdx; /* 4 bytes */
uint32_t count; /* 4 bytes */
// Dirty vbma will be re-uploaded next render
bool dirty; /* 1 byte */
// padding
char pad[7];
};
VertexBufferMemArea *vertex_buffer_mem_area_new(VertexBuffer *vb,
VertexAttributes *start,
uint32_t startIdx,
uint32_t count);
void vertex_buffer_mem_area_free_all(VertexBufferMemArea *front);
bool vertex_buffer_mem_area_assign_to_chunk(VertexBufferMemArea *vbma,
Chunk *chunk,
bool transparent);
void vertex_buffer_new_empty_gap_at_end(VertexBuffer *vb);
VertexBufferMemArea *vertex_buffer_mem_area_split_and_make_gap(VertexBufferMemArea *vbma,
uint32_t vbma_size);
bool vertex_buffer_mem_area_is_gap(const VertexBufferMemArea *vbma);
bool vertex_buffer_mem_area_is_null_or_empty(const VertexBufferMemArea *vbma);
void vertex_buffer_mem_area_free(VertexBufferMemArea *vbma);
bool vertex_buffer_mem_area_insert_after(VertexBufferMemArea *vbma1,
VertexBufferMemArea *vbma2,
bool transparent);
void vertex_buffer_mem_area_leave_group_list(VertexBufferMemArea *vbma, bool transparent);
void vertex_buffer_mem_area_leave_global_list(VertexBufferMemArea *vbma);
void _vertex_buffer_memcpy(VertexAttributes *dst,
VertexAttributes *src,
size_t count,
size_t offset);
VertexAttributes *_vertex_buffer_data_add_ptr(VertexAttributes *ptr, size_t count);
// debug
#if VERTEX_BUFFER_DEBUG == 1
void vertex_buffer_check_mem_area_chain(VertexBuffer *vb);
#endif // VERTEX_BUFFER_DEBUG == 1
//---------------------
// MARK: VertexBuffer
//---------------------
// Only one DrawUnit will be allocated for a small shape, but bigger ones
// may need more, there will be one draw call per DrawUnit
struct _VertexBuffer {
VertexAttributes *data; /* 8 bytes */
// draw write slices define data index ranges that need re-upload after a structural change
// populated when updating chunks during shape_refresh_vertices()
// flushed by renderer calling vertex_buffer_flush_draw_slices() after re-upload
DoublyLinkedList *drawSlices; /* 8 bytes */
// vertex buffer's unique id
uint32_t id; /* 4 bytes */
// all vertices are computed by the shader using Shape's transformation,
// chunk's position and block coordinates within chunk
uint32_t nbVertices; /* 4 bytes */
// mem areas used by chunks to store vertices
// (references to memory areas within vertex buffer)
VertexBufferMemArea *firstMemArea; /* 8 bytes */
VertexBufferMemArea *lastMemArea; /* 8 bytes */
// how many faces can fit with the size allocated for mem areas
// when reaching this amount, a different vb must be used
size_t maxCount; /* 8 bytes */
// list of mem areas that are not used by any chunk
VertexBufferMemArea *firstMemAreaGap; /* 8 bytes */
VertexBufferMemArea *lastMemAreaGap; /* 8 bytes */
// vertex buffer can be enlisted
VertexBuffer *next; /* 8 bytes */
VertexBuffer *previous; /* 8 bytes */
// can be used by external object to remember whether vertex buffer
// is already part of a list or not
bool enlisted; /* 1 byte */
// draw write slices count
uint8_t nbDrawSlices; /* 1 byte */
bool isTransparent; /* 1 byte */
// padding
char pad[6];
};
// vb optionally writes lighting data
static bool vertex_buffer_lighting_enabled = true;
// MARK: DEBUG UTILS
#if VERTEX_BUFFER_DEBUG == 1
typedef struct {
uint32_t nbVertices;
bool isGap;
char chunkID[64];
char pad[3];
} VbmaRepresentation;
typedef struct {
VbmaRepresentation *vbmas;
uint32_t n;
char pad[4];
} VbSnapshot;
VbSnapshot *vertex_buffer_snapshot(const VertexBuffer *vb) {
VbSnapshot *vbs = (VbSnapshot *)malloc(sizeof(VbSnapshot));
if (vbs == NULL) {
return NULL;
}
vbs->n = 0;
// determine number of mem areas
VertexBufferMemArea *vbma = vb->firstMemArea;
while (vbma != NULL) {
vbs->n++;
vbma = vbma->_globalListNext;
}
vbs->vbmas = (VbmaRepresentation *)malloc(sizeof(VbmaRepresentation) * vbs->n);
if (vbs->vbmas == NULL) {
free(vbs);
return NULL;
}
//
vbma = vb->firstMemArea;
int i = 0;
while (vbma != NULL) {
vbs->vbmas[i].nbVertices = vbma->count;
vbs->vbmas[i].isGap = vbma->chunk == NULL;
if (vbma->chunk != NULL) {
sprintf(vbs->vbmas[i].chunkID, "%p", (void *)vbma->chunk);
} else {
snprintf(vbs->vbmas[i].chunkID, 4, "GAP");
}
i++;
vbma = vbma->_globalListNext;
}
return vbs;
}
void vertex_buffer_snapshot_free(VbSnapshot *vbs) {
free(vbs->vbmas);
free(vbs);
}
void vertex_buffer_log_diff(VbSnapshot *snap1, VbSnapshot *snap2) {
uint32_t nb1 = 0;
uint32_t nb2 = 0;
for (uint32_t i = 0; i < snap1->n || i < snap2->n; i++) {
if (i < snap1->n) {
if (snap1->vbmas[i].isGap == false) {
nb1 += snap1->vbmas[i].nbVertices;
}
cclog_info("[%04u] - %16s", snap1->vbmas[i].nbVertices, snap1->vbmas[i].chunkID);
} else {
cclog_info("[%04u] - %16s", 0, "EMPTY");
}
cclog_trace(" | ");
if (i < snap2->n) {
if (snap2->vbmas[i].isGap == false) {
nb2 += snap2->vbmas[i].nbVertices;
}
cclog_info("[%04u] - %16s", snap2->vbmas[i].nbVertices, snap2->vbmas[i].chunkID);
}
}
cclog_info("[%04u] - %16s", nb1, "TOTAL");
cclog_trace(" | ");
cclog_info("[%04u] - %16s", nb2, "TOTAL");
}
// END DEBUG UTILS
#endif
VertexBuffer *vertex_buffer_new(bool lighting, bool transparent) {
return vertex_buffer_new_with_max_count(SHAPE_BUFFER_MAX_COUNT, lighting, transparent);
}
VertexBuffer *vertex_buffer_new_with_max_count(size_t n, bool lighting, bool transparent) {
VertexBuffer *vb = (VertexBuffer *)malloc(sizeof(VertexBuffer));
if (vb == NULL) {
return NULL;
}
vb->id = vertex_buffer_get_new_id();
vb->enlisted = false;
// this represents the maximum number of faces from all chunks in that vertex buffer
vb->maxCount = n;
// pointers to first and last mem areas
vb->firstMemArea = NULL;
vb->lastMemArea = NULL;
vb->firstMemAreaGap = NULL;
vb->lastMemAreaGap = NULL;
vb->nbVertices = 0;
// nothing to initialize, the vertices won't be used if count == 0
vb->next = NULL;
vb->previous = NULL;
// container for draw buffers pointer
vb->data = (VertexAttributes *)malloc(n * DRAWBUFFER_VERTICES_PER_FACE_BYTES);
;
vb->drawSlices = doubly_linked_list_new();
vb->nbDrawSlices = 0;
vb->isTransparent = transparent;
return vb;
}
void vertex_buffer_nb_vertices_incr(VertexBuffer *vb, uint32_t v) {
vb->nbVertices += v;
#if VERTEX_BUFFER_DEBUG == 1
if (vb->nbVertices > vb->maxCount) {
cclog_debug("⚠️⚠️⚠️ vertex_buffer_nb_vertices_incr too many vertices!");
}
#endif
}
void vertex_buffer_nb_vertices_decr(VertexBuffer *vb, uint32_t v) {
vb->nbVertices -= v;
#if VERTEX_BUFFER_DEBUG == 1
if (vb->nbVertices > vb->maxCount) {
cclog_debug("⚠️⚠️⚠️ vertex_buffer_nb_vertices_decr too many vertices!");
}
#endif
}
bool vertex_buffer_is_fragmented(const VertexBuffer *vb) {
return (vb->firstMemAreaGap != NULL);
}
bool vertex_buffer_is_enlisted(const VertexBuffer *vb) {
return vb->enlisted;
}
void vertex_buffer_set_enlisted(VertexBuffer *vb, const bool b) {
vb->enlisted = b;
}
void vertex_buffer_free(VertexBuffer *vb) {
vertex_buffer_add_destroyed_id(vb->id);
free(vb->data);
vertex_buffer_mem_area_free_all(vb->firstMemArea);
doubly_linked_list_flush(vb->drawSlices, free);
doubly_linked_list_free(vb->drawSlices);
//!\\ vb->next has to be freed manually or using vertex_buffer_free_all
free(vb);
}
void vertex_buffer_free_all(VertexBuffer *front) {
VertexBuffer *vb = front;
VertexBuffer *tmp;
while (vb != NULL) {
tmp = vb;
vb = vb->next;
vertex_buffer_free(tmp);
}
}
bool vertex_buffer_is_not_full(const VertexBuffer *vb) {
return vb != NULL && vb->nbVertices < vb->maxCount;
}
void vertex_buffer_insert_after(VertexBuffer *vb1, VertexBuffer *vb2) {
if (vb2 == NULL)
return;
vb1->previous = vb2;
vb1->next = vb2->next;
vb2->next = vb1;
}
VertexBuffer *vertex_buffer_get_next(const VertexBuffer *vb) {
return vb->next;
}
VertexBufferMemArea *vertex_buffer_get_first_mem_area(const VertexBuffer *vb) {
return vb->firstMemArea;
}
uint32_t vertex_buffer_get_id(const VertexBuffer *vb) {
return vb->id;
}
VertexAttributes *vertex_buffer_get_draw_buffer(const VertexBuffer *vb) {
return vb->data;
}
DoublyLinkedList *vertex_buffer_get_draw_slices(const VertexBuffer *vb) {
return vb->drawSlices;
}
void vertex_buffer_log_draw_slices(const VertexBuffer *vb) {
if (vb->nbDrawSlices > 0) {
cclog_trace("-- DRAW SLICES --");
DoublyLinkedListNode *itr = doubly_linked_list_first(vb->drawSlices);
DrawBufferWriteSlice *ws;
while (itr != NULL) {
ws = (DrawBufferWriteSlice *)doubly_linked_list_node_pointer(itr);
cclog_info("[%d,%d]", ws->from, ws->to);
itr = doubly_linked_list_node_next(itr);
}
cclog_trace("-----------------");
}
}
void vertex_buffer_add_draw_slice(VertexBuffer *vb, uint32_t start, uint32_t count) {
if (count == 0) {
cclog_error("⚠️ vertex_buffer_add_draw_slice: empty, slice skipped");
return;
}
DrawBufferWriteSlice value = {start, start + count - 1};
DrawBufferWriteSlice *leftMerged = NULL, *rightMerged = NULL;
DoublyLinkedListNode *rightMergedNode = NULL;
DoublyLinkedListNode *itr = doubly_linked_list_first(vb->drawSlices);
DrawBufferWriteSlice *ws;
while (itr != NULL) {
ws = (DrawBufferWriteSlice *)doubly_linked_list_node_pointer(itr);
if (value.from == ws->to + 1) {
ws->to = value.to;
leftMerged = ws;
} else if (value.to == ws->from - 1) {
ws->from = value.from;
rightMerged = ws;
rightMergedNode = itr;
}
itr = doubly_linked_list_node_next(itr);
}
// reduce if merged right & left, or insert if not merged at all
if (leftMerged != NULL && rightMerged != NULL) {
leftMerged->to = rightMerged->to;
doubly_linked_list_delete_node(vb->drawSlices, rightMergedNode);
free(rightMerged);
vb->nbDrawSlices--;
} else if (leftMerged == NULL && rightMerged == NULL) {
DrawBufferWriteSlice *node = (DrawBufferWriteSlice *)malloc(sizeof(DrawBufferWriteSlice));
if (node != NULL) {
node->from = value.from;
node->to = value.to;
doubly_linked_list_push_last(vb->drawSlices, node);
vb->nbDrawSlices++;
}
}
}
void vertex_buffer_fill_draw_slices(VertexBuffer *vb) {
VertexBufferMemArea *vbma = vb->firstMemArea;
uint32_t idx = 0;
while (vbma != NULL) {
if (vbma->dirty) {
if (vertex_buffer_mem_area_is_gap(vbma) == false) {
vertex_buffer_add_draw_slice(vb, idx, vbma->count);
}
vbma->dirty = false;
}
idx += vbma->count;
vbma = vbma->_globalListNext;
}
}
void vertex_buffer_flush_draw_slices(VertexBuffer *vb) {
// just for safety, but normally draw slices were consumed before calling this
doubly_linked_list_flush(vb->drawSlices, free);
vb->nbDrawSlices = 0;
}
size_t vertex_buffer_get_nb_draw_slices(const VertexBuffer *vb) {
return vb->nbDrawSlices;
}
size_t vertex_buffer_get_nb_faces(const VertexBuffer *vb) {
return vb->nbVertices;
}
size_t vertex_buffer_get_max_length(const VertexBuffer *vb) {
return vb->maxCount;
}
void vertex_buffer_mem_area_remove(VertexBufferMemArea *vbma, bool transparent) {
// leave group list
vertex_buffer_mem_area_leave_group_list(vbma, transparent);
#if VERTEX_BUFFER_DEBUG == 1
if (vbma->_globalListNext == NULL && vbma->vb->lastMemArea != vbma) {
cclog_debug("⚠️⚠️⚠️ vbma->_globalListNext == NULL though not lastMemArea");
}
#endif
// remove from main list
vertex_buffer_mem_area_leave_global_list(vbma);
vertex_buffer_mem_area_free(vbma);
}
void vertex_buffer_remove_last_mem_area(VertexBuffer *vb) {
vertex_buffer_nb_vertices_decr(vb, vb->lastMemArea->count);
vertex_buffer_mem_area_remove(vb->lastMemArea, vb->isTransparent);
}
// reorganizes data to fill the gaps
void vertex_buffer_fill_gaps(VertexBuffer *vb) {
#if VERTEX_BUFFER_DEBUG == 1
vertex_buffer_check_mem_area_chain(vb);
#endif
// no gap remaining at the end of this function
vb->firstMemAreaGap = NULL;
// here we know there are gaps, and none of them is at the end
// of global mem area list
// vb->firstMemAreaGap may not be the first gap in the
// global mem area list... So need to start filling up the
// first gap in the global list.
// start at first mem area
VertexBufferMemArea *cursor = vb->firstMemArea;
VertexBufferMemArea *vbma;
while (cursor != NULL) {
// loop until finding a gap to fill
while (cursor != NULL && vertex_buffer_mem_area_is_gap(cursor) == false) {
// merge with following mem areas referencing same chunk
// also destroy mem areas with 0 vertices
while (cursor->_globalListNext != NULL &&
(cursor->_globalListNext->chunk == cursor->chunk ||
cursor->_globalListNext->count == 0)) {
// vbma that will be destroyed
vbma = cursor->_globalListNext;
cursor->count += vbma->count; // can be 0
// maintain dirty flag
if (vbma->count > 0 && vbma->dirty) {
cursor->dirty = true;
}
vertex_buffer_mem_area_remove(vbma, vb->isTransparent);
}
cursor = cursor->_globalListNext;
}
// already reached the end of the list
if (cursor == NULL) {
break;
}
// HERE: cursor is a gap & not the end of the list
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->chunk != NULL) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be a gap");
}
#endif
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->_globalListNext == NULL) {
if (cursor != vb->lastMemArea) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be vb->lastMemArea (1)");
}
}
#endif
// merge gap pointed by cursor with following gaps
// also destroy mem areas with 0 vertices
while (cursor->_globalListNext != NULL &&
(vertex_buffer_mem_area_is_gap(cursor->_globalListNext) == true ||
cursor->_globalListNext->count == 0)) {
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->chunk != NULL) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be a gap");
}
#endif
// vbma that will be destroyed
vbma = cursor->_globalListNext;
cursor->count += vbma->count; // can be 0
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->_globalListNext->_globalListPrevious != cursor) {
cclog_debug("⚠️⚠️⚠️ global list chain error (1)");
}
#endif
#if VERTEX_BUFFER_DEBUG == 1
if (vbma->_globalListPrevious != cursor) {
cclog_debug("⚠️⚠️⚠️ global list chain error (2)");
}
#endif
vertex_buffer_mem_area_remove(vbma, vb->isTransparent);
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->_globalListNext == NULL) {
if (cursor->_globalListPrevious == NULL) {
cclog_debug("⚠️⚠️⚠️ cursor not enlisted!");
}
if (cursor != vb->lastMemArea) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be vb->lastMemArea (3)");
}
}
#endif
}
// at this point: no gap after gap pointed by cursor
// and no vbma with size of 0
#if VERTEX_BUFFER_DEBUG == 1
if (cursor->_globalListNext != NULL) {
if (vertex_buffer_mem_area_is_gap(cursor->_globalListNext) == true ||
cursor->_globalListNext->count == 0) {
cclog_debug("⚠️⚠️⚠️ error when merging gaps");
}
}
#endif
#if VERTEX_BUFFER_DEBUG == 1
if (vertex_buffer_mem_area_is_gap(cursor) == false) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be a gap");
}
#endif
// already reached the end of the list
if (cursor->_globalListNext == NULL) {
#if VERTEX_BUFFER_DEBUG == 1
if (cursor != vb->lastMemArea) {
cclog_debug("⚠️⚠️⚠️ cursor is supposed to be vb->lastMemArea (2)");
}
#endif
vertex_buffer_remove_last_mem_area(vb);
break; // breaks main loop
}
uint32_t written = 0;
// loop until gap is filled with vertices
// taking them from non gap mem areas at the end of the list
while (written < cursor->count) {
// options
// 1) cursor == vb->lastMemArea
// -> split and remove last part, or remove whole gap if written==0
// -> BREAK
if (cursor == vb->lastMemArea) {
if (written > 0) {
vertex_buffer_mem_area_split_and_make_gap(cursor, written);
}
// remove created gap or mem area if written == 0
vertex_buffer_remove_last_mem_area(vb);
// maintain cursor == vb->lastMemArea to exit main loop
cursor = vb->lastMemArea;
break;
}
// 2) vb->lastMemArea is an other gap
// -> remove it and continue
else if (vertex_buffer_mem_area_is_gap(vb->lastMemArea)) {
vertex_buffer_remove_last_mem_area(vb);
continue;
}
// 3) vb->lastMemArea is not a gap but has no vertices
// -> remove it and continue
else if (vb->lastMemArea->count == 0) {
vertex_buffer_remove_last_mem_area(vb);
continue;
}
// HERE: lastMemArea is not a gap and has vertices
if (vertex_buffer_mem_area_is_gap(cursor)) {
// assign to same chunk as last mem area
vertex_buffer_mem_area_insert_after(cursor, vb->lastMemArea, vb->isTransparent);
}
// 1) chunk is different than the one being written
// -> if written == 0, make it gap again
// -> otherwise split
// -> BREAK
if (cursor->chunk != vb->lastMemArea->chunk) {
if (written == 0) {
// make it a gap again
cclog_warning("⚠️ vertex_buffer_fill_gaps: gap is skipped");
vertex_buffer_mem_area_make_gap(cursor, vb->isTransparent);
} else {
vertex_buffer_mem_area_split_and_make_gap(cursor, written);
// gap can't be removed because not at last position
}
break;
}
// 2) rightVbma has exact amount of vertices:
// -> memcpy all, remove last vbma
// -> LOOP WILL EXIT
else if (cursor->count == vb->lastMemArea->count) {
_vertex_buffer_memcpy(cursor->start,
vb->lastMemArea->start,
vb->lastMemArea->count,
0);
cursor->dirty = true;
written += vb->lastMemArea->count;
vertex_buffer_remove_last_mem_area(vb);
continue;
}
// 3) last vbma has enough vertices:
// -> memcpy end of rightVbma, split, remove last part
// -> LOOP WILL EXIT
else if (cursor->count < vb->lastMemArea->count) {
uint32_t diff = vb->lastMemArea->count - cursor->count;
_vertex_buffer_memcpy(cursor->start, vb->lastMemArea->start, cursor->count, diff);
cursor->dirty = true;
written += cursor->count;
vertex_buffer_mem_area_split_and_make_gap(vb->lastMemArea, diff);
vertex_buffer_remove_last_mem_area(vb);
}
// 4) last vbma has not enough vertices:
// -> memcpy all, split gap, remove last vbma
else {
_vertex_buffer_memcpy(cursor->start,
vb->lastMemArea->start,
vb->lastMemArea->count,
0);
cursor->dirty = true;
written += vb->lastMemArea->count;
vertex_buffer_mem_area_split_and_make_gap(cursor, written);
vertex_buffer_remove_last_mem_area(vb);
}
} // end while (loop until gap is filled with vertices)
#if VERTEX_BUFFER_DEBUG == 1
if (cursor == NULL) {
cclog_debug("⚠️⚠️⚠️ cursor shouldn't be NULL");
}
#endif
cursor = cursor->_globalListNext;
} // end of main loop: while (cursor != NULL)
#if VERTEX_BUFFER_DEBUG == 1
vertex_buffer_check_mem_area_chain(vb);
#endif
}
//---------------------
// MARK: Draw buffers
//---------------------
void _vertex_buffer_memcpy(VertexAttributes *dst,
VertexAttributes *src,
size_t count,
size_t offset) {
memcpy(dst,
src + offset * DRAWBUFFER_VERTICES_PER_FACE,
count * DRAWBUFFER_VERTICES_PER_FACE_BYTES);
}
VertexAttributes *_vertex_buffer_data_add_ptr(VertexAttributes *ptr, size_t count) {
ptr += count * DRAWBUFFER_VERTICES_PER_FACE;
return ptr;
}
//---------------------
// MARK: VertexBufferMemArea
//---------------------
bool vertex_buffer_mem_area_is_gap(const VertexBufferMemArea *vbma) {
return vbma->chunk == NULL;
}
bool vertex_buffer_mem_area_is_null_or_empty(const VertexBufferMemArea *vbma) {
return vbma == NULL || vbma->count == 0;
}
// creates new VertexBufferMemArea
VertexBufferMemArea *vertex_buffer_mem_area_new(VertexBuffer *vb,
VertexAttributes *start,
uint32_t startIdx,
uint32_t count) {
VertexBufferMemArea *vbma = (VertexBufferMemArea *)malloc(sizeof(VertexBufferMemArea));
if (vbma == NULL) {
return NULL;
}
vbma->vb = vb;
vbma->_globalListNext = NULL;
vbma->_globalListPrevious = NULL;
vbma->_groupListNext = NULL;
vbma->_groupListPrevious = NULL;
vbma->chunk = NULL;
vbma->startIdx = startIdx;
vbma->count = count;
vbma->start = start;
vbma->dirty = false;
return vbma;
}
void vertex_buffer_mem_area_free(VertexBufferMemArea *vbma) {
free(vbma);
}
void vertex_buffer_mem_area_leave_group_list(VertexBufferMemArea *vbma, bool transparent) {
// maybe it is the front mem area of chunk (if not a gap)
if (vertex_buffer_mem_area_is_gap(vbma) == false) {
if (chunk_get_vbma(vbma->chunk, transparent) == vbma) {
chunk_set_vbma(vbma->chunk, vbma->_groupListNext, transparent);
}
} else { // not the front mem area of a chunk
if (vbma == vbma->vb->firstMemAreaGap) {
vbma->vb->firstMemAreaGap = vbma->_groupListNext;
}
if (vbma == vbma->vb->lastMemAreaGap) {
vbma->vb->lastMemAreaGap = vbma->_groupListPrevious;
}
}
if (vbma->_groupListNext != NULL) {
vbma->_groupListNext->_groupListPrevious = vbma->_groupListPrevious;
}
if (vbma->_groupListPrevious != NULL) {
vbma->_groupListPrevious->_groupListNext = vbma->_groupListNext;
}
vbma->_groupListNext = NULL;
vbma->_groupListPrevious = NULL;
}
void vertex_buffer_mem_area_leave_global_list(VertexBufferMemArea *vbma) {
#if VERTEX_BUFFER_DEBUG == 1
if (vbma->_globalListNext == NULL && vbma != vbma->vb->lastMemArea) {
cclog_debug("⚠️⚠️⚠️ vbma should be last mem area or have non NULL _globalListNext");
}
if (vbma->_globalListNext != NULL && vbma == vbma->vb->lastMemArea) {
cclog_debug("⚠️⚠️⚠️ vbma shouldn't be last mem area (non NULL _globalListNext)");
}
#endif
if (vbma == vbma->vb->lastMemArea) {
// printf("new last mem area: %p\n", vbma->_globalListPrevious);
vbma->vb->lastMemArea = vbma->_globalListPrevious;
}
if (vbma == vbma->vb->firstMemArea) {
vbma->vb->firstMemArea = vbma->_globalListNext;
}
if (vbma->_globalListNext != NULL) {
vbma->_globalListNext->_globalListPrevious = vbma->_globalListPrevious;
}
if (vbma->_globalListPrevious != NULL) {
vbma->_globalListPrevious->_globalListNext = vbma->_globalListNext;
}
vbma->_globalListNext = NULL;
vbma->_globalListPrevious = NULL;
}
// this is used to create a new mem area at the end, as only gaps
// can be used by chunks. The mem area will have a size of zero, but
// the last mem area is allowed to increase in size as vertices are added
//!\\ if this gap gets assigned to a chunk without being written, another
// empty gap can be added pointing to the same location in vertex buffer's
// vertices. This one would then become useless, empty forever until it
// finally/eventually gets merged with another gap.
void vertex_buffer_new_empty_gap_at_end(VertexBuffer *vb) {
VertexAttributes *start;
uint32_t startdIdx;
if (vb->lastMemArea != NULL) {
start = _vertex_buffer_data_add_ptr(vb->lastMemArea->start, vb->lastMemArea->count);
startdIdx = vb->lastMemArea->startIdx + vb->lastMemArea->count;
} else {
// no lastMemArea means no mem area at all
// so we should start at beginning of the buffers in vb->data
start = vb->data;
startdIdx = 0;
}
VertexBufferMemArea *memArea = vertex_buffer_mem_area_new(vb, start, startdIdx, 0);
// if firstMemArea == NULL, lastMemArea has to be NULL
// it simply means the vertex buffer has no mem area yet
if (vb->firstMemArea == NULL) {
vb->firstMemArea = memArea;
vb->lastMemArea = memArea;
} else if (vb->lastMemArea != NULL) { // add as last mem area
vb->lastMemArea->_globalListNext = memArea;
memArea->_globalListPrevious = vb->lastMemArea;
vb->lastMemArea = memArea;
}
// enlist with other gaps if some exist already
if (vb->firstMemAreaGap == NULL) {
vb->firstMemAreaGap = memArea;
vb->lastMemAreaGap = memArea;
} else {
vb->lastMemAreaGap->_groupListNext = memArea;
memArea->_groupListPrevious = vb->lastMemAreaGap;
vb->lastMemAreaGap = memArea;
}
#if VERTEX_BUFFER_DEBUG == 1
vertex_buffer_check_mem_area_chain(vb);
#endif
}
// assigns mem area to chunk, only works if mem area is a gap
// it's not possible to move a mem area from one chunk to another without
// going through the gap state
// returns true on success, false otherwise
bool vertex_buffer_mem_area_assign_to_chunk(VertexBufferMemArea *vbma,
Chunk *chunk,
bool transparent) {
if (vertex_buffer_mem_area_is_gap(vbma) == false) {
return false;
}
vertex_buffer_mem_area_leave_group_list(vbma, transparent);
vbma->chunk = chunk;
VertexBufferMemArea *memArea = (VertexBufferMemArea *)chunk_get_vbma(chunk, transparent);
// if chunk has no mem area, vbma simply becomes the first one
if (memArea == NULL) {
chunk_set_vbma(chunk, vbma, transparent);
}
// otherwise go to last mem area of this chunk
else {
while (memArea->_groupListNext != NULL) {
memArea = memArea->_groupListNext;
}
// add vbma at the end of the list
vbma->_groupListPrevious = memArea;
memArea->_groupListNext = vbma;
}
return true;
}
// inserts vbma1 after vbma2
// if vbma2 is a gap, vbma1 will become a gap
// otherwise, it will be attributed to the same chunk as vbma2
bool vertex_buffer_mem_area_insert_after(VertexBufferMemArea *vbma1,
VertexBufferMemArea *vbma2,
bool transparent) {
if (vbma2 == NULL)
return false;
vertex_buffer_mem_area_leave_group_list(vbma1, transparent);
vbma1->chunk = vbma2->chunk;
if (vbma2->_groupListNext != NULL) {
vbma2->_groupListNext->_groupListPrevious = vbma1;
vbma1->_groupListNext = vbma2->_groupListNext;
}
vbma2->_groupListNext = vbma1;
vbma1->_groupListPrevious = vbma2;
return true;
}
// a vbmaw is permanently bound to a shape and chunk, while the vbma to write onto can change
// - vbma can be wherever there is room within allocated vb memory
// - occasionally, a new vb can be created for the shape if it is at full capacity,
// this is because vb capacity vs. chunk size can be set independently
struct _VertexBufferMemAreaWriter {
VertexAttributes *cursor; /* 8 bytes */
Shape *s; /* 8 bytes */
Chunk *c; /* 8 bytes */
VertexBufferMemArea *vbma; /* 8 bytes */
// amount of vertices written in current mem area
// this is being reset when jumping to a different mem area
uint32_t writtenFaces; /* 4 bytes */
bool isTransparent; /* 1 byte */
char pad[3]; /* 3 bytes */
};
void vertex_buffer_mem_area_writer_reset(VertexBufferMemAreaWriter *vbmaw,
VertexBufferMemArea *vbma) {
vbmaw->vbma = vbma;
if (vbmaw->vbma != NULL) {
vbmaw->cursor = vbmaw->vbma->start;
} else {
vbmaw->cursor = NULL;
}
vbmaw->writtenFaces = 0;
}
void vertex_buffer_mem_area_writer_write(VertexBufferMemAreaWriter *vbmaw,
float x,
float y,
float z,
ATLAS_COLOR_INDEX_INT_T color,
FACE_INDEX_INT_T faceIndex,
FACE_AMBIENT_OCCLUSION_STRUCT_T ao,
bool vLighting,
VERTEX_LIGHT_STRUCT_T vlight1,
VERTEX_LIGHT_STRUCT_T vlight2,
VERTEX_LIGHT_STRUCT_T vlight3,
VERTEX_LIGHT_STRUCT_T vlight4) {