tu: Implement fallback linear staging blit for CopyImage
[mesa.git] / src / gallium / state_trackers / clover / core / resource.cpp
index 80bcd2a851a3a0cd33328415ba22d77177088e4f..dd207982588c2684336008aba52e6e9129f6d74e 100644 (file)
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
-// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
-// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-// SOFTWARE.
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
 //
 
 #include "core/resource.hpp"
+#include "core/memory.hpp"
 #include "pipe/p_screen.h"
 #include "util/u_sampler.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
+#include "util/u_inlines.h"
 
 using namespace clover;
 
 namespace {
    class box {
    public:
-      box(const resource::point &origin, const resource::point &size) :
-         pipe({ (int)origin[0], (int)origin[1],
-                (int)origin[2], (int)size[0],
-                (int)size[1], (int)size[2] }) {
+      box(const resource::vector &origin, const resource::vector &size) :
+        pipe({ (int)origin[0], (int16_t)origin[1],
+               (int16_t)origin[2], (int)size[0],
+               (int16_t)size[1], (int16_t)size[2] }) {
       }
 
       operator const pipe_box *() {
@@ -45,17 +47,17 @@ namespace {
    };
 }
 
-resource::resource(clover::device &dev, clover::memory_obj &obj) :
-   dev(dev), obj(obj), pipe(NULL), offset{0} {
+resource::resource(clover::device &dev, memory_obj &obj) :
+   device(dev), obj(obj), pipe(NULL), offset() {
 }
 
 resource::~resource() {
 }
 
 void
-resource::copy(command_queue &q, const point &origin, const point &region,
-               resource &src_res, const point &src_origin) {
-   point p = offset + origin;
+resource::copy(command_queue &q, const vector &origin, const vector &region,
+               resource &src_res, const vector &src_origin) {
+   auto p = offset + origin;
 
    q.pipe->resource_copy_region(q.pipe, pipe, 0, p[0], p[1], p[2],
                                 src_res.pipe, 0,
@@ -64,16 +66,16 @@ resource::copy(command_queue &q, const point &origin, const point &region,
 
 void *
 resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,
-                  const point &origin, const point &region) {
+                  const vector &origin, const vector &region) {
    maps.emplace_back(q, *this, flags, blocking, origin, region);
    return maps.back();
 }
 
 void
 resource::del_map(void *p) {
-   auto it = std::find(maps.begin(), maps.end(), p);
-   if (it != maps.end())
-      maps.erase(it);
+   erase_if([&](const mapping &m) {
+         return static_cast<void *>(m) == p;
+      }, maps);
 }
 
 unsigned
@@ -82,7 +84,7 @@ resource::map_count() const {
 }
 
 pipe_sampler_view *
-resource::bind_sampler_view(clover::command_queue &q) {
+resource::bind_sampler_view(command_queue &q) {
    pipe_sampler_view info;
 
    u_sampler_view_default_template(&info, pipe, pipe->format);
@@ -90,17 +92,16 @@ resource::bind_sampler_view(clover::command_queue &q) {
 }
 
 void
-resource::unbind_sampler_view(clover::command_queue &q,
+resource::unbind_sampler_view(command_queue &q,
                               pipe_sampler_view *st) {
    q.pipe->sampler_view_destroy(q.pipe, st);
 }
 
 pipe_surface *
-resource::bind_surface(clover::command_queue &q, bool rw) {
+resource::bind_surface(command_queue &q, bool rw) {
    pipe_surface info {};
 
    info.format = pipe->format;
-   info.usage = pipe->bind;
    info.writable = rw;
 
    if (pipe->target == PIPE_BUFFER)
@@ -110,15 +111,16 @@ resource::bind_surface(clover::command_queue &q, bool rw) {
 }
 
 void
-resource::unbind_surface(clover::command_queue &q, pipe_surface *st) {
+resource::unbind_surface(command_queue &q, pipe_surface *st) {
    q.pipe->surface_destroy(q.pipe, st);
 }
 
-root_resource::root_resource(clover::device &dev, clover::memory_obj &obj,
-                             clover::command_queue &q,
-                             const std::string &data) :
+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());
@@ -131,51 +133,69 @@ root_resource::root_resource(clover::device &dev, clover::memory_obj &obj,
       info.depth0 = 1;
    }
 
+   info.array_size = 1;
    info.target = translate_target(obj.type());
    info.bind = (PIPE_BIND_SAMPLER_VIEW |
                 PIPE_BIND_COMPUTE_RESOURCE |
-                PIPE_BIND_GLOBAL |
-                PIPE_BIND_TRANSFER_READ |
-                PIPE_BIND_TRANSFER_WRITE);
+                PIPE_BIND_GLOBAL);
+
+   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;
+   }
 
    pipe = dev.pipe->resource_create(dev.pipe, &info);
    if (!pipe)
       throw error(CL_OUT_OF_RESOURCES);
 
-   if (!data.empty()) {
-      box rect { { 0, 0, 0 }, { info.width0, info.height0, info.depth0 } };
+   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,
-                                    cpp * info.width0 * info.height0);
+      if (pipe->target == PIPE_BUFFER)
+         q.pipe->buffer_subdata(q.pipe, pipe, PIPE_TRANSFER_WRITE,
+                                0, info.width0, data_ptr);
+      else
+         q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_TRANSFER_WRITE,
+                                 rect, data_ptr, cpp * info.width0,
+                                 cpp * info.width0 * info.height0);
    }
 }
 
-root_resource::root_resource(clover::device &dev, clover::memory_obj &obj,
-                             clover::root_resource &r) :
+root_resource::root_resource(clover::device &dev, memory_obj &obj,
+                             root_resource &r) :
    resource(dev, obj) {
    assert(0); // XXX -- resource shared among dev and r.dev
 }
 
 root_resource::~root_resource() {
-   dev.pipe->resource_destroy(dev.pipe, pipe);
+   pipe_resource_reference(&this->pipe, NULL);
 }
 
-sub_resource::sub_resource(clover::resource &r, point offset) :
-   resource(r.dev, r.obj) {
-   pipe = r.pipe;
-   offset = r.offset + offset;
+sub_resource::sub_resource(resource &r, const vector &offset) :
+   resource(r.device(), r.obj) {
+   this->pipe = r.pipe;
+   this->offset = r.offset + offset;
 }
 
 mapping::mapping(command_queue &q, resource &r,
                  cl_map_flags flags, bool blocking,
-                 const resource::point &origin,
-                 const resource::point &region) :
-   pctx(q.pipe) {
+                 const resource::vector &origin,
+                 const resource::vector &region) :
+   pctx(q.pipe), pres(NULL) {
    unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_TRANSFER_WRITE : 0 ) |
                      (flags & CL_MAP_READ ? PIPE_TRANSFER_READ : 0 ) |
-                     (blocking ? PIPE_TRANSFER_UNSYNCHRONIZED : 0));
+                     (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
+                      PIPE_TRANSFER_DISCARD_RANGE : 0) |
+                     (!blocking ? PIPE_TRANSFER_UNSYNCHRONIZED : 0));
 
    p = pctx->transfer_map(pctx, r.pipe, 0, usage,
                           box(origin + r.offset, region), &pxfer);
@@ -183,16 +203,29 @@ mapping::mapping(command_queue &q, resource &r,
       pxfer = NULL;
       throw error(CL_OUT_OF_RESOURCES);
    }
+   pipe_resource_reference(&pres, r.pipe);
 }
 
 mapping::mapping(mapping &&m) :
-   pctx(m.pctx), pxfer(m.pxfer), p(m.p) {
-   m.p = NULL;
+   pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
+   m.pctx = NULL;
    m.pxfer = NULL;
+   m.pres = NULL;
+   m.p = NULL;
 }
 
 mapping::~mapping() {
    if (pxfer) {
       pctx->transfer_unmap(pctx, pxfer);
    }
+   pipe_resource_reference(&pres, NULL);
+}
+
+mapping &
+mapping::operator=(mapping m) {
+   std::swap(pctx, m.pctx);
+   std::swap(pxfer, m.pxfer);
+   std::swap(pres, m.pres);
+   std::swap(p, m.p);
+   return *this;
 }