gl-indexbuffer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "gl-subsystem.h"
  15. static inline bool init_ib(struct gs_index_buffer *ib)
  16. {
  17. GLenum usage = ib->dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
  18. bool success;
  19. success = gl_create_buffer(GL_ELEMENT_ARRAY_BUFFER, &ib->buffer, ib->size, ib->data, usage);
  20. if (!ib->dynamic) {
  21. bfree(ib->data);
  22. ib->data = NULL;
  23. }
  24. return success;
  25. }
  26. gs_indexbuffer_t *device_indexbuffer_create(gs_device_t *device, enum gs_index_type type, void *indices, size_t num,
  27. uint32_t flags)
  28. {
  29. struct gs_index_buffer *ib = bzalloc(sizeof(struct gs_index_buffer));
  30. size_t width = type == GS_UNSIGNED_LONG ? 4 : 2;
  31. ib->device = device;
  32. ib->data = indices;
  33. ib->dynamic = flags & GS_DYNAMIC;
  34. ib->num = num;
  35. ib->width = width;
  36. ib->size = width * num;
  37. ib->type = type;
  38. ib->gl_type = type == GS_UNSIGNED_LONG ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
  39. if (!init_ib(ib)) {
  40. blog(LOG_ERROR, "device_indexbuffer_create (GL) failed");
  41. gs_indexbuffer_destroy(ib);
  42. return NULL;
  43. }
  44. return ib;
  45. }
  46. void gs_indexbuffer_destroy(gs_indexbuffer_t *ib)
  47. {
  48. if (ib) {
  49. if (ib->buffer)
  50. gl_delete_buffers(1, &ib->buffer);
  51. bfree(ib->data);
  52. bfree(ib);
  53. }
  54. }
  55. static inline void gs_indexbuffer_flush_internal(gs_indexbuffer_t *ib, const void *data)
  56. {
  57. if (!ib->dynamic) {
  58. blog(LOG_ERROR, "Index buffer is not dynamic");
  59. goto fail;
  60. }
  61. if (!update_buffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer, data, ib->size))
  62. goto fail;
  63. return;
  64. fail:
  65. blog(LOG_ERROR, "gs_indexbuffer_flush (GL) failed");
  66. }
  67. void gs_indexbuffer_flush(gs_indexbuffer_t *ib)
  68. {
  69. gs_indexbuffer_flush_internal(ib, ib->data);
  70. }
  71. void gs_indexbuffer_flush_direct(gs_indexbuffer_t *ib, const void *data)
  72. {
  73. gs_indexbuffer_flush_internal(ib, data);
  74. }
  75. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *ib)
  76. {
  77. return ib->data;
  78. }
  79. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *ib)
  80. {
  81. return ib->num;
  82. }
  83. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *ib)
  84. {
  85. return ib->type;
  86. }
  87. void device_load_indexbuffer(gs_device_t *device, gs_indexbuffer_t *ib)
  88. {
  89. device->cur_index_buffer = ib;
  90. }