💿🐜 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.

120 lines
3.6 KiB

  1. /*
  2. * Copyright (C) 2021 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 <cstddef>
  22. #include <cstdint>
  23. #include "gl/buffer-usage.hpp"
  24. namespace gl {
  25. class vertex_array;
  26. /**
  27. * Vertex buffer object (VBO).
  28. */
  29. class vertex_buffer
  30. {
  31. public:
  32. /**
  33. * Creates a vertex buffer, settings its size, uploading its data, and setting its usage hint.
  34. *
  35. * @param size Size of the buffer's data, in bytes.
  36. * @param data Pointer to data that will be copied into the buffer, or `nullptr` if no data is to be copied.
  37. * @param usage Buffer usage hint.
  38. */
  39. explicit vertex_buffer(std::size_t size, const void* data = nullptr, buffer_usage usage = buffer_usage::static_draw);
  40. /// Creates an empty vertex buffer.
  41. vertex_buffer();
  42. /// Destroys a vertex buffer.
  43. ~vertex_buffer();
  44. vertex_buffer(const vertex_buffer&) = delete;
  45. vertex_buffer& operator=(const vertex_buffer&) = delete;
  46. /**
  47. * Repurposes a vertex buffer, changing its usage hint, its size, and replacing its data.
  48. *
  49. * @param usage New usage hint for the buffer.
  50. * @param size New size of the buffer's data, in bytes.
  51. * @param data Pointer to data that will be copied into the buffer, or `nullptr` if no data is to be copied.
  52. */
  53. void repurpose(buffer_usage usage, std::size_t size, const void* data = nullptr);
  54. /**
  55. * Resizes the vertex buffer.
  56. *
  57. * @param size New size of the buffer's data, in bytes.
  58. * @param data Pointer to data that will be copied into the buffer, or `nullptr` if no data is to be copied.
  59. */
  60. void resize(std::size_t size, const void* data = nullptr);
  61. /**
  62. * Writes data into the vertex buffer.
  63. *
  64. * @param offset Offset into the buffer's data, in bytes, where writing will begin.
  65. * @param size Size of the write operation, in bytes.
  66. * @param data Pointer to the data that will be written.
  67. *
  68. * @except std::out_of_range Vertex buffer write operation exceeded buffer bounds.
  69. */
  70. void write(std::size_t offset, std::size_t size, const void* data);
  71. /**
  72. * Reads a subset of the buffer's data from the GL and returns it to the application.
  73. *
  74. * @param offset Offset into the buffer's data, in bytes, where reading will begin.
  75. * @param size Size of the data to read, in bytes.
  76. * @param data Pointer to where the read bytes will be stored.
  77. *
  78. * @except std::out_of_range Vertex buffer read operation exceeded buffer bounds.
  79. */
  80. void read(std::size_t offset, std::size_t size, void* data) const;
  81. /// Returns the size of the buffer's data, in bytes.
  82. std::size_t get_size() const;
  83. /// Return's the buffer's usage hint.
  84. buffer_usage get_usage() const;
  85. private:
  86. friend class vertex_array;
  87. std::uint_fast32_t gl_buffer_id;
  88. std::size_t size;
  89. buffer_usage usage;
  90. };
  91. inline std::size_t vertex_buffer::get_size() const
  92. {
  93. return size;
  94. }
  95. inline buffer_usage vertex_buffer::get_usage() const
  96. {
  97. return usage;
  98. }
  99. } // namespace gl
  100. #endif // ANTKEEPER_GL_VERTEX_BUFFER_HPP