gallium/u_upload_mgr: reduce dereferences by adding buffer_size
authorMarek Olšák <marek.olsak@amd.com>
Fri, 24 Jan 2020 01:35:48 +0000 (20:35 -0500)
committerMarek Olšák <marek.olsak@amd.com>
Fri, 14 Feb 2020 23:16:27 +0000 (18:16 -0500)
Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3829>

src/gallium/auxiliary/util/u_upload_mgr.c

index 4ac468524d835c6021b487aa7c55b317da03a644..c199ad0edf12050d996b9254e5483bec6d61e6c4 100644 (file)
@@ -51,6 +51,7 @@ struct u_upload_mgr {
    struct pipe_resource *buffer;   /* Upload buffer. */
    struct pipe_transfer *transfer; /* Transfer object for the upload buffer. */
    uint8_t *map;    /* Pointer to the mapped upload buffer. */
+   unsigned buffer_size; /* Same as buffer->width0. */
    unsigned offset; /* Aligned offset to the upload buffer, pointing
                      * at the first unused byte. */
    unsigned flushed_size; /* Size we have flushed by transfer_flush_region. */
@@ -171,6 +172,7 @@ u_upload_release_buffer(struct u_upload_mgr *upload)
    /* Unmap and unreference the upload buffer. */
    upload_unmap_internal(upload, TRUE);
    pipe_resource_reference(&upload->buffer, NULL);
+   upload->buffer_size = 0;
 }
 
 
@@ -181,8 +183,8 @@ u_upload_destroy(struct u_upload_mgr *upload)
    FREE(upload);
 }
 
-
-static void
+/* Return the allocated buffer size or 0 if it failed. */
+static unsigned
 u_upload_alloc_buffer(struct u_upload_mgr *upload, unsigned min_size)
 {
    struct pipe_screen *screen = upload->pipe->screen;
@@ -215,7 +217,7 @@ u_upload_alloc_buffer(struct u_upload_mgr *upload, unsigned min_size)
 
    upload->buffer = screen->resource_create(screen, &buffer);
    if (upload->buffer == NULL)
-      return;
+      return 0;
 
    /* Map the new buffer. */
    upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
@@ -224,10 +226,12 @@ u_upload_alloc_buffer(struct u_upload_mgr *upload, unsigned min_size)
    if (upload->map == NULL) {
       upload->transfer = NULL;
       pipe_resource_reference(&upload->buffer, NULL);
-      return;
+      return 0;
    }
 
+   upload->buffer_size = size;
    upload->offset = 0;
+   return size;
 }
 
 void
@@ -239,7 +243,7 @@ u_upload_alloc(struct u_upload_mgr *upload,
                struct pipe_resource **outbuf,
                void **ptr)
 {
-   unsigned buffer_size = upload->buffer ? upload->buffer->width0 : 0;
+   unsigned buffer_size = upload->buffer_size;
    unsigned offset;
 
    min_out_offset = align(min_out_offset, alignment);
@@ -250,10 +254,10 @@ u_upload_alloc(struct u_upload_mgr *upload,
    /* Make sure we have enough space in the upload buffer
     * for the sub-allocation.
     */
-   if (unlikely(!upload->buffer || offset + size > buffer_size)) {
-      u_upload_alloc_buffer(upload, min_out_offset + size);
+   if (unlikely(offset + size > buffer_size)) {
+      buffer_size = u_upload_alloc_buffer(upload, min_out_offset + size);
 
-      if (unlikely(!upload->buffer)) {
+      if (unlikely(!buffer_size)) {
          *out_offset = ~0;
          pipe_resource_reference(outbuf, NULL);
          *ptr = NULL;
@@ -261,7 +265,6 @@ u_upload_alloc(struct u_upload_mgr *upload,
       }
 
       offset = min_out_offset;
-      buffer_size = upload->buffer->width0;
    }
 
    if (unlikely(!upload->map)) {