Mesa (main): gallium: Add a u_default_clear_buffer helper

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jun 23 23:41:55 UTC 2022


Module: Mesa
Branch: main
Commit: cd21d32fe404311fe7edc49801513525c19993c0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=cd21d32fe404311fe7edc49801513525c19993c0

Author: Jason Ekstrand <jason.ekstrand at collabora.com>
Date:   Thu Apr 14 11:41:41 2022 -0500

gallium: Add a u_default_clear_buffer helper

[Alyssa: Add a default CPU implementation of pipe->clear_buffer(). This hook is
mandatory for OpenCL support. Even though this implementation isn't optimal by
any means, having a conformant default available in core will lower the barrier
of entry to OpenCL support.]

Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Reviewed-by: Alyssa Rosenzweig <alyssa at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16044>

---

 src/gallium/auxiliary/util/u_transfer.c | 32 ++++++++++++++++++++++++++++++++
 src/gallium/auxiliary/util/u_transfer.h |  6 ++++++
 2 files changed, 38 insertions(+)

diff --git a/src/gallium/auxiliary/util/u_transfer.c b/src/gallium/auxiliary/util/u_transfer.c
index 80576ddf10a..ba3f578caff 100644
--- a/src/gallium/auxiliary/util/u_transfer.c
+++ b/src/gallium/auxiliary/util/u_transfer.c
@@ -39,6 +39,38 @@ void u_default_buffer_subdata(struct pipe_context *pipe,
    pipe_buffer_unmap(pipe, transfer);
 }
 
+void u_default_clear_buffer(struct pipe_context *pipe,
+                            struct pipe_resource *resource,
+                            unsigned offset, unsigned size,
+                            const void *clear_value,
+                            int clear_value_size)
+{
+   struct pipe_transfer *transfer = NULL;
+   struct pipe_box box;
+   uint8_t *map = NULL;
+
+   /* the write flag is implicit by the nature of buffer_subdata */
+   unsigned usage = PIPE_MAP_WRITE;
+
+   /* clear_buffer implicitly discards the rewritten buffer range. */
+   if (offset == 0 && size == resource->width0) {
+      usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
+   } else {
+      usage |= PIPE_MAP_DISCARD_RANGE;
+   }
+
+   u_box_1d(offset, size, &box);
+
+   map = pipe->buffer_map(pipe, resource, 0, usage, &box, &transfer);
+   if (!map)
+      return;
+
+   assert(clear_value_size > 0);
+   for (unsigned off = 0; off < size; off += clear_value_size)
+      memcpy(map + off, clear_value, MIN2(clear_value_size, size - off));
+   pipe_buffer_unmap(pipe, transfer);
+}
+
 void u_default_texture_subdata(struct pipe_context *pipe,
                                struct pipe_resource *resource,
                                unsigned level,
diff --git a/src/gallium/auxiliary/util/u_transfer.h b/src/gallium/auxiliary/util/u_transfer.h
index a885ad83009..5999d92a520 100644
--- a/src/gallium/auxiliary/util/u_transfer.h
+++ b/src/gallium/auxiliary/util/u_transfer.h
@@ -16,6 +16,12 @@ void u_default_buffer_subdata(struct pipe_context *pipe,
                               unsigned usage, unsigned offset,
                               unsigned size, const void *data);
 
+void u_default_clear_buffer(struct pipe_context *pipe,
+                            struct pipe_resource *resource,
+                            unsigned offset, unsigned size,
+                            const void *clear_value,
+                            int clear_value_size);
+
 void u_default_texture_subdata(struct pipe_context *pipe,
                                struct pipe_resource *resource,
                                unsigned level,



More information about the mesa-commit mailing list