| 1 |
// Copyright (C) 2022 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 points[] = { 0.0f, 0.0f, 0.0f }; |
| 7 |
|
| 8 |
struct |
| 9 |
{ |
| 10 |
QVector<QRhiResource *> releasePool; |
| 11 |
QRhiBuffer *vbuf = nullptr; |
| 12 |
QRhiBuffer *ubuf = nullptr; |
| 13 |
QRhiShaderResourceBindings *srb = nullptr; |
| 14 |
QRhiGraphicsPipeline *ps = nullptr; |
| 15 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 16 |
float radius = 0.0f; |
| 17 |
} d; |
| 18 |
|
| 19 |
void Window::customInit() |
| 20 |
{ |
| 21 |
if (!m_r->isFeatureSupported(QRhi::GeometryShader)) |
| 22 |
qFatal("Geometry shaders are not supported"); |
| 23 |
|
| 24 |
m_clearColor.setRgb(0, 0, 0); |
| 25 |
|
| 26 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(points)); |
| 27 |
d.vbuf->create(); |
| 28 |
d.releasePool << d.vbuf; |
| 29 |
|
| 30 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 4); |
| 31 |
d.ubuf->create(); |
| 32 |
d.releasePool << d.ubuf; |
| 33 |
|
| 34 |
d.srb = m_r->newShaderResourceBindings(); |
| 35 |
d.releasePool << d.srb; |
| 36 |
const QRhiShaderResourceBinding::StageFlags geom = QRhiShaderResourceBinding::GeometryStage; |
| 37 |
d.srb->setBindings({ QRhiShaderResourceBinding::uniformBuffer(0, geom, d.ubuf) }); |
| 38 |
d.srb->create(); |
| 39 |
|
| 40 |
d.ps = m_r->newGraphicsPipeline(); |
| 41 |
d.releasePool << d.ps; |
| 42 |
|
| 43 |
d.ps->setTopology(QRhiGraphicsPipeline::Points); |
| 44 |
|
| 45 |
d.ps->setShaderStages( |
| 46 |
{ { QRhiShaderStage::Vertex, getShader(QLatin1String(":/test.vert.qsb")) }, |
| 47 |
{ QRhiShaderStage::Geometry, getShader(QLatin1String(":/test.geom.qsb")) }, |
| 48 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/test.frag.qsb")) } }); |
| 49 |
|
| 50 |
d.ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 51 |
d.ps->setDepthTest(true); |
| 52 |
d.ps->setDepthWrite(true); |
| 53 |
QRhiVertexInputLayout inputLayout; |
| 54 |
inputLayout.setBindings({ { 3 * sizeof(float) } }); |
| 55 |
inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } }); |
| 56 |
d.ps->setVertexInputLayout(inputLayout); |
| 57 |
d.ps->setShaderResourceBindings(d.srb); |
| 58 |
d.ps->setRenderPassDescriptor(m_rp); |
| 59 |
d.ps->create(); |
| 60 |
|
| 61 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 62 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, points); |
| 63 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 0, 4, &d.radius); |
| 64 |
} |
| 65 |
|
| 66 |
void Window::customRelease() |
| 67 |
{ |
| 68 |
qDeleteAll(d.releasePool); |
| 69 |
d.releasePool.clear(); |
| 70 |
} |
| 71 |
|
| 72 |
void Window::customRender() |
| 73 |
{ |
| 74 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 75 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 76 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 77 |
if (d.initialUpdates) { |
| 78 |
u->merge(d.initialUpdates); |
| 79 |
d.initialUpdates->release(); |
| 80 |
d.initialUpdates = nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
u->updateDynamicBuffer(d.ubuf, 0, 4, &d.radius); |
| 84 |
d.radius = std::fmod(d.radius + 0.01f, 1.0f); |
| 85 |
|
| 86 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 87 |
cb->setGraphicsPipeline(d.ps); |
| 88 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 89 |
cb->setShaderResources(); |
| 90 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 91 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 92 |
cb->draw(1); |
| 93 |
cb->endPass(); |
| 94 |
} |