Debian Codegraph @

tests/manual/rhi/stenciloutline/stenciloutline.cpp

raw
1 // Copyright (C) 2019 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "../shared/examplefw.h"
5 #include "../shared/cube.h"
6
7 static const quint32 UBUF_SIZE = 76;
8 static const float OUTLINE_SIZE = 1.05f;
9
10 struct {
11 QList<QRhiResource *> releasePool;
12
13 QRhiBuffer *vbuf;
14 QRhiBuffer *ubuf;
15 QRhiShaderResourceBindings *srb;
16 QRhiGraphicsPipeline *psPass1;
17 QRhiGraphicsPipeline *psPass2;
18 float rot = 0.0f;
19
20 QRhiResourceUpdateBatch *initialUpdates = nullptr;
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, 2 * m_r->ubufAligned(UBUF_SIZE));
34 d.ubuf->create();
35 d.releasePool << d.ubuf;
36
37 d.srb = m_r->newShaderResourceBindings();
38 d.releasePool << d.srb;
39 d.srb->setBindings({
40 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, UBUF_SIZE)
41 });
42 d.srb->create();
43
44 QRhiVertexInputLayout inputLayout;
45 inputLayout.setBindings({
46 { 3 * sizeof(float) },
47 });
48 inputLayout.setAttributes({
49 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
50 });
51
52 const QRhiShaderStage stages[] = {
53 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/material.vert.qsb")) },
54 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/material.frag.qsb")) }
55 };
56
57 d.psPass1 = m_r->newGraphicsPipeline();
58 d.releasePool << d.psPass1;
59 d.psPass1->setShaderStages(stages, stages + 2);
60 d.psPass1->setFlags(QRhiGraphicsPipeline::UsesStencilRef);
61 d.psPass1->setDepthTest(true);
62 d.psPass1->setDepthWrite(true);
63 QRhiGraphicsPipeline::StencilOpState stencilPass1 = { QRhiGraphicsPipeline::Keep,
64 QRhiGraphicsPipeline::Keep,
65 QRhiGraphicsPipeline::Replace,
66 QRhiGraphicsPipeline::Always };
67 d.psPass1->setStencilFront(stencilPass1);
68 d.psPass1->setCullMode(QRhiGraphicsPipeline::Back);
69 //d.psPass1->setStencilBack(stencilPass1);
70 d.psPass1->setStencilTest(true);
71 d.psPass1->setVertexInputLayout(inputLayout);
72 d.psPass1->setShaderResourceBindings(d.srb);
73 d.psPass1->setRenderPassDescriptor(m_rp);
74 d.psPass1->create();
75
76 d.psPass2 = m_r->newGraphicsPipeline();
77 d.releasePool << d.psPass2;
78 d.psPass2->setShaderStages(stages, stages + 2);
79 d.psPass2->setFlags(QRhiGraphicsPipeline::UsesStencilRef);
80 d.psPass2->setDepthTest(false);
81 d.psPass2->setDepthWrite(false);
82 QRhiGraphicsPipeline::StencilOpState stencilPass2 = { QRhiGraphicsPipeline::Keep,
83 QRhiGraphicsPipeline::Keep,
84 QRhiGraphicsPipeline::Replace,
85 QRhiGraphicsPipeline::NotEqual };
86 d.psPass2->setStencilFront(stencilPass2);
87 d.psPass2->setCullMode(QRhiGraphicsPipeline::Back);
88 //d.psPass2->setStencilBack(stencilPass2);
89 d.psPass2->setStencilTest(true);
90 d.psPass2->setStencilWriteMask(0);
91 d.psPass2->setVertexInputLayout(inputLayout);
92 d.psPass2->setShaderResourceBindings(d.srb);
93 d.psPass2->setRenderPassDescriptor(m_rp);
94 d.psPass2->create();
95 }
96
97 void Window::customRelease()
98 {
99 qDeleteAll(d.releasePool);
100 d.releasePool.clear();
101 }
102
103 void Window::customRender()
104 {
105 const QSize outputSizeInPixels = m_sc->currentPixelSize();
106 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
107 QRhiResourceUpdateBatch *u = nullptr;
108 if (d.initialUpdates) {
109 u = d.initialUpdates;
110 d.initialUpdates = nullptr;
111 }
112
113 char *p = d.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
114 QMatrix4x4 mvp = m_proj;
115 mvp.rotate(d.rot, 1, 1, 0);
116 mvp.scale(0.5f);
117
118 memcpy(p, mvp.constData(), 64);
119 const float color[] = { 1.0f, 0.0f, 0.0f };
120 memcpy(p + 64, color, 3 * sizeof(float));
121
122 static const size_t pass2Ofs = m_r->ubufAligned(UBUF_SIZE);
123 mvp.scale(OUTLINE_SIZE);
124 memcpy(p + pass2Ofs, mvp.constData(), 64);
125 const float pass2Color[] = { 0.0f, 0.0f, 1.0f };
126 memcpy(p + pass2Ofs + 64, pass2Color, 3 * sizeof(float));
127
128 d.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
129
130 const QRhiCommandBuffer::VertexInput vbufBinding[] = {
131 { d.vbuf, 0 },
132 };
133
134 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u, QRhiCommandBuffer::DoNotTrackResourcesForCompute);
135
136 cb->setGraphicsPipeline(d.psPass1);
137 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
138 QRhiCommandBuffer::DynamicOffset pass1DynOffset = { 0, 0 };
139 cb->setShaderResources(d.srb, 1, &pass1DynOffset);
140 cb->setVertexInput(0, 1, vbufBinding);
141 cb->setStencilRef(1);
142 cb->draw(36);
143
144 cb->setGraphicsPipeline(d.psPass2);
145 QRhiCommandBuffer::DynamicOffset pass2DynOffset = { 0, m_r->ubufAligned(UBUF_SIZE) };
146 cb->setShaderResources(d.srb, 1, &pass2DynOffset);
147 cb->setVertexInput(0, 1, vbufBinding);
148 cb->setStencilRef(1);
149 cb->draw(36);
150
151 cb->endPass();
152
153 d.rot += 1;
154 }