draw: avoid overflows in the llvm draw loop
[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 if (draw_current_shader_num_written_culldistances(draw))
98 return TRUE;
99 }
100
101 if (points(prim))
102 {
103 /* large points */
104 if (rasterizer->point_size > draw->pipeline.wide_point_threshold)
105 return TRUE;
106
107 /* sprite points */
108 if (rasterizer->point_quad_rasterization
109 && draw->pipeline.wide_point_sprites)
110 return TRUE;
111
112 /* AA points */
113 if (rasterizer->point_smooth && draw->pipeline.aapoint)
114 return TRUE;
115
116 /* point sprites */
117 if (rasterizer->sprite_coord_enable && draw->pipeline.point_sprite)
118 return TRUE;
119 }
120
121
122 if (triangles(prim))
123 {
124 /* polygon stipple */
125 if (rasterizer->poly_stipple_enable && draw->pipeline.pstipple)
126 return TRUE;
127
128 /* unfilled polygons */
129 if (rasterizer->fill_front != PIPE_POLYGON_MODE_FILL ||
130 rasterizer->fill_back != PIPE_POLYGON_MODE_FILL)
131 return TRUE;
132
133 /* polygon offset */
134 if (rasterizer->offset_point ||
135 rasterizer->offset_line ||
136 rasterizer->offset_tri)
137 return TRUE;
138
139 /* two-side lighting */
140 if (rasterizer->light_twoside)
141 return TRUE;
142
143 if (draw_current_shader_num_written_culldistances(draw))
144 return TRUE;
145 }
146
147 /* polygon cull - this is difficult - hardware can cull just fine
148 * most of the time (though sometimes CULL_NEITHER is unsupported.
149 *
150 * Generally this isn't a reason to require the pipeline, though.
151 *
152 if (rasterizer->cull_mode)
153 return TRUE;
154 */
155
156 return FALSE;
157 }
158
159
160
161 /**
162 * Rebuild the rendering pipeline.
163 */
164 static struct draw_stage *validate_pipeline( struct draw_stage *stage )
165 {
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;
172
173 /* Set the validate's next stage to the rasterize stage, so that it
174 * can be found later if needed for flushing.
175 */
176 stage->next = next;
177
178 /* drawing wide lines? */
179 wide_lines = (roundf(rast->line_width) > draw->pipeline.wide_line_threshold
180 && !rast->line_smooth);
181
182 /* drawing large/sprite points (but not AA points)? */
183 if (rast->sprite_coord_enable && draw->pipeline.point_sprite)
184 wide_points = TRUE;
185 else if (rast->point_smooth && draw->pipeline.aapoint)
186 wide_points = FALSE;
187 else if (rast->point_size > draw->pipeline.wide_point_threshold)
188 wide_points = TRUE;
189 else if (rast->point_quad_rasterization && draw->pipeline.wide_point_sprites)
190 wide_points = TRUE;
191 else
192 wide_points = FALSE;
193
194 /*
195 * NOTE: we build up the pipeline in end-to-start order.
196 *
197 * TODO: make the current primitive part of the state and build
198 * shorter pipelines for lines & points.
199 */
200
201 if (rast->line_smooth && draw->pipeline.aaline) {
202 draw->pipeline.aaline->next = next;
203 next = draw->pipeline.aaline;
204 precalc_flat = TRUE;
205 }
206
207 if (rast->point_smooth && draw->pipeline.aapoint) {
208 draw->pipeline.aapoint->next = next;
209 next = draw->pipeline.aapoint;
210 }
211
212 if (wide_lines) {
213 draw->pipeline.wide_line->next = next;
214 next = draw->pipeline.wide_line;
215 precalc_flat = TRUE;
216 }
217
218 if (wide_points) {
219 draw->pipeline.wide_point->next = next;
220 next = draw->pipeline.wide_point;
221 }
222
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 */
227 }
228
229 if (rast->poly_stipple_enable
230 && draw->pipeline.pstipple) {
231 draw->pipeline.pstipple->next = next;
232 next = draw->pipeline.pstipple;
233 }
234
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 */
240 need_det = TRUE;
241 }
242
243 if (rast->flatshade && precalc_flat) {
244 draw->pipeline.flatshade->next = next;
245 next = draw->pipeline.flatshade;
246 }
247
248 if (rast->offset_point ||
249 rast->offset_line ||
250 rast->offset_tri) {
251 draw->pipeline.offset->next = next;
252 next = draw->pipeline.offset;
253 need_det = TRUE;
254 }
255
256 if (rast->light_twoside) {
257 draw->pipeline.twoside->next = next;
258 next = draw->pipeline.twoside;
259 need_det = TRUE;
260 }
261
262 /* Always run the cull stage as we calculate determinant there
263 * also.
264 *
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.
268 */
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;
273 }
274
275 /* Clip stage
276 */
277 if (draw->clip_xy || draw->clip_z || draw->clip_user)
278 {
279 draw->pipeline.clip->next = next;
280 next = draw->pipeline.clip;
281 }
282
283
284 draw->pipeline.first = next;
285
286 if (0) {
287 debug_printf("draw pipeline:\n");
288 for (next = draw->pipeline.first; next ; next = next->next )
289 debug_printf(" %s\n", next->name);
290 debug_printf("\n");
291 }
292
293 return draw->pipeline.first;
294 }
295
296 static void validate_tri( struct draw_stage *stage,
297 struct prim_header *header )
298 {
299 struct draw_stage *pipeline = validate_pipeline( stage );
300 pipeline->tri( pipeline, header );
301 }
302
303 static void validate_line( struct draw_stage *stage,
304 struct prim_header *header )
305 {
306 struct draw_stage *pipeline = validate_pipeline( stage );
307 pipeline->line( pipeline, header );
308 }
309
310 static void validate_point( struct draw_stage *stage,
311 struct prim_header *header )
312 {
313 struct draw_stage *pipeline = validate_pipeline( stage );
314 pipeline->point( pipeline, header );
315 }
316
317 static void validate_reset_stipple_counter( struct draw_stage *stage )
318 {
319 struct draw_stage *pipeline = validate_pipeline( stage );
320 pipeline->reset_stipple_counter( pipeline );
321 }
322
323 static void validate_flush( struct draw_stage *stage,
324 unsigned flags )
325 {
326 /* May need to pass a backend flush on to the rasterize stage.
327 */
328 if (stage->next)
329 stage->next->flush( stage->next, flags );
330 }
331
332
333 static void validate_destroy( struct draw_stage *stage )
334 {
335 FREE( stage );
336 }
337
338
339 /**
340 * Create validate pipeline stage.
341 */
342 struct draw_stage *draw_validate_stage( struct draw_context *draw )
343 {
344 struct draw_stage *stage = CALLOC_STRUCT(draw_stage);
345 if (stage == NULL)
346 return NULL;
347
348 stage->draw = draw;
349 stage->name = "validate";
350 stage->next = NULL;
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;
357
358 return stage;
359 }