Test instance matrix update after command recording
authorRicardo Garcia <rgarcia@igalia.com>
Wed, 9 Jun 2021 07:42:57 +0000 (09:42 +0200)
committerRicardo Garcia <rgarcia@igalia.com>
Wed, 9 Jun 2021 07:42:57 +0000 (09:42 +0200)
This commit modifies ray query and ray tracing direction length tests so
the instance transform matrix in the top level acceleration structure is
sometimes updated after the AS build commands have been recorded in the
command buffer.

Affected tests:
dEQP-VK.ray_tracing_pipeline.direction_length.*
dEQP-VK.ray_tracing_pipeline.inside_aabbs.*
dEQP-VK.ray_query.direction_length.*
dEQP-VK.ray_query.inside_aabbs.*

Components: Vulkan
VK-GL-CTS issue: 2881

Change-Id: I9051783443e3775e20ccaa0eab32c5bc2f21d165

external/vulkancts/framework/vulkan/vkRayTracingUtil.cpp
external/vulkancts/framework/vulkan/vkRayTracingUtil.hpp
external/vulkancts/modules/vulkan/ray_query/vktRayQueryDirectionTests.cpp
external/vulkancts/modules/vulkan/ray_tracing/vktRayTracingDirectionTests.cpp

index 688b809917827ce1bd0512ab4d2fb95d45f79ae7..7bf18557caa5be71fad8ecbd547a4c6742208f5f 100644 (file)
@@ -1479,65 +1479,74 @@ BufferWithMemory* createInstanceBuffer (const DeviceInterface&                                                                                  vk,
        return new BufferWithMemory(vk, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible | MemoryRequirement::Coherent | MemoryRequirement::DeviceAddress);
 }
 
