| 1 |
// Copyright (C) 2021 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
|
| 6 |
static const float geom[] = { |
| 7 |
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 8 |
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, |
| 9 |
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 10 |
0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, |
| 11 |
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, |
| 12 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, |
| 13 |
|
| 14 |
}; |
| 15 |
|
| 16 |
struct { |
| 17 |
QVector<QRhiResource *> releasePool; |
| 18 |
QRhiBuffer *vbuf = nullptr; |
| 19 |
QRhiShaderResourceBindings *srb = nullptr; |
| 20 |
QRhiGraphicsPipeline *ps = nullptr; |
| 21 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 22 |
int count = 0; |
| 23 |
} d; |
| 24 |
|
| 25 |
void Window::customInit() |
| 26 |
{ |
| 27 |
m_clearColor = QColor("black"); |
| 28 |
|
| 29 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(geom)); |
| 30 |
d.vbuf->create(); |
| 31 |
d.releasePool << d.vbuf; |
| 32 |
|
| 33 |
d.srb = m_r->newShaderResourceBindings(); |
| 34 |
d.releasePool << d.srb; |
| 35 |
d.srb->create(); |
| 36 |
|
| 37 |
d.ps = m_r->newGraphicsPipeline(); |
| 38 |
d.releasePool << d.ps; |
| 39 |
|
| 40 |
d.ps->setTopology(QRhiGraphicsPipeline::TriangleStrip); |
| 41 |
|
| 42 |
d.ps->setShaderStages({ |
| 43 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/test.vert.qsb")) }, |
| 44 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/test.frag.qsb")) } |
| 45 |
}); |
| 46 |
|
| 47 |
d.ps->setCullMode(QRhiGraphicsPipeline::None); |
| 48 |
d.ps->setPolygonMode(QRhiGraphicsPipeline::Line); |
| 49 |
d.ps->setDepthTest(true); |
| 50 |
d.ps->setDepthWrite(true); |
| 51 |
QRhiVertexInputLayout inputLayout; |
| 52 |
inputLayout.setBindings({ |
| 53 |
{ 6 * sizeof(float) } |
| 54 |
}); |
| 55 |
inputLayout.setAttributes({ |
| 56 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 57 |
{ 0, 1, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) } |
| 58 |
}); |
| 59 |
d.ps->setVertexInputLayout(inputLayout); |
| 60 |
d.ps->setShaderResourceBindings(d.srb); |
| 61 |
d.ps->setRenderPassDescriptor(m_rp); |
| 62 |
d.ps->create(); |
| 63 |
|
| 64 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 65 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, geom); |
| 66 |
} |
| 67 |
|
| 68 |
void Window::customRelease() |
| 69 |
{ |
| 70 |
qDeleteAll(d.releasePool); |
| 71 |
d.releasePool.clear(); |
| 72 |
} |
| 73 |
|
| 74 |
void Window::customRender() |
| 75 |
{ |
| 76 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 77 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 78 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 79 |
if (d.initialUpdates) { |
| 80 |
u->merge(d.initialUpdates); |
| 81 |
d.initialUpdates->release(); |
| 82 |
d.initialUpdates = nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
QRhiGraphicsPipeline::PolygonMode polygonMode = |
| 86 |
(++d.count / 60) % 2 ? QRhiGraphicsPipeline::Fill : QRhiGraphicsPipeline::Line; |
| 87 |
|
| 88 |
if (d.ps->polygonMode() != polygonMode) { |
| 89 |
d.ps->setPolygonMode(polygonMode); |
| 90 |
d.ps->create(); |
| 91 |
} |
| 92 |
|
| 93 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 94 |
cb->setGraphicsPipeline(d.ps); |
| 95 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 96 |
cb->setShaderResources(); |
| 97 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 98 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 99 |
cb->draw(6); |
| 100 |
cb->endPass(); |
| 101 |
} |