Replace fence by timeline semaphore

This commit is contained in:
mittorn 2024-11-11 20:21:00 +03:00
parent a01c830da5
commit 6cba7c8865
2 changed files with 25 additions and 8 deletions

View file

@ -132,7 +132,8 @@ struct ComputeApplication {
VkCommandBuffer decomposeCommandBuffer;
VkCommandBuffer compressCommandBuffer;
VkFence decomposeFence;
VkFence compressFence;
//VkFence compressFence;
VkSemaphore compressSemaphore;
bool running = false;
uint8_t *pReconstructionData;
@ -250,7 +251,8 @@ struct ComputeApplication {
vkCmdBindDescriptorSets(chain[chidx].compressCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, compressPipeline.pipelineLayout, 0, 1, &chain[chidx].compressDescriptorSet, 0, NULL);
vkCmdDispatch(chain[chidx].compressCommandBuffer, (uint32_t)ceil(WIDTH/2 / float(WORKGROUP_SIZE)), (uint32_t)ceil(HEIGHT/2 / float(WORKGROUP_SIZE)), 1);
VK_CHECK_RESULT(vkEndCommandBuffer(chain[chidx].compressCommandBuffer)); // end recording commands.
VK_CHECK_RESULT(vkCreateFence(dev.device, &fenceCreateInfo, NULL, &chain[chidx].compressFence));
//VK_CHECK_RESULT(vkCreateFence(dev.device, &fenceCreateInfo, NULL, &chain[chidx].compressFence));
CallWith($Sc(VkSemaphoreCreateInfo{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}, $M(VkSemaphoreTypeCreateInfoKHR{VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO_KHR},$(semaphoreType) = VK_SEMAPHORE_TYPE_TIMELINE_KHR)), vkCreateSemaphore(dev.device, &ref, NULL, &chain[chidx].compressSemaphore));
}
void waitFence(int chidx)
{
@ -259,14 +261,21 @@ struct ComputeApplication {
chain[chidx].running = false;
}
void runCommandBuffer(int chidx) {
void runCommandBuffer(int chidx, int frameNum) {
$Sc submitInfo = SubmitInfo(chain[chidx].decomposeCommandBuffer);
waitFence(chidx);
vkResetFences(dev.device, 1, &chain[chidx].decomposeFence);
vkResetFences(dev.device, 1, &chain[chidx].compressFence);
//vkResetFences(dev.device, 1, &chain[chidx].compressFence);
VK_CHECK_RESULT(vkQueueSubmit(dev.defaultQueue, 1, &submitInfo, chain[chidx].decomposeFence));
//VK_CHECK_RESULT(vkWaitForFences(dev.device, 1, &chain[chidx].decomposeFence, VK_TRUE, 100000000000));
CallWith(SubmitInfo(chain[chidx].compressCommandBuffer),vkQueueSubmit(dev.defaultQueue, 1, &ref, chain[chidx].compressFence));
//$M(VkTimelineSemaphoreSubmitInfoKHR{VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR}, $(signalSemaphoreValueCount), $(pSignalSemaphoreValues) &= (uint64_t)frameNum);
CallWith($Sc(
SubmitInfo(chain[chidx].compressCommandBuffer, $(signalSemaphoreCount),
$(pSignalSemaphores) = &chain[chidx].compressSemaphore),
$M(VkTimelineSemaphoreSubmitInfoKHR{VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR}, $(signalSemaphoreValueCount),
$(pSignalSemaphoreValues) &= (uint64_t)frameNum)
)
,vkQueueSubmit(dev.defaultQueue, 1, &ref, NULL));// 1, &ref, chain[chidx].compressFence));
chain[chidx].running = true;
}
template <typename Codec>
@ -386,13 +395,14 @@ struct ComputeApplication {
//size_t lz4Size = LZ4_sizeofState();
//uint64_t lz4State[lz4Size/8];
xpack_compressor *comp = xpack_alloc_compressor((((1024) + 3) >> 2) * (((HEIGHT/2) + 3) >> 2)*8 + 16, 4);
PFN_vkWaitSemaphoresKHR pvkWaitSemaphoresKHR = (PFN_vkWaitSemaphoresKHR)vkGetInstanceProcAddr(context.instance, "vkWaitSemaphoresKHR");
int maxsize = 0;
constexpr uint32_t imageSize = (((1024) + 3) >> 2) * (((HEIGHT/2) + 3) >> 2)*8;//(WIDTH/2) * (HEIGHT/2);
while(frameNum++ < 1000)
{
int chidx = frameNum & 3;
// Finally, run the recorded command buffer.
runCommandBuffer(chidx);
runCommandBuffer(chidx, frameNum);
#ifndef SKIP_FENCE_SYNC
waitFence(chidx);
#endif
@ -402,7 +412,10 @@ struct ComputeApplication {
enc.EncodeIDR(chidx);
else
enc.EncodeP(chidx);
VK_CHECK_RESULT(vkWaitForFences(dev.device, 1, &chain[chidx].compressFence, VK_TRUE, 100000000000));
#if 1
//VK_CHECK_RESULT(vkWaitForFences(dev.device, 1, &chain[chidx].compressFence, VK_TRUE, 100000000000));
CallWith($M(VkSemaphoreWaitInfoKHR{VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO_KHR}, $(pSemaphores) = &chain[chidx].compressSemaphore, $(semaphoreCount), $(pValues) &= (uint64_t)frameNum ),
pvkWaitSemaphoresKHR(dev.device,&ref, 100000000000));
#if 1
static char buffer[imageSize + 16];
@ -419,6 +432,7 @@ struct ComputeApplication {
#else
fwrite(&imageSize, sizeof(imageSize), 1, reconstructionStream);
fwrite(chain[chidx].pReconstructionData, 1, imageSize, reconstructionStream);
#endif
#endif
((UBO*)chain[chidx].uboBuf.mapped)->frameNum = frameNum;

View file

@ -402,14 +402,17 @@ struct VulkanDevice
addExtension(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME);
addExtension(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME);
addExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
addExtension(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
VkPhysicalDeviceFeatures enabled_features = {
.samplerAnisotropy = VK_TRUE
};
VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcrFeat = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES, .samplerYcbcrConversion = VK_TRUE};
VkPhysicalDeviceVulkan12Features feat12 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES, &ycbcrFeat};
feat12.timelineSemaphore = true;
VkDeviceCreateInfo device_info = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = &ycbcrFeat,
.pNext = &feat12,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queue_info,
.enabledLayerCount = ctx.enabledLayersCount,