Path: blob/21.2-virgl/src/gallium/frontends/nine/nine_buffer_upload.c
4561 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* Copyright 2016 Axel Davy <[email protected]>4* All Rights Reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/27/* Adapted from u_upload_mgr.28* Makes suballocations from bigger allocations,29* while enabling fast mapping. */3031#include "pipe/p_defines.h"32#include "util/u_inlines.h"33#include "pipe/p_context.h"34#include "util/u_memory.h"35#include "util/u_math.h"36#include "util/slab.h"3738#include "nine_buffer_upload.h"3940#include "nine_debug.h"4142#define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)4344struct nine_buffer_group {45unsigned refcount; /* How many sub-buffers live inside the buffer */46struct pipe_resource *resource;47struct pipe_transfer *transfer;48uint8_t *map;49unsigned free_offset; /* Aligned offset to the upload buffer, pointing50* at the first unused byte. */51};5253struct nine_subbuffer {54struct nine_buffer_group *parent; /* Can be NULL */55struct pipe_resource *resource; /* The parent resource if apply */56unsigned offset; /* Offset inside the resource */57/* If there is no parent, the resource map. Else NULL. */58struct pipe_transfer *transfer;59uint8_t *map;60};6162struct nine_buffer_upload {63struct pipe_context *pipe;64struct slab_mempool buffer_pool;6566unsigned buffers_size; /* Size of the big allocated buffers */67unsigned num_buffers;68struct nine_buffer_group *buffers;69};7071static void72nine_upload_create_buffer_group(struct nine_buffer_upload *upload,73struct nine_buffer_group *group)74{75struct pipe_resource resource;76struct pipe_screen *screen = upload->pipe->screen;77DBG("Allocating %p %p\n", upload, group);7879memset(&resource, 0, sizeof(resource));80resource.target = PIPE_BUFFER;81resource.format = PIPE_FORMAT_R8_UNORM;82resource.bind = PIPE_BIND_VERTEX_BUFFER;83resource.usage = PIPE_USAGE_STREAM;84resource.width0 = upload->buffers_size;85resource.height0 = 1;86resource.depth0 = 1;87resource.array_size = 1;88resource.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |89PIPE_RESOURCE_FLAG_MAP_COHERENT;9091group->refcount = 0;92group->resource = screen->resource_create(screen, &resource);93if (group->resource == NULL)94return;9596group->map = pipe_buffer_map_range(upload->pipe, group->resource,970, upload->buffers_size,98PIPE_MAP_WRITE |99#ifdef PIPE_ARCH_X86100PIPE_MAP_ONCE |101#endif102PIPE_MAP_PERSISTENT |103PIPE_MAP_COHERENT,104&group->transfer);105if (group->map == NULL) {106group->transfer = NULL;107pipe_resource_reference(&group->resource, NULL);108return;109}110111group->free_offset = 0;112DBG("Success: %p %p\n", group->map, group->map+upload->buffers_size);113}114115static void116nine_upload_destroy_buffer_group(struct nine_buffer_upload *upload,117struct nine_buffer_group *group)118{119DBG("%p %p\n", upload, group);120DBG("Release: %p %p\n", group->map, group->map+upload->buffers_size);121assert(group->refcount == 0);122123if (group->transfer)124pipe_buffer_unmap(upload->pipe, group->transfer);125if (group->resource)126pipe_resource_reference(&group->resource, NULL);127group->transfer = NULL;128group->map = NULL;129}130131struct nine_buffer_upload *132nine_upload_create(struct pipe_context *pipe, unsigned buffers_size,133unsigned num_buffers)134{135struct nine_buffer_upload *upload;136int i;137138DBG("\n");139140if (!pipe->screen->get_param(pipe->screen,141PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT))142return NULL;143144upload = CALLOC_STRUCT(nine_buffer_upload);145146if (!upload)147return NULL;148149slab_create(&upload->buffer_pool, sizeof(struct nine_subbuffer), 4096);150151upload->pipe = pipe;152upload->buffers_size = align(buffers_size, 4096);153upload->num_buffers = num_buffers;154155upload->buffers = CALLOC(num_buffers, sizeof(struct nine_buffer_group));156if (!upload->buffers)157goto buffers_fail;158159for (i = 0; i < num_buffers; i++)160nine_upload_create_buffer_group(upload, &upload->buffers[i]);161162return upload;163164buffers_fail:165slab_destroy(&upload->buffer_pool);166FREE(upload);167return NULL;168}169170void171nine_upload_destroy(struct nine_buffer_upload *upload)172{173int i;174175DBG("%p\n", upload);176177for (i = 0; i < upload->num_buffers; i++)178nine_upload_destroy_buffer_group(upload, &upload->buffers[i]);179slab_destroy(&upload->buffer_pool);180FREE(upload);181}182183struct nine_subbuffer *184nine_upload_create_buffer(struct nine_buffer_upload *upload,185unsigned buffer_size)186{187struct nine_subbuffer *buf = slab_alloc_st(&upload->buffer_pool);188struct nine_buffer_group *group = NULL;189unsigned size = align(buffer_size, 4096);190int i = 0;191192DBG("%p %d\n", upload, buffer_size);193194if (!buf)195return NULL;196197for (i = 0; i < upload->num_buffers; i++) {198group = &upload->buffers[i];199if (group->resource &&200group->free_offset + size <= upload->buffers_size)201break;202}203204if (i == upload->num_buffers) {205/* Allocate lonely buffer */206struct pipe_resource resource;207struct pipe_screen *screen = upload->pipe->screen;208209DBG("Allocating buffer\n");210buf->parent = NULL;211212memset(&resource, 0, sizeof(resource));213resource.target = PIPE_BUFFER;214resource.format = PIPE_FORMAT_R8_UNORM;215resource.bind = PIPE_BIND_VERTEX_BUFFER;216resource.usage = PIPE_USAGE_STREAM;217resource.width0 = buffer_size;218resource.height0 = 1;219resource.depth0 = 1;220resource.array_size = 1;221resource.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |222PIPE_RESOURCE_FLAG_MAP_COHERENT;223224buf->resource = screen->resource_create(screen, &resource);225if (buf->resource == NULL) {226slab_free_st(&upload->buffer_pool, buf);227return NULL;228}229230buf->map = pipe_buffer_map_range(upload->pipe, buf->resource,2310, buffer_size,232PIPE_MAP_WRITE |233#ifdef PIPE_ARCH_X86234PIPE_MAP_ONCE |235#endif236PIPE_MAP_PERSISTENT |237PIPE_MAP_COHERENT,238&buf->transfer);239if (buf->map == NULL) {240pipe_resource_reference(&buf->resource, NULL);241slab_free_st(&upload->buffer_pool, buf);242return NULL;243}244buf->offset = 0;245return buf;246}247248DBG("Using buffer group %d\n", i);249250buf->parent = group;251buf->resource = NULL;252pipe_resource_reference(&buf->resource, group->resource);253buf->offset = group->free_offset;254255group->free_offset += size;256group->refcount += 1;257258return buf;259}260261void262nine_upload_release_buffer(struct nine_buffer_upload *upload,263struct nine_subbuffer *buf)264{265DBG("%p %p %p\n", upload, buf, buf->parent);266267if (buf->parent) {268pipe_resource_reference(&buf->resource, NULL);269buf->parent->refcount--;270if (buf->parent->refcount == 0) {271/* Allocate new buffer */272nine_upload_destroy_buffer_group(upload, buf->parent);273nine_upload_create_buffer_group(upload, buf->parent);274}275} else {276/* lonely buffer */277if (buf->transfer)278pipe_buffer_unmap(upload->pipe, buf->transfer);279pipe_resource_reference(&buf->resource, NULL);280}281282slab_free_st(&upload->buffer_pool, buf);283}284285uint8_t *286nine_upload_buffer_get_map(struct nine_subbuffer *buf)287{288if (buf->parent) {289DBG("%d\n", buf->parent->refcount);290return buf->parent->map + buf->offset;291}292/* lonely buffer */293return buf->map;294}295296struct pipe_resource *297nine_upload_buffer_resource_and_offset(struct nine_subbuffer *buf,298unsigned *offset)299{300*offset = buf->offset;301return buf->resource;302}303304305