mesa: don't ever bind NullBufferObj for glBindBuffer targets
authorMarek Olšák <marek.olsak@amd.com>
Sun, 22 Mar 2020 02:49:03 +0000 (22:49 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Mon, 6 Apr 2020 14:28:53 +0000 (10:28 -0400)
Since VAOs don't use NullBufferObj for vertex attribs anymore, let's remove
more uses of NullBufferObj.

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

16 files changed:
src/mesa/drivers/x11/xm_dd.c
src/mesa/main/arrayobj.c
src/mesa/main/attrib.c
src/mesa/main/bufferobj.c
src/mesa/main/draw.c
src/mesa/main/get.c
src/mesa/main/pixel.c
src/mesa/main/pixelstore.c
src/mesa/main/queryobj.c
src/mesa/main/texstate.c
src/mesa/main/transformfeedback.c
src/mesa/main/varray.c
src/mesa/tnl/t_rebase.c
src/mesa/tnl/t_split_copy.c
src/mesa/tnl/t_split_inplace.c
src/mesa/vbo/vbo_primitive_restart.c

index 790cbccf50534dd4660194cf8e26a59d3fcc7be6..1f8d78cb541ccdeeb1e71bd9a11f35e79e207087 100644 (file)
@@ -546,7 +546,7 @@ xmesa_DrawPixels_5R6G5B( struct gl_context *ctx,
          XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
       }
 
-      if (unpack->BufferObj->Name) {
+      if (unpack->BufferObj) {
          ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj, MAP_INTERNAL);
       }
    }
index c05627b05b98bc5bf823977b1041b54b8127ea09..cf848bc177e200823f4f1e2828b080470a91d6e4 100644 (file)
@@ -394,10 +394,7 @@ _mesa_initialize_vao(struct gl_context *ctx,
                      GLuint name)
 {
    memcpy(vao, &ctx->Array.DefaultVAOState, sizeof(*vao));
-
    vao->Name = name;
-   _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj,
-                                 ctx->Shared->NullBufferObj);
 }
 
 
@@ -1242,14 +1239,16 @@ vertex_array_element_buffer(struct gl_context *ctx, GLuint vaobj, GLuint buffer,
       } else {
          bufObj = _mesa_lookup_bufferobj(ctx, buffer);
       }
-   } else {
-      bufObj = ctx->Shared->NullBufferObj;
-   }
 
-   if (bufObj) {
+      if (!bufObj)
+         return;
+
       bufObj->UsageHistory |= USAGE_ELEMENT_ARRAY_BUFFER;
-      _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj, bufObj);
+   } else {
+      bufObj = NULL;
    }
+
+   _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj, bufObj);
 }
 
 
@@ -1299,5 +1298,5 @@ _mesa_GetVertexArrayiv(GLuint vaobj, GLenum pname, GLint *param)
       return;
    }
 
-   param[0] = vao->IndexBufferObj->Name;
+   param[0] = vao->IndexBufferObj ? vao->IndexBufferObj->Name : 0;
 }
index ce6c519737fccdfd6dd05a05d2ce62d2b6d8659c..f84844ccea24e53bddbfa7394cae66cc33baa040 100644 (file)
@@ -1666,21 +1666,24 @@ restore_array_attrib(struct gl_context *ctx,
    _mesa_BindVertexArray(src->VAO->Name);
 
    /* Restore or recreate the buffer objects by the names ... */
-   if (is_vao_name_zero || src->ArrayBufferObj->Name == 0 ||
+   if (is_vao_name_zero || !src->ArrayBufferObj ||
        _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
       /* ... and restore its content */
       copy_array_attrib(ctx, dest, src, false);
 
       _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
-                       src->ArrayBufferObj->Name);
+                       src->ArrayBufferObj ?
+                          src->ArrayBufferObj->Name : 0);
    } else {
       copy_array_attrib(ctx, dest, src, true);
    }
 
