• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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 #include "pipe/p_shader_tokens.h"
47 
48 #include "draw_vertex_header.h"
49 
50 #if DRAW_LLVM_AVAILABLE
51 struct gallivm_state;
52 #endif
53 
54 /**
55  * The max stage the draw stores resources for.
56  * i.e. vs, tcs, tes, gs. no fs/cs/ms/ts.
57  */
58 #define DRAW_MAX_SHADER_STAGE (PIPE_SHADER_GEOMETRY + 1)
59 
60 /**
61  * The largest possible index of a vertex that can be fetched.
62  */
63 #define DRAW_MAX_FETCH_IDX 0xffffffff
64 
65 /**
66  * Maximum number of extra shader outputs.  These are allocated by:
67  * - draw_pipe_aaline.c (1)
68  * - draw_pipe_aapoint.c (1)
69  * - draw_pipe_unfilled.c (1)
70  * - draw_pipe_wide_point.c (up to 32)
71  * - draw_prim_assembler.c (1)
72  */
73 #define DRAW_MAX_EXTRA_SHADER_OUTPUTS 32
74 
75 /**
76  * Despite some efforts to determine the number of extra shader outputs ahead
77  * of time, the matter of fact is that this number will vary as primitives
78  * flow through the draw pipeline.  In particular, aaline/aapoint stages
79  * only allocate their extra shader outputs on the first line/point.
80  *
81  * Consequently dup_vert() ends up copying vertices larger than those
82  * allocated.
83  *
84  * Ideally we'd keep track of incoming/outgoing vertex sizes (and strides)
85  * throughout the draw pipeline, but unfortunately we recompute these all over
86  * the place, so preemptively expanding the vertex stride/size does not work
87  * as mismatches ensue.
88  *
89  * As stopgap to prevent buffer read overflows, we allocate an extra bit of
90  * padding at the end of temporary vertex buffers, allowing dup_vert() to copy
91  * more vertex attributes than allocated.
92  */
93 #define DRAW_EXTRA_VERTICES_PADDING \
94    (DRAW_MAX_EXTRA_SHADER_OUTPUTS * sizeof(float[4]))
95 
96 struct pipe_context;
97 struct draw_vertex_shader;
98 struct draw_stage;
99 struct draw_pt_front_end;
100 struct draw_assembler;
101 struct draw_llvm;
102 struct vbuf_render;
103 struct tgsi_exec_machine;
104 struct tgsi_sampler;
105 struct tgsi_image;
106 struct tgsi_buffer;
107 struct lp_cached_code;
108 struct draw_vertex_info;
109 struct draw_prim_info;
110 
111 /**
112  * Represents the mapped vertex buffer.
113  */
114 struct draw_vertex_buffer {
115    const void *map;
116    uint32_t size;
117 };
118 
119 /* NOTE: It should match vertex_id size above */
120 #define UNDEFINED_VERTEX_ID 0xffff
121 
122 
123 /* maximum number of shader variants we can cache */
124 #define DRAW_MAX_SHADER_VARIANTS 512
125 
126 struct draw_buffer_info {
127    const void *ptr;
128    unsigned size;
129 };
130 
131 /**
132  * Private context for the drawing module.
133  */
134 struct draw_context
135 {
136    struct pipe_context *pipe;
137 
138    /** Drawing/primitive pipeline stages */
139    struct {
140       struct draw_stage *first;  /**< one of the following */
141 
142       struct draw_stage *validate;
143 
144       /* stages (in logical order) */
145       struct draw_stage *flatshade;
146       struct draw_stage *clip;
147       struct draw_stage *cull;
148       struct draw_stage *user_cull;
149       struct draw_stage *twoside;
150       struct draw_stage *offset;
151       struct draw_stage *unfilled;
152       struct draw_stage *stipple;
153       struct draw_stage *aapoint;
154       struct draw_stage *aaline;
155       struct draw_stage *pstipple;
156       struct draw_stage *wide_line;
157       struct draw_stage *wide_point;
158       struct draw_stage *rasterize;
159 
160       float wide_point_threshold; /**< convert pnts to tris if larger than this */
161       float wide_line_threshold;  /**< convert lines to tris if wider than this */
162       bool wide_point_sprites; /**< convert points to tris for sprite mode */
163       bool line_stipple;       /**< do line stipple? */
164       bool point_sprite;       /**< convert points to quads for sprites? */
165 
166       /* Temporary storage while the pipeline is being run:
167        */
168       char *verts;
169       unsigned vertex_stride;
170       unsigned vertex_count;
171    } pipeline;
172 
173    struct vbuf_render *render;
174 
175    /* Support prototype passthrough path:
176     */
177    struct {
178       /* Current active frontend */
179       struct draw_pt_front_end *frontend;
180       enum mesa_prim prim;
181       uint8_t vertices_per_patch;
182       bool rebind_parameters;
183 
184       unsigned opt;     /**< bitmask of PT_x flags */
185       unsigned eltSize; /* saved eltSize for flushing */
186       unsigned viewid; /* saved viewid for flushing */
187 
188       struct {
189          struct draw_pt_middle_end *fetch_shade_emit;
190          struct draw_pt_middle_end *general;
191          struct draw_pt_middle_end *llvm;
192          struct draw_pt_middle_end *mesh;
193       } middle;
194 
195       struct {
196          struct draw_pt_front_end *vsplit;
197       } front;
198 
199       struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
200       unsigned nr_vertex_buffers;
201 
202       /*
203        * This is the largest legal index value for the current set of
204        * bound vertex buffers.  Regardless of any other consideration,
205        * all vertex lookups need to be clamped to 0..max_index to
206        * prevent out-of-bound access.
207        */
208       unsigned max_index;
209 
210       unsigned vertex_strides[PIPE_MAX_ATTRIBS];
211       struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
212       unsigned nr_vertex_elements;
213 
214       bool test_fse;         /* enable FSE even though its not correct (eg for softpipe) */
215       bool no_fse;           /* disable FSE even when it is correct */
216 
217       /* user-space vertex data, buffers */
218       struct {
219          /** vertex element/index buffer (ex: glDrawElements) */
220          const void *elts;
221          /** bytes per index (0, 1, 2 or 4) */
222          unsigned eltSizeIB;
223          unsigned eltSize;
224          unsigned eltMax;
225          int eltBias;
226          unsigned min_index;
227          unsigned max_index;
228          unsigned drawid;
229          bool increment_draw_id;
230          unsigned viewid;
231 
232          /** vertex arrays */
233          struct draw_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
234 
235          /** constant buffers for each shader stage */
236          struct draw_buffer_info constants[DRAW_MAX_SHADER_STAGE][PIPE_MAX_CONSTANT_BUFFERS];
237          struct draw_buffer_info ssbos[DRAW_MAX_SHADER_STAGE][PIPE_MAX_SHADER_BUFFERS];
238 
239          /* pointer to planes */
240          float (*planes)[DRAW_TOTAL_CLIP_PLANES][4];
241       } user;
242    } pt;
243 
244    struct {
245       bool bypass_clip_xy;
246       bool bypass_clip_z;
247       bool guard_band_xy;
248       bool bypass_clip_points_lines;
249    } driver;
250 
251    bool quads_always_flatshade_last;
252 
253    bool flushing;         /**< debugging/sanity */
254    bool suspend_flushing; /**< internally set */
255 
256    /* Flags set if API requires clipping in these planes and the
257     * driver doesn't indicate that it can do it for us.
258     */
259    bool clip_xy;
260    bool clip_z;
261    bool clip_user;
262    bool guard_band_xy;
263    bool guard_band_points_lines_xy;
264 
265    bool dump_vs;
266    bool identity_viewport;
267    bool bypass_viewport;
268 
269    /** Depth format and bias related settings. */
270    bool floating_point_depth;
271    double mrd;  /**< minimum resolvable depth value, for polygon offset */
272 
273    /** Current rasterizer state given to us by the driver */
274    const struct pipe_rasterizer_state *rasterizer;
275    /** Driver CSO handle for the current rasterizer state */
276    void *rast_handle;
277 
278    /** Rasterizer CSOs without culling/stipple/etc */
279    void *rasterizer_no_cull[2][2][2];
280 
281    struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];
282 
283    /** Vertex shader state */
284    struct {
285       struct draw_vertex_shader *vertex_shader;
286       unsigned num_vs_outputs;  /**< convenience, from vertex_shader */
287       unsigned position_output;
288       unsigned edgeflag_output;
289       unsigned clipvertex_output;
290       unsigned ccdistance_output[2];
291 
292       /** Fields for TGSI interpreter / execution */
293       struct {
294          struct tgsi_exec_machine *machine;
295 
296          struct tgsi_sampler *sampler;
297          struct tgsi_image *image;
298          struct tgsi_buffer *buffer;
299       } tgsi;
300 
301       struct translate *fetch;
302       struct translate_cache *fetch_cache;
303       struct translate *emit;
304       struct translate_cache *emit_cache;
305    } vs;
306 
307    /** Geometry shader state */
308    struct {
309       struct draw_geometry_shader *geometry_shader;
310       unsigned num_gs_outputs;  /**< convenience, from geometry_shader */
311       unsigned position_output;
312       unsigned clipvertex_output;
313 
314       /** Fields for TGSI interpreter / execution */
315       struct {
316          struct tgsi_exec_machine *machine;
317 
318          struct tgsi_sampler *sampler;
319          struct tgsi_image *image;
320          struct tgsi_buffer *buffer;
321       } tgsi;
322    } gs;
323 
324    /* Tessellation state */
325    struct {
326       struct draw_tess_ctrl_shader *tess_ctrl_shader;
327    } tcs;
328 
329    struct {
330       struct draw_tess_eval_shader *tess_eval_shader;
331       unsigned num_tes_outputs;  /**< convenience, from tess_eval_shader */
332       unsigned position_output;
333       unsigned clipvertex_output;
334    } tes;
335 
336    /** Fragment shader state */
337    struct {
338       struct draw_fragment_shader *fragment_shader;
339    } fs;
340 
341    struct {
342       struct draw_mesh_shader *mesh_shader;
343       unsigned num_ms_outputs;  /**< convenience, from geometry_shader */
344       unsigned position_output;
345       unsigned clipvertex_output;
346    } ms;
347 
348    /** Stream output (vertex feedback) state */
349    struct {
350       struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
351       unsigned num_targets;
352    } so;
353 
354    /* Clip derived state:
355     */
356    float plane[DRAW_TOTAL_CLIP_PLANES][4];
357 
358    uint32_t viewmask;
359 
360    /* If a prim stage introduces new vertex attributes, they'll be stored here
361     */
362    struct {
363       unsigned num;
364       enum tgsi_semantic semantic_name[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
365       unsigned semantic_index[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
366       unsigned slot[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
367    } extra_shader_outputs;
368 
369    unsigned instance_id;
370    unsigned start_instance;
371    unsigned start_index;
372    unsigned constant_buffer_stride;
373    struct draw_llvm *llvm;
374 
375    /** Texture sampler and sampler view state.
376     * Note that we have arrays indexed by shader type.  At this time
377     * we only handle vertex and geometry shaders in the draw module, but
378     * there may be more in the future (ex: hull and tessellation).
379     */
380    struct pipe_sampler_view *sampler_views[DRAW_MAX_SHADER_STAGE][PIPE_MAX_SHADER_SAMPLER_VIEWS];
381    unsigned num_sampler_views[DRAW_MAX_SHADER_STAGE];
382    const struct pipe_sampler_state *samplers[DRAW_MAX_SHADER_STAGE][PIPE_MAX_SAMPLERS];
383    unsigned num_samplers[DRAW_MAX_SHADER_STAGE];
384 
385    struct pipe_image_view *images[DRAW_MAX_SHADER_STAGE][PIPE_MAX_SHADER_IMAGES];
386    unsigned num_images[DRAW_MAX_SHADER_STAGE];
387 
388    struct pipe_query_data_pipeline_statistics statistics;
389    bool collect_statistics;
390    bool collect_primgen;
391 
392    float default_outer_tess_level[4];
393    float default_inner_tess_level[2];
394 
395    struct draw_assembler *ia;
396 
397    void *disk_cache_cookie;
398    void (*disk_cache_find_shader)(void *cookie,
399                                   struct lp_cached_code *cache,
400                                   unsigned char ir_sha1_cache_key[20]);
401    void (*disk_cache_insert_shader)(void *cookie,
402                                     struct lp_cached_code *cache,
403                                     unsigned char ir_sha1_cache_key[20]);
404 
405    void *driver_private;
406 };
407 
408 
409 struct draw_fetch_info {
410    bool linear;
411    unsigned start;
412    const unsigned *elts;
413    unsigned count;
414 };
415 
416 /* these flags are set if the primitive is a segment of a larger one */
417 #define DRAW_SPLIT_BEFORE        0x1
418 #define DRAW_SPLIT_AFTER         0x2
419 #define DRAW_LINE_LOOP_AS_STRIP  0x4
420 
421 /*******************************************************************************
422  * Draw common initialization code
423  */
424 bool draw_init(struct draw_context *draw);
425 void draw_new_instance(struct draw_context *draw);
426 
427 /*******************************************************************************
428  * Vertex shader code:
429  */
430 bool draw_vs_init(struct draw_context *draw);
431 void draw_vs_destroy(struct draw_context *draw);
432 
433 
434 /*******************************************************************************
435  * Geometry shading code:
436  */
437 bool draw_gs_init(struct draw_context *draw);
438 
439 
440 void draw_gs_destroy(struct draw_context *draw);
441 
442 /*******************************************************************************
443  * Common shading code:
444  */
445 unsigned draw_current_shader_outputs(const struct draw_context *draw);
446 unsigned draw_current_shader_position_output(const struct draw_context *draw);
447 unsigned draw_current_shader_viewport_index_output(const struct draw_context *draw);
448 unsigned draw_current_shader_clipvertex_output(const struct draw_context *draw);
449 unsigned draw_current_shader_ccdistance_output(const struct draw_context *draw, int index);
450 unsigned draw_current_shader_num_written_clipdistances(const struct draw_context *draw);
451 unsigned draw_current_shader_num_written_culldistances(const struct draw_context *draw);
452 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
453                                    enum tgsi_semantic semantic_name,
454                                    unsigned semantic_index);
455 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
456 bool draw_current_shader_uses_viewport_index(
457    const struct draw_context *draw);
458 
459 
460 /*******************************************************************************
461  * Vertex processing (was passthrough) code:
462  */
463 bool draw_pt_init(struct draw_context *draw);
464 void draw_pt_destroy(struct draw_context *draw);
465 void draw_pt_reset_vertex_ids(struct draw_context *draw);
466 void draw_pt_flush(struct draw_context *draw, unsigned flags);
467 
468 
469 /*******************************************************************************
470  * Primitive processing (pipeline) code:
471  */
472 
473 bool draw_pipeline_init(struct draw_context *draw);
474 void draw_pipeline_destroy(struct draw_context *draw);
475 
476 /*
477  * These flags are used by the pipeline when unfilled and/or line stipple modes
478  * are operational.
479  */
480 #define DRAW_PIPE_EDGE_FLAG_0   0x1
481 #define DRAW_PIPE_EDGE_FLAG_1   0x2
482 #define DRAW_PIPE_EDGE_FLAG_2   0x4
483 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
484 #define DRAW_PIPE_RESET_STIPPLE 0x8
485 
486 void
487 draw_pipeline_run(struct draw_context *draw,
488                   const struct draw_vertex_info *vert,
489                   const struct draw_prim_info *prim);
490 
491 void
492 draw_pipeline_run_linear(struct draw_context *draw,
493                          const struct draw_vertex_info *vert,
494                          const struct draw_prim_info *prim);
495 
496 void
497 draw_pipeline_flush(struct draw_context *draw,
498                     unsigned flags);
499 
500 
501 /*
502  * Flushing
503  */
504 
505 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1  /**< Constants, viewport, etc */
506 #define DRAW_FLUSH_STATE_CHANGE     0x2  /**< Other/heavy state changes */
507 #define DRAW_FLUSH_BACKEND          0x4  /**< Flush the output buffer */
508 
509 
510 void
511 draw_do_flush(struct draw_context *draw, unsigned flags);
512 
513 void *
514 draw_get_rasterizer_no_cull(struct draw_context *draw,
515                              const struct pipe_rasterizer_state *rast);
516 
517 void
518 draw_stats_clipper_primitives(struct draw_context *draw,
519                               const struct draw_prim_info *prim_info);
520 
521 void
522 draw_update_clip_flags(struct draw_context *draw);
523 
524 void
525 draw_update_viewport_flags(struct draw_context *draw);
526 
527 
528 /**
529  * Return index i from the index buffer.
530  * If the index buffer would overflow we return index 0.
531  */
532 #define DRAW_GET_IDX(_elts, _i)                   \
533    (((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])
534 
535 
536 /**
537  * Return index of the given viewport clamping it
538  * to be between 0 <= and < PIPE_MAX_VIEWPORTS
539  */
540 static inline unsigned
draw_clamp_viewport_idx(int idx)541 draw_clamp_viewport_idx(int idx)
542 {
543    return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0);
544 }
545 
546 #endif /* DRAW_PRIVATE_H */
547