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 "pipe/p_shader_tokens.h"
33 #include "draw_private.h"
36 /** subclass of draw_stage */
39 struct draw_stage stage
;
41 uint num_color_attribs
;
42 uint color_attribs
[4]; /* front/back primary/secondary colors */
46 static INLINE
struct flat_stage
*
47 flat_stage(struct draw_stage
*stage
)
49 return (struct flat_stage
*) stage
;
53 static void flatshade_begin( struct draw_stage
*stage
)
55 struct flat_stage
*flat
= flat_stage(stage
);
56 const struct pipe_shader_state
*vs
= stage
->draw
->vertex_shader
->state
;
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
;
68 stage
->next
->begin( stage
->next
);
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
)
77 const struct flat_stage
*flat
= flat_stage(stage
);
80 /* Look for constant/flat attribs and duplicate from src to dst vertex */
81 /* skip attrib[0] which is vert pos */
82 for (i
= 0; i
< flat
->num_color_attribs
; i
++) {
83 const uint attr
= flat
->color_attribs
[i
];
84 memcpy(dst
->data
[attr
], src
->data
[attr
], sizeof(src
->data
[0]));
90 * Flatshade tri. Required for clipping and when unfilled tris are
91 * active, otherwise handled by hardware.
93 static void flatshade_tri( struct draw_stage
*stage
,
94 struct prim_header
*header
)
96 struct prim_header tmp
;
98 tmp
.det
= header
->det
;
99 tmp
.edgeflags
= header
->edgeflags
;
100 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
101 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 1);
102 tmp
.v
[2] = header
->v
[2];
104 copy_colors(stage
, tmp
.v
[0], tmp
.v
[2]);
105 copy_colors(stage
, tmp
.v
[1], tmp
.v
[2]);
107 stage
->next
->tri( stage
->next
, &tmp
);
112 * Flatshade line. Required for clipping.
114 static void flatshade_line( struct draw_stage
*stage
,
115 struct prim_header
*header
)
117 struct prim_header tmp
;
119 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
120 tmp
.v
[1] = header
->v
[1];
122 copy_colors(stage
, tmp
.v
[0], tmp
.v
[1]);
124 stage
->next
->line( stage
->next
, &tmp
);
128 static void flatshade_point( struct draw_stage
*stage
,
129 struct prim_header
*header
)
131 stage
->next
->point( stage
->next
, header
);
135 static void flatshade_end( struct draw_stage
*stage
)
137 stage
->next
->end( stage
->next
);
141 static void flatshade_reset_stipple_counter( struct draw_stage
*stage
)
143 stage
->next
->reset_stipple_counter( stage
->next
);
147 static void flatshade_destroy( struct draw_stage
*stage
)
149 draw_free_tmps( stage
);
155 * Create flatshading drawing stage.
157 struct draw_stage
*draw_flatshade_stage( struct draw_context
*draw
)
159 struct flat_stage
*flatshade
= CALLOC_STRUCT(flat_stage
);
161 draw_alloc_tmps( &flatshade
->stage
, 2 );
163 flatshade
->stage
.draw
= draw
;
164 flatshade
->stage
.next
= NULL
;
165 flatshade
->stage
.begin
= flatshade_begin
;
166 flatshade
->stage
.point
= flatshade_point
;
167 flatshade
->stage
.line
= flatshade_line
;
168 flatshade
->stage
.tri
= flatshade_tri
;
169 flatshade
->stage
.end
= flatshade_end
;
170 flatshade
->stage
.reset_stipple_counter
= flatshade_reset_stipple_counter
;
171 flatshade
->stage
.destroy
= flatshade_destroy
;
173 return &flatshade
->stage
;