summaryrefslogtreecommitdiffstats
path: root/src/render/jobs/rendersyncjobs_p.h
blob: 2307e509f65307b0ad096829c0ffc68ab018ead9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/****************************************************************************
**
** Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT3DRENDER_RENDER_RENDERSYNCJOBS_H
#define QT3DRENDER_RENDER_RENDERSYNCJOBS_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <Qt3DCore/qaspectjob.h>
#include <Qt3DRender/private/renderviewinitializerjob_p.h>
#include <Qt3DRender/private/frustumcullingjob_p.h>
#include <Qt3DRender/private/filterlayerentityjob_p.h>
#include <Qt3DRender/private/filterproximitydistancejob_p.h>
#include <Qt3DRender/private/materialparametergathererjob_p.h>
#include <Qt3DRender/private/renderviewcommandbuilderjob_p.h>
#include <Qt3DRender/private/renderviewcommandupdaterjob_p.h>
#include <Qt3DRender/private/renderercache_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

namespace Render {

enum RebuildFlag {
    FullCommandRebuild = 1 << 0,
    LayerCacheRebuild = 1 << 1,
    MaterialCacheRebuild = 1 << 2,
    LightCacheRebuild = 1 << 3
};
Q_DECLARE_FLAGS(RebuildFlagSet, RebuildFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(RebuildFlagSet)

#define RenderViewInitializerJobPtrAlias RenderViewInitializerJobPtr<RenderView, Renderer>
#define RenderViewCommandBuilderJobPtrAlias RenderViewCommandBuilderJobPtr<RenderView, RenderCommand>
#define RenderViewCommandUpdaterJobPtrAlias RenderViewCommandUpdaterJobPtr<RenderView, RenderCommand>

template<class RenderView, class Renderer, class RenderCommand>
class SyncPreCommandBuilding
{
public:
    explicit SyncPreCommandBuilding(RenderViewInitializerJobPtrAlias renderViewInitializerJob,
                                    const std::vector<RenderViewCommandBuilderJobPtrAlias> &renderViewCommandBuilderJobs,
                                    Renderer *renderer,
                                    FrameGraphNode *leafNode)
        : m_renderViewInitializer(renderViewInitializerJob)
        , m_renderViewCommandBuilderJobs(renderViewCommandBuilderJobs)
        , m_renderer(renderer)
        , m_leafNode(leafNode)
    {
    }

    void operator()()
    {
        // Split commands to build among jobs

        // Rebuild RenderCommands for all entities in RV (ignoring filtering)
        auto *cache = m_renderer->cache();
        QMutexLocker lock(cache->mutex());

        Q_ASSERT(cache->leafNodeCache.contains(m_leafNode));
        // The cache leaf should already have been created so we don't need to protect the access
        const auto &dataCacheForLeaf = cache->leafNodeCache[m_leafNode];
        RenderView *rv = m_renderViewInitializer->renderView();
        const auto &entities = !rv->isCompute() ? cache->renderableEntities : cache->computeEntities;

        rv->setMaterialParameterTable(dataCacheForLeaf.materialParameterGatherer);

        // Split among the ideal number of command builders
        const int jobCount = int(m_renderViewCommandBuilderJobs.size());
        const int entityCount = int(entities.size());
        const int idealPacketSize = std::min(std::max(10, entityCount / jobCount), entityCount);
        // Try to split work into an ideal number of workers
        const int m = findIdealNumberOfWorkers(entityCount, idealPacketSize, jobCount);

        const Entity **entitiesPtr = const_cast<const Entity **>(entities.data());
        for (int i = 0; i < m; ++i) {
            const auto &renderViewCommandBuilder = m_renderViewCommandBuilderJobs[i];
            const int count = (i == m - 1) ? entityCount - (i * idealPacketSize) : idealPacketSize;
            renderViewCommandBuilder->setEntities(entitiesPtr, i * idealPacketSize, count);
        }
    }

private:
    RenderViewInitializerJobPtrAlias m_renderViewInitializer;
    std::vector<RenderViewCommandBuilderJobPtrAlias> m_renderViewCommandBuilderJobs;
    Renderer *m_renderer;
    FrameGraphNode *m_leafNode;
};

template<class RenderView, class Renderer, class RenderCommand>
class SyncRenderViewPostCommandUpdate
{
public:
    explicit SyncRenderViewPostCommandUpdate(const RenderViewInitializerJobPtrAlias &renderViewJob,
                                             const std::vector<RenderViewCommandUpdaterJobPtrAlias> &renderViewCommandUpdateJobs,
                                             Renderer *renderer)
        : m_renderViewJob(renderViewJob)
        , m_renderViewCommandUpdaterJobs(renderViewCommandUpdateJobs)
        , m_renderer(renderer)
    {
    }

    void operator()()
    {
        // Append all the commands and sort them
        RenderView *rv = m_renderViewJob->renderView();

        if (!rv->noDraw()) {
            // Sort command on RenderView
            rv->sort();
        }
        // Enqueue our fully populated RenderView with the RenderThread
        m_renderer->enqueueRenderView(rv, m_renderViewJob->submitOrderIndex());
    }

private:
    RenderViewInitializerJobPtrAlias m_renderViewJob;
    std::vector<RenderViewCommandUpdaterJobPtrAlias> m_renderViewCommandUpdaterJobs;
    Renderer *m_renderer;
};

template<class RenderView, class Renderer>
class SyncPreFrustumCulling
{
public:
    explicit SyncPreFrustumCulling(const RenderViewInitializerJobPtrAlias &renderViewJob,
                                   const FrustumCullingJobPtr &frustumCulling)
        : m_renderViewJob(renderViewJob)
        , m_frustumCullingJob(frustumCulling)
    {}

    void operator()()
    {
        RenderView *rv = m_renderViewJob->renderView();

        // Update matrices now that all transforms have been updated
        rv->updateMatrices();

        // Frustum culling
        m_frustumCullingJob->setViewProjection(rv->viewProjectionMatrix());
    }

private:
    RenderViewInitializerJobPtrAlias m_renderViewJob;
    FrustumCullingJobPtr m_frustumCullingJob;
};

template<class RenderView, class Renderer, class RenderCommand>
class SyncRenderViewPostInitialization
{
public:
    explicit SyncRenderViewPostInitialization(const RenderViewInitializerJobPtrAlias &renderViewJob,
                                              const FrustumCullingJobPtr &frustumCullingJob,
                                              const FilterLayerEntityJobPtr &filterEntityByLayerJob,
                                              const FilterProximityDistanceJobPtr &filterProximityJob,
                                              const std::vector<MaterialParameterGathererJobPtr> &materialGathererJobs,
                                              const std::vector<RenderViewCommandUpdaterJobPtrAlias> &renderViewCommandUpdaterJobs,
                                              const std::vector<RenderViewCommandBuilderJobPtrAlias> &renderViewCommandBuilderJobs)
        : m_renderViewJob(renderViewJob)
        , m_frustumCullingJob(frustumCullingJob)
        , m_filterEntityByLayerJob(filterEntityByLayerJob)
        , m_filterProximityJob(filterProximityJob)
        , m_materialGathererJobs(materialGathererJobs)
        , m_renderViewCommandUpdaterJobs(renderViewCommandUpdaterJobs)
        , m_renderViewCommandBuilderJobs(renderViewCommandBuilderJobs)
    {}

    void operator()()
    {
        RenderView *rv = m_renderViewJob->renderView();

        // Layer filtering
        if (!m_filterEntityByLayerJob.isNull())
            m_filterEntityByLayerJob->setLayerFilters(rv->layerFilters());

        // Proximity filtering
        m_filterProximityJob->setProximityFilterIds(rv->proximityFilterIds());

        // Material Parameter building
        for (const auto &materialGatherer : m_materialGathererJobs) {
            materialGatherer->setRenderPassFilter(const_cast<RenderPassFilter *>(rv->renderPassFilter()));
            materialGatherer->setTechniqueFilter(const_cast<TechniqueFilter *>(rv->techniqueFilter()));
        }

        // Command builders and updates
        for (const auto &renderViewCommandUpdater : m_renderViewCommandUpdaterJobs)
            renderViewCommandUpdater->setRenderView(rv);
        for (const auto &renderViewCommandBuilder : m_renderViewCommandBuilderJobs)
            renderViewCommandBuilder->setRenderView(rv);

        // Set whether frustum culling is enabled or not
        m_frustumCullingJob->setActive(rv->frustumCulling());
    }

private:
    RenderViewInitializerJobPtrAlias m_renderViewJob;
    FrustumCullingJobPtr m_frustumCullingJob;
    FilterLayerEntityJobPtr m_filterEntityByLayerJob;
    FilterProximityDistanceJobPtr m_filterProximityJob;
    std::vector<MaterialParameterGathererJobPtr> m_materialGathererJobs;
    std::vector<RenderViewCommandUpdaterJobPtrAlias> m_renderViewCommandUpdaterJobs;
    std::vector<RenderViewCommandBuilderJobPtrAlias> m_renderViewCommandBuilderJobs;
};

template<class RenderView, class Renderer, class RenderCommand>
class SyncRenderViewPreCommandUpdate
{
public:
    explicit SyncRenderViewPreCommandUpdate(const RenderViewInitializerJobPtrAlias &renderViewJob,
                                            const FrustumCullingJobPtr &frustumCullingJob,
                                            const FilterProximityDistanceJobPtr &filterProximityJob,
                                            const std::vector<MaterialParameterGathererJobPtr> &materialGathererJobs,
                                            const std::vector<RenderViewCommandUpdaterJobPtrAlias> &renderViewCommandUpdaterJobs,
                                            const std::vector<RenderViewCommandBuilderJobPtrAlias> &renderViewCommandBuilderJobs,
                                            Renderer *renderer,
                                            FrameGraphNode *leafNode,
                                            RebuildFlagSet rebuildFlags)
        : m_renderViewJob(renderViewJob)
        , m_frustumCullingJob(frustumCullingJob)
        , m_filterProximityJob(filterProximityJob)
        , m_materialGathererJobs(materialGathererJobs)
        , m_renderViewCommandUpdaterJobs(renderViewCommandUpdaterJobs)
        , m_renderViewCommandBuilderJobs(renderViewCommandBuilderJobs)
        , m_renderer(renderer)
        , m_leafNode(leafNode)
        , m_rebuildFlags(rebuildFlags)
    {}

    void operator()()
    {
        // Set the result of previous job computations
        // for final RenderCommand building
        RenderView *rv = m_renderViewJob->renderView();

        if (!rv->noDraw()) {
            ///////// CACHE LOCKED ////////////
            // Retrieve Data from Cache
            auto *cache = m_renderer->cache();
            QMutexLocker lock(cache->mutex());
            Q_ASSERT(cache->leafNodeCache.contains(m_leafNode));

            // We don't need to protect the cache access as
            // 1) The cache leaf is created
            // 2) We are only reading conccurently the cache values that are shared across all RV
            // 3) Each instance of this job is reading and writing in its own cache leaf so there's
            // no conflict

            const bool isDraw = !rv->isCompute();
            auto &cacheForLeaf = cache->leafNodeCache[m_leafNode];

            const bool fullRebuild = m_rebuildFlags.testFlag(RebuildFlag::FullCommandRebuild);
            const bool layerFilteringRebuild = m_rebuildFlags.testFlag(RebuildFlag::LayerCacheRebuild);
            const bool lightsCacheRebuild = m_rebuildFlags.testFlag(RebuildFlag::LightCacheRebuild);
            const bool cameraDirty = cacheForLeaf.viewProjectionMatrix != rv->viewProjectionMatrix();
            const bool hasProximityFilter = !rv->proximityFilterIds().empty();
            bool commandFilteringRequired =
                    fullRebuild ||
                    layerFilteringRebuild ||
                    lightsCacheRebuild ||
                    cameraDirty ||
                    hasProximityFilter;

            // If we have no filteredRenderCommandDataViews then we should have fullRebuild set to true
            // otherwise something is wrong
            Q_ASSERT(fullRebuild || cacheForLeaf.filteredRenderCommandDataViews);

            // Rebuild RenderCommands if required
            // This should happen fairly infrequently (FrameGraph Change, Geometry/Material change)
            // and allow to skip that step most of the time
            if (fullRebuild) {
                EntityRenderCommandData<RenderCommand> commandData;
                // Reduction
                {
                    int totalCommandCount = 0;
                    for (const RenderViewCommandBuilderJobPtrAlias &renderViewCommandBuilder : qAsConst(m_renderViewCommandBuilderJobs))
                        totalCommandCount += int(renderViewCommandBuilder->commandData().size());
                    commandData.reserve(totalCommandCount);
                    for (const RenderViewCommandBuilderJobPtrAlias &renderViewCommandBuilder : qAsConst(m_renderViewCommandBuilderJobs))
                        commandData += std::move(renderViewCommandBuilder->commandData());
                }

                // Store new cache
                auto dataView = EntityRenderCommandDataViewPtr<RenderCommand>::create();
                dataView->data = std::move(commandData);
                // Store the update dataView
                cacheForLeaf.filteredRenderCommandDataViews = dataView;
            }


            // Should be fairly infrequent
            if (layerFilteringRebuild || fullRebuild) {
                // Filter out renderable entities that weren't selected by the layer filters and store that in cache
                cacheForLeaf.layeredFilteredRenderables = entitiesInSubset(
                            isDraw ? cache->renderableEntities : cache->computeEntities,
                            cacheForLeaf.filterEntitiesByLayer);
                // Set default value for filteredAndCulledRenderables
                if (isDraw)
                    cacheForLeaf.filteredAndCulledRenderables = cacheForLeaf.layeredFilteredRenderables;
            }

            // Should be fairly infrequent
            if (lightsCacheRebuild) {
                // Filter out light sources that weren't selected by the
                // layer filters and store that in cache
                const std::vector<Entity *> &layeredFilteredEntities = cacheForLeaf.filterEntitiesByLayer;
                std::vector<LightSource> filteredLightSources = cache->gatheredLights;

                auto it = filteredLightSources.begin();

                while (it != filteredLightSources.end()) {
                    if (!std::binary_search(layeredFilteredEntities.begin(),
                                            layeredFilteredEntities.end(),
                                            it->entity))
                        it = filteredLightSources.erase(it);
                    else
                        ++it;
                }
                cacheForLeaf.layeredFilteredLightSources = std::move(filteredLightSources);
            }

            // This is likely very frequent
            if (cameraDirty) {
                // Record the updated viewProjectionMatrix in the cache to allow check to be performed
                // next frame
                cacheForLeaf.viewProjectionMatrix = rv->viewProjectionMatrix();
            }

            // Filter out frustum culled entity for drawable entities and store in cache
            // We need to check this regardless of whether the camera has moved since
            // entities in the scene themselves could have moved
            if (isDraw && rv->frustumCulling()) {
                const std::vector<Entity *> &subset = entitiesInSubset(
                            cacheForLeaf.layeredFilteredRenderables,
                            m_frustumCullingJob->visibleEntities());
                // Force command filtering if what we contain in cache and what we filtered differ
                commandFilteringRequired |= (subset != cacheForLeaf.filteredAndCulledRenderables);
                cacheForLeaf.filteredAndCulledRenderables = subset;
            }

            rv->setMaterialParameterTable(cacheForLeaf.materialParameterGatherer);
            rv->setEnvironmentLight(cache->environmentLight);

            // Set the light sources, with layer filters applied.
            rv->setLightSources(cacheForLeaf.layeredFilteredLightSources);

            std::vector<Entity *> renderableEntities = isDraw ? cacheForLeaf.filteredAndCulledRenderables : cacheForLeaf.layeredFilteredRenderables;

            // TO DO: Find a way to do that only if proximity entities has changed
            if (isDraw) {
                // Filter out entities which didn't satisfy proximity filtering
                if (hasProximityFilter)
                    renderableEntities = entitiesInSubset(renderableEntities,
                                                          m_filterProximityJob->filteredEntities());
            }

            EntityRenderCommandDataViewPtr<RenderCommand> filteredCommandData = cacheForLeaf.filteredRenderCommandDataViews;

            // Set RenderCommandDataView on RV (will be used later on to sort commands ...)
            rv->setRenderCommandDataView(filteredCommandData);

            // Filter out Render commands for which the Entity wasn't selected because
            // of frustum, proximity or layer filtering
            if (commandFilteringRequired) {
                const std::vector<const Entity *> &entities = filteredCommandData->data.entities;
                // Because cacheForLeaf.renderableEntities or computeEntities are sorted
                // What we get out of EntityRenderCommandData is also sorted by Entity
                auto eIt = renderableEntities.cbegin();
                const auto eEnd = renderableEntities.cend();
                size_t cIt = 0;
                const size_t cEnd = entities.size();

                std::vector<size_t> filteredCommandIndices;
                filteredCommandIndices.reserve(renderableEntities.size());

                while (eIt != eEnd) {
                    const Entity *targetEntity = *eIt;
                    // Advance until we have commands whose Entity has a lower address
                    // than the selected filtered entity
                    while (cIt != cEnd && entities[cIt] < targetEntity)
                        ++cIt;

                    // Push pointers to command data for all commands that match the
                    // entity
                    while (cIt != cEnd && entities[cIt] == targetEntity) {
                        filteredCommandIndices.push_back(cIt);
                        ++cIt;
                    }
                    ++eIt;
                }

                // Store result in cache
                cacheForLeaf.filteredRenderCommandDataViews->indices = std::move(filteredCommandIndices);
            }

            // Split among the number of command updaters
            const int jobCount = int(m_renderViewCommandUpdaterJobs.size());
            const int commandCount = int(filteredCommandData->size());
            const int idealPacketSize = std::min(std::max(10, commandCount), commandCount);
            const int m = findIdealNumberOfWorkers(commandCount, idealPacketSize, jobCount);

            for (int i = 0; i < m; ++i) {
                // TO DO: Based on whether we had to update the commands
                // we should be able to know what needs to be recomputed
                // -> lights/standard uniforms ... might no have to be set over and over again
                // if they are identical
                const RenderViewCommandUpdaterJobPtrAlias &renderViewCommandUpdater = m_renderViewCommandUpdaterJobs.at(i);
                const size_t count = (i == m - 1) ? commandCount - (i * idealPacketSize) : idealPacketSize;
                renderViewCommandUpdater->setRenderablesSubView({filteredCommandData, size_t(i * idealPacketSize), count});
            }
        }
    }

private:
    RenderViewInitializerJobPtrAlias m_renderViewJob;
    FrustumCullingJobPtr m_frustumCullingJob;
    FilterProximityDistanceJobPtr m_filterProximityJob;
    std::vector<MaterialParameterGathererJobPtr> m_materialGathererJobs;
    std::vector<RenderViewCommandUpdaterJobPtrAlias> m_renderViewCommandUpdaterJobs;
    std::vector<RenderViewCommandBuilderJobPtrAlias> m_renderViewCommandBuilderJobs;
    Renderer *m_renderer;
    FrameGraphNode *m_leafNode;
    RebuildFlagSet m_rebuildFlags;
};

template<class Renderer>
class SyncFilterEntityByLayer
{
public:
    explicit SyncFilterEntityByLayer(const FilterLayerEntityJobPtr &filterEntityByLayerJob,
                                     Renderer *renderer,
                                     FrameGraphNode *leafNode)
        : m_filterEntityByLayerJob(filterEntityByLayerJob)
        , m_renderer(renderer)
        , m_leafNode(leafNode)
    {
    }

    void operator()()
    {
        QMutexLocker lock(m_renderer->cache()->mutex());
        Q_ASSERT(m_renderer->cache()->leafNodeCache.contains(m_leafNode));
        // The cache leaf should already have been created so we don't need to protect the access
        auto &dataCacheForLeaf = m_renderer->cache()->leafNodeCache[m_leafNode];
        // Save the filtered by layer subset into the cache
        dataCacheForLeaf.filterEntitiesByLayer = std::move(m_filterEntityByLayerJob->filteredEntities());
    }

private:
    FilterLayerEntityJobPtr m_filterEntityByLayerJob;
    Renderer *m_renderer;
    FrameGraphNode *m_leafNode;
};

template<class Renderer>
class SyncMaterialParameterGatherer
{
public:
    explicit SyncMaterialParameterGatherer(const std::vector<MaterialParameterGathererJobPtr> &materialParameterGathererJobs,
                                           Renderer *renderer,
                                           FrameGraphNode *leafNode)
        : m_materialParameterGathererJobs(materialParameterGathererJobs)
        , m_renderer(renderer)
        , m_leafNode(leafNode)
    {
    }

    void operator()()
    {
        // The cache leaf was created by SyncRenderViewPostInitialization on which we depend
        // so we don't need to protect the access
        QMutexLocker lock(m_renderer->cache()->mutex());
        auto &dataCacheForLeaf = m_renderer->cache()->leafNodeCache[m_leafNode];
        dataCacheForLeaf.materialParameterGatherer.clear();

        for (const auto &materialGatherer : m_materialParameterGathererJobs) {
            const MaterialParameterGathererData &source = materialGatherer->materialToPassAndParameter();
            for (auto it = std::begin(source); it != std::end(source); ++it) {
                Q_ASSERT(!dataCacheForLeaf.materialParameterGatherer.contains(it.key()));
                dataCacheForLeaf.materialParameterGatherer.insert(it.key(), it.value());
            }
        }
    }

private:
    std::vector<MaterialParameterGathererJobPtr> m_materialParameterGathererJobs;
    Renderer *m_renderer;
    FrameGraphNode *m_leafNode;
};

} // Render

} // Qt3DRender

QT_END_NAMESPACE

#endif // RENDERSYNCJOBS_H