Skip to content

Commit

Permalink
rendervulkan: Add CreateTimelineSemaphore/ImportTimelineSemaphore
Browse files Browse the repository at this point in the history
Not used yet, but will be soon
  • Loading branch information
misyltoad committed Aug 9, 2024
1 parent dd77e4d commit 94271f3
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/rendervulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,132 @@ void CVulkanDevice::garbageCollect( void )
resetCmdBuffers(currentSeqNo);
}

VulkanTimelineSemaphore_t::~VulkanTimelineSemaphore_t()
{
if ( uHandle != 0 )
{
drmSyncobjDestroy( pDevice->drmRenderFd(), uHandle );
uHandle = 0;
}

if ( nFd >= 0 )
{
close( nFd );
nFd = -1;
}

if ( pVkSemaphore != VK_NULL_HANDLE )
{
pDevice->vk.DestroySemaphore( pDevice->device(), pVkSemaphore, nullptr );
pVkSemaphore = VK_NULL_HANDLE;
}
}

std::shared_ptr<VulkanTimelineSemaphore_t> CVulkanDevice::CreateTimelineSemaphore( uint64_t ulStartPoint )
{
std::shared_ptr<VulkanTimelineSemaphore_t> pSemaphore = std::make_unique<VulkanTimelineSemaphore_t>();
pSemaphore->pDevice = this;

const VkExportSemaphoreCreateInfo exportInfo =
{
.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
// This is a syncobj fd for any drivers using syncobj.
.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
};

const VkSemaphoreTypeCreateInfo typeInfo =
{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
.pNext = &exportInfo,
.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
.initialValue = ulStartPoint,
};

const VkSemaphoreCreateInfo createInfo =
{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = &typeInfo,
};

VkResult res;
if ( ( res = vk.CreateSemaphore( m_device, &createInfo, nullptr, &pSemaphore->pVkSemaphore ) ) != VK_SUCCESS )
{
vk_errorf( res, "vkCreateSemaphore failed" );
return nullptr;
}

const VkSemaphoreGetFdInfoKHR semaphoreGetInfo =
{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
.semaphore = pSemaphore->pVkSemaphore,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
};

if ( ( res = vk.GetSemaphoreFdKHR( m_device, &semaphoreGetInfo, &pSemaphore->nFd ) ) != VK_SUCCESS )
{
vk_errorf( res, "vkGetSemaphoreFdKHR failed" );
return nullptr;
}

int ret;
if ( ( ret = drmSyncobjFDToHandle( m_drmRendererFd, pSemaphore->nFd, &pSemaphore->uHandle ) ) != 0 )
{
vk_log.errorf_errno( "drmSyncobjFDToHandle failed." );
return nullptr;
}

return pSemaphore;
}

std::shared_ptr<VulkanTimelineSemaphore_t> CVulkanDevice::ImportTimelineSemaphore( uint32_t uHandle )
{
std::shared_ptr<VulkanTimelineSemaphore_t> pSemaphore = std::make_unique<VulkanTimelineSemaphore_t>();
pSemaphore->pDevice = this;
pSemaphore->uHandle = uHandle;

const VkSemaphoreTypeCreateInfo typeInfo =
{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
};

const VkSemaphoreCreateInfo createInfo =
{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = &typeInfo,
};

VkResult res;
if ( ( res = vk.CreateSemaphore( m_device, &createInfo, nullptr, &pSemaphore->pVkSemaphore ) ) != VK_SUCCESS )
{
vk_errorf( res, "vkCreateSemaphore failed" );
return nullptr;
}

if ( drmSyncobjHandleToFD( m_drmRendererFd, pSemaphore->uHandle, &pSemaphore->nFd ) != 0 )
{
vk_log.errorf_errno( "drmSyncobjHandleToFD failed." );
return nullptr;
}

VkImportSemaphoreFdInfoKHR importFdInfo =
{
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
.pNext = nullptr,
.semaphore = pSemaphore->pVkSemaphore,
.flags = 0, // not temporary
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
.fd = pSemaphore->nFd,
};
if ( ( res = vk.ImportSemaphoreFdKHR( m_device, &importFdInfo ) ) != VK_SUCCESS )
{
vk_errorf( res, "vkImportSemaphoreFdKHR failed" );
return nullptr;
}

return pSemaphore;
}

void CVulkanDevice::wait(uint64_t sequence, bool reset)
{
if (m_submissionSeqNo == sequence)
Expand Down
18 changes: 18 additions & 0 deletions src/rendervulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ static inline uint32_t div_roundup(uint32_t x, uint32_t y)
VK_FUNC(CreateSampler) \
VK_FUNC(CreateSamplerYcbcrConversion) \
VK_FUNC(CreateSemaphore) \
VK_FUNC(GetSemaphoreFdKHR) \
VK_FUNC(ImportSemaphoreFdKHR) \
VK_FUNC(CreateShaderModule) \
VK_FUNC(CreateSwapchainKHR) \
VK_FUNC(DestroyBuffer) \
Expand All @@ -680,6 +682,7 @@ static inline uint32_t div_roundup(uint32_t x, uint32_t y)
VK_FUNC(DestroyImage) \
VK_FUNC(DestroyImageView) \
VK_FUNC(DestroyPipeline) \
VK_FUNC(DestroySemaphore) \
VK_FUNC(DestroyPipelineLayout) \
VK_FUNC(DestroySampler) \
VK_FUNC(DestroySwapchainKHR) \
Expand Down Expand Up @@ -713,6 +716,18 @@ constexpr T align(T what, U to) {
return (what + to - 1) & ~(to - 1);
}

class CVulkanDevice;

struct VulkanTimelineSemaphore_t
{
~VulkanTimelineSemaphore_t();

CVulkanDevice *pDevice = nullptr;
VkSemaphore pVkSemaphore = VK_NULL_HANDLE;
int nFd = -1; // Syncobj FD, not renderer fd. Get that from pDevice.
uint32_t uHandle = 0;
};

class CVulkanDevice
{
public:
Expand All @@ -734,6 +749,9 @@ class CVulkanDevice
return ret;
}

std::shared_ptr<VulkanTimelineSemaphore_t> CreateTimelineSemaphore( uint64_t ulStartingPoint );
std::shared_ptr<VulkanTimelineSemaphore_t> ImportTimelineSemaphore( uint32_t uHandle );

static const uint32_t upload_buffer_size = 1920 * 1080 * 4;

inline VkDevice device() { return m_device; }
Expand Down

0 comments on commit 94271f3

Please sign in to comment.