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

Added support for using vsg::Surface natively or QSurface::VulkanSurface/QSurface #7

Merged
merged 12 commits into from
Sep 24, 2021
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
15 changes: 10 additions & 5 deletions examples/vsgqtviewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ int main(int argc, char* argv[])
windowTraits->fullscreen = false;
}
auto horizonMountainHeight = arguments.value(0.0, "--hmh");
bool useQtSurface = !arguments.read("--vsg");

if (arguments.errors())
return arguments.writeErrorMessages(std::cerr);
Expand Down Expand Up @@ -63,10 +64,14 @@ int main(int argc, char* argv[])
QMainWindow* mainWindow = new QMainWindow();

auto* viewerWindow = new vsgQt::ViewerWindow();

// if required set the QWindow's SurfaceType to QSurface::VulkanSurface.
if (useQtSurface) viewerWindow->setSurfaceType(QSurface::VulkanSurface);

viewerWindow->traits = windowTraits;

// provide the calls to set up the vsg::Viewer that will be used to render to the QWindow subclass vsgQt::ViewerWindow
viewerWindow->initializeCallback = [&](vsgQt::ViewerWindow& vw) {
viewerWindow->initializeCallback = [&](vsgQt::ViewerWindow& vw, uint32_t width, uint32_t height) {

auto& window = vw.windowAdapter;
if (!window) return false;
Expand All @@ -92,16 +97,16 @@ int main(int argc, char* argv[])
{
perspective = vsg::EllipsoidPerspective::create(
lookAt, ellipsoidModel, 30.0,
static_cast<double>(window->extent2D().width) /
static_cast<double>(window->extent2D().height),
static_cast<double>(width) /
static_cast<double>(height),
nearFarRatio, horizonMountainHeight);
}
else
{
perspective = vsg::Perspective::create(
30.0,
static_cast<double>(window->extent2D().width) /
static_cast<double>(window->extent2D().height),
static_cast<double>(width) /
static_cast<double>(height),
nearFarRatio * radius, radius * 4.5);
}

Expand Down
10 changes: 6 additions & 4 deletions include/vsgQt/ViewerWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <QVulkanInstance>
#include <QWindow>

#include <vsg/viewer/WindowAdapter.h>
#include <vsg/viewer/WindowAdapter.h>

#include <vsgQt/KeyboardMap.h>
Expand All @@ -34,23 +33,26 @@ namespace vsgQt
vsg::ref_ptr<vsg::Instance> instance;
vsg::ref_ptr<vsg::Viewer> viewer;

vsg::ref_ptr<vsg::WindowAdapter> windowAdapter;
vsg::ref_ptr<vsg::Window> windowAdapter;
vsg::ref_ptr<KeyboardMap> keyboardMap;

using InitialCallback = std::function<void(ViewerWindow&)>;
using InitialCallback = std::function<void(ViewerWindow&, uint32_t width, uint32_t height)>;
InitialCallback initializeCallback;

using FrameCallback = std::function<bool(ViewerWindow&)>;
FrameCallback frameCallback;

protected:
void intializeUsingAdapterWindow(uint32_t width, uint32_t height);
void intializeUsingVSGWindow(uint32_t width, uint32_t height);

void render();
void cleanup();

bool event(QEvent* e) override;

void exposeEvent(QExposeEvent*) override;
void hideEvent(QHideEvent *ev) override;
void hideEvent(QHideEvent* ev) override;

void keyPressEvent(QKeyEvent*) override;
void keyReleaseEvent(QKeyEvent*) override;
Expand Down
128 changes: 77 additions & 51 deletions src/vsgQt/ViewerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

#include <vsg/all.h>
#ifdef vsgXchange_FOUND
#include <vsgXchange/all.h>
# include <vsgXchange/all.h>
#endif

#include <QPlatformSurfaceEvent>
Expand Down Expand Up @@ -43,7 +43,6 @@ const char* instanceExtensionSurfaceName()
ViewerWindow::ViewerWindow() :
QWindow()
{
setSurfaceType(QSurface::VulkanSurface);
keyboardMap = vsgQt::KeyboardMap::create();
}

Expand All @@ -57,7 +56,7 @@ ViewerWindow::~ViewerWindow()
void ViewerWindow::cleanup()
{
// remove links to all the VSG related classes.
if (windowAdapter)
if (windowAdapter && surfaceType() == QSurface::VulkanSurface)
{
windowAdapter->getSurface()->release();
}
Expand Down Expand Up @@ -111,10 +110,9 @@ bool ViewerWindow::event(QEvent* e)
render();
break;

case QEvent::PlatformSurface:
{
case QEvent::PlatformSurface: {
auto surfaceEvent = dynamic_cast<QPlatformSurfaceEvent*>(e);
if (surfaceEvent->surfaceEventType()==QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
if (surfaceEvent->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
{
cleanup();
}
Expand All @@ -127,74 +125,99 @@ bool ViewerWindow::event(QEvent* e)
return QWindow::event(e);
}


void ViewerWindow::exposeEvent(QExposeEvent* e)
void ViewerWindow::intializeUsingAdapterWindow(uint32_t width, uint32_t height)
{
if (!_initialized && isExposed())
{
_initialized = true;
_initialized = true;

const auto rect = e->region().boundingRect();
const uint32_t width = static_cast<uint32_t>(rect.width());
const uint32_t height = static_cast<uint32_t>(rect.height());
traits->width = width;
traits->height = height;
traits->fullscreen = false;

traits->width = width;
traits->height = height;
traits->fullscreen = false;
// create instance
vsg::Names instanceExtensions;
vsg::Names requestedLayers;

// create instance
vsg::Names instanceExtensions;
vsg::Names requestedLayers;
instanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
instanceExtensions.push_back("VK_KHR_surface");
instanceExtensions.push_back(instanceExtensionSurfaceName());

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");
}

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);

vsg::Names validatedNames =
vsg::validateInstancelayerNames(requestedLayers);
instance = vsg::Instance::create(instanceExtensions, validatedNames);

instance = vsg::Instance::create(instanceExtensions, validatedNames);
// create Qt wrapper of vkInstance
vulkanInstance = new QVulkanInstance;
vulkanInstance->setVkInstance(*instance);

// create Qt wrapper of vkInstance
vulkanInstance = new QVulkanInstance;
vulkanInstance->setVkInstance(*instance);
if (vulkanInstance->create())
{
// set up the window for Vulkan usage
setVulkanInstance(vulkanInstance);

if (vulkanInstance->create())
{
// set up the window for Vulkan usage
setVulkanInstance(vulkanInstance);
auto surface = vsg::Surface::create(QVulkanInstance::surfaceForWindow(this), instance);
windowAdapter = new vsg::WindowAdapter(surface, traits);

auto surface = vsg::Surface::create(QVulkanInstance::surfaceForWindow(this), instance);
windowAdapter = new vsg::WindowAdapter(surface, traits);
// vsg::clock::time_point event_time = vsg::clock::now();
// windowAdapter->bufferedEvents.emplace_back(new vsg::ExposeWindowEvent(windowAdapter, event_time, rect.x(), rect.y(), width, height));
}
}

vsg::clock::time_point event_time = vsg::clock::now();
windowAdapter->bufferedEvents.emplace_back(new vsg::ExposeWindowEvent(windowAdapter, event_time, rect.x(), rect.y(), width, height));
void ViewerWindow::intializeUsingVSGWindow(uint32_t width, uint32_t height)
{
_initialized = true;

if (initializeCallback) initializeCallback(*this);
traits->nativeWindow = static_cast<xcb_window_t>(winId());
traits->width = width;
traits->height = height;

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

void ViewerWindow::exposeEvent(QExposeEvent* e)
{
if (!_initialized && isExposed())
{
const auto rect = e->region().boundingRect();
const uint32_t width = static_cast<uint32_t>(rect.width());
const uint32_t height = static_cast<uint32_t>(rect.height());

if (surfaceType() == QSurface::VulkanSurface)
{
std::cout << "Using QSurface" << std::endl;
intializeUsingAdapterWindow(width, height);
}
else
{
std::cout << "Using vsg::Surface" << std::endl;
intializeUsingVSGWindow(width, height);
}

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

requestUpdate();
}

if (windowAdapter)
if (auto adapter = windowAdapter.cast<vsg::WindowAdapter>(); adapter)
{
windowAdapter->windowValid = true;
windowAdapter->windowVisible = isExposed();
adapter->windowValid = true;
adapter->windowVisible = isExposed();
}
}

void ViewerWindow::hideEvent(QHideEvent* /*e*/)
{
if (windowAdapter)
if (auto adapter = windowAdapter.cast<vsg::WindowAdapter>(); adapter)
{
windowAdapter->windowVisible = false;
adapter->windowVisible = false;
}
}

Expand All @@ -205,7 +228,10 @@ void ViewerWindow::resizeEvent(QResizeEvent* e)
// std::cout << __func__ << std::endl;

// WindowAdapter
windowAdapter->updateExtents(e->size().width(), e->size().height());
if (auto adapter = windowAdapter.cast<vsg::WindowAdapter>(); adapter)
{
adapter->updateExtents(e->size().width(), e->size().height());
}

vsg::clock::time_point event_time = vsg::clock::now();
windowAdapter->bufferedEvents.emplace_back(new vsg::ConfigureWindowEvent(windowAdapter, event_time, x(), y(), static_cast<uint32_t>(e->size().width()), static_cast<uint32_t>(e->size().height())));
Expand Down