-
-
Notifications
You must be signed in to change notification settings - Fork 22k
/
Copy pathrendering_device.h
1437 lines (1211 loc) · 56.5 KB
/
rendering_device.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
/**************************************************************************/
/* rendering_device.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef RENDERING_DEVICE_H
#define RENDERING_DEVICE_H
#include "core/object/class_db.h"
#include "core/object/worker_thread_pool.h"
#include "core/os/thread_safe.h"
#include "core/templates/local_vector.h"
#include "core/templates/oa_hash_map.h"
#include "core/templates/rid_owner.h"
#include "core/variant/typed_array.h"
#include "servers/display_server.h"
#include "servers/rendering/rendering_device_commons.h"
#include "servers/rendering/rendering_device_driver.h"
#include "servers/rendering/rendering_device_graph.h"
class RDTextureFormat;
class RDTextureView;
class RDAttachmentFormat;
class RDSamplerState;
class RDVertexAttribute;
class RDShaderSource;
class RDShaderSPIRV;
class RDUniform;
class RDPipelineRasterizationState;
class RDPipelineMultisampleState;
class RDPipelineDepthStencilState;
class RDPipelineColorBlendState;
class RDFramebufferPass;
class RDPipelineSpecializationConstant;
class RenderingDevice : public RenderingDeviceCommons {
GDCLASS(RenderingDevice, Object)
_THREAD_SAFE_CLASS_
public:
enum ShaderLanguage {
SHADER_LANGUAGE_GLSL,
SHADER_LANGUAGE_HLSL
};
typedef int64_t DrawListID;
typedef int64_t ComputeListID;
typedef String (*ShaderSPIRVGetCacheKeyFunction)(const RenderingDevice *p_render_device);
typedef Vector<uint8_t> (*ShaderCompileToSPIRVFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const RenderingDevice *p_render_device);
typedef Vector<uint8_t> (*ShaderCacheFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language);
typedef void (*InvalidationCallback)(void *);
private:
static ShaderCompileToSPIRVFunction compile_to_spirv_function;
static ShaderCacheFunction cache_function;
static ShaderSPIRVGetCacheKeyFunction get_spirv_cache_key_function;
static RenderingDevice *singleton;
RenderingContextDriver *context = nullptr;
RenderingDeviceDriver *driver = nullptr;
RenderingContextDriver::Device device;
protected:
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
RID _shader_create_from_bytecode_bind_compat_79606(const Vector<uint8_t> &p_shader_binary);
static void _bind_compatibility_methods();
#endif
/***************************/
/**** ID INFRASTRUCTURE ****/
/***************************/
public:
//base numeric ID for all types
enum {
INVALID_FORMAT_ID = -1
};
enum IDType {
ID_TYPE_FRAMEBUFFER_FORMAT,
ID_TYPE_VERTEX_FORMAT,
ID_TYPE_DRAW_LIST,
ID_TYPE_COMPUTE_LIST = 4,
ID_TYPE_MAX,
ID_BASE_SHIFT = 58, // 5 bits for ID types.
ID_MASK = (ID_BASE_SHIFT - 1),
};
private:
HashMap<RID, HashSet<RID>> dependency_map; // IDs to IDs that depend on it.
HashMap<RID, HashSet<RID>> reverse_dependency_map; // Same as above, but in reverse.
void _add_dependency(RID p_id, RID p_depends_on);
void _free_dependencies(RID p_id);
private:
/***************************/
/**** BUFFER MANAGEMENT ****/
/***************************/
// These are temporary buffers on CPU memory that hold
// the information until the CPU fetches it and places it
// either on GPU buffers, or images (textures). It ensures
// updates are properly synchronized with whatever the
// GPU is doing.
//
// The logic here is as follows, only 3 of these
// blocks are created at the beginning (one per frame)
// they can each belong to a frame (assigned to current when
// used) and they can only be reused after the same frame is
// recycled.
//
// When CPU requires to allocate more than what is available,
// more of these buffers are created. If a limit is reached,
// then a fence will ensure will wait for blocks allocated
// in previous frames are processed. If that fails, then
// another fence will ensure everything pending for the current
// frame is processed (effectively stalling).
//
// See the comments in the code to understand better how it works.
struct StagingBufferBlock {
RDD::BufferID driver_id;
uint64_t frame_used = 0;
uint32_t fill_amount = 0;
};
Vector<StagingBufferBlock> staging_buffer_blocks;
int staging_buffer_current = 0;
uint32_t staging_buffer_block_size = 0;
uint64_t staging_buffer_max_size = 0;
bool staging_buffer_used = false;
enum StagingRequiredAction {
STAGING_REQUIRED_ACTION_NONE,
STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL,
STAGING_REQUIRED_ACTION_STALL_PREVIOUS
};
Error _staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, StagingRequiredAction &r_required_action, bool p_can_segment = true);
void _staging_buffer_execute_required_action(StagingRequiredAction p_required_action);
Error _insert_staging_block();
struct Buffer {
RDD::BufferID driver_id;
uint32_t size = 0;
BitField<RDD::BufferUsageBits> usage;
RDG::ResourceTracker *draw_tracker = nullptr;
};
Buffer *_get_buffer_from_owner(RID p_buffer);
Error _buffer_update(Buffer *p_buffer, RID p_buffer_id, size_t p_offset, const uint8_t *p_data, size_t p_data_size, bool p_use_draw_queue = false, uint32_t p_required_align = 32);
RID_Owner<Buffer> uniform_buffer_owner;
RID_Owner<Buffer> storage_buffer_owner;
RID_Owner<Buffer> texture_buffer_owner;
public:
Error buffer_copy(RID p_src_buffer, RID p_dst_buffer, uint32_t p_src_offset, uint32_t p_dst_offset, uint32_t p_size);
Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data);
Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size);
Vector<uint8_t> buffer_get_data(RID p_buffer, uint32_t p_offset = 0, uint32_t p_size = 0); // This causes stall, only use to retrieve large buffers for saving.
/*****************/
/**** TEXTURE ****/
/*****************/
// In modern APIs, the concept of textures may not exist;
// instead there is the image (the memory pretty much,
// the view (how the memory is interpreted) and the
// sampler (how it's sampled from the shader).
//
// Texture here includes the first two stages, but
// It's possible to create textures sharing the image
// but with different views. The main use case for this
// is textures that can be read as both SRGB/Linear,
// or slices of a texture (a mipmap, a layer, a 3D slice)
// for a framebuffer to render into it.
struct Texture {
RDD::TextureID driver_id;
TextureType type = TEXTURE_TYPE_MAX;
DataFormat format = DATA_FORMAT_MAX;
TextureSamples samples = TEXTURE_SAMPLES_MAX;
TextureSliceType slice_type = TEXTURE_SLICE_MAX;
Rect2i slice_rect;
uint32_t width = 0;
uint32_t height = 0;
uint32_t depth = 0;
uint32_t layers = 0;
uint32_t mipmaps = 0;
uint32_t usage_flags = 0;
uint32_t base_mipmap = 0;
uint32_t base_layer = 0;
Vector<DataFormat> allowed_shared_formats;
bool is_resolve_buffer = false;
bool has_initial_data = false;
BitField<RDD::TextureAspectBits> read_aspect_flags;
BitField<RDD::TextureAspectBits> barrier_aspect_flags;
bool bound = false; // Bound to framebuffer.
RID owner;
RDG::ResourceTracker *draw_tracker = nullptr;
HashMap<Rect2i, RDG::ResourceTracker *> slice_trackers;
RDD::TextureSubresourceRange barrier_range() const {
RDD::TextureSubresourceRange r;
r.aspect = barrier_aspect_flags;
r.base_mipmap = base_mipmap;
r.mipmap_count = mipmaps;
r.base_layer = base_layer;
r.layer_count = layers;
return r;
}
};
RID_Owner<Texture> texture_owner;
uint32_t texture_upload_region_size_px = 0;
Vector<uint8_t> _texture_get_data(Texture *tex, uint32_t p_layer, bool p_2d = false);
Error _texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, bool p_use_setup_queue, bool p_validate_can_update);
public:
struct TextureView {
DataFormat format_override = DATA_FORMAT_MAX; // // Means, use same as format.
TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;
TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;
TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;
TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;
bool operator==(const TextureView &p_other) const {
if (format_override != p_other.format_override) {
return false;
} else if (swizzle_r != p_other.swizzle_r) {
return false;
} else if (swizzle_g != p_other.swizzle_g) {
return false;
} else if (swizzle_b != p_other.swizzle_b) {
return false;
} else if (swizzle_a != p_other.swizzle_a) {
return false;
} else {
return true;
}
}
};
RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
RID texture_create_from_extension(TextureType p_type, DataFormat p_format, TextureSamples p_samples, BitField<RenderingDevice::TextureUsageBits> p_usage, uint64_t p_image, uint64_t p_width, uint64_t p_height, uint64_t p_depth, uint64_t p_layers);
RID texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps = 1, TextureSliceType p_slice_type = TEXTURE_SLICE_2D, uint32_t p_layers = 0);
Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data);
Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer); // CPU textures will return immediately, while GPU textures will most likely force a flush
bool texture_is_format_supported_for_usage(DataFormat p_format, BitField<TextureUsageBits> p_usage) const;
bool texture_is_shared(RID p_texture);
bool texture_is_valid(RID p_texture);
TextureFormat texture_get_format(RID p_texture);
Size2i texture_size(RID p_texture);
#ifndef DISABLE_DEPRECATED
uint64_t texture_get_native_handle(RID p_texture);
#endif
Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer);
Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers);
Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture);
/************************/
/**** DRAW LISTS (I) ****/
/************************/
enum InitialAction {
INITIAL_ACTION_LOAD,
INITIAL_ACTION_CLEAR,
INITIAL_ACTION_DISCARD,
INITIAL_ACTION_MAX,
#ifndef DISABLE_DEPRECATED
INITIAL_ACTION_CLEAR_REGION = INITIAL_ACTION_CLEAR,
INITIAL_ACTION_CLEAR_REGION_CONTINUE = INITIAL_ACTION_CLEAR,
INITIAL_ACTION_KEEP = INITIAL_ACTION_LOAD,
INITIAL_ACTION_DROP = INITIAL_ACTION_DISCARD,
INITIAL_ACTION_CONTINUE = INITIAL_ACTION_LOAD,
#endif
};
enum FinalAction {
FINAL_ACTION_STORE,
FINAL_ACTION_DISCARD,
FINAL_ACTION_MAX,
#ifndef DISABLE_DEPRECATED
FINAL_ACTION_READ = FINAL_ACTION_STORE,
FINAL_ACTION_CONTINUE = FINAL_ACTION_STORE,
#endif
};
/*********************/
/**** FRAMEBUFFER ****/
/*********************/
// In modern APIs, generally, framebuffers work similar to how they
// do in OpenGL, with the exception that
// the "format" (RDD::RenderPassID) is not dynamic
// and must be more or less the same as the one
// used for the render pipelines.
struct AttachmentFormat {
enum { UNUSED_ATTACHMENT = 0xFFFFFFFF };
DataFormat format;
TextureSamples samples;
uint32_t usage_flags;
AttachmentFormat() {
format = DATA_FORMAT_R8G8B8A8_UNORM;
samples = TEXTURE_SAMPLES_1;
usage_flags = 0;
}
};
struct FramebufferPass {
Vector<int32_t> color_attachments;
Vector<int32_t> input_attachments;
Vector<int32_t> resolve_attachments;
Vector<int32_t> preserve_attachments;
int32_t depth_attachment = ATTACHMENT_UNUSED;
int32_t vrs_attachment = ATTACHMENT_UNUSED; // density map for VRS, only used if supported
};
typedef int64_t FramebufferFormatID;
private:
struct FramebufferFormatKey {
Vector<AttachmentFormat> attachments;
Vector<FramebufferPass> passes;
uint32_t view_count = 1;
bool operator<(const FramebufferFormatKey &p_key) const {
if (view_count != p_key.view_count) {
return view_count < p_key.view_count;
}
uint32_t pass_size = passes.size();
uint32_t key_pass_size = p_key.passes.size();
if (pass_size != key_pass_size) {
return pass_size < key_pass_size;
}
const FramebufferPass *pass_ptr = passes.ptr();
const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
for (uint32_t i = 0; i < pass_size; i++) {
{ // Compare color attachments.
uint32_t attachment_size = pass_ptr[i].color_attachments.size();
uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
if (attachment_size != key_attachment_size) {
return attachment_size < key_attachment_size;
}
const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
for (uint32_t j = 0; j < attachment_size; j++) {
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
}
}
}
{ // Compare input attachments.
uint32_t attachment_size = pass_ptr[i].input_attachments.size();
uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
if (attachment_size != key_attachment_size) {
return attachment_size < key_attachment_size;
}
const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
for (uint32_t j = 0; j < attachment_size; j++) {
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
}
}
}
{ // Compare resolve attachments.
uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
if (attachment_size != key_attachment_size) {
return attachment_size < key_attachment_size;
}
const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
for (uint32_t j = 0; j < attachment_size; j++) {
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
}
}
}
{ // Compare preserve attachments.
uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
if (attachment_size != key_attachment_size) {
return attachment_size < key_attachment_size;
}
const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
for (uint32_t j = 0; j < attachment_size; j++) {
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
}
}
}
if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
}
}
int as = attachments.size();
int bs = p_key.attachments.size();
if (as != bs) {
return as < bs;
}
const AttachmentFormat *af_a = attachments.ptr();
const AttachmentFormat *af_b = p_key.attachments.ptr();
for (int i = 0; i < as; i++) {
const AttachmentFormat &a = af_a[i];
const AttachmentFormat &b = af_b[i];
if (a.format != b.format) {
return a.format < b.format;
}
if (a.samples != b.samples) {
return a.samples < b.samples;
}
if (a.usage_flags != b.usage_flags) {
return a.usage_flags < b.usage_flags;
}
}
return false; // Equal.
}
};
RDD::RenderPassID _render_pass_create(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count = 1, Vector<TextureSamples> *r_samples = nullptr);
// This is a cache and it's never freed, it ensures
// IDs for a given format are always unique.
RBMap<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
struct FramebufferFormat {
const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E;
RDD::RenderPassID render_pass; // Here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec).
Vector<TextureSamples> pass_samples;
uint32_t view_count = 1; // Number of views.
};
HashMap<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
struct Framebuffer {
FramebufferFormatID format_id;
struct VersionKey {
InitialAction initial_color_action;
FinalAction final_color_action;
InitialAction initial_depth_action;
FinalAction final_depth_action;
uint32_t view_count;
bool operator<(const VersionKey &p_key) const {
if (initial_color_action == p_key.initial_color_action) {
if (final_color_action == p_key.final_color_action) {
if (initial_depth_action == p_key.initial_depth_action) {
if (final_depth_action == p_key.final_depth_action) {
return view_count < p_key.view_count;
} else {
return final_depth_action < p_key.final_depth_action;
}
} else {
return initial_depth_action < p_key.initial_depth_action;
}
} else {
return final_color_action < p_key.final_color_action;
}
} else {
return initial_color_action < p_key.initial_color_action;
}
}
};
uint32_t storage_mask = 0;
Vector<RID> texture_ids;
InvalidationCallback invalidated_callback = nullptr;
void *invalidated_callback_userdata = nullptr;
struct Version {
RDD::FramebufferID framebuffer;
RDD::RenderPassID render_pass; // This one is owned.
uint32_t subpass_count = 1;
};
RBMap<VersionKey, Version> framebuffers;
Size2 size;
uint32_t view_count;
};
RID_Owner<Framebuffer> framebuffer_owner;
public:
// This ID is warranted to be unique for the same formats, does not need to be freed
FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
bool framebuffer_is_valid(RID p_framebuffer) const;
void framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata);
FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
/*****************/
/**** SAMPLER ****/
/*****************/
private:
RID_Owner<RDD::SamplerID> sampler_owner;
public:
RID sampler_create(const SamplerState &p_state);
bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_sampler_filter) const;
/**********************/
/**** VERTEX ARRAY ****/
/**********************/
typedef int64_t VertexFormatID;
private:
// Vertex buffers in Vulkan are similar to how
// they work in OpenGL, except that instead of
// an attribute index, there is a buffer binding
// index (for binding the buffers in real-time)
// and a location index (what is used in the shader).
//
// This mapping is done here internally, and it's not
// exposed.
RID_Owner<Buffer> vertex_buffer_owner;
struct VertexDescriptionKey {
Vector<VertexAttribute> vertex_formats;
bool operator==(const VertexDescriptionKey &p_key) const {
int vdc = vertex_formats.size();
int vdck = p_key.vertex_formats.size();
if (vdc != vdck) {
return false;
} else {
const VertexAttribute *a_ptr = vertex_formats.ptr();
const VertexAttribute *b_ptr = p_key.vertex_formats.ptr();
for (int i = 0; i < vdc; i++) {
const VertexAttribute &a = a_ptr[i];
const VertexAttribute &b = b_ptr[i];
if (a.location != b.location) {
return false;
}
if (a.offset != b.offset) {
return false;
}
if (a.format != b.format) {
return false;
}
if (a.stride != b.stride) {
return false;
}
if (a.frequency != b.frequency) {
return false;
}
}
return true; // They are equal.
}
}
uint32_t hash() const {
int vdc = vertex_formats.size();
uint32_t h = hash_murmur3_one_32(vdc);
const VertexAttribute *ptr = vertex_formats.ptr();
for (int i = 0; i < vdc; i++) {
const VertexAttribute &vd = ptr[i];
h = hash_murmur3_one_32(vd.location, h);
h = hash_murmur3_one_32(vd.offset, h);
h = hash_murmur3_one_32(vd.format, h);
h = hash_murmur3_one_32(vd.stride, h);
h = hash_murmur3_one_32(vd.frequency, h);
}
return hash_fmix32(h);
}
};
struct VertexDescriptionHash {
static _FORCE_INLINE_ uint32_t hash(const VertexDescriptionKey &p_key) {
return p_key.hash();
}
};
// This is a cache and it's never freed, it ensures that
// ID used for a specific format always remain the same.
HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache;
struct VertexDescriptionCache {
Vector<VertexAttribute> vertex_formats;
RDD::VertexFormatID driver_id;
};
HashMap<VertexFormatID, VertexDescriptionCache> vertex_formats;
struct VertexArray {
RID buffer;
VertexFormatID description;
int vertex_count = 0;
uint32_t max_instances_allowed = 0;
Vector<RDD::BufferID> buffers; // Not owned, just referenced.
Vector<RDG::ResourceTracker *> draw_trackers; // Not owned, just referenced.
Vector<uint64_t> offsets;
HashSet<RID> untracked_buffers;
};
RID_Owner<VertexArray> vertex_array_owner;
struct IndexBuffer : public Buffer {
uint32_t max_index = 0; // Used for validation.
uint32_t index_count = 0;
IndexBufferFormat format = INDEX_BUFFER_FORMAT_UINT16;
bool supports_restart_indices = false;
};
RID_Owner<IndexBuffer> index_buffer_owner;
struct IndexArray {
uint32_t max_index = 0; // Remember the maximum index here too, for validation.
RDD::BufferID driver_id; // Not owned, inherited from index buffer.
RDG::ResourceTracker *draw_tracker = nullptr; // Not owned, inherited from index buffer.
uint32_t offset = 0;
uint32_t indices = 0;
IndexBufferFormat format = INDEX_BUFFER_FORMAT_UINT16;
bool supports_restart_indices = false;
};
RID_Owner<IndexArray> index_array_owner;
public:
RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_as_storage = false);
// This ID is warranted to be unique for the same formats, does not need to be freed
VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_descriptions);
RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers, const Vector<uint64_t> &p_offsets = Vector<uint64_t>());
RID index_buffer_create(uint32_t p_size_indices, IndexBufferFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_restart_indices = false);
RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
/****************/
/**** SHADER ****/
/****************/
// Some APIs (e.g., Vulkan) specifies a really complex behavior for the application
// in order to tell when descriptor sets need to be re-bound (or not).
// "When binding a descriptor set (see Descriptor Set Binding) to set
// number N, if the previously bound descriptor sets for sets zero
// through N-1 were all bound using compatible pipeline layouts,
// then performing this binding does not disturb any of the lower numbered sets.
// If, additionally, the previous bound descriptor set for set N was
// bound using a pipeline layout compatible for set N, then the bindings
// in sets numbered greater than N are also not disturbed."
// As a result, we need to figure out quickly when something is no longer "compatible".
// in order to avoid costly rebinds.
private:
struct UniformSetFormat {
Vector<ShaderUniform> uniforms;
_FORCE_INLINE_ bool operator<(const UniformSetFormat &p_other) const {
if (uniforms.size() != p_other.uniforms.size()) {
return uniforms.size() < p_other.uniforms.size();
}
for (int i = 0; i < uniforms.size(); i++) {
if (uniforms[i] < p_other.uniforms[i]) {
return true;
} else if (p_other.uniforms[i] < uniforms[i]) {
return false;
}
}
return false;
}
};
// Always grows, never shrinks, ensuring unique IDs, but we assume
// the amount of formats will never be a problem, as the amount of shaders
// in a game is limited.
RBMap<UniformSetFormat, uint32_t> uniform_set_format_cache;
// Shaders in Vulkan are just pretty much
// precompiled blocks of SPIR-V bytecode. They
// are most likely not really compiled to host
// assembly until a pipeline is created.
//
// When supplying the shaders, this implementation
// will use the reflection abilities of glslang to
// understand and cache everything required to
// create and use the descriptor sets (Vulkan's
// biggest pain).
//
// Additionally, hashes are created for every set
// to do quick validation and ensuring the user
// does not submit something invalid.
struct Shader : public ShaderDescription {
String name; // Used for debug.
RDD::ShaderID driver_id;
uint32_t layout_hash = 0;
BitField<RDD::PipelineStageBits> stage_bits;
Vector<uint32_t> set_formats;
};
String _shader_uniform_debug(RID p_shader, int p_set = -1);
RID_Owner<Shader> shader_owner;
#ifndef DISABLE_DEPRECATED
public:
enum BarrierMask{
BARRIER_MASK_VERTEX = 1,
BARRIER_MASK_FRAGMENT = 8,
BARRIER_MASK_COMPUTE = 2,
BARRIER_MASK_TRANSFER = 4,
BARRIER_MASK_RASTER = BARRIER_MASK_VERTEX | BARRIER_MASK_FRAGMENT, // 9,
BARRIER_MASK_ALL_BARRIERS = 0x7FFF, // all flags set
BARRIER_MASK_NO_BARRIER = 0x8000,
};
void barrier(BitField<BarrierMask> p_from = BARRIER_MASK_ALL_BARRIERS, BitField<BarrierMask> p_to = BARRIER_MASK_ALL_BARRIERS);
void full_barrier();
void draw_command_insert_label(String p_label_name, const Color &p_color = Color(1, 1, 1, 1));
Error draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>());
Error draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids);
Vector<int64_t> _draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const TypedArray<RID> &p_storage_textures = TypedArray<RID>());
Vector<int64_t> _draw_list_switch_to_next_pass_split(uint32_t p_splits);
private:
void _draw_list_end_bind_compat_81356(BitField<BarrierMask> p_post_barrier);
void _compute_list_end_bind_compat_81356(BitField<BarrierMask> p_post_barrier);
void _barrier_bind_compat_81356(BitField<BarrierMask> p_from, BitField<BarrierMask> p_to);
void _draw_list_end_bind_compat_84976(BitField<BarrierMask> p_post_barrier);
void _compute_list_end_bind_compat_84976(BitField<BarrierMask> p_post_barrier);
InitialAction _convert_initial_action_84976(InitialAction p_old_initial_action);
FinalAction _convert_final_action_84976(FinalAction p_old_final_action);
DrawListID _draw_list_begin_bind_compat_84976(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const TypedArray<RID> &p_storage_textures);
ComputeListID _compute_list_begin_bind_compat_84976(bool p_allow_draw_overlap);
Error _buffer_update_bind_compat_84976(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier);
Error _buffer_clear_bind_compat_84976(RID p_buffer, uint32_t p_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier);
Error _texture_update_bind_compat_84976(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier);
Error _texture_copy_bind_compat_84976(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, BitField<BarrierMask> p_post_barrier);
Error _texture_clear_bind_compat_84976(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, BitField<BarrierMask> p_post_barrier);
Error _texture_resolve_multisample_bind_compat_84976(RID p_from_texture, RID p_to_texture, BitField<BarrierMask> p_post_barrier);
FramebufferFormatID _screen_get_framebuffer_format_bind_compat_87340() const;
#endif
public:
const RDD::Capabilities &get_device_capabilities() const { return driver->get_capabilities(); }
bool has_feature(const Features p_feature) const;
Vector<uint8_t> shader_compile_spirv_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language = SHADER_LANGUAGE_GLSL, String *r_error = nullptr, bool p_allow_cache = true);
String shader_get_spirv_cache_key() const;
static void shader_set_compile_to_spirv_function(ShaderCompileToSPIRVFunction p_function);
static void shader_set_spirv_cache_function(ShaderCacheFunction p_function);
static void shader_set_get_cache_key_function(ShaderSPIRVGetCacheKeyFunction p_function);
String shader_get_binary_cache_key() const;
Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
RID shader_create_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, RID p_placeholder = RID());
RID shader_create_placeholder();
uint64_t shader_get_vertex_input_attribute_mask(RID p_shader);
/******************/
/**** UNIFORMS ****/
/******************/
enum StorageBufferUsage {
STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT = 1,
};
RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
RID storage_buffer_create(uint32_t p_size, const Vector<uint8_t> &p_data = Vector<uint8_t>(), BitField<StorageBufferUsage> p_usage = 0);
RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
struct Uniform {
UniformType uniform_type = UNIFORM_TYPE_IMAGE;
uint32_t binding = 0; // Binding index as specified in shader.
private:
// In most cases only one ID is provided per binding, so avoid allocating memory unnecessarily for performance.
RID id; // If only one is provided, this is used.
Vector<RID> ids; // If multiple ones are provided, this is used instead.
public:
_FORCE_INLINE_ uint32_t get_id_count() const {
return (id.is_valid() ? 1 : ids.size());
}
_FORCE_INLINE_ RID get_id(uint32_t p_idx) const {
if (id.is_valid()) {
ERR_FAIL_COND_V(p_idx != 0, RID());
return id;
} else {
return ids[p_idx];
}
}
_FORCE_INLINE_ void set_id(uint32_t p_idx, RID p_id) {
if (id.is_valid()) {
ERR_FAIL_COND(p_idx != 0);
id = p_id;
} else {
ids.write[p_idx] = p_id;
}
}
_FORCE_INLINE_ void append_id(RID p_id) {
if (ids.is_empty()) {
if (id == RID()) {
id = p_id;
} else {
ids.push_back(id);
ids.push_back(p_id);
id = RID();
}
} else {
ids.push_back(p_id);
}
}
_FORCE_INLINE_ void clear_ids() {
id = RID();
ids.clear();
}
_FORCE_INLINE_ Uniform(UniformType p_type, int p_binding, RID p_id) {
uniform_type = p_type;
binding = p_binding;
id = p_id;
}
_FORCE_INLINE_ Uniform(UniformType p_type, int p_binding, const Vector<RID> &p_ids) {
uniform_type = p_type;
binding = p_binding;
ids = p_ids;
}
_FORCE_INLINE_ Uniform() = default;
};
private:
static const uint32_t MAX_UNIFORM_SETS = 16;
static const uint32_t MAX_PUSH_CONSTANT_SIZE = 128;
// This structure contains the descriptor set. They _need_ to be allocated
// for a shader (and will be erased when this shader is erased), but should
// work for other shaders as long as the hash matches. This covers using
// them in shader variants.
//
// Keep also in mind that you can share buffers between descriptor sets, so
// the above restriction is not too serious.
struct UniformSet {
uint32_t format = 0;
RID shader_id;
uint32_t shader_set = 0;
RDD::UniformSetID driver_id;
struct AttachableTexture {
uint32_t bind = 0;
RID texture;
};
LocalVector<AttachableTexture> attachable_textures; // Used for validation.
Vector<RDG::ResourceTracker *> draw_trackers;
Vector<RDG::ResourceUsage> draw_trackers_usage;
HashMap<RID, RDG::ResourceUsage> untracked_usage;
InvalidationCallback invalidated_callback = nullptr;
void *invalidated_callback_userdata = nullptr;
};
RID_Owner<UniformSet> uniform_set_owner;
public:
RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
bool uniform_set_is_valid(RID p_uniform_set);
void uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata);
/*******************/
/**** PIPELINES ****/
/*******************/
// Render pipeline contains ALL the
// information required for drawing.
// This includes all the rasterizer state
// as well as shader used, framebuffer format,
// etc.
// While the pipeline is just a single object
// (VkPipeline) a lot of values are also saved
// here to do validation (vulkan does none by
// default) and warn the user if something
// was not supplied as intended.
private:
struct RenderPipeline {
// Cached values for validation.
#ifdef DEBUG_ENABLED
struct Validation {
FramebufferFormatID framebuffer_format;
uint32_t render_pass = 0;
uint32_t dynamic_state = 0;
VertexFormatID vertex_format;
bool uses_restart_indices = false;
uint32_t primitive_minimum = 0;
uint32_t primitive_divisor = 0;
} validation;
#endif
// Actual pipeline.
RID shader;
RDD::ShaderID shader_driver_id;
uint32_t shader_layout_hash = 0;
Vector<uint32_t> set_formats;
RDD::PipelineID driver_id;
BitField<RDD::PipelineStageBits> stage_bits;
uint32_t push_constant_size = 0;
};
RID_Owner<RenderPipeline> render_pipeline_owner;
bool pipeline_cache_enabled = false;
size_t pipeline_cache_size = 0;
String pipeline_cache_file_path;
WorkerThreadPool::TaskID pipeline_cache_save_task = WorkerThreadPool::INVALID_TASK_ID;
Vector<uint8_t> _load_pipeline_cache();
void _update_pipeline_cache(bool p_closing = false);
static void _save_pipeline_cache(void *p_data);
struct ComputePipeline {
RID shader;
RDD::ShaderID shader_driver_id;
uint32_t shader_layout_hash = 0;
Vector<uint32_t> set_formats;
RDD::PipelineID driver_id;
uint32_t push_constant_size = 0;
uint32_t local_group_size[3] = { 0, 0, 0 };
};
RID_Owner<ComputePipeline> compute_pipeline_owner;
public:
RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
bool render_pipeline_is_valid(RID p_pipeline);
RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
bool compute_pipeline_is_valid(RID p_pipeline);
private: