1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 **************************************************************************/
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
31 #include "util/u_memory.h"
32 #include "util/u_math.h"
33 #include "pipe/p_defines.h"
34 #include "draw_private.h"
35 #include "draw_pipe.h"
36 #include "draw_context.h"
37 #include "draw_vbuf.h"
39 static boolean points( unsigned prim )
41 return (prim == PIPE_PRIM_POINTS);
44 static boolean lines( unsigned prim )
46 return (prim == PIPE_PRIM_LINES ||
47 prim == PIPE_PRIM_LINE_STRIP ||
48 prim == PIPE_PRIM_LINE_LOOP);
51 static boolean triangles( unsigned prim )
53 return prim >= PIPE_PRIM_TRIANGLES;
57 * Default version of a function to check if we need any special
58 * pipeline stages, or whether prims/verts can go through untouched.
59 * Don't test for bypass clipping or vs modes, this function is just
60 * about the primitive pipeline stages.
62 * This can be overridden by the driver.
65 draw_need_pipeline(const struct draw_context *draw,
66 const struct pipe_rasterizer_state *rasterizer,
69 /* If the driver has overridden this, use that version:
72 draw->render->need_pipeline)
74 return draw->render->need_pipeline( draw->render,
79 /* Don't have to worry about triangles turning into lines/points
80 * and triggering the pipeline, because we have to trigger the
81 * pipeline *anyway* if unfilled mode is active.
86 if (rasterizer->line_stipple_enable && draw->pipeline.line_stipple)
90 if (roundf(rasterizer->line_width) > draw->pipeline.wide_line_threshold)
94 if (rasterizer->line_smooth && draw->pipeline.aaline)
97 if (draw_current_shader_num_written_culldistances(draw))
104 if (rasterizer->point_size > draw->pipeline.wide_point_threshold)
108 if (rasterizer->point_quad_rasterization
109 && draw->pipeline.wide_point_sprites)
113 if (rasterizer->point_smooth && draw->pipeline.aapoint)
117 if (rasterizer->sprite_coord_enable && draw->pipeline.point_sprite)
124 /* polygon stipple */
125 if (rasterizer->poly_stipple_enable && draw->pipeline.pstipple)
128 /* unfilled polygons */
129 if (rasterizer->fill_front != PIPE_POLYGON_MODE_FILL ||
130 rasterizer->fill_back != PIPE_POLYGON_MODE_FILL)
134 if (rasterizer->offset_point ||
135 rasterizer->offset_line ||
136 rasterizer->offset_tri)
139 /* two-side lighting */
140 if (rasterizer->light_twoside)
143 if (draw_current_shader_num_written_culldistances(draw))
147 /* polygon cull - this is difficult - hardware can cull just fine
148 * most of the time (though sometimes CULL_NEITHER is unsupported.
150 * Generally this isn't a reason to require the pipeline, though.
152 if (rasterizer->cull_mode)
162 * Rebuild the rendering pipeline.
164 static struct draw_stage *validate_pipeline( struct draw_stage *stage )
166 struct draw_context *draw = stage->draw;
167 struct draw_stage *next = draw->pipeline.rasterize;
168 boolean need_det = FALSE;
169 boolean precalc_flat = FALSE;
170 boolean wide_lines, wide_points;
171 const struct pipe_rasterizer_state *rast = draw->rasterizer;
173 /* Set the validate's next stage to the rasterize stage, so that it
174 * can be found later if needed for flushing.
178 /* drawing wide lines? */
179 wide_lines = (roundf(rast->line_width) > draw->pipeline.wide_line_threshold
180 && !rast->line_smooth);
182 /* drawing large/sprite points (but not AA points)? */
183 if (rast->sprite_coord_enable && draw->pipeline.point_sprite)
185 else if (rast->point_smooth && draw->pipeline.aapoint)
187 else if (rast->point_size > draw->pipeline.wide_point_threshold)
189 else if (rast->point_quad_rasterization && draw->pipeline.wide_point_sprites)
195 * NOTE: we build up the pipeline in end-to-start order.
197 * TODO: make the current primitive part of the state and build
198 * shorter pipelines for lines & points.
201 if (rast->line_smooth && draw->pipeline.aaline) {
202 draw->pipeline.aaline->next = next;
203 next = draw->pipeline.aaline;
207 if (rast->point_smooth && draw->pipeline.aapoint) {
208 draw->pipeline.aapoint->next = next;
209 next = draw->pipeline.aapoint;
213 draw->pipeline.wide_line->next = next;
214 next = draw->pipeline.wide_line;
219 draw->pipeline.wide_point->next = next;
220 next = draw->pipeline.wide_point;
223 if (rast->line_stipple_enable && draw->pipeline.line_stipple) {
224 draw->pipeline.stipple->next = next;
225 next = draw->pipeline.stipple;
226 precalc_flat = TRUE; /* only needed for lines really */
229 if (rast->poly_stipple_enable
230 && draw->pipeline.pstipple) {
231 draw->pipeline.pstipple->next = next;
232 next = draw->pipeline.pstipple;
235 if (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
236 rast->fill_back != PIPE_POLYGON_MODE_FILL) {
237 draw->pipeline.unfilled->next = next;
238 next = draw->pipeline.unfilled;
239 precalc_flat = TRUE; /* only needed for triangles really */
243 if (rast->flatshade && precalc_flat) {
244 draw->pipeline.flatshade->next = next;
245 next = draw->pipeline.flatshade;
248 if (rast->offset_point ||
251 draw->pipeline.offset->next = next;
252 next = draw->pipeline.offset;
256 if (rast->light_twoside) {
257 draw->pipeline.twoside->next = next;
258 next = draw->pipeline.twoside;
262 /* Always run the cull stage as we calculate determinant there
265 * This can actually be a win as culling out the triangles can lead
266 * to less work emitting vertices, smaller vertex buffers, etc.
267 * It's difficult to say whether this will be true in general.
269 if (need_det || rast->cull_face != PIPE_FACE_NONE ||
270 draw_current_shader_num_written_culldistances(draw)) {
271 draw->pipeline.cull->next = next;
272 next = draw->pipeline.cull;
277 if (draw->clip_xy || draw->clip_z || draw->clip_user)
279 draw->pipeline.clip->next = next;
280 next = draw->pipeline.clip;
284 draw->pipeline.first = next;
287 debug_printf("draw pipeline:\n");
288 for (next = draw->pipeline.first; next ; next = next->next )
289 debug_printf(" %s\n", next->name);
293 return draw->pipeline.first;
296 static void validate_tri( struct draw_stage *stage,
297 struct prim_header *header )
299 struct draw_stage *pipeline = validate_pipeline( stage );
300 pipeline->tri( pipeline, header );
303 static void validate_line( struct draw_stage *stage,
304 struct prim_header *header )
306 struct draw_stage *pipeline = validate_pipeline( stage );
307 pipeline->line( pipeline, header );
310 static void validate_point( struct draw_stage *stage,
311 struct prim_header *header )
313 struct draw_stage *pipeline = validate_pipeline( stage );
314 pipeline->point( pipeline, header );
317 static void validate_reset_stipple_counter( struct draw_stage *stage )
319 struct draw_stage *pipeline = validate_pipeline( stage );
320 pipeline->reset_stipple_counter( pipeline );
323 static void validate_flush( struct draw_stage *stage,
326 /* May need to pass a backend flush on to the rasterize stage.
329 stage->next->flush( stage->next, flags );
333 static void validate_destroy( struct draw_stage *stage )
340 * Create validate pipeline stage.
342 struct draw_stage *draw_validate_stage( struct draw_context *draw )
344 struct draw_stage *stage = CALLOC_STRUCT(draw_stage);
349 stage->name = "validate";
351 stage->point = validate_point;
352 stage->line = validate_line;
353 stage->tri = validate_tri;
354 stage->flush = validate_flush;
355 stage->reset_stipple_counter = validate_reset_stipple_counter;
356 stage->destroy = validate_destroy;