draw_pipe_wide_line.c revision 06441af657a1956a5b2a700a70a2b59b0488ee84
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28/* Authors:  Keith Whitwell <keith@tungstengraphics.com>
29 */
30
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"
38
39
40struct wideline_stage {
41   struct draw_stage stage;
42
43   float half_line_width;
44};
45
46
47
48static INLINE struct wideline_stage *wideline_stage( struct draw_stage *stage )
49{
50   return (struct wideline_stage *)stage;
51}
52
53
54
55/**
56 * Draw a wide line by drawing a quad (two triangles).
57 */
58static void wideline_line( struct draw_stage *stage,
59                           struct prim_header *header )
60{
61   /*const struct wideline_stage *wide = wideline_stage(stage);*/
62   const unsigned pos = draw_current_shader_position_output(stage->draw);
63   const float half_width = 0.5f * stage->draw->rasterizer->line_width;
64
65   struct prim_header tri;
66
67   struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
68   struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
69   struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);
70   struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);
71
72   float *pos0 = v0->data[pos];
73   float *pos1 = v1->data[pos];
74   float *pos2 = v2->data[pos];
75   float *pos3 = v3->data[pos];
76
77   const float dx = fabsf(pos0[0] - pos2[0]);
78   const float dy = fabsf(pos0[1] - pos2[1]);
79
80   /* small tweak to meet GL specification */
81   const float bias = 0.125f;
82
83   /*
84    * Draw wide line as a quad (two tris) by "stretching" the line along
85    * X or Y.
86    * We need to tweak coords in several ways to be conformant here.
87    */
88
89   if (dx > dy) {
90      /* x-major line */
91      pos0[1] = pos0[1] - half_width - bias;
92      pos1[1] = pos1[1] + half_width - bias;
93      pos2[1] = pos2[1] - half_width - bias;
94      pos3[1] = pos3[1] + half_width - bias;
95      if (pos0[0] < pos2[0]) {
96         /* left to right line */
97         pos0[0] -= 0.5f;
98         pos1[0] -= 0.5f;
99         pos2[0] -= 0.5f;
100         pos3[0] -= 0.5f;
101      }
102      else {
103         /* right to left line */
104         pos0[0] += 0.5f;
105         pos1[0] += 0.5f;
106         pos2[0] += 0.5f;
107         pos3[0] += 0.5f;
108      }
109   }
110   else {
111      /* y-major line */
112      pos0[0] = pos0[0] - half_width + bias;
113      pos1[0] = pos1[0] + half_width + bias;
114      pos2[0] = pos2[0] - half_width + bias;
115      pos3[0] = pos3[0] + half_width + bias;
116      if (pos0[1] < pos2[1]) {
117         /* top to bottom line */
118         pos0[1] -= 0.5f;
119         pos1[1] -= 0.5f;
120         pos2[1] -= 0.5f;
121         pos3[1] -= 0.5f;
122      }
123      else {
124         /* bottom to top line */
125         pos0[1] += 0.5f;
126         pos1[1] += 0.5f;
127         pos2[1] += 0.5f;
128         pos3[1] += 0.5f;
129      }
130   }
131
132   tri.det = header->det;  /* only the sign matters */
133   tri.v[0] = v0;
134   tri.v[1] = v2;
135   tri.v[2] = v3;
136   stage->next->tri( stage->next, &tri );
137
138   tri.v[0] = v0;
139   tri.v[1] = v3;
140   tri.v[2] = v1;
141   stage->next->tri( stage->next, &tri );
142}
143
144
145static void wideline_first_line( struct draw_stage *stage,
146                                 struct prim_header *header )
147{
148   struct draw_context *draw = stage->draw;
149   struct pipe_context *pipe = draw->pipe;
150   const struct pipe_rasterizer_state *rast = draw->rasterizer;
151   void *r;
152
153   /* Disable triangle culling, stippling, unfilled mode etc. */
154   r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
155   draw->suspend_flushing = TRUE;
156   pipe->bind_rasterizer_state(pipe, r);
157   draw->suspend_flushing = FALSE;
158
159   stage->line = wideline_line;
160
161   wideline_line(stage, header);
162}
163
164
165static void wideline_flush( struct draw_stage *stage, unsigned flags )
166{
167   struct draw_context *draw = stage->draw;
168   struct pipe_context *pipe = draw->pipe;
169
170   stage->line = wideline_first_line;
171   stage->next->flush( stage->next, flags );
172
173   /* restore original rasterizer state */
174   if (draw->rast_handle) {
175      draw->suspend_flushing = TRUE;
176      pipe->bind_rasterizer_state(pipe, draw->rast_handle);
177      draw->suspend_flushing = FALSE;
178   }
179}
180
181
182static void wideline_reset_stipple_counter( struct draw_stage *stage )
183{
184   stage->next->reset_stipple_counter( stage->next );
185}
186
187
188static void wideline_destroy( struct draw_stage *stage )
189{
190   draw_free_temp_verts( stage );
191   FREE( stage );
192}
193
194
195struct draw_stage *draw_wide_line_stage( struct draw_context *draw )
196{
197   struct wideline_stage *wide = CALLOC_STRUCT(wideline_stage);
198
199   draw_alloc_temp_verts( &wide->stage, 4 );
200
201   wide->stage.draw = draw;
202   wide->stage.name = "wide-line";
203   wide->stage.next = NULL;
204   wide->stage.point = draw_pipe_passthrough_point;
205   wide->stage.line = wideline_first_line;
206   wide->stage.tri = draw_pipe_passthrough_tri;
207   wide->stage.flush = wideline_flush;
208   wide->stage.reset_stipple_counter = wideline_reset_stipple_counter;
209   wide->stage.destroy = wideline_destroy;
210
211   return &wide->stage;
212}
213