draw: fix line stippling with unfilled prims
[mesa.git] / src / gallium / auxiliary / draw / draw_private.h
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 HAVE_LLVM
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 struct pipe_context;
63 struct draw_vertex_shader;
64 struct draw_context;
65 struct draw_stage;
66 struct vbuf_render;
67 struct tgsi_exec_machine;
68 struct tgsi_sampler;
69 struct draw_pt_front_end;
70 struct draw_assembler;
71 struct draw_llvm;
72
73
74 /**
75 * Represents the mapped vertex buffer.
76 */
77 struct draw_vertex_buffer {
78 const void *map;
79 uint32_t size;
80 };
81
82 /**
83 * Basic vertex info.
84 * Carry some useful information around with the vertices in the prim pipe.
85 */
86 struct vertex_header {
87 unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
88 unsigned edgeflag:1;
89 unsigned pad:1;
90 unsigned vertex_id:16;
91
92 float 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 boolean bypass_clip_points;
220 } driver;
221
222 boolean quads_always_flatshade_last;
223
224 boolean flushing; /**< debugging/sanity */
225 boolean suspend_flushing; /**< internally set */
226
227 /* Flags set if API requires clipping in these planes and the
228 * driver doesn't indicate that it can do it for us.
229 */
230 boolean clip_xy;
231 boolean clip_z;
232 boolean clip_user;
233 boolean guard_band_xy;
234 boolean guard_band_points_xy;
235
236 boolean force_passthrough; /**< never clip or shade */
237
238 boolean dump_vs;
239
240 /** Depth format and bias related settings. */
241 boolean floating_point_depth;
242 double mrd; /**< minimum resolvable depth value, for polygon offset */
243
244 /** Current rasterizer state given to us by the driver */
245 const struct pipe_rasterizer_state *rasterizer;
246 /** Driver CSO handle for the current rasterizer state */
247 void *rast_handle;
248
249 /** Rasterizer CSOs without culling/stipple/etc */
250 void *rasterizer_no_cull[2][2];
251
252 struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];
253 boolean identity_viewport;
254 boolean bypass_viewport;
255
256 /** Vertex shader state */
257 struct {
258 struct draw_vertex_shader *vertex_shader;
259 uint num_vs_outputs; /**< convenience, from vertex_shader */
260 uint position_output;
261 uint edgeflag_output;
262 uint clipvertex_output;
263 uint clipdistance_output[2];
264
265 /** Fields for TGSI interpreter / execution */
266 struct {
267 struct tgsi_exec_machine *machine;
268
269 struct tgsi_sampler *sampler;
270 } tgsi;
271
272 struct translate *fetch;
273 struct translate_cache *fetch_cache;
274 struct translate *emit;
275 struct translate_cache *emit_cache;
276 } vs;
277
278 /** Geometry shader state */
279 struct {
280 struct draw_geometry_shader *geometry_shader;
281 uint num_gs_outputs; /**< convenience, from geometry_shader */
282 uint position_output;
283
284 /** Fields for TGSI interpreter / execution */
285 struct {
286 struct tgsi_exec_machine *machine;
287
288 struct tgsi_sampler *sampler;
289 } tgsi;
290
291 } gs;
292
293 /** Fragment shader state */
294 struct {
295 struct draw_fragment_shader *fragment_shader;
296 } fs;
297
298 /** Stream output (vertex feedback) state */
299 struct {
300 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
301 uint num_targets;
302 } so;
303
304 /* Clip derived state:
305 */
306 float plane[DRAW_TOTAL_CLIP_PLANES][4];
307
308 /* If a prim stage introduces new vertex attributes, they'll be stored here
309 */
310 struct {
311 uint num;
312 uint semantic_name[10];
313 uint semantic_index[10];
314 uint slot[10];
315 } extra_shader_outputs;
316
317 unsigned instance_id;
318 unsigned start_instance;
319 unsigned start_index;
320
321 struct draw_llvm *llvm;
322
323 /** Texture sampler and sampler view state.
324 * Note that we have arrays indexed by shader type. At this time
325 * we only handle vertex and geometry shaders in the draw module, but
326 * there may be more in the future (ex: hull and tessellation).
327 */
328 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
329 unsigned num_sampler_views[PIPE_SHADER_TYPES];
330 const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
331 unsigned num_samplers[PIPE_SHADER_TYPES];
332
333 struct pipe_query_data_pipeline_statistics statistics;
334 boolean collect_statistics;
335
336 struct draw_assembler *ia;
337
338 void *driver_private;
339 };
340
341
342 struct draw_fetch_info {
343 boolean linear;
344 unsigned start;
345 const unsigned *elts;
346 unsigned count;
347 };
348
349 struct draw_vertex_info {
350 struct vertex_header *verts;
351 unsigned vertex_size;
352 unsigned stride;
353 unsigned count;
354 };
355
356 /* these flags are set if the primitive is a segment of a larger one */
357 #define DRAW_SPLIT_BEFORE 0x1
358 #define DRAW_SPLIT_AFTER 0x2
359 #define DRAW_LINE_LOOP_AS_STRIP 0x4
360
361 struct draw_prim_info {
362 boolean linear;
363 unsigned start;
364
365 const ushort *elts;
366 unsigned count;
367
368 unsigned prim;
369 unsigned flags;
370 unsigned *primitive_lengths;
371 unsigned primitive_count;
372 };
373
374
375 /*******************************************************************************
376 * Draw common initialization code
377 */
378 boolean draw_init(struct draw_context *draw);
379 void draw_new_instance(struct draw_context *draw);
380
381 /*******************************************************************************
382 * Vertex shader code:
383 */
384 boolean draw_vs_init( struct draw_context *draw );
385 void draw_vs_destroy( struct draw_context *draw );
386
387
388 /*******************************************************************************
389 * Geometry shading code:
390 */
391 boolean draw_gs_init( struct draw_context *draw );
392
393
394 void draw_gs_destroy( struct draw_context *draw );
395
396 /*******************************************************************************
397 * Common shading code:
398 */
399 uint draw_current_shader_outputs(const struct draw_context *draw);
400 uint draw_current_shader_position_output(const struct draw_context *draw);
401 uint draw_current_shader_viewport_index_output(const struct draw_context *draw);
402 uint draw_current_shader_clipvertex_output(const struct draw_context *draw);
403 uint draw_current_shader_clipdistance_output(const struct draw_context *draw, int index);
404 uint draw_current_shader_num_written_clipdistances(const struct draw_context *draw);
405 uint draw_current_shader_culldistance_output(const struct draw_context *draw, int index);
406 uint draw_current_shader_num_written_culldistances(const struct draw_context *draw);
407 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
408 uint semantic_name, uint semantic_index);
409 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
410 boolean draw_current_shader_uses_viewport_index(
411 const struct draw_context *draw);
412
413
414 /*******************************************************************************
415 * Vertex processing (was passthrough) code:
416 */
417 boolean draw_pt_init( struct draw_context *draw );
418 void draw_pt_destroy( struct draw_context *draw );
419 void draw_pt_reset_vertex_ids( struct draw_context *draw );
420 void draw_pt_flush( struct draw_context *draw, unsigned flags );
421
422
423 /*******************************************************************************
424 * Primitive processing (pipeline) code:
425 */
426
427 boolean draw_pipeline_init( struct draw_context *draw );
428 void draw_pipeline_destroy( struct draw_context *draw );
429
430
431
432
433
434 /*
435 * These flags are used by the pipeline when unfilled and/or line stipple modes
436 * are operational.
437 */
438 #define DRAW_PIPE_EDGE_FLAG_0 0x1
439 #define DRAW_PIPE_EDGE_FLAG_1 0x2
440 #define DRAW_PIPE_EDGE_FLAG_2 0x4
441 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
442 #define DRAW_PIPE_RESET_STIPPLE 0x8
443
444 void draw_pipeline_run( struct draw_context *draw,
445 const struct draw_vertex_info *vert,
446 const struct draw_prim_info *prim);
447
448 void draw_pipeline_run_linear( struct draw_context *draw,
449 const struct draw_vertex_info *vert,
450 const struct draw_prim_info *prim);
451
452
453
454
455 void draw_pipeline_flush( struct draw_context *draw,
456 unsigned flags );
457
458
459
460 /*******************************************************************************
461 * Flushing
462 */
463
464 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1 /**< Constants, viewport, etc */
465 #define DRAW_FLUSH_STATE_CHANGE 0x2 /**< Other/heavy state changes */
466 #define DRAW_FLUSH_BACKEND 0x4 /**< Flush the output buffer */
467
468
469 void draw_do_flush( struct draw_context *draw, unsigned flags );
470
471
472
473 void *
474 draw_get_rasterizer_no_cull( struct draw_context *draw,
475 boolean scissor,
476 boolean flatshade );
477
478 void
479 draw_stats_clipper_primitives(struct draw_context *draw,
480 const struct draw_prim_info *prim_info);
481
482 void draw_update_clip_flags(struct draw_context *draw);
483 void draw_update_viewport_flags(struct draw_context *draw);
484
485 /**
486 * Return index i from the index buffer.
487 * If the index buffer would overflow we return the
488 * maximum possible index.
489 */
490 #define DRAW_GET_IDX(_elts, _i) \
491 (((_i) >= draw->pt.user.eltMax) ? DRAW_MAX_FETCH_IDX : (_elts)[_i])
492
493 /**
494 * Return index of the given viewport clamping it
495 * to be between 0 <= and < PIPE_MAX_VIEWPORTS
496 */
497 static inline unsigned
498 draw_clamp_viewport_idx(int idx)
499 {
500 return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0);
501 }
502
503 /**
504 * Adds two unsigned integers and if the addition
505 * overflows then it returns the value from
506 * the overflow_value variable.
507 */
508 static inline unsigned
509 draw_overflow_uadd(unsigned a, unsigned b,
510 unsigned overflow_value)
511 {
512 unsigned res = a + b;
513 if (res < a || res < b) {
514 res = overflow_value;
515 }
516 return res;
517 }
518
519 #endif /* DRAW_PRIVATE_H */