Home | History | Annotate | Download | only in draw
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 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 /**
     29  * Private data structures, etc for the draw module.
     30  */
     31 
     32 
     33 /**
     34  * Authors:
     35  * Keith Whitwell <keithw (at) vmware.com>
     36  * Brian Paul
     37  */
     38 
     39 
     40 #ifndef DRAW_PRIVATE_H
     41 #define DRAW_PRIVATE_H
     42 
     43 
     44 #include "pipe/p_state.h"
     45 #include "pipe/p_defines.h"
     46 
     47 #include "tgsi/tgsi_scan.h"
     48 
     49 #ifdef HAVE_LLVM
     50 struct gallivm_state;
     51 #endif
     52 
     53 
     54 /** Sum of frustum planes and user-defined planes */
     55 #define DRAW_TOTAL_CLIP_PLANES (6 + PIPE_MAX_CLIP_PLANES)
     56 
     57 /**
     58  * The largest possible index of a vertex that can be fetched.
     59  */
     60 #define DRAW_MAX_FETCH_IDX 0xffffffff
     61 
     62 struct pipe_context;
     63 struct draw_vertex_shader;
     64 struct draw_context;
     65 struct draw_stage;
     66 struct vbuf_render;
     67 struct tgsi_exec_machine;
     68 struct tgsi_sampler;
     69 struct tgsi_image;
     70 struct tgsi_buffer;
     71 struct draw_pt_front_end;
     72 struct draw_assembler;
     73 struct draw_llvm;
     74 
     75 
     76 /**
     77  * Represents the mapped vertex buffer.
     78  */
     79 struct draw_vertex_buffer {
     80    const void *map;
     81    uint32_t size;
     82 };
     83 
     84 /**
     85  * Basic vertex info.
     86  * Carry some useful information around with the vertices in the prim pipe.
     87  */
     88 struct vertex_header {
     89    unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
     90    unsigned edgeflag:1;
     91    unsigned pad:1;
     92    unsigned vertex_id:16;
     93 
     94    float clip_pos[4];
     95 
     96    /* This will probably become float (*data)[4] soon:
     97     */
     98    float data[][4];
     99 };
    100 
    101 /* NOTE: It should match vertex_id size above */
    102 #define UNDEFINED_VERTEX_ID 0xffff
    103 
    104 
    105 /* maximum number of shader variants we can cache */
    106 #define DRAW_MAX_SHADER_VARIANTS 128
    107 
    108 /**
    109  * Private context for the drawing module.
    110  */
    111 struct draw_context
    112 {
    113    struct pipe_context *pipe;
    114 
    115    /** Drawing/primitive pipeline stages */
    116    struct {
    117       struct draw_stage *first;  /**< one of the following */
    118 
    119       struct draw_stage *validate;
    120 
    121       /* stages (in logical order) */
    122       struct draw_stage *flatshade;
    123       struct draw_stage *clip;
    124       struct draw_stage *cull;
    125       struct draw_stage *twoside;
    126       struct draw_stage *offset;
    127       struct draw_stage *unfilled;
    128       struct draw_stage *stipple;
    129       struct draw_stage *aapoint;
    130       struct draw_stage *aaline;
    131       struct draw_stage *pstipple;
    132       struct draw_stage *wide_line;
    133       struct draw_stage *wide_point;
    134       struct draw_stage *rasterize;
    135 
    136       float wide_point_threshold; /**< convert pnts to tris if larger than this */
    137       float wide_line_threshold;  /**< convert lines to tris if wider than this */
    138       boolean wide_point_sprites; /**< convert points to tris for sprite mode */
    139       boolean line_stipple;       /**< do line stipple? */
    140       boolean point_sprite;       /**< convert points to quads for sprites? */
    141 
    142       /* Temporary storage while the pipeline is being run:
    143        */
    144       char *verts;
    145       unsigned vertex_stride;
    146       unsigned vertex_count;
    147    } pipeline;
    148 
    149 
    150    struct vbuf_render *render;
    151 
    152    /* Support prototype passthrough path:
    153     */
    154    struct {
    155       /* Current active frontend */
    156       struct draw_pt_front_end *frontend;
    157       unsigned prim;
    158       unsigned opt;     /**< bitmask of PT_x flags */
    159       unsigned eltSize; /* saved eltSize for flushing */
    160 
    161       boolean rebind_parameters;
    162 
    163       struct {
    164          struct draw_pt_middle_end *fetch_emit;
    165          struct draw_pt_middle_end *fetch_shade_emit;
    166          struct draw_pt_middle_end *general;
    167          struct draw_pt_middle_end *llvm;
    168       } middle;
    169 
    170       struct {
    171          struct draw_pt_front_end *vsplit;
    172       } front;
    173 
    174       struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
    175       unsigned nr_vertex_buffers;
    176 
    177       /*
    178        * This is the largest legal index value for the current set of
    179        * bound vertex buffers.  Regardless of any other consideration,
    180        * all vertex lookups need to be clamped to 0..max_index to
    181        * prevent out-of-bound access.
    182        */
    183       unsigned max_index;
    184 
    185       struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
    186       unsigned nr_vertex_elements;
    187 
    188       /* user-space vertex data, buffers */
    189       struct {
    190          /** vertex element/index buffer (ex: glDrawElements) */
    191          const void *elts;
    192          /** bytes per index (0, 1, 2 or 4) */
    193          unsigned eltSizeIB;
    194          unsigned eltSize;
    195          unsigned eltMax;
    196          int eltBias;
    197          unsigned min_index;
    198          unsigned max_index;
    199 
    200          /** vertex arrays */
    201          struct draw_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
    202 
    203          /** constant buffers (for vertex/geometry shader) */
    204          const void *vs_constants[PIPE_MAX_CONSTANT_BUFFERS];
    205          unsigned vs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
    206          const void *gs_constants[PIPE_MAX_CONSTANT_BUFFERS];
    207          unsigned gs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
    208 
    209          /* pointer to planes */
    210          float (*planes)[DRAW_TOTAL_CLIP_PLANES][4];
    211       } user;
    212 
    213       boolean test_fse;         /* enable FSE even though its not correct (eg for softpipe) */
    214       boolean no_fse;           /* disable FSE even when it is correct */
    215    } pt;
    216 
    217    struct {
    218       boolean bypass_clip_xy;
    219       boolean bypass_clip_z;
    220       boolean guard_band_xy;
    221       boolean bypass_clip_points;
    222    } driver;
    223 
    224    boolean quads_always_flatshade_last;
    225 
    226    boolean flushing;         /**< debugging/sanity */
    227    boolean suspend_flushing; /**< internally set */
    228 
    229    /* Flags set if API requires clipping in these planes and the
    230     * driver doesn't indicate that it can do it for us.
    231     */
    232    boolean clip_xy;
    233    boolean clip_z;
    234    boolean clip_user;
    235    boolean guard_band_xy;
    236    boolean guard_band_points_xy;
    237 
    238    boolean force_passthrough; /**< never clip or shade */
    239 
    240    boolean dump_vs;
    241 
    242    /** Depth format and bias related settings. */
    243    boolean floating_point_depth;
    244    double mrd;  /**< minimum resolvable depth value, for polygon offset */
    245 
    246    /** Current rasterizer state given to us by the driver */
    247    const struct pipe_rasterizer_state *rasterizer;
    248    /** Driver CSO handle for the current rasterizer state */
    249    void *rast_handle;
    250 
    251    /** Rasterizer CSOs without culling/stipple/etc */
    252    void *rasterizer_no_cull[2][2];
    253 
    254    struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];
    255    boolean identity_viewport;
    256    boolean bypass_viewport;
    257 
    258    /** Vertex shader state */
    259    struct {
    260       struct draw_vertex_shader *vertex_shader;
    261       uint num_vs_outputs;  /**< convenience, from vertex_shader */
    262       uint position_output;
    263       uint edgeflag_output;
    264       uint clipvertex_output;
    265       uint ccdistance_output[2];
    266 
    267       /** Fields for TGSI interpreter / execution */
    268       struct {
    269          struct tgsi_exec_machine *machine;
    270 
    271          struct tgsi_sampler *sampler;
    272          struct tgsi_image *image;
    273          struct tgsi_buffer *buffer;
    274       } tgsi;
    275 
    276       struct translate *fetch;
    277       struct translate_cache *fetch_cache;
    278       struct translate *emit;
    279       struct translate_cache *emit_cache;
    280    } vs;
    281 
    282    /** Geometry shader state */
    283    struct {
    284       struct draw_geometry_shader *geometry_shader;
    285       uint num_gs_outputs;  /**< convenience, from geometry_shader */
    286       uint position_output;
    287 
    288       /** Fields for TGSI interpreter / execution */
    289       struct {
    290          struct tgsi_exec_machine *machine;
    291 
    292          struct tgsi_sampler *sampler;
    293          struct tgsi_image *image;
    294          struct tgsi_buffer *buffer;
    295       } tgsi;
    296 
    297    } gs;
    298 
    299    /** Fragment shader state */
    300    struct {
    301       struct draw_fragment_shader *fragment_shader;
    302    } fs;
    303 
    304    /** Stream output (vertex feedback) state */
    305    struct {
    306       struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
    307       uint num_targets;
    308    } so;
    309 
    310    /* Clip derived state:
    311     */
    312    float plane[DRAW_TOTAL_CLIP_PLANES][4];
    313 
    314    /* If a prim stage introduces new vertex attributes, they'll be stored here
    315     */
    316    struct {
    317       uint num;
    318       uint semantic_name[10];
    319       uint semantic_index[10];
    320       uint slot[10];
    321    } extra_shader_outputs;
    322 
    323    unsigned instance_id;
    324    unsigned start_instance;
    325    unsigned start_index;
    326 
    327    struct draw_llvm *llvm;
    328 
    329    /** Texture sampler and sampler view state.
    330     * Note that we have arrays indexed by shader type.  At this time
    331     * we only handle vertex and geometry shaders in the draw module, but
    332     * there may be more in the future (ex: hull and tessellation).
    333     */
    334    struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
    335    unsigned num_sampler_views[PIPE_SHADER_TYPES];
    336    const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
    337    unsigned num_samplers[PIPE_SHADER_TYPES];
    338 
    339    struct pipe_query_data_pipeline_statistics statistics;
    340    boolean collect_statistics;
    341 
    342    struct draw_assembler *ia;
    343 
    344    void *driver_private;
    345 };
    346 
    347 
    348 struct draw_fetch_info {
    349    boolean linear;
    350    unsigned start;
    351    const unsigned *elts;
    352    unsigned count;
    353 };
    354 
    355 struct draw_vertex_info {
    356    struct vertex_header *verts;
    357    unsigned vertex_size;
    358    unsigned stride;
    359    unsigned count;
    360 };
    361 
    362 /* these flags are set if the primitive is a segment of a larger one */
    363 #define DRAW_SPLIT_BEFORE        0x1
    364 #define DRAW_SPLIT_AFTER         0x2
    365 #define DRAW_LINE_LOOP_AS_STRIP  0x4
    366 
    367 struct draw_prim_info {
    368    boolean linear;
    369    unsigned start;
    370 
    371    const ushort *elts;
    372    unsigned count;
    373 
    374    unsigned prim;
    375    unsigned flags;
    376    unsigned *primitive_lengths;
    377    unsigned primitive_count;
    378 };
    379 
    380 
    381 /*******************************************************************************
    382  * Draw common initialization code
    383  */
    384 boolean draw_init(struct draw_context *draw);
    385 void draw_new_instance(struct draw_context *draw);
    386 
    387 /*******************************************************************************
    388  * Vertex shader code:
    389  */
    390 boolean draw_vs_init( struct draw_context *draw );
    391 void draw_vs_destroy( struct draw_context *draw );
    392 
    393 
    394 /*******************************************************************************
    395  * Geometry shading code:
    396  */
    397 boolean draw_gs_init( struct draw_context *draw );
    398 
    399 
    400 void draw_gs_destroy( struct draw_context *draw );
    401 
    402 /*******************************************************************************
    403  * Common shading code:
    404  */
    405 uint draw_current_shader_outputs(const struct draw_context *draw);
    406 uint draw_current_shader_position_output(const struct draw_context *draw);
    407 uint draw_current_shader_viewport_index_output(const struct draw_context *draw);
    408 uint draw_current_shader_clipvertex_output(const struct draw_context *draw);
    409 uint draw_current_shader_ccdistance_output(const struct draw_context *draw, int index);
    410 uint draw_current_shader_num_written_clipdistances(const struct draw_context *draw);
    411 uint draw_current_shader_num_written_culldistances(const struct draw_context *draw);
    412 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
    413                                    uint semantic_name, uint semantic_index);
    414 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
    415 boolean draw_current_shader_uses_viewport_index(
    416    const struct draw_context *draw);
    417 
    418 
    419 /*******************************************************************************
    420  * Vertex processing (was passthrough) code:
    421  */
    422 boolean draw_pt_init( struct draw_context *draw );
    423 void draw_pt_destroy( struct draw_context *draw );
    424 void draw_pt_reset_vertex_ids( struct draw_context *draw );
    425 void draw_pt_flush( struct draw_context *draw, unsigned flags );
    426 
    427 
    428 /*******************************************************************************
    429  * Primitive processing (pipeline) code:
    430  */
    431 
    432 boolean draw_pipeline_init( struct draw_context *draw );
    433 void draw_pipeline_destroy( struct draw_context *draw );
    434 
    435 
    436 
    437 
    438 
    439 /*
    440  * These flags are used by the pipeline when unfilled and/or line stipple modes
    441  * are operational.
    442  */
    443 #define DRAW_PIPE_EDGE_FLAG_0   0x1
    444 #define DRAW_PIPE_EDGE_FLAG_1   0x2
    445 #define DRAW_PIPE_EDGE_FLAG_2   0x4
    446 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
    447 #define DRAW_PIPE_RESET_STIPPLE 0x8
    448 
    449 void draw_pipeline_run( struct draw_context *draw,
    450                         const struct draw_vertex_info *vert,
    451                         const struct draw_prim_info *prim);
    452 
    453 void draw_pipeline_run_linear( struct draw_context *draw,
    454                                const struct draw_vertex_info *vert,
    455                                const struct draw_prim_info *prim);
    456 
    457 
    458 
    459 
    460 void draw_pipeline_flush( struct draw_context *draw,
    461                           unsigned flags );
    462 
    463 
    464 
    465 /*******************************************************************************
    466  * Flushing
    467  */
    468 
    469 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1  /**< Constants, viewport, etc */
    470 #define DRAW_FLUSH_STATE_CHANGE     0x2  /**< Other/heavy state changes */
    471 #define DRAW_FLUSH_BACKEND          0x4  /**< Flush the output buffer */
    472 
    473 
    474 void draw_do_flush( struct draw_context *draw, unsigned flags );
    475 
    476 
    477 
    478 void *
    479 draw_get_rasterizer_no_cull( struct draw_context *draw,
    480                              boolean scissor,
    481                              boolean flatshade );
    482 
    483 void
    484 draw_stats_clipper_primitives(struct draw_context *draw,
    485                               const struct draw_prim_info *prim_info);
    486 
    487 void draw_update_clip_flags(struct draw_context *draw);
    488 void draw_update_viewport_flags(struct draw_context *draw);
    489 
    490 /**
    491  * Return index i from the index buffer.
    492  * If the index buffer would overflow we return index 0.
    493  */
    494 #define DRAW_GET_IDX(_elts, _i)                   \
    495    (((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])
    496 
    497 /**
    498  * Return index of the given viewport clamping it
    499  * to be between 0 <= and < PIPE_MAX_VIEWPORTS
    500  */
    501 static inline unsigned
    502 draw_clamp_viewport_idx(int idx)
    503 {
    504    return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0);
    505 }
    506 
    507 /**
    508  * Adds two unsigned integers and if the addition
    509  * overflows then it returns the value from
    510  * the overflow_value variable.
    511  */
    512 static inline unsigned
    513 draw_overflow_uadd(unsigned a, unsigned b,
    514                    unsigned overflow_value)
    515 {
    516    unsigned res = a + b;
    517    if (res < a) {
    518       res = overflow_value;
    519    }
    520    return res;
    521 }
    522 
    523 #endif /* DRAW_PRIVATE_H */
    524