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