aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/scenegraph/rendernode/metalrenderer.mm
blob: d78a54b88a520fa40921560b49c183d15d3249f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "metalrenderer.h"
#include <QQuickItem>
#include <QQuickWindow>
#include <QFile>

#include <Metal/Metal.h>

using FuncAndLib = QPair<id<MTLFunction>, id<MTLLibrary> >;

const int MAX_FRAMES_IN_FLIGHT = 3;

struct {
    id<MTLDevice> dev = nil;
    QByteArray vsSource;
    FuncAndLib vs;
    QByteArray fsSource;
    FuncAndLib fs;
    id<MTLBuffer> vbuf[MAX_FRAMES_IN_FLIGHT];
    id<MTLBuffer> ubuf[MAX_FRAMES_IN_FLIGHT];
    id<MTLDepthStencilState> stencilEnabledDsState = nil;
    id<MTLRenderPipelineState> pipeline = nil;
} g;

static FuncAndLib compileShaderFromSource(const QByteArray &src, const QByteArray &entryPoint)
{
    FuncAndLib fl;

    NSString *srcstr = [NSString stringWithUTF8String: src.constData()];
    MTLCompileOptions *opts = [[MTLCompileOptions alloc] init];
    opts.languageVersion = MTLLanguageVersion1_2;
    NSError *err = nil;
    fl.second = [g.dev newLibraryWithSource: srcstr options: opts error: &err];
    [opts release];
    // srcstr is autoreleased

    if (err) {
        const QString msg = QString::fromNSString(err.localizedDescription);
        qFatal("%s", qPrintable(msg));
        return fl;
    }

    NSString *name = [NSString stringWithUTF8String: entryPoint.constData()];
    fl.first = [fl.second newFunctionWithName: name];
    [name release];

    return fl;
}

const int VERTEX_SIZE = 6 * sizeof(float);

static float colors[] = {
    1.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f
};

void MetalRenderNodeResourceBuilder::build()
{
    if (!g.dev) {
        QSGRendererInterface *rif = m_window->rendererInterface();
        Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::MetalRhi);

        g.dev = (id<MTLDevice>) rif->getResource(m_window, QSGRendererInterface::DeviceResource);
        Q_ASSERT(g.dev);
    }

    if (g.vsSource.isEmpty()) {
        const QString filename = QLatin1String(":/scenegraph/rendernode/metalshader.vert");
        QFile f(filename);
        if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
            qFatal("Failed to read shader %s", qPrintable(filename));
        g.vsSource = f.readAll();
        g.vs = compileShaderFromSource(g.vsSource, QByteArrayLiteral("main0"));
    }

    if (g.fsSource.isEmpty()) {
        const QString filename = QLatin1String(":/scenegraph/rendernode/metalshader.frag");
        QFile f(filename);
        if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
            qFatal("Failed to read shader %s", qPrintable(filename));
        g.fsSource = f.readAll();
        g.fs = compileShaderFromSource(g.fsSource, QByteArrayLiteral("main0"));
    }

    const int framesInFlight = m_window->graphicsStateInfo().framesInFlight;

    // For simplicity's sake we use shared mode (something like host visible +
    // host coherent) for everything.

    for (int i = 0; i < framesInFlight; ++i) {
        // Have multiple versions for vertex too since we'll just memcpy new
        // vertices based on item width and height on every render(). This could
        // be optimized further however.
        if (!g.vbuf[i]) {
            g.vbuf[i] = [g.dev newBufferWithLength: VERTEX_SIZE + sizeof(colors) options: MTLResourceStorageModeShared];
            char *p = (char *) [g.vbuf[i] contents];
            memcpy(p + VERTEX_SIZE, colors, sizeof(colors));
        }

        if (!g.ubuf[i])
            g.ubuf[i] = [g.dev newBufferWithLength: 256 options: MTLResourceStorageModeShared];
    }

    if (!g.stencilEnabledDsState) {
        MTLDepthStencilDescriptor *dsDesc = [[MTLDepthStencilDescriptor alloc] init];
        dsDesc.frontFaceStencil = [[MTLStencilDescriptor alloc] init];
        dsDesc.frontFaceStencil.stencilFailureOperation = MTLStencilOperationKeep;
        dsDesc.frontFaceStencil.depthFailureOperation = MTLStencilOperationKeep;
        dsDesc.frontFaceStencil.depthStencilPassOperation = MTLStencilOperationKeep;
        dsDesc.frontFaceStencil.stencilCompareFunction = MTLCompareFunctionEqual;
        dsDesc.frontFaceStencil.readMask = 0xFF;
        dsDesc.frontFaceStencil.writeMask = 0xFF;

        dsDesc.backFaceStencil = [[MTLStencilDescriptor alloc] init];
        dsDesc.backFaceStencil.stencilFailureOperation = MTLStencilOperationKeep;
        dsDesc.backFaceStencil.depthFailureOperation = MTLStencilOperationKeep;
        dsDesc.backFaceStencil.depthStencilPassOperation = MTLStencilOperationKeep;
        dsDesc.backFaceStencil.stencilCompareFunction = MTLCompareFunctionEqual;
        dsDesc.backFaceStencil.readMask = 0xFF;
        dsDesc.backFaceStencil.writeMask = 0xFF;

        g.stencilEnabledDsState = [g.dev newDepthStencilStateWithDescriptor: dsDesc];
        [dsDesc release];
    }

    if (!g.pipeline) {
        MTLVertexDescriptor *inputLayout = [MTLVertexDescriptor vertexDescriptor];
        inputLayout.attributes[0].format = MTLVertexFormatFloat2;
        inputLayout.attributes[0].offset = 0;
        inputLayout.attributes[0].bufferIndex = 1; // ubuf is 0, vbuf is 1 and 2
        inputLayout.attributes[1].format = MTLVertexFormatFloat3;
        inputLayout.attributes[1].offset = 0;
        inputLayout.attributes[1].bufferIndex = 2;
        inputLayout.layouts[1].stride = 2 * sizeof(float);
        inputLayout.layouts[2].stride = 3 * sizeof(float);

        MTLRenderPipelineDescriptor *rpDesc = [[MTLRenderPipelineDescriptor alloc] init];
        rpDesc.vertexDescriptor = inputLayout;

        rpDesc.vertexFunction = g.vs.first;
        rpDesc.fragmentFunction = g.fs.first;

        rpDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
        rpDesc.colorAttachments[0].blendingEnabled = true;
        rpDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorOne;
        rpDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
        rpDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
        rpDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;

        if (g.dev.depth24Stencil8PixelFormatSupported) {
            rpDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth24Unorm_Stencil8;
            rpDesc.stencilAttachmentPixelFormat = MTLPixelFormatDepth24Unorm_Stencil8;
        } else {
            rpDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
            rpDesc.stencilAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
        }

        NSError *err = nil;
        g.pipeline = [g.dev newRenderPipelineStateWithDescriptor: rpDesc error: &err];
        if (!g.pipeline) {
            const QString msg = QString::fromNSString(err.localizedDescription);
            qFatal("Failed to create render pipeline state: %s", qPrintable(msg));
        }
        [rpDesc release];
    }
}

