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 "pipe/p_context.h"
56 #include "util/u_math.h"
57 #include "util/u_memory.h"
58 #include "pipe/p_defines.h"
59 #include "pipe/p_shader_tokens.h"
62 #include "draw_pipe.h"
65 struct widepoint_stage
{
66 struct draw_stage stage
; /**< base class */
68 float half_point_size
;
73 /** for automatic texcoord generation/replacement */
74 uint num_texcoord_gen
;
75 uint texcoord_gen_slot
[PIPE_MAX_SHADER_OUTPUTS
];
82 static INLINE
struct widepoint_stage
*
83 widepoint_stage( struct draw_stage
*stage
)
85 return (struct widepoint_stage
*)stage
;
90 * Set the vertex texcoords for sprite mode.
91 * Coords may be left untouched or set to a right-side-up or upside-down
94 static void set_texcoords(const struct widepoint_stage
*wide
,
95 struct vertex_header
*v
, const float tc
[4])
97 const struct draw_context
*draw
= wide
->stage
.draw
;
98 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
99 const uint texcoord_mode
= rast
->sprite_coord_mode
;
102 for (i
= 0; i
< wide
->num_texcoord_gen
; i
++) {
103 const uint slot
= wide
->texcoord_gen_slot
[i
];
104 v
->data
[slot
][0] = tc
[0];
105 if (texcoord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
)
106 v
->data
[slot
][1] = 1.0f
- tc
[1];
108 v
->data
[slot
][1] = tc
[1];
109 v
->data
[slot
][2] = tc
[2];
110 v
->data
[slot
][3] = tc
[3];
115 /* If there are lots of sprite points (and why wouldn't there be?) it
116 * would probably be more sensible to change hardware setup to
117 * optimize this rather than doing the whole thing in software like
120 static void widepoint_point( struct draw_stage
*stage
,
121 struct prim_header
*header
)
123 const struct widepoint_stage
*wide
= widepoint_stage(stage
);
124 const unsigned pos
= draw_current_shader_position_output(stage
->draw
);
125 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->point_quad_rasterization
;
127 float left_adj
, right_adj
, bot_adj
, top_adj
;
129 struct prim_header tri
;
131 /* four dups of original vertex */
132 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
133 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
134 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[0], 2);
135 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[0], 3);
137 float *pos0
= v0
->data
[pos
];
138 float *pos1
= v1
->data
[pos
];
139 float *pos2
= v2
->data
[pos
];
140 float *pos3
= v3
->data
[pos
];
142 /* point size is either per-vertex or fixed size */
143 if (wide
->psize_slot
>= 0) {
144 half_size
= header
->v
[0]->data
[wide
->psize_slot
][0];
148 half_size
= wide
->half_point_size
;
151 left_adj
= -half_size
+ wide
->xbias
;
152 right_adj
= half_size
+ wide
->xbias
;
153 bot_adj
= half_size
+ wide
->ybias
;
154 top_adj
= -half_size
+ wide
->ybias
;
162 pos2
[0] += right_adj
;
165 pos3
[0] += right_adj
;
169 static const float tex00
[4] = { 0, 0, 0, 1 };
170 static const float tex01
[4] = { 0, 1, 0, 1 };
171 static const float tex11
[4] = { 1, 1, 0, 1 };
172 static const float tex10
[4] = { 1, 0, 0, 1 };
173 set_texcoords( wide
, v0
, tex00
);
174 set_texcoords( wide
, v1
, tex01
);
175 set_texcoords( wide
, v2
, tex10
);
176 set_texcoords( wide
, v3
, tex11
);
179 tri
.det
= header
->det
; /* only the sign matters */
183 stage
->next
->tri( stage
->next
, &tri
);
188 stage
->next
->tri( stage
->next
, &tri
);
193 widepoint_first_point(struct draw_stage
*stage
,
194 struct prim_header
*header
)
196 struct widepoint_stage
*wide
= widepoint_stage(stage
);
197 struct draw_context
*draw
= stage
->draw
;
198 struct pipe_context
*pipe
= draw
->pipe
;
199 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
202 wide
->half_point_size
= 0.5f
* rast
->point_size
;
206 if (rast
->gl_rasterization_rules
) {
208 wide
->ybias
= -0.125;
211 /* Disable triangle culling, stippling, unfilled mode etc. */
212 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
213 draw
->suspend_flushing
= TRUE
;
214 pipe
->bind_rasterizer_state(pipe
, r
);
215 draw
->suspend_flushing
= FALSE
;
217 /* XXX we won't know the real size if it's computed by the vertex shader! */
218 if ((rast
->point_size
> draw
->pipeline
.wide_point_threshold
) ||
219 (rast
->point_quad_rasterization
&& draw
->pipeline
.point_sprite
)) {
220 stage
->point
= widepoint_point
;
223 stage
->point
= draw_pipe_passthrough_point
;
226 draw_remove_extra_vertex_attribs(draw
);
228 if (rast
->point_quad_rasterization
) {
229 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
232 wide
->num_texcoord_gen
= 0;
234 /* Loop over fragment shader inputs looking for generic inputs
235 * for which bit 'k' in sprite_coord_enable is set.
237 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
238 if (fs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
239 const int generic_index
= fs
->info
.input_semantic_index
[i
];
240 /* Note that sprite_coord enable is a bitfield of
241 * PIPE_MAX_SHADER_OUTPUTS bits.
243 if (generic_index
< PIPE_MAX_SHADER_OUTPUTS
&&
244 (rast
->sprite_coord_enable
& (1 << generic_index
))) {
245 /* OK, this generic attribute needs to be replaced with a
246 * texcoord (see above).
248 int slot
= draw_find_shader_output(draw
,
249 TGSI_SEMANTIC_GENERIC
,
253 /* there's already a post-vertex shader attribute
254 * for this fragment shader input attribute.
258 /* need to allocate a new post-vertex shader attribute */
259 slot
= draw_alloc_extra_vertex_attrib(draw
,
260 TGSI_SEMANTIC_GENERIC
,
264 /* add this slot to the texcoord-gen list */
265 wide
->texcoord_gen_slot
[wide
->num_texcoord_gen
++] = slot
;
271 wide
->psize_slot
= -1;
272 if (rast
->point_size_per_vertex
) {
273 /* find PSIZ vertex output */
274 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
276 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
277 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
278 wide
->psize_slot
= i
;
284 stage
->point( stage
, header
);
288 static void widepoint_flush( struct draw_stage
*stage
, unsigned flags
)
290 struct draw_context
*draw
= stage
->draw
;
291 struct pipe_context
*pipe
= draw
->pipe
;
293 stage
->point
= widepoint_first_point
;
294 stage
->next
->flush( stage
->next
, flags
);
296 draw_remove_extra_vertex_attribs(draw
);
298 /* restore original rasterizer state */
299 if (draw
->rast_handle
) {
300 draw
->suspend_flushing
= TRUE
;
301 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
302 draw
->suspend_flushing
= FALSE
;
307 static void widepoint_reset_stipple_counter( struct draw_stage
*stage
)
309 stage
->next
->reset_stipple_counter( stage
->next
);
313 static void widepoint_destroy( struct draw_stage
*stage
)
315 draw_free_temp_verts( stage
);
320 struct draw_stage
*draw_wide_point_stage( struct draw_context
*draw
)
322 struct widepoint_stage
*wide
= CALLOC_STRUCT(widepoint_stage
);
326 wide
->stage
.draw
= draw
;
327 wide
->stage
.name
= "wide-point";
328 wide
->stage
.next
= NULL
;
329 wide
->stage
.point
= widepoint_first_point
;
330 wide
->stage
.line
= draw_pipe_passthrough_line
;
331 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
332 wide
->stage
.flush
= widepoint_flush
;
333 wide
->stage
.reset_stipple_counter
= widepoint_reset_stipple_counter
;
334 wide
->stage
.destroy
= widepoint_destroy
;
336 if (!draw_alloc_temp_verts( &wide
->stage
, 4 ))
343 wide
->stage
.destroy( &wide
->stage
);