forked from chaoticbob/tinyrenderers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyvk.h
5539 lines (4728 loc) · 249 KB
/
tinyvk.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copyright (c) 2017, The Cinder Project, All rights reserved.
This code is intended for use with the Cinder C++ library: http://libcinder.org
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
NOTES:
- Microsoft's C compiler doesn't support certain C11/C99 features, such as VLAs
- tinyvk/tinydx is written for experimentation and fun-having - not performance
- For simplicity, only one descriptor set can be bound at once
- In D3D12, this means two descriptor heaps (CBVSRVUAVs and samplers)
- For Vulkan shaders the 'set' parameter for 'layout' should always be 0
- For D3D12 shaders the 'space' parameter for resource bindings should always be 0
- Vulkan like idioms are used primarily with some D3D12 wherever it makes sense
- Storage buffers created with tr_create_storage_buffer are not host visible.
- This was done to align the behavior on Vulkan and D3D12. Vulkan's storage
buffers can be host visible, but D3D12's UAV buffers are not permitted to
be host visible.
COMPILING & LINKING
In one C/C++ file that #includes this file, do this:
#define TINY_RENDERER_IMPLEMENTATION
before the #include. That will create the implementation in that file.
*/
#pragma once
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#if defined(__linux__)
#define TINY_RENDERER_LINUX
#define VK_USE_PLATFORM_XCB_KHR
#include <X11/Xlib-xcb.h>
#endif
#if defined(_WIN32)
#define TINY_RENDERER_MSW
#define VK_USE_PLATFORM_WIN32_KHR
// Pull in minimal Windows headers
#if ! defined(NOMINMAX)
#define NOMINMAX
#endif
#if ! defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#endif
#include <vulkan/vulkan.h>
#if defined(__cplusplus) && defined(TINY_RENDERER_CPP_NAMESPACE)
namespace TINY_RENDERER_CPP_NAMESPACE {
#endif
#if defined(__cplusplus)
#define tr_api_export extern "C"
#else
#define tr_api_export
#endif
#if ! defined(TINY_RENDERER_CUSTOM_MAX)
enum {
tr_max_instance_extensions = 1024,
tr_max_device_extensions = 1024,
tr_max_gpus = 4,
tr_max_descriptors = 32,
tr_max_descriptor_sets = 8,
tr_max_render_target_attachments = 8,
tr_max_submit_cmds = 8,
tr_max_submit_wait_semaphores = 8,
tr_max_submit_signal_semaphores = 8,
tr_max_present_wait_semaphores = 8,
tr_max_vertex_bindings = 15,
tr_max_vertex_attribs = 15,
tr_max_semantic_name_length = 128,
tr_max_descriptor_entries = 256,
tr_max_mip_levels = 0xFFFFFFFF,
};
#endif
typedef enum tr_api {
tr_api_vulkan = 0,
tr_api_d3d12
} tr_api;
typedef enum tr_log_type {
tr_log_type_info = 0,
tr_log_type_warn,
tr_log_type_debug,
tr_log_type_error
} tr_log_type;
typedef void(*tr_log_fn)(tr_log_type, const char*, const char*);
/*
There's a lot of things to get right in Vulkan for it to work at all. So for now, the renderer
will just assert in all the places where something goes wrong. Keep in mind that any memory
allocated prior to the assert is not freed.
In the future, maybe add a error handling system so keep these values around.
*/
/*
typedef enum tr_result {
// No errors
tr_result_ok = 0x00000000,
// Errors
tr_result_error_mask = 0x0000FFFF,
tr_result_unknown = 0x00000001,
tr_result_bad_ptr = 0x00000002,
tr_result_alloc_failed = 0x00000004,
tr_result_exceeded_max = 0x00000008,
// Components
tr_result_component_mask = 0x0FFF0000,
tr_result_general = 0x00000000,
tr_result_renderer = 0x00010000,
tr_result_environment = 0x00020000,
tr_result_device = 0x00040000,
tr_result_queue = 0x00080000,
tr_result_surface = 0x00100000,
tr_result_swapchain = 0x00200000,
tr_result_render_target = 0x00400000,
tr_result_buffer = 0x00800000,
tr_result_texture = 0x01000000,
tr_result_cmd = 0x02000000,
tr_result_fence = 0x04000000,
tr_result_semaphore = 0x08000000,
// Internal API
tr_result_internal_api_mask = 0xF0000000,
tr_result_internal_api = 0x10000000,
} tr_result;
*/
typedef enum tr_buffer_usage {
tr_buffer_usage_index = 0x00000001,
tr_buffer_usage_vertex = 0x00000002,
tr_buffer_usage_indirect = 0x00000004,
tr_buffer_usage_transfer_src = 0x00000008,
tr_buffer_usage_transfer_dst = 0x00000010,
tr_buffer_usage_uniform_cbv = 0x00000020,
tr_buffer_usage_storage_srv = 0x00000040,
tr_buffer_usage_storage_uav = 0x00000080,
tr_buffer_usage_uniform_texel_srv = 0x00000100,
tr_buffer_usage_storage_texel_uav = 0x00000200,
} tr_buffer_usage;
typedef enum tr_texture_type {
tr_texture_type_1d,
tr_texture_type_2d,
tr_texture_type_3d,
tr_texture_type_cube,
} tr_texture_type;
typedef enum tr_texture_usage {
tr_texture_usage_undefined = 0x00000000,
tr_texture_usage_transfer_src = 0x00000001,
tr_texture_usage_transfer_dst = 0x00000002,
tr_texture_usage_sampled_image = 0x00000004,
tr_texture_usage_storage_image = 0x00000008,
tr_texture_usage_color_attachment = 0x00000010,
tr_texture_usage_depth_stencil_attachment = 0x00000020,
tr_texture_usage_resolve_src = 0x00000040,
tr_texture_usage_resolve_dst = 0x00000080,
tr_texture_usage_present = 0x00000100,
} tr_texture_usage;
typedef uint32_t tr_texture_usage_flags;
typedef enum tr_format {
tr_format_undefined = 0,
// 1 channel
tr_format_r8_unorm,
tr_format_r16_unorm,
tr_format_r16_float,
tr_format_r32_uint,
tr_format_r32_float,
// 2 channel
tr_format_r8g8_unorm,
tr_format_r16g16_unorm,
tr_format_r16g16_float,
tr_format_r32g32_uint,
tr_format_r32g32_float,
// 3 channel
tr_format_r8g8b8_unorm,
tr_format_r16g16b16_unorm,
tr_format_r16g16b16_float,
tr_format_r32g32b32_uint,
tr_format_r32g32b32_float,
// 4 channel
tr_format_b8g8r8a8_unorm,
tr_format_r8g8b8a8_unorm,
tr_format_r16g16b16a16_unorm,
tr_format_r16g16b16a16_float,
tr_format_r32g32b32a32_uint,
tr_format_r32g32b32a32_float,
// Depth/stencil
tr_format_d16_unorm,
tr_format_x8_d24_unorm_pack32,
tr_format_d32_float,
tr_format_s8_uint,
tr_format_d16_unorm_s8_uint,
tr_format_d24_unorm_s8_uint,
tr_format_d32_float_s8_uint,
} tr_format;
typedef enum tr_descriptor_type {
tr_descriptor_type_undefined = 0,
tr_descriptor_type_sampler,
tr_descriptor_type_uniform_buffer_cbv, // CBV | VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
tr_descriptor_type_storage_buffer_srv, // SRV | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
tr_descriptor_type_storage_buffer_uav, // UAV | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
tr_descriptor_type_uniform_texel_buffer_srv, // SRV | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
tr_descriptor_type_storage_texel_buffer_uav, // UAV | VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
tr_descriptor_type_texture_srv, // SRV | VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
tr_descriptor_type_texture_uav, // UAV | VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
} tr_descriptor_type;
typedef enum tr_sample_count {
tr_sample_count_1 = 1,
tr_sample_count_2 = 2,
tr_sample_count_4 = 4,
tr_sample_count_8 = 8,
tr_sample_count_16 = 16,
} tr_sample_count;
typedef enum tr_shader_stage {
tr_shader_stage_vert = 0x00000001,
tr_shader_stage_tesc = 0x00000002,
tr_shader_stage_tese = 0x00000004,
tr_shader_stage_geom = 0x00000008,
tr_shader_stage_frag = 0x00000010,
tr_shader_stage_comp = 0x00000020,
tr_shader_stage_all_graphics = 0x0000001F,
tr_shader_stage_count = 6,
} tr_shader_stage;
typedef enum tr_primitive_topo {
tr_primitive_topo_point_list = 0,
tr_primitive_topo_line_list,
tr_primitive_topo_line_strip,
tr_primitive_topo_tri_list,
tr_primitive_topo_tri_strip,
tr_primitive_topo_tri_fan,
tr_primitive_topo_1_point_patch,
tr_primitive_topo_2_point_patch,
tr_primitive_topo_3_point_patch,
tr_primitive_topo_4_point_patch,
} tr_primitive_topo;
typedef enum tr_index_type {
tr_index_type_uint32 = 0,
tr_index_type_uint16,
} tr_index_type;
typedef enum tr_semantic {
tr_semantic_undefined = 0,
tr_semantic_position,
tr_semantic_normal,
tr_semantic_color,
tr_semantic_tangent,
tr_semantic_bitangent,
tr_semantic_texcoord0,
tr_semantic_texcoord1,
tr_semantic_texcoord2,
tr_semantic_texcoord3,
tr_semantic_texcoord4,
tr_semantic_texcoord5,
tr_semantic_texcoord6,
tr_semantic_texcoord7,
tr_semantic_texcoord8,
tr_semantic_texcoord9,
} tr_semantic;
typedef enum tr_cull_mode {
tr_cull_mode_none = 0,
tr_cull_mode_back,
tr_cull_mode_front,
tr_cull_mode_both
} tr_cull_mode;
typedef enum tr_front_face {
tr_front_face_ccw = 0,
tr_fornt_face_cw
} tr_front_face;
typedef enum tr_pipeline_type {
tr_pipeline_type_undefined = 0,
tr_pipeline_type_compute,
tr_pipeline_type_graphics
} tr_pipeline_type;
// Forward declarations
typedef struct tr_renderer tr_renderer;
typedef struct tr_render_target tr_render_target;
typedef struct tr_buffer tr_buffer;
typedef struct tr_texture tr_texture;
typedef struct tr_sampler tr_sampler;
typedef struct tr_clear_value {
union {
struct {
float r;
float g;
float b;
float a;
};
struct {
float depth;
uint32_t stencil;
};
};
} tr_clear_value;
typedef struct tr_platform_handle {
#if defined(__linux__)
xcb_connection_t* connection;
xcb_window_t window;
#elif defined(TINY_RENDERER_MSW)
HINSTANCE hinstance;
HWND hwnd;
#endif
} tr_platform_handle;
typedef struct tr_swapchain_settings {
uint32_t image_count;
tr_sample_count sample_count;
uint32_t sample_quality;
tr_format color_format;
tr_clear_value color_clear_value;
tr_format depth_stencil_format;
tr_clear_value depth_stencil_clear_value;
} tr_swapchain_settings;
typedef struct tr_string_list {
uint32_t count;
const char** names;
} tr_string_list;
typedef struct tr_renderer_settings {
tr_platform_handle handle;
uint32_t width;
uint32_t height;
tr_swapchain_settings swapchain;
tr_log_fn log_fn;
// Vulkan specific options
tr_string_list instance_layers;
tr_string_list instance_extensions;
//tr_string_list device_layers;
tr_string_list device_extensions;
PFN_vkDebugReportCallbackEXT vk_debug_fn;
} tr_renderer_settings;
typedef struct tr_fence {
VkFence vk_fence;
} tr_fence;
typedef struct tr_semaphore {
VkSemaphore vk_semaphore;
} tr_semaphore;
typedef struct tr_queue {
tr_renderer* renderer;
VkQueue vk_queue;
uint32_t vk_queue_family_index;
} tr_queue;
typedef struct tr_renderer {
tr_api api;
tr_renderer_settings settings;
tr_render_target** swapchain_render_targets;
uint32_t swapchain_image_index;
tr_queue* graphics_queue;
tr_queue* present_queue;
tr_fence** image_acquired_fences;
tr_semaphore** image_acquired_semaphores;
tr_semaphore** render_complete_semaphores;
VkInstance vk_instance;
uint32_t vk_gpu_count;
VkPhysicalDevice vk_gpus[tr_max_gpus];
VkPhysicalDevice vk_active_gpu;
uint32_t vk_active_gpu_index;
VkPhysicalDeviceMemoryProperties vk_memory_properties;
VkPhysicalDeviceProperties vk_active_gpu_properties;
VkDevice vk_device;
VkSurfaceKHR vk_surface;
VkSwapchainKHR vk_swapchain;
VkDebugReportCallbackEXT vk_debug_report;
bool vk_device_ext_VK_AMD_negative_viewport_height;
} tr_renderer;
typedef struct tr_descriptor {
tr_descriptor_type type;
uint32_t binding;
uint32_t count;
tr_shader_stage shader_stages;
tr_buffer* uniform_buffers[tr_max_descriptor_entries];
tr_texture* textures[tr_max_descriptor_entries];
tr_sampler* samplers[tr_max_descriptor_entries];
tr_buffer* buffers[tr_max_descriptor_entries];
} tr_descriptor;
typedef struct tr_descriptor_set {
uint32_t descriptor_count;
tr_descriptor* descriptors;
VkDescriptorSetLayout vk_descriptor_set_layout;
VkDescriptorSet vk_descriptor_set;
VkDescriptorPool vk_descriptor_pool;
} tr_descriptor_set;
typedef struct tr_cmd_pool {
tr_renderer* renderer;
VkCommandPool vk_cmd_pool;
} tr_cmd_pool;
typedef struct tr_cmd {
tr_cmd_pool* cmd_pool;
VkCommandBuffer vk_cmd_buf;
} tr_cmd;
typedef struct tr_buffer {
tr_renderer* renderer;
tr_buffer_usage usage;
uint64_t size;
bool host_visible;
tr_index_type index_type;
uint32_t vertex_stride;
tr_format format;
uint64_t first_element;
uint64_t element_count;
uint64_t struct_stride;
bool raw;
void* cpu_mapped_address;
VkBuffer vk_buffer;
VkDeviceMemory vk_memory;
// Used for uniform and storage buffers
VkDescriptorBufferInfo vk_buffer_info;
// Used for uniform texel and storage texel buffers
VkBufferView vk_buffer_view;
// Counter buffer
tr_buffer* counter_buffer;
} tr_buffer;
typedef struct tr_texture {
tr_renderer* renderer;
tr_texture_type type;
tr_texture_usage_flags usage;
uint32_t width;
uint32_t height;
uint32_t depth;
tr_format format;
uint32_t mip_levels;
tr_sample_count sample_count;
uint32_t sample_quality;
tr_clear_value clear_value;
bool host_visible;
void* cpu_mapped_address;
uint32_t owns_image;
VkImage vk_image;
VkDeviceMemory vk_memory;
VkImageView vk_image_view;
VkImageAspectFlags vk_aspect_mask;
VkDescriptorImageInfo vk_texture_view;
} tr_texture;
typedef struct tr_sampler {
tr_renderer* renderer;
VkSampler vk_sampler;
VkDescriptorImageInfo vk_sampler_view;
} tr_sampler;
typedef struct tr_shader_program {
tr_renderer* renderer;
uint32_t shader_stages;
VkShaderModule vk_vert;
VkShaderModule vk_tesc;
VkShaderModule vk_tese;
VkShaderModule vk_geom;
VkShaderModule vk_frag;
VkShaderModule vk_comp;
const char* vert_entry_point;
const char* tesc_entry_point;
const char* tese_entry_point;
const char* geom_entry_point;
const char* frag_entry_point;
const char* comp_entry_point;
} tr_shader_program;
typedef struct tr_vertex_attrib {
tr_semantic semantic;
uint32_t semantic_name_length;
char semantic_name[tr_max_semantic_name_length];
tr_format format;
uint32_t binding;
uint32_t location;
uint32_t offset;
} tr_vertex_attrib;
typedef struct tr_vertex_layout {
uint32_t attrib_count;
tr_vertex_attrib attribs[tr_max_vertex_attribs];
} tr_vertex_layout;
typedef struct tr_pipeline_settings {
tr_primitive_topo primitive_topo;
tr_cull_mode cull_mode;
tr_front_face front_face;
bool depth;
} tr_pipeline_settings;
typedef struct tr_pipeline {
tr_renderer* renderer;
tr_pipeline_settings settings;
tr_pipeline_type type;
VkPipelineLayout vk_pipeline_layout;
VkPipeline vk_pipeline;
} tr_pipeline;
typedef struct tr_render_target {
tr_renderer* renderer;
uint32_t width;
uint32_t height;
tr_sample_count sample_count;
tr_format color_format;
uint32_t color_attachment_count;
tr_texture* color_attachments[tr_max_render_target_attachments];
tr_texture* color_attachments_multisample[tr_max_render_target_attachments];
tr_format depth_stencil_format;
tr_texture* depth_stencil_attachment;
tr_texture* depth_stencil_attachment_multisample;
VkRenderPass vk_render_pass;
VkFramebuffer vk_framebuffer;
} tr_render_target;
typedef struct tr_mesh {
tr_renderer* renderer;
tr_buffer* uniform_buffer;
tr_buffer* index_buffer;
tr_buffer* vertex_buffer;
tr_shader_program* shader_program;
tr_pipeline* pipeline;
} tr_mesh;
typedef bool(*tr_image_resize_uint8_fn)(uint32_t src_width, uint32_t src_height, uint32_t src_row_stride, const uint8_t* p_src_data,
uint32_t dst_width, uint32_t dst_height, uint32_t dst_row_stride, uint8_t* p_dst_data,
uint32_t channel_cout, void* p_user_data);
typedef bool(*tr_image_resize_float_fn)(uint32_t src_width, uint32_t src_height, uint32_t src_row_stride, const float* p_src_data,
uint32_t dst_width, uint32_t dst_height, uint32_t dst_row_stride, float* p_dst_data,
uint32_t channel_cout, void* p_user_data);
// API functions
tr_api_export void tr_create_renderer(const char* app_name, const tr_renderer_settings* p_settings, tr_renderer** pp_renderer);
tr_api_export void tr_destroy_renderer(tr_renderer* p_renderer);
tr_api_export void tr_create_fence(tr_renderer* p_renderer, tr_fence** pp_fence);
tr_api_export void tr_destroy_fence(tr_renderer* p_renderer, tr_fence* p_fence);
tr_api_export void tr_create_semaphore(tr_renderer* p_renderer, tr_semaphore** pp_semaphore);
tr_api_export void tr_destroy_semaphore(tr_renderer* p_renderer, tr_semaphore* p_semaphore);
tr_api_export void tr_create_descriptor_set(tr_renderer* p_renderer, uint32_t descriptor_count, const tr_descriptor* descriptors, tr_descriptor_set** pp_descriptor_set);
tr_api_export void tr_destroy_descriptor_set(tr_renderer* p_renderer, tr_descriptor_set* p_descriptor_set);
tr_api_export void tr_create_cmd_pool(tr_renderer* p_renderer, tr_queue* p_queue, bool transient, tr_cmd_pool** pp_cmd_pool);
tr_api_export void tr_destroy_cmd_pool(tr_renderer* p_renderer, tr_cmd_pool* p_cmd_pool);
tr_api_export void tr_create_cmd(tr_cmd_pool* p_cmd_pool, bool secondary, tr_cmd** pp_cmd);
tr_api_export void tr_destroy_cmd(tr_cmd_pool* p_cmd_pool, tr_cmd* p_cmd);
tr_api_export void tr_create_cmd_n(tr_cmd_pool* p_cmd_pool, bool secondary, uint32_t cmd_count, tr_cmd*** ppp_cmd);
tr_api_export void tr_destroy_cmd_n(tr_cmd_pool* p_cmd_pool, uint32_t cmd_count, tr_cmd** pp_cmd);
tr_api_export void tr_create_buffer(tr_renderer* p_renderer, tr_buffer_usage usage, uint64_t size, bool host_visible, tr_buffer** pp_buffer);
tr_api_export void tr_create_index_buffer(tr_renderer*p_renderer, uint64_t size, bool host_visible, tr_index_type index_type, tr_buffer** pp_buffer);
tr_api_export void tr_create_uniform_buffer(tr_renderer* p_renderer, uint64_t size, bool host_visible, tr_buffer** pp_buffer);
tr_api_export void tr_create_vertex_buffer(tr_renderer* p_renderer, uint64_t size, bool host_visible, uint32_t vertex_stride, tr_buffer** pp_buffer);
tr_api_export void tr_create_structured_buffer(tr_renderer* p_renderer, uint64_t size, uint64_t first_element, uint64_t element_count, uint64_t struct_stride, bool raw, tr_buffer** pp_buffer);
tr_api_export void tr_create_rw_structured_buffer(tr_renderer* p_renderer, uint64_t size, uint64_t first_element, uint64_t element_count, uint64_t struct_stride, bool raw, tr_buffer** pp_counter_buffer, tr_buffer** pp_buffer);
tr_api_export void tr_destroy_buffer(tr_renderer* p_renderer, tr_buffer* p_buffer);
tr_api_export void tr_create_texture(tr_renderer* p_renderer, tr_texture_type type, uint32_t width, uint32_t height, uint32_t depth, tr_sample_count sample_count, tr_format format, uint32_t mip_levels, const tr_clear_value* p_clear_value, bool host_visible, tr_texture_usage_flags usage, tr_texture** pp_texture);
tr_api_export void tr_create_texture_1d(tr_renderer* p_renderer, uint32_t width, tr_sample_count sample_count, tr_format format, bool host_visible, tr_texture_usage_flags usage, tr_texture** pp_texture);
tr_api_export void tr_create_texture_2d(tr_renderer* p_renderer, uint32_t width, uint32_t height, tr_sample_count sample_count, tr_format format, uint32_t mip_levels, const tr_clear_value* p_clear_value, bool host_visible, tr_texture_usage_flags usage, tr_texture** pp_texture);
tr_api_export void tr_create_texture_3d(tr_renderer* p_renderer, uint32_t width, uint32_t height, uint32_t depth, tr_sample_count sample_count, tr_format format, bool host_visible, tr_texture_usage_flags usage, tr_texture** pp_texture);
tr_api_export void tr_destroy_texture(tr_renderer* p_renderer, tr_texture*p_texture);
tr_api_export void tr_create_sampler(tr_renderer* p_renderer, tr_sampler** pp_sampler);
tr_api_export void tr_destroy_sampler(tr_renderer* p_renderer, tr_sampler* p_sampler);
tr_api_export void tr_create_shader_program_n(tr_renderer* p_renderer, uint32_t vert_size, const void* vert_code, const char* vert_enpt, uint32_t tesc_size, const void* tesc_code, const char* tesc_enpt, uint32_t tese_size, const void* tese_code, const char* tese_enpt, uint32_t geom_size, const void* geom_code, const char* geom_enpt, uint32_t frag_size, const void* frag_code, const char* frag_enpt, uint32_t comp_size, const void* comp_code, const char* comp_enpt, tr_shader_program** pp_shader_program);
tr_api_export void tr_create_shader_program(tr_renderer* p_renderer, uint32_t vert_size, const void* vert_code, const char* vert_enpt, uint32_t frag_size, const void* frag_code, const char* frag_enpt, tr_shader_program** p_shader_program);
tr_api_export void tr_create_shader_program_compute(tr_renderer* p_renderer, uint32_t comp_size, const void* comp_code, const char* comp_enpt, tr_shader_program** pp_shader_program);
tr_api_export void tr_destroy_shader_program(tr_renderer* p_renderer, tr_shader_program* p_shader_program);
tr_api_export void tr_create_pipeline(tr_renderer* p_renderer, tr_shader_program* p_shader_program, const tr_vertex_layout* p_vertex_layout, tr_descriptor_set* p_descriptor_set, tr_render_target* p_render_target, const tr_pipeline_settings* p_pipeline_settings, tr_pipeline** pp_pipeline);
tr_api_export void tr_create_compute_pipeline(tr_renderer* p_renderer, tr_shader_program* p_shader_program, tr_descriptor_set* p_descriptor_set, const tr_pipeline_settings* p_pipeline_settings, tr_pipeline** pp_pipeline);
tr_api_export void tr_destroy_pipeline(tr_renderer* p_renderer, tr_pipeline* p_pipeline);
tr_api_export void tr_create_render_target(tr_renderer* p_renderer, uint32_t width, uint32_t height, tr_sample_count sample_count, tr_format color_format, uint32_t color_attachment_count, const tr_clear_value* color_clear_values, tr_format depth_stencil_format, const tr_clear_value* depth_stencil_clear_value, tr_render_target** pp_render_target);
tr_api_export void tr_destroy_render_target(tr_renderer* p_renderer, tr_render_target* p_render_target);
tr_api_export void tr_update_descriptor_set(tr_renderer* p_renderer, tr_descriptor_set* p_descriptor_set);
tr_api_export void tr_begin_cmd(tr_cmd* p_cmd);
tr_api_export void tr_end_cmd(tr_cmd* p_cmd);
tr_api_export void tr_cmd_begin_render(tr_cmd* p_cmd, tr_render_target* p_render_target);
tr_api_export void tr_cmd_end_render(tr_cmd* p_cmd);
tr_api_export void tr_cmd_set_viewport(tr_cmd* p_cmd, float x, float, float width, float height, float min_depth, float max_depth);
tr_api_export void tr_cmd_set_scissor(tr_cmd* p_cmd, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
tr_api_export void tr_cmd_set_line_width(tr_cmd* p_cmd, float line_width);
tr_api_export void tr_cmd_clear_color_attachment(tr_cmd* p_cmd, uint32_t attachment_index, const tr_clear_value* clear_value);
tr_api_export void tr_cmd_clear_depth_stencil_attachment(tr_cmd* p_cmd, const tr_clear_value* clear_value);
tr_api_export void tr_cmd_bind_pipeline(tr_cmd* p_cmd, tr_pipeline* p_pipeline);
tr_api_export void tr_cmd_bind_descriptor_sets(tr_cmd* p_cmd, tr_pipeline* p_pipeline, tr_descriptor_set* p_descriptor_set);
tr_api_export void tr_cmd_bind_index_buffer(tr_cmd* p_cmd, tr_buffer* p_buffer);
tr_api_export void tr_cmd_bind_vertex_buffers(tr_cmd* p_cmd, uint32_t buffer_count, tr_buffer** pp_buffers);
tr_api_export void tr_cmd_draw(tr_cmd* p_cmd, uint32_t vertex_count, uint32_t first_vertex);
tr_api_export void tr_cmd_draw_indexed(tr_cmd* p_cmd, uint32_t index_count, uint32_t first_index);
tr_api_export void tr_cmd_draw_mesh(tr_cmd* p_cmd, const tr_mesh* p_mesh);
tr_api_export void tr_cmd_buffer_transition(tr_cmd* p_cmd, tr_buffer* p_buffer, tr_buffer_usage old_usage, tr_buffer_usage new_usage);
tr_api_export void tr_cmd_image_transition(tr_cmd* p_cmd, tr_texture* p_texture, tr_texture_usage old_usage, tr_texture_usage new_usage);
tr_api_export void tr_cmd_render_target_transition(tr_cmd* p_cmd, tr_render_target* p_render_target, tr_texture_usage old_usage, tr_texture_usage new_usage);
tr_api_export void tr_cmd_depth_stencil_transition(tr_cmd* p_cmd, tr_render_target* p_render_target, tr_texture_usage old_usage, tr_texture_usage new_usage);
tr_api_export void tr_cmd_dispatch(tr_cmd* p_cmd, uint32_t group_count_x, uint32_t group_count_y, uint32_t group_count_z);
tr_api_export void tr_cmd_copy_buffer_to_texture2d(tr_cmd* p_cmd, uint32_t width, uint32_t height, uint32_t row_pitch, uint64_t buffer_offset, uint32_t mip_level, tr_buffer* p_buffer, tr_texture* p_texture);
tr_api_export void tr_acquire_next_image(tr_renderer* p_renderer, tr_semaphore* p_signal_semaphore, tr_fence* p_fence);
tr_api_export void tr_queue_submit(tr_queue* p_queue, uint32_t cmd_count, tr_cmd** pp_cmds, uint32_t wait_semaphore_count, tr_semaphore** pp_wait_semaphores, uint32_t signal_semaphore_count, tr_semaphore** pp_signal_semaphores);
tr_api_export void tr_queue_present(tr_queue* p_queue, uint32_t wait_semaphore_count, tr_semaphore** pp_wait_semaphores);
tr_api_export void tr_queue_wait_idle(tr_queue* p_queue);
tr_api_export void tr_render_target_set_color_clear_value(tr_render_target* p_render_target, uint32_t attachment_index, float r, float g, float b, float a);
tr_api_export void tr_render_target_set_depth_stencil_clear_value(tr_render_target* p_render_target, float depth, uint8_t stencil);
tr_api_export bool tr_vertex_layout_support_format(tr_format format);
tr_api_export uint32_t tr_vertex_layout_stride(const tr_vertex_layout* p_vertex_layout);
// Utility functions
tr_api_export uint32_t tr_util_calc_mip_levels(uint32_t width, uint32_t height);
tr_api_export VkFormat tr_util_to_vk_format(tr_format format);
tr_api_export tr_format tr_util_from_vk_format(VkFormat fomat);
tr_api_export uint32_t tr_util_format_stride(tr_format format);
tr_api_export uint32_t tr_util_format_channel_count(tr_format format);
tr_api_export VkShaderStageFlags tr_util_to_vk_shader_stages(tr_shader_stage shader_stages);
tr_api_export void tr_util_transition_buffer(tr_queue* p_queue, tr_buffer* p_buffer, tr_buffer_usage old_usage, tr_buffer_usage new_usage);
tr_api_export void tr_util_transition_image(tr_queue* p_queue, tr_texture* p_texture, tr_texture_usage old_usage, tr_texture_usage new_usage);
tr_api_export void tr_util_set_storage_buffer_count(tr_queue* p_queue, uint64_t count_offset, uint32_t count, tr_buffer* p_buffer);
tr_api_export void tr_util_clear_buffer(tr_queue* p_queue, tr_buffer* p_buffer);
tr_api_export void tr_util_update_buffer(tr_queue* p_queue, uint64_t size, const void* p_src_data, tr_buffer* p_buffer);
tr_api_export void tr_util_update_texture_uint8(tr_queue* p_queue, uint32_t src_width, uint32_t src_height, uint32_t src_row_stride, const uint8_t* p_src_data, uint32_t src_channel_count, tr_texture* p_texture, tr_image_resize_uint8_fn resize_fn, void* p_user_data);
tr_api_export void tr_util_update_texture_float(tr_queue* p_queue, uint32_t src_width, uint32_t src_height, uint32_t src_row_stride, const float* p_src_data, uint32_t channels, tr_texture* p_texture, tr_image_resize_float_fn resize_fn, void* p_user_data);
// =================================================================================================
// IMPLEMENTATION
// =================================================================================================
#if defined(TINY_RENDERER_IMPLEMENTATION)
#pragma comment(lib, "vulkan-1.lib")
#define TINY_RENDERER_RENDERER_PTR_CHECK(p_renderer) \
assert(NULL != s_tr_internal); \
assert(NULL != s_tr_internal->renderer); \
assert(NULL != p_renderer); \
assert(s_tr_internal->renderer == p_renderer);
#define TINY_RENDERER_SAFE_FREE(p_var) \
if (NULL != p_var) { \
free((void*)p_var); \
p_var = NULL; \
}
#if defined(__cplusplus)
#define TINY_RENDERER_DECLARE_ZERO(type, var) \
type var = {};
#else
#define TINY_RENDERER_DECLARE_ZERO(type, var) \
type var = {0};
#endif
static inline uint32_t tr_max(uint32_t a, uint32_t b)
{
return a > b ? a : b;
}
static inline uint32_t tr_min(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
static inline uint32_t tr_round_up(uint32_t value, uint32_t multiple)
{
assert(multiple);
return ((value + multiple - 1) / multiple) * multiple;
}
// Internal utility functions (may become external one day)
VkSampleCountFlagBits tr_util_to_vk_sample_count(tr_sample_count sample_count);
VkBufferUsageFlags tr_util_to_vk_buffer_usage(tr_buffer_usage usage);
VkImageUsageFlags tr_util_to_vk_image_usage(tr_texture_usage usage);
VkImageLayout tr_util_to_vk_image_layout(tr_texture_usage usage);
VkImageAspectFlags tr_util_vk_determine_aspect_mask(VkFormat format);
bool tr_util_vk_get_memory_type(const VkPhysicalDeviceMemoryProperties* mem_props, uint32_t type_bits, VkMemoryPropertyFlags flags, uint32_t* p_index);
VkFormatFeatureFlags tr_util_vk_image_usage_to_format_features(VkImageUsageFlags usage);
// Internal init functions
void tr_internal_vk_create_instance(const char* app_name, tr_renderer* p_renderer);
void tr_internal_vk_create_surface(tr_renderer* p_renderer);
void tr_internal_vk_create_device(tr_renderer* p_renderer);
void tr_internal_vk_create_swapchain(tr_renderer* p_renderer);
void tr_internal_create_swapchain_renderpass(tr_renderer* p_renderer);
void tr_internal_vk_create_swapchain_renderpass(tr_renderer* p_renderer);
void tr_internal_vk_destroy_instance(tr_renderer* p_renderer);
void tr_internal_vk_destroy_surface(tr_renderer* p_renderer);
void tr_internal_vk_destroy_device(tr_renderer* p_renderer);
void tr_internal_vk_destroy_swapchain(tr_renderer* p_renderer);
// Internal create functions
void tr_internal_vk_create_fence(tr_renderer *p_renderer, tr_fence* p_fence);
void tr_internal_vk_destroy_fence(tr_renderer *p_renderer, tr_fence* p_fence);
void tr_internal_vk_create_semaphore(tr_renderer *p_renderer, tr_semaphore* p_semaphore);
void tr_internal_vk_destroy_semaphore(tr_renderer *p_renderer, tr_semaphore* p_semaphore);
void tr_internal_vk_create_descriptor_set(tr_renderer* p_renderer, tr_descriptor_set* p_descriptor_set);
void tr_internal_vk_destroy_descriptor_set(tr_renderer* p_renderer, tr_descriptor_set* p_descriptor_set);
void tr_internal_vk_create_cmd_pool(tr_renderer *p_renderer, tr_queue* p_queue, bool transient, tr_cmd_pool* p_cmd_pool);
void tr_internal_vk_destroy_cmd_pool(tr_renderer *p_renderer, tr_cmd_pool* p_cmd_pool);
void tr_internal_vk_create_cmd(tr_cmd_pool *p_cmd_pool, bool secondary, tr_cmd* p_cmd);
void tr_internal_vk_destroy_cmd(tr_cmd_pool *p_cmd_pool, tr_cmd* p_cmd);
void tr_internal_vk_create_buffer(tr_renderer* p_renderer, tr_buffer* p_buffer);
void tr_internal_vk_destroy_buffer(tr_renderer* p_renderer, tr_buffer* p_buffer);
void tr_internal_vk_create_texture(tr_renderer* p_renderer, tr_texture* p_texture);
void tr_internal_vk_destroy_texture(tr_renderer* p_renderer, tr_texture* p_texture);
void tr_internal_vk_create_sampler(tr_renderer* p_renderer, tr_sampler* p_sampler);
void tr_internal_vk_destroy_sampler(tr_renderer* p_renderer, tr_sampler* p_sampler);
void tr_internal_vk_create_pipeline(tr_renderer* p_renderer, tr_shader_program* p_shader_program, const tr_vertex_layout* p_vertex_layout, tr_descriptor_set* pp_descriptor_set, tr_render_target* p_render_target, const tr_pipeline_settings* p_pipeline_settings, tr_pipeline* p_pipeline);
void tr_internal_vk_create_compute_pipeline(tr_renderer* p_renderer, tr_shader_program* p_shader_program, tr_descriptor_set* p_descriptor_set, const tr_pipeline_settings* p_pipeline_settings, tr_pipeline* p_pipeline);
void tr_internal_vk_destroy_pipeline(tr_renderer* p_renderer, tr_pipeline* p_pipeline);
void tr_internal_vk_create_shader_program(tr_renderer* p_renderer, uint32_t vert_size, const void* vert_code, const char* vert_enpt, uint32_t tesc_size, const void* tesc_code, const char* tesc_enpt, uint32_t tese_size, const void* tese_code, const char* tese_enpt, uint32_t geom_size, const void* geom_code, const char* geom_enpt, uint32_t frag_size, const void* frag_code, const char* frag_enpt, uint32_t comp_size, const void* comp_code, const char* comp_enpt, tr_shader_program* p_shader_program);
void tr_internal_vk_destroy_shader_program(tr_renderer* p_renderer, tr_shader_program* p_shader_program);
void tr_internal_vk_create_render_target(tr_renderer* p_renderer, bool is_swapchain, tr_render_target* p_render_target);
void tr_internal_vk_destroy_render_target(tr_renderer* p_renderer, tr_render_target* p_render_target);
// Internal descriptor set functions
void tr_internal_vk_update_descriptor_set(tr_renderer* p_renderer, tr_descriptor_set* p_descriptor_set);
// Internal command buffer functions
void tr_internal_vk_begin_cmd(tr_cmd* p_cmd);
void tr_internal_vk_end_cmd(tr_cmd* p_cmd);
void tr_internal_vk_cmd_begin_render(tr_cmd* p_cmd, tr_render_target* p_render_target);
void tr_internal_vk_cmd_end_render(tr_cmd* p_cmd);
void tr_internal_vk_cmd_set_viewport(tr_cmd* p_cmd, float x, float, float width, float height, float min_depth, float max_depth);
void tr_internal_vk_cmd_set_scissor(tr_cmd* p_cmd, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
void tr_internal_vk_cmd_set_line_width(tr_cmd* p_cmd, float line_width);
void tr_cmd_internal_vk_cmd_clear_color_attachment(tr_cmd* p_cmd, uint32_t attachment_index, const tr_clear_value* clear_value);
void tr_cmd_internal_vk_cmd_clear_depth_stencil_attachment(tr_cmd* p_cmd, const tr_clear_value* clear_value);
void tr_internal_vk_cmd_bind_pipeline(tr_cmd* p_cmd, tr_pipeline* p_pipeline);
void tr_internal_vk_cmd_bind_descriptor_sets(tr_cmd* p_cmd, tr_pipeline* p_pipeline, tr_descriptor_set* p_descriptor_set);
void tr_internal_vk_cmd_bind_index_buffer(tr_cmd* p_cmd, tr_buffer* p_buffer);
void tr_internal_vk_cmd_bind_vertex_buffers(tr_cmd* p_cmd, uint32_t buffer_count, tr_buffer** pp_buffers);
void tr_internal_vk_cmd_draw(tr_cmd* p_cmd, uint32_t vertex_count, uint32_t first_vertex);
void tr_internal_vk_cmd_draw_indexed(tr_cmd* p_cmd, uint32_t index_count, uint32_t first_index);
void tr_internal_vk_cmd_draw_mesh(tr_cmd* p_cmd, const tr_mesh* p_mesh);
void tr_internal_vk_cmd_buffer_transition(tr_cmd* p_cmd, tr_buffer* p_buffer, tr_buffer_usage old_usage, tr_buffer_usage new_usage);
void tr_internal_vk_cmd_image_transition(tr_cmd* p_cmd, tr_texture* p_texture, tr_texture_usage old_usage, tr_texture_usage new_usage);
void tr_internal_vk_cmd_render_target_transition(tr_cmd* p_cmd, tr_render_target* p_render_target, tr_texture_usage old_usage, tr_texture_usage new_usage);
void tr_internal_vk_cmd_dispatch(tr_cmd* p_cmd, uint32_t group_count_x, uint32_t group_count_y, uint32_t group_count_z);
void tr_internal_vk_cmd_copy_buffer_to_texture2d(tr_cmd* p_cmd, uint32_t width, uint32_t height, uint32_t row_pitch, uint64_t buffer_offset, uint32_t mip_level, tr_buffer* p_buffer, tr_texture* p_texture);
// Internal queue/swapchain functions
void tr_internal_vk_acquire_next_image(tr_renderer* p_renderer, tr_semaphore* p_signal_semaphore, tr_fence* p_fence);
void tr_internal_vk_queue_submit(tr_queue* p_queue, uint32_t cmd_count, tr_cmd** pp_cmds, uint32_t wait_semaphore_count, tr_semaphore** pp_wait_semaphores, uint32_t signal_semaphore_count, tr_semaphore** pp_signal_semaphores);
void tr_internal_vk_queue_present(tr_queue* p_queue, uint32_t wait_semaphore_count, tr_semaphore** pp_wait_semaphores);
void tr_internal_vk_queue_wait_idle(tr_queue* p_queue);
// -------------------------------------------------------------------------------------------------
// ptr_vector (begin)
// -------------------------------------------------------------------------------------------------
typedef void(*pfn_ptr_vector_destroy_elem)(void*);
typedef struct ptr_vector {
size_t _size;
size_t _capacity;
void** _data;
pfn_ptr_vector_destroy_elem _destroy_elem_fn;
} ptr_vector;
bool ptr_vector_create(ptr_vector** pp_vector, pfn_ptr_vector_destroy_elem pfn_destroy_elem)
{
if (NULL == pfn_destroy_elem) {
return false;
}
ptr_vector* p_vector = (ptr_vector*)calloc(1, sizeof(*p_vector));
if (NULL == p_vector) {
return false;
}
p_vector->_size = 0;
p_vector->_capacity = 0;
p_vector->_destroy_elem_fn = pfn_destroy_elem;
*pp_vector = p_vector;
return true;
}
bool ptr_vector_resize(ptr_vector* p_vector, size_t n)
{
if (NULL == p_vector) {
return false;
}
size_t new_capacity = ((7 * n) / 4) + (n > 0 ? 1 : 0);
void** new_data = NULL;
if ((new_capacity != p_vector->_capacity) && (new_capacity > 0)) {
new_data = (void**)calloc(new_capacity, sizeof(*new_data));
if (NULL == new_data) {
return false;
}
}
if (NULL != new_data) {
if (p_vector->_size > 0) {
if (NULL == p_vector->_data) {
return false;
}
void* ret = memcpy(new_data,
p_vector->_data, p_vector->_size * sizeof(*(p_vector->_data)));
if (ret != new_data) {
return false;
}
}
if (n < p_vector->_size) {
pfn_ptr_vector_destroy_elem destroy_elem_fn = p_vector->_destroy_elem_fn;
for (size_t i = n; i < p_vector->_size; ++i) {
if (NULL != destroy_elem_fn) {
destroy_elem_fn(p_vector->_data[i]);
}
p_vector->_data[i] = NULL;
}
}
if (NULL != p_vector->_data) {
free(p_vector->_data);
}
p_vector->_size = n;
p_vector->_capacity = new_capacity;
p_vector->_data = new_data;
}
return true;
}
bool ptr_vector_push_back(ptr_vector* p_vector, void* p)
{
if ((NULL == p_vector) || (NULL == p)) {
return false;
}
size_t new_size = p_vector->_size + 1;
if (new_size >= p_vector->_capacity) {
bool ret = ptr_vector_resize(p_vector, new_size);
if (! ret) {
return false;
}
}
p_vector->_size = new_size;
p_vector->_data[new_size - 1] = p;
return true;
}
bool ptr_vector_erase(ptr_vector* p_vector, void* p)
{
if ((NULL == p_vector) || (NULL == p)) {
return false;
}
size_t n = (size_t)-1;
for (size_t i = 0; i < p_vector->_size; ++i) {
if (p == p_vector->_data[i]) {
n = i;
break;
}
}
if ((((size_t)-1) != n) && (NULL != p_vector->_data)) {
if (n > 0) {
for (size_t i = 0; i < (n - 1); ++i) {
p_vector->_data[i] = p_vector->_data[i + 1];
}
}
if (n < (p_vector->_size - 1)) {
for (size_t i = n; i < p_vector->_size; ++i) {
p_vector->_data[i] = p_vector->_data[i + 1];
}
}
for (size_t i = (p_vector->_size - 1); i < p_vector->_capacity; ++i) {
p_vector->_data[i] = NULL;
}
pfn_ptr_vector_destroy_elem destroy_elem_fn = p_vector->_destroy_elem_fn;
if (NULL != destroy_elem_fn) {
destroy_elem_fn(p);
}
p_vector->_size -= 1;
if ((p_vector->_size < (p_vector->_capacity)) && (p_vector->_capacity > 10)) {
bool ret = ptr_vector_resize(p_vector, p_vector->_size);
if (! ret) {
return false;
}
}
}
return true;
}
bool ptr_vector_remove(ptr_vector* p_vector, void* p)
{
if ((NULL == p_vector) || (NULL == p)) {
return false;
}
size_t n = (size_t)-1;
for (size_t i = 0; i < p_vector->_size; ++i) {
if (p == p_vector->_data[i]) {
n = i;
break;
}
}
if ((((size_t)-1) != n) && (NULL != p_vector->_data)) {
if (n > 0) {
for (size_t i = 0; i < (n - 1); ++i) {
p_vector->_data[i] = p_vector->_data[i + 1];
}
}
if (n < (p_vector->_size - 1)) {
for (size_t i = n; i < p_vector->_size; ++i) {
p_vector->_data[i] = p_vector->_data[i + 1];
}
}
for (size_t i = (p_vector->_size - 1); i < p_vector->_capacity; ++i) {
p_vector->_data[i] = NULL;
}
p_vector->_size -= 1;
if ((p_vector->_size < (p_vector->_capacity)) && (p_vector->_capacity > 10)) {
bool ret = ptr_vector_resize(p_vector, p_vector->_size);