1// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB).
2// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QT3DRENDER_RENDER_RHI_SUBMISSIONCONTEXT_H
6#define QT3DRENDER_RENDER_RHI_SUBMISSIONCONTEXT_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of other Qt classes. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <rhibuffer_p.h>
20#include <Qt3DRender/qclearbuffers.h>
21#include <Qt3DRender/private/handle_types_p.h>
22#include <Qt3DRender/qblitframebuffer.h>
23#include <Qt3DRender/private/shader_p.h>
24#include <Qt3DRender/private/statemask_p.h>
25#include <Qt3DRender/private/qgraphicsapifilter_p.h>
26#include <shaderparameterpack_p.h>
27#include <shadervariables_p.h>
28#include <rhihandle_types_p.h>
29#include <QSurface>
30#include <QOffscreenSurface>
31#include <rhi/qrhi.h>
32
33QT_BEGIN_NAMESPACE
34
35class QAbstractOpenGLFunctions;
36
37namespace Qt3DRender {
38
39namespace Render {
40
41class Material;
42class AttachmentPack;
43class Attribute;
44class Buffer;
45class ShaderManager;
46struct StateVariant;
47class RenderTarget;
48class RenderStateSet;
49
50namespace Rhi {
51
52class Renderer;
53class GraphicsHelperInterface;
54class RHITexture;
55class RHIShader;
56class RHIShaderManager;
57class RenderCommand;
58
59typedef QPair<QString, int> NamedUniformLocation;
60
61class Q_AUTOTEST_EXPORT SubmissionContext
62{
63public:
64 enum Feature {
65 MRT = 0,
66 Tessellation,
67 UniformBufferObject,
68 BindableFragmentOutputs,
69 PrimitiveRestart,
70 RenderBufferDimensionRetrieval,
71 TextureDimensionRetrieval,
72 ShaderStorageObject,
73 Compute,
74 DrawBuffersBlend,
75 BlitFramebuffer,
76 IndirectDrawing,
77 MapBuffer,
78 Fences,
79 ShaderImage
80 };
81
82 enum FBOBindMode { FBODraw, FBORead, FBOReadAndDraw };
83
84 SubmissionContext();
85 ~SubmissionContext();
86
87 void initialize();
88 int id() const; // unique, small integer ID of this context
89 void setRenderer(Renderer *renderer) { m_renderer = renderer; }
90
91 void setDrivenExternally(bool drivenExternally);
92 bool drivenExternally() const;
93
94 bool beginDrawing(QSurface *surface);
95 void endDrawing(bool swapBuffers);
96 void releaseResources();
97 void setOpenGLContext(QOpenGLContext *ctx);
98 void setRHIContext(QRhi *ctx);
99 void setDefaultRenderTarget(QRhiRenderTarget *target);
100 void setCommandBuffer(QRhiCommandBuffer *commandBuffer);
101 bool isInitialized() const { return m_initialized; }
102 const GraphicsApiFilterData *contextInfo() const;
103
104 // Shaders
105 struct ShaderCreationInfo
106 {
107 bool linkSucceeded = false;
108 QString logs;
109 };
110
111 ShaderCreationInfo createShaderProgram(RHIShader *shaderNode);
112 void loadShader(Shader *shader, ShaderManager *shaderManager,
113 RHIShaderManager *rhiShaderManager);
114
115
116 // FBO
117 QImage readFramebuffer(const QRect &rect);
118 void blitFramebuffer(Qt3DCore::QNodeId outputRenderTargetId,
119 Qt3DCore::QNodeId inputRenderTargetId, QRect inputRect, QRect outputRect,
120 uint defaultFboId,
121 QRenderTargetOutput::AttachmentPoint inputAttachmentPoint,
122 QRenderTargetOutput::AttachmentPoint outputAttachmentPoint,
123 QBlitFramebuffer::InterpolationMethod interpolationMethod);
124
125 // Attributes
126 void specifyAttribute(const Attribute *attribute, Buffer *buffer,
127 const ShaderAttribute *attributeDescription);
128 void specifyIndices(Buffer *buffer);
129
130 // Buffer
131 void updateBuffer(Buffer *buffer);
132 QByteArray downloadBufferContent(Buffer *buffer);
133 void releaseBuffer(Qt3DCore::QNodeId bufferId);
134 bool hasRHIBufferForBuffer(Buffer *buffer);
135 RHIBuffer *rhiBufferForRenderBuffer(Buffer *buf);
136
137 // RenderState
138 void applyStateSet(const RenderStateSet *ss, QRhiGraphicsPipeline *graphicsPipeline);
139 StateVariant *getState(RenderStateSet *ss, StateMask type) const;
140
141 // Swap chain
142
143 struct SwapChainInfo
144 {
145 QRhiSwapChain *swapChain = nullptr;
146 QRhiRenderBuffer *renderBuffer = nullptr;
147 QRhiRenderPassDescriptor *renderPassDescriptor = nullptr;
148 };
149 SwapChainInfo *swapChainForSurface(QSurface *surface) noexcept;
150
151 QRhiResourceUpdateBatch *m_currentUpdates {};
152
153 QRhi *rhi() const { return m_rhi; }
154 QRhiCommandBuffer *currentFrameCommandBuffer() const;
155 QRhiRenderTarget *currentFrameRenderTarget() const;
156 QRhiRenderTarget *defaultRenderTarget() const;
157 QRhiRenderPassDescriptor *currentRenderPassDescriptor() const;
158 QRhiSwapChain *currentSwapChain() const;
159 QSurfaceFormat format() const noexcept;
160
161private:
162 // FBO
163 void bindFrameBufferAttachmentHelper(GLuint fboId, const AttachmentPack &attachments);
164 void activateDrawBuffers(const AttachmentPack &attachments);
165 // Buffers
166 HRHIBuffer createRHIBufferFor(Buffer *buffer);
167 void uploadDataToRHIBuffer(Buffer *buffer, RHIBuffer *b);
168 QByteArray downloadDataFromRHIBuffer(Buffer *buffer, RHIBuffer *b);
169 bool bindRHIBuffer(RHIBuffer *buffer, RHIBuffer::Type type);
170
171 // States
172 void applyState(const StateVariant &state, QRhiGraphicsPipeline *graphicsPipeline);
173
174 bool m_initialized;
175 bool m_ownsRhiCtx;
176 bool m_drivenExternally;
177 const unsigned int m_id;
178
179 QHash<Qt3DCore::QNodeId, HRHIBuffer> m_renderBufferHash;
180
181 Material *m_material;
182
183 Renderer *m_renderer;
184
185 GraphicsApiFilterData m_contextInfo;
186
187 QRhi *m_rhi;
188 QHash<QSurface *, SwapChainInfo> m_swapChains;
189 QRhiSwapChain *m_currentSwapChain;
190 QRhiRenderPassDescriptor *m_currentRenderPassDescriptor;
191 QRhiRenderTarget *m_defaultRenderTarget;
192 QRhiCommandBuffer *m_defaultCommandBuffer;
193
194#ifndef QT_NO_OPENGL
195 QOffscreenSurface *m_fallbackSurface;
196#endif
197};
198
199} // namespace Rhi
200} // namespace Render
201} // namespace Qt3DRender
202
203QT_END_NAMESPACE
204
205#endif // QT3DRENDER_RENDER_RHI_SUBMISSIONCONTEXT_H
206

source code of qt3d/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h