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 #ifndef SVGA_DRAW_H_
     27 #define SVGA_DRAW_H_
     28 
     29 #include "pipe/p_compiler.h"
     30 #include "pipe/p_defines.h"
     31 #include "indices/u_indices.h"
     32 #include "util/u_prim.h"
     33 #include "svga_context.h"
     34 #include "svga_hw_reg.h"
     35 #include "svga3d_shaderdefs.h"
     36 
     37 struct svga_context;
     38 struct u_upload_mgr;
     39 
     40 /**
     41  * Mask indicating which types of gallium primitives are actually
     42  * handled by the svga device.  Other types will be converted to
     43  * these types by the index/translation code.
     44  */
     45 static const unsigned svga_hw_prims =
     46    ((1 << PIPE_PRIM_POINTS) |
     47     (1 << PIPE_PRIM_LINES) |
     48     (1 << PIPE_PRIM_LINE_STRIP) |
     49     (1 << PIPE_PRIM_TRIANGLES) |
     50     (1 << PIPE_PRIM_TRIANGLE_STRIP) |
     51     (1 << PIPE_PRIM_TRIANGLE_FAN) |
     52     (1 << PIPE_PRIM_LINES_ADJACENCY) |
     53     (1 << PIPE_PRIM_LINE_STRIP_ADJACENCY) |
     54     (1 << PIPE_PRIM_TRIANGLES_ADJACENCY) |
     55     (1 << PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY));
     56 
     57 
     58 /**
     59  * Translate a gallium PIPE_PRIM_x value to an SVGA3D_PRIMITIVE_x value.
     60  * Also, compute the number of primitives that'll be drawn given a
     61  * vertex count.
     62  * Note that this function doesn't have to handle PIPE_PRIM_LINE_LOOP,
     63  * PIPE_PRIM_QUADS, PIPE_PRIM_QUAD_STRIP or PIPE_PRIM_POLYGON.  We convert
     64  * those to other types of primitives with index/translation code.
     65  */
     66 static inline SVGA3dPrimitiveType
     67 svga_translate_prim(unsigned mode, unsigned vcount, unsigned *prim_count)
     68 {
     69    switch (mode) {
     70    case PIPE_PRIM_POINTS:
     71       *prim_count = vcount;
     72       return SVGA3D_PRIMITIVE_POINTLIST;
     73 
     74    case PIPE_PRIM_LINES:
     75       *prim_count = vcount / 2;
     76       return SVGA3D_PRIMITIVE_LINELIST;
     77 
     78    case PIPE_PRIM_LINE_STRIP:
     79       *prim_count = vcount - 1;
     80       return SVGA3D_PRIMITIVE_LINESTRIP;
     81 
     82    case PIPE_PRIM_TRIANGLES:
     83       *prim_count = vcount / 3;
     84       return SVGA3D_PRIMITIVE_TRIANGLELIST;
     85 
     86    case PIPE_PRIM_TRIANGLE_STRIP:
     87       *prim_count = vcount - 2;
     88       return SVGA3D_PRIMITIVE_TRIANGLESTRIP;
     89 
     90    case PIPE_PRIM_TRIANGLE_FAN:
     91       *prim_count = vcount - 2;
     92       return SVGA3D_PRIMITIVE_TRIANGLEFAN;
     93 
     94    case PIPE_PRIM_LINES_ADJACENCY:
     95       *prim_count = vcount / 4;
     96       return SVGA3D_PRIMITIVE_LINELIST_ADJ;
     97 
     98    case PIPE_PRIM_LINE_STRIP_ADJACENCY:
     99       *prim_count = vcount - 3;
    100       return SVGA3D_PRIMITIVE_LINESTRIP_ADJ;
    101 
    102    case PIPE_PRIM_TRIANGLES_ADJACENCY:
    103       *prim_count = vcount / 6;
    104       return SVGA3D_PRIMITIVE_TRIANGLELIST_ADJ;
    105 
    106    case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
    107       *prim_count = vcount / 2 - 2 ;
    108       return SVGA3D_PRIMITIVE_TRIANGLESTRIP_ADJ;
    109 
    110    default:
    111       assert(0);
    112       *prim_count = 0;
    113       return 0;
    114    }
    115 }
    116 
    117 
    118 struct index_cache {
    119    u_generate_func generate;
    120    unsigned gen_nr;
    121 
    122    /* If non-null, this buffer is filled by calling generate(nr, map(buffer))
    123     */
    124    struct pipe_resource *buffer;
    125 };
    126 
    127 
    128 /** Max number of primitives per draw call */
    129 #define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
    130 
    131 struct draw_cmd {
    132    struct svga_winsys_context *swc;
    133 
    134    /* vertex layout info */
    135    SVGA3dVertexDecl vdecl[SVGA3D_INPUTREG_MAX];
    136    unsigned vdecl_count;
    137    SVGA3dElementLayoutId vdecl_layout_id;
    138    unsigned vdecl_buffer_index[SVGA3D_INPUTREG_MAX];
    139 
    140    /* vertex buffer info */
    141    struct pipe_vertex_buffer vbufs[SVGA3D_INPUTREG_MAX];
    142    unsigned vbuf_count;
    143 
    144    SVGA3dPrimitiveRange prim[QSZ];
    145    struct pipe_resource *prim_ib[QSZ];
    146    unsigned prim_count;   /**< number of primitives for this draw */
    147    unsigned min_index[QSZ];
    148    unsigned max_index[QSZ];
    149 };
    150 
    151 #define IDX_CACHE_MAX  8
    152 
    153 struct svga_hwtnl {
    154    struct svga_context *svga;
    155    struct u_upload_mgr *upload_ib;
    156 
    157    /* Additional negative index bias due to partial buffer uploads
    158     * This is compensated for in the offset associated with all
    159     * vertex buffers.
    160     */
    161    int index_bias;
    162 
    163    /* Provoking vertex information (for flat shading). */
    164    unsigned api_pv;  /**< app-requested PV mode (PV_FIRST or PV_LAST) */
    165    unsigned hw_pv;   /**< device-supported PV mode (PV_FIRST or PV_LAST) */
    166 
    167    /* The triangle fillmode for the device (one of PIPE_POLYGON_MODE_{FILL,
    168     * LINE,POINT}).  If the polygon front mode matches the back mode,
    169     * api_fillmode will be that mode.  Otherwise, api_fillmode will be
    170     * PIPE_POLYGON_MODE_FILL.
    171     */
    172    unsigned api_fillmode;
    173 
    174    /* Cache the results of running a particular generate func on each
    175     * primitive type.
    176     */
    177    struct index_cache index_cache[PIPE_PRIM_MAX][IDX_CACHE_MAX];
    178 
    179    /* Try to build the maximal draw command packet before emitting:
    180     */
    181    struct draw_cmd cmd;
    182 };
    183 
    184 
    185 
    186 /**
    187  * Do we need to use the gallium 'indices' helper to render unfilled
    188  * triangles?
    189  */
    190 static inline boolean
    191 svga_need_unfilled_fallback(const struct svga_hwtnl *hwtnl,
    192                             enum pipe_prim_type prim)
    193 {
    194    const struct svga_context *svga = hwtnl->svga;
    195 
    196    if (u_reduced_prim(prim) != PIPE_PRIM_TRIANGLES) {
    197       /* if we're drawing points or lines, no fallback needed */
    198       return FALSE;
    199    }
    200 
    201    if (svga_have_vgpu10(svga)) {
    202       /* vgpu10 supports polygon fill and line modes */
    203       if ((prim == PIPE_PRIM_QUADS ||
    204            prim == PIPE_PRIM_QUAD_STRIP ||
    205            prim == PIPE_PRIM_POLYGON) &&
    206           hwtnl->api_fillmode == PIPE_POLYGON_MODE_LINE) {
    207          /* VGPU10 doesn't directly render quads or polygons.  They're
    208           * converted to triangles.  If we let the device draw the triangle
    209           * outlines we'll get an extra, stray lines in the interiors.
    210           * So, to draw unfilled quads correctly, we need the fallback.
    211           */
    212          return true;
    213       }
    214       return hwtnl->api_fillmode == PIPE_POLYGON_MODE_POINT;
    215    } else {
    216       /* vgpu9 doesn't support line or point fill modes */
    217       return hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL;
    218    }
    219 }
    220 
    221 
    222 enum pipe_error
    223 svga_hwtnl_prim(struct svga_hwtnl *hwtnl,
    224                 const SVGA3dPrimitiveRange *range,
    225                 unsigned vcount,
    226                 unsigned min_index,
    227                 unsigned max_index,
    228                 struct pipe_resource *ib,
    229                 unsigned start_instance, unsigned instance_count);
    230 
    231 enum pipe_error
    232 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
    233                                       struct pipe_resource *indexBuffer,
    234                                       unsigned index_size,
    235                                       int index_bias,
    236                                       unsigned min_index,
    237                                       unsigned max_index,
    238                                       enum pipe_prim_type prim,
    239                                       unsigned start,
    240                                       unsigned count,
    241                                       unsigned start_instance,
    242                                       unsigned instance_count);
    243 
    244 #endif
    245