summaryrefslogtreecommitdiffstats
path: root/src/plugins/renderers/rhi/io/rhibuffer.cpp
blob: 73338e3cc6197cab3a4d583b449458c3b319c2e6 (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
/****************************************************************************
**
** 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:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "rhibuffer_p.h"
#include <submissioncontext_p.h>
#include <QtGui/private/qrhi_p.h>
QT_BEGIN_NAMESPACE

namespace Qt3DRender {

namespace Render {

namespace Rhi {

namespace {
QRhiBuffer::UsageFlag bufferTypeToRhi(RHIBuffer::Type t)
{
    switch (t) {
    case RHIBuffer::Type::ArrayBuffer:
        return QRhiBuffer::VertexBuffer;
    case RHIBuffer::Type::IndexBuffer:
        return QRhiBuffer::IndexBuffer;
    case RHIBuffer::Type::UniformBuffer:
        return QRhiBuffer::UniformBuffer;
    default:
        RHI_UNIMPLEMENTED;
        return QRhiBuffer::StorageBuffer;
    }
}
}

// A UBO is created for each ShaderData Shader Pair
// That means a UBO is unique to a shader/shaderdata

RHIBuffer::RHIBuffer() : m_bufferId(0), m_dynamic(true), m_lastTarget(GL_ARRAY_BUFFER) { }

bool RHIBuffer::bind(SubmissionContext *ctx, Type t)
{
    assert(ctx->m_currentUpdates);
    if (this->m_datasToUpload.empty())
        return bool(m_rhiBuffer);

    const auto uploadMethod = m_dynamic ? &QRhiResourceUpdateBatch::updateDynamicBuffer
                                        : qOverload<QRhiBuffer *, int, int, const void *>(
                                                &QRhiResourceUpdateBatch::uploadStaticBuffer);
    if (!m_rhiBuffer) {
        if (m_allocSize <= 0)
            return false;

        const auto kind = m_dynamic ? QRhiBuffer::Dynamic : QRhiBuffer::Static;
        const auto usage = bufferTypeToRhi(t);

        // RHI does not seem to support using the same buffer with different types
        if (m_rhiBuffer)
            assert(m_rhiBuffer->usage() == usage);

        if (!m_rhiBuffer)
            m_rhiBuffer = ctx->rhi()->newBuffer(kind, usage, m_allocSize);
        assert(m_rhiBuffer);

        m_rhiBuffer->build();
#if defined(QT_DEBUG)
        {
            // for debug: we set the buffer to zero
            auto ptr = new char[m_allocSize] {};
            (ctx->m_currentUpdates->*uploadMethod)(m_rhiBuffer, 0, m_allocSize, ptr);
            delete[] ptr;
        }
#endif
    }

    for (const std::pair<QByteArray, int> &pair : this->m_datasToUpload) {
        const QByteArray &data = pair.first;
        int offset = pair.second;
        (ctx->m_currentUpdates->*uploadMethod)(m_rhiBuffer, offset, data.size(), data.constData());
    }

    m_datasToUpload.clear();
    return true;
}

bool RHIBuffer::release(SubmissionContext *ctx)
{
    if (m_rhiBuffer)
        m_rhiBuffer->release();
    return true;
}

bool RHIBuffer::create(SubmissionContext *ctx)
{
    return true;
}

void RHIBuffer::destroy(SubmissionContext *ctx)
{
    if (m_rhiBuffer) {
        m_rhiBuffer->releaseAndDestroyLater();
        m_rhiBuffer = nullptr;
    }
    m_allocSize = 0;
}

void RHIBuffer::orphan(SubmissionContext *)
{
    m_datasToUpload.clear();
    if (m_rhiBuffer) {
        m_rhiBuffer->releaseAndDestroyLater();
        m_rhiBuffer = nullptr;
    }
    m_allocSize = 0;
}

void RHIBuffer::allocate(SubmissionContext *ctx, const QByteArray &data, bool dynamic)
{
    m_datasToUpload.clear();
    m_datasToUpload.push_back({ data, 0 });
    m_allocSize = data.size();
    m_dynamic = dynamic;
}

void RHIBuffer::update(SubmissionContext *ctx, const QByteArray &data, int offset)
{
    m_datasToUpload.push_back({ data, offset });
}

QByteArray RHIBuffer::download(SubmissionContext *ctx, uint size)
{
    RHI_UNIMPLEMENTED;
    //    char *gpu_ptr = ctx->mapBuffer(m_lastTarget, size);
    //    QByteArray data;
    //    if (gpu_ptr != nullptr) {
    //        data.resize(size);
    //        std::copy(gpu_ptr, gpu_ptr+size, data.data());
    //    }
    //    ctx->unmapBuffer(m_lastTarget);
    //    return data;
    return {};
}

void RHIBuffer::bindBufferBase(SubmissionContext *ctx, int bindingPoint, RHIBuffer::Type t)
{
    RHI_UNIMPLEMENTED;
    //    ctx->bindBufferBase(glBufferTypes[t], bindingPoint, m_bufferId);
}

void RHIBuffer::bindBufferBase(SubmissionContext *ctx, int bindingPoint)
{
    RHI_UNIMPLEMENTED;
    //    ctx->bindBufferBase(m_lastTarget, bindingPoint, m_bufferId);
}

void RHIBuffer::cleanup()
{
    destroy(nullptr);
}

} // namespace Rhi

} // namespace Render

} // namespace Qt3DRender

QT_END_NAMESPACE