llvmpipe: Fix perspective interpolation for point sprites.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup_point.c
1 /**************************************************************************
2 *
3 * Copyright 2010, VMware Inc.
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 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.
25 *
26 **************************************************************************/
27
28 /*
29 * Binning code for points
30 */
31
32 #include "lp_setup_context.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "lp_perf.h"
36 #include "lp_rast.h"
37 #include "lp_state_fs.h"
38 #include "tgsi/tgsi_scan.h"
39
40 #define NUM_CHANNELS 4
41
42 struct point_info {
43 /* x,y deltas */
44 int dy01, dy12;
45 int dx01, dx12;
46
47 const float (*v0)[4];
48 };
49
50
51 /**
52 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
53 */
54 static void
55 constant_coef(struct lp_setup_context *setup,
56 struct lp_rast_triangle *point,
57 unsigned slot,
58 const float value,
59 unsigned i)
60 {
61 point->inputs.a0[slot][i] = value;
62 point->inputs.dadx[slot][i] = 0.0f;
63 point->inputs.dady[slot][i] = 0.0f;
64 }
65
66
67 static void
68 point_persp_coeff(struct lp_setup_context *setup,
69 struct lp_rast_triangle *point,
70 const struct point_info *info,
71 unsigned slot,
72 unsigned i)
73 {
74 /*
75 * Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A
76 * better stratergy would be to take the primitive in consideration when
77 * generating the fragment shader key, and therefore avoid the per-fragment
78 * perspective divide.
79 */
80
81 float w0 = info->v0[0][3];
82
83 assert(i < 4);
84
85 point->inputs.a0[slot][i] = info->v0[slot][i]*w0;
86 point->inputs.dadx[slot][i] = 0.0f;
87 point->inputs.dady[slot][i] = 0.0f;
88 }
89
90
91 /**
92 * Setup automatic texcoord coefficients (for sprite rendering).
93 * \param slot the vertex attribute slot to setup
94 * \param i the attribute channel in [0,3]
95 * \param sprite_coord_origin one of PIPE_SPRITE_COORD_x
96 * \param perspective does the shader expects pre-multiplied w, i.e.,
97 * LP_INTERP_PERSPECTIVE is specified in the shader key
98 */
99 static void
100 texcoord_coef(struct lp_setup_context *setup,
101 struct lp_rast_triangle *point,
102 const struct point_info *info,
103 unsigned slot,
104 unsigned i,
105 unsigned sprite_coord_origin,
106 boolean perspective)
107 {
108 assert(i < 4);
109
110 if (i == 0) {
111 float dadx = FIXED_ONE / (float)info->dx12;
112 float dady = 0.0f;
113 float x0 = info->v0[0][0] - setup->pixel_offset;
114 float y0 = info->v0[0][1] - setup->pixel_offset;
115
116 point->inputs.dadx[slot][0] = dadx;
117 point->inputs.dady[slot][0] = dady;
118 point->inputs.a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);
119
120 if (perspective) {
121 /* Divide coefficients by vertex.w here.
122 *
123 * It would be clearer to always multiply by w0 above and
124 * then divide it out for perspective projection here, but
125 * doing it this way involves less algebra.
126 */
127 float w0 = info->v0[0][3];
128 point->inputs.dadx[slot][0] *= w0;
129 point->inputs.dady[slot][0] *= w0;
130 point->inputs.a0[slot][0] *= w0;
131 }
132 }
133 else if (i == 1) {
134 float dadx = 0.0f;
135 float dady = FIXED_ONE / (float)info->dx12;
136 float x0 = info->v0[0][0] - setup->pixel_offset;
137 float y0 = info->v0[0][1] - setup->pixel_offset;
138
139 if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {
140 dady = -dady;
141 }
142
143 point->inputs.dadx[slot][1] = dadx;
144 point->inputs.dady[slot][1] = dady;
145 point->inputs.a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);
146
147 if (perspective) {
148 float w0 = info->v0[0][3];
149 point->inputs.dadx[slot][1] *= w0;
150 point->inputs.dady[slot][1] *= w0;
151 point->inputs.a0[slot][1] *= w0;
152 }
153 }
154 else if (i == 2) {
155 point->inputs.a0[slot][2] = 0.0f;
156 point->inputs.dadx[slot][2] = 0.0f;
157 point->inputs.dady[slot][2] = 0.0f;
158 }
159 else {
160 point->inputs.a0[slot][3] = 1.0f;
161 point->inputs.dadx[slot][3] = 0.0f;
162 point->inputs.dady[slot][3] = 0.0f;
163 }
164 }
165
166
167 /**
168 * Special coefficient setup for gl_FragCoord.
169 * X and Y are trivial
170 * Z and W are copied from position_coef which should have already been computed.
171 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
172 */
173 static void
174 setup_point_fragcoord_coef(struct lp_setup_context *setup,
175 struct lp_rast_triangle *point,
176 const struct point_info *info,
177 unsigned slot,
178 unsigned usage_mask)
179 {
180 /*X*/
181 if (usage_mask & TGSI_WRITEMASK_X) {
182 point->inputs.a0[slot][0] = 0.0;
183 point->inputs.dadx[slot][0] = 1.0;
184 point->inputs.dady[slot][0] = 0.0;
185 }
186
187 /*Y*/
188 if (usage_mask & TGSI_WRITEMASK_Y) {
189 point->inputs.a0[slot][1] = 0.0;
190 point->inputs.dadx[slot][1] = 0.0;
191 point->inputs.dady[slot][1] = 1.0;
192 }
193
194 /*Z*/
195 if (usage_mask & TGSI_WRITEMASK_Z) {
196 constant_coef(setup, point, slot, info->v0[0][2], 2);
197 }
198
199 /*W*/
200 if (usage_mask & TGSI_WRITEMASK_W) {
201 constant_coef(setup, point, slot, info->v0[0][3], 3);
202 }
203 }
204
205
206 /**
207 * Compute the point->coef[] array dadx, dady, a0 values.
208 */
209 static void
210 setup_point_coefficients( struct lp_setup_context *setup,
211 struct lp_rast_triangle *point,
212 const struct point_info *info)
213 {
214 const struct lp_fragment_shader *shader = setup->fs.current.variant->shader;
215 unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
216 unsigned slot;
217
218 /* setup interpolation for all the remaining attributes:
219 */
220 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
221 enum lp_interp interp = setup->fs.input[slot].interp;
222 boolean perspective = !!(interp == LP_INTERP_PERSPECTIVE);
223 unsigned vert_attr = setup->fs.input[slot].src_index;
224 unsigned usage_mask = setup->fs.input[slot].usage_mask;
225 unsigned i;
226
227 if (perspective & usage_mask) {
228 fragcoord_usage_mask |= TGSI_WRITEMASK_W;
229 }
230
231 switch (interp) {
232 case LP_INTERP_POSITION:
233 /*
234 * The generated pixel interpolators will pick up the coeffs from
235 * slot 0, so all need to ensure that the usage mask is covers all
236 * usages.
237 */
238 fragcoord_usage_mask |= usage_mask;
239 break;
240
241 case LP_INTERP_LINEAR:
242 /* Sprite tex coords may use linear interpolation someday */
243 /* fall-through */
244 case LP_INTERP_PERSPECTIVE:
245 /* check if the sprite coord flag is set for this attribute.
246 * If so, set it up so it up so x and y vary from 0 to 1.
247 */
248 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_GENERIC) {
249 unsigned semantic_index = shader->info.input_semantic_index[slot];
250 /* Note that sprite_coord enable is a bitfield of
251 * PIPE_MAX_SHADER_OUTPUTS bits.
252 */
253 if (semantic_index < PIPE_MAX_SHADER_OUTPUTS &&
254 (setup->sprite_coord_enable & (1 << semantic_index))) {
255 for (i = 0; i < NUM_CHANNELS; i++) {
256 if (usage_mask & (1 << i)) {
257 texcoord_coef(setup, point, info, slot + 1, i,
258 setup->sprite_coord_origin,
259 perspective);
260 }
261 }
262 break;
263 }
264 }
265 /* fall-through */
266 case LP_INTERP_CONSTANT:
267 for (i = 0; i < NUM_CHANNELS; i++) {
268 if (usage_mask & (1 << i)) {
269 if (perspective) {
270 point_persp_coeff(setup, point, info, slot+1, i);
271 }
272 else {
273 constant_coef(setup, point, slot+1, info->v0[vert_attr][i], i);
274 }
275 }
276 }
277 break;
278
279 case LP_INTERP_FACING:
280 for (i = 0; i < NUM_CHANNELS; i++)
281 if (usage_mask & (1 << i))
282 constant_coef(setup, point, slot+1, 1.0, i);
283 break;
284
285 default:
286 assert(0);
287 break;
288 }
289 }
290
291 /* The internal position input is in slot zero:
292 */
293 setup_point_fragcoord_coef(setup, point, info, 0,
294 fragcoord_usage_mask);
295 }
296
297
298 static INLINE int
299 subpixel_snap(float a)
300 {
301 return util_iround(FIXED_ONE * a);
302 }
303
304
305 static boolean
306 try_setup_point( struct lp_setup_context *setup,
307 const float (*v0)[4] )
308 {
309 /* x/y positions in fixed point */
310 const int sizeAttr = setup->psize;
311 const float size
312 = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
313 : setup->point_size;
314
315 /* Point size as fixed point integer, remove rounding errors
316 * and gives minimum width for very small points
317 */
318 int fixed_width = MAX2(FIXED_ONE,
319 (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
320
321 const int x0 = subpixel_snap(v0[0][0] - setup->pixel_offset) - fixed_width/2;
322 const int y0 = subpixel_snap(v0[0][1] - setup->pixel_offset) - fixed_width/2;
323
324 struct lp_scene *scene = setup->scene;
325 struct lp_rast_triangle *point;
326 unsigned bytes;
327 struct u_rect bbox;
328 unsigned nr_planes = 4;
329 struct point_info info;
330
331
332 /* Bounding rectangle (in pixels) */
333 {
334 /* Yes this is necessary to accurately calculate bounding boxes
335 * with the two fill-conventions we support. GL (normally) ends
336 * up needing a bottom-left fill convention, which requires
337 * slightly different rounding.
338 */
339 int adj = (setup->pixel_offset != 0) ? 1 : 0;
340
341 bbox.x0 = (x0 + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
342 bbox.x1 = (x0 + fixed_width + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
343 bbox.y0 = (y0 + (FIXED_ONE-1)) >> FIXED_ORDER;
344 bbox.y1 = (y0 + fixed_width + (FIXED_ONE-1)) >> FIXED_ORDER;
345
346 /* Inclusive coordinates:
347 */
348 bbox.x1--;
349 bbox.y1--;
350 }
351
352 if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
353 if (0) debug_printf("offscreen\n");
354 LP_COUNT(nr_culled_tris);
355 return TRUE;
356 }
357
358 u_rect_find_intersection(&setup->draw_region, &bbox);
359
360 point = lp_setup_alloc_triangle(scene,
361 setup->fs.nr_inputs,
362 nr_planes,
363 &bytes);
364 if (!point)
365 return FALSE;
366
367 #ifdef DEBUG
368 point->v[0][0] = v0[0][0];
369 point->v[0][1] = v0[0][1];
370 #endif
371
372 info.v0 = v0;
373 info.dx01 = 0;
374 info.dx12 = fixed_width;
375 info.dy01 = fixed_width;
376 info.dy12 = 0;
377
378 /* Setup parameter interpolants:
379 */
380 setup_point_coefficients(setup, point, &info);
381
382 point->inputs.facing = 1.0F;
383 point->inputs.state = setup->fs.stored;
384 point->inputs.disable = FALSE;
385 point->inputs.opaque = FALSE;
386
387 {
388 point->plane[0].dcdx = -1;
389 point->plane[0].dcdy = 0;
390 point->plane[0].c = 1-bbox.x0;
391 point->plane[0].ei = 0;
392 point->plane[0].eo = 1;
393
394 point->plane[1].dcdx = 1;
395 point->plane[1].dcdy = 0;
396 point->plane[1].c = bbox.x1+1;
397 point->plane[1].ei = -1;
398 point->plane[1].eo = 0;
399
400 point->plane[2].dcdx = 0;
401 point->plane[2].dcdy = 1;
402 point->plane[2].c = 1-bbox.y0;
403 point->plane[2].ei = 0;
404 point->plane[2].eo = 1;
405
406 point->plane[3].dcdx = 0;
407 point->plane[3].dcdy = -1;
408 point->plane[3].c = bbox.y1+1;
409 point->plane[3].ei = -1;
410 point->plane[3].eo = 0;
411 }
412
413 return lp_setup_bin_triangle(setup, point, &bbox, nr_planes);
414 }
415
416
417 static void
418 lp_setup_point(struct lp_setup_context *setup,
419 const float (*v0)[4])
420 {
421 if (!try_setup_point( setup, v0 ))
422 {
423 lp_setup_flush_and_restart(setup);
424
425 if (!try_setup_point( setup, v0 ))
426 assert(0);
427 }
428 }
429
430
431 void
432 lp_setup_choose_point( struct lp_setup_context *setup )
433 {
434 setup->point = lp_setup_point;
435 }
436
437