2013-10-10 23:03:42 -07:00
|
|
|
/******************************************************************************
|
|
|
|
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
|
2013-12-02 21:24:38 -08:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-10-10 23:03:42 -07:00
|
|
|
(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/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-11-26 22:07:27 -08:00
|
|
|
#include <graphics/vec3.h>
|
2013-10-10 23:03:42 -07:00
|
|
|
#include "gl-subsystem.h"
|
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
static bool create_buffers(struct gs_vertex_buffer *vb)
|
2013-10-11 11:41:36 -07:00
|
|
|
{
|
2014-02-16 18:28:21 -08:00
|
|
|
GLenum usage = vb->dynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW;
|
2013-10-11 11:41:36 -07:00
|
|
|
size_t i;
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->vertex_buffer,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3),
|
|
|
|
vb->data->points, usage))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (vb->data->normals) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->normal_buffer,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3),
|
|
|
|
vb->data->normals, usage))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vb->data->tangents) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->tangent_buffer,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3),
|
|
|
|
vb->data->tangents, usage))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vb->data->colors) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->color_buffer,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(uint32_t),
|
|
|
|
vb->data->colors, usage))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
da_reserve(vb->uv_buffers, vb->data->num_tex);
|
2013-10-11 20:14:26 -07:00
|
|
|
da_reserve(vb->uv_sizes, vb->data->num_tex);
|
2013-10-11 11:41:36 -07:00
|
|
|
|
|
|
|
for (i = 0; i < vb->data->num_tex; i++) {
|
|
|
|
GLuint tex_buffer;
|
2014-08-07 23:42:07 -07:00
|
|
|
struct gs_tvertarray *tv = vb->data->tvarray+i;
|
2013-10-11 11:41:36 -07:00
|
|
|
size_t size = vb->data->num * sizeof(float) * tv->width;
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_create_buffer(GL_ARRAY_BUFFER, &tex_buffer, size,
|
|
|
|
tv->array, usage))
|
2013-10-11 11:41:36 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
da_push_back(vb->uv_buffers, &tex_buffer);
|
2013-10-11 20:14:26 -07:00
|
|
|
da_push_back(vb->uv_sizes, &tv->width);
|
2013-10-11 11:41:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!vb->dynamic) {
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_vbdata_destroy(vb->data);
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data = NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:34:05 -08:00
|
|
|
if (!gl_gen_vertex_arrays(1, &vb->vao))
|
|
|
|
return false;
|
|
|
|
|
2013-10-11 11:41:36 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_vertbuffer_t *device_vertexbuffer_create(gs_device_t *device,
|
2014-08-07 23:42:07 -07:00
|
|
|
struct gs_vb_data *data, uint32_t flags)
|
2013-10-10 23:03:42 -07:00
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct gs_vertex_buffer *vb = bzalloc(sizeof(struct gs_vertex_buffer));
|
2013-10-11 13:00:39 -07:00
|
|
|
vb->device = device;
|
|
|
|
vb->data = data;
|
2013-10-16 23:31:18 -07:00
|
|
|
vb->num = data->num;
|
2013-10-11 13:00:39 -07:00
|
|
|
vb->dynamic = flags & GS_DYNAMIC;
|
2013-10-11 11:41:36 -07:00
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
if (!create_buffers(vb)) {
|
2014-08-07 23:42:07 -07:00
|
|
|
blog(LOG_ERROR, "device_vertexbuffer_create (GL) failed");
|
|
|
|
gs_vertexbuffer_destroy(vb);
|
2013-10-11 11:41:36 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vb;
|
2013-10-10 23:03:42 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void gs_vertexbuffer_destroy(gs_vertbuffer_t *vb)
|
2013-10-10 23:03:42 -07:00
|
|
|
{
|
2013-10-11 11:41:36 -07:00
|
|
|
if (vb) {
|
|
|
|
if (vb->vertex_buffer)
|
|
|
|
gl_delete_buffers(1, &vb->vertex_buffer);
|
|
|
|
if (vb->normal_buffer)
|
|
|
|
gl_delete_buffers(1, &vb->normal_buffer);
|
|
|
|
if (vb->tangent_buffer)
|
|
|
|
gl_delete_buffers(1, &vb->tangent_buffer);
|
|
|
|
if (vb->color_buffer)
|
|
|
|
gl_delete_buffers(1, &vb->color_buffer);
|
|
|
|
if (vb->uv_buffers.num)
|
|
|
|
gl_delete_buffers((GLsizei)vb->uv_buffers.num,
|
|
|
|
vb->uv_buffers.array);
|
|
|
|
|
2013-11-15 05:34:05 -08:00
|
|
|
if (vb->vao)
|
|
|
|
gl_delete_vertex_arrays(1, &vb->vao);
|
|
|
|
|
2013-10-11 20:14:26 -07:00
|
|
|
da_free(vb->uv_sizes);
|
2013-10-11 11:41:36 -07:00
|
|
|
da_free(vb->uv_buffers);
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_vbdata_destroy(vb->data);
|
2013-10-11 11:41:36 -07:00
|
|
|
|
|
|
|
bfree(vb);
|
|
|
|
}
|
2013-10-10 23:03:42 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void gs_vertexbuffer_flush(gs_vertbuffer_t *vb)
|
2013-10-10 23:03:42 -07:00
|
|
|
{
|
2013-10-11 11:41:36 -07:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!vb->dynamic) {
|
|
|
|
blog(LOG_ERROR, "vertex buffer is not dynamic");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!update_buffer(GL_ARRAY_BUFFER, vb->vertex_buffer,
|
|
|
|
vb->data->points,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3)))
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (vb->normal_buffer) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!update_buffer(GL_ARRAY_BUFFER, vb->normal_buffer,
|
|
|
|
vb->data->normals,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3)))
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vb->tangent_buffer) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!update_buffer(GL_ARRAY_BUFFER, vb->tangent_buffer,
|
|
|
|
vb->data->tangents,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(struct vec3)))
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vb->color_buffer) {
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!update_buffer(GL_ARRAY_BUFFER, vb->color_buffer,
|
|
|
|
vb->data->colors,
|
2013-10-11 11:41:36 -07:00
|
|
|
vb->data->num * sizeof(uint32_t)))
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vb->data->num_tex; i++) {
|
|
|
|
GLuint buffer = vb->uv_buffers.array[i];
|
2014-08-07 23:42:07 -07:00
|
|
|
struct gs_tvertarray *tv = vb->data->tvarray+i;
|
2013-10-11 11:41:36 -07:00
|
|
|
size_t size = vb->data->num * tv->width * sizeof(float);
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!update_buffer(GL_ARRAY_BUFFER, buffer, tv->array, size))
|
2013-10-11 11:41:36 -07:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2014-08-07 23:42:07 -07:00
|
|
|
blog(LOG_ERROR, "gs_vertexbuffer_flush (GL) failed");
|
2013-10-10 23:03:42 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vb)
|
2013-10-10 23:03:42 -07:00
|
|
|
{
|
2013-10-11 11:41:36 -07:00
|
|
|
return vb->data;
|
2013-10-10 23:03:42 -07:00
|
|
|
}
|
2013-10-11 20:14:26 -07:00
|
|
|
|
|
|
|
static inline GLuint get_vb_buffer(struct gs_vertex_buffer *vb,
|
|
|
|
enum attrib_type type, size_t index, GLint *width,
|
|
|
|
GLenum *gl_type)
|
|
|
|
{
|
|
|
|
*gl_type = GL_FLOAT;
|
|
|
|
*width = 4;
|
|
|
|
|
|
|
|
if (type == ATTRIB_POSITION) {
|
|
|
|
return vb->vertex_buffer;
|
|
|
|
} else if (type == ATTRIB_NORMAL) {
|
|
|
|
return vb->normal_buffer;
|
|
|
|
} else if (type == ATTRIB_TANGENT) {
|
|
|
|
return vb->tangent_buffer;
|
|
|
|
} else if (type == ATTRIB_COLOR) {
|
|
|
|
*gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
return vb->color_buffer;
|
|
|
|
} else if (type == ATTRIB_TEXCOORD) {
|
|
|
|
if (vb->uv_buffers.num <= index)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*width = (GLint)vb->uv_sizes.array[index];
|
|
|
|
return vb->uv_buffers.array[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:13:36 -08:00
|
|
|
static bool load_vb_buffer(struct shader_attrib *attrib,
|
2014-09-19 04:33:49 -07:00
|
|
|
struct gs_vertex_buffer *vb, GLint id)
|
2013-10-11 20:14:26 -07:00
|
|
|
{
|
|
|
|
GLenum type;
|
|
|
|
GLint width;
|
|
|
|
GLuint buffer;
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
buffer = get_vb_buffer(vb, attrib->type, attrib->index, &width, &type);
|
|
|
|
if (!buffer) {
|
|
|
|
blog(LOG_ERROR, "Vertex buffer does not have the required "
|
|
|
|
"inputs for vertex shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gl_bind_buffer(GL_ARRAY_BUFFER, buffer))
|
|
|
|
return false;
|
|
|
|
|
2014-09-19 04:33:49 -07:00
|
|
|
glVertexAttribPointer(id, width, type, GL_TRUE, 0, 0);
|
2013-10-11 20:14:26 -07:00
|
|
|
if (!gl_success("glVertexAttribPointer"))
|
|
|
|
success = false;
|
|
|
|
|
2014-09-19 04:33:49 -07:00
|
|
|
glEnableVertexAttribArray(id);
|
2013-10-16 23:31:18 -07:00
|
|
|
if (!gl_success("glEnableVertexAttribArray"))
|
|
|
|
success = false;
|
|
|
|
|
2013-11-15 05:34:05 -08:00
|
|
|
if (!gl_bind_buffer(GL_ARRAY_BUFFER, 0))
|
|
|
|
success = false;
|
|
|
|
|
2013-10-11 20:14:26 -07:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-09-19 04:33:49 -07:00
|
|
|
bool load_vb_buffers(struct gs_program *program, struct gs_vertex_buffer *vb)
|
2013-10-11 20:14:26 -07:00
|
|
|
{
|
2014-09-19 04:33:49 -07:00
|
|
|
struct gs_shader *shader = program->vertex_shader;
|
2013-10-11 20:14:26 -07:00
|
|
|
size_t i;
|
2013-11-15 05:34:05 -08:00
|
|
|
|
|
|
|
if (!gl_bind_vertex_array(vb->vao))
|
|
|
|
return false;
|
|
|
|
|
2013-10-11 20:14:26 -07:00
|
|
|
for (i = 0; i < shader->attribs.num; i++) {
|
|
|
|
struct shader_attrib *attrib = shader->attribs.array+i;
|
2014-09-19 04:33:49 -07:00
|
|
|
if (!load_vb_buffer(attrib, vb, program->attribs.array[i]))
|
2013-10-11 20:14:26 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void device_load_vertexbuffer(gs_device_t *device, gs_vertbuffer_t *vb)
|
2013-10-12 12:35:38 -07:00
|
|
|
{
|
2014-09-19 04:33:49 -07:00
|
|
|
device->cur_vertex_buffer = vb;
|
2013-10-11 20:14:26 -07:00
|
|
|
}
|