-   if (is_vao_name_zero || src->VAO->IndexBufferObj->Name == 0 ||
-       _mesa_IsBuffer(src->VAO->IndexBufferObj->Name))
+   if (is_vao_name_zero || !src->VAO->IndexBufferObj ||
+       _mesa_IsBuffer(src->VAO->IndexBufferObj->Name)) {
       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
-                       src->VAO->IndexBufferObj->Name);
+                       src->VAO->IndexBufferObj ?
+                          src->VAO->IndexBufferObj->Name : 0);
+   }
 }
 
 /**
index 9c4630bca12c5daa57c0da39bfba6b69631d33bf..39a620b22e668ede252a39c7bc4f602c0b70dbf0 100644 (file)
@@ -869,35 +869,6 @@ _mesa_init_buffer_objects( struct gl_context *ctx )
    simple_mtx_init(&DummyBufferObject.MinMaxCacheMutex, mtx_plain);
    DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
 
-   _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
-                                 ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
-                                 ctx->Shared->NullBufferObj);
-   _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
-                                 ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
-                                ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer,
-                                 ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer,
-                                ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
-                                ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->ParameterBuffer,
-                                ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer,
-                                ctx->Shared->NullBufferObj);
-
-   _mesa_reference_buffer_object(ctx, &ctx->QueryBuffer,
-                                 ctx->Shared->NullBufferObj);
-
    for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
       _mesa_reference_buffer_object(ctx,
                                    &ctx->UniformBufferBindings[i].BufferObject,
@@ -1012,29 +983,23 @@ bind_buffer_object(struct gl_context *ctx,
 
    /* Get pointer to old buffer object (to be unbound) */
    oldBufObj = *bindTarget;
-   if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
+   if ((oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending) ||
+       (!oldBufObj && buffer == 0))
       return;   /* rebinding the same buffer object- no change */
 
    /*
     * Get pointer to new buffer object (newBufObj)
     */
-   if (buffer == 0) {
-      /* The spec says there's not a buffer object named 0, but we use
-       * one internally because it simplifies things.
-       */
-      newBufObj = ctx->Shared->NullBufferObj;
-   }
-   else {
+   if (buffer != 0) {
       /* non-default buffer object */
       newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
       if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
                                         &newBufObj, "glBindBuffer"))
          return;
-   }
 
-   /* record usage history */
-   if (bindTarget == &ctx->Pack.BufferObj) {
-      newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER;
+      /* record usage history */
+      if (bindTarget == &ctx->Pack.BufferObj)
+         newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER;
    }
 
    /* bind new buffer */
@@ -1050,9 +1015,7 @@ bind_buffer_object(struct gl_context *ctx,
 void
 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
 {
-   /* Bind the NullBufferObj to remove references to those
-    * in the shared context hash table.
-    */
+   /* Bind 0 to remove references to those in the shared context hash table. */
    bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0);
    bind_buffer_object(ctx, &ctx->Array.VAO->IndexBufferObj, 0);
    bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0);
index 5bb254e00d2621191b064096518ecca22afca963..53946a72b2033690411c406afbd7dfa20e4b2d00 100644 (file)
@@ -917,7 +917,8 @@ _mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
              "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
              "base %d\n",
              start, end, type, count,
-             ctx->Array.VAO->IndexBufferObj->Name, basevertex);
+             ctx->Array.VAO->IndexBufferObj ?
+                ctx->Array.VAO->IndexBufferObj->Name : 0, basevertex);
    }
 
    if ((int) start + basevertex < 0 || end + basevertex >= max_element)
index cbfe9c8b309b3b77e07b46a439923e5e619e2d22..f71fd55c814a24fe593f6988d89a844f385ba4c3 100644 (file)
@@ -1009,14 +1009,16 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       v->value_int = (*buffer_obj) ? (*buffer_obj)->Name : 0;
       break;
    case GL_ARRAY_BUFFER_BINDING_ARB:
-      v->value_int = ctx->Array.ArrayBufferObj->Name;
+      buf = ctx->Array.ArrayBufferObj;
+      v->value_int = buf ? buf->Name : 0;
       break;
    case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING_ARB:
       buf = ctx->Array.VAO->BufferBinding[VERT_ATTRIB_TEX(ctx->Array.ActiveTexture)].BufferObj;
       v->value_int = buf ? buf->Name : 0;
       break;
    case GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB:
