Tatooine
buffer.h
Go to the documentation of this file.
1#ifndef TATOOINE_GL_BUFFER
2#define TATOOINE_GL_BUFFER
3//==============================================================================
9
10#include <mutex>
11#include <vector>
12//==============================================================================
13namespace tatooine::gl {
14//==============================================================================
15template <GLsizei array_type, typename T>
16class buffer;
17//==============================================================================
18template <GLsizei ArrayType, typename T, GLbitfield Access>
20 public:
21 using value_type = T;
22 static constexpr auto access = Access;
23 static constexpr auto array_type = ArrayType;
25 static constexpr auto data_size = buffer_type::data_size;
26
27 private:
32 bool m_unmapped = false;
33
34 public:
40 static_cast<GLsizei>(data_size * m_length), access));
42 }
43 //============================================================================
44 buffer_map(buffer_map const&) = delete;
46 //============================================================================
47 auto operator=(buffer_map const&) -> buffer_map& = delete;
48 auto operator=(buffer_map&&) -> buffer_map& = delete;
49 //============================================================================
50 auto operator=(std::vector<T> const& data) -> buffer_map& {
51 assert(size(data) == m_buffer->size());
52 for (std::size_t i = 0; i < size(data); ++i) {
53 at(i) = data[i];
54 }
55 return &this;
56 }
57 //============================================================================
60 //============================================================================
61 auto unmap() {
63 if (!m_unmapped) {
65 m_unmapped = true;
66 }
67 }
68 //============================================================================
69 auto at(std::size_t i) -> auto& { return m_gpu_mapping[i]; }
70 auto at(std::size_t i) const -> auto const& { return m_gpu_mapping[i]; }
71 //============================================================================
72 auto front() -> auto& { return at(0); }
73 auto front() const -> auto const& { return at(0); }
74 //============================================================================
75 auto back() -> auto& { return at(m_length - 1); }
76 auto back() const -> auto const& { return at(m_length - 1); }
77 //============================================================================
78 auto operator[](std::size_t i) -> auto& { return at(i); }
79 auto operator[](std::size_t i) const -> auto const& { return at(i); }
80 //============================================================================
81 auto begin() { return m_gpu_mapping; }
82 auto begin() const { return m_gpu_mapping; }
83 //============================================================================
84 auto end() { return m_gpu_mapping + m_length; }
85 auto end() const { return m_gpu_mapping + m_length; }
86 //============================================================================
87 auto offset() const { return m_offset; }
88 auto length() const { return m_length; }
89};
90//------------------------------------------------------------------------------
91// buffer_map free functions
92//------------------------------------------------------------------------------
93template <GLsizei ArrayType, typename T, GLbitfield Access>
95 return map.begin();
96}
97// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98template <GLsizei ArrayType, typename T, GLbitfield Access>
100 return map.begin();
101}
102//------------------------------------------------------------------------------
103template <GLsizei ArrayType, typename T, GLbitfield Access>
105 return map.end();
106}
107// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108template <GLsizei ArrayType, typename T, GLbitfield Access>
110 return map.end();
111}
112//------------------------------------------------------------------------------
113// buffer_map free typedefs
114//------------------------------------------------------------------------------
115template <GLsizei array_type, typename T>
117
118template <GLsizei array_type, typename T>
120
121template <GLsizei array_type, typename T>
124//==============================================================================
126template <GLsizei array_type, typename T>
128 public:
131
132 private:
134 std::size_t m_idx;
135
136 public:
137 rbuffer_map_element(const buffer_type* buffer, std::size_t idx)
138 : m_buffer{buffer}, m_idx{idx} {}
140 rbuffer_map_element(rbuffer_map_element&& other) noexcept = default;
141
143 -> rbuffer_map_element& = default;
144 auto operator=(rbuffer_map_element&& other) noexcept
145 -> rbuffer_map_element& = default;
146
148
150 explicit operator T() const { return download(); }
151
152 [[nodiscard]] auto download() const {
154 return map.front();
155 }
156 auto operator==(const T& t) const -> bool { return download() == t; }
157 auto operator!=(const T& t) const -> bool { return !operator==(t); }
158 auto operator>(const T& t) const -> bool { return download() > t; }
159 auto operator>=(const T& t) const -> bool { return download() >= t; }
160 auto operator<(const T& t) const -> bool { return download() < t; }
161 auto operator<=(const T& t) const -> bool { return download() <= t; }
162};
163
164template <GLsizei array_type, typename T>
165inline auto operator<<(std::ostream& out,
166 rbuffer_map_element<array_type, T>& data) -> auto& {
167 out << data.download();
168 return out;
169}
170
171//==============================================================================
173template <GLsizei array_type, typename T>
175 public:
178
179 private:
181 std::size_t m_idx;
182
183 public:
184 rwbuffer_map_element(const buffer_type* buffer, std::size_t idx)
185 : m_buffer{buffer}, m_idx{idx} {}
187 rwbuffer_map_element(rwbuffer_map_element&& other) noexcept = default;
188
190 -> rwbuffer_map_element& = default;
191 auto operator=(rwbuffer_map_element&& other) noexcept
192 -> rwbuffer_map_element& = default;
193
196 auto operator=(T const& data) -> auto& {
199 std::cout << "attention!\n";
200 }
203 return *this;
204 }
205
207 explicit operator T() const { return download(); }
208
209 [[nodiscard]] auto download() const {
211 return map.front();
212 }
213 auto operator==(const T& t) const -> bool { return download() == t; }
214 auto operator!=(const T& t) const -> bool { return !operator==(t); }
215 auto operator>(const T& t) const -> bool { return download() > t; }
216 auto operator>=(const T& t) const -> bool { return download() >= t; }
217 auto operator<(const T& t) const -> bool { return download() < t; }
218 auto operator<=(const T& t) const -> bool { return download() <= t; }
219};
220//==============================================================================
223template <GLsizei array_type, typename T>
225 public:
227
228 private:
230 std::size_t m_idx;
231
232 public:
233 wbuffer_map_element(const buffer_type* buffer, std::size_t idx)
234 : m_buffer{buffer}, m_idx{idx} {}
236 wbuffer_map_element(wbuffer_map_element&& other) noexcept = default;
237
239 -> wbuffer_map_element& = default;
240 auto operator=(wbuffer_map_element&& other) noexcept
241 -> wbuffer_map_element& = default;
242
245 auto operator=(T const& data) -> auto& {
248 std::cout << "attention!\n";
249 }
252 return *this;
253 }
254};
255//------------------------------------------------------------------------------
256template <GLsizei array_type, typename T>
257inline auto operator<<(std::ostream& out,
259 out << data.download();
260 return out;
261}
262
263//==============================================================================
265template <GLsizei array_type, typename T>
267 public:
269 //----------------------------------------------------------------------------
270 // iterator typedefs
271 using value_type = T;
272 using reference = T&;
273 using pointer = T*;
274 using difference_type = std::ptrdiff_t;
275 using iterator_category = std::bidirectional_iterator_tag;
276 //----------------------------------------------------------------------------
278 : m_buffer(buffer), m_idx(idx) {}
279 //----------------------------------------------------------------------------
281 buffer_iterator(buffer_iterator&&) noexcept = default;
282 //----------------------------------------------------------------------------
283 auto operator=(buffer_iterator const&) -> buffer_iterator& = default;
284 auto operator=(buffer_iterator&&) noexcept -> buffer_iterator& = default;
285 //----------------------------------------------------------------------------
286 ~buffer_iterator() = default;
287 //----------------------------------------------------------------------------
289 auto operator*() const -> T { return rbuffer_map_element(m_buffer, m_idx); }
290 //----------------------------------------------------------------------------
292 auto operator==(const buffer_iterator& other) const {
293 return m_idx == other.m_idx;
294 }
295 //----------------------------------------------------------------------------
297 auto operator!=(const buffer_iterator& other) const {
298 return !operator==(other);
299 }
300 //----------------------------------------------------------------------------
302 auto operator++() -> auto& {
303 ++m_idx;
304 return *this;
305 }
306 //----------------------------------------------------------------------------
308 auto operator++(int) {
309 buffer_iterator vi{*this};
310 ++(*this);
311 return vi;
312 }
313 //----------------------------------------------------------------------------
315 auto operator--() -> auto& {
316 --m_idx;
317 return *this;
318 }
319 //----------------------------------------------------------------------------
321 auto operator--(int) {
322 buffer_iterator vi(*this);
323 --(*this);
324 return vi;
325 }
326
327 private:
329 std::size_t m_idx;
330};
331
332//==============================================================================
334template <GLsizei array_type, typename T>
336 public:
338
339 // iterator typedefs
340 using value_type = T;
341 using reference = T&;
342 using pointer = T*;
343 using difference_type = std::ptrdiff_t;
344 using iterator_category = std::bidirectional_iterator_tag;
345 //----------------------------------------------------------------------------
346 cbuffer_iterator(const buffer_type* buffer, std::size_t idx)
347 : m_buffer(buffer), m_idx(idx) {}
348 //----------------------------------------------------------------------------
349 cbuffer_iterator(const cbuffer_iterator& other) = default;
350 cbuffer_iterator(cbuffer_iterator&& other) noexcept = default;
351 //----------------------------------------------------------------------------
352 auto operator=(const cbuffer_iterator& other) -> cbuffer_iterator& = default;
353 auto operator=(cbuffer_iterator&& other) noexcept
354 -> cbuffer_iterator& = default;
355 //----------------------------------------------------------------------------
356 ~cbuffer_iterator() = default;
357 //----------------------------------------------------------------------------
359 auto operator*() const -> T { return rbuffer_map_element(m_buffer, m_idx); }
360 //----------------------------------------------------------------------------
362 auto operator==(const cbuffer_iterator& other) const {
363 return (m_idx == other.m_idx);
364 }
365 //----------------------------------------------------------------------------
367 auto operator!=(const cbuffer_iterator& other) const {
368 return !operator==(other);
369 }
370 //----------------------------------------------------------------------------
372 auto operator++() -> auto& {
373 ++m_idx;
374 return *this;
375 }
376 //----------------------------------------------------------------------------
378 auto operator++(int) {
379 cbuffer_iterator vi(*this);
380 ++(*this);
381 return vi;
382 }
383 //----------------------------------------------------------------------------
385 auto operator--() -> auto& {
386 --m_idx;
387 return *this;
388 }
389 //----------------------------------------------------------------------------
391 auto operator--(int) {
392 cbuffer_iterator vi(*this);
393 --(*this);
394 return vi;
395 }
396
397 private:
399 std::size_t m_idx;
400};
401
402//==============================================================================
404template <GLsizei ArrayType, typename T>
405class buffer : public id_holder<GLuint> {
406 public:
408 using parent_type::id;
409 friend class rbuffer_map_element<ArrayType, T>;
410 friend class rwbuffer_map_element<ArrayType, T>;
411
412 constexpr static auto array_type = ArrayType;
413 constexpr static GLsizei data_size = static_cast<GLsizei>(sizeof(T));
414
416 using value_type = T;
417
421
424
428
429 private:
433
434 public:
435 explicit buffer(buffer_usage usage);
436 buffer(const buffer& other);
437 buffer(buffer&& other) noexcept;
438 auto operator=(const buffer& other) -> buffer&;
439 auto operator=(buffer&& other) noexcept -> buffer&;
440 auto operator=(const std::vector<T>& data) -> buffer&;
441
443 buffer(GLsizei n, const T& initial, buffer_usage usage);
444 buffer(const std::vector<T>& data, buffer_usage usage);
446
447 auto create_handle() -> void;
448 auto destroy_handle() -> void;
449
450 auto upload_data(const T& data) -> void;
451 auto upload_data(const std::vector<T>& data) -> void;
452 [[nodiscard]] auto download_data() const -> std::vector<T>;
453
454 auto bind() const -> void;
455 static auto unbind() -> void;
456
457 auto copy(const this_type& other) -> void;
458
459 [[nodiscard]] auto empty() const -> bool { return m_size == 0; }
460 [[nodiscard]] auto size() const { return m_size; }
461 [[nodiscard]] auto capacity() const { return m_capacity; }
462
463 auto reserve(GLsizei size) -> void;
464 auto resize(GLsizei size) -> void;
465 auto clear() { m_size = 0; }
466
467 auto gpu_malloc(GLsizei n) -> void;
468 auto gpu_malloc(GLsizei n, const T& initial) -> void;
469 auto set_usage(buffer_usage) -> void;
470
471 auto push_back(T const&) -> void;
472 auto pop_back() -> void;
473
474 template <typename... Ts>
475 auto emplace_back(Ts&&...) -> void;
476
477 [[nodiscard]] auto read_write_element_at(std::size_t idx) {
478 return read_write_element_type(this, idx);
479 }
480 [[nodiscard]] auto read_element_at(std::size_t idx) const {
481 return read_only_element_type(this, idx);
482 }
483 [[nodiscard]] auto write_only_element_at(std::size_t idx) {
484 return write_only_element_type(this, idx);
485 }
486
487 [[nodiscard]] auto at(std::size_t idx) { return read_write_element_at(idx); }
488 [[nodiscard]] auto at(std::size_t idx) const { return read_element_at(idx); }
489
490 [[nodiscard]] auto operator[](std::size_t idx) { return at(idx); }
491 [[nodiscard]] auto operator[](std::size_t idx) const { return at(idx); }
492
493 [[nodiscard]] auto front() { return at(0); }
494 [[nodiscard]] auto front() const { return at(0); }
495
496 [[nodiscard]] auto back() { return at(m_size - 1); }
497 [[nodiscard]] auto back() const { return at(m_size - 1); }
498
499 [[nodiscard]] auto begin() { return iterator(this, 0); }
500 [[nodiscard]] auto end() { return iterator(this, m_size); }
501
502 [[nodiscard]] auto begin() const { return const_iterator(this, 0); }
503 [[nodiscard]] auto end() const { return const_iterator(this, m_size); }
504
505 [[nodiscard]] auto rmap() const { return read_map_type(this, 0, m_size); }
506 [[nodiscard]] auto wmap() { return write_map_type(this, 0, m_size); }
507 [[nodiscard]] auto rwmap() { return read_write_map_type(this, 0, m_size); }
508
509 [[nodiscard]] auto map() { return read_write_map_type(this, 0, m_size); }
510 [[nodiscard]] auto map() const { return read_map_type(this, 0, m_size); }
511
512 [[nodiscard]] auto map(std::size_t offset, std::size_t length) {
513 return read_write_map_type(this, offset, length);
514 }
515 [[nodiscard]] auto map(std::size_t offset, std::size_t length) const {
516 return read_map_type(this, offset, length);
517 }
518};
519//==============================================================================
520template <GLsizei array_type, typename T>
522 : m_size{}, m_capacity{}, m_usage{usage} {
524}
525//------------------------------------------------------------------------------
526template <GLsizei array_type, typename T>
527buffer<array_type, T>::buffer(const buffer& other) : buffer{other.m_usage} {
528 copy(other);
529}
530//------------------------------------------------------------------------------
531template <GLsizei array_type, typename T>
533 : parent_type{std::move(other)},
534 m_size{std::exchange(other.m_size, 0)},
535 m_capacity{std::exchange(other.m_capacity, 0)},
536 m_usage{other.m_usage} {}
537//------------------------------------------------------------------------------
538template <GLsizei array_type, typename T>
540 m_usage = other.m_usage;
541 copy(other);
542 return *this;
543}
544//------------------------------------------------------------------------------
545template <GLsizei array_type, typename T>
547 parent_type::operator=(std::move(other));
548 std::swap(m_size, other.m_size);
549 std::swap(m_capacity, other.m_capacity);
550 std::swap(m_usage, other.m_usage);
551 return *this;
552}
553//------------------------------------------------------------------------------
554template <GLsizei array_type, typename T>
555auto buffer<array_type, T>::operator=(const std::vector<T>& data) -> buffer& {
556 auto mapped = map();
557 mapped = data;
558 mapped.unmap();
559 return *this;
560}
561//------------------------------------------------------------------------------
562template <GLsizei array_type, typename T>
564 : m_size{}, m_capacity{}, m_usage{usage} {
566 gpu_malloc(n);
567 m_size = n;
568}
569//------------------------------------------------------------------------------
570template <GLsizei array_type, typename T>
572 : m_size{}, m_capacity{}, m_usage{usage} {
574 gpu_malloc(n, initial);
575 m_size = n;
576}
577//------------------------------------------------------------------------------
578template <GLsizei array_type, typename T>
579buffer<array_type, T>::buffer(const std::vector<T>& data, buffer_usage usage)
580 : m_size{}, m_capacity{}, m_usage{usage} {
582 upload_data(data);
583}
584//------------------------------------------------------------------------------
585template <GLsizei array_type, typename T>
587 destroy_handle();
588}
589//------------------------------------------------------------------------------
590template <GLsizei array_type, typename T>
592 gl::create_buffers(1, &id_ref());
593}
594//------------------------------------------------------------------------------
595template <GLsizei array_type, typename T>
597 if (id() != 0) {
598 gl::delete_buffers(1, &id_ref());
599 }
600 set_id(0);
601}
602//------------------------------------------------------------------------------
603template <GLsizei array_type, typename T>
604auto buffer<array_type, T>::upload_data(const T& data) -> void {
605 if constexpr (std::is_arithmetic_v<T>) {
606 using s = tex::settings<T, R>;
607 gl::clear_named_buffer_data(id(), s::internal_format, s::format, s::type,
608 &data);
609 } else {
610 std::vector<T> data(m_capacity, data);
611 gl::named_buffer_data(this->id(), data_size * m_capacity, data.data(),
612 static_cast<GLenum>(m_usage));
613 }
614}
615//------------------------------------------------------------------------------
616template <GLsizei array_type, typename T>
617auto buffer<array_type, T>::upload_data(const std::vector<T>& data) -> void {
618 auto const s = static_cast<GLsizei>(data_size * data.size());
619 if (capacity() < static_cast<GLsizei>(data.size())) {
620 // reallocate new memory
621 gl::named_buffer_data(id(), s, data.data(), static_cast<GLenum>(m_usage));
622 m_size = m_capacity = static_cast<GLsizei>(data.size());
623 } else {
624 // just update buffer
625 gl::named_buffer_data(id(), s, data.data(), static_cast<GLenum>(m_usage));
626 m_size = static_cast<GLsizei>(data.size());
627 }
628}
629//------------------------------------------------------------------------------
630template <GLsizei array_type, typename T>
632 if (capacity() < size) {
633 auto tmp = *this;
634 gpu_malloc(size);
635 copy(tmp);
636 }
637}
638//------------------------------------------------------------------------------
639template <GLsizei array_type, typename T>
641 if (capacity() < size) {
642 reserve(size);
643 }
644 m_size = size;
645}
646//------------------------------------------------------------------------------
647template <GLsizei array_type, typename T>
648auto buffer<array_type, T>::download_data() const -> std::vector<T> {
649 read_map_type map(this, 0, size());
650 std::vector<T> data(size());
651 std::copy(map.begin(), map.end(), data.begin());
652 return data;
653}
654//------------------------------------------------------------------------------
655template <GLsizei array_type, typename T>
657 auto const s = static_cast<GLsizei>(data_size * n);
658 gl::named_buffer_data<void>(this->id(), s, nullptr,
659 static_cast<GLenum>(m_usage));
660 m_capacity = n;
661}
662//------------------------------------------------------------------------------
663template <GLsizei array_type, typename T>
664auto buffer<array_type, T>::gpu_malloc(GLsizei n, const T& initial) -> void {
665 auto const s = static_cast<GLsizei>(data_size * n);
666 if constexpr (std::is_arithmetic_v<T>) {
667 gl::named_buffer_data<void>(this->id(), s, nullptr,
668 static_cast<GLenum>(m_usage));
669 upload_data(initial);
670 } else {
671 std::vector<T> data(n, initial);
672 gl::named_buffer_data(this->id(), s, data.data(),
673 static_cast<GLenum>(m_usage));
674 }
675 m_capacity = n;
676}
677//------------------------------------------------------------------------------
678template <GLsizei array_type, typename T>
680 m_usage = u;
681}
682//------------------------------------------------------------------------------
683template <GLsizei array_type, typename T>
684auto buffer<array_type, T>::bind() const -> void {
685 gl::bind_buffer(array_type, id());
686}
687//------------------------------------------------------------------------------
688template <GLsizei array_type, typename T>
690 gl::bind_buffer(array_type, 0);
691}
692//------------------------------------------------------------------------------
693template <GLsizei array_type, typename T>
694auto buffer<array_type, T>::copy(const this_type& other) -> void {
695 if (capacity() < other.size()) {
696 gpu_malloc(other.size());
697 }
698 gl::copy_named_buffer_sub_data(other.id(), id(), 0, 0,
699 data_size * other.size());
700 m_size = other.size();
701}
702//------------------------------------------------------------------------------
703template <GLsizei array_type, typename T>
704auto buffer<array_type, T>::push_back(T const& t) -> void {
705 if (m_capacity < m_size + 1) {
706 reserve(std::max<GLsizei>(m_size * 2, 1));
707 }
708 ++m_size;
709 at(m_size - 1) = t;
710}
711//------------------------------------------------------------------------------
712template <GLsizei array_type, typename T>
714 --m_size;
715}
716//------------------------------------------------------------------------------
717template <GLsizei array_type, typename T>
718template <typename... Ts>
719auto buffer<array_type, T>::emplace_back(Ts&&... ts) -> void {
720 if (m_capacity < m_size + 1) {
721 // reallocate
722 this_type tmp(*this);
723 gpu_malloc(m_size * 2);
724 copy(tmp);
725 }
726 at(size()) = T(std::forward<Ts>(ts)...);
727 ++m_size;
728}
729//==============================================================================
730} // namespace tatooine::gl
731//==============================================================================
732#endif
non-const buffer iterator
Definition: buffer.h:266
std::ptrdiff_t difference_type
Definition: buffer.h:274
auto operator--(int)
post-decrement iterator
Definition: buffer.h:321
buffer_iterator(buffer_iterator &&) noexcept=default
auto operator++() -> auto &
pre-increment iterator
Definition: buffer.h:302
auto operator++(int)
post-increment iterator
Definition: buffer.h:308
auto operator--() -> auto &
pre-decrement iterator
Definition: buffer.h:315
buffer_iterator(buffer_iterator const &)=default
std::bidirectional_iterator_tag iterator_category
Definition: buffer.h:275
T & reference
Definition: buffer.h:272
T * pointer
Definition: buffer.h:273
buffer_type * m_buffer
Definition: buffer.h:328
auto operator!=(const buffer_iterator &other) const
are two iterators different?
Definition: buffer.h:297
buffer_iterator(buffer_type *buffer, std::size_t idx)
Definition: buffer.h:277
std::size_t m_idx
Definition: buffer.h:329
auto operator==(const buffer_iterator &other) const
are two iterators equal?
Definition: buffer.h:292
T value_type
Definition: buffer.h:271
Definition: buffer.h:19
auto back() -> auto &
Definition: buffer.h:75
auto offset() const
Definition: buffer.h:87
GLsizei m_offset
Definition: buffer.h:29
T * m_gpu_mapping
Definition: buffer.h:31
auto front() -> auto &
Definition: buffer.h:72
GLsizei m_length
Definition: buffer.h:30
auto end()
Definition: buffer.h:84
auto operator=(std::vector< T > const &data) -> buffer_map &
Definition: buffer.h:50
static constexpr auto data_size
Definition: buffer.h:25
~buffer_map()
destructor unmaps the buffer
Definition: buffer.h:59
auto front() const -> auto const &
Definition: buffer.h:73
buffer_map(buffer_map &&)=delete
auto at(std::size_t i) -> auto &
Definition: buffer.h:69
buffer_map(buffer_type const *buffer, GLsizei offset, GLsizei length)
constructor gets a mapping to gpu_buffer
Definition: buffer.h:36
auto begin()
Definition: buffer.h:81
bool m_unmapped
Definition: buffer.h:32
auto operator=(buffer_map &&) -> buffer_map &=delete
auto operator=(buffer_map const &) -> buffer_map &=delete
auto operator[](std::size_t i) const -> auto const &
Definition: buffer.h:79
auto end() const
Definition: buffer.h:85
buffer_type const * m_buffer
Definition: buffer.h:28
auto begin() const
Definition: buffer.h:82
auto operator[](std::size_t i) -> auto &
Definition: buffer.h:78
auto at(std::size_t i) const -> auto const &
Definition: buffer.h:70
T value_type
Definition: buffer.h:21
static constexpr auto array_type
Definition: buffer.h:23
buffer_map(buffer_map const &)=delete
auto back() const -> auto const &
Definition: buffer.h:76
auto unmap()
Definition: buffer.h:61
static constexpr auto access
Definition: buffer.h:22
auto length() const
Definition: buffer.h:88
buffer base class for each of the OpenGL buffer types
Definition: buffer.h:405
auto back() const
Definition: buffer.h:497
auto begin()
Definition: buffer.h:499
buffer(GLsizei n, buffer_usage usage)
Definition: buffer.h:563
static auto unbind() -> void
Definition: buffer.h:689
auto destroy_handle() -> void
Definition: buffer.h:596
cbuffer_iterator< array_type, T > const_iterator
Definition: buffer.h:423
buffer(const std::vector< T > &data, buffer_usage usage)
Definition: buffer.h:579
auto map() const
Definition: buffer.h:510
~buffer()
Definition: buffer.h:586
auto reserve(GLsizei size) -> void
Definition: buffer.h:631
buffer_iterator< array_type, T > iterator
Definition: buffer.h:422
buffer(GLsizei n, const T &initial, buffer_usage usage)
Definition: buffer.h:571
buffer(buffer_usage usage)
Definition: buffer.h:521
rwbuffer_map_element< array_type, T > read_write_element_type
Definition: buffer.h:419
auto back()
Definition: buffer.h:496
static constexpr auto array_type
Definition: buffer.h:412
auto download_data() const -> std::vector< T >
Definition: buffer.h:648
auto create_handle() -> void
Definition: buffer.h:591
auto set_usage(buffer_usage) -> void
Definition: buffer.h:679
auto at(std::size_t idx) const
Definition: buffer.h:488
auto upload_data(const T &data) -> void
Definition: buffer.h:604
T value_type
Definition: buffer.h:416
auto size() const
Definition: buffer.h:460
auto resize(GLsizei size) -> void
Definition: buffer.h:640
auto map(std::size_t offset, std::size_t length) const
Definition: buffer.h:515
buffer_usage m_usage
Definition: buffer.h:432
auto map(std::size_t offset, std::size_t length)
Definition: buffer.h:512
auto wmap()
Definition: buffer.h:506
auto copy(const this_type &other) -> void
Definition: buffer.h:694
auto rmap() const
Definition: buffer.h:505
auto begin() const
Definition: buffer.h:502
auto map()
Definition: buffer.h:509
auto bind() const -> void
Definition: buffer.h:684
auto operator=(buffer &&other) noexcept -> buffer &
Definition: buffer.h:546
auto gpu_malloc(GLsizei n, const T &initial) -> void
Definition: buffer.h:664
static constexpr GLsizei data_size
Definition: buffer.h:413
auto emplace_back(Ts &&...) -> void
buffer(const buffer &other)
Definition: buffer.h:527
auto gpu_malloc(GLsizei n) -> void
Definition: buffer.h:656
wbuffer_map< array_type, T > write_map_type
Definition: buffer.h:426
wbuffer_map_element< array_type, T > write_only_element_type
Definition: buffer.h:420
auto read_write_element_at(std::size_t idx)
Definition: buffer.h:477
auto empty() const -> bool
Definition: buffer.h:459
auto capacity() const
Definition: buffer.h:461
auto operator=(const std::vector< T > &data) -> buffer &
Definition: buffer.h:555
auto at(std::size_t idx)
Definition: buffer.h:487
buffer(buffer &&other) noexcept
Definition: buffer.h:532
auto operator=(const buffer &other) -> buffer &
Definition: buffer.h:539
auto push_back(T const &) -> void
Definition: buffer.h:704
auto end() const
Definition: buffer.h:503
auto clear()
Definition: buffer.h:465
auto front()
Definition: buffer.h:493
GLsizei m_capacity
Definition: buffer.h:431
rbuffer_map< array_type, T > read_map_type
Definition: buffer.h:425
auto read_element_at(std::size_t idx) const
Definition: buffer.h:480
GLsizei m_size
Definition: buffer.h:430
auto pop_back() -> void
Definition: buffer.h:713
rbuffer_map_element< array_type, T > read_only_element_type
Definition: buffer.h:418
auto operator[](std::size_t idx) const
Definition: buffer.h:491
auto end()
Definition: buffer.h:500
auto operator[](std::size_t idx)
Definition: buffer.h:490
auto write_only_element_at(std::size_t idx)
Definition: buffer.h:483
auto rwmap()
Definition: buffer.h:507
auto front() const
Definition: buffer.h:494
auto upload_data(const std::vector< T > &data) -> void
Definition: buffer.h:617
rwbuffer_map< array_type, T > read_write_map_type
Definition: buffer.h:427
const buffer iterator
Definition: buffer.h:335
cbuffer_iterator(const buffer_type *buffer, std::size_t idx)
Definition: buffer.h:346
cbuffer_iterator(cbuffer_iterator &&other) noexcept=default
T value_type
Definition: buffer.h:340
std::bidirectional_iterator_tag iterator_category
Definition: buffer.h:344
std::size_t m_idx
Definition: buffer.h:399
std::ptrdiff_t difference_type
Definition: buffer.h:343
T & reference
Definition: buffer.h:341
cbuffer_iterator(const cbuffer_iterator &other)=default
const buffer_type * m_buffer
Definition: buffer.h:398
auto operator--(int)
post-decrement iterator
Definition: buffer.h:391
auto operator!=(const cbuffer_iterator &other) const
are two iterators different?
Definition: buffer.h:367
auto operator=(const cbuffer_iterator &other) -> cbuffer_iterator &=default
auto operator--() -> auto &
pre-decrement iterator
Definition: buffer.h:385
T * pointer
Definition: buffer.h:342
auto operator++() -> auto &
pre-increment iterator
Definition: buffer.h:372
auto operator==(const cbuffer_iterator &other) const
are two iterators equal?
Definition: buffer.h:362
auto operator++(int)
post-increment iterator
Definition: buffer.h:378
auto operator=(cbuffer_iterator &&other) noexcept -> cbuffer_iterator &=default
auto operator*() const -> T
get the buffer element the iterator refers to
Definition: buffer.h:359
Returned by buffer::operator[] const for reading single elements.
Definition: buffer.h:127
const buffer_type * m_buffer
Definition: buffer.h:133
auto operator=(rbuffer_map_element &&other) noexcept -> rbuffer_map_element &=default
auto operator=(const rbuffer_map_element &other) -> rbuffer_map_element &=default
auto operator!=(const T &t) const -> bool
Definition: buffer.h:157
auto download() const
Definition: buffer.h:152
auto operator<=(const T &t) const -> bool
Definition: buffer.h:161
auto operator==(const T &t) const -> bool
Definition: buffer.h:156
rbuffer_map_element(const buffer_type *buffer, std::size_t idx)
Definition: buffer.h:137
rbuffer_map_element(const rbuffer_map_element &other)=default
auto operator>(const T &t) const -> bool
Definition: buffer.h:158
auto operator>=(const T &t) const -> bool
Definition: buffer.h:159
auto operator<(const T &t) const -> bool
Definition: buffer.h:160
std::size_t m_idx
Definition: buffer.h:134
rbuffer_map_element(rbuffer_map_element &&other) noexcept=default
Returned by buffer::operator[] for reading and writing single elements.
Definition: buffer.h:174
auto operator==(const T &t) const -> bool
Definition: buffer.h:213
const buffer_type * m_buffer
Definition: buffer.h:180
rwbuffer_map_element(rwbuffer_map_element &&other) noexcept=default
std::size_t m_idx
Definition: buffer.h:181
auto operator!=(const T &t) const -> bool
Definition: buffer.h:214
rwbuffer_map_element(const buffer_type *buffer, std::size_t idx)
Definition: buffer.h:184
auto download() const
Definition: buffer.h:209
auto operator>=(const T &t) const -> bool
Definition: buffer.h:216
auto operator>(const T &t) const -> bool
Definition: buffer.h:215
rwbuffer_map_element(const rwbuffer_map_element &other)=default
auto operator=(T const &data) -> auto &
for assigning single gpu data element.
Definition: buffer.h:196
auto operator=(rwbuffer_map_element &&other) noexcept -> rwbuffer_map_element &=default
auto operator<=(const T &t) const -> bool
Definition: buffer.h:218
auto operator<(const T &t) const -> bool
Definition: buffer.h:217
auto operator=(const rwbuffer_map_element &other) -> rwbuffer_map_element &=default
Definition: buffer.h:224
std::size_t m_idx
Definition: buffer.h:230
const buffer_type * m_buffer
Definition: buffer.h:229
auto operator=(wbuffer_map_element &&other) noexcept -> wbuffer_map_element &=default
auto operator=(const wbuffer_map_element &other) -> wbuffer_map_element &=default
auto operator=(T const &data) -> auto &
for assigning single gpu data element.
Definition: buffer.h:245
wbuffer_map_element(wbuffer_map_element &&other) noexcept=default
wbuffer_map_element(const wbuffer_map_element &other)=default
wbuffer_map_element(const buffer_type *buffer, std::size_t idx)
Definition: buffer.h:233
Definition: ansiformat.h:6
DLL_API auto map_named_buffer_range(GLuint buffer, GLintptr offset, GLsizei length, GLbitfield access) -> void *
DLL_API auto copy_named_buffer_sub_data(GLuint readBuffer, GLuint writeBuffer, GLintptr readOffset, GLintptr writeOffset, GLsizei size) -> void
buffer_usage
Definition: buffer_usage.h:8
DLL_API auto named_buffer_sub_data(GLuint buffer, GLintptr offset, GLsizei size, const void *data) -> void
DLL_API auto create_buffers(GLsizei n, GLuint *buffers) -> void
auto begin(buffer_map< ArrayType, T, Access > &map)
Definition: buffer.h:94
DLL_API auto unmap_named_buffer(GLuint buffer) -> GLboolean
DLL_API auto delete_buffers(GLsizei n, GLuint *buffers) -> void
auto end(buffer_map< ArrayType, T, Access > &map)
Definition: buffer.h:104
auto operator<<(std::ostream &out, rbuffer_map_element< array_type, T > &data) -> auto &
Definition: buffer.h:165
DLL_API auto clear_named_buffer_data(GLuint buffer, GLenum internalformat, GLenum format, GLenum type, const void *data) -> void
DLL_API auto named_buffer_data(GLuint buffer, GLsizeiptr size, const void *data, GLenum usage) -> void
DLL_API auto bind_buffer(GLenum target, GLuint buffer) -> void
auto constexpr map(F &&f, Ts &&... ts)
maps unary function f to all single parameters of parameter pack ts
Definition: map.h:10
auto size(vec< ValueType, N > const &v)
Definition: vec.h:148
static std::mutex gl_call
Definition: mutexhandler.h:10
Definition: idholder.h:31
auto id() const
Definition: idholder.h:56
Definition: texsettings.h:11
type_list_push_back< this_type, T > push_back
Definition: type_list.h:254