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
.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];
96 copy_colors(stage
, tmp
.v
[0], tmp
.v
[2]);
97 copy_colors(stage
, tmp
.v
[1], tmp
.v
[2]);
99 stage
->next
->tri( stage
->next
, &tmp
);
104 * Flatshade line. Required for clipping.
106 static void flatshade_line( struct draw_stage
*stage
,
107 struct prim_header
*header
)
109 struct prim_header tmp
;
111 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
112 tmp
.v
[1] = header
->v
[1];
114 copy_colors(stage
, tmp
.v
[0], tmp
.v
[1]);
116 stage
->next
->line( stage
->next
, &tmp
);
120 static void flatshade_point( struct draw_stage
*stage
,
121 struct prim_header
*header
)
123 stage
->next
->point( stage
->next
, header
);
127 static void flatshade_end( struct draw_stage
*stage
)
129 stage
->next
->end( stage
->next
);
133 static void flatshade_reset_stipple_counter( struct draw_stage
*stage
)
135 stage
->next
->reset_stipple_counter( stage
->next
);
140 * Create flatshading drawing stage.
142 struct draw_stage
*draw_flatshade_stage( struct draw_context
*draw
)
144 struct flatshade_stage
*flatshade
= CALLOC_STRUCT(flatshade_stage
);
146 draw_alloc_tmps( &flatshade
->stage
, 2 );
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
;
157 flatshade
->lookup
= draw
->vertex_info
.attrib_to_slot
;
159 return &flatshade
->stage
;