4398abbc60c953775eb15d61b7775535afb63cae
[mesa.git] / src / gallium / aux / draw / draw_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_private.h"
34
35
36 /** subclass of draw_stage */
37 struct flat_stage
38 {
39 struct draw_stage stage;
40
41 uint num_color_attribs;
42 uint color_attribs[4]; /* front/back primary/secondary colors */
43 };
44
45
46 static INLINE struct flat_stage *
47 flat_stage(struct draw_stage *stage)
48 {
49 return (struct flat_stage *) stage;
50 }
51
52
53 /** Copy all the color attributes from 'src' vertex to 'dst' vertex */
54 static INLINE void copy_colors( struct draw_stage *stage,
55 struct vertex_header *dst,
56 const struct vertex_header *src )
57 {
58 const struct flat_stage *flat = flat_stage(stage);
59 uint i;
60 for (i = 0; i < flat->num_color_attribs; i++) {
61 const uint attr = flat->color_attribs[i];
62 COPY_4FV(dst->data[attr], src->data[attr]);
63 }
64 }
65
66
67 /** Copy all the color attributes from src vertex to dst0 & dst1 vertices */
68 static INLINE void copy_colors2( struct draw_stage *stage,
69 struct vertex_header *dst0,
70 struct vertex_header *dst1,
71 const struct vertex_header *src )
72 {
73 const struct flat_stage *flat = flat_stage(stage);
74 uint i;
75 for (i = 0; i < flat->num_color_attribs; i++) {
76 const uint attr = flat->color_attribs[i];
77 COPY_4FV(dst0->data[attr], src->data[attr]);
78 COPY_4FV(dst1->data[attr], src->data[attr]);
79 }
80 }
81
82
83 /**
84 * Flatshade tri. Required for clipping and when unfilled tris are
85 * active, otherwise handled by hardware.
86 */
87 static void flatshade_tri( struct draw_stage *stage,
88 struct prim_header *header )
89 {
90 struct prim_header tmp;
91
92 tmp.det = header->det;
93 tmp.edgeflags = header->edgeflags;
94 tmp.v[0] = dup_vert(stage, header->v[0], 0);
95 tmp.v[1] = dup_vert(stage, header->v[1], 1);
96 tmp.v[2] = header->v[2];
97
98 copy_colors2(stage, tmp.v[0], tmp.v[1], tmp.v[2]);
99
100 stage->next->tri( stage->next, &tmp );
101 }
102
103
104 /**
105 * Flatshade line. Required for clipping.
106 */
107 static void flatshade_line( struct draw_stage *stage,
108 struct prim_header *header )
109 {
110 struct prim_header tmp;
111
112 tmp.v[0] = dup_vert(stage, header->v[0], 0);
113 tmp.v[1] = header->v[1];
114
115 copy_colors(stage, tmp.v[0], tmp.v[1]);
116
117 stage->next->line( stage->next, &tmp );
118 }
119
120
121 static void flatshade_point( struct draw_stage *stage,
122 struct prim_header *header )
123 {
124 stage->next->point( stage->next, header );
125 }
126
127
128 static void flatshade_init_state( struct draw_stage *stage )
129 {
130 struct flat_stage *flat = flat_stage(stage);
131 const struct pipe_shader_state *vs = stage->draw->vertex_shader->state;
132 uint i;
133
134 /* Find which vertex shader outputs are colors, make a list */
135 flat->num_color_attribs = 0;
136 for (i = 0; i < vs->num_outputs; i++) {
137 if (vs->output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
138 vs->output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
139 flat->color_attribs[flat->num_color_attribs++] = i;
140 }
141 }
142
143 stage->line = flatshade_line;
144 stage->tri = flatshade_tri;
145 }
146
147 static void flatshade_first_tri( struct draw_stage *stage,
148 struct prim_header *header )
149 {
150 flatshade_init_state( stage );
151 stage->tri( stage, header );
152 }
153
154 static void flatshade_first_line( struct draw_stage *stage,
155 struct prim_header *header )
156 {
157 flatshade_init_state( stage );
158 stage->line( stage, header );
159 }
160
161
162 static void flatshade_flush( struct draw_stage *stage,
163 unsigned flags )
164 {
165 stage->tri = flatshade_first_tri;
166 stage->line = flatshade_first_line;
167 stage->next->flush( stage->next, flags );
168 }
169
170
171 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
172 {
173 stage->next->reset_stipple_counter( stage->next );
174 }
175
176
177 static void flatshade_destroy( struct draw_stage *stage )
178 {
179 draw_free_temp_verts( stage );
180 FREE( stage );
181 }
182
183
184 /**
185 * Create flatshading drawing stage.
186 */
187 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
188 {
189 struct flat_stage *flatshade = CALLOC_STRUCT(flat_stage);
190
191 draw_alloc_temp_verts( &flatshade->stage, 2 );
192
193 flatshade->stage.draw = draw;
194 flatshade->stage.next = NULL;
195 flatshade->stage.point = flatshade_point;
196 flatshade->stage.line = flatshade_first_line;
197 flatshade->stage.tri = flatshade_first_tri;
198 flatshade->stage.flush = flatshade_flush;
199 flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;
200 flatshade->stage.destroy = flatshade_destroy;
201
202 return &flatshade->stage;
203 }
204
205