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

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