summaryrefslogtreecommitdiffstats
path: root/src/render/Qt3DSRenderDataBuffer.cpp
blob: b00b0af0c435bee3eedf9c8bc6631e90edfc91af (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
/****************************************************************************
**
** 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 "render/Qt3DSRenderContext.h"
#include "render/Qt3DSRenderDataBuffer.h"
#include "render/Qt3DSRenderBaseTypes.h"
#include "foundation/Qt3DSFoundation.h"

namespace qt3ds {
namespace render {

    NVRenderDataBuffer::NVRenderDataBuffer(NVRenderContextImpl &context, NVFoundationBase &fnd,
                                           size_t size, NVRenderBufferBindFlags bindFlags,
                                           NVRenderBufferUsageType::Enum usageType,
                                           NVDataRef<QT3DSU8> data)
        : m_Context(context)
        , m_Foundation(fnd)
        , mRefCount(0)
        , m_Backend(context.GetBackend())
        , m_UsageType(usageType)
        , m_BindFlags(bindFlags)
        , m_BufferData(data)
        , m_BufferCapacity(data.size())
        , m_BufferSize(size)
        , m_OwnsData(false)
        , m_Mapped(false)
    {
        m_BufferHandle =
            m_Backend->CreateBuffer(size, bindFlags, usageType, (const void *)m_BufferData.begin());
    }

    NVRenderDataBuffer::~NVRenderDataBuffer()
    {
        if (m_BufferHandle) {
            m_Backend->ReleaseBuffer(m_BufferHandle);
        }
        m_BufferHandle = 0;

        releaseMemory();
    }

    void NVRenderDataBuffer::releaseMemory()
    {
        // chekc if we should release memory
        if (m_BufferData.size() && m_OwnsData) {
            m_Foundation.getAllocator().deallocate(m_BufferData.begin());
        }

        m_BufferData = NVDataRef<QT3DSU8>();
        m_OwnsData = false;
    }

    NVDataRef<QT3DSU8> NVRenderDataBuffer::MapBuffer()
    {
        // don't map twice
        if (m_Mapped) {
            qCCritical(INVALID_OPERATION, "Attempting to map a mapped buffer");
            QT3DS_ASSERT(false);
        }

        QT3DSU8 *pData = (QT3DSU8 *)m_Backend->MapBuffer(
            m_BufferHandle, m_BindFlags, 0, m_BufferSize,
            NVRenderBufferAccessFlags(NVRenderBufferAccessTypeValues::Read
                                      | NVRenderBufferAccessTypeValues::Write));

        releaseMemory();
        m_BufferData = toDataRef(const_cast<QT3DSU8 *>(pData), (QT3DSU32)m_BufferSize);
        m_BufferCapacity = (QT3DSU32)m_BufferSize;
        m_OwnsData = false;

        // currently we return a reference to the system memory
        m_Mapped = true;
        return m_BufferData;
    }

    NVDataRef<QT3DSU8> NVRenderDataBuffer::MapBufferRange(size_t offset, size_t size,
                                                       NVRenderBufferAccessFlags flags)
    {
        // don't map twice
        if (m_Mapped) {
            qCCritical(INVALID_OPERATION, "Attempting to map a mapped buffer");
            QT3DS_ASSERT(false);
        }
        // don't map out of range
        if ((m_BufferSize < (offset + size)) || (size == 0)) {
            qCCritical(INVALID_OPERATION, "Attempting to map out of buffer range");
            QT3DS_ASSERT(false);
        }

        QT3DSU8 *pData =
            (QT3DSU8 *)m_Backend->MapBuffer(m_BufferHandle, m_BindFlags, offset, size, flags);

        releaseMemory();
        m_BufferData = toDataRef(const_cast<QT3DSU8 *>(pData), (QT3DSU32)size);
        m_BufferCapacity = (QT3DSU32)size;
        m_OwnsData = false;

        // currently we return a reference to the system memory
        m_Mapped = true;
        return m_BufferData;
    }

    void NVRenderDataBuffer::UnmapBuffer()
    {
        if (m_Mapped) {
            // update hardware
            m_Backend->UnmapBuffer(m_BufferHandle, m_BindFlags);
            m_Mapped = false;
            releaseMemory();
        }
    }

    void NVRenderDataBuffer::UpdateBuffer(NVConstDataRef<QT3DSU8> data, bool ownsMemory)
    {
        // don't update a mapped buffer
        if (m_Mapped) {
            qCCritical(INVALID_OPERATION, "Attempting to update a mapped buffer");
            QT3DS_ASSERT(false);
        }

        releaseMemory();

        m_BufferData = toDataRef(const_cast<QT3DSU8 *>(data.begin()), data.size());
        m_BufferCapacity = data.mSize;
        m_OwnsData = ownsMemory;
        // update hardware
        m_Backend->UpdateBuffer(m_BufferHandle, m_BindFlags, m_BufferCapacity, m_UsageType,
                                (const void *)m_BufferData.begin());
    }
}
}