Optimize the AS build tests to not use host uncached memory
authorSlawomir Cygan <slawomir.cygan@intel.com>
Tue, 11 May 2021 14:53:20 +0000 (16:53 +0200)
committerAlexander Galazin <Alexander.Galazin@arm.com>
Fri, 11 Jun 2021 09:25:00 +0000 (09:25 +0000)
Use userspace host memory for scratch buffer (instead mapped device memory)
and try using HOST_CACHED memory (if supported) for the accelleration structure.

Components: Vulkan

VK-GL-CTS Issue: 2922

Affects:
dEQP-VK.ray_tracing_pipeline.build.*
dEQP-VK.ray_tracing_pipeline.*

Change-Id: I7592c3f93f2e10766cd1276f37ca51924e1cc16f

external/vulkancts/framework/vulkan/vkRayTracingUtil.cpp

index 2c24dd9..e5601bf 100644 (file)
@@ -538,7 +538,15 @@ SerialStorage::SerialStorage (const DeviceInterface&                                                                       vk,
        , m_storageSize(storageSize)
 {
        const VkBufferCreateInfo        bufferCreateInfo        = makeBufferCreateInfo(storageSize, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-       m_buffer                                                                                = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+       try
+       {
+               m_buffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::Cached | MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+       }
+       catch (const tcu::NotSupportedError&)
+       {
+               // retry without Cached flag
+               m_buffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+       }
 }
 
 VkDeviceOrHostAddressKHR SerialStorage::getAddress (const DeviceInterface&                             vk,
@@ -812,7 +820,8 @@ protected:
        de::MovePtr<BufferWithMemory>                                                   m_accelerationStructureBuffer;
        de::MovePtr<BufferWithMemory>                                                   m_vertexBuffer;
        de::MovePtr<BufferWithMemory>                                                   m_indexBuffer;
-       de::MovePtr<BufferWithMemory>                                                   m_scratchBuffer;
+       de::MovePtr<BufferWithMemory>                                                   m_deviceScratchBuffer;
+       std::vector<deUint8>                                                                    m_hostScratchBuffer;
        Move<VkAccelerationStructureKHR>                                                m_accelerationStructureKHR;
        VkBuffer                                                                                                m_indirectBuffer;
        VkDeviceSize                                                                                    m_indirectBufferOffset;
@@ -831,7 +840,7 @@ deUint32 BottomLevelAccelerationStructureKHR::getRequiredAllocationCount (void)
        /*
                de::MovePtr<BufferWithMemory>                                                   m_geometryBuffer; // but only when m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR
                de::MovePtr<Allocation>                                                                 m_accelerationStructureAlloc;
-               de::MovePtr<BufferWithMemory>                                                   m_scratchBuffer;
+               de::MovePtr<BufferWithMemory>                                                   m_deviceScratchBuffer;
        */
        return 3u;
 }
@@ -854,7 +863,7 @@ BottomLevelAccelerationStructureKHR::BottomLevelAccelerationStructureKHR ()
        , m_accelerationStructureBuffer         (DE_NULL)
        , m_vertexBuffer                                        (DE_NULL)
        , m_indexBuffer                                         (DE_NULL)
-       , m_scratchBuffer                                       (DE_NULL)
+       , m_deviceScratchBuffer                         (DE_NULL)
        , m_accelerationStructureKHR            ()
        , m_indirectBuffer                                      (DE_NULL)
        , m_indirectBufferOffset                        (0)
@@ -977,7 +986,15 @@ void BottomLevelAccelerationStructureKHR::create (const DeviceInterface&                           vk,
 
        {
                const VkBufferCreateInfo                bufferCreateInfo        = makeBufferCreateInfo(m_structureSize, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-               m_accelerationStructureBuffer                                           = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               try
+               {
+                       m_accelerationStructureBuffer                                           = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::Cached | MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
+               catch (const tcu::NotSupportedError&)
+               {
+                       // retry without Cached flag
+                       m_accelerationStructureBuffer                                           = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
        }
 
        {
@@ -1001,8 +1018,15 @@ void BottomLevelAccelerationStructureKHR::create (const DeviceInterface&                         vk,
 
        if (m_buildScratchSize > 0u)
        {
-               const VkBufferCreateInfo                bufferCreateInfo        = makeBufferCreateInfo(m_buildScratchSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-               m_scratchBuffer                                                                         = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               if (m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
+               {
+                       const VkBufferCreateInfo                bufferCreateInfo = makeBufferCreateInfo(m_buildScratchSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
+                       m_deviceScratchBuffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
+               else
+               {
+                       m_hostScratchBuffer.resize(static_cast<size_t>(m_buildScratchSize));
+               }
        }
 
        if (m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR && !m_geometriesData.empty())
@@ -1038,8 +1062,8 @@ void BottomLevelAccelerationStructureKHR::build (const DeviceInterface&                                           vk,
                const VkAccelerationStructureGeometryKHR*                       accelerationStructureGeometriesKHRPointer       = accelerationStructureGeometriesKHR.data();
                const VkAccelerationStructureGeometryKHR* const*        accelerationStructureGeometry                           = accelerationStructureGeometriesKHRPointers.data();
                VkDeviceOrHostAddressKHR                                                        scratchData                                                                     = (m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
-                                                                                                                                                                                                               ? makeDeviceOrHostAddressKHR(vk, device, m_scratchBuffer->get(), 0)
-                                                                                                                                                                                                               : makeDeviceOrHostAddressKHR(m_scratchBuffer->getAllocation().getHostPtr());
+                                                                                                                                                                                                               ? makeDeviceOrHostAddressKHR(vk, device, m_deviceScratchBuffer->get(), 0)
+                                                                                                                                                                                                               : makeDeviceOrHostAddressKHR(m_hostScratchBuffer.data());
                const deUint32                                                                          geometryCount                                                           = (m_buildWithoutGeometries
                                                                                                                                                                                                                ? 0u
                                                                                                                                                                                                                : static_cast<deUint32>(accelerationStructureGeometriesKHR.size()));
@@ -1461,7 +1485,15 @@ BufferWithMemory* createInstanceBuffer (const DeviceInterface&                                                                                   vk,
 
        const VkDeviceSize                      bufferSizeBytes         = bottomLevelInstances.size() * sizeof(VkAccelerationStructureInstanceKHR);
        const VkBufferCreateInfo        bufferCreateInfo        = makeBufferCreateInfo(bufferSizeBytes, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-       return new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress);
+       try
+       {
+               return new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::Cached | MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress);
+       }
+       catch (const tcu::NotSupportedError&)
+       {
+               // retry without Cached flag
+               return new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress);
+       }
 }
 
 void updateInstanceBuffer (const DeviceInterface&                                                                                      vk,
@@ -1589,7 +1621,8 @@ protected:
        de::MovePtr<BufferWithMemory>                                                   m_accelerationStructureBuffer;
        de::MovePtr<BufferWithMemory>                                                   m_instanceBuffer;
        de::MovePtr<BufferWithMemory>                                                   m_instanceAddressBuffer;
-       de::MovePtr<BufferWithMemory>                                                   m_scratchBuffer;
+       de::MovePtr<BufferWithMemory>                                                   m_deviceScratchBuffer;
+       std::vector<deUint8>                                                                    m_hostScratchBuffer;
        Move<VkAccelerationStructureKHR>                                                m_accelerationStructureKHR;
        VkBuffer                                                                                                m_indirectBuffer;
        VkDeviceSize                                                                                    m_indirectBufferOffset;
@@ -1606,7 +1639,7 @@ deUint32 TopLevelAccelerationStructureKHR::getRequiredAllocationCount (void)
        /*
                de::MovePtr<BufferWithMemory>                                                   m_instanceBuffer;
                de::MovePtr<Allocation>                                                                 m_accelerationStructureAlloc;
-               de::MovePtr<BufferWithMemory>                                                   m_scratchBuffer;
+               de::MovePtr<BufferWithMemory>                                                   m_deviceScratchBuffer;
        */
        return 3u;
 }
@@ -1625,7 +1658,7 @@ TopLevelAccelerationStructureKHR::TopLevelAccelerationStructureKHR ()
        , m_accelerationStructureBuffer (DE_NULL)
        , m_instanceBuffer                              (DE_NULL)
        , m_instanceAddressBuffer               (DE_NULL)
-       , m_scratchBuffer                               (DE_NULL)
+       , m_deviceScratchBuffer                 (DE_NULL)
        , m_accelerationStructureKHR    ()
        , m_indirectBuffer                              (DE_NULL)
        , m_indirectBufferOffset                (0)
@@ -1748,7 +1781,15 @@ void TopLevelAccelerationStructureKHR::create (const DeviceInterface&                            vk,
 
        {
                const VkBufferCreateInfo                bufferCreateInfo = makeBufferCreateInfo(m_structureSize, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-               m_accelerationStructureBuffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               try
+               {
+                       m_accelerationStructureBuffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::Cached | MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
+               catch (const tcu::NotSupportedError&)
+               {
+                       // retry without Cached flag
+                       m_accelerationStructureBuffer = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
        }
 
        {
@@ -1772,8 +1813,15 @@ void TopLevelAccelerationStructureKHR::create (const DeviceInterface&                            vk,
 
        if (m_buildScratchSize > 0u)
        {
-               const VkBufferCreateInfo                bufferCreateInfo        = makeBufferCreateInfo(m_buildScratchSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
-               m_scratchBuffer                                                                         = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               if (m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
+               {
+                       const VkBufferCreateInfo                bufferCreateInfo        = makeBufferCreateInfo(m_buildScratchSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
+                       m_deviceScratchBuffer                                                           = de::MovePtr<BufferWithMemory>(new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress));
+               }
+               else
+               {
+                       m_hostScratchBuffer.resize(static_cast<size_t>(m_buildScratchSize));
+               }
        }
 
        if (m_useArrayOfPointers)
@@ -1801,8 +1849,8 @@ void TopLevelAccelerationStructureKHR::build (const DeviceInterface&      vk,
        prepareInstances(vk, device, accelerationStructureGeometryKHR, maxPrimitiveCounts);
 
        VkDeviceOrHostAddressKHR                                scratchData                                                                             = (m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
-                                                                                                                                                                                       ? makeDeviceOrHostAddressKHR(vk, device, m_scratchBuffer->get(), 0)
-                                                                                                                                                                                       : makeDeviceOrHostAddressKHR(m_scratchBuffer->getAllocation().getHostPtr());
+                                                                                                                                                                                       ? makeDeviceOrHostAddressKHR(vk, device, m_deviceScratchBuffer->get(), 0)
+                                                                                                                                                                                       : makeDeviceOrHostAddressKHR(m_hostScratchBuffer.data());
 
        VkAccelerationStructureBuildGeometryInfoKHR accelerationStructureBuildGeometryInfoKHR           =
        {