QSGTexture Class
QSGTexture 类是场景图中所用纹理的基类。更多
头文件: | #include <QSGTexture> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Quick) target_link_libraries(mytarget PRIVATE Qt6::Quick) |
qmake: | QT += quick |
继承: | QObject |
继承于: |
公共类型
enum | AnisotropyLevel { AnisotropyNone, Anisotropy2x, Anisotropy4x, Anisotropy8x, Anisotropy16x } |
enum | Filtering { None, Nearest, Linear } |
enum | WrapMode { Repeat, ClampToEdge, MirroredRepeat } |
公共函数
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 |
详细说明
用户可以自由实现自己的纹理类,以支持任意输入纹理,如 YUV 视频帧或 8 位 alpha 蒙版。场景图为 RGBA 纹理提供了默认实现。默认实现不是直接实例化的,而是通过工厂函数(如QQuickWindow::createTextureFromImage() )构建的。
在默认实现中,每个 QSGTexture 都由QRhiTexture 支持,而 又包含一个本地纹理对象,如 OpenGL 纹理或 Vulkan 图像。
textureSize hasAlphaChannel() 会报告纹理是否包含不透明度值, () 会报告纹理是否包含 mipmap 级别。hasMipmaps
Materials 与纹理一起工作的 QSGTexture 会重新实现 () 以提供逻辑,决定在给定的着色器资源绑定点应暴露哪个 QSGTexture 的底层本地纹理。updateSampledImage
QSGTexture 并不分离图像(纹理)和采样器对象。可使用setMipmapFiltering(),setFiltering(),setHorizontalWrapMode() 和setVerticalWrapMode() 指定过滤和包装参数。场景图和 Qt 图形抽象会在适用时负责创建单独的采样器对象。
纹理图集
有些场景图后端使用纹理图类,将多个小纹理组合成一个大纹理。如果是这种情况,函数isAtlasTexture() 将返回 true。图集可帮助渲染算法更好地进行排序,从而提高性能。图集对于批处理(合并几何体以减少绘制调用次数)也很重要,因为使用两种不同 QSGTextures 的相同材质的两个实例是无法批处理的,而如果两个 QSGTextures 都引用了相同的图集,则可以进行批处理,前提是材质在其他方面是兼容的。
纹理在图集中的位置通过normalizedTextureSubRect() 函数给出。
如果纹理的使用方式不适合使用图集,可以使用函数removedFromAtlas() 提取非图集副本。
注意: 所有带有 QSG 前缀的类都只能在场景图的渲染线程中使用。更多信息,请参阅场景图和渲染。
成员类型文档
enum QSGTexture::AnisotropyLevel
指定纹理未与屏幕对齐时使用的各向异性过滤级别。
常数 | 值 | 说明 |
---|---|---|
QSGTexture::AnisotropyNone | 0 | 无各向异性过滤。 |
QSGTexture::Anisotropy2x | 1 | 2x 各向异性过滤。 |
QSGTexture::Anisotropy4x | 2 | 4 倍各向异性过滤。 |
QSGTexture::Anisotropy8x | 3 | 8 倍各向异性过滤。 |
QSGTexture::Anisotropy16x | 4 | 16 倍各向异性过滤。 |
enum QSGTexture::Filtering
指定纹理坐标未按像素对齐时如何过滤纹理采样。
常数 | 值 | 说明 |
---|---|---|
QSGTexture::None | 0 | 不进行过滤。该值只能与setMipmapFiltering() 一起使用。 |
QSGTexture::Nearest | 1 | 采样返回最近的像素。 |
QSGTexture::Linear | 2 | 采样返回相邻像素的线性插值。 |
enum QSGTexture::WrapMode
指定采样器处理纹理坐标的方式。
常数 | 值 | 说明 |
---|---|---|
QSGTexture::Repeat | 0 | 只使用纹理坐标的小数部分,导致高于 1 和低于 0 的值重复。 |
QSGTexture::ClampToEdge | 1 | 高于 1 的值被箝位为 1,低于 0 的值被箝位为 0。 |
QSGTexture::MirroredRepeat | 2 | 当纹理坐标为偶数时,只使用分数部分。当奇数时,纹理坐标被设置为1 - fractional part 。该值已在 Qt 5.10 中引入。 |
成员函数文档
QSGTexture::QSGTexture()
构造 QSGTexture 基类。
[override virtual noexcept]
QSGTexture::~QSGTexture()
摧毁QSGTexture.
QSGTexture::AnisotropyLevel QSGTexture::anisotropyLevel() const
返回用于过滤此纹理的各向异性级别。
另请参阅 setAnisotropyLevel()。
[virtual, since 6.0]
void QSGTexture::commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
调用此函数可将图片上传操作顺序排列到resourceUpdates ,以防有任何等待上传的操作。如果没有新数据(例如,因为自上次调用此函数以来没有进行过 setImage()),则该函数不会执行任何操作。
涉及rhi 纹理的材料应在其updateSampledImage() 实现中调用此函数,通常不需要任何条件,只需从QSGMaterialShader::RenderState 传递state.rhi()
和state.resourceUpdateBatch()
即可。
警告: 此函数只能在渲染线程中调用。
此函数在 Qt 6.0 中引入。
[pure virtual]
qint64 QSGTexture::comparisonKey() const
返回一个适合比较纹理的键值。通常用于QSGMaterial::compare() 实现。
仅仅比较QSGTexture 指针并不总是足够的,因为引用同一本地纹理对象的两个QSGTexture 实例也应被视为相同。因此需要使用此函数。
如果还没有图形资源(本地纹理对象),则该函数的实现不需要也不应该创建任何图形资源。
通常情况下,没有本地纹理对象的QSGTexture 与其他任何QSGTexture 都是不相等的,因此必须对返回值进行相应的处理。但也有例外情况,特别是在使用图集的情况下(多个纹理共享相同的图集纹理),这将由子类实现酌情处理。
警告 此函数只能在渲染线程中调用。
QRectF QSGTexture::convertToNormalizedSourceRect(const QRectF &rect) const
返回rect 转换为归一化坐标的结果。
另请参见 normalizedTextureSubRect()。
QSGTexture::Filtering QSGTexture::filtering() const
返回此纹理使用的采样模式。
另请参阅 setFiltering()。
[pure virtual]
bool QSGTexture::hasAlphaChannel() const
如果纹理数据包含 alpha 通道,则返回 true。
[pure virtual]
bool QSGTexture::hasMipmaps() const
如果纹理数据包含 mipmap 层次,则返回 true。
QSGTexture::WrapMode QSGTexture::horizontalWrapMode() const
返回此纹理使用的水平缠绕模式。
另请参阅 setHorizontalWrapMode()。
[virtual]
bool QSGTexture::isAtlasTexture() const
返回此纹理是否是图集的一部分。
默认实现返回 false。
QSGTexture::Filtering QSGTexture::mipmapFiltering() const
返回从该纹理采样时是否使用 mipmapping。
另请参阅 setMipmapFiltering()。
template <typename QNativeInterface> QNativeInterface *QSGTexture::nativeInterface() const
返回纹理的给定类型的本地接口。
该函数提供了对QSGTexture 平台特定功能的访问,正如在QNativeInterface 命名空间中所声明的那样:
提供对 Direct3D 11 纹理对象的访问权限,并允许采用 Direct3D 11 纹理对象 | |
提供对 Direct3D 12 纹理对象的访问并使其能够采用 | |
提供对金属纹理对象的访问并使其成为可能 | |
提供访问 OpenGL 纹理对象的权限,并支持采用 OpenGL 纹理对象 | |
提供对 Vulkan 图像对象的访问权限并使其能够被采用 |
这允许访问底层本地纹理对象,如 OpenGL 的GLuint
纹理 ID 或 Vulkan 的VkImage
句柄。
如果请求的接口不可用,则返回nullptr
。
[virtual]
QRectF QSGTexture::normalizedTextureSubRect() const
返回此纹理在textureSize() 内以归一化坐标表示的矩形。
默认实现会返回一个位于位置 (0, 0) 的矩形,其宽度和高度均为 1。
[virtual]
QSGTexture *QSGTexture::removedFromAtlas(QRhiResourceUpdateBatch *resourceUpdates = nullptr) const
此函数返回一个当前纹理的副本,该副本已从纹理图集中删除。
当前纹理保持不变,因此无需更新纹理坐标。
将纹理从图集中移除主要是为了将其传递给着色器,让着色器根据纹理坐标 0-1 而不是图集中的纹理子坐标进行操作。
如果纹理不在纹理图集中,此函数返回 0。
建议在多次调用此函数时返回同一个实例,以限制内存使用量。
resourceUpdates 是一个可选的资源更新批次,纹理操作(如果有)将在此批次上排队。材料可以从 获取一个实例。如果为空,removedFromAtlas() 实现会创建自己的批次并立即提交。但是,如果指定了一个有效的实例,此函数将不会提交更新批次。QSGMaterialShader::RenderState
警告: 此函数只能在渲染线程中调用。
[virtual, since 6.0]
QRhiTexture *QSGTexture::rhiTexture() const
返回此QSGTexture 的QRhiTexture ,如果没有(因为内部尚未创建有效的纹理,或者因为此概念不适用于使用中的场景图后端),则返回 null。
如果没有,则该函数不会创建新的QRhiTexture 。在这种情况下,它应该返回空值。渲染器的期望是,空纹理会导致使用透明的假纹理。
警告 此函数只能在渲染线程中调用。
此函数在 Qt 6.0 中引入。
void QSGTexture::setAnisotropyLevel(QSGTexture::AnisotropyLevel level)
将各向异性过滤的级别设置为level 。默认值为QSGTexture::AnisotropyNone ,表示不启用各向异性过滤。
注意: 根据使用的图形 API,该请求可能会被忽略。不保证运行时支持各向异性过滤。
另请参阅 anisotropyLevel().
void QSGTexture::setFiltering(QSGTexture::Filtering filter)
将采样模式设置为filter 。
另请参阅 filtering() 。
void QSGTexture::setHorizontalWrapMode(QSGTexture::WrapMode hwrap)
将水平换行模式设置为hwrap
另请参阅 horizontalWrapMode().
void QSGTexture::setMipmapFiltering(QSGTexture::Filtering filter)
将 mipmap 采样模式设置为filter 。
如果纹理没有 mipmap,则设置 mipmap 过滤没有任何作用。
另请参阅 mipmapFiltering() 和hasMipmaps()。
void QSGTexture::setVerticalWrapMode(QSGTexture::WrapMode vwrap)
将垂直换行模式设置为vwrap
另请参阅 verticalWrapMode().
[pure virtual]
QSize QSGTexture::textureSize() const
以像素为单位返回纹理的大小。
QSGTexture::WrapMode QSGTexture::verticalWrapMode() const
返回此纹理使用的垂直换行模式。
另请参阅 setVerticalWrapMode()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.