655774b155f5a0cc18dce621f044390cdff498c8
[mesa.git] / src / gallium / aux / draw / draw_wide_prims.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
36
37 struct wide_stage {
38 struct draw_stage stage;
39
40 float half_line_width;
41 float half_point_size;
42
43 uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
44 uint texcoord_mode[PIPE_MAX_SHADER_OUTPUTS];
45 uint num_texcoords;
46
47 int psize_slot;
48 };
49
50
51
52 static INLINE struct wide_stage *wide_stage( struct draw_stage *stage )
53 {
54 return (struct wide_stage *)stage;
55 }
56
57
58 static void passthrough_point( struct draw_stage *stage,
59 struct prim_header *header )
60 {
61 stage->next->point( stage->next, header );
62 }
63
64 static void passthrough_line( struct draw_stage *stage,
65 struct prim_header *header )
66 {
67 stage->next->line(stage->next, header);
68 }
69
70 static void passthrough_tri( struct draw_stage *stage,
71 struct prim_header *header )
72 {
73 stage->next->tri(stage->next, header);
74 }
75
76
77 /**
78 * Draw a wide line by drawing a quad (two triangles).
79 * XXX need to disable polygon stipple.
80 */
81 static void wide_line( struct draw_stage *stage,
82 struct prim_header *header )
83 {
84 const struct wide_stage *wide = wide_stage(stage);
85 const float half_width = wide->half_line_width;
86
87 struct prim_header tri;
88
89 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
90 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
91 struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);
92 struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);
93
94 float *pos0 = v0->data[0];
95 float *pos1 = v1->data[0];
96 float *pos2 = v2->data[0];
97 float *pos3 = v3->data[0];
98
99 const float dx = FABSF(pos0[0] - pos2[0]);
100 const float dy = FABSF(pos0[1] - pos2[1]);
101
102 /*
103 * Draw wide line as a quad (two tris) by "stretching" the line along
104 * X or Y.
105 * We need to tweak coords in several ways to be conformant here.
106 */
107
108 if (dx > dy) {
109 /* x-major line */
110 pos0[1] = pos0[1] - half_width - 0.25f;
111 pos1[1] = pos1[1] + half_width - 0.25f;
112 pos2[1] = pos2[1] - half_width - 0.25f;
113 pos3[1] = pos3[1] + half_width - 0.25f;
114 if (pos0[0] < pos2[0]) {
115 /* left to right line */
116 pos0[0] -= 0.5f;
117 pos1[0] -= 0.5f;
118 pos2[0] -= 0.5f;
119 pos3[0] -= 0.5f;
120 }
121 else {
122 /* right to left line */
123 pos0[0] += 0.5f;
124 pos1[0] += 0.5f;
125 pos2[0] += 0.5f;
126 pos3[0] += 0.5f;
127 }
128 }
129 else {
130 /* y-major line */
131 pos0[0] = pos0[0] - half_width + 0.25f;
132 pos1[0] = pos1[0] + half_width + 0.25f;
133 pos2[0] = pos2[0] - half_width + 0.25f;
134 pos3[0] = pos3[0] + half_width + 0.25f;
135 if (pos0[1] < pos2[1]) {
136 /* top to bottom line */
137 pos0[1] -= 0.5f;
138 pos1[1] -= 0.5f;
139 pos2[1] -= 0.5f;
140 pos3[1] -= 0.5f;
141 }
142 else {
143 /* bottom to top line */
144 pos0[1] += 0.5f;
145 pos1[1] += 0.5f;
146 pos2[1] += 0.5f;
147 pos3[1] += 0.5f;
148 }
149 }
150
151 tri.det = header->det; /* only the sign matters */
152 tri.v[0] = v0;
153 tri.v[1] = v2;
154 tri.v[2] = v3;
155 stage->next->tri( stage->next, &tri );
156
157 tri.v[0] = v0;
158 tri.v[1] = v3;
159 tri.v[2] = v1;
160 stage->next->tri( stage->next, &tri );
161 }
162
163
164 /**
165 * Draw a wide line by drawing a quad, using geometry which will
166 * fullfill GL's antialiased line requirements.
167 */
168 static void wide_line_aa(struct draw_stage *stage,
169 struct prim_header *header)
170 {
171 const struct wide_stage *wide = wide_stage(stage);
172 const float half_width = wide->half_line_width;
173 struct prim_header tri;
174 struct vertex_header *v[4];
175 float *pos;
176 float dx = header->v[1]->data[0][0] - header->v[0]->data[0][0];
177 float dy = header->v[1]->data[0][1] - header->v[0]->data[0][1];
178 const float len = (float) sqrt(dx * dx + dy * dy);
179 uint i;
180
181 dx = dx * half_width / len;
182 dy = dy * half_width / len;
183
184 /* allocate/dup new verts */
185 for (i = 0; i < 4; i++) {
186 v[i] = dup_vert(stage, header->v[i/2], i);
187 }
188
189 /*
190 * Quad for line from v0 to v1:
191 *
192 * 1 3
193 * +-------------------------+
194 * | |
195 * *v0 v1*
196 * | |
197 * +-------------------------+
198 * 0 2
199 */
200
201 pos = v[0]->data[0];
202 pos[0] += dy;
203 pos[1] -= dx;
204
205 pos = v[1]->data[0];
206 pos[0] -= dy;
207 pos[1] += dx;
208
209 pos = v[2]->data[0];
210 pos[0] += dy;
211 pos[1] -= dx;
212
213 pos = v[3]->data[0];
214 pos[0] -= dy;
215 pos[1] += dx;
216
217 tri.det = header->det; /* only the sign matters */
218
219 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
220 stage->next->tri( stage->next, &tri );
221
222 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
223 stage->next->tri( stage->next, &tri );
224
225 }
226
227
228 /**
229 * Set the vertex texcoords for sprite mode.
230 * Coords may be left untouched or set to a right-side-up or upside-down
231 * orientation.
232 */
233 static void set_texcoords(const struct wide_stage *wide,
234 struct vertex_header *v, const float tc[4])
235 {
236 uint i;
237 for (i = 0; i < wide->num_texcoords; i++) {
238 if (wide->texcoord_mode[i] != PIPE_SPRITE_COORD_NONE) {
239 uint j = wide->texcoord_slot[i];
240 v->data[j][0] = tc[0];
241 if (wide->texcoord_mode[i] == PIPE_SPRITE_COORD_LOWER_LEFT)
242 v->data[j][1] = 1.0f - tc[1];
243 else
244 v->data[j][1] = tc[1];
245 v->data[j][2] = tc[2];
246 v->data[j][3] = tc[3];
247 }
248 }
249 }
250
251
252 /* If there are lots of sprite points (and why wouldn't there be?) it
253 * would probably be more sensible to change hardware setup to
254 * optimize this rather than doing the whole thing in software like
255 * this.
256 */
257 static void wide_point( struct draw_stage *stage,
258 struct prim_header *header )
259 {
260 const struct wide_stage *wide = wide_stage(stage);
261 const boolean sprite = (boolean) stage->draw->rasterizer->point_sprite;
262 float half_size;
263 float left_adj, right_adj;
264
265 struct prim_header tri;
266
267 /* four dups of original vertex */
268 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
269 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
270 struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
271 struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
272
273 float *pos0 = v0->data[0];
274 float *pos1 = v1->data[0];
275 float *pos2 = v2->data[0];
276 float *pos3 = v3->data[0];
277
278 /* point size is either per-vertex or fixed size */
279 if (wide->psize_slot >= 0) {
280 half_size = 0.5f * header->v[0]->data[wide->psize_slot][0];
281 }
282 else {
283 half_size = wide->half_point_size;
284 }
285
286 left_adj = -half_size + 0.25f;
287 right_adj = half_size + 0.25f;
288
289 pos0[0] += left_adj;
290 pos0[1] -= half_size;
291
292 pos1[0] += left_adj;
293 pos1[1] += half_size;
294
295 pos2[0] += right_adj;
296 pos2[1] -= half_size;
297
298 pos3[0] += right_adj;
299 pos3[1] += half_size;
300
301 if (sprite) {
302 static const float tex00[4] = { 0, 0, 0, 1 };
303 static const float tex01[4] = { 0, 1, 0, 1 };
304 static const float tex11[4] = { 1, 1, 0, 1 };
305 static const float tex10[4] = { 1, 0, 0, 1 };
306 set_texcoords( wide, v0, tex00 );
307 set_texcoords( wide, v1, tex01 );
308 set_texcoords( wide, v2, tex10 );
309 set_texcoords( wide, v3, tex11 );
310 }
311
312 tri.det = header->det; /* only the sign matters */
313 tri.v[0] = v0;
314 tri.v[1] = v2;
315 tri.v[2] = v3;
316 stage->next->tri( stage->next, &tri );
317
318 tri.v[0] = v0;
319 tri.v[1] = v3;
320 tri.v[2] = v1;
321 stage->next->tri( stage->next, &tri );
322 }
323
324
325 static void wide_first_point( struct draw_stage *stage,
326 struct prim_header *header )
327 {
328 struct wide_stage *wide = wide_stage(stage);
329 struct draw_context *draw = stage->draw;
330
331 wide->half_point_size = 0.5f * draw->rasterizer->point_size;
332
333 if (draw->rasterizer->point_size != 1.0) {
334 stage->point = wide_point;
335 }
336 else {
337 stage->point = passthrough_point;
338 }
339
340 if (draw->rasterizer->point_sprite) {
341 /* find vertex shader texcoord outputs */
342 const struct draw_vertex_shader *vs = draw->vertex_shader;
343 uint i, j = 0;
344 for (i = 0; i < vs->state->num_outputs; i++) {
345 if (vs->state->output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
346 wide->texcoord_slot[j] = i;
347 wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
348 j++;
349 }
350 }
351 wide->num_texcoords = j;
352 }
353
354 wide->psize_slot = -1;
355
356 if (draw->rasterizer->point_size_per_vertex) {
357 /* find PSIZ vertex output */
358 const struct draw_vertex_shader *vs = draw->vertex_shader;
359 uint i;
360 for (i = 0; i < vs->state->num_outputs; i++) {
361 if (vs->state->output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
362 wide->psize_slot = i;
363 break;
364 }
365 }
366 }
367
368 stage->point( stage, header );
369 }
370
371
372
373 static void wide_first_line( struct draw_stage *stage,
374 struct prim_header *header )
375 {
376 struct wide_stage *wide = wide_stage(stage);
377 struct draw_context *draw = stage->draw;
378
379 wide->half_line_width = 0.5f * draw->rasterizer->line_width;
380
381 if (draw->rasterizer->line_width != 1.0) {
382 if (draw->rasterizer->line_smooth)
383 wide->stage.line = wide_line_aa;
384 else
385 wide->stage.line = wide_line;
386 }
387 else {
388 wide->stage.line = passthrough_line;
389 }
390
391 stage->line( stage, header );
392 }
393
394
395 static void wide_flush( struct draw_stage *stage, unsigned flags )
396 {
397 stage->line = wide_first_line;
398 stage->point = wide_first_point;
399 stage->next->flush( stage->next, flags );
400 }
401
402
403 static void wide_reset_stipple_counter( struct draw_stage *stage )
404 {
405 stage->next->reset_stipple_counter( stage->next );
406 }
407
408
409 static void wide_destroy( struct draw_stage *stage )
410 {
411 draw_free_temp_verts( stage );
412 FREE( stage );
413 }
414
415
416 struct draw_stage *draw_wide_stage( struct draw_context *draw )
417 {
418 struct wide_stage *wide = CALLOC_STRUCT(wide_stage);
419
420 draw_alloc_temp_verts( &wide->stage, 4 );
421
422 wide->stage.draw = draw;
423 wide->stage.next = NULL;
424 wide->stage.point = wide_first_point;
425 wide->stage.line = wide_first_line;
426 wide->stage.tri = passthrough_tri;
427 wide->stage.flush = wide_flush;
428 wide->stage.reset_stipple_counter = wide_reset_stipple_counter;
429 wide->stage.destroy = wide_destroy;
430
431 return &wide->stage;
432 }