clover: try userptr for CL_MEM_USE_HOST_PTR
authorGrigori Goronzy <greg@chown.ath.cx>
Tue, 19 May 2015 07:28:30 +0000 (09:28 +0200)
committerGrigori Goronzy <greg@chown.ath.cx>
Sat, 23 May 2015 23:14:49 +0000 (01:14 +0200)
According to spec, CL_MEM_USE_HOST_PTR should directly use host memory,
if possible. This is just what userptr is for, so use it.

In case the memory cannot be mapped, a fallback similar to
CL_MEM_COPY_HOST_PTR is used.

v2: constify, drop unneeded cast

Reviewed-by: Francisco Jerez <currojerez@riseup.net>
src/gallium/state_trackers/clover/core/memory.cpp
src/gallium/state_trackers/clover/core/resource.cpp

index 905ebc0fd02996502bd6c9e001031c6d1c599929..055336a33255c236d4f7e7e8b4e22e9bb2372c9d 100644 (file)
@@ -30,7 +30,7 @@ memory_obj::memory_obj(clover::context &ctx, cl_mem_flags flags,
                        size_t size, void *host_ptr) :
    context(ctx), _flags(flags),
    _size(size), _host_ptr(host_ptr) {
-   if (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR))
+   if (flags & CL_MEM_COPY_HOST_PTR)
       data.append((char *)host_ptr, size);
 }
 
index 8ed4c4284e1f1056fa45b822c515fdd08be6d2a5..78ebafb644fa1ff72501dda4a9e2d7296d0d393f 100644 (file)
@@ -118,6 +118,8 @@ root_resource::root_resource(clover::device &dev, memory_obj &obj,
                              command_queue &q, const std::string &data) :
    resource(dev, obj) {
    pipe_resource info {};
+   const bool user_ptr_support = dev.pipe->get_param(dev.pipe,
+         PIPE_CAP_RESOURCE_FROM_USER_MEMORY);
 
    if (image *img = dynamic_cast<image *>(&obj)) {
       info.format = translate_format(img->format());
@@ -137,7 +139,15 @@ root_resource::root_resource(clover::device &dev, memory_obj &obj,
                 PIPE_BIND_TRANSFER_READ |
                 PIPE_BIND_TRANSFER_WRITE);
 
-   if (obj.flags() & CL_MEM_ALLOC_HOST_PTR) {
+   if (obj.flags() & CL_MEM_USE_HOST_PTR && user_ptr_support) {
+      // Page alignment is normally required for this, just try, hope for the
+      // best and fall back if it fails.
+      pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());
+      if (pipe)
+         return;
+   }
+
+   if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
       info.usage = PIPE_USAGE_STAGING;
    }
 
@@ -145,12 +155,13 @@ root_resource::root_resource(clover::device &dev, memory_obj &obj,
    if (!pipe)
       throw error(CL_OUT_OF_RESOURCES);
 
-   if (!data.empty()) {
+   if (obj.flags() & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) {
+      const void *data_ptr = !data.empty() ? data.data() : obj.host_ptr();
       box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
       unsigned cpp = util_format_get_blocksize(info.format);
 
       q.pipe->transfer_inline_write(q.pipe, pipe, 0, PIPE_TRANSFER_WRITE,
-                                    rect, data.data(), cpp * info.width0,
+                                    rect, data_ptr, cpp * info.width0,
                                     cpp * info.width0 * info.height0);
    }
 }