本页内容

QRhiColorAttachment Class

描述了渲染目标的单色附件。更多...

标题: #include <rhi/qrhi.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS GuiPrivate)
target_link_libraries(mytarget PRIVATE Qt6::GuiPrivate)
qmake: QT += gui-private
自: Qt 6.6

公共函数

QRhiColorAttachment()
QRhiColorAttachment(QRhiRenderBuffer *renderBuffer)
QRhiColorAttachment(QRhiTexture *texture)
int layer() const
int level() const
(since 6.7) int multiViewCount() const
QRhiRenderBuffer *renderBuffer() const
int resolveLayer() const
int resolveLevel() const
QRhiTexture *resolveTexture() const
void setLayer(int layer)
void setLevel(int level)
(since 6.7) void setMultiViewCount(int count)
void setRenderBuffer(QRhiRenderBuffer *rb)
void setResolveLayer(int layer)
void setResolveLevel(int level)
void setResolveTexture(QRhiTexture *tex)
void setTexture(QRhiTexture *tex)
QRhiTexture *texture() const

详细说明

颜色附加项可以是QRhiTexture ,也可以是QRhiRenderBuffer 。前者(即当texture()被设置时)在绝大多数情况下被使用。QRhiColorAttachment通常与QRhiTextureRenderTargetDescription 配合使用。

注意: texture() 和renderBuffer() 不能同时被设置(即不能同时不为空)。

仅当需要多采样时,才建议设置renderBuffer 。在实际应用中,使用QRhi::MultisampleRenderBuffer 比QRhi::MultisampleTexture 更佳,因为前者在更多运行时配置下可用(例如在 OpenGL ES 3.0 上运行时,该版本虽不支持多采样纹理,但支持多采样渲染缓冲区)。

当目标为非多采样纹理时,layer() 和level() 分别指定目标层(对于立方体贴图,面索引为0-5 )和 MIP 级别。对于 3D 纹理,layer() 指定要渲染到的切片(3D 纹理中的一个 2D 图像)。对于纹理数组,layer() 表示数组索引。

当texture()或renderBuffer()为多采样时,resolveTexture()可选。若设置该参数,采样结果将在渲染过程结束时自动解析到该(非多采样)纹理中。当渲染到多采样渲染缓冲区时,这是从中获取已解析的非多采样内容的唯一方法。 多采样纹理允许在着色器中进行采样,因此对于它们而言,这仅是一种选项。

注意:当 解析功能启用时 ,多采样数据可能完全不会被写出。这意味着,当设置了 `resolveTexture()` 时,后续不得在着色器中使用多采样 `texture()` 进行采样。

注意:这是一个 兼容性保证有限的 RHI API,详情请参见QRhi 。

另请参阅 QRhiTextureRenderTargetDescription 。

成员函数文档

[constexpr noexcept default] QRhiColorAttachment::QRhiColorAttachment()

创建一个空的颜色附件描述。

QRhiColorAttachment::QRhiColorAttachment(QRhiRenderBuffer *renderBuffer)

构建一个颜色附件描述,其中将renderBuffer 指定为关联的颜色缓冲区。

QRhiColorAttachment::QRhiColorAttachment(QRhiTexture *texture)

构建一个颜色附件描述,其中将texture 指定为关联的颜色缓冲区。

int QRhiColorAttachment::layer() const

返回图层索引(立方贴图面或数组图层)。默认值为 0。

另请参阅 setLayer()。

int QRhiColorAttachment::level() const

返回MIP级别。默认值为0。

另请参阅 setLevel()。

[since 6.7] int QRhiColorAttachment::multiViewCount() const

返回当前设置的视图数量。默认值为 0,表示带有此颜色附加项的渲染目标不会用于多视图渲染。

该函数在 Qt 6.7 中引入。

另请参阅 setMultiViewCount()。

QRhiRenderBuffer *QRhiColorAttachment::renderBuffer() const

返回该附件描述所引用的渲染缓冲区,如果不存在则返回nullptr 。

实际上,当通过多采样color 渲染缓冲区设置多采样渲染,并在渲染阶段结束时将其解析为非多采样纹理时,将QRhiRenderBuffer 与QRhiColorAttachment 关联最为合理。

另请参阅 setRenderBuffer()。

int QRhiColorAttachment::resolveLayer() const

返回当前设置的解析纹理层。默认值为 0。

另请参阅 setResolveLayer()。

int QRhiColorAttachment::resolveLevel() const

返回当前设置的纹理解析 MIP 级别。默认值为 0。

另请参阅 setResolveLevel()。

QRhiTexture *QRhiColorAttachment::resolveTexture() const

返回该附件描述所引用的解析纹理;如果不存在,则返回nullptr 。

当附件引用多采样纹理或渲染缓冲区时,应设置一个非空的解析纹理。此时,resolveTexture() 中的QRhiTexture 将是一个与之尺寸相同(但采样数为 1)的非多采样 2D 纹理(或纹理数组)。 在每次渲染通道结束时,多采样内容会自动解析到该纹理中。