MetalRenderNode::MetalRenderNode()
{
    g.vs.first = g.fs.first = nil;
    g.vs.second = g.fs.second = nil;

    for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
        g.vbuf[i] = nil;
        g.ubuf[i] = nil;
    }
}

MetalRenderNode::~MetalRenderNode()
{
    releaseResources();
}

void MetalRenderNode::releaseResources()
{
    [g.stencilEnabledDsState release];
    g.stencilEnabledDsState = nil;

    [g.pipeline release];
    g.pipeline = nil;

    [g.vs.first release];
    [g.vs.second release];

    [g.fs.first release];
    [g.fs.second release];

    g.vs.first = g.fs.first = nil;
    g.vs.second = g.fs.second = nil;

    for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
        [g.vbuf[i] release];
        g.vbuf[i] = nil;
        [g.ubuf[i] release];
        g.ubuf[i] = nil;
    }
}

void MetalRenderNode::render(const RenderState *state)
{
    Q_ASSERT(m_window);
    const QQuickWindow::GraphicsStateInfo &stateInfo(m_window->graphicsStateInfo());
    id<MTLBuffer> vbuf = g.vbuf[stateInfo.currentFrameSlot];
    id<MTLBuffer> ubuf = g.ubuf[stateInfo.currentFrameSlot];

    QPointF p0(m_width - 1, m_height - 1);
    QPointF p1(0, 0);
    QPointF p2(0, m_height - 1);

    float vertices[6] = { float(p0.x()), float(p0.y()),
                          float(p1.x()), float(p1.y()),
                          float(p2.x()), float(p2.y()) };
    char *p = (char *) [vbuf contents];
    memcpy(p, vertices, VERTEX_SIZE);

    const QMatrix4x4 mvp = *state->projectionMatrix() * *matrix();
    const float opacity = inheritedOpacity();

    p = (char *) [ubuf contents];
    memcpy(p, mvp.constData(), 64);
    memcpy(p + 64, &opacity, 4);

    QSGRendererInterface *rif = m_window->rendererInterface();
    id<MTLRenderCommandEncoder> encoder = (id<MTLRenderCommandEncoder>) rif->getResource(
                m_window, QSGRendererInterface::CommandEncoderResource);
    Q_ASSERT(encoder);

    [encoder setVertexBuffer: vbuf offset: 0 atIndex: 1];
    [encoder setVertexBuffer: vbuf offset: VERTEX_SIZE atIndex: 2];

    [encoder setVertexBuffer: ubuf offset: 0 atIndex: 0];
    [encoder setFragmentBuffer: ubuf offset: 0 atIndex: 0];

    // Clip support.
    if (state->scissorEnabled()) {
        const QRect r = state->scissorRect(); // bottom-up
        MTLScissorRect s;
        s.x = r.x();
        s.y = m_outputHeight - (r.y() + r.height());
        s.width = r.width();
        s.height = r.height();
        [encoder setScissorRect: s];
    }
    if (state->stencilEnabled()) {
        [encoder setDepthStencilState: g.stencilEnabledDsState];
        [encoder setStencilReferenceValue: state->stencilValue()];
    }

    [encoder setRenderPipelineState: g.pipeline];
    [encoder drawPrimitives: MTLPrimitiveTypeTriangle vertexStart: 0 vertexCount: 3 instanceCount: 1 baseInstance: 0];
}

QSGRenderNode::StateFlags MetalRenderNode::changedStates() const
{
    return BlendState | ScissorState | StencilState;
}

QSGRenderNode::RenderingFlags MetalRenderNode::flags() const
{
    return BoundedRectRendering | DepthAwareRendering;
}

QRectF MetalRenderNode::rect() const
{
    return QRect(0, 0, m_width, m_height);
}

void MetalRenderNode::sync(QQuickItem *item)
{
    m_window = item->window();
    m_width = item->width();
    m_height = item->height();
    m_outputHeight = m_window->height() * m_window->effectiveDevicePixelRatio();
}