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 QRHINULL_P_H
38 #define QRHINULL_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 "qrhinull_p.h"
52 #include "qrhi_p_p.h"
53 
54 QT_BEGIN_NAMESPACE
55 
56 struct QNullBuffer : public QRhiBuffer
57 {
58     QNullBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size);
59     ~QNullBuffer();
60     void release() override;
61     bool build() override;
62 
63     QByteArray data;
64 };
65 
66 struct QNullRenderBuffer : public QRhiRenderBuffer
67 {
68     QNullRenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize,
69                        int sampleCount, QRhiRenderBuffer::Flags flags);
70     ~QNullRenderBuffer();
71     void release() override;
72     bool build() override;
73     QRhiTexture::Format backingFormat() const override;
74 };
75 
76 struct QNullTexture : public QRhiTexture
77 {
78     QNullTexture(QRhiImplementation *rhi, Format format, const QSize &pixelSize,
79                   int sampleCount, Flags flags);
80     ~QNullTexture();
81     void release() override;
82     bool build() override;
83     bool buildFrom(NativeTexture src) override;
84 
85     QImage image[QRhi::MAX_LAYERS][QRhi::MAX_LEVELS];
86 };
87 
88 struct QNullSampler : public QRhiSampler
89 {
90     QNullSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
91                  AddressMode u, AddressMode v, AddressMode w);
92     ~QNullSampler();
93     void release() override;
94     bool build() override;
95 };
96 
97 struct QNullRenderPassDescriptor : public QRhiRenderPassDescriptor
98 {
99     QNullRenderPassDescriptor(QRhiImplementation *rhi);
100     ~QNullRenderPassDescriptor();
101     void release() override;
102     bool isCompatible(const QRhiRenderPassDescriptor *other) const override;
103 };
104 
105 struct QNullRenderTargetData
106 {
QNullRenderTargetDataQNullRenderTargetData107     QNullRenderTargetData(QRhiImplementation *) { }
108 
109     QNullRenderPassDescriptor *rp = nullptr;
110     QSize pixelSize;
111     float dpr = 1;
112 };
113 
114 struct QNullReferenceRenderTarget : public QRhiRenderTarget
115 {
116     QNullReferenceRenderTarget(QRhiImplementation *rhi);
117     ~QNullReferenceRenderTarget();
118     void release() override;
119 
120     QSize pixelSize() const override;
121     float devicePixelRatio() const override;
122     int sampleCount() const override;
123 
124     QNullRenderTargetData d;
125 };
126 
127 struct QNullTextureRenderTarget : public QRhiTextureRenderTarget
128 {
129     QNullTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc, Flags flags);
130     ~QNullTextureRenderTarget();
131     void release() override;
132 
133     QSize pixelSize() const override;
134     float devicePixelRatio() const override;
135     int sampleCount() const override;
136 
137     QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
138     bool build() override;
139 
140     QNullRenderTargetData d;
141 };
142 
143 struct QNullShaderResourceBindings : public QRhiShaderResourceBindings
144 {
145     QNullShaderResourceBindings(QRhiImplementation *rhi);
146     ~QNullShaderResourceBindings();
147     void release() override;
148     bool build() override;
149 };
150 
151 struct QNullGraphicsPipeline : public QRhiGraphicsPipeline
152 {
153     QNullGraphicsPipeline(QRhiImplementation *rhi);
154     ~QNullGraphicsPipeline();
155     void release() override;
156     bool build() override;
157 };
158 
159 struct QNullComputePipeline : public QRhiComputePipeline
160 {
161     QNullComputePipeline(QRhiImplementation *rhi);
162     ~QNullComputePipeline();
163     void release() override;
164     bool build() override;
165 };
166 
167 struct QNullCommandBuffer : public QRhiCommandBuffer
168 {
169     QNullCommandBuffer(QRhiImplementation *rhi);
170     ~QNullCommandBuffer();
171     void release() override;
172 };
173 
174 struct QNullSwapChain : public QRhiSwapChain
175 {
176     QNullSwapChain(QRhiImplementation *rhi);
177     ~QNullSwapChain();
178     void release() override;
179 
180     QRhiCommandBuffer *currentFrameCommandBuffer() override;
181     QRhiRenderTarget *currentFrameRenderTarget() override;
182 
183     QSize surfacePixelSize() override;
184 
185     QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
186     bool buildOrResize() override;
187 
188     QNullReferenceRenderTarget rt;
189     QNullCommandBuffer cb;
190     int frameCount = 0;
191 };
192 
193 class QRhiNull : public QRhiImplementation
194 {
195 public:
196     QRhiNull(QRhiNullInitParams *params);
197 
198     bool create(QRhi::Flags flags) override;
199     void destroy() override;
200 
201     QRhiGraphicsPipeline *createGraphicsPipeline() override;
202     QRhiComputePipeline *createComputePipeline() override;
203     QRhiShaderResourceBindings *createShaderResourceBindings() override;
204     QRhiBuffer *createBuffer(QRhiBuffer::Type type,
205                              QRhiBuffer::UsageFlags usage,
206                              int size) override;
207     QRhiRenderBuffer *createRenderBuffer(QRhiRenderBuffer::Type type,
208                                          const QSize &pixelSize,
209                                          int sampleCount,
210                                          QRhiRenderBuffer::Flags flags) override;
211     QRhiTexture *createTexture(QRhiTexture::Format format,
212                                const QSize &pixelSize,
213                                int sampleCount,
214                                QRhiTexture::Flags flags) override;
215     QRhiSampler *createSampler(QRhiSampler::Filter magFilter,
216                                QRhiSampler::Filter minFilter,
217                                QRhiSampler::Filter mipmapMode,
218                                QRhiSampler:: AddressMode u,
219                                QRhiSampler::AddressMode v,
220                                QRhiSampler::AddressMode w) override;
221 
222     QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
223                                                        QRhiTextureRenderTarget::Flags flags) override;
224 
225     QRhiSwapChain *createSwapChain() override;
226     QRhi::FrameOpResult beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags) override;
227     QRhi::FrameOpResult endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) override;
228     QRhi::FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi::BeginFrameFlags flags) override;
229     QRhi::FrameOpResult endOffscreenFrame(QRhi::EndFrameFlags flags) override;
230     QRhi::FrameOpResult finish() override;
231 
232     void resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
233 
234     void beginPass(QRhiCommandBuffer *cb,
235                    QRhiRenderTarget *rt,
236                    const QColor &colorClearValue,
237                    const QRhiDepthStencilClearValue &depthStencilClearValue,
238                    QRhiResourceUpdateBatch *resourceUpdates) override;
239     void endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
240 
241     void setGraphicsPipeline(QRhiCommandBuffer *cb,
242                              QRhiGraphicsPipeline *ps) override;
243 
244     void setShaderResources(QRhiCommandBuffer *cb,
245                             QRhiShaderResourceBindings *srb,
246                             int dynamicOffsetCount,
247                             const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) override;
248 
249     void setVertexInput(QRhiCommandBuffer *cb,
250                         int startBinding, int bindingCount, const QRhiCommandBuffer::VertexInput *bindings,
251                         QRhiBuffer *indexBuf, quint32 indexOffset,
252                         QRhiCommandBuffer::IndexFormat indexFormat) override;
253 
254     void setViewport(QRhiCommandBuffer *cb, const QRhiViewport &viewport) override;
255     void setScissor(QRhiCommandBuffer *cb, const QRhiScissor &scissor) override;
256     void setBlendConstants(QRhiCommandBuffer *cb, const QColor &c) override;
257     void setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) override;
258 
259     void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
260               quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) override;
261 
262     void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
263                      quint32 instanceCount, quint32 firstIndex,
264                      qint32 vertexOffset, quint32 firstInstance) override;
265 
266     void debugMarkBegin(QRhiCommandBuffer *cb, const QByteArray &name) override;
267     void debugMarkEnd(QRhiCommandBuffer *cb) override;
268     void debugMarkMsg(QRhiCommandBuffer *cb, const QByteArray &msg) override;
269 
270     void beginComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
271     void endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
272     void setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) override;
273     void dispatch(QRhiCommandBuffer *cb, int x, int y, int z) override;
274 
275     const QRhiNativeHandles *nativeHandles(QRhiCommandBuffer *cb) override;
276     void beginExternal(QRhiCommandBuffer *cb) override;
277     void endExternal(QRhiCommandBuffer *cb) override;
278 
279     QVector<int> supportedSampleCounts() const override;
280     int ubufAlignment() const override;
281     bool isYUpInFramebuffer() const override;
282     bool isYUpInNDC() const override;
283     bool isClipDepthZeroToOne() const override;
284     QMatrix4x4 clipSpaceCorrMatrix() const override;
285     bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
286     bool isFeatureSupported(QRhi::Feature feature) const override;
287     int resourceLimit(QRhi::ResourceLimit limit) const override;
288     const QRhiNativeHandles *nativeHandles() override;
289     void sendVMemStatsToProfiler() override;
290     bool makeThreadLocalNativeContextCurrent() override;
291     void releaseCachedResources() override;
292     bool isDeviceLost() const override;
293 
294     void simulateTextureUpload(const QRhiResourceUpdateBatchPrivate::TextureOp &u);
295     void simulateTextureCopy(const QRhiResourceUpdateBatchPrivate::TextureOp &u);
296     void simulateTextureGenMips(const QRhiResourceUpdateBatchPrivate::TextureOp &u);
297 
298     QRhiNullNativeHandles nativeHandlesStruct;
299     QRhiSwapChain *currentSwapChain = nullptr;
300     QNullCommandBuffer offscreenCommandBuffer;
301 };
302 
303 QT_END_NAMESPACE
304 
305 #endif
306