yolobs-studio/libobs-opengl/gl-vertexbuffer.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

278 lines
7 KiB
C
Raw Permalink Normal View History

2016-02-23 23:16:51 +00: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
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 <graphics/vec3.h>
#include "gl-subsystem.h"
static bool create_buffers(struct gs_vertex_buffer *vb)
{
GLenum usage = vb->dynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW;
size_t i;
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->vertex_buffer,
2019-09-22 21:19:10 +00:00
vb->data->num * sizeof(struct vec3),
vb->data->points, usage))
2016-02-23 23:16:51 +00:00
return false;
if (vb->data->normals) {
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->normal_buffer,
2019-09-22 21:19:10 +00:00
vb->data->num * sizeof(struct vec3),
vb->data->normals, usage))
2016-02-23 23:16:51 +00:00
return false;
}
if (vb->data->tangents) {
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->tangent_buffer,
2019-09-22 21:19:10 +00:00
vb->data->num * sizeof(struct vec3),
vb->data->tangents, usage))
2016-02-23 23:16:51 +00:00
return false;
}
if (vb->data->colors) {
if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->color_buffer,
2019-09-22 21:19:10 +00:00
vb->data->num * sizeof(uint32_t),
vb->data->colors, usage))
2016-02-23 23:16:51 +00:00
return false;
}
da_reserve(vb->uv_buffers, vb->data->num_tex);
2019-09-22 21:19:10 +00:00
da_reserve(vb->uv_sizes, vb->data->num_tex);
2016-02-23 23:16:51 +00:00
for (i = 0; i < vb->data->num_tex; i++) {
GLuint tex_buffer;
2019-09-22 21:19:10 +00:00
struct gs_tvertarray *tv = vb->data->tvarray + i;
2016-02-23 23:16:51 +00:00
size_t size = vb->data->num * sizeof(float) * tv->width;
if (!gl_create_buffer(GL_ARRAY_BUFFER, &tex_buffer, size,
2019-09-22 21:19:10 +00:00
tv->array, usage))
2016-02-23 23:16:51 +00:00
return false;
da_push_back(vb->uv_buffers, &tex_buffer);
2019-09-22 21:19:10 +00:00
da_push_back(vb->uv_sizes, &tv->width);
2016-02-23 23:16:51 +00:00
}
if (!vb->dynamic) {
gs_vbdata_destroy(vb->data);
vb->data = NULL;
}
if (!gl_gen_vertex_arrays(1, &vb->vao))
return false;
return true;
}
gs_vertbuffer_t *device_vertexbuffer_create(gs_device_t *device,
2019-09-22 21:19:10 +00:00
struct gs_vb_data *data,
uint32_t flags)
2016-02-23 23:16:51 +00:00
{
struct gs_vertex_buffer *vb = bzalloc(sizeof(struct gs_vertex_buffer));
2019-09-22 21:19:10 +00:00
vb->device = device;
vb->data = data;
vb->num = data->num;
2016-02-23 23:16:51 +00:00
vb->dynamic = flags & GS_DYNAMIC;
if (!create_buffers(vb)) {
blog(LOG_ERROR, "device_vertexbuffer_create (GL) failed");
gs_vertexbuffer_destroy(vb);
return NULL;
}
return vb;
}
void gs_vertexbuffer_destroy(gs_vertbuffer_t *vb)
{
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,
2019-09-22 21:19:10 +00:00
vb->uv_buffers.array);
2016-02-23 23:16:51 +00:00
if (vb->vao)
gl_delete_vertex_arrays(1, &vb->vao);
da_free(vb->uv_sizes);
da_free(vb->uv_buffers);
gs_vbdata_destroy(vb->data);
bfree(vb);
}
}
2018-02-19 19:54:37 +00:00
static inline void gs_vertexbuffer_flush_internal(gs_vertbuffer_t *vb,
2019-09-22 21:19:10 +00:00
const struct gs_vb_data *data)
2016-02-23 23:16:51 +00:00
{
size_t i;
2019-09-22 21:19:10 +00:00
size_t num_tex = data->num_tex < vb->data->num_tex ? data->num_tex
: vb->data->num_tex;
2016-02-23 23:16:51 +00:00
if (!vb->dynamic) {
blog(LOG_ERROR, "vertex buffer is not dynamic");
goto failed;
}
2018-02-19 19:54:37 +00:00
if (data->points) {
if (!update_buffer(GL_ARRAY_BUFFER, vb->vertex_buffer,
2019-09-22 21:19:10 +00:00
data->points,
data->num * sizeof(struct vec3)))
2018-02-19 19:54:37 +00:00
goto failed;
}
2016-02-23 23:16:51 +00:00
2018-02-19 19:54:37 +00:00
if (vb->normal_buffer && data->normals) {
2016-02-23 23:16:51 +00:00
if (!update_buffer(GL_ARRAY_BUFFER, vb->normal_buffer,
2019-09-22 21:19:10 +00:00
data->normals,
data->num * sizeof(struct vec3)))
2016-02-23 23:16:51 +00:00
goto failed;
}
2018-02-19 19:54:37 +00:00
if (vb->tangent_buffer && data->tangents) {
2016-02-23 23:16:51 +00:00
if (!update_buffer(GL_ARRAY_BUFFER, vb->tangent_buffer,
2019-09-22 21:19:10 +00:00
data->tangents,
data->num * sizeof(struct vec3)))
2016-02-23 23:16:51 +00:00
goto failed;
}
2018-02-19 19:54:37 +00:00
if (vb->color_buffer && data->colors) {
2016-02-23 23:16:51 +00:00
if (!update_buffer(GL_ARRAY_BUFFER, vb->color_buffer,
2019-09-22 21:19:10 +00:00
data->colors, data->num * sizeof(uint32_t)))
2016-02-23 23:16:51 +00:00
goto failed;
}
2018-02-19 19:54:37 +00:00
for (i = 0; i < num_tex; i++) {
2016-02-23 23:16:51 +00:00
GLuint buffer = vb->uv_buffers.array[i];
2019-09-22 21:19:10 +00:00
struct gs_tvertarray *tv = data->tvarray + i;
2018-02-19 19:54:37 +00:00
size_t size = data->num * tv->width * sizeof(float);
2016-02-23 23:16:51 +00:00
if (!update_buffer(GL_ARRAY_BUFFER, buffer, tv->array, size))
goto failed;
}
return;
failed:
blog(LOG_ERROR, "gs_vertexbuffer_flush (GL) failed");
}
2018-02-19 19:54:37 +00:00
void gs_vertexbuffer_flush(gs_vertbuffer_t *vb)
{
gs_vertexbuffer_flush_internal(vb, vb->data);
}
void gs_vertexbuffer_flush_direct(gs_vertbuffer_t *vb,
2019-09-22 21:19:10 +00:00
const struct gs_vb_data *data)
2018-02-19 19:54:37 +00:00
{
gs_vertexbuffer_flush_internal(vb, data);
}
2016-02-23 23:16:51 +00:00
struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vb)
{
return vb->data;
}
static inline GLuint get_vb_buffer(struct gs_vertex_buffer *vb,
2019-09-22 21:19:10 +00:00
enum attrib_type type, size_t index,
GLint *width, GLenum *gl_type)
2016-02-23 23:16:51 +00:00
{
*gl_type = GL_FLOAT;
2019-09-22 21:19:10 +00:00
*width = 4;
2016-02-23 23:16:51 +00:00
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;
}
static bool load_vb_buffer(struct shader_attrib *attrib,
2019-09-22 21:19:10 +00:00
struct gs_vertex_buffer *vb, GLint id)
2016-02-23 23:16:51 +00: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 "
2019-09-22 21:19:10 +00:00
"inputs for vertex shader");
2016-02-23 23:16:51 +00:00
return false;
}
if (!gl_bind_buffer(GL_ARRAY_BUFFER, buffer))
return false;
glVertexAttribPointer(id, width, type, GL_TRUE, 0, 0);
if (!gl_success("glVertexAttribPointer"))
success = false;
glEnableVertexAttribArray(id);
if (!gl_success("glEnableVertexAttribArray"))
success = false;
if (!gl_bind_buffer(GL_ARRAY_BUFFER, 0))
success = false;
return success;
}
2016-05-24 19:53:01 +00:00
bool load_vb_buffers(struct gs_program *program, struct gs_vertex_buffer *vb,
2019-09-22 21:19:10 +00:00
struct gs_index_buffer *ib)
2016-02-23 23:16:51 +00:00
{
struct gs_shader *shader = program->vertex_shader;
size_t i;
if (!gl_bind_vertex_array(vb->vao))
return false;
for (i = 0; i < shader->attribs.num; i++) {
2019-09-22 21:19:10 +00:00
struct shader_attrib *attrib = shader->attribs.array + i;
2016-02-23 23:16:51 +00:00
if (!load_vb_buffer(attrib, vb, program->attribs.array[i]))
return false;
}
2016-05-24 19:53:01 +00:00
if (ib && !gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer))
return false;
2016-02-23 23:16:51 +00:00
return true;
}
void device_load_vertexbuffer(gs_device_t *device, gs_vertbuffer_t *vb)
{
device->cur_vertex_buffer = vb;
}