| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "examplewidget.h" |
| 5 |
#include <QFile> |
| 6 |
|
| 7 |
static float vertexData[] = { |
| 8 |
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, |
| 9 |
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, |
| 10 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, |
| 11 |
}; |
| 12 |
|
| 13 |
//![get-shader] |
| 14 |
static QShader getShader(const QString &name) |
| 15 |
{ |
| 16 |
QFile f(name); |
| 17 |
return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader(); |
| 18 |
} |
| 19 |
//![get-shader] |
| 20 |
|
| 21 |
//![init-1] |
| 22 |
void ExampleRhiWidget::initialize(QRhiCommandBuffer *cb) |
| 23 |
{ |
| 24 |
if (m_rhi != rhi()) { |
| 25 |
m_pipeline.reset(); |
| 26 |
m_rhi = rhi(); |
| 27 |
} |
| 28 |
//![init-1] |
| 29 |
//![init-pipeline] |
| 30 |
if (!m_pipeline) { |
| 31 |
m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData))); |
| 32 |
m_vbuf->create(); |
| 33 |
|
| 34 |
m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64)); |
| 35 |
m_ubuf->create(); |
| 36 |
|
| 37 |
m_srb.reset(m_rhi->newShaderResourceBindings()); |
| 38 |
m_srb->setBindings({ |
| 39 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, m_ubuf.get()), |
| 40 |
}); |
| 41 |
m_srb->create(); |
| 42 |
|
| 43 |
m_pipeline.reset(m_rhi->newGraphicsPipeline()); |
| 44 |
m_pipeline->setShaderStages({ |
| 45 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/shader_assets/color.vert.qsb")) }, |
| 46 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/shader_assets/color.frag.qsb")) } |
| 47 |
}); |
| 48 |
QRhiVertexInputLayout inputLayout; |
| 49 |
inputLayout.setBindings({ |
| 50 |
{ 5 * sizeof(float) } |
| 51 |
}); |
| 52 |
inputLayout.setAttributes({ |
| 53 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 54 |
{ 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) } |
| 55 |
}); |
| 56 |
m_pipeline->setVertexInputLayout(inputLayout); |
| 57 |
m_pipeline->setShaderResourceBindings(m_srb.get()); |
| 58 |
m_pipeline->setRenderPassDescriptor(renderTarget()->renderPassDescriptor()); |
| 59 |
m_pipeline->create(); |
| 60 |
|
| 61 |
QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 62 |
resourceUpdates->uploadStaticBuffer(m_vbuf.get(), vertexData); |
| 63 |
cb->resourceUpdate(resourceUpdates); |
| 64 |
} |
| 65 |
//![init-pipeline] |
| 66 |
|
| 67 |
//![init-matrix] |
| 68 |
const QSize outputSize = renderTarget()->pixelSize(); |
| 69 |
m_viewProjection = m_rhi->clipSpaceCorrMatrix(); |
| 70 |
m_viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 71 |
m_viewProjection.translate(0, 0, -4); |
| 72 |
} |
| 73 |
//![init-matrix] |
| 74 |
|
| 75 |
//![render-1] |
| 76 |
void ExampleRhiWidget::render(QRhiCommandBuffer *cb) |
| 77 |
{ |
| 78 |
QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 79 |
m_rotation += 1.0f; |
| 80 |
QMatrix4x4 modelViewProjection = m_viewProjection; |
| 81 |
modelViewProjection.rotate(m_rotation, 0, 1, 0); |
| 82 |
resourceUpdates->updateDynamicBuffer(m_ubuf.get(), 0, 64, modelViewProjection.constData()); |
| 83 |
//![render-1] |
| 84 |
//![render-pass] |
| 85 |
const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f); |
| 86 |
cb->beginPass(renderTarget(), clearColor, { 1.0f, 0 }, resourceUpdates); |
| 87 |
|
| 88 |
cb->setGraphicsPipeline(m_pipeline.get()); |
| 89 |
const QSize outputSize = renderTarget()->pixelSize(); |
| 90 |
cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height())); |
| 91 |
cb->setShaderResources(); |
| 92 |
const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0); |
| 93 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 94 |
cb->draw(3); |
| 95 |
|
| 96 |
cb->endPass(); |
| 97 |
//![render-pass] |
| 98 |
|
| 99 |
//![render-2] |
| 100 |
update(); |
| 101 |
} |
| 102 |
//![render-2] |