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 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | namespace Qt3DRender { |
32 | |
33 | namespace Render { |
34 | |
35 | namespace Rhi { |
36 | |
37 | class Q_AUTOTEST_EXPORT RHIBufferManager |
38 | : public Qt3DCore::QResourceManager<RHIBuffer, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy> |
39 | { |
40 | }; |
41 | |
42 | class Q_AUTOTEST_EXPORT RHITextureManager |
43 | : public Qt3DCore::QResourceManager<RHITexture, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy> |
44 | { |
45 | public: |
46 | QHash<RHITexture *, Qt3DCore::QNodeId> texNodeIdForRHITexture; |
47 | }; |
48 | |
49 | class Q_AUTOTEST_EXPORT RHIRenderTargetManager |
50 | : public Qt3DCore::QResourceManager<RHIRenderTarget, Qt3DCore::QNodeId, Qt3DCore::NonLockingPolicy> |
51 | { |
52 | }; |
53 | |
54 | class Q_AUTOTEST_EXPORT RHIShaderManager : public APIShaderManager<RHIShader> |
55 | { |
56 | public: |
57 | explicit RHIShaderManager() : APIShaderManager<RHIShader>() { } |
58 | }; |
59 | |
60 | class Q_AUTOTEST_EXPORT RHIGraphicsPipelineManager |
61 | : public Qt3DCore::QResourceManager<RHIGraphicsPipeline, GraphicsPipelineIdentifier, |
62 | Qt3DCore::NonLockingPolicy> |
63 | { |
64 | public: |
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 | |
73 | private: |
74 | using AttributeInfoVec= std::vector<AttributeInfo>; |
75 | std::vector<AttributeInfoVec> m_attributesInfo; |
76 | std::vector<std::vector<StateVariant>> m_renderStates; |
77 | }; |
78 | |
79 | class Q_AUTOTEST_EXPORT RHIComputePipelineManager |
80 | : public Qt3DCore::QResourceManager<RHIComputePipeline, ComputePipelineIdentifier, |
81 | Qt3DCore::NonLockingPolicy> |
82 | { |
83 | public: |
84 | RHIComputePipelineManager() { } |
85 | |
86 | void releasePipelinesReferencingShader(const Qt3DCore::QNodeId &shaderId); |
87 | }; |
88 | |
89 | class Q_AUTOTEST_EXPORT RHIResourceManagers |
90 | { |
91 | public: |
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 | |
110 | private: |
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 | |
119 | inline 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 | |
130 | inline 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 | |
139 | inline 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 | |
147 | inline 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 | |
159 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIGraphicsPipeline, Q_REQUIRES_CLEANUP) |
160 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIComputePipeline, Q_REQUIRES_CLEANUP) |
161 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHITexture, Q_REQUIRES_CLEANUP) |
162 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIBuffer, Q_REQUIRES_CLEANUP) |
163 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIRenderTarget, Q_REQUIRES_CLEANUP) |
164 | Q_DECLARE_RESOURCE_INFO(Qt3DRender::Render::Rhi::RHIShader, Q_REQUIRES_CLEANUP) |
165 | QT_END_NAMESPACE |
166 | |
167 | #endif // QT3DRENDER_RENDER_RHI_RHIRESOURCEMANAGERS_P_H |
168 | |