Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_private.h
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* Private data structures, etc for the draw module.29*/303132/**33* Authors:34* Keith Whitwell <[email protected]>35* Brian Paul36*/373839#ifndef DRAW_PRIVATE_H40#define DRAW_PRIVATE_H414243#include "pipe/p_state.h"44#include "pipe/p_defines.h"4546#include "tgsi/tgsi_scan.h"4748#ifdef DRAW_LLVM_AVAILABLE49struct gallivm_state;50#endif515253/** Sum of frustum planes and user-defined planes */54#define DRAW_TOTAL_CLIP_PLANES (6 + PIPE_MAX_CLIP_PLANES)5556/**57* The largest possible index of a vertex that can be fetched.58*/59#define DRAW_MAX_FETCH_IDX 0xffffffff6061/**62* Maximum number of extra shader outputs. These are allocated by:63* - draw_pipe_aaline.c (1)64* - draw_pipe_aapoint.c (1)65* - draw_pipe_unfilled.c (1)66* - draw_pipe_wide_point.c (up to 32)67* - draw_prim_assembler.c (1)68*/69#define DRAW_MAX_EXTRA_SHADER_OUTPUTS 327071/**72* Despite some efforts to determine the number of extra shader outputs ahead73* of time, the matter of fact is that this number will vary as primitives74* flow through the draw pipeline. In particular, aaline/aapoint stages75* only allocate their extra shader outputs on the first line/point.76*77* Consequently dup_vert() ends up copying vertices larger than those78* allocated.79*80* Ideally we'd keep track of incoming/outgoing vertex sizes (and strides)81* throughout the draw pipeline, but unfortunately we recompute these all over82* the place, so preemptively expanding the vertex stride/size does not work83* as mismatches ensue.84*85* As stopgap to prevent buffer read overflows, we allocate an extra bit of86* padding at the end of temporary vertex buffers, allowing dup_vert() to copy87* more vertex attributes than allocated.88*/89#define DRAW_EXTRA_VERTICES_PADDING \90(DRAW_MAX_EXTRA_SHADER_OUTPUTS * sizeof(float[4]))9192struct pipe_context;93struct draw_vertex_shader;94struct draw_context;95struct draw_stage;96struct vbuf_render;97struct tgsi_exec_machine;98struct tgsi_sampler;99struct tgsi_image;100struct tgsi_buffer;101struct draw_pt_front_end;102struct draw_assembler;103struct draw_llvm;104struct lp_cached_code;105106/**107* Represents the mapped vertex buffer.108*/109struct draw_vertex_buffer {110const void *map;111uint32_t size;112};113114/**115* Basic vertex info.116* Carry some useful information around with the vertices in the prim pipe.117*/118struct vertex_header {119unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;120unsigned edgeflag:1;121unsigned pad:1;122unsigned vertex_id:16;123124float clip_pos[4];125126/* This will probably become float (*data)[4] soon:127*/128float data[][4];129};130131/* NOTE: It should match vertex_id size above */132#define UNDEFINED_VERTEX_ID 0xffff133134135/* maximum number of shader variants we can cache */136#define DRAW_MAX_SHADER_VARIANTS 512137138/**139* Private context for the drawing module.140*/141struct draw_context142{143struct pipe_context *pipe;144145/** Drawing/primitive pipeline stages */146struct {147struct draw_stage *first; /**< one of the following */148149struct draw_stage *validate;150151/* stages (in logical order) */152struct draw_stage *flatshade;153struct draw_stage *clip;154struct draw_stage *cull;155struct draw_stage *user_cull;156struct draw_stage *twoside;157struct draw_stage *offset;158struct draw_stage *unfilled;159struct draw_stage *stipple;160struct draw_stage *aapoint;161struct draw_stage *aaline;162struct draw_stage *pstipple;163struct draw_stage *wide_line;164struct draw_stage *wide_point;165struct draw_stage *rasterize;166167float wide_point_threshold; /**< convert pnts to tris if larger than this */168float wide_line_threshold; /**< convert lines to tris if wider than this */169boolean wide_point_sprites; /**< convert points to tris for sprite mode */170boolean line_stipple; /**< do line stipple? */171boolean point_sprite; /**< convert points to quads for sprites? */172173/* Temporary storage while the pipeline is being run:174*/175char *verts;176unsigned vertex_stride;177unsigned vertex_count;178} pipeline;179180181struct vbuf_render *render;182183/* Support prototype passthrough path:184*/185struct {186/* Current active frontend */187struct draw_pt_front_end *frontend;188unsigned prim;189unsigned opt; /**< bitmask of PT_x flags */190unsigned eltSize; /* saved eltSize for flushing */191ubyte vertices_per_patch;192boolean rebind_parameters;193194struct {195struct draw_pt_middle_end *fetch_shade_emit;196struct draw_pt_middle_end *general;197struct draw_pt_middle_end *llvm;198} middle;199200struct {201struct draw_pt_front_end *vsplit;202} front;203204struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];205unsigned nr_vertex_buffers;206207/*208* This is the largest legal index value for the current set of209* bound vertex buffers. Regardless of any other consideration,210* all vertex lookups need to be clamped to 0..max_index to211* prevent out-of-bound access.212*/213unsigned max_index;214215struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];216unsigned nr_vertex_elements;217218/* user-space vertex data, buffers */219struct {220/** vertex element/index buffer (ex: glDrawElements) */221const void *elts;222/** bytes per index (0, 1, 2 or 4) */223unsigned eltSizeIB;224unsigned eltSize;225unsigned eltMax;226int eltBias;227unsigned min_index;228unsigned max_index;229unsigned drawid;230bool increment_draw_id;231unsigned viewid;232233/** vertex arrays */234struct draw_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];235236/** constant buffers (for vertex/geometry shader) */237const void *vs_constants[PIPE_MAX_CONSTANT_BUFFERS];238unsigned vs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];239const void *gs_constants[PIPE_MAX_CONSTANT_BUFFERS];240unsigned gs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];241const void *tcs_constants[PIPE_MAX_CONSTANT_BUFFERS];242unsigned tcs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];243const void *tes_constants[PIPE_MAX_CONSTANT_BUFFERS];244unsigned tes_constants_size[PIPE_MAX_CONSTANT_BUFFERS];245246/** shader buffers (for vertex/geometry shader) */247const void *vs_ssbos[PIPE_MAX_SHADER_BUFFERS];248unsigned vs_ssbos_size[PIPE_MAX_SHADER_BUFFERS];249const void *gs_ssbos[PIPE_MAX_SHADER_BUFFERS];250unsigned gs_ssbos_size[PIPE_MAX_SHADER_BUFFERS];251const void *tcs_ssbos[PIPE_MAX_SHADER_BUFFERS];252unsigned tcs_ssbos_size[PIPE_MAX_SHADER_BUFFERS];253const void *tes_ssbos[PIPE_MAX_SHADER_BUFFERS];254unsigned tes_ssbos_size[PIPE_MAX_SHADER_BUFFERS];255256/* pointer to planes */257float (*planes)[DRAW_TOTAL_CLIP_PLANES][4];258} user;259260boolean test_fse; /* enable FSE even though its not correct (eg for softpipe) */261boolean no_fse; /* disable FSE even when it is correct */262} pt;263264struct {265boolean bypass_clip_xy;266boolean bypass_clip_z;267boolean guard_band_xy;268boolean bypass_clip_points;269} driver;270271boolean quads_always_flatshade_last;272273boolean flushing; /**< debugging/sanity */274boolean suspend_flushing; /**< internally set */275276/* Flags set if API requires clipping in these planes and the277* driver doesn't indicate that it can do it for us.278*/279boolean clip_xy;280boolean clip_z;281boolean clip_user;282boolean guard_band_xy;283boolean guard_band_points_xy;284285boolean dump_vs;286287/** Depth format and bias related settings. */288boolean floating_point_depth;289double mrd; /**< minimum resolvable depth value, for polygon offset */290291/** Current rasterizer state given to us by the driver */292const struct pipe_rasterizer_state *rasterizer;293/** Driver CSO handle for the current rasterizer state */294void *rast_handle;295296/** Rasterizer CSOs without culling/stipple/etc */297void *rasterizer_no_cull[2][2][2];298299struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];300boolean identity_viewport;301boolean bypass_viewport;302303/** Vertex shader state */304struct {305struct draw_vertex_shader *vertex_shader;306uint num_vs_outputs; /**< convenience, from vertex_shader */307uint position_output;308uint edgeflag_output;309uint clipvertex_output;310uint ccdistance_output[2];311312/** Fields for TGSI interpreter / execution */313struct {314struct tgsi_exec_machine *machine;315316struct tgsi_sampler *sampler;317struct tgsi_image *image;318struct tgsi_buffer *buffer;319} tgsi;320321struct translate *fetch;322struct translate_cache *fetch_cache;323struct translate *emit;324struct translate_cache *emit_cache;325} vs;326327/** Geometry shader state */328struct {329struct draw_geometry_shader *geometry_shader;330uint num_gs_outputs; /**< convenience, from geometry_shader */331uint position_output;332333/** Fields for TGSI interpreter / execution */334struct {335struct tgsi_exec_machine *machine;336337struct tgsi_sampler *sampler;338struct tgsi_image *image;339struct tgsi_buffer *buffer;340} tgsi;341342} gs;343344/* Tessellation state */345struct {346struct draw_tess_ctrl_shader *tess_ctrl_shader;347348/** Fields for TGSI interpreter / execution */349struct {350struct tgsi_exec_machine *machine;351352struct tgsi_sampler *sampler;353struct tgsi_image *image;354struct tgsi_buffer *buffer;355} tgsi;356} tcs;357358struct {359struct draw_tess_eval_shader *tess_eval_shader;360uint position_output;361362/** Fields for TGSI interpreter / execution */363struct {364struct tgsi_exec_machine *machine;365366struct tgsi_sampler *sampler;367struct tgsi_image *image;368struct tgsi_buffer *buffer;369} tgsi;370} tes;371372/** Fragment shader state */373struct {374struct draw_fragment_shader *fragment_shader;375} fs;376377/** Stream output (vertex feedback) state */378struct {379struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];380uint num_targets;381} so;382383/* Clip derived state:384*/385float plane[DRAW_TOTAL_CLIP_PLANES][4];386387/* If a prim stage introduces new vertex attributes, they'll be stored here388*/389struct {390uint num;391uint semantic_name[DRAW_MAX_EXTRA_SHADER_OUTPUTS];392uint semantic_index[DRAW_MAX_EXTRA_SHADER_OUTPUTS];393uint slot[DRAW_MAX_EXTRA_SHADER_OUTPUTS];394} extra_shader_outputs;395396unsigned instance_id;397unsigned start_instance;398unsigned start_index;399unsigned constant_buffer_stride;400struct draw_llvm *llvm;401402/** Texture sampler and sampler view state.403* Note that we have arrays indexed by shader type. At this time404* we only handle vertex and geometry shaders in the draw module, but405* there may be more in the future (ex: hull and tessellation).406*/407struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];408unsigned num_sampler_views[PIPE_SHADER_TYPES];409const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];410unsigned num_samplers[PIPE_SHADER_TYPES];411412struct pipe_image_view *images[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_IMAGES];413unsigned num_images[PIPE_SHADER_TYPES];414415struct pipe_query_data_pipeline_statistics statistics;416boolean collect_statistics;417418float default_outer_tess_level[4];419float default_inner_tess_level[2];420bool collect_primgen;421422struct draw_assembler *ia;423424void *disk_cache_cookie;425void (*disk_cache_find_shader)(void *cookie,426struct lp_cached_code *cache,427unsigned char ir_sha1_cache_key[20]);428void (*disk_cache_insert_shader)(void *cookie,429struct lp_cached_code *cache,430unsigned char ir_sha1_cache_key[20]);431432void *driver_private;433};434435436struct draw_fetch_info {437boolean linear;438unsigned start;439const unsigned *elts;440unsigned count;441};442443struct draw_vertex_info {444struct vertex_header *verts;445unsigned vertex_size;446unsigned stride;447unsigned count;448};449450/* these flags are set if the primitive is a segment of a larger one */451#define DRAW_SPLIT_BEFORE 0x1452#define DRAW_SPLIT_AFTER 0x2453#define DRAW_LINE_LOOP_AS_STRIP 0x4454455struct draw_prim_info {456boolean linear;457unsigned start;458459const ushort *elts;460unsigned count;461462unsigned prim;463unsigned flags;464unsigned *primitive_lengths;465unsigned primitive_count;466};467468469/*******************************************************************************470* Draw common initialization code471*/472boolean draw_init(struct draw_context *draw);473void draw_new_instance(struct draw_context *draw);474475/*******************************************************************************476* Vertex shader code:477*/478boolean draw_vs_init( struct draw_context *draw );479void draw_vs_destroy( struct draw_context *draw );480481482/*******************************************************************************483* Geometry shading code:484*/485boolean draw_gs_init( struct draw_context *draw );486487488void draw_gs_destroy( struct draw_context *draw );489490/*******************************************************************************491* Common shading code:492*/493uint draw_current_shader_outputs(const struct draw_context *draw);494uint draw_current_shader_position_output(const struct draw_context *draw);495uint draw_current_shader_viewport_index_output(const struct draw_context *draw);496uint draw_current_shader_clipvertex_output(const struct draw_context *draw);497uint draw_current_shader_ccdistance_output(const struct draw_context *draw, int index);498uint draw_current_shader_num_written_clipdistances(const struct draw_context *draw);499uint draw_current_shader_num_written_culldistances(const struct draw_context *draw);500int draw_alloc_extra_vertex_attrib(struct draw_context *draw,501uint semantic_name, uint semantic_index);502void draw_remove_extra_vertex_attribs(struct draw_context *draw);503boolean draw_current_shader_uses_viewport_index(504const struct draw_context *draw);505506507/*******************************************************************************508* Vertex processing (was passthrough) code:509*/510boolean draw_pt_init( struct draw_context *draw );511void draw_pt_destroy( struct draw_context *draw );512void draw_pt_reset_vertex_ids( struct draw_context *draw );513void draw_pt_flush( struct draw_context *draw, unsigned flags );514515516/*******************************************************************************517* Primitive processing (pipeline) code:518*/519520boolean draw_pipeline_init( struct draw_context *draw );521void draw_pipeline_destroy( struct draw_context *draw );522523524525526527/*528* These flags are used by the pipeline when unfilled and/or line stipple modes529* are operational.530*/531#define DRAW_PIPE_EDGE_FLAG_0 0x1532#define DRAW_PIPE_EDGE_FLAG_1 0x2533#define DRAW_PIPE_EDGE_FLAG_2 0x4534#define DRAW_PIPE_EDGE_FLAG_ALL 0x7535#define DRAW_PIPE_RESET_STIPPLE 0x8536537void draw_pipeline_run( struct draw_context *draw,538const struct draw_vertex_info *vert,539const struct draw_prim_info *prim);540541void draw_pipeline_run_linear( struct draw_context *draw,542const struct draw_vertex_info *vert,543const struct draw_prim_info *prim);544545546547548void draw_pipeline_flush( struct draw_context *draw,549unsigned flags );550551552553/*******************************************************************************554* Flushing555*/556557#define DRAW_FLUSH_PARAMETER_CHANGE 0x1 /**< Constants, viewport, etc */558#define DRAW_FLUSH_STATE_CHANGE 0x2 /**< Other/heavy state changes */559#define DRAW_FLUSH_BACKEND 0x4 /**< Flush the output buffer */560561562void draw_do_flush( struct draw_context *draw, unsigned flags );563564565566void *567draw_get_rasterizer_no_cull( struct draw_context *draw,568const struct pipe_rasterizer_state *rast );569570void571draw_stats_clipper_primitives(struct draw_context *draw,572const struct draw_prim_info *prim_info);573574void draw_update_clip_flags(struct draw_context *draw);575void draw_update_viewport_flags(struct draw_context *draw);576577/**578* Return index i from the index buffer.579* If the index buffer would overflow we return index 0.580*/581#define DRAW_GET_IDX(_elts, _i) \582(((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])583584/**585* Return index of the given viewport clamping it586* to be between 0 <= and < PIPE_MAX_VIEWPORTS587*/588static inline unsigned589draw_clamp_viewport_idx(int idx)590{591return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0);592}593594/**595* Adds two unsigned integers and if the addition596* overflows then it returns the value from597* the overflow_value variable.598*/599static inline unsigned600draw_overflow_uadd(unsigned a, unsigned b,601unsigned overflow_value)602{603unsigned res = a + b;604if (res < a) {605res = overflow_value;606}607return res;608}609610#endif /* DRAW_PRIVATE_H */611612613