Simplify draw module's vertex_info.
[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 enum interp_mode *interp = stage->draw->vertex_info.interp_mode;
60 uint i;
61
62 /* Look for constant/flat attribs and duplicate from src to dst vertex */
63 /* skip attrib[0] which is vert pos */
64 for (i = 1; i < num_attribs; i++) {
65 if (interp[i] == INTERP_CONSTANT) {
66 copy_attr( i, dst, src );
67 }
68 }
69 }
70
71
72 /**
73 * Flatshade tri. Required for clipping and when unfilled tris are
74 * active, otherwise handled by hardware.
75 */
76 static void flatshade_tri( struct draw_stage *stage,
77 struct prim_header *header )
78 {
79 struct prim_header tmp;
80
81 tmp.det = header->det;
82 tmp.edgeflags = header->edgeflags;
83 tmp.v[0] = dup_vert(stage, header->v[0], 0);
84 tmp.v[1] = dup_vert(stage, header->v[1], 1);
85 tmp.v[2] = header->v[2];
86
87 copy_colors(stage, tmp.v[0], tmp.v[2]);
88 copy_colors(stage, tmp.v[1], tmp.v[2]);
89
90 stage->next->tri( stage->next, &tmp );
91 }
92
93
94 /**
95 * Flatshade line. Required for clipping.
96 */
97 static void flatshade_line( struct draw_stage *stage,
98 struct prim_header *header )
99 {
100 struct prim_header tmp;
101
102 tmp.v[0] = dup_vert(stage, header->v[0], 0);
103 tmp.v[1] = header->v[1];
104
105 copy_colors(stage, tmp.v[0], tmp.v[1]);
106
107 stage->next->line( stage->next, &tmp );
108 }
109
110
111 static void flatshade_point( struct draw_stage *stage,
112 struct prim_header *header )
113 {
114 stage->next->point( stage->next, header );
115 }
116
117
118 static void flatshade_end( struct draw_stage *stage )
119 {
120 stage->next->end( stage->next );
121 }
122
123
124 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
125 {
126 stage->next->reset_stipple_counter( stage->next );
127 }
128
129
130 /**
131 * Create flatshading drawing stage.
132 */
133 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
134 {
135 struct draw_stage *flatshade = CALLOC_STRUCT(draw_stage);
136
137 draw_alloc_tmps( flatshade, 2 );
138
139 flatshade->draw = draw;
140 flatshade->next = NULL;
141 flatshade->begin = flatshade_begin;
142 flatshade->point = flatshade_point;
143 flatshade->line = flatshade_line;
144 flatshade->tri = flatshade_tri;
145 flatshade->end = flatshade_end;
146 flatshade->reset_stipple_counter = flatshade_reset_stipple_counter;
147
148 return flatshade;
149 }
150
151