forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyspritebatch.h
1671 lines (1408 loc) · 56.7 KB
/
tinyspritebatch.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
tinyspritebatch.h - v1.0
To create implementation (the function definitions)
#define SPRITEBATCH_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
SUMMARY:
This header implements a 2D sprite batcher by tracking different textures within
a rolling atlas cache. Over time atlases are decayed and recreated when textures
stop being used. This header is useful for batching sprites at run-time. This avoids
the need to compile texture atlases as a pre-process step, letting the game load
images up individually, dramatically simplifying art pipelines.
MORE DETAILS:
`spritebatch_push` is used to push sprite instances into a buffer. Rendering sprites
works by calling `spritebatch_flush`. `spritebatch_flush` will use a user-supplied
callback to report sprite batches. This callback is of type `submit_batch_t`. The
batches are reported as an array of `spritebatch_sprite_t` sprites, and can be
further sorted by the user (for example to sort by depth). Sprites in a batch share
the same texture handle (either from the same base image, or from the same internal
atlas).
tinypsritebatch does not know anything about how to generate texture handles, or
destroy them. As such, the user must supply two callbacks for creating handles and
destroying them. These can be simple wrappers around, for example, `glGenTextures`
and `glDeleteTextures`.
Finally, tinyspritebatch will periodically need access to pixels from images. These
pixels are used to generate textures, or to build atlases (which in turn generate a
texture). tinyspritebatch does not need to know much about your images, other than
the pixel stride. The user supplies callback of type `get_pixels_t`, which lets
tinypsritebatch retreive the pixels associated with a particular image. The pixels
can be stored in RAM and handed to tinyspritebatch whenever requested, or the pixels
can be fetched directly from disk and handed to tinyspritebatch. It doesn't matter
to tinyspritebatch, and the pointer to the pixels are *not* stored anywhere after
the callback returns. Since `get_pixels_t` can be called from `spritebatch_flush` it
is recommended to avoid file i/o within the `get_pixels_t` callback, and instead try
to already have pixels ready in RAM.
The `spritebatch_defrag` function performs atlas creation and texture management. It
should be called periodically. It can be called once per game tick (once per render),
or optionally called at a different frequency (once ever N game ticks).
PROS AND CONS:
PROS
- Texture atlases are completely hidden behind an api. The api in this header can
easily be implemented with different backend sprite batchers. For example on
some platforms bindless textures can be utilized in order to avoid texture
atlases entirely! Code using this API can have the backend implementation swapped
without requiring any user code to change.
- Sprites are batched in an effective manner to dramatically reduce draw call counts.
- Supporting hotswapping or live-reloading of images can be trivialized due to
moving atlas creation out of the art-pipeline and into the run-time.
- Since atlases are built at run-time and continually maintained, images are
guaranteed to be drawn at the same time on-screen as their atlas neighbors. This is
typically not the case for atlas preprocessors, as a *guess* must be made to try
and organize images together in atlases that need to be drawn at roughly the same
time.
CONS
- Performance hits in the `spritebatch_defrag` function, and a little as well in
the `spritebatch_flush` function. Extra run-time memory usage for bookkeeping,
which implies a RAM hit as well as more things to clog the CPU cache.
- If each texture comes from a separate image on-disk, opening individual files on
disk can be very slow. For example on Windows just performing permissions and
related work to open a file is time-consuming.
- For large numbers of separate images, some file abstraction is necessary to avoid
a large performance hit on opening/closing many individual files. This problem is
*not* solved by tinyspritebatch.h, and instead should be solved by some separate
file abstraction system.
EXAMPLE USAGE:
spritebatch_config_t config;
spritebatch_set_default_config(&config);
config.batch_callback = my_report_batches_function;
config.get_pixels_callback = my_get_pixels_function;
config.generate_texture_callback = my_make_texture_handle_function;
config.delete_texture_callback = my_destroy_texture_handle_function;
spritebatch_t batcher;
spritebatch_init(&batcher, &config);
while (game_is_running)
{
for (int i = 0; i < sprite_count; ++i)
spritebatch_push(
&batcher,
sprites[i].image_id,
sprites[i].image_width_in_pixels,
sprites[i].image_height_in_pixels,
sprites[i].position_x,
sprites[i].poxition_y,
sprites[i].scale_x,
sprites[i].scale_y,
sprites[i].cos_rotation_angle,
sprites[i].sin_rotation_angle
);
spritebatch_tick(&batcher);
spritebatch_defrag(&batcher);
spritebatch_flush(&batcher);
}
CUSTOMIZATION:
The following macros can be defined before including this header with the
SPRITEBATCH_IMPLEMENTATION symbol defined, in order to customize the internal
behavior of tinyspritebatch.h. Search this header to find how each macro is
defined and used. Note that MALLOC/FREE functions can optionally take a context
parameter for custom allocation.
SPRITEBATCH_MALLOC
SPRITEBATCH_MEMCPY
SPRITEBATCH_MEMSET
SPRITEBATCH_ASSERT
SPRITEBATCH_ATLAS_FLIP_Y_AXIS_FOR_UV
SPRITEBATCH_ATLAS_EMPTY_COLOR
SPRITEBATCH_ALLOCA
SPRITEBATCH_LOG
Revision history:
0.01 (11/20/2017) experimental release
1.00 (04/14/2018) initial release
*/
#ifndef SPRITEBATCH_H
#ifndef SPRITEBATCH_U64
#define SPRITEBATCH_U64 unsigned long long
#endif SPRITEBATCH_U64
typedef struct spritebatch_t spritebatch_t;
typedef struct spritebatch_config_t spritebatch_config_t;
typedef struct spritebatch_sprite_t spritebatch_sprite_t;
// Pushes a sprite onto an internal buffer. Does no other logic. `image_id` must be a unique
// identifier for the image a sprite references. `image_w` and image_h` are the width and height
// of the image referenced by `image_id`. `x` and `y` are the position of the sprite. `sx` and
// `sy` are the scale factors on the x and y axis for the sprite. `c` and `s` are the cosine and
// sine of the angle of the sprite, and represent a 2D rotation matrix.
int spritebatch_push(spritebatch_t* sb, SPRITEBATCH_U64 image_id, int image_w, int image_h, float x, float y, float sx, float sy, float c, float s, int sort_bits);
// Increments internal timestamps on all textures, for use in `spritebatch_defrag`.
void spritebatch_tick(spritebatch_t* sb);
// Sorts the internal sprites and flushes the buffer built by `spritebatch_push`. Will call
// the `submit_batch_t` function for each batch of sprites and return them as an array. Any `image_id`
// within the `spritebatch_push` buffer that do not yet have a texture handle will request pixels
// from the image via `get_pixels_t` and request a texture handle via `generate_texture_handle_t`.
int spritebatch_flush(spritebatch_t* sb);
// All textures created so far by `spritebatch_flush` will be considered as candidates for creating
// new internal texture atlases. Internal texture atlases compress images together inside of one
// texture to dramatically reduce draw calls. When an atlas is created, the most recently used `image_id`
// instances are prioritized, to ensure atlases are filled with images all drawn at the same time.
// As some textures cease to draw on screen, they "decay" over time. Once enough images in an atlas
// decay, the atlas is removed, and any "live" images in the atlas are used to create new atlases.
// Can be called every 1/N times `spritebatch_flush` is called.
int spritebatch_defrag(spritebatch_t* sb);
int spritebatch_init(spritebatch_t* sb, spritebatch_config_t* config);
void spritebatch_term(spritebatch_t* sb);
// Sprite batches are submit via synchronous callback back to the user. This function is called
// from inside `spritebatch_flush`. Each time `submit_batch_t` is called an array of sprites
// is handed to the user. The sprites are intended to be further sorted by the user as desired
// (for example, additional sorting based on depth).
typedef void (*submit_batch_t)(spritebatch_sprite_t* sprites, int count);
// tinyspritebatch.h needs to know how to get the pixels of an image, generate textures handles (for
// example glGenTextures for OpenGL), and destroy texture handles. These functions are all called
// from within the `spritebatch_defrag` function, and sometimes from `spritebatch_flush`.
typedef void* (*get_pixels_t)(SPRITEBATCH_U64 image_id);
typedef SPRITEBATCH_U64 (*generate_texture_handle_t)(void* pixels, int w, int h);
typedef void (*destroy_texture_handle_t)(SPRITEBATCH_U64 texture_id);
// Initializes a set of good default paramaters. The users must still set
// the four callbacks inside of `config`.
void spritebatch_set_default_config(spritebatch_config_t* config);
struct spritebatch_config_t
{
int pixel_stride;
int atlas_width_in_pixels;
int atlas_height_in_pixels;
int ticks_to_decay_texture; // number of ticks it takes for a texture handle to be destroyed via `destroy_texture_handle_t`
int lonely_buffer_count_till_flush; // number of unique textures until an atlas is constructed
float ratio_to_decay_atlas; // from 0 to 1, once ratio is less than `ratio_to_decay_atlas`, flush active textures in atlas to lonely buffer
float ratio_to_merge_atlases; // from 0 to 0.5, attempts to merge atlases with some ratio of empty space
submit_batch_t batch_callback;
get_pixels_t get_pixels_callback;
generate_texture_handle_t generate_texture_callback;
destroy_texture_handle_t delete_texture_callback;
void* allocator_context;
};
struct spritebatch_sprite_t
{
SPRITEBATCH_U64 texture_id;
// User-defined sorting key, see: http://realtimecollisiondetection.net/blog/?p=86
// The first 32-bits store the user's sort bits. The bottom 32-bits are for internal
// usage, and are not ever set by the user. Internally sprites are sorted first
// based on `sort_bits`, and to break ties they are sorted on `texture_id`. Feel free
// to change the sort predicate `spritebatch_internal_instance_pred` in the
// implementation section.
SPRITEBATCH_U64 sort_bits;
float x, y; // x and y position
float sx, sy; // scale on x and y axis
float c, s; // cosine and sine (represents cos(angle) and sin(angle))
float minx, miny; // u coordinate
float maxx, maxy; // v coordinate
};
#define SPRITEBATCH_H
#endif
#ifdef SPRITEBATCH_IMPLEMENTATION
#ifndef SPRITEBATCH_IMPLEMENTATION_ONCE
#define SPRITEBATCH_IMPLEMENTATION_ONCE
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#ifndef SPRITEBATCH_MALLOC
#include <stdlib.h>
#define SPRITEBATCH_MALLOC(size, ctx) malloc(size)
#define SPRITEBATCH_FREE(ptr, ctx) free(ptr)
#endif
#ifndef SPRITEBATCH_MEMCPY
#include <string.h>
#define SPRITEBATCH_MEMCPY(dst, src, n) memcpy(dst, src, n)
#endif
#ifndef SPRITEBATCH_MEMSET
#include <string.h>
#define SPRITEBATCH_MEMSET(ptr, val, n) memset(ptr, val, n)
#endif
#ifndef SPRITEBATCH_ASSERT
#include <assert.h>
#define SPRITEBATCH_ASSERT(condition) assert(condition)
#endif
// flips output uv coordinate's y. Can be useful to "flip image on load"
#ifndef SPRITEBATCH_ATLAS_FLIP_Y_AXIS_FOR_UV
#define SPRITEBATCH_ATLAS_FLIP_Y_AXIS_FOR_UV 1
#endif
// flips output uv coordinate's y. Can be useful to "flip image on load"
#ifndef SPRITEBATCH_LONELY_FLIP_Y_AXIS_FOR_UV
#define SPRITEBATCH_LONELY_FLIP_Y_AXIS_FOR_UV 1
#endif
#ifndef SPRITEBATCH_ATLAS_EMPTY_COLOR
#define SPRITEBATCH_ATLAS_EMPTY_COLOR 0x000000FF
#endif
#ifndef SPRITEBATCH_ALLOCA
#ifdef _WIN32
#include <malloc.h>
#else
#include <alloca.h>
#endif
#define SPRITEBATCH_ALLOCA(ctx, size) alloca(size)
#endif
#ifndef SPRITEBATCH_LOG
#if 0
#define SPRITEBATCH_LOG printf
#else
#define SPRITEBATCH_LOG(...)
#endif
#endif
#define HASHTABLE_MEMSET(ptr, val, n) SPRITEBATCH_MEMSET(ptr, val, n)
#define HASHTABLE_MEMCPY(dst, src, n) SPRITEBATCH_MEMCPY(dst, src, n)
#define HASHTABLE_MALLOC(ctx, size) SPRITEBATCH_MALLOC(size, ctx)
#define HASHTABLE_FREE(ctx, ptr) SPRITEBATCH_FREE(ptr, ctx)
// hashtable.h implementation by Mattias Gustavsson
// See: http://www.mattiasgustavsson.com/ and https://github.com/mattiasgustavsson/libs/blob/master/hashtable.h
// begin hashtable.h
#ifndef HASHTABLE_U64
#define HASHTABLE_U64 SPRITEBATCH_U64
#endif
#ifndef HASHTABLE_U32
#define HASHTABLE_U32 unsigned int
#endif
typedef struct hashtable_t hashtable_t;
void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx );
void hashtable_term( hashtable_t* table );
void* hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item );
void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key );
void hashtable_clear( hashtable_t* table );
void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key );
int hashtable_count( hashtable_t const* table );
void* hashtable_items( hashtable_t const* table );
HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table );
void hashtable_swap( hashtable_t* table, int index_a, int index_b );
struct hashtable_internal_slot_t
{
HASHTABLE_U32 key_hash;
int item_index;
int base_count;
};
struct hashtable_t
{
void* memctx;
int count;
int item_size;
struct hashtable_internal_slot_t* slots;
int slot_capacity;
HASHTABLE_U64* items_key;
int* items_slot;
void* items_data;
int item_capacity;
void* swap_temp;
};
#ifndef HASHTABLE_SIZE_T
#include <stddef.h>
#define HASHTABLE_SIZE_T size_t
#endif
#ifndef HASHTABLE_ASSERT
#include <assert.h>
#define HASHTABLE_ASSERT( x ) assert( x )
#endif
static HASHTABLE_U32 hashtable_internal_pow2ceil( HASHTABLE_U32 v )
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;
v += ( v == 0 );
return v;
}
void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx )
{
initial_capacity = (int)hashtable_internal_pow2ceil( initial_capacity >=0 ? (HASHTABLE_U32) initial_capacity : 32U );
table->memctx = memctx;
table->count = 0;
table->item_size = item_size;
table->slot_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) ( initial_capacity + initial_capacity / 2 ) );
int slots_size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) slots_size );
HASHTABLE_ASSERT( table->slots );
HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) slots_size );
table->item_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) initial_capacity );
table->items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size );
HASHTABLE_ASSERT( table->items_key );
table->items_slot = (int*)( table->items_key + table->item_capacity );
table->items_data = (void*)( table->items_slot + table->item_capacity );
table->swap_temp = (void*)( ( (uintptr_t) table->items_data ) + table->item_size * table->item_capacity );
}
void hashtable_term( hashtable_t* table )
{
HASHTABLE_FREE( table->memctx, table->items_key );
HASHTABLE_FREE( table->memctx, table->slots );
}
// from https://gist.github.com/badboy/6267743
static HASHTABLE_U32 hashtable_internal_calculate_hash( HASHTABLE_U64 key )
{
key = ( ~key ) + ( key << 18 );
key = key ^ ( key >> 31 );
key = key * 21;
key = key ^ ( key >> 11 );
key = key + ( key << 6 );
key = key ^ ( key >> 22 );
HASHTABLE_ASSERT( key );
return (HASHTABLE_U32) key;
}
static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U64 key )
{
int const slot_mask = table->slot_capacity - 1;
HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key );
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
while( base_count > 0 )
{
HASHTABLE_U32 slot_hash = table->slots[ slot ].key_hash;
if( slot_hash )
{
int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask );
if( slot_base == base_slot )
{
HASHTABLE_ASSERT( base_count > 0 );
--base_count;
if( slot_hash == hash && table->items_key[ table->slots[ slot ].item_index ] == key )
return slot;
}
}
slot = ( slot + 1 ) & slot_mask;
}
return -1;
}
static void hashtable_internal_expand_slots( hashtable_t* table )
{
int const old_capacity = table->slot_capacity;
struct hashtable_internal_slot_t* old_slots = table->slots;
table->slot_capacity *= 2;
int const slot_mask = table->slot_capacity - 1;
int const size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) size );
HASHTABLE_ASSERT( table->slots );
HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) size );
for( int i = 0; i < old_capacity; ++i )
{
HASHTABLE_U32 const hash = old_slots[ i ].key_hash;
if( hash )
{
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int slot = base_slot;
while( table->slots[ slot ].key_hash )
slot = ( slot + 1 ) & slot_mask;
table->slots[ slot ].key_hash = hash;
int item_index = old_slots[ i ].item_index;
table->slots[ slot ].item_index = item_index;
table->items_slot[ item_index ] = slot;
++table->slots[ base_slot ].base_count;
}
}
HASHTABLE_FREE( table->memctx, old_slots );
}
static void hashtable_internal_expand_items( hashtable_t* table )
{
table->item_capacity *= 2;
HASHTABLE_U64* const new_items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size);
HASHTABLE_ASSERT( new_items_key );
int* const new_items_slot = (int*)( new_items_key + table->item_capacity );
void* const new_items_data = (void*)( new_items_slot + table->item_capacity );
void* const new_swap_temp = (void*)( ( (uintptr_t) new_items_data ) + table->item_size * table->item_capacity );
HASHTABLE_MEMCPY( new_items_key, table->items_key, table->count * sizeof( *table->items_key ) );
HASHTABLE_MEMCPY( new_items_slot, table->items_slot, table->count * sizeof( *table->items_key ) );
HASHTABLE_MEMCPY( new_items_data, table->items_data, (HASHTABLE_SIZE_T) table->count * table->item_size );
HASHTABLE_FREE( table->memctx, table->items_key );
table->items_key = new_items_key;
table->items_slot = new_items_slot;
table->items_data = new_items_data;
table->swap_temp = new_swap_temp;
}
void* hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item )
{
HASHTABLE_ASSERT( hashtable_internal_find_slot( table, key ) < 0 );
if( table->count >= ( table->slot_capacity - table->slot_capacity / 3 ) )
hashtable_internal_expand_slots( table );
int const slot_mask = table->slot_capacity - 1;
HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key );
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
int first_free = slot;
while( base_count )
{
HASHTABLE_U32 const slot_hash = table->slots[ slot ].key_hash;
if( slot_hash == 0 && table->slots[ first_free ].key_hash != 0 ) first_free = slot;
int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask );
if( slot_base == base_slot )
--base_count;
slot = ( slot + 1 ) & slot_mask;
}
slot = first_free;
while( table->slots[ slot ].key_hash )
slot = ( slot + 1 ) & slot_mask;
if( table->count >= table->item_capacity )
hashtable_internal_expand_items( table );
HASHTABLE_ASSERT( !table->slots[ slot ].key_hash && ( hash & (HASHTABLE_U32) slot_mask ) == (HASHTABLE_U32) base_slot );
HASHTABLE_ASSERT( hash );
table->slots[ slot ].key_hash = hash;
table->slots[ slot ].item_index = table->count;
++table->slots[ base_slot ].base_count;
void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size );
memcpy( dest_item, item, (HASHTABLE_SIZE_T) table->item_size );
table->items_key[ table->count ] = key;
table->items_slot[ table->count ] = slot;
++table->count;
return dest_item;
}
void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key )
{
int const slot = hashtable_internal_find_slot( table, key );
HASHTABLE_ASSERT( slot >= 0 );
int const slot_mask = table->slot_capacity - 1;
HASHTABLE_U32 const hash = table->slots[ slot ].key_hash;
int const base_slot = (int)( hash & (HASHTABLE_U32) slot_mask );
HASHTABLE_ASSERT( hash );
--table->slots[ base_slot ].base_count;
table->slots[ slot ].key_hash = 0;
int index = table->slots[ slot ].item_index;
int last_index = table->count - 1;
if( index != last_index )
{
table->items_key[ index ] = table->items_key[ last_index ];
table->items_slot[ index ] = table->items_slot[ last_index ];
void* dst_item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size );
void* src_item = (void*)( ( (uintptr_t) table->items_data ) + last_index * table->item_size );
HASHTABLE_MEMCPY( dst_item, src_item, (HASHTABLE_SIZE_T) table->item_size );
table->slots[ table->items_slot[ last_index ] ].item_index = index;
}
--table->count;
}
void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key )
{
int const slot = hashtable_internal_find_slot( table, key );
if( slot < 0 ) return 0;
int const index = table->slots[ slot ].item_index;
void* const item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size );
return item;
}
void hashtable_clear( hashtable_t* table )
{
table->count = 0;
HASHTABLE_MEMSET( table->slots, 0, table->slot_capacity * sizeof( *table->slots ) );
}
int hashtable_count( hashtable_t const* table )
{
return table->count;
}
void* hashtable_items( hashtable_t const* table )
{
return table->items_data;
}
HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table )
{
return table->items_key;
}
void hashtable_swap( hashtable_t* table, int index_a, int index_b )
{
if( index_a < 0 || index_a >= table->count || index_b < 0 || index_b >= table->count ) return;
int slot_a = table->items_slot[ index_a ];
int slot_b = table->items_slot[ index_b ];
table->items_slot[ index_a ] = slot_b;
table->items_slot[ index_b ] = slot_a;
HASHTABLE_U64 temp_key = table->items_key[ index_a ];
table->items_key[ index_a ] = table->items_key[ index_b ];
table->items_key[ index_b ] = temp_key;
void* item_a = (void*)( ( (uintptr_t) table->items_data ) + index_a * table->item_size );
void* item_b = (void*)( ( (uintptr_t) table->items_data ) + index_b * table->item_size );
HASHTABLE_MEMCPY( table->swap_temp, item_a, table->item_size );
HASHTABLE_MEMCPY( item_a, item_b, table->item_size );
HASHTABLE_MEMCPY( item_b, table->swap_temp, table->item_size );
table->slots[ slot_a ].item_index = index_b;
table->slots[ slot_b ].item_index = index_a;
}
// end of hashtable.h
typedef struct
{
SPRITEBATCH_U64 image_id;
SPRITEBATCH_U64 sort_bits;
int w;
int h;
float x, y;
float sx, sy;
float c, s;
} spritebatch_internal_sprite_t;
typedef struct
{
int timestamp;
int w, h;
float minx, miny;
float maxx, maxy;
SPRITEBATCH_U64 image_id;
} spritebatch_internal_texture_t;
typedef struct spritebatch_internal_atlas_t
{
SPRITEBATCH_U64 texture_id;
float volume_ratio;
hashtable_t sprites_to_textures;
struct spritebatch_internal_atlas_t* next;
struct spritebatch_internal_atlas_t* prev;
} spritebatch_internal_atlas_t;
typedef struct
{
int timestamp;
int w, h;
SPRITEBATCH_U64 image_id;
SPRITEBATCH_U64 texture_id;
} spritebatch_internal_lonely_texture_t;
struct spritebatch_t
{
int input_count;
int input_capacity;
spritebatch_internal_sprite_t* input_buffer;
int sprite_count;
int sprite_capacity;
spritebatch_sprite_t* sprites;
int key_buffer_count;
int key_buffer_capacity;
SPRITEBATCH_U64* key_buffer;
hashtable_t sprites_to_lonely_textures;
hashtable_t sprites_to_atlases;
spritebatch_internal_atlas_t* atlases;
int pixel_stride;
int atlas_width_in_pixels;
int atlas_height_in_pixels;
int ticks_to_decay_texture;
int lonely_buffer_count_till_flush;
int lonely_buffer_count_till_decay;
float ratio_to_decay_atlas;
float ratio_to_merge_atlases;
submit_batch_t batch_callback;
get_pixels_t get_pixels_callback;
generate_texture_handle_t generate_texture_callback;
destroy_texture_handle_t delete_texture_callback;
void* mem_ctx;
};
int spritebatch_init(spritebatch_t* sb, spritebatch_config_t* config)
{
// read config params
if (!config | !sb) return 1;
sb->pixel_stride = config->pixel_stride;
sb->atlas_width_in_pixels = config->atlas_width_in_pixels;
sb->atlas_height_in_pixels = config->atlas_height_in_pixels;
sb->ticks_to_decay_texture = config->ticks_to_decay_texture;
sb->lonely_buffer_count_till_flush = config->lonely_buffer_count_till_flush;
sb->lonely_buffer_count_till_decay = sb->lonely_buffer_count_till_flush / 2;
if (sb->lonely_buffer_count_till_decay <= 0) sb->lonely_buffer_count_till_decay = 1;
sb->ratio_to_decay_atlas = config->ratio_to_decay_atlas;
sb->ratio_to_merge_atlases = config->ratio_to_merge_atlases;
sb->batch_callback = config->batch_callback;
sb->get_pixels_callback = config->get_pixels_callback;
sb->generate_texture_callback = config->generate_texture_callback;
sb->delete_texture_callback = config->delete_texture_callback;
sb->mem_ctx = config->allocator_context;
if (sb->atlas_width_in_pixels < 1 || sb->atlas_height_in_pixels < 1) return 1;
if (sb->ticks_to_decay_texture < 1) return 1;
if (sb->ratio_to_decay_atlas < 0 || sb->ratio_to_decay_atlas > 1.0f) return 1;
if (sb->ratio_to_merge_atlases < 0 || sb->ratio_to_merge_atlases > 0.5f) return 1;
if (!sb->batch_callback) return 1;
if (!sb->get_pixels_callback) return 1;
if (!sb->generate_texture_callback) return 1;
if (!sb->delete_texture_callback) return 1;
// initialize input buffer
sb->input_count = 0;
sb->input_capacity = 1024;
sb->input_buffer = (spritebatch_internal_sprite_t*)SPRITEBATCH_MALLOC(sizeof(spritebatch_internal_sprite_t) * sb->input_capacity, sb->mem_ctx);
if (!sb->input_buffer) return 1;
// initialize sprite buffer
sb->sprite_count = 0;
sb->sprite_capacity = 1024;
sb->sprites = (spritebatch_sprite_t*)SPRITEBATCH_MALLOC(sizeof(spritebatch_sprite_t) * sb->sprite_capacity, sb->mem_ctx);
if (!sb->sprites) return 1;
// initialize key buffer (for marking hash table entries for deletion)
sb->key_buffer_count = 0;
sb->key_buffer_capacity = 1024;
sb->key_buffer = (SPRITEBATCH_U64*)SPRITEBATCH_MALLOC(sizeof(SPRITEBATCH_U64) * sb->key_buffer_capacity, sb->mem_ctx);
// setup tables
hashtable_init(&sb->sprites_to_lonely_textures, sizeof(spritebatch_internal_lonely_texture_t), 1024, sb->mem_ctx);
hashtable_init(&sb->sprites_to_atlases, sizeof(spritebatch_internal_atlas_t*), 16, sb->mem_ctx);
sb->atlases = 0;
return 0;
}
void spritebatch_term(spritebatch_t* sb)
{
SPRITEBATCH_FREE(sb->input_buffer, sb->mem_ctx);
SPRITEBATCH_FREE(sb->sprites, sb->mem_ctx);
SPRITEBATCH_FREE(sb->key_buffer, sb->mem_ctx);
hashtable_term(&sb->sprites_to_lonely_textures);
hashtable_term(&sb->sprites_to_atlases);
if (sb->atlases)
{
spritebatch_internal_atlas_t* atlas = sb->atlases;
spritebatch_internal_atlas_t* sentinel = sb->atlases;
do
{
hashtable_term(&atlas->sprites_to_textures);
spritebatch_internal_atlas_t* next = atlas->next;
SPRITEBATCH_FREE(atlas, sb->mem_ctx);
atlas = next;
}
while (atlas != sentinel);
}
SPRITEBATCH_MEMSET(sb, 0, sizeof(spritebatch_t));
}
void spritebatch_set_default_config(spritebatch_config_t* config)
{
config->pixel_stride = sizeof(char) * 4;
config->atlas_width_in_pixels = 1024;
config->atlas_height_in_pixels = 1024;
config->ticks_to_decay_texture = 60 * 30;
config->lonely_buffer_count_till_flush = 64;
config->ratio_to_decay_atlas = 0.5f;
config->ratio_to_merge_atlases = 0.25f;
config->batch_callback = 0;
config->generate_texture_callback = 0;
config->delete_texture_callback = 0;
config->allocator_context = 0;
}
#define SPRITEBATCH_CHECK_BUFFER_GROW(ctx, count, capacity, data, type) \
do { \
if (ctx->count == ctx->capacity) \
{ \
int new_capacity = ctx->capacity * 2; \
void* new_data = SPRITEBATCH_MALLOC(sizeof(type) * new_capacity, ctx->mem_ctx); \
if (!new_data) return 0; \
SPRITEBATCH_MEMCPY(new_data, ctx->data, sizeof(type) * ctx->count); \
SPRITEBATCH_FREE(ctx->data, ctx->mem_ctx); \
ctx->data = (type*)new_data; \
ctx->capacity = new_capacity; \
} \
} while (0)
static SPRITEBATCH_U64 spritebatch_make_sort_key(int index, int sort_bits)
{
return (((SPRITEBATCH_U64)sort_bits) << 32) | ((SPRITEBATCH_U64)index);
}
int spritebatch_push(spritebatch_t* sb, SPRITEBATCH_U64 image_id, int w, int h, float x, float y, float sx, float sy, float c, float s, int sort_bits)
{
SPRITEBATCH_CHECK_BUFFER_GROW(sb, input_count, input_capacity, input_buffer, spritebatch_internal_sprite_t);
spritebatch_internal_sprite_t sprite;
sprite.image_id = image_id;
sprite.sort_bits = spritebatch_make_sort_key(sb->input_count, sort_bits);
sprite.w = w;
sprite.h = h;
sprite.x = x;
sprite.y = y;
sprite.sx = sx;
sprite.sy = sy;
sprite.c = c;
sprite.s = s;
sb->input_buffer[sb->input_count++] = sprite;
return 1;
}
static int spritebatch_internal_instance_pred(spritebatch_sprite_t* a, spritebatch_sprite_t* b)
{
if (a->sort_bits < b->sort_bits) return 1;
else if(a->sort_bits == b->sort_bits) return a->texture_id < b->texture_id;
else return 0;
}
static void spritebatch_internal_qsort_sprites(spritebatch_sprite_t* items, int count)
{
if (count <= 1) return;
spritebatch_sprite_t pivot = items[count - 1];
int low = 0;
for (int i = 0; i < count - 1; ++i)
{
if (spritebatch_internal_instance_pred(items + i, &pivot))
{
spritebatch_sprite_t tmp = items[i];
items[i] = items[low];
items[low] = tmp;
low++;
}
}
items[count - 1] = items[low];
items[low] = pivot;
spritebatch_internal_qsort_sprites(items, low);
spritebatch_internal_qsort_sprites(items + low + 1, count - 1 - low);
}
spritebatch_internal_lonely_texture_t* spritebatch_internal_lonelybuffer_push(spritebatch_t* sb, SPRITEBATCH_U64 image_id, int w, int h, int make_tex)
{
spritebatch_internal_lonely_texture_t texture;
texture.timestamp = 0;
texture.w = w;
texture.h = h;
texture.image_id = image_id;
texture.texture_id = make_tex ? sb->generate_texture_callback(sb->get_pixels_callback(image_id), w, h) : ~0;
return (spritebatch_internal_lonely_texture_t*)hashtable_insert(&sb->sprites_to_lonely_textures, image_id, &texture);
}
int spritebatch_internal_lonely_sprite(spritebatch_t* sb, spritebatch_internal_sprite_t* s, spritebatch_sprite_t* sprite, int skip_missing_textures)
{
spritebatch_internal_lonely_texture_t* tex = (spritebatch_internal_lonely_texture_t*)hashtable_find(&sb->sprites_to_lonely_textures, s->image_id);
if (skip_missing_textures)
{
if (!tex) spritebatch_internal_lonelybuffer_push(sb, s->image_id, s->w, s->h, 0);
return 1;
}
else
{
if (!tex) tex = spritebatch_internal_lonelybuffer_push(sb, s->image_id, s->w, s->h, 1);
else if (tex->texture_id == ~0) tex->texture_id = sb->generate_texture_callback(sb->get_pixels_callback(s->image_id), s->w, s->h);
tex->timestamp = 0;
sprite->texture_id = tex->texture_id;
sprite->minx = sprite->miny = 0;
sprite->maxx = sprite->maxy = 1.0f;
if (SPRITEBATCH_LONELY_FLIP_Y_AXIS_FOR_UV)
{
float tmp = sprite->miny;
sprite->miny = sprite->maxy;
sprite->maxy = tmp;
}
return 0;
}
}
int spritebatch_internal_push_sprite(spritebatch_t* sb, spritebatch_internal_sprite_t* s, int skip_missing_textures)
{
int skipped_tex = 0;
spritebatch_sprite_t sprite;
sprite.sort_bits = s->sort_bits;
sprite.x = s->x;
sprite.y = s->y;
sprite.sx = s->sx;
sprite.sy = s->sy;
sprite.c = s->c;
sprite.s = s->s;
void* atlas_ptr = hashtable_find(&sb->sprites_to_atlases, s->image_id);
if (atlas_ptr)
{
spritebatch_internal_atlas_t* atlas = *(spritebatch_internal_atlas_t**)atlas_ptr;
sprite.texture_id = atlas->texture_id;
spritebatch_internal_texture_t* tex = (spritebatch_internal_texture_t*)hashtable_find(&atlas->sprites_to_textures, s->image_id);
SPRITEBATCH_ASSERT(tex);
tex->timestamp = 0;
tex->w = s->w;
tex->h = s->h;
sprite.minx = tex->minx;
sprite.miny = tex->miny;
sprite.maxx = tex->maxx;
sprite.maxy = tex->maxy;
}
else skipped_tex = spritebatch_internal_lonely_sprite(sb, s, &sprite, skip_missing_textures);
if (!skipped_tex)
{
SPRITEBATCH_CHECK_BUFFER_GROW(sb, sprite_count, sprite_capacity, sprites, spritebatch_sprite_t);
sb->sprites[sb->sprite_count++] = sprite;
}
return skipped_tex;
}
void spritebatch_internal_process_input(spritebatch_t* sb, int skip_missing_textures)
{
int skipped_index = 0;
for (int i = 0; i < sb->input_count; ++i)
{
spritebatch_internal_sprite_t* s = sb->input_buffer + i;
int skipped = spritebatch_internal_push_sprite(sb, s, skip_missing_textures);
if (skip_missing_textures && skipped) sb->input_buffer[skipped_index++] = *s;
}
sb->input_count = skipped_index;
}
void spritebatch_tick(spritebatch_t* sb)
{
spritebatch_internal_atlas_t* atlas = sb->atlases;
if (atlas)
{
spritebatch_internal_atlas_t* sentinel = atlas;
do
{
int texture_count = hashtable_count(&atlas->sprites_to_textures);
spritebatch_internal_texture_t* textures = (spritebatch_internal_texture_t*)hashtable_items(&atlas->sprites_to_textures);
for (int i = 0; i < texture_count; ++i) textures[i].timestamp += 1;
atlas = atlas->next;
}
while (atlas != sentinel);
}
int texture_count = hashtable_count(&sb->sprites_to_lonely_textures);
spritebatch_internal_lonely_texture_t* lonely_textures = (spritebatch_internal_lonely_texture_t*)hashtable_items(&sb->sprites_to_lonely_textures);
for (int i = 0; i < texture_count; ++i) lonely_textures[i].timestamp += 1;
}
int spritebatch_flush(spritebatch_t* sb)
{
// process input buffer, make any necessary lonely textures
// convert user sprites to internal format
// lookup uv coordinates
spritebatch_internal_process_input(sb, 0);
// patchup any missing lonely textures that may have come from atlases decaying and whatnot
int texture_count = hashtable_count(&sb->sprites_to_lonely_textures);
spritebatch_internal_lonely_texture_t* lonely_textures = (spritebatch_internal_lonely_texture_t*)hashtable_items(&sb->sprites_to_lonely_textures);
for (int i = 0; i < texture_count; ++i)
{
spritebatch_internal_lonely_texture_t* lonely = lonely_textures + i;