gl-indexbuffer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_ARRAY_BUFFER, &ib->buffer, ib->size,
  20. ib->data, usage);
  21. if (!ib->dynamic) {
  22. bfree(ib->data);
  23. ib->data = NULL;
  24. }
  25. return success;
  26. }
  27. indexbuffer_t device_create_indexbuffer(device_t device,
  28. enum gs_index_type type, void *indices, size_t num,
  29. uint32_t flags)
  30. {
  31. struct gs_index_buffer *ib = bmalloc(sizeof(struct gs_index_buffer));
  32. size_t width = type == GS_UNSIGNED_LONG ? sizeof(long) : sizeof(short);
  33. memset(ib, 0, sizeof(struct gs_index_buffer));
  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_create_indexbuffer (GL) failed");
  45. indexbuffer_destroy(ib);
  46. return NULL;
  47. }
  48. return ib;
  49. }
  50. void indexbuffer_destroy(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. void indexbuffer_flush(indexbuffer_t ib)
  60. {
  61. if (!ib->dynamic) {
  62. blog(LOG_ERROR, "Index buffer is not dynamic");
  63. goto fail;
  64. }
  65. if (!update_buffer(GL_ARRAY_BUFFER, ib->buffer, ib->data, ib->size))
  66. goto fail;
  67. return;
  68. fail:
  69. blog(LOG_ERROR, "indexbuffer_flush (GL) failed");
  70. }
  71. void *indexbuffer_getdata(indexbuffer_t ib)
  72. {
  73. return ib->data;
  74. }
  75. size_t indexbuffer_numindices(indexbuffer_t ib)
  76. {
  77. return ib->num;
  78. }
  79. enum gs_index_type indexbuffer_gettype(indexbuffer_t ib)
  80. {
  81. return ib->type;
  82. }
  83. void device_load_indexbuffer(device_t device, indexbuffer_t ib)
  84. {
  85. if (ib == device->cur_index_buffer)
  86. return;
  87. device->cur_index_buffer = ib;
  88. if (!gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer))
  89. blog(LOG_ERROR, "device_load_indexbuffer (GL) failed");
  90. }