diff options
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan13_features.h | 127 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 6 |
2 files changed, 131 insertions, 2 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 | |||
| 23 | namespace leonida { | ||
| 24 | |||
| 25 | class Vulkan13Features { | ||
| 26 | public: | ||
| 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 | |||
| 113 | private: | ||
| 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. | ||
| 122 | inline 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 | ||
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e577e6af0..814f53549 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -3,7 +3,8 @@ | |||
| 3 | // The execution side of the D3D12 layer, without COM: the per-device Vulkan context (resources, layout tracker, | 3 | // The execution side of the D3D12 layer, without COM: the per-device Vulkan context (resources, layout tracker, |
| 4 | // image views), queues that execute command lists, and timeline-semaphore fences. The COM objects in | 4 | // image views), queues that execute command lists, and timeline-semaphore fences. The COM objects in |
| 5 | // shims/d3d12_com.cpp are thin wrappers over these. Needs Vulkan 1.2 timeline semaphores; rectangle clears and | 5 | // shims/d3d12_com.cpp are thin wrappers over these. Needs Vulkan 1.2 timeline semaphores; rectangle clears and |
| 6 | // enhanced barriers need the Vulkan 1.3 functions (dynamicRendering, synchronization2) to be enabled. | 6 | // enhanced barriers need the Vulkan 1.3 functions (dynamicRendering, synchronization2) to be enabled, as core |
| 7 | // features or through their KHR extensions on a 1.2 device (vulkan13_features.h). | ||
| 7 | 8 | ||
| 8 | #ifndef VK_NO_PROTOTYPES | 9 | #ifndef VK_NO_PROTOTYPES |
| 9 | #define VK_NO_PROTOTYPES | 10 | #define VK_NO_PROTOTYPES |
| @@ -12,6 +13,7 @@ | |||
| 12 | #include "occlusion_sum_spirv.h" | 13 | #include "occlusion_sum_spirv.h" |
| 13 | #include "retire_tickets.h" | 14 | #include "retire_tickets.h" |
| 14 | #include "statistics_sum_spirv.h" | 15 | #include "statistics_sum_spirv.h" |
| 16 | #include "vulkan13_features.h" | ||
| 15 | #include "vulkan_depth24.h" | 17 | #include "vulkan_depth24.h" |
| 16 | #include "vulkan_scratch.h" | 18 | #include "vulkan_scratch.h" |
| 17 | #include "vulkan_replay.h" | 19 | #include "vulkan_replay.h" |
| @@ -84,7 +86,7 @@ struct VulkanDeviceFunctions { | |||
| 84 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is | 86 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is |
| 85 | // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results. | 87 | // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results. |
| 86 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | 88 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { |
| 87 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); | 89 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(leonida::device_proc_or_khr(get, device, #name)); |
| 88 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) | 90 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) |
| 89 | #undef LEONIDA_VK_LOAD | 91 | #undef LEONIDA_VK_LOAD |
| 90 | return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer && | 92 | return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer && |