Home | History | Annotate | Download | only in r300
      1 /*
      2  * Copyright 2010 Red Hat Inc.
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * on the rights to use, copy, modify, merge, publish, distribute, sub
      8  * license, and/or sell copies of the Software, and to permit persons to whom
      9  * the Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  *
     23  * Authors: Dave Airlie
     24  */
     25 
     26 #include <stdio.h>
     27 
     28 #include "util/u_inlines.h"
     29 #include "util/u_memory.h"
     30 #include "util/u_upload_mgr.h"
     31 #include "util/u_math.h"
     32 
     33 #include "r300_screen_buffer.h"
     34 
     35 void r300_upload_index_buffer(struct r300_context *r300,
     36 			      struct pipe_resource **index_buffer,
     37 			      unsigned index_size, unsigned *start,
     38 			      unsigned count, const uint8_t *ptr)
     39 {
     40     unsigned index_offset;
     41 
     42     *index_buffer = NULL;
     43 
     44     u_upload_data(r300->uploader,
     45                   0, count * index_size,
     46                   ptr + (*start * index_size),
     47                   &index_offset,
     48                   index_buffer);
     49 
     50     *start = index_offset / index_size;
     51 }
     52 
     53 static void r300_buffer_destroy(struct pipe_screen *screen,
     54 				struct pipe_resource *buf)
     55 {
     56     struct r300_resource *rbuf = r300_resource(buf);
     57 
     58     if (rbuf->malloced_buffer)
     59         FREE(rbuf->malloced_buffer);
     60 
     61     if (rbuf->buf)
     62         pb_reference(&rbuf->buf, NULL);
     63 
     64     FREE(rbuf);
     65 }
     66 
     67 static struct pipe_transfer*
     68 r300_buffer_get_transfer(struct pipe_context *context,
     69                          struct pipe_resource *resource,
     70                          unsigned level,
     71                          unsigned usage,
     72                          const struct pipe_box *box)
     73 {
     74    struct r300_context *r300 = r300_context(context);
     75    struct pipe_transfer *transfer =
     76          util_slab_alloc(&r300->pool_transfers);
     77 
     78    transfer->resource = resource;
     79    transfer->level = level;
     80    transfer->usage = usage;
     81    transfer->box = *box;
     82    transfer->stride = 0;
     83    transfer->layer_stride = 0;
     84    transfer->data = NULL;
     85 
     86    /* Note strides are zero, this is ok for buffers, but not for
     87     * textures 2d & higher at least.
     88     */
     89    return transfer;
     90 }
     91 
     92 static void r300_buffer_transfer_destroy(struct pipe_context *pipe,
     93                                          struct pipe_transfer *transfer)
     94 {
     95    struct r300_context *r300 = r300_context(pipe);
     96    util_slab_free(&r300->pool_transfers, transfer);
     97 }
     98 
     99 static void *
    100 r300_buffer_transfer_map( struct pipe_context *pipe,
    101 			  struct pipe_transfer *transfer )
    102 {
    103     struct r300_context *r300 = r300_context(pipe);
    104     struct r300_screen *r300screen = r300_screen(pipe->screen);
    105     struct radeon_winsys *rws = r300screen->rws;
    106     struct r300_resource *rbuf = r300_resource(transfer->resource);
    107     uint8_t *map;
    108     enum pipe_transfer_usage usage;
    109 
    110     if (rbuf->malloced_buffer)
    111         return (uint8_t *) rbuf->malloced_buffer + transfer->box.x;
    112 
    113     /* Buffers are never used for write, therefore mapping for read can be
    114      * unsynchronized. */
    115     usage = transfer->usage;
    116     if (!(usage & PIPE_TRANSFER_WRITE)) {
    117        usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
    118     }
    119 
    120     map = rws->buffer_map(rbuf->cs_buf, r300->cs, usage);
    121 
    122     if (map == NULL)
    123         return NULL;
    124 
    125     return map + transfer->box.x;
    126 }
    127 
    128 static void r300_buffer_transfer_unmap( struct pipe_context *pipe,
    129 			    struct pipe_transfer *transfer )
    130 {
    131     /* no-op */
    132 }
    133 
    134 static const struct u_resource_vtbl r300_buffer_vtbl =
    135 {
    136    NULL,                               /* get_handle */
    137    r300_buffer_destroy,                /* resource_destroy */
    138    r300_buffer_get_transfer,           /* get_transfer */
    139    r300_buffer_transfer_destroy,       /* transfer_destroy */
    140    r300_buffer_transfer_map,           /* transfer_map */
    141    NULL,                               /* transfer_flush_region */
    142    r300_buffer_transfer_unmap,         /* transfer_unmap */
    143    NULL   /* transfer_inline_write */
    144 };
    145 
    146 struct pipe_resource *r300_buffer_create(struct pipe_screen *screen,
    147 					 const struct pipe_resource *templ)
    148 {
    149     struct r300_screen *r300screen = r300_screen(screen);
    150     struct r300_resource *rbuf;
    151     unsigned alignment = 16;
    152 
    153     rbuf = MALLOC_STRUCT(r300_resource);
    154 
    155     rbuf->b.b = *templ;
    156     rbuf->b.vtbl = &r300_buffer_vtbl;
    157     pipe_reference_init(&rbuf->b.b.reference, 1);
    158     rbuf->b.b.screen = screen;
    159     rbuf->domain = RADEON_DOMAIN_GTT;
    160     rbuf->buf = NULL;
    161     rbuf->malloced_buffer = NULL;
    162 
    163     /* Alloc constant buffers and SWTCL buffers in RAM. */
    164     if (templ->bind & PIPE_BIND_CONSTANT_BUFFER ||
    165         (!r300screen->caps.has_tcl &&
    166          (templ->bind & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER)))) {
    167         rbuf->malloced_buffer = MALLOC(templ->width0);
    168         return &rbuf->b.b;
    169     }
    170 
    171     rbuf->buf =
    172         r300screen->rws->buffer_create(r300screen->rws,
    173                                        rbuf->b.b.width0, alignment,
    174                                        rbuf->b.b.bind, rbuf->domain);
    175     if (!rbuf->buf) {
    176         FREE(rbuf);
    177         return NULL;
    178     }
    179 
    180     rbuf->cs_buf =
    181         r300screen->rws->buffer_get_cs_handle(rbuf->buf);
    182 
    183     return &rbuf->b.b;
    184 }
    185