summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/foundation/Qt3DSMemoryBuffer.h
blob: bee2f0c20fee3f534534ab617e7b69c0ac9b3569 (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
/****************************************************************************
**
** 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$
**
****************************************************************************/

#ifndef QT3DS_FOUNDATION_MEMORYBUFFER_H
#define QT3DS_FOUNDATION_MEMORYBUFFER_H
#include "foundation/Qt3DS.h"
#include "foundation/Qt3DSAllocator.h"
#include "foundation/Qt3DSDataRef.h"
#include "foundation/Qt3DSIntrinsics.h"

#ifdef __INTEGRITY
#define __restrict
#endif

namespace qt3ds {
namespace foundation {
    using namespace intrinsics;

    template <typename TAllocator = ForwardingAllocator>
    class MemoryBuffer : public TAllocator
    {
        QT3DSU8 *mBegin;
        QT3DSU8 *mEnd;
        QT3DSU8 *mCapacityEnd;

    public:
        MemoryBuffer(const TAllocator &inAlloc = TAllocator())
            : TAllocator(inAlloc)
            , mBegin(0)
            , mEnd(0)
            , mCapacityEnd(0)
        {
        }
        ~MemoryBuffer()
        {
            if (mBegin)
                TAllocator::deallocate(mBegin);
        }
        QT3DSU32 size() const { return static_cast<QT3DSU32>(mEnd - mBegin); }
        QT3DSU32 capacity() const { return static_cast<QT3DSU32>(mCapacityEnd - mBegin); }
        QT3DSU8 *begin() { return mBegin; }
        QT3DSU8 *end() { return mEnd; }
        const QT3DSU8 *begin() const { return mBegin; }
        const QT3DSU8 *end() const { return mEnd; }
        void clear() { mEnd = mBegin; }
        void write(QT3DSU8 inValue) { *growBuf(1) = inValue; }

        template <typename TDataType>
        void write(const TDataType &inValue)
        {
            const QT3DSU8 *__restrict readPtr = reinterpret_cast<const QT3DSU8 *>(&inValue);
            QT3DSU8 *__restrict writePtr = growBuf(sizeof(TDataType));
            for (QT3DSU32 idx = 0; idx < sizeof(TDataType); ++idx)
                writePtr[idx] = readPtr[idx];
        }

        template <typename TDataType>
        void write(const TDataType *inValue, QT3DSU32 inLength)
        {
            using namespace qt3ds::intrinsics;
            if (inValue && inLength) {
                QT3DSU32 writeSize = inLength * sizeof(TDataType);
                memCopy(growBuf(writeSize), inValue, writeSize);
            }
            if (inLength && !inValue) {
                QT3DS_ASSERT(false);
                // You can't not write something, because that will cause
                // the receiving end to crash.
                QT3DSU32 writeSize = inLength * sizeof(TDataType);
                for (QT3DSU32 idx = 0; idx < writeSize; ++idx)
                    write((QT3DSU8)0);
            }
        }

        void writeStrided(const QT3DSU8 *__restrict inData, QT3DSU32 inItemSize, QT3DSU32 inLength,
                          QT3DSU32 inStride)
        {
            if (inStride == 0 || inStride == inItemSize)
                write(inData, inLength * inItemSize);
            else if (inData && inLength) {
                QT3DSU32 writeSize = inLength * inItemSize;
                QT3DSU8 *__restrict writePtr = growBuf(writeSize);
                for (QT3DSU32 idx = 0; idx < inLength;
                     ++idx, writePtr += inItemSize, inData += inStride)
                    memCopy(writePtr, inData, inItemSize);
            }
        }
        QT3DSU8 *growBuf(QT3DSU32 inAmount)
        {
            QT3DSU32 offset = size();
            QT3DSU32 newSize = offset + inAmount;
            reserve(newSize);
            mEnd += inAmount;
            return mBegin + offset;
        }
        void writeZeros(QT3DSU32 inAmount)
        {
            QT3DSU32 offset = size();
            growBuf(inAmount);
            qt3ds::foundation::memZero(begin() + offset, inAmount);
        }
        void align(QT3DSU32 inAmount)
        {
            QT3DSU32 leftover = size() % inAmount;
            if (leftover)
                writeZeros(inAmount - leftover);
        }
        void reserve(QT3DSU32 newSize)
        {
            using namespace qt3ds::intrinsics;
            QT3DSU32 currentSize = size();
            if (newSize && newSize >= capacity()) {
                QT3DSU32 newDataSize = newSize * 2;
                if (newDataSize > 8192)
                    newDataSize = (QT3DSU32)((QT3DSU32)newSize * 1.2f);
                QT3DSU8 *newData =
                    static_cast<QT3DSU8 *>(TAllocator::allocate(newDataSize, __FILE__, __LINE__));
                if (mBegin) {
                    memCopy(newData, mBegin, currentSize);
                    TAllocator::deallocate(mBegin);
                }
                mBegin = newData;
                mEnd = mBegin + currentSize;
                mCapacityEnd = mBegin + newDataSize;
            }
        }
        operator NVDataRef<QT3DSU8>() { return NVDataRef<QT3DSU8>(begin(), size()); }
        operator NVConstDataRef<QT3DSU8>() const { return NVConstDataRef<QT3DSU8>(begin(), size()); }
    };
}
}

#endif