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
    123     *   generate(nr, map(buffer))
    124     */
    125    struct pipe_resource *buffer;
    126 };
    127 
    128 
    129 /** Max number of primitives per draw call */
    130 #define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
    131 
    132 struct draw_cmd {
    133    struct svga_winsys_context *swc;
    134 
    135    /* vertex layout info */
    136    SVGA3dVertexDecl vdecl[SVGA3D_INPUTREG_MAX];
    137    unsigned vdecl_count;
    138    SVGA3dElementLayoutId vdecl_layout_id;
    139    unsigned vdecl_buffer_index[SVGA3D_INPUTREG_MAX];
    140 
    141    /* vertex buffer info */
    142    struct pipe_vertex_buffer vbufs[SVGA3D_INPUTREG_MAX];
    143    unsigned vbuf_count;
    144 
    145    SVGA3dPrimitiveRange prim[QSZ];
    146    struct pipe_resource *prim_ib[QSZ];
    147    unsigned prim_count;   /**< number of primitives for this draw */
    148    unsigned min_index[QSZ];
    149    unsigned max_index[QSZ];
    150 };
    151 
    152 #define IDX_CACHE_MAX  8
    153 
    154 struct svga_hwtnl {
    155    struct svga_context *svga;
    156    struct u_upload_mgr *upload_ib;
    157 
    158    /* Additional negative index bias due to partial buffer uploads
    159     * This is compensated for in the offset associated with all
    160     * vertex buffers.
    161     */
    162    int index_bias;
    163 
    164    /* Provoking vertex information (for flat shading). */
    165    unsigned api_pv;  /**< app-requested PV mode (PV_FIRST or PV_LAST) */
    166    unsigned hw_pv;   /**< device-supported PV mode (PV_FIRST or PV_LAST) */
    167 
    168    /* The triangle fillmode for the device (one of PIPE_POLYGON_MODE_{FILL,
    169     * LINE,POINT}).  If the polygon front mode matches the back mode,
    170     * api_fillmode will be that mode.  Otherwise, api_fillmode will be
    171     * PIPE_POLYGON_MODE_FILL.
    172     */
    173    unsigned api_fillmode;
    174 
    175    /* Cache the results of running a particular generate func on each
    176     * primitive type.
    177     */
    178    struct index_cache index_cache[PIPE_PRIM_MAX][IDX_CACHE_MAX];
    179 
    180    /* Try to build the maximal draw command packet before emitting:
    181     */
    182    struct draw_cmd cmd;
    183 };
    184 
    185 
    186 
    187 /**
    188  * Do we need to use the gallium 'indices' helper to render unfilled
    189  * triangles?
    190  */
    191 static inline boolean
    192 svga_need_unfilled_fallback(const struct svga_hwtnl *hwtnl,
    193                             enum pipe_prim_type prim)
    194 {
    195    const struct svga_context *svga = hwtnl->svga;
    196 
    197    if (u_reduced_prim(prim) != PIPE_PRIM_TRIANGLES) {
    198       /* if we're drawing points or lines, no fallback needed */
    199       return FALSE;
    200    }
    201 
    202    if (svga_have_vgpu10(svga)) {
    203       /* vgpu10 supports polygon fill and line modes */
    204       if ((prim == PIPE_PRIM_QUADS ||
    205            prim == PIPE_PRIM_QUAD_STRIP ||
    206            prim == PIPE_PRIM_POLYGON) &&
    207           hwtnl->api_fillmode == PIPE_POLYGON_MODE_LINE) {
    208          /* VGPU10 doesn't directly render quads or polygons.  They're
    209           * converted to triangles.  If we let the device draw the triangle
    210           * outlines we'll get an extra, stray lines in the interiors.
    211           * So, to draw unfilled quads correctly, we need the fallback.
    212           */
    213          return true;
    214       }
    215       return hwtnl->api_fillmode == PIPE_POLYGON_MODE_POINT;
    216    } else {
    217       /* vgpu9 doesn't support line or point fill modes */
    218       return hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL;
    219    }
    220 }
    221 
    222 
    223 enum pipe_error
    224 svga_hwtnl_prim( struct svga_hwtnl *hwtnl,
    225                  const SVGA3dPrimitiveRange *range,
    226                  unsigned vcount,
    227                  unsigned min_index,
    228                  unsigned max_index,
    229                  struct pipe_resource *ib,
    230                  unsigned start_instance, unsigned instance_count);
    231 
    232 enum pipe_error
    233 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
    234                                        struct pipe_resource *indexBuffer,
    235                                        unsigned index_size,
    236                                        int index_bias,
    237                                        unsigned min_index,
    238                                        unsigned max_index,
    239                                        enum pipe_prim_type prim,
    240                                        unsigned start,
    241                                        unsigned count,
    242                                        unsigned start_instance,
    243                                        unsigned instance_count);
    244 
    245 
    246 #endif
    247