mesa: extend _mesa_bind_vertex_buffer to take ownership of the buffer reference
authorMarek Olšák <marek.olsak@amd.com>
Sun, 22 Mar 2020 22:13:45 +0000 (18:13 -0400)
committerMarge Bot <eric+marge@anholt.net>
Mon, 27 Apr 2020 11:56:06 +0000 (11:56 +0000)
This reduces overhead of _mesa_reference_buffer_object_ from 6% to 4%
with glthread when profiling the game "torcs" with non-VBO data uploaded
by glthread.

Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4758>

src/mesa/drivers/common/meta.c
src/mesa/main/bufferobj.c
src/mesa/main/varray.c
src/mesa/main/varray.h
src/mesa/vbo/vbo_exec_draw.c
src/mesa/vbo/vbo_save_api.c

index f079d73..59049b9 100644 (file)
@@ -356,7 +356,8 @@ _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
                                    GL_FALSE, GL_FALSE,
                                    offsetof(struct vertex, x));
          _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_GENERIC(0),
-                                  *buf_obj, 0, sizeof(struct vertex), false);
+                                  *buf_obj, 0, sizeof(struct vertex), false,
+                                  false);
          _mesa_enable_vertex_array_attrib(ctx, array_obj,
                                           VERT_ATTRIB_GENERIC(0));
          if (texcoord_size > 0) {
@@ -365,7 +366,8 @@ _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
                                       GL_FALSE, GL_FALSE, GL_FALSE,
                                       offsetof(struct vertex, tex));
             _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_GENERIC(1),
-                                     *buf_obj, 0, sizeof(struct vertex), false);
+                                     *buf_obj, 0, sizeof(struct vertex), false,
+                                     false);
             _mesa_enable_vertex_array_attrib(ctx, array_obj,
                                              VERT_ATTRIB_GENERIC(1));
          }
@@ -375,7 +377,8 @@ _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
                                    GL_FALSE, GL_FALSE,
                                    offsetof(struct vertex, x));
          _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_POS,
-                                  *buf_obj, 0, sizeof(struct vertex), false);
+                                  *buf_obj, 0, sizeof(struct vertex), false,
+                                  false);
          _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_POS);
 
          if (texcoord_size > 0) {
@@ -384,7 +387,8 @@ _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
                                       GL_FALSE, GL_FALSE,
                                       offsetof(struct vertex, tex));
             _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_TEX(0),
-                                     *buf_obj, 0, sizeof(struct vertex), false);
+                                     *buf_obj, 0, sizeof(struct vertex), false,
+                                     false);
             _mesa_enable_vertex_array_attrib(ctx, array_obj,
                                              VERT_ATTRIB_TEX(0));
          }
@@ -395,7 +399,8 @@ _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
                                       GL_FALSE, GL_FALSE,
                                       offsetof(struct vertex, r));
             _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_COLOR0,
-                                     *buf_obj, 0, sizeof(struct vertex), false);
+                                     *buf_obj, 0, sizeof(struct vertex), false,
+                                     false);
             _mesa_enable_vertex_array_attrib(ctx, array_obj,
                                              VERT_ATTRIB_COLOR0);
          }
@@ -3397,7 +3402,7 @@ _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
                                 offsetof(struct vertex, x));
       _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_POS,
                                drawtex->buf_obj, 0, sizeof(struct vertex),
-                               false);
+                               false, false);
       _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_POS);
 
 
@@ -3409,7 +3414,7 @@ _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
                                    offsetof(struct vertex, st[i]));
          _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_TEX(i),
                                   drawtex->buf_obj, 0, sizeof(struct vertex),
-                                  false);
+                                  false, false);
          _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_TEX(i));
       }
    }
index 4e10cc4..c8116cf 100644 (file)
@@ -1148,7 +1148,7 @@ unbind(struct gl_context *ctx,
    if (vao->BufferBinding[index].BufferObj == obj) {
       _mesa_bind_vertex_buffer(ctx, vao, index, NULL,
                                vao->BufferBinding[index].Offset,
-                               vao->BufferBinding[index].Stride, true);
+                               vao->BufferBinding[index].Stride, true, false);
    }
 }
 
