Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qssgrhiquadrenderer.cpp
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2020 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#include <QtQuick3DRuntimeRender/private/qssgrhiquadrenderer_p.h>
6
8
10 QVector3D(-1, -1, 0),
11 QVector3D(-1, 1, 0),
12 QVector3D(1, 1, 0),
13 QVector3D(1, -1, 0),
14
15 QVector3D(-1, -1, 1),
16 QVector3D(-1, 1, 1),
17 QVector3D(1, 1, 1),
18 QVector3D(1, -1, 1),
19
20 QVector3D(-1, -1, -1),
21 QVector3D(-1, 1, -1),
22 QVector3D(1, 1, -1),
23 QVector3D(1, -1, -1),
24};
25
27 QVector2D(0, 0),
28 QVector2D(0, 1),
29 QVector2D(1, 1),
30 QVector2D(1, 0)
31};
32
33static const quint16 g_rectIndex[] = {
34 0, 1, 2, 0, 2, 3, // front face - 0, 1, 2, 3
35 0, 4, 5, 0, 5, 1, // left face - 0, 4, 5, 1
36 1, 5, 6, 1, 6, 2, // top face - 1, 5, 6, 2
37 3, 2, 6, 3, 6, 7, // right face - 3, 2, 6, 7
38 0, 3, 7, 0, 7, 4, // bottom face - 0, 3, 7, 4
39 7, 6, 5, 7, 5, 4 // back face - 7, 6, 5, 4
40};
41
42void QSSGRhiQuadRenderer::ensureBuffers(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
43{
44 if (!m_vbuf) {
45 constexpr int vertexCount = 8;
46 m_vbuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
49 quint32(5 * sizeof(float)),
50 5 * vertexCount * sizeof(float));
51 m_vbuf->buffer()->setName(QByteArrayLiteral("quad vertex buffer"));
52 float buf[5 * vertexCount];
53 float *p = buf;
54 for (int i = 0; i < vertexCount; ++i) {
55 *p++ = g_fullScreenRectFaces[i].x();
56 *p++ = g_fullScreenRectFaces[i].y();
57 *p++ = g_fullScreenRectFaces[i].z();
58 *p++ = g_fullScreenRectUVs[i % 4].x();
59 *p++ = g_fullScreenRectUVs[i % 4].y();
60 }
61 rub->uploadStaticBuffer(m_vbuf->buffer(), buf);
62 }
63 if (!m_ibuf) {
64 m_ibuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
67 0,
68 6 * sizeof(quint16),
70 m_ibuf->buffer()->setName(QByteArrayLiteral("quad index buffer"));
71 const quint16 buf[] = { 0, 1, 2, 0, 2, 3 };
72 rub->uploadStaticBuffer(m_ibuf->buffer(), buf);
73 }
74}
75
77{
78 QRhiResourceUpdateBatch *rub = maybeRub ? maybeRub : rhiCtx->rhi()->nextResourceUpdateBatch();
79 ensureBuffers(rhiCtx, rub);
80 rhiCtx->commandBuffer()->resourceUpdate(rub);
81}
82
86{
87 // ps must have viewport and shaderPipeline set already
88 if (flags.testFlag(UvCoords)) {
91 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
92 });
94 } else {
97 }
98 ps->ia.inputLayout.setBindings({ 5 * sizeof(float) });
100
101 ps->depthTestEnable = flags.testFlag(DepthTest);
102 ps->depthWriteEnable = flags.testFlag(DepthWrite);
104 if (flags.testFlag(PremulBlend)) {
105 ps->blendEnable = true;
110 } else { // set to default, since we may not have had a renderable previously
115 }
116
117 QRhiGraphicsPipeline *pipeline = rhiCtx->pipeline(QSSGGraphicsPipelineStateKey::create(*ps, rpDesc, srb), rpDesc, srb);
118 // Make sure that we were able to create the pipeline before trying to use it
119 // When GraphicsPipeline creation fails it should return nullptr and print a warning
120 if (!pipeline)
121 return;
122
124 cb->setGraphicsPipeline(pipeline);
125 cb->setShaderResources(srb);
126 cb->setViewport(ps->viewport);
127
128 quint32 vertexOffset = flags.testAnyFlags(RenderBehind) ? 5 * 4 * sizeof(float) : 0;
129
130 QRhiCommandBuffer::VertexInput vb(m_vbuf->buffer(), vertexOffset);
131 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
132 cb->setVertexInput(0, 1, &vb, m_ibuf->buffer(), m_ibuf->indexFormat());
133 cb->drawIndexed(6);
134 QSSGRHICTX_STAT(rhiCtx, drawIndexed(6, 1));
135 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderCall, 36llu | (1llu << 32), QByteArrayLiteral("render_quad"));
136}
137
141{
143 cb->beginPass(rt, Qt::black, { 1.0f, 0 }, nullptr, QSSGRhiContext::commonPassFlags());
144 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
145 recordRenderQuad(rhiCtx, ps, srb, rt->renderPassDescriptor(), flags);
146 cb->endPass();
147 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
148}
149
151{
152 QRhiResourceUpdateBatch *rub = maybeRub ? maybeRub : rhiCtx->rhi()->nextResourceUpdateBatch();
153 ensureBuffers(rhiCtx, rub);
154 rhiCtx->commandBuffer()->resourceUpdate(rub);
155}
156
157//### The flags UvCoords and RenderBehind are ignored
159{
160 // ps must have viewport and shaderPipeline set already
163 ps->ia.inputLayout.setBindings({ 3 * sizeof(float) });
165
170 ps->blendEnable = true;
175 } else { // set to default, since we may not have had a renderable previously
180 }
181
182 QRhiGraphicsPipeline *pipeline = rhiCtx->pipeline(QSSGGraphicsPipelineStateKey::create(*ps, rpDesc, srb), rpDesc, srb);
183 // Make sure that we were able to create the pipeline before trying to use it
184 // When GraphicsPipeline creation fails it should return nullptr and print a warning
185 if (!pipeline)
186 return;
187
189 cb->setGraphicsPipeline(pipeline);
190 cb->setShaderResources(srb);
191 cb->setViewport(ps->viewport);
192
193 QRhiCommandBuffer::VertexInput vb(m_vbuf->buffer(), 0);
194 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
195 cb->setVertexInput(0, 1, &vb, m_ibuf->buffer(), m_ibuf->indexFormat());
196 cb->drawIndexed(36);
197 QSSGRHICTX_STAT(rhiCtx, drawIndexed(36, 1));
198 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderCall, 36, QByteArrayLiteral("render_cube"));
199}
200
201void QSSGRhiCubeRenderer::ensureBuffers(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
202{
203 if (!m_vbuf) {
204 constexpr int vertexCount = 8;
205 m_vbuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
208 quint32(3 * sizeof(float)),
209 3 * vertexCount * sizeof(float));
210 m_vbuf->buffer()->setName(QByteArrayLiteral("cube vertex buffer"));
211
212 float buf[3 * vertexCount];
213 float *p = buf;
214 for (int i = 0; i < vertexCount; ++i) {
215 *p++ = g_fullScreenRectFaces[4 + i].x();
216 *p++ = g_fullScreenRectFaces[4 + i].y();
217 *p++ = g_fullScreenRectFaces[4 + i].z();
218 }
219 rub->uploadStaticBuffer(m_vbuf->buffer(), buf);
220 }
221 if (!m_ibuf) {
222 m_ibuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
225 0,
226 sizeof(g_rectIndex),
228 m_ibuf->buffer()->setName(QByteArrayLiteral("cube index buffer"));
229 rub->uploadStaticBuffer(m_ibuf->buffer(), g_rectIndex);
230 }
231}
232
@ Immutable
Definition qrhi.h:837
@ IndexBuffer
Definition qrhi.h:844
@ VertexBuffer
Definition qrhi.h:843
\inmodule QtGui
Definition qrhi.h:1614
QPair< QRhiBuffer *, quint32 > VertexInput
Synonym for QPair<QRhiBuffer *, quint32>.
Definition qrhi.h:1643
void resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates)
Sometimes committing resource updates is necessary or just more convenient without starting a render ...
Definition qrhi.cpp:8986
\inmodule QtGui
Definition qrhi.h:1241
\inmodule QtGui
Definition qrhi.h:1119
QRhiRenderPassDescriptor * renderPassDescriptor() const
Definition qrhi.h:1141
\inmodule QtGui
Definition qrhi.h:1694
void uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
Enqueues updating a region of a QRhiBuffer buf created with the type QRhiBuffer::Immutable or QRhiBuf...
Definition qrhi.cpp:8615
\inmodule QtGui
Definition qrhi.h:1190
\inmodule QtGui
Definition qrhi.h:1161
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Sets the bindings from the specified list.
Definition qrhi.h:317
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Sets the attributes from the specified list.
Definition qrhi.h:329
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition qrhi.cpp:8854
QRhiCommandBuffer * commandBuffer() const
static QRhiCommandBuffer::BeginPassFlags commonPassFlags()
QRhiGraphicsPipeline * pipeline(const QSSGGraphicsPipelineStateKey &key, QRhiRenderPassDescriptor *rpDesc, QRhiShaderResourceBindings *srb)
QRhi * rhi() const
void prepareCube(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *maybeRub)
void recordRenderCube(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc, QSSGRhiQuadRenderer::Flags flags)
void recordRenderQuadPass(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiTextureRenderTarget *rt, Flags flags)
void recordRenderQuad(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc, Flags flags)
void prepareQuad(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *maybeRub)
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:502
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:501
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:671
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:670
constexpr float z() const noexcept
Returns the z coordinate of this point.
Definition qvectornd.h:672
Combined button and popup list for selecting options.
@ black
Definition qnamespace.h:29
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
Flags
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLfloat GLfloat p
[1]
#define Q_QUICK3D_PROFILE_START(Type)
#define Q_QUICK3D_PROFILE_END_WITH_STRING(Type, Payload, Str)
#define QSSGRHICTX_STAT(ctx, f)
static QT_BEGIN_NAMESPACE const QVector3D g_fullScreenRectFaces[]
static const QVector2D g_fullScreenRectUVs[]
static const quint16 g_rectIndex[]
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
unsigned int quint32
Definition qtypes.h:45
unsigned short quint16
Definition qtypes.h:43
static QSSGGraphicsPipelineStateKey create(const QSSGRhiGraphicsPipelineState &state, const QRhiRenderPassDescriptor *rpDesc, const QRhiShaderResourceBindings *srb)
QSSGRhiInputAssemblerState ia
QRhiGraphicsPipeline::CullMode cullMode
QRhiGraphicsPipeline::TargetBlend targetBlend
QVarLengthArray< InputSemantic, 8 > inputs
QRhiVertexInputLayout inputLayout
QRhiGraphicsPipeline::Topology topology