This commit makes full use of the enabled queues to provide asynchronous uploads of images (downloads remain synchronous). For a pure uploading use cases, the performance gains can be significant.
Unified · Split
| @@ -41,11 +41,23 @@ · 40 unmodified lines | ||
| 41 | 41 | #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x) |
| 42 | 42 | #endif |
| 43 | 43 | |
| 44 | +typedef struct VulkanQueueCtx { | |
| 45 | + VkFence fence; | |
| 46 | + VkQueue queue; | |
| 47 | + int was_synchronous; | |
| 48 | + | |
| 49 | + /* Buffer dependencies */ | |
| 50 | + AVBufferRef **buf_deps; | |
| 51 | + int nb_buf_deps; | |
| 52 | + int buf_deps_alloc_size; | |
| 53 | +} VulkanQueueCtx; | |
| 54 | + | |
| 44 | 55 | typedef struct VulkanExecCtx { |
| 45 | 56 | VkCommandPool pool; |
| 46 | − VkCommandBuffer buf; | |
| 47 | − VkQueue queue; | |
| 48 | − VkFence fence; | |
| 57 | + VkCommandBuffer *bufs; | |
| 58 | + VulkanQueueCtx *queues; | |
| 59 | + int nb_queues; | |
| 60 | + int cur_queue_idx; | |
| 49 | 61 | } VulkanExecCtx; |
| 50 | 62 | |
| 51 | 63 | typedef struct VulkanDevicePriv { |
| @@ -60,8 +72,9 @@ typedef struct VulkanDevicePriv { · 8 unmodified lines | ||
| 60 | 72 | /* Debug callback */ |
| 61 | 73 | VkDebugUtilsMessengerEXT debug_ctx; |
| 62 | 74 | |
| 63 | − /* Image uploading */ | |
| 64 | − VulkanExecCtx cmd; | |
| 75 | + /* Image transfers */ | |
| 76 | + VulkanExecCtx upload_ctx; | |
| 77 | + VulkanExecCtx download_ctx; | |
| 65 | 78 | |
| 66 | 79 | /* Extensions */ |
| 67 | 80 | uint64_t extensions; |
| @@ -89,6 +102,16 @@ typedef struct AVVkFrameInternal { · 21 unmodified lines | ||
| 89 | 102 | #endif |
| 90 | 103 | } AVVkFrameInternal; |
| 91 | 104 | |
| 105 | +#define GET_QUEUE_COUNT(hwctx, graph, comp, tx) ( \ | |
| 106 | + graph ? hwctx->nb_graphics_queues : \ | |
| 107 | + comp ? (hwctx->nb_comp_queues ? \ | |
| 108 | + hwctx->nb_comp_queues : hwctx->nb_graphics_queues) : \ | |
| 109 | + tx ? (hwctx->nb_tx_queues ? hwctx->nb_tx_queues : \ | |
| 110 | + (hwctx->nb_comp_queues ? \ | |
| 111 | + hwctx->nb_comp_queues : hwctx->nb_graphics_queues)) : \ | |
| 112 | + 0 \ | |
| 113 | +) | |
| 114 | + | |
| 92 | 115 | #define VK_LOAD_PFN(inst, name) PFN_##name pfn_##name = (PFN_##name) \ |
| 93 | 116 | vkGetInstanceProcAddr(inst, #name) |
| 94 | 117 | |
| @@ -709,7 +732,7 @@ fail: · 614 unmodified lines | ||
| 709 | 732 | } |
| 710 | 733 | |
| 711 | 734 | static int create_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd, |
| 712 | − int queue_family_index) | |
| 735 | + int queue_family_index, int num_queues) | |
| 713 | 736 | { |
| 714 | 737 | VkResult ret; |
| 715 | 738 | AVVulkanDeviceContext *hwctx = ctx->hwctx; |
| @@ -722,21 +745,20 @@ static int create_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd, · 6 unmodified lines | ||
| 722 | 745 | VkCommandBufferAllocateInfo cbuf_create = { |
| 723 | 746 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
| 724 | 747 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
| 725 | − .commandBufferCount = 1, | |
| 748 | + .commandBufferCount = num_queues, | |
| 726 | 749 | }; |
| 727 | 750 | |
| 728 | − VkFenceCreateInfo fence_spawn = { | |
| 729 | − .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, | |
| 730 | − }; | |
| 751 | + cmd->nb_queues = num_queues; | |
| 731 | 752 | |
| 732 | − ret = vkCreateFence(hwctx->act_dev, &fence_spawn, | |
| 733 | − hwctx->alloc, &cmd->fence); | |
| 734 | − if (ret != VK_SUCCESS) { | |
| 735 | − av_log(ctx, AV_LOG_ERROR, "Failed to create frame fence: %s\n", | |
| 736 | − vk_ret2str(ret)); | |
| 737 | − return AVERROR_EXTERNAL; | |
| 738 | − } | |
| 753 | + cmd->queues = av_mallocz(num_queues * sizeof(*cmd->queues)); | |
| 754 | + if (!cmd->queues) | |
| 755 | + return AVERROR(ENOMEM); | |
| 739 | 756 | |
| 757 | + cmd->bufs = av_mallocz(num_queues * sizeof(*cmd->bufs)); | |
| 758 | + if (!cmd->bufs) | |
| 759 | + return AVERROR(ENOMEM); | |
| 760 | + | |
| 761 | + /* Create command pool */ | |
| 740 | 762 | ret = vkCreateCommandPool(hwctx->act_dev, &cqueue_create, |
| 741 | 763 | hwctx->alloc, &cmd->pool); |
| 742 | 764 | if (ret != VK_SUCCESS) { |
| @@ -747,15 +769,19 @@ static int create_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd, · 4 unmodified lines | ||
| 747 | 769 | |
| 748 | 770 | cbuf_create.commandPool = cmd->pool; |
| 749 | 771 | |
| 750 | − ret = vkAllocateCommandBuffers(hwctx->act_dev, &cbuf_create, &cmd->buf); | |
| 772 | + /* Allocate command buffer */ | |
| 773 | + ret = vkAllocateCommandBuffers(hwctx->act_dev, &cbuf_create, cmd->bufs); | |
| 751 | 774 | if (ret != VK_SUCCESS) { |
| 752 | 775 | av_log(ctx, AV_LOG_ERROR, "Command buffer alloc failure: %s\n", |
| 753 | 776 | vk_ret2str(ret)); |
| 754 | 777 | return AVERROR_EXTERNAL; |
| 755 | 778 | } |
| 756 | 779 | |
| 757 | − vkGetDeviceQueue(hwctx->act_dev, cqueue_create.queueFamilyIndex, 0, | |
| 758 | − &cmd->queue); | |
| 780 | + for (int i = 0; i < num_queues; i++) { | |
| 781 | + VulkanQueueCtx *q = &cmd->queues[i]; | |
| 782 | + vkGetDeviceQueue(hwctx->act_dev, queue_family_index, i, &q->queue); | |
| 783 | + q->was_synchronous = 1; | |
| 784 | + } | |
| 759 | 785 | |
| 760 | 786 | return 0; |
| 761 | 787 | } |
| @@ -764,12 +790,154 @@ static void free_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd) · 2 unmodified lines | ||
| 764 | 790 | { |
| 765 | 791 | AVVulkanDeviceContext *hwctx = ctx->hwctx; |
| 766 | 792 | |
| 767 | − if (cmd->fence) | |
| 768 | − vkDestroyFence(hwctx->act_dev, cmd->fence, hwctx->alloc); | |
| 769 | − if (cmd->buf) | |
| 770 | − vkFreeCommandBuffers(hwctx->act_dev, cmd->pool, 1, &cmd->buf); | |
| 793 | + /* Make sure all queues have finished executing */ | |
| 794 | + for (int i = 0; i < cmd->nb_queues; i++) { | |
| 795 | + VulkanQueueCtx *q = &cmd->queues[i]; | |
| 796 | + | |
| 797 | + if (q->fence && !q->was_synchronous) { | |
| 798 | + vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX); | |
| 799 | + vkResetFences(hwctx->act_dev, 1, &q->fence); | |
| 800 | + } | |
| 801 | + | |
| 802 | + /* Free the fence */ | |
| 803 | + if (q->fence) | |
| 804 | + vkDestroyFence(hwctx->act_dev, q->fence, hwctx->alloc); | |
| 805 | + | |
| 806 | + /* Free buffer dependencies */ | |
| 807 | + for (int j = 0; j < q->nb_buf_deps; j++) | |
| 808 | + av_buffer_unref(&q->buf_deps[j]); | |
| 809 | + av_free(q->buf_deps); | |
| 810 | + } | |
| 811 | + | |
| 812 | + if (cmd->bufs) | |
| 813 | + vkFreeCommandBuffers(hwctx->act_dev, cmd->pool, cmd->nb_queues, cmd->bufs); | |
| 771 | 814 | if (cmd->pool) |
| 772 | 815 | vkDestroyCommandPool(hwctx->act_dev, cmd->pool, hwctx->alloc); |
| 816 | + | |
| 817 | + av_freep(&cmd->bufs); | |
| 818 | + av_freep(&cmd->queues); | |
| 819 | +} | |
| 820 | + | |
| 821 | +static VkCommandBuffer get_buf_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd) | |
| 822 | +{ | |
| 823 | + return cmd->bufs[cmd->cur_queue_idx]; | |
| 824 | +} | |
| 825 | + | |
| 826 | +static void unref_exec_ctx_deps(AVHWDeviceContext *ctx, VulkanExecCtx *cmd) | |
| 827 | +{ | |
| 828 | + VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx]; | |
| 829 | + | |
| 830 | + for (int j = 0; j < q->nb_buf_deps; j++) | |
| 831 | + av_buffer_unref(&q->buf_deps[j]); | |
| 832 | + q->nb_buf_deps = 0; | |
| 833 | +} | |
| 834 | + | |
| 835 | +static int wait_start_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd) | |
| 836 | +{ | |
| 837 | + VkResult ret; | |
| 838 | + AVVulkanDeviceContext *hwctx = ctx->hwctx; | |
| 839 | + VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx]; | |
| 840 | + | |
| 841 | + VkCommandBufferBeginInfo cmd_start = { | |
| 842 | + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
| 843 | + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, | |
| 844 | + }; | |
| 845 | + | |
| 846 | + /* Create the fence and don't wait for it initially */ | |
| 847 | + if (!q->fence) { | |
| 848 | + VkFenceCreateInfo fence_spawn = { | |
| 849 | + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, | |
| 850 | + }; | |
| 851 | + ret = vkCreateFence(hwctx->act_dev, &fence_spawn, hwctx->alloc, | |
| 852 | + &q->fence); | |
| 853 | + if (ret != VK_SUCCESS) { | |
| 854 | + av_log(ctx, AV_LOG_ERROR, "Failed to queue frame fence: %s\n", | |
| 855 | + vk_ret2str(ret)); | |
| 856 | + return AVERROR_EXTERNAL; | |
| 857 | + } | |
| 858 | + } else if (!q->was_synchronous) { | |
| 859 | + vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX); | |
| 860 | + vkResetFences(hwctx->act_dev, 1, &q->fence); | |
| 861 | + } | |
| 862 | + | |
| 863 | + /* Discard queue dependencies */ | |
| 864 | + unref_exec_ctx_deps(ctx, cmd); | |
| 865 | + | |
| 866 | + ret = vkBeginCommandBuffer(cmd->bufs[cmd->cur_queue_idx], &cmd_start); | |
| 867 | + if (ret != VK_SUCCESS) { | |
| 868 | + av_log(ctx, AV_LOG_ERROR, "Unable to init command buffer: %s\n", | |
| 869 | + vk_ret2str(ret)); | |
| 870 | + return AVERROR_EXTERNAL; | |
| 871 | + } | |
| 872 | + | |
| 873 | + return 0; | |
| 874 | +} | |
| 875 | + | |
| 876 | +static int add_buf_dep_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd, | |
| 877 | + AVBufferRef * const *deps, int nb_deps) | |
| 878 | +{ | |
| 879 | + AVBufferRef **dst; | |
| 880 | + VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx]; | |
| 881 | + | |
| 882 | + if (!deps || !nb_deps) | |
| 883 | + return 0; | |
| 884 | + | |
| 885 | + dst = av_fast_realloc(q->buf_deps, &q->buf_deps_alloc_size, | |
| 886 | + (q->nb_buf_deps + nb_deps) * sizeof(*dst)); | |
| 887 | + if (!dst) | |
| 888 | + goto err; | |
| 889 | + | |
| 890 | + q->buf_deps = dst; | |
| 891 | + | |
| 892 | + for (int i = 0; i < nb_deps; i++) { | |
| 893 | + q->buf_deps[q->nb_buf_deps] = av_buffer_ref(deps[i]); | |
| 894 | + if (!q->buf_deps[q->nb_buf_deps]) | |
| 895 | + goto err; | |
| 896 | + q->nb_buf_deps++; | |
| 897 | + } | |
| 898 | + | |
| 899 | + return 0; | |
| 900 | + | |
| 901 | +err: | |
| 902 | + unref_exec_ctx_deps(ctx, cmd); | |
| 903 | + return AVERROR(ENOMEM); | |
| 904 | +} | |
| 905 | + | |
| 906 | +static int submit_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd, | |
| 907 | + VkSubmitInfo *s_info, int synchronous) | |
| 908 | +{ | |
| 909 | + VkResult ret; | |
| 910 | + VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx]; | |
| 911 | + | |
| 912 | + ret = vkEndCommandBuffer(cmd->bufs[cmd->cur_queue_idx]); | |
| 913 | + if (ret != VK_SUCCESS) { | |
| 914 | + av_log(ctx, AV_LOG_ERROR, "Unable to finish command buffer: %s\n", | |
| 915 | + vk_ret2str(ret)); | |
| 916 | + unref_exec_ctx_deps(ctx, cmd); | |
| 917 | + return AVERROR_EXTERNAL; | |
| 918 | + } | |
| 919 | + | |
| 920 | + s_info->pCommandBuffers = &cmd->bufs[cmd->cur_queue_idx]; | |
| 921 | + s_info->commandBufferCount = 1; | |
| 922 | + | |
| 923 | + ret = vkQueueSubmit(q->queue, 1, s_info, q->fence); | |
| 924 | + if (ret != VK_SUCCESS) { | |
| 925 | + unref_exec_ctx_deps(ctx, cmd); | |
| 926 | + return AVERROR_EXTERNAL; | |
| 927 | + } | |
| 928 | + | |
| 929 | + q->was_synchronous = synchronous; | |
| 930 | + | |
| 931 | + if (synchronous) { | |
| 932 | + AVVulkanDeviceContext *hwctx = ctx->hwctx; | |
| 933 | + vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX); | |
| 934 | + vkResetFences(hwctx->act_dev, 1, &q->fence); | |
| 935 | + unref_exec_ctx_deps(ctx, cmd); | |
| 936 | + } else { /* Rotate queues */ | |
| 937 | + cmd->cur_queue_idx = (cmd->cur_queue_idx + 1) % cmd->nb_queues; | |
| 938 | + } | |
| 939 | + | |
| 940 | + return 0; | |
| 773 | 941 | } |
| 774 | 942 | |
| 775 | 943 | static void vulkan_device_free(AVHWDeviceContext *ctx) |
| @@ -937,7 +1105,8 @@ if (n >= queue_num) { · 161 unmodified lines | ||
| 937 | 1105 | p->qfs[p->num_qfs++] = hwctx->queue_family_comp_index; |
| 938 | 1106 | |
| 939 | 1107 | /* Create exec context - if there's something invalid this will error out */ |
| 940 | − err = create_exec_ctx(ctx, &p->cmd, hwctx->queue_family_tx_index); | |
| 1108 | + err = create_exec_ctx(ctx, &p->cmd, hwctx->queue_family_tx_index, | |
| 1109 | + GET_QUEUE_COUNT(hwctx, 0, 0, 1)); | |
| 941 | 1110 | if (err) |
| 942 | 1111 | return err; |
| 943 | 1112 | |
| @@ -1257,26 +1426,17 @@ enum PrepMode { · 313 unmodified lines | ||
| 1257 | 1426 | static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx, |
| 1258 | 1427 | AVVkFrame *frame, enum PrepMode pmode) |
| 1259 | 1428 | { |
| 1260 | − VkResult ret; | |
| 1429 | + int err; | |
| 1261 | 1430 | uint32_t dst_qf; |
| 1262 | 1431 | VkImageLayout new_layout; |
| 1263 | 1432 | VkAccessFlags new_access; |
| 1264 | 1433 | AVHWDeviceContext *ctx = hwfc->device_ctx; |
| 1265 | − AVVulkanDeviceContext *hwctx = ctx->hwctx; | |
| 1266 | 1434 | const int planes = av_pix_fmt_count_planes(hwfc->sw_format); |
| 1267 | 1435 | |
| 1268 | 1436 | VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 }; |
| 1269 | 1437 | |
| 1270 | − VkCommandBufferBeginInfo cmd_start = { | |
| 1271 | − .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
| 1272 | − .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, | |
| 1273 | − }; | |
| 1274 | − | |
| 1275 | 1438 | VkSubmitInfo s_info = { |
| 1276 | 1439 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 1277 | − .commandBufferCount = 1, | |
| 1278 | − .pCommandBuffers = &ectx->buf, | |
| 1279 | − | |
| 1280 | 1440 | .pSignalSemaphores = frame->sem, |
| 1281 | 1441 | .signalSemaphoreCount = planes, |
| 1282 | 1442 | }; |
| @@ -1306,9 +1466,8 @@ static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx, · 23 unmodified lines | ||
| 1306 | 1466 | break; |
| 1307 | 1467 | } |
| 1308 | 1468 | |
| 1309 | − ret = vkBeginCommandBuffer(ectx->buf, &cmd_start); | |
| 1310 | − if (ret != VK_SUCCESS) | |
| 1311 | − return AVERROR_EXTERNAL; | |
| 1469 | + if ((err = wait_start_exec_ctx(ctx, ectx))) | |
| 1470 | + return err; | |
| 1312 | 1471 | |
| 1313 | 1472 | /* Change the image layout to something more optimal for writes. |
| 1314 | 1473 | * This also signals the newly created semaphore, making it usable |
| @@ -1330,23 +1489,12 @@ static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx, · 15 unmodified lines | ||
| 1330 | 1489 | frame->access[i] = img_bar[i].dstAccessMask; |
| 1331 | 1490 | } |
| 1332 | 1491 | |
| 1333 | − vkCmdPipelineBarrier(ectx->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | |
| 1334 | − VK_PIPELINE_STAGE_TRANSFER_BIT, 0, | |
| 1335 | − 0, NULL, 0, NULL, planes, img_bar); | |
| 1336 | − | |
| 1337 | − ret = vkEndCommandBuffer(ectx->buf); | |
| 1338 | − if (ret != VK_SUCCESS) | |
| 1339 | − return AVERROR_EXTERNAL; | |
| 1340 | − | |
| 1341 | − ret = vkQueueSubmit(ectx->queue, 1, &s_info, ectx->fence); | |
| 1342 | − if (ret != VK_SUCCESS) { | |
| 1343 | − return AVERROR_EXTERNAL; | |
| 1344 | − } else { | |
| 1345 | − vkWaitForFences(hwctx->act_dev, 1, &ectx->fence, VK_TRUE, UINT64_MAX); | |
| 1346 | − vkResetFences(hwctx->act_dev, 1, &ectx->fence); | |
| 1347 | − } | |
| 1492 | + vkCmdPipelineBarrier(get_buf_exec_ctx(ctx, ectx), | |
| 1493 | + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | |
| 1494 | + VK_PIPELINE_STAGE_TRANSFER_BIT, | |
| 1495 | + 0, 0, NULL, 0, NULL, planes, img_bar); | |
| 1348 | 1496 | |
| 1349 | − return 0; | |
| 1497 | + return submit_exec_ctx(ctx, ectx, &s_info, 0); | |
| 1350 | 1498 | } |
| 1351 | 1499 | |
| 1352 | 1500 | static int create_frame(AVHWFramesContext *hwfc, AVVkFrame **frame, |
| @@ -1559,7 +1707,8 @@ static int vulkan_frames_init(AVHWFramesContext *hwfc) · 206 unmodified lines | ||
| 1559 | 1707 | hwctx->usage |= DEFAULT_USAGE_FLAGS; |
| 1560 | 1708 | |
| 1561 | 1709 | err = create_exec_ctx(hwfc->device_ctx, &fp->cmd, |
| 1562 | − dev_hwctx->queue_family_tx_index); | |
| 1710 | + dev_hwctx->queue_family_tx_index, | |
| 1711 | + GET_QUEUE_COUNT(dev_hwctx, 0, 0, 1)); | |
| 1563 | 1712 | if (err) |
| 1564 | 1713 | return err; |
| 1565 | 1714 | |
| @@ -2620,12 +2769,12 @@ static int unmap_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, · 1054 unmodified lines | ||
| 2620 | 2769 | return err; |
| 2621 | 2770 | } |
| 2622 | 2771 | |
| 2623 | −static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame, | |
| 2772 | +static int transfer_image_buf(AVHWDeviceContext *ctx, const AVFrame *f, | |
| 2624 | 2773 | AVBufferRef **bufs, const int *buf_stride, int w, |
| 2625 | 2774 | int h, enum AVPixelFormat pix_fmt, int to_buf) |
| 2626 | 2775 | { |
| 2627 | − VkResult ret; | |
| 2628 | − AVVulkanDeviceContext *hwctx = ctx->hwctx; | |
| 2776 | + int err; | |
| 2777 | + AVVkFrame *frame = (AVVkFrame *)f->data[0]; | |
| 2629 | 2778 | VulkanDevicePriv *s = ctx->internal->priv; |
| 2630 | 2779 | |
| 2631 | 2780 | int bar_num = 0; |
| @@ -2634,17 +2783,11 @@ static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame, · 2 unmodified lines | ||
| 2634 | 2783 | const int planes = av_pix_fmt_count_planes(pix_fmt); |
| 2635 | 2784 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); |
| 2636 | 2785 | |
| 2637 | − VkCommandBufferBeginInfo cmd_start = { | |
| 2638 | − .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
| 2639 | − .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, | |
| 2640 | − }; | |
| 2641 | − | |
| 2642 | 2786 | VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 }; |
| 2787 | + VkCommandBuffer cmd_buf = get_buf_exec_ctx(ctx, &s->cmd); | |
| 2643 | 2788 | |
| 2644 | 2789 | VkSubmitInfo s_info = { |
| 2645 | 2790 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 2646 | − .commandBufferCount = 1, | |
| 2647 | − .pCommandBuffers = &s->cmd.buf, | |
| 2648 | 2791 | .pSignalSemaphores = frame->sem, |
| 2649 | 2792 | .pWaitSemaphores = frame->sem, |
| 2650 | 2793 | .pWaitDstStageMask = sem_wait_dst, |
| @@ -2652,12 +2795,8 @@ static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame, · 1 unmodified line | ||
| 2652 | 2795 | .waitSemaphoreCount = planes, |
| 2653 | 2796 | }; |
| 2654 | 2797 | |
| 2655 | − ret = vkBeginCommandBuffer(s->cmd.buf, &cmd_start); | |
| 2656 | − if (ret != VK_SUCCESS) { | |
| 2657 | − av_log(ctx, AV_LOG_ERROR, "Unable to init command buffer: %s\n", | |
| 2658 | − vk_ret2str(ret)); | |
| 2659 | − return AVERROR_EXTERNAL; | |
| 2660 | − } | |
| 2798 | + if ((err = wait_start_exec_ctx(ctx, &s->cmd))) | |
| 2799 | + return err; | |
| 2661 | 2800 | |
| 2662 | 2801 | /* Change the image layout to something more optimal for transfers */ |
| 2663 | 2802 | for (int i = 0; i < planes; i++) { |
| @@ -2691,7 +2830,7 @@ static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame, · 27 unmodified lines | ||
| 2691 | 2830 | } |
| 2692 | 2831 | |
| 2693 | 2832 | if (bar_num) |
| 2694 | − vkCmdPipelineBarrier(s->cmd.buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | |
| 2833 | + vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | |
| 2695 | 2834 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, |
| 2696 | 2835 | 0, NULL, 0, NULL, bar_num, img_bar); |
| 2697 | 2836 | |
| @@ -2715,33 +2854,33 @@ static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame, · 17 unmodified lines | ||
| 2715 | 2854 | }; |
| 2716 | 2855 | |
| 2717 | 2856 | if (to_buf) |
| 2718 | − vkCmdCopyImageToBuffer(s->cmd.buf, frame->img[i], frame->layout[i], | |
| 2857 | + vkCmdCopyImageToBuffer(cmd_buf, frame->img[i], frame->layout[i], | |
| 2719 | 2858 | vkbuf->buf, 1, &buf_reg); |
| 2720 | 2859 | else |
| 2721 | − vkCmdCopyBufferToImage(s->cmd.buf, vkbuf->buf, frame->img[i], | |
| 2860 | + vkCmdCopyBufferToImage(cmd_buf, vkbuf->buf, frame->img[i], | |
| 2722 | 2861 | frame->layout[i], 1, &buf_reg); |
| 2723 | 2862 | } |
| 2724 | 2863 | |
| 2725 | − ret = vkEndCommandBuffer(s->cmd.buf); | |
| 2726 | − if (ret != VK_SUCCESS) { | |
| 2727 | − av_log(ctx, AV_LOG_ERROR, "Unable to finish command buffer: %s\n", | |
| 2728 | − vk_ret2str(ret)); | |
| 2729 | − return AVERROR_EXTERNAL; | |
| 2730 | − } | |
| 2731 | − | |
| 2732 | − /* Wait for the download/upload to finish if uploading, otherwise the | |
| 2733 | − * semaphore will take care of synchronization when uploading */ | |
| 2734 | − ret = vkQueueSubmit(s->cmd.queue, 1, &s_info, s->cmd.fence); | |
| 2735 | − if (ret != VK_SUCCESS) { | |
| 2736 | − av_log(ctx, AV_LOG_ERROR, "Unable to submit command buffer: %s\n", | |
| 2737 | − vk_ret2str(ret)); | |
| 2738 | − return AVERROR_EXTERNAL; | |
| 2864 | + /* When uploading, do this asynchronously if the source is refcounted by | |
| 2865 | + * keeping the buffers as a submission dependency. | |
| 2866 | + * The hwcontext is guaranteed to not be freed until all frames are freed | |
| 2867 | + * in the frames_unint function. | |
| 2868 | + * When downloading to buffer, do this synchronously and wait for the | |
| 2869 | + * queue submission to finish executing */ | |
| 2870 | + if (!to_buf) { | |
| 2871 | + int ref; | |
| 2872 | + for (ref = 0; ref < AV_NUM_DATA_POINTERS; ref++) { | |
| 2873 | + if (!f->buf[ref]) | |
| 2874 | + break; | |
| 2875 | + if ((err = add_buf_dep_exec_ctx(hwfc, &s->cmd, &f->buf[ref], 1))) | |
| 2876 | + return err; | |
| 2877 | + } | |
| 2878 | + if (ref && (err = add_buf_dep_exec_ctx(hwfc, &s->cmd, bufs, planes))) | |
| 2879 | + return err; | |
| 2880 | + return submit_exec_ctx(hwfc, &s->cmd, &s_info, !ref); | |
| 2739 | 2881 | } else { |
| 2740 | − vkWaitForFences(hwctx->act_dev, 1, &s->cmd.fence, VK_TRUE, UINT64_MAX); | |
| 2741 | − vkResetFences(hwctx->act_dev, 1, &s->cmd.fence); | |
| 2882 | + return submit_exec_ctx(hwfc, &s->cmd, &s_info, 1); | |
| 2742 | 2883 | } |
| 2743 | − | |
| 2744 | − return 0; | |
| 2745 | 2884 | } |
| 2746 | 2885 | |
| 2747 | 2886 | /* Technically we can use VK_EXT_external_memory_host to upload and download, |
| @@ -2778,11 +2917,11 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 30 unmodified lines | ||
| 2778 | 2917 | |
| 2779 | 2918 | err = vulkan_map_frame_to_mem(hwfc, map, dst, AV_HWFRAME_MAP_WRITE); |
| 2780 | 2919 | if (err) |
| 2781 | − goto end; | |
| 2920 | + return err; | |
| 2782 | 2921 | |
| 2783 | 2922 | err = av_frame_copy(map, src); |
| 2784 | 2923 | av_frame_free(&map); |
| 2785 | − goto end; | |
| 2924 | + return err; | |
| 2786 | 2925 | } |
| 2787 | 2926 | |
| 2788 | 2927 | /* Create buffers */ |
| @@ -2809,7 +2948,7 @@ static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 20 unmodified lines | ||
| 2809 | 2948 | goto end; |
| 2810 | 2949 | |
| 2811 | 2950 | /* Copy buffers to image */ |
| 2812 | − err = transfer_image_buf(dev_ctx, f, bufs, tmp.linesize, | |
| 2951 | + err = transfer_image_buf(dev_ctx, dst, bufs, tmp.linesize, | |
| 2813 | 2952 | src->width, src->height, src->format, 0); |
| 2814 | 2953 | |
| 2815 | 2954 | end: |
| @@ -2949,10 +3088,12 @@ static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, · 133 unmodified lines | ||
| 2949 | 3088 | err = create_buf(dev_ctx, &bufs[i], p_height, |
| 2950 | 3089 | &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_DST_BIT, |
| 2951 | 3090 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL); |
| 3091 | + if (err) | |
| 3092 | + goto end; | |
| 2952 | 3093 | } |
| 2953 | 3094 | |
| 2954 | 3095 | /* Copy image to buffer */ |
| 2955 | − if ((err = transfer_image_buf(dev_ctx, f, bufs, tmp.linesize, | |
| 3096 | + if ((err = transfer_image_buf(dev_ctx, src, bufs, tmp.linesize, | |
| 2956 | 3097 | dst->width, dst->height, dst->format, 1))) |
| 2957 | 3098 | goto end; |
| 2958 | 3099 | |