OSDN Git Service

misc changes to support vertex shaders (disabled by default)
[android-x86/external-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 "main/imports.h"
32 #include "draw_private.h"
33
34
35 struct flatshade_stage {
36    struct draw_stage stage;
37
38    const GLuint *lookup;
39 };
40
41
42
43 static INLINE struct flatshade_stage *flatshade_stage( struct draw_stage *stage )
44 {
45    return (struct flatshade_stage *)stage;
46 }
47
48
49 static void flatshade_begin( struct draw_stage *stage )
50 {
51    stage->next->begin( stage->next );
52 }
53
54
55
56 static INLINE void copy_attr( GLuint attr,
57                               struct vertex_header *dst, 
58                               const struct vertex_header *src )
59 {
60    if (attr) {
61       memcpy( dst->data[attr],
62               src->data[attr],
63               sizeof(src->data[0]) );
64    }
65 }
66
67
68 static INLINE void copy_colors( struct draw_stage *stage, 
69                                 struct vertex_header *dst, 
70                                 const struct vertex_header *src )
71 {
72    const struct flatshade_stage *flatshade = flatshade_stage(stage);
73    const GLuint *lookup = flatshade->lookup;
74
75    copy_attr( lookup[VF_ATTRIB_COLOR0], dst, src );
76    copy_attr( lookup[VF_ATTRIB_COLOR1], dst, src );
77    copy_attr( lookup[VF_ATTRIB_BFC0], dst, src );
78    copy_attr( lookup[VF_ATTRIB_BFC1], dst, src );
79 }
80
81
82 /**
83  * Flatshade tri.  Required for clipping and when unfilled tris are
84  * active, otherwise handled by hardware.
85  */
86 static void flatshade_tri( struct draw_stage *stage,
87                            struct prim_header *header )
88 {
89    struct prim_header tmp;
90
91    tmp.det = header->det;
92    tmp.v[0] = dup_vert(stage, header->v[0], 0);
93    tmp.v[1] = dup_vert(stage, header->v[1], 1);
94    tmp.v[2] = header->v[2];
95
96    copy_colors(stage, tmp.v[0], tmp.v[2]);
97    copy_colors(stage, tmp.v[1], tmp.v[2]);
98    
99    stage->next->tri( stage->next, &tmp );
100 }
101
102
103 /**
104  * Flatshade line.  Required for clipping.
105  */
106 static void flatshade_line( struct draw_stage *stage,
107                             struct prim_header *header )
108 {
109    struct prim_header tmp;
110
111    tmp.v[0] = dup_vert(stage, header->v[0], 0);
112    tmp.v[1] = header->v[1];
113
114    copy_colors(stage, tmp.v[0], tmp.v[1]);
115    
116    stage->next->line( stage->next, &tmp );
117 }
118
119
120 static void flatshade_point( struct draw_stage *stage,
121                              struct prim_header *header )
122 {
123    stage->next->point( stage->next, header );
124 }
125
126
127 static void flatshade_end( struct draw_stage *stage )
128 {
129    stage->next->end( stage->next );
130 }
131
132
133 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
134 {
135    stage->next->reset_stipple_counter( stage->next );
136 }
137
138
139 /**
140  * Create flatshading drawing stage.
141  */
142 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
143 {
144    struct flatshade_stage *flatshade = CALLOC_STRUCT(flatshade_stage);
145
146    draw_alloc_tmps( &flatshade->stage, 2 );
147
148    flatshade->stage.draw = draw;
149    flatshade->stage.next = NULL;
150    flatshade->stage.begin = flatshade_begin;
151    flatshade->stage.point = flatshade_point;
152    flatshade->stage.line = flatshade_line;
153    flatshade->stage.tri = flatshade_tri;
154    flatshade->stage.end = flatshade_end;
155    flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;
156
157    flatshade->lookup = draw->vf_attr_to_slot;
158
159    return &flatshade->stage;
160 }
161
162