| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Paul Lemire | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "materialparametergathererjob_p.h" | 
| 41 | #include <Qt3DRender/private/nodemanagers_p.h> | 
| 42 | #include <Qt3DRender/private/managers_p.h> | 
| 43 | #include <Qt3DRender/private/renderpassfilternode_p.h> | 
| 44 | #include <Qt3DRender/private/techniquefilternode_p.h> | 
| 45 | #include <Qt3DRender/private/job_common_p.h> | 
| 46 |  | 
| 47 | QT_BEGIN_NAMESPACE | 
| 48 |  | 
| 49 | namespace Qt3DRender { | 
| 50 |  | 
| 51 | namespace Render { | 
| 52 |  | 
| 53 | namespace OpenGL { | 
| 54 |  | 
| 55 | namespace { | 
| 56 |  | 
| 57 | int materialParameterGathererCounter = 0; | 
| 58 | const int likelyNumberOfParameters = 24; | 
| 59 |  | 
| 60 | } // anonymous | 
| 61 |  | 
| 62 | class MaterialParameterGathererJobPrivate : public Qt3DCore::QAspectJobPrivate | 
| 63 | { | 
| 64 | public: | 
| 65 |     MaterialParameterGathererJobPrivate(MaterialParameterGathererJob *q) : q_ptr(q) { } | 
| 66 |     ~MaterialParameterGathererJobPrivate() override = default; | 
| 67 |  | 
| 68 |     bool isRequired() const override; | 
| 69 |     void postFrame(Qt3DCore::QAspectManager *manager) override; | 
| 70 |  | 
| 71 |     MaterialParameterGathererJob *q_ptr; | 
| 72 |     Q_DECLARE_PUBLIC(MaterialParameterGathererJob) | 
| 73 | }; | 
| 74 |  | 
| 75 | bool MaterialParameterGathererJobPrivate::isRequired() const | 
| 76 | { | 
| 77 |     return !q_ptr->m_handles.empty(); | 
| 78 | } | 
| 79 |  | 
| 80 | void MaterialParameterGathererJobPrivate::postFrame(Qt3DCore::QAspectManager *manager) | 
| 81 | { | 
| 82 |     Q_UNUSED(manager) | 
| 83 |     materialParameterGathererCounter = 0; | 
| 84 | } | 
| 85 |  | 
| 86 | MaterialParameterGathererJob::MaterialParameterGathererJob() | 
| 87 |     : Qt3DCore::QAspectJob(*new MaterialParameterGathererJobPrivate(this)) | 
| 88 |     , m_manager(nullptr) | 
| 89 |     , m_techniqueFilter(nullptr) | 
| 90 |     , m_renderPassFilter(nullptr) | 
| 91 | { | 
| 92 |     SET_JOB_RUN_STAT_TYPE(this, JobTypes::MaterialParameterGathering, materialParameterGathererCounter++) | 
| 93 | } | 
| 94 |  | 
| 95 | // TechniqueFilter / RenderPassFilter | 
| 96 |  | 
| 97 | // Parameters from Material/Effect/Technique | 
| 98 |  | 
| 99 | // Note: we could maybe improve that by having a smart update when we detect | 
| 100 | // that a parameter value has changed. That might require way more book keeping | 
| 101 | // which might make this solution a bit too complex | 
| 102 |  | 
| 103 | // The fact that this can now be performed in parallel should already provide a big | 
| 104 | // improvement | 
| 105 | void MaterialParameterGathererJob::run() | 
| 106 | { | 
| 107 |     for (const HMaterial &materialHandle : qAsConst(t&: m_handles)) { | 
| 108 |         Material *material = m_manager->materialManager()->data(handle: materialHandle); | 
| 109 |  | 
| 110 |         if (Q_UNLIKELY(!material->isEnabled())) | 
| 111 |             continue; | 
| 112 |  | 
| 113 |         Effect *effect = m_manager->effectManager()->lookupResource(id: material->effect()); | 
| 114 |         Technique *technique = findTechniqueForEffect(manager: m_manager, techniqueFilter: m_techniqueFilter, effect); | 
| 115 |  | 
| 116 |         if (Q_LIKELY(technique != nullptr)) { | 
| 117 |             RenderPassList passes = findRenderPassesForTechnique(manager: m_manager, passFilter: m_renderPassFilter, technique); | 
| 118 |             if (Q_LIKELY(passes.size() > 0)) { | 
| 119 |                 // Order set: | 
| 120 |                 // 1 Pass Filter | 
| 121 |                 // 2 Technique Filter | 
| 122 |                 // 3 Material | 
| 123 |                 // 4 Effect | 
| 124 |                 // 5 Technique | 
| 125 |                 // 6 RenderPass | 
| 126 |  | 
| 127 |                 // Add Parameters define in techniqueFilter and passFilter | 
| 128 |                 // passFilter have priority over techniqueFilter | 
| 129 |  | 
| 130 |                 ParameterInfoList parameters; | 
| 131 |                 // Doing the reserve allows a gain of 0.5ms on some of the demo examples | 
| 132 |                 parameters.reserve(asize: likelyNumberOfParameters); | 
| 133 |  | 
| 134 |                 if (m_renderPassFilter) | 
| 135 |                     parametersFromParametersProvider(infoList: ¶meters, manager: m_manager->parameterManager(), | 
| 136 |                                                      provider: m_renderPassFilter); | 
| 137 |                 if (m_techniqueFilter) | 
| 138 |                     parametersFromParametersProvider(infoList: ¶meters, manager: m_manager->parameterManager(), | 
| 139 |                                                      provider: m_techniqueFilter); | 
| 140 |                 // Get the parameters for our selected rendering setup (override what was defined in the technique/pass filter) | 
| 141 |                 parametersFromMaterialEffectTechnique(infoList: ¶meters, manager: m_manager->parameterManager(), material, effect, technique); | 
| 142 |  | 
| 143 |                 for (RenderPass *renderPass : passes) { | 
| 144 |                     ParameterInfoList globalParameters = parameters; | 
| 145 |                     parametersFromParametersProvider(infoList: &globalParameters, manager: m_manager->parameterManager(), provider: renderPass); | 
| 146 |                     auto it = m_parameters.find(akey: material->peerId()); | 
| 147 |                     if (it != m_parameters.end()) | 
| 148 |                         it->push_back(t: {.pass: renderPass, .parameterInfo: globalParameters}); | 
| 149 |                     else | 
| 150 |                         m_parameters.insert(akey: material->peerId(), avalue: {{.pass: renderPass, .parameterInfo: globalParameters}}); | 
| 151 |                 } | 
| 152 |             } | 
| 153 |         } | 
| 154 |     } | 
| 155 | } | 
| 156 |  | 
| 157 | } // OpenGL | 
| 158 |  | 
| 159 | } // Render | 
| 160 |  | 
| 161 | } // Qt3DRender | 
| 162 |  | 
| 163 | QT_END_NAMESPACE | 
| 164 |  |