In draw_flatshade.c use vertex_info->interp_mode[] to choose attribs/colors to cpy.
[mesa.git] / src / mesa / pipe / 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 "draw_private.h"
33
34
35 static void flatshade_begin( struct draw_stage *stage )
36 {
37 stage->next->begin( stage->next );
38 }
39
40
41
42 static INLINE void copy_attr( unsigned attr,
43 struct vertex_header *dst,
44 const struct vertex_header *src )
45 {
46 if (attr) {
47 memcpy( dst->data[attr],
48 src->data[attr],
49 sizeof(src->data[0]) );
50 }
51 }
52
53
54 static INLINE void copy_colors( struct draw_stage *stage,
55 struct vertex_header *dst,
56 const struct vertex_header *src )
57 {
58 const uint num_attribs = stage->draw->vertex_info.num_attribs;
59 const uint *interp_mode = stage->draw->vertex_info.interp_mode;
60 uint i;
61
62 /* Look for constant/flat attribs and duplicate from src to dst vertex */
63 for (i = 1; i < num_attribs - 2; i++) {
64 if (interp_mode[i + 2] == INTERP_CONSTANT) {
65 copy_attr( i, dst, src );
66 }
67 }
68 }
69
70
71 /**
72 * Flatshade tri. Required for clipping and when unfilled tris are
73 * active, otherwise handled by hardware.
74 */
75 static void flatshade_tri( struct draw_stage *stage,
76 struct prim_header *header )
77 {
78 struct prim_header tmp;
79
80 tmp.det = header->det;
81 tmp.edgeflags = header->edgeflags;
82 tmp.v[0] = dup_vert(stage, header->v[0], 0);
83 tmp.v[1] = dup_vert(stage, header->v[1], 1);
84 tmp.v[2] = header->v[2];
85
86 copy_colors(stage, tmp.v[0], tmp.v[2]);
87 copy_colors(stage, tmp.v[1], tmp.v[2]);
88
89 stage->next->tri( stage->next, &tmp );
90 }
91
92
93 /**
94 * Flatshade line. Required for clipping.
95 */
96 static void flatshade_line( struct draw_stage *stage,
97 struct prim_header *header )
98 {
99 struct prim_header tmp;
100
101 tmp.v[0] = dup_vert(stage, header->v[0], 0);
102 tmp.v[1] = header->v[1];
103
104 copy_colors(stage, tmp.v[0], tmp.v[1]);
105
106 stage->next->line( stage->next, &tmp );
107 }
108
109
110 static void flatshade_point( struct draw_stage *stage,
111 struct prim_header *header )
112 {
113 stage->next->point( stage->next, header );
114 }
115
116
117 static void flatshade_end( struct draw_stage *stage )
118 {
119 stage->next->end( stage->next );
120 }
121
122
123 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
124 {
125 stage->next->reset_stipple_counter( stage->next );
126 }
127
128
129 /**
130 * Create flatshading drawing stage.
131 */
132 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
133 {
134 struct draw_stage *flatshade = CALLOC_STRUCT(draw_stage);
135
136 draw_alloc_tmps( &flatshade->stage, 2 );
137
138 flatshade->draw = draw;
139 flatshade->next = NULL;
140 flatshade->begin = flatshade_begin;
141 flatshade->point = flatshade_point;
142 flatshade->line = flatshade_line;
143 flatshade->tri = flatshade_tri;
144 flatshade->end = flatshade_end;
145 flatshade->reset_stipple_counter = flatshade_reset_stipple_counter;
146
147 return flatshade;
148 }
149
150