summaryrefslogtreecommitdiffstats
path: root/src/runtimerender/Qt3DSRenderTextureAtlas.cpp
blob: 062e7d573df6fa3e72d0683262097650bf72f0d0 (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
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) 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.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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "Qt3DSRenderTextureAtlas.h"
#include "foundation/Qt3DSContainers.h"
#include "foundation/Qt3DSAtomic.h"
#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#include "render/Qt3DSRenderTexture2D.h"
#include "render/Qt3DSRenderContext.h"

using namespace qt3ds::render;

namespace {

// a algorithm based on http://clb.demon.fi/files/RectangleBinPack/
struct STextureAtlasBinPackSL
{
public:
    STextureAtlasBinPackSL(NVRenderContext &inContext, QT3DSI32 width, QT3DSI32 height)
        : m_BinWidth(width)
        , m_BinHeight(height)
        , m_SkyLine(inContext.GetAllocator(), "STextureAtlasBinPackSL::m_SkyLine")
    {
        // setup first entry
        SSkylineNode theNode = { 0, 0, width };
        m_SkyLine.push_back(theNode);
    }

    ~STextureAtlasBinPackSL() { m_SkyLine.clear(); }

    /*	insert new rect
     *
     */
    STextureAtlasRect Insert(QT3DSI32 width, QT3DSI32 height)
    {
        QT3DSI32 binHeight;
        QT3DSI32 binWidth;
        QT3DSI32 binIndex;

        STextureAtlasRect newNode = findPosition(width, height, &binWidth, &binHeight, &binIndex);

        if (binIndex != -1) {
            // adjust skyline nodes
            addSkylineLevelNode(binIndex, newNode);
        }

        return newNode;
    }

private:
    /// Represents a single level (a horizontal line) of the skyline/horizon/envelope.
    struct SSkylineNode
    {
        int x; ///< The starting x-coordinate (leftmost).
        int y; ///< The y-coordinate of the skyline level line.
        int width; /// The line width. The ending coordinate (inclusive) will be x+width-1.
    };

    /*	find position
     *
     */
    STextureAtlasRect findPosition(QT3DSI32 width, QT3DSI32 height, QT3DSI32 *binWidth, QT3DSI32 *binHeight,
                                   QT3DSI32 *binIndex)
    {
        *binWidth = m_BinWidth;
        *binHeight = m_BinHeight;
        *binIndex = -1;
        STextureAtlasRect newRect;

        for (QT3DSU32 i = 0; i < m_SkyLine.size(); ++i) {
            QT3DSI32 y = getSkylineLevel(i, width, height);

            if (y >= 0) {
                if ((y + height < *binHeight)
                    || ((y + height == *binHeight) && m_SkyLine[i].width < *binWidth)) {
                    *binHeight = y + height;
                    *binIndex = i;
                    *binWidth = m_SkyLine[i].width;
                    newRect.m_X = m_SkyLine[i].x;
                    newRect.m_Y = y;
                    newRect.m_Width = width;
                    newRect.m_Height = height;
                }
            }
        }

        return newRect;
    }

    /* @brief check if rectangle can be placed into the the bin
     *
     * return skyline hight
     */
    int getSkylineLevel(QT3DSU32 binIndex, QT3DSI32 width, QT3DSI32 height)
    {
        // first check width exceed
        QT3DSI32 x = m_SkyLine[binIndex].x;
        if (x + width > m_BinWidth)
            return -1;

        QT3DSI32 leftAlign = width;
        QT3DSU32 index = binIndex;
        QT3DSI32 y = m_SkyLine[index].y;

        while (leftAlign > 0) {
            y = (y > m_SkyLine[index].y) ? y : m_SkyLine[index].y;
            // check hight
            if (y + height > m_BinHeight)
                return -1;

            leftAlign -= m_SkyLine[index].width;
            ++index;

            if (index > m_SkyLine.size())
                return -1;
        }

        return y;
    }

