本页内容

QRhiResource 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
由以下版本继承:
12 种类型

QRhiBuffer、QRhiCommandBuffer 、QRhiComputePipeline 、QRhiGraphicsPipeline 、QRhiRenderBuffer 、QRhiRenderPassDescriptor 、QRhiRenderTarget 、QRhiSampler 、QRhiShaderResourceBindings 、QRhiShadingRateMap 、QRhiSwapChain ,以及QRhiTexture

公共类型

enum Type { Buffer, Texture, Sampler, RenderBuffer, RenderPassDescriptor, …, ShadingRateMap }

公共函数

virtual ~QRhiResource()
void deleteLater()
virtual void destroy() = 0
quint64 globalResourceId() const
QByteArray name() const
virtual QRhiResource::Type resourceType() const = 0
QRhi *rhi() const
void setName(const QByteArray &name)

受保护函数

QRhiResource(const QRhiResource &)
QRhiResource &operator=(const QRhiResource &)

详细说明

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

成员类型文档

enum QRhiResource::Type

指定资源的类型。

常量值
QRhiResource::Buffer0
QRhiResource::Texture1
QRhiResource::Sampler2
QRhiResource::RenderBuffer3
QRhiResource::RenderPassDescriptor4
QRhiResource::SwapChainRenderTarget5
QRhiResource::TextureRenderTarget6
QRhiResource::ShaderResourceBindings7
QRhiResource::GraphicsPipeline8
QRhiResource::SwapChain9
QRhiResource::ComputePipeline10
QRhiResource::CommandBuffer11
QRhiResource::ShadingRateMap12

成员函数文档

[protected delete] QRhiResource::QRhiResource(const QRhiResource &)

复制并构造一个QRhiResource 实例。该函数已被删除。

[virtual noexcept] QRhiResource::~QRhiResource()

析构函数。

释放(或请求延迟释放)底层本机图形资源(如有)。

注意: 当前帧的命令所引用的资源 ,在通过QRhi::endFrame() 提交该帧之前不应被释放。

另请参阅 destroy()。

void QRhiResource::deleteLater()

如果在未录制帧的情况下调用此函数,其效果等同于删除该对象。但在QRhi::beginFrame()和QRhi::endFrame()之间,其行为有所不同:QRhiResource 不会被销毁,直到该帧通过QRhi::endFrame()提交为止,从而满足QRhi 的要求,即不得更改被录制帧引用的QRhiResource 对象。

如果创建该对象的QRhi 已被销毁,则该对象将立即被删除。

在许多情况下,使用 deleteLater() 非常方便, 它通过提供一种方法来确保 C++ 对象实例(如QRhiBuffer 、QRhiTexture 、 等)本身也能一直保持有效,直至当前帧结束。

以下示例展示了一种便捷的方法,用于创建仅在一个帧中使用并在 endFrame() 中自动释放的一次性缓冲区。 (至于底层的原生缓冲区,则适用常规保证:QRhi 后端会推迟释放这些缓冲区,直到确保 GPU 访问该缓冲区的帧已完成后再进行释放)

rhi->beginFrame(swapchain);
QRhiBuffer *buf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, 256);
buf->deleteLater(); // !
u = rhi->nextResourceUpdateBatch();
u->uploadStaticBuffer(buf, data);
// ... draw with buf
rhi->endFrame();

另请参阅 destroy()。

[pure virtual] void QRhiResource::destroy()

释放(或请求延迟释放)底层本机图形资源。可安全地多次调用,后续调用将不执行任何操作。

一旦调用 destroy(),可以通过再次调用create() 来重用该QRhiResource 实例。这将导致在底层创建新的本机图形资源。

注意: 当前帧中命令引用的资源 ,在QRhi::endFrame()提交该帧之前不应被释放。

QRhiResource 的析构函数也会执行相同的操作,因此在删除QRhiResource 之前无需调用此函数。

另请参阅 deleteLater()。

quint64 QRhiResource::globalResourceId() const

返回此QRhiResource 的全局唯一标识符。

用户代码很少需要直接处理该值。该标识符仅用于内部跟踪和账务管理。

QByteArray QRhiResource::name() const

返回当前设置的对象名称。默认情况下,该名称为空。

另请参阅 setName()。

[pure virtual] QRhiResource::Type QRhiResource::resourceType() const

返回资源的类型。

QRhi *QRhiResource::rhi() const

返回创建该资源的QRhi 。

如果创建此对象的QRhi 已被销毁,则返回nullptr 。

void QRhiResource::setName(const QByteArray &name)

为对象设置name 。

这使得可以在图形调试工具(如RenderDoc和XCode)中为原生图形资源获取描述性名称。

在通过相应的图形 API 传递名称来命名本机对象时,请注意:如果不支持QRhi::DebugMarkers ,该名称将被忽略;此外,根据后端的不同,如果未设置QRhi::EnableDebugMarkers ,该名称也可能被忽略。

注意: 对于缓冲区、渲染缓冲区和纹理以外的对象,根据后端的不同,该 名称可能会被忽略。

注意:名称 可能会被修改。对于带有插槽的资源(例如由多个本机缓冲区支持的QRhiBuffer ),QRhi 会追加一个后缀,以便于区分底层的各个本机缓冲区。

另请参阅 name()。

[protected delete] QRhiResource &QRhiResource::operator=(const QRhiResource &)

将other 复制赋值给此QRhiResource 实例。该函数已被删除。

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