1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
31 #include "pipe/p_context.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "draw_private.h"
37 #include "draw_pipe.h"
40 struct wideline_stage
{
41 struct draw_stage stage
;
46 * Draw a wide line by drawing a quad (two triangles).
48 static void wideline_line( struct draw_stage
*stage
,
49 struct prim_header
*header
)
51 const unsigned pos
= draw_current_shader_position_output(stage
->draw
);
52 const float half_width
= 0.5f
* stage
->draw
->rasterizer
->line_width
;
54 struct prim_header tri
;
56 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
57 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
58 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[1], 2);
59 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[1], 3);
61 float *pos0
= v0
->data
[pos
];
62 float *pos1
= v1
->data
[pos
];
63 float *pos2
= v2
->data
[pos
];
64 float *pos3
= v3
->data
[pos
];
66 const float dx
= fabsf(pos0
[0] - pos2
[0]);
67 const float dy
= fabsf(pos0
[1] - pos2
[1]);
69 const boolean half_pixel_center
=
70 stage
->draw
->rasterizer
->half_pixel_center
;
72 /* small tweak to meet GL specification */
73 const float bias
= half_pixel_center
? 0.125f
: 0.0f
;
76 * Draw wide line as a quad (two tris) by "stretching" the line along
78 * We need to tweak coords in several ways to be conformant here.
83 pos0
[1] = pos0
[1] - half_width
- bias
;
84 pos1
[1] = pos1
[1] + half_width
- bias
;
85 pos2
[1] = pos2
[1] - half_width
- bias
;
86 pos3
[1] = pos3
[1] + half_width
- bias
;
87 if (half_pixel_center
) {
88 if (pos0
[0] < pos2
[0]) {
89 /* left to right line */
96 /* right to left line */
106 pos0
[0] = pos0
[0] - half_width
+ bias
;
107 pos1
[0] = pos1
[0] + half_width
+ bias
;
108 pos2
[0] = pos2
[0] - half_width
+ bias
;
109 pos3
[0] = pos3
[0] + half_width
+ bias
;
110 if (half_pixel_center
) {
111 if (pos0
[1] < pos2
[1]) {
112 /* top to bottom line */
119 /* bottom to top line */
128 tri
.det
= header
->det
; /* only the sign matters */
132 stage
->next
->tri( stage
->next
, &tri
);
137 stage
->next
->tri( stage
->next
, &tri
);
141 static void wideline_first_line( struct draw_stage
*stage
,
142 struct prim_header
*header
)
144 struct draw_context
*draw
= stage
->draw
;
145 struct pipe_context
*pipe
= draw
->pipe
;
146 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
149 /* Disable triangle culling, stippling, unfilled mode etc. */
150 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
151 draw
->suspend_flushing
= TRUE
;
152 pipe
->bind_rasterizer_state(pipe
, r
);
153 draw
->suspend_flushing
= FALSE
;
155 stage
->line
= wideline_line
;
157 wideline_line(stage
, header
);
161 static void wideline_flush( struct draw_stage
*stage
, unsigned flags
)
163 struct draw_context
*draw
= stage
->draw
;
164 struct pipe_context
*pipe
= draw
->pipe
;
166 stage
->line
= wideline_first_line
;
167 stage
->next
->flush( stage
->next
, flags
);
169 /* restore original rasterizer state */
170 if (draw
->rast_handle
) {
171 draw
->suspend_flushing
= TRUE
;
172 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
173 draw
->suspend_flushing
= FALSE
;
178 static void wideline_reset_stipple_counter( struct draw_stage
*stage
)
180 stage
->next
->reset_stipple_counter( stage
->next
);
184 static void wideline_destroy( struct draw_stage
*stage
)
186 draw_free_temp_verts( stage
);
191 struct draw_stage
*draw_wide_line_stage( struct draw_context
*draw
)
193 struct wideline_stage
*wide
= CALLOC_STRUCT(wideline_stage
);
197 wide
->stage
.draw
= draw
;
198 wide
->stage
.name
= "wide-line";
199 wide
->stage
.next
= NULL
;
200 wide
->stage
.point
= draw_pipe_passthrough_point
;
201 wide
->stage
.line
= wideline_first_line
;
202 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
203 wide
->stage
.flush
= wideline_flush
;
204 wide
->stage
.reset_stipple_counter
= wideline_reset_stipple_counter
;
205 wide
->stage
.destroy
= wideline_destroy
;
207 if (!draw_alloc_temp_verts( &wide
->stage
, 4 ))
214 wide
->stage
.destroy( &wide
->stage
);