VK_KHR_dedicated_allocation: Extend pipeline.image
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / pipeline / vktPipelineImageUtil.cpp
index ed5ba91..0bc6922 100644 (file)
@@ -238,40 +238,11 @@ de::MovePtr<tcu::TextureLevel> readColorAttachment (const vk::DeviceInterface&    v
        }
 
        // Create command pool and buffer
-       {
-               const VkCommandPoolCreateInfo cmdPoolParams =
-               {
-                       VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,             // VkStructureType              sType;
-                       DE_NULL,                                                                                // const void*                  pNext;
-                       VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,                   // VkCmdPoolCreateFlags flags;
-                       queueFamilyIndex,                                                               // deUint32                             queueFamilyIndex;
-               };
-
-               cmdPool = createCommandPool(vk, device, &cmdPoolParams);
-
-               const VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
-               {
-                       VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
-                       DE_NULL,                                                                                // const void*                          pNext;
-                       *cmdPool,                                                                               // VkCommandPool                        commandPool;
-                       VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel         level;
-                       1u                                                                                              // deUint32                                     bufferCount;
-               };
-
-               cmdBuffer = allocateCommandBuffer(vk, device, &cmdBufferAllocateInfo);
-       }
+       cmdPool         = createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
+       cmdBuffer       = allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
 
        // Create fence
-       {
-               const VkFenceCreateInfo fenceParams =
-               {
-                       VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,            // VkStructureType              sType;
-                       DE_NULL,                                                                        // const void*                  pNext;
-                       0u                                                                                      // VkFenceCreateFlags   flags;
-               };
-
-               fence = createFence(vk, device, &fenceParams);
-       }
+       fence = createFence(vk, device);
 
        // Barriers for copying image to buffer
 
@@ -351,7 +322,7 @@ de::MovePtr<tcu::TextureLevel> readColorAttachment (const vk::DeviceInterface&      v
        VK_CHECK(vk.waitForFences(device, 1, &fence.get(), 0, ~(0ull) /* infinity */));
 
        // Read buffer data
-       invalidateMappedMemoryRange(vk, device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), pixelDataSize);
+       invalidateMappedMemoryRange(vk, device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), VK_WHOLE_SIZE);
        tcu::copy(*resultLevel, tcu::ConstPixelBufferAccess(resultLevel->getFormat(), resultLevel->getSize(), bufferAlloc->getHostPtr()));
 
        return resultLevel;
@@ -378,13 +349,15 @@ VkImageAspectFlags getImageAspectFlags (const tcu::TextureFormat textureFormat)
 
 } // anonymous
 
