summaryrefslogtreecommitdiffstats
path: root/src/render/texture/texture.cpp
blob: 5b33b86276a46a89c32712ca4aaed51bb8c54744 (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
// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "texture_p.h"

#include <QDebug>
#include <QOpenGLFunctions>
#include <QOpenGLTexture>

#include <Qt3DRender/private/texture_p.h>
#include <Qt3DRender/private/qabstracttexture_p.h>
#include <Qt3DRender/private/managers_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {
namespace Render {

using namespace Qt3DCore;

Texture::Texture()
    // We need backend -> frontend notifications to update the status of the texture
    : BackendNode(ReadWrite)
    , m_dirty(DirtyImageGenerators|DirtyProperties|DirtyParameters|DirtyDataGenerator)
    , m_sharedTextureId(-1)
{
}

Texture::~Texture()
{
    // We do not abandon the api texture
    // because if the dtor is called that means
    // the manager was destroyed otherwise cleanup
    // would have been called
}

void Texture::addDirtyFlag(DirtyFlags flags)
{
    QMutexLocker lock(&m_flagsMutex);
    m_dirty |= flags;
    if (m_renderer)
        markDirty(AbstractRenderer::TexturesDirty);
}

Texture::DirtyFlags Texture::dirtyFlags()
{
    QMutexLocker lock(&m_flagsMutex);
    return m_dirty;
}

void Texture::unsetDirty()
{
    QMutexLocker lock(&m_flagsMutex);
    m_dirty = Texture::NotDirty;
}

// This is called by Renderer::updateGLResources
// when the texture has been marked for cleanup
void Texture::cleanup()
{
    // Whoever calls this must make sure to also check if this
    // texture is being referenced by a shared API specific texture (GLTexture)
    m_dataFunctor.reset();
    m_textureImageIds.clear();
    m_pendingTextureDataUpdates.clear();
    m_sharedTextureId = -1;

    // set default values
    m_properties = {};
    m_parameters = {};

    m_dirty = NotDirty;
}

void Texture::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
{
    BackendNode::syncFromFrontEnd(frontEnd, firstTime);
    const QAbstractTexture *node = qobject_cast<const QAbstractTexture *>(frontEnd);
    if (!node)
        return;

    TextureProperties p = m_properties;
    p.width = node->width();
    p.height = node->height();
    p.depth = node->depth();
    p.format = node->format();
    p.target = node->target();
    p.generateMipMaps = node->generateMipMaps();
    p.layers = node->layers();
    p.samples = node->samples();
    p.mipLevels = node->mipLevels();
    if (p != m_properties) {
        m_properties = p;
        addDirtyFlag(DirtyProperties);
    }

    TextureParameters q = m_parameters;
    q.magnificationFilter = node->magnificationFilter();
    q.minificationFilter = node->minificationFilter();
    q.wrapModeX = const_cast<QAbstractTexture *>(node)->wrapMode()->x();
    q.wrapModeY = const_cast<QAbstractTexture *>(node)->wrapMode()->y();
    q.wrapModeZ = const_cast<QAbstractTexture *>(node)->wrapMode()->z();
    q.maximumAnisotropy = node->maximumAnisotropy();
    q.comparisonFunction = node->comparisonFunction();
    q.comparisonMode = node->comparisonMode();
    if (q != m_parameters) {
        m_parameters = q;
        addDirtyFlag(DirtyParameters);
    }

    QAbstractTexturePrivate *dnode = static_cast<QAbstractTexturePrivate *>(QAbstractTexturePrivate::get(const_cast<QAbstractTexture *>(node)));
    auto newGenerator = dnode->dataFunctor();
    if (newGenerator != m_dataFunctor) {
        setDataGenerator(newGenerator);
        QAbstractTexturePrivate *dTexture = static_cast<QAbstractTexturePrivate *>(QNodePrivate::get(const_cast<QNode *>(frontEnd)));
        dTexture->setStatus(QAbstractTexture::Loading);
    }

    if (dnode) {
        for (const QTextureDataUpdate &pendingUpdate : dnode->m_pendingDataUpdates)
            addTextureDataUpdate(pendingUpdate);
        dnode->m_pendingDataUpdates.clear();

        auto ids = Qt3DCore::qIdsForNodes(dnode->m_textureImages);
        std::sort(std::begin(ids), std::end(ids));
        if (ids != m_textureImageIds) {
            m_textureImageIds = ids;
            addDirtyFlag(DirtyImageGenerators);
        }

        if (dnode->m_sharedTextureId != m_sharedTextureId) {
            m_sharedTextureId = dnode->m_sharedTextureId;
            addDirtyFlag(DirtySharedTextureId);
        }
    }
}

// Called by syncFromFrontend or TextureDownloadRequest (both in AspectThread context)
void Texture::setDataGenerator(const QTextureGeneratorPtr &generator)
{
    m_dataFunctor = generator;
    addDirtyFlag(DirtyDataGenerator);
}

bool Texture::isValid(TextureImageManager *manager) const
{
    for (const QNodeId &id : m_textureImageIds) {
        TextureImage *img = manager->lookupResource(id);
        if (img == nullptr)
            return false;
    }
    return true;
}

void Texture::addTextureDataUpdate(const QTextureDataUpdate &update)
{
    m_pendingTextureDataUpdates.push_back(update);
    addDirtyFlag(DirtyPendingDataUpdates);
}


TextureFunctor::TextureFunctor(AbstractRenderer *renderer,
                               TextureManager *textureNodeManager)
    : m_renderer(renderer)
    , m_textureNodeManager(textureNodeManager)
{
}

Qt3DCore::QBackendNode *TextureFunctor::create(Qt3DCore::QNodeId id) const
{
    Texture *backend = m_textureNodeManager->getOrCreateResource(id);
    backend->setRenderer(m_renderer);
    // Remove id from cleanupList if for some reason we were in the dirty list of texture
    // (Can happen when a node destroyed is followed by a node created change
    // in the same loop, when changing parent for instance)
    m_textureNodeManager->removeTextureIdToCleanup(id);
    return backend;
}

Qt3DCore::QBackendNode *TextureFunctor::get(Qt3DCore::QNodeId id) const
{
    return m_textureNodeManager->lookupResource(id);
}

void TextureFunctor::destroy(Qt3DCore::QNodeId id) const
{
    m_textureNodeManager->addTextureIdToCleanup(id);
    // We add ourselves to the dirty list to tell the shared texture managers
    // in the renderer that this texture has been destroyed
    m_textureNodeManager->releaseResource(id);
}


} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE