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 struct flatshade_stage
{
36 struct draw_stage stage
;
38 const unsigned *lookup
;
43 static INLINE
struct flatshade_stage
*flatshade_stage( struct draw_stage
*stage
)
45 return (struct flatshade_stage
*)stage
;
49 static void flatshade_begin( struct draw_stage
*stage
)
51 stage
->next
->begin( stage
->next
);
56 static INLINE
void copy_attr( unsigned attr
,
57 struct vertex_header
*dst
,
58 const struct vertex_header
*src
)
61 memcpy( dst
->data
[attr
],
63 sizeof(src
->data
[0]) );
68 static INLINE
void copy_colors( struct draw_stage
*stage
,
69 struct vertex_header
*dst
,
70 const struct vertex_header
*src
)
72 const struct flatshade_stage
*flatshade
= flatshade_stage(stage
);
73 const unsigned *lookup
= flatshade
->lookup
;
75 copy_attr( lookup
[TGSI_ATTRIB_COLOR0
], dst
, src
);
76 copy_attr( lookup
[TGSI_ATTRIB_COLOR1
], dst
, src
);
77 copy_attr( lookup
[TGSI_ATTRIB_BFC0
], dst
, src
);
78 copy_attr( lookup
[TGSI_ATTRIB_BFC1
], dst
, src
);
83 * Flatshade tri. Required for clipping and when unfilled tris are
84 * active, otherwise handled by hardware.
86 static void flatshade_tri( struct draw_stage
*stage
,
87 struct prim_header
*header
)
89 struct prim_header tmp
;
91 tmp
.det
= header
->det
;
92 tmp
.edgeflags
= header
->edgeflags
;
93 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
94 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 1);
95 tmp
.v
[2] = header
->v
[2];
97 copy_colors(stage
, tmp
.v
[0], tmp
.v
[2]);
98 copy_colors(stage
, tmp
.v
[1], tmp
.v
[2]);
100 stage
->next
->tri( stage
->next
, &tmp
);
105 * Flatshade line. Required for clipping.
107 static void flatshade_line( struct draw_stage
*stage
,
108 struct prim_header
*header
)
110 struct prim_header tmp
;
112 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
113 tmp
.v
[1] = header
->v
[1];
115 copy_colors(stage
, tmp
.v
[0], tmp
.v
[1]);
117 stage
->next
->line( stage
->next
, &tmp
);
121 static void flatshade_point( struct draw_stage
*stage
,
122 struct prim_header
*header
)
124 stage
->next
->point( stage
->next
, header
);
128 static void flatshade_end( struct draw_stage
*stage
)
130 stage
->next
->end( stage
->next
);
134 static void flatshade_reset_stipple_counter( struct draw_stage
*stage
)
136 stage
->next
->reset_stipple_counter( stage
->next
);
141 * Create flatshading drawing stage.
143 struct draw_stage
*draw_flatshade_stage( struct draw_context
*draw
)
145 struct flatshade_stage
*flatshade
= CALLOC_STRUCT(flatshade_stage
);
147 draw_alloc_tmps( &flatshade
->stage
, 2 );
149 flatshade
->stage
.draw
= draw
;
150 flatshade
->stage
.next
= NULL
;
151 flatshade
->stage
.begin
= flatshade_begin
;
152 flatshade
->stage
.point
= flatshade_point
;
153 flatshade
->stage
.line
= flatshade_line
;
154 flatshade
->stage
.tri
= flatshade_tri
;
155 flatshade
->stage
.end
= flatshade_end
;
156 flatshade
->stage
.reset_stipple_counter
= flatshade_reset_stipple_counter
;
158 flatshade
->lookup
= draw
->vertex_info
.attrib_to_slot
;
160 return &flatshade
->stage
;