- Qt 6.11
- Qt Quick
- C++ Classes
- QSGTexture
- Qt 6.11.1 Reference Documentation

# QSGTexture Class

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

Header: | `#include <QSGTexture>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Quick)` `target_link_libraries(mytarget PRIVATE Qt6::Quick)`
qmake: | `QT += quick`
Inherits: | QObject
Inherited By: | QSGDynamicTexture

- List of all members, including inherited members

## Public Types

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
`(since 6.0)` virtual void | commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
virtual qint64 | comparisonKey() const = 0
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
QNativeInterface * | nativeInterface() const
virtual QRectF | normalizedTextureSubRect() const
virtual QSGTexture * | removedFromAtlas(QRhiResourceUpdateBatch *resourceUpdates = nullptr) const
`(since 6.0)` virtual QRhiTexture * | rhiTexture() const
void | setAnisotropyLevel(QSGTexture::AnisotropyLevel level)
void | setFiltering(QSGTexture::Filtering filter)
void | setHorizontalWrapMode(QSGTexture::WrapMode hwrap)
void | setMipmapFiltering(QSGTexture::Filtering filter)
void | setVerticalWrapMode(QSGTexture::WrapMode vwrap)
virtual QSize | textureSize() const = 0
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 provides a default implementation for RGBA textures.The default implementation is not instantiated directly, rather they are constructed via factory functions, such as QQuickWindow::createTextureFromImage().

With the default implementation, each QSGTexture is backed by a QRhiTexture, which in turn contains a native texture object, such as an OpenGL texture or a Vulkan image.

The size in pixels is given by textureSize(). hasAlphaChannel() reports if the texture contains opacity values and hasMipmaps() reports if the texture contains mipmap levels.

Materials that work with textures reimplement updateSampledImage() to provide logic that decides which QSGTexture's underlying native texture should be exposed at a given shader resource binding point.

QSGTexture does not separate image (texture) and sampler objects. The parameters for filtering and wrapping can be specified with setMipmapFiltering(), setFiltering(), setHorizontalWrapMode() and setVerticalWrapMode(). The scene graph and Qt's graphics abstraction takes care of creating separate sampler objects, when applicable.

### Texture Atlases

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. Atlases are used to aid the rendering algorithm to do better sorting which increases performance. Atlases are also essential for batching (merging together geometry to reduce the number of draw calls), because two instances of the same material using two different QSGTextures are not batchable, whereas if both QSGTextures refer to the same atlas, batching can happen, assuming the materials are otherwise compatible.

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-atlased 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.

## Member Type Documentation

### enum QSGTexture::AnisotropyLevel

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

Constant | Value | Description
--- | --- | ---
`QSGTexture::AnisotropyNone` | `0` | No anisotropic filtering.
`QSGTexture::Anisotropy2x` | `1` | 2x anisotropic filtering.
`QSGTexture::Anisotropy4x` | `2` | 4x anisotropic filtering.
`QSGTexture::Anisotropy8x` | `3` | 8x anisotropic filtering.
`QSGTexture::Anisotropy16x` | `4` | 16x anisotropic filtering.

### enum QSGTexture::Filtering

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

Constant | Value | Description
--- | --- | ---
`QSGTexture::None` | `0` | No filtering should occur. This value is only used together with setMipmapFiltering().
`QSGTexture::Nearest` | `1` | Sampling returns the nearest texel.
`QSGTexture::Linear` | `2` | Sampling returns a linear interpolation of the neighboring texels.

### enum QSGTexture::WrapMode

Specifies how the sampler should treat texture coordinates.

