diff options
Diffstat (limited to 'examples/widgets/rhi/simplerhiwidget')
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/CMakeLists.txt | 48 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/examplewidget.cpp | 102 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/examplewidget.h | 30 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/main.cpp | 26 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/shader_assets/color.frag.qsb | bin | 0 -> 738 bytes | |||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/shader_assets/color.vert.qsb | bin | 0 -> 1091 bytes | |||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/shaders/color.frag | 10 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/shaders/color.vert | 16 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/simplerhiwidget.pro | 12 | ||||
-rw-r--r-- | examples/widgets/rhi/simplerhiwidget/simplerhiwidget.qrc | 6 |
10 files changed, 250 insertions, 0 deletions
diff --git a/examples/widgets/rhi/simplerhiwidget/CMakeLists.txt b/examples/widgets/rhi/simplerhiwidget/CMakeLists.txt new file mode 100644 index 0000000000..cf471f1f8a --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(simplerhiwidget LANGUAGES CXX) + +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) + +qt_standard_project_setup() + +qt_add_executable(simplerhiwidget + examplewidget.cpp examplewidget.h + main.cpp +) + +set_target_properties(simplerhiwidget PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +# needs GuiPrivate to be able to include <rhi/qrhi.h> +target_link_libraries(simplerhiwidget PRIVATE + Qt6::Core + Qt6::Gui + Qt6::GuiPrivate + Qt6::Widgets +) + +qt_add_resources(simplerhiwidget "simplerhiwidget" + PREFIX + "/" + FILES + "shader_assets/color.vert.qsb" + "shader_assets/color.frag.qsb" +) + +install(TARGETS simplerhiwidget + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET simplerhiwidget + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR +) +install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/rhi/simplerhiwidget/examplewidget.cpp b/examples/widgets/rhi/simplerhiwidget/examplewidget.cpp new file mode 100644 index 0000000000..7de33059a3 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/examplewidget.cpp @@ -0,0 +1,102 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "examplewidget.h" +#include <QFile> + +static float vertexData[] = { + 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, +}; + +//![get-shader] +static QShader getShader(const QString &name) +{ + QFile f(name); + return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader(); +} +//![get-shader] + +//![init-1] +void ExampleRhiWidget::initialize(QRhiCommandBuffer *cb) +{ + if (m_rhi != rhi()) { + m_pipeline.reset(); + m_rhi = rhi(); + } +//![init-1] +//![init-pipeline] + if (!m_pipeline) { + m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData))); + m_vbuf->create(); + + m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64)); + m_ubuf->create(); + + m_srb.reset(m_rhi->newShaderResourceBindings()); + m_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, m_ubuf.get()), + }); + m_srb->create(); + + m_pipeline.reset(m_rhi->newGraphicsPipeline()); + m_pipeline->setShaderStages({ + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/shader_assets/color.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/shader_assets/color.frag.qsb")) } + }); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 5 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float2, 0 }, + { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) } + }); + m_pipeline->setVertexInputLayout(inputLayout); + m_pipeline->setShaderResourceBindings(m_srb.get()); + m_pipeline->setRenderPassDescriptor(renderTarget()->renderPassDescriptor()); + m_pipeline->create(); + + QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch(); + resourceUpdates->uploadStaticBuffer(m_vbuf.get(), vertexData); + cb->resourceUpdate(resourceUpdates); + } +//![init-pipeline] + +//![init-matrix] + const QSize outputSize = renderTarget()->pixelSize(); + m_viewProjection = m_rhi->clipSpaceCorrMatrix(); + m_viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); + m_viewProjection.translate(0, 0, -4); +} +//![init-matrix] + +//![render-1] +void ExampleRhiWidget::render(QRhiCommandBuffer *cb) +{ + QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch(); + m_rotation += 1.0f; + QMatrix4x4 modelViewProjection = m_viewProjection; + modelViewProjection.rotate(m_rotation, 0, 1, 0); + resourceUpdates->updateDynamicBuffer(m_ubuf.get(), 0, 64, modelViewProjection.constData()); +//![render-1] +//![render-pass] + const QColor clearColor = QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f); + cb->beginPass(renderTarget(), clearColor, { 1.0f, 0 }, resourceUpdates); + + cb->setGraphicsPipeline(m_pipeline.get()); + const QSize outputSize = renderTarget()->pixelSize(); + cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height())); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0); + cb->setVertexInput(0, 1, &vbufBinding); + cb->draw(3); + + cb->endPass(); +//![render-pass] + +//![render-2] + update(); +} +//![render-2] diff --git a/examples/widgets/rhi/simplerhiwidget/examplewidget.h b/examples/widgets/rhi/simplerhiwidget/examplewidget.h new file mode 100644 index 0000000000..efd3b90d91 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/examplewidget.h @@ -0,0 +1,30 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef EXAMPLEWIDGET_H +#define EXAMPLEWIDGET_H + +//![0] +#include <QRhiWidget> +#include <rhi/qrhi.h> + +class ExampleRhiWidget : public QRhiWidget +{ +public: + ExampleRhiWidget(QWidget *parent = nullptr) : QRhiWidget(parent) { } + + void initialize(QRhiCommandBuffer *cb) override; + void render(QRhiCommandBuffer *cb) override; + +private: + QRhi *m_rhi = nullptr; + std::unique_ptr<QRhiBuffer> m_vbuf; + std::unique_ptr<QRhiBuffer> m_ubuf; + std::unique_ptr<QRhiShaderResourceBindings> m_srb; + std::unique_ptr<QRhiGraphicsPipeline> m_pipeline; + QMatrix4x4 m_viewProjection; + float m_rotation = 0.0f; +}; +//![0] + +#endif diff --git a/examples/widgets/rhi/simplerhiwidget/main.cpp b/examples/widgets/rhi/simplerhiwidget/main.cpp new file mode 100644 index 0000000000..b9cf848125 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/main.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QApplication> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include "examplewidget.h" + +//![0] +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + ExampleRhiWidget *rhiWidget = new ExampleRhiWidget; + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(rhiWidget); + + QWidget w; + w.setLayout(layout); + w.resize(1280, 720); + w.show(); + + return app.exec(); +} +//![0] diff --git a/examples/widgets/rhi/simplerhiwidget/shader_assets/color.frag.qsb b/examples/widgets/rhi/simplerhiwidget/shader_assets/color.frag.qsb Binary files differnew file mode 100644 index 0000000000..32bd2d5953 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/shader_assets/color.frag.qsb diff --git a/examples/widgets/rhi/simplerhiwidget/shader_assets/color.vert.qsb b/examples/widgets/rhi/simplerhiwidget/shader_assets/color.vert.qsb Binary files differnew file mode 100644 index 0000000000..bf97035d7e --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/shader_assets/color.vert.qsb diff --git a/examples/widgets/rhi/simplerhiwidget/shaders/color.frag b/examples/widgets/rhi/simplerhiwidget/shaders/color.frag new file mode 100644 index 0000000000..375587662f --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/shaders/color.frag @@ -0,0 +1,10 @@ +#version 440 + +layout(location = 0) in vec3 v_color; + +layout(location = 0) out vec4 fragColor; + +void main() +{ + fragColor = vec4(v_color, 1.0); +} diff --git a/examples/widgets/rhi/simplerhiwidget/shaders/color.vert b/examples/widgets/rhi/simplerhiwidget/shaders/color.vert new file mode 100644 index 0000000000..e876f290e7 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/shaders/color.vert @@ -0,0 +1,16 @@ +#version 440 + +layout(location = 0) in vec4 position; +layout(location = 1) in vec3 color; + +layout(location = 0) out vec3 v_color; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; +}; + +void main() +{ + v_color = color; + gl_Position = mvp * position; +} diff --git a/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.pro b/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.pro new file mode 100644 index 0000000000..2477d7f368 --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.pro @@ -0,0 +1,12 @@ +TEMPLATE = app + +# needs gui-private to be able to include <rhi/qrhi.h> +QT += gui-private widgets + +HEADERS += examplewidget.h +SOURCES += examplewidget.cpp main.cpp + +RESOURCES += simplerhiwidget.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/widgets/rhi/simplerhiwidget +INSTALLS += target diff --git a/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.qrc b/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.qrc new file mode 100644 index 0000000000..ddc6dfbe5a --- /dev/null +++ b/examples/widgets/rhi/simplerhiwidget/simplerhiwidget.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> + <file>shader_assets/color.vert.qsb</file> + <file>shader_assets/color.frag.qsb</file> +</qresource> +</RCC> |