1/************************************************************************** 2 * 3 * Copyright 2007 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 * \brief Primitive rasterization/rendering (points, lines, triangles) 30 * 31 * \author Keith Whitwell <keithw@vmware.com> 32 * \author Brian Paul 33 */ 34 35#include "sp_context.h" 36#include "sp_quad.h" 37#include "sp_quad_pipe.h" 38#include "sp_setup.h" 39#include "sp_state.h" 40#include "draw/draw_context.h" 41#include "pipe/p_shader_tokens.h" 42#include "util/u_math.h" 43#include "util/u_memory.h" 44 45 46#define DEBUG_VERTS 0 47#define DEBUG_FRAGS 0 48 49 50/** 51 * Triangle edge info 52 */ 53struct edge { 54 float dx; /**< X(v1) - X(v0), used only during setup */ 55 float dy; /**< Y(v1) - Y(v0), used only during setup */ 56 float dxdy; /**< dx/dy */ 57 float sx, sy; /**< first sample point coord */ 58 int lines; /**< number of lines on this edge */ 59}; 60 61 62/** 63 * Max number of quads (2x2 pixel blocks) to process per batch. 64 * This can't be arbitrarily increased since we depend on some 32-bit 65 * bitmasks (two bits per quad). 66 */ 67#define MAX_QUADS 16 68 69 70/** 71 * Triangle setup info. 72 * Also used for line drawing (taking some liberties). 73 */ 74struct setup_context { 75 struct softpipe_context *softpipe; 76 77 /* Vertices are just an array of floats making up each attribute in 78 * turn. Currently fixed at 4 floats, but should change in time. 79 * Codegen will help cope with this. 80 */ 81 const float (*vmax)[4]; 82 const float (*vmid)[4]; 83 const float (*vmin)[4]; 84 const float (*vprovoke)[4]; 85 86 struct edge ebot; 87 struct edge etop; 88 struct edge emaj; 89 90 float oneoverarea; 91 int facing; 92 93 float pixel_offset; 94 unsigned max_layer; 95 96 struct quad_header quad[MAX_QUADS]; 97 struct quad_header *quad_ptrs[MAX_QUADS]; 98 unsigned count; 99 100 struct tgsi_interp_coef coef[PIPE_MAX_SHADER_INPUTS]; 101 struct tgsi_interp_coef posCoef; /* For Z, W */ 102 103 struct { 104 int left[2]; /**< [0] = row0, [1] = row1 */ 105 int right[2]; 106 int y; 107 } span; 108 109#if DEBUG_FRAGS 110 uint numFragsEmitted; /**< per primitive */ 111 uint numFragsWritten; /**< per primitive */ 112#endif 113 114 unsigned cull_face; /* which faces cull */ 115 unsigned nr_vertex_attrs; 116}; 117 118 119 120 121 122 123 124/** 125 * Clip setup->quad against the scissor/surface bounds. 126 */ 127static inline void 128quad_clip(struct setup_context *setup, struct quad_header *quad) 129{ 130 unsigned viewport_index = quad[0].input.viewport_index; 131 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect[viewport_index]; 132 const int minx = (int) cliprect->minx; 133 const int maxx = (int) cliprect->maxx; 134 const int miny = (int) cliprect->miny; 135 const int maxy = (int) cliprect->maxy; 136 137 if (quad->input.x0 >= maxx || 138 quad->input.y0 >= maxy || 139 quad->input.x0 + 1 < minx || 140 quad->input.y0 + 1 < miny) { 141 /* totally clipped */ 142 quad->inout.mask = 0x0; 143 return; 144 } 145 if (quad->input.x0 < minx) 146 quad->inout.mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT); 147 if (quad->input.y0 < miny) 148 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT); 149 if (quad->input.x0 == maxx - 1) 150 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT); 151 if (quad->input.y0 == maxy - 1) 152 quad->inout.mask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT); 153} 154 155 156/** 157 * Emit a quad (pass to next stage) with clipping. 158 */ 159static inline void 160clip_emit_quad(struct setup_context *setup, struct quad_header *quad) 161{ 162 quad_clip(setup, quad); 163 164 if (quad->inout.mask) { 165 struct softpipe_context *sp = setup->softpipe; 166 167#if DEBUG_FRAGS 168 setup->numFragsEmitted += util_bitcount(quad->inout.mask); 169#endif 170 171 sp->quad.first->run( sp->quad.first, &quad, 1 ); 172 } 173} 174 175 176 177/** 178 * Given an X or Y coordinate, return the block/quad coordinate that it 179 * belongs to. 180 */ 181static inline int 182block(int x) 183{ 184 return x & ~(2-1); 185} 186 187 188static inline int 189block_x(int x) 190{ 191 return x & ~(16-1); 192} 193 194 195/** 196 * Render a horizontal span of quads 197 */ 198static void 199flush_spans(struct setup_context *setup) 200{ 201 const int step = MAX_QUADS; 202 const int xleft0 = setup->span.left[0]; 203 const int xleft1 = setup->span.left[1]; 204 const int xright0 = setup->span.right[0]; 205 const int xright1 = setup->span.right[1]; 206 struct quad_stage *pipe = setup->softpipe->quad.first; 207 208 const int minleft = block_x(MIN2(xleft0, xleft1)); 209 const int maxright = MAX2(xright0, xright1); 210 int x; 211 212 /* process quads in horizontal chunks of 16 */ 213 for (x = minleft; x < maxright; x += step) { 214 unsigned skip_left0 = CLAMP(xleft0 - x, 0, step); 215 unsigned skip_left1 = CLAMP(xleft1 - x, 0, step); 216 unsigned skip_right0 = CLAMP(x + step - xright0, 0, step); 217 unsigned skip_right1 = CLAMP(x + step - xright1, 0, step); 218 unsigned lx = x; 219 unsigned q = 0; 220 221 unsigned skipmask_left0 = (1U << skip_left0) - 1U; 222 unsigned skipmask_left1 = (1U << skip_left1) - 1U; 223 224 /* These calculations fail when step == 32 and skip_right == 0. 225 */ 226 unsigned skipmask_right0 = ~0U << (unsigned)(step - skip_right0); 227 unsigned skipmask_right1 = ~0U << (unsigned)(step - skip_right1); 228 229 unsigned mask0 = ~skipmask_left0 & ~skipmask_right0; 230 unsigned mask1 = ~skipmask_left1 & ~skipmask_right1; 231 232 if (mask0 | mask1) { 233 do { 234 unsigned quadmask = (mask0 & 3) | ((mask1 & 3) << 2); 235 if (quadmask) { 236 setup->quad[q].input.x0 = lx; 237 setup->quad[q].input.y0 = setup->span.y; 238 setup->quad[q].input.facing = setup->facing; 239 setup->quad[q].inout.mask = quadmask; 240 setup->quad_ptrs[q] = &setup->quad[q]; 241 q++; 242#if DEBUG_FRAGS 243 setup->numFragsEmitted += util_bitcount(quadmask); 244#endif 245 } 246 mask0 >>= 2; 247 mask1 >>= 2; 248 lx += 2; 249 } while (mask0 | mask1); 250 251 pipe->run( pipe, setup->quad_ptrs, q ); 252 } 253 } 254 255 256 setup->span.y = 0; 257 setup->span.right[0] = 0; 258 setup->span.right[1] = 0; 259 setup->span.left[0] = 1000000; /* greater than right[0] */ 260 setup->span.left[1] = 1000000; /* greater than right[1] */ 261} 262 263 264#if DEBUG_VERTS 265static void 266print_vertex(const struct setup_context *setup, 267 const float (*v)[4]) 268{ 269 int i; 270 debug_printf(" Vertex: (%p)\n", (void *) v); 271 for (i = 0; i < setup->nr_vertex_attrs; i++) { 272 debug_printf(" %d: %f %f %f %f\n", i, 273 v[i][0], v[i][1], v[i][2], v[i][3]); 274 if (util_is_inf_or_nan(v[i][0])) { 275 debug_printf(" NaN!\n"); 276 } 277 } 278} 279#endif 280 281 282/** 283 * Sort the vertices from top to bottom order, setting up the triangle 284 * edge fields (ebot, emaj, etop). 285 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise 286 */ 287static boolean 288setup_sort_vertices(struct setup_context *setup, 289 float det, 290 const float (*v0)[4], 291 const float (*v1)[4], 292 const float (*v2)[4]) 293{ 294 if (setup->softpipe->rasterizer->flatshade_first) 295 setup->vprovoke = v0; 296 else 297 setup->vprovoke = v2; 298 299 /* determine bottom to top order of vertices */ 300 { 301 float y0 = v0[0][1]; 302 float y1 = v1[0][1]; 303 float y2 = v2[0][1]; 304 if (y0 <= y1) { 305 if (y1 <= y2) { 306 /* y0<=y1<=y2 */ 307 setup->vmin = v0; 308 setup->vmid = v1; 309 setup->vmax = v2; 310 } 311 else if (y2 <= y0) { 312 /* y2<=y0<=y1 */ 313 setup->vmin = v2; 314 setup->vmid = v0; 315 setup->vmax = v1; 316 } 317 else { 318 /* y0<=y2<=y1 */ 319 setup->vmin = v0; 320 setup->vmid = v2; 321 setup->vmax = v1; 322 } 323 } 324 else { 325 if (y0 <= y2) { 326 /* y1<=y0<=y2 */ 327 setup->vmin = v1; 328 setup->vmid = v0; 329 setup->vmax = v2; 330 } 331 else if (y2 <= y1) { 332 /* y2<=y1<=y0 */ 333 setup->vmin = v2; 334 setup->vmid = v1; 335 setup->vmax = v0; 336 } 337 else { 338 /* y1<=y2<=y0 */ 339 setup->vmin = v1; 340 setup->vmid = v2; 341 setup->vmax = v0; 342 } 343 } 344 } 345 346 setup->ebot.dx = setup->vmid[0][0] - setup->vmin[0][0]; 347 setup->ebot.dy = setup->vmid[0][1] - setup->vmin[0][1]; 348 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0]; 349 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1]; 350 setup->etop.dx = setup->vmax[0][0] - setup->vmid[0][0]; 351 setup->etop.dy = setup->vmax[0][1] - setup->vmid[0][1]; 352 353 /* 354 * Compute triangle's area. Use 1/area to compute partial 355 * derivatives of attributes later. 356 * 357 * The area will be the same as prim->det, but the sign may be 358 * different depending on how the vertices get sorted above. 359 * 360 * To determine whether the primitive is front or back facing we 361 * use the prim->det value because its sign is correct. 362 */ 363 { 364 const float area = (setup->emaj.dx * setup->ebot.dy - 365 setup->ebot.dx * setup->emaj.dy); 366 367 setup->oneoverarea = 1.0f / area; 368 369 /* 370 debug_printf("%s one-over-area %f area %f det %f\n", 371 __FUNCTION__, setup->oneoverarea, area, det ); 372 */ 373 if (util_is_inf_or_nan(setup->oneoverarea)) 374 return FALSE; 375 } 376 377 /* We need to know if this is a front or back-facing triangle for: 378 * - the GLSL gl_FrontFacing fragment attribute (bool) 379 * - two-sided stencil test 380 * 0 = front-facing, 1 = back-facing 381 */ 382 setup->facing = 383 ((det < 0.0) ^ 384 (setup->softpipe->rasterizer->front_ccw)); 385 386 { 387 unsigned face = setup->facing == 0 ? PIPE_FACE_FRONT : PIPE_FACE_BACK; 388 389 if (face & setup->cull_face) 390 return FALSE; 391 } 392 393 return TRUE; 394} 395 396 397/* Apply cylindrical wrapping to v0, v1, v2 coordinates, if enabled. 398 * Input coordinates must be in [0, 1] range, otherwise results are undefined. 399 * Some combinations of coordinates produce invalid results, 400 * but this behaviour is acceptable. 401 */ 402static void 403tri_apply_cylindrical_wrap(float v0, 404 float v1, 405 float v2, 406 uint cylindrical_wrap, 407 float output[3]) 408{ 409 if (cylindrical_wrap) { 410 float delta; 411 412 delta = v1 - v0; 413 if (delta > 0.5f) { 414 v0 += 1.0f; 415 } 416 else if (delta < -0.5f) { 417 v1 += 1.0f; 418 } 419 420 delta = v2 - v1; 421 if (delta > 0.5f) { 422 v1 += 1.0f; 423 } 424 else if (delta < -0.5f) { 425 v2 += 1.0f; 426 } 427 428 delta = v0 - v2; 429 if (delta > 0.5f) { 430 v2 += 1.0f; 431 } 432 else if (delta < -0.5f) { 433 v0 += 1.0f; 434 } 435 } 436 437 output[0] = v0; 438 output[1] = v1; 439 output[2] = v2; 440} 441 442 443/** 444 * Compute a0 for a constant-valued coefficient (GL_FLAT shading). 445 * The value value comes from vertex[slot][i]. 446 * The result will be put into setup->coef[slot].a0[i]. 447 * \param slot which attribute slot 448 * \param i which component of the slot (0..3) 449 */ 450static void 451const_coeff(struct setup_context *setup, 452 struct tgsi_interp_coef *coef, 453 uint vertSlot, uint i) 454{ 455 assert(i <= 3); 456 457 coef->dadx[i] = 0; 458 coef->dady[i] = 0; 459 460 /* need provoking vertex info! 461 */ 462 coef->a0[i] = setup->vprovoke[vertSlot][i]; 463} 464 465 466/** 467 * Compute a0, dadx and dady for a linearly interpolated coefficient, 468 * for a triangle. 469 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively. 470 */ 471static void 472tri_linear_coeff(struct setup_context *setup, 473 struct tgsi_interp_coef *coef, 474 uint i, 475 const float v[3]) 476{ 477 float botda = v[1] - v[0]; 478 float majda = v[2] - v[0]; 479 float a = setup->ebot.dy * majda - botda * setup->emaj.dy; 480 float b = setup->emaj.dx * botda - majda * setup->ebot.dx; 481 float dadx = a * setup->oneoverarea; 482 float dady = b * setup->oneoverarea; 483 484 assert(i <= 3); 485 486 coef->dadx[i] = dadx; 487 coef->dady[i] = dady; 488 489 /* calculate a0 as the value which would be sampled for the 490 * fragment at (0,0), taking into account that we want to sample at 491 * pixel centers, in other words (pixel_offset, pixel_offset). 492 * 493 * this is neat but unfortunately not a good way to do things for 494 * triangles with very large values of dadx or dady as it will 495 * result in the subtraction and re-addition from a0 of a very 496 * large number, which means we'll end up loosing a lot of the 497 * fractional bits and precision from a0. the way to fix this is 498 * to define a0 as the sample at a pixel center somewhere near vmin 499 * instead - i'll switch to this later. 500 */ 501 coef->a0[i] = (v[0] - 502 (dadx * (setup->vmin[0][0] - setup->pixel_offset) + 503 dady * (setup->vmin[0][1] - setup->pixel_offset))); 504} 505 506 507/** 508 * Compute a0, dadx and dady for a perspective-corrected interpolant, 509 * for a triangle. 510 * We basically multiply the vertex value by 1/w before computing 511 * the plane coefficients (a0, dadx, dady). 512 * Later, when we compute the value at a particular fragment position we'll 513 * divide the interpolated value by the interpolated W at that fragment. 514 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively. 515 */ 516static void 517tri_persp_coeff(struct setup_context *setup, 518 struct tgsi_interp_coef *coef, 519 uint i, 520 const float v[3]) 521{ 522 /* premultiply by 1/w (v[0][3] is always W): 523 */ 524 float mina = v[0] * setup->vmin[0][3]; 525 float mida = v[1] * setup->vmid[0][3]; 526 float maxa = v[2] * setup->vmax[0][3]; 527 float botda = mida - mina; 528 float majda = maxa - mina; 529 float a = setup->ebot.dy * majda - botda * setup->emaj.dy; 530 float b = setup->emaj.dx * botda - majda * setup->ebot.dx; 531 float dadx = a * setup->oneoverarea; 532 float dady = b * setup->oneoverarea; 533 534 assert(i <= 3); 535 536 coef->dadx[i] = dadx; 537 coef->dady[i] = dady; 538 coef->a0[i] = (mina - 539 (dadx * (setup->vmin[0][0] - setup->pixel_offset) + 540 dady * (setup->vmin[0][1] - setup->pixel_offset))); 541} 542 543 544/** 545 * Special coefficient setup for gl_FragCoord. 546 * X and Y are trivial, though Y may have to be inverted for OpenGL. 547 * Z and W are copied from posCoef which should have already been computed. 548 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask. 549 */ 550static void 551setup_fragcoord_coeff(struct setup_context *setup, uint slot) 552{ 553 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info; 554 boolean origin_lower_left = 555 fsInfo->properties[TGSI_PROPERTY_FS_COORD_ORIGIN]; 556 boolean pixel_center_integer = 557 fsInfo->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER]; 558 559 /*X*/ 560 setup->coef[slot].a0[0] = pixel_center_integer ? 0.0f : 0.5f; 561 setup->coef[slot].dadx[0] = 1.0f; 562 setup->coef[slot].dady[0] = 0.0f; 563 /*Y*/ 564 setup->coef[slot].a0[1] = 565 (origin_lower_left ? setup->softpipe->framebuffer.height-1 : 0) 566 + (pixel_center_integer ? 0.0f : 0.5f); 567 setup->coef[slot].dadx[1] = 0.0f; 568 setup->coef[slot].dady[1] = origin_lower_left ? -1.0f : 1.0f; 569 /*Z*/ 570 setup->coef[slot].a0[2] = setup->posCoef.a0[2]; 571 setup->coef[slot].dadx[2] = setup->posCoef.dadx[2]; 572 setup->coef[slot].dady[2] = setup->posCoef.dady[2]; 573 /*W*/ 574 setup->coef[slot].a0[3] = setup->posCoef.a0[3]; 575 setup->coef[slot].dadx[3] = setup->posCoef.dadx[3]; 576 setup->coef[slot].dady[3] = setup->posCoef.dady[3]; 577} 578 579 580 581/** 582 * Compute the setup->coef[] array dadx, dady, a0 values. 583 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized. 584 */ 585static void 586setup_tri_coefficients(struct setup_context *setup) 587{ 588 struct softpipe_context *softpipe = setup->softpipe; 589 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info; 590 const struct sp_setup_info *sinfo = &softpipe->setup_info; 591 uint fragSlot; 592 float v[3]; 593 594 assert(sinfo->valid); 595 596 /* z and w are done by linear interpolation: 597 */ 598 v[0] = setup->vmin[0][2]; 599 v[1] = setup->vmid[0][2]; 600 v[2] = setup->vmax[0][2]; 601 tri_linear_coeff(setup, &setup->posCoef, 2, v); 602 603 v[0] = setup->vmin[0][3]; 604 v[1] = setup->vmid[0][3]; 605 v[2] = setup->vmax[0][3]; 606 tri_linear_coeff(setup, &setup->posCoef, 3, v); 607 608 /* setup interpolation for all the remaining attributes: 609 */ 610 for (fragSlot = 0; fragSlot < fsInfo->num_inputs; fragSlot++) { 611 const uint vertSlot = sinfo->attrib[fragSlot].src_index; 612 uint j; 613 614 switch (sinfo->attrib[fragSlot].interp) { 615 case SP_INTERP_CONSTANT: 616 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 617 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j); 618 } 619 break; 620 case SP_INTERP_LINEAR: 621 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 622 tri_apply_cylindrical_wrap(setup->vmin[vertSlot][j], 623 setup->vmid[vertSlot][j], 624 setup->vmax[vertSlot][j], 625 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j), 626 v); 627 tri_linear_coeff(setup, &setup->coef[fragSlot], j, v); 628 } 629 break; 630 case SP_INTERP_PERSPECTIVE: 631 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 632 tri_apply_cylindrical_wrap(setup->vmin[vertSlot][j], 633 setup->vmid[vertSlot][j], 634 setup->vmax[vertSlot][j], 635 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j), 636 v); 637 tri_persp_coeff(setup, &setup->coef[fragSlot], j, v); 638 } 639 break; 640 case SP_INTERP_POS: 641 setup_fragcoord_coeff(setup, fragSlot); 642 break; 643 default: 644 assert(0); 645 } 646 647 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) { 648 /* convert 0 to 1.0 and 1 to -1.0 */ 649 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f; 650 setup->coef[fragSlot].dadx[0] = 0.0; 651 setup->coef[fragSlot].dady[0] = 0.0; 652 } 653 654 if (0) { 655 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 656 debug_printf("attr[%d].%c: a0:%f dx:%f dy:%f\n", 657 fragSlot, "xyzw"[j], 658 setup->coef[fragSlot].a0[j], 659 setup->coef[fragSlot].dadx[j], 660 setup->coef[fragSlot].dady[j]); 661 } 662 } 663 } 664} 665 666 667static void 668setup_tri_edges(struct setup_context *setup) 669{ 670 float vmin_x = setup->vmin[0][0] + setup->pixel_offset; 671 float vmid_x = setup->vmid[0][0] + setup->pixel_offset; 672 673 float vmin_y = setup->vmin[0][1] - setup->pixel_offset; 674 float vmid_y = setup->vmid[0][1] - setup->pixel_offset; 675 float vmax_y = setup->vmax[0][1] - setup->pixel_offset; 676 677 setup->emaj.sy = ceilf(vmin_y); 678 setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy); 679 setup->emaj.dxdy = setup->emaj.dy ? setup->emaj.dx / setup->emaj.dy : .0f; 680 setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy; 681 682 setup->etop.sy = ceilf(vmid_y); 683 setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy); 684 setup->etop.dxdy = setup->etop.dy ? setup->etop.dx / setup->etop.dy : .0f; 685 setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy; 686 687 setup->ebot.sy = ceilf(vmin_y); 688 setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy); 689 setup->ebot.dxdy = setup->ebot.dy ? setup->ebot.dx / setup->ebot.dy : .0f; 690 setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy; 691} 692 693 694/** 695 * Render the upper or lower half of a triangle. 696 * Scissoring/cliprect is applied here too. 697 */ 698static void 699subtriangle(struct setup_context *setup, 700 struct edge *eleft, 701 struct edge *eright, 702 int lines, 703 unsigned viewport_index) 704{ 705 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect[viewport_index]; 706 const int minx = (int) cliprect->minx; 707 const int maxx = (int) cliprect->maxx; 708 const int miny = (int) cliprect->miny; 709 const int maxy = (int) cliprect->maxy; 710 int y, start_y, finish_y; 711 int sy = (int)eleft->sy; 712 713 assert((int)eleft->sy == (int) eright->sy); 714 assert(lines >= 0); 715 716 /* clip top/bottom */ 717 start_y = sy; 718 if (start_y < miny) 719 start_y = miny; 720 721 finish_y = sy + lines; 722 if (finish_y > maxy) 723 finish_y = maxy; 724 725 start_y -= sy; 726 finish_y -= sy; 727 728 /* 729 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y); 730 */ 731 732 for (y = start_y; y < finish_y; y++) { 733 734 /* avoid accumulating adds as floats don't have the precision to 735 * accurately iterate large triangle edges that way. luckily we 736 * can just multiply these days. 737 * 738 * this is all drowned out by the attribute interpolation anyway. 739 */ 740 int left = (int)(eleft->sx + y * eleft->dxdy); 741 int right = (int)(eright->sx + y * eright->dxdy); 742 743 /* clip left/right */ 744 if (left < minx) 745 left = minx; 746 if (right > maxx) 747 right = maxx; 748 749 if (left < right) { 750 int _y = sy + y; 751 if (block(_y) != setup->span.y) { 752 flush_spans(setup); 753 setup->span.y = block(_y); 754 } 755 756 setup->span.left[_y&1] = left; 757 setup->span.right[_y&1] = right; 758 } 759 } 760 761 762 /* save the values so that emaj can be restarted: 763 */ 764 eleft->sx += lines * eleft->dxdy; 765 eright->sx += lines * eright->dxdy; 766 eleft->sy += lines; 767 eright->sy += lines; 768} 769 770 771/** 772 * Recalculate prim's determinant. This is needed as we don't have 773 * get this information through the vbuf_render interface & we must 774 * calculate it here. 775 */ 776static float 777calc_det(const float (*v0)[4], 778 const float (*v1)[4], 779 const float (*v2)[4]) 780{ 781 /* edge vectors e = v0 - v2, f = v1 - v2 */ 782 const float ex = v0[0][0] - v2[0][0]; 783 const float ey = v0[0][1] - v2[0][1]; 784 const float fx = v1[0][0] - v2[0][0]; 785 const float fy = v1[0][1] - v2[0][1]; 786 787 /* det = cross(e,f).z */ 788 return ex * fy - ey * fx; 789} 790 791 792/** 793 * Do setup for triangle rasterization, then render the triangle. 794 */ 795void 796sp_setup_tri(struct setup_context *setup, 797 const float (*v0)[4], 798 const float (*v1)[4], 799 const float (*v2)[4]) 800{ 801 float det; 802 uint layer = 0; 803 unsigned viewport_index = 0; 804#if DEBUG_VERTS 805 debug_printf("Setup triangle:\n"); 806 print_vertex(setup, v0); 807 print_vertex(setup, v1); 808 print_vertex(setup, v2); 809#endif 810 811 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard) 812 return; 813 814 det = calc_det(v0, v1, v2); 815 /* 816 debug_printf("%s\n", __FUNCTION__ ); 817 */ 818 819#if DEBUG_FRAGS 820 setup->numFragsEmitted = 0; 821 setup->numFragsWritten = 0; 822#endif 823 824 if (!setup_sort_vertices( setup, det, v0, v1, v2 )) 825 return; 826 827 setup_tri_coefficients( setup ); 828 setup_tri_edges( setup ); 829 830 assert(setup->softpipe->reduced_prim == PIPE_PRIM_TRIANGLES); 831 832 setup->span.y = 0; 833 setup->span.right[0] = 0; 834 setup->span.right[1] = 0; 835 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */ 836 if (setup->softpipe->layer_slot > 0) { 837 layer = *(unsigned *)setup->vprovoke[setup->softpipe->layer_slot]; 838 layer = MIN2(layer, setup->max_layer); 839 } 840 setup->quad[0].input.layer = layer; 841 842 if (setup->softpipe->viewport_index_slot > 0) { 843 unsigned *udata = (unsigned*)v0[setup->softpipe->viewport_index_slot]; 844 viewport_index = sp_clamp_viewport_idx(*udata); 845 } 846 setup->quad[0].input.viewport_index = viewport_index; 847 848 /* init_constant_attribs( setup ); */ 849 850 if (setup->oneoverarea < 0.0) { 851 /* emaj on left: 852 */ 853 subtriangle(setup, &setup->emaj, &setup->ebot, setup->ebot.lines, viewport_index); 854 subtriangle(setup, &setup->emaj, &setup->etop, setup->etop.lines, viewport_index); 855 } 856 else { 857 /* emaj on right: 858 */ 859 subtriangle(setup, &setup->ebot, &setup->emaj, setup->ebot.lines, viewport_index); 860 subtriangle(setup, &setup->etop, &setup->emaj, setup->etop.lines, viewport_index); 861 } 862 863 flush_spans( setup ); 864 865 if (setup->softpipe->active_statistics_queries) { 866 setup->softpipe->pipeline_statistics.c_primitives++; 867 } 868 869#if DEBUG_FRAGS 870 printf("Tri: %u frags emitted, %u written\n", 871 setup->numFragsEmitted, 872 setup->numFragsWritten); 873#endif 874} 875 876 877/* Apply cylindrical wrapping to v0, v1 coordinates, if enabled. 878 * Input coordinates must be in [0, 1] range, otherwise results are undefined. 879 */ 880static void 881line_apply_cylindrical_wrap(float v0, 882 float v1, 883 uint cylindrical_wrap, 884 float output[2]) 885{ 886 if (cylindrical_wrap) { 887 float delta; 888 889 delta = v1 - v0; 890 if (delta > 0.5f) { 891 v0 += 1.0f; 892 } 893 else if (delta < -0.5f) { 894 v1 += 1.0f; 895 } 896 } 897 898 output[0] = v0; 899 output[1] = v1; 900} 901 902 903/** 904 * Compute a0, dadx and dady for a linearly interpolated coefficient, 905 * for a line. 906 * v[0] and v[1] are vmin and vmax, respectively. 907 */ 908static void 909line_linear_coeff(const struct setup_context *setup, 910 struct tgsi_interp_coef *coef, 911 uint i, 912 const float v[2]) 913{ 914 const float da = v[1] - v[0]; 915 const float dadx = da * setup->emaj.dx * setup->oneoverarea; 916 const float dady = da * setup->emaj.dy * setup->oneoverarea; 917 coef->dadx[i] = dadx; 918 coef->dady[i] = dady; 919 coef->a0[i] = (v[0] - 920 (dadx * (setup->vmin[0][0] - setup->pixel_offset) + 921 dady * (setup->vmin[0][1] - setup->pixel_offset))); 922} 923 924 925/** 926 * Compute a0, dadx and dady for a perspective-corrected interpolant, 927 * for a line. 928 * v[0] and v[1] are vmin and vmax, respectively. 929 */ 930static void 931line_persp_coeff(const struct setup_context *setup, 932 struct tgsi_interp_coef *coef, 933 uint i, 934 const float v[2]) 935{ 936 const float a0 = v[0] * setup->vmin[0][3]; 937 const float a1 = v[1] * setup->vmax[0][3]; 938 const float da = a1 - a0; 939 const float dadx = da * setup->emaj.dx * setup->oneoverarea; 940 const float dady = da * setup->emaj.dy * setup->oneoverarea; 941 coef->dadx[i] = dadx; 942 coef->dady[i] = dady; 943 coef->a0[i] = (a0 - 944 (dadx * (setup->vmin[0][0] - setup->pixel_offset) + 945 dady * (setup->vmin[0][1] - setup->pixel_offset))); 946} 947 948 949/** 950 * Compute the setup->coef[] array dadx, dady, a0 values. 951 * Must be called after setup->vmin,vmax are initialized. 952 */ 953static boolean 954setup_line_coefficients(struct setup_context *setup, 955 const float (*v0)[4], 956 const float (*v1)[4]) 957{ 958 struct softpipe_context *softpipe = setup->softpipe; 959 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info; 960 const struct sp_setup_info *sinfo = &softpipe->setup_info; 961 uint fragSlot; 962 float area; 963 float v[2]; 964 965 assert(sinfo->valid); 966 967 /* use setup->vmin, vmax to point to vertices */ 968 if (softpipe->rasterizer->flatshade_first) 969 setup->vprovoke = v0; 970 else 971 setup->vprovoke = v1; 972 setup->vmin = v0; 973 setup->vmax = v1; 974 975 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0]; 976 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1]; 977 978 /* NOTE: this is not really area but something proportional to it */ 979 area = setup->emaj.dx * setup->emaj.dx + setup->emaj.dy * setup->emaj.dy; 980 if (area == 0.0f || util_is_inf_or_nan(area)) 981 return FALSE; 982 setup->oneoverarea = 1.0f / area; 983 984 /* z and w are done by linear interpolation: 985 */ 986 v[0] = setup->vmin[0][2]; 987 v[1] = setup->vmax[0][2]; 988 line_linear_coeff(setup, &setup->posCoef, 2, v); 989 990 v[0] = setup->vmin[0][3]; 991 v[1] = setup->vmax[0][3]; 992 line_linear_coeff(setup, &setup->posCoef, 3, v); 993 994 /* setup interpolation for all the remaining attributes: 995 */ 996 for (fragSlot = 0; fragSlot < fsInfo->num_inputs; fragSlot++) { 997 const uint vertSlot = sinfo->attrib[fragSlot].src_index; 998 uint j; 999 1000 switch (sinfo->attrib[fragSlot].interp) { 1001 case SP_INTERP_CONSTANT: 1002 for (j = 0; j < TGSI_NUM_CHANNELS; j++) 1003 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j); 1004 break; 1005 case SP_INTERP_LINEAR: 1006 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 1007 line_apply_cylindrical_wrap(setup->vmin[vertSlot][j], 1008 setup->vmax[vertSlot][j], 1009 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j), 1010 v); 1011 line_linear_coeff(setup, &setup->coef[fragSlot], j, v); 1012 } 1013 break; 1014 case SP_INTERP_PERSPECTIVE: 1015 for (j = 0; j < TGSI_NUM_CHANNELS; j++) { 1016 line_apply_cylindrical_wrap(setup->vmin[vertSlot][j], 1017 setup->vmax[vertSlot][j], 1018 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j), 1019 v); 1020 line_persp_coeff(setup, &setup->coef[fragSlot], j, v); 1021 } 1022 break; 1023 case SP_INTERP_POS: 1024 setup_fragcoord_coeff(setup, fragSlot); 1025 break; 1026 default: 1027 assert(0); 1028 } 1029 1030 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) { 1031 /* convert 0 to 1.0 and 1 to -1.0 */ 1032 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f; 1033 setup->coef[fragSlot].dadx[0] = 0.0; 1034 setup->coef[fragSlot].dady[0] = 0.0; 1035 } 1036 } 1037 return TRUE; 1038} 1039 1040 1041/** 1042 * Plot a pixel in a line segment. 1043 */ 1044static inline void 1045plot(struct setup_context *setup, int x, int y) 1046{ 1047 const int iy = y & 1; 1048 const int ix = x & 1; 1049 const int quadX = x - ix; 1050 const int quadY = y - iy; 1051 const int mask = (1 << ix) << (2 * iy); 1052 1053 if (quadX != setup->quad[0].input.x0 || 1054 quadY != setup->quad[0].input.y0) 1055 { 1056 /* flush prev quad, start new quad */ 1057 1058 if (setup->quad[0].input.x0 != -1) 1059 clip_emit_quad(setup, &setup->quad[0]); 1060 1061 setup->quad[0].input.x0 = quadX; 1062 setup->quad[0].input.y0 = quadY; 1063 setup->quad[0].inout.mask = 0x0; 1064 } 1065 1066 setup->quad[0].inout.mask |= mask; 1067} 1068 1069 1070/** 1071 * Do setup for line rasterization, then render the line. 1072 * Single-pixel width, no stipple, etc. We rely on the 'draw' module 1073 * to handle stippling and wide lines. 1074 */ 1075void 1076sp_setup_line(struct setup_context *setup, 1077 const float (*v0)[4], 1078 const float (*v1)[4]) 1079{ 1080 int x0 = (int) v0[0][0]; 1081 int x1 = (int) v1[0][0]; 1082 int y0 = (int) v0[0][1]; 1083 int y1 = (int) v1[0][1]; 1084 int dx = x1 - x0; 1085 int dy = y1 - y0; 1086 int xstep, ystep; 1087 uint layer = 0; 1088 unsigned viewport_index = 0; 1089 1090#if DEBUG_VERTS 1091 debug_printf("Setup line:\n"); 1092 print_vertex(setup, v0); 1093 print_vertex(setup, v1); 1094#endif 1095 1096 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard) 1097 return; 1098 1099 if (dx == 0 && dy == 0) 1100 return; 1101 1102 if (!setup_line_coefficients(setup, v0, v1)) 1103 return; 1104 1105 assert(v0[0][0] < 1.0e9); 1106 assert(v0[0][1] < 1.0e9); 1107 assert(v1[0][0] < 1.0e9); 1108 assert(v1[0][1] < 1.0e9); 1109 1110 if (dx < 0) { 1111 dx = -dx; /* make positive */ 1112 xstep = -1; 1113 } 1114 else { 1115 xstep = 1; 1116 } 1117 1118 if (dy < 0) { 1119 dy = -dy; /* make positive */ 1120 ystep = -1; 1121 } 1122 else { 1123 ystep = 1; 1124 } 1125 1126 assert(dx >= 0); 1127 assert(dy >= 0); 1128 assert(setup->softpipe->reduced_prim == PIPE_PRIM_LINES); 1129 1130 setup->quad[0].input.x0 = setup->quad[0].input.y0 = -1; 1131 setup->quad[0].inout.mask = 0x0; 1132 if (setup->softpipe->layer_slot > 0) { 1133 layer = *(unsigned *)setup->vprovoke[setup->softpipe->layer_slot]; 1134 layer = MIN2(layer, setup->max_layer); 1135 } 1136 setup->quad[0].input.layer = layer; 1137 1138 if (setup->softpipe->viewport_index_slot > 0) { 1139 unsigned *udata = (unsigned*)setup->vprovoke[setup->softpipe->viewport_index_slot]; 1140 viewport_index = sp_clamp_viewport_idx(*udata); 1141 } 1142 setup->quad[0].input.viewport_index = viewport_index; 1143 1144 /* XXX temporary: set coverage to 1.0 so the line appears 1145 * if AA mode happens to be enabled. 1146 */ 1147 setup->quad[0].input.coverage[0] = 1148 setup->quad[0].input.coverage[1] = 1149 setup->quad[0].input.coverage[2] = 1150 setup->quad[0].input.coverage[3] = 1.0; 1151 1152 if (dx > dy) { 1153 /*** X-major line ***/ 1154 int i; 1155 const int errorInc = dy + dy; 1156 int error = errorInc - dx; 1157 const int errorDec = error - dx; 1158 1159 for (i = 0; i < dx; i++) { 1160 plot(setup, x0, y0); 1161 1162 x0 += xstep; 1163 if (error < 0) { 1164 error += errorInc; 1165 } 1166 else { 1167 error += errorDec; 1168 y0 += ystep; 1169 } 1170 } 1171 } 1172 else { 1173 /*** Y-major line ***/ 1174 int i; 1175 const int errorInc = dx + dx; 1176 int error = errorInc - dy; 1177 const int errorDec = error - dy; 1178 1179 for (i = 0; i < dy; i++) { 1180 plot(setup, x0, y0); 1181 1182 y0 += ystep; 1183 if (error < 0) { 1184 error += errorInc; 1185 } 1186 else { 1187 error += errorDec; 1188 x0 += xstep; 1189 } 1190 } 1191 } 1192 1193 /* draw final quad */ 1194 if (setup->quad[0].inout.mask) { 1195 clip_emit_quad(setup, &setup->quad[0]); 1196 } 1197} 1198 1199 1200static void 1201point_persp_coeff(const struct setup_context *setup, 1202 const float (*vert)[4], 1203 struct tgsi_interp_coef *coef, 1204 uint vertSlot, uint i) 1205{ 1206 assert(i <= 3); 1207 coef->dadx[i] = 0.0F; 1208 coef->dady[i] = 0.0F; 1209 coef->a0[i] = vert[vertSlot][i] * vert[0][3]; 1210} 1211 1212 1213/** 1214 * Do setup for point rasterization, then render the point. 1215 * Round or square points... 1216 * XXX could optimize a lot for 1-pixel points. 1217 */ 1218void 1219sp_setup_point(struct setup_context *setup, 1220 const float (*v0)[4]) 1221{ 1222 struct softpipe_context *softpipe = setup->softpipe; 1223 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info; 1224 const int sizeAttr = setup->softpipe->psize_slot; 1225 const float size 1226 = sizeAttr > 0 ? v0[sizeAttr][0] 1227 : setup->softpipe->rasterizer->point_size; 1228 const float halfSize = 0.5F * size; 1229 const boolean round = (boolean) setup->softpipe->rasterizer->point_smooth; 1230 const float x = v0[0][0]; /* Note: data[0] is always position */ 1231 const float y = v0[0][1]; 1232 const struct sp_setup_info *sinfo = &softpipe->setup_info; 1233 uint fragSlot; 1234 uint layer = 0; 1235 unsigned viewport_index = 0; 1236#if DEBUG_VERTS 1237 debug_printf("Setup point:\n"); 1238 print_vertex(setup, v0); 1239#endif 1240 1241 assert(sinfo->valid); 1242 1243 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard) 1244 return; 1245 1246 assert(setup->softpipe->reduced_prim == PIPE_PRIM_POINTS); 1247 1248 if (setup->softpipe->layer_slot > 0) { 1249 layer = *(unsigned *)v0[setup->softpipe->layer_slot]; 1250 layer = MIN2(layer, setup->max_layer); 1251 } 1252 setup->quad[0].input.layer = layer; 1253 1254 if (setup->softpipe->viewport_index_slot > 0) { 1255 unsigned *udata = (unsigned*)v0[setup->softpipe->viewport_index_slot]; 1256 viewport_index = sp_clamp_viewport_idx(*udata); 1257 } 1258 setup->quad[0].input.viewport_index = viewport_index; 1259 1260 /* For points, all interpolants are constant-valued. 1261 * However, for point sprites, we'll need to setup texcoords appropriately. 1262 * XXX: which coefficients are the texcoords??? 1263 * We may do point sprites as textured quads... 1264 * 1265 * KW: We don't know which coefficients are texcoords - ultimately 1266 * the choice of what interpolation mode to use for each attribute 1267 * should be determined by the fragment program, using 1268 * per-attribute declaration statements that include interpolation 1269 * mode as a parameter. So either the fragment program will have 1270 * to be adjusted for pointsprite vs normal point behaviour, or 1271 * otherwise a special interpolation mode will have to be defined 1272 * which matches the required behaviour for point sprites. But - 1273 * the latter is not a feature of normal hardware, and as such 1274 * probably should be ruled out on that basis. 1275 */ 1276 setup->vprovoke = v0; 1277 1278 /* setup Z, W */ 1279 const_coeff(setup, &setup->posCoef, 0, 2); 1280 const_coeff(setup, &setup->posCoef, 0, 3); 1281 1282 for (fragSlot = 0; fragSlot < fsInfo->num_inputs; fragSlot++) { 1283 const uint vertSlot = sinfo->attrib[fragSlot].src_index; 1284 uint j; 1285 1286 switch (sinfo->attrib[fragSlot].interp) { 1287 case SP_INTERP_CONSTANT: 1288 /* fall-through */ 1289 case SP_INTERP_LINEAR: 1290 for (j = 0; j < TGSI_NUM_CHANNELS; j++) 1291 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j); 1292 break; 1293 case SP_INTERP_PERSPECTIVE: 1294 for (j = 0; j < TGSI_NUM_CHANNELS; j++) 1295 point_persp_coeff(setup, setup->vprovoke, 1296 &setup->coef[fragSlot], vertSlot, j); 1297 break; 1298 case SP_INTERP_POS: 1299 setup_fragcoord_coeff(setup, fragSlot); 1300 break; 1301 default: 1302 assert(0); 1303 } 1304 1305 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) { 1306 /* convert 0 to 1.0 and 1 to -1.0 */ 1307 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f; 1308 setup->coef[fragSlot].dadx[0] = 0.0; 1309 setup->coef[fragSlot].dady[0] = 0.0; 1310 } 1311 } 1312 1313 1314 if (halfSize <= 0.5 && !round) { 1315 /* special case for 1-pixel points */ 1316 const int ix = ((int) x) & 1; 1317 const int iy = ((int) y) & 1; 1318 setup->quad[0].input.x0 = (int) x - ix; 1319 setup->quad[0].input.y0 = (int) y - iy; 1320 setup->quad[0].inout.mask = (1 << ix) << (2 * iy); 1321 clip_emit_quad(setup, &setup->quad[0]); 1322 } 1323 else { 1324 if (round) { 1325 /* rounded points */ 1326 const int ixmin = block((int) (x - halfSize)); 1327 const int ixmax = block((int) (x + halfSize)); 1328 const int iymin = block((int) (y - halfSize)); 1329 const int iymax = block((int) (y + halfSize)); 1330 const float rmin = halfSize - 0.7071F; /* 0.7071 = sqrt(2)/2 */ 1331 const float rmax = halfSize + 0.7071F; 1332 const float rmin2 = MAX2(0.0F, rmin * rmin); 1333 const float rmax2 = rmax * rmax; 1334 const float cscale = 1.0F / (rmax2 - rmin2); 1335 int ix, iy; 1336 1337 for (iy = iymin; iy <= iymax; iy += 2) { 1338 for (ix = ixmin; ix <= ixmax; ix += 2) { 1339 float dx, dy, dist2, cover; 1340 1341 setup->quad[0].inout.mask = 0x0; 1342 1343 dx = (ix + 0.5f) - x; 1344 dy = (iy + 0.5f) - y; 1345 dist2 = dx * dx + dy * dy; 1346 if (dist2 <= rmax2) { 1347 cover = 1.0F - (dist2 - rmin2) * cscale; 1348 setup->quad[0].input.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f); 1349 setup->quad[0].inout.mask |= MASK_TOP_LEFT; 1350 } 1351 1352 dx = (ix + 1.5f) - x; 1353 dy = (iy + 0.5f) - y; 1354 dist2 = dx * dx + dy * dy; 1355 if (dist2 <= rmax2) { 1356 cover = 1.0F - (dist2 - rmin2) * cscale; 1357 setup->quad[0].input.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f); 1358 setup->quad[0].inout.mask |= MASK_TOP_RIGHT; 1359 } 1360 1361 dx = (ix + 0.5f) - x; 1362 dy = (iy + 1.5f) - y; 1363 dist2 = dx * dx + dy * dy; 1364 if (dist2 <= rmax2) { 1365 cover = 1.0F - (dist2 - rmin2) * cscale; 1366 setup->quad[0].input.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f); 1367 setup->quad[0].inout.mask |= MASK_BOTTOM_LEFT; 1368 } 1369 1370 dx = (ix + 1.5f) - x; 1371 dy = (iy + 1.5f) - y; 1372 dist2 = dx * dx + dy * dy; 1373 if (dist2 <= rmax2) { 1374 cover = 1.0F - (dist2 - rmin2) * cscale; 1375 setup->quad[0].input.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f); 1376 setup->quad[0].inout.mask |= MASK_BOTTOM_RIGHT; 1377 } 1378 1379 if (setup->quad[0].inout.mask) { 1380 setup->quad[0].input.x0 = ix; 1381 setup->quad[0].input.y0 = iy; 1382 clip_emit_quad(setup, &setup->quad[0]); 1383 } 1384 } 1385 } 1386 } 1387 else { 1388 /* square points */ 1389 const int xmin = (int) (x + 0.75 - halfSize); 1390 const int ymin = (int) (y + 0.25 - halfSize); 1391 const int xmax = xmin + (int) size; 1392 const int ymax = ymin + (int) size; 1393 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */ 1394 const int ixmin = block(xmin); 1395 const int ixmax = block(xmax - 1); 1396 const int iymin = block(ymin); 1397 const int iymax = block(ymax - 1); 1398 int ix, iy; 1399 1400 /* 1401 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax); 1402 */ 1403 for (iy = iymin; iy <= iymax; iy += 2) { 1404 uint rowMask = 0xf; 1405 if (iy < ymin) { 1406 /* above the top edge */ 1407 rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT); 1408 } 1409 if (iy + 1 >= ymax) { 1410 /* below the bottom edge */ 1411 rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT); 1412 } 1413 1414 for (ix = ixmin; ix <= ixmax; ix += 2) { 1415 uint mask = rowMask; 1416 1417 if (ix < xmin) { 1418 /* fragment is past left edge of point, turn off left bits */ 1419 mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT); 1420 } 1421 if (ix + 1 >= xmax) { 1422 /* past the right edge */ 1423 mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT); 1424 } 1425 1426 setup->quad[0].inout.mask = mask; 1427 setup->quad[0].input.x0 = ix; 1428 setup->quad[0].input.y0 = iy; 1429 clip_emit_quad(setup, &setup->quad[0]); 1430 } 1431 } 1432 } 1433 } 1434} 1435 1436 1437/** 1438 * Called by vbuf code just before we start buffering primitives. 1439 */ 1440void 1441sp_setup_prepare(struct setup_context *setup) 1442{ 1443 struct softpipe_context *sp = setup->softpipe; 1444 int i; 1445 unsigned max_layer = ~0; 1446 if (sp->dirty) { 1447 softpipe_update_derived(sp, sp->reduced_api_prim); 1448 } 1449 1450 /* Note: nr_attrs is only used for debugging (vertex printing) */ 1451 setup->nr_vertex_attrs = draw_num_shader_outputs(sp->draw); 1452 1453 /* 1454 * Determine how many layers the fb has (used for clamping layer value). 1455 * OpenGL (but not d3d10) permits different amount of layers per rt, however 1456 * results are undefined if layer exceeds the amount of layers of ANY 1457 * attachment hence don't need separate per cbuf and zsbuf max. 1458 */ 1459 for (i = 0; i < setup->softpipe->framebuffer.nr_cbufs; i++) { 1460 struct pipe_surface *cbuf = setup->softpipe->framebuffer.cbufs[i]; 1461 if (cbuf) { 1462 max_layer = MIN2(max_layer, 1463 cbuf->u.tex.last_layer - cbuf->u.tex.first_layer); 1464 1465 } 1466 } 1467 1468 /* Prepare pixel offset for rasterisation: 1469 * - pixel center (0.5, 0.5) for GL, or 1470 * - assume (0.0, 0.0) for other APIs. 1471 */ 1472 if (setup->softpipe->rasterizer->half_pixel_center) { 1473 setup->pixel_offset = 0.5f; 1474 } else { 1475 setup->pixel_offset = 0.0f; 1476 } 1477 1478 setup->max_layer = max_layer; 1479 1480 sp->quad.first->begin( sp->quad.first ); 1481 1482 if (sp->reduced_api_prim == PIPE_PRIM_TRIANGLES && 1483 sp->rasterizer->fill_front == PIPE_POLYGON_MODE_FILL && 1484 sp->rasterizer->fill_back == PIPE_POLYGON_MODE_FILL) { 1485 /* we'll do culling */ 1486 setup->cull_face = sp->rasterizer->cull_face; 1487 } 1488 else { 1489 /* 'draw' will do culling */ 1490 setup->cull_face = PIPE_FACE_NONE; 1491 } 1492} 1493 1494 1495void 1496sp_setup_destroy_context(struct setup_context *setup) 1497{ 1498 FREE( setup ); 1499} 1500 1501 1502/** 1503 * Create a new primitive setup/render stage. 1504 */ 1505struct setup_context * 1506sp_setup_create_context(struct softpipe_context *softpipe) 1507{ 1508 struct setup_context *setup = CALLOC_STRUCT(setup_context); 1509 unsigned i; 1510 1511 setup->softpipe = softpipe; 1512 1513 for (i = 0; i < MAX_QUADS; i++) { 1514 setup->quad[i].coef = setup->coef; 1515 setup->quad[i].posCoef = &setup->posCoef; 1516 } 1517 1518 setup->span.left[0] = 1000000; /* greater than right[0] */ 1519 setup->span.left[1] = 1000000; /* greater than right[1] */ 1520 1521 return setup; 1522} 1523