QRhiShaderResourceBindings Class

Encapsulates resources for making buffer, texture, sampler resources visible to shaders. More...

Header: #include <QRhiShaderResourceBindings>
qmake: QT += rhi
Inherits: QRhiResource

Public Functions

QVector<QRhiShaderResourceBinding> bindings() const
virtual bool build() = 0
bool isLayoutCompatible(const QRhiShaderResourceBindings *other) const
void setBindings(const QVector<QRhiShaderResourceBinding> &b)

Reimplemented Public Functions

virtual QRhiResource::Type resourceType() const override

Protected Variables

QVector<QRhiShaderResourceBinding> m_bindings

Additional Inherited Members

Detailed Description

Encapsulates resources for making buffer, texture, sampler resources visible to shaders.

A QRhiShaderResourceBindings is a collection of QRhiShaderResourceBinding objects, each of which describe a single binding.

Take a fragment shader with the following interface:


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

  layout(binding = 1) uniform sampler2D tex;

To make resources visible to the shader, the following QRhiShaderResourceBindings could be created and then passed to QRhiGraphicsPipeline::setShaderResourceBindings():


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

This assumes that ubuf is a QRhiBuffer, texture is a QRhiTexture, while sampler is a QRhiSampler. The example also assumes that the uniform block is present in the vertex shader as well so the same buffer is made visible to the vertex stage too.

Advanced usage

Building on the above example, let's assume that a pass now needs to use the exact same pipeline and shaders with a different texture. Creating a whole separate QRhiGraphicsPipeline just for this would be an overkill. This is why QRhiCommandBuffer::setShaderResources() allows specifying a srb argument. As long as the layouts (so the number of bindings and the binding points) match between two QRhiShaderResourceBindings, they can both be used with the same pipeline, assuming the pipeline was built with one of them in the first place.

Creating and then using a new srb2 that is very similar to srb with the exception of referencing another texture could be implemented like the following:


  srb2 = rhi->newShaderResourceBindings();
  QVector<QRhiShaderResourceBinding> bindings = srb->bindings();
  bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, anotherTexture, sampler);
  srb2->setBindings(bindings);
  srb2->build();
  ...
  cb->setGraphicsPipeline(ps);
  cb->setShaderResources(srb2); // binds srb2

Member Type Documentation

Property Documentation

Member Function Documentation

QVector<QRhiShaderResourceBinding> QRhiShaderResourceBindings::bindings() const

See also setBindings().

[pure virtual] bool QRhiShaderResourceBindings::build()

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

Returns true if the layout is compatible with other. The layout does not include the actual resource (such as, buffer or texture) and related parameters (such as, offset or size). It does include the binding point, pipeline stage, and resource type, however. The number and order of the bindings must also match in order to be compatible.

When there is a QRhiGraphicsPipeline created with this QRhiShaderResourceBindings, and the function returns true, other can then safely be passed to QRhiCommandBuffer::setShaderResources(), and so be used with the pipeline in place of this QRhiShaderResourceBindings.

This function can be called before build() as well. The bindings must already be set via setBindings() however.

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

Reimplemented from QRhiResource::resourceType().

Returns the resource type.

void QRhiShaderResourceBindings::setBindings(const QVector<QRhiShaderResourceBinding> &b)

See also bindings().

Member Variable Documentation

Related Non-Members

Macro Documentation