From 3951babc930cebaa59d66647b7712061ae2e4377 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Fri, 11 Oct 2013 13:00:39 -0700 Subject: [PATCH] add GL index buffers and a few tweaks to the helper functions --- libobs-opengl/gl-helpers.c | 29 ++++- libobs-opengl/gl-helpers.h | 5 +- libobs-opengl/gl-indexbuffer.c | 102 ++++++++++++++++++ libobs-opengl/gl-subsystem.c | 27 +---- libobs-opengl/gl-subsystem.h | 12 +++ libobs-opengl/gl-vertexbuffer.c | 49 ++++----- vs/2010/libobs-opengl/libobs-opengl.vcxproj | 1 + .../libobs-opengl.vcxproj.filters | 3 + 8 files changed, 165 insertions(+), 63 deletions(-) create mode 100644 libobs-opengl/gl-indexbuffer.c diff --git a/libobs-opengl/gl-helpers.c b/libobs-opengl/gl-helpers.c index 514ec309f..9607b1215 100644 --- a/libobs-opengl/gl-helpers.c +++ b/libobs-opengl/gl-helpers.c @@ -80,18 +80,37 @@ bool gl_copy_texture(struct gs_device *device, return success; } -bool gl_create_buffer(GLuint *buffer, GLsizeiptr size, const GLvoid *data, - GLenum usage) +bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size, + const GLvoid *data, GLenum usage) { bool success; if (!gl_gen_buffers(1, buffer)) return false; - if (!gl_bind_buffer(GL_ARRAY_BUFFER, *buffer)) + if (!gl_bind_buffer(target, *buffer)) return false; - glBufferData(GL_ARRAY_BUFFER, size, data, usage); + glBufferData(target, size, data, usage); success = gl_success("glBufferData"); - gl_bind_buffer(GL_ARRAY_BUFFER, 0); + gl_bind_buffer(target, 0); + return success; +} + +bool update_buffer(GLenum target, GLuint buffer, void *data, size_t size) +{ + void *ptr; + bool success = true; + + if (!gl_bind_buffer(target, buffer)) + return false; + + ptr = glMapBuffer(target, GL_WRITE_ONLY); + success = gl_success("glMapBuffer"); + if (success && ptr) { + memcpy(ptr, data, size); + glUnmapBuffer(target); + } + + gl_bind_buffer(target, 0); return success; } diff --git a/libobs-opengl/gl-helpers.h b/libobs-opengl/gl-helpers.h index a909aa485..fb54c3f83 100644 --- a/libobs-opengl/gl-helpers.h +++ b/libobs-opengl/gl-helpers.h @@ -87,7 +87,10 @@ extern bool gl_copy_texture(struct gs_device *device, GLuint dst, GLenum dst_target, uint32_t width, uint32_t height); -extern bool gl_create_buffer(GLuint *buffer, GLsizeiptr size, +extern bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size, const GLvoid *data, GLenum usage); +extern bool update_buffer(GLenum target, GLuint buffer, void *data, + size_t size); + #endif diff --git a/libobs-opengl/gl-indexbuffer.c b/libobs-opengl/gl-indexbuffer.c new file mode 100644 index 000000000..71232839b --- /dev/null +++ b/libobs-opengl/gl-indexbuffer.c @@ -0,0 +1,102 @@ +/****************************************************************************** + Copyright (C) 2013 by Hugh Bailey + + 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 3 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 . +******************************************************************************/ + +#include "gl-subsystem.h" + +static inline bool init_ib(struct gs_index_buffer *ib) +{ + GLenum usage = ib->dynamic ? GL_DYNAMIC_COPY : GL_STATIC_DRAW; + bool success; + + success = gl_create_buffer(GL_ARRAY_BUFFER, &ib->buffer, ib->size, + ib->data, usage); + + if (!ib->dynamic) { + bfree(ib->data); + ib->data = NULL; + } + + return success; +} + +indexbuffer_t device_create_indexbuffer(device_t device, + enum gs_index_type type, void *indices, size_t num, + uint32_t flags) +{ + struct gs_index_buffer *ib = bmalloc(sizeof(struct gs_index_buffer)); + size_t width = type == GS_UNSIGNED_LONG ? sizeof(long) : sizeof(short); + memset(ib, 0, sizeof(struct gs_index_buffer)); + + ib->device = device; + ib->data = indices; + ib->dynamic = flags & GS_DYNAMIC; + ib->num = num; + 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_create_indexbuffer (GL) failed"); + indexbuffer_destroy(ib); + return NULL; + } + + return ib; +} + +void indexbuffer_destroy(indexbuffer_t ib) +{ + if (ib) { + if (ib->buffer) + gl_delete_buffers(1, &ib->buffer); + + bfree(ib->data); + bfree(ib); + } +} + +void indexbuffer_flush(indexbuffer_t ib) +{ + if (!ib->dynamic) { + blog(LOG_ERROR, "Index buffer is not dynamic"); + goto fail; + } + + if (!update_buffer(GL_ARRAY_BUFFER, ib->buffer, ib->data, ib->size)) + goto fail; + + return; + +fail: + blog(LOG_ERROR, "indexbuffer_flush (GL) failed"); +} + +void *indexbuffer_getdata(indexbuffer_t ib) +{ + return ib->data; +} + +size_t indexbuffer_numindices(indexbuffer_t ib) +{ + return ib->num; +} + +enum gs_index_type indexbuffer_gettype(indexbuffer_t ib) +{ + return ib->type; +} diff --git a/libobs-opengl/gl-subsystem.c b/libobs-opengl/gl-subsystem.c index 278132f0c..0b7572406 100644 --- a/libobs-opengl/gl-subsystem.c +++ b/libobs-opengl/gl-subsystem.c @@ -112,12 +112,6 @@ samplerstate_t device_create_samplerstate(device_t device, return sampler; } -indexbuffer_t device_create_indexbuffer(device_t device, - enum gs_index_type type, void *indices, size_t num, - uint32_t flags) -{ -} - enum gs_texture_type device_gettexturetype(device_t device, texture_t texture) { @@ -357,24 +351,5 @@ enum gs_color_format volumetexture_getcolorformat(texture_t voltex) void samplerstate_destroy(samplerstate_t samplerstate) { -} - -void indexbuffer_destroy(indexbuffer_t indexbuffer) -{ -} - -void indexbuffer_flush(indexbuffer_t indexbuffer) -{ -} - -void *indexbuffer_getdata(indexbuffer_t indexbuffer) -{ -} - -size_t indexbuffer_numindices(indexbuffer_t indexbuffer) -{ -} - -enum gs_index_type indexbuffer_gettype(indexbuffer_t indexbuffer) -{ + bfree(samplerstate); } diff --git a/libobs-opengl/gl-subsystem.h b/libobs-opengl/gl-subsystem.h index a6819978c..ab03db6a8 100644 --- a/libobs-opengl/gl-subsystem.h +++ b/libobs-opengl/gl-subsystem.h @@ -232,6 +232,18 @@ struct gs_vertex_buffer { struct vb_data *data; }; +struct gs_index_buffer { + GLuint buffer; + enum gs_index_type type; + GLuint gl_type; + + device_t device; + void *data; + size_t num; + size_t size; + bool dynamic; +}; + struct gs_texture { device_t device; enum gs_texture_type type; diff --git a/libobs-opengl/gl-vertexbuffer.c b/libobs-opengl/gl-vertexbuffer.c index 6b043845b..06ae096b2 100644 --- a/libobs-opengl/gl-vertexbuffer.c +++ b/libobs-opengl/gl-vertexbuffer.c @@ -18,51 +18,32 @@ #include "graphics/vec3.h" #include "gl-subsystem.h" -static bool update_buffer(GLuint buffer, void *data, size_t size) -{ - void *ptr; - bool success = true; - - if (!gl_bind_buffer(GL_ARRAY_BUFFER, buffer)) - return false; - - ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - success = gl_success("glMapBuffer"); - if (success && ptr) { - memcpy(ptr, data, size); - glUnmapBuffer(GL_ARRAY_BUFFER); - } - - gl_bind_buffer(GL_ARRAY_BUFFER, 0); - return success; -} - static bool init_vb(struct gs_vertex_buffer *vb) { GLenum usage = vb->dynamic ? GL_DYNAMIC_COPY : GL_STATIC_DRAW; size_t i; - if (!gl_create_buffer(&vb->vertex_buffer, + if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->vertex_buffer, vb->data->num * sizeof(struct vec3), vb->data->points, usage)) return false; if (vb->data->normals) { - if (!gl_create_buffer(&vb->normal_buffer, + if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->normal_buffer, vb->data->num * sizeof(struct vec3), vb->data->normals, usage)) return false; } if (vb->data->tangents) { - if (!gl_create_buffer(&vb->tangent_buffer, + if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->tangent_buffer, vb->data->num * sizeof(struct vec3), vb->data->tangents, usage)) return false; } if (vb->data->colors) { - if (!gl_create_buffer(&vb->color_buffer, + if (!gl_create_buffer(GL_ARRAY_BUFFER, &vb->color_buffer, vb->data->num * sizeof(uint32_t), vb->data->colors, usage)) return false; @@ -75,7 +56,8 @@ static bool init_vb(struct gs_vertex_buffer *vb) struct tvertarray *tv = vb->data->tvarray+i; size_t size = vb->data->num * sizeof(float) * tv->width; - if (!gl_create_buffer(&tex_buffer, size, tv->array, usage)) + if (!gl_create_buffer(GL_ARRAY_BUFFER, &tex_buffer, size, + tv->array, usage)) return false; da_push_back(vb->uv_buffers, &tex_buffer); @@ -95,8 +77,9 @@ vertbuffer_t device_create_vertexbuffer(device_t device, struct gs_vertex_buffer *vb = bmalloc(sizeof(struct gs_vertex_buffer)); memset(vb, 0, sizeof(struct gs_vertex_buffer)); - vb->data = data; - vb->dynamic = flags & GS_DYNAMIC; + vb->device = device; + vb->data = data; + vb->dynamic = flags & GS_DYNAMIC; if (!init_vb(vb)) { blog(LOG_ERROR, "device_create_vertexbuffer (GL) failed"); @@ -142,24 +125,28 @@ void vertexbuffer_flush(vertbuffer_t vb, bool rebuild) goto failed; } - if (!update_buffer(vb->vertex_buffer, vb->data->points, + if (!update_buffer(GL_ARRAY_BUFFER, vb->vertex_buffer, + vb->data->points, vb->data->num * sizeof(struct vec3))) goto failed; if (vb->normal_buffer) { - if (!update_buffer(vb->normal_buffer, vb->data->normals, + if (!update_buffer(GL_ARRAY_BUFFER, vb->normal_buffer, + vb->data->normals, vb->data->num * sizeof(struct vec3))) goto failed; } if (vb->tangent_buffer) { - if (!update_buffer(vb->tangent_buffer, vb->data->tangents, + if (!update_buffer(GL_ARRAY_BUFFER, vb->tangent_buffer, + vb->data->tangents, vb->data->num * sizeof(struct vec3))) goto failed; } if (vb->color_buffer) { - if (!update_buffer(vb->color_buffer, vb->data->colors, + if (!update_buffer(GL_ARRAY_BUFFER, vb->color_buffer, + vb->data->colors, vb->data->num * sizeof(uint32_t))) goto failed; } @@ -169,7 +156,7 @@ void vertexbuffer_flush(vertbuffer_t vb, bool rebuild) struct tvertarray *tv = vb->data->tvarray+i; size_t size = vb->data->num * tv->width * sizeof(float); - if (!update_buffer(buffer, tv->array, size)) + if (!update_buffer(GL_ARRAY_BUFFER, buffer, tv->array, size)) goto failed; } diff --git a/vs/2010/libobs-opengl/libobs-opengl.vcxproj b/vs/2010/libobs-opengl/libobs-opengl.vcxproj index baa4fd490..2c9ec5ee1 100644 --- a/vs/2010/libobs-opengl/libobs-opengl.vcxproj +++ b/vs/2010/libobs-opengl/libobs-opengl.vcxproj @@ -162,6 +162,7 @@ + diff --git a/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters b/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters index 6432ced36..ec394c1d1 100644 --- a/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters +++ b/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters @@ -59,5 +59,8 @@ Source Files + + Source Files + \ No newline at end of file