draw: don't assume vertex position is in data[0]
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_wide_line.c
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_util.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "draw_private.h"
35 #include "draw_pipe.h"
36
37
38 struct wideline_stage {
39 struct draw_stage stage;
40
41 float half_line_width;
42 };
43
44
45
46 static INLINE struct wideline_stage *wideline_stage( struct draw_stage *stage )
47 {
48 return (struct wideline_stage *)stage;
49 }
50
51
52
53 /**
54 * Draw a wide line by drawing a quad (two triangles).
55 * XXX need to disable polygon stipple.
56 */
57 static void wideline_line( struct draw_stage *stage,
58 struct prim_header *header )
59 {
60 /*const struct wideline_stage *wide = wideline_stage(stage);*/
61 const unsigned pos = stage->draw->vs.position_output;
62 const float half_width = 0.5f * stage->draw->rasterizer->line_width;
63
64 struct prim_header tri;
65
66 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
67 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
68 struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);
69 struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);
70
71 float *pos0 = v0->data[pos];
72 float *pos1 = v1->data[pos];
73 float *pos2 = v2->data[pos];
74 float *pos3 = v3->data[pos];
75
76 const float dx = FABSF(pos0[0] - pos2[0]);
77 const float dy = FABSF(pos0[1] - pos2[1]);
78
79 /* small tweak to meet GL specification */
80 const float bias = 0.125f;
81
82 /*
83 * Draw wide line as a quad (two tris) by "stretching" the line along
84 * X or Y.
85 * We need to tweak coords in several ways to be conformant here.
86 */
87
88 if (dx > dy) {
89 /* x-major line */
90 pos0[1] = pos0[1] - half_width - bias;
91 pos1[1] = pos1[1] + half_width - bias;
92 pos2[1] = pos2[1] - half_width - bias;
93 pos3[1] = pos3[1] + half_width - bias;
94 if (pos0[0] < pos2[0]) {
95 /* left to right line */
96 pos0[0] -= 0.5f;
97 pos1[0] -= 0.5f;
98 pos2[0] -= 0.5f;
99 pos3[0] -= 0.5f;
100 }
101 else {
102 /* right to left line */
103 pos0[0] += 0.5f;
104 pos1[0] += 0.5f;
105 pos2[0] += 0.5f;
106 pos3[0] += 0.5f;
107 }
108 }
109 else {
110 /* y-major line */
111 pos0[0] = pos0[0] - half_width + bias;
112 pos1[0] = pos1[0] + half_width + bias;
113 pos2[0] = pos2[0] - half_width + bias;
114 pos3[0] = pos3[0] + half_width + bias;
115 if (pos0[1] < pos2[1]) {
116 /* top to bottom line */
117 pos0[1] -= 0.5f;
118 pos1[1] -= 0.5f;
119 pos2[1] -= 0.5f;
120 pos3[1] -= 0.5f;
121 }
122 else {
123 /* bottom to top line */
124 pos0[1] += 0.5f;
125 pos1[1] += 0.5f;
126 pos2[1] += 0.5f;
127 pos3[1] += 0.5f;
128 }
129 }
130
131 tri.det = header->det; /* only the sign matters */
132 tri.v[0] = v0;
133 tri.v[1] = v2;
134 tri.v[2] = v3;
135 stage->next->tri( stage->next, &tri );
136
137 tri.v[0] = v0;
138 tri.v[1] = v3;
139 tri.v[2] = v1;
140 stage->next->tri( stage->next, &tri );
141 }
142
143
144 static void wideline_flush( struct draw_stage *stage, unsigned flags )
145 {
146 stage->next->flush( stage->next, flags );
147 }
148
149
150 static void wideline_reset_stipple_counter( struct draw_stage *stage )
151 {
152 stage->next->reset_stipple_counter( stage->next );
153 }
154
155
156 static void wideline_destroy( struct draw_stage *stage )
157 {
158 draw_free_temp_verts( stage );
159 FREE( stage );
160 }
161
162
163 struct draw_stage *draw_wide_line_stage( struct draw_context *draw )
164 {
165 struct wideline_stage *wide = CALLOC_STRUCT(wideline_stage);
166
167 draw_alloc_temp_verts( &wide->stage, 4 );
168
169 wide->stage.draw = draw;
170 wide->stage.next = NULL;
171 wide->stage.point = draw_pipe_passthrough_point;
172 wide->stage.line = wideline_line;
173 wide->stage.tri = draw_pipe_passthrough_tri;
174 wide->stage.flush = wideline_flush;
175 wide->stage.reset_stipple_counter = wideline_reset_stipple_counter;
176 wide->stage.destroy = wideline_destroy;
177
178 return &wide->stage;
179 }