gallium: new draw stage for polygon stipple.
[mesa.git] / src / gallium / auxiliary / draw / draw_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
35
36
37
38
39 /**
40 * Rebuild the rendering pipeline.
41 */
42 static struct draw_stage *validate_pipeline( struct draw_stage *stage )
43 {
44 struct draw_context *draw = stage->draw;
45 struct draw_stage *next = draw->pipeline.rasterize;
46 int need_det = 0;
47 int precalc_flat = 0;
48
49 /* Set the validate's next stage to the rasterize stage, so that it
50 * can be found later if needed for flushing.
51 */
52 stage->next = next;
53
54 /*
55 * NOTE: we build up the pipeline in end-to-start order.
56 *
57 * TODO: make the current primitive part of the state and build
58 * shorter pipelines for lines & points.
59 */
60
61 if (draw->rasterizer->line_smooth && draw->pipeline.aaline) {
62 draw->pipeline.aaline->next = next;
63 next = draw->pipeline.aaline;
64 }
65
66 if (draw->rasterizer->point_smooth && draw->pipeline.aapoint) {
67 draw->pipeline.aapoint->next = next;
68 next = draw->pipeline.aapoint;
69 }
70
71 if ((draw->rasterizer->line_width != 1.0 && draw->convert_wide_lines
72 && !draw->rasterizer->line_smooth) ||
73 (draw->rasterizer->point_size != 1.0 && draw->convert_wide_points) ||
74 draw->rasterizer->point_sprite) {
75 draw->pipeline.wide->next = next;
76 next = draw->pipeline.wide;
77 }
78
79 if (draw->rasterizer->line_stipple_enable) {
80 draw->pipeline.stipple->next = next;
81 next = draw->pipeline.stipple;
82 precalc_flat = 1; /* only needed for lines really */
83 }
84
85 if (draw->rasterizer->poly_stipple_enable
86 && draw->pipeline.pstipple) {
87 draw->pipeline.pstipple->next = next;
88 next = draw->pipeline.pstipple;
89 }
90
91 if (draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
92 draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) {
93 draw->pipeline.unfilled->next = next;
94 next = draw->pipeline.unfilled;
95 precalc_flat = 1; /* only needed for triangles really */
96 need_det = 1;
97 }
98
99 if (draw->rasterizer->flatshade && precalc_flat) {
100 draw->pipeline.flatshade->next = next;
101 next = draw->pipeline.flatshade;
102 }
103
104 if (draw->rasterizer->offset_cw ||
105 draw->rasterizer->offset_ccw) {
106 draw->pipeline.offset->next = next;
107 next = draw->pipeline.offset;
108 need_det = 1;
109 }
110
111 if (draw->rasterizer->light_twoside) {
112 draw->pipeline.twoside->next = next;
113 next = draw->pipeline.twoside;
114 need_det = 1;
115 }
116
117 /* Always run the cull stage as we calculate determinant there
118 * also.
119 *
120 * This can actually be a win as culling out the triangles can lead
121 * to less work emitting vertices, smaller vertex buffers, etc.
122 * It's difficult to say whether this will be true in general.
123 */
124 if (need_det || draw->rasterizer->cull_mode) {
125 draw->pipeline.cull->next = next;
126 next = draw->pipeline.cull;
127 }
128
129 /* Clip stage
130 */
131 if (!draw->rasterizer->bypass_clipping)
132 {
133 draw->pipeline.clip->next = next;
134 next = draw->pipeline.clip;
135 }
136
137
138 draw->pipeline.first = next;
139 return next;
140 }
141
142 static void validate_tri( struct draw_stage *stage,
143 struct prim_header *header )
144 {
145 struct draw_stage *pipeline = validate_pipeline( stage );
146 pipeline->tri( pipeline, header );
147 }
148
149 static void validate_line( struct draw_stage *stage,
150 struct prim_header *header )
151 {
152 struct draw_stage *pipeline = validate_pipeline( stage );
153 pipeline->line( pipeline, header );
154 }
155
156 static void validate_point( struct draw_stage *stage,
157 struct prim_header *header )
158 {
159 struct draw_stage *pipeline = validate_pipeline( stage );
160 pipeline->point( pipeline, header );
161 }
162
163 static void validate_reset_stipple_counter( struct draw_stage *stage )
164 {
165 struct draw_stage *pipeline = validate_pipeline( stage );
166 pipeline->reset_stipple_counter( pipeline );
167 }
168
169 static void validate_flush( struct draw_stage *stage,
170 unsigned flags )
171 {
172 /* May need to pass a backend flush on to the rasterize stage.
173 */
174 if (stage->next)
175 stage->next->flush( stage->next, flags );
176 }
177
178
179 static void validate_destroy( struct draw_stage *stage )
180 {
181 FREE( stage );
182 }
183
184
185 /**
186 * Create validate pipeline stage.
187 */
188 struct draw_stage *draw_validate_stage( struct draw_context *draw )
189 {
190 struct draw_stage *stage = CALLOC_STRUCT(draw_stage);
191
192 stage->draw = draw;
193 stage->next = NULL;
194 stage->point = validate_point;
195 stage->line = validate_line;
196 stage->tri = validate_tri;
197 stage->flush = validate_flush;
198 stage->reset_stipple_counter = validate_reset_stipple_counter;
199 stage->destroy = validate_destroy;
200
201 return stage;
202 }