Debian Codegraph @

tests/manual/rhi/tessellation/tessellation.cpp

raw
1 // Copyright (C) 2021 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5
6 static const float tri[] = {
7 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
8 -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
9 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
10 };
11
12 static const bool INDEXED = false;
13 static const quint32 indices[] = { 0, 1, 2 };
14
15 struct {
16 QVector<QRhiResource *> releasePool;
17 QRhiBuffer *vbuf = nullptr;
18 QRhiBuffer *ibuf = nullptr;
19 QRhiBuffer *ubuf = nullptr;
20 QRhiShaderResourceBindings *srb = nullptr;
21 QRhiGraphicsPipeline *ps = nullptr;
22 QRhiResourceUpdateBatch *initialUpdates = nullptr;
23 QMatrix4x4 winProj;
24 float time = 0.0f;
25 } d;
26
27 void Window::customInit()
28 {
29 if (!m_r->isFeatureSupported(QRhi::Tessellation))
30 qFatal("Tessellation is not supported");
31
32 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(tri));
33 d.vbuf->create();
34 d.releasePool << d.vbuf;
35
36 if (INDEXED) {
37 d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indices));
38 d.ibuf->create();
39 d.releasePool << d.ibuf;
40 }
41
42 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4 + 4);
43 d.ubuf->create();
44 d.releasePool << d.ubuf;
45
46 d.srb = m_r->newShaderResourceBindings();
47 d.releasePool << d.srb;
48 const QRhiShaderResourceBinding::StageFlags tese = QRhiShaderResourceBinding::TessellationEvaluationStage;
49 d.srb->setBindings({ QRhiShaderResourceBinding::uniformBuffer(0, tese, d.ubuf) });
50 d.srb->create();
51
52 d.ps = m_r->newGraphicsPipeline();
53 d.releasePool << d.ps;
54
55 d.ps->setTopology(QRhiGraphicsPipeline::Patches);
56 d.ps->setPatchControlPointCount(3);
57
58 d.ps->setShaderStages({
59 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/test.vert.qsb")) },
60 { QRhiShaderStage::TessellationControl, getShader(QLatin1String(":/test.tesc.qsb")) },
61 { QRhiShaderStage::TessellationEvaluation, getShader(QLatin1String(":/test.tese.qsb")) },
62 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/test.frag.qsb")) }
63 });
64
65 d.ps->setCullMode(QRhiGraphicsPipeline::Back);
66 d.ps->setPolygonMode(QRhiGraphicsPipeline::Line);
67 d.ps->setDepthTest(true);
68 d.ps->setDepthWrite(true);
69 QRhiVertexInputLayout inputLayout;
70 inputLayout.setBindings({
71 { 6 * sizeof(float) }
72 });
73 inputLayout.setAttributes({
74 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
75 { 0, 1, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) }
76 });
77 d.ps->setVertexInputLayout(inputLayout);
78 d.ps->setShaderResourceBindings(d.srb);
79 d.ps->setRenderPassDescriptor(m_rp);
80 d.ps->create();
81
82 d.initialUpdates = m_r->nextResourceUpdateBatch();
83 d.initialUpdates->uploadStaticBuffer(d.vbuf, tri);
84
85 const float amplitude = 0.5f;
86 d.initialUpdates->updateDynamicBuffer(d.ubuf, 68, 4, &amplitude);
87
88 if (INDEXED)
89 d.initialUpdates->uploadStaticBuffer(d.ibuf, indices);
90 }
91
92 void Window::customRelease()
93 {
94 qDeleteAll(d.releasePool);
95 d.releasePool.clear();
96 }
97
98 void Window::customRender()
99 {
100 const QSize outputSizeInPixels = m_sc->currentPixelSize();
101 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
102 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
103 if (d.initialUpdates) {
104 u->merge(d.initialUpdates);
105 d.initialUpdates->release();
106 d.initialUpdates = nullptr;
107 }
108 if (d.winProj != m_proj) {
109 d.winProj = m_proj;
110 u->updateDynamicBuffer(d.ubuf, 0, 64, d.winProj.constData());
111 }
112 u->updateDynamicBuffer(d.ubuf, 64, 4, &d.time);
113 d.time += 0.01f;
114
115 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
116 cb->setGraphicsPipeline(d.ps);
117 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
118 cb->setShaderResources();
119 QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
120 if (INDEXED) {
121 cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt32);
122 cb->drawIndexed(3);
123 } else {
124 cb->setVertexInput(0, 1, &vbufBinding);
125 cb->draw(3);
126 }
127
128 cb->endPass();
129 }