Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Buffer.hpp
1#pragma once
2#include "graphics/GPUTypes.hpp"
3#include "Common.hpp"
4
5class GPUBuffer;
6
10class Buffer {
11public:
12
18 Buffer(size_t sizeInBytes, BufferType atype, const std::string & name);
19
26 void upload(size_t sizeInBytes, unsigned char * data, size_t offset);
27
33 template<typename T>
34 void upload(const std::vector<T>& data, size_t offset = 0);
35
41 void download(size_t sizeInBytes, unsigned char * data, size_t offset);
42
45 void clean();
46
50 const std::string & name() const;
51
55 Buffer & operator=(const Buffer &) = delete;
56
58 Buffer(const Buffer &) = delete;
59
63 Buffer & operator=(Buffer &&) = delete;
64
66 Buffer(Buffer &&) = delete;
67
69 virtual ~Buffer();
70
72
73protected:
74
79 Buffer(BufferType atype, const std::string & name);
80
81public:
82
83 size_t size;
84 std::unique_ptr<GPUBuffer> gpu;
85
86private:
87
88 std::string _name;
89
90};
91
92template<typename T>
93void Buffer::upload(const std::vector<T>& data, size_t offset){
94 upload(sizeof(T) * data.size(), (unsigned char*)data.data(), offset);
95}
96
105class UniformBufferBase : public Buffer {
106public:
107
113 UniformBufferBase(size_t sizeInBytes, UniformFrequency use, const std::string& name);
114
120 bool upload(unsigned char * data);
121
123 void clean();
124
126 size_t currentOffset() const { return _offset; }
127
129 size_t baseSize() const { return _baseSize; }
130
135
138
143
146
148 virtual ~UniformBufferBase();
149
150private:
151
152 const size_t _baseSize;
153 size_t _alignment = 0;
154 size_t _offset = 0;
155 bool _wrapAround = true;
156};
157
163template<typename T>
165public:
166
172 UniformBuffer(size_t count, UniformFrequency usage, const std::string& name);
173
178 T & operator[](size_t i){
179 return data[i];
180 }
181
186 const T & operator[](size_t i) const {
187 return data[i];
188 }
189
194 T & at(size_t i){
195 return data[i];
196 }
197
202 const T & at(size_t i) const {
203 return data[i];
204 }
205
207 size_t size() const {
208 return data.size();
209 }
210
215 bool upload();
216
221
223 UniformBuffer(const UniformBuffer &) = delete;
224
229
232
234 ~UniformBuffer() = default;
235
236 std::vector<T> data;
237
238};
239
240template <typename T>
241UniformBuffer<T>::UniformBuffer(size_t count, UniformFrequency usage, const std::string& name) :
242 UniformBufferBase(count * sizeof(T), usage, name) {
243 data.resize(count);
244}
245
246template <typename T>
248 return UniformBufferBase::upload(reinterpret_cast<unsigned char*>(data.data()));
249}
General purpose GPU buffer, with different use types determining its memory type, visibility and acce...
Definition: Buffer.hpp:10
void download(size_t sizeInBytes, unsigned char *data, size_t offset)
Definition: Buffer.cpp:43
size_t size
Buffer size in bytes.
Definition: Buffer.hpp:83
Buffer & operator=(const Buffer &)=delete
Buffer(const Buffer &)=delete
const BufferType type
Buffer type.
Definition: Buffer.hpp:71
virtual ~Buffer()
Definition: Buffer.cpp:30
Buffer(Buffer &&)=delete
std::unique_ptr< GPUBuffer > gpu
The GPU data (optional).
Definition: Buffer.hpp:84
void clean()
Definition: Buffer.cpp:18
const std::string & name() const
Definition: Buffer.cpp:26
Buffer & operator=(Buffer &&)=delete
void upload(size_t sizeInBytes, unsigned char *data, size_t offset)
Definition: Buffer.cpp:34
std::string _name
Resource name.
Definition: Buffer.hpp:88
Store data in a GPU buffer.
Definition: GPUObjects.hpp:86
Uniform buffer exposed to all shader stages, that can be updated at varying frequencies....
Definition: Buffer.hpp:105
UniformBufferBase(UniformBufferBase &&)=delete
size_t baseSize() const
Definition: Buffer.hpp:129
const size_t _baseSize
The uniform buffer size (ie the size of one instance).
Definition: Buffer.hpp:152
size_t currentOffset() const
Definition: Buffer.hpp:126
bool _wrapAround
Should the buffer attempt to reuse the same memory once full.
Definition: Buffer.hpp:155
void clean()
Definition: Buffer.cpp:110
virtual ~UniformBufferBase()
Definition: Buffer.cpp:114
UniformBufferBase(const UniformBufferBase &)=delete
UniformBufferBase & operator=(const UniformBufferBase &)=delete
UniformBufferBase & operator=(UniformBufferBase &&)=delete
size_t _offset
The current offset in bytes in the array of instances.
Definition: Buffer.hpp:154
bool upload(unsigned char *data)
Definition: Buffer.cpp:85
size_t _alignment
The alignment constraint to respect between successive instances.
Definition: Buffer.hpp:153
Represents a buffer containing uniform data, stored on the CPU and GPU. Depending on the update frequ...
Definition: Buffer.hpp:164
UniformBuffer(UniformBuffer &&)=default
bool upload()
Definition: Buffer.hpp:247
UniformBuffer & operator=(UniformBuffer &&)=default
UniformBuffer & operator=(const UniformBuffer &)=delete
UniformBuffer(size_t count, UniformFrequency usage, const std::string &name)
Definition: Buffer.hpp:241
std::vector< T > data
The CPU data.
Definition: Buffer.hpp:236
const T & at(size_t i) const
Definition: Buffer.hpp:202
~UniformBuffer()=default
const T & operator[](size_t i) const
Definition: Buffer.hpp:186
T & operator[](size_t i)
Definition: Buffer.hpp:178
T & at(size_t i)
Definition: Buffer.hpp:194
size_t size() const
Definition: Buffer.hpp:207
UniformBuffer(const UniformBuffer &)=delete
UniformFrequency
The frequency at which a uniform buffer might be updated.
Definition: GPUTypes.hpp:48
BufferType
The type of data a buffer is storing, determining its use.
Definition: GPUTypes.hpp:33