gallium/util: add new helpers for user index buffer uploading
authorMarek Olšák <marek.olsak@amd.com>
Wed, 22 Feb 2017 19:52:49 +0000 (20:52 +0100)
committerMarek Olšák <marek.olsak@amd.com>
Fri, 24 Feb 2017 23:03:09 +0000 (00:03 +0100)
v3: split from the etnaviv patch; fix new_ib.buffer leak

Reviewed-by: Brian Paul <brianp@vmware.com>
Tested-by: Brian Paul <brianp@vmware.com> (VMware driver only)
src/gallium/auxiliary/util/u_helpers.c
src/gallium/auxiliary/util/u_helpers.h

index 09020b061a3e4b2d1cbb78b06f42ec01532ef802..35cca82c8e03aa276e4292bd32d03ebe07fdc505 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "util/u_helpers.h"
 #include "util/u_inlines.h"
+#include "util/u_upload_mgr.h"
 
 /**
  * This function is used to copy an array of pipe_vertex_buffer structures,
@@ -109,3 +110,32 @@ util_set_index_buffer(struct pipe_index_buffer *dst,
       memset(dst, 0, sizeof(*dst));
    }
 }
+
+/**
+ * Given a user index buffer, save the structure to "saved", and upload it.
+ */
+bool
+util_save_and_upload_index_buffer(struct pipe_context *pipe,
+                                  const struct pipe_draw_info *info,
+                                  const struct pipe_index_buffer *ib,
+                                  struct pipe_index_buffer *out_saved)
+{
+   struct pipe_index_buffer new_ib = {0};
+   unsigned start_offset = info->start * ib->index_size;
+
+   u_upload_data(pipe->stream_uploader, start_offset,
+                 info->count * ib->index_size, 4,
+                 (char*)ib->user_buffer + start_offset,
+                 &new_ib.offset, &new_ib.buffer);
+   if (!new_ib.buffer)
+      return false;
+   u_upload_unmap(pipe->stream_uploader);
+
+   new_ib.offset -= start_offset;
+   new_ib.index_size = ib->index_size;
+
+   util_set_index_buffer(out_saved, ib);
+   pipe->set_index_buffer(pipe, &new_ib);
+   pipe_resource_reference(&new_ib.buffer, NULL);
+   return true;
+}
index a9a53e4347a4c1ecc0303cf308efad448237a16a..7de960b907d07ac6dd2434a2d3c2ef5b0cd01feb 100644 (file)
@@ -47,6 +47,11 @@ void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,
 void util_set_index_buffer(struct pipe_index_buffer *dst,
                            const struct pipe_index_buffer *src);
 
+bool util_save_and_upload_index_buffer(struct pipe_context *pipe,
+                                       const struct pipe_draw_info *info,
+                                       const struct pipe_index_buffer *ib,
+                                       struct pipe_index_buffer *out_saved);
+
 #ifdef __cplusplus
 }
 #endif