-void uploadTestTexture (const DeviceInterface&                 vk,
-                                               VkDevice                                                device,
-                                               VkQueue                                                 queue,
-                                               deUint32                                                queueFamilyIndex,
-                                               Allocator&                                              allocator,
-                                               const TestTexture&                              srcTexture,
-                                               VkImage                                                 destImage)
+void uploadTestTextureInternal (const DeviceInterface&                 vk,
+                                                               VkDevice                                                device,
+                                                               VkQueue                                                 queue,
+                                                               deUint32                                                queueFamilyIndex,
+                                                               Allocator&                                              allocator,
+                                                               const TestTexture&                              srcTexture,
+                                                               const TestTexture*                              srcStencilTexture,
+                                                               tcu::TextureFormat                              format,
+                                                               VkImage                                                 destImage)
 {
        deUint32                                                bufferSize;
        Move<VkBuffer>                                  buffer;
@@ -392,11 +365,21 @@ void uploadTestTexture (const DeviceInterface&                    vk,
        Move<VkCommandPool>                             cmdPool;
        Move<VkCommandBuffer>                   cmdBuffer;
        Move<VkFence>                                   fence;
-       const VkImageAspectFlags                imageAspectFlags        = getImageAspectFlags(srcTexture.getTextureFormat());
+       const VkImageAspectFlags                imageAspectFlags        = getImageAspectFlags(format);
+       deUint32                                                stencilOffset           = 0u;
 
        // Calculate buffer size
        bufferSize =  (srcTexture.isCompressed())? srcTexture.getCompressedSize(): srcTexture.getSize();
 
+       // Stencil-only texture should be provided if (and only if) the image has a combined DS format
+       DE_ASSERT((tcu::hasDepthComponent(format.order) && tcu::hasStencilComponent(format.order)) == (srcStencilTexture != DE_NULL));
+
+       if (srcStencilTexture != DE_NULL)
+       {
+               stencilOffset   = static_cast<deUint32>(deAlign32(static_cast<deInt32>(bufferSize), 4));
+               bufferSize              = stencilOffset + srcStencilTexture->getSize();
+       }
+
        // Create source buffer
        {
                const VkBufferCreateInfo bufferParams =
@@ -417,40 +400,11 @@ void uploadTestTexture (const DeviceInterface&                    vk,
        }
 
        // Create command pool and buffer
-       {
-               const VkCommandPoolCreateInfo cmdPoolParams =
-               {
-                       VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,             // VkStructureType                      sType;
-                       DE_NULL,                                                                                // const void*                          pNext;
-                       VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,                   // VkCommandPoolCreateFlags     flags;
-                       queueFamilyIndex,                                                               // deUint32                                     queueFamilyIndex;
-               };
-
-               cmdPool = createCommandPool(vk, device, &cmdPoolParams);
-
-               const VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
-               {
-                       VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
-                       DE_NULL,                                                                                // const void*                          pNext;
-                       *cmdPool,                                                                               // VkCommandPool                        commandPool;
-                       VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel         level;
-                       1u,                                                                                             // deUint32                                     bufferCount;
-               };
-
-               cmdBuffer = allocateCommandBuffer(vk, device, &cmdBufferAllocateInfo);
-       }
+       cmdPool         = createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
+       cmdBuffer       = allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
 
        // Create fence
-       {
-               const VkFenceCreateInfo fenceParams =
-               {
-                       VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,            // VkStructureType              sType;
-                       DE_NULL,                                                                        // const void*                  pNext;
-                       0u                                                                                      // VkFenceCreateFlags   flags;
-               };
-
-               fence = createFence(vk, device, &fenceParams);
-       }
+       fence = createFence(vk, device);
 
        // Barriers for copying buffer to image
        const VkBufferMemoryBarrier preBufferBarrier =
@@ -514,11 +468,28 @@ void uploadTestTexture (const DeviceInterface&                    vk,
                (const VkCommandBufferInheritanceInfo*)DE_NULL,
        };
 
-       const std::vector<VkBufferImageCopy>    copyRegions             = srcTexture.getBufferCopyRegions();
+       std::vector<VkBufferImageCopy>  copyRegions             = srcTexture.getBufferCopyRegions();
 
        // Write buffer data
        srcTexture.write(reinterpret_cast<deUint8*>(bufferAlloc->getHostPtr()));
-       flushMappedMemoryRange(vk, device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), bufferSize);
+
+       if (srcStencilTexture != DE_NULL)
+       {
+               DE_ASSERT(stencilOffset != 0u);
+
+               srcStencilTexture->write(reinterpret_cast<deUint8*>(bufferAlloc->getHostPtr()) + stencilOffset);
+
+               std::vector<VkBufferImageCopy>  stencilCopyRegions = srcStencilTexture->getBufferCopyRegions();
+               for (size_t regionIdx = 0; regionIdx < stencilCopyRegions.size(); regionIdx++)
+               {
+                       VkBufferImageCopy region = stencilCopyRegions[regionIdx];
+                       region.bufferOffset += stencilOffset;
+
+                       copyRegions.push_back(region);
+               }
+       }
+
+       flushMappedMemoryRange(vk, device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), VK_WHOLE_SIZE);
 
        // Copy buffer to image
        VK_CHECK(vk.beginCommandBuffer(*cmdBuffer, &cmdBufferBeginInfo));
@@ -545,6 +516,48 @@ void uploadTestTexture (const DeviceInterface&                     vk,
        VK_CHECK(vk.waitForFences(device, 1, &fence.get(), true, ~(0ull) /* infinity */));
 }
 
+void uploadTestTexture (const DeviceInterface&                 vk,
+                                               VkDevice                                                device,
+                                               VkQueue                                                 queue,
+                                               deUint32                                                queueFamilyIndex,
+                                               Allocator&                                              allocator,
+                                               const TestTexture&                              srcTexture,
+                                               VkImage                                                 destImage)
+{
+       if (tcu::isCombinedDepthStencilType(srcTexture.getTextureFormat().type))
+       {
+               de::MovePtr<TestTexture> srcDepthTexture;
+               de::MovePtr<TestTexture> srcStencilTexture;
+
+               if (tcu::hasDepthComponent(srcTexture.getTextureFormat().order))
+               {
+                       tcu::TextureFormat format;
+                       switch (srcTexture.getTextureFormat().type) {
+                       case tcu::TextureFormat::UNSIGNED_INT_16_8_8:
+                               format = tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT16);
+                               break;
+                       case tcu::TextureFormat::UNSIGNED_INT_24_8_REV:
+                               format = tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNSIGNED_INT_24_8_REV);
+                               break;
+                       case tcu::TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:
+                               format = tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::FLOAT);
+                               break;
+                       default:
+                               DE_ASSERT(0);
+                               break;
+                       }
+                       srcDepthTexture = srcTexture.copy(format);
+               }
+
+               if (tcu::hasStencilComponent(srcTexture.getTextureFormat().order))
+                       srcStencilTexture = srcTexture.copy(tcu::getEffectiveDepthStencilTextureFormat(srcTexture.getTextureFormat(), tcu::Sampler::MODE_STENCIL));
+
+               uploadTestTextureInternal(vk, device, queue, queueFamilyIndex, allocator, *srcDepthTexture, srcStencilTexture.get(), srcTexture.getTextureFormat(), destImage);
+       }
+       else
+               uploadTestTextureInternal(vk, device, queue, queueFamilyIndex, allocator, srcTexture, DE_NULL, srcTexture.getTextureFormat(), destImage);
+}
+
 // Utilities for test textures
 
 template<typename TcuTextureType>