    /* @brief add an new skyline entry
     *
     * return no return
     */
    void addSkylineLevelNode(QT3DSI32 binIndex, const STextureAtlasRect &newRect)
    {
        SSkylineNode newNode;

        newNode.x = newRect.m_X;
        newNode.y = newRect.m_Y + newRect.m_Height;
        newNode.width = newRect.m_Width;
        m_SkyLine.insert(m_SkyLine.begin() + binIndex, newNode);

        // iterate over follow up nodes and adjust
        for (QT3DSU32 i = binIndex + 1; i < m_SkyLine.size(); ++i) {
            if (m_SkyLine[i].x < m_SkyLine[i - 1].x + m_SkyLine[i - 1].width) {
                int shrink = m_SkyLine[i - 1].x + m_SkyLine[i - 1].width - m_SkyLine[i].x;

                m_SkyLine[i].x += shrink;
                m_SkyLine[i].width -= shrink;

                if (m_SkyLine[i].width <= 0) {
                    m_SkyLine.erase(m_SkyLine.begin() + i);
                    --i;
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        mergeSkylineLevelNodes();
    }

    /* @brief merge skyline node
     *
     * return no return
     */
    void mergeSkylineLevelNodes()
    {
        // check if we can merge nodes
        for (QT3DSU32 i = 0; i < m_SkyLine.size() - 1; ++i) {
            if (m_SkyLine[i].y == m_SkyLine[i + 1].y) {
                m_SkyLine[i].width += m_SkyLine[i + 1].width;
                m_SkyLine.erase(m_SkyLine.begin() + (i + 1));
                --i;
            }
        }
    }

    QT3DSI32 m_BinWidth;
    QT3DSI32 m_BinHeight;

    nvvector<SSkylineNode> m_SkyLine;
};

struct STextureAtlasEntry
{
    STextureAtlasEntry()
        : m_X(0)
        , m_Y(0)
        , m_Width(0)
        , m_Height(0)
        , m_pBuffer(NVDataRef<QT3DSU8>())
    {
    }
    STextureAtlasEntry(QT3DSF32 x, QT3DSF32 y, QT3DSF32 w, QT3DSF32 h, NVDataRef<QT3DSU8> buffer)
        : m_X(x)
        , m_Y(y)
        , m_Width(w)
        , m_Height(h)
        , m_pBuffer(buffer)
    {
    }
    STextureAtlasEntry(const STextureAtlasEntry &entry)
    {
        m_X = entry.m_X;
        m_Y = entry.m_Y;
        m_Width = entry.m_Width;
        m_Height = entry.m_Height;
        m_pBuffer = entry.m_pBuffer;
    }
    ~STextureAtlasEntry() {}

    QT3DSF32 m_X, m_Y;
    QT3DSF32 m_Width, m_Height;
    NVDataRef<QT3DSU8> m_pBuffer;
};

struct STextureAtlas : public ITextureAtlas
{
    NVFoundationBase &m_Foundation;
    volatile QT3DSI32 mRefCount;
    NVScopedRefCounted<NVRenderContext> m_RenderContext;

    STextureAtlas(NVFoundationBase &inFnd, NVRenderContext &inRenderContext, QT3DSI32 width,
                  QT3DSI32 height)
        : m_Foundation(inFnd)
        , mRefCount(0)
        , m_RenderContext(inRenderContext)
        , m_Width(width)
        , m_Height(height)
        , m_Spacing(1)
        , m_AtlasEntrys(inFnd.getAllocator(), "STextureAtlas::m_SkyLine")
    {
        m_pBinPack =
            QT3DS_NEW(inFnd.getAllocator(), STextureAtlasBinPackSL)(inRenderContext, width, height);
    }

    virtual ~STextureAtlas()
    {
        RelaseEntries();

        if (m_pBinPack)
            NVDelete(m_Foundation.getAllocator(), m_pBinPack);
    }

    void RelaseEntries() override
    {
        nvvector<STextureAtlasEntry>::iterator it;

        for (it = m_AtlasEntrys.begin(); it != m_AtlasEntrys.end(); it++) {
            QT3DS_FREE(m_Foundation.getAllocator(), it->m_pBuffer.begin());
        }

        m_AtlasEntrys.clear();
    }
    QT3DSI32 GetWidth() const override { return m_Width; }
    QT3DSI32 GetHeight() const override { return m_Height; }

    QT3DSI32 GetAtlasEntryCount() const override { return m_AtlasEntrys.size(); }

    TTextureAtlasEntryAndBuffer GetAtlasEntryByIndex(QT3DSU32 index) override
    {
        if (index >= m_AtlasEntrys.size())
            return eastl::make_pair(STextureAtlasRect(), NVDataRef<QT3DSU8>());

        return eastl::make_pair(STextureAtlasRect((QT3DSI32)m_AtlasEntrys[index].m_X,
                                                  (QT3DSI32)m_AtlasEntrys[index].m_Y,
                                                  (QT3DSI32)m_AtlasEntrys[index].m_Width,
                                                  (QT3DSI32)m_AtlasEntrys[index].m_Height),
                                m_AtlasEntrys[index].m_pBuffer);
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_Foundation.getAllocator())

    STextureAtlasRect AddAtlasEntry(QT3DSI32 width, QT3DSI32 height, QT3DSI32 pitch,
                                    QT3DSI32 dataWidth, NVConstDataRef<QT3DSU8> bufferData) override
    {
        STextureAtlasRect rect;

        // pitch is the number of bytes per line in bufferData
        // dataWidth is the relevant data width in bufferData. Rest is padding that can be ignored.
        if (m_pBinPack) {
            QT3DSI32 paddedWith, paddedPitch, paddedHeight;
            // add spacing around the character
            paddedWith = width + 2 * m_Spacing;
            paddedPitch = dataWidth + 2 * m_Spacing;
            paddedHeight = height + 2 * m_Spacing;
            // first get entry in the texture atlas
            rect = m_pBinPack->Insert(paddedWith, paddedHeight);
            if (rect.m_Width == 0)
                return rect;

            // we align the data be to 4 byte
            int alignment = (4 - (paddedPitch % 4)) % 4;
            paddedPitch += alignment;

            // since we do spacing around the character we need to copy line by line
            QT3DSU8 *glyphBuffer =
                (QT3DSU8 *)QT3DS_ALLOC(m_Foundation.getAllocator(),
                                 paddedHeight * paddedPitch * sizeof(QT3DSU8), "STextureAtlas");
            if (glyphBuffer) {
                memset(glyphBuffer, 0, paddedHeight * paddedPitch);

                QT3DSU8 *pDst = glyphBuffer + paddedPitch + m_Spacing;
                QT3DSU8 *pSrc = const_cast<QT3DSU8 *>(bufferData.begin());
                for (QT3DSI32 i = 0; i < height; ++i) {
                    memcpy(pDst, pSrc, dataWidth);

                    pDst += paddedPitch;
                    pSrc += pitch;
                }

                // add new entry
                m_AtlasEntrys.push_back(STextureAtlasEntry(
                    (QT3DSF32)rect.m_X, (QT3DSF32)rect.m_Y, (QT3DSF32)paddedWith, (QT3DSF32)paddedHeight,
                    NVDataRef<QT3DSU8>(glyphBuffer, paddedHeight * paddedPitch * sizeof(QT3DSU8))));

                // normalize texture coordinates
                rect.m_NormX = (QT3DSF32)rect.m_X / (QT3DSF32)m_Width;
                rect.m_NormY = (QT3DSF32)rect.m_Y / (QT3DSF32)m_Height;
                rect.m_NormWidth = (QT3DSF32)paddedWith / (QT3DSF32)m_Width;
                rect.m_NormHeight = (QT3DSF32)paddedHeight / (QT3DSF32)m_Height;
            }
        }

        return rect;
    }

private:
    QT3DSI32 m_Width; ///< texture atlas width
    QT3DSI32 m_Height; ///< texture atlas height
    QT3DSI32 m_Spacing; ///< spacing around the entry
    nvvector<STextureAtlasEntry> m_AtlasEntrys; ///< our entries in the atlas
    STextureAtlasBinPackSL *m_pBinPack; ///< our bin packer which actually does most of the work
};

} // namespace

ITextureAtlas &ITextureAtlas::CreateTextureAtlas(NVFoundationBase &inFnd,
                                                 NVRenderContext &inRenderContext, QT3DSI32 width,
                                                 QT3DSI32 height)
{
    return *QT3DS_NEW(inFnd.getAllocator(), STextureAtlas)(inFnd, inRenderContext, width, height);
}