Skip to content

Commit

Permalink
[fix]: 增强Vulkan支持检测及运行时处理
Browse files Browse the repository at this point in the history
- **添加Vulkan运行时支持检测**:通过`VulkanViewer::isSupported()`静态方法检查系统是否支持Vulkan,避免在不支持的环境下初始化相关组件。
- **条件化Vulkan组件创建**:仅在检测到Vulkan支持时创建`VulkanViewer`实例,防止运行时崩溃。
- **动态控制菜单项显示**:若Vulkan不可用,菜单中对应的"Vulkan Viewer"选项将不会显示。
- **优化Vulkan实例管理**:在`VulkanView`中增加`isVulkanSupported()`全局检测函数,输出Vulkan版本信息以便调试。
- **修复潜在空指针问题**:将`vulkanViewer`指针初始化为`nullptr`,并增加判空保护逻辑。
  • Loading branch information
RealChuan committed Feb 20, 2025
1 parent 750baac commit 18005a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
16 changes: 10 additions & 6 deletions examples/graphics/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class MainWindow::MainWindowPrivate
stackedWidget->addWidget(subtitlSplicingWidget);
stackedWidget->addWidget(openglViewer);
#ifdef BUILD_VULKAN
vulkanViewer = new VulkanViewer(q_ptr);
stackedWidget->addWidget(vulkanViewer);
if (VulkanViewer::isSupported()) {
vulkanViewer = new VulkanViewer(q_ptr);
stackedWidget->addWidget(vulkanViewer);
}
#endif
}
~MainWindowPrivate() {}
Expand All @@ -41,7 +43,7 @@ class MainWindow::MainWindowPrivate
ImageViewer *imageViewer;
OpenglViewer *openglViewer;
#ifdef BUILD_VULKAN
VulkanViewer *vulkanViewer;
VulkanViewer *vulkanViewer = nullptr;
#endif
SubtitlSplicingWidget *subtitlSplicingWidget;
QStackedWidget *stackedWidget;
Expand Down Expand Up @@ -99,9 +101,11 @@ void MainWindow::initMenuBar()
d_ptr->stackedWidget->setCurrentWidget(d_ptr->openglViewer);
}));
#ifdef BUILD_VULKAN
setCheckable(menu->addAction(tr("Vulakn Viewer"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->vulkanViewer);
}));
if (nullptr != d_ptr->vulkanViewer) {
setCheckable(menu->addAction(tr("Vulakn Viewer"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->vulkanViewer);
}));
}
#endif
setCheckable(menu->addAction(tr("Draw"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->drawWidget);
Expand Down
6 changes: 6 additions & 0 deletions examples/graphics/vulkanviewer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ VulkanViewer::~VulkanViewer()
clearThumbnail();
}

bool VulkanViewer::isSupported()
{
static bool ret = GpuGraphics::isVulkanSupported();
return ret;
}

void VulkanViewer::onOpenImage()
{
const auto filename = openImage();
Expand Down
2 changes: 2 additions & 0 deletions examples/graphics/vulkanviewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class VulkanViewer : public Viewer
explicit VulkanViewer(QWidget *parent = nullptr);
~VulkanViewer() override;

static bool isSupported();

private slots:
void onOpenImage();
void onChangedImage(int index);
Expand Down
12 changes: 12 additions & 0 deletions src/gpugraphics/vulkanview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

namespace GpuGraphics {

bool isVulkanSupported()
{
QVulkanInstance inst;
if (inst.create()) {
inst.destroy();
return true;
}
qWarning() << "Vulkan is not supported, error code:" << inst.errorCode();
return false;
}

class VulkanView::VulkanViewPrivate
{
public:
Expand All @@ -28,6 +39,7 @@ class VulkanView::VulkanViewPrivate
if (!inst.create()) {
qFatal("Failed to create Vulkan instance: %d", inst.errorCode());
}
qInfo() << "Vulkan Version: " << inst.apiVersion();
q_ptr->setVulkanInstance(&inst);

menuPtr.reset(new QMenu);
Expand Down
2 changes: 2 additions & 0 deletions src/gpugraphics/vulkanview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace GpuGraphics {

GPUAPHICS bool isVulkanSupported();

class VulkanRenderer;

class GPUAPHICS VulkanView : public QVulkanWindow
Expand Down

0 comments on commit 18005a5

Please sign in to comment.