draw_pipe_flatshade.c revision cddeca51adf0d2b736a223e47b60f6ef3be85bff
18e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell/**************************************************************************
28e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
38e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
48e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * All Rights Reserved.
58e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
68e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
78e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * copy of this software and associated documentation files (the
88e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * "Software"), to deal in the Software without restriction, including
98e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
108e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
118e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * permit persons to whom the Software is furnished to do so, subject to
128e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * the following conditions:
138e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
148e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * The above copyright notice and this permission notice (including the
158e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * next paragraph) shall be included in all copies or substantial portions
168e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * of the Software.
178e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
188e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
198e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
208e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
218e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
228e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
238e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
248e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
258e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
268e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell **************************************************************************/
278e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
288e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell/* Authors:  Keith Whitwell <keith@tungstengraphics.com>
298e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell */
308e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
313fc926f3740da9ec27853d158243055f3cb43d43Brian#include "pipe/p_util.h"
32cd3643698eafa0869a8317b002e5b066de0172e7Brian#include "pipe/p_shader_tokens.h"
33279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian#include "draw_private.h"
348e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
358e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
36cd3643698eafa0869a8317b002e5b066de0172e7Brian/** subclass of draw_stage */
37cd3643698eafa0869a8317b002e5b066de0172e7Brianstruct flat_stage
388e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
39cd3643698eafa0869a8317b002e5b066de0172e7Brian   struct draw_stage stage;
40cd3643698eafa0869a8317b002e5b066de0172e7Brian
41cd3643698eafa0869a8317b002e5b066de0172e7Brian   uint num_color_attribs;
42cd3643698eafa0869a8317b002e5b066de0172e7Brian   uint color_attribs[4];  /* front/back primary/secondary colors */
43cd3643698eafa0869a8317b002e5b066de0172e7Brian};
448e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
458e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
46cd3643698eafa0869a8317b002e5b066de0172e7Brianstatic INLINE struct flat_stage *
47cd3643698eafa0869a8317b002e5b066de0172e7Brianflat_stage(struct draw_stage *stage)
48cd3643698eafa0869a8317b002e5b066de0172e7Brian{
49cd3643698eafa0869a8317b002e5b066de0172e7Brian   return (struct flat_stage *) stage;
50cd3643698eafa0869a8317b002e5b066de0172e7Brian}
51cd3643698eafa0869a8317b002e5b066de0172e7Brian
528e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
53cd3643698eafa0869a8317b002e5b066de0172e7Brian/** Copy all the color attributes from 'src' vertex to 'dst' vertex */
54a148cc51fbe56266199057cfde2abb2b59eb8713Brianstatic INLINE void copy_colors( struct draw_stage *stage,
55a148cc51fbe56266199057cfde2abb2b59eb8713Brian                                struct vertex_header *dst,
56279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian                                const struct vertex_header *src )
578e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
58cd3643698eafa0869a8317b002e5b066de0172e7Brian   const struct flat_stage *flat = flat_stage(stage);
59942b9bc5bc13d959baa86779a7c669cf96659b9aBrian   uint i;
60a148cc51fbe56266199057cfde2abb2b59eb8713Brian   for (i = 0; i < flat->num_color_attribs; i++) {
61a148cc51fbe56266199057cfde2abb2b59eb8713Brian      const uint attr = flat->color_attribs[i];
62a148cc51fbe56266199057cfde2abb2b59eb8713Brian      COPY_4FV(dst->data[attr], src->data[attr]);
63a148cc51fbe56266199057cfde2abb2b59eb8713Brian   }
64a148cc51fbe56266199057cfde2abb2b59eb8713Brian}
65942b9bc5bc13d959baa86779a7c669cf96659b9aBrian
66a148cc51fbe56266199057cfde2abb2b59eb8713Brian
67a148cc51fbe56266199057cfde2abb2b59eb8713Brian/** Copy all the color attributes from src vertex to dst0 & dst1 vertices */
68a148cc51fbe56266199057cfde2abb2b59eb8713Brianstatic INLINE void copy_colors2( struct draw_stage *stage,
69a148cc51fbe56266199057cfde2abb2b59eb8713Brian                                 struct vertex_header *dst0,
70a148cc51fbe56266199057cfde2abb2b59eb8713Brian                                 struct vertex_header *dst1,
71a148cc51fbe56266199057cfde2abb2b59eb8713Brian                                 const struct vertex_header *src )
72a148cc51fbe56266199057cfde2abb2b59eb8713Brian{
73a148cc51fbe56266199057cfde2abb2b59eb8713Brian   const struct flat_stage *flat = flat_stage(stage);
74a148cc51fbe56266199057cfde2abb2b59eb8713Brian   uint i;
75cd3643698eafa0869a8317b002e5b066de0172e7Brian   for (i = 0; i < flat->num_color_attribs; i++) {
76cd3643698eafa0869a8317b002e5b066de0172e7Brian      const uint attr = flat->color_attribs[i];
77a148cc51fbe56266199057cfde2abb2b59eb8713Brian      COPY_4FV(dst0->data[attr], src->data[attr]);
78a148cc51fbe56266199057cfde2abb2b59eb8713Brian      COPY_4FV(dst1->data[attr], src->data[attr]);
79942b9bc5bc13d959baa86779a7c669cf96659b9aBrian   }
808e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
818e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
828e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
83279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian/**
84279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian * Flatshade tri.  Required for clipping and when unfilled tris are
858e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * active, otherwise handled by hardware.
868e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell */
87ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brianstatic void flatshade_tri( struct draw_stage *stage,
888e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell			   struct prim_header *header )
898e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
908e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   struct prim_header tmp;
918e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
928e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.det = header->det;
9330236573dadd83714220b72b0c04f1bbce69fbd6Brian   tmp.edgeflags = header->edgeflags;
948e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.v[0] = dup_vert(stage, header->v[0], 0);
958e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.v[1] = dup_vert(stage, header->v[1], 1);
968e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.v[2] = header->v[2];
978e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
98a148cc51fbe56266199057cfde2abb2b59eb8713Brian   copy_colors2(stage, tmp.v[0], tmp.v[1], tmp.v[2]);
998e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1008e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   stage->next->tri( stage->next, &tmp );
1018e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
1028e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1038e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
104279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian/**
105279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian * Flatshade line.  Required for clipping.
1068e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell */
107ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brianstatic void flatshade_line( struct draw_stage *stage,
1088e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell			    struct prim_header *header )
1098e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
1108e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   struct prim_header tmp;
1118e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1128e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.v[0] = dup_vert(stage, header->v[0], 0);
1138e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   tmp.v[1] = header->v[1];
1148e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1158e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   copy_colors(stage, tmp.v[0], tmp.v[1]);
1168e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1178e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   stage->next->line( stage->next, &tmp );
1188e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
1198e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
1208e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
121ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brianstatic void flatshade_point( struct draw_stage *stage,
122ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brian                             struct prim_header *header )
1238e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
1248e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   stage->next->point( stage->next, header );
1258e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
1268e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
127279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian
1280bfd085e2866fbbd40209dcee23f0e6240583fe8Brianstatic void flatshade_init_state( struct draw_stage *stage )
1290bfd085e2866fbbd40209dcee23f0e6240583fe8Brian{
1300bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   struct flat_stage *flat = flat_stage(stage);
131cddeca51adf0d2b736a223e47b60f6ef3be85bffBrian   const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
1320bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   uint i;
1330bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1340bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   /* Find which vertex shader outputs are colors, make a list */
1350bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flat->num_color_attribs = 0;
136cddeca51adf0d2b736a223e47b60f6ef3be85bffBrian   for (i = 0; i < vs->info.num_outputs; i++) {
137cddeca51adf0d2b736a223e47b60f6ef3be85bffBrian      if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
138cddeca51adf0d2b736a223e47b60f6ef3be85bffBrian          vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
1390bfd085e2866fbbd40209dcee23f0e6240583fe8Brian         flat->color_attribs[flat->num_color_attribs++] = i;
1400bfd085e2866fbbd40209dcee23f0e6240583fe8Brian      }
1410bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   }
1420bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1430bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->line = flatshade_line;
1440bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->tri = flatshade_tri;
1450bfd085e2866fbbd40209dcee23f0e6240583fe8Brian}
1460bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1470bfd085e2866fbbd40209dcee23f0e6240583fe8Brianstatic void flatshade_first_tri( struct draw_stage *stage,
1480bfd085e2866fbbd40209dcee23f0e6240583fe8Brian				 struct prim_header *header )
1490bfd085e2866fbbd40209dcee23f0e6240583fe8Brian{
1500bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flatshade_init_state( stage );
1510bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->tri( stage, header );
1520bfd085e2866fbbd40209dcee23f0e6240583fe8Brian}
1530bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1540bfd085e2866fbbd40209dcee23f0e6240583fe8Brianstatic void flatshade_first_line( struct draw_stage *stage,
1550bfd085e2866fbbd40209dcee23f0e6240583fe8Brian				  struct prim_header *header )
1560bfd085e2866fbbd40209dcee23f0e6240583fe8Brian{
1570bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flatshade_init_state( stage );
1580bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->line( stage, header );
1590bfd085e2866fbbd40209dcee23f0e6240583fe8Brian}
1600bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1610bfd085e2866fbbd40209dcee23f0e6240583fe8Brian
1620bfd085e2866fbbd40209dcee23f0e6240583fe8Brianstatic void flatshade_flush( struct draw_stage *stage,
1630bfd085e2866fbbd40209dcee23f0e6240583fe8Brian			     unsigned flags )
1648e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
1650bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->tri = flatshade_first_tri;
1660bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->line = flatshade_first_line;
1670bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   stage->next->flush( stage->next, flags );
1688e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
1698e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
170279ffe3f163fd6a5e7bfa108db14c81acbb06eceBrian
1710360b49afbcd839f99ba0745d01cf9dc5be4d122Brianstatic void flatshade_reset_stipple_counter( struct draw_stage *stage )
1720360b49afbcd839f99ba0745d01cf9dc5be4d122Brian{
1730360b49afbcd839f99ba0745d01cf9dc5be4d122Brian   stage->next->reset_stipple_counter( stage->next );
1740360b49afbcd839f99ba0745d01cf9dc5be4d122Brian}
1750360b49afbcd839f99ba0745d01cf9dc5be4d122Brian
1760360b49afbcd839f99ba0745d01cf9dc5be4d122Brian
177d75454840672f462de933724daae24a839aac48eMichalstatic void flatshade_destroy( struct draw_stage *stage )
178d75454840672f462de933724daae24a839aac48eMichal{
179b08102a8f3ef558743f5f952c726ba2c28b6e82eBrian   draw_free_temp_verts( stage );
180d75454840672f462de933724daae24a839aac48eMichal   FREE( stage );
181d75454840672f462de933724daae24a839aac48eMichal}
182d75454840672f462de933724daae24a839aac48eMichal
183d75454840672f462de933724daae24a839aac48eMichal
184ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brian/**
185ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brian * Create flatshading drawing stage.
186ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brian */
187ea470eec86715cd2bc9aa86d36e6ea803d0d4017Brianstruct draw_stage *draw_flatshade_stage( struct draw_context *draw )
1888e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
189cd3643698eafa0869a8317b002e5b066de0172e7Brian   struct flat_stage *flatshade = CALLOC_STRUCT(flat_stage);
1908e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
191b08102a8f3ef558743f5f952c726ba2c28b6e82eBrian   draw_alloc_temp_verts( &flatshade->stage, 2 );
1928e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
193cd3643698eafa0869a8317b002e5b066de0172e7Brian   flatshade->stage.draw = draw;
194cd3643698eafa0869a8317b002e5b066de0172e7Brian   flatshade->stage.next = NULL;
195cd3643698eafa0869a8317b002e5b066de0172e7Brian   flatshade->stage.point = flatshade_point;
1960bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flatshade->stage.line = flatshade_first_line;
1970bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flatshade->stage.tri = flatshade_first_tri;
1980bfd085e2866fbbd40209dcee23f0e6240583fe8Brian   flatshade->stage.flush = flatshade_flush;
199cd3643698eafa0869a8317b002e5b066de0172e7Brian   flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;
200cd3643698eafa0869a8317b002e5b066de0172e7Brian   flatshade->stage.destroy = flatshade_destroy;
2018e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
202cd3643698eafa0869a8317b002e5b066de0172e7Brian   return &flatshade->stage;
2038e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
2048e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
2058e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
206