| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#define EXAMPLEFW_IMGUI |
| 5 |
#include "../shared/examplefw.h" |
| 6 |
|
| 7 |
#include "../shared/cube.h" |
| 8 |
|
| 9 |
struct { |
| 10 |
QMatrix4x4 winProj; |
| 11 |
QList<QRhiResource *> releasePool; |
| 12 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 13 |
QRhiBuffer *vbuf = nullptr; |
| 14 |
QRhiBuffer *ubuf = nullptr; |
| 15 |
QRhiTexture *tex = nullptr; |
| 16 |
QRhiSampler *sampler = nullptr; |
| 17 |
QRhiShaderResourceBindings *srb = nullptr; |
| 18 |
QRhiGraphicsPipeline *ps = nullptr; |
| 19 |
bool showDemoWindow = true; |
| 20 |
float rotation = 0.0f; |
| 21 |
} d; |
| 22 |
|
| 23 |
void Window::customInit() |
| 24 |
{ |
| 25 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 26 |
|
| 27 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 28 |
d.vbuf->create(); |
| 29 |
d.releasePool << d.vbuf; |
| 30 |
|
| 31 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); |
| 32 |
|
| 33 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 34 |
d.ubuf->create(); |
| 35 |
d.releasePool << d.ubuf; |
| 36 |
float opacity = 1.0f; |
| 37 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &opacity); |
| 38 |
|
| 39 |
QImage image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888).flipped(); |
| 40 |
d.tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(image.width(), image.height()), 1, {}); |
| 41 |
d.releasePool << d.tex; |
| 42 |
d.tex->create(); |
| 43 |
d.initialUpdates->uploadTexture(d.tex, image); |
| 44 |
|
| 45 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 46 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 47 |
d.releasePool << d.sampler; |
| 48 |
d.sampler->create(); |
| 49 |
|
| 50 |
d.srb = m_r->newShaderResourceBindings(); |
| 51 |
d.releasePool << d.srb; |
| 52 |
d.srb->setBindings({ |
| 53 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 54 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler) |
| 55 |
}); |
| 56 |
d.srb->create(); |
| 57 |
|
| 58 |
d.ps = m_r->newGraphicsPipeline(); |
| 59 |
d.releasePool << d.ps; |
| 60 |
d.ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 61 |
const QRhiShaderStage stages[] = { |
| 62 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 63 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 64 |
}; |
| 65 |
d.ps->setShaderStages(stages, stages + 2); |
| 66 |
QRhiVertexInputLayout inputLayout; |
| 67 |
inputLayout.setBindings({ |
| 68 |
{ 3 * sizeof(float) }, |
| 69 |
{ 2 * sizeof(float) } |
| 70 |
}); |
| 71 |
inputLayout.setAttributes({ |
| 72 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 73 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 } |
| 74 |
}); |
| 75 |
d.ps->setVertexInputLayout(inputLayout); |
| 76 |
d.ps->setShaderResourceBindings(d.srb); |
| 77 |
d.ps->setRenderPassDescriptor(m_rp); |
| 78 |
d.ps->create(); |
| 79 |
} |
| 80 |
|
| 81 |
void Window::customRelease() |
| 82 |
{ |
| 83 |
qDeleteAll(d.releasePool); |
| 84 |
d.releasePool.clear(); |
| 85 |
} |
| 86 |
|
| 87 |
void Window::customRender() |
| 88 |
{ |
| 89 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 90 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 91 |
if (d.initialUpdates) { |
| 92 |
u->merge(d.initialUpdates); |
| 93 |
d.initialUpdates->release(); |
| 94 |
d.initialUpdates = nullptr; |
| 95 |
} |
| 96 |
|
| 97 |
QMatrix4x4 mvp = m_proj; |
| 98 |
mvp.rotate(d.rotation, 0, 1, 0); |
| 99 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 100 |
|
| 101 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 102 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 103 |
|
| 104 |
cb->setGraphicsPipeline(d.ps); |
| 105 |
cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); |
| 106 |
cb->setShaderResources(); |
| 107 |
const QRhiCommandBuffer::VertexInput vbufBindings[] = { |
| 108 |
{ d.vbuf, 0 }, |
| 109 |
{ d.vbuf, quint32(36 * 3 * sizeof(float)) } |
| 110 |
}; |
| 111 |
cb->setVertexInput(0, 2, vbufBindings); |
| 112 |
cb->draw(36); |
| 113 |
|
| 114 |
m_imguiRenderer->render(); |
| 115 |
|
| 116 |
cb->endPass(); |
| 117 |
} |
| 118 |
|
| 119 |
void Window::customGui() |
| 120 |
{ |
| 121 |
ImGui::ShowDemoWindow(&d.showDemoWindow); |
| 122 |
|
| 123 |
ImGui::SetNextWindowPos(ImVec2(50, 120), ImGuiCond_FirstUseEver); |
| 124 |
ImGui::SetNextWindowSize(ImVec2(400, 100), ImGuiCond_FirstUseEver); |
| 125 |
ImGui::Begin("Test"); |
| 126 |
ImGui::SliderFloat("Rotation", &d.rotation, 0.0f, 360.0f); |
| 127 |
ImGui::End(); |
| 128 |
} |