-void updateInstanceBuffer (const DeviceInterface&                                                                                      vk,
-                                                  const VkDevice                                                                                                       device,
-                                                  std::vector<de::SharedPtr<BottomLevelAccelerationStructure> >        bottomLevelInstances,
-                                                  std::vector<InstanceData>                                                                            instanceData,
-                                                  BufferWithMemory*                                                                                            instanceBuffer,
-                                                  VkAccelerationStructureBuildTypeKHR                                                          buildType,
-                                                  bool                                                                                                                         inactiveInstances)
-{
-       DE_ASSERT(bottomLevelInstances.size() != 0);
-       DE_ASSERT(bottomLevelInstances.size() == instanceData.size());
-
-       const Allocation&                       instancesAlloc          = instanceBuffer->getAllocation();
-
-       deUint8*                                        bufferStart                     = static_cast<deUint8*>(instancesAlloc.getHostPtr());
-       VkDeviceSize                            bufferOffset            = 0;
+void updateSingleInstance (const DeviceInterface&                                      vk,
+                                                  const VkDevice                                                       device,
+                                                  const BottomLevelAccelerationStructure&      bottomLevelAccelerationStructure,
+                                                  const InstanceData&                                          instanceData,
+                                                  deUint8*                                                                     bufferLocation,
+                                                  VkAccelerationStructureBuildTypeKHR          buildType,
+                                                  bool                                                                         inactiveInstances)
+{
+       const VkAccelerationStructureKHR accelerationStructureKHR = *bottomLevelAccelerationStructure.getPtr();
+
+       // This part needs to be fixed once a new version of the VkAccelerationStructureInstanceKHR will be added to vkStructTypes.inl
+       VkDeviceAddress accelerationStructureAddress;
+       if (buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
+       {
+               VkAccelerationStructureDeviceAddressInfoKHR asDeviceAddressInfo =
+               {
+                       VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,       // VkStructureType                              sType;
+                       DE_NULL,                                                                                                                        // const void*                                  pNext;
+                       accelerationStructureKHR                                                                                        // VkAccelerationStructureKHR   accelerationStructure;
+               };
+               accelerationStructureAddress = vk.getAccelerationStructureDeviceAddressKHR(device, &asDeviceAddressInfo);
+       }
 
-       for (size_t instanceNdx = 0; instanceNdx < bottomLevelInstances.size(); ++instanceNdx)
+       deUint64 structureReference;
+       if (inactiveInstances)
        {
-               const BottomLevelAccelerationStructure&         bottomLevelAccelerationStructure        = *bottomLevelInstances[instanceNdx];
-               const VkAccelerationStructureKHR                        accelerationStructureKHR                        = *bottomLevelAccelerationStructure.getPtr();
+               // Instances will be marked inactive by making their references VK_NULL_HANDLE or having address zero.
+               structureReference = 0ull;
+       }
+       else
+       {
+               structureReference      = (buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
+                                                       ? deUint64(accelerationStructureAddress)
+                                                       : deUint64(accelerationStructureKHR.getInternal());
+       }
 
-               // This part needs to be fixed once a new version of the VkAccelerationStructureInstanceKHR will be added to vkStructTypes.inl
-               VkDeviceAddress accelerationStructureAddress;
-               if (buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
-               {
-                       VkAccelerationStructureDeviceAddressInfoKHR asDeviceAddressInfo =
-                       {
-                               VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,       // VkStructureType                              sType;
-                               DE_NULL,                                                                                                                        // const void*                                  pNext;
-                               accelerationStructureKHR                                                                                        // VkAccelerationStructureKHR   accelerationStructure;
-                       };
-                       accelerationStructureAddress = vk.getAccelerationStructureDeviceAddressKHR(device, &asDeviceAddressInfo);
-               }
+       VkAccelerationStructureInstanceKHR      accelerationStructureInstanceKHR = makeVkAccelerationStructureInstanceKHR
+       (
+               instanceData.matrix,                                                                    //  VkTransformMatrixKHR                transform;
+               instanceData.instanceCustomIndex,                                               //  deUint32                                    instanceCustomIndex:24;
+               instanceData.mask,                                                                              //  deUint32                                    mask:8;
+               instanceData.instanceShaderBindingTableRecordOffset,    //  deUint32                                    instanceShaderBindingTableRecordOffset:24;
+               instanceData.flags,                                                                             //  VkGeometryInstanceFlagsKHR  flags:8;
+               structureReference                                                                              //  deUint64                                    accelerationStructureReference;
+       );
 
-               deUint64 structureReference;
-               if (inactiveInstances)
-               {
-                       // Instances will be marked inactive by making their references VK_NULL_HANDLE or having address zero.
-                       structureReference = 0ull;
-               }
-               else
-               {
-                       structureReference      = (buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR)
-                                                               ? deUint64(accelerationStructureAddress)
-                                                               : deUint64(accelerationStructureKHR.getInternal());
-               }
+       deMemcpy(bufferLocation, &accelerationStructureInstanceKHR, sizeof(VkAccelerationStructureInstanceKHR));
+}
 
-               VkAccelerationStructureInstanceKHR      accelerationStructureInstanceKHR = makeVkAccelerationStructureInstanceKHR
-               (
-                       instanceData[instanceNdx].matrix,                                                                                                       //  VkTransformMatrixKHR                transform;
-                       instanceData[instanceNdx].instanceCustomIndex,                                                                          //  deUint32                                    instanceCustomIndex:24;
-                       instanceData[instanceNdx].mask,                                                                                                         //  deUint32                                    mask:8;
-                       instanceData[instanceNdx].instanceShaderBindingTableRecordOffset,                                       //  deUint32                                    instanceShaderBindingTableRecordOffset:24;
-                       instanceData[instanceNdx].flags,                                                                                                        //  VkGeometryInstanceFlagsKHR  flags:8;
-                       structureReference                                                                                                                                      //  deUint64                                    accelerationStructureReference;
-               );
+void updateInstanceBuffer (const DeviceInterface&                                                                                              vk,
+                                                  const VkDevice                                                                                                               device,
+                                                  const std::vector<de::SharedPtr<BottomLevelAccelerationStructure>>&  bottomLevelInstances,
+                                                  const std::vector<InstanceData>&                                                                             instanceData,
+                                                  const BufferWithMemory*                                                                                              instanceBuffer,
+                                                  VkAccelerationStructureBuildTypeKHR                                                                  buildType,
+                                                  bool                                                                                                                                 inactiveInstances)
+{
+       DE_ASSERT(bottomLevelInstances.size() != 0);
+       DE_ASSERT(bottomLevelInstances.size() == instanceData.size());
 
-               deMemcpy(&bufferStart[bufferOffset], &accelerationStructureInstanceKHR, sizeof(VkAccelerationStructureInstanceKHR));
+       auto&                   instancesAlloc          = instanceBuffer->getAllocation();
+       auto                    bufferStart                     = reinterpret_cast<deUint8*>(instancesAlloc.getHostPtr());
+       VkDeviceSize    bufferOffset            = 0ull;
 
+       for (size_t instanceNdx = 0; instanceNdx < bottomLevelInstances.size(); ++instanceNdx)
+       {
+               const auto& blas = *bottomLevelInstances[instanceNdx];
+               updateSingleInstance(vk, device, blas, instanceData[instanceNdx], bufferStart + bufferOffset, buildType, inactiveInstances);
                bufferOffset += sizeof(VkAccelerationStructureInstanceKHR);
        }
 
@@ -1592,6 +1601,11 @@ public:
 
        const VkAccelerationStructureKHR*                                               getPtr                                                                                          (void) const override;
 
+       void                                                                                                    updateInstanceMatrix                                                            (const DeviceInterface&                                                 vk,
+                                                                                                                                                                                                                                const VkDevice                                                                 device,
+                                                                                                                                                                                                                                size_t                                                                                 instanceIndex,
+                                                                                                                                                                                                                                const VkTransformMatrixKHR&                                    matrix) override;
+
 protected:
        VkAccelerationStructureBuildTypeKHR                                             m_buildType;
        VkAccelerationStructureCreateFlagsKHR                                   m_createFlags;
@@ -1810,6 +1824,23 @@ void TopLevelAccelerationStructureKHR::create (const DeviceInterface&                            vk,
                m_instanceBuffer = de::MovePtr<BufferWithMemory>(createInstanceBuffer(vk, device, allocator, m_bottomLevelInstances, m_instanceData));
 }
 
+void TopLevelAccelerationStructureKHR::updateInstanceMatrix (const DeviceInterface& vk, const VkDevice device, size_t instanceIndex, const VkTransformMatrixKHR& matrix)
+{
+       DE_ASSERT(m_buildType == VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR);
+       DE_ASSERT(instanceIndex < m_bottomLevelInstances.size());
+       DE_ASSERT(instanceIndex < m_instanceData.size());
+
+       const auto&             blas                    = *m_bottomLevelInstances[instanceIndex];
+       auto&                   instanceData    = m_instanceData[instanceIndex];
+       auto&                   instancesAlloc  = m_instanceBuffer->getAllocation();
+       auto                    bufferStart             = reinterpret_cast<deUint8*>(instancesAlloc.getHostPtr());
+       VkDeviceSize    bufferOffset    = sizeof(VkAccelerationStructureInstanceKHR) * instanceIndex;
+
+       instanceData.matrix = matrix;
+       updateSingleInstance(vk, device, blas, instanceData, bufferStart + bufferOffset, m_buildType, m_inactiveInstances);
+       flushMappedMemoryRange(vk, device, instancesAlloc.getMemory(), instancesAlloc.getOffset(), VK_WHOLE_SIZE);
+}
+
 void TopLevelAccelerationStructureKHR::build (const DeviceInterface&   vk,
                                                                                          const VkDevice                        device,
                                                                                          const VkCommandBuffer         cmdBuffer)
index 8bca3d840a7be7fd213b43d7917b4066a432dcd7..a6a58e7d86ea2470b97ed824d5ea2b7f4f6fcb08 100644 (file)
@@ -682,6 +682,11 @@ public:
 
        virtual const VkAccelerationStructureKHR*                                               getPtr                                                          (void) const = DE_NULL;
 
+       virtual void                                                                                                    updateInstanceMatrix                            (const DeviceInterface&                                         vk,
+                                                                                                                                                                                                                const VkDevice                                                         device,
+                                                                                                                                                                                                                size_t                                                                         instanceIndex,
+                                                                                                                                                                                                                const VkTransformMatrixKHR&                            matrix) = 0;
+
 protected:
        std::vector<de::SharedPtr<BottomLevelAccelerationStructure> >   m_bottomLevelInstances;
        std::vector<InstanceData>                                                                               m_instanceData;
index f0b86a6c5fad7cd0f0aeadd4f9c348c745b09ddc..0fc3545de5e04eba8caecba8d36b81f2febbcc4e 100644 (file)
@@ -213,6 +213,7 @@ struct TestParams
        float                                   rotationY;
        VkGeometryTypeKHR               geometryType;
        bool                                    useArraysOfPointers;
+       bool                                    updateMatrixAfterBuild;
        RayOriginType                   rayOriginType;
        RayEndType                              rayEndtype;
 };
@@ -358,8 +359,13 @@ tcu::TestStatus DirectionTestInstance::iterate (void)
        topLevelAS->setUseArrayOfPointers(m_params.useArraysOfPointers);
        topLevelAS->setUsePPGeometries(m_params.useArraysOfPointers);
        topLevelAS->setInstanceCount(1);
-       topLevelAS->addInstance(blasSharedPtr, transformMatrix, 0, 0xFFu, 0u, instanceFlags);
+       {
+               const auto& initialMatrix = (m_params.updateMatrixAfterBuild ? identityMatrix3x4 : transformMatrix);
+               topLevelAS->addInstance(blasSharedPtr, initialMatrix, 0, 0xFFu, 0u, instanceFlags);
+       }
        topLevelAS->createAndBuild(vkd, device, cmdBuffer, alloc);
+       if (m_params.updateMatrixAfterBuild)
+               topLevelAS->updateInstanceMatrix(vkd, device, 0u, transformMatrix);
 
        // Create output buffer.
        const auto                      bufferSize                      = static_cast<VkDeviceSize>(sizeof(float));
@@ -577,10 +583,14 @@ tcu::TestCaseGroup*       createDirectionLengthTests (tcu::TestContext& testCtx)
                                        angles.first,                           //              float                                   rotationX;
                                        angles.second,                          //              float                                   rotationY;
                                        geometryType,                           //              VkGeometryTypeKHR               geometryType;
-                                       (caseCounter++ % 2u == 0u),     //              bool                                    useArraysOfPointers;
+                                       // Use arrays of pointers when building the TLAS in every other test.
+                                       (caseCounter % 2u == 0u),       //              bool                                    useArraysOfPointers;
+                                       // Sometimes, update matrix after building the lop level AS and before submitting the command buffer.
+                                       (caseCounter % 3u == 0u),       //              bool                                    updateMatrixAfterBuild;
                                        rayOrigType,                            //              RayOriginType                   rayOriginType;
                                        rayEndType,                                     //              RayEndType                              rayEndType;
                                };
+                               ++caseCounter;
 
                                factorGroup->addChild(new DirectionTestCase(testCtx, angleName, "", params));
                        }
@@ -646,6 +656,7 @@ tcu::TestCaseGroup* createInsideAABBsTests (tcu::TestContext& testCtx)
                                        angles.second,                  //              float                                   rotationY;
                                        geometryType,                   //              VkGeometryTypeKHR               geometryType;
                                        false,                                  //              bool                                    useArraysOfPointers;
+                                       false,                                  //              bool                                    updateMatrixAfterBuild;
                                        rayOrigType,                    //              RayOriginType                   rayOriginType;
                                        rayEndCase.rayEndType,  //              RayEndType                              rayEndType;
                                };
index d9c58d813dcc6b6674898d7aa0cf29a07655b8fd..b43ea252af602947a52bdd566fef108af7682149 100644 (file)
@@ -214,6 +214,7 @@ struct TestParams
        VkShaderStageFlagBits   testStage;
        VkGeometryTypeKHR               geometryType;
        bool                                    useArraysOfPointers;
+       bool                                    updateMatrixAfterBuild;
        RayOriginType                   rayOriginType;
        RayEndType                              rayEndtype;
 
@@ -440,8 +441,13 @@ tcu::TestStatus DirectionTestInstance::iterate (void)
        topLevelAS->setUseArrayOfPointers(m_params.useArraysOfPointers);
        topLevelAS->setUsePPGeometries(m_params.useArraysOfPointers);
        topLevelAS->setInstanceCount(1);
-       topLevelAS->addInstance(blasSharedPtr, transformMatrix, 0, 0xFFu, 0u, instanceFlags);
+       {
+               const auto& initialMatrix = (m_params.updateMatrixAfterBuild ? identityMatrix3x4 : transformMatrix);
+               topLevelAS->addInstance(blasSharedPtr, initialMatrix, 0, 0xFFu, 0u, instanceFlags);
+       }
        topLevelAS->createAndBuild(vkd, device, cmdBuffer, alloc);
+       if (m_params.updateMatrixAfterBuild)
+               topLevelAS->updateInstanceMatrix(vkd, device, 0u, transformMatrix);
 
        // Create output buffer.
        const auto                      bufferSize                      = static_cast<VkDeviceSize>(sizeof(float));
@@ -733,10 +739,13 @@ tcu::TestCaseGroup*       createDirectionLengthTests(tcu::TestContext& testCtx)
                                                stageData.hitStage,                     //              VkShaderStageFlagBits   hitStage;
                                                geometryType,                           //              VkGeometryTypeKHR               geometryType;
                                                // Use arrays of pointers when building the TLAS in every other test.
-                                               (caseCounter++ % 2u == 0u),     //              bool                                    useArraysOfPointers;
+                                               (caseCounter % 2u == 0u),       //              bool                                    useArraysOfPointers;
+                                               // Sometimes, update matrix after building the lop level AS and before submitting the command buffer.
+                                               (caseCounter % 3u == 0u),       //              bool                                    updateMatrixAfterBuild;
                                                rayOrigType,                            //              RayOriginType                   rayOriginType;
                                                rayEndType,                                     //              RayEndType                              rayEndType;
                                        };
+                                       ++caseCounter;
 
                                        factorGroup->addChild(new DirectionTestCase(testCtx, angleName, "", params));
                                }
@@ -821,6 +830,7 @@ tcu::TestCaseGroup* createInsideAABBsTests(tcu::TestContext& testCtx)
                                                stageData.hitStage,                             //              VkShaderStageFlagBits   hitStage;
                                                geometryType,                                   //              VkGeometryTypeKHR               geometryType;
                                                false,                                                  //              bool                                    useArraysOfPointers;
+                                               false,                                                  //              bool                                    updateMatrixAfterBuild;
                                                rayOrigType,                                    //              RayOriginType                   rayOriginType;
                                                rayEndCase.rayEndType,                  //              RayEndType                              rayEndType;
                                        };