1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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_screen.h"
56 #include "pipe/p_context.h"
57 #include "util/u_math.h"
58 #include "util/u_memory.h"
59 #include "pipe/p_defines.h"
60 #include "pipe/p_shader_tokens.h"
63 #include "draw_pipe.h"
66 struct widepoint_stage
{
67 struct draw_stage stage
; /**< base class */
69 float half_point_size
;
74 /** for automatic texcoord generation/replacement */
75 uint num_texcoord_gen
;
76 uint texcoord_gen_slot
[PIPE_MAX_SHADER_OUTPUTS
];
78 /* TGSI_SEMANTIC to which sprite_coord_enable applies */
79 enum tgsi_semantic sprite_coord_semantic
;
86 static inline struct widepoint_stage
*
87 widepoint_stage( struct draw_stage
*stage
)
89 return (struct widepoint_stage
*)stage
;
94 * Set the vertex texcoords for sprite mode.
95 * Coords may be left untouched or set to a right-side-up or upside-down
98 static void set_texcoords(const struct widepoint_stage
*wide
,
99 struct vertex_header
*v
, const float tc
[4])
101 const struct draw_context
*draw
= wide
->stage
.draw
;
102 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
103 const uint texcoord_mode
= rast
->sprite_coord_mode
;
106 for (i
= 0; i
< wide
->num_texcoord_gen
; i
++) {
107 const uint slot
= wide
->texcoord_gen_slot
[i
];
108 v
->data
[slot
][0] = tc
[0];
109 if (texcoord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
)
110 v
->data
[slot
][1] = 1.0f
- tc
[1];
112 v
->data
[slot
][1] = tc
[1];
113 v
->data
[slot
][2] = tc
[2];
114 v
->data
[slot
][3] = tc
[3];
119 /* If there are lots of sprite points (and why wouldn't there be?) it
120 * would probably be more sensible to change hardware setup to
121 * optimize this rather than doing the whole thing in software like
124 static void widepoint_point( struct draw_stage
*stage
,
125 struct prim_header
*header
)
127 const struct widepoint_stage
*wide
= widepoint_stage(stage
);
128 const unsigned pos
= draw_current_shader_position_output(stage
->draw
);
129 const boolean sprite
= (boolean
) stage
->draw
->rasterizer
->point_quad_rasterization
;
131 float left_adj
, right_adj
, bot_adj
, top_adj
;
133 struct prim_header tri
;
135 /* four dups of original vertex */
136 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
137 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
138 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[0], 2);
139 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[0], 3);
141 float *pos0
= v0
->data
[pos
];
142 float *pos1
= v1
->data
[pos
];
143 float *pos2
= v2
->data
[pos
];
144 float *pos3
= v3
->data
[pos
];
146 /* point size is either per-vertex or fixed size */
147 if (wide
->psize_slot
>= 0) {
148 half_size
= header
->v
[0]->data
[wide
->psize_slot
][0];
152 half_size
= wide
->half_point_size
;
155 left_adj
= -half_size
+ wide
->xbias
;
156 right_adj
= half_size
+ wide
->xbias
;
157 bot_adj
= half_size
+ wide
->ybias
;
158 top_adj
= -half_size
+ wide
->ybias
;
166 pos2
[0] += right_adj
;
169 pos3
[0] += right_adj
;
173 static const float tex00
[4] = { 0, 0, 0, 1 };
174 static const float tex01
[4] = { 0, 1, 0, 1 };
175 static const float tex11
[4] = { 1, 1, 0, 1 };
176 static const float tex10
[4] = { 1, 0, 0, 1 };
177 set_texcoords( wide
, v0
, tex00
);
178 set_texcoords( wide
, v1
, tex01
);
179 set_texcoords( wide
, v2
, tex10
);
180 set_texcoords( wide
, v3
, tex11
);
183 tri
.det
= header
->det
; /* only the sign matters */
187 stage
->next
->tri( stage
->next
, &tri
);
192 stage
->next
->tri( stage
->next
, &tri
);
197 widepoint_first_point(struct draw_stage
*stage
,
198 struct prim_header
*header
)
200 struct widepoint_stage
*wide
= widepoint_stage(stage
);
201 struct draw_context
*draw
= stage
->draw
;
202 struct pipe_context
*pipe
= draw
->pipe
;
203 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
206 wide
->half_point_size
= 0.5f
* rast
->point_size
;
210 if (rast
->half_pixel_center
) {
212 wide
->ybias
= -0.125;
215 /* Disable triangle culling, stippling, unfilled mode etc. */
216 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
217 draw
->suspend_flushing
= TRUE
;
218 pipe
->bind_rasterizer_state(pipe
, r
);
219 draw
->suspend_flushing
= FALSE
;
221 /* XXX we won't know the real size if it's computed by the vertex shader! */
222 if ((rast
->point_size
> draw
->pipeline
.wide_point_threshold
) ||
223 (rast
->point_quad_rasterization
&& draw
->pipeline
.point_sprite
)) {
224 stage
->point
= widepoint_point
;
227 stage
->point
= draw_pipe_passthrough_point
;
230 draw_remove_extra_vertex_attribs(draw
);
232 if (rast
->point_quad_rasterization
) {
233 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
238 wide
->num_texcoord_gen
= 0;
240 /* Loop over fragment shader inputs looking for the PCOORD input or inputs
241 * for which bit 'k' in sprite_coord_enable is set.
243 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
245 const enum tgsi_semantic sn
= fs
->info
.input_semantic_name
[i
];
246 const unsigned si
= fs
->info
.input_semantic_index
[i
];
248 if (sn
== wide
->sprite_coord_semantic
) {
249 /* Note that sprite_coord_enable is a bitfield of 32 bits. */
250 if (si
>= 32 || !(rast
->sprite_coord_enable
& (1 << si
)))
252 } else if (sn
!= TGSI_SEMANTIC_PCOORD
) {
256 /* OK, this generic attribute needs to be replaced with a
257 * sprite coord (see above).
259 slot
= draw_alloc_extra_vertex_attrib(draw
, sn
, si
);
261 /* add this slot to the texcoord-gen list */
262 wide
->texcoord_gen_slot
[wide
->num_texcoord_gen
++] = slot
;
266 wide
->psize_slot
= -1;
267 if (rast
->point_size_per_vertex
) {
268 /* find PSIZ vertex output */
269 wide
->psize_slot
= draw_find_shader_output(draw
, TGSI_SEMANTIC_PSIZE
, 0);
272 stage
->point( stage
, header
);
276 static void widepoint_flush( struct draw_stage
*stage
, unsigned flags
)
278 struct draw_context
*draw
= stage
->draw
;
279 struct pipe_context
*pipe
= draw
->pipe
;
281 stage
->point
= widepoint_first_point
;
282 stage
->next
->flush( stage
->next
, flags
);
284 draw_remove_extra_vertex_attribs(draw
);
286 /* restore original rasterizer state */
287 if (draw
->rast_handle
) {
288 draw
->suspend_flushing
= TRUE
;
289 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
290 draw
->suspend_flushing
= FALSE
;
295 static void widepoint_reset_stipple_counter( struct draw_stage
*stage
)
297 stage
->next
->reset_stipple_counter( stage
->next
);
301 static void widepoint_destroy( struct draw_stage
*stage
)
303 draw_free_temp_verts( stage
);
308 struct draw_stage
*draw_wide_point_stage( struct draw_context
*draw
)
310 struct widepoint_stage
*wide
= CALLOC_STRUCT(widepoint_stage
);
314 wide
->stage
.draw
= draw
;
315 wide
->stage
.name
= "wide-point";
316 wide
->stage
.next
= NULL
;
317 wide
->stage
.point
= widepoint_first_point
;
318 wide
->stage
.line
= draw_pipe_passthrough_line
;
319 wide
->stage
.tri
= draw_pipe_passthrough_tri
;
320 wide
->stage
.flush
= widepoint_flush
;
321 wide
->stage
.reset_stipple_counter
= widepoint_reset_stipple_counter
;
322 wide
->stage
.destroy
= widepoint_destroy
;
324 if (!draw_alloc_temp_verts( &wide
->stage
, 4 ))
327 wide
->sprite_coord_semantic
=
328 draw
->pipe
->screen
->get_param(draw
->pipe
->screen
, PIPE_CAP_TGSI_TEXCOORD
)
330 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
336 wide
->stage
.destroy( &wide
->stage
);