Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#include <QtQuick3DUtils/private/qquick3dprofiler_p.h>
7
9
11 QVector3D(-1, -1, 0),
12 QVector3D(-1, 1, 0),
13 QVector3D(1, 1, 0),
14 QVector3D(1, -1, 0),
15
16 QVector3D(-1, -1, 1),
17 QVector3D(-1, 1, 1),
18 QVector3D(1, 1, 1),
19 QVector3D(1, -1, 1),
20
21 QVector3D(-1, -1, -1),
22 QVector3D(-1, 1, -1),
23 QVector3D(1, 1, -1),
24 QVector3D(1, -1, -1),
25};
26
28 QVector2D(0, 0),
29 QVector2D(0, 1),
30 QVector2D(1, 1),
31 QVector2D(1, 0)
32};
33
34static const quint16 g_rectIndex[] = {
35 0, 1, 2, 0, 2, 3, // front face - 0, 1, 2, 3
36 0, 4, 5, 0, 5, 1, // left face - 0, 4, 5, 1
37 1, 5, 6, 1, 6, 2, // top face - 1, 5, 6, 2
38 3, 2, 6, 3, 6, 7, // right face - 3, 2, 6, 7
39 0, 3, 7, 0, 7, 4, // bottom face - 0, 3, 7, 4
40 7, 6, 5, 7, 5, 4 // back face - 7, 6, 5, 4
41};
42
43void QSSGRhiQuadRenderer::ensureBuffers(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
44{
45 if (!m_vbuf) {
46 constexpr int vertexCount = 8;
47 m_vbuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
50 quint32(5 * sizeof(float)),
51 5 * vertexCount * sizeof(float));
52 m_vbuf->buffer()->setName(QByteArrayLiteral("quad vertex buffer"));
53 float buf[5 * vertexCount];
54 float *p = buf;
55 for (int i = 0; i < vertexCount; ++i) {
56 *p++ = g_fullScreenRectFaces[i].x();
57 *p++ = g_fullScreenRectFaces[i].y();
58 *p++ = g_fullScreenRectFaces[i].z();
59 *p++ = g_fullScreenRectUVs[i % 4].x();
60 *p++ = g_fullScreenRectUVs[i % 4].y();
61 }
62 rub->uploadStaticBuffer(m_vbuf->buffer(), buf);
63 }
64 if (!m_ibuf) {
65 m_ibuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
68 0,
69 6 * sizeof(quint16),
71 m_ibuf->buffer()->setName(QByteArrayLiteral("quad index buffer"));
72 const quint16 buf[] = { 0, 1, 2, 0, 2, 3 };
73 rub->uploadStaticBuffer(m_ibuf->buffer(), buf);
74 }
75}
76
78{
79 QRhiResourceUpdateBatch *rub = maybeRub ? maybeRub : rhiCtx->rhi()->nextResourceUpdateBatch();
80 ensureBuffers(rhiCtx, rub);
81 rhiCtx->commandBuffer()->resourceUpdate(rub);
82}
83
87{
88 // ps must have viewport and shaderPipeline set already
90 if (flags.testFlag(UvCoords)) {
91 ia.inputLayout.setAttributes({
93 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
94 });
95 ia.inputs << QSSGRhiInputAssemblerState::PositionSemantic << QSSGRhiInputAssemblerState::TexCoord0Semantic;
96 } else {
97 ia.inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } });
98 ia.inputs << QSSGRhiInputAssemblerState::PositionSemantic;
99 }
100 ia.inputLayout.setBindings({ 5 * sizeof(float) });
102
106 if (flags.testFlag(PremulBlend)) {
112 } else { // set to default, since we may not have had a renderable previously
117 }
118
120 QRhiGraphicsPipeline *pipeline = rhiCtxD->pipeline(*ps, rpDesc, srb);
121 // Make sure that we were able to create the pipeline before trying to use it
122 // When GraphicsPipeline creation fails it should return nullptr and print a warning
123 if (!pipeline)
124 return;
125
127 cb->setGraphicsPipeline(pipeline);
128 cb->setShaderResources(srb);
129 cb->setViewport(ps->viewport);
130
131 quint32 vertexOffset = flags.testAnyFlags(RenderBehind) ? 5 * 4 * sizeof(float) : 0;
132
133 QRhiCommandBuffer::VertexInput vb(m_vbuf->buffer(), vertexOffset);
134 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
135 cb->setVertexInput(0, 1, &vb, m_ibuf->buffer(), m_ibuf->indexFormat());
136 cb->drawIndexed(6);
137 QSSGRHICTX_STAT(rhiCtx, drawIndexed(6, 1));
138 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderCall, 36llu | (1llu << 32), QByteArrayLiteral("render_quad"));
139}
140
144{
146 cb->beginPass(rt, Qt::black, { 1.0f, 0 }, nullptr, rhiCtx->commonPassFlags());
147 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
148 recordRenderQuad(rhiCtx, ps, srb, rt->renderPassDescriptor(), flags);
149 cb->endPass();
150 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
151}
152
154{
155 QRhiResourceUpdateBatch *rub = maybeRub ? maybeRub : rhiCtx->rhi()->nextResourceUpdateBatch();
156 ensureBuffers(rhiCtx, rub);
157 rhiCtx->commandBuffer()->resourceUpdate(rub);
158}
159
160//### The flags UvCoords and RenderBehind are ignored
162{
164 // ps must have viewport and shaderPipeline set already
165 ia.inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } });
166 ia.inputs << QSSGRhiInputAssemblerState::PositionSemantic;
167 ia.inputLayout.setBindings({ 3 * sizeof(float) });
169
179 } else { // set to default, since we may not have had a renderable previously
184 }
185
187 QRhiGraphicsPipeline *pipeline = rhiCtxD->pipeline(*ps, rpDesc, srb);
188 // Make sure that we were able to create the pipeline before trying to use it
189 // When GraphicsPipeline creation fails it should return nullptr and print a warning
190 if (!pipeline)
191 return;
192
194 cb->setGraphicsPipeline(pipeline);
195 cb->setShaderResources(srb);
196 cb->setViewport(ps->viewport);
197
198 QRhiCommandBuffer::VertexInput vb(m_vbuf->buffer(), 0);
199 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
200 cb->setVertexInput(0, 1, &vb, m_ibuf->buffer(), m_ibuf->indexFormat());
201 cb->drawIndexed(36);
202 QSSGRHICTX_STAT(rhiCtx, drawIndexed(36, 1));
203 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderCall, 36, QByteArrayLiteral("render_cube"));
204}
205
206void QSSGRhiCubeRenderer::ensureBuffers(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
207{
208 if (!m_vbuf) {
209 constexpr int vertexCount = 8;
210 m_vbuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
213 quint32(3 * sizeof(float)),
214 3 * vertexCount * sizeof(float));
215 m_vbuf->buffer()->setName(QByteArrayLiteral("cube vertex buffer"));
216
217 float buf[3 * vertexCount];
218 float *p = buf;
219 for (int i = 0; i < vertexCount; ++i) {
220 *p++ = g_fullScreenRectFaces[4 + i].x();
221 *p++ = g_fullScreenRectFaces[4 + i].y();
222 *p++ = g_fullScreenRectFaces[4 + i].z();
223 }
224 rub->uploadStaticBuffer(m_vbuf->buffer(), buf);
225 }
226 if (!m_ibuf) {
227 m_ibuf = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
230 0,
231 sizeof(g_rectIndex),
233 m_ibuf->buffer()->setName(QByteArrayLiteral("cube index buffer"));
234 rub->uploadStaticBuffer(m_ibuf->buffer(), g_rectIndex);
235 }
236}
237
@ Immutable
Definition qrhi.h:849
@ IndexBuffer
Definition qrhi.h:856
@ VertexBuffer
Definition qrhi.h:855
\inmodule QtGui
Definition qrhi.h:1651
void resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates)
Sometimes committing resource updates is necessary or just more convenient without starting a render ...
Definition qrhi.cpp:9384
QPair< QRhiBuffer *, quint32 > VertexInput
Synonym for QPair<QRhiBuffer *, quint32>.
Definition qrhi.h:1680
\inmodule QtGui
Definition qrhi.h:1270
\inmodule QtGui
Definition qrhi.h:1142
QRhiRenderPassDescriptor * renderPassDescriptor() const
Definition qrhi.h:1164
\inmodule QtGui
Definition qrhi.h:1731
\inmodule QtGui
Definition qrhi.h:1214
\inmodule QtGui
Definition qrhi.h:1184
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition qrhi.cpp:9252
static QSSGRhiContextPrivate * get(QSSGRhiContext *q)
\inmodule QtQuick3D
QRhiCommandBuffer * commandBuffer() const
QRhiCommandBuffer::BeginPassFlags commonPassFlags() const
QRhi * rhi() const
void prepareCube(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *maybeRub)
void recordRenderCube(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc, QSSGRhiQuadRenderer::Flags flags)
QRhiGraphicsPipeline::CullMode cullMode
QRhiGraphicsPipeline::TargetBlend targetBlend
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
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
Combined button and popup list for selecting options.
@ black
Definition qnamespace.h:30
#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:50
unsigned short quint16
Definition qtypes.h:48
static const InputAssemblerState & get(const QSSGRhiGraphicsPipelineState &ps)