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