index 34b6eda..ff743f7 100644 (file)
@@ -199,7 +199,7 @@ _mesa_bind_vertex_buffer(struct gl_context *ctx,
                          GLuint index,
                          struct gl_buffer_object *vbo,
                          GLintptr offset, GLsizei stride,
-                         bool offset_is_int32)
+                         bool offset_is_int32, bool take_vbo_ownership)
 {
    assert(index < ARRAY_SIZE(vao->BufferBinding));
    assert(!vao->SharedAndImmutable);
@@ -223,7 +223,12 @@ _mesa_bind_vertex_buffer(struct gl_context *ctx,
        binding->Offset != offset ||
        binding->Stride != stride) {
 
-      _mesa_reference_buffer_object(ctx, &binding->BufferObj, vbo);
+      if (take_vbo_ownership) {
+         _mesa_reference_buffer_object(ctx, &binding->BufferObj, NULL);
+         binding->BufferObj = vbo;
+      } else {
+         _mesa_reference_buffer_object(ctx, &binding->BufferObj, vbo);
+      }
 
       binding->Offset = offset;
       binding->Stride = stride;
@@ -910,7 +915,7 @@ update_array(struct gl_context *ctx,
       stride : array->Format._ElementSize;
    _mesa_bind_vertex_buffer(ctx, vao, attrib,
                             obj, (GLintptr) ptr,
-                            effectiveStride, false);
+                            effectiveStride, false, false);
 }
 
 
@@ -2940,7 +2945,7 @@ vertex_array_vertex_buffer(struct gl_context *ctx,
    }
 
    _mesa_bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex),
-                            vbo, offset, stride, false);
+                            vbo, offset, stride, false, false);
 }
 
 
@@ -3105,7 +3110,7 @@ vertex_array_vertex_buffers(struct gl_context *ctx,
        */
       for (i = 0; i < count; i++)
          _mesa_bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
-                                  NULL, 0, 16, false);
+                                  NULL, 0, 16, false, false);
 
       return;
    }
@@ -3183,7 +3188,7 @@ vertex_array_vertex_buffers(struct gl_context *ctx,
       }
 
       _mesa_bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
-                               vbo, offsets[i], strides[i], false);
+                               vbo, offsets[i], strides[i], false, false);
    }
 
    _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
index 93fac8b..0db3fa9 100644 (file)
@@ -110,7 +110,7 @@ _mesa_bind_vertex_buffer(struct gl_context *ctx,
                          GLuint index,
                          struct gl_buffer_object *vbo,
                          GLintptr offset, GLsizei stride,
-                         bool offset_is_int32);
+                         bool offset_is_int32, bool take_vbo_ownership);
 
 extern void GLAPIENTRY
 _mesa_VertexPointer_no_error(GLint size, GLenum type, GLsizei stride,
index b3ea9c2..b31c1f9 100644 (file)
@@ -110,7 +110,7 @@ vbo_exec_bind_arrays(struct gl_context *ctx)
    /* Bind the buffer object */
    const GLuint stride = exec->vtx.vertex_size*sizeof(GLfloat);
    _mesa_bind_vertex_buffer(ctx, vao, 0, exec->vtx.bufferobj, buffer_offset,
-                            stride, false);
+                            stride, false, false);
 
    /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space
     * Note that the position/generic0 aliasing is done in the VAO.
index 0839f11..d6f9b53 100644 (file)
@@ -434,7 +434,8 @@ update_vao(struct gl_context *ctx,
     */
 
    /* Bind the buffer object at binding point 0 */
-   _mesa_bind_vertex_buffer(ctx, *vao, 0, bo, buffer_offset, stride, false);
+   _mesa_bind_vertex_buffer(ctx, *vao, 0, bo, buffer_offset, stride, false,
+                            false);
 
    /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space
     * Note that the position/generic0 aliasing is done in the VAO.