2019-05-24 15:57:50 +01:00
|
|
|
/*
|
2022-03-18 12:05:46 +00:00
|
|
|
* Copyright (c) 2017-2022 Arm Limited.
|
2019-05-24 15:57:50 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file swapchain.cpp
|
|
|
|
|
*
|
|
|
|
|
* @brief Contains the implementation for a headless swapchain.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
#include <util/timed_semaphore.hpp>
|
|
|
|
|
|
|
|
|
|
#include "swapchain.hpp"
|
|
|
|
|
|
|
|
|
|
namespace wsi
|
|
|
|
|
{
|
|
|
|
|
namespace headless
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
struct image_data
|
|
|
|
|
{
|
|
|
|
|
/* Device memory backing the image. */
|
2021-10-07 14:48:44 +01:00
|
|
|
VkDeviceMemory memory{};
|
2021-09-01 22:30:31 +01:00
|
|
|
fence_sync present_fence;
|
2019-05-24 15:57:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
swapchain::swapchain(layer::device_private_data &dev_data, const VkAllocationCallbacks *pAllocator)
|
|
|
|
|
: wsi::swapchain_base(dev_data, pAllocator)
|
2021-10-27 15:47:08 +01:00
|
|
|
, m_image_create_info()
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swapchain::~swapchain()
|
|
|
|
|
{
|
|
|
|
|
/* Call the base's teardown */
|
|
|
|
|
teardown();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
VkImageCompressionControlEXT swapchain::construct_image_compression_control()
|
|
|
|
|
{
|
|
|
|
|
VkImageCompressionControlEXT compression_control = {};
|
|
|
|
|
compression_control.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
|
|
|
|
|
compression_control.compressionControlPlaneCount = m_image_compression_control.compression_control_plane_count;
|
|
|
|
|
compression_control.flags = m_image_compression_control.flags;
|
|
|
|
|
compression_control.pFixedRateFlags = m_image_compression_control.fixed_rate_flags.data();
|
|
|
|
|
return compression_control;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-09-23 15:35:51 +01:00
|
|
|
VkResult swapchain::create_aliased_image_handle(const VkImageCreateInfo *image_create_info, VkImage *image)
|
|
|
|
|
{
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
VkImageCompressionControlEXT compression_control;
|
|
|
|
|
if (m_device_data.is_swapchain_compression_control_enabled())
|
|
|
|
|
{
|
|
|
|
|
compression_control = construct_image_compression_control();
|
|
|
|
|
compression_control.pNext = m_image_create_info.pNext;
|
|
|
|
|
m_image_create_info.pNext = &compression_control;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
VkResult res = m_device_data.disp.CreateImage(m_device, &m_image_create_info, get_allocation_callbacks(), image);
|
|
|
|
|
|
|
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
if (m_device_data.is_swapchain_compression_control_enabled())
|
|
|
|
|
{
|
|
|
|
|
/* Restore pNext pointer of m_image_create_info as compression_control will be garbage after this function exits */
|
|
|
|
|
m_image_create_info.pNext = compression_control.pNext;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return res;
|
2021-09-23 15:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkResult swapchain::create_and_bind_swapchain_image(VkImageCreateInfo image_create, wsi::swapchain_image &image)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
VkResult res = VK_SUCCESS;
|
2021-05-27 14:57:53 +01:00
|
|
|
const std::lock_guard<std::recursive_mutex> lock(m_image_status_mutex);
|
|
|
|
|
|
2021-10-27 15:47:08 +01:00
|
|
|
m_image_create_info = image_create;
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
VkImageCompressionControlEXT compression_control;
|
|
|
|
|
if (m_device_data.is_swapchain_compression_control_enabled())
|
|
|
|
|
{
|
|
|
|
|
compression_control = construct_image_compression_control();
|
|
|
|
|
compression_control.pNext = m_image_create_info.pNext;
|
|
|
|
|
m_image_create_info.pNext = &compression_control;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
res = m_device_data.disp.CreateImage(m_device, &m_image_create_info, get_allocation_callbacks(), &image.image);
|
|
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
if (m_device_data.is_swapchain_compression_control_enabled())
|
|
|
|
|
{
|
|
|
|
|
/* Restore pNext pointer of m_image_create_info as compression_control will be garbage after this function exits */
|
|
|
|
|
m_image_create_info.pNext = compression_control.pNext;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2020-11-26 18:00:05 +00:00
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2019-05-24 15:57:50 +01:00
|
|
|
|
2022-07-04 11:11:51 +01:00
|
|
|
VkMemoryRequirements memory_requirements = {};
|
2019-05-24 15:57:50 +01:00
|
|
|
m_device_data.disp.GetImageMemoryRequirements(m_device, image.image, &memory_requirements);
|
|
|
|
|
|
|
|
|
|
/* Find a memory type */
|
|
|
|
|
size_t mem_type_idx = 0;
|
|
|
|
|
for (; mem_type_idx < 8 * sizeof(memory_requirements.memoryTypeBits); ++mem_type_idx)
|
|
|
|
|
{
|
|
|
|
|
if (memory_requirements.memoryTypeBits & (1u << mem_type_idx))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(mem_type_idx <= 8 * sizeof(memory_requirements.memoryTypeBits) - 1);
|
|
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo mem_info = {};
|
|
|
|
|
mem_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
|
mem_info.allocationSize = memory_requirements.size;
|
|
|
|
|
mem_info.memoryTypeIndex = mem_type_idx;
|
|
|
|
|
image_data *data = nullptr;
|
|
|
|
|
|
|
|
|
|
/* Create image_data */
|
2020-11-27 09:59:18 +00:00
|
|
|
data = m_allocator.create<image_data>(1);
|
2019-05-24 15:57:50 +01:00
|
|
|
if (data == nullptr)
|
|
|
|
|
{
|
2020-11-27 09:59:18 +00:00
|
|
|
m_device_data.disp.DestroyImage(m_device, image.image, get_allocation_callbacks());
|
2019-05-24 15:57:50 +01:00
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
image.data = reinterpret_cast<void *>(data);
|
|
|
|
|
image.status = wsi::swapchain_image::FREE;
|
|
|
|
|
|
2021-09-23 15:35:51 +01:00
|
|
|
res = m_device_data.disp.AllocateMemory(m_device, &mem_info, get_allocation_callbacks(), &data->memory);
|
2019-05-24 15:57:50 +01:00
|
|
|
assert(VK_SUCCESS == res);
|
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
destroy_image(image);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res = m_device_data.disp.BindImageMemory(m_device, image.image, data->memory, 0);
|
|
|
|
|
assert(VK_SUCCESS == res);
|
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
destroy_image(image);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize presentation fence. */
|
2021-09-01 22:30:31 +01:00
|
|
|
auto present_fence = fence_sync::create(m_device_data);
|
|
|
|
|
if (!present_fence.has_value())
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
destroy_image(image);
|
2021-09-01 22:30:31 +01:00
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
2021-09-01 22:30:31 +01:00
|
|
|
data->present_fence = std::move(present_fence.value());
|
|
|
|
|
|
2019-05-24 15:57:50 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swapchain::present_image(uint32_t pending_index)
|
|
|
|
|
{
|
|
|
|
|
unpresent_image(pending_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swapchain::destroy_image(wsi::swapchain_image &image)
|
|
|
|
|
{
|
2021-05-27 14:57:53 +01:00
|
|
|
std::unique_lock<std::recursive_mutex> image_status_lock(m_image_status_mutex);
|
2019-05-24 15:57:50 +01:00
|
|
|
if (image.status != wsi::swapchain_image::INVALID)
|
|
|
|
|
{
|
|
|
|
|
if (image.image != VK_NULL_HANDLE)
|
|
|
|
|
{
|
2020-11-27 09:59:18 +00:00
|
|
|
m_device_data.disp.DestroyImage(m_device, image.image, get_allocation_callbacks());
|
2019-05-24 15:57:50 +01:00
|
|
|
image.image = VK_NULL_HANDLE;
|
|
|
|
|
}
|
2021-05-27 14:57:53 +01:00
|
|
|
|
|
|
|
|
image.status = wsi::swapchain_image::INVALID;
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-27 14:57:53 +01:00
|
|
|
image_status_lock.unlock();
|
|
|
|
|
|
2019-05-24 15:57:50 +01:00
|
|
|
if (image.data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto *data = reinterpret_cast<image_data *>(image.data);
|
|
|
|
|
if (data->memory != VK_NULL_HANDLE)
|
|
|
|
|
{
|
2021-09-23 15:35:51 +01:00
|
|
|
m_device_data.disp.FreeMemory(m_device, data->memory, get_allocation_callbacks());
|
2019-05-24 15:57:50 +01:00
|
|
|
data->memory = VK_NULL_HANDLE;
|
|
|
|
|
}
|
2020-11-27 09:59:18 +00:00
|
|
|
m_allocator.destroy(1, data);
|
2019-05-24 15:57:50 +01:00
|
|
|
image.data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 22:30:31 +01:00
|
|
|
VkResult swapchain::image_set_present_payload(swapchain_image &image, VkQueue queue, const VkSemaphore *sem_payload,
|
|
|
|
|
uint32_t sem_count)
|
|
|
|
|
{
|
|
|
|
|
auto data = reinterpret_cast<image_data *>(image.data);
|
|
|
|
|
return data->present_fence.set_payload(queue, sem_payload, sem_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkResult swapchain::image_wait_present(swapchain_image &image, uint64_t timeout)
|
|
|
|
|
{
|
|
|
|
|
auto data = reinterpret_cast<image_data *>(image.data);
|
|
|
|
|
return data->present_fence.wait_payload(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 15:35:51 +01:00
|
|
|
VkResult swapchain::bind_swapchain_image(VkDevice &device, const VkBindImageMemoryInfo *bind_image_mem_info,
|
|
|
|
|
const VkBindImageMemorySwapchainInfoKHR *bind_sc_info)
|
|
|
|
|
{
|
|
|
|
|
auto &device_data = layer::device_private_data::get(device);
|
|
|
|
|
|
|
|
|
|
const wsi::swapchain_image &swapchain_image = m_swapchain_images[bind_sc_info->imageIndex];
|
|
|
|
|
VkDeviceMemory memory = reinterpret_cast<image_data *>(swapchain_image.data)->memory;
|
|
|
|
|
|
|
|
|
|
return device_data.disp.BindImageMemory(device, bind_image_mem_info->image, memory, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 15:57:50 +01:00
|
|
|
} /* namespace headless */
|
|
|
|
|
} /* namespace wsi */
|