另请参阅 setResolveTexture()。

void QRhiColorAttachment::setLayer(int layer)

设置layer 索引。

另请参阅 layer()。

void QRhiColorAttachment::setLevel(int level)

设置miplevel 。

另请参阅 level()。

[since 6.7] void QRhiColorAttachment::setMultiViewCount(int count)

设置视图count 。设置大于1的值表示将使用带有此颜色附件的渲染目标进行多视图渲染。默认值为0。小于2的值表示不进行多视图渲染。

当count 设置为2 或更大时,颜色附件必须与一个2D纹理数组相关联。layer()和multiViewCount()共同定义了多视图渲染过程中作为目标的纹理数组元素范围。

例如,如果layer 为0 ,且multiViewCount 为2 ,则纹理数组必须包含2个(或更多)元素,多视图渲染将针对元素0和1进行渲染。此时,着色器中的gl_ViewIndex 变量的值为0 或1 ,其中视图0 对应于纹理数组元素0 ,视图1 对应于数组元素1 。

注意:将 count 设置为 大于1,将纹理数组用作texture(),并在具有此颜色附加的QRhiTextureRenderTarget 上调用beginPass(),将导致整个渲染阶段采用多视图渲染。除非需要多视图渲染,否则不应设置multiViewCount()。 除 2D 纹理数组外,其他纹理类型均无法用于多视图渲染。(尽管根据图形 API 和后端的不同,3D 纹理可能有效;但仍建议应用程序不要依赖此功能,仅将 2D 纹理数组用作多视图渲染的渲染目标)

有关多视图渲染的更多详细信息,请参阅GL_OVR_multiview。请注意,在 OpenGL (ES) 上运行时,Qt OpenGL 还要求GL_OVR_multiview2。

仅当通过isFeatureSupported() 报告支持MultiView 功能时,才可使用多视图渲染。

注意:出于 可移植性考虑 ,请注意某些图形 API 在多视图渲染方面存在的限制。建议多视图渲染阶段不要依赖GL_OVR_multiview声明为不支持的任何功能。 唯一的例外是依赖于gl_ViewIndex 的、除gl_Position 之外的着色器阶段输出:可以依赖这些输出(即使在 OpenGL 中),因为QRhi 绝不会在GL_OVR_multiview2 未同时存在的情况下报告支持多视图。

注意:多视图 渲染不支持与细分或几何着色器结合使用,尽管某些图形 API 的某些实现可能允许这样做。

该函数在 Qt 6.7 中引入。

另请参阅 multiViewCount()。

void QRhiColorAttachment::setRenderBuffer(QRhiRenderBuffer *rb)

设置渲染缓冲区rb 。

注意: texture() 和renderBuffer() 不能同时被设置(即不能同时为非空)。

另请参阅 renderBuffer()。

void QRhiColorAttachment::setResolveLayer(int layer)

设置要使用的解析纹理layer 。

另请参阅 resolveLayer()。

void QRhiColorAttachment::setResolveLevel(int level)

设置要使用的解析纹理MIPlevel 。

另请参阅 resolveLevel()。

void QRhiColorAttachment::setResolveTexture(QRhiTexture *tex)

设置解析纹理tex 。

tex 该参数应为 2D 纹理或 2D 纹理数组。无论哪种情况,解析操作都针对 `tex` 的单个层(数组元素)中的单个 MIP 级别。MIP 级别和数组层分别由 `resolveLevel()` 和 `resolveLayer()` 指定。

multiview 是一个例外:当颜色附件与纹理数组相关联且启用了多视图时,解析纹理也必须是一个纹理数组,且其元素数量需足以覆盖所有视图。在这种情况下,所有对应视图的元素都会被自动解析;其行为类似于以下伪代码:

for (i = 0; i < multiViewCount(); ++i)
    resolve texture's layer() + i into resolveTexture's resolveLayer() + i

在渲染阶段结束时,设置一个非多采样纹理来自动解析多采样纹理或渲染缓冲区,通常优于直接处理多采样纹理(且不设置解析纹理),因为这避免了编写专门处理多采样纹理的片段着色器的必要(如sampler2DMS 、texelFetch 等), 等),而是允许使用与最初纹理附件非多采样时完全相同的着色器。此方法的代价是需要额外占用一个资源(非多采样的tex )。

另请参阅 resolveTexture()。

void QRhiColorAttachment::setTexture(QRhiTexture *tex)

设置纹理tex 。

注意: texture() 和renderBuffer() 不能同时被设置(即不能同时为非空)。

另请参阅 texture()。

QRhiTexture *QRhiColorAttachment::texture() const

返回该附件描述所引用的纹理;如果不存在,则返回nullptr 。

另请参阅 setTexture()。

© 2026 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.