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_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
;
43 float half_line_width
;
48 static INLINE
struct wideline_stage
*wideline_stage( struct draw_stage
*stage
)
50 return (struct wideline_stage
*)stage
;
56 * Draw a wide line by drawing a quad (two triangles).
57 * XXX need to disable polygon stipple.
59 static void wideline_line( struct draw_stage
*stage
,
60 struct prim_header
*header
)
62 /*const struct wideline_stage *wide = wideline_stage(stage);*/
63 const unsigned pos
= draw_current_shader_position_output(stage
->draw
);
64 const float half_width
= 0.5f
* stage
->draw
->rasterizer
->line_width
;
66 struct prim_header tri
;
68 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
69 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
70 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[1], 2);
71 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[1], 3);
73 float *pos0
= v0
->data
[pos
];
74 float *pos1
= v1
->data
[pos
];
75 float *pos2
= v2
->data
[pos
];
76 float *pos3
= v3
->data
[pos
];
78 const float dx
= fabsf(pos0
[0] - pos2
[0]);
79 const float dy
= fabsf(pos0
[1] - pos2
[1]);
81 /* small tweak to meet GL specification */
82 const float bias
= 0.125f
;
85 * Draw wide line as a quad (two tris) by "stretching" the line along
87 * We need to tweak coords in several ways to be conformant here.
92 pos0
[1] = pos0
[1] - half_width
- bias
;
93 pos1
[1] = pos1
[1] + half_width
- bias
;
94 pos2
[1] = pos2
[1] - half_width
- bias
;
95 pos3
[1] = pos3
[1] + half_width
- bias
;
96 if (pos0
[0] < pos2
[0]) {
97 /* left to right line */
104 /* right to left line */
113 pos0
[0] = pos0
[0] - half_width
+ bias
;
114 pos1
[0] = pos1
[0] + half_width
+ bias
;
115 pos2
[0] = pos2
[0] - half_width
+ bias
;
116 pos3
[0] = pos3
[0] + half_width
+ bias
;
117 if (pos0
[1] < pos2
[1]) {
118 /* top to bottom line */
125 /* bottom to top line */
133 tri
.det
= header
->det
; /* only the sign matters */
137 stage
->next
->tri( stage
->next
, &tri
);
142 stage
->next
->tri( stage
->next
, &tri
);
146 static void wideline_first_line( struct draw_stage
*stage
,
147 struct prim_header
*header
)
149 struct draw_context
*draw
= stage
->draw
;
150 struct pipe_context
*pipe
= draw
->pipe
;
151 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
154 /* Disable triangle culling, stippling, unfilled mode etc. */
155 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
156 draw
->suspend_flushing
= TRUE
;
157 pipe
->bind_rasterizer_state(pipe
, r
);
158 draw
->suspend_flushing
= FALSE
;
160 stage
->line
= wideline_line
;
162 wideline_line(stage
, header
);
166 static void wideline_flush( struct draw_stage
*stage
, unsigned flags
)
168 struct draw_context
*draw
= stage
->draw
;
169 struct pipe_context
*pipe
= draw
->pipe
;
171 stage
->line
= wideline_first_line
;
172 stage
->next
->flush( stage
->next
, flags
);
174 /* restore original rasterizer state */
175 if (draw
->rast_handle
) {
176 draw
->suspend_flushing
= TRUE
;
177 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
178 draw
->suspend_flushing
= FALSE
;
183 static void wideline_reset_stipple_counter( struct draw_stage
*stage
)
185 stage
->next
->reset_stipple_counter( stage
->next
);
189 static void wideline_destroy( struct draw_stage
*stage
)
191 draw_free_temp_verts( stage
);
196 struct draw_stage
*draw_wide_line_stage( struct draw_context
*draw
)
198 struct wideline_stage
*wide
= CALLOC_STRUCT(wideline_stage
);
200 draw_alloc_temp_verts( &wide
->stage
, 4 );
202 wide
->stage
.draw
= draw
;
203 wide
->stage
.name
= "wide-line";
204 wide
->stage
.next
= NULL
;
205 wide
->stage
.point
= draw_pipe_passthrough_point
;
206 wide
->stage
.line
= wideline_first_line
;
207 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
208 wide
->stage
.flush
= wideline_flush
;
209 wide
->stage
.reset_stipple_counter
= wideline_reset_stipple_counter
;
210 wide
->stage
.destroy
= wideline_destroy
;