1#ifndef TATOOINE_GL_BUFFER
2#define TATOOINE_GL_BUFFER
15template <GLsizei array_type,
typename T>
18template <GLsizei ArrayType,
typename T, GLbitfield Access>
22 static constexpr auto access = Access;
52 for (std::size_t i = 0; i <
size(data); ++i) {
73 auto front() const -> auto const& {
return at(0); }
79 auto operator[](std::size_t i)
const ->
auto const& {
return at(i); }
93template <GLsizei ArrayType,
typename T, GLbitfield Access>
98template <GLsizei ArrayType,
typename T, GLbitfield Access>
103template <GLsizei ArrayType,
typename T, GLbitfield Access>
108template <GLsizei ArrayType,
typename T, GLbitfield Access>
115template <GLsizei array_type,
typename T>
118template <GLsizei array_type,
typename T>
121template <GLsizei array_type,
typename T>
126template <GLsizei array_type,
typename T>
164template <GLsizei array_type,
typename T>
167 out << data.download();
173template <GLsizei array_type,
typename T>
199 std::cout <<
"attention!\n";
223template <GLsizei array_type,
typename T>
248 std::cout <<
"attention!\n";
256template <GLsizei array_type,
typename T>
259 out << data.download();
265template <GLsizei array_type,
typename T>
334template <GLsizei array_type,
typename T>
404template <GLsizei ArrayType,
typename T>
474 template <
typename... Ts>
491 [[nodiscard]]
auto operator[](std::size_t idx)
const {
return at(idx); }
494 [[nodiscard]]
auto front()
const {
return at(0); }
512 [[nodiscard]]
auto map(std::size_t offset, std::size_t length) {
515 [[nodiscard]]
auto map(std::size_t offset, std::size_t length)
const {
520template <GLsizei array_type,
typename T>
522 : m_size{}, m_capacity{}, m_usage{usage} {
526template <GLsizei array_type,
typename T>
531template <GLsizei array_type,
typename T>
534 m_size{std::exchange(other.m_size, 0)},
535 m_capacity{std::exchange(other.m_capacity, 0)},
536 m_usage{other.m_usage} {}
538template <GLsizei array_type,
typename T>
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);
554template <GLsizei array_type,
typename T>
562template <GLsizei array_type,
typename T>
564 : m_size{}, m_capacity{}, m_usage{usage} {
570template <GLsizei array_type,
typename T>
572 : m_size{}, m_capacity{}, m_usage{usage} {
578template <GLsizei array_type,
typename T>
580 : m_size{}, m_capacity{}, m_usage{usage} {
585template <GLsizei array_type,
typename T>
590template <GLsizei array_type,
typename T>
595template <GLsizei array_type,
typename T>
603template <GLsizei array_type,
typename T>
605 if constexpr (std::is_arithmetic_v<T>) {
610 std::vector<T> data(m_capacity, data);
612 static_cast<GLenum
>(m_usage));
616template <GLsizei array_type,
typename T>
618 auto const s =
static_cast<GLsizei>(data_size * data.size());
619 if (capacity() <
static_cast<GLsizei>(data.size())) {
622 m_size = m_capacity =
static_cast<GLsizei>(data.size());
626 m_size =
static_cast<GLsizei>(data.size());
630template <GLsizei array_type,
typename T>
632 if (capacity() <
size) {
639template <GLsizei array_type,
typename T>
641 if (capacity() <
size) {
647template <GLsizei array_type,
typename T>
650 std::vector<T> data(
size());
651 std::copy(
map.begin(),
map.end(), data.begin());
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));
663template <GLsizei array_type,
typename T>
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);
671 std::vector<T> data(n, initial);
673 static_cast<GLenum
>(m_usage));
678template <GLsizei array_type,
typename T>
683template <GLsizei array_type,
typename T>
688template <GLsizei array_type,
typename T>
693template <GLsizei array_type,
typename T>
695 if (capacity() < other.size()) {
696 gpu_malloc(other.size());
699 data_size * other.size());
700 m_size = other.size();
703template <GLsizei array_type,
typename T>
705 if (m_capacity < m_size + 1) {
706 reserve(std::max<GLsizei>(m_size * 2, 1));
712template <GLsizei array_type,
typename T>
717template <GLsizei array_type,
typename T>
718template <
typename... Ts>
720 if (m_capacity < m_size + 1) {
723 gpu_malloc(m_size * 2);
726 at(
size()) = T(std::forward<Ts>(ts)...);
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
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
~cbuffer_iterator()=default
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
~rbuffer_map_element()=default
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
~rwbuffer_map_element()=default
auto operator=(const rwbuffer_map_element &other) -> rwbuffer_map_element &=default
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()=default
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