Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_wide_point.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31 /**
32 * Notes on wide points and sprite mode:
33 *
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
37 * in the draw module.
38 *
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
43 * array.
44 *
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.
49 *
50 * With geometry shaders, the state tracker could create a GS to do
51 * most/all of this.
52 */
53
54
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"
60 #include "draw_vs.h"
61 #include "draw_pipe.h"
62
63
64 struct widepoint_stage {
65 struct draw_stage stage;
66
67 float half_point_size;
68
69 float xbias;
70 float ybias;
71
72 uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
73 uint texcoord_enable[PIPE_MAX_SHADER_OUTPUTS];
74 uint num_texcoords;
75 uint texcoord_mode;
76
77 int psize_slot;
78
79 int point_coord_fs_input; /**< input for pointcoord */
80 };
81
82
83
84 static INLINE struct widepoint_stage *
85 widepoint_stage( struct draw_stage *stage )
86 {
87 return (struct widepoint_stage *)stage;
88 }
89
90
91 /**
92 * Set the vertex texcoords for sprite mode.
93 * Coords may be left untouched or set to a right-side-up or upside-down
94 * orientation.
95 */
96 static void set_texcoords(const struct widepoint_stage *wide,
97 struct vertex_header *v, const float tc[4])
98 {
99 uint i;
100 for (i = 0; i < wide->num_texcoords; i++) {
101 if (wide->texcoord_enable[i]) {
102 uint j = wide->texcoord_slot[i];
103 v->data[j][0] = tc[0];
104 if (wide->texcoord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
105 v->data[j][1] = 1.0f - tc[1];
106 else
107 v->data[j][1] = tc[1];
108 v->data[j][2] = tc[2];
109 v->data[j][3] = tc[3];
110 }
111 }
112
113 if (wide->point_coord_fs_input >= 0) {
114 /* put gl_PointCoord into the extra vertex slot */
115 uint slot = wide->stage.draw->extra_shader_outputs.slot;
116 v->data[slot][0] = tc[0];
117 if (wide->texcoord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
118 v->data[slot][1] = 1.0f - tc[1];
119 else
120 v->data[slot][1] = tc[1];
121 v->data[slot][2] = 0.0F;
122 v->data[slot][3] = 1.0F;
123 }
124 }
125
126
127 /* If there are lots of sprite points (and why wouldn't there be?) it
128 * would probably be more sensible to change hardware setup to
129 * optimize this rather than doing the whole thing in software like
130 * this.
131 */
132 static void widepoint_point( struct draw_stage *stage,
133 struct prim_header *header )
134 {
135 /* XXX should take point_quad_rasterization into account? */
136 const struct widepoint_stage *wide = widepoint_stage(stage);
137 const unsigned pos = draw_current_shader_position_output(stage->draw);
138 const boolean sprite = (boolean) stage->draw->rasterizer->sprite_coord_enable;
139 float half_size;
140 float left_adj, right_adj, bot_adj, top_adj;
141
142 struct prim_header tri;
143
144 /* four dups of original vertex */
145 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
146 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
147 struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
148 struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
149
150 float *pos0 = v0->data[pos];
151 float *pos1 = v1->data[pos];
152 float *pos2 = v2->data[pos];
153 float *pos3 = v3->data[pos];
154
155 /* point size is either per-vertex or fixed size */
156 if (wide->psize_slot >= 0) {
157 half_size = header->v[0]->data[wide->psize_slot][0];
158 half_size *= 0.5f;
159 }
160 else {
161 half_size = wide->half_point_size;
162 }
163
164 left_adj = -half_size + wide->xbias;
165 right_adj = half_size + wide->xbias;
166 bot_adj = half_size + wide->ybias;
167 top_adj = -half_size + wide->ybias;
168
169 pos0[0] += left_adj;
170 pos0[1] += top_adj;
171
172 pos1[0] += left_adj;
173 pos1[1] += bot_adj;
174
175 pos2[0] += right_adj;
176 pos2[1] += top_adj;
177
178 pos3[0] += right_adj;
179 pos3[1] += bot_adj;
180
181 if (sprite) {
182 static const float tex00[4] = { 0, 0, 0, 1 };
183 static const float tex01[4] = { 0, 1, 0, 1 };
184 static const float tex11[4] = { 1, 1, 0, 1 };
185 static const float tex10[4] = { 1, 0, 0, 1 };
186 set_texcoords( wide, v0, tex00 );
187 set_texcoords( wide, v1, tex01 );
188 set_texcoords( wide, v2, tex10 );
189 set_texcoords( wide, v3, tex11 );
190 }
191
192 tri.det = header->det; /* only the sign matters */
193 tri.v[0] = v0;
194 tri.v[1] = v2;
195 tri.v[2] = v3;
196 stage->next->tri( stage->next, &tri );
197
198 tri.v[0] = v0;
199 tri.v[1] = v3;
200 tri.v[2] = v1;
201 stage->next->tri( stage->next, &tri );
202 }
203
204
205 static int
206 find_pntc_input_attrib(struct draw_context *draw)
207 {
208 /* Scan the fragment program's input decls to find the pointcoord
209 * attribute. The xy components will store the point coord.
210 */
211 return 0; /* XXX fix this */
212 }
213
214
215 static void widepoint_first_point( struct draw_stage *stage,
216 struct prim_header *header )
217 {
218 struct widepoint_stage *wide = widepoint_stage(stage);
219 struct draw_context *draw = stage->draw;
220 struct pipe_context *pipe = draw->pipe;
221 const struct pipe_rasterizer_state *rast = draw->rasterizer;
222 void *r;
223
224 wide->half_point_size = 0.5f * rast->point_size;
225 wide->xbias = 0.0;
226 wide->ybias = 0.0;
227
228 if (rast->gl_rasterization_rules) {
229 wide->xbias = 0.125;
230 }
231
232 /* Disable triangle culling, stippling, unfilled mode etc. */
233 r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
234 draw->suspend_flushing = TRUE;
235 pipe->bind_rasterizer_state(pipe, r);
236 draw->suspend_flushing = FALSE;
237
238 /* XXX we won't know the real size if it's computed by the vertex shader! */
239 if ((rast->point_size > draw->pipeline.wide_point_threshold) ||
240 (rast->sprite_coord_enable && draw->pipeline.point_sprite)) {
241 stage->point = widepoint_point;
242 }
243 else {
244 stage->point = draw_pipe_passthrough_point;
245 }
246
247 if (rast->sprite_coord_enable) {
248 /* find vertex shader texcoord outputs */
249 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
250 uint i, j = 0;
251 wide->texcoord_mode = rast->sprite_coord_mode;
252 for (i = 0; i < vs->info.num_outputs; i++) {
253 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
254 wide->texcoord_slot[j] = i;
255 wide->texcoord_enable[j] = (rast->sprite_coord_enable >> j) & 1;
256 j++;
257 }
258 }
259 wide->num_texcoords = j;
260
261 /* find fragment shader PointCoord input */
262 wide->point_coord_fs_input = find_pntc_input_attrib(draw);
263
264 /* setup extra vp output (point coord implemented as a texcoord) */
265 draw->extra_shader_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
266 draw->extra_shader_outputs.semantic_index = 0;
267 draw->extra_shader_outputs.slot = draw_current_shader_outputs(draw);
268 }
269 else {
270 wide->point_coord_fs_input = -1;
271 draw->extra_shader_outputs.slot = 0;
272 }
273
274 wide->psize_slot = -1;
275 if (rast->point_size_per_vertex) {
276 /* find PSIZ vertex output */
277 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
278 uint i;
279 for (i = 0; i < vs->info.num_outputs; i++) {
280 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
281 wide->psize_slot = i;
282 break;
283 }
284 }
285 }
286
287 stage->point( stage, header );
288 }
289
290
291 static void widepoint_flush( struct draw_stage *stage, unsigned flags )
292 {
293 struct draw_context *draw = stage->draw;
294 struct pipe_context *pipe = draw->pipe;
295
296 stage->point = widepoint_first_point;
297 stage->next->flush( stage->next, flags );
298 stage->draw->extra_shader_outputs.slot = 0;
299
300 /* restore original rasterizer state */
301 if (draw->rast_handle) {
302 draw->suspend_flushing = TRUE;
303 pipe->bind_rasterizer_state(pipe, draw->rast_handle);
304 draw->suspend_flushing = FALSE;
305 }
306 }
307
308
309 static void widepoint_reset_stipple_counter( struct draw_stage *stage )
310 {
311 stage->next->reset_stipple_counter( stage->next );
312 }
313
314
315 static void widepoint_destroy( struct draw_stage *stage )
316 {
317 draw_free_temp_verts( stage );
318 FREE( stage );
319 }
320
321
322 struct draw_stage *draw_wide_point_stage( struct draw_context *draw )
323 {
324 struct widepoint_stage *wide = CALLOC_STRUCT(widepoint_stage);
325 if (wide == NULL)
326 goto fail;
327
328 if (!draw_alloc_temp_verts( &wide->stage, 4 ))
329 goto fail;
330
331 wide->stage.draw = draw;
332 wide->stage.name = "wide-point";
333 wide->stage.next = NULL;
334 wide->stage.point = widepoint_first_point;
335 wide->stage.line = draw_pipe_passthrough_line;
336 wide->stage.tri = draw_pipe_passthrough_tri;
337 wide->stage.flush = widepoint_flush;
338 wide->stage.reset_stipple_counter = widepoint_reset_stipple_counter;
339 wide->stage.destroy = widepoint_destroy;
340
341 return &wide->stage;
342
343 fail:
344 if (wide)
345 wide->stage.destroy( &wide->stage );
346
347 return NULL;
348 }