QSGTexture Class

The QSGTexture class is a baseclass for textures used in the scene graph. More...

Header: #include <QSGTexture>
qmake: QT += quick
Inherits: QObject
Inherited By:

QSGDynamicTexture

Public Types

struct NativeTexture
enum AnisotropyLevel { AnisotropyNone, Anisotropy2x, Anisotropy4x, Anisotropy8x, Anisotropy16x }
enum Filtering { None, Nearest, Linear }
enum WrapMode { Repeat, ClampToEdge, MirroredRepeat }

Public Functions

QSGTexture()
virtual ~QSGTexture() override
QSGTexture::AnisotropyLevel anisotropyLevel() const
virtual void bind() = 0
int comparisonKey() const
QRectF convertToNormalizedSourceRect(const QRectF &rect) const
QSGTexture::Filtering filtering() const
virtual bool hasAlphaChannel() const = 0
virtual bool hasMipmaps() const = 0
QSGTexture::WrapMode horizontalWrapMode() const
virtual bool isAtlasTexture() const
QSGTexture::Filtering mipmapFiltering() const
NativeTexture nativeTexture() const
virtual QRectF normalizedTextureSubRect() const
virtual QSGTexture *removedFromAtlas() const
void setAnisotropyLevel(AnisotropyLevel level)
void setFiltering(Filtering filter)
void setHorizontalWrapMode(WrapMode hwrap)
void setMipmapFiltering(Filtering filter)
void setVerticalWrapMode(WrapMode vwrap)
virtual int textureId() const = 0
virtual QSize textureSize() const = 0
void updateBindOptions(bool force = false)
void updateRhiTexture(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
QSGTexture::WrapMode verticalWrapMode() const

Detailed Description

Users can freely implement their own texture classes to support arbitrary input textures, such as YUV video frames or 8 bit alpha masks. The scene graph backend provides a default implementation of normal color textures. As the implementation of these may be hardware specific, they are constructed via the factory function QQuickWindow::createTextureFromImage().

The texture is a wrapper around an OpenGL texture, which texture id is given by textureId() and which size in pixels is given by textureSize(). hasAlphaChannel() reports if the texture contains opacity values and hasMipmaps() reports if the texture contains mipmap levels.

To use a texture, call the bind() function. The texture parameters specifying how the texture is bound, can be specified with setMipmapFiltering(), setFiltering(), setHorizontalWrapMode() and setVerticalWrapMode(). The texture will internally try to store these values to minimize the OpenGL state changes when the texture is bound.

Texture Atlasses

Some scene graph backends use texture atlasses, grouping multiple small textures into one large texture. If this is the case, the function isAtlasTexture() will return true. Atlasses are used to aid the rendering algorithm to do better sorting which increases performance. The location of the texture inside the atlas is given with the normalizedTextureSubRect() function.

If the texture is used in such a way that atlas is not preferable, the function removedFromAtlas() can be used to extract a non-atlassed copy.

Note: All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.

See also Scene Graph - Rendering FBOs and Scene Graph - Rendering FBOs in a thread.

Member Type Documentation

enum QSGTexture::AnisotropyLevel

Specifies the anisotropic filtering level to be used when the texture is not screen aligned.

ConstantValueDescription
QSGTexture::AnisotropyNone0No anisotropic filtering.
QSGTexture::Anisotropy2x12x anisotropic filtering.
QSGTexture::Anisotropy4x24x anisotropic filtering.
QSGTexture::Anisotropy8x38x anisotropic filtering.
QSGTexture::Anisotropy16x416x anisotropic filtering.

This enum was introduced or modified in Qt 5.9.

enum QSGTexture::Filtering

Specifies how sampling of texels should filter when texture coordinates are not pixel aligned.

ConstantValueDescription
QSGTexture::None0No filtering should occur. This value is only used together with setMipmapFiltering().
QSGTexture::Nearest1Sampling returns the nearest texel.
QSGTexture::Linear2Sampling returns a linear interpolation of the neighboring texels.

enum QSGTexture::WrapMode

Specifies how the texture should treat texture coordinates.

ConstantValueDescription
QSGTexture::Repeat0Only the fractional part of the texture coordinate is used, causing values above 1 and below 0 to repeat.
QSGTexture::ClampToEdge1Values above 1 are clamped to 1 and values below 0 are clamped to 0.
QSGTexture::MirroredRepeat2When the texture coordinate is even, only the fractional part is used. When odd, the texture coordinate is set to 1 - fractional part. This value has been introduced in Qt 5.10.

Member Function Documentation

QSGTexture::QSGTexture()

Constructs the QSGTexture base class.

[override virtual] QSGTexture::~QSGTexture()

Destroys the QSGTexture.

QSGTexture::AnisotropyLevel QSGTexture::anisotropyLevel() const

Returns the anisotropy level in use for filtering this texture.

This function was introduced in Qt 5.9.

See also setAnisotropyLevel().

[pure virtual] void QSGTexture::bind()

Call this function to bind this texture to the current texture target.

Binding a texture may also include uploading the texture data from a previously set QImage.

Warning: This function should only be called when running with the direct OpenGL rendering path.

Warning: This function can only be called from the rendering thread.

int QSGTexture::comparisonKey() const

Returns a key suitable for comparing textures. Typically used in QSGMaterial::compare() implementations.

Just comparing QSGTexture pointers is not always sufficient because two QSGTexture instances that refer to the same native texture object underneath should also be considered equal. Hence this function.

Note: Unlike textureId(), implementations of this function are not expected to and should not create any graphics resources (so texture objects) in case there is none yet.

A QSGTexture that does not have a native texture object underneath is typically not equal to any other QSGTexture. There are exceptions to this, in particular when atlasing is used (where multiple textures share the same atlas texture under the hood), that is then up to the subclass implementations to deal with as appropriate.

Warning: This function can only be called from the rendering thread.

This function was introduced in Qt 5.14.

QRectF QSGTexture::convertToNormalizedSourceRect(const QRectF &rect) const

Returns rect converted to normalized coordinates.

See also normalizedTextureSubRect().

QSGTexture::Filtering QSGTexture::filtering() const

Returns the sampling mode to be used for this texture.

See also setFiltering().

[pure virtual] bool QSGTexture::hasAlphaChannel() const

Returns true if the texture data contains an alpha channel.

[pure virtual] bool QSGTexture::hasMipmaps() const

Returns true if the texture data contains mipmap levels.

QSGTexture::WrapMode QSGTexture::horizontalWrapMode() const

Returns the horizontal wrap mode to be used for this texture.

See also setHorizontalWrapMode().

[virtual] bool QSGTexture::isAtlasTexture() const

Returns weither this texture is part of an atlas or not.

The default implementation returns false.

QSGTexture::Filtering QSGTexture::mipmapFiltering() const

Returns whether mipmapping should be used when sampling from this texture.

See also setMipmapFiltering().

NativeTexture QSGTexture::nativeTexture() const

Returns the platform-specific texture data for this texture.

Note: This is only available when running the graphics API independent rendering path of the scene graph. Use textureId() otherwise.

Returns an empty result (object is null) if there is no available underlying native texture.

This function was introduced in Qt 5.15.

See also QQuickWindow::createTextureFromNativeObject().

[virtual] QRectF QSGTexture::normalizedTextureSubRect() const

Returns the rectangle inside textureSize() that this texture represents in normalized coordinates.

The default implementation returns a rect at position (0, 0) with width and height of 1.

[virtual] QSGTexture *QSGTexture::removedFromAtlas() const

This function returns a copy of the current texture which is removed from its atlas.

The current texture remains unchanged, so texture coordinates do not need to be updated.

Removing a texture from an atlas is primarily useful when passing it to a shader that operates on the texture coordinates 0-1 instead of the texture subrect inside the atlas.

If the texture is not part of a texture atlas, this function returns 0.

Implementations of this function are recommended to return the same instance for multiple calls to limit memory usage.

Warning: This function can only be called from the rendering thread.

void QSGTexture::setAnisotropyLevel(AnisotropyLevel level)

Sets the level of anisotropic filtering to be used for the upcoming bind() call to level. The default value is QSGTexture::AnisotropyNone, which means no anisotropic filtering is enabled.

This function was introduced in Qt 5.9.

See also anisotropyLevel().

void QSGTexture::setFiltering(Filtering filter)

Sets the sampling mode to be used for the upcoming bind() call to filter.

See also filtering().

void QSGTexture::setHorizontalWrapMode(WrapMode hwrap)

Sets the horizontal wrap mode to be used for the upcoming bind() call to hwrap

See also horizontalWrapMode().

void QSGTexture::setMipmapFiltering(Filtering filter)

Sets the mipmap sampling mode to be used for the upcoming bind() call to filter.

Setting the mipmap filtering has no effect it the texture does not have mipmaps.

See also mipmapFiltering() and hasMipmaps().

void QSGTexture::setVerticalWrapMode(WrapMode vwrap)

Sets the vertical wrap mode to be used for the upcoming bind() call to vwrap

See also verticalWrapMode().

[pure virtual] int QSGTexture::textureId() const

Returns the OpenGL texture id for this texture.

The default value is 0, indicating that it is an invalid texture id.

The function should at all times return the correct texture id.

Warning: This function can only be called from the rendering thread.

[pure virtual] QSize QSGTexture::textureSize() const

Returns the size of the texture.

void QSGTexture::updateBindOptions(bool force = false)

Update the texture state to match the filtering, mipmap and wrap options currently set.

If force is true, all properties will be updated regardless of weither they have changed or not.

void QSGTexture::updateRhiTexture(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)

Call this function to enqueue image upload operations to resourceUpdates, in case there are any pending ones. When there is no new data (for example, because there was no setImage() since the last call to this function), the function does nothing.

Materials involving rhi textures are expected to call this function from their updateSampledImage() implementation, typically without any conditions.

Note: This function is only used when running the graphics API independent rendering path of the scene graph.

Warning: This function can only be called from the rendering thread.

This function was introduced in Qt 5.14.

QSGTexture::WrapMode QSGTexture::verticalWrapMode() const

Returns the vertical wrap mode to be used for this texture.

See also setVerticalWrapMode().