Debian Codegraph @

tests/manual/rhi/cubemap/cubemap.cpp

raw
1 // Copyright (C) 2018 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 struct {
8 QList<QRhiResource *> releasePool;
9 QRhiBuffer *vbuf = nullptr;
10 QRhiBuffer *ubuf = nullptr;
11 QRhiTexture *tex = nullptr;
12 QRhiSampler *sampler = nullptr;
13 QRhiShaderResourceBindings *srb = nullptr;
14 QRhiGraphicsPipeline *ps = nullptr;
15 QRhiResourceUpdateBatch *initialUpdates = nullptr;
16 } d;
17
18 void Window::customInit()
19 {
20 d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
21 d.vbuf->create();
22 d.releasePool << d.vbuf;
23
24 d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
25 d.ubuf->create();
26 d.releasePool << d.ubuf;
27
28 const QSize cubeMapSize(512, 512);
29 d.tex = m_r->newTexture(QRhiTexture::RGBA8, cubeMapSize, 1, QRhiTexture::CubeMap
30 | QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips); // exercise mipmap generation as well
31 d.releasePool << d.tex;
32 d.tex->create();
33
34 d.initialUpdates = m_r->nextResourceUpdateBatch();
35 d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
36
37 QImage img = QImage(":/c.png").flipped().convertToFormat(QImage::Format_RGBA8888);
38 // just use the same image for all faces for now
39 QRhiTextureSubresourceUploadDescription subresDesc(img);
40 QRhiTextureUploadDescription desc({
41 { 0, 0, subresDesc }, // +X
42 { 1, 0, subresDesc }, // -X
43 { 2, 0, subresDesc }, // +Y
44 { 3, 0, subresDesc }, // -Y
45 { 4, 0, subresDesc }, // +Z
46 { 5, 0, subresDesc } // -Z
47 });
48 d.initialUpdates->uploadTexture(d.tex, desc);
49
50 d.initialUpdates->generateMips(d.tex);
51
52 d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
53 QRhiSampler::Repeat, QRhiSampler::Repeat);
54 d.releasePool << d.sampler;
55 d.sampler->create();
56
57 d.srb = m_r->newShaderResourceBindings();
58 d.releasePool << d.srb;
59 d.srb->setBindings({
60 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
61 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
62 });
63 d.srb->create();
64
65 d.ps = m_r->newGraphicsPipeline();
66 d.releasePool << d.ps;
67
68 d.ps->setDepthTest(true);
69 d.ps->setDepthWrite(true);
70 d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
71
72 d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back
73 d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data
74
75 QShader vs = getShader(QLatin1String(":/cubemap.vert.qsb"));
76 Q_ASSERT(vs.isValid());
77 QShader fs = getShader(QLatin1String(":/cubemap.frag.qsb"));
78 Q_ASSERT(fs.isValid());
79 d.ps->setShaderStages({
80 { QRhiShaderStage::Vertex, vs },
81 { QRhiShaderStage::Fragment, fs }
82 });
83
84 QRhiVertexInputLayout inputLayout;
85 inputLayout.setBindings({
86 { 3 * sizeof(float) }
87 });
88 inputLayout.setAttributes({
89 { 0, 0, QRhiVertexInputAttribute::Float3, 0 }
90 });
91
92 d.ps->setVertexInputLayout(inputLayout);
93 d.ps->setShaderResourceBindings(d.srb);
94 d.ps->setRenderPassDescriptor(m_rp);
95
96 d.ps->create();
97 }
98
99 void Window::customRelease()
100 {
101 qDeleteAll(d.releasePool);
102 d.releasePool.clear();
103 }
104
105 void Window::customRender()
106 {
107 const QSize outputSizeInPixels = m_sc->currentPixelSize();
108 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
109 QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
110
111 if (d.initialUpdates) {
112 u->merge(d.initialUpdates);
113 d.initialUpdates->release();
114 d.initialUpdates = nullptr;
115 }
116
117 QMatrix4x4 mvp = m_r->clipSpaceCorrMatrix();
118 mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f);
119 // cube vertices go from -1..1
120 mvp.scale(10);
121 static float rx = 0;
122 mvp.rotate(rx, 1, 0, 0);
123 rx += 0.5f;
124 // no translation
125 u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
126
127 cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u);
128 cb->setGraphicsPipeline(d.ps);
129 cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
130 cb->setShaderResources();
131 const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0);
132 cb->setVertexInput(0, 1, &vbufBinding);
133 cb->draw(36);
134 cb->endPass();
135 }