17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2020 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "anv_private.h"
257ec681f3Smrg
267ec681f3Smrgvoid
277ec681f3Smrganv_GetAccelerationStructureBuildSizesKHR(
287ec681f3Smrg    VkDevice                                    device,
297ec681f3Smrg    VkAccelerationStructureBuildTypeKHR         buildType,
307ec681f3Smrg    const VkAccelerationStructureBuildGeometryInfoKHR* pBuildInfo,
317ec681f3Smrg    const uint32_t*                             pMaxPrimitiveCounts,
327ec681f3Smrg    VkAccelerationStructureBuildSizesInfoKHR*   pSizeInfo)
337ec681f3Smrg{
347ec681f3Smrg   assert(pSizeInfo->sType ==
357ec681f3Smrg          VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR);
367ec681f3Smrg
377ec681f3Smrg   uint64_t max_prim_count = 0;
387ec681f3Smrg   for (uint32_t i = 0; i < pBuildInfo->geometryCount; i++)
397ec681f3Smrg      max_prim_count += pMaxPrimitiveCounts[i];
407ec681f3Smrg
417ec681f3Smrg   pSizeInfo->accelerationStructureSize = 0; /* TODO */
427ec681f3Smrg
437ec681f3Smrg   uint64_t cpu_build_scratch_size = 0; /* TODO */
447ec681f3Smrg   uint64_t cpu_update_scratch_size = cpu_build_scratch_size;
457ec681f3Smrg
467ec681f3Smrg   uint64_t gpu_build_scratch_size = 0; /* TODO */
477ec681f3Smrg   uint64_t gpu_update_scratch_size = gpu_build_scratch_size;
487ec681f3Smrg
497ec681f3Smrg   switch (buildType) {
507ec681f3Smrg   case VK_ACCELERATION_STRUCTURE_BUILD_TYPE_HOST_KHR:
517ec681f3Smrg      pSizeInfo->buildScratchSize = cpu_build_scratch_size;
527ec681f3Smrg      pSizeInfo->updateScratchSize = cpu_update_scratch_size;
537ec681f3Smrg      break;
547ec681f3Smrg
557ec681f3Smrg   case VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR:
567ec681f3Smrg      pSizeInfo->buildScratchSize = gpu_build_scratch_size;
577ec681f3Smrg      pSizeInfo->updateScratchSize = gpu_update_scratch_size;
587ec681f3Smrg      break;
597ec681f3Smrg
607ec681f3Smrg   case VK_ACCELERATION_STRUCTURE_BUILD_TYPE_HOST_OR_DEVICE_KHR:
617ec681f3Smrg      pSizeInfo->buildScratchSize = MAX2(cpu_build_scratch_size,
627ec681f3Smrg                                         gpu_build_scratch_size);
637ec681f3Smrg      pSizeInfo->updateScratchSize = MAX2(cpu_update_scratch_size,
647ec681f3Smrg                                          gpu_update_scratch_size);
657ec681f3Smrg      break;
667ec681f3Smrg
677ec681f3Smrg   default:
687ec681f3Smrg      unreachable("Invalid acceleration structure build type");
697ec681f3Smrg   }
707ec681f3Smrg}
717ec681f3Smrg
727ec681f3SmrgVkResult
737ec681f3Smrganv_CreateAccelerationStructureKHR(
747ec681f3Smrg    VkDevice                                    _device,
757ec681f3Smrg    const VkAccelerationStructureCreateInfoKHR* pCreateInfo,
767ec681f3Smrg    const VkAllocationCallbacks*                pAllocator,
777ec681f3Smrg    VkAccelerationStructureKHR*                 pAccelerationStructure)
787ec681f3Smrg{
797ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
807ec681f3Smrg   ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
817ec681f3Smrg   struct anv_acceleration_structure *accel;
827ec681f3Smrg
837ec681f3Smrg   accel = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*accel), 8,
847ec681f3Smrg                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
857ec681f3Smrg   if (accel == NULL)
867ec681f3Smrg      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
877ec681f3Smrg
887ec681f3Smrg   vk_object_base_init(&device->vk, &accel->base,
897ec681f3Smrg                       VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR);
907ec681f3Smrg
917ec681f3Smrg   accel->size = pCreateInfo->size;
927ec681f3Smrg   accel->address = anv_address_add(buffer->address, pCreateInfo->offset);
937ec681f3Smrg
947ec681f3Smrg   *pAccelerationStructure = anv_acceleration_structure_to_handle(accel);
957ec681f3Smrg
967ec681f3Smrg   return VK_SUCCESS;
977ec681f3Smrg}
987ec681f3Smrg
997ec681f3Smrgvoid
1007ec681f3Smrganv_DestroyAccelerationStructureKHR(
1017ec681f3Smrg    VkDevice                                    _device,
1027ec681f3Smrg    VkAccelerationStructureKHR                  accelerationStructure,
1037ec681f3Smrg    const VkAllocationCallbacks*                pAllocator)
1047ec681f3Smrg{
1057ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1067ec681f3Smrg   ANV_FROM_HANDLE(anv_acceleration_structure, accel, accelerationStructure);
1077ec681f3Smrg
1087ec681f3Smrg   if (!accel)
1097ec681f3Smrg      return;
1107ec681f3Smrg
1117ec681f3Smrg   vk_object_base_finish(&accel->base);
1127ec681f3Smrg   vk_free2(&device->vk.alloc, pAllocator, accel);
1137ec681f3Smrg}
1147ec681f3Smrg
1157ec681f3SmrgVkDeviceAddress
1167ec681f3Smrganv_GetAccelerationStructureDeviceAddressKHR(
1177ec681f3Smrg    VkDevice                                    device,
1187ec681f3Smrg    const VkAccelerationStructureDeviceAddressInfoKHR* pInfo)
1197ec681f3Smrg{
1207ec681f3Smrg   ANV_FROM_HANDLE(anv_acceleration_structure, accel,
1217ec681f3Smrg                   pInfo->accelerationStructure);
1227ec681f3Smrg
1237ec681f3Smrg   assert(!anv_address_is_null(accel->address));
1247ec681f3Smrg   assert(accel->address.bo->flags & EXEC_OBJECT_PINNED);
1257ec681f3Smrg
1267ec681f3Smrg   return anv_address_physical(accel->address);
1277ec681f3Smrg}
1287ec681f3Smrg
1297ec681f3Smrgvoid
1307ec681f3Smrganv_GetDeviceAccelerationStructureCompatibilityKHR(
1317ec681f3Smrg    VkDevice                                    device,
1327ec681f3Smrg    const VkAccelerationStructureVersionInfoKHR* pVersionInfo,
1337ec681f3Smrg    VkAccelerationStructureCompatibilityKHR*    pCompatibility)
1347ec681f3Smrg{
1357ec681f3Smrg   unreachable("Unimplemented");
1367ec681f3Smrg}
1377ec681f3Smrg
1387ec681f3SmrgVkResult
1397ec681f3Smrganv_BuildAccelerationStructuresKHR(
1407ec681f3Smrg    VkDevice                                    _device,
1417ec681f3Smrg    VkDeferredOperationKHR                      deferredOperation,
1427ec681f3Smrg    uint32_t                                    infoCount,
1437ec681f3Smrg    const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
1447ec681f3Smrg    const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos)
1457ec681f3Smrg{
1467ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1477ec681f3Smrg   unreachable("Unimplemented");
1487ec681f3Smrg   return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
1497ec681f3Smrg}
1507ec681f3Smrg
1517ec681f3SmrgVkResult
1527ec681f3Smrganv_CopyAccelerationStructureKHR(
1537ec681f3Smrg    VkDevice                                    _device,
1547ec681f3Smrg    VkDeferredOperationKHR                      deferredOperation,
1557ec681f3Smrg    const VkCopyAccelerationStructureInfoKHR*   pInfo)
1567ec681f3Smrg{
1577ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1587ec681f3Smrg   unreachable("Unimplemented");
1597ec681f3Smrg   return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
1607ec681f3Smrg}
1617ec681f3Smrg
1627ec681f3SmrgVkResult
1637ec681f3Smrganv_CopyAccelerationStructureToMemoryKHR(
1647ec681f3Smrg    VkDevice                                    _device,
1657ec681f3Smrg    VkDeferredOperationKHR                      deferredOperation,
1667ec681f3Smrg    const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo)
1677ec681f3Smrg{
1687ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1697ec681f3Smrg   unreachable("Unimplemented");
1707ec681f3Smrg   return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
1717ec681f3Smrg}
1727ec681f3Smrg
1737ec681f3SmrgVkResult
1747ec681f3Smrganv_CopyMemoryToAccelerationStructureKHR(
1757ec681f3Smrg    VkDevice                                    _device,
1767ec681f3Smrg    VkDeferredOperationKHR                      deferredOperation,
1777ec681f3Smrg    const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo)
1787ec681f3Smrg{
1797ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1807ec681f3Smrg   unreachable("Unimplemented");
1817ec681f3Smrg   return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
1827ec681f3Smrg}
1837ec681f3Smrg
1847ec681f3SmrgVkResult
1857ec681f3Smrganv_WriteAccelerationStructuresPropertiesKHR(
1867ec681f3Smrg    VkDevice                                    _device,
1877ec681f3Smrg    uint32_t                                    accelerationStructureCount,
1887ec681f3Smrg    const VkAccelerationStructureKHR*           pAccelerationStructures,
1897ec681f3Smrg    VkQueryType                                 queryType,
1907ec681f3Smrg    size_t                                      dataSize,
1917ec681f3Smrg    void*                                       pData,
1927ec681f3Smrg    size_t                                      stride)
1937ec681f3Smrg{
1947ec681f3Smrg   ANV_FROM_HANDLE(anv_device, device, _device);
1957ec681f3Smrg   unreachable("Unimplemented");
1967ec681f3Smrg   return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
1977ec681f3Smrg}
1987ec681f3Smrg
1997ec681f3Smrgvoid
2007ec681f3Smrganv_CmdBuildAccelerationStructuresKHR(
2017ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2027ec681f3Smrg    uint32_t                                    infoCount,
2037ec681f3Smrg    const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
2047ec681f3Smrg    const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos)
2057ec681f3Smrg{
2067ec681f3Smrg   unreachable("Unimplemented");
2077ec681f3Smrg}
2087ec681f3Smrg
2097ec681f3Smrgvoid
2107ec681f3Smrganv_CmdBuildAccelerationStructuresIndirectKHR(
2117ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2127ec681f3Smrg    uint32_t                                    infoCount,
2137ec681f3Smrg    const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
2147ec681f3Smrg    const VkDeviceAddress*                      pIndirectDeviceAddresses,
2157ec681f3Smrg    const uint32_t*                             pIndirectStrides,
2167ec681f3Smrg    const uint32_t* const*                      ppMaxPrimitiveCounts)
2177ec681f3Smrg{
2187ec681f3Smrg   unreachable("Unimplemented");
2197ec681f3Smrg}
2207ec681f3Smrg
2217ec681f3Smrgvoid
2227ec681f3Smrganv_CmdCopyAccelerationStructureKHR(
2237ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2247ec681f3Smrg    const VkCopyAccelerationStructureInfoKHR*   pInfo)
2257ec681f3Smrg{
2267ec681f3Smrg   unreachable("Unimplemented");
2277ec681f3Smrg}
2287ec681f3Smrg
2297ec681f3Smrgvoid
2307ec681f3Smrganv_CmdCopyAccelerationStructureToMemoryKHR(
2317ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2327ec681f3Smrg    const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo)
2337ec681f3Smrg{
2347ec681f3Smrg   unreachable("Unimplemented");
2357ec681f3Smrg}
2367ec681f3Smrg
2377ec681f3Smrgvoid
2387ec681f3Smrganv_CmdCopyMemoryToAccelerationStructureKHR(
2397ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2407ec681f3Smrg    const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo)
2417ec681f3Smrg{
2427ec681f3Smrg   unreachable("Unimplemented");
2437ec681f3Smrg}
2447ec681f3Smrg
2457ec681f3Smrgvoid
2467ec681f3Smrganv_CmdWriteAccelerationStructuresPropertiesKHR(
2477ec681f3Smrg    VkCommandBuffer                             commandBuffer,
2487ec681f3Smrg    uint32_t                                    accelerationStructureCount,
2497ec681f3Smrg    const VkAccelerationStructureKHR*           pAccelerationStructures,
2507ec681f3Smrg    VkQueryType                                 queryType,
2517ec681f3Smrg    VkQueryPool                                 queryPool,
2527ec681f3Smrg    uint32_t                                    firstQuery)
2537ec681f3Smrg{
2547ec681f3Smrg   unreachable("Unimplemented");
2557ec681f3Smrg}
256