-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBufferObject.h
195 lines (163 loc) · 7.58 KB
/
BufferObject.h
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
/*
This file is part of the Rendering library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
Copyright (C) 2014-2019 Sascha Brandt <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef RENDERING_BUFFEROBJECT_H
#define RENDERING_BUFFEROBJECT_H
#include <cstddef>
#include <cstdint>
#include <vector>
#include <Util/CountedObjectWrapper.h>
namespace Rendering {
//! @defgroup rendering_resources Resources
/**
* Representation of an OpenGL buffer object (BO).
*
* @author Benjamin Eikel
* @date 2012-04-19
* @ingroup rendering_resources
*/
class BufferObject {
public:
RENDERINGAPI static const uint32_t TARGET_ARRAY_BUFFER;
RENDERINGAPI static const uint32_t TARGET_ATOMIC_COUNTER_BUFFER;
RENDERINGAPI static const uint32_t TARGET_COPY_READ_BUFFER;
RENDERINGAPI static const uint32_t TARGET_COPY_WRITE_BUFFER;
RENDERINGAPI static const uint32_t TARGET_DISPATCH_INDIRECT_BUFFER;
RENDERINGAPI static const uint32_t TARGET_DRAW_INDIRECT_BUFFER;
RENDERINGAPI static const uint32_t TARGET_ELEMENT_ARRAY_BUFFER;
RENDERINGAPI static const uint32_t TARGET_PIXEL_PACK_BUFFER;
RENDERINGAPI static const uint32_t TARGET_PIXEL_UNPACK_BUFFER;
RENDERINGAPI static const uint32_t TARGET_QUERY_BUFFER;
RENDERINGAPI static const uint32_t TARGET_SHADER_STORAGE_BUFFER;
RENDERINGAPI static const uint32_t TARGET_TEXTURE_BUFFER;
RENDERINGAPI static const uint32_t TARGET_TRANSFORM_FEEDBACK_BUFFER;
RENDERINGAPI static const uint32_t TARGET_UNIFORM_BUFFER;
RENDERINGAPI static const uint32_t USAGE_STREAM_DRAW;
RENDERINGAPI static const uint32_t USAGE_STREAM_READ;
RENDERINGAPI static const uint32_t USAGE_STREAM_COPY;
RENDERINGAPI static const uint32_t USAGE_STATIC_DRAW;
RENDERINGAPI static const uint32_t USAGE_STATIC_READ;
RENDERINGAPI static const uint32_t USAGE_STATIC_COPY;
RENDERINGAPI static const uint32_t USAGE_DYNAMIC_DRAW;
RENDERINGAPI static const uint32_t USAGE_DYNAMIC_READ;
RENDERINGAPI static const uint32_t USAGE_DYNAMIC_COPY;
enum class AccessFlag : uint8_t {
NO_ACCESS = 0,
READ_ONLY = 1,
WRITE_ONLY = 2,
READ_WRITE = 3
};
private:
//! OpenGL handle for this buffer object.
uint32_t bufferId;
public:
//! Create an invalid buffer object for the given target.
RENDERINGAPI BufferObject();
//! Data of an buffer object should not be copied.
BufferObject(const BufferObject &) = delete;
//! Take ownership of the data of the other buffer object.
RENDERINGAPI BufferObject(BufferObject && other);
//! Free the data of the buffer object.
RENDERINGAPI ~BufferObject();
//! Data of an buffer object should not be copied.
BufferObject & operator=(const BufferObject &) = delete;
//! Take ownership of the data of the other buffer object.
RENDERINGAPI BufferObject & operator=(BufferObject && other);
//! Swap the gl buffer with another BufferObject
RENDERINGAPI void swap(BufferObject & other);
//! Request a new handle from OpenGL for this buffer object.
RENDERINGAPI void prepare();
//! Free the handle of this buffer object.
RENDERINGAPI void destroy();
//! Bind the buffer object to the given target.
RENDERINGAPI void bind(uint32_t bufferTarget) const;
RENDERINGAPI void bind(uint32_t bufferTarget, uint32_t location) const;
//! Remove any binding of the given target.
RENDERINGAPI void unbind(uint32_t bufferTarget) const;
RENDERINGAPI void unbind(uint32_t bufferTarget, uint32_t location) const;
/**
* @brief Allocate buffer data
*
* Bind the buffer object to the given target,
* allocate @a numberOfElements times <tt>sizeof(T)</tt> bytes,
* and unbind the buffer object.
*/
template<typename T>
void allocateData(uint32_t bufferTarget, std::size_t numberOfElements, uint32_t usageHint) {
uploadData(bufferTarget, nullptr, numberOfElements * sizeof(T), usageHint);
}
/**
* @brief Copy data to the buffer object
*
* Bind the buffer object to the given target,
* copy <tt>data.size()</tt> times <tt>sizeof(T)</tt> bytes from the vector to the buffer object,
* and unbind the buffer object.
*/
template<typename T>
void uploadData(uint32_t bufferTarget, const std::vector<T> & data, uint32_t usageHint) {
uploadData(bufferTarget, reinterpret_cast<const uint8_t*>(data.data()),data.size() * sizeof(T),usageHint);
}
RENDERINGAPI void uploadData(uint32_t bufferTarget, const uint8_t* data, size_t numBytes, uint32_t usageHint);
/**
* @brief Copy data to the buffer object
*
* Bind the buffer object to the given target,
* copy <tt>data.size()</tt> times <tt>sizeof(T)</tt> bytes from the vector to the buffer object,
* and unbind the buffer object.
*/
template<typename T>
void uploadSubData(uint32_t bufferTarget, const std::vector<T> & data, size_t offset=0) {
uploadSubData(bufferTarget, reinterpret_cast<const uint8_t*>(data.data()),data.size() * sizeof(T),offset);
}
RENDERINGAPI void uploadSubData(uint32_t bufferTarget, const uint8_t* data, size_t numBytes, size_t offset=0);
/**
* @brief Retrieve data from the buffer object
*
* Bind the buffer object to the given target,
* copy @a numberOfElements times <tt>sizeof(T)</tt> bytes from the buffer object to the vector,
* and unbind the buffer object.
*/
RENDERINGAPI void downloadData(uint32_t bufferTarget, size_t numBytes, uint8_t* targetPtr, size_t offset=0) const;
template<typename T>
std::vector<T> downloadData(uint32_t bufferTarget, size_t numberOfElements, size_t offset=0) const {
std::vector<T> result(numberOfElements);
downloadData(bufferTarget, numberOfElements * sizeof(T), reinterpret_cast<uint8_t*>(result.data()), offset);
return result;
}
//! @c true if and only if prepare() was executed at least once without an execution of destroy() afterwards.
bool isValid() const {
return bufferId != 0;
}
uint32_t getGLId()const{ return bufferId; }
RENDERINGAPI void clear(uint32_t bufferTarget, uint32_t internalFormat, uint32_t format, uint32_t type, const uint8_t* data=nullptr);
RENDERINGAPI void clear(uint32_t internalFormat, uint32_t format, uint32_t type, const uint8_t* data=nullptr);
RENDERINGAPI void clear();
RENDERINGAPI void copy(const BufferObject& source, uint32_t sourceOffset, uint32_t targetOffset, uint32_t size);
/**
* Map all or part of a buffer object's data store into the client's address space
* @note The buffer has to be unmapped before using it for rendering.
*
* @param offset The offset into the buffer in bytes.
* @param size The size of the buffer range in bytes to map. When <tt>size</tt> is 0, the entire buffer is mapped.
* @param allowWrite Indicates that the returned pointer may be used to modify buffer object data.
* @param allowRead Indicates that the returned pointer may be used to read buffer object data.
* @return The mapped data pointer, or <tt>nullptr</tt> if the mapping failed.
*/
RENDERINGAPI uint8_t* map(uint32_t offset=0, uint32_t size=0, AccessFlag access=AccessFlag::READ_WRITE);
/**
* Unmaps a previously mapped buffer.
*/
RENDERINGAPI void unmap();
//! (internal) sets the glId of the buffer. Used for creating buffers from existing gl buffers.
RENDERINGAPI void _setGLId(uint32_t glId);
};
typedef Util::CountedObjectWrapper<BufferObject> CountedBufferObject;
}
#endif /* RENDERING_BUFFEROBJECT_H */