Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5741 don't crash when vkCreateDevice fails #5742

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2212,10 +2212,7 @@ class VkDummyCompute : public VkCompute
class VulkanDevicePrivate
{
public:
VulkanDevicePrivate(VulkanDevice* _vkdev)
: vkdev(_vkdev)
{
}
VulkanDevicePrivate(VulkanDevice* _vkdev);
VulkanDevice* const vkdev;

// dummy buffer and image
Expand Down Expand Up @@ -2270,8 +2267,22 @@ class VulkanDevicePrivate
// to pack1 | pack4 | pack8
mutable ncnn::Packing_vulkan* uop_packing[2][2][3][3][3];
mutable Mutex uop_lock;

// device is valid and sucessfully initialized
bool valid;
};

VulkanDevicePrivate::VulkanDevicePrivate(VulkanDevice* _vkdev)
: vkdev(_vkdev)
{
device = 0;
texelfetch_sampler = 0;
dummy_allocator = 0;
pipeline_cache = 0;
valid = false;
memset(uop_packing, 0, sizeof(uop_packing));
}

int VulkanDevicePrivate::create_dummy_buffer_image()
{
dummy_allocator = new VkDummyAllocator(vkdev);
Expand Down Expand Up @@ -2310,7 +2321,11 @@ void VulkanDevicePrivate::destroy_dummy_buffer_image()
dummy_image_readonly.release();
#endif

delete dummy_allocator;
if (dummy_allocator)
{
delete dummy_allocator;
dummy_allocator = 0;
}
}

const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storage_type_from, int storage_type_to, int cast_type_from_index, int cast_type_to_index, int packing_type_to_index) const
Expand Down Expand Up @@ -2653,6 +2668,7 @@ VulkanDevice::VulkanDevice(int device_index)
if (ret != VK_SUCCESS)
{
NCNN_LOGE("vkCreateDevice failed %d", ret);
return;
}

init_device_extension();
Expand Down Expand Up @@ -2712,7 +2728,6 @@ VulkanDevice::VulkanDevice(int device_index)
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
samplerCreateInfo.unnormalizedCoordinates = VK_TRUE;

d->texelfetch_sampler = 0;
ret = vkCreateSampler(d->device, &samplerCreateInfo, 0, &d->texelfetch_sampler);
if (ret != VK_SUCCESS)
{
Expand All @@ -2724,11 +2739,12 @@ VulkanDevice::VulkanDevice(int device_index)
if (cret != 0)
{
NCNN_LOGE("VulkanDevice create_dummy_buffer_image failed %d", cret);
return;
}

d->pipeline_cache = new PipelineCache(this);

memset(d->uop_packing, 0, sizeof(d->uop_packing));
d->valid = true;
}

VulkanDevice::~VulkanDevice()
Expand All @@ -2753,9 +2769,15 @@ VulkanDevice::~VulkanDevice()
}
d->staging_allocators.clear();

delete d->pipeline_cache;
if (d->pipeline_cache)
{
delete d->pipeline_cache;
}

vkDestroyDevice(d->device, 0);
if (d->device)
{
vkDestroyDevice(d->device, 0);
}

delete d;
}
Expand All @@ -2775,6 +2797,11 @@ VkDevice VulkanDevice::vkdevice() const
return d->device;
}

bool VulkanDevice::is_valid() const
{
return d->valid;
}

VkShaderModule VulkanDevice::compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const
{
VkShaderModuleCreateInfo shaderModuleCreateInfo;
Expand Down
1 change: 1 addition & 0 deletions src/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class NCNN_EXPORT VulkanDevice
const GpuInfo& info;

VkDevice vkdevice() const;
bool is_valid() const;

VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const;

Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ int Net::load_param(const DataReader& dr)
if (opt.use_vulkan_compute)
{
if (!d->vkdev) d->vkdev = get_gpu_device();
if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
nihui marked this conversation as resolved.
Show resolved Hide resolved
}
if (opt.use_vulkan_compute)
{
Expand Down Expand Up @@ -1677,7 +1677,7 @@ int Net::load_param_bin(const DataReader& dr)
if (opt.use_vulkan_compute)
{
if (!d->vkdev) d->vkdev = get_gpu_device();
if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
}
if (opt.use_vulkan_compute)
{
Expand Down
Loading