1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Private data structures, etc for the draw module.
35 * Keith Whitwell <keithw@vmware.com>
40 #ifndef DRAW_PRIVATE_H
41 #define DRAW_PRIVATE_H
44 #include "pipe/p_state.h"
45 #include "pipe/p_defines.h"
47 #include "tgsi/tgsi_scan.h"
54 /** Sum of frustum planes and user-defined planes */
55 #define DRAW_TOTAL_CLIP_PLANES (6 + PIPE_MAX_CLIP_PLANES)
58 * The largest possible index of a vertex that can be fetched.
60 #define DRAW_MAX_FETCH_IDX 0xffffffff
63 struct draw_vertex_shader
;
67 struct tgsi_exec_machine
;
71 struct draw_pt_front_end
;
72 struct draw_assembler
;
77 * Represents the mapped vertex buffer.
79 struct draw_vertex_buffer
{
86 * Carry some useful information around with the vertices in the prim pipe.
88 struct vertex_header
{
89 unsigned clipmask
:DRAW_TOTAL_CLIP_PLANES
;
92 unsigned vertex_id
:16;
96 /* This will probably become float (*data)[4] soon:
101 /* NOTE: It should match vertex_id size above */
102 #define UNDEFINED_VERTEX_ID 0xffff
105 /* maximum number of shader variants we can cache */
106 #define DRAW_MAX_SHADER_VARIANTS 512
109 * Private context for the drawing module.
113 struct pipe_context
*pipe
;
115 /** Drawing/primitive pipeline stages */
117 struct draw_stage
*first
; /**< one of the following */
119 struct draw_stage
*validate
;
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
;
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? */
143 /* Temporary storage while the pipeline is being run:
146 unsigned vertex_stride
;
147 unsigned vertex_count
;
151 struct vbuf_render
*render
;
153 /* Support prototype passthrough path:
156 /* Current active frontend */
157 struct draw_pt_front_end
*frontend
;
159 unsigned opt
; /**< bitmask of PT_x flags */
160 unsigned eltSize
; /* saved eltSize for flushing */
161 ubyte vertices_per_patch
;
162 boolean rebind_parameters
;
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
;
172 struct draw_pt_front_end
*vsplit
;
175 struct pipe_vertex_buffer vertex_buffer
[PIPE_MAX_ATTRIBS
];
176 unsigned nr_vertex_buffers
;
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.
186 struct pipe_vertex_element vertex_element
[PIPE_MAX_ATTRIBS
];
187 unsigned nr_vertex_elements
;
189 /* user-space vertex data, buffers */
191 /** vertex element/index buffer (ex: glDrawElements) */
193 /** bytes per index (0, 1, 2 or 4) */
203 struct draw_vertex_buffer vbuffer
[PIPE_MAX_ATTRIBS
];
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
];
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
];
225 /* pointer to planes */
226 float (*planes
)[DRAW_TOTAL_CLIP_PLANES
][4];
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 */
234 boolean bypass_clip_xy
;
235 boolean bypass_clip_z
;
236 boolean guard_band_xy
;
237 boolean bypass_clip_points
;
240 boolean quads_always_flatshade_last
;
242 boolean flushing
; /**< debugging/sanity */
243 boolean suspend_flushing
; /**< internally set */
245 /* Flags set if API requires clipping in these planes and the
246 * driver doesn't indicate that it can do it for us.
251 boolean guard_band_xy
;
252 boolean guard_band_points_xy
;
254 boolean force_passthrough
; /**< never clip or shade */
258 /** Depth format and bias related settings. */
259 boolean floating_point_depth
;
260 double mrd
; /**< minimum resolvable depth value, for polygon offset */
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 */
267 /** Rasterizer CSOs without culling/stipple/etc */
268 void *rasterizer_no_cull
[2][2];
270 struct pipe_viewport_state viewports
[PIPE_MAX_VIEWPORTS
];
271 boolean identity_viewport
;
272 boolean bypass_viewport
;
274 /** Vertex shader state */
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];
283 /** Fields for TGSI interpreter / execution */
285 struct tgsi_exec_machine
*machine
;
287 struct tgsi_sampler
*sampler
;
288 struct tgsi_image
*image
;
289 struct tgsi_buffer
*buffer
;
292 struct translate
*fetch
;
293 struct translate_cache
*fetch_cache
;
294 struct translate
*emit
;
295 struct translate_cache
*emit_cache
;
298 /** Geometry shader state */
300 struct draw_geometry_shader
*geometry_shader
;
301 uint num_gs_outputs
; /**< convenience, from geometry_shader */
302 uint position_output
;
304 /** Fields for TGSI interpreter / execution */
306 struct tgsi_exec_machine
*machine
;
308 struct tgsi_sampler
*sampler
;
309 struct tgsi_image
*image
;
310 struct tgsi_buffer
*buffer
;
315 /* Tessellation state */
317 struct draw_tess_ctrl_shader
*tess_ctrl_shader
;
319 /** Fields for TGSI interpreter / execution */
321 struct tgsi_exec_machine
*machine
;
323 struct tgsi_sampler
*sampler
;
324 struct tgsi_image
*image
;
325 struct tgsi_buffer
*buffer
;
330 struct draw_tess_eval_shader
*tess_eval_shader
;
331 uint position_output
;
333 /** Fields for TGSI interpreter / execution */
335 struct tgsi_exec_machine
*machine
;
337 struct tgsi_sampler
*sampler
;
338 struct tgsi_image
*image
;
339 struct tgsi_buffer
*buffer
;
343 /** Fragment shader state */
345 struct draw_fragment_shader
*fragment_shader
;
348 /** Stream output (vertex feedback) state */
350 struct draw_so_target
*targets
[PIPE_MAX_SO_BUFFERS
];
354 /* Clip derived state:
356 float plane
[DRAW_TOTAL_CLIP_PLANES
][4];
358 /* If a prim stage introduces new vertex attributes, they'll be stored here
362 uint semantic_name
[10];
363 uint semantic_index
[10];
365 } extra_shader_outputs
;
367 unsigned instance_id
;
368 unsigned start_instance
;
369 unsigned start_index
;
371 struct draw_llvm
*llvm
;
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).
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
];
383 struct pipe_image_view
*images
[PIPE_SHADER_TYPES
][PIPE_MAX_SHADER_IMAGES
];
384 unsigned num_images
[PIPE_SHADER_TYPES
];
386 struct pipe_query_data_pipeline_statistics statistics
;
387 boolean collect_statistics
;
389 float default_outer_tess_level
[4];
390 float default_inner_tess_level
[2];
391 bool collect_primgen
;
393 struct draw_assembler
*ia
;
395 void *driver_private
;
399 struct draw_fetch_info
{
402 const unsigned *elts
;
406 struct draw_vertex_info
{
407 struct vertex_header
*verts
;
408 unsigned vertex_size
;
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
418 struct draw_prim_info
{
427 unsigned *primitive_lengths
;
428 unsigned primitive_count
;
432 /*******************************************************************************
433 * Draw common initialization code
435 boolean
draw_init(struct draw_context
*draw
);
436 void draw_new_instance(struct draw_context
*draw
);
438 /*******************************************************************************
439 * Vertex shader code:
441 boolean
draw_vs_init( struct draw_context
*draw
);
442 void draw_vs_destroy( struct draw_context
*draw
);
445 /*******************************************************************************
446 * Geometry shading code:
448 boolean
draw_gs_init( struct draw_context
*draw
);
451 void draw_gs_destroy( struct draw_context
*draw
);
453 /*******************************************************************************
454 * Common shading code:
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
);
470 /*******************************************************************************
471 * Vertex processing (was passthrough) code:
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
);
479 /*******************************************************************************
480 * Primitive processing (pipeline) code:
483 boolean
draw_pipeline_init( struct draw_context
*draw
);
484 void draw_pipeline_destroy( struct draw_context
*draw
);
491 * These flags are used by the pipeline when unfilled and/or line stipple modes
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
500 void draw_pipeline_run( struct draw_context
*draw
,
501 const struct draw_vertex_info
*vert
,
502 const struct draw_prim_info
*prim
);
504 void draw_pipeline_run_linear( struct draw_context
*draw
,
505 const struct draw_vertex_info
*vert
,
506 const struct draw_prim_info
*prim
);
511 void draw_pipeline_flush( struct draw_context
*draw
,
516 /*******************************************************************************
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 */
525 void draw_do_flush( struct draw_context
*draw
, unsigned flags
);
530 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
535 draw_stats_clipper_primitives(struct draw_context
*draw
,
536 const struct draw_prim_info
*prim_info
);
538 void draw_update_clip_flags(struct draw_context
*draw
);
539 void draw_update_viewport_flags(struct draw_context
*draw
);
542 * Return index i from the index buffer.
543 * If the index buffer would overflow we return index 0.
545 #define DRAW_GET_IDX(_elts, _i) \
546 (((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])
549 * Return index of the given viewport clamping it
550 * to be between 0 <= and < PIPE_MAX_VIEWPORTS
552 static inline unsigned
553 draw_clamp_viewport_idx(int idx
)
555 return ((PIPE_MAX_VIEWPORTS
> idx
&& idx
>= 0) ? idx
: 0);
559 * Adds two unsigned integers and if the addition
560 * overflows then it returns the value from
561 * the overflow_value variable.
563 static inline unsigned
564 draw_overflow_uadd(unsigned a
, unsigned b
,
565 unsigned overflow_value
)
567 unsigned res
= a
+ b
;
569 res
= overflow_value
;
574 #endif /* DRAW_PRIVATE_H */