gl-indexbuffer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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,
  20. ib->size, ib->data, usage);
  21. if (!ib->dynamic) {
  22. bfree(ib->data);
  23. ib->data = NULL;
  24. }
  25. return success;
  26. }
  27. gs_indexbuffer_t *device_indexbuffer_create(gs_device_t *device,
  28. enum gs_index_type type,
  29. void *indices, size_t num,
  30. uint32_t flags)
  31. {
  32. struct gs_index_buffer *ib = bzalloc(sizeof(struct gs_index_buffer));
  33. size_t width = type == GS_UNSIGNED_LONG ? 4 : 2;
  34. ib->device = device;
  35. ib->data = indices;
  36. ib->dynamic = flags & GS_DYNAMIC;
  37. ib->num = num;
  38. ib->width = width;
  39. ib->size = width * num;
  40. ib->type = type;
  41. ib->gl_type = type == GS_UNSIGNED_LONG ? GL_UNSIGNED_INT
  42. : GL_UNSIGNED_SHORT;
  43. if (!init_ib(ib)) {
  44. blog(LOG_ERROR, "device_indexbuffer_create (GL) failed");
  45. gs_indexbuffer_destroy(ib);
  46. return NULL;
  47. }
  48. return ib;
  49. }
  50. void gs_indexbuffer_destroy(gs_indexbuffer_t *ib)
  51. {
  52. if (ib) {
  53. if (ib->buffer)
  54. gl_delete_buffers(1, &ib->buffer);
  55. bfree(ib->data);
  56. bfree(ib);
  57. }
  58. }
  59. static inline void gs_indexbuffer_flush_internal(gs_indexbuffer_t *ib,
  60. const void *data)
  61. {
  62. if (!ib->dynamic) {
  63. blog(LOG_ERROR, "Index buffer is not dynamic");
  64. goto fail;
  65. }
  66. if (!update_buffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer, data, ib->size))
  67. goto fail;
  68. return;
  69. fail:
  70. blog(LOG_ERROR, "gs_indexbuffer_flush (GL) failed");
  71. }
  72. void gs_indexbuffer_flush(gs_indexbuffer_t *ib)
  73. {
  74. gs_indexbuffer_flush_internal(ib, ib->data);
  75. }
  76. void gs_indexbuffer_flush_direct(gs_indexbuffer_t *ib, const void *data)
  77. {
  78. gs_indexbuffer_flush_internal(ib, data);
  79. }
  80. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *ib)
  81. {
  82. return ib->data;
  83. }
  84. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *ib)
  85. {
  86. return ib->num;
  87. }
  88. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *ib)
  89. {
  90. return ib->type;
  91. }
  92. void device_load_indexbuffer(gs_device_t *device, gs_indexbuffer_t *ib)
  93. {
  94. device->cur_index_buffer = ib;
  95. }