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 * Set the vertex texcoords for sprite mode.
166 * Coords may be left untouched or set to a right-side-up or upside-down
169 static void set_texcoords(const struct wide_stage
*wide
,
170 struct vertex_header
*v
, const float tc
[4])
173 for (i
= 0; i
< wide
->num_texcoords
; i
++) {
174 if (wide
->texcoord_mode
[i
] != PIPE_SPRITE_COORD_NONE
) {
175 uint j
= wide
->texcoord_slot
[i
];
176 v
->data
[j
][0] = tc
[0];
177 if (wide
->texcoord_mode
[i
] == PIPE_SPRITE_COORD_LOWER_LEFT
)
178 v
->data
[j
][1] = 1.0f
- tc
[1];
180 v
->data
[j
][1] = tc
[1];
181 v
->data
[j
][2] = tc
[2];
182 v
->data
[j
][3] = tc
[3];
188 /* If there are lots of sprite points (and why wouldn't there be?) it
189 * would probably be more sensible to change hardware setup to
190 * optimize this rather than doing the whole thing in software like
193 static void wide_point( struct draw_stage
*stage
,
194 struct prim_header
*header
)
196 const struct wide_stage
*wide
= wide_stage(stage
);
197 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->point_sprite
;
199 float left_adj
, right_adj
;
201 struct prim_header tri
;
203 /* four dups of original vertex */
204 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
205 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
206 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[0], 2);
207 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[0], 3);
209 float *pos0
= v0
->data
[0];
210 float *pos1
= v1
->data
[0];
211 float *pos2
= v2
->data
[0];
212 float *pos3
= v3
->data
[0];
214 /* point size is either per-vertex or fixed size */
215 if (wide
->psize_slot
>= 0) {
216 half_size
= 0.5f
* header
->v
[0]->data
[wide
->psize_slot
][0];
219 half_size
= wide
->half_point_size
;
222 left_adj
= -half_size
; /* + 0.25f;*/
223 right_adj
= half_size
; /* + 0.25f;*/
226 pos0
[1] -= half_size
;
229 pos1
[1] += half_size
;
231 pos2
[0] += right_adj
;
232 pos2
[1] -= half_size
;
234 pos3
[0] += right_adj
;
235 pos3
[1] += half_size
;
238 static const float tex00
[4] = { 0, 0, 0, 1 };
239 static const float tex01
[4] = { 0, 1, 0, 1 };
240 static const float tex11
[4] = { 1, 1, 0, 1 };
241 static const float tex10
[4] = { 1, 0, 0, 1 };
242 set_texcoords( wide
, v0
, tex00
);
243 set_texcoords( wide
, v1
, tex01
);
244 set_texcoords( wide
, v2
, tex10
);
245 set_texcoords( wide
, v3
, tex11
);
248 tri
.det
= header
->det
; /* only the sign matters */
252 stage
->next
->tri( stage
->next
, &tri
);
257 stage
->next
->tri( stage
->next
, &tri
);
261 static void wide_first_point( struct draw_stage
*stage
,
262 struct prim_header
*header
)
264 struct wide_stage
*wide
= wide_stage(stage
);
265 struct draw_context
*draw
= stage
->draw
;
267 wide
->half_point_size
= 0.5f
* draw
->rasterizer
->point_size
;
269 /* XXX we won't know the real size if it's computed by the vertex shader! */
270 if (draw
->rasterizer
->point_size
> draw
->wide_point_threshold
) {
271 stage
->point
= wide_point
;
274 stage
->point
= passthrough_point
;
277 if (draw
->rasterizer
->point_sprite
) {
278 /* find vertex shader texcoord outputs */
279 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
281 for (i
= 0; i
< vs
->state
->num_outputs
; i
++) {
282 if (vs
->state
->output_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
283 wide
->texcoord_slot
[j
] = i
;
284 wide
->texcoord_mode
[j
] = draw
->rasterizer
->sprite_coord_mode
[j
];
288 wide
->num_texcoords
= j
;
291 wide
->psize_slot
= -1;
293 if (draw
->rasterizer
->point_size_per_vertex
) {
294 /* find PSIZ vertex output */
295 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
297 for (i
= 0; i
< vs
->state
->num_outputs
; i
++) {
298 if (vs
->state
->output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
299 wide
->psize_slot
= i
;
305 stage
->point( stage
, header
);
310 static void wide_first_line( struct draw_stage
*stage
,
311 struct prim_header
*header
)
313 struct wide_stage
*wide
= wide_stage(stage
);
314 struct draw_context
*draw
= stage
->draw
;
316 wide
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
318 if (draw
->rasterizer
->line_width
!= 1.0) {
319 wide
->stage
.line
= wide_line
;
322 wide
->stage
.line
= passthrough_line
;
325 stage
->line( stage
, header
);
329 static void wide_flush( struct draw_stage
*stage
, unsigned flags
)
331 stage
->line
= wide_first_line
;
332 stage
->point
= wide_first_point
;
333 stage
->next
->flush( stage
->next
, flags
);
337 static void wide_reset_stipple_counter( struct draw_stage
*stage
)
339 stage
->next
->reset_stipple_counter( stage
->next
);
343 static void wide_destroy( struct draw_stage
*stage
)
345 draw_free_temp_verts( stage
);
350 struct draw_stage
*draw_wide_stage( struct draw_context
*draw
)
352 struct wide_stage
*wide
= CALLOC_STRUCT(wide_stage
);
354 draw_alloc_temp_verts( &wide
->stage
, 4 );
356 wide
->stage
.draw
= draw
;
357 wide
->stage
.next
= NULL
;
358 wide
->stage
.point
= wide_first_point
;
359 wide
->stage
.line
= wide_first_line
;
360 wide
->stage
.tri
= passthrough_tri
;
361 wide
->stage
.flush
= wide_flush
;
362 wide
->stage
.reset_stipple_counter
= wide_reset_stipple_counter
;
363 wide
->stage
.destroy
= wide_destroy
;