1// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_RENDER_RHI_RHIRESOURCEMANAGERS_P_H
5#define QT3DRENDER_RENDER_RHI_RHIRESOURCEMANAGERS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <Qt3DRender/private/qt3drender_global_p.h>
19#include <Qt3DCore/private/qresourcemanager_p.h>
20#include <texture_p.h>
21#include <rhibuffer_p.h>
22#include <rhishader_p.h>
23#include <rhigraphicspipeline_p.h>
24#include <rhirendertarget_p.h>
25#include <rendercommand_p.h>
26#include <Qt3DRender/private/apishadermanager_p.h>
27#include <Qt3DRender/private/renderstateset_p.h>
28
29QT_BEGIN_NAMESPACE
30
31namespace Qt3DRender {
32
33namespace Render {
34
35namespace Rhi {
36
37class Q_AUTOTEST_EXPORT RHIBufferManager
38 : public Qt3DCore::QResourceManager<RHIBuffer, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy>
39{
40};
41
42class Q_AUTOTEST_EXPORT RHITextureManager
43 : public Qt3DCore::QResourceManager<RHITexture, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy>
44{
45public:
46 QHash<RHITexture *, Qt3DCore::QNodeId> texNodeIdForRHITexture;
47};
48
49class Q_AUTOTEST_EXPORT RHIRenderTargetManager
50 : public Qt3DCore::QResourceManager<RHIRenderTarget, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy>
51{
52};
53
54class Q_AUTOTEST_EXPORT RHIShaderManager : public APIShaderManager<RHIShader>
55{
56public:
57 explicit RHIShaderManager() : APIShaderManager<RHIShader>() { }
58};
59
60class Q_AUTOTEST_EXPORT RHIGraphicsPipelineManager
61 : public Qt3DCore::QResourceManager<RHIGraphicsPipeline, GraphicsPipelineIdentifier,
62 Qt3DCore::NonLockingPolicy>
63{
64public:
65 RHIGraphicsPipelineManager() { }
66
67 int getIdForAttributeVec(const std::vector<AttributeInfo> &attributesInfo);
68 int getIdForRenderStates(const RenderStateSetPtr &stateSet);
69
70 void releasePipelinesReferencingShader(const Qt3DCore::QNodeId &shaderId);
71 void releasePipelinesReferencingRenderTarget(const Qt3DCore::QNodeId &renderTargetId);
72
73private:
74 using AttributeInfoVec= std::vector<AttributeInfo>;
75 std::vector<AttributeInfoVec> m_attributesInfo;
76 std::vector<std::vector<StateVariant>> m_renderStates;
77};
78
79class Q_AUTOTEST_EXPORT RHIComputePipelineManager
80 : public Qt3DCore::QResourceManager<RHIComputePipeline, ComputePipelineIdentifier,
81 Qt3DCore::NonLockingPolicy>
82{
83public:
84 RHIComputePipelineManager() { }
85
86 void releasePipelinesReferencingShader(const Qt3DCore::QNodeId &shaderId);
87};
88
89class Q_AUTOTEST_EXPORT RHIResourceManagers
90{
91public:
92 RHIResourceManagers();
93 ~RHIResourceManagers();
94
95 inline RHIShaderManager *rhiShaderManager() const noexcept { return m_rhiShaderManager; }
96 inline RHITextureManager *rhiTextureManager() const noexcept { return m_rhiTextureManager; }
97 inline RHIBufferManager *rhiBufferManager() const noexcept { return m_rhiBufferManager; }
98 inline RHIRenderTargetManager *rhiRenderTargetManager() const noexcept { return m_rhiRenderTargetManager; }
99 inline RHIGraphicsPipelineManager *rhiGraphicsPipelineManager() const noexcept
100 {
101 return m_rhiGraphicsPipelineManager;
102 }
103 inline RHIComputePipelineManager *rhiComputePipelineManager() const noexcept
104 {
105 return m_rhiComputePipelineManager;
106 }
107
108 void releaseAllResources();
109
110private:
111 RHIBufferManager *m_rhiBufferManager;
112 RHIShaderManager *m_rhiShaderManager;
113 RHITextureManager *m_rhiTextureManager;
114 RHIRenderTargetManager *m_rhiRenderTargetManager;
115 RHIGraphicsPipelineManager *m_rhiGraphicsPipelineManager;
116 RHIComputePipelineManager *m_rhiComputePipelineManager;
117};
118
119inline size_t qHash(const GraphicsPipelineIdentifier &key, size_t seed = 0)
120{
121 const QPair<int, Qt3DCore::QNodeId> p = { key.geometryLayoutKey, key.shader };
122 using QT_PREPEND_NAMESPACE(qHash);
123 seed = qHash(key: p, seed);
124 seed = qHash(id: key.renderTarget, seed);
125 seed = qHash(key: key.renderStatesKey, seed);
126 seed = qHash(e: key.primitiveType, seed);
127 return seed;
128}
129
130inline bool operator==(const GraphicsPipelineIdentifier &a, const GraphicsPipelineIdentifier &b)
131{
132 return a.geometryLayoutKey == b.geometryLayoutKey &&
133 a.shader == b.shader &&
134 a.renderTarget == b.renderTarget &&
135 a.renderStatesKey == b.renderStatesKey &&
136 a.primitiveType == b.primitiveType;
137}
138
139inline size_t qHash(const ComputePipelineIdentifier &key, size_t seed = 0)
140{
141 using QT_PREPEND_NAMESPACE(qHash);
142 seed = qHash(id: key.shader, seed);
143 seed = qHash(key: key.renderViewIndex, seed);
144 return seed;
145}
146
147inline bool operator==(const ComputePipelineIdentifier &a, const ComputePipelineIdentifier &b)
148{
149 return a.shader == b.shader &&
150 a.renderViewIndex == b.renderViewIndex;
151}
152
153} // Rhi
154
155} // Render
156
157} // Qt3DRender
158
159Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIGraphicsPipeline, Q_REQUIRES_CLEANUP)
160Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIComputePipeline, Q_REQUIRES_CLEANUP)
161Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHITexture, Q_REQUIRES_CLEANUP)
162Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIBuffer, Q_REQUIRES_CLEANUP)
163Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIRenderTarget, Q_REQUIRES_CLEANUP)
164Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIShader, Q_REQUIRES_CLEANUP)
165QT_END_NAMESPACE
166
167#endif // QT3DRENDER_RENDER_RHI_RHIRESOURCEMANAGERS_P_H
168

source code of qt3d/src/plugins/renderers/rhi/managers/rhiresourcemanagers_p.h