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
;
69 struct draw_pt_front_end
;
70 struct draw_assembler
;
75 * Represents the mapped vertex buffer.
77 struct draw_vertex_buffer
{
84 * Carry some useful information around with the vertices in the prim pipe.
86 struct vertex_header
{
87 unsigned clipmask
:DRAW_TOTAL_CLIP_PLANES
;
90 unsigned vertex_id
:16;
93 float pre_clip_pos
[4];
95 /* This will probably become float (*data)[4] soon:
100 /* NOTE: It should match vertex_id size above */
101 #define UNDEFINED_VERTEX_ID 0xffff
104 /* maximum number of shader variants we can cache */
105 #define DRAW_MAX_SHADER_VARIANTS 128
108 * Private context for the drawing module.
112 struct pipe_context
*pipe
;
114 /** Drawing/primitive pipeline stages */
116 struct draw_stage
*first
; /**< one of the following */
118 struct draw_stage
*validate
;
120 /* stages (in logical order) */
121 struct draw_stage
*flatshade
;
122 struct draw_stage
*clip
;
123 struct draw_stage
*cull
;
124 struct draw_stage
*twoside
;
125 struct draw_stage
*offset
;
126 struct draw_stage
*unfilled
;
127 struct draw_stage
*stipple
;
128 struct draw_stage
*aapoint
;
129 struct draw_stage
*aaline
;
130 struct draw_stage
*pstipple
;
131 struct draw_stage
*wide_line
;
132 struct draw_stage
*wide_point
;
133 struct draw_stage
*rasterize
;
135 float wide_point_threshold
; /**< convert pnts to tris if larger than this */
136 float wide_line_threshold
; /**< convert lines to tris if wider than this */
137 boolean wide_point_sprites
; /**< convert points to tris for sprite mode */
138 boolean line_stipple
; /**< do line stipple? */
139 boolean point_sprite
; /**< convert points to quads for sprites? */
141 /* Temporary storage while the pipeline is being run:
144 unsigned vertex_stride
;
145 unsigned vertex_count
;
149 struct vbuf_render
*render
;
151 /* Support prototype passthrough path:
154 /* Current active frontend */
155 struct draw_pt_front_end
*frontend
;
157 unsigned opt
; /**< bitmask of PT_x flags */
158 unsigned eltSize
; /* saved eltSize for flushing */
160 boolean rebind_parameters
;
163 struct draw_pt_middle_end
*fetch_emit
;
164 struct draw_pt_middle_end
*fetch_shade_emit
;
165 struct draw_pt_middle_end
*general
;
166 struct draw_pt_middle_end
*llvm
;
170 struct draw_pt_front_end
*vsplit
;
173 struct pipe_vertex_buffer vertex_buffer
[PIPE_MAX_ATTRIBS
];
174 unsigned nr_vertex_buffers
;
177 * This is the largest legal index value for the current set of
178 * bound vertex buffers. Regardless of any other consideration,
179 * all vertex lookups need to be clamped to 0..max_index to
180 * prevent out-of-bound access.
184 struct pipe_vertex_element vertex_element
[PIPE_MAX_ATTRIBS
];
185 unsigned nr_vertex_elements
;
187 /* user-space vertex data, buffers */
189 /** vertex element/index buffer (ex: glDrawElements) */
191 /** bytes per index (0, 1, 2 or 4) */
200 struct draw_vertex_buffer vbuffer
[PIPE_MAX_ATTRIBS
];
202 /** constant buffers (for vertex/geometry shader) */
203 const void *vs_constants
[PIPE_MAX_CONSTANT_BUFFERS
];
204 unsigned vs_constants_size
[PIPE_MAX_CONSTANT_BUFFERS
];
205 const void *gs_constants
[PIPE_MAX_CONSTANT_BUFFERS
];
206 unsigned gs_constants_size
[PIPE_MAX_CONSTANT_BUFFERS
];
208 /* pointer to planes */
209 float (*planes
)[DRAW_TOTAL_CLIP_PLANES
][4];
212 boolean test_fse
; /* enable FSE even though its not correct (eg for softpipe) */
213 boolean no_fse
; /* disable FSE even when it is correct */
217 boolean bypass_clip_xy
;
218 boolean bypass_clip_z
;
219 boolean guard_band_xy
;
220 boolean bypass_clip_points
;
223 boolean quads_always_flatshade_last
;
225 boolean flushing
; /**< debugging/sanity */
226 boolean suspend_flushing
; /**< internally set */
228 /* Flags set if API requires clipping in these planes and the
229 * driver doesn't indicate that it can do it for us.
234 boolean guard_band_xy
;
235 boolean guard_band_points_xy
;
237 boolean force_passthrough
; /**< never clip or shade */
241 /** Depth format and bias related settings. */
242 boolean floating_point_depth
;
243 double mrd
; /**< minimum resolvable depth value, for polygon offset */
245 /** Current rasterizer state given to us by the driver */
246 const struct pipe_rasterizer_state
*rasterizer
;
247 /** Driver CSO handle for the current rasterizer state */
250 /** Rasterizer CSOs without culling/stipple/etc */
251 void *rasterizer_no_cull
[2][2];
253 struct pipe_viewport_state viewports
[PIPE_MAX_VIEWPORTS
];
254 boolean identity_viewport
;
255 boolean bypass_viewport
;
257 /** Vertex shader state */
259 struct draw_vertex_shader
*vertex_shader
;
260 uint num_vs_outputs
; /**< convenience, from vertex_shader */
261 uint position_output
;
262 uint edgeflag_output
;
263 uint clipvertex_output
;
264 uint clipdistance_output
[2];
266 /** Fields for TGSI interpreter / execution */
268 struct tgsi_exec_machine
*machine
;
270 struct tgsi_sampler
*sampler
;
273 struct translate
*fetch
;
274 struct translate_cache
*fetch_cache
;
275 struct translate
*emit
;
276 struct translate_cache
*emit_cache
;
279 /** Geometry shader state */
281 struct draw_geometry_shader
*geometry_shader
;
282 uint num_gs_outputs
; /**< convenience, from geometry_shader */
283 uint position_output
;
285 /** Fields for TGSI interpreter / execution */
287 struct tgsi_exec_machine
*machine
;
289 struct tgsi_sampler
*sampler
;
294 /** Fragment shader state */
296 struct draw_fragment_shader
*fragment_shader
;
299 /** Stream output (vertex feedback) state */
301 struct draw_so_target
*targets
[PIPE_MAX_SO_BUFFERS
];
305 /* Clip derived state:
307 float plane
[DRAW_TOTAL_CLIP_PLANES
][4];
309 /* If a prim stage introduces new vertex attributes, they'll be stored here
313 uint semantic_name
[10];
314 uint semantic_index
[10];
316 } extra_shader_outputs
;
318 unsigned instance_id
;
319 unsigned start_instance
;
320 unsigned start_index
;
322 struct draw_llvm
*llvm
;
324 /** Texture sampler and sampler view state.
325 * Note that we have arrays indexed by shader type. At this time
326 * we only handle vertex and geometry shaders in the draw module, but
327 * there may be more in the future (ex: hull and tessellation).
329 struct pipe_sampler_view
*sampler_views
[PIPE_SHADER_TYPES
][PIPE_MAX_SHADER_SAMPLER_VIEWS
];
330 unsigned num_sampler_views
[PIPE_SHADER_TYPES
];
331 const struct pipe_sampler_state
*samplers
[PIPE_SHADER_TYPES
][PIPE_MAX_SAMPLERS
];
332 unsigned num_samplers
[PIPE_SHADER_TYPES
];
334 struct pipe_query_data_pipeline_statistics statistics
;
335 boolean collect_statistics
;
337 struct draw_assembler
*ia
;
339 void *driver_private
;
343 struct draw_fetch_info
{
346 const unsigned *elts
;
350 struct draw_vertex_info
{
351 struct vertex_header
*verts
;
352 unsigned vertex_size
;
357 /* these flags are set if the primitive is a segment of a larger one */
358 #define DRAW_SPLIT_BEFORE 0x1
359 #define DRAW_SPLIT_AFTER 0x2
360 #define DRAW_LINE_LOOP_AS_STRIP 0x4
362 struct draw_prim_info
{
371 unsigned *primitive_lengths
;
372 unsigned primitive_count
;
376 /*******************************************************************************
377 * Draw common initialization code
379 boolean
draw_init(struct draw_context
*draw
);
380 void draw_new_instance(struct draw_context
*draw
);
382 /*******************************************************************************
383 * Vertex shader code:
385 boolean
draw_vs_init( struct draw_context
*draw
);
386 void draw_vs_destroy( struct draw_context
*draw
);
389 /*******************************************************************************
390 * Geometry shading code:
392 boolean
draw_gs_init( struct draw_context
*draw
);
395 void draw_gs_destroy( struct draw_context
*draw
);
397 /*******************************************************************************
398 * Common shading code:
400 uint
draw_current_shader_outputs(const struct draw_context
*draw
);
401 uint
draw_current_shader_position_output(const struct draw_context
*draw
);
402 uint
draw_current_shader_viewport_index_output(const struct draw_context
*draw
);
403 uint
draw_current_shader_clipvertex_output(const struct draw_context
*draw
);
404 uint
draw_current_shader_clipdistance_output(const struct draw_context
*draw
, int index
);
405 uint
draw_current_shader_num_written_clipdistances(const struct draw_context
*draw
);
406 uint
draw_current_shader_culldistance_output(const struct draw_context
*draw
, int index
);
407 uint
draw_current_shader_num_written_culldistances(const struct draw_context
*draw
);
408 int draw_alloc_extra_vertex_attrib(struct draw_context
*draw
,
409 uint semantic_name
, uint semantic_index
);
410 void draw_remove_extra_vertex_attribs(struct draw_context
*draw
);
411 boolean
draw_current_shader_uses_viewport_index(
412 const struct draw_context
*draw
);
415 /*******************************************************************************
416 * Vertex processing (was passthrough) code:
418 boolean
draw_pt_init( struct draw_context
*draw
);
419 void draw_pt_destroy( struct draw_context
*draw
);
420 void draw_pt_reset_vertex_ids( struct draw_context
*draw
);
421 void draw_pt_flush( struct draw_context
*draw
, unsigned flags
);
424 /*******************************************************************************
425 * Primitive processing (pipeline) code:
428 boolean
draw_pipeline_init( struct draw_context
*draw
);
429 void draw_pipeline_destroy( struct draw_context
*draw
);
436 * These flags are used by the pipeline when unfilled and/or line stipple modes
439 #define DRAW_PIPE_EDGE_FLAG_0 0x1
440 #define DRAW_PIPE_EDGE_FLAG_1 0x2
441 #define DRAW_PIPE_EDGE_FLAG_2 0x4
442 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
443 #define DRAW_PIPE_RESET_STIPPLE 0x8
445 void draw_pipeline_run( struct draw_context
*draw
,
446 const struct draw_vertex_info
*vert
,
447 const struct draw_prim_info
*prim
);
449 void draw_pipeline_run_linear( struct draw_context
*draw
,
450 const struct draw_vertex_info
*vert
,
451 const struct draw_prim_info
*prim
);
456 void draw_pipeline_flush( struct draw_context
*draw
,
461 /*******************************************************************************
465 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1 /**< Constants, viewport, etc */
466 #define DRAW_FLUSH_STATE_CHANGE 0x2 /**< Other/heavy state changes */
467 #define DRAW_FLUSH_BACKEND 0x4 /**< Flush the output buffer */
470 void draw_do_flush( struct draw_context
*draw
, unsigned flags
);
475 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
480 draw_stats_clipper_primitives(struct draw_context
*draw
,
481 const struct draw_prim_info
*prim_info
);
483 void draw_update_clip_flags(struct draw_context
*draw
);
484 void draw_update_viewport_flags(struct draw_context
*draw
);
487 * Return index i from the index buffer.
488 * If the index buffer would overflow we return the
489 * maximum possible index.
491 #define DRAW_GET_IDX(_elts, _i) \
492 (((_i) >= draw->pt.user.eltMax) ? DRAW_MAX_FETCH_IDX : (_elts)[_i])
495 * Return index of the given viewport clamping it
496 * to be between 0 <= and < PIPE_MAX_VIEWPORTS
498 static inline unsigned
499 draw_clamp_viewport_idx(int idx
)
501 return ((PIPE_MAX_VIEWPORTS
> idx
&& idx
>= 0) ? idx
: 0);
505 * Adds two unsigned integers and if the addition
506 * overflows then it returns the value from
507 * the overflow_value variable.
509 static inline unsigned
510 draw_overflow_uadd(unsigned a
, unsigned b
,
511 unsigned overflow_value
)
513 unsigned res
= a
+ b
;
514 if (res
< a
|| res
< b
) {
515 res
= overflow_value
;
520 #endif /* DRAW_PRIVATE_H */