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

102 lines
2.8 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. #include "gl/vertex-buffer.hpp"
  20. #include <stdexcept>
  21. #include <glad/glad.h>
  22. namespace gl {
  23. static constexpr GLenum buffer_usage_lut[] =
  24. {
  25. GL_STREAM_DRAW,
  26. GL_STREAM_READ,
  27. GL_STREAM_COPY,
  28. GL_STATIC_DRAW,
  29. GL_STATIC_READ,
  30. GL_STATIC_COPY,
  31. GL_DYNAMIC_DRAW,
  32. GL_DYNAMIC_READ,
  33. GL_DYNAMIC_COPY
  34. };
  35. vertex_buffer::vertex_buffer(std::size_t size, const void* data, buffer_usage usage):
  36. gl_buffer_id(0),
  37. size(size),
  38. usage(usage)
  39. {
  40. GLenum gl_usage = buffer_usage_lut[static_cast<std::size_t>(usage)];
  41. glGenBuffers(1, &gl_buffer_id);
  42. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  43. glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(size), data, gl_usage);
  44. }
  45. vertex_buffer::vertex_buffer():
  46. vertex_buffer(0, nullptr, buffer_usage::static_draw)
  47. {}
  48. vertex_buffer::~vertex_buffer()
  49. {
  50. glDeleteBuffers(1, &gl_buffer_id);
  51. }
  52. void vertex_buffer::repurpose(buffer_usage usage, std::size_t size, const void* data)
  53. {
  54. this->size = size;
  55. this->usage = usage;
  56. GLenum gl_usage = buffer_usage_lut[static_cast<std::size_t>(usage)];
  57. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  58. glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(size), data, gl_usage);
  59. }
  60. void vertex_buffer::resize(std::size_t size, const void* data)
  61. {
  62. repurpose(usage, size, data);
  63. }
  64. void vertex_buffer::write(std::size_t offset, std::size_t size, const void* data)
  65. {
  66. // Abort empty write operations
  67. if (!size)
  68. return;
  69. // Bounds check
  70. if (offset + size > this->size)
  71. throw std::out_of_range("Vertex buffer write operation exceeded buffer bounds.");
  72. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  73. glBufferSubData(GL_ARRAY_BUFFER, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(size), data);
  74. }
  75. void vertex_buffer::read(std::size_t offset, std::size_t size, void* data) const
  76. {
  77. // Abort empty read operations
  78. if (!size)
  79. return;
  80. // Bounds check
  81. if (offset + size > this->size)
  82. throw std::out_of_range("Vertex buffer read operation exceeded buffer bounds.");
  83. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  84. glGetBufferSubData(GL_ARRAY_BUFFER, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(size), data);
  85. }
  86. } // namespace gl