-      v->value_int = ctx->Array.VAO->IndexBufferObj->Name;
+      buf = ctx->Array.VAO->IndexBufferObj;
+      v->value_int = buf ? buf->Name : 0;
       break;
 
    /* ARB_vertex_array_bgra */
@@ -1031,20 +1033,21 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
 
    /* ARB_copy_buffer */
    case GL_COPY_READ_BUFFER:
-      v->value_int = ctx->CopyReadBuffer->Name;
+      v->value_int = ctx->CopyReadBuffer ? ctx->CopyReadBuffer->Name : 0;
       break;
    case GL_COPY_WRITE_BUFFER:
-      v->value_int = ctx->CopyWriteBuffer->Name;
+      v->value_int = ctx->CopyWriteBuffer ? ctx->CopyWriteBuffer->Name : 0;
       break;
 
    case GL_PIXEL_PACK_BUFFER_BINDING_EXT:
-      v->value_int = ctx->Pack.BufferObj->Name;
+      v->value_int = ctx->Pack.BufferObj ? ctx->Pack.BufferObj->Name : 0;
       break;
    case GL_PIXEL_UNPACK_BUFFER_BINDING_EXT:
-      v->value_int = ctx->Unpack.BufferObj->Name;
+      v->value_int = ctx->Unpack.BufferObj ? ctx->Unpack.BufferObj->Name : 0;
       break;
    case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
-      v->value_int = ctx->TransformFeedback.CurrentBuffer->Name;
+      v->value_int = ctx->TransformFeedback.CurrentBuffer ?
+                        ctx->TransformFeedback.CurrentBuffer->Name : 0;
       break;
    case GL_TRANSFORM_FEEDBACK_BUFFER_PAUSED:
       v->value_int = ctx->TransformFeedback.CurrentObject->Paused;
@@ -1120,7 +1123,7 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
 
    /* GL_ARB_texture_buffer_object */
    case GL_TEXTURE_BUFFER_ARB:
-      v->value_int = ctx->Texture.BufferObject->Name;
+      v->value_int = ctx->Texture.BufferObject ? ctx->Texture.BufferObject->Name : 0;
       break;
    case GL_TEXTURE_BINDING_BUFFER_ARB:
       unit = ctx->Texture.CurrentUnit;
@@ -1150,15 +1153,15 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       break;
    /* GL_ARB_uniform_buffer_object */
    case GL_UNIFORM_BUFFER_BINDING:
-      v->value_int = ctx->UniformBuffer->Name;
+      v->value_int = ctx->UniformBuffer ? ctx->UniformBuffer->Name : 0;
       break;
    /* GL_ARB_shader_storage_buffer_object */
    case GL_SHADER_STORAGE_BUFFER_BINDING:
-      v->value_int = ctx->ShaderStorageBuffer->Name;
+      v->value_int = ctx->ShaderStorageBuffer ? ctx->ShaderStorageBuffer->Name : 0;
       break;
    /* GL_ARB_query_buffer_object */
    case GL_QUERY_BUFFER_BINDING:
-      v->value_int = ctx->QueryBuffer->Name;
+      v->value_int = ctx->QueryBuffer ? ctx->QueryBuffer->Name : 0;
       break;
    /* GL_ARB_timer_query */
    case GL_TIMESTAMP:
@@ -1179,11 +1182,7 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       break;
    /* GL_ARB_shader_atomic_counters */
    case GL_ATOMIC_COUNTER_BUFFER_BINDING:
-      if (ctx->AtomicBuffer) {
-         v->value_int = ctx->AtomicBuffer->Name;
-      } else {
-         v->value_int = 0;
-      }
+      v->value_int = ctx->AtomicBuffer ? ctx->AtomicBuffer->Name : 0;
       break;
    /* GL 4.3 */
    case GL_NUM_SHADING_LANGUAGE_VERSIONS:
@@ -1191,11 +1190,11 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       break;
    /* GL_ARB_draw_indirect */
    case GL_DRAW_INDIRECT_BUFFER_BINDING:
-      v->value_int = ctx->DrawIndirectBuffer->Name;
+      v->value_int = ctx->DrawIndirectBuffer ? ctx->DrawIndirectBuffer->Name: 0;
       break;
    /* GL_ARB_indirect_parameters */
    case GL_PARAMETER_BUFFER_BINDING_ARB:
