| 1 | // Copyright (C) 2008-2012 NVIDIA Corporation. |
| 2 | // Copyright (C) 2019 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #include "qssgrendercontextcore.h" |
| 6 | #include <QtQuick3DRuntimeRender/private/qssgrendernode_p.h> |
| 7 | #include <QtQuick3DRuntimeRender/private/qssgrenderbuffermanager_p.h> |
| 8 | #include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h> |
| 9 | #include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h> |
| 10 | #include <QtQuick3DRuntimeRender/private/qssgrendershaderlibrarymanager_p.h> |
| 11 | #include <QtQuick3DRuntimeRender/private/qssgrendershadercodegenerator_p.h> |
| 12 | #include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterialshadergenerator_p.h> |
| 13 | #include <QtQuick3DRuntimeRender/private/qssgrhicustommaterialsystem_p.h> |
| 14 | #include <QtQuick3DRuntimeRender/private/qssgperframeallocator_p.h> |
| 15 | #include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h> |
| 16 | #include <QtQuick3DRuntimeRender/private/qssgrendererutil_p.h> |
| 17 | #include <QtQuick3DRuntimeRender/private/qssgdebugdrawsystem_p.h> |
| 18 | |
| 19 | #include <QtQuick/private/qquickwindow_p.h> |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | /*! |
| 24 | \class QSSGRenderContextInterface |
| 25 | \inmodule QtQuick3D |
| 26 | \since 6.7 |
| 27 | |
| 28 | \brief Aggregate class for sub-parts of the QtQuick3D rendering engine. |
| 29 | |
| 30 | The QSSGRenderContextInterface, and the objects owned by it are always |
| 31 | per-QQuickWindow, and so per scenegraph render thread. Some resources might |
| 32 | be shared, like the shader library, but that's all take care of internally |
| 33 | by the QSSGRenderContextInterface. |
| 34 | |
| 35 | \note Some sub-components might not be exposed as semi-public, or their use might require |
| 36 | private headers to be used. In those cases it's likely their APIs have a high likelihood |
| 37 | of being changed in the near future. One the APIs for those class have stabilized they will |
| 38 | be made available with the same guarantee as the rest of the semi-public APIs. |
| 39 | */ |
| 40 | |
| 41 | static bool loadPregenratedShaders() |
| 42 | { |
| 43 | return qEnvironmentVariableIntValue(varName: "QT_QUICK3D_DISABLE_GENSHADERS" ) == 0; |
| 44 | } |
| 45 | |
| 46 | void QSSGRenderContextInterface::init() |
| 47 | { |
| 48 | if (m_renderer) |
| 49 | QSSGRendererPrivate::setRenderContextInterface(renderer&: *m_renderer, ctx: this); |
| 50 | |
| 51 | if (m_bufferManager) |
| 52 | m_bufferManager->setRenderContextInterface(this); |
| 53 | |
| 54 | if (m_customMaterialSystem) |
| 55 | m_customMaterialSystem->setRenderContextInterface(this); |
| 56 | if (m_shaderLibraryManager && loadPregenratedShaders()) |
| 57 | m_shaderLibraryManager->loadPregeneratedShaderInfo(); |
| 58 | } |
| 59 | |
| 60 | void QSSGRenderContextInterface::releaseCachedResources() |
| 61 | { |
| 62 | if (m_renderer) |
| 63 | m_renderer->releaseCachedResources(); |
| 64 | if (m_shaderCache) |
| 65 | m_shaderCache->releaseCachedResources(); |
| 66 | if (m_customMaterialSystem) |
| 67 | m_customMaterialSystem->releaseCachedResources(); |
| 68 | if (m_bufferManager) |
| 69 | m_bufferManager->releaseCachedResources(); |
| 70 | if (m_rhiContext) |
| 71 | QSSGRhiContextPrivate::get(q: m_rhiContext.get())->releaseCachedResources(); |
| 72 | } |
| 73 | |
| 74 | const std::unique_ptr<QSSGPerFrameAllocator> &QSSGRenderContextInterface::perFrameAllocator() const |
| 75 | { |
| 76 | return m_perFrameAllocator; |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | \internal |
| 81 | */ |
| 82 | QSSGRenderContextInterface::QSSGRenderContextInterface(std::unique_ptr<QSSGBufferManager> bufferManager, |
| 83 | std::unique_ptr<QSSGRenderer> renderer, |
| 84 | std::shared_ptr<QSSGShaderLibraryManager> shaderLibraryManager, |
| 85 | std::unique_ptr<QSSGShaderCache> shaderCache, |
| 86 | std::unique_ptr<QSSGCustomMaterialSystem> customMaterialSystem, |
| 87 | std::unique_ptr<QSSGProgramGenerator> shaderProgramGenerator, |
| 88 | std::unique_ptr<QSSGRhiContext> ctx, |
| 89 | std::unique_ptr<QSSGDebugDrawSystem> debugDrawSystem) |
| 90 | : m_rhiContext(std::move(ctx)) |
| 91 | , m_shaderCache(std::move(shaderCache)) |
| 92 | , m_bufferManager(std::move(bufferManager)) |
| 93 | , m_renderer(std::move(renderer)) |
| 94 | , m_shaderLibraryManager(std::move(shaderLibraryManager)) |
| 95 | , m_customMaterialSystem(std::move(customMaterialSystem)) |
| 96 | , m_shaderProgramGenerator(std::move(shaderProgramGenerator)) |
| 97 | , m_debugDrawSystem(std::move(debugDrawSystem)) |
| 98 | , m_perFrameAllocator(new QSSGPerFrameAllocator) |
| 99 | { |
| 100 | init(); |
| 101 | } |
| 102 | |
| 103 | // The shader library is a global object, not per-QQuickWindow, hence not owned |
| 104 | // by the QSSGRenderContextInterface. |
| 105 | static const std::shared_ptr<QSSGShaderLibraryManager> &q3ds_shaderLibraryManager() |
| 106 | { |
| 107 | static auto shaderLibraryManager = std::make_shared<QSSGShaderLibraryManager>(); |
| 108 | return shaderLibraryManager; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | \internal |
| 113 | */ |
| 114 | QSSGRenderContextInterface::QSSGRenderContextInterface(QRhi *rhi) |
| 115 | : m_rhiContext(new QSSGRhiContext(rhi)) |
| 116 | , m_shaderCache(new QSSGShaderCache(*m_rhiContext)) |
| 117 | , m_bufferManager(new QSSGBufferManager()) |
| 118 | , m_renderer(new QSSGRenderer()) |
| 119 | , m_shaderLibraryManager(q3ds_shaderLibraryManager()) |
| 120 | , m_customMaterialSystem(new QSSGCustomMaterialSystem()) |
| 121 | , m_shaderProgramGenerator(new QSSGProgramGenerator()) |
| 122 | , m_debugDrawSystem(new QSSGDebugDrawSystem()) |
| 123 | , m_perFrameAllocator(new QSSGPerFrameAllocator) |
| 124 | { |
| 125 | init(); |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | \internal |
| 130 | */ |
| 131 | QSSGRenderContextInterface::~QSSGRenderContextInterface() |
| 132 | { |
| 133 | m_renderer->releaseCachedResources(); |
| 134 | } |
| 135 | |
| 136 | /*! |
| 137 | \internal |
| 138 | */ |
| 139 | const std::unique_ptr<QSSGRenderer> &QSSGRenderContextInterface::renderer() const |
| 140 | { |
| 141 | return m_renderer; |
| 142 | } |
| 143 | |
| 144 | /*! |
| 145 | \internal |
| 146 | */ |
| 147 | const std::unique_ptr<QSSGBufferManager> &QSSGRenderContextInterface::bufferManager() const |
| 148 | { |
| 149 | return m_bufferManager; |
| 150 | } |
| 151 | |
| 152 | /*! |
| 153 | \return the context object that wraps QRhi-related settings and helper functionality. |
| 154 | */ |
| 155 | const std::unique_ptr<QSSGRhiContext> &QSSGRenderContextInterface::rhiContext() const |
| 156 | { |
| 157 | return m_rhiContext; |
| 158 | } |
| 159 | |
| 160 | /*! |
| 161 | \internal |
| 162 | */ |
| 163 | const std::unique_ptr<QSSGShaderCache> &QSSGRenderContextInterface::shaderCache() const |
| 164 | { |
| 165 | return m_shaderCache; |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | \internal |
| 170 | */ |
| 171 | const std::shared_ptr<QSSGShaderLibraryManager> &QSSGRenderContextInterface::shaderLibraryManager() const |
| 172 | { |
| 173 | return m_shaderLibraryManager; |
| 174 | } |
| 175 | |
| 176 | /*! |
| 177 | \internal |
| 178 | */ |
| 179 | const std::unique_ptr<QSSGCustomMaterialSystem> &QSSGRenderContextInterface::customMaterialSystem() const |
| 180 | { |
| 181 | return m_customMaterialSystem; |
| 182 | } |
| 183 | |
| 184 | /*! |
| 185 | \internal |
| 186 | */ |
| 187 | const std::unique_ptr<QSSGProgramGenerator> &QSSGRenderContextInterface::shaderProgramGenerator() const |
| 188 | { |
| 189 | return m_shaderProgramGenerator; |
| 190 | } |
| 191 | |
| 192 | /*! |
| 193 | \internal |
| 194 | */ |
| 195 | const std::unique_ptr<QSSGDebugDrawSystem> &QSSGRenderContextInterface::debugDrawSystem() const |
| 196 | { |
| 197 | return m_debugDrawSystem; |
| 198 | } |
| 199 | |
| 200 | QRhi *QSSGRenderContextInterface::rhi() const |
| 201 | { |
| 202 | return m_rhiContext->rhi(); |
| 203 | } |
| 204 | |
| 205 | QT_END_NAMESPACE |
| 206 | |
| 207 | |