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