Skip to content

Commit

Permalink
Qt5.15 compatible graphics updates
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Nov 13, 2023
1 parent 8eba03e commit 22cb41a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
17 changes: 8 additions & 9 deletions src/plugins/minimal_scene/MinimalScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include <gz/common/Console.hh>
Expand All @@ -52,17 +53,10 @@
#include "gz/gui/Helpers.hh"
#include "gz/gui/MainWindow.hh"

#define GZ_GUI_HAVE_VULKAN \
QT_VERSION >= QT_VERSION_CHECK(5, 15, 2) && \
QT_CONFIG(vulkan) && \
GZ_RENDERING_HAVE_VULKAN

#define GZ_GUI_HAVE_METAL __APPLE__

#if GZ_GUI_HAVE_VULKAN
# include <QVulkanInstance>
# include <gz/rendering/RenderEngineVulkanExternalDeviceStructs.hh>
#endif // GZ_HAVE_VULKAN
#endif // GZ_GUI_HAVE_VULKAN

Q_DECLARE_METATYPE(gz::gui::plugins::RenderSync*)

Expand Down Expand Up @@ -1189,7 +1183,12 @@ QSGNode *RenderWindowItem::updatePaintNode(QSGNode *_node,

if (this->dataPtr->graphicsAPI == rendering::GraphicsAPI::OPENGL)
{
QOpenGLContext *current = this->window()->openglContext();
auto *rif = this->window()->rendererInterface();
Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::OpenGL);

auto *current = static_cast<QOpenGLContext*>(
rif->getResource(this->window(), QSGRendererInterface::OpenGLContextResource));

// Some GL implementations require that the currently bound context is
// made non-current before we set up sharing, so we doneCurrent here
// and makeCurrent down below while setting up our own context.
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/minimal_scene/MinimalSceneRhi.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#include <QSGTexture>
#include <QSize>

#define GZ_GUI_HAVE_VULKAN \
QT_VERSION >= QT_VERSION_CHECK(5, 15, 2) && \
QT_CONFIG(vulkan) && \
GZ_RENDERING_HAVE_VULKAN

#define GZ_GUI_HAVE_METAL __APPLE__

namespace gz::gui::plugins
{
/// \brief Render interface class to handle OpenGL / Metal compatibility
Expand Down
43 changes: 27 additions & 16 deletions src/plugins/minimal_scene/MinimalSceneRhiOpenGL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "MinimalSceneRhiOpenGL.hh"
#include <qsgtexture_platform.h>

#include "EngineToQtInterface.hh"
#include "MinimalScene.hh"
Expand Down Expand Up @@ -59,6 +60,27 @@ namespace gz::gui::plugins
public: QMutex mutex;
public: QSGTexture *texture = nullptr;
public: QQuickWindow *window = nullptr;

public: void CreateTexture(GLuint *_id, QSize _size) {
delete this->texture;
this->texture = nullptr;


#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
this->texture = QNativeInterface::QSGOpenGLTexture::fromNative(
*_id,
this->window,
_size);
#else
this->texture =
this->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void*>(_id),
0,
_size);
#endif
}

};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -206,12 +228,8 @@ TextureNodeRhiOpenGL::TextureNodeRhiOpenGL(QQuickWindow *_window)
this->dataPtr->window = _window;

// Our texture node must have a texture, so use the default 0 texture.
this->dataPtr->texture =
this->dataPtr->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void*>(&this->dataPtr->textureId),
0,
QSize(1, 1));
this->dataPtr->CreateTexture(
&this->dataPtr->textureId, QSize(1, 1));
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -246,17 +264,10 @@ void TextureNodeRhiOpenGL::PrepareNode()
this->dataPtr->textureId = 0;
this->dataPtr->mutex.unlock();

if (this->dataPtr->newTextureId)
if (this->dataPtr->newTextureId != 0)
{
delete this->dataPtr->texture;
this->dataPtr->texture = nullptr;

this->dataPtr->texture =
this->dataPtr->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void*>(&this->dataPtr->newTextureId),
0,
this->dataPtr->newSize);
this->dataPtr->CreateTexture(
&this->dataPtr->newTextureId, this->dataPtr->newSize);
}
}
} // namespace gz::gui::plugins
42 changes: 27 additions & 15 deletions src/plugins/minimal_scene/MinimalSceneRhiVulkan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "MinimalSceneRhiVulkan.hh"

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 2) && QT_CONFIG(vulkan)
#if GZ_GUI_HAVE_VULKAN

#include "EngineToQtInterface.hh"
#include "MinimalScene.hh"
Expand Down Expand Up @@ -60,8 +60,29 @@ class TextureNodeRhiVulkanPrivate
public: QMutex mutex;
public: QSGTexture *texture = nullptr;
public: QQuickWindow *window = nullptr;

public: void CreateTexture(VkImage *_id, QSize _size) {
delete this->texture;
this->texture = nullptr;


#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
this->texture = QNativeInterface::QSGVulkanTexture::fromNative(
*_id,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
this->window,
_size);
#else
this->texture = this->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void *>(_id), //
VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
_size);
#endif
}
};


/////////////////////////////////////////////////
GzCameraTextureRhiVulkan::~GzCameraTextureRhiVulkan() = default;

Expand Down Expand Up @@ -182,10 +203,8 @@ TextureNodeRhiVulkan::TextureNodeRhiVulkan(QQuickWindow *_window,
_camera->RenderTextureMetalId(&this->dataPtr->textureId);
this->dataPtr->lastCamera = _camera;

this->dataPtr->texture = this->dataPtr->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void *>(&this->dataPtr->textureId), //
0, //
this->dataPtr->CreateTexture(
&this->dataPtr->textureId,
QSize(static_cast<int>(_camera->ImageWidth()),
static_cast<int>(_camera->ImageHeight())));
}
Expand Down Expand Up @@ -229,16 +248,9 @@ void TextureNodeRhiVulkan::PrepareNode()

if (this->dataPtr->newTextureId != nullptr)
{
delete this->dataPtr->texture;
this->dataPtr->texture = nullptr;

this->dataPtr->texture =
this->dataPtr->window->createTextureFromNativeObject(
QQuickWindow::NativeObjectTexture,
static_cast<void*>(&this->dataPtr->newTextureId),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
this->dataPtr->newSize);
this->dataPtr->CreateTexture(
&this->dataPtr->newTextureId, this->dataPtr->newSize);
}
}
} // namespace gz::gui::plugins
#endif // HAVE_QT_VULKAN
#endif // GZ_GUI_HAVE_VULKAN

0 comments on commit 22cb41a

Please sign in to comment.