QRhiShaderResourceBindings Class

封装用于使着色器可见缓冲区、纹理和采样器资源的资源。更多

头文件: #include <rhi/qrhi.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::GuiPrivate)
qmake: QT += gui-private
Qt 6.6
继承: QRhiResource

公共类型

flags UpdateFlags

公共函数

const QRhiShaderResourceBinding *bindingAt(qsizetype index) const
qsizetype bindingCount() const
const QRhiShaderResourceBinding *cbeginBindings() const
const QRhiShaderResourceBinding *cendBindings() const
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

该示例假定ubufQRhiBuffertextureQRhiTexture ,而samplerQRhiSampler 。该示例还假定顶点着色器中也存在均匀块,因此顶点阶段也能看到相同的缓冲区。

高级用法

在上述示例的基础上,我们假设一个通道现在需要使用完全相同的管道和着色器,但纹理却不同。为此单独创建一个完整的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

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

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

如果布局与other 兼容,则返回true 。布局不包括实际资源(如缓冲区或纹理)和相关参数(如偏移量或大小)。但它包含绑定点、流水线阶段和资源类型。为了兼容,绑定的数量和顺序也必须匹配。

当有一个QRhiGraphicsPipeline 与此QRhiShaderResourceBindings 一起创建,且函数返回true 时,other 就可以安全地传递给QRhiCommandBuffer::setShaderResources() ,从而代替此QRhiShaderResourceBindings 与管道一起使用。

注意: 该函数必须在成功创建()后才能调用,因为它依赖于底层数据结构烘焙过程中生成的数据。这样,该函数就能实现一种比较方法,这种方法比遍历两个绑定列表并在每对绑定列表上调用QRhiShaderResourceBinding::isLayoutCompatible() 更有效。尤其是在高频率调用该函数时,这一点变得尤为重要。

另请参见 serializedLayoutDescription()。

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

重实现:QRhiResource::resourceType() 常量。

返回资源类型。

QVector<quint32> QRhiShaderResourceBindings::serializedLayoutDescription() const

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

给定两个对象srb1srb2 ,如果该函数返回的数据相同,则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)

设置来自迭代器firstlast 的绑定列表。

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