src/emu/video/polynew.c
| r1282 | r1283 | |
| 1 | | /*************************************************************************** |
| 2 | | |
| 3 | | polynew.c |
| 4 | | |
| 5 | | Helper routines for polygon rendering. |
| 6 | | |
| 7 | | ***************************************************************************/ |
| 8 | | |
| 9 | | #include "polynew.h" |
| 10 | | #include "eminline.h" |
| 11 | | #include "mame.h" |
| 12 | | #include <math.h> |
| 13 | | |
| 14 | | |
| 15 | | /*************************************************************************** |
| 16 | | DEBUGGING |
| 17 | | ***************************************************************************/ |
| 18 | | |
| 19 | | /* keep statistics */ |
| 20 | | #define KEEP_STATISTICS 0 |
| 21 | | |
| 22 | | /* turn this on to log the reasons for any long waits */ |
| 23 | | #define LOG_WAITS 0 |
| 24 | | |
| 25 | | /* number of profiling ticks before we consider a wait "long" */ |
| 26 | | #define LOG_WAIT_THRESHOLD 1000 |
| 27 | | |
| 28 | | |
| 29 | | |
| 30 | | /*************************************************************************** |
| 31 | | CONSTANTS |
| 32 | | ***************************************************************************/ |
| 33 | | |
| 34 | | #define SCANLINES_PER_BUCKET 8 |
| 35 | | #define CACHE_LINE_SIZE 64 /* this is a general guess */ |
| 36 | | #define TOTAL_BUCKETS (512 / SCANLINES_PER_BUCKET) |
| 37 | | #define UNITS_PER_POLY (100 / SCANLINES_PER_BUCKET) |
| 38 | | |
| 39 | | |
| 40 | | |
| 41 | | /*************************************************************************** |
| 42 | | TYPE DEFINITIONS |
| 43 | | ***************************************************************************/ |
| 44 | | |
| 45 | | /* forward definitions */ |
| 46 | | typedef struct _polygon_info polygon_info; |
| 47 | | |
| 48 | | |
| 49 | | /* poly edge is used internally for quad rendering */ |
| 50 | | typedef struct _poly_edge poly_edge; |
| 51 | | struct _poly_edge |
| 52 | | { |
| 53 | | poly_edge * next; /* next edge in sequence */ |
| 54 | | int index; /* index of this edge */ |
| 55 | | const poly_vertex * v1; /* pointer to first vertex */ |
| 56 | | const poly_vertex * v2; /* pointer to second vertex */ |
| 57 | | float dxdy; /* dx/dy along the edge */ |
| 58 | | float dpdy[MAX_VERTEX_PARAMS];/* per-parameter dp/dy values */ |
| 59 | | }; |
| 60 | | |
| 61 | | |
| 62 | | /* poly section is used internally for quad rendering */ |
| 63 | | typedef struct _poly_section poly_section; |
| 64 | | struct _poly_section |
| 65 | | { |
| 66 | | const poly_edge * ledge; /* pointer to left edge */ |
| 67 | | const poly_edge * redge; /* pointer to right edge */ |
| 68 | | float ybottom; /* bottom of this section */ |
| 69 | | }; |
| 70 | | |
| 71 | | |
| 72 | | /* work_unit_shared is a common set of data shared between tris and quads */ |
| 73 | | typedef struct _work_unit_shared work_unit_shared; |
| 74 | | struct _work_unit_shared |
| 75 | | { |
| 76 | | polygon_info * polygon; /* pointer to polygon */ |
| 77 | | volatile UINT32 count_next; /* number of scanlines and index of next item to process */ |
| 78 | | INT16 scanline; /* starting scanline and count */ |
| 79 | | UINT16 previtem; /* index of previous item in the same bucket */ |
| 80 | | #ifndef PTR64 |
| 81 | | UINT32 dummy; /* pad to 16 bytes */ |
| 82 | | #endif |
| 83 | | }; |
| 84 | | |
| 85 | | |
| 86 | | /* tri_work_unit is a triangle-specific work-unit */ |
| 87 | | typedef struct _tri_work_unit tri_work_unit; |
| 88 | | struct _tri_work_unit |
| 89 | | { |
| 90 | | work_unit_shared shared; /* shared data */ |
| 91 | | tri_extent extent[SCANLINES_PER_BUCKET]; /* array of scanline extents */ |
| 92 | | }; |
| 93 | | |
| 94 | | |
| 95 | | /* quad_work_unit is a quad-specific work-unit */ |
| 96 | | typedef struct _quad_work_unit quad_work_unit; |
| 97 | | struct _quad_work_unit |
| 98 | | { |
| 99 | | work_unit_shared shared; /* shared data */ |
| 100 | | quad_extent extent[SCANLINES_PER_BUCKET]; /* array of scanline extents */ |
| 101 | | }; |
| 102 | | |
| 103 | | |
| 104 | | /* work_unit is a union of the two types */ |
| 105 | | typedef union _work_unit work_unit; |
| 106 | | union _work_unit |
| 107 | | { |
| 108 | | work_unit_shared shared; /* shared data */ |
| 109 | | tri_work_unit tri; /* triangle work unit */ |
| 110 | | quad_work_unit quad; /* quad work unit */ |
| 111 | | }; |
| 112 | | |
| 113 | | |
| 114 | | /* polygon_info describes a single polygon, which includes the poly_params */ |
| 115 | | struct _polygon_info |
| 116 | | { |
| 117 | | poly_manager * poly; /* pointer back to the poly manager */ |
| 118 | | void * dest; /* pointer to the destination we are rendering to */ |
| 119 | | void * extra; /* extra data pointer */ |
| 120 | | poly_draw_tri_scanline tricallback; /* callback to handle a scanline's worth of work */ |
| 121 | | poly_draw_quad_scanline quadcallback; /* callback to handle a scanline's worth of work */ |
| 122 | | poly_params params; /* poly_params structure */ |
| 123 | | }; |
| 124 | | |
| 125 | | |
| 126 | | /* full poly manager description */ |
| 127 | | struct _poly_manager |
| 128 | | { |
| 129 | | /* queue management */ |
| 130 | | osd_work_queue * queue; /* work queue */ |
| 131 | | |
| 132 | | /* triangle work units */ |
| 133 | | work_unit ** unit; /* array of work unit pointers */ |
| 134 | | UINT32 unit_next; /* index of next unit to allocate */ |
| 135 | | UINT32 unit_count; /* number of work units available */ |
| 136 | | size_t unit_size; /* size of each work unit, in bytes */ |
| 137 | | |
| 138 | | /* quad work units */ |
| 139 | | UINT32 quadunit_next; /* index of next unit to allocate */ |
| 140 | | UINT32 quadunit_count; /* number of work units available */ |
| 141 | | size_t quadunit_size; /* size of each work unit, in bytes */ |
| 142 | | |
| 143 | | /* poly data */ |
| 144 | | polygon_info ** polygon; /* array of polygon pointers */ |
| 145 | | UINT32 polygon_next; /* index of next polygon to allocate */ |
| 146 | | UINT32 polygon_count; /* number of polygon items available */ |
| 147 | | size_t polygon_size; /* size of each polygon, in bytes */ |
| 148 | | |
| 149 | | /* extra data */ |
| 150 | | void ** extra; /* array of extra data pointers */ |
| 151 | | UINT32 extra_next; /* index of next extra data to allocate */ |
| 152 | | UINT32 extra_count; /* number of extra data items available */ |
| 153 | | size_t extra_size; /* size of each extra data, in bytes */ |
| 154 | | |
| 155 | | /* misc data */ |
| 156 | | UINT8 flags; /* flags */ |
| 157 | | |
| 158 | | /* buckets */ |
| 159 | | UINT16 unit_bucket[TOTAL_BUCKETS]; /* buckets for tracking unit usage */ |
| 160 | | |
| 161 | | /* statistics */ |
| 162 | | UINT32 triangles; /* number of triangles queued */ |
| 163 | | UINT32 quads; /* number of quads queued */ |
| 164 | | UINT64 pixels; /* number of pixels rendered */ |
| 165 | | #if KEEP_STATISTICS |
| 166 | | UINT32 unit_waits; /* number of times we waited for a unit */ |
| 167 | | UINT32 unit_max; /* maximum units used */ |
| 168 | | UINT32 polygon_waits; /* number of times we waited for a polygon */ |
| 169 | | UINT32 polygon_max; /* maximum polygons used */ |
| 170 | | UINT32 extra_waits; /* number of times we waited for an extra data */ |
| 171 | | UINT32 extra_max; /* maximum extra data used */ |
| 172 | | UINT32 conflicts[WORK_MAX_THREADS]; /* number of conflicts found, per thread */ |
| 173 | | UINT32 resolved[WORK_MAX_THREADS]; /* number of conflicts resolved, per thread */ |
| 174 | | #endif |
| 175 | | }; |
| 176 | | |
| 177 | | |
| 178 | | |
| 179 | | /*************************************************************************** |
| 180 | | FUNCTION PROTOTYPES |
| 181 | | ***************************************************************************/ |
| 182 | | |
| 183 | | static void **allocate_array(size_t *itemsize, UINT32 itemcount); |
| 184 | | static void free_array(void **array); |
| 185 | | static void *poly_item_callback(void *param, int threadid); |
| 186 | | |
| 187 | | |
| 188 | | |
| 189 | | /*************************************************************************** |
| 190 | | INLINE FUNCTIONS |
| 191 | | ***************************************************************************/ |
| 192 | | |
| 193 | | /*------------------------------------------------- |
| 194 | | round_coordinate - round a coordinate to |
| 195 | | an integer, following rules that 0.5 rounds |
| 196 | | down |
| 197 | | -------------------------------------------------*/ |
| 198 | | |
| 199 | | INLINE INT32 round_coordinate(float value) |
| 200 | | { |
| 201 | | INT32 result = floor(value); |
| 202 | | return result + (value - (float)result > 0.5f); |
| 203 | | } |
| 204 | | |
| 205 | | |
| 206 | | /*------------------------------------------------- |
| 207 | | interpolate_vertex - interpolate values in |
| 208 | | a vertex based on p[0] crossing the clipval |
| 209 | | -------------------------------------------------*/ |
| 210 | | |
| 211 | | INLINE void interpolate_vertex(poly_vertex *outv, const poly_vertex *v1, const poly_vertex *v2, int paramcount, float clipval) |
| 212 | | { |
| 213 | | float frac = (clipval - v1->p[0]) / (v2->p[0] - v1->p[0]); |
| 214 | | int paramnum; |
| 215 | | |
| 216 | | /* create a new one at the intersection point */ |
| 217 | | outv->x = v1->x + frac * (v2->x - v1->x); |
| 218 | | outv->y = v1->y + frac * (v2->y - v1->y); |
| 219 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 220 | | outv->p[paramnum] = v1->p[paramnum] + frac * (v2->p[paramnum] - v1->p[paramnum]); |
| 221 | | } |
| 222 | | |
| 223 | | |
| 224 | | /*------------------------------------------------- |
| 225 | | copy_vertex - copy vertex data from one to |
| 226 | | another |
| 227 | | -------------------------------------------------*/ |
| 228 | | |
| 229 | | INLINE void copy_vertex(poly_vertex *outv, const poly_vertex *v, int paramcount) |
| 230 | | { |
| 231 | | int paramnum; |
| 232 | | |
| 233 | | outv->x = v->x; |
| 234 | | outv->y = v->y; |
| 235 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 236 | | outv->p[paramnum] = v->p[paramnum]; |
| 237 | | } |
| 238 | | |
| 239 | | |
| 240 | | /*------------------------------------------------- |
| 241 | | allocate_polygon - allocate a new polygon |
| 242 | | object, blocking if we run out |
| 243 | | -------------------------------------------------*/ |
| 244 | | |
| 245 | | INLINE polygon_info *allocate_polygon(poly_manager *poly, int miny, int maxy) |
| 246 | | { |
| 247 | | /* wait for a work item if we have to */ |
| 248 | | if (poly->polygon_next + 1 > poly->polygon_count) |
| 249 | | { |
| 250 | | poly_wait(poly, "Out of polygons"); |
| 251 | | #if KEEP_STATISTICS |
| 252 | | poly->polygon_waits++; |
| 253 | | #endif |
| 254 | | } |
| 255 | | else if (poly->unit_next + (maxy - miny) / SCANLINES_PER_BUCKET + 2 > poly->unit_count) |
| 256 | | { |
| 257 | | poly_wait(poly, "Out of work units"); |
| 258 | | #if KEEP_STATISTICS |
| 259 | | poly->unit_waits++; |
| 260 | | #endif |
| 261 | | } |
| 262 | | #if KEEP_STATISTICS |
| 263 | | poly->polygon_max = MAX(poly->polygon_max, poly->polygon_next + 1); |
| 264 | | #endif |
| 265 | | return poly->polygon[poly->polygon_next++]; |
| 266 | | } |
| 267 | | |
| 268 | | |
| 269 | | |
| 270 | | /*************************************************************************** |
| 271 | | INITIALIZATION/TEARDOWN |
| 272 | | ***************************************************************************/ |
| 273 | | |
| 274 | | /*------------------------------------------------- |
| 275 | | poly_alloc - initialize a new polygon |
| 276 | | manager |
| 277 | | -------------------------------------------------*/ |
| 278 | | |
| 279 | | poly_manager *poly_alloc(int max_polys, size_t extra_data_size, UINT8 flags) |
| 280 | | { |
| 281 | | poly_manager *poly; |
| 282 | | |
| 283 | | /* allocate the manager itself */ |
| 284 | | poly = malloc_or_die(sizeof(*poly)); |
| 285 | | memset(poly, 0, sizeof(*poly)); |
| 286 | | poly->flags = flags; |
| 287 | | |
| 288 | | /* allocate polygons */ |
| 289 | | poly->polygon_size = sizeof(polygon_info); |
| 290 | | poly->polygon_count = MAX(max_polys, 1); |
| 291 | | poly->polygon_next = 0; |
| 292 | | poly->polygon = (polygon_info **)allocate_array(&poly->polygon_size, poly->polygon_count); |
| 293 | | |
| 294 | | /* allocate extra data */ |
| 295 | | poly->extra_size = extra_data_size; |
| 296 | | poly->extra_count = poly->polygon_count; |
| 297 | | poly->extra_next = 1; |
| 298 | | poly->extra = allocate_array(&poly->extra_size, poly->extra_count); |
| 299 | | |
| 300 | | /* allocate triangle work units */ |
| 301 | | poly->unit_size = (flags & POLYFLAG_ALLOW_QUADS) ? sizeof(quad_work_unit) : sizeof(tri_work_unit); |
| 302 | | poly->unit_count = MIN(poly->polygon_count * UNITS_PER_POLY, 65535); |
| 303 | | poly->unit_next = 0; |
| 304 | | poly->unit = (work_unit **)allocate_array(&poly->unit_size, poly->unit_count); |
| 305 | | |
| 306 | | /* create the work queue */ |
| 307 | | if (!(flags & POLYFLAG_NO_WORK_QUEUE)) |
| 308 | | poly->queue = osd_work_queue_alloc(WORK_QUEUE_FLAG_MULTI | WORK_QUEUE_FLAG_HIGH_FREQ); |
| 309 | | return poly; |
| 310 | | } |
| 311 | | |
| 312 | | |
| 313 | | /*------------------------------------------------- |
| 314 | | poly_free - free a polygon manager |
| 315 | | -------------------------------------------------*/ |
| 316 | | |
| 317 | | void poly_free(poly_manager *poly) |
| 318 | | { |
| 319 | | #if KEEP_STATISTICS |
| 320 | | { |
| 321 | | int i, conflicts = 0, resolved = 0; |
| 322 | | for (i = 0; i < ARRAY_LENGTH(poly->conflicts); i++) |
| 323 | | { |
| 324 | | conflicts += poly->conflicts[i]; |
| 325 | | resolved += poly->resolved[i]; |
| 326 | | } |
| 327 | | printf("Total triangles = %d\n", poly->triangles); |
| 328 | | printf("Total quads = %d\n", poly->quads); |
| 329 | | if (poly->pixels > 1000000000) |
| 330 | | printf("Total pixels = %d%09d\n", (UINT32)(poly->pixels / 1000000000), (UINT32)(poly->pixels % 1000000000)); |
| 331 | | else |
| 332 | | printf("Total pixels = %d\n", (UINT32)poly->pixels); |
| 333 | | printf("Conflicts: %d resolved, %d total\n", resolved, conflicts); |
| 334 | | printf("Units: %5d used, %5d allocated, %5d waits, %4d bytes each, %7d total\n", poly->unit_max, poly->unit_count, poly->unit_waits, poly->unit_size, poly->unit_count * poly->unit_size); |
| 335 | | printf("Polygons: %5d used, %5d allocated, %5d waits, %4d bytes each, %7d total\n", poly->polygon_max, poly->polygon_count, poly->polygon_waits, poly->polygon_size, poly->polygon_count * poly->polygon_size); |
| 336 | | printf("Extra data: %5d used, %5d allocated, %5d waits, %4d bytes each, %7d total\n", poly->extra_max, poly->extra_count, poly->extra_waits, poly->extra_size, poly->extra_count * poly->extra_size); |
| 337 | | } |
| 338 | | #endif |
| 339 | | |
| 340 | | /* free the work queue */ |
| 341 | | if (poly->queue != NULL) |
| 342 | | osd_work_queue_free(poly->queue); |
| 343 | | |
| 344 | | /* free the arrays */ |
| 345 | | free_array(poly->extra); |
| 346 | | free_array((void **)poly->polygon); |
| 347 | | free_array((void **)poly->unit); |
| 348 | | |
| 349 | | /* free the manager itself */ |
| 350 | | free(poly); |
| 351 | | } |
| 352 | | |
| 353 | | |
| 354 | | |
| 355 | | /*************************************************************************** |
| 356 | | COMMON FUNCTIONS |
| 357 | | ***************************************************************************/ |
| 358 | | |
| 359 | | /*------------------------------------------------- |
| 360 | | poly_wait - wait for all pending rendering |
| 361 | | to complete |
| 362 | | -------------------------------------------------*/ |
| 363 | | |
| 364 | | void poly_wait(poly_manager *poly, const char *debug_reason) |
| 365 | | { |
| 366 | | osd_ticks_t time; |
| 367 | | |
| 368 | | /* remember the start time if we're logging */ |
| 369 | | if (LOG_WAITS) |
| 370 | | time = osd_profiling_ticks(); |
| 371 | | |
| 372 | | /* wait for all pending work items to complete */ |
| 373 | | if (poly->queue != NULL) |
| 374 | | osd_work_queue_wait(poly->queue, osd_ticks_per_second() * 100); |
| 375 | | |
| 376 | | /* if we don't have a queue, just run the whole list now */ |
| 377 | | else |
| 378 | | { |
| 379 | | int unitnum; |
| 380 | | for (unitnum = 0; unitnum < poly->unit_next; unitnum++) |
| 381 | | poly_item_callback(poly->unit[unitnum], 0); |
| 382 | | } |
| 383 | | |
| 384 | | /* log any long waits */ |
| 385 | | if (LOG_WAITS) |
| 386 | | { |
| 387 | | time = osd_profiling_ticks() - time; |
| 388 | | if (time > LOG_WAIT_THRESHOLD) |
| 389 | | logerror("Poly:Waited %d cycles for %s\n", (int)time, debug_reason); |
| 390 | | } |
| 391 | | |
| 392 | | /* reset the state */ |
| 393 | | poly->polygon_next = poly->unit_next = 0; |
| 394 | | memset(poly->unit_bucket, 0xff, sizeof(poly->unit_bucket)); |
| 395 | | |
| 396 | | /* we need to preserve the last extra data that was supplied */ |
| 397 | | if (poly->extra_next > 1) |
| 398 | | memcpy(poly->extra[0], poly->extra[poly->extra_next - 1], poly->extra_size); |
| 399 | | poly->extra_next = 1; |
| 400 | | } |
| 401 | | |
| 402 | | |
| 403 | | /*------------------------------------------------- |
| 404 | | poly_get_extra_data - get a pointer to the |
| 405 | | extra data for the next polygon |
| 406 | | -------------------------------------------------*/ |
| 407 | | |
| 408 | | void *poly_get_extra_data(poly_manager *poly) |
| 409 | | { |
| 410 | | /* wait for a work item if we have to */ |
| 411 | | if (poly->extra_next + 1 > poly->extra_count) |
| 412 | | { |
| 413 | | poly_wait(poly, "Out of extra data"); |
| 414 | | #if KEEP_STATISTICS |
| 415 | | poly->extra_waits++; |
| 416 | | #endif |
| 417 | | } |
| 418 | | |
| 419 | | /* return a pointer to the extra data for the next item */ |
| 420 | | #if KEEP_STATISTICS |
| 421 | | poly->extra_max = MAX(poly->extra_max, poly->extra_next + 1); |
| 422 | | #endif |
| 423 | | return poly->extra[poly->extra_next++]; |
| 424 | | } |
| 425 | | |
| 426 | | |
| 427 | | |
| 428 | | /*************************************************************************** |
| 429 | | CORE TRIANGLE RENDERING |
| 430 | | ***************************************************************************/ |
| 431 | | |
| 432 | | /*------------------------------------------------- |
| 433 | | poly_render_triangle - render a single |
| 434 | | triangle given 3 vertexes |
| 435 | | -------------------------------------------------*/ |
| 436 | | |
| 437 | | UINT32 poly_render_triangle(poly_manager *poly, void *dest, const rectangle *cliprect, poly_draw_tri_scanline callback, int paramcount, const poly_vertex *v1, const poly_vertex *v2, const poly_vertex *v3) |
| 438 | | { |
| 439 | | float dxdy_v1v2, dxdy_v1v3, dxdy_v2v3; |
| 440 | | const poly_vertex *tv; |
| 441 | | INT32 curscan, scaninc; |
| 442 | | polygon_info *polygon; |
| 443 | | INT32 v1yclip, v3yclip; |
| 444 | | INT32 v1y, v3y, v1x; |
| 445 | | INT32 pixels = 0; |
| 446 | | UINT32 startunit; |
| 447 | | |
| 448 | | /* first sort by Y */ |
| 449 | | if (v2->y < v1->y) |
| 450 | | { |
| 451 | | tv = v1; |
| 452 | | v1 = v2; |
| 453 | | v2 = tv; |
| 454 | | } |
| 455 | | if (v3->y < v2->y) |
| 456 | | { |
| 457 | | tv = v2; |
| 458 | | v2 = v3; |
| 459 | | v3 = tv; |
| 460 | | if (v2->y < v1->y) |
| 461 | | { |
| 462 | | tv = v1; |
| 463 | | v1 = v2; |
| 464 | | v2 = tv; |
| 465 | | } |
| 466 | | } |
| 467 | | |
| 468 | | /* compute some integral X/Y vertex values */ |
| 469 | | v1x = round_coordinate(v1->x); |
| 470 | | v1y = round_coordinate(v1->y); |
| 471 | | v3y = round_coordinate(v3->y); |
| 472 | | |
| 473 | | /* clip coordinates */ |
| 474 | | v1yclip = v1y; |
| 475 | | v3yclip = v3y + ((poly->flags & POLYFLAG_INCLUDE_BOTTOM_EDGE) ? 1 : 0); |
| 476 | | if (cliprect != NULL) |
| 477 | | { |
| 478 | | v1yclip = MAX(v1yclip, cliprect->min_y); |
| 479 | | v3yclip = MIN(v3yclip, cliprect->max_y + 1); |
| 480 | | } |
| 481 | | if (v3yclip - v1yclip <= 0) |
| 482 | | return 0; |
| 483 | | |
| 484 | | /* allocate a new polygon */ |
| 485 | | polygon = allocate_polygon(poly, v1yclip, v3yclip); |
| 486 | | |
| 487 | | /* fill in the polygon information */ |
| 488 | | polygon->poly = poly; |
| 489 | | polygon->dest = dest; |
| 490 | | polygon->tricallback = callback; |
| 491 | | polygon->quadcallback = NULL; |
| 492 | | polygon->extra = poly->extra[poly->extra_next - 1]; |
| 493 | | |
| 494 | | /* set the start X/Y coordinates */ |
| 495 | | polygon->params.xorigin = v1x; |
| 496 | | polygon->params.yorigin = v1y; |
| 497 | | |
| 498 | | /* compute the slopes for each portion of the triangle */ |
| 499 | | dxdy_v1v2 = (v2->y == v1->y) ? 0.0f : (v2->x - v1->x) / (v2->y - v1->y); |
| 500 | | dxdy_v1v3 = (v3->y == v1->y) ? 0.0f : (v3->x - v1->x) / (v3->y - v1->y); |
| 501 | | dxdy_v2v3 = (v3->y == v2->y) ? 0.0f : (v3->x - v2->x) / (v3->y - v2->y); |
| 502 | | |
| 503 | | /* compute the X extents for each scanline */ |
| 504 | | startunit = poly->unit_next; |
| 505 | | for (curscan = v1yclip; curscan < v3yclip; curscan += scaninc) |
| 506 | | { |
| 507 | | UINT32 bucketnum = ((UINT32)curscan / SCANLINES_PER_BUCKET) % TOTAL_BUCKETS; |
| 508 | | UINT32 unit_index = poly->unit_next++; |
| 509 | | tri_work_unit *unit = &poly->unit[unit_index]->tri; |
| 510 | | int extnum; |
| 511 | | |
| 512 | | /* determine how much to advance to hit the next bucket */ |
| 513 | | scaninc = SCANLINES_PER_BUCKET - (UINT32)curscan % SCANLINES_PER_BUCKET; |
| 514 | | |
| 515 | | /* fill in the work unit basics */ |
| 516 | | unit->shared.polygon = polygon; |
| 517 | | unit->shared.count_next = MIN(v3yclip - curscan, scaninc); |
| 518 | | unit->shared.scanline = curscan; |
| 519 | | unit->shared.previtem = poly->unit_bucket[bucketnum]; |
| 520 | | poly->unit_bucket[bucketnum] = unit_index; |
| 521 | | |
| 522 | | /* iterate over extents */ |
| 523 | | for (extnum = 0; extnum < unit->shared.count_next; extnum++) |
| 524 | | { |
| 525 | | float fully = (float)(curscan + extnum) + 0.5f; |
| 526 | | float startx = v1->x + (fully - v1->y) * dxdy_v1v3; |
| 527 | | float stopx; |
| 528 | | INT32 istartx, istopx; |
| 529 | | |
| 530 | | /* compute the ending X based on which part of the triangle we're in */ |
| 531 | | if (fully < v2->y) |
| 532 | | stopx = v1->x + (fully - v1->y) * dxdy_v1v2; |
| 533 | | else |
| 534 | | stopx = v2->x + (fully - v2->y) * dxdy_v2v3; |
| 535 | | |
| 536 | | /* clamp to full pixels */ |
| 537 | | istartx = round_coordinate(startx); |
| 538 | | istopx = round_coordinate(stopx); |
| 539 | | |
| 540 | | /* force start < stop */ |
| 541 | | if (istartx > istopx) |
| 542 | | { |
| 543 | | INT32 temp = istartx; |
| 544 | | istartx = istopx; |
| 545 | | istopx = temp; |
| 546 | | } |
| 547 | | |
| 548 | | /* include the right edge if requested */ |
| 549 | | if (poly->flags & POLYFLAG_INCLUDE_RIGHT_EDGE) |
| 550 | | istopx++; |
| 551 | | |
| 552 | | /* apply left/right clipping */ |
| 553 | | if (cliprect != NULL) |
| 554 | | { |
| 555 | | if (istartx < cliprect->min_x) |
| 556 | | istartx = cliprect->min_x; |
| 557 | | if (istopx > cliprect->max_x) |
| 558 | | istopx = cliprect->max_x + 1; |
| 559 | | } |
| 560 | | |
| 561 | | /* set the extent and update the total pixel count */ |
| 562 | | if (istartx >= istopx) |
| 563 | | istartx = istopx = 0; |
| 564 | | unit->extent[extnum].startx = istartx; |
| 565 | | unit->extent[extnum].stopx = istopx; |
| 566 | | pixels += istopx - istartx; |
| 567 | | } |
| 568 | | } |
| 569 | | #if KEEP_STATISTICS |
| 570 | | poly->unit_max = MAX(poly->unit_max, poly->unit_next); |
| 571 | | #endif |
| 572 | | |
| 573 | | /* compute parameter starting points and deltas */ |
| 574 | | if (paramcount > 0) |
| 575 | | { |
| 576 | | float divisor, dx1, dx2, dy1, dy2, xoffset, yoffset; |
| 577 | | int paramnum; |
| 578 | | |
| 579 | | /* compute the divisor */ |
| 580 | | divisor = 1.0f / ((v1->x - v2->x) * (v1->y - v3->y) - (v1->x - v3->x) * (v1->y - v2->y)); |
| 581 | | |
| 582 | | /* compute the dx/dy values */ |
| 583 | | dx1 = v1->y - v3->y; |
| 584 | | dx2 = v1->y - v2->y; |
| 585 | | dy1 = v1->x - v2->x; |
| 586 | | dy2 = v1->x - v3->x; |
| 587 | | |
| 588 | | /* determine x/y offset for subpixel correction so that the starting values are |
| 589 | | relative to the integer coordinates */ |
| 590 | | xoffset = v1->x - ((float)polygon->params.xorigin + 0.5f); |
| 591 | | yoffset = v1->y - ((float)polygon->params.yorigin + 0.5f); |
| 592 | | |
| 593 | | /* iterate over parameters */ |
| 594 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 595 | | { |
| 596 | | poly_param *params = &polygon->params.param[paramnum]; |
| 597 | | params->dpdx = ((v1->p[paramnum] - v2->p[paramnum]) * dx1 - (v1->p[paramnum] - v3->p[paramnum]) * dx2) * divisor; |
| 598 | | params->dpdy = ((v1->p[paramnum] - v3->p[paramnum]) * dy1 - (v1->p[paramnum] - v2->p[paramnum]) * dy2) * divisor; |
| 599 | | params->start = v1->p[paramnum] - xoffset * params->dpdx - yoffset * params->dpdy; |
| 600 | | } |
| 601 | | } |
| 602 | | |
| 603 | | /* enqueue the work items */ |
| 604 | | if (poly->queue != NULL) |
| 605 | | osd_work_item_queue_multiple(poly->queue, poly_item_callback, poly->unit_next - startunit, poly->unit[startunit], poly->unit_size, WORK_ITEM_FLAG_AUTO_RELEASE); |
| 606 | | |
| 607 | | /* return the total number of pixels in the triangle */ |
| 608 | | poly->triangles++; |
| 609 | | poly->pixels += pixels; |
| 610 | | return pixels; |
| 611 | | } |
| 612 | | |
| 613 | | |
| 614 | | /*------------------------------------------------- |
| 615 | | poly_render_triangle_fan - render a set of |
| 616 | | triangles in a fan |
| 617 | | -------------------------------------------------*/ |
| 618 | | |
| 619 | | UINT32 poly_render_triangle_fan(poly_manager *poly, void *dest, const rectangle *cliprect, poly_draw_tri_scanline callback, int paramcount, int numverts, const poly_vertex *v) |
| 620 | | { |
| 621 | | UINT32 pixels = 0; |
| 622 | | int vertnum; |
| 623 | | |
| 624 | | /* iterate over vertices */ |
| 625 | | for (vertnum = 2; vertnum < numverts; vertnum++) |
| 626 | | pixels += poly_render_triangle(poly, dest, cliprect, callback, paramcount, &v[0], &v[vertnum - 1], &v[vertnum]); |
| 627 | | return pixels; |
| 628 | | } |
| 629 | | |
| 630 | | |
| 631 | | /*------------------------------------------------- |
| 632 | | poly_render_triangle_custom - perform a custom |
| 633 | | render of an object, given specific extents |
| 634 | | -------------------------------------------------*/ |
| 635 | | |
| 636 | | UINT32 poly_render_triangle_custom(poly_manager *poly, void *dest, const rectangle *cliprect, poly_draw_tri_scanline callback, int startscanline, int numscanlines, const tri_extent *extents) |
| 637 | | { |
| 638 | | INT32 curscan, scaninc; |
| 639 | | polygon_info *polygon; |
| 640 | | INT32 v1yclip, v3yclip; |
| 641 | | INT32 pixels = 0; |
| 642 | | UINT32 startunit; |
| 643 | | |
| 644 | | /* clip coordinates */ |
| 645 | | if (cliprect != NULL) |
| 646 | | { |
| 647 | | v1yclip = MAX(startscanline, cliprect->min_y); |
| 648 | | v3yclip = MIN(startscanline + numscanlines, cliprect->max_y + 1); |
| 649 | | } |
| 650 | | else |
| 651 | | { |
| 652 | | v1yclip = startscanline; |
| 653 | | v3yclip = startscanline + numscanlines; |
| 654 | | } |
| 655 | | if (v3yclip - v1yclip <= 0) |
| 656 | | return 0; |
| 657 | | |
| 658 | | /* allocate a new polygon */ |
| 659 | | polygon = allocate_polygon(poly, v1yclip, v3yclip); |
| 660 | | |
| 661 | | /* fill in the polygon information */ |
| 662 | | polygon->poly = poly; |
| 663 | | polygon->dest = dest; |
| 664 | | polygon->tricallback = callback; |
| 665 | | polygon->quadcallback = NULL; |
| 666 | | polygon->extra = poly->extra[poly->extra_next - 1]; |
| 667 | | |
| 668 | | /* compute the X extents for each scanline */ |
| 669 | | startunit = poly->unit_next; |
| 670 | | for (curscan = v1yclip; curscan < v3yclip; curscan += scaninc) |
| 671 | | { |
| 672 | | UINT32 bucketnum = ((UINT32)curscan / SCANLINES_PER_BUCKET) % TOTAL_BUCKETS; |
| 673 | | UINT32 unit_index = poly->unit_next++; |
| 674 | | tri_work_unit *unit = &poly->unit[unit_index]->tri; |
| 675 | | int extnum; |
| 676 | | |
| 677 | | /* determine how much to advance to hit the next bucket */ |
| 678 | | scaninc = SCANLINES_PER_BUCKET - (UINT32)curscan % SCANLINES_PER_BUCKET; |
| 679 | | |
| 680 | | /* fill in the work unit basics */ |
| 681 | | unit->shared.polygon = polygon; |
| 682 | | unit->shared.count_next = MIN(v3yclip - curscan, scaninc); |
| 683 | | unit->shared.scanline = curscan; |
| 684 | | unit->shared.previtem = poly->unit_bucket[bucketnum]; |
| 685 | | poly->unit_bucket[bucketnum] = unit_index; |
| 686 | | |
| 687 | | /* iterate over extents */ |
| 688 | | for (extnum = 0; extnum < unit->shared.count_next; extnum++) |
| 689 | | { |
| 690 | | const tri_extent *extent = &extents[(curscan + extnum) - startscanline]; |
| 691 | | INT32 istartx = extent->startx, istopx = extent->stopx; |
| 692 | | |
| 693 | | /* force start < stop */ |
| 694 | | if (istartx > istopx) |
| 695 | | { |
| 696 | | INT32 temp = istartx; |
| 697 | | istartx = istopx; |
| 698 | | istopx = temp; |
| 699 | | } |
| 700 | | |
| 701 | | /* apply left/right clipping */ |
| 702 | | if (cliprect != NULL) |
| 703 | | { |
| 704 | | if (istartx < cliprect->min_x) |
| 705 | | istartx = cliprect->min_x; |
| 706 | | if (istopx > cliprect->max_x) |
| 707 | | istopx = cliprect->max_x + 1; |
| 708 | | } |
| 709 | | |
| 710 | | /* set the extent and update the total pixel count */ |
| 711 | | unit->extent[extnum].startx = istartx; |
| 712 | | unit->extent[extnum].stopx = istopx; |
| 713 | | if (istartx < istopx) |
| 714 | | pixels += istopx - istartx; |
| 715 | | } |
| 716 | | } |
| 717 | | #if KEEP_STATISTICS |
| 718 | | poly->unit_max = MAX(poly->unit_max, poly->unit_next); |
| 719 | | #endif |
| 720 | | |
| 721 | | /* enqueue the work items */ |
| 722 | | if (poly->queue != NULL) |
| 723 | | osd_work_item_queue_multiple(poly->queue, poly_item_callback, poly->unit_next - startunit, poly->unit[startunit], poly->unit_size, WORK_ITEM_FLAG_AUTO_RELEASE); |
| 724 | | |
| 725 | | /* return the total number of pixels in the object */ |
| 726 | | poly->triangles++; |
| 727 | | poly->pixels += pixels; |
| 728 | | return pixels; |
| 729 | | } |
| 730 | | |
| 731 | | |
| 732 | | |
| 733 | | /*************************************************************************** |
| 734 | | CORE QUAD RENDERING |
| 735 | | ***************************************************************************/ |
| 736 | | |
| 737 | | /*------------------------------------------------- |
| 738 | | poly_render_quad - render a single quad |
| 739 | | given 4 vertexes |
| 740 | | -------------------------------------------------*/ |
| 741 | | |
| 742 | | UINT32 poly_render_quad(poly_manager *poly, void *dest, const rectangle *cliprect, poly_draw_quad_scanline callback, int paramcount, const poly_vertex *v1, const poly_vertex *v2, const poly_vertex *v3, const poly_vertex *v4) |
| 743 | | { |
| 744 | | poly_edge fedgelist[3], bedgelist[3]; |
| 745 | | const poly_edge *ledge, *redge; |
| 746 | | const poly_vertex *v[4]; |
| 747 | | poly_edge *edgeptr; |
| 748 | | int minv, maxv, curv; |
| 749 | | INT32 minyclip, maxyclip; |
| 750 | | INT32 miny, maxy; |
| 751 | | INT32 curscan, scaninc; |
| 752 | | polygon_info *polygon; |
| 753 | | INT32 pixels = 0; |
| 754 | | UINT32 startunit; |
| 755 | | |
| 756 | | assert(poly->flags & POLYFLAG_ALLOW_QUADS); |
| 757 | | |
| 758 | | /* arrays make things easier */ |
| 759 | | v[0] = v1; |
| 760 | | v[1] = v2; |
| 761 | | v[2] = v3; |
| 762 | | v[3] = v4; |
| 763 | | |
| 764 | | /* determine min/max Y vertices */ |
| 765 | | if (v[1]->y < v[0]->y) |
| 766 | | minv = 1, maxv = 0; |
| 767 | | else |
| 768 | | minv = 0, maxv = 1; |
| 769 | | if (v[2]->y < v[minv]->y) |
| 770 | | minv = 2; |
| 771 | | else if (v[2]->y > v[maxv]->y) |
| 772 | | maxv = 2; |
| 773 | | if (v[3]->y < v[minv]->y) |
| 774 | | minv = 3; |
| 775 | | else if (v[3]->y > v[maxv]->y) |
| 776 | | maxv = 3; |
| 777 | | |
| 778 | | /* determine start/end scanlines */ |
| 779 | | miny = round_coordinate(v[minv]->y); |
| 780 | | maxy = round_coordinate(v[maxv]->y); |
| 781 | | |
| 782 | | /* clip coordinates */ |
| 783 | | minyclip = miny; |
| 784 | | maxyclip = maxy + ((poly->flags & POLYFLAG_INCLUDE_BOTTOM_EDGE) ? 1 : 0); |
| 785 | | if (cliprect != NULL) |
| 786 | | { |
| 787 | | minyclip = MAX(minyclip, cliprect->min_y); |
| 788 | | maxyclip = MIN(maxyclip, cliprect->max_y + 1); |
| 789 | | } |
| 790 | | if (maxyclip - minyclip <= 0) |
| 791 | | return 0; |
| 792 | | |
| 793 | | /* allocate a new polygon */ |
| 794 | | polygon = allocate_polygon(poly, minyclip, maxyclip); |
| 795 | | |
| 796 | | /* fill in the polygon information */ |
| 797 | | polygon->poly = poly; |
| 798 | | polygon->dest = dest; |
| 799 | | polygon->tricallback = NULL; |
| 800 | | polygon->quadcallback = callback; |
| 801 | | polygon->extra = poly->extra[poly->extra_next - 1]; |
| 802 | | |
| 803 | | /* walk forward to build up the forward edge list */ |
| 804 | | edgeptr = &fedgelist[0]; |
| 805 | | for (curv = minv; curv != maxv; curv = (curv + 1) & 3) |
| 806 | | { |
| 807 | | int paramnum; |
| 808 | | float ooy; |
| 809 | | |
| 810 | | /* set the two vertices */ |
| 811 | | edgeptr->v1 = v[curv]; |
| 812 | | edgeptr->v2 = v[(curv + 1) & 3]; |
| 813 | | |
| 814 | | /* if horizontal, skip altogether */ |
| 815 | | if (edgeptr->v1->y == edgeptr->v2->y) |
| 816 | | continue; |
| 817 | | |
| 818 | | /* need dx/dy always, and parameter deltas as necessary */ |
| 819 | | ooy = 1.0f / (edgeptr->v2->y - edgeptr->v1->y); |
| 820 | | edgeptr->dxdy = (edgeptr->v2->x - edgeptr->v1->x) * ooy; |
| 821 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 822 | | edgeptr->dpdy[paramnum] = (edgeptr->v2->p[paramnum] - edgeptr->v1->p[paramnum]) * ooy; |
| 823 | | edgeptr++; |
| 824 | | } |
| 825 | | |
| 826 | | /* walk backward to build up the backward edge list */ |
| 827 | | edgeptr = &bedgelist[0]; |
| 828 | | for (curv = minv; curv != maxv; curv = (curv - 1) & 3) |
| 829 | | { |
| 830 | | int paramnum; |
| 831 | | float ooy; |
| 832 | | |
| 833 | | /* set the two vertices */ |
| 834 | | edgeptr->v1 = v[curv]; |
| 835 | | edgeptr->v2 = v[(curv - 1) & 3]; |
| 836 | | |
| 837 | | /* if horizontal, skip altogether */ |
| 838 | | if (edgeptr->v1->y == edgeptr->v2->y) |
| 839 | | continue; |
| 840 | | |
| 841 | | /* need dx/dy always, and parameter deltas as necessary */ |
| 842 | | ooy = 1.0f / (edgeptr->v2->y - edgeptr->v1->y); |
| 843 | | edgeptr->dxdy = (edgeptr->v2->x - edgeptr->v1->x) * ooy; |
| 844 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 845 | | edgeptr->dpdy[paramnum] = (edgeptr->v2->p[paramnum] - edgeptr->v1->p[paramnum]) * ooy; |
| 846 | | edgeptr++; |
| 847 | | } |
| 848 | | |
| 849 | | /* determine which list is left/right: */ |
| 850 | | /* if the first vertex is shared, compare the slopes */ |
| 851 | | /* if the first vertex is not shared, compare the X coordinates */ |
| 852 | | if ((fedgelist[0].v1 == bedgelist[0].v1 && fedgelist[0].dxdy < bedgelist[0].dxdy) || |
| 853 | | (fedgelist[0].v1 != bedgelist[0].v1 && fedgelist[0].v1->x < bedgelist[0].v1->x)) |
| 854 | | { |
| 855 | | ledge = fedgelist; |
| 856 | | redge = bedgelist; |
| 857 | | } |
| 858 | | else |
| 859 | | { |
| 860 | | ledge = bedgelist; |
| 861 | | redge = fedgelist; |
| 862 | | } |
| 863 | | |
| 864 | | /* compute the X extents for each scanline */ |
| 865 | | startunit = poly->unit_next; |
| 866 | | for (curscan = minyclip; curscan < maxyclip; curscan += scaninc) |
| 867 | | { |
| 868 | | UINT32 bucketnum = ((UINT32)curscan / SCANLINES_PER_BUCKET) % TOTAL_BUCKETS; |
| 869 | | UINT32 unit_index = poly->unit_next++; |
| 870 | | quad_work_unit *unit = &poly->unit[unit_index]->quad; |
| 871 | | int extnum; |
| 872 | | |
| 873 | | /* determine how much to advance to hit the next bucket */ |
| 874 | | scaninc = SCANLINES_PER_BUCKET - (UINT32)curscan % SCANLINES_PER_BUCKET; |
| 875 | | |
| 876 | | /* fill in the work unit basics */ |
| 877 | | unit->shared.polygon = polygon; |
| 878 | | unit->shared.count_next = MIN(maxyclip - curscan, scaninc); |
| 879 | | unit->shared.scanline = curscan; |
| 880 | | unit->shared.previtem = poly->unit_bucket[bucketnum]; |
| 881 | | poly->unit_bucket[bucketnum] = unit_index; |
| 882 | | |
| 883 | | /* iterate over extents */ |
| 884 | | for (extnum = 0; extnum < unit->shared.count_next; extnum++) |
| 885 | | { |
| 886 | | float fully = (float)(curscan + extnum) + 0.5f; |
| 887 | | float startx, stopx; |
| 888 | | INT32 istartx, istopx; |
| 889 | | int paramnum; |
| 890 | | |
| 891 | | /* compute the ending X based on which part of the triangle we're in */ |
| 892 | | while (fully > ledge->v2->y && fully < v[maxv]->y) |
| 893 | | ledge++; |
| 894 | | while (fully > redge->v2->y && fully < v[maxv]->y) |
| 895 | | redge++; |
| 896 | | startx = ledge->v1->x + (fully - ledge->v1->y) * ledge->dxdy; |
| 897 | | stopx = redge->v1->x + (fully - redge->v1->y) * redge->dxdy; |
| 898 | | |
| 899 | | /* clamp to full pixels */ |
| 900 | | istartx = round_coordinate(startx); |
| 901 | | istopx = round_coordinate(stopx); |
| 902 | | |
| 903 | | /* compute parameter starting points and deltas */ |
| 904 | | if (paramcount > 0) |
| 905 | | { |
| 906 | | float ldy = fully - ledge->v1->y; |
| 907 | | float rdy = fully - redge->v1->y; |
| 908 | | float oox = 1.0f / (stopx - startx); |
| 909 | | |
| 910 | | /* iterate over parameters */ |
| 911 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 912 | | { |
| 913 | | float lparam = ledge->v1->p[paramnum] + ldy * ledge->dpdy[paramnum]; |
| 914 | | float rparam = redge->v1->p[paramnum] + rdy * redge->dpdy[paramnum]; |
| 915 | | float dpdx = (rparam - lparam) * oox; |
| 916 | | |
| 917 | | unit->extent[extnum].param[paramnum].start = lparam;// - ((float)istartx + 0.5f) * dpdx; |
| 918 | | unit->extent[extnum].param[paramnum].dpdx = dpdx; |
| 919 | | } |
| 920 | | } |
| 921 | | |
| 922 | | /* include the right edge if requested */ |
| 923 | | if (poly->flags & POLYFLAG_INCLUDE_RIGHT_EDGE) |
| 924 | | istopx++; |
| 925 | | |
| 926 | | /* apply left/right clipping */ |
| 927 | | if (cliprect != NULL) |
| 928 | | { |
| 929 | | if (istartx < cliprect->min_x) |
| 930 | | { |
| 931 | | for (paramnum = 0; paramnum < paramcount; paramnum++) |
| 932 | | unit->extent[extnum].param[paramnum].start += (cliprect->min_x - istartx) * unit->extent[extnum].param[paramnum].dpdx; |
| 933 | | istartx = cliprect->min_x; |
| 934 | | } |
| 935 | | if (istopx > cliprect->max_x) |
| 936 | | istopx = cliprect->max_x + 1; |
| 937 | | } |
| 938 | | |
| 939 | | /* set the extent and update the total pixel count */ |
| 940 | | if (istartx >= istopx) |
| 941 | | istartx = istopx = 0; |
| 942 | | unit->extent[extnum].startx = istartx; |
| 943 | | unit->extent[extnum].stopx = istopx; |
| 944 | | pixels += istopx - istartx; |
| 945 | | } |
| 946 | | } |
| 947 | | #if KEEP_STATISTICS |
| 948 | | poly->unit_max = MAX(poly->unit_max, poly->unit_next); |
| 949 | | #endif |
| 950 | | |
| 951 | | /* enqueue the work items */ |
| 952 | | if (poly->queue != NULL) |
| 953 | | osd_work_item_queue_multiple(poly->queue, poly_item_callback, poly->unit_next - startunit, poly->unit[startunit], poly->unit_size, WORK_ITEM_FLAG_AUTO_RELEASE); |
| 954 | | |
| 955 | | /* return the total number of pixels in the triangle */ |
| 956 | | poly->quads++; |
| 957 | | poly->pixels += pixels; |
| 958 | | return pixels; |
| 959 | | } |
| 960 | | |
| 961 | | |
| 962 | | /*------------------------------------------------- |
| 963 | | poly_render_quad_fan - render a set of |
| 964 | | quads in a fan |
| 965 | | -------------------------------------------------*/ |
| 966 | | |
| 967 | | UINT32 poly_render_quad_fan(poly_manager *poly, void *dest, const rectangle *cliprect, poly_draw_quad_scanline callback, int paramcount, int numverts, const poly_vertex *v) |
| 968 | | { |
| 969 | | UINT32 pixels = 0; |
| 970 | | int vertnum; |
| 971 | | |
| 972 | | /* iterate over vertices */ |
| 973 | | for (vertnum = 2; vertnum < numverts; vertnum += 2) |
| 974 | | pixels += poly_render_quad(poly, dest, cliprect, callback, paramcount, &v[0], &v[vertnum - 1], &v[vertnum], &v[MIN(vertnum + 1, numverts - 1)]); |
| 975 | | return pixels; |
| 976 | | } |
| 977 | | |
| 978 | | |
| 979 | | |
| 980 | | /*************************************************************************** |
| 981 | | CLIPPING |
| 982 | | ***************************************************************************/ |
| 983 | | |
| 984 | | /*------------------------------------------------- |
| 985 | | poly_zclip_if_less - z clip a polygon against |
| 986 | | the given value, returning a set of clipped |
| 987 | | vertices |
| 988 | | -------------------------------------------------*/ |
| 989 | | |
| 990 | | int poly_zclip_if_less(int numverts, const poly_vertex *v, poly_vertex *outv, int paramcount, float clipval) |
| 991 | | { |
| 992 | | int prevclipped = (v[numverts - 1].p[0] < clipval); |
| 993 | | poly_vertex *nextout = outv; |
| 994 | | int vertnum; |
| 995 | | |
| 996 | | /* iterate over vertices */ |
| 997 | | for (vertnum = 0; vertnum < numverts; vertnum++) |
| 998 | | { |
| 999 | | int thisclipped = (v[vertnum].p[0] < clipval); |
| 1000 | | |
| 1001 | | /* if we switched from clipped to non-clipped, interpolate a vertex */ |
| 1002 | | if (thisclipped != prevclipped) |
| 1003 | | interpolate_vertex(nextout++, &v[(vertnum == 0) ? (numverts - 1) : (vertnum - 1)], &v[vertnum], paramcount, clipval); |
| 1004 | | |
| 1005 | | /* if this vertex is not clipped, copy it in */ |
| 1006 | | if (!thisclipped) |
| 1007 | | copy_vertex(nextout++, &v[vertnum], paramcount); |
| 1008 | | |
| 1009 | | /* remember the last state */ |
| 1010 | | prevclipped = thisclipped; |
| 1011 | | } |
| 1012 | | return nextout - outv; |
| 1013 | | } |
| 1014 | | |
| 1015 | | |
| 1016 | | |
| 1017 | | /*************************************************************************** |
| 1018 | | INTERNAL FUNCTIONS |
| 1019 | | ***************************************************************************/ |
| 1020 | | |
| 1021 | | /*------------------------------------------------- |
| 1022 | | allocate_array - allocate an array of pointers |
| 1023 | | -------------------------------------------------*/ |
| 1024 | | |
| 1025 | | static void **allocate_array(size_t *itemsize, UINT32 itemcount) |
| 1026 | | { |
| 1027 | | void **ptrarray; |
| 1028 | | int itemnum; |
| 1029 | | |
| 1030 | | /* fail if 0 */ |
| 1031 | | if (itemcount == 0) |
| 1032 | | return NULL; |
| 1033 | | |
| 1034 | | /* round to a cache line boundary */ |
| 1035 | | *itemsize = ((*itemsize + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE) * CACHE_LINE_SIZE; |
| 1036 | | |
| 1037 | | /* allocate the array */ |
| 1038 | | ptrarray = malloc_or_die(sizeof(*ptrarray) * itemcount); |
| 1039 | | memset(ptrarray, 0, sizeof(*ptrarray) * itemcount); |
| 1040 | | |
| 1041 | | /* allocate the actual items */ |
| 1042 | | ptrarray[0] = malloc_or_die(*itemsize * itemcount); |
| 1043 | | memset(ptrarray[0], 0, *itemsize * itemcount); |
| 1044 | | |
| 1045 | | /* initialize the pointer array */ |
| 1046 | | for (itemnum = 1; itemnum < itemcount; itemnum++) |
| 1047 | | ptrarray[itemnum] = (UINT8 *)ptrarray[0] + *itemsize * itemnum; |
| 1048 | | return ptrarray; |
| 1049 | | } |
| 1050 | | |
| 1051 | | |
| 1052 | | /*------------------------------------------------- |
| 1053 | | free_array - release an array of pointers |
| 1054 | | -------------------------------------------------*/ |
| 1055 | | |
| 1056 | | static void free_array(void **array) |
| 1057 | | { |
| 1058 | | if (array != NULL) |
| 1059 | | { |
| 1060 | | if (array[0] != NULL) |
| 1061 | | free(array[0]); |
| 1062 | | free(array); |
| 1063 | | } |
| 1064 | | } |
| 1065 | | |
| 1066 | | |
| 1067 | | /*------------------------------------------------- |
| 1068 | | poly_item_callback - callback for each poly |
| 1069 | | item |
| 1070 | | -------------------------------------------------*/ |
| 1071 | | |
| 1072 | | static void *poly_item_callback(void *param, int threadid) |
| 1073 | | { |
| 1074 | | while (1) |
| 1075 | | { |
| 1076 | | work_unit *unit = param; |
| 1077 | | polygon_info *polygon = unit->shared.polygon; |
| 1078 | | int count = unit->shared.count_next & 0xffff; |
| 1079 | | UINT32 orig_count_next; |
| 1080 | | int curscan; |
| 1081 | | |
| 1082 | | /* if our previous item isn't done yet, enqueue this item to the end and proceed */ |
| 1083 | | if (unit->shared.previtem != 0xffff) |
| 1084 | | { |
| 1085 | | work_unit *prevunit = polygon->poly->unit[unit->shared.previtem]; |
| 1086 | | if (prevunit->shared.count_next != 0) |
| 1087 | | { |
| 1088 | | UINT32 unitnum = ((UINT8 *)unit - (UINT8 *)polygon->poly->unit[0]) / polygon->poly->unit_size; |
| 1089 | | UINT32 new_count_next; |
| 1090 | | |
| 1091 | | /* attempt to atomically swap in this new value */ |
| 1092 | | do |
| 1093 | | { |
| 1094 | | orig_count_next = prevunit->shared.count_next; |
| 1095 | | new_count_next = orig_count_next | (unitnum << 16); |
| 1096 | | } while (compare_exchange32((volatile INT32 *)&prevunit->shared.count_next, orig_count_next, new_count_next) != orig_count_next); |
| 1097 | | |
| 1098 | | #if KEEP_STATISTICS |
| 1099 | | /* track resolved conflicts */ |
| 1100 | | polygon->poly->conflicts[threadid]++; |
| 1101 | | if (orig_count_next != 0) |
| 1102 | | polygon->poly->resolved[threadid]++; |
| 1103 | | #endif |
| 1104 | | /* if we succeeded, skip out early so we can do other work */ |
| 1105 | | if (orig_count_next != 0) |
| 1106 | | break; |
| 1107 | | } |
| 1108 | | } |
| 1109 | | |
| 1110 | | /* iterate over extents */ |
| 1111 | | for (curscan = 0; curscan < count; curscan++) |
| 1112 | | if (polygon->tricallback != NULL) |
| 1113 | | (*polygon->tricallback)(polygon->dest, unit->shared.scanline + curscan, &unit->tri.extent[curscan], &polygon->params, polygon->extra, threadid); |
| 1114 | | else |
| 1115 | | (*polygon->quadcallback)(polygon->dest, unit->shared.scanline + curscan, &unit->quad.extent[curscan], polygon->extra, threadid); |
| 1116 | | |
| 1117 | | /* set our count to 0 and re-fetch the original count value */ |
| 1118 | | do |
| 1119 | | { |
| 1120 | | orig_count_next = unit->shared.count_next; |
| 1121 | | } while (compare_exchange32((volatile INT32 *)&unit->shared.count_next, orig_count_next, 0) != orig_count_next); |
| 1122 | | |
| 1123 | | /* if we have no more work to do, do nothing */ |
| 1124 | | orig_count_next >>= 16; |
| 1125 | | if (orig_count_next == 0) |
| 1126 | | break; |
| 1127 | | param = polygon->poly->unit[orig_count_next]; |
| 1128 | | } |
| 1129 | | return NULL; |
| 1130 | | } |