本页内容

QRhiShaderResourceBindings 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
继承自: QRhiResource

公共函数

const QRhiShaderResourceBinding *bindingAt(qsizetype index) const
qsizetype bindingCount() const
const QRhiShaderResourceBinding *cbeginBindings() const
const QRhiShaderResourceBinding *cendBindings() const
virtual bool create() = 0
bool isLayoutCompatible(const QRhiShaderResourceBindings *other) const
QVector<quint32> serializedLayoutDescription() const
void setBindings(std::initializer_list<QRhiShaderResourceBinding> list)
void setBindings(InputIterator first, InputIterator last)

重新实现的公共函数

virtual QRhiResource::Type resourceType() const override

详细说明

QRhiShaderResourceBindings 是一个由QRhiShaderResourceBinding 对象组成的集合,其中每个对象描述一个绑定。

假设有一个具有以下接口的片段着色器:

layout(std140, binding = 0) uniform buf {
    mat4 mvp;
    int flip;
} ubuf;

layout(binding = 1) uniform sampler2D tex;

为了使资源对着色器可见,可以创建以下 QRhiShaderResourceBindings,然后将其传递给QRhiGraphicsPipeline::setShaderResourceBindings():

QRhiShaderResourceBindings *srb = rhi->newShaderResourceBindings();
srb->setBindings({
    QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf),
    QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, texture, sampler)
});
srb->create();
// ...
QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
// ...
ps->setShaderResourceBindings(srb);
ps->create();
// ...
cb->setGraphicsPipeline(ps);
cb->setShaderResources(); // binds srb

此处假设ubuf 是一个QRhiBuffer ,texture 是一个QRhiTexture ,而sampler 是一个QRhiSampler 。该示例还假设顶点着色器中也存在uniform块,因此同一缓冲区也会对顶点阶段可见。

高级用法

基于上述示例,假设现在某个渲染通道需要使用完全相同的管道和着色器,但使用不同的纹理。仅为此目的而创建一个完全独立的QRhiGraphicsPipeline 就显得有些大材小用了。因此,QRhiCommandBuffer::setShaderResources() 允许指定srb 参数。 只要两个 QRhiShaderResourceBindings 之间的布局(即绑定数量和绑定点)匹配,它们就可以与同一条管道配合使用——前提是该管道最初是使用其中一个创建的。更多详细信息请参阅isLayoutCompatible()。

QRhiShaderResourceBindings *srb2 = rhi->newShaderResourceBindings();
// ...
cb->setGraphicsPipeline(ps);
cb->setShaderResources(srb2); // binds srb2

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

成员函数文档

const QRhiShaderResourceBinding *QRhiShaderResourceBindings::bindingAt(qsizetype index) const

返回位于指定index 处的绑定。

qsizetype QRhiShaderResourceBindings::bindingCount() const

返回绑定项的数量。

const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cbeginBindings() const

返回一个指向绑定列表中第一个元素的常量迭代器。

const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cendBindings() const

返回一个常量迭代器,该迭代器指向绑定列表中最后一个项的下一位置。

[pure virtual] bool QRhiShaderResourceBindings::create()

创建相应的资源绑定集。根据底层图形 API 的不同,此操作可能涉及创建本机图形资源,因此不应认为这是一项开销很小的操作。

如果此前已调用过 create() 但未调用相应的destroy(),则会先隐式调用destroy()。

成功时返回true ,失败时返回false 。无论返回值为何,调用destroy()始终是安全的。

bool QRhiShaderResourceBindings::isLayoutCompatible(const QRhiShaderResourceBindings *other) const

如果布局与other 兼容,则返回true 。该布局不包含实际资源(例如缓冲区或纹理)及相关参数(例如偏移量或大小),但包含绑定点、管道阶段和资源类型。此外,绑定项的数量和顺序也必须一致,才能视为兼容。

当使用该QRhiShaderResourceBindings 创建了QRhiGraphicsPipeline ,且函数返回true 时,other 即可安全地传递给QRhiCommandBuffer::setShaderResources(),从而在管道中替代该QRhiShaderResourceBindings 使用。

注意:该 函数必须仅在create() 成功执行后才调用,因为它依赖于底层数据结构预处理过程中生成的数据。这样,该函数就能实现一种比遍历两个绑定列表并对每对元素调用QRhiShaderResourceBinding::isLayoutCompatible() 更高效的比较方法。当该函数被高频调用时,这一点尤为重要。

另请参阅 serializedLayoutDescription()。

[override virtual] QRhiResource::Type QRhiShaderResourceBindings::resourceType() const

重写:QRhiResource::resourceType() const。

返回资源类型。

QVector<quint32> QRhiShaderResourceBindings::serializedLayoutDescription() const

返回一个整数向量,其中包含一个描述绑定列表布局的不透明数据块,即与 `layout compatibility tests` 相关的数据。

给定两个对象srb1 和srb2 ,如果此函数返回的数据完全相同,则srb1->isLayoutCompatible(srb2) 成立,反之亦然。

注意: 返回的数据 旨在用于存储在内存中,并在所属QRhi 对象的生命周期内进行比较。它不适用于存储在磁盘上、在进程之间复用,或与多个可能具有不同后端的QRhi 实例一起使用。

另请参阅 isLayoutCompatible()。

void QRhiShaderResourceBindings::setBindings(std::initializer_list<QRhiShaderResourceBinding> list)

设置绑定项的list 。

template <typename InputIterator> void QRhiShaderResourceBindings::setBindings(InputIterator first, InputIterator last)

设置来自迭代器first 和last 的绑定列表。

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