changeset 36678:61a9df2e25d0

Use glMapBufferRange by default. It should provide better performance and in addition is available in GLESv3.
author reimar
date Sat, 01 Feb 2014 15:13:00 +0000
parents 967ec6558314
children 36cbede21428
files libvo/gl_common.c libvo/gl_common.h libvo/gl_compat.h libvo/vo_gl.c
diffstat 4 files changed, 47 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libvo/gl_common.c	Sat Feb 01 08:01:55 2014 +0000
+++ b/libvo/gl_common.c	Sat Feb 01 15:13:00 2014 +0000
@@ -99,7 +99,8 @@
 void (GLAPIENTRY *mpglGenBuffers)(GLsizei, GLuint *);
 void (GLAPIENTRY *mpglDeleteBuffers)(GLsizei, const GLuint *);
 void (GLAPIENTRY *mpglBindBuffer)(GLenum, GLuint);
-GLvoid* (GLAPIENTRY *mpglMapBuffer)(GLenum, GLenum);
+static GLvoid* (GLAPIENTRY *mpglMapBuffer)(GLenum, GLenum);
+GLvoid* (GLAPIENTRY *mpglMapBufferRange)(GLenum, ptrdiff_t, ptrdiff_t, unsigned);
 GLboolean (GLAPIENTRY *mpglUnmapBuffer)(GLenum);
 void (GLAPIENTRY *mpglBufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
 void (GLAPIENTRY *mpglCombinerParameterfv)(GLenum, const GLfloat *);
@@ -488,6 +489,7 @@
   {&mpglDeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
   {&mpglBindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
   {&mpglMapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
+  {&mpglMapBufferRange, NULL, {"glMapBufferRange", "glMapBufferRangeARB", NULL}},
   {&mpglUnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
   {&mpglBufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
   {&mpglCombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
@@ -547,6 +549,17 @@
   {NULL}
 };
 
+static GLvoid *wrap_glMapBuffer(GLenum target, ptrdiff_t offset, ptrdiff_t range, unsigned flags) {
+  GLenum access = GL_READ_WRITE;
+  if (offset != 0 || range == 0 || !(flags & (GL_MAP_WRITE_BIT | GL_MAP_READ_BIT)))
+    return NULL;
+  if (!(flags & GL_MAP_WRITE_BIT))
+    access = GL_READ_ONLY;
+  else if (!(flags & GL_MAP_READ_BIT))
+    access = GL_WRITE_ONLY;
+  return mpglMapBuffer(target, access);
+}
+
 /**
  * \brief find the function pointers of some useful OpenGL extensions
  * \param getProcAddress function to resolve function names, may be NULL
@@ -598,6 +611,10 @@
                   !!strstr(allexts, "GL_ARB_shadow") ||
                   !!strstr(allexts, "GL_OES_depth_texture");
   free(allexts);
+
+  // replacement functions
+  if (!mpglMapBufferRange && mpglMapBuffer)
+    mpglMapBufferRange = wrap_glMapBuffer;
 }
 
 /**
--- a/libvo/gl_common.h	Sat Feb 01 08:01:55 2014 +0000
+++ b/libvo/gl_common.h	Sat Feb 01 15:13:00 2014 +0000
@@ -281,7 +281,7 @@
 extern void (GLAPIENTRY *mpglGenBuffers)(GLsizei, GLuint *);
 extern void (GLAPIENTRY *mpglDeleteBuffers)(GLsizei, const GLuint *);
 extern void (GLAPIENTRY *mpglBindBuffer)(GLenum, GLuint);
-extern GLvoid* (GLAPIENTRY *mpglMapBuffer)(GLenum, GLenum);
+extern GLvoid* (GLAPIENTRY *mpglMapBufferRange)(GLenum, ptrdiff_t, ptrdiff_t, unsigned);
 extern GLboolean (GLAPIENTRY *mpglUnmapBuffer)(GLenum);
 extern void (GLAPIENTRY *mpglBufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
 extern void (GLAPIENTRY *mpglCombinerParameterfv)(GLenum, const GLfloat *);
--- a/libvo/gl_compat.h	Sat Feb 01 08:01:55 2014 +0000
+++ b/libvo/gl_compat.h	Sat Feb 01 15:13:00 2014 +0000
@@ -308,9 +308,33 @@
 #ifndef GL_DYNAMIC_DRAW
 #define GL_DYNAMIC_DRAW 0x88E8
 #endif
+#ifndef GL_READ_ONLY
+#define GL_READ_ONLY 0x88B8
+#endif
 #ifndef GL_WRITE_ONLY
 #define GL_WRITE_ONLY 0x88B9
 #endif
+#ifndef GL_READ_WRITE
+#define GL_READ_WRITE 0x88BA
+#endif
+#ifndef GL_MAP_READ_BIT
+#define GL_MAP_READ_BIT 0x0001
+#endif
+#ifndef GL_MAP_WRITE_BIT
+#define GL_MAP_WRITE_BIT 0x0002
+#endif
+#ifndef GL_MAP_INVALIDATE_RANGE_BIT
+#define GL_MAP_INVALIDATE_RANGE_BIT 0x0004
+#endif
+#ifndef GL_MAP_INVALIDATE_BUFFER_BIT
+#define GL_MAP_INVALIDATE_BUFFER_BIT 0x0008
+#endif
+#ifndef GL_MAP_FLUSH_EXPLICIT_BIT
+#define GL_MAP_FLUSH_EXPLICIT_BIT 0x0010
+#endif
+#ifndef GL_MAP_UNSYNCHRONIZED_BIT
+#define GL_MAP_UNSYNCHRONIZED_BIT 0x0020
+#endif
 #ifndef GL_BGR
 #define GL_BGR 0x80E0
 #endif
--- a/libvo/vo_gl.c	Sat Feb 01 08:01:55 2014 +0000
+++ b/libvo/vo_gl.c	Sat Feb 01 15:13:00 2014 +0000
@@ -934,7 +934,7 @@
 
 static int get_pbo_image(mp_image_t *mpi) {
   int needed_size;
-  if (!mpglGenBuffers || !mpglBindBuffer || !mpglBufferData || !mpglMapBuffer) {
+  if (!mpglGenBuffers || !mpglBindBuffer || !mpglBufferData || !mpglMapBufferRange) {
     if (!err_shown)
       mp_msg(MSGT_VO, MSGL_ERR, "[gl] extensions missing for dr\n"
                                 "Expect a _major_ speed penalty\n");
@@ -964,7 +964,7 @@
                      NULL, GL_DYNAMIC_DRAW);
     }
     if (!gl_bufferptr)
-      gl_bufferptr = mpglMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
+      gl_bufferptr = mpglMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, needed_size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
     mpi->priv = gl_bufferptr;
     mpi->planes[0] = (uint8_t *)gl_bufferptr + (-(intptr_t)gl_bufferptr & 31);
     mpglBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
@@ -1002,9 +1002,9 @@
       }
       if (!gl_bufferptr_uv[0]) {
         mpglBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
-        gl_bufferptr_uv[0] = mpglMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
+        gl_bufferptr_uv[0] = mpglMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, mpi->stride[1] * mpi->height, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
         mpglBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
-        gl_bufferptr_uv[1] = mpglMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
+        gl_bufferptr_uv[1] = mpglMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, mpi->stride[1] * mpi->height, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
       }
       mpi->planes[1] = gl_bufferptr_uv[0];
       mpi->planes[2] = gl_bufferptr_uv[1];