-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathvkd3d_private.h
6270 lines (5274 loc) · 216 KB
/
vkd3d_private.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 2016 Józef Kucia for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef __VKD3D_PRIVATE_H
#define __VKD3D_PRIVATE_H
#define COBJMACROS
#define VK_NO_PROTOTYPES
#include "vkd3d_common.h"
#include "vkd3d_memory.h"
#include "vkd3d_utf8.h"
#include "hashmap.h"
#include "list.h"
#include "rbtree.h"
#include "vkd3d.h"
#include "vkd3d_build.h"
#include "vkd3d_version.h"
#include "vkd3d_shader.h"
#include "vkd3d_threads.h"
#include "vkd3d_platform.h"
#include "vkd3d_swapchain_factory.h"
#include "vkd3d_command_list_vkd3d_ext.h"
#include "vkd3d_command_queue_vkd3d_ext.h"
#include "vkd3d_device_vkd3d_ext.h"
#include "vkd3d_string.h"
#include "vkd3d_file_utils.h"
#include "vkd3d_native_sync_handle.h"
#include "copy_utils.h"
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#define VK_CALL(f) (vk_procs->f)
#define MAKE_MAGIC(a,b,c,d) (((uint32_t)a) | (((uint32_t)b) << 8) | (((uint32_t)c) << 16) | (((uint32_t)d) << 24))
#define VKD3D_MAX_COMPATIBLE_FORMAT_COUNT 10u
#define VKD3D_MAX_SHADER_STAGES 5u
#define VKD3D_MAX_VK_SYNC_OBJECTS 4u
/* 6 types for CBV_SRV_UAV and 1 for sampler. */
#define VKD3D_MAX_BINDLESS_DESCRIPTOR_SETS 7u
/* The above plus one push descriptor set + static sampler set + static sampler set for local root signatures. */
#define VKD3D_MAX_DESCRIPTOR_SETS (VKD3D_MAX_BINDLESS_DESCRIPTOR_SETS + 3u)
#define VKD3D_MAX_MUTABLE_DESCRIPTOR_TYPES 6u
#define VKD3D_MAX_DESCRIPTOR_SIZE 256u /* Maximum allowed value in VK_EXT_descriptor_buffer. */
#define VKD3D_MIN_VIEW_DESCRIPTOR_COUNT (1000000u)
#define VKD3D_MIN_SAMPLER_DESCRIPTOR_COUNT (2048u)
#define VKD3D_TILE_SIZE 65536
typedef ID3D12Fence1 d3d12_fence_iface;
struct d3d12_command_list;
struct d3d12_command_allocator;
struct d3d12_device;
struct d3d12_resource;
struct vkd3d_bindless_set_info;
struct vkd3d_dynamic_state;
struct vkd3d_vk_global_procs
{
PFN_vkCreateInstance vkCreateInstance;
PFN_vkEnumerateInstanceVersion vkEnumerateInstanceVersion;
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties;
PFN_vkEnumerateInstanceLayerProperties vkEnumerateInstanceLayerProperties;
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
};
#define DECLARE_VK_PFN(name) PFN_##name name;
struct vkd3d_vk_instance_procs
{
#define VK_INSTANCE_PFN DECLARE_VK_PFN
#define VK_INSTANCE_EXT_PFN DECLARE_VK_PFN
#include "vulkan_procs.h"
};
struct vkd3d_vk_device_procs
{
#define VK_INSTANCE_PFN DECLARE_VK_PFN
#define VK_INSTANCE_EXT_PFN DECLARE_VK_PFN
#define VK_DEVICE_PFN DECLARE_VK_PFN
#define VK_DEVICE_EXT_PFN DECLARE_VK_PFN
#include "vulkan_procs.h"
};
#undef DECLARE_VK_PFN
HRESULT hresult_from_errno(int rc);
HRESULT hresult_from_vk_result(VkResult vr);
HRESULT hresult_from_vkd3d_result(int vkd3d_result);
struct vkd3d_vulkan_info
{
/* EXT instance extensions */
bool EXT_debug_utils;
/* KHR device extensions */
bool KHR_push_descriptor;
bool KHR_ray_tracing_pipeline;
bool KHR_acceleration_structure;
bool KHR_deferred_host_operations;
bool KHR_pipeline_library;
bool KHR_ray_query;
bool KHR_fragment_shading_rate;
bool KHR_ray_tracing_maintenance1;
bool KHR_fragment_shader_barycentric;
bool KHR_external_memory_win32;
bool KHR_external_semaphore_win32;
bool KHR_present_wait;
bool KHR_present_id;
bool KHR_maintenance5;
bool KHR_maintenance6;
bool KHR_maintenance7;
bool KHR_maintenance8;
bool KHR_shader_maximal_reconvergence;
bool KHR_shader_quad_control;
bool KHR_compute_shader_derivatives;
bool KHR_calibrated_timestamps;
/* EXT device extensions */
bool EXT_conditional_rendering;
bool EXT_conservative_rasterization;
bool EXT_custom_border_color;
bool EXT_depth_clip_enable;
bool EXT_device_generated_commands;
bool EXT_image_view_min_lod;
bool EXT_robustness2;
bool EXT_shader_stencil_export;
bool EXT_transform_feedback;
bool EXT_vertex_attribute_divisor;
bool EXT_extended_dynamic_state2;
bool EXT_extended_dynamic_state3;
bool EXT_external_memory_host;
bool EXT_shader_image_atomic_int64;
bool EXT_mesh_shader;
bool EXT_mutable_descriptor_type; /* EXT promotion of VALVE one. */
bool EXT_hdr_metadata;
bool EXT_shader_module_identifier;
bool EXT_descriptor_buffer;
bool EXT_pipeline_library_group_handles;
bool EXT_image_sliced_view_of_3d;
bool EXT_graphics_pipeline_library;
bool EXT_fragment_shader_interlock;
bool EXT_pageable_device_local_memory;
bool EXT_memory_priority;
bool EXT_dynamic_rendering_unused_attachments;
bool EXT_line_rasterization;
bool EXT_image_compression_control;
bool EXT_device_fault;
bool EXT_memory_budget;
bool EXT_device_address_binding_report;
bool EXT_depth_bias_control;
/* AMD device extensions */
bool AMD_buffer_marker;
bool AMD_device_coherent_memory;
bool AMD_shader_core_properties;
bool AMD_shader_core_properties2;
/* NV device extensions */
bool NV_optical_flow;
bool NV_shader_sm_builtins;
bool NVX_binary_import;
bool NVX_image_view_handle;
bool NV_fragment_shader_barycentric;
bool NV_compute_shader_derivatives;
bool NV_device_diagnostic_checkpoints;
bool NV_device_generated_commands;
bool NV_shader_subgroup_partitioned;
bool NV_memory_decompression;
bool NV_device_generated_commands_compute;
bool NV_low_latency2;
bool NV_raw_access_chains;
/* VALVE extensions */
bool VALVE_mutable_descriptor_type;
/* MESA extensions */
bool MESA_image_alignment_control;
/* Optional extensions which are enabled externally as optional extensions
* if swapchain/surface extensions are enabled. */
bool EXT_surface_maintenance1;
bool EXT_swapchain_maintenance1;
unsigned int extension_count;
const char* const* extension_names;
bool rasterization_stream;
unsigned int max_vertex_attrib_divisor;
VkPhysicalDeviceLimits device_limits;
VkPhysicalDeviceSparseProperties sparse_properties;
unsigned int shader_extension_count;
enum vkd3d_shader_target_extension shader_extensions[VKD3D_SHADER_TARGET_EXTENSION_COUNT];
};
struct vkd3d_instance
{
VkInstance vk_instance;
uint32_t instance_version;
struct vkd3d_vk_instance_procs vk_procs;
struct vkd3d_vulkan_info vk_info;
struct vkd3d_vk_global_procs vk_global_procs;
VkDebugUtilsMessengerEXT vk_debug_callback;
LONG refcount;
};
extern uint64_t vkd3d_config_flags;
extern struct vkd3d_shader_quirk_info vkd3d_shader_quirk_info;
struct vkd3d_queue_timeline_trace_cookie
{
unsigned int index;
};
struct vkd3d_fence_worker;
typedef void (*vkd3d_waiting_fence_callback)(struct vkd3d_fence_worker *, void *, bool);
struct vkd3d_fence_wait_info
{
VkSemaphore vk_semaphore;
uint64_t vk_semaphore_value;
vkd3d_waiting_fence_callback release_callback;
unsigned char userdata[32];
};
struct vkd3d_waiting_fence
{
struct vkd3d_fence_wait_info fence_info;
struct vkd3d_queue_timeline_trace_cookie timeline_cookie;
};
struct vkd3d_fence_worker
{
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
bool should_exit;
uint32_t enqueued_fence_count;
struct vkd3d_waiting_fence *enqueued_fences;
size_t enqueued_fences_size;
struct d3d12_device *device;
struct d3d12_command_queue *queue;
/* To aid timeline profiles. A single fence worker processes work monotonically. */
struct
{
char tid[64];
/* The lock timestamps is to ensure that the timeline trace becomes readable in chrome://tracing.
* For us, start and end ranges can overlap. This ends up as an unreadable trace
* since the tracer expects a stack-like nesting for overlapping events.
* To work around this, we ensure that start TS of a following event is moved to end TS of previous event. */
double lock_end_gpu_ts;
double lock_end_cpu_ts;
double lock_end_event_ts;
double lock_end_present_wait_ts;
/* A thread local buffer used to avoid holding locks for too long.
* Only submission threads flush out JSON IO and this serves as thread-local
* scratch space. */
unsigned int *list_buffer;
size_t list_buffer_size;
} timeline;
};
/* 2 MiB is a good threshold, because it's huge page size. */
#define VKD3D_VA_BLOCK_SIZE_BITS (21)
#define VKD3D_VA_BLOCK_SIZE (1ull << VKD3D_VA_BLOCK_SIZE_BITS)
#define VKD3D_VA_LO_MASK (VKD3D_VA_BLOCK_SIZE - 1)
#define VKD3D_VA_BLOCK_BITS (20)
#define VKD3D_VA_BLOCK_COUNT (1ull << VKD3D_VA_BLOCK_BITS)
#define VKD3D_VA_BLOCK_MASK (VKD3D_VA_BLOCK_COUNT - 1)
#define VKD3D_VA_NEXT_BITS (12)
#define VKD3D_VA_NEXT_COUNT (1ull << VKD3D_VA_NEXT_BITS)
#define VKD3D_VA_NEXT_MASK (VKD3D_VA_NEXT_COUNT - 1)
void vkd3d_add_wait_to_all_queues(struct d3d12_device *device,
VkSemaphore vk_semaphore, uint64_t value);
HRESULT vkd3d_create_timeline_semaphore(struct d3d12_device *device,
uint64_t initial_value, bool shared, VkSemaphore *vk_semaphore);
HRESULT vkd3d_enqueue_timeline_semaphore(struct vkd3d_fence_worker *worker,
const struct vkd3d_fence_wait_info *fence_info,
const struct vkd3d_queue_timeline_trace_cookie *timeline_cookie);
struct vkd3d_unique_resource;
struct vkd3d_va_entry
{
DECLSPEC_ALIGN(8) VkDeviceAddress va;
const struct vkd3d_unique_resource *resource;
};
struct vkd3d_va_block
{
struct vkd3d_va_entry l;
struct vkd3d_va_entry r;
};
struct vkd3d_va_tree
{
struct vkd3d_va_block blocks[VKD3D_VA_BLOCK_COUNT];
struct vkd3d_va_tree *next[VKD3D_VA_NEXT_COUNT];
};
struct vkd3d_va_range
{
VkDeviceAddress base;
VkDeviceSize size;
};
struct vkd3d_va_map
{
struct vkd3d_va_tree va_tree;
pthread_mutex_t mutex;
struct vkd3d_unique_resource **small_entries;
size_t small_entries_size;
size_t small_entries_count;
};
void vkd3d_va_map_insert(struct vkd3d_va_map *va_map, struct vkd3d_unique_resource *resource);
void vkd3d_va_map_remove(struct vkd3d_va_map *va_map, const struct vkd3d_unique_resource *resource);
const struct vkd3d_unique_resource *vkd3d_va_map_deref(struct vkd3d_va_map *va_map, VkDeviceAddress va);
VkAccelerationStructureKHR vkd3d_va_map_place_acceleration_structure(struct vkd3d_va_map *va_map,
struct d3d12_device *device,
VkDeviceAddress va);
void vkd3d_va_map_init(struct vkd3d_va_map *va_map);
void vkd3d_va_map_cleanup(struct vkd3d_va_map *va_map);
struct vkd3d_private_store
{
pthread_mutex_t mutex;
struct list content;
};
struct vkd3d_private_data
{
struct list entry;
GUID tag;
unsigned int size;
bool is_object;
union
{
BYTE data[1];
IUnknown *object;
};
};
static inline void vkd3d_private_data_destroy(struct vkd3d_private_data *data)
{
if (data->is_object)
IUnknown_Release(data->object);
list_remove(&data->entry);
vkd3d_free(data);
}
static inline HRESULT vkd3d_private_store_init(struct vkd3d_private_store *store)
{
int rc;
list_init(&store->content);
if ((rc = pthread_mutex_init(&store->mutex, NULL)))
ERR("Failed to initialize mutex, error %d.\n", rc);
return hresult_from_errno(rc);
}
static inline void vkd3d_private_store_destroy(struct vkd3d_private_store *store)
{
struct vkd3d_private_data *data, *cursor;
LIST_FOR_EACH_ENTRY_SAFE(data, cursor, &store->content, struct vkd3d_private_data, entry)
{
vkd3d_private_data_destroy(data);
}
pthread_mutex_destroy(&store->mutex);
}
static inline HRESULT vkd3d_private_data_lock(struct vkd3d_private_store *store)
{
int rc;
if ((rc = pthread_mutex_lock(&store->mutex)))
{
ERR("Failed to lock mutex, error %d.\n", rc);
return hresult_from_errno(rc);
}
return S_OK;
}
static inline void vkd3d_private_data_unlock(struct vkd3d_private_store *store)
{
pthread_mutex_unlock(&store->mutex);
}
HRESULT vkd3d_get_private_data(struct vkd3d_private_store *store,
const GUID *tag, unsigned int *out_size, void *out);
HRESULT vkd3d_private_store_set_private_data(struct vkd3d_private_store *store,
const GUID *tag, const void *data, unsigned int data_size, bool is_object);
typedef void(*vkd3d_set_name_callback)(void *, const char *);
static inline bool vkd3d_private_data_object_name_ptr(REFGUID guid,
UINT data_size, const void *data, const char **out_name)
{
if (out_name)
*out_name = NULL;
/* This is also handled in the object_name implementation
* but this avoids an additional, needless allocation
* and some games may spam SetName.
*/
if (!(vkd3d_config_flags & VKD3D_CONFIG_FLAG_DEBUG_UTILS))
return false;
if (IsEqualGUID(guid, &WKPDID_D3DDebugObjectName))
{
const char *name = (const char *)data;
if (!data || !data_size)
return true;
if (out_name)
*out_name = name[data_size - 1] != '\0'
? vkd3d_strdup_n(name, data_size)
: name;
return true;
}
else if (IsEqualGUID(guid, &WKPDID_D3DDebugObjectNameW))
{
const WCHAR *name = (const WCHAR *)data;
if (!data || data_size < sizeof(WCHAR))
return true;
if (out_name)
*out_name = vkd3d_strdup_w_utf8(name, data_size / sizeof(WCHAR));
return true;
}
return false;
}
static inline HRESULT vkd3d_set_private_data(struct vkd3d_private_store *store,
const GUID *tag, unsigned int data_size, const void *data,
vkd3d_set_name_callback set_name_callback, void *calling_object)
{
const char *name;
HRESULT hr;
if (FAILED(hr = vkd3d_private_data_lock(store)))
return hr;
if (FAILED(hr = vkd3d_private_store_set_private_data(store, tag, data, data_size, false)))
{
vkd3d_private_data_unlock(store);
return hr;
}
if (set_name_callback && vkd3d_private_data_object_name_ptr(tag, data_size, data, &name))
{
set_name_callback(calling_object, name);
if (name && name != data)
vkd3d_free((void *)name);
}
vkd3d_private_data_unlock(store);
return hr;
}
static inline HRESULT vkd3d_set_private_data_interface(struct vkd3d_private_store *store,
const GUID *tag, const IUnknown *object,
vkd3d_set_name_callback set_name_callback, void *calling_object)
{
const void *data = object ? object : (void *)&object;
HRESULT hr;
if (FAILED(hr = vkd3d_private_data_lock(store)))
return hr;
if (FAILED(hr = vkd3d_private_store_set_private_data(store, tag, data, sizeof(object), !!object)))
{
vkd3d_private_data_unlock(store);
return hr;
}
if (set_name_callback && vkd3d_private_data_object_name_ptr(tag, 0, NULL, NULL))
set_name_callback(calling_object, NULL);
vkd3d_private_data_unlock(store);
return hr;
}
HRESULT STDMETHODCALLTYPE d3d12_object_SetName(ID3D12Object *iface, const WCHAR *name);
struct d3d_destruction_callback_entry
{
PFN_DESTRUCTION_CALLBACK callback;
void *userdata;
UINT callback_id;
};
struct d3d_destruction_notifier
{
ID3DDestructionNotifier ID3DDestructionNotifier_iface;
IUnknown *parent;
pthread_mutex_t mutex;
struct d3d_destruction_callback_entry *callbacks;
size_t callback_size;
size_t callback_count;
UINT next_callback_id;
};
void d3d_destruction_notifier_init(struct d3d_destruction_notifier *notifier, IUnknown *parent);
void d3d_destruction_notifier_free(struct d3d_destruction_notifier *notifier);
void d3d_destruction_notifier_notify(struct d3d_destruction_notifier *notifier);
/* ID3D12Fence */
struct d3d12_fence_value
{
uint64_t virtual_value;
uint64_t update_count;
VkSemaphore vk_semaphore;
uint64_t vk_semaphore_value;
};
#define VKD3D_WAITING_EVENT_SIGNAL_BIT (1u << 31)
enum vkd3d_waiting_event_type
{
VKD3D_WAITING_EVENT_SINGLE,
VKD3D_WAITING_EVENT_MULTI_ALL,
VKD3D_WAITING_EVENT_MULTI_ANY,
};
struct vkd3d_waiting_event
{
enum vkd3d_waiting_event_type wait_type;
uint64_t value;
vkd3d_native_sync_handle handle;
bool *latch;
uint32_t *payload;
struct vkd3d_queue_timeline_trace_cookie timeline_cookie;
};
struct d3d12_fence
{
d3d12_fence_iface ID3D12Fence_iface;
LONG refcount_internal;
LONG refcount;
D3D12_FENCE_FLAGS d3d12_flags;
/* only used for shared semaphores */
VkSemaphore timeline_semaphore;
uint64_t max_pending_virtual_timeline_value;
uint64_t virtual_value;
uint64_t signal_count;
uint64_t update_count;
struct d3d12_fence_value *pending_updates;
size_t pending_updates_count;
size_t pending_updates_size;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_cond_t null_event_cond;
struct vkd3d_waiting_event *events;
size_t events_size;
size_t event_count;
struct d3d12_device *device;
struct vkd3d_private_store private_store;
struct d3d_destruction_notifier destruction_notifier;
};
static inline struct d3d12_fence *impl_from_ID3D12Fence1(ID3D12Fence1 *iface)
{
extern CONST_VTBL struct ID3D12Fence1Vtbl d3d12_fence_vtbl;
if (!iface)
return NULL;
assert(iface->lpVtbl == &d3d12_fence_vtbl);
return CONTAINING_RECORD(iface, struct d3d12_fence, ID3D12Fence_iface);
}
static inline struct d3d12_fence *impl_from_ID3D12Fence(ID3D12Fence *iface)
{
return impl_from_ID3D12Fence1((ID3D12Fence1 *)iface);
}
HRESULT d3d12_fence_create(struct d3d12_device *device,
uint64_t initial_value, D3D12_FENCE_FLAGS flags, struct d3d12_fence **fence);
HRESULT d3d12_fence_set_event_on_completion(struct d3d12_fence *fence,
UINT64 value, HANDLE event);
HRESULT d3d12_fence_set_native_sync_handle_on_completion(struct d3d12_fence *fence,
UINT64 value, vkd3d_native_sync_handle handle);
struct vkd3d_shared_fence_waiting_event
{
struct list entry;
struct vkd3d_waiting_event wait;
};
struct d3d12_shared_fence
{
d3d12_fence_iface ID3D12Fence_iface;
LONG refcount_internal;
LONG refcount;
D3D12_FENCE_FLAGS d3d12_flags;
VkSemaphore timeline_semaphore;
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond_var;
uint32_t is_running;
struct list events;
struct d3d12_device *device;
struct vkd3d_private_store private_store;
struct d3d_destruction_notifier destruction_notifier;
};
static inline struct d3d12_shared_fence *shared_impl_from_ID3D12Fence1(ID3D12Fence1 *iface)
{
extern CONST_VTBL struct ID3D12Fence1Vtbl d3d12_shared_fence_vtbl;
if (!iface)
return NULL;
assert(iface->lpVtbl == &d3d12_shared_fence_vtbl);
return CONTAINING_RECORD(iface, struct d3d12_shared_fence, ID3D12Fence_iface);
}
static inline struct d3d12_shared_fence *shared_impl_from_ID3D12Fence(ID3D12Fence *iface)
{
return shared_impl_from_ID3D12Fence1((ID3D12Fence1 *)iface);
}
HRESULT d3d12_shared_fence_create(struct d3d12_device *device,
uint64_t initial_value, D3D12_FENCE_FLAGS flags, struct d3d12_shared_fence **fence);
static inline bool is_shared_ID3D12Fence1(ID3D12Fence1 *iface)
{
extern CONST_VTBL struct ID3D12Fence1Vtbl d3d12_shared_fence_vtbl;
extern CONST_VTBL struct ID3D12Fence1Vtbl d3d12_fence_vtbl;
assert(iface->lpVtbl == &d3d12_shared_fence_vtbl || iface->lpVtbl == &d3d12_fence_vtbl);
return iface->lpVtbl == &d3d12_shared_fence_vtbl;
}
static inline bool is_shared_ID3D12Fence(ID3D12Fence *iface)
{
return is_shared_ID3D12Fence1((ID3D12Fence1 *)iface);
}
HRESULT d3d12_fence_iface_set_native_sync_handle_on_completion_explicit(ID3D12Fence *iface,
enum vkd3d_waiting_event_type wait_type, UINT64 value, vkd3d_native_sync_handle handle, uint32_t *payload);
enum vkd3d_allocation_flag
{
VKD3D_ALLOCATION_FLAG_GLOBAL_BUFFER = (1u << 0),
VKD3D_ALLOCATION_FLAG_GPU_ADDRESS = (1u << 1),
VKD3D_ALLOCATION_FLAG_CPU_ACCESS = (1u << 2),
VKD3D_ALLOCATION_FLAG_ALLOW_WRITE_WATCH = (1u << 3),
VKD3D_ALLOCATION_FLAG_NO_FALLBACK = (1u << 4),
VKD3D_ALLOCATION_FLAG_DEDICATED = (1u << 5),
/* Intended for internal allocation of scratch buffers.
* They are never suballocated since we do that ourselves,
* and we do not consume space in the VA map. */
VKD3D_ALLOCATION_FLAG_INTERNAL_SCRATCH = (1u << 6),
VKD3D_ALLOCATION_FLAG_ALLOW_IMAGE_SUBALLOCATION = (1u << 7),
};
#define VKD3D_MEMORY_CHUNK_SIZE (VKD3D_VA_BLOCK_SIZE * 8)
#define VKD3D_MEMORY_IMAGE_HEAP_SUBALLOCATE_THRESHOLD (8 * 1024 * 1024)
#define VKD3D_MEMORY_LARGE_CHUNK_SIZE (VKD3D_MEMORY_IMAGE_HEAP_SUBALLOCATE_THRESHOLD * 4)
struct vkd3d_memory_chunk;
struct vkd3d_allocate_memory_info
{
VkMemoryRequirements memory_requirements;
D3D12_HEAP_PROPERTIES heap_properties;
D3D12_HEAP_FLAGS heap_flags;
void *host_ptr;
const void *pNext;
uint32_t flags;
VkBufferUsageFlags2KHR explicit_global_buffer_usage;
VkMemoryPropertyFlags optional_memory_properties;
float vk_memory_priority;
};
struct vkd3d_allocate_heap_memory_info
{
D3D12_HEAP_DESC heap_desc;
void *host_ptr;
uint32_t extra_allocation_flags;
float vk_memory_priority;
VkBufferUsageFlags2KHR explicit_global_buffer_usage;
};
struct vkd3d_allocate_resource_memory_info
{
D3D12_HEAP_PROPERTIES heap_properties;
D3D12_HEAP_FLAGS heap_flags;
VkBuffer vk_buffer;
VkImage vk_image;
void *host_ptr;
};
uint32_t vkd3d_get_priority_adjust(VkDeviceSize size);
float vkd3d_convert_to_vk_prio(D3D12_RESIDENCY_PRIORITY d3d12prio);
struct vkd3d_view_map;
struct vkd3d_unique_resource
{
union
{
VkBuffer vk_buffer;
VkImage vk_image;
};
uint64_t cookie;
VkDeviceAddress va;
VkDeviceSize size;
/* This is used to handle views when we cannot bind it to a
* specific ID3D12Resource, i.e. RTAS. Only allocated as needed. */
struct vkd3d_view_map *view_map;
};
struct vkd3d_device_memory_allocation
{
VkDeviceMemory vk_memory;
uint32_t vk_memory_type;
VkDeviceSize size;
};
struct vkd3d_memory_allocation
{
struct vkd3d_unique_resource resource;
struct vkd3d_device_memory_allocation device_allocation;
VkDeviceSize offset;
void *cpu_address;
D3D12_HEAP_TYPE heap_type;
uint32_t flags;
VkBufferUsageFlags2KHR explicit_global_buffer_usage;
uint64_t clear_semaphore_value;
struct vkd3d_memory_chunk *chunk;
};
static inline void vkd3d_memory_allocation_slice(struct vkd3d_memory_allocation *dst,
const struct vkd3d_memory_allocation *src, VkDeviceSize offset, VkDeviceSize size)
{
*dst = *src;
dst->offset += offset;
dst->resource.size = size;
dst->resource.va += offset;
if (dst->cpu_address)
dst->cpu_address = void_ptr_offset(dst->cpu_address, offset);
}
struct vkd3d_memory_free_range
{
VkDeviceSize offset;
VkDeviceSize length;
};
struct vkd3d_memory_chunk
{
struct vkd3d_memory_allocation allocation;
struct vkd3d_memory_free_range *free_ranges;
size_t free_ranges_size;
size_t free_ranges_count;
};
#define VKD3D_MEMORY_TRANSFER_COMMAND_BUFFER_COUNT (16u)
enum vkd3d_memory_transfer_op
{
VKD3D_MEMORY_TRANSFER_OP_CLEAR_ALLOCATION,
VKD3D_MEMORY_TRANSFER_OP_WRITE_SUBRESOURCE,
};
struct vkd3d_memory_transfer_info
{
enum vkd3d_memory_transfer_op op;
struct vkd3d_memory_allocation *allocation;
struct d3d12_resource *resource;
uint32_t subresource_idx;
VkOffset3D offset;
VkExtent3D extent;
};
struct vkd3d_memory_transfer_tracked_resource
{
struct d3d12_resource *resource;
UINT64 semaphore_value;
};
struct vkd3d_memory_transfer_queue
{
struct d3d12_device *device;
struct vkd3d_queue *vkd3d_queue;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t thread;
VkCommandBuffer vk_command_buffers[VKD3D_MEMORY_TRANSFER_COMMAND_BUFFER_COUNT];
VkCommandPool vk_command_pool;
VkSemaphore vk_semaphore;
UINT64 last_known_value;
UINT64 next_signal_value;
VkDeviceSize num_bytes_pending;
uint32_t command_buffer_index;
struct vkd3d_memory_transfer_info *transfers;
size_t transfer_size;
size_t transfer_count;
struct vkd3d_memory_transfer_tracked_resource *tracked_resources;
size_t tracked_resource_size;
size_t tracked_resource_count;
};
void vkd3d_memory_transfer_queue_cleanup(struct vkd3d_memory_transfer_queue *queue);
HRESULT vkd3d_memory_transfer_queue_init(struct vkd3d_memory_transfer_queue *queue, struct d3d12_device *device);
HRESULT vkd3d_memory_transfer_queue_flush(struct vkd3d_memory_transfer_queue *queue);
HRESULT vkd3d_memory_transfer_queue_write_subresource(struct vkd3d_memory_transfer_queue *queue,
struct d3d12_resource *resource, uint32_t subresource_idx, VkOffset3D offset, VkExtent3D extent);
struct vkd3d_memory_allocator
{
pthread_mutex_t mutex;
struct vkd3d_memory_chunk **chunks;
size_t chunks_size;
size_t chunks_count;
struct vkd3d_va_map va_map;
};
void vkd3d_free_memory(struct d3d12_device *device, struct vkd3d_memory_allocator *allocator,
const struct vkd3d_memory_allocation *allocation);
HRESULT vkd3d_allocate_memory(struct d3d12_device *device, struct vkd3d_memory_allocator *allocator,
const struct vkd3d_allocate_memory_info *info, struct vkd3d_memory_allocation *allocation);
bool vkd3d_allocate_image_memory_prefers_dedicated(struct d3d12_device *device,
D3D12_HEAP_FLAGS heap_flags, const VkMemoryRequirements *requirements);
HRESULT vkd3d_allocate_heap_memory(struct d3d12_device *device, struct vkd3d_memory_allocator *allocator,
const struct vkd3d_allocate_heap_memory_info *info, struct vkd3d_memory_allocation *allocation);
HRESULT vkd3d_memory_allocator_init(struct vkd3d_memory_allocator *allocator, struct d3d12_device *device);
void vkd3d_memory_allocator_cleanup(struct vkd3d_memory_allocator *allocator, struct d3d12_device *device);
/* ID3D12Heap */
typedef ID3D12Heap1 d3d12_heap_iface;
typedef struct
{
bool allows_dynamic_residency;
spinlock_t spinlock; /* covers access to any of the following fields after creation */
D3D12_RESIDENCY_PRIORITY d3d12priority;
LONG residency_count;
} priority_info;
struct d3d12_heap
{
d3d12_heap_iface ID3D12Heap_iface;
LONG internal_refcount;
LONG refcount;
D3D12_HEAP_DESC desc;
struct vkd3d_memory_allocation allocation;
priority_info priority;
struct d3d12_device *device;
struct vkd3d_private_store private_store;
struct d3d_destruction_notifier destruction_notifier;
};
HRESULT d3d12_heap_create(struct d3d12_device *device, const D3D12_HEAP_DESC *desc,
void *host_address, struct d3d12_heap **heap);
HRESULT d3d12_device_validate_custom_heap_type(struct d3d12_device *device,
const D3D12_HEAP_PROPERTIES *heap_properties);
ULONG d3d12_heap_incref(struct d3d12_heap *heap);
ULONG d3d12_heap_decref(struct d3d12_heap *heap);
static inline struct d3d12_heap *impl_from_ID3D12Heap1(ID3D12Heap1 *iface)
{
extern CONST_VTBL struct ID3D12Heap1Vtbl d3d12_heap_vtbl;
if (!iface)
return NULL;
assert(iface->lpVtbl == &d3d12_heap_vtbl);
return CONTAINING_RECORD(iface, struct d3d12_heap, ID3D12Heap_iface);
}
static inline struct d3d12_heap *impl_from_ID3D12Heap(ID3D12Heap *iface)
{
return impl_from_ID3D12Heap1((ID3D12Heap1 *)iface);
}
enum vkd3d_resource_flag
{
VKD3D_RESOURCE_COMMITTED = (1u << 0),
VKD3D_RESOURCE_PLACED = (1u << 1),
VKD3D_RESOURCE_RESERVED = (1u << 2),
VKD3D_RESOURCE_ALLOCATION = (1u << 3),
VKD3D_RESOURCE_LINEAR_STAGING_COPY = (1u << 4),
VKD3D_RESOURCE_EXTERNAL = (1u << 5),
VKD3D_RESOURCE_ACCELERATION_STRUCTURE = (1u << 6),
VKD3D_RESOURCE_GENERAL_LAYOUT = (1u << 7),
};
#define VKD3D_INVALID_TILE_INDEX (~0u)
struct d3d12_sparse_image_region
{
VkImageSubresource subresource;
uint32_t subresource_index;
VkOffset3D offset;
VkExtent3D extent;
};
struct d3d12_sparse_buffer_region
{
VkDeviceSize offset;
VkDeviceSize length;
};
struct d3d12_sparse_tile
{
union
{
struct d3d12_sparse_image_region image;
struct d3d12_sparse_buffer_region buffer;
};
VkDeviceMemory vk_memory;
VkDeviceSize vk_offset;
};
struct d3d12_sparse_info
{
uint32_t tile_count;
uint32_t tiling_count;
struct d3d12_sparse_tile *tiles;
D3D12_TILE_SHAPE tile_shape;
D3D12_PACKED_MIP_INFO packed_mips;
D3D12_SUBRESOURCE_TILING *tilings;