Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_validate.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
25 *
26 **************************************************************************/
27
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
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"
38
39 static boolean points( unsigned prim )
40 {
41 return (prim == PIPE_PRIM_POINTS);
42 }
43
44 static boolean lines( unsigned prim )
45 {
46 return (prim == PIPE_PRIM_LINES ||
47 prim == PIPE_PRIM_LINE_STRIP ||
48 prim == PIPE_PRIM_LINE_LOOP);
49 }
50
51 static boolean triangles( unsigned prim )
52 {
53 return prim >= PIPE_PRIM_TRIANGLES;
54 }
55
56 /**
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.
61 *
62 * This can be overridden by the driver.
63 */
64 boolean
65 draw_need_pipeline(const struct draw_context *draw,
66 const struct pipe_rasterizer_state *rasterizer,
67 unsigned int prim )
68 {
69 /* If the driver has overridden this, use that version:
70 */
71 if (draw->render &&
72 draw->render->need_pipeline)
73 {
74 return draw->render->need_pipeline( draw->render,
75 rasterizer,
76 prim );
77 }
78
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.
82 */
83 if (lines(prim))
84 {
85 /* line stipple */
86 if (rasterizer->line_stipple_enable && draw->pipeline.line_stipple)
87 return TRUE;
88
89 /* wide lines */
90 if (roundf(rasterizer->line_width) > draw->pipeline.wide_line_threshold)
91 return TRUE;
92
93 /* AA lines */
94 if (rasterizer->line_smooth && draw->pipeline.aaline)
95 return TRUE;
96 }
97
98 if (points(prim))
99 {
100 /* large points */
101 if (rasterizer->point_size > draw->pipeline.wide_point_threshold)
102 return TRUE;
103
104 /* sprite points */
105 if (rasterizer->point_quad_rasterization
106 && draw->pipeline.wide_point_sprites)
107 return TRUE;
108
109 /* AA points */
110 if (rasterizer->point_smooth && draw->pipeline.aapoint)
111 return TRUE;
112
113 /* point sprites */
114 if (rasterizer->sprite_coord_enable && draw->pipeline.point_sprite)
115 return TRUE;
116 }
117
118
119 if (triangles(prim))
120 {
121 /* polygon stipple */
122 if (rasterizer->poly_stipple_enable && draw->pipeline.pstipple)
123 return TRUE;
124
125 /* unfilled polygons */
126 if (rasterizer->fill_front != PIPE_POLYGON_MODE_FILL ||
127 rasterizer->fill_back != PIPE_POLYGON_MODE_FILL)
128 return TRUE;
129
130 /* polygon offset */
131 if (rasterizer->offset_point ||
132 rasterizer->offset_line ||
133 rasterizer->offset_tri)
134 return TRUE;
135
136 /* two-side lighting */
137 if (rasterizer->light_twoside)
138 return TRUE;
139 }
140
141 /* polygon cull - this is difficult - hardware can cull just fine
142 * most of the time (though sometimes CULL_NEITHER is unsupported.
143 *
144 * Generally this isn't a reason to require the pipeline, though.
145 *
146 if (rasterizer->cull_mode)
147 return TRUE;
148 */
149
150 return FALSE;
151 }
152
153
154
155 /**
156 * Rebuild the rendering pipeline.
157 */
158 static struct draw_stage *validate_pipeline( struct draw_stage *stage )
159 {
160 struct draw_context *draw = stage->draw;
161 struct draw_stage *next = draw->pipeline.rasterize;
162 boolean need_det = FALSE;
163 boolean precalc_flat = FALSE;
164 boolean wide_lines, wide_points;
165 const struct pipe_rasterizer_state *rast = draw->rasterizer;
166
167 /* Set the validate's next stage to the rasterize stage, so that it
168 * can be found later if needed for flushing.
169 */
170 stage->next = next;
171
172 /* drawing wide lines? */
173 wide_lines = (roundf(rast->line_width) > draw->pipeline.wide_line_threshold
174 && !rast->line_smooth);
175
176 /* drawing large/sprite points (but not AA points)? */
177 if (rast->sprite_coord_enable && draw->pipeline.point_sprite)
178 wide_points = TRUE;
179 else if (rast->point_smooth && draw->pipeline.aapoint)
180 wide_points = FALSE;
181 else if (rast->point_size > draw->pipeline.wide_point_threshold)
182 wide_points = TRUE;
183 else if (rast->point_quad_rasterization && draw->pipeline.wide_point_sprites)
184 wide_points = TRUE;
185 else
186 wide_points = FALSE;
187
188 /*
189 * NOTE: we build up the pipeline in end-to-start order.
190 *
191 * TODO: make the current primitive part of the state and build
192 * shorter pipelines for lines & points.
193 */
194
195 if (rast->line_smooth && draw->pipeline.aaline) {
196 draw->pipeline.aaline->next = next;
197 next = draw->pipeline.aaline;
198 }
199
200 if (rast->point_smooth && draw->pipeline.aapoint) {
201 draw->pipeline.aapoint->next = next;
202 next = draw->pipeline.aapoint;
203 }
204
205 if (wide_lines) {
206 draw->pipeline.wide_line->next = next;
207 next = draw->pipeline.wide_line;
208 precalc_flat = TRUE;
209 }
210
211 if (wide_points) {
212 draw->pipeline.wide_point->next = next;
213 next = draw->pipeline.wide_point;
214 }
215
216 if (rast->line_stipple_enable && draw->pipeline.line_stipple) {
217 draw->pipeline.stipple->next = next;
218 next = draw->pipeline.stipple;
219 precalc_flat = TRUE; /* only needed for lines really */
220 }
221
222 if (rast->poly_stipple_enable
223 && draw->pipeline.pstipple) {
224 draw->pipeline.pstipple->next = next;
225 next = draw->pipeline.pstipple;
226 }
227
228 if (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
229 rast->fill_back != PIPE_POLYGON_MODE_FILL) {
230 draw->pipeline.unfilled->next = next;
231 next = draw->pipeline.unfilled;
232 precalc_flat = TRUE; /* only needed for triangles really */
233 need_det = TRUE;
234 }
235
236 if (rast->flatshade && precalc_flat) {
237 draw->pipeline.flatshade->next = next;
238 next = draw->pipeline.flatshade;
239 }
240
241 if (rast->offset_point ||
242 rast->offset_line ||
243 rast->offset_tri) {
244 draw->pipeline.offset->next = next;
245 next = draw->pipeline.offset;
246 need_det = TRUE;
247 }
248
249 if (rast->light_twoside) {
250 draw->pipeline.twoside->next = next;
251 next = draw->pipeline.twoside;
252 need_det = TRUE;
253 }
254
255 /* Always run the cull stage as we calculate determinant there
256 * also.
257 *
258 * This can actually be a win as culling out the triangles can lead
259 * to less work emitting vertices, smaller vertex buffers, etc.
260 * It's difficult to say whether this will be true in general.
261 */
262 if (need_det || rast->cull_face != PIPE_FACE_NONE) {
263 draw->pipeline.cull->next = next;
264 next = draw->pipeline.cull;
265 }
266
267 /* Clip stage
268 */
269 if (draw->clip_xy || draw->clip_z || draw->clip_user)
270 {
271 draw->pipeline.clip->next = next;
272 next = draw->pipeline.clip;
273 }
274
275
276 draw->pipeline.first = next;
277
278 if (0) {
279 debug_printf("draw pipeline:\n");
280 for (next = draw->pipeline.first; next ; next = next->next )
281 debug_printf(" %s\n", next->name);
282 debug_printf("\n");
283 }
284
285 return draw->pipeline.first;
286 }
287
288 static void validate_tri( struct draw_stage *stage,
289 struct prim_header *header )
290 {
291 struct draw_stage *pipeline = validate_pipeline( stage );
292 pipeline->tri( pipeline, header );
293 }
294
295 static void validate_line( struct draw_stage *stage,
296 struct prim_header *header )
297 {
298 struct draw_stage *pipeline = validate_pipeline( stage );
299 pipeline->line( pipeline, header );
300 }
301
302 static void validate_point( struct draw_stage *stage,
303 struct prim_header *header )
304 {
305 struct draw_stage *pipeline = validate_pipeline( stage );
306 pipeline->point( pipeline, header );
307 }
308
309 static void validate_reset_stipple_counter( struct draw_stage *stage )
310 {
311 struct draw_stage *pipeline = validate_pipeline( stage );
312 pipeline->reset_stipple_counter( pipeline );
313 }
314
315 static void validate_flush( struct draw_stage *stage,
316 unsigned flags )
317 {
318 /* May need to pass a backend flush on to the rasterize stage.
319 */
320 if (stage->next)
321 stage->next->flush( stage->next, flags );
322 }
323
324
325 static void validate_destroy( struct draw_stage *stage )
326 {
327 FREE( stage );
328 }
329
330
331 /**
332 * Create validate pipeline stage.
333 */
334 struct draw_stage *draw_validate_stage( struct draw_context *draw )
335 {
336 struct draw_stage *stage = CALLOC_STRUCT(draw_stage);
337 if (stage == NULL)
338 return NULL;
339
340 stage->draw = draw;
341 stage->name = "validate";
342 stage->next = NULL;
343 stage->point = validate_point;
344 stage->line = validate_line;
345 stage->tri = validate_tri;
346 stage->flush = validate_flush;
347 stage->reset_stipple_counter = validate_reset_stipple_counter;
348 stage->destroy = validate_destroy;
349
350 return stage;
351 }