Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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