1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
31 #include "pipe/p_util.h"
32 #include "draw_private.h"
35 static void flatshade_begin( struct draw_stage
*stage
)
37 stage
->next
->begin( stage
->next
);
42 static INLINE
void copy_attr( unsigned attr
,
43 struct vertex_header
*dst
,
44 const struct vertex_header
*src
)
47 memcpy( dst
->data
[attr
],
49 sizeof(src
->data
[0]) );
54 static INLINE
void copy_colors( struct draw_stage
*stage
,
55 struct vertex_header
*dst
,
56 const struct vertex_header
*src
)
58 const uint num_attribs
= stage
->draw
->vertex_info
.num_attribs
;
59 const interp_mode
*interp
= stage
->draw
->vertex_info
.interp_mode
;
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
[i
+ 2] == INTERP_CONSTANT
) {
65 copy_attr( i
, dst
, src
);
72 * Flatshade tri. Required for clipping and when unfilled tris are
73 * active, otherwise handled by hardware.
75 static void flatshade_tri( struct draw_stage
*stage
,
76 struct prim_header
*header
)
78 struct prim_header tmp
;
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];
86 copy_colors(stage
, tmp
.v
[0], tmp
.v
[2]);
87 copy_colors(stage
, tmp
.v
[1], tmp
.v
[2]);
89 stage
->next
->tri( stage
->next
, &tmp
);
94 * Flatshade line. Required for clipping.
96 static void flatshade_line( struct draw_stage
*stage
,
97 struct prim_header
*header
)
99 struct prim_header tmp
;
101 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
102 tmp
.v
[1] = header
->v
[1];
104 copy_colors(stage
, tmp
.v
[0], tmp
.v
[1]);
106 stage
->next
->line( stage
->next
, &tmp
);
110 static void flatshade_point( struct draw_stage
*stage
,
111 struct prim_header
*header
)
113 stage
->next
->point( stage
->next
, header
);
117 static void flatshade_end( struct draw_stage
*stage
)
119 stage
->next
->end( stage
->next
);
123 static void flatshade_reset_stipple_counter( struct draw_stage
*stage
)
125 stage
->next
->reset_stipple_counter( stage
->next
);
130 * Create flatshading drawing stage.
132 struct draw_stage
*draw_flatshade_stage( struct draw_context
*draw
)
134 struct draw_stage
*flatshade
= CALLOC_STRUCT(draw_stage
);
136 draw_alloc_tmps( flatshade
, 2 );
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
;