gallium: rename pipe_buffer_handle to pipe_buffer, rework pipebuffer/ code
[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 "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 static void flatshade_begin( struct draw_stage *stage )
54 {
55 struct flat_stage *flat = flat_stage(stage);
56 const struct pipe_shader_state *vs = stage->draw->vertex_shader->state;
57 uint i;
58
59 /* Find which vertex shader outputs are colors, make a list */
60 flat->num_color_attribs = 0;
61 for (i = 0; i < vs->num_outputs; i++) {
62 if (vs->output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
63 vs->output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
64 flat->color_attribs[flat->num_color_attribs++] = i;
65 }
66 }
67
68 stage->next->begin( stage->next );
69 }
70
71
72 /** Copy all the color attributes from 'src' vertex to 'dst' vertex */
73 static INLINE void copy_colors( struct draw_stage *stage,
74 struct vertex_header *dst,
75 const struct vertex_header *src )
76 {
77 const struct flat_stage *flat = flat_stage(stage);
78 uint i;
79 for (i = 0; i < flat->num_color_attribs; i++) {
80 const uint attr = flat->color_attribs[i];
81 COPY_4FV(dst->data[attr], src->data[attr]);
82 }
83 }
84
85
86 /** Copy all the color attributes from src vertex to dst0 & dst1 vertices */
87 static INLINE void copy_colors2( struct draw_stage *stage,
88 struct vertex_header *dst0,
89 struct vertex_header *dst1,
90 const struct vertex_header *src )
91 {
92 const struct flat_stage *flat = flat_stage(stage);
93 uint i;
94 for (i = 0; i < flat->num_color_attribs; i++) {
95 const uint attr = flat->color_attribs[i];
96 COPY_4FV(dst0->data[attr], src->data[attr]);
97 COPY_4FV(dst1->data[attr], src->data[attr]);
98 }
99 }
100
101
102 /**
103 * Flatshade tri. Required for clipping and when unfilled tris are
104 * active, otherwise handled by hardware.
105 */
106 static void flatshade_tri( struct draw_stage *stage,
107 struct prim_header *header )
108 {
109 struct prim_header tmp;
110
111 tmp.det = header->det;
112 tmp.edgeflags = header->edgeflags;
113 tmp.v[0] = dup_vert(stage, header->v[0], 0);
114 tmp.v[1] = dup_vert(stage, header->v[1], 1);
115 tmp.v[2] = header->v[2];
116
117 copy_colors2(stage, tmp.v[0], tmp.v[1], tmp.v[2]);
118
119 stage->next->tri( stage->next, &tmp );
120 }
121
122
123 /**
124 * Flatshade line. Required for clipping.
125 */
126 static void flatshade_line( struct draw_stage *stage,
127 struct prim_header *header )
128 {
129 struct prim_header tmp;
130
131 tmp.v[0] = dup_vert(stage, header->v[0], 0);
132 tmp.v[1] = header->v[1];
133
134 copy_colors(stage, tmp.v[0], tmp.v[1]);
135
136 stage->next->line( stage->next, &tmp );
137 }
138
139
140 static void flatshade_point( struct draw_stage *stage,
141 struct prim_header *header )
142 {
143 stage->next->point( stage->next, header );
144 }
145
146
147 static void flatshade_end( struct draw_stage *stage )
148 {
149 stage->next->end( stage->next );
150 }
151
152
153 static void flatshade_reset_stipple_counter( struct draw_stage *stage )
154 {
155 stage->next->reset_stipple_counter( stage->next );
156 }
157
158
159 static void flatshade_destroy( struct draw_stage *stage )
160 {
161 draw_free_tmps( stage );
162 FREE( stage );
163 }
164
165
166 /**
167 * Create flatshading drawing stage.
168 */
169 struct draw_stage *draw_flatshade_stage( struct draw_context *draw )
170 {
171 struct flat_stage *flatshade = CALLOC_STRUCT(flat_stage);
172
173 draw_alloc_tmps( &flatshade->stage, 2 );
174
175 flatshade->stage.draw = draw;
176 flatshade->stage.next = NULL;
177 flatshade->stage.begin = flatshade_begin;
178 flatshade->stage.point = flatshade_point;
179 flatshade->stage.line = flatshade_line;
180 flatshade->stage.tri = flatshade_tri;
181 flatshade->stage.end = flatshade_end;
182 flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;
183 flatshade->stage.destroy = flatshade_destroy;
184
185 return &flatshade->stage;
186 }
187
188