upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan13_features.h
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 09:48:06 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 09:48:06 -0500
commitbf0ce0d062acede44535e62d48187cedf4b0c2b3 (patch)
treedc0a3cdcf828a42a0b6465d6b103b95202983f5d /runtime/src/vulkan13_features.h
parentc034e448c37d8522978af94f3cb01ec3e903fcfc (diff)
parenteebf85b60f021ce567427ce766f4ec1c88b0f13a (diff)
Merge pull request #1706 from Ash Hopkins
Diffstat (limited to 'runtime/src/vulkan13_features.h')
-rw-r--r--runtime/src/vulkan13_features.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/runtime/src/vulkan13_features.h b/runtime/src/vulkan13_features.h
new file mode 100644
index 000000000..2057d953b
--- /dev/null
+++ b/runtime/src/vulkan13_features.h
@@ -0,0 +1,127 @@
1#pragma once
2
3// dynamicRendering and synchronization2, the two Vulkan 1.3 features the D3D12 layer needs, on a Vulkan 1.3 device
4// or on a Vulkan 1.2 device through VK_KHR_dynamic_rendering and VK_KHR_synchronization2. CrossOver's MoltenVK on
5// macOS is such a 1.2 device (1.2.10: Vulkan 1.2 with both extensions); the extension structs, functions and
6// behaviour are the core ones under another name, so the rest of the layer does not see the difference.
7//
8// leonida::Vulkan13Features v13;
9// v13.query(physical, props.apiVersion, get_features2, enumerate_extensions); // per candidate
10// if (!v13.usable()) continue;
11// enable12.pNext = v13.enable(chain); // chain: what came after VkPhysicalDeviceVulkan13Features
12// ... v13.next() is where later feature structs go (it used to be enable13.pNext)
13// v13.add_extensions(device_extensions);
14// fn = leonida::device_proc_or_khr(get_device_proc, device, "vkCmdPipelineBarrier2");
15
16#include <vulkan/vulkan.h>
17
18#include <cstdint>
19#include <cstring>
20#include <string>
21#include <vector>
22
23namespace leonida {
24
25class Vulkan13Features {
26public:
27 Vulkan13Features() = default;
28 Vulkan13Features(const Vulkan13Features&) = delete; // enable() links pointers into this object
29 Vulkan13Features& operator=(const Vulkan13Features&) = delete;
30
31 bool core = false; // a Vulkan 1.3 device: the features are core
32 bool dynamic_rendering = false; // supported, as a core feature or through VK_KHR_dynamic_rendering
33 bool synchronization2 = false; // supported, as a core feature or through VK_KHR_synchronization2
34 // Optional 1.3 core features, enabled where supported; on a 1.2 device they stay off (no required subgroup size).
35 bool subgroup_size_control = false;
36 bool compute_full_subgroups = false;
37
38 bool usable() const { return dynamic_rendering && synchronization2; }
39
40 // Fills the flags above for one physical device. A device older than 1.2 never qualifies.
41 void query(VkPhysicalDevice physical, std::uint32_t api_version, PFN_vkGetPhysicalDeviceFeatures2 features2,
42 PFN_vkEnumerateDeviceExtensionProperties enumerate_extensions) {
43 core = api_version >= VK_API_VERSION_1_3;
44 dynamic_rendering = synchronization2 = subgroup_size_control = compute_full_subgroups = false;
45 if (!features2) return;
46 if (core) {
47 VkPhysicalDeviceVulkan13Features f13{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES};
48 VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &f13};
49 features2(physical, &query);
50 dynamic_rendering = f13.dynamicRendering == VK_TRUE;
51 synchronization2 = f13.synchronization2 == VK_TRUE;
52 subgroup_size_control = f13.subgroupSizeControl == VK_TRUE;
53 compute_full_subgroups = f13.computeFullSubgroups == VK_TRUE;
54 return;
55 }
56 if (api_version < VK_API_VERSION_1_2 || !enumerate_extensions) return;
57 std::uint32_t count = 0;
58 enumerate_extensions(physical, nullptr, &count, nullptr);
59 std::vector<VkExtensionProperties> extensions(count);
60 enumerate_extensions(physical, nullptr, &count, extensions.data());
61 bool has_dynamic_rendering = false, has_synchronization2 = false;
62 for (const auto& e : extensions) {
63 has_dynamic_rendering |= std::strcmp(e.extensionName, VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME) == 0;
64 has_synchronization2 |= std::strcmp(e.extensionName, VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME) == 0;
65 }
66 if (!has_dynamic_rendering || !has_synchronization2) return;
67 VkPhysicalDeviceSynchronization2FeaturesKHR sync2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SYNCHRONIZATION_2_FEATURES_KHR};
68 VkPhysicalDeviceDynamicRenderingFeaturesKHR rendering{
69 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR, &sync2};
70 VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &rendering};
71 features2(physical, &query);
72 dynamic_rendering = rendering.dynamicRendering == VK_TRUE;
73 synchronization2 = sync2.synchronization2 == VK_TRUE;
74 }
75
76 // Copies the flags of the device query() chose (a later query() for another candidate overwrites them).
77 void choose(const Vulkan13Features& from) {
78 core = from.core;
79 dynamic_rendering = from.dynamic_rendering;
80 synchronization2 = from.synchronization2;
81 subgroup_size_control = from.subgroup_size_control;
82 compute_full_subgroups = from.compute_full_subgroups;
83 }
84
85 // The structs that enable both features, ahead of `chain`; returns their head for VkPhysicalDeviceVulkan12Features'
86 // pNext. Call once, after query() or choose().
87 void* enable(void* chain) {
88 if (core) {
89 enable13_.pNext = chain;
90 enable13_.dynamicRendering = VK_TRUE;
91 enable13_.synchronization2 = VK_TRUE;
92 enable13_.subgroupSizeControl = subgroup_size_control ? VK_TRUE : VK_FALSE;
93 enable13_.computeFullSubgroups = compute_full_subgroups ? VK_TRUE : VK_FALSE;
94 return &enable13_;
95 }
96 sync2_.pNext = chain;
97 sync2_.synchronization2 = VK_TRUE;
98 rendering_.pNext = &sync2_;
99 rendering_.dynamicRendering = VK_TRUE;
100 return &rendering_;
101 }
102
103 // Where a feature struct goes to follow these: insert with `s.pNext = *next(); *next() = &s;`.
104 void** next() { return core ? &enable13_.pNext : &sync2_.pNext; }
105
106 // The device extensions a 1.2 device needs for both features; none on 1.3.
107 void add_extensions(std::vector<const char*>& extensions) const {
108 if (core) return;
109 extensions.push_back(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME);
110 extensions.push_back(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
111 }
112
113private:
114 VkPhysicalDeviceVulkan13Features enable13_{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES};
115 VkPhysicalDeviceDynamicRenderingFeaturesKHR rendering_{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR};
116 VkPhysicalDeviceSynchronization2FeaturesKHR sync2_{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SYNCHRONIZATION_2_FEATURES_KHR};
117};
118
119// A device function by its core name, or by its KHR name where the core one is null: a Vulkan 1.2 device returns
120// null for Vulkan 1.3 core names (vkCmdPipelineBarrier2, vkCmdBeginRendering, ...) and serves the enabled
121// extension's vkCmdPipelineBarrier2KHR and the like, which have the same signatures.
122inline PFN_vkVoidFunction device_proc_or_khr(PFN_vkGetDeviceProcAddr get, VkDevice device, const char* name) {
123 if (const PFN_vkVoidFunction function = get(device, name)) return function;
124 return get(device, (std::string(name) + "KHR").c_str());
125}
126
127} // namespace leonida