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 "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "lp_setup_context.h"
35 #include "lp_perf.h"
36 #include "lp_rast.h"
37 #include "lp_state_fs.h"
38 #include "lp_state_setup.h"
39 #include "lp_context.h"
40 #include "tgsi/tgsi_scan.h"
41 #include "draw/draw_context.h"
42
43 #define NUM_CHANNELS 4
44
45 struct point_info {
46 /* x,y deltas */
47 int dy01, dy12;
48 int dx01, dx12;
49
50 const float (*v0)[4];
51
52 float (*a0)[4];
53 float (*dadx)[4];
54 float (*dady)[4];
55
56 boolean frontfacing;
57 };
58
59
60 /**
61 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
62 */
63 static void
constant_coef(struct lp_setup_context * setup,struct point_info * info,unsigned slot,const float value,unsigned i)64 constant_coef(struct lp_setup_context *setup,
65 struct point_info *info,
66 unsigned slot,
67 const float value,
68 unsigned i)
69 {
70 info->a0[slot][i] = value;
71 info->dadx[slot][i] = 0.0f;
72 info->dady[slot][i] = 0.0f;
73 }
74
75
76 static void
point_persp_coeff(struct lp_setup_context * setup,const struct point_info * info,unsigned slot,unsigned i)77 point_persp_coeff(struct lp_setup_context *setup,
78 const struct point_info *info,
79 unsigned slot,
80 unsigned i)
81 {
82 /*
83 * Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A
84 * better strategy would be to take the primitive in consideration when
85 * generating the fragment shader key, and therefore avoid the per-fragment
86 * perspective divide.
87 */
88
89 float w0 = info->v0[0][3];
90
91 assert(i < 4);
92
93 info->a0[slot][i] = info->v0[slot][i]*w0;
94 info->dadx[slot][i] = 0.0f;
95 info->dady[slot][i] = 0.0f;
96 }
97
98
99 /**
100 * Setup automatic texcoord coefficients (for sprite rendering).
101 * \param slot the vertex attribute slot to setup
102 * \param i the attribute channel in [0,3]
103 * \param sprite_coord_origin one of PIPE_SPRITE_COORD_x
104 * \param perspective does the shader expects pre-multiplied w, i.e.,
105 * LP_INTERP_PERSPECTIVE is specified in the shader key
106 */
107 static void
texcoord_coef(struct lp_setup_context * setup,const struct point_info * info,unsigned slot,unsigned i,unsigned sprite_coord_origin,boolean perspective)108 texcoord_coef(struct lp_setup_context *setup,
109 const struct point_info *info,
110 unsigned slot,
111 unsigned i,
112 unsigned sprite_coord_origin,
113 boolean perspective)
114 {
115 float w0 = info->v0[0][3];
116
117 assert(i < 4);
118
119 if (i == 0) {
120 float dadx = FIXED_ONE / (float)info->dx12;
121 float dady = 0.0f;
122 float x0 = info->v0[0][0] - setup->pixel_offset;
123 float y0 = info->v0[0][1] - setup->pixel_offset;
124
125 info->dadx[slot][0] = dadx;
126 info->dady[slot][0] = dady;
127 info->a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);
128
129 if (perspective) {
130 info->dadx[slot][0] *= w0;
131 info->dady[slot][0] *= w0;
132 info->a0[slot][0] *= w0;
133 }
134 }
135 else if (i == 1) {
136 float dadx = 0.0f;
137 float dady = FIXED_ONE / (float)info->dx12;
138 float x0 = info->v0[0][0] - setup->pixel_offset;
139 float y0 = info->v0[0][1] - setup->pixel_offset;
140
141 if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {
142 dady = -dady;
143 }
144
145 info->dadx[slot][1] = dadx;
146 info->dady[slot][1] = dady;
147 info->a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);
148
149 if (perspective) {
150 info->dadx[slot][1] *= w0;
151 info->dady[slot][1] *= w0;
152 info->a0[slot][1] *= w0;
153 }
154 }
155 else if (i == 2) {
156 info->a0[slot][2] = 0.0f;
157 info->dadx[slot][2] = 0.0f;
158 info->dady[slot][2] = 0.0f;
159 }
160 else {
161 info->a0[slot][3] = perspective ? w0 : 1.0f;
162 info->dadx[slot][3] = 0.0f;
163 info->dady[slot][3] = 0.0f;
164 }
165 }
166
167
168 /**
169 * Special coefficient setup for gl_FragCoord.
170 * X and Y are trivial
171 * Z and W are copied from position_coef which should have already been computed.
172 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
173 */
174 static void
setup_point_fragcoord_coef(struct lp_setup_context * setup,struct point_info * info,unsigned slot,unsigned usage_mask)175 setup_point_fragcoord_coef(struct lp_setup_context *setup,
176 struct point_info *info,
177 unsigned slot,
178 unsigned usage_mask)
179 {
180 /*X*/
181 if (usage_mask & TGSI_WRITEMASK_X) {
182 info->a0[slot][0] = 0.0;
183 info->dadx[slot][0] = 1.0;
184 info->dady[slot][0] = 0.0;
185 }
186
187 /*Y*/
188 if (usage_mask & TGSI_WRITEMASK_Y) {
189 info->a0[slot][1] = 0.0;
190 info->dadx[slot][1] = 0.0;
191 info->dady[slot][1] = 1.0;
192 }
193
194 /*Z*/
195 if (usage_mask & TGSI_WRITEMASK_Z) {
196 constant_coef(setup, info, slot, info->v0[0][2], 2);
197 }
198
199 /*W*/
200 if (usage_mask & TGSI_WRITEMASK_W) {
201 constant_coef(setup, info, 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
setup_point_coefficients(struct lp_setup_context * setup,struct point_info * info)210 setup_point_coefficients( struct lp_setup_context *setup,
211 struct point_info *info)
212 {
213 const struct lp_setup_variant_key *key = &setup->setup.variant->key;
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 < key->num_inputs; slot++) {
221 unsigned vert_attr = key->inputs[slot].src_index;
222 unsigned usage_mask = key->inputs[slot].usage_mask;
223 enum lp_interp interp = key->inputs[slot].interp;
224 boolean perspective = !!(interp == LP_INTERP_PERSPECTIVE);
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 FALLTHROUGH;
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 bool do_texcoord_coef = false;
249 if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_PCOORD) {
250 do_texcoord_coef = true;
251 }
252 else if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_TEXCOORD) {
253 unsigned semantic_index = shader->info.base.input_semantic_index[slot];
254 /* Note that sprite_coord enable is a bitfield of
255 * PIPE_MAX_SHADER_OUTPUTS bits.
256 */
257 if (semantic_index < PIPE_MAX_SHADER_OUTPUTS &&
258 (setup->sprite_coord_enable & (1u << semantic_index))) {
259 do_texcoord_coef = true;
260 }
261 }
262 if (do_texcoord_coef) {
263 for (i = 0; i < NUM_CHANNELS; i++) {
264 if (usage_mask & (1 << i)) {
265 texcoord_coef(setup, info, slot + 1, i,
266 setup->sprite_coord_origin,
267 perspective);
268 }
269 }
270 break;
271 }
272 }
273 FALLTHROUGH;
274 case LP_INTERP_CONSTANT:
275 for (i = 0; i < NUM_CHANNELS; i++) {
276 if (usage_mask & (1 << i)) {
277 if (perspective) {
278 point_persp_coeff(setup, info, slot+1, i);
279 }
280 else {
281 constant_coef(setup, info, slot+1, info->v0[vert_attr][i], i);
282 }
283 }
284 }
285 break;
286
287 case LP_INTERP_FACING:
288 for (i = 0; i < NUM_CHANNELS; i++)
289 if (usage_mask & (1 << i))
290 constant_coef(setup, info, slot+1,
291 info->frontfacing ? 1.0f : -1.0f, i);
292 break;
293
294 default:
295 assert(0);
296 break;
297 }
298 }
299
300 /* The internal position input is in slot zero:
301 */
302 setup_point_fragcoord_coef(setup, info, 0,
303 fragcoord_usage_mask);
304 }
305
306
307 static inline int
subpixel_snap(float a)308 subpixel_snap(float a)
309 {
310 return util_iround(FIXED_ONE * a);
311 }
312
313 /**
314 * Print point vertex attribs (for debug).
315 */
316 static void
print_point(struct lp_setup_context * setup,const float (* v0)[4],const float size)317 print_point(struct lp_setup_context *setup,
318 const float (*v0)[4],
319 const float size)
320 {
321 const struct lp_setup_variant_key *key = &setup->setup.variant->key;
322 uint i;
323
324 debug_printf("llvmpipe point, width %f\n", size);
325 for (i = 0; i < 1 + key->num_inputs; i++) {
326 debug_printf(" v0[%d]: %f %f %f %f\n", i,
327 v0[i][0], v0[i][1], v0[i][2], v0[i][3]);
328 }
329 }
330
331
332 static boolean
try_setup_point(struct lp_setup_context * setup,const float (* v0)[4])333 try_setup_point( struct lp_setup_context *setup,
334 const float (*v0)[4] )
335 {
336 struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
337 /* x/y positions in fixed point */
338 const struct lp_setup_variant_key *key = &setup->setup.variant->key;
339 const int sizeAttr = setup->psize_slot;
340 float size
341 = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
342 : setup->point_size;
343
344 if (size > LP_MAX_POINT_WIDTH)
345 size = LP_MAX_POINT_WIDTH;
346
347 /* Yes this is necessary to accurately calculate bounding boxes
348 * with the two fill-conventions we support. GL (normally) ends
349 * up needing a bottom-left fill convention, which requires
350 * slightly different rounding.
351 */
352 int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;
353 float pixel_offset = setup->multisample ? 0.0 : setup->pixel_offset;
354 struct lp_scene *scene = setup->scene;
355 struct u_rect bbox;
356 int x[2], y[2];
357 struct point_info info;
358 unsigned viewport_index = 0;
359 unsigned layer = 0;
360 int fixed_width;
361
362 if (setup->viewport_index_slot > 0) {
363 unsigned *udata = (unsigned*)v0[setup->viewport_index_slot];
364 viewport_index = lp_clamp_viewport_idx(*udata);
365 }
366 if (setup->layer_slot > 0) {
367 layer = *(unsigned*)v0[setup->layer_slot];
368 layer = MIN2(layer, scene->fb_max_layer);
369 }
370
371 if (0)
372 print_point(setup, v0, size);
373
374 /* Bounding rectangle (in pixels) */
375 if (!setup->legacy_points || setup->multisample) {
376 /*
377 * Rasterize points as quads.
378 */
379 int x0, y0;
380 /* Point size as fixed point integer, remove rounding errors
381 * and gives minimum width for very small points.
382 */
383 fixed_width = MAX2(FIXED_ONE, subpixel_snap(size));
384
385 x0 = subpixel_snap(v0[0][0] - pixel_offset) - fixed_width/2;
386 y0 = subpixel_snap(v0[0][1] - pixel_offset) - fixed_width/2;
387
388 x[0] = x0;
389 x[1] = x0 + fixed_width;
390 y[0] = y0;
391 y[1] = y0 + fixed_width;
392 bbox.x0 = x[0] >> FIXED_ORDER;
393 bbox.x1 = (x[1] + (FIXED_ONE-1)) >> FIXED_ORDER;
394 bbox.y0 = (y[0] + adj) >> FIXED_ORDER;
395 bbox.y1 = (y[1] + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
396
397 /* Inclusive coordinates:
398 */
399 bbox.x1--;
400 bbox.y1--;
401 } else {
402 /*
403 * OpenGL legacy rasterization rules for non-sprite points.
404 *
405 * Per OpenGL 2.1 spec, section 3.3.1, "Basic Point Rasterization".
406 *
407 * This type of point rasterization is only available in pre 3.0 contexts
408 * (or compatibility contexts which we don't support) anyway.
409 */
410
411 const int x0 = subpixel_snap(v0[0][0]);
412 const int y0 = subpixel_snap(v0[0][1]) - adj;
413
414 int int_width;
415 /* Point size as fixed point integer. For GL legacy points
416 * the point size is always a whole integer.
417 */
418 fixed_width = MAX2(FIXED_ONE,
419 (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
420 int_width = fixed_width >> FIXED_ORDER;
421
422 assert(setup->pixel_offset != 0);
423
424 if (int_width == 1) {
425 bbox.x0 = x0 >> FIXED_ORDER;
426 bbox.y0 = y0 >> FIXED_ORDER;
427 bbox.x1 = bbox.x0;
428 bbox.y1 = bbox.y0;
429 } else {
430 if (int_width & 1) {
431 /* Odd width */
432 bbox.x0 = (x0 >> FIXED_ORDER) - (int_width - 1)/2;
433 bbox.y0 = (y0 >> FIXED_ORDER) - (int_width - 1)/2;
434 } else {
435 /* Even width */
436 bbox.x0 = ((x0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;
437 bbox.y0 = ((y0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;
438 }
439
440 bbox.x1 = bbox.x0 + int_width - 1;
441 bbox.y1 = bbox.y0 + int_width - 1;
442 }
443
444 x[0] = (bbox.x0 - 1) << 8;
445 x[1] = (bbox.x1 + 1) << 8;
446 y[0] = (bbox.y0 - 1) << 8;
447 y[1] = (bbox.y1 + 1) << 8;
448 }
449
450 if (0) {
451 debug_printf(" bbox: (%i, %i) - (%i, %i)\n",
452 bbox.x0, bbox.y0,
453 bbox.x1, bbox.y1);
454 }
455
456 if (lp_context->active_statistics_queries) {
457 lp_context->pipeline_statistics.c_primitives++;
458 }
459
460 if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {
461 if (0) debug_printf("no intersection\n");
462 LP_COUNT(nr_culled_tris);
463 return TRUE;
464 }
465
466 u_rect_find_intersection(&setup->draw_regions[viewport_index], &bbox);
467
468 /* We can't use rectangle reasterizer for non-legacy points for now. */
469 if (!setup->legacy_points || setup->multisample) {
470 struct lp_rast_triangle *point;
471 struct lp_rast_plane *plane;
472 unsigned bytes;
473 unsigned nr_planes = 4;
474
475 point = lp_setup_alloc_triangle(scene,
476 key->num_inputs,
477 nr_planes,
478 &bytes);
479 if (!point)
480 return FALSE;
481
482 #ifdef DEBUG
483 point->v[0][0] = v0[0][0];
484 point->v[0][1] = v0[0][1];
485 #endif
486
487 LP_COUNT(nr_tris);
488
489 if (draw_will_inject_frontface(lp_context->draw) &&
490 setup->face_slot > 0) {
491 point->inputs.frontfacing = v0[setup->face_slot][0];
492 } else {
493 point->inputs.frontfacing = TRUE;
494 }
495
496 info.v0 = v0;
497 info.dx01 = 0;
498 info.dx12 = fixed_width;
499 info.dy01 = fixed_width;
500 info.dy12 = 0;
501 info.a0 = GET_A0(&point->inputs);
502 info.dadx = GET_DADX(&point->inputs);
503 info.dady = GET_DADY(&point->inputs);
504 info.frontfacing = point->inputs.frontfacing;
505
506 /* Setup parameter interpolants:
507 */
508 setup_point_coefficients(setup, &info);
509
510 point->inputs.disable = FALSE;
511 point->inputs.is_blit = FALSE;
512 point->inputs.opaque = setup->fs.current.variant->opaque;
513 point->inputs.layer = layer;
514 point->inputs.viewport_index = viewport_index;
515 point->inputs.view_index = setup->view_index;
516
517 plane = GET_PLANES(point);
518
519 plane[0].dcdx = ~0U << 8;
520 plane[0].dcdy = 0;
521 plane[0].c = -MAX2(x[0], bbox.x0 << 8);
522 plane[0].eo = 1 << 8;
523
524 plane[1].dcdx = 1 << 8;
525 plane[1].dcdy = 0;
526 plane[1].c = MIN2(x[1], (bbox.x1 + 1) << 8);
527 plane[1].eo = 0;
528
529 plane[2].dcdx = 0;
530 plane[2].dcdy = 1 << 8;
531 plane[2].c = -MAX2(y[0], (bbox.y0 << 8) - adj);
532 plane[2].eo = 1 << 8;
533
534 plane[3].dcdx = 0;
535 plane[3].dcdy = ~0U << 8;
536 plane[3].c = MIN2(y[1], (bbox.y1 + 1) << 8);
537 plane[3].eo = 0;
538
539 if (!setup->legacy_points || setup->multisample) {
540 /* adjust for fill-rule*/
541 plane[0].c++; /* left */
542 if (setup->bottom_edge_rule == 0)
543 plane[2].c++; /* top-left */
544 else
545 plane[3].c++; /* bottom-left */
546 }
547
548 return lp_setup_bin_triangle(setup, point, &bbox, &bbox, nr_planes, viewport_index);
549
550 } else {
551 struct lp_rast_rectangle *point;
552 point = lp_setup_alloc_rectangle(scene,
553 key->num_inputs);
554 if (!point)
555 return FALSE;
556 #ifdef DEBUG
557 point->v[0][0] = v0[0][0];
558 point->v[0][1] = v0[0][1];
559 #endif
560
561 point->box.x0 = bbox.x0;
562 point->box.x1 = bbox.x1;
563 point->box.y0 = bbox.y0;
564 point->box.y1 = bbox.y1;
565
566 LP_COUNT(nr_tris);
567
568 if (draw_will_inject_frontface(lp_context->draw) &&
569 setup->face_slot > 0) {
570 point->inputs.frontfacing = v0[setup->face_slot][0];
571 } else {
572 point->inputs.frontfacing = TRUE;
573 }
574
575 info.v0 = v0;
576 info.dx01 = 0;
577 info.dx12 = fixed_width;
578 info.dy01 = fixed_width;
579 info.dy12 = 0;
580 info.a0 = GET_A0(&point->inputs);
581 info.dadx = GET_DADX(&point->inputs);
582 info.dady = GET_DADY(&point->inputs);
583 info.frontfacing = point->inputs.frontfacing;
584
585 /* Setup parameter interpolants:
586 */
587 setup_point_coefficients(setup, &info);
588
589 point->inputs.disable = FALSE;
590 point->inputs.is_blit = FALSE;
591 point->inputs.opaque = setup->fs.current.variant->opaque;
592 point->inputs.layer = layer;
593 point->inputs.viewport_index = viewport_index;
594 point->inputs.view_index = setup->view_index;
595
596 return lp_setup_bin_rectangle(setup, point);
597 }
598 }
599
600
601 static void
lp_setup_point_discard(struct lp_setup_context * setup,const float (* v0)[4])602 lp_setup_point_discard(struct lp_setup_context *setup,
603 const float (*v0)[4])
604 {
605 }
606
607 static void
lp_setup_point(struct lp_setup_context * setup,const float (* v0)[4])608 lp_setup_point(struct lp_setup_context *setup,
609 const float (*v0)[4])
610 {
611 if (!try_setup_point(setup, v0)) {
612 if (!lp_setup_flush_and_restart(setup))
613 return;
614
615 if (!try_setup_point(setup, v0))
616 return;
617 }
618 }
619
620
621 void
lp_setup_choose_point(struct lp_setup_context * setup)622 lp_setup_choose_point(struct lp_setup_context *setup)
623 {
624 if (setup->rasterizer_discard) {
625 setup->point = lp_setup_point_discard;
626 } else {
627 setup->point = lp_setup_point;
628 }
629 }
630
631
632