Constant | Value | Description
--- | --- | ---
`QSGTexture::Repeat` | `0` | Only the fractional part of the texture coordinate is used, causing values above 1 and below 0 to repeat.
`QSGTexture::ClampToEdge` | `1` | Values above 1 are clamped to 1 and values below 0 are clamped to 0.
`QSGTexture::MirroredRepeat` | `2` | When 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 noexcept]` QSGTexture::~QSGTexture()

Destroys the QSGTexture.

### QSGTexture::AnisotropyLevel QSGTexture::anisotropyLevel() const

Returns the anisotropy level in use for filtering this texture.

See also setAnisotropyLevel().

### `[virtual, since 6.0]` void QSGTexture::commitTextureOperations(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, passing `state.rhi()` and `state.resourceUpdateBatch()` from the QSGMaterialShader::RenderState.

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

This function was introduced in Qt 6.0.

### `[pure virtual]` qint64 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 the need for this function.

Implementations of this function are not expected to, and should not create any graphics resources (native texture objects) in case there are none yet.

A QSGTexture that does not have a native texture object underneath is typically not equal to any other QSGTexture, so the return value has to be crafted accordingly. 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.

### 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 whether 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().

### template <typename QNativeInterface> QNativeInterface *QSGTexture::nativeInterface() const

Returns a native interface of the given type for the texture.

This function provides access to platform specific functionality of QSGTexture, as declared in the QNativeInterface namespace:

QNativeInterface::QSGD3D11Texture | Provides access to and enables adopting Direct3D 11 texture objects
QNativeInterface::QSGD3D12Texture | Provides access to and enables adopting Direct3D 12 texture objects
QNativeInterface::QSGMetalTexture | Provides access to and enables adopting Metal texture objects
QNativeInterface::QSGOpenGLTexture | Provides access to and enables adopting OpenGL texture objects
QNativeInterface::QSGVulkanTexture | Provides access to and enables adopting Vulkan image objects

This allows accessing the underlying native texture object, such as, the `GLuint` texture ID with OpenGL, or the `VkImage` handle with Vulkan.

If the requested interface is not available a `nullptr` is returned.

### `[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(QRhiResourceUpdateBatch *resourceUpdates = nullptr) 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.

resourceUpdates is an optional resource update batch, on which texture operations, if any, are enqueued. Materials can retrieve an instance from QSGMaterialShader::RenderState. When null, the removedFromAtlas() implementation creates its own batch and submit it right away. However, when a valid instance is specified, this function will not submit the update batch.

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

### `[virtual, since 6.0]` QRhiTexture *QSGTexture::rhiTexture() const

Returns the QRhiTexture for this QSGTexture or null if there is none (either because a valid texture has not been created internally yet, or because the concept is not applicable to the scenegraph backend in use).

This function is not expected to create a new QRhiTexture in case there is none. It should return null in that case. The expectation towards the renderer is that a null texture leads to using a transparent, dummy texture instead.

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

This function was introduced in Qt 6.0.

### void QSGTexture::setAnisotropyLevel(QSGTexture::AnisotropyLevel level)

Sets the level of anisotropic filtering to level. The default value is QSGTexture::AnisotropyNone, which means no anisotropic filtering is enabled.

Note: The request may be ignored depending on the graphics API in use. There is no guarantee anisotropic filtering is supported at run time.

See also anisotropyLevel().

### void QSGTexture::setFiltering(QSGTexture::Filtering filter)

Sets the sampling mode to filter.

See also filtering().

### void QSGTexture::setHorizontalWrapMode(QSGTexture::WrapMode hwrap)

Sets the horizontal wrap mode to hwrap

See also horizontalWrapMode().

### void QSGTexture::setMipmapFiltering(QSGTexture::Filtering filter)

Sets the mipmap sampling mode to filter.

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

See also mipmapFiltering() and hasMipmaps().

### void QSGTexture::setVerticalWrapMode(QSGTexture::WrapMode vwrap)

Sets the vertical wrap mode to vwrap

See also verticalWrapMode().

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

Returns the size of the texture in pixels.

### QSGTexture::WrapMode QSGTexture::verticalWrapMode() const

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

See also setVerticalWrapMode().