@@ -800,6 +813,13 @@ void TestTexture::write (deUint8* destPtr) const
        }
 }
 
+void TestTexture::copyToTexture (TestTexture& destTexture) const
+{
+       for (int levelNdx = 0; levelNdx < getNumLevels(); levelNdx++)
+               for (int layerNdx = 0; layerNdx < getArraySize(); layerNdx++)
+                       tcu::copy(destTexture.getLevel(levelNdx, layerNdx), getLevel(levelNdx, layerNdx));
+}
+
 void TestTexture::populateLevels (const std::vector<tcu::PixelBufferAccess>& levels)
 {
        for (size_t levelNdx = 0; levelNdx < levels.size(); levelNdx++)
@@ -897,6 +917,17 @@ tcu::Texture1D& TestTexture1D::getTexture (void)
        return m_texture;
 }
 
+de::MovePtr<TestTexture> TestTexture1D::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTexture1D(format, m_texture.getWidth()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 // TestTexture1DArray
 
 TestTexture1DArray::TestTexture1DArray (const tcu::TextureFormat& format, int width, int arraySize)
@@ -963,6 +994,17 @@ int TestTexture1DArray::getArraySize (void) const
        return m_texture.getNumLayers();
 }
 
+de::MovePtr<TestTexture> TestTexture1DArray::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTexture1DArray(format, m_texture.getWidth(), getArraySize()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 // TestTexture2D
 
 TestTexture2D::TestTexture2D (const tcu::TextureFormat& format, int width, int height)
@@ -1014,6 +1056,17 @@ tcu::Texture2D& TestTexture2D::getTexture (void)
        return m_texture;
 }
 
+de::MovePtr<TestTexture> TestTexture2D::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTexture2D(format, m_texture.getWidth(), m_texture.getHeight()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 // TestTexture2DArray
 
 TestTexture2DArray::TestTexture2DArray (const tcu::TextureFormat& format, int width, int height, int arraySize)
@@ -1080,6 +1133,16 @@ int TestTexture2DArray::getArraySize (void) const
        return m_texture.getNumLayers();
 }
 
+de::MovePtr<TestTexture> TestTexture2DArray::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTexture2DArray(format, m_texture.getWidth(), m_texture.getHeight(), getArraySize()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
 
 // TestTexture3D
 
@@ -1132,6 +1195,17 @@ tcu::Texture3D& TestTexture3D::getTexture (void)
        return m_texture;
 }
 
+de::MovePtr<TestTexture> TestTexture3D::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTexture3D(format, m_texture.getWidth(), m_texture.getHeight(), m_texture.getDepth()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 // TestTextureCube
 
 const static tcu::CubeFace tcuFaceMapping[tcu::CUBEFACE_LAST] =
@@ -1210,6 +1284,17 @@ tcu::TextureCube& TestTextureCube::getTexture (void)
        return m_texture;
 }
 
+de::MovePtr<TestTexture> TestTextureCube::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTextureCube(format, m_texture.getSize()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 // TestTextureCubeArray
 
 TestTextureCubeArray::TestTextureCubeArray (const tcu::TextureFormat& format, int size, int arraySize)
@@ -1278,5 +1363,16 @@ tcu::TextureCubeArray& TestTextureCubeArray::getTexture (void)
        return m_texture;
 }
 
+de::MovePtr<TestTexture> TestTextureCubeArray::copy(const tcu::TextureFormat format) const
+{
+       DE_ASSERT(!isCompressed());
+
+       de::MovePtr<TestTexture>        texture (new TestTextureCubeArray(format, m_texture.getSize(), getArraySize()));
+
+       copyToTexture(*texture);
+
+       return texture;
+}
+
 } // pipeline
 } // vkt