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 "util/u_math.h"
32 #include "util/u_memory.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_shader_tokens.h"
36 #include "draw_pipe.h"
39 struct widepoint_stage
{
40 struct draw_stage stage
;
42 float half_point_size
;
49 uint texcoord_slot
[PIPE_MAX_SHADER_OUTPUTS
];
50 uint texcoord_mode
[PIPE_MAX_SHADER_OUTPUTS
];
55 int point_coord_fs_input
; /**< input for pointcoord (and fog) */
60 static INLINE
struct widepoint_stage
*
61 widepoint_stage( struct draw_stage
*stage
)
63 return (struct widepoint_stage
*)stage
;
70 * Set the vertex texcoords for sprite mode.
71 * Coords may be left untouched or set to a right-side-up or upside-down
74 static void set_texcoords(const struct widepoint_stage
*wide
,
75 struct vertex_header
*v
, const float tc
[4])
78 for (i
= 0; i
< wide
->num_texcoords
; i
++) {
79 if (wide
->texcoord_mode
[i
] != PIPE_SPRITE_COORD_NONE
) {
80 uint j
= wide
->texcoord_slot
[i
];
81 v
->data
[j
][0] = tc
[0];
82 if (wide
->texcoord_mode
[i
] == PIPE_SPRITE_COORD_LOWER_LEFT
)
83 v
->data
[j
][1] = 1.0f
- tc
[1];
85 v
->data
[j
][1] = tc
[1];
86 v
->data
[j
][2] = tc
[2];
87 v
->data
[j
][3] = tc
[3];
91 if (wide
->point_coord_fs_input
>= 0) {
92 /* put gl_PointCoord into extra vertex output's zw components */
93 uint k
= wide
->stage
.draw
->extra_vp_outputs
.slot
;
94 v
->data
[k
][2] = tc
[0];
95 v
->data
[k
][3] = tc
[1];
100 /* If there are lots of sprite points (and why wouldn't there be?) it
101 * would probably be more sensible to change hardware setup to
102 * optimize this rather than doing the whole thing in software like
105 static void widepoint_point( struct draw_stage
*stage
,
106 struct prim_header
*header
)
108 const struct widepoint_stage
*wide
= widepoint_stage(stage
);
109 const unsigned pos
= stage
->draw
->vs
.position_output
;
110 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->point_sprite
;
112 float left_adj
, right_adj
, bot_adj
, top_adj
;
114 struct prim_header tri
;
116 /* four dups of original vertex */
117 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
118 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
119 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[0], 2);
120 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[0], 3);
122 float *pos0
= v0
->data
[pos
];
123 float *pos1
= v1
->data
[pos
];
124 float *pos2
= v2
->data
[pos
];
125 float *pos3
= v3
->data
[pos
];
127 /* point size is either per-vertex or fixed size */
128 if (wide
->psize_slot
>= 0) {
129 half_size
= header
->v
[0]->data
[wide
->psize_slot
][0];
131 /* XXX: temporary -- do this in the vertex shader??
133 half_size
= CLAMP(half_size
,
134 wide
->point_size_min
,
135 wide
->point_size_max
);
140 half_size
= wide
->half_point_size
;
143 left_adj
= -half_size
+ wide
->xbias
;
144 right_adj
= half_size
+ wide
->xbias
;
145 bot_adj
= half_size
+ wide
->ybias
;
146 top_adj
= -half_size
+ wide
->ybias
;
154 pos2
[0] += right_adj
;
157 pos3
[0] += right_adj
;
161 static const float tex00
[4] = { 0, 0, 0, 1 };
162 static const float tex01
[4] = { 0, 1, 0, 1 };
163 static const float tex11
[4] = { 1, 1, 0, 1 };
164 static const float tex10
[4] = { 1, 0, 0, 1 };
165 set_texcoords( wide
, v0
, tex00
);
166 set_texcoords( wide
, v1
, tex01
);
167 set_texcoords( wide
, v2
, tex10
);
168 set_texcoords( wide
, v3
, tex11
);
171 tri
.det
= header
->det
; /* only the sign matters */
175 stage
->next
->tri( stage
->next
, &tri
);
180 stage
->next
->tri( stage
->next
, &tri
);
184 static void widepoint_first_point( struct draw_stage
*stage
,
185 struct prim_header
*header
)
187 struct widepoint_stage
*wide
= widepoint_stage(stage
);
188 struct draw_context
*draw
= stage
->draw
;
190 wide
->half_point_size
= 0.5f
* draw
->rasterizer
->point_size
;
191 wide
->point_size_min
= draw
->rasterizer
->point_size_min
;
192 wide
->point_size_max
= draw
->rasterizer
->point_size_max
;
196 if (draw
->rasterizer
->gl_rasterization_rules
) {
200 /* XXX we won't know the real size if it's computed by the vertex shader! */
201 if ((draw
->rasterizer
->point_size
> draw
->pipeline
.wide_point_threshold
) ||
202 (draw
->rasterizer
->point_sprite
&& draw
->pipeline
.point_sprite
)) {
203 stage
->point
= widepoint_point
;
206 stage
->point
= draw_pipe_passthrough_point
;
209 if (draw
->rasterizer
->point_sprite
) {
210 /* find vertex shader texcoord outputs */
211 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
213 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
214 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
215 wide
->texcoord_slot
[j
] = i
;
216 wide
->texcoord_mode
[j
] = draw
->rasterizer
->sprite_coord_mode
[j
];
220 wide
->num_texcoords
= j
;
222 /* find fragment shader PointCoord/Fog input */
223 wide
->point_coord_fs_input
= 0; /* XXX fix this! */
225 /* setup extra vp output (point coord implemented as a texcoord) */
226 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
227 draw
->extra_vp_outputs
.semantic_index
= 0;
228 draw
->extra_vp_outputs
.slot
= draw
->vs
.num_vs_outputs
;
231 wide
->point_coord_fs_input
= -1;
232 draw
->extra_vp_outputs
.slot
= 0;
235 wide
->psize_slot
= -1;
236 if (draw
->rasterizer
->point_size_per_vertex
) {
237 /* find PSIZ vertex output */
238 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
240 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
241 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
242 wide
->psize_slot
= i
;
248 stage
->point( stage
, header
);
252 static void widepoint_flush( struct draw_stage
*stage
, unsigned flags
)
254 stage
->point
= widepoint_first_point
;
255 stage
->next
->flush( stage
->next
, flags
);
259 static void widepoint_reset_stipple_counter( struct draw_stage
*stage
)
261 stage
->next
->reset_stipple_counter( stage
->next
);
265 static void widepoint_destroy( struct draw_stage
*stage
)
267 draw_free_temp_verts( stage
);
272 struct draw_stage
*draw_wide_point_stage( struct draw_context
*draw
)
274 struct widepoint_stage
*wide
= CALLOC_STRUCT(widepoint_stage
);
278 if (!draw_alloc_temp_verts( &wide
->stage
, 4 ))
281 wide
->stage
.draw
= draw
;
282 wide
->stage
.next
= NULL
;
283 wide
->stage
.point
= widepoint_first_point
;
284 wide
->stage
.line
= draw_pipe_passthrough_line
;
285 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
286 wide
->stage
.flush
= widepoint_flush
;
287 wide
->stage
.reset_stipple_counter
= widepoint_reset_stipple_counter
;
288 wide
->stage
.destroy
= widepoint_destroy
;
294 wide
->stage
.destroy( &wide
->stage
);