35class QRhiImplementation
38 virtual ~QRhiImplementation();
40 virtual bool create(QRhi::Flags flags) = 0;
41 virtual void destroy() = 0;
42 virtual QRhi::AdapterList enumerateAdaptersBeforeCreate(QRhiNativeHandles *nativeHandles)
const;
44 virtual QRhiGraphicsPipeline *createGraphicsPipeline() = 0;
45 virtual QRhiComputePipeline *createComputePipeline() = 0;
46 virtual QRhiShaderResourceBindings *createShaderResourceBindings() = 0;
47 virtual QRhiBuffer *createBuffer(QRhiBuffer::Type type,
48 QRhiBuffer::UsageFlags usage,
50 virtual QRhiRenderBuffer *createRenderBuffer(QRhiRenderBuffer::Type type,
51 const QSize &pixelSize,
53 QRhiRenderBuffer::Flags flags,
54 QRhiTexture::Format backingFormatHint) = 0;
55 virtual QRhiTexture *createTexture(QRhiTexture::Format format,
56 const QSize &pixelSize,
60 QRhiTexture::Flags flags) = 0;
61 virtual QRhiSampler *createSampler(QRhiSampler::Filter magFilter,
62 QRhiSampler::Filter minFilter,
63 QRhiSampler::Filter mipmapMode,
64 QRhiSampler:: AddressMode u,
65 QRhiSampler::AddressMode v,
66 QRhiSampler::AddressMode w) = 0;
68 virtual QRhiTextureRenderTarget *createTextureRenderTarget(
const QRhiTextureRenderTargetDescription &desc,
69 QRhiTextureRenderTarget::Flags flags) = 0;
71 virtual QRhiShadingRateMap *createShadingRateMap() = 0;
73 virtual QRhiSwapChain *createSwapChain() = 0;
74 virtual QRhi::FrameOpResult beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags) = 0;
75 virtual QRhi::FrameOpResult endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) = 0;
76 virtual QRhi::FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi::BeginFrameFlags flags) = 0;
77 virtual QRhi::FrameOpResult endOffscreenFrame(QRhi::EndFrameFlags flags) = 0;
78 virtual QRhi::FrameOpResult finish() = 0;
80 virtual void resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
82 virtual void beginPass(QRhiCommandBuffer *cb,
84 const QColor &colorClearValue,
85 const QRhiDepthStencilClearValue &depthStencilClearValue,
86 QRhiResourceUpdateBatch *resourceUpdates,
87 QRhiCommandBuffer::BeginPassFlags flags) = 0;
88 virtual void endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
90 virtual void setGraphicsPipeline(QRhiCommandBuffer *cb,
91 QRhiGraphicsPipeline *ps) = 0;
93 virtual void setShaderResources(QRhiCommandBuffer *cb,
94 QRhiShaderResourceBindings *srb,
95 int dynamicOffsetCount,
96 const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) = 0;
98 virtual void setVertexInput(QRhiCommandBuffer *cb,
99 int startBinding,
int bindingCount,
const QRhiCommandBuffer::VertexInput *bindings,
100 QRhiBuffer *indexBuf, quint32 indexOffset,
101 QRhiCommandBuffer::IndexFormat indexFormat) = 0;
103 virtual void setViewport(QRhiCommandBuffer *cb,
const QRhiViewport &viewport) = 0;
104 virtual void setScissor(QRhiCommandBuffer *cb,
const QRhiScissor &scissor) = 0;
105 virtual void setBlendConstants(QRhiCommandBuffer *cb,
const QColor &c) = 0;
106 virtual void setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) = 0;
107 virtual void setShadingRate(QRhiCommandBuffer *cb,
const QSize &coarsePixelSize) = 0;
109 virtual void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
110 quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) = 0;
111 virtual void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
112 quint32 instanceCount, quint32 firstIndex,
113 qint32 vertexOffset, quint32 firstInstance) = 0;
115 virtual void debugMarkBegin(QRhiCommandBuffer *cb,
const QByteArray &name) = 0;
116 virtual void debugMarkEnd(QRhiCommandBuffer *cb) = 0;
117 virtual void debugMarkMsg(QRhiCommandBuffer *cb,
const QByteArray &msg) = 0;
119 virtual void beginComputePass(QRhiCommandBuffer *cb,
120 QRhiResourceUpdateBatch *resourceUpdates,
121 QRhiCommandBuffer::BeginPassFlags flags) = 0;
122 virtual void endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
123 virtual void setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) = 0;
124 virtual void dispatch(QRhiCommandBuffer *cb,
int x,
int y,
int z) = 0;
126 virtual const QRhiNativeHandles *nativeHandles(QRhiCommandBuffer *cb) = 0;
127 virtual void beginExternal(QRhiCommandBuffer *cb) = 0;
128 virtual void endExternal(QRhiCommandBuffer *cb) = 0;
129 virtual double lastCompletedGpuTime(QRhiCommandBuffer *cb) = 0;
131 virtual QList<
int> supportedSampleCounts()
const = 0;
132 virtual int ubufAlignment()
const = 0;
133 virtual QList<QSize> supportedShadingRates(
int sampleCount)
const = 0;
134 virtual bool isYUpInFramebuffer()
const = 0;
135 virtual bool isYUpInNDC()
const = 0;
136 virtual bool isClipDepthZeroToOne()
const = 0;
137 virtual QMatrix4x4 clipSpaceCorrMatrix()
const = 0;
138 virtual bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags)
const = 0;
139 virtual bool isFeatureSupported(QRhi::Feature feature)
const = 0;
140 virtual int resourceLimit(QRhi::ResourceLimit limit)
const = 0;
141 virtual const QRhiNativeHandles *nativeHandles() = 0;
142 virtual QRhiDriverInfo driverInfo()
const = 0;
143 virtual QRhiStats statistics() = 0;
144 virtual bool makeThreadLocalNativeContextCurrent() = 0;
145 virtual void setQueueSubmitParams(QRhiNativeHandles *params) = 0;
146 virtual void releaseCachedResources() = 0;
147 virtual bool isDeviceLost()
const = 0;
149 virtual QByteArray pipelineCacheData() = 0;
150 virtual void setPipelineCacheData(
const QByteArray &data) = 0;
152 static QRhiImplementation *newInstance(QRhi::Implementation impl, QRhiInitParams *params, QRhiNativeHandles *importDevice);
153 void prepareForCreate(QRhi *rhi, QRhi::Implementation impl, QRhi::Flags flags, QRhiAdapter *adapter);
155 bool isCompressedFormat(QRhiTexture::Format format)
const;
156 void compressedFormatInfo(QRhiTexture::Format format,
const QSize &size,
157 quint32 *bpl, quint32 *byteSize,
158 QSize *blockDim)
const;
159 void textureFormatInfo(QRhiTexture::Format format,
const QSize &size,
160 quint32 *bpl, quint32 *byteSize, quint32 *bytesPerPixel)
const;
161 bool isStencilSupportingFormat(QRhiTexture::Format format)
const;
163 void registerResource(QRhiResource *res,
bool ownsNativeResources =
true)
170 resources.insert(res, ownsNativeResources);
173 void unregisterResource(QRhiResource *res)
175 resources.remove(res);
178 void addDeleteLater(QRhiResource *res)
181 pendingDeleteResources.insert(res);
186 void addCleanupCallback(
const QRhi::CleanupCallback &callback)
188 cleanupCallbacks.append(callback);
191 void addCleanupCallback(
const void *key,
const QRhi::CleanupCallback &callback)
193 keyedCleanupCallbacks[key] = callback;
196 void removeCleanupCallback(
const void *key)
198 keyedCleanupCallbacks.remove(key);
201 bool sanityCheckGraphicsPipeline(QRhiGraphicsPipeline *ps);
202 bool sanityCheckShaderResourceBindings(QRhiShaderResourceBindings *srb);
203 void updateLayoutDesc(QRhiShaderResourceBindings *srb);
205 quint32 pipelineCacheRhiId()
const
207 const quint32 ver = (QT_VERSION_MAJOR << 16) | (QT_VERSION_MINOR << 8) | (QT_VERSION_PATCH);
208 return (quint32(implType) << 24) | ver;
211 void pipelineCreationStart()
213 pipelineCreationTimer.start();
216 void pipelineCreationEnd()
218 accumulatedPipelineCreationTime += pipelineCreationTimer.elapsed();
221 qint64 totalPipelineCreationTime()
const
223 return accumulatedPipelineCreationTime;
226 QRhiVertexInputAttribute::Format shaderDescVariableFormatToVertexInputFormat(QShaderDescription::VariableType type)
const;
227 quint32 byteSizePerVertexForVertexInputFormat(QRhiVertexInputAttribute::Format format)
const;
229 static const QRhiShaderResourceBinding::Data *shaderResourceBindingData(
const QRhiShaderResourceBinding &binding)
234 static QRhiShaderResourceBinding::Data *shaderResourceBindingData(QRhiShaderResourceBinding &binding)
239 static bool sortedBindingLessThan(
const QRhiShaderResourceBinding &a,
const QRhiShaderResourceBinding &b)
241 return a.d.binding < b.d.binding;
244 int effectiveSampleCount(
int sampleCount)
const;
250 static const int MAX_SHADER_CACHE_ENTRIES = 128;
252 bool debugMarkers =
false;
253 int currentFrameSlot = 0;
254 bool inFrame =
false;
256 QRhiAdapter *requestedRhiAdapter =
nullptr;
259 QRhi::Implementation implType;
261 QVarLengthArray<QRhiResourceUpdateBatch *, 4> resUpdPool;
262 quint64 resUpdPoolMap = 0;
263 int lastResUpdIdx = -1;
264 QHash<QRhiResource *,
bool> resources;
265 QSet<QRhiResource *> pendingDeleteResources;
266 QVarLengthArray<QRhi::CleanupCallback, 4> cleanupCallbacks;
267 QHash<
const void *, QRhi::CleanupCallback> keyedCleanupCallbacks;
268 QElapsedTimer pipelineCreationTimer;
269 qint64 accumulatedPipelineCreationTime = 0;
272 friend class QRhiResourceUpdateBatchPrivate;
273 friend class QRhiBufferData;
807 const bool hasDepthStencil = desc.depthStencilBuffer() || desc.depthTexture();
808 dst->resize(desc.colorAttachmentCount() * 2 + (hasDepthStencil ? 1 : 0));
810 for (
auto it = desc.cbeginColorAttachments(), itEnd = desc.cendColorAttachments(); it != itEnd; ++it, ++n) {
811 const QRhiColorAttachment &colorAtt(*it);
812 if (colorAtt.texture()) {
813 TexType *texD =
QRHI_RES(TexType, colorAtt.texture());
814 (*dst)[n] = { texD->globalResourceId(), texD->generation };
815 }
else if (colorAtt.renderBuffer()) {
816 RenderBufferType *rbD =
QRHI_RES(RenderBufferType, colorAtt.renderBuffer());
817 (*dst)[n] = { rbD->globalResourceId(), rbD->generation };
819 (*dst)[n] = { 0, 0 };
822 if (colorAtt.resolveTexture()) {
823 TexType *texD =
QRHI_RES(TexType, colorAtt.resolveTexture());
824 (*dst)[n] = { texD->globalResourceId(), texD->generation };
826 (*dst)[n] = { 0, 0 };
829 if (hasDepthStencil) {
830 if (desc.depthTexture()) {
831 TexType *depthTexD =
QRHI_RES(TexType, desc.depthTexture());
832 (*dst)[n] = { depthTexD->globalResourceId(), depthTexD->generation };
833 }
else if (desc.depthStencilBuffer()) {
834 RenderBufferType *depthRbD =
QRHI_RES(RenderBufferType, desc.depthStencilBuffer());
835 (*dst)[n] = { depthRbD->globalResourceId(), depthRbD->generation };
837 (*dst)[n] = { 0, 0 };