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 "util/u_math.h"
32 #include "util/u_memory.h"
34 #include "pipe/p_shader_tokens.h"
36 #include "draw_pipe.h"
39 /** subclass of draw_stage */
42 struct draw_stage stage
;
44 uint num_color_attribs
;
45 uint color_attribs
[2]; /* front/back primary colors */
47 uint num_spec_attribs
;
48 uint spec_attribs
[2]; /* front/back secondary colors */
51 #define COPY_3FV( DST, SRC ) \
53 (DST)[0] = (SRC)[0]; \
54 (DST)[1] = (SRC)[1]; \
55 (DST)[2] = (SRC)[2]; \
59 static INLINE
struct flat_stage
*
60 flat_stage(struct draw_stage
*stage
)
62 return (struct flat_stage
*) stage
;
66 /** Copy all the color attributes from 'src' vertex to 'dst' vertex */
67 static INLINE
void copy_colors( struct draw_stage
*stage
,
68 struct vertex_header
*dst
,
69 const struct vertex_header
*src
)
71 const struct flat_stage
*flat
= flat_stage(stage
);
74 for (i
= 0; i
< flat
->num_color_attribs
; i
++) {
75 const uint attr
= flat
->color_attribs
[i
];
76 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
79 for (i
= 0; i
< flat
->num_spec_attribs
; i
++) {
80 const uint attr
= flat
->spec_attribs
[i
];
81 COPY_3FV(dst
->data
[attr
], src
->data
[attr
]);
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
)
92 const struct flat_stage
*flat
= flat_stage(stage
);
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
]);
100 for (i
= 0; i
< flat
->num_spec_attribs
; i
++) {
101 const uint attr
= flat
->spec_attribs
[i
];
102 COPY_3FV(dst0
->data
[attr
], src
->data
[attr
]);
103 COPY_3FV(dst1
->data
[attr
], src
->data
[attr
]);
109 * Flatshade tri. Required for clipping and when unfilled tris are
110 * active, otherwise handled by hardware.
112 static void flatshade_tri_0( struct draw_stage
*stage
,
113 struct prim_header
*header
)
115 struct prim_header tmp
;
117 tmp
.det
= header
->det
;
118 tmp
.flags
= header
->flags
;
119 tmp
.pad
= header
->pad
;
120 tmp
.v
[0] = header
->v
[0];
121 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 0);
122 tmp
.v
[2] = dup_vert(stage
, header
->v
[2], 1);
124 copy_colors2(stage
, tmp
.v
[1], tmp
.v
[2], tmp
.v
[0]);
126 stage
->next
->tri( stage
->next
, &tmp
);
130 static void flatshade_tri_2( struct draw_stage
*stage
,
131 struct prim_header
*header
)
133 struct prim_header tmp
;
135 tmp
.det
= header
->det
;
136 tmp
.flags
= header
->flags
;
137 tmp
.pad
= header
->pad
;
138 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
139 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 1);
140 tmp
.v
[2] = header
->v
[2];
142 copy_colors2(stage
, tmp
.v
[0], tmp
.v
[1], tmp
.v
[2]);
144 stage
->next
->tri( stage
->next
, &tmp
);
152 * Flatshade line. Required for clipping.
154 static void flatshade_line_0( struct draw_stage
*stage
,
155 struct prim_header
*header
)
157 struct prim_header tmp
;
159 tmp
.v
[0] = header
->v
[0];
160 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 0);
162 copy_colors(stage
, tmp
.v
[1], tmp
.v
[0]);
164 stage
->next
->line( stage
->next
, &tmp
);
167 static void flatshade_line_1( struct draw_stage
*stage
,
168 struct prim_header
*header
)
170 struct prim_header tmp
;
172 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
173 tmp
.v
[1] = header
->v
[1];
175 copy_colors(stage
, tmp
.v
[0], tmp
.v
[1]);
177 stage
->next
->line( stage
->next
, &tmp
);
183 static void flatshade_init_state( struct draw_stage
*stage
)
185 struct flat_stage
*flat
= flat_stage(stage
);
186 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
189 /* Find which vertex shader outputs are colors, make a list */
190 flat
->num_color_attribs
= 0;
191 flat
->num_spec_attribs
= 0;
192 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
193 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
||
194 vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_BCOLOR
) {
195 if (vs
->info
.output_semantic_index
[i
] == 0)
196 flat
->color_attribs
[flat
->num_color_attribs
++] = i
;
198 flat
->spec_attribs
[flat
->num_spec_attribs
++] = i
;
202 /* Choose flatshade routine according to provoking vertex:
204 if (stage
->draw
->rasterizer
->flatshade_first
) {
205 stage
->line
= flatshade_line_0
;
206 stage
->tri
= flatshade_tri_0
;
209 stage
->line
= flatshade_line_1
;
210 stage
->tri
= flatshade_tri_2
;
214 static void flatshade_first_tri( struct draw_stage
*stage
,
215 struct prim_header
*header
)
217 flatshade_init_state( stage
);
218 stage
->tri( stage
, header
);
221 static void flatshade_first_line( struct draw_stage
*stage
,
222 struct prim_header
*header
)
224 flatshade_init_state( stage
);
225 stage
->line( stage
, header
);
229 static void flatshade_flush( struct draw_stage
*stage
,
232 stage
->tri
= flatshade_first_tri
;
233 stage
->line
= flatshade_first_line
;
234 stage
->next
->flush( stage
->next
, flags
);
238 static void flatshade_reset_stipple_counter( struct draw_stage
*stage
)
240 stage
->next
->reset_stipple_counter( stage
->next
);
244 static void flatshade_destroy( struct draw_stage
*stage
)
246 draw_free_temp_verts( stage
);
252 * Create flatshading drawing stage.
254 struct draw_stage
*draw_flatshade_stage( struct draw_context
*draw
)
256 struct flat_stage
*flatshade
= CALLOC_STRUCT(flat_stage
);
257 if (flatshade
== NULL
)
260 flatshade
->stage
.draw
= draw
;
261 flatshade
->stage
.name
= "flatshade";
262 flatshade
->stage
.next
= NULL
;
263 flatshade
->stage
.point
= draw_pipe_passthrough_point
;
264 flatshade
->stage
.line
= flatshade_first_line
;
265 flatshade
->stage
.tri
= flatshade_first_tri
;
266 flatshade
->stage
.flush
= flatshade_flush
;
267 flatshade
->stage
.reset_stipple_counter
= flatshade_reset_stipple_counter
;
268 flatshade
->stage
.destroy
= flatshade_destroy
;
270 if (!draw_alloc_temp_verts( &flatshade
->stage
, 2 ))
273 return &flatshade
->stage
;
277 flatshade
->stage
.destroy( &flatshade
->stage
);