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