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 enum pipe_prim_type 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 num_tes_outputs; /**< convenience, from tess_eval_shader */
363 uint position_output;
364 uint clipvertex_output;
365
366 /** Fields for TGSI interpreter / execution */
367 struct {
368 struct tgsi_exec_machine *machine;
369
370 struct tgsi_sampler *sampler;
371 struct tgsi_image *image;
372 struct tgsi_buffer *buffer;
373 } tgsi;
374 } tes;
375
376 /** Fragment shader state */
377 struct {
378 struct draw_fragment_shader *fragment_shader;
379 } fs;
380
381 /** Stream output (vertex feedback) state */
382 struct {
383 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
384 uint num_targets;
385 } so;
386
387 /* Clip derived state:
388 */
389 float plane[DRAW_TOTAL_CLIP_PLANES][4];
390
391 /* If a prim stage introduces new vertex attributes, they'll be stored here
392 */
393 struct {
394 uint num;
395 uint semantic_name[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
396 uint semantic_index[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
397 uint slot[DRAW_MAX_EXTRA_SHADER_OUTPUTS];
398 } extra_shader_outputs;
399
400 unsigned instance_id;
401 unsigned start_instance;
402 unsigned start_index;
403 unsigned constant_buffer_stride;
404 struct draw_llvm *llvm;
405
406 /** Texture sampler and sampler view state.
407 * Note that we have arrays indexed by shader type. At this time
408 * we only handle vertex and geometry shaders in the draw module, but
409 * there may be more in the future (ex: hull and tessellation).
410 */
411 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
412 unsigned num_sampler_views[PIPE_SHADER_TYPES];
413 const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
414 unsigned num_samplers[PIPE_SHADER_TYPES];
415
416 struct pipe_image_view *images[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_IMAGES];
417 unsigned num_images[PIPE_SHADER_TYPES];
418
419 struct pipe_query_data_pipeline_statistics statistics;
420 boolean collect_statistics;
421
422 float default_outer_tess_level[4];
423 float default_inner_tess_level[2];
424 bool collect_primgen;
425
426 struct draw_assembler *ia;
427
428 void *disk_cache_cookie;
429 void (*disk_cache_find_shader)(void *cookie,
430 struct lp_cached_code *cache,
431 unsigned char ir_sha1_cache_key[20]);
432 void (*disk_cache_insert_shader)(void *cookie,
433 struct lp_cached_code *cache,
434 unsigned char ir_sha1_cache_key[20]);
435
436 void *driver_private;
437 };
438
439
440 struct draw_fetch_info {
441 boolean linear;
442 unsigned start;
443 const unsigned *elts;
444 unsigned count;
445 };
446
447 struct draw_vertex_info {
448 struct vertex_header *verts;
449 unsigned vertex_size;
450 unsigned stride;
451 unsigned count;
452 };
453
454 /* these flags are set if the primitive is a segment of a larger one */
455 #define DRAW_SPLIT_BEFORE 0x1
456 #define DRAW_SPLIT_AFTER 0x2
457 #define DRAW_LINE_LOOP_AS_STRIP 0x4
458
459 struct draw_prim_info {
460 boolean linear;
461 unsigned start;
462
463 const ushort *elts;
464 unsigned count;
465
466 enum pipe_prim_type prim;
467 unsigned flags;
468 unsigned *primitive_lengths;
469 unsigned primitive_count;
470 };
471
472
473 /*******************************************************************************
474 * Draw common initialization code
475 */
476 boolean draw_init(struct draw_context *draw);
477 void draw_new_instance(struct draw_context *draw);
478
479 /*******************************************************************************
480 * Vertex shader code:
481 */
482 boolean draw_vs_init(struct draw_context *draw);
483 void draw_vs_destroy(struct draw_context *draw);
484
485
486 /*******************************************************************************
487 * Geometry shading code:
488 */
489 boolean draw_gs_init(struct draw_context *draw);
490
491
492 void draw_gs_destroy(struct draw_context *draw);
493
494 /*******************************************************************************
495 * Common shading code:
496 */
497 uint draw_current_shader_outputs(const struct draw_context *draw);
498 uint draw_current_shader_position_output(const struct draw_context *draw);
499 uint draw_current_shader_viewport_index_output(const struct draw_context *draw);
500 uint draw_current_shader_clipvertex_output(const struct draw_context *draw);
501 uint draw_current_shader_ccdistance_output(const struct draw_context *draw, int index);
502 uint draw_current_shader_num_written_clipdistances(const struct draw_context *draw);
503 uint draw_current_shader_num_written_culldistances(const struct draw_context *draw);
504 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
505 uint semantic_name, uint semantic_index);
506 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
507 boolean draw_current_shader_uses_viewport_index(
508 const struct draw_context *draw);
509
510
511 /*******************************************************************************
512 * Vertex processing (was passthrough) code:
513 */
514 boolean draw_pt_init(struct draw_context *draw);
515 void draw_pt_destroy(struct draw_context *draw);
516 void draw_pt_reset_vertex_ids(struct draw_context *draw);
517 void draw_pt_flush(struct draw_context *draw, unsigned flags);
518
519
520 /*******************************************************************************
521 * Primitive processing (pipeline) code:
522 */
523
524 boolean draw_pipeline_init(struct draw_context *draw);
525 void draw_pipeline_destroy(struct draw_context *draw);
526
527 /*
528 * These flags are used by the pipeline when unfilled and/or line stipple modes
529 * are operational.
530 */
531 #define DRAW_PIPE_EDGE_FLAG_0 0x1
532 #define DRAW_PIPE_EDGE_FLAG_1 0x2
533 #define DRAW_PIPE_EDGE_FLAG_2 0x4
534 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
535 #define DRAW_PIPE_RESET_STIPPLE 0x8
536
537 void
538 draw_pipeline_run(struct draw_context *draw,
539 const struct draw_vertex_info *vert,
540 const struct draw_prim_info *prim);
541
542 void
543 draw_pipeline_run_linear(struct draw_context *draw,
544 const struct draw_vertex_info *vert,
545 const struct draw_prim_info *prim);
546
547 void
548 draw_pipeline_flush(struct draw_context *draw,
549 unsigned flags);
550
551
552 /*
553 * Flushing
554 */
555
556 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1 /**< Constants, viewport, etc */
557 #define DRAW_FLUSH_STATE_CHANGE 0x2 /**< Other/heavy state changes */
558 #define DRAW_FLUSH_BACKEND 0x4 /**< Flush the output buffer */
559
560
561 void
562 draw_do_flush(struct draw_context *draw, unsigned flags);
563
564 void *
565 draw_get_rasterizer_no_cull(struct draw_context *draw,
566 const struct pipe_rasterizer_state *rast);
567
568 void
569 draw_stats_clipper_primitives(struct draw_context *draw,
570 const struct draw_prim_info *prim_info);
571
572 void
573 draw_update_clip_flags(struct draw_context *draw);
574
575 void
576 draw_update_viewport_flags(struct draw_context *draw);
577
578
579 /**
580 * Return index i from the index buffer.
581 * If the index buffer would overflow we return index 0.
582 */
583 #define DRAW_GET_IDX(_elts, _i) \
584 (((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])
585
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 /**
599 * Adds two unsigned integers and if the addition
600 * overflows then it returns the value from
601 * the overflow_value variable.
602 */
603 static inline unsigned
draw_overflow_uadd(unsigned a,unsigned b,unsigned overflow_value)604 draw_overflow_uadd(unsigned a, unsigned b,
605 unsigned overflow_value)
606 {
607 unsigned res = a + b;
608 if (res < a) {
609 res = overflow_value;
610 }
611 return res;
612 }
613
614 #endif /* DRAW_PRIVATE_H */
615