Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Gui module
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#ifndef QRHIMETAL_P_H
38#define QRHIMETAL_P_H
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49//
50
51#include "qrhimetal_p.h"
52#include "qrhi_p_p.h"
53#include <QWindow>
54
55QT_BEGIN_NAMESPACE
56
57static const int QMTL_FRAMES_IN_FLIGHT = 2;
58
59// have to hide the ObjC stuff, this header cannot contain MTL* at all
60struct QMetalBufferData;
61
62struct QMetalBuffer : public QRhiBuffer
63{
64 QMetalBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size);
65 ~QMetalBuffer();
66 void release() override;
67 bool build() override;
68
69 QMetalBufferData *d;
70 uint generation = 0;
71 int lastActiveFrameSlot = -1;
72 friend class QRhiMetal;
73 friend struct QMetalShaderResourceBindings;
74};
75
76struct QMetalRenderBufferData;
77
78struct QMetalRenderBuffer : public QRhiRenderBuffer
79{
80 QMetalRenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize,
81 int sampleCount, QRhiRenderBuffer::Flags flags);
82 ~QMetalRenderBuffer();
83 void release() override;
84 bool build() override;
85 QRhiTexture::Format backingFormat() const override;
86
87 QMetalRenderBufferData *d;
88 int samples = 1;
89 uint generation = 0;
90 int lastActiveFrameSlot = -1;
91 friend class QRhiMetal;
92};
93
94struct QMetalTextureData;
95
96struct QMetalTexture : public QRhiTexture
97{
98 QMetalTexture(QRhiImplementation *rhi, Format format, const QSize &pixelSize,
99 int sampleCount, Flags flags);
100 ~QMetalTexture();
101 void release() override;
102 bool build() override;
103 bool buildFrom(const QRhiNativeHandles *src) override;
104 const QRhiNativeHandles *nativeHandles() override;
105
106 bool prepareBuild(QSize *adjustedSize = nullptr);
107
108 QMetalTextureData *d;
109 QRhiMetalTextureNativeHandles nativeHandlesStruct;
110 int mipLevelCount = 0;
111 int samples = 1;
112 uint generation = 0;
113 int lastActiveFrameSlot = -1;
114 friend class QRhiMetal;
115 friend struct QMetalShaderResourceBindings;
116 friend struct QMetalTextureData;
117};
118
119struct QMetalSamplerData;
120
121struct QMetalSampler : public QRhiSampler
122{
123 QMetalSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
124 AddressMode u, AddressMode v);
125 ~QMetalSampler();
126 void release() override;
127 bool build() override;
128
129 QMetalSamplerData *d;
130 uint generation = 0;
131 int lastActiveFrameSlot = -1;
132 friend class QRhiMetal;
133 friend struct QMetalShaderResourceBindings;
134};
135
136struct QMetalRenderPassDescriptor : public QRhiRenderPassDescriptor
137{
138 QMetalRenderPassDescriptor(QRhiImplementation *rhi);
139 ~QMetalRenderPassDescriptor();
140 void release() override;
141
142 // there is no MTLRenderPassDescriptor here as one will be created for each pass in beginPass()
143
144 // but the things needed for the render pipeline descriptor have to be provided
145 static const int MAX_COLOR_ATTACHMENTS = 8;
146 int colorAttachmentCount = 0;
147 bool hasDepthStencil = false;
148 int colorFormat[MAX_COLOR_ATTACHMENTS];
149 int dsFormat;
150};
151
152struct QMetalRenderTargetData;
153
154struct QMetalReferenceRenderTarget : public QRhiRenderTarget
155{
156 QMetalReferenceRenderTarget(QRhiImplementation *rhi);
157 ~QMetalReferenceRenderTarget();
158 void release() override;
159
160 QSize pixelSize() const override;
161 float devicePixelRatio() const override;
162 int sampleCount() const override;
163
164 QMetalRenderTargetData *d;
165};
166
167struct QMetalTextureRenderTarget : public QRhiTextureRenderTarget
168{
169 QMetalTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc, Flags flags);
170 ~QMetalTextureRenderTarget();
171 void release() override;
172
173 QSize pixelSize() const override;
174 float devicePixelRatio() const override;
175 int sampleCount() const override;
176
177 QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
178 bool build() override;
179
180 QMetalRenderTargetData *d;
181 friend class QRhiMetal;
182};
183
184struct QMetalShaderResourceBindings : public QRhiShaderResourceBindings
185{
186 QMetalShaderResourceBindings(QRhiImplementation *rhi);
187 ~QMetalShaderResourceBindings();
188 void release() override;
189 bool build() override;
190
191 QVector<QRhiShaderResourceBinding> sortedBindings;
192 int maxBinding = -1;
193
194 struct BoundUniformBufferData {
195 quint64 id;
196 uint generation;
197 };
198 struct BoundSampledTextureData {
199 quint64 texId;
200 uint texGeneration;
201 quint64 samplerId;
202 uint samplerGeneration;
203 };
204 struct BoundStorageImageData {
205 quint64 id;
206 uint generation;
207 };
208 struct BoundStorageBufferData {
209 quint64 id;
210 uint generation;
211 };
212 struct BoundResourceData {
213 union {
214 BoundUniformBufferData ubuf;
215 BoundSampledTextureData stex;
216 BoundStorageImageData simage;
217 BoundStorageBufferData sbuf;
218 };
219 };
220 QVector<BoundResourceData> boundResourceData;
221
222 uint generation = 0;
223 friend class QRhiMetal;
224};
225
226struct QMetalGraphicsPipelineData;
227
228struct QMetalGraphicsPipeline : public QRhiGraphicsPipeline
229{
230 QMetalGraphicsPipeline(QRhiImplementation *rhi);
231 ~QMetalGraphicsPipeline();
232 void release() override;
233 bool build() override;
234
235 QMetalGraphicsPipelineData *d;
236 uint generation = 0;
237 int lastActiveFrameSlot = -1;
238 friend class QRhiMetal;
239};
240
241struct QMetalComputePipelineData;
242
243struct QMetalComputePipeline : public QRhiComputePipeline
244{
245 QMetalComputePipeline(QRhiImplementation *rhi);
246 ~QMetalComputePipeline();
247 void release() override;
248 bool build() override;
249
250 QMetalComputePipelineData *d;
251 uint generation = 0;
252 int lastActiveFrameSlot = -1;
253 friend class QRhiMetal;
254};
255
256struct QMetalCommandBufferData;
257struct QMetalSwapChain;
258
259struct QMetalCommandBuffer : public QRhiCommandBuffer
260{
261 QMetalCommandBuffer(QRhiImplementation *rhi);
262 ~QMetalCommandBuffer();
263 void release() override;
264
265 QMetalCommandBufferData *d = nullptr;
266 QRhiMetalCommandBufferNativeHandles nativeHandlesStruct;
267
268 enum PassType {
269 NoPass,
270 RenderPass,
271 ComputePass
272 };
273
274 PassType recordingPass;
275 QRhiRenderTarget *currentTarget;
276 QRhiGraphicsPipeline *currentGraphicsPipeline;
277 QRhiComputePipeline *currentComputePipeline;
278 uint currentPipelineGeneration;
279 QRhiShaderResourceBindings *currentGraphicsSrb;
280 QRhiShaderResourceBindings *currentComputeSrb;
281 uint currentSrbGeneration;
282 int currentResSlot;
283 QRhiBuffer *currentIndexBuffer;
284 quint32 currentIndexOffset;
285 QRhiCommandBuffer::IndexFormat currentIndexFormat;
286
287 const QRhiNativeHandles *nativeHandles();
288 void resetState();
289 void resetPerPassState();
290 void resetPerPassCachedState();
291};
292
293struct QMetalSwapChainData;
294
295struct QMetalSwapChain : public QRhiSwapChain
296{
297 QMetalSwapChain(QRhiImplementation *rhi);
298 ~QMetalSwapChain();
299 void release() override;
300
301 QRhiCommandBuffer *currentFrameCommandBuffer() override;
302 QRhiRenderTarget *currentFrameRenderTarget() override;
303 QSize surfacePixelSize() override;
304
305 QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
306
307 bool buildOrResize() override;
308
309 void chooseFormats();
310
311 QWindow *window = nullptr;
312 QSize pixelSize;
313 int currentFrameSlot = 0; // 0..QMTL_FRAMES_IN_FLIGHT-1
314 int frameCount = 0;
315 int samples = 1;
316 QMetalReferenceRenderTarget rtWrapper;
317 QMetalCommandBuffer cbWrapper;
318 QMetalRenderBuffer *ds = nullptr;
319 QMetalSwapChainData *d = nullptr;
320};
321
322struct QRhiMetalData;
323
324class QRhiMetal : public QRhiImplementation
325{
326public:
327 QRhiMetal(QRhiMetalInitParams *params, QRhiMetalNativeHandles *importDevice = nullptr);
328 ~QRhiMetal();
329
330 bool create(QRhi::Flags flags) override;
331 void destroy() override;
332
333 QRhiGraphicsPipeline *createGraphicsPipeline() override;
334 QRhiComputePipeline *createComputePipeline() override;
335 QRhiShaderResourceBindings *createShaderResourceBindings() override;
336 QRhiBuffer *createBuffer(QRhiBuffer::Type type,
337 QRhiBuffer::UsageFlags usage,
338 int size) override;
339 QRhiRenderBuffer *createRenderBuffer(QRhiRenderBuffer::Type type,
340 const QSize &pixelSize,
341 int sampleCount,
342 QRhiRenderBuffer::Flags flags) override;
343 QRhiTexture *createTexture(QRhiTexture::Format format,
344 const QSize &pixelSize,
345 int sampleCount,
346 QRhiTexture::Flags flags) override;
347 QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
348 QRhiSampler::Filter mipmapMode,
349 QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
350
351 QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
352 QRhiTextureRenderTarget::Flags flags) override;
353
354 QRhiSwapChain *createSwapChain() override;
355 QRhi::FrameOpResult beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags) override;
356 QRhi::FrameOpResult endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) override;
357 QRhi::FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb) override;
358 QRhi::FrameOpResult endOffscreenFrame() override;
359 QRhi::FrameOpResult finish() override;
360
361 void resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
362
363 void beginPass(QRhiCommandBuffer *cb,
364 QRhiRenderTarget *rt,
365 const QColor &colorClearValue,
366 const QRhiDepthStencilClearValue &depthStencilClearValue,
367 QRhiResourceUpdateBatch *resourceUpdates) override;
368 void endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
369
370 void setGraphicsPipeline(QRhiCommandBuffer *cb,
371 QRhiGraphicsPipeline *ps) override;
372
373 void setShaderResources(QRhiCommandBuffer *cb,
374 QRhiShaderResourceBindings *srb,
375 int dynamicOffsetCount,
376 const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) override;
377
378 void setVertexInput(QRhiCommandBuffer *cb,
379 int startBinding, int bindingCount, const QRhiCommandBuffer::VertexInput *bindings,
380 QRhiBuffer *indexBuf, quint32 indexOffset,
381 QRhiCommandBuffer::IndexFormat indexFormat) override;
382
383 void setViewport(QRhiCommandBuffer *cb, const QRhiViewport &viewport) override;
384 void setScissor(QRhiCommandBuffer *cb, const QRhiScissor &scissor) override;
385 void setBlendConstants(QRhiCommandBuffer *cb, const QColor &c) override;
386 void setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) override;
387
388 void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
389 quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) override;
390
391 void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
392 quint32 instanceCount, quint32 firstIndex,
393 qint32 vertexOffset, quint32 firstInstance) override;
394
395 void debugMarkBegin(QRhiCommandBuffer *cb, const QByteArray &name) override;
396 void debugMarkEnd(QRhiCommandBuffer *cb) override;
397 void debugMarkMsg(QRhiCommandBuffer *cb, const QByteArray &msg) override;
398
399 void beginComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
400 void endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
401 void setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) override;
402 void dispatch(QRhiCommandBuffer *cb, int x, int y, int z) override;
403
404 const QRhiNativeHandles *nativeHandles(QRhiCommandBuffer *cb) override;
405 void beginExternal(QRhiCommandBuffer *cb) override;
406 void endExternal(QRhiCommandBuffer *cb) override;
407
408 QVector<int> supportedSampleCounts() const override;
409 int ubufAlignment() const override;
410 bool isYUpInFramebuffer() const override;
411 bool isYUpInNDC() const override;
412 bool isClipDepthZeroToOne() const override;
413 QMatrix4x4 clipSpaceCorrMatrix() const override;
414 bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
415 bool isFeatureSupported(QRhi::Feature feature) const override;
416 int resourceLimit(QRhi::ResourceLimit limit) const override;
417 const QRhiNativeHandles *nativeHandles() override;
418 void sendVMemStatsToProfiler() override;
419 void makeThreadLocalNativeContextCurrent() override;
420
421 void executeDeferredReleases(bool forced = false);
422 void finishActiveReadbacks(bool forced = false);
423 qsizetype subresUploadByteSize(const QRhiTextureSubresourceUploadDescription &subresDesc) const;
424 void enqueueSubresUpload(QMetalTexture *texD, void *mp, void *blitEncPtr,
425 int layer, int level, const QRhiTextureSubresourceUploadDescription &subresDesc,
426 qsizetype *curOfs);
427 void enqueueResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates);
428 void executeBufferHostWritesForCurrentFrame(QMetalBuffer *bufD);
429 void enqueueShaderResourceBindings(QMetalShaderResourceBindings *srbD, QMetalCommandBuffer *cbD,
430 int dynamicOffsetCount,
431 const QRhiCommandBuffer::DynamicOffset *dynamicOffsets,
432 bool offsetOnlyChange);
433 int effectiveSampleCount(int sampleCount) const;
434
435 bool importedDevice = false;
436 bool importedCmdQueue = false;
437 QMetalSwapChain *currentSwapChain = nullptr;
438 QSet<QMetalSwapChain *> swapchains;
439 QRhiMetalNativeHandles nativeHandlesStruct;
440
441 struct {
442 int maxTextureSize = 4096;
443 } caps;
444
445 QRhiMetalData *d = nullptr;
446};
447
448QT_END_NAMESPACE
449
450#endif
451

Warning: That file was not part of the compilation database. It may have many parsing errors.