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