| 1 |
// Copyright (C) 2019 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#define EXAMPLEFW_PREINIT |
| 5 |
#include "../shared/examplefw.h" |
| 6 |
#include "../shared/cube.h" |
| 7 |
#include <QRandomGenerator> |
| 8 |
|
| 9 |
// A variation on the instancing test. No instancing here, just |
| 10 |
// individual uniform buffer updates and draw calls. |
| 11 |
|
| 12 |
const int INSTANCE_COUNT = 25000; |
| 13 |
|
| 14 |
struct { |
| 15 |
QList<QRhiResource *> releasePool; |
| 16 |
|
| 17 |
QRhiBuffer *vbuf; |
| 18 |
QRhiBuffer *ubuf[INSTANCE_COUNT]; |
| 19 |
QRhiShaderResourceBindings *srb[INSTANCE_COUNT]; |
| 20 |
QRhiGraphicsPipeline *ps; |
| 21 |
|
| 22 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 23 |
|
| 24 |
struct { |
| 25 |
float x, y, z; |
| 26 |
float r, g, b; |
| 27 |
} instData[INSTANCE_COUNT]; |
| 28 |
float rot = 0.0f; |
| 29 |
} d; |
| 30 |
|
| 31 |
void preInit() |
| 32 |
{ |
| 33 |
debugLayer = false; |
| 34 |
} |
| 35 |
|
| 36 |
void Window::customInit() |
| 37 |
{ |
| 38 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 39 |
|
| 40 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 41 |
d.vbuf->create(); |
| 42 |
d.releasePool << d.vbuf; |
| 43 |
|
| 44 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); |
| 45 |
|
| 46 |
for (int i = 0; i < INSTANCE_COUNT; ++i) { |
| 47 |
d.ubuf[i] = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 92); |
| 48 |
d.ubuf[i]->create(); |
| 49 |
d.releasePool << d.ubuf[i]; |
| 50 |
|
| 51 |
d.srb[i] = m_r->newShaderResourceBindings(); |
| 52 |
d.releasePool << d.srb[i]; |
| 53 |
d.srb[i]->setBindings({ |
| 54 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, d.ubuf[i]) |
| 55 |
}); |
| 56 |
d.srb[i]->create(); |
| 57 |
} |
| 58 |
|
| 59 |
d.ps = m_r->newGraphicsPipeline(); |
| 60 |
d.releasePool << d.ps; |
| 61 |
d.ps->setShaderStages({ |
| 62 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/material.vert.qsb")) }, |
| 63 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/material.frag.qsb")) } |
| 64 |
}); |
| 65 |
QRhiVertexInputLayout inputLayout; |
| 66 |
inputLayout.setBindings({ |
| 67 |
{ 3 * sizeof(float) }, |
| 68 |
}); |
| 69 |
inputLayout.setAttributes({ |
| 70 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 71 |
}); |
| 72 |
d.ps->setVertexInputLayout(inputLayout); |
| 73 |
d.ps->setShaderResourceBindings(d.srb[0]); |
| 74 |
d.ps->setRenderPassDescriptor(m_rp); |
| 75 |
d.ps->create(); |
| 76 |
|
| 77 |
QRandomGenerator *rgen = QRandomGenerator::global(); |
| 78 |
for (int i = 0; i < INSTANCE_COUNT; ++i) { |
| 79 |
d.instData[i].x = rgen->bounded(8000) / 100.0f - 40.0f; |
| 80 |
d.instData[i].y = rgen->bounded(8000) / 100.0f - 40.0f; |
| 81 |
d.instData[i].z = rgen->bounded(100) / -4.0f; |
| 82 |
d.instData[i].r = i / float(INSTANCE_COUNT); |
| 83 |
d.instData[i].g = 0.0f; |
| 84 |
d.instData[i].b = 0.0f; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void Window::customRelease() |
| 89 |
{ |
| 90 |
qDeleteAll(d.releasePool); |
| 91 |
d.releasePool.clear(); |
| 92 |
} |
| 93 |
|
| 94 |
void Window::customRender() |
| 95 |
{ |
| 96 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 97 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 98 |
QRhiResourceUpdateBatch *u = nullptr; |
| 99 |
if (d.initialUpdates) { |
| 100 |
u = d.initialUpdates; |
| 101 |
d.initialUpdates = nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
for (int i = 0; i < INSTANCE_COUNT; ++i) { |
| 105 |
char *p = d.ubuf[i]->beginFullDynamicBufferUpdateForCurrentFrame(); |
| 106 |
QMatrix4x4 mvp = m_proj; |
| 107 |
mvp.rotate(d.rot, 0, 1, 0); |
| 108 |
mvp.scale(0.05f); |
| 109 |
memcpy(p, mvp.constData(), 64); |
| 110 |
|
| 111 |
// float *v = reinterpret_cast<float *>(p + 64); |
| 112 |
// v[0] = d.instData[i].x; |
| 113 |
// v[1] = d.instData[i].y; |
| 114 |
// v[2] = d.instData[i].z; |
| 115 |
memcpy(p + 64, &d.instData[i].x, 4); |
| 116 |
memcpy(p + 68, &d.instData[i].y, 4); |
| 117 |
memcpy(p + 72, &d.instData[i].z, 4); |
| 118 |
|
| 119 |
// v = reinterpret_cast<float *>(p + 80); |
| 120 |
// v[0] = d.instData[i].r; |
| 121 |
// v[1] = d.instData[i].g; |
| 122 |
// v[2] = d.instData[i].b; |
| 123 |
memcpy(p + 80, &d.instData[i].r, 4); |
| 124 |
memcpy(p + 84, &d.instData[i].g, 4); |
| 125 |
memcpy(p + 88, &d.instData[i].b, 4); |
| 126 |
|
| 127 |
d.ubuf[i]->endFullDynamicBufferUpdateForCurrentFrame(); |
| 128 |
} |
| 129 |
|
| 130 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u, QRhiCommandBuffer::DoNotTrackResourcesForCompute); |
| 131 |
for (int i = 0; i < INSTANCE_COUNT; ++i) { |
| 132 |
cb->setGraphicsPipeline(d.ps); |
| 133 |
if (i == 0) |
| 134 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 135 |
cb->setShaderResources(d.srb[i]); |
| 136 |
const QRhiCommandBuffer::VertexInput vbufBinding[] = { |
| 137 |
{ d.vbuf, 0 }, |
| 138 |
}; |
| 139 |
cb->setVertexInput(0, 1, vbufBinding); |
| 140 |
cb->draw(36); |
| 141 |
} |
| 142 |
cb->endPass(); |
| 143 |
|
| 144 |
d.rot += 0.1f; |
| 145 |
} |