💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.5 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_GL_VERTEX_BUFFER_HPP
  20. #define ANTKEEPER_GL_VERTEX_BUFFER_HPP
  21. #include <engine/gl/buffer-usage.hpp>
  22. #include <cstddef>
  23. #include <span>
  24. namespace gl {
  25. /**
  26. * Vertex buffer object (VBO).
  27. */
  28. class vertex_buffer
  29. {
  30. public:
  31. /**
  32. * Constructs a vertex buffer.
  33. *
  34. * @param usage Buffer usage hint.
  35. * @param size Buffer size, in bytes.
  36. * @param data Buffer data. If empty, buffer data will not be set.
  37. *
  38. * @except std::out_of_range Vertex buffer construct operation exceeded data bounds.
  39. */
  40. /// @{
  41. vertex_buffer(buffer_usage usage, std::size_t size, std::span<const std::byte> data = {});
  42. inline explicit vertex_buffer(buffer_usage usage, std::span<const std::byte> data = {}):
  43. vertex_buffer(usage, data.size(), data)
  44. {}
  45. inline vertex_buffer():
  46. vertex_buffer(buffer_usage::static_draw, 0)
  47. {}
  48. /// @}
  49. /**
  50. * Constructs a copy of a vertex buffer.
  51. *
  52. * @param buffer Buffer to copy.
  53. */
  54. vertex_buffer(const vertex_buffer& buffer);
  55. /**
  56. * Move-constructs a vertex buffer.
  57. *
  58. * @param buffer Buffer to move.
  59. */
  60. vertex_buffer(vertex_buffer&& buffer);
  61. /// Destroys a vertex buffer.
  62. ~vertex_buffer();
  63. /**
  64. * Copies another vertex buffer.
  65. *
  66. * @param buffer Buffer to copy.
  67. *
  68. * @return Reference to this vertex buffer.
  69. */
  70. vertex_buffer& operator=(const vertex_buffer& buffer);
  71. /**
  72. * Moves another vertex buffer into this buffer.
  73. *
  74. * @param buffer Buffer to move.
  75. *
  76. * @return Reference to this vertex buffer.
  77. */
  78. vertex_buffer& operator=(vertex_buffer&& buffer);
  79. /**
  80. * Repurposes the vertex buffer, changing its usage hint, size, and updating its data.
  81. *
  82. * @param usage New buffer usage hint.
  83. * @param size New buffer size, in bytes.
  84. * @param data New buffer data. If empty, buffer data will not be updated.
  85. *
  86. * @except std::out_of_range Vertex buffer resize operation exceeded data bounds.
  87. */
  88. /// @{
  89. void repurpose(buffer_usage usage, std::size_t size, std::span<const std::byte> data = {});
  90. inline void repurpose(buffer_usage usage, std::span<const std::byte> data)
  91. {
  92. repurpose(usage, data.size(), data);
  93. }
  94. inline void repurpose(buffer_usage usage)
  95. {
  96. repurpose(usage, m_size, {});
  97. }
  98. /// @}
  99. /**
  100. * Resizes the vertex buffer.
  101. *
  102. * @param size New buffer size, in bytes.
  103. * @param data New buffer data. If empty, buffer data will not be updated.
  104. *
  105. * @except std::out_of_range Vertex buffer resize operation exceeded data bounds.
  106. */
  107. /// @{
  108. inline void resize(std::size_t size, std::span<const std::byte> data = {})
  109. {
  110. repurpose(m_usage, size, data);
  111. }
  112. inline void resize(std::span<const std::byte> data)
  113. {
  114. repurpose(m_usage, data.size(), data);
  115. }
  116. /// @}
  117. /**
  118. * Writes data into the vertex buffer.
  119. *
  120. * @param offset Offset into the buffer's data, in bytes, where writing will begin.
  121. * @param data Data to write into the buffer.
  122. *
  123. * @except std::out_of_range Vertex buffer write operation exceeded buffer bounds.
  124. */
  125. /// @{
  126. void write(std::size_t offset, std::span<const std::byte> data);
  127. inline void write(std::span<const std::byte> data)
  128. {
  129. write(0, data);
  130. }
  131. /// @}
  132. /**
  133. * Reads a subset of the buffer's data from the GL and returns it to the application.
  134. *
  135. * @param offset Offset into the buffer's data, in bytes, where reading will begin.
  136. * @param data Data buffer where the read bytes will be stored.
  137. *
  138. * @except std::out_of_range Vertex buffer read operation exceeded buffer bounds.
  139. */
  140. /// @{
  141. void read(std::size_t offset, std::span<std::byte> data) const;
  142. inline void read(std::span<std::byte> data) const
  143. {
  144. read(0, data);
  145. }
  146. /// @}
  147. /**
  148. * Copies a subset of another vertex buffer's data into this vertex buffer.
  149. *
  150. * @param read_buffer Buffer from which data will be read.
  151. * @param copy_size Number of bytes to copy from the read buffer into this buffer.
  152. * @param read_offset Offset into the read buffer's data, in bytes, where reading will begin.
  153. * @param write_offset Offset into the this buffer's data, in bytes, where writing will begin.
  154. *
  155. * @except std::out_of_range Vertex buffer copy operation exceeded read buffer bounds.
  156. * @except std::out_of_range Vertex buffer copy operation exceeded write buffer bounds.
  157. */
  158. void copy(const vertex_buffer& read_buffer, std::size_t copy_size, std::size_t read_offset = 0, std::size_t write_offset = 0);
  159. /// Returns the size of the buffer's data, in bytes.
  160. [[nodiscard]] inline constexpr std::size_t size() const noexcept
  161. {
  162. return m_size;
  163. }
  164. /// Return's the buffer's usage hint.
  165. [[nodiscard]] inline constexpr buffer_usage usage() const noexcept
  166. {
  167. return m_usage;
  168. }
  169. private:
  170. friend class pipeline;
  171. unsigned int m_gl_named_buffer{0};
  172. buffer_usage m_usage{buffer_usage::static_draw};
  173. std::size_t m_size{0};
  174. };
  175. } // namespace gl
  176. #endif // ANTKEEPER_GL_VERTEX_BUFFER_HPP