1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
29  */
30 
31 #include "util/u_memory.h"
32 #include "util/u_math.h"
33 #include "util/u_prim.h"
34 #include "pipe/p_defines.h"
35 #include "draw_private.h"
36 #include "draw_pipe.h"
37 #include "draw_context.h"
38 #include "draw_vbuf.h"
39 
40 
41 /**
42  * Default version of a function to check if we need any special
43  * pipeline stages, or whether prims/verts can go through untouched.
44  * Don't test for bypass clipping or vs modes, this function is just
45  * about the primitive pipeline stages.
46  *
47  * This can be overridden by the driver.
48  */
49 boolean
draw_need_pipeline(const struct draw_context * draw,const struct pipe_rasterizer_state * rasterizer,unsigned int prim)50 draw_need_pipeline(const struct draw_context *draw,
51                    const struct pipe_rasterizer_state *rasterizer,
52                    unsigned int prim )
53 {
54    unsigned reduced_prim = u_reduced_prim(prim);
55 
56    /* If the driver has overridden this, use that version:
57     */
58    if (draw->render &&
59        draw->render->need_pipeline)
60    {
61       return draw->render->need_pipeline( draw->render,
62                                           rasterizer,
63                                           prim );
64    }
65 
66    /* Don't have to worry about triangles turning into lines/points
67     * and triggering the pipeline, because we have to trigger the
68     * pipeline *anyway* if unfilled mode is active.
69     */
70    if (reduced_prim == PIPE_PRIM_LINES) {
71       /* line stipple */
72       if (rasterizer->line_stipple_enable && draw->pipeline.line_stipple)
73          return TRUE;
74 
75       /* wide lines */
76       if (roundf(rasterizer->line_width) > draw->pipeline.wide_line_threshold)
77          return TRUE;
78 
79       /* AA lines */
80       if ((!rasterizer->multisample && rasterizer->line_smooth) && draw->pipeline.aaline)
81          return TRUE;
82 
83       if (draw_current_shader_num_written_culldistances(draw))
84          return TRUE;
85    }
86    else if (reduced_prim == PIPE_PRIM_POINTS) {
87       /* large points */
88       if (rasterizer->point_size > draw->pipeline.wide_point_threshold)
89          return TRUE;
90 
91       /* sprite points */
92       if (rasterizer->point_quad_rasterization
93           && draw->pipeline.wide_point_sprites)
94          return TRUE;
95 
96       /* AA points */
97       if ((!rasterizer->multisample && rasterizer->point_smooth) && draw->pipeline.aapoint)
98          return TRUE;
99 
100       /* point sprites */
101       if (rasterizer->sprite_coord_enable && draw->pipeline.point_sprite)
102          return TRUE;
103 
104       if (draw_current_shader_num_written_culldistances(draw))
105          return TRUE;
106    }
107    else if (reduced_prim == PIPE_PRIM_TRIANGLES) {
108       /* polygon stipple */
109       if (rasterizer->poly_stipple_enable && draw->pipeline.pstipple)
110          return TRUE;
111 
112       /* unfilled polygons */
113       if (rasterizer->fill_front != PIPE_POLYGON_MODE_FILL ||
114           rasterizer->fill_back != PIPE_POLYGON_MODE_FILL)
115          return TRUE;
116 
117       /* polygon offset */
118       if (rasterizer->offset_point ||
119           rasterizer->offset_line ||
120           rasterizer->offset_tri)
121          return TRUE;
122 
123       /* two-side lighting */
124       if (rasterizer->light_twoside)
125          return TRUE;
126 
127       if (draw_current_shader_num_written_culldistances(draw))
128          return TRUE;
129    }
130 
131    /* polygon cull - this is difficult - hardware can cull just fine
132     * most of the time (though sometimes CULL_NEITHER is unsupported.
133     *
134     * Generally this isn't a reason to require the pipeline, though.
135     *
136    if (rasterizer->cull_mode)
137       return TRUE;
138    */
139 
140    return FALSE;
141 }
142 
143 
144 
145 /**
146  * Rebuild the rendering pipeline.
147  */
validate_pipeline(struct draw_stage * stage)148 static struct draw_stage *validate_pipeline( struct draw_stage *stage )
149 {
150    struct draw_context *draw = stage->draw;
151    struct draw_stage *next = draw->pipeline.rasterize;
152    boolean need_det = FALSE;
153    boolean precalc_flat = FALSE;
154    boolean wide_lines, wide_points;
155    const struct pipe_rasterizer_state *rast = draw->rasterizer;
156 
157    /* Set the validate's next stage to the rasterize stage, so that it
158     * can be found later if needed for flushing.
159     */
160    stage->next = next;
161 
162    /* drawing wide, non-AA lines? */
163    wide_lines = rast->line_width != 1.0f &&
164                 roundf(rast->line_width) > draw->pipeline.wide_line_threshold &&
165                 (!rast->line_smooth || rast->multisample);
166 
167    /* drawing large/sprite points (but not AA points)? */
168    if (rast->sprite_coord_enable && draw->pipeline.point_sprite)
169       wide_points = TRUE;
170    else if ((!rast->multisample && rast->point_smooth) && draw->pipeline.aapoint)
171       wide_points = FALSE;
172    else if (rast->point_size > draw->pipeline.wide_point_threshold)
173       wide_points = TRUE;
174    else if (rast->point_quad_rasterization && draw->pipeline.wide_point_sprites)
175       wide_points = TRUE;
176    else
177       wide_points = FALSE;
178 
179    /*
180     * NOTE: we build up the pipeline in end-to-start order.
181     *
182     * TODO: make the current primitive part of the state and build
183     * shorter pipelines for lines & points.
184     */
185 
186    if ((!rast->multisample && rast->line_smooth) && draw->pipeline.aaline) {
187       draw->pipeline.aaline->next = next;
188       next = draw->pipeline.aaline;
189       precalc_flat = TRUE;
190    }
191 
192    if ((!rast->multisample && rast->point_smooth) && draw->pipeline.aapoint) {
193       draw->pipeline.aapoint->next = next;
194       next = draw->pipeline.aapoint;
195    }
196 
197    if (wide_lines) {
198       draw->pipeline.wide_line->next = next;
199       next = draw->pipeline.wide_line;
200       precalc_flat = TRUE;
201    }
202 
203    if (wide_points) {
204       draw->pipeline.wide_point->next = next;
205       next = draw->pipeline.wide_point;
206    }
207 
208    if (rast->line_stipple_enable && draw->pipeline.line_stipple) {
209       draw->pipeline.stipple->next = next;
210       next = draw->pipeline.stipple;
211       precalc_flat = TRUE;		/* only needed for lines really */
212    }
213 
214    if (rast->poly_stipple_enable
215        && draw->pipeline.pstipple) {
216       draw->pipeline.pstipple->next = next;
217       next = draw->pipeline.pstipple;
218    }
219 
220    if (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
221        rast->fill_back != PIPE_POLYGON_MODE_FILL) {
222       draw->pipeline.unfilled->next = next;
223       next = draw->pipeline.unfilled;
224       precalc_flat = TRUE;		/* only needed for triangles really */
225       need_det = TRUE;
226    }
227 
228    if (precalc_flat) {
229       /*
230        * could only run the stage if either rast->flatshade is true
231        * or there's constant interpolated values.
232        */
233       draw->pipeline.flatshade->next = next;
234       next = draw->pipeline.flatshade;
235    }
236 
237    if (rast->offset_point ||
238        rast->offset_line ||
239        rast->offset_tri) {
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_face != PIPE_FACE_NONE) {
259       draw->pipeline.cull->next = next;
260       next = draw->pipeline.cull;
261    }
262 
263    /* Clip stage
264     */
265    if (draw->clip_xy || draw->clip_z || draw->clip_user)
266    {
267       draw->pipeline.clip->next = next;
268       next = draw->pipeline.clip;
269    }
270 
271    if (draw_current_shader_num_written_culldistances(draw)) {
272       draw->pipeline.user_cull->next = next;
273       next = draw->pipeline.user_cull;
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 
validate_tri(struct draw_stage * stage,struct prim_header * header)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 
validate_line(struct draw_stage * stage,struct prim_header * header)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 
validate_point(struct draw_stage * stage,struct prim_header * header)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 
validate_reset_stipple_counter(struct draw_stage * stage)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 
validate_flush(struct draw_stage * stage,unsigned flags)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 
validate_destroy(struct draw_stage * stage)325 static void validate_destroy( struct draw_stage *stage )
326 {
327    FREE( stage );
328 }
329 
330 
331 /**
332  * Create validate pipeline stage.
333  */
draw_validate_stage(struct draw_context * draw)334 struct draw_stage *draw_validate_stage( struct draw_context *draw )
335 {
336    struct draw_stage *stage = CALLOC_STRUCT(draw_stage);
337    if (!stage)
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 }
352