Path: blob/21.2-virgl/src/gallium/frontends/clover/core/resource.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "core/resource.hpp"23#include "core/memory.hpp"24#include "pipe/p_screen.h"25#include "util/u_sampler.h"26#include "util/format/u_format.h"27#include "util/u_inlines.h"28#include "util/u_resource.h"2930using namespace clover;3132namespace {33class box {34public:35box(const resource::vector &origin, const resource::vector &size) :36pipe({ (int)origin[0], (int16_t)origin[1],37(int16_t)origin[2], (int)size[0],38(int16_t)size[1], (int16_t)size[2] }) {39}4041operator const pipe_box *() {42return &pipe;43}4445protected:46pipe_box pipe;47};48}4950resource::resource(clover::device &dev, memory_obj &obj) :51device(dev), obj(obj), pipe(NULL), offset() {52}5354resource::~resource() {55}5657void58resource::copy(command_queue &q, const vector &origin, const vector ®ion,59resource &src_res, const vector &src_origin) {60auto p = offset + origin;6162q.pipe->resource_copy_region(q.pipe, pipe, 0, p[0], p[1], p[2],63src_res.pipe, 0,64box(src_res.offset + src_origin, region));65}6667void68resource::clear(command_queue &q, const vector &origin, const vector ®ion,69const std::string &data) {70auto from = offset + origin;7172if (pipe->target == PIPE_BUFFER) {73q.pipe->clear_buffer(q.pipe, pipe, from[0], region[0], data.data(), data.size());74} else {75std::string texture_data;76texture_data.reserve(util_format_get_blocksize(pipe->format));77util_format_pack_rgba(pipe->format, &texture_data[0], data.data(), 1);78q.pipe->clear_texture(q.pipe, pipe, 0, box(from, region), texture_data.data());79}80}8182mapping *83resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,84const vector &origin, const vector ®ion) {85maps.emplace_back(q, *this, flags, blocking, origin, region);86return &maps.back();87}8889void90resource::del_map(void *p) {91erase_if([&](const mapping &m) {92return static_cast<void *>(m) == p;93}, maps);94}9596unsigned97resource::map_count() const {98return maps.size();99}100101pipe_sampler_view *102resource::bind_sampler_view(command_queue &q) {103pipe_sampler_view info;104105u_sampler_view_default_template(&info, pipe, pipe->format);106return q.pipe->create_sampler_view(q.pipe, pipe, &info);107}108109void110resource::unbind_sampler_view(command_queue &q,111pipe_sampler_view *st) {112q.pipe->sampler_view_destroy(q.pipe, st);113}114115pipe_image_view116resource::create_image_view(command_queue &q) {117pipe_image_view view;118view.resource = pipe;119view.format = pipe->format;120view.access = 0;121view.shader_access = PIPE_IMAGE_ACCESS_WRITE;122123if (pipe->target == PIPE_BUFFER) {124view.u.buf.offset = 0;125view.u.buf.size = obj.size();126} else {127view.u.tex.first_layer = 0;128if (util_texture_is_array(pipe->target))129view.u.tex.last_layer = pipe->array_size - 1;130else131view.u.tex.last_layer = 0;132view.u.tex.level = 0;133}134135return view;136}137138pipe_surface *139resource::bind_surface(command_queue &q, bool rw) {140pipe_surface info {};141142info.format = pipe->format;143info.writable = rw;144145if (pipe->target == PIPE_BUFFER)146info.u.buf.last_element = pipe->width0 - 1;147148return q.pipe->create_surface(q.pipe, pipe, &info);149}150151void152resource::unbind_surface(command_queue &q, pipe_surface *st) {153q.pipe->surface_destroy(q.pipe, st);154}155156root_resource::root_resource(clover::device &dev, memory_obj &obj,157command_queue &q, const void *data_ptr) :158resource(dev, obj) {159pipe_resource info {};160161if (image *img = dynamic_cast<image *>(&obj)) {162info.format = translate_format(img->format());163info.width0 = img->width();164info.height0 = img->height();165info.depth0 = img->depth();166} else {167info.width0 = obj.size();168info.height0 = 1;169info.depth0 = 1;170}171172info.array_size = 1;173info.target = translate_target(obj.type());174info.bind = (PIPE_BIND_SAMPLER_VIEW |175PIPE_BIND_COMPUTE_RESOURCE |176PIPE_BIND_GLOBAL);177178if (obj.flags() & CL_MEM_USE_HOST_PTR && dev.allows_user_pointers()) {179// Page alignment is normally required for this, just try, hope for the180// best and fall back if it fails.181pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());182if (pipe)183return;184}185186if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {187info.usage = PIPE_USAGE_STAGING;188}189190pipe = dev.pipe->resource_create(dev.pipe, &info);191if (!pipe)192throw error(CL_OUT_OF_RESOURCES);193194if (data_ptr) {195box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };196unsigned cpp = util_format_get_blocksize(info.format);197198if (pipe->target == PIPE_BUFFER)199q.pipe->buffer_subdata(q.pipe, pipe, PIPE_MAP_WRITE,2000, info.width0, data_ptr);201else202q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_MAP_WRITE,203rect, data_ptr, cpp * info.width0,204cpp * info.width0 * info.height0);205}206}207208root_resource::root_resource(clover::device &dev, memory_obj &obj,209root_resource &r) :210resource(dev, obj) {211assert(0); // XXX -- resource shared among dev and r.dev212}213214root_resource::~root_resource() {215pipe_resource_reference(&this->pipe, NULL);216}217218sub_resource::sub_resource(resource &r, const vector &offset) :219resource(r.device(), r.obj) {220this->pipe = r.pipe;221this->offset = r.offset + offset;222}223224mapping::mapping(command_queue &q, resource &r,225cl_map_flags flags, bool blocking,226const resource::vector &origin,227const resource::vector ®ion) :228pctx(q.pipe), pres(NULL) {229unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_MAP_WRITE : 0 ) |230(flags & CL_MAP_READ ? PIPE_MAP_READ : 0 ) |231(flags & CL_MAP_WRITE_INVALIDATE_REGION ?232PIPE_MAP_DISCARD_RANGE : 0) |233(!blocking ? PIPE_MAP_UNSYNCHRONIZED : 0));234235p = pctx->buffer_map(pctx, r.pipe, 0, usage,236box(origin + r.offset, region), &pxfer);237if (!p) {238pxfer = NULL;239throw error(CL_OUT_OF_RESOURCES);240}241pipe_resource_reference(&pres, r.pipe);242}243244mapping::mapping(mapping &&m) :245pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {246m.pctx = NULL;247m.pxfer = NULL;248m.pres = NULL;249m.p = NULL;250}251252mapping::~mapping() {253if (pxfer) {254pctx->buffer_unmap(pctx, pxfer);255}256pipe_resource_reference(&pres, NULL);257}258259mapping &260mapping::operator=(mapping m) {261std::swap(pctx, m.pctx);262std::swap(pxfer, m.pxfer);263std::swap(pres, m.pres);264std::swap(p, m.p);265return *this;266}267268resource::vector269mapping::pitch() const270{271return {272util_format_get_blocksize(pres->format),273pxfer->stride,274pxfer->layer_stride,275};276}277278279