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