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_RENDERCOMMAND_H
6#define QT3DRENDER_RENDER_RHI_RENDERCOMMAND_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 <qglobal.h>
20#include <shaderparameterpack_p.h>
21#include <rhihandle_types_p.h>
22#include <Qt3DCore/private/vector_helper_p.h>
23#include <Qt3DRender/private/renderviewjobutils_p.h>
24#include <Qt3DRender/private/handle_types_p.h>
25#include <Qt3DRender/qgeometryrenderer.h>
26#include <QOpenGLShaderProgram>
27#include <QOpenGLTexture>
28#include <QMatrix4x4>
29#include <rhi/qrhi.h>
30#include <Qt3DCore/qattribute.h>
31#include <variant>
32
33QT_BEGIN_NAMESPACE
34class QRhiGraphicsPipeline;
35class QRhiShaderResourceBindings;
36
37namespace Qt3DRender {
38
39namespace Render {
40
41class RenderStateSet;
42using RenderStateSetPtr = QSharedPointer<RenderStateSet>;
43
44namespace Rhi {
45
46class RHIShader;
47class RHIGraphicsPipeline;
48class RHIComputePipeline;
49
50struct CommandUBO
51{
52 float modelMatrix[16];
53 float inverseModelMatrix[16];
54 float modelViewMatrix[16];
55 float modelNormalMatrix[12];
56 float inverseModelViewMatrix[16];
57 float mvp[16];
58 float inverseModelViewProjectionMatrix[16];
59 float modelViewNormalMatrix[12];
60};
61static_assert(sizeof(CommandUBO) == 6 * (16 * sizeof(float)) + 2 * (12 * sizeof(float)),
62 "UBO doesn't match std140");
63
64struct Q_AUTOTEST_EXPORT AttributeInfo
65{
66 int nameId = -1;
67 QRhiVertexInputBinding::Classification classification = QRhiVertexInputBinding::PerVertex;
68 size_t stride = 0;
69 size_t offset = 0;
70 size_t divisor = 0;
71};
72
73Q_AUTOTEST_EXPORT bool operator==(const AttributeInfo &a, const AttributeInfo &b);
74Q_AUTOTEST_EXPORT bool operator!=(const AttributeInfo &a, const AttributeInfo &b);
75
76class Q_AUTOTEST_EXPORT RenderCommand
77{
78public:
79 RenderCommand();
80 ~RenderCommand();
81
82 bool isValid() const noexcept;
83
84 HMaterial m_material; // Purely used to ease sorting (minimize stage changes, binding changes
85 // ....)
86 RHIShader *m_rhiShader; // Shader to be used at render time
87 Qt3DCore::QNodeId m_shaderId; // Shader for given pass and mesh
88 ShaderParameterPack m_parameterPack; // Might need to be reworked so as to be able to destroy
89 // the Texture while submission is happening.
90 RenderStateSetPtr m_stateSet;
91
92 HGeometry m_geometry;
93 HGeometryRenderer m_geometryRenderer;
94
95 HBuffer m_indirectDrawBuffer; // Reference to indirect draw buffer (valid only m_drawIndirect ==
96 // true)
97 HComputeCommand m_computeCommand;
98
99 // A QAttribute pack might be interesting
100 // This is a temporary fix in the meantime, to remove the hacked methods in Technique
101 std::vector<int> m_activeAttributes;
102
103 float m_depth;
104 int m_changeCost;
105
106 enum CommandType { Draw, Compute };
107
108 CommandType m_type;
109 int m_workGroups[3];
110
111 // Values filled for draw calls by Renderer (in prepare Submission)
112 GLsizei m_primitiveCount;
113 QGeometryRenderer::PrimitiveType m_primitiveType;
114 int m_restartIndexValue;
115 int m_firstInstance;
116 int m_firstVertex;
117 int m_verticesPerPatch;
118 int m_instanceCount;
119 int m_indexOffset;
120 uint m_indexAttributeByteOffset;
121 Qt3DCore::QAttribute::VertexBaseType m_indexAttributeDataType;
122 uint m_indirectAttributeByteOffset;
123 bool m_drawIndexed;
124 bool m_drawIndirect;
125 bool m_primitiveRestartEnabled;
126 bool m_isValid;
127
128 std::vector<AttributeInfo> m_attributeInfo;
129 QVarLengthArray<QRhiCommandBuffer::VertexInput, 8> vertex_input;
130
131 const Attribute *indexAttribute {};
132 QRhiBuffer *indexBuffer {};
133
134 CommandUBO m_commandUBO;
135 QRhiShaderResourceBindings *shaderResourceBindings = nullptr;
136 std::vector<QRhiShaderResourceBinding> resourcesBindings;
137
138 struct Pipeline : std::variant<std::monostate, RHIGraphicsPipeline *, RHIComputePipeline*>
139 {
140 using variant::variant;
141
142 bool isValid() const noexcept;
143
144 RHIGraphicsPipeline* graphics() const noexcept
145 {
146 auto ptr = std::get_if<RHIGraphicsPipeline*>(ptr: this);
147 return ptr ? *ptr : nullptr;
148 }
149
150 RHIComputePipeline* compute() const noexcept
151 {
152 auto ptr = std::get_if<RHIComputePipeline*>(ptr: this);
153 return ptr ? *ptr : nullptr;
154 }
155
156 template<typename F>
157 auto visit(F&& f) const
158 {
159 return std::visit(f, (const variant&) *this);
160 }
161 };
162
163 Pipeline pipeline {};
164};
165
166Q_AUTOTEST_EXPORT bool operator==(const RenderCommand &a, const RenderCommand &b) noexcept;
167
168inline bool operator!=(const RenderCommand &lhs, const RenderCommand &rhs) noexcept
169{
170 return !operator==(a: lhs, b: rhs);
171}
172
173} // namespace Rhi
174
175} // namespace Render
176
177} // namespace Qt3DRender
178
179QT_END_NAMESPACE
180
181#endif // QT3DRENDER_RENDER_RHI_RENDERCOMMAND_H
182

source code of qt3d/src/plugins/renderers/rhi/renderer/rendercommand_p.h