2017-07-21 16:21:34 +00:00
|
|
|
#include "QskTextureNode.h"
|
2021-12-13 15:43:34 +00:00
|
|
|
#include "QskFunctions.h"
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
#include <qopenglfunctions.h>
|
2018-07-19 12:10:48 +00:00
|
|
|
#include <qsggeometry.h>
|
|
|
|
#include <qsgmaterial.h>
|
2020-11-01 14:44:15 +00:00
|
|
|
#include <qquickwindow.h>
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
QSK_QT_PRIVATE_BEGIN
|
2017-07-21 16:21:34 +00:00
|
|
|
#include <private/qsgnode_p.h>
|
2020-11-01 14:44:15 +00:00
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
|
|
|
|
|
|
|
|
#include <qsgtexture.h>
|
|
|
|
#include <qsgtexturematerial.h>
|
|
|
|
#include <qsgtexture_platform.h>
|
|
|
|
|
|
|
|
QSK_QT_PRIVATE_BEGIN
|
|
|
|
#include <private/qrhi_p.h>
|
|
|
|
#include <private/qrhigles2_p_p.h>
|
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
|
|
|
static void qskUpdateGLTextureId( QRhiTexture* rhiTexture, uint textureId )
|
|
|
|
{
|
|
|
|
// hack time: we do not want to create a new QSGTexture object for each texture
|
|
|
|
|
|
|
|
class Texture : public QRhiTexture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GLuint texture;
|
|
|
|
// ...
|
|
|
|
};
|
|
|
|
|
|
|
|
GLuint id = rhiTexture->nativeTexture().object;
|
|
|
|
|
|
|
|
if ( id )
|
|
|
|
{
|
|
|
|
auto funcs = QOpenGLContext::currentContext()->functions();
|
|
|
|
funcs->glDeleteTextures( 1, &id );
|
|
|
|
}
|
2020-12-05 14:09:31 +00:00
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
auto glTexture = static_cast< Texture* >( rhiTexture );
|
|
|
|
glTexture->texture = textureId;
|
|
|
|
|
|
|
|
Q_ASSERT( rhiTexture->nativeTexture().object == textureId );
|
|
|
|
}
|
|
|
|
|
|
|
|
using TextureMaterial = QSGTextureMaterial;
|
|
|
|
using OpaqueTextureMaterial = QSGOpaqueTextureMaterial;
|
|
|
|
|
|
|
|
static inline void qskDeleteTexture( const TextureMaterial& material )
|
|
|
|
{
|
|
|
|
delete material.texture();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class MaterialShader final : public QSGMaterialShader
|
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
public:
|
2017-07-21 16:21:34 +00:00
|
|
|
MaterialShader( bool isOpaque );
|
|
|
|
|
2018-07-31 15:32:25 +00:00
|
|
|
char const* const* attributeNames() const override;
|
|
|
|
void updateState( const RenderState&, QSGMaterial*, QSGMaterial* ) override;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
protected:
|
2018-07-31 15:32:25 +00:00
|
|
|
void initialize() override;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
private:
|
2019-05-16 06:14:32 +00:00
|
|
|
int m_matrixId = -1;
|
|
|
|
int m_opacityId = -1;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
const bool m_isOpaque : 1;
|
|
|
|
};
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
class Material : public QSGMaterial
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
public:
|
2017-07-21 16:21:34 +00:00
|
|
|
Material( bool isOpaque );
|
|
|
|
|
2018-07-31 15:32:25 +00:00
|
|
|
QSGMaterialType* type() const override;
|
|
|
|
QSGMaterialShader* createShader() const override;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2017-12-07 16:04:05 +00:00
|
|
|
void setTextureId( uint );
|
|
|
|
uint textureId() const;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
int compare( const QSGMaterial* ) const override;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
private:
|
2020-11-01 08:28:20 +00:00
|
|
|
uint m_textureId = 0;
|
2017-07-21 16:21:34 +00:00
|
|
|
const bool m_isOpaque : 1;
|
|
|
|
};
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
class TextureMaterial final : public Material
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TextureMaterial()
|
|
|
|
: Material( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class OpaqueTextureMaterial final : public Material
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OpaqueTextureMaterial()
|
|
|
|
: Material( true )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
MaterialShader::MaterialShader( bool isOpaque )
|
|
|
|
: m_isOpaque( isOpaque )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
setShaderSourceFile( QOpenGLShader::Vertex,
|
|
|
|
QStringLiteral( ":/qt-project.org/scenegraph/shaders/opaquetexture.vert" ) );
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2020-11-01 08:28:20 +00:00
|
|
|
const auto fragmentShaderFile = m_isOpaque
|
|
|
|
? QStringLiteral( ":/qt-project.org/scenegraph/shaders/opaquetexture.frag" )
|
|
|
|
: QStringLiteral( ":/qt-project.org/scenegraph/shaders/texture.frag" );
|
|
|
|
|
|
|
|
setShaderSourceFile( QOpenGLShader::Fragment, fragmentShaderFile );
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char const* const* MaterialShader::attributeNames() const
|
|
|
|
{
|
|
|
|
static char const* const attr[] = { "qt_VertexPosition", "qt_VertexTexCoord", 0 };
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
void MaterialShader::updateState(
|
|
|
|
const RenderState& state, QSGMaterial* newMaterial, QSGMaterial* oldMaterial )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
if ( !m_isOpaque && state.isOpacityDirty() )
|
|
|
|
program()->setUniformValue( m_opacityId, state.opacity() );
|
|
|
|
|
|
|
|
auto* materialOld = static_cast< Material* >( oldMaterial );
|
|
|
|
auto* materialNew = static_cast< Material* >( newMaterial );
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
if ( ( materialOld == nullptr ) ||
|
2019-01-04 12:42:16 +00:00
|
|
|
( materialOld->textureId() != materialNew->textureId() ) )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
auto funcs = QOpenGLContext::currentContext()->functions();
|
|
|
|
funcs->glBindTexture( GL_TEXTURE_2D, materialNew->textureId() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( state.isMatrixDirty() )
|
|
|
|
program()->setUniformValue( m_matrixId, state.combinedMatrix() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialShader::initialize()
|
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
m_matrixId = program()->uniformLocation( "qt_Matrix" );
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
if ( !m_isOpaque )
|
2018-08-03 06:15:28 +00:00
|
|
|
m_opacityId = program()->uniformLocation( "opacity" );
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
Material::Material( bool isOpaque )
|
2020-11-01 08:28:20 +00:00
|
|
|
: m_isOpaque( isOpaque )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
setFlag( Blending, true ); // alpha blending
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:04:05 +00:00
|
|
|
void Material::setTextureId( uint id )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
m_textureId = id;
|
|
|
|
}
|
|
|
|
|
2017-12-07 16:04:05 +00:00
|
|
|
uint Material::textureId() const
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
return m_textureId;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSGMaterialType* Material::type() const
|
|
|
|
{
|
|
|
|
if ( m_isOpaque )
|
|
|
|
{
|
|
|
|
static QSGMaterialType typeOpaque;
|
|
|
|
return &typeOpaque;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static QSGMaterialType type;
|
|
|
|
return &type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSGMaterialShader* Material::createShader() const
|
|
|
|
{
|
|
|
|
return new MaterialShader( m_isOpaque );
|
|
|
|
}
|
|
|
|
|
|
|
|
int Material::compare( const QSGMaterial* other ) const
|
|
|
|
{
|
|
|
|
const auto otherMaterial = static_cast< const Material* >( other );
|
2017-12-07 16:04:05 +00:00
|
|
|
|
|
|
|
if ( m_textureId == otherMaterial->m_textureId )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ( m_textureId > otherMaterial->m_textureId ) ? 1 : -1;
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
static inline void qskDeleteTexture( const TextureMaterial& material )
|
|
|
|
{
|
|
|
|
if ( material.textureId() > 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
In certain environments we have the effect, that at
|
|
|
|
program termination the context is already gone
|
|
|
|
*/
|
|
|
|
if ( auto context = QOpenGLContext::currentContext() )
|
|
|
|
{
|
|
|
|
GLuint id = material.textureId();
|
|
|
|
|
|
|
|
auto funcs = context->functions();
|
|
|
|
funcs->glDeleteTextures( 1, &id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-12-07 16:04:05 +00:00
|
|
|
class QskTextureNodePrivate final : public QSGGeometryNodePrivate
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-08-03 06:15:28 +00:00
|
|
|
public:
|
|
|
|
QskTextureNodePrivate()
|
|
|
|
: geometry( QSGGeometry::defaultAttributes_TexturedPoint2D(), 4 )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
void setTextureId( QQuickWindow*, uint id );
|
|
|
|
|
2021-12-13 15:43:34 +00:00
|
|
|
void updateTextureGeometry()
|
2020-10-26 13:13:57 +00:00
|
|
|
{
|
|
|
|
QRectF r( 0, 0, 1, 1 );
|
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
if ( this->mirrored & Qt::Horizontal )
|
2020-10-26 13:13:57 +00:00
|
|
|
{
|
|
|
|
r.setLeft( 1 );
|
|
|
|
r.setRight( 0 );
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
if ( mirrored & Qt::Vertical )
|
2020-10-26 13:13:57 +00:00
|
|
|
{
|
|
|
|
r.setTop( 1 );
|
|
|
|
r.setBottom( 0 );
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:43:34 +00:00
|
|
|
QSGGeometry::updateTexturedRectGeometry( &geometry, rect, r );
|
2020-10-26 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 16:21:34 +00:00
|
|
|
QSGGeometry geometry;
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
OpaqueTextureMaterial opaqueMaterial;
|
|
|
|
TextureMaterial material;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
QRectF rect;
|
2020-11-01 14:44:15 +00:00
|
|
|
Qt::Orientations mirrored;
|
2017-07-21 16:21:34 +00:00
|
|
|
};
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
QskTextureNode::QskTextureNode()
|
|
|
|
: QSGGeometryNode( *new QskTextureNodePrivate )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
Q_D( QskTextureNode );
|
|
|
|
|
|
|
|
setGeometry( &d->geometry );
|
|
|
|
|
|
|
|
setMaterial( &d->material );
|
|
|
|
setOpaqueMaterial( &d->opaqueMaterial );
|
|
|
|
}
|
|
|
|
|
|
|
|
QskTextureNode::~QskTextureNode()
|
|
|
|
{
|
2020-11-01 15:57:59 +00:00
|
|
|
Q_D( const QskTextureNode );
|
|
|
|
qskDeleteTexture( d->material );
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
void QskTextureNode::setTexture( QQuickWindow* window,
|
|
|
|
const QRectF& rect, uint textureId,
|
|
|
|
Qt::Orientations mirrored )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
Q_D( QskTextureNode );
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
if ( ( d->rect != rect ) || ( d->mirrored != mirrored ) )
|
2020-10-26 13:13:57 +00:00
|
|
|
{
|
2020-11-01 14:44:15 +00:00
|
|
|
d->rect = rect;
|
|
|
|
d->mirrored = mirrored;
|
|
|
|
|
2021-12-13 15:43:34 +00:00
|
|
|
d->updateTextureGeometry();
|
2020-10-26 13:13:57 +00:00
|
|
|
markDirty( DirtyGeometry );
|
|
|
|
}
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
if ( textureId != this->textureId() )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2020-11-01 14:44:15 +00:00
|
|
|
d->setTextureId( window, textureId );
|
2020-10-26 13:13:57 +00:00
|
|
|
markDirty( DirtyMaterial );
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
|
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
void QskTextureNodePrivate::setTextureId( QQuickWindow*, uint textureId )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2020-11-01 15:57:59 +00:00
|
|
|
qskDeleteTexture( this->material );
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2020-11-01 14:44:15 +00:00
|
|
|
this->material.setTextureId( textureId );
|
|
|
|
this->opaqueMaterial.setTextureId( textureId );
|
2020-10-26 13:13:57 +00:00
|
|
|
}
|
2018-07-13 13:09:25 +00:00
|
|
|
|
2020-10-26 13:13:57 +00:00
|
|
|
uint QskTextureNode::textureId() const
|
|
|
|
{
|
|
|
|
Q_D( const QskTextureNode );
|
|
|
|
return d->material.textureId();
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
2020-10-26 17:06:06 +00:00
|
|
|
|
2020-11-01 15:57:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
|
|
|
|
|
|
|
|
void QskTextureNodePrivate::setTextureId( QQuickWindow* window, uint textureId )
|
|
|
|
{
|
|
|
|
auto texture = this->material.texture();
|
|
|
|
|
|
|
|
if ( texture )
|
|
|
|
{
|
|
|
|
// we do not want to create a new QSGTexture object only
|
|
|
|
// to replace the textureId
|
|
|
|
|
|
|
|
switch( window->rendererInterface()->graphicsApi() )
|
|
|
|
{
|
|
|
|
case QSGRendererInterface::OpenGL:
|
|
|
|
{
|
|
|
|
qskUpdateGLTextureId( texture->rhiTexture(), textureId );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
delete texture;
|
|
|
|
texture = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( textureId > 0 && texture == nullptr )
|
|
|
|
{
|
|
|
|
texture = QNativeInterface::QSGOpenGLTexture::fromNative(
|
|
|
|
static_cast< GLuint >( textureId ), window,
|
|
|
|
this->rect.size().toSize(), QQuickWindow::TextureHasAlphaChannel );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this->material.setTexture( texture );
|
|
|
|
this->opaqueMaterial.setTexture( texture );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint QskTextureNode::textureId() const
|
|
|
|
{
|
|
|
|
Q_D( const QskTextureNode );
|
|
|
|
|
|
|
|
if ( auto texture = d->material.texture() )
|
|
|
|
{
|
|
|
|
const auto nativeTexture = texture->rhiTexture()->nativeTexture();
|
|
|
|
return nativeTexture.object;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-10-26 17:06:06 +00:00
|
|
|
bool QskTextureNode::isNull() const
|
2020-11-01 14:44:15 +00:00
|
|
|
{
|
|
|
|
return textureId() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF QskTextureNode::rect() const
|
|
|
|
{
|
|
|
|
Q_D( const QskTextureNode );
|
|
|
|
return d->rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations QskTextureNode::mirrored() const
|
2020-10-26 17:06:06 +00:00
|
|
|
{
|
|
|
|
Q_D( const QskTextureNode );
|
2020-11-01 14:44:15 +00:00
|
|
|
return d->mirrored;
|
2020-10-26 17:06:06 +00:00
|
|
|
}
|