205000cbea57db259b8443b23ef9586c88ac579a
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_flatshade.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_shader_tokens.h"
33 #include "draw_vs.h"
34 #include "draw_pipe.h"
35
36
37 /** subclass of draw_stage */
38 struct flat_stage
39 {
40 struct draw_stage stage;
41
42 uint num_color_attribs;
43 uint color_attribs[4]; /* front/back primary/secondary colors */
44 };
45
46
47 static INLINE struct flat_stage *
48 flat_stage(struct draw_stage *stage)
49 {
50 return (struct flat_stage *) stage;
51 }
52
53
54 /** Copy all the color attributes from 'src' vertex to 'dst' vertex */
55 static INLINE void copy_colors( struct draw_stage *stage,
56 struct vertex_header *dst,
57 const struct vertex_header *src )
58 {
59 const struct flat_stage *flat = flat_stage(stage);
60 uint i;
61 for (i = 0; i < flat->num_color_attribs; i++) {
62 const uint attr = flat->color_attribs[i];
63 COPY_4FV(dst->data[attr], src->data[attr]);
64 }
65 }
66
67
68 /** Copy all the color attributes from src vertex to dst0 & dst1 vertices */
69 static INLINE void copy_colors2( struct draw_stage *stage,
70 struct vertex_header *dst0,
71 struct vertex_header *dst1,
72 const struct vertex_header *src )
73 {
74 const struct flat_stage *flat = flat_stage(stage);
75 uint i;
76 for (i = 0; i < flat->num_color_attribs; i++) {
77 const uint attr = flat->color_attribs[i];
78 COPY_4FV(dst0->data[attr], src->data[attr]);
79 COPY_4FV(dst1->data[attr], src->data[attr]);
80 }
81 }
82
83
84 /**
85 * Flatshade tri. Required for clipping and when unfilled tris are
86 * active, otherwise handled by hardware.
87 */
88 static void flatshade_tri_0( struct draw_stage *stage,
89 struct prim_header *header )
90 {
91 struct prim_header tmp;
92
93 tmp.det = header->det;
94 tmp.edgeflags = header->edgeflags;
95 tmp.v[0] = header->v[0];
96 tmp.v[1] = dup_vert(stage, header->v[1], 0);
97 tmp.v[2] = dup_vert(stage, header->v[2], 1);
98
99 copy_colors2(stage, tmp.v[1], tmp.v[2], tmp.v[0]);
100
101 stage->next->tri( stage->next, &tmp );
102 }
103
104
105 static void flatshade_tri_2( struct draw_stage *stage,
106 struct prim_header *header )
107 {
108 struct prim_header tmp;
109
110 tmp.det = header->det;
111 tmp.edgeflags = header->edgeflags;
112 tmp.v[0] = dup_vert(stage, header->v[0], 0);
113 tmp.v[1] = dup_vert(stage, header->v[1], 1);
114 tmp.v[2] = header->v[2];
115
116 copy_colors2(stage, tmp.v[0], tmp.v[1], tmp.v[2]);
117
118 stage->next->tri( stage->next, &tmp );
119 }
120
121
122
123
124
125 /**
126 * Flatshade line. Required for clipping.
127 */
128 static void flatshade_line_0( struct draw_stage *stage,
129 struct prim_header *header )
130 {
131 struct prim_header tmp;
132
133 tmp.v[0] = header->v[0];
134 tmp.v[1] = dup_vert(stage, header->v[1], 0);
135
136 copy_colors(stage, tmp.v[1], tmp.v[0]);
137
138 stage->next->line( stage->next, &tmp );
139 }
140
141 static void flatshade_line_1( struct draw_stage *stage,
142 struct prim_header *header )
143 {
144 struct prim_header tmp;
145
146 tmp.v[0] = dup_vert(stage, header->v[0], 0);
147 tmp.v[1] = header->v[1];
148
149 copy_colors(stage, tmp.v[0], tmp.v[1]);
150
151 stage->next->line( stage->next, &tmp );
152 }
153
154
155
156
157 static void flatshade_init_state( struct draw_stage *stage )
158 {
159 struct flat_stage *flat = flat_stage(stage);
160 const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
161 uint i;
162
163 /* Find which vertex shader outputs are colors, make a list */
164 flat->num_color_attribs = 0;
165 for (i = 0; i < vs->info.num_outputs; i++) {
166 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
167 vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
168 flat->color_attribs[flat->num_color_attribs++] = i;
169 }
170 }
171
172 /* Choose flatshade routine according to provoking vertex:
173 */
174 if (stage->draw->rasterizer->flatshade_first) {
175 stage->line = flatshade_line_0;
176 stage->tri = flatshade_tri_0;
177 }
178 else {
179 stage->line = flatshade_line_1;
180 stage->tri = flatshade_tri_2;
181 }
182 }
183
184 static void flatshade_first_tri( struct draw_stage *stage,
185 struct prim_header *header )
186 {
187 flatshade_init_state( stage );
188 stage->tri( stage, header );
189 }
190
191 static void flatshade_first_line( struct draw_stage *stage,
192 struct prim_header *header )
193 {
194 flatshade_init_state( stage );
195 stage->line( stage, header );
196 }
197
198
199 static void flatshade_flush( struct draw_stage *stage,
200 unsigned flags )
201 {
202 stage->tri = flatshade_first_tri;
203 stage->line = flatshade_first_line;
204 stage->next->flush( stage->next, flags );
205 }
206
207
208 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
209 {
210 stage->next->reset_stipple_counter( stage->next );
211 }
212
213
214 static void flatshade_destroy( struct draw_stage *stage )
215 {
216 draw_free_temp_verts( stage );
217 FREE( stage );
218 }
219
220
221 /**
222 * Create flatshading drawing stage.
223 */
224 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
225 {
226 struct flat_stage *flatshade = CALLOC_STRUCT(flat_stage);
227 if (flatshade == NULL)
228 goto fail;
229
230 if (!draw_alloc_temp_verts( &flatshade->stage, 2 ))
231 goto fail;
232
233 flatshade->stage.draw = draw;
234 flatshade->stage.next = NULL;
235 flatshade->stage.point = draw_pipe_passthrough_point;
236 flatshade->stage.line = flatshade_first_line;
237 flatshade->stage.tri = flatshade_first_tri;
238 flatshade->stage.flush = flatshade_flush;
239 flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;
240 flatshade->stage.destroy = flatshade_destroy;
241
242 return &flatshade->stage;
243
244 fail:
245 if (flatshade)
246 flatshade->stage.destroy( &flatshade->stage );
247
248 return NULL;
249 }
250
251