1
0

gl-indexbuffer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, void *indices, size_t num,
  29. uint32_t flags)
  30. {
  31. struct gs_index_buffer *ib = bzalloc(sizeof(struct gs_index_buffer));
  32. size_t width = type == GS_UNSIGNED_LONG ? sizeof(long) : sizeof(short);
  33. ib->device = device;
  34. ib->data = indices;
  35. ib->dynamic = flags & GS_DYNAMIC;
  36. ib->num = num;
  37. ib->width = width;
  38. ib->size = width * num;
  39. ib->type = type;
  40. ib->gl_type = type == GS_UNSIGNED_LONG ? GL_UNSIGNED_INT :
  41. GL_UNSIGNED_SHORT;
  42. if (!init_ib(ib)) {
  43. blog(LOG_ERROR, "device_indexbuffer_create (GL) failed");
  44. gs_indexbuffer_destroy(ib);
  45. return NULL;
  46. }
  47. return ib;
  48. }
  49. void gs_indexbuffer_destroy(gs_indexbuffer_t *ib)
  50. {
  51. if (ib) {
  52. if (ib->buffer)
  53. gl_delete_buffers(1, &ib->buffer);
  54. bfree(ib->data);
  55. bfree(ib);
  56. }
  57. }
  58. void gs_indexbuffer_flush(gs_indexbuffer_t *ib)
  59. {
  60. if (!ib->dynamic) {
  61. blog(LOG_ERROR, "Index buffer is not dynamic");
  62. goto fail;
  63. }
  64. if (!update_buffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer, ib->data,
  65. ib->size))
  66. goto fail;
  67. return;
  68. fail:
  69. blog(LOG_ERROR, "gs_indexbuffer_flush (GL) failed");
  70. }
  71. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *ib)
  72. {
  73. return ib->data;
  74. }
  75. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *ib)
  76. {
  77. return ib->num;
  78. }
  79. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *ib)
  80. {
  81. return ib->type;
  82. }
  83. void device_load_indexbuffer(gs_device_t *device, gs_indexbuffer_t *ib)
  84. {
  85. device->cur_index_buffer = ib;
  86. }