| 1 |
// Copyright (C) 2019 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
#include "../shared/cube.h" |
| 6 |
#include <QRandomGenerator> |
| 7 |
|
| 8 |
// Instanced draw example. When running with OpenGL, at least 3.3 or ES 3.0 is |
| 9 |
// needed. |
| 10 |
|
| 11 |
const int INSTANCE_COUNT = 1024; |
| 12 |
|
| 13 |
struct { |
| 14 |
QList<QRhiResource *> releasePool; |
| 15 |
|
| 16 |
QRhiBuffer *vbuf = nullptr; |
| 17 |
QRhiBuffer *instBuf = nullptr; |
| 18 |
QRhiBuffer *ubuf = nullptr; |
| 19 |
QRhiShaderResourceBindings *srb = nullptr; |
| 20 |
QRhiGraphicsPipeline *ps = nullptr; |
| 21 |
|
| 22 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 23 |
QMatrix4x4 winProj; |
| 24 |
} d; |
| 25 |
|
| 26 |
void Window::customInit() |
| 27 |
{ |
| 28 |
if (!m_r->isFeatureSupported(QRhi::Instancing)) |
| 29 |
qFatal("Instanced drawing is not supported"); |
| 30 |
|
| 31 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 32 |
|
| 33 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 34 |
d.vbuf->create(); |
| 35 |
d.releasePool << d.vbuf; |
| 36 |
|
| 37 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); |
| 38 |
|
| 39 |
// transform + color (mat4 + vec3), interleaved, for each instance |
| 40 |
d.instBuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, INSTANCE_COUNT * 19 * sizeof(float)); |
| 41 |
d.instBuf->create(); |
| 42 |
d.releasePool << d.instBuf; |
| 43 |
|
| 44 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 12); |
| 45 |
d.ubuf->create(); |
| 46 |
d.releasePool << d.ubuf; |
| 47 |
|
| 48 |
d.srb = m_r->newShaderResourceBindings(); |
| 49 |
d.releasePool << d.srb; |
| 50 |
d.srb->setBindings({ |
| 51 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, d.ubuf) |
| 52 |
}); |
| 53 |
d.srb->create(); |
| 54 |
|
| 55 |
d.ps = m_r->newGraphicsPipeline(); |
| 56 |
d.releasePool << d.ps; |
| 57 |
d.ps->setShaderStages({ |
| 58 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/inst.vert.qsb")) }, |
| 59 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/inst.frag.qsb")) } |
| 60 |
}); |
| 61 |
QRhiVertexInputLayout inputLayout; |
| 62 |
inputLayout.setBindings({ |
| 63 |
{ 3 * sizeof(float) }, // cube vertices |
| 64 |
{ 19 * sizeof(float), QRhiVertexInputBinding::PerInstance }, // per-instance transform and color |
| 65 |
}); |
| 66 |
inputLayout.setAttributes({ |
| 67 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, // position |
| 68 |
{ 1, 1, QRhiVertexInputAttribute::Float4, 0, 0 }, // instMat |
| 69 |
{ 1, 2, QRhiVertexInputAttribute::Float4, 4 * sizeof(float), 1 }, |
| 70 |
{ 1, 3, QRhiVertexInputAttribute::Float4, 8 * sizeof(float), 2 }, |
| 71 |
{ 1, 4, QRhiVertexInputAttribute::Float4, 12 * sizeof(float), 3 }, |
| 72 |
{ 1, 5, QRhiVertexInputAttribute::Float3, 16 * sizeof(float) }, // instColor |
| 73 |
}); |
| 74 |
d.ps->setVertexInputLayout(inputLayout); |
| 75 |
d.ps->setShaderResourceBindings(d.srb); |
| 76 |
d.ps->setRenderPassDescriptor(m_rp); |
| 77 |
d.ps->create(); |
| 78 |
|
| 79 |
QByteArray instData; |
| 80 |
instData.resize(INSTANCE_COUNT * 19 * sizeof(float)); |
| 81 |
float *p = reinterpret_cast<float *>(instData.data()); |
| 82 |
QRandomGenerator *rgen = QRandomGenerator::global(); |
| 83 |
for (int i = 0; i < INSTANCE_COUNT; ++i) { |
| 84 |
QMatrix4x4 m; |
| 85 |
m.translate(rgen->bounded(8000) / 100.0f - 40.0f, |
| 86 |
rgen->bounded(8000) / 100.0f - 40.0f, |
| 87 |
0.0f); |
| 88 |
memcpy(p, m.constData(), 16 * sizeof(float)); |
| 89 |
p += 16; |
| 90 |
// color |
| 91 |
*p++ = i / float(INSTANCE_COUNT); |
| 92 |
*p++ = 0.0f; |
| 93 |
*p++ = 0.0f; |
| 94 |
} |
| 95 |
d.initialUpdates->uploadStaticBuffer(d.instBuf, instData.constData()); |
| 96 |
} |
| 97 |
|
| 98 |
void Window::customRelease() |
| 99 |
{ |
| 100 |
qDeleteAll(d.releasePool); |
| 101 |
d.releasePool.clear(); |
| 102 |
} |
| 103 |
|
| 104 |
void Window::customRender() |
| 105 |
{ |
| 106 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 107 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 108 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 109 |
if (d.initialUpdates) { |
| 110 |
u->merge(d.initialUpdates); |
| 111 |
d.initialUpdates->release(); |
| 112 |
d.initialUpdates = nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
if (d.winProj != m_proj) { |
| 116 |
d.winProj = m_proj; |
| 117 |
QMatrix4x4 mvp = m_proj; |
| 118 |
mvp.scale(0.05f); |
| 119 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 120 |
} |
| 121 |
|
| 122 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 123 |
cb->setGraphicsPipeline(d.ps); |
| 124 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 125 |
cb->setShaderResources(); |
| 126 |
const QRhiCommandBuffer::VertexInput vbufBinding[] = { |
| 127 |
{ d.vbuf, 0 }, |
| 128 |
{ d.instBuf, 0 } |
| 129 |
}; |
| 130 |
cb->setVertexInput(0, 2, vbufBinding); |
| 131 |
cb->draw(36, INSTANCE_COUNT); |
| 132 |
cb->endPass(); |
| 133 |
} |