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