Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2009 VMware, Inc.
      4  * All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sub license, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  **************************************************************************/
     27 
     28 /* Helper utility for uploading user buffers & other data, and
     29  * coalescing small buffers into larger ones.
     30  */
     31 
     32 #include "pipe/p_defines.h"
     33 #include "util/u_inlines.h"
     34 #include "pipe/p_context.h"
     35 #include "util/u_memory.h"
     36 #include "util/u_math.h"
     37 
     38 #include "u_upload_mgr.h"
     39 
     40 
     41 struct u_upload_mgr {
     42    struct pipe_context *pipe;
     43 
     44    unsigned default_size;  /* Minimum size of the upload buffer, in bytes. */
     45    unsigned bind;          /* Bitmask of PIPE_BIND_* flags. */
     46    enum pipe_resource_usage usage;
     47    unsigned map_flags;     /* Bitmask of PIPE_TRANSFER_* flags. */
     48    boolean map_persistent; /* If persistent mappings are supported. */
     49 
     50    struct pipe_resource *buffer;   /* Upload buffer. */
     51    struct pipe_transfer *transfer; /* Transfer object for the upload buffer. */
     52    uint8_t *map;    /* Pointer to the mapped upload buffer. */
     53    unsigned offset; /* Aligned offset to the upload buffer, pointing
     54                      * at the first unused byte. */
     55 };
     56 
     57 
     58 struct u_upload_mgr *
     59 u_upload_create(struct pipe_context *pipe, unsigned default_size,
     60                 unsigned bind, enum pipe_resource_usage usage)
     61 {
     62    struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr );
     63    if (!upload)
     64       return NULL;
     65 
     66    upload->pipe = pipe;
     67    upload->default_size = default_size;
     68    upload->bind = bind;
     69    upload->usage = usage;
     70 
     71    upload->map_persistent =
     72       pipe->screen->get_param(pipe->screen,
     73                               PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT);
     74 
     75    if (upload->map_persistent) {
     76       upload->map_flags = PIPE_TRANSFER_WRITE |
     77                           PIPE_TRANSFER_PERSISTENT |
     78                           PIPE_TRANSFER_COHERENT;
     79    }
     80    else {
     81       upload->map_flags = PIPE_TRANSFER_WRITE |
     82                           PIPE_TRANSFER_UNSYNCHRONIZED |
     83                           PIPE_TRANSFER_FLUSH_EXPLICIT;
     84    }
     85 
     86    return upload;
     87 }
     88 
     89 
     90 static void upload_unmap_internal(struct u_upload_mgr *upload, boolean destroying)
     91 {
     92    if (!destroying && upload->map_persistent)
     93       return;
     94 
     95    if (upload->transfer) {
     96       struct pipe_box *box = &upload->transfer->box;
     97 
     98       if (!upload->map_persistent && (int) upload->offset > box->x) {
     99          pipe_buffer_flush_mapped_range(upload->pipe, upload->transfer,
    100                                         box->x, upload->offset - box->x);
    101       }
    102 
    103       pipe_transfer_unmap(upload->pipe, upload->transfer);
    104       upload->transfer = NULL;
    105       upload->map = NULL;
    106    }
    107 }
    108 
    109 
    110 void u_upload_unmap( struct u_upload_mgr *upload )
    111 {
    112    upload_unmap_internal(upload, FALSE);
    113 }
    114 
    115 
    116 static void u_upload_release_buffer(struct u_upload_mgr *upload)
    117 {
    118    /* Unmap and unreference the upload buffer. */
    119    upload_unmap_internal(upload, TRUE);
    120    pipe_resource_reference( &upload->buffer, NULL );
    121 }
    122 
    123 
    124 void u_upload_destroy( struct u_upload_mgr *upload )
    125 {
    126    u_upload_release_buffer( upload );
    127    FREE( upload );
    128 }
    129 
    130 
    131 static void
    132 u_upload_alloc_buffer(struct u_upload_mgr *upload,
    133                       unsigned min_size)
    134 {
    135    struct pipe_screen *screen = upload->pipe->screen;
    136    struct pipe_resource buffer;
    137    unsigned size;
    138 
    139    /* Release the old buffer, if present:
    140     */
    141    u_upload_release_buffer( upload );
    142 
    143    /* Allocate a new one:
    144     */
    145    size = align(MAX2(upload->default_size, min_size), 4096);
    146 
    147    memset(&buffer, 0, sizeof buffer);
    148    buffer.target = PIPE_BUFFER;
    149    buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
    150    buffer.bind = upload->bind;
    151    buffer.usage = upload->usage;
    152    buffer.width0 = size;
    153    buffer.height0 = 1;
    154    buffer.depth0 = 1;
    155    buffer.array_size = 1;
    156 
    157    if (upload->map_persistent) {
    158       buffer.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
    159                      PIPE_RESOURCE_FLAG_MAP_COHERENT;
    160    }
    161 
    162    upload->buffer = screen->resource_create(screen, &buffer);
    163    if (upload->buffer == NULL)
    164       return;
    165 
    166    /* Map the new buffer. */
    167    upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
    168                                        0, size, upload->map_flags,
    169                                        &upload->transfer);
    170    if (upload->map == NULL) {
    171       upload->transfer = NULL;
    172       pipe_resource_reference(&upload->buffer, NULL);
    173       return;
    174    }
    175 
    176    upload->offset = 0;
    177 }
    178 
    179 void
    180 u_upload_alloc(struct u_upload_mgr *upload,
    181                unsigned min_out_offset,
    182                unsigned size,
    183                unsigned alignment,
    184                unsigned *out_offset,
    185                struct pipe_resource **outbuf,
    186                void **ptr)
    187 {
    188    unsigned buffer_size = upload->buffer ? upload->buffer->width0 : 0;
    189    unsigned offset;
    190 
    191    min_out_offset = align(min_out_offset, alignment);
    192 
    193    offset = align(upload->offset, alignment);
    194    offset = MAX2(offset, min_out_offset);
    195 
    196    /* Make sure we have enough space in the upload buffer
    197     * for the sub-allocation.
    198     */
    199    if (unlikely(!upload->buffer || offset + size > buffer_size)) {
    200       u_upload_alloc_buffer(upload, min_out_offset + size);
    201 
    202       if (unlikely(!upload->buffer)) {
    203          *out_offset = ~0;
    204          pipe_resource_reference(outbuf, NULL);
    205          *ptr = NULL;
    206          return;
    207       }
    208 
    209       offset = min_out_offset;
    210       buffer_size = upload->buffer->width0;
    211    }
    212 
    213    if (unlikely(!upload->map)) {
    214       upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
    215                                           offset,
    216                                           buffer_size - offset,
    217                                           upload->map_flags,
    218 					  &upload->transfer);
    219       if (unlikely(!upload->map)) {
    220          upload->transfer = NULL;
    221          *out_offset = ~0;
    222          pipe_resource_reference(outbuf, NULL);
    223          *ptr = NULL;
    224          return;
    225       }
    226 
    227       upload->map -= offset;
    228    }
    229 
    230    assert(offset < buffer_size);
    231    assert(offset + size <= buffer_size);
    232    assert(size);
    233 
    234    /* Emit the return values: */
    235    *ptr = upload->map + offset;
    236    pipe_resource_reference(outbuf, upload->buffer);
    237    *out_offset = offset;
    238 
    239    upload->offset = offset + size;
    240 }
    241 
    242 void u_upload_data(struct u_upload_mgr *upload,
    243                    unsigned min_out_offset,
    244                    unsigned size,
    245                    unsigned alignment,
    246                    const void *data,
    247                    unsigned *out_offset,
    248                    struct pipe_resource **outbuf)
    249 {
    250    uint8_t *ptr;
    251 
    252    u_upload_alloc(upload, min_out_offset, size, alignment,
    253                   out_offset, outbuf,
    254                   (void**)&ptr);
    255    if (ptr)
    256       memcpy(ptr, data, size);
    257 }
    258 
    259 /* XXX: Remove. It's basically a CPU fallback of resource_copy_region. */
    260 void u_upload_buffer(struct u_upload_mgr *upload,
    261                      unsigned min_out_offset,
    262                      unsigned offset,
    263                      unsigned size,
    264                      unsigned alignment,
    265                      struct pipe_resource *inbuf,
    266                      unsigned *out_offset,
    267                      struct pipe_resource **outbuf)
    268 {
    269    struct pipe_transfer *transfer = NULL;
    270    const char *map = NULL;
    271 
    272    map = (const char *)pipe_buffer_map_range(upload->pipe,
    273                                              inbuf,
    274                                              offset, size,
    275                                              PIPE_TRANSFER_READ,
    276                                              &transfer);
    277 
    278    if (!map) {
    279       pipe_resource_reference(outbuf, NULL);
    280       return;
    281    }
    282 
    283    if (0)
    284       debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
    285 
    286    u_upload_data(upload, min_out_offset, size, alignment,
    287                  map, out_offset, outbuf);
    288    pipe_buffer_unmap( upload->pipe, transfer );
    289 }
    290