1 /*
2 * Copyright © 2021 Bas Nieuwenhuizen
3 * Copyright © 2023 Valve Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "vk_acceleration_structure.h"
26
27 #include "vk_alloc.h"
28 #include "vk_common_entrypoints.h"
29 #include "vk_device.h"
30 #include "vk_log.h"
31
32 VkDeviceAddress
vk_acceleration_structure_get_va(struct vk_acceleration_structure * accel_struct)33 vk_acceleration_structure_get_va(struct vk_acceleration_structure *accel_struct)
34 {
35 VkBufferDeviceAddressInfo info = {
36 .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
37 .buffer = accel_struct->buffer,
38 };
39
40 VkDeviceAddress base_addr = accel_struct->base.device->dispatch_table.GetBufferDeviceAddress(
41 vk_device_to_handle(accel_struct->base.device), &info);
42
43 return base_addr + accel_struct->offset;
44 }
45
46
47 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreateAccelerationStructureKHR(VkDevice _device,const VkAccelerationStructureCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkAccelerationStructureKHR * pAccelerationStructure)48 vk_common_CreateAccelerationStructureKHR(VkDevice _device,
49 const VkAccelerationStructureCreateInfoKHR *pCreateInfo,
50 const VkAllocationCallbacks *pAllocator,
51 VkAccelerationStructureKHR *pAccelerationStructure)
52 {
53 VK_FROM_HANDLE(vk_device, device, _device);
54
55 struct vk_acceleration_structure *accel_struct = vk_object_alloc(
56 device, pAllocator, sizeof(struct vk_acceleration_structure),
57 VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR);
58
59 if (!accel_struct)
60 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
61
62 accel_struct->buffer = pCreateInfo->buffer;
63 accel_struct->offset = pCreateInfo->offset;
64 accel_struct->size = pCreateInfo->size;
65
66 if (pCreateInfo->deviceAddress &&
67 vk_acceleration_structure_get_va(accel_struct) != pCreateInfo->deviceAddress)
68 return vk_error(device, VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR);
69
70 *pAccelerationStructure = vk_acceleration_structure_to_handle(accel_struct);
71 return VK_SUCCESS;
72 }
73
74 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyAccelerationStructureKHR(VkDevice _device,VkAccelerationStructureKHR accelerationStructure,const VkAllocationCallbacks * pAllocator)75 vk_common_DestroyAccelerationStructureKHR(VkDevice _device,
76 VkAccelerationStructureKHR accelerationStructure,
77 const VkAllocationCallbacks *pAllocator)
78 {
79 VK_FROM_HANDLE(vk_device, device, _device);
80 VK_FROM_HANDLE(vk_acceleration_structure, accel_struct, accelerationStructure);
81
82 if (!accel_struct)
83 return;
84
85 vk_object_free(device, pAllocator, accel_struct);
86 }
87
88 VKAPI_ATTR VkDeviceAddress VKAPI_CALL
vk_common_GetAccelerationStructureDeviceAddressKHR(VkDevice _device,const VkAccelerationStructureDeviceAddressInfoKHR * pInfo)89 vk_common_GetAccelerationStructureDeviceAddressKHR(
90 VkDevice _device, const VkAccelerationStructureDeviceAddressInfoKHR *pInfo)
91 {
92 VK_FROM_HANDLE(vk_acceleration_structure, accel_struct, pInfo->accelerationStructure);
93 return vk_acceleration_structure_get_va(accel_struct);
94 }
95