Commit 02cc1cd6 authored by WuZhiwen's avatar WuZhiwen Committed by Alexander Alekhin

Merge pull request #13244 from wzw-intel:init_vulkan

* dnn/Vulkan: don't init Vulkan runtime if using other backend/target

Don't need to explictly call a init API but will automatically
init Vulkan environment the first time to use an VkCom object.
Signed-off-by: 's avatarWu Zhiwen <zhiwen.wu@intel.com>

* dnn/Vulkan: depress compilier warning for "-Wsign-promo"
Signed-off-by: 's avatarWu Zhiwen <zhiwen.wu@intel.com>
parent 0e6cf419
......@@ -911,21 +911,8 @@ struct Net::Impl
typedef std::map<int, LayerShapes> LayersShapesMap;
typedef std::map<int, LayerData> MapIdToLayerData;
~Impl()
{
#ifdef HAVE_VULKAN
// Vulkan requires explicit releasing the child objects of
// VkDevice object prior to releasing VkDevice object itself.
layers.clear();
backendWrappers.clear();
vkcom::deinitPerThread();
#endif
}
Impl()
{
#ifdef HAVE_VULKAN
vkcom::initPerThread();
#endif
//allocate fake net input layer
netInputLayer = Ptr<DataLayer>(new DataLayer());
LayerData &inpl = layers.insert( make_pair(0, LayerData()) ).first->second;
......
......@@ -36,7 +36,6 @@ protected:
void recordCommandBuffer(void* push_constants = NULL, size_t push_constants_size = 0);
void runCommandBuffer();
const Context* ctx_;
VkPipeline pipeline_;
VkCommandBuffer cmd_buffer_;
VkDescriptorPool descriptor_pool_;
......
......@@ -39,9 +39,6 @@ enum PaddingMode { kPaddingModeSame, kPaddingModeValid, kPaddingModeCaffe, kPadd
enum FusedActivationType { kNone, kRelu, kRelu1, kRelu6, kActivationNum };
typedef std::vector<int> Shape;
/* context APIs */
bool initPerThread();
void deinitPerThread();
bool isAvailable();
#endif // HAVE_VULKAN
......
......@@ -18,7 +18,7 @@ static uint32_t findMemoryType(uint32_t memoryTypeBits, VkMemoryPropertyFlags pr
{
VkPhysicalDeviceMemoryProperties memoryProperties;
vkGetPhysicalDeviceMemoryProperties(getPhysicalDevice(), &memoryProperties);
vkGetPhysicalDeviceMemoryProperties(kPhysicalDevice, &memoryProperties);
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; ++i) {
if ((memoryTypeBits & (1 << i)) &&
......
......@@ -29,6 +29,10 @@
namespace cv { namespace dnn { namespace vkcom {
#ifdef HAVE_VULKAN
extern VkPhysicalDevice kPhysicalDevice;
extern VkDevice kDevice;
extern VkQueue kQueue;
extern VkCommandPool kCmdPool;
enum ShapeIdx
{
......@@ -42,7 +46,7 @@ enum ShapeIdx
{ \
if (f != VK_SUCCESS) \
{ \
CV_LOG_ERROR(NULL, "Vulkan check failed, result = " << f); \
CV_LOG_ERROR(NULL, "Vulkan check failed, result = " << (int)f); \
CV_Error(Error::StsError, "Vulkan check failed"); \
} \
}
......
......@@ -7,7 +7,6 @@
#ifndef OPENCV_DNN_VKCOM_CONTEXT_HPP
#define OPENCV_DNN_VKCOM_CONTEXT_HPP
#include "common.hpp"
namespace cv { namespace dnn { namespace vkcom {
......@@ -15,13 +14,12 @@ namespace cv { namespace dnn { namespace vkcom {
struct Context
{
VkDevice device;
VkQueue queue;
VkCommandPool cmd_pool;
std::map<std::string, VkShaderModule> shader_modules;
int ref;
Context();
~Context();
};
void createContext();
#endif // HAVE_VULKAN
}}} // namespace cv::dnn::vkcom
......
......@@ -16,8 +16,8 @@ namespace cv { namespace dnn { namespace vkcom {
OpBase::OpBase()
{
ctx_ = getContext();
device_ = ctx_->device;
createContext();
device_ = kDevice;
pipeline_ = VK_NULL_HANDLE;
cmd_buffer_ = VK_NULL_HANDLE;
descriptor_pool_ = VK_NULL_HANDLE;
......@@ -139,7 +139,7 @@ void OpBase::createCommandBuffer()
{
VkCommandBufferAllocateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
info.commandPool = ctx_->cmd_pool;
info.commandPool = kCmdPool;
info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
info.commandBufferCount = 1;
VK_CHECK_RESULT(vkAllocateCommandBuffers(device_, &info, &cmd_buffer_));
......@@ -176,7 +176,7 @@ void OpBase::runCommandBuffer()
fence_create_info_.flags = 0;
VK_CHECK_RESULT(vkCreateFence(device_, &fence_create_info_, NULL, &fence));
VK_CHECK_RESULT(vkQueueSubmit(ctx_->queue, 1, &submit_info, fence));
VK_CHECK_RESULT(vkQueueSubmit(kQueue, 1, &submit_info, fence));
VK_CHECK_RESULT(vkWaitForFences(device_, 1, &fence, VK_TRUE, 100000000000));
vkDestroyFence(device_, fence, NULL);
}
......
......@@ -15,15 +15,15 @@ namespace cv { namespace dnn { namespace vkcom {
Tensor::Tensor(Format fmt) : size_in_byte_(0), format_(fmt)
{
Context *ctx = getContext();
device_ = ctx->device;
createContext();
device_ = kDevice;
}
Tensor::Tensor(const char* data, std::vector<int>& shape, Format fmt)
: size_in_byte_(0), format_(fmt)
{
Context *ctx = getContext();
device_ = ctx->device;
createContext();
device_ = kDevice;
reshape(data, shape);
}
......
......@@ -8,6 +8,10 @@
#ifndef OPENCV_DNN_VKCOM_VULKAN_VK_LOADER_HPP
#define OPENCV_DNN_VKCOM_VULKAN_VK_LOADER_HPP
#ifdef HAVE_VULKAN
#include <vulkan/vulkan.h>
#endif // HAVE_VULKAN
namespace cv { namespace dnn { namespace vkcom {
#ifdef HAVE_VULKAN
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment