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>
32 * Notes on wide points and sprite mode:
34 * In wide point/sprite mode we effectively need to convert each incoming
35 * vertex into four outgoing vertices specifying the corners of a quad.
36 * Since we don't (yet) have geometry shaders, we have to handle this here
39 * For sprites, it also means that this is where we have to handle texcoords
40 * for the vertices of the quad. OpenGL's GL_COORD_REPLACE state specifies
41 * if/how enabled texcoords are automatically generated for sprites. We pass
42 * that info through gallium in the pipe_rasterizer_state::sprite_coord_mode
45 * Additionally, GLSL's gl_PointCoord fragment attribute has to be handled
46 * here as well. This is basically an additional texture/generic attribute
47 * that varies .x from 0 to 1 horizontally across the point and varies .y
48 * vertically from 0 to 1 down the sprite.
50 * With geometry shaders, the state tracker could create a GS to do
55 #include "util/u_math.h"
56 #include "util/u_memory.h"
57 #include "pipe/p_defines.h"
58 #include "pipe/p_shader_tokens.h"
60 #include "draw_pipe.h"
63 struct widepoint_stage
{
64 struct draw_stage stage
;
66 float half_point_size
;
71 uint texcoord_slot
[PIPE_MAX_SHADER_OUTPUTS
];
72 uint texcoord_enable
[PIPE_MAX_SHADER_OUTPUTS
];
78 int point_coord_fs_input
; /**< input for pointcoord */
83 static INLINE
struct widepoint_stage
*
84 widepoint_stage( struct draw_stage
*stage
)
86 return (struct widepoint_stage
*)stage
;
91 * Set the vertex texcoords for sprite mode.
92 * Coords may be left untouched or set to a right-side-up or upside-down
95 static void set_texcoords(const struct widepoint_stage
*wide
,
96 struct vertex_header
*v
, const float tc
[4])
99 for (i
= 0; i
< wide
->num_texcoords
; i
++) {
100 if (wide
->texcoord_enable
[i
]) {
101 uint j
= wide
->texcoord_slot
[i
];
102 v
->data
[j
][0] = tc
[0];
103 if (wide
->texcoord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
)
104 v
->data
[j
][1] = 1.0f
- tc
[1];
106 v
->data
[j
][1] = tc
[1];
107 v
->data
[j
][2] = tc
[2];
108 v
->data
[j
][3] = tc
[3];
112 if (wide
->point_coord_fs_input
>= 0) {
113 /* put gl_PointCoord into the extra vertex slot */
114 uint slot
= wide
->stage
.draw
->extra_shader_outputs
.slot
;
115 v
->data
[slot
][0] = tc
[0];
116 v
->data
[slot
][1] = tc
[1];
117 v
->data
[slot
][2] = 0.0F
;
118 v
->data
[slot
][3] = 1.0F
;
123 /* If there are lots of sprite points (and why wouldn't there be?) it
124 * would probably be more sensible to change hardware setup to
125 * optimize this rather than doing the whole thing in software like
128 static void widepoint_point( struct draw_stage
*stage
,
129 struct prim_header
*header
)
131 /* XXX should take point_quad_rasterization into account? */
132 const struct widepoint_stage
*wide
= widepoint_stage(stage
);
133 const unsigned pos
= draw_current_shader_position_output(stage
->draw
);
134 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->sprite_coord_enable
;
136 float left_adj
, right_adj
, bot_adj
, top_adj
;
138 struct prim_header tri
;
140 /* four dups of original vertex */
141 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
142 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
143 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[0], 2);
144 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[0], 3);
146 float *pos0
= v0
->data
[pos
];
147 float *pos1
= v1
->data
[pos
];
148 float *pos2
= v2
->data
[pos
];
149 float *pos3
= v3
->data
[pos
];
151 /* point size is either per-vertex or fixed size */
152 if (wide
->psize_slot
>= 0) {
153 half_size
= header
->v
[0]->data
[wide
->psize_slot
][0];
157 half_size
= wide
->half_point_size
;
160 left_adj
= -half_size
+ wide
->xbias
;
161 right_adj
= half_size
+ wide
->xbias
;
162 bot_adj
= half_size
+ wide
->ybias
;
163 top_adj
= -half_size
+ wide
->ybias
;
171 pos2
[0] += right_adj
;
174 pos3
[0] += right_adj
;
178 static const float tex00
[4] = { 0, 0, 0, 1 };
179 static const float tex01
[4] = { 0, 1, 0, 1 };
180 static const float tex11
[4] = { 1, 1, 0, 1 };
181 static const float tex10
[4] = { 1, 0, 0, 1 };
182 set_texcoords( wide
, v0
, tex00
);
183 set_texcoords( wide
, v1
, tex01
);
184 set_texcoords( wide
, v2
, tex10
);
185 set_texcoords( wide
, v3
, tex11
);
188 tri
.det
= header
->det
; /* only the sign matters */
192 stage
->next
->tri( stage
->next
, &tri
);
197 stage
->next
->tri( stage
->next
, &tri
);
202 find_pntc_input_attrib(struct draw_context
*draw
)
204 /* Scan the fragment program's input decls to find the pointcoord
205 * attribute. The xy components will store the point coord.
207 return 0; /* XXX fix this */
211 static void widepoint_first_point( struct draw_stage
*stage
,
212 struct prim_header
*header
)
214 struct widepoint_stage
*wide
= widepoint_stage(stage
);
215 struct draw_context
*draw
= stage
->draw
;
217 wide
->half_point_size
= 0.5f
* draw
->rasterizer
->point_size
;
221 if (draw
->rasterizer
->gl_rasterization_rules
) {
225 /* XXX we won't know the real size if it's computed by the vertex shader! */
226 if ((draw
->rasterizer
->point_size
> draw
->pipeline
.wide_point_threshold
) ||
227 (draw
->rasterizer
->sprite_coord_enable
&& draw
->pipeline
.point_sprite
)) {
228 stage
->point
= widepoint_point
;
231 stage
->point
= draw_pipe_passthrough_point
;
234 if (draw
->rasterizer
->sprite_coord_enable
) {
235 /* find vertex shader texcoord outputs */
236 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
238 wide
->texcoord_mode
= draw
->rasterizer
->sprite_coord_mode
;
239 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
240 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
241 wide
->texcoord_slot
[j
] = i
;
242 wide
->texcoord_enable
[j
] = (draw
->rasterizer
->sprite_coord_enable
>> j
) & 1;
246 wide
->num_texcoords
= j
;
248 /* find fragment shader PointCoord input */
249 wide
->point_coord_fs_input
= find_pntc_input_attrib(draw
);
251 /* setup extra vp output (point coord implemented as a texcoord) */
252 draw
->extra_shader_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
253 draw
->extra_shader_outputs
.semantic_index
= 0;
254 draw
->extra_shader_outputs
.slot
= draw_current_shader_outputs(draw
);
257 wide
->point_coord_fs_input
= -1;
258 draw
->extra_shader_outputs
.slot
= 0;
261 wide
->psize_slot
= -1;
262 if (draw
->rasterizer
->point_size_per_vertex
) {
263 /* find PSIZ vertex output */
264 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
266 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
267 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
268 wide
->psize_slot
= i
;
274 stage
->point( stage
, header
);
278 static void widepoint_flush( struct draw_stage
*stage
, unsigned flags
)
280 stage
->point
= widepoint_first_point
;
281 stage
->next
->flush( stage
->next
, flags
);
282 stage
->draw
->extra_shader_outputs
.slot
= 0;
286 static void widepoint_reset_stipple_counter( struct draw_stage
*stage
)
288 stage
->next
->reset_stipple_counter( stage
->next
);
292 static void widepoint_destroy( struct draw_stage
*stage
)
294 draw_free_temp_verts( stage
);
299 struct draw_stage
*draw_wide_point_stage( struct draw_context
*draw
)
301 struct widepoint_stage
*wide
= CALLOC_STRUCT(widepoint_stage
);
305 if (!draw_alloc_temp_verts( &wide
->stage
, 4 ))
308 wide
->stage
.draw
= draw
;
309 wide
->stage
.name
= "wide-point";
310 wide
->stage
.next
= NULL
;
311 wide
->stage
.point
= widepoint_first_point
;
312 wide
->stage
.line
= draw_pipe_passthrough_line
;
313 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
314 wide
->stage
.flush
= widepoint_flush
;
315 wide
->stage
.reset_stipple_counter
= widepoint_reset_stipple_counter
;
316 wide
->stage
.destroy
= widepoint_destroy
;
322 wide
->stage
.destroy( &wide
->stage
);