Qt5/RHI supported

This commit is contained in:
Uwe Rathmann 2023-12-22 13:57:45 +01:00
parent e1069d9292
commit 6ad68a6da8
7 changed files with 86 additions and 50 deletions

View File

@ -34,14 +34,15 @@ namespace
class FilterNode : public TextureFilterNode class FilterNode : public TextureFilterNode
{ {
public: public:
FilterNode( QSGTexture* texture ) FilterNode( bool useRhi, QSGTexture* texture )
{ {
QString shaders[] = { ":/shaders/blur.vert", ":/shaders/blur.frag" }; QString shaders[] = { ":/shaders/blur.vert", ":/shaders/blur.frag" };
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) if ( useRhi )
shaders[0] += ".qsb"; {
shaders[1] += ".qsb"; shaders[0] += ".qsb";
#endif shaders[1] += ".qsb";
}
setFlag( QSGNode::OwnsMaterial, true ); setFlag( QSGNode::OwnsMaterial, true );
setTextureMaterial( new Material( shaders[0], shaders[1] ) ); setTextureMaterial( new Material( shaders[0], shaders[1] ) );
@ -150,7 +151,9 @@ namespace
QObject::connect( texture, &QskSceneTexture::updateRequested, QObject::connect( texture, &QskSceneTexture::updateRequested,
overlay, &QQuickItem::update ); overlay, &QQuickItem::update );
textureNode = new FilterNode( texture ); const bool useRhi = qskRenderingHardwareInterface( window );
textureNode = new FilterNode( useRhi, texture );
texture->setTextureNode( textureNode ); texture->setTextureNode( textureNode );
} }

View File

@ -9,6 +9,12 @@
#include <qsgmaterialshader.h> #include <qsgmaterialshader.h>
#include <qsgtexture.h> #include <qsgtexture.h>
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
#include <qsgmaterialrhishader.h>
using RhiShader = QSGMaterialRhiShader;
#else
using RhiShader = QSGMaterialShader;
#endif
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) #if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
@ -74,31 +80,26 @@ namespace
}; };
} }
QSGMaterialShader* TextureFilterMaterial::createShader() const #endif
{
Q_ASSERT( !( flags() & QSGMaterial::RhiShaderWanted ) );
auto shader = new ShaderGL();
shader->setSource( QOpenGLShader::Vertex, m_shaderSourceFiles[ 0 ] );
shader->setSource( QOpenGLShader::Fragment, m_shaderSourceFiles[ 1 ] );
return shader;
}
#else // Qt6
namespace namespace
{ {
class Shader : public QSGMaterialShader class ShaderRhi : public RhiShader
{ {
public: public:
Shader() ShaderRhi()
{ {
setFlag( UpdatesGraphicsPipelineState, true ); setFlag( UpdatesGraphicsPipelineState, true );
} }
void setSource( QSGMaterialShader::Stage stage, const QString& filename ) #if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
void setSource( QOpenGLShader::ShaderType type, const QString& fileName )
{
setShaderSourceFile( type, fileName );
}
#endif
void setSource( Stage stage, const QString& filename )
{ {
setShaderFileName( stage, filename ); setShaderFileName( stage, filename );
} }
@ -139,20 +140,50 @@ namespace
auto mat = dynamic_cast< TextureFilterMaterial* >( newMaterial ); auto mat = dynamic_cast< TextureFilterMaterial* >( newMaterial );
if ( auto txt = mat->texture() ) if ( auto txt = mat->texture() )
{ {
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
txt->updateRhiTexture( state.rhi(), state.resourceUpdateBatch() );
#else
txt->commitTextureOperations( state.rhi(), state.resourceUpdateBatch() ); txt->commitTextureOperations( state.rhi(), state.resourceUpdateBatch() );
#endif
*texture = txt; *texture = txt;
} }
} }
}; };
} }
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
QSGMaterialShader* TextureFilterMaterial::createShader() const
{
if ( flags() & QSGMaterial::RhiShaderWanted )
{
auto shader = new ShaderRhi();
shader->setSource( ShaderRhi::VertexStage, m_shaderFiles[ 0 ] );
shader->setSource( ShaderRhi::FragmentStage, m_shaderFiles[ 1 ] );
return shader;
}
else
{
auto shader = new ShaderGL();
shader->setSource( QOpenGLShader::Vertex, m_shaderFiles[ 0 ] );
shader->setSource( QOpenGLShader::Fragment, m_shaderFiles[ 1 ] );
return shader;
}
}
#else
QSGMaterialShader* TextureFilterMaterial::createShader( QSGMaterialShader* TextureFilterMaterial::createShader(
QSGRendererInterface::RenderMode ) const QSGRendererInterface::RenderMode ) const
{ {
auto shader = new Shader(); auto shader = new ShaderRhi();
shader->setSource( Shader::VertexStage, m_shaderSourceFiles[ 0 ] ); shader->setSource( ShaderRhi::VertexStage, m_shaderFiles[ 0 ] );
shader->setSource( Shader::FragmentStage, m_shaderSourceFiles[ 1 ] ); shader->setSource( ShaderRhi::FragmentStage, m_shaderFiles[ 1 ] );
return shader; return shader;
} }
@ -160,11 +191,14 @@ QSGMaterialShader* TextureFilterMaterial::createShader(
#endif #endif
TextureFilterMaterial::TextureFilterMaterial( TextureFilterMaterial::TextureFilterMaterial(
const QString& vertexShaderSourceFile, const QString& vertexShaderFile, const QString& fragmentShaderFile )
const QString& fragmentShaderSourceFile ) : m_shaderFiles{ vertexShaderFile, fragmentShaderFile }
: m_shaderSourceFiles{ vertexShaderSourceFile, fragmentShaderSourceFile }
{ {
setFlag( Blending | RequiresFullMatrix, true ); setFlag( Blending | RequiresFullMatrix, true );
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
setFlag( SupportsRhiShader, true );
#endif
} }
TextureFilterMaterial::~TextureFilterMaterial() TextureFilterMaterial::~TextureFilterMaterial()

View File

@ -32,5 +32,5 @@ class TextureFilterMaterial : public QSGMaterial
private: private:
QSGTexture* m_texture = nullptr; QSGTexture* m_texture = nullptr;
const QString m_shaderSourceFiles[ 2 ]; const QString m_shaderFiles[ 2 ];
}; };

