mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 23:33:05 +04:00
dnn/Vulkan: make thread safe
Use a global dedicated mutex to make sure initialize once and protect command buffer pool and queue. Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>
This commit is contained in:
@@ -33,6 +33,7 @@ extern VkPhysicalDevice kPhysicalDevice;
|
||||
extern VkDevice kDevice;
|
||||
extern VkQueue kQueue;
|
||||
extern VkCommandPool kCmdPool;
|
||||
extern cv::Mutex kContextMtx;
|
||||
|
||||
enum ShapeIdx
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@ VkDebugReportCallbackEXT kDebugReportCallback;
|
||||
uint32_t kQueueFamilyIndex;
|
||||
std::vector<const char *> kEnabledLayers;
|
||||
std::map<std::string, std::vector<uint32_t>> kShaders;
|
||||
cv::Mutex kContextMtx;
|
||||
|
||||
static uint32_t getComputeQueueFamilyIndex()
|
||||
{
|
||||
@@ -86,7 +87,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugReportCallbackFn(
|
||||
// internally used
|
||||
void createContext()
|
||||
{
|
||||
cv::AutoLock lock(getInitializationMutex());
|
||||
cv::AutoLock lock(kContextMtx);
|
||||
if (!kCtx)
|
||||
{
|
||||
kCtx.reset(new Context());
|
||||
|
||||
@@ -150,6 +150,7 @@ void OpBase::recordCommandBuffer(void* push_constants, size_t push_constants_siz
|
||||
VkCommandBufferBeginInfo beginInfo = {};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
cv::AutoLock lock(kContextMtx);
|
||||
VK_CHECK_RESULT(vkBeginCommandBuffer(cmd_buffer_, &beginInfo));
|
||||
if (push_constants)
|
||||
vkCmdPushConstants(cmd_buffer_, pipeline_layout_,
|
||||
@@ -176,7 +177,10 @@ void OpBase::runCommandBuffer()
|
||||
fence_create_info_.flags = 0;
|
||||
|
||||
VK_CHECK_RESULT(vkCreateFence(device_, &fence_create_info_, NULL, &fence));
|
||||
VK_CHECK_RESULT(vkQueueSubmit(kQueue, 1, &submit_info, fence));
|
||||
{
|
||||
cv::AutoLock lock(kContextMtx);
|
||||
VK_CHECK_RESULT(vkQueueSubmit(kQueue, 1, &submit_info, fence));
|
||||
}
|
||||
VK_CHECK_RESULT(vkWaitForFences(device_, 1, &fence, VK_TRUE, 100000000000));
|
||||
vkDestroyFence(device_, fence, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user