-      v->value_int = ctx->ParameterBuffer->Name;
+      v->value_int = ctx->ParameterBuffer ? ctx->ParameterBuffer->Name : 0;
       break;
    /* GL_ARB_separate_shader_objects */
    case GL_PROGRAM_PIPELINE_BINDING:
@@ -1207,7 +1206,8 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       break;
    /* GL_ARB_compute_shader */
    case GL_DISPATCH_INDIRECT_BUFFER_BINDING:
-      v->value_int = ctx->DispatchIndirectBuffer->Name;
+      v->value_int = ctx->DispatchIndirectBuffer ?
+                        ctx->DispatchIndirectBuffer->Name : 0;
       break;
    /* GL_ARB_multisample */
    case GL_SAMPLES:
index b234bacd9b6d5102774da28f47e029331bc1c1d4..f5856a0ea79dcdfe457583e45891e8fad7c29b1c 100644 (file)
@@ -155,8 +155,7 @@ validate_pbo_access(struct gl_context *ctx,
 
    /* restore */
    _mesa_reference_buffer_object(ctx,
-                                 &ctx->DefaultPacking.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+                                 &ctx->DefaultPacking.BufferObj, NULL);
 
    if (!ok) {
       if (_mesa_is_bufferobj(pack->BufferObj)) {
index 6123da9227818eda9daa3e77bc9c94776c4913da..a7c3492b604b394566fcb782146480ea9e1396a0 100644 (file)
@@ -272,8 +272,7 @@ _mesa_init_pixelstore(struct gl_context *ctx)
    ctx->Pack.CompressedBlockHeight = 0;
    ctx->Pack.CompressedBlockDepth = 0;
    ctx->Pack.CompressedBlockSize = 0;
-   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
    ctx->Unpack.Alignment = 4;
    ctx->Unpack.RowLength = 0;
    ctx->Unpack.ImageHeight = 0;
@@ -287,8 +286,7 @@ _mesa_init_pixelstore(struct gl_context *ctx)
    ctx->Unpack.CompressedBlockHeight = 0;
    ctx->Unpack.CompressedBlockDepth = 0;
    ctx->Unpack.CompressedBlockSize = 0;
-   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
 
    /*
     * _mesa_unpack_image() returns image data in this format.  When we
@@ -305,8 +303,7 @@ _mesa_init_pixelstore(struct gl_context *ctx)
    ctx->DefaultPacking.SwapBytes = GL_FALSE;
    ctx->DefaultPacking.LsbFirst = GL_FALSE;
    ctx->DefaultPacking.Invert = GL_FALSE;
-   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
 }
 
 
index 032f9e9a43719204f2071e2bd18d35da7da5b65b..0bbc5cfb88779086e7ee3530319da3a216a8b8da 100644 (file)
@@ -825,7 +825,7 @@ get_query_object(struct gl_context *ctx, const char *func,
       return;
    }
 
-   if (buf && buf != ctx->Shared->NullBufferObj) {
+   if (buf) {
       bool is_64bit = ptype == GL_INT64_ARB ||
          ptype == GL_UNSIGNED_INT64_ARB;
       if (!_mesa_has_ARB_query_buffer_object(ctx)) {
index 9b64b64ae583a4c36489fdb95b93eb36e85e0b05..dfcfc4e8015f0d5d0e9a64aee3e72dcab0f3fdf6 100644 (file)
@@ -1072,8 +1072,7 @@ _mesa_init_texture(struct gl_context *ctx)
       return GL_FALSE;
 
    /* GL_ARB_texture_buffer_object */
-   _mesa_reference_buffer_object(ctx, &ctx->Texture.BufferObject,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->Texture.BufferObject, NULL);
 
    ctx->Texture.NumCurrentTexUsed = 0;
 
index 06719b1ce827dda23a6c777607c809abf3008534..b9cbe5a309b0c05b2f9f2fb2a4e12bef850aa48c 100644 (file)
@@ -142,8 +142,7 @@ _mesa_init_transform_feedback(struct gl_context *ctx)
    ctx->TransformFeedback.Objects = _mesa_NewHashTable();
 
    _mesa_reference_buffer_object(ctx,
-                                 &ctx->TransformFeedback.CurrentBuffer,
-                                 ctx->Shared->NullBufferObj);
+                                 &ctx->TransformFeedback.CurrentBuffer, NULL);
 }
 
 
index ec5e71917613b12fcf8616c09e2cc7f7f56ff770..f516d0008d689615cbbd851562627ccc95ab21e5 100644 (file)
@@ -901,10 +901,6 @@ update_array(struct gl_context *ctx,
    assert((vao->NewArrays | ~vao->Enabled) & VERT_BIT(attrib));
    array->Ptr = ptr;
 
-   /* TODO: remove this hack by not using NullBufferObj in callers */
-   if (obj == ctx->Shared->NullBufferObj)
-      obj = NULL;
-
    /* Update the vertex buffer binding */
    GLsizei effectiveStride = stride != 0 ?
       stride : array->Format._ElementSize;
index 07e603b010703e180100324ac7abbe28c0f8cf13..e6e5c9f3dfaafa75826ac4ad076cba726d39c1c1 100644 (file)
@@ -151,7 +151,7 @@ void t_rebase_prims( struct gl_context *ctx,
    } else if (ib) {
       /* Unfortunately need to adjust each index individually.
        */
-      GLboolean map_ib = ib->obj->Name &&
+      GLboolean map_ib = ib->obj &&
                          !ib->obj->Mappings[MAP_INTERNAL].Pointer;
       void *ptr;
 
@@ -184,7 +184,7 @@ void t_rebase_prims( struct gl_context *ctx,
          return;
       }
 
-      tmp_ib.obj = ctx->Shared->NullBufferObj;
+      tmp_ib.obj = NULL;
       tmp_ib.ptr = tmp_indices;
       tmp_ib.count = ib->count;
       tmp_ib.index_size_shift = ib->index_size_shift;
index 5b300e935538e113921a311816f35b670debc51b..9df55c78558cbb8b8d0689bada23b40dcce8c0cc 100644 (file)
@@ -555,7 +555,7 @@ replay_init(struct copy_context *copy)
     */
    copy->dstib.count = 0;        /* duplicates dstelt_nr */
    copy->dstib.index_size_shift = 2;
-   copy->dstib.obj = ctx->Shared->NullBufferObj;
+   copy->dstib.obj = NULL;
    copy->dstib.ptr = copy->dstelt;
 }
 
index 76d3d89094a994e8c55be67fd8a467101532160b..817b641e515663eddc4e8a6a4cd68c0b06eeb3ea 100644 (file)
@@ -228,7 +228,7 @@ split_prims(struct split_context *split)
 
          ib.count = count;
          ib.index_size_shift = 2;
-         ib.obj = split->ctx->Shared->NullBufferObj;
+         ib.obj = NULL;
          ib.ptr = elts;
 
          tmpprim = *prim;
index 830f658d29cbf0548ef7fb6bf0d09501e80fcbb9..1349740249f513a47251a3c7d99e4b8129cfddc3 100644 (file)
@@ -179,8 +179,8 @@ vbo_sw_primitive_restart(struct gl_context *ctx,
    GLuint sub_end_index;
    GLuint restart_index = _mesa_primitive_restart_index(ctx, 1 << ib->index_size_shift);
    struct _mesa_prim temp_prim;
-   GLboolean map_ib = ib->obj->Name && !ib->obj->Mappings[MAP_INTERNAL].Pointer;
-   void *ptr;
+   GLboolean map_ib = ib->obj && !ib->obj->Mappings[MAP_INTERNAL].Pointer;
+   const void *ptr;
 
    /* If there is an indirect buffer, map it and extract the draw params */
    if (indirect) {
@@ -224,7 +224,10 @@ vbo_sw_primitive_restart(struct gl_context *ctx,
                                  ib->obj, MAP_INTERNAL);
    }
 
-   ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
+   if (ib->obj)
+      ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
+   else
+      ptr = ib->ptr;
 
    sub_prims = find_sub_primitives(ptr, 1 << ib->index_size_shift,
                                    0, ib->count, restart_index,