View File

@ -202,32 +202,31 @@ namespace
void Renderer::createTarget( const QSize& size ) void Renderer::createTarget( const QSize& size )
{ {
==== BASE ==== if ( const auto rhi = context()->rhi() )
const auto rhi = context()->rhi(); {
==== BASE ==== auto flags = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
==== BASE ==== m_rhiTexture = rhi->newTexture( QRhiTexture::RGBA8, size, 1, flags );
auto flags = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource; #if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
==== BASE ==== m_rhiTexture->build();
#else
m_rhiTexture->create();
#endif
==== BASE ==== QRhiColorAttachment color0( m_rhiTexture );
m_rhiTexture = rhi->newTexture( QRhiTexture::RGBA8, size, 1, flags ); auto target = rhi->newTextureRenderTarget( { color0 } );
m_rhiTexture->create();
==== BASE ====
==== BASE ==== target->setRenderPassDescriptor(
QRhiColorAttachment color0( m_rhiTexture ); target->newCompatibleRenderPassDescriptor() );
auto target = rhi->newTextureRenderTarget( { color0 } );
==== BASE ====
==== BASE ==== #if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
target->setRenderPassDescriptor( target->build();
target->newCompatibleRenderPassDescriptor() ); #else
==== BASE ==== target->create();
#endif
==== BASE ==== m_rt.rt = target;
target->create(); m_rt.rpDesc = target->renderPassDescriptor();
==== BASE ====
auto defaultContext = qobject_cast< QSGDefaultRenderContext* >( context() ); auto defaultContext = qobject_cast< QSGDefaultRenderContext* >( context() );
m_rt.cb = defaultContext->currentFrameCommandBuffer(); m_rt.cb = defaultContext->currentFrameCommandBuffer();