Home | History | Annotate | Download | only in svga
      1 /**********************************************************
      2  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
      3  *
      4  * Permission is hereby granted, free of charge, to any person
      5  * obtaining a copy of this software and associated documentation
      6  * files (the "Software"), to deal in the Software without
      7  * restriction, including without limitation the rights to use, copy,
      8  * modify, merge, publish, distribute, sublicense, and/or sell copies
      9  * of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be
     13  * included in all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
     19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  *
     24  **********************************************************/
     25 
     26 #include "draw/draw_vbuf.h"
     27 #include "draw/draw_context.h"
     28 #include "draw/draw_vertex.h"
     29 
     30 #include "util/u_debug.h"
     31 #include "util/u_inlines.h"
     32 #include "util/u_math.h"
     33 #include "util/u_memory.h"
     34 
     35 #include "svga_context.h"
     36 #include "svga_state.h"
     37 #include "svga_swtnl.h"
     38 
     39 #include "svga_types.h"
     40 #include "svga_reg.h"
     41 #include "svga3d_reg.h"
     42 #include "svga_draw.h"
     43 #include "svga_shader.h"
     44 #include "svga_swtnl_private.h"
     45 
     46 
     47 static const struct vertex_info *
     48 svga_vbuf_render_get_vertex_info( struct vbuf_render *render )
     49 {
     50    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
     51    struct svga_context *svga = svga_render->svga;
     52 
     53    svga_swtnl_update_vdecl(svga);
     54 
     55    return &svga_render->vertex_info;
     56 }
     57 
     58 
     59 static boolean
     60 svga_vbuf_render_allocate_vertices( struct vbuf_render *render,
     61                                     ushort vertex_size,
     62                                     ushort nr_vertices )
     63 {
     64    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
     65    struct svga_context *svga = svga_render->svga;
     66    struct pipe_screen *screen = svga->pipe.screen;
     67    size_t size = (size_t)nr_vertices * (size_t)vertex_size;
     68    boolean new_vbuf = FALSE;
     69    boolean new_ibuf = FALSE;
     70 
     71    SVGA_STATS_TIME_PUSH(svga_sws(svga),
     72                         SVGA_STATS_TIME_VBUFRENDERALLOCVERT);
     73 
     74    if (svga_render->vertex_size != vertex_size)
     75       svga->swtnl.new_vdecl = TRUE;
     76    svga_render->vertex_size = (size_t)vertex_size;
     77 
     78    if (svga->swtnl.new_vbuf)
     79       new_ibuf = new_vbuf = TRUE;
     80    svga->swtnl.new_vbuf = FALSE;
     81 
     82    if (svga_render->vbuf_size < svga_render->vbuf_offset + svga_render->vbuf_used + size)
     83       new_vbuf = TRUE;
     84 
     85    if (new_vbuf)
     86       pipe_resource_reference(&svga_render->vbuf, NULL);
     87    if (new_ibuf)
     88       pipe_resource_reference(&svga_render->ibuf, NULL);
     89 
     90    if (!svga_render->vbuf) {
     91       svga_render->vbuf_size = MAX2(size, svga_render->vbuf_alloc_size);
     92       svga_render->vbuf = pipe_buffer_create(screen,
     93                                              PIPE_BIND_VERTEX_BUFFER,
     94                                              PIPE_USAGE_STREAM,
     95                                              svga_render->vbuf_size);
     96       if(!svga_render->vbuf) {
     97          svga_context_flush(svga, NULL);
     98          assert(!svga_render->vbuf);
     99          svga_render->vbuf = pipe_buffer_create(screen,
    100                                                 PIPE_BIND_VERTEX_BUFFER,
    101                                                 PIPE_USAGE_STREAM,
    102                                                 svga_render->vbuf_size);
    103          /* The buffer allocation may fail if we run out of memory.
    104           * The draw module's vbuf code should handle that without crashing.
    105           */
    106       }
    107 
    108       svga->swtnl.new_vdecl = TRUE;
    109       svga_render->vbuf_offset = 0;
    110    } else {
    111       svga_render->vbuf_offset += svga_render->vbuf_used;
    112    }
    113 
    114    svga_render->vbuf_used = 0;
    115 
    116    if (svga->swtnl.new_vdecl)
    117       svga_render->vdecl_offset = svga_render->vbuf_offset;
    118 
    119    SVGA_STATS_TIME_POP(svga_sws(svga));
    120 
    121    return TRUE;
    122 }
    123 
    124 static void *
    125 svga_vbuf_render_map_vertices( struct vbuf_render *render )
    126 {
    127    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    128    struct svga_context *svga = svga_render->svga;
    129    void * retPtr = NULL;
    130 
    131    SVGA_STATS_TIME_PUSH(svga_sws(svga),
    132                         SVGA_STATS_TIME_VBUFRENDERMAPVERT);
    133 
    134    if (svga_render->vbuf) {
    135       char *ptr = (char*)pipe_buffer_map(&svga->pipe,
    136                                          svga_render->vbuf,
    137                                          PIPE_TRANSFER_WRITE |
    138                                          PIPE_TRANSFER_FLUSH_EXPLICIT |
    139                                          PIPE_TRANSFER_DISCARD_RANGE |
    140                                          PIPE_TRANSFER_UNSYNCHRONIZED,
    141                                          &svga_render->vbuf_transfer);
    142       if (ptr) {
    143          svga_render->vbuf_ptr = ptr;
    144          retPtr = ptr + svga_render->vbuf_offset;
    145       }
    146       else {
    147          svga_render->vbuf_ptr = NULL;
    148          svga_render->vbuf_transfer = NULL;
    149          retPtr = NULL;
    150       }
    151    }
    152    else {
    153       /* we probably ran out of memory when allocating the vertex buffer */
    154       retPtr = NULL;
    155    }
    156 
    157    SVGA_STATS_TIME_POP(svga_sws(svga));
    158    return retPtr;
    159 }
    160 
    161 static void
    162 svga_vbuf_render_unmap_vertices( struct vbuf_render *render,
    163                                  ushort min_index,
    164                                  ushort max_index )
    165 {
    166    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    167    struct svga_context *svga = svga_render->svga;
    168    unsigned offset, length;
    169    size_t used = svga_render->vertex_size * ((size_t)max_index + 1);
    170 
    171    SVGA_STATS_TIME_PUSH(svga_sws(svga),
    172                         SVGA_STATS_TIME_VBUFRENDERUNMAPVERT);
    173 
    174    offset = svga_render->vbuf_offset + svga_render->vertex_size * min_index;
    175    length = svga_render->vertex_size * (max_index + 1 - min_index);
    176 
    177    if (0) {
    178       /* dump vertex data */
    179       const float *f = (const float *) ((char *) svga_render->vbuf_ptr +
    180                                         svga_render->vbuf_offset);
    181       unsigned i;
    182       debug_printf("swtnl vertex data:\n");
    183       for (i = 0; i < length / 4; i += 4) {
    184          debug_printf("%u: %f %f %f %f\n", i, f[i], f[i+1], f[i+2], f[i+3]);
    185       }
    186    }
    187 
    188    pipe_buffer_flush_mapped_range(&svga->pipe,
    189 				  svga_render->vbuf_transfer,
    190 				  offset, length);
    191    pipe_buffer_unmap(&svga->pipe, svga_render->vbuf_transfer);
    192    svga_render->min_index = min_index;
    193    svga_render->max_index = max_index;
    194    svga_render->vbuf_used = MAX2(svga_render->vbuf_used, used);
    195 
    196    SVGA_STATS_TIME_POP(svga_sws(svga));
    197 }
    198 
    199 static void
    200 svga_vbuf_render_set_primitive( struct vbuf_render *render,
    201                                 enum pipe_prim_type prim )
    202 {
    203    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    204    svga_render->prim = prim;
    205 }
    206 
    207 static void
    208 svga_vbuf_submit_state( struct svga_vbuf_render *svga_render )
    209 {
    210    struct svga_context *svga = svga_render->svga;
    211    SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
    212    enum pipe_error ret;
    213    unsigned i;
    214    static const unsigned zero[PIPE_MAX_ATTRIBS] = {0};
    215 
    216    /* if the vdecl or vbuf hasn't changed do nothing */
    217    if (!svga->swtnl.new_vdecl)
    218       return;
    219 
    220    SVGA_STATS_TIME_PUSH(svga_sws(svga),
    221                         SVGA_STATS_TIME_VBUFSUBMITSTATE);
    222 
    223    memcpy(vdecl, svga_render->vdecl, sizeof(vdecl));
    224 
    225    /* flush the hw state */
    226    ret = svga_hwtnl_flush(svga->hwtnl);
    227    if (ret != PIPE_OK) {
    228       svga_context_flush(svga, NULL);
    229       ret = svga_hwtnl_flush(svga->hwtnl);
    230       /* if we hit this path we might become synced with hw */
    231       svga->swtnl.new_vbuf = TRUE;
    232       assert(ret == PIPE_OK);
    233    }
    234 
    235    for (i = 0; i < svga_render->vdecl_count; i++) {
    236       vdecl[i].array.offset += svga_render->vdecl_offset;
    237    }
    238 
    239    svga_hwtnl_vertex_decls(svga->hwtnl,
    240                            svga_render->vdecl_count,
    241                            vdecl,
    242                            zero,
    243                            svga_render->layout_id);
    244 
    245    /* Specify the vertex buffer (there's only ever one) */
    246    {
    247       struct pipe_vertex_buffer vb;
    248       vb.buffer = svga_render->vbuf;
    249       vb.buffer_offset = svga_render->vdecl_offset;
    250       vb.stride = vdecl[0].array.stride;
    251       vb.user_buffer = NULL;
    252       svga_hwtnl_vertex_buffers(svga->hwtnl, 1, &vb);
    253    }
    254 
    255    /* We have already taken care of flatshading, so let the hwtnl
    256     * module use whatever is most convenient:
    257     */
    258    if (svga->state.sw.need_pipeline) {
    259       svga_hwtnl_set_flatshade(svga->hwtnl, FALSE, FALSE);
    260       svga_hwtnl_set_fillmode(svga->hwtnl, PIPE_POLYGON_MODE_FILL);
    261    }
    262    else {
    263       svga_hwtnl_set_flatshade( svga->hwtnl,
    264                                 svga->curr.rast->templ.flatshade ||
    265                                 svga->state.hw_draw.fs->uses_flat_interp,
    266                                 svga->curr.rast->templ.flatshade_first );
    267 
    268       svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
    269    }
    270 
    271    svga->swtnl.new_vdecl = FALSE;
    272    SVGA_STATS_TIME_POP(svga_sws(svga));
    273 }
    274 
    275 static void
    276 svga_vbuf_render_draw_arrays( struct vbuf_render *render,
    277                               unsigned start, uint nr )
    278 {
    279    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    280    struct svga_context *svga = svga_render->svga;
    281    unsigned bias = (svga_render->vbuf_offset - svga_render->vdecl_offset) / svga_render->vertex_size;
    282    enum pipe_error ret = PIPE_OK;
    283    /* instancing will already have been resolved at this point by 'draw' */
    284    const unsigned start_instance = 0;
    285    const unsigned instance_count = 1;
    286 
    287    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_VBUFDRAWARRAYS);
    288 
    289    /* off to hardware */
    290    svga_vbuf_submit_state(svga_render);
    291 
    292    /* Need to call update_state() again as the draw module may have
    293     * altered some of our state behind our backs.  Testcase:
    294     * redbook/polys.c
    295     */
    296    svga_update_state_retry( svga, SVGA_STATE_HW_DRAW );
    297 
    298    ret = svga_hwtnl_draw_arrays(svga->hwtnl, svga_render->prim, start + bias, nr,
    299                                 start_instance, instance_count);
    300    if (ret != PIPE_OK) {
    301       svga_context_flush(svga, NULL);
    302       ret = svga_hwtnl_draw_arrays(svga->hwtnl, svga_render->prim,
    303                                    start + bias, nr,
    304                                    start_instance, instance_count);
    305       svga->swtnl.new_vbuf = TRUE;
    306       assert(ret == PIPE_OK);
    307    }
    308    SVGA_STATS_TIME_POP(svga_sws(svga));
    309 }
    310 
    311 
    312 static void
    313 svga_vbuf_render_draw_elements( struct vbuf_render *render,
    314                                 const ushort *indices,
    315                                 uint nr_indices)
    316 {
    317    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    318    struct svga_context *svga = svga_render->svga;
    319    struct pipe_screen *screen = svga->pipe.screen;
    320    int bias = (svga_render->vbuf_offset - svga_render->vdecl_offset) / svga_render->vertex_size;
    321    boolean ret;
    322    size_t size = 2 * nr_indices;
    323    /* instancing will already have been resolved at this point by 'draw' */
    324    const unsigned start_instance = 0;
    325    const unsigned instance_count = 1;
    326 
    327    assert(( svga_render->vbuf_offset - svga_render->vdecl_offset) % svga_render->vertex_size == 0);
    328 
    329    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_VBUFDRAWELEMENTS);
    330 
    331    if (svga_render->ibuf_size < svga_render->ibuf_offset + size)
    332       pipe_resource_reference(&svga_render->ibuf, NULL);
    333 
    334    if (!svga_render->ibuf) {
    335       svga_render->ibuf_size = MAX2(size, svga_render->ibuf_alloc_size);
    336       svga_render->ibuf = pipe_buffer_create(screen,
    337                                              PIPE_BIND_INDEX_BUFFER,
    338                                              PIPE_USAGE_STREAM,
    339                                              svga_render->ibuf_size);
    340       svga_render->ibuf_offset = 0;
    341    }
    342 
    343    pipe_buffer_write_nooverlap(&svga->pipe, svga_render->ibuf,
    344 			       svga_render->ibuf_offset, 2 * nr_indices, indices);
    345 
    346    /* off to hardware */
    347    svga_vbuf_submit_state(svga_render);
    348 
    349    /* Need to call update_state() again as the draw module may have
    350     * altered some of our state behind our backs.  Testcase:
    351     * redbook/polys.c
    352     */
    353    svga_update_state_retry( svga, SVGA_STATE_HW_DRAW );
    354 
    355    ret = svga_hwtnl_draw_range_elements(svga->hwtnl,
    356                                         svga_render->ibuf,
    357                                         2,
    358                                         bias,
    359                                         svga_render->min_index,
    360                                         svga_render->max_index,
    361                                         svga_render->prim,
    362                                         svga_render->ibuf_offset / 2, nr_indices,
    363                                         start_instance, instance_count);
    364    if(ret != PIPE_OK) {
    365       svga_context_flush(svga, NULL);
    366       ret = svga_hwtnl_draw_range_elements(svga->hwtnl,
    367                                            svga_render->ibuf,
    368                                            2,
    369                                            bias,
    370                                            svga_render->min_index,
    371                                            svga_render->max_index,
    372                                            svga_render->prim,
    373                                            svga_render->ibuf_offset / 2,
    374                                            nr_indices,
    375                                            start_instance, instance_count);
    376       svga->swtnl.new_vbuf = TRUE;
    377       assert(ret == PIPE_OK);
    378    }
    379    svga_render->ibuf_offset += size;
    380 
    381    SVGA_STATS_TIME_POP(svga_sws(svga));
    382 }
    383 
    384 
    385 static void
    386 svga_vbuf_render_release_vertices( struct vbuf_render *render )
    387 {
    388 
    389 }
    390 
    391 
    392 static void
    393 svga_vbuf_render_destroy( struct vbuf_render *render )
    394 {
    395    struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
    396 
    397    pipe_resource_reference(&svga_render->vbuf, NULL);
    398    pipe_resource_reference(&svga_render->ibuf, NULL);
    399    FREE(svga_render);
    400 }
    401 
    402 
    403 /**
    404  * Create a new primitive render.
    405  */
    406 struct vbuf_render *
    407 svga_vbuf_render_create( struct svga_context *svga )
    408 {
    409    struct svga_vbuf_render *svga_render = CALLOC_STRUCT(svga_vbuf_render);
    410 
    411    svga_render->svga = svga;
    412    svga_render->ibuf_size = 0;
    413    svga_render->vbuf_size = 0;
    414    svga_render->ibuf_alloc_size = 4*1024;
    415    svga_render->vbuf_alloc_size = 64*1024;
    416    svga_render->layout_id = SVGA3D_INVALID_ID;
    417    svga_render->base.max_vertex_buffer_bytes = 64*1024/10;
    418    svga_render->base.max_indices = 65536;
    419    svga_render->base.get_vertex_info = svga_vbuf_render_get_vertex_info;
    420    svga_render->base.allocate_vertices = svga_vbuf_render_allocate_vertices;
    421    svga_render->base.map_vertices = svga_vbuf_render_map_vertices;
    422    svga_render->base.unmap_vertices = svga_vbuf_render_unmap_vertices;
    423    svga_render->base.set_primitive = svga_vbuf_render_set_primitive;
    424    svga_render->base.draw_elements = svga_vbuf_render_draw_elements;
    425    svga_render->base.draw_arrays = svga_vbuf_render_draw_arrays;
    426    svga_render->base.release_vertices = svga_vbuf_render_release_vertices;
    427    svga_render->base.destroy = svga_vbuf_render_destroy;
    428 
    429    return &svga_render->base;
    430 }
    431