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