device/vulkan/native.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/device/vulkan/native.h" | ||
| 3 | |||
| 4 | #include "chrono_batch_render/device/buffer.h" | ||
| 5 | #include "chrono_batch_render/device/queue.h" | ||
| 6 | #include "chrono_batch_render/device/vulkan/device.h" | ||
| 7 | #include "chrono_batch_render/util/error.h" | ||
| 8 | |||
| 9 | namespace chrono::batch_render::device::vulkan { | ||
| 10 | namespace { | ||
| 11 | |||
| 12 | 2 | VulkanDevice& Checked(Device& device) { | |
| 13 | 2 | if (device.Adapter().backend != Backend::Vulkan) { | |
| 14 | ✗ | throw UnsupportedError("{} is a {} device, not a Vulkan one", device.Adapter().alias, | |
| 15 | ✗ | BackendName(device.Adapter().backend)); | |
| 16 | } | ||
| 17 | 2 | return static_cast<VulkanDevice&>(device); | |
| 18 | } | ||
| 19 | |||
| 20 | ✗ | Queue& RoleQueue(VulkanDevice& vulkan, QueueRole role) { | |
| 21 | ✗ | return role == QueueRole::Display ? vulkan.DisplayQueue() : vulkan.GetQueue(); | |
| 22 | } | ||
| 23 | |||
| 24 | } | ||
| 25 | |||
| 26 | 2 | Native NativeHandles(Device& device) { | |
| 27 | 2 | VulkanDevice& vulkan = Checked(device); | |
| 28 | 2 | return {.getInstanceProcAddr = vkGetInstanceProcAddr, | |
| 29 | 2 | .instance = vulkan.InstanceHandle(), | |
| 30 | 2 | .physicalDevice = vulkan.PhysicalDevice(), | |
| 31 | 2 | .device = vulkan.Handle(), | |
| 32 | .apiVersion = VK_API_VERSION_1_3, | ||
| 33 | 2 | .queueFamily = vulkan.QueueFamily(), | |
| 34 | 2 | .displaySharesRender = vulkan.SharesDisplayQueue()}; | |
| 35 | } | ||
| 36 | |||
| 37 | ✗ | VkBuffer NativeBuffer(const Tensor& tensor) { | |
| 38 | ✗ | if (tensor.Empty()) { | |
| 39 | ✗ | throw Error("an empty tensor has no Vulkan buffer"); | |
| 40 | } | ||
| 41 | ✗ | Buffer& buffer = tensor.GetBuffer(); | |
| 42 | ✗ | Checked(buffer.GetDevice()); | |
| 43 | ✗ | return static_cast<VulkanBuffer&>(buffer).Handle(); | |
| 44 | } | ||
| 45 | |||
| 46 | ✗ | Operation Submit(Device& device, const NativeSubmit& submit, QueueRole role) { | |
| 47 | ✗ | Queue& queue = RoleQueue(Checked(device), role); | |
| 48 | ✗ | for (const Operation& operation : submit.after) { | |
| 49 | ✗ | queue.Follow(operation); | |
| 50 | } | ||
| 51 | ✗ | return SubmitNative(queue, submit); | |
| 52 | } | ||
| 53 | |||
| 54 | ✗ | VkResult Present(Device& device, const VkPresentInfoKHR& present) { | |
| 55 | ✗ | Queue& queue = Checked(device).DisplayQueue(); | |
| 56 | ✗ | if (!device.Presents()) { | |
| 57 | ✗ | throw UnsupportedError("{} wasn't created with presentation, so it presents nothing", | |
| 58 | ✗ | device.Adapter().alias); | |
| 59 | } | ||
| 60 | ✗ | return PresentNative(queue, present); | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | void Exclusive(Device& device, const std::function<void(VkQueue queue)>& use, QueueRole role) { | |
| 64 | ✗ | Queue& queue = RoleQueue(Checked(device), role); | |
| 65 | ✗ | queue.Exclusive([&] { use(NativeQueue(queue)); }); | |
| 66 | ✗ | } | |
| 67 | |||
| 68 | } | ||
| 69 |