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_util.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "draw_private.h"
38 struct draw_stage stage
;
40 float half_line_width
;
41 float half_point_size
;
43 uint texcoord_slot
[PIPE_MAX_SHADER_OUTPUTS
];
44 uint texcoord_mode
[PIPE_MAX_SHADER_OUTPUTS
];
52 static INLINE
struct wide_stage
*wide_stage( struct draw_stage
*stage
)
54 return (struct wide_stage
*)stage
;
58 static void passthrough_point( struct draw_stage
*stage
,
59 struct prim_header
*header
)
61 stage
->next
->point( stage
->next
, header
);
64 static void passthrough_line( struct draw_stage
*stage
,
65 struct prim_header
*header
)
67 stage
->next
->line(stage
->next
, header
);
70 static void passthrough_tri( struct draw_stage
*stage
,
71 struct prim_header
*header
)
73 stage
->next
->tri(stage
->next
, header
);
78 * Draw a wide line by drawing a quad (two triangles).
79 * XXX need to disable polygon stipple.
81 static void wide_line( struct draw_stage
*stage
,
82 struct prim_header
*header
)
84 const struct wide_stage
*wide
= wide_stage(stage
);
85 const float half_width
= wide
->half_line_width
;
87 struct prim_header tri
;
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);
94 float *pos0
= v0
->data
[0];
95 float *pos1
= v1
->data
[0];
96 float *pos2
= v2
->data
[0];
97 float *pos3
= v3
->data
[0];
99 const float dx
= FABSF(pos0
[0] - pos2
[0]);
100 const float dy
= FABSF(pos0
[1] - pos2
[1]);
103 * Draw wide line as a quad (two tris) by "stretching" the line along
105 * We need to tweak coords in several ways to be conformant here.
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 */
122 /* right to left 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 */
143 /* bottom to top line */
151 tri
.det
= header
->det
; /* only the sign matters */
155 stage
->next
->tri( stage
->next
, &tri
);
160 stage
->next
->tri( stage
->next
, &tri
);
165 * Draw a wide line by drawing a quad, using geometry which will
166 * fullfill GL's antialiased line requirements.
168 static void wide_line_aa(struct draw_stage
*stage
,
169 struct prim_header
*header
)
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];
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
);
181 dx
= dx
* half_width
/ len
;
182 dy
= dy
* half_width
/ len
;
184 /* allocate/dup new verts */
185 for (i
= 0; i
< 4; i
++) {
186 v
[i
] = dup_vert(stage
, header
->v
[i
/2], i
);
190 * Quad for line from v0 to v1:
193 * +-------------------------+
197 * +-------------------------+
217 tri
.det
= header
->det
; /* only the sign matters */
219 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
220 stage
->next
->tri( stage
->next
, &tri
);
222 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
223 stage
->next
->tri( stage
->next
, &tri
);
229 * Set the vertex texcoords for sprite mode.
230 * Coords may be left untouched or set to a right-side-up or upside-down
233 static void set_texcoords(const struct wide_stage
*wide
,
234 struct vertex_header
*v
, const float tc
[4])
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];
244 v
->data
[j
][1] = tc
[1];
245 v
->data
[j
][2] = tc
[2];
246 v
->data
[j
][3] = tc
[3];
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
257 static void wide_point( struct draw_stage
*stage
,
258 struct prim_header
*header
)
260 const struct wide_stage
*wide
= wide_stage(stage
);
261 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->point_sprite
;
263 float left_adj
, right_adj
;
265 struct prim_header tri
;
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);
273 float *pos0
= v0
->data
[0];
274 float *pos1
= v1
->data
[0];
275 float *pos2
= v2
->data
[0];
276 float *pos3
= v3
->data
[0];
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];
283 half_size
= wide
->half_point_size
;
286 left_adj
= -half_size
+ 0.25f
;
287 right_adj
= half_size
+ 0.25f
;
290 pos0
[1] -= half_size
;
293 pos1
[1] += half_size
;
295 pos2
[0] += right_adj
;
296 pos2
[1] -= half_size
;
298 pos3
[0] += right_adj
;
299 pos3
[1] += half_size
;
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
);
312 tri
.det
= header
->det
; /* only the sign matters */
316 stage
->next
->tri( stage
->next
, &tri
);
321 stage
->next
->tri( stage
->next
, &tri
);
325 static void wide_first_point( struct draw_stage
*stage
,
326 struct prim_header
*header
)
328 struct wide_stage
*wide
= wide_stage(stage
);
329 struct draw_context
*draw
= stage
->draw
;
331 wide
->half_point_size
= 0.5f
* draw
->rasterizer
->point_size
;
333 if (draw
->rasterizer
->point_size
!= 1.0) {
334 stage
->point
= wide_point
;
337 stage
->point
= passthrough_point
;
340 if (draw
->rasterizer
->point_sprite
) {
341 /* find vertex shader texcoord outputs */
342 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
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
];
351 wide
->num_texcoords
= j
;
354 wide
->psize_slot
= -1;
356 if (draw
->rasterizer
->point_size_per_vertex
) {
357 /* find PSIZ vertex output */
358 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
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
;
368 stage
->point( stage
, header
);
373 static void wide_first_line( struct draw_stage
*stage
,
374 struct prim_header
*header
)
376 struct wide_stage
*wide
= wide_stage(stage
);
377 struct draw_context
*draw
= stage
->draw
;
379 wide
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
381 if (draw
->rasterizer
->line_width
!= 1.0) {
382 if (draw
->rasterizer
->line_smooth
)
383 wide
->stage
.line
= wide_line_aa
;
385 wide
->stage
.line
= wide_line
;
388 wide
->stage
.line
= passthrough_line
;
391 stage
->line( stage
, header
);
395 static void wide_flush( struct draw_stage
*stage
, unsigned flags
)
397 stage
->line
= wide_first_line
;
398 stage
->point
= wide_first_point
;
399 stage
->next
->flush( stage
->next
, flags
);
403 static void wide_reset_stipple_counter( struct draw_stage
*stage
)
405 stage
->next
->reset_stipple_counter( stage
->next
);
409 static void wide_destroy( struct draw_stage
*stage
)
411 draw_free_temp_verts( stage
);
416 struct draw_stage
*draw_wide_stage( struct draw_context
*draw
)
418 struct wide_stage
*wide
= CALLOC_STRUCT(wide_stage
);
420 draw_alloc_temp_verts( &wide
->stage
, 4 );
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
;