Skip to content

Commit

Permalink
Merge pull request #26 from vsg-dev/RefactorInstanceExtensionSurfaceName
Browse files Browse the repository at this point in the history
Refactored ViewerWindow so that it always uses QSurface rather than a use vsg::Surface support as fallback.  Added support for wayland. Added support for using to modern vsg::WindowTraits capabilities.
  • Loading branch information
robertosfield authored Apr 22, 2023
2 parents d77cb88 + 8803952 commit c0cb810
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 88 deletions.
11 changes: 3 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ find_package(vsg 1.0.0)
vsg_setup_dir_vars()
vsg_setup_build_vars()


find_package(vsgXchange) # only used by exanples

# if Qt5 then we need 5.10 or later
find_package(${QT_PACKAGE_NAME} COMPONENTS Widgets REQUIRED)

vsg_add_target_clang_format(
Expand Down Expand Up @@ -60,14 +61,8 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(WIN32)
add_definitions(-DVK_USE_PLATFORM_WIN32_KHR -DNOMINMAX)
add_definitions(-DNOMINMAX)
set(MODE WIN32)
elseif(APPLE)
add_definitions(-DVK_USE_PLATFORM_MACOS_MVK)
elseif(UNIX)
add_definitions(-DVK_USE_PLATFORM_XCB_KHR)
else()
add_definitions(-DVK_USE_PLATFORM_XLIB_KHR)
endif()

add_subdirectory(src/vsgQt)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Qt, and hence vsgQt, is suited for interactive, event driven applications. For r
* [VulkanSDK](https://www.lunarg.com/vulkan-sdk/) version 1.2.162 or later
* [VulkanSceneGraph](https://github.com/vsg-dev/VulkanSceneGraph) master recommended.
* [CMake](https://cmake.org/) version 3.7 or later
* [Qt](https://www.qt.io/) version 5 or later
* [Qt](https://www.qt.io/) version 5.10 or later
* C++17 capable compiler

## Building vsgQt
Expand Down
97 changes: 18 additions & 79 deletions src/vsgQt/ViewerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <QWindow>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QVulkanInstance>

#include <vulkan/vulkan.h>

#include <vsgQt/ViewerWindow.h>

#include <vsg/app/WindowAdapter.h>

#if QT_HAS_VULKAN_SUPPORT
# include <QVulkanInstance>
#endif
#include <vsgQt/ViewerWindow.h>

#include <iostream>


using namespace vsgQt;

const char* instanceExtensionSurfaceName()
{
#if defined(VK_USE_PLATFORM_WIN32_KHR)
return VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
return VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
#elif defined(VK_USE_PLATFORM_XCB_KHR)
return VK_KHR_XCB_SURFACE_EXTENSION_NAME;
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
return VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
#endif
auto platformName = qGuiApp->platformName();
std::cout<<"qGuiApp->platformName() "<<platformName.toStdString()<<std::endl;

if (platformName == "xcb") return "VK_KHR_xcb_surface";
else if (platformName == "wayland") return "VK_KHR_wayland_surface";
else if (platformName == "windows") return "VK_KHR_win32_surface";
else if (platformName == "cocoa") return "VK_MVK_macos_surface";
else if (platformName == "ios") return "VK_EXT_metal_surface";
else if (platformName == "android") return "VK_KHR_android_surface";
else return "VK_KHR_xlib_surface"; // not clear from platformName() docs how xlib would map as it's not mention.
}

ViewerWindow::ViewerWindow() :
Expand All @@ -61,16 +57,7 @@ void ViewerWindow::cleanup()
// remove links to all the VSG related classes.
if (windowAdapter)
{
#if QT_HAS_VULKAN_SUPPORT
if (surfaceType() == QSurface::VulkanSurface)
{
windowAdapter->getSurface()->release();
}
else
#endif
{
windowAdapter->releaseWindow();
}
windowAdapter->getSurface()->release();
}

windowAdapter = {};
Expand Down Expand Up @@ -137,33 +124,20 @@ bool ViewerWindow::event(QEvent* e)

void ViewerWindow::intializeUsingAdapterWindow(uint32_t width, uint32_t height)
{
#if QT_HAS_VULKAN_SUPPORT
_initialized = true;

traits->width = width;
traits->height = height;
traits->fullscreen = false;

// create instance
vsg::Names instanceExtensions;
vsg::Names requestedLayers;
traits->validate();

vsg::Names& instanceExtensions = traits->instanceExtensionNames;

instanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
instanceExtensions.push_back("VK_KHR_surface");
instanceExtensions.push_back(instanceExtensionSurfaceName());

if (traits->debugLayer || traits->apiDumpLayer)
{
instanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
requestedLayers.push_back("VK_LAYER_KHRONOS_validation");
if (traits->apiDumpLayer)
requestedLayers.push_back("VK_LAYER_LUNARG_api_dump");
}

vsg::Names validatedNames =
vsg::validateInstancelayerNames(requestedLayers);

instance = vsg::Instance::create(instanceExtensions, validatedNames);
instance = vsg::Instance::create(instanceExtensions, traits->requestedLayers, traits->vulkanVersion);

// create Qt wrapper of vkInstance
auto vulkanInstance = new QVulkanInstance;
Expand All @@ -184,51 +158,16 @@ void ViewerWindow::intializeUsingAdapterWindow(uint32_t width, uint32_t height)
{
delete vulkanInstance;
}
#else
vsg::info("ViewerWindow::intializeUsingAdapterWindow(", width, ", ", height, ") not supported, requires Qt 5.10 or later.");
#endif
}

void ViewerWindow::intializeUsingVSGWindow(uint32_t width, uint32_t height)
{
_initialized = true;

#if defined(VK_USE_PLATFORM_WIN32_KHR)
traits->nativeWindow = reinterpret_cast<HWND>(winId());
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
traits->nativeWindow = static_cast<::Window>(winId());
#elif defined(VK_USE_PLATFORM_XCB_KHR)
traits->nativeWindow = static_cast<xcb_window_t>(winId());
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
traits->nativeWindow = reinterpret_cast<NSView*>(winId()); // or NSWindow* ?
#endif

traits->width = width;
traits->height = height;

windowAdapter = vsg::Window::create(traits);
}

void ViewerWindow::exposeEvent(QExposeEvent* /*e*/)
{
if (!_initialized && isExposed())
{
#if QT_HAS_VULKAN_SUPPORT
if (surfaceType() == QSurface::VulkanSurface)
{
vsg::info("Using QSurface");
intializeUsingAdapterWindow(convert_coord(width()), convert_coord(height()));
}
else
#endif
{
vsg::info("Using vsg::Surface");
intializeUsingVSGWindow(convert_coord(width()), convert_coord(height()));
}
intializeUsingAdapterWindow(convert_coord(width()), convert_coord(height()));

if (initializeCallback) initializeCallback(*this, convert_coord(width()), convert_coord(height()));


requestUpdate();
}

Expand Down

0 comments on commit c0cb810

Please sign in to comment.