vo_opengl: split out a helper for drawing primitives

Useful if we want to reduce the size of gl_video.c further.

To some degree this emulates traditional glDrawArrays() usage. It also
leaves a loophole for avoiding a reupload every time by leaving
ptr==NULL, although this is unused for now.
This commit is contained in:
wm4 2015-01-29 17:19:01 +01:00
parent 0bd147bd14
commit 20c5c7e521
3 changed files with 29 additions and 16 deletions

View File

@ -298,6 +298,27 @@ void gl_vao_bind_attribs(struct gl_vao *vao, GLuint program)
gl->BindAttribLocation(program, n, vao->entries[n].name);
}
// Draw the vertex data (as described by the gl_vao_entry entries) in ptr
// to the screen. num is the number of vertexes. prim is usually GL_TRIANGLES.
// If ptr is NULL, then skip the upload, and use the data uploaded with the
// previous call.
void gl_vao_draw_data(struct gl_vao *vao, GLenum prim, void *ptr, size_t num)
{
GL *gl = vao->gl;
if (ptr) {
gl->BindBuffer(GL_ARRAY_BUFFER, vao->buffer);
gl->BufferData(GL_ARRAY_BUFFER, num * vao->stride, ptr, GL_DYNAMIC_DRAW);
gl->BindBuffer(GL_ARRAY_BUFFER, 0);
}
gl_vao_bind(vao);
gl->DrawArrays(prim, 0, num);
gl_vao_unbind(vao);
}
// Create a texture and a FBO using the texture as color attachments.
// gl_target: GL_TEXTURE_2D
// gl_filter: GL_LINEAR

View File

@ -44,8 +44,10 @@ mp_image_t *glGetWindowScreenshot(GL *gl);
void mp_log_source(struct mp_log *log, int lev, const char *src);
struct gl_vao_entry {
// used for shader / glBindAttribLocation
const char *name;
int num_elems;
// glVertexAttribPointer() arguments
int num_elems; // size (number of elements)
GLenum type;
bool normalized;
int offset;
@ -53,9 +55,9 @@ struct gl_vao_entry {
struct gl_vao {
GL *gl;
GLuint vao;
GLuint buffer;
int stride; // always assuming interleaved elements
GLuint vao; // the VAO object, or 0 if unsupported by driver
GLuint buffer; // GL_ARRAY_BUFFER used for the data
int stride; // size of each element (interleaved elements are assumed)
const struct gl_vao_entry *entries;
};
@ -65,6 +67,7 @@ void gl_vao_uninit(struct gl_vao *vao);
void gl_vao_bind(struct gl_vao *vao);
void gl_vao_unbind(struct gl_vao *vao);
void gl_vao_bind_attribs(struct gl_vao *vao, GLuint program);
void gl_vao_draw_data(struct gl_vao *vao, GLenum prim, void *ptr, size_t num);
struct fbotex {
GL *gl;

View File

@ -465,20 +465,9 @@ void gl_video_set_debug(struct gl_video *p, bool enable)
static void draw_triangles(struct gl_video *p, struct vertex *vb, int vert_count)
{
GL *gl = p->gl;
assert(vert_count % 3 == 0);
gl->BindBuffer(GL_ARRAY_BUFFER, p->vao.buffer);
gl->BufferData(GL_ARRAY_BUFFER, vert_count * sizeof(struct vertex), vb,
GL_DYNAMIC_DRAW);
gl->BindBuffer(GL_ARRAY_BUFFER, 0);
gl_vao_bind(&p->vao);
gl->DrawArrays(GL_TRIANGLES, 0, vert_count);
gl_vao_unbind(&p->vao);
gl_vao_draw_data(&p->vao, GL_TRIANGLES, vb, vert_count);
debug_check_gl(p, "after rendering");
}