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