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 /* Implement line stipple by cutting lines up into smaller lines.
32 * There are hundreds of ways to implement line stipple, this is one
33 * choice that should work in all situations, requires no state
34 * manipulations, but with a penalty in terms of large amounts of
39 #include "pipe/p_util.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "draw_pipe.h"
45 /** Subclass of draw_stage */
46 struct stipple_stage
{
47 struct draw_stage stage
;
54 static INLINE
struct stipple_stage
*
55 stipple_stage(struct draw_stage
*stage
)
57 return (struct stipple_stage
*) stage
;
62 * Compute interpolated vertex attributes for 'dst' at position 't'
63 * between 'v0' and 'v1'.
64 * XXX using linear interpolation for all attribs at this time.
67 screen_interp( struct draw_context
*draw
,
68 struct vertex_header
*dst
,
70 const struct vertex_header
*v0
,
71 const struct vertex_header
*v1
)
74 for (attr
= 0; attr
< draw
->vs
.num_vs_outputs
; attr
++) {
75 const float *val0
= v0
->data
[attr
];
76 const float *val1
= v1
->data
[attr
];
77 float *newv
= dst
->data
[attr
];
79 for (i
= 0; i
< 4; i
++) {
80 newv
[i
] = val0
[i
] + t
* (val1
[i
] - val0
[i
]);
87 emit_segment(struct draw_stage
*stage
, struct prim_header
*header
,
90 struct vertex_header
*v0new
= dup_vert(stage
, header
->v
[0], 0);
91 struct vertex_header
*v1new
= dup_vert(stage
, header
->v
[1], 1);
92 struct prim_header newprim
= *header
;
95 screen_interp( stage
->draw
, v0new
, t0
, header
->v
[0], header
->v
[1] );
100 screen_interp( stage
->draw
, v1new
, t1
, header
->v
[0], header
->v
[1] );
101 newprim
.v
[1] = v1new
;
104 stage
->next
->line( stage
->next
, &newprim
);
108 static INLINE
unsigned
109 stipple_test(int counter
, ushort pattern
, int factor
)
111 int b
= (counter
/ factor
) & 0xf;
112 return (1 << b
) & pattern
;
117 stipple_line(struct draw_stage
*stage
, struct prim_header
*header
)
119 struct stipple_stage
*stipple
= stipple_stage(stage
);
120 struct vertex_header
*v0
= header
->v
[0];
121 struct vertex_header
*v1
= header
->v
[1];
122 const float *pos0
= v0
->data
[0];
123 const float *pos1
= v1
->data
[0];
132 float dx
= x0
> x1
? x0
- x1
: x1
- x0
;
133 float dy
= y0
> y1
? y0
- y1
: y1
- y0
;
135 float length
= MAX2(dx
, dy
);
138 if (header
->flags
& DRAW_PIPE_RESET_STIPPLE
)
139 stipple
->counter
= 0;
142 /* XXX ToDo: intead of iterating pixel-by-pixel, use a look-up table.
144 for (i
= 0; i
< length
; i
++) {
145 int result
= stipple_test( (int) stipple
->counter
+i
,
146 (ushort
) stipple
->pattern
, stipple
->factor
);
147 if (result
!= state
) {
148 /* changing from "off" to "on" or vice versa */
151 /* finishing an "on" segment */
152 emit_segment( stage
, header
, start
/ length
, i
/ length
);
156 /* starting an "on" segment */
163 if (state
&& start
< length
)
164 emit_segment( stage
, header
, start
/ length
, 1.0 );
166 stipple
->counter
+= length
;
171 reset_stipple_counter(struct draw_stage
*stage
)
173 struct stipple_stage
*stipple
= stipple_stage(stage
);
174 stipple
->counter
= 0;
175 stage
->next
->reset_stipple_counter( stage
->next
);
179 stipple_reset_point(struct draw_stage
*stage
, struct prim_header
*header
)
181 struct stipple_stage
*stipple
= stipple_stage(stage
);
182 stipple
->counter
= 0;
183 stage
->next
->point(stage
->next
, header
);
187 stipple_reset_tri(struct draw_stage
*stage
, struct prim_header
*header
)
189 struct stipple_stage
*stipple
= stipple_stage(stage
);
190 stipple
->counter
= 0;
191 stage
->next
->tri(stage
->next
, header
);
196 stipple_first_line(struct draw_stage
*stage
,
197 struct prim_header
*header
)
199 struct stipple_stage
*stipple
= stipple_stage(stage
);
200 struct draw_context
*draw
= stage
->draw
;
202 stipple
->pattern
= draw
->rasterizer
->line_stipple_pattern
;
203 stipple
->factor
= draw
->rasterizer
->line_stipple_factor
+ 1;
205 stage
->line
= stipple_line
;
206 stage
->line( stage
, header
);
211 stipple_flush(struct draw_stage
*stage
, unsigned flags
)
213 stage
->line
= stipple_first_line
;
214 stage
->next
->flush( stage
->next
, flags
);
221 stipple_destroy( struct draw_stage
*stage
)
223 draw_free_temp_verts( stage
);
229 * Create line stippler stage
231 struct draw_stage
*draw_stipple_stage( struct draw_context
*draw
)
233 struct stipple_stage
*stipple
= CALLOC_STRUCT(stipple_stage
);
235 draw_alloc_temp_verts( &stipple
->stage
, 2 );
237 stipple
->stage
.draw
= draw
;
238 stipple
->stage
.next
= NULL
;
239 stipple
->stage
.point
= stipple_reset_point
;
240 stipple
->stage
.line
= stipple_first_line
;
241 stipple
->stage
.tri
= stipple_reset_tri
;
242 stipple
->stage
.reset_stipple_counter
= reset_stipple_counter
;
243 stipple
->stage
.flush
= stipple_flush
;
244 stipple
->stage
.destroy
= stipple_destroy
;
246 return &stipple
->stage
;