Merge commit 'origin/gallium-winsys-handle-rebased'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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 #include <limits.h>
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "util/u_cpu_detect.h"
32 #include "util/u_surface.h"
33
34 #include "lp_scene_queue.h"
35 #include "lp_debug.h"
36 #include "lp_fence.h"
37 #include "lp_perf.h"
38 #include "lp_rast.h"
39 #include "lp_rast_priv.h"
40 #include "lp_tile_soa.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "lp_scene.h"
43
44
45 /* Begin rasterizing a scene:
46 */
47 static boolean
48 lp_rast_begin( struct lp_rasterizer *rast,
49 struct lp_scene *scene )
50 {
51 const struct pipe_framebuffer_state *fb = &scene->fb;
52 boolean write_color = fb->nr_cbufs != 0;
53 boolean write_zstencil = fb->zsbuf != NULL;
54 int i;
55
56 rast->curr_scene = scene;
57
58 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
59
60 rast->state.nr_cbufs = scene->fb.nr_cbufs;
61 rast->state.write_zstencil = write_zstencil;
62 rast->state.write_color = write_color;
63
64 for (i = 0; i < rast->state.nr_cbufs; i++) {
65 rast->cbuf[i].map = scene->cbuf_map[i];
66 rast->cbuf[i].format = scene->cbuf_transfer[i]->texture->format;
67 rast->cbuf[i].width = scene->cbuf_transfer[i]->width;
68 rast->cbuf[i].height = scene->cbuf_transfer[i]->height;
69 rast->cbuf[i].stride = scene->cbuf_transfer[i]->stride;
70 }
71
72 if (write_zstencil) {
73 rast->zsbuf.map = scene->zsbuf_map;
74 rast->zsbuf.stride = scene->zsbuf_transfer->stride;
75 rast->zsbuf.blocksize =
76 util_format_get_blocksize(scene->zsbuf_transfer->texture->format);
77 }
78
79 lp_scene_bin_iter_begin( scene );
80
81 return TRUE;
82 }
83
84
85 static void
86 lp_rast_end( struct lp_rasterizer *rast )
87 {
88 int i;
89
90 lp_scene_reset( rast->curr_scene );
91
92 for (i = 0; i < rast->state.nr_cbufs; i++)
93 rast->cbuf[i].map = NULL;
94
95 rast->zsbuf.map = NULL;
96 rast->curr_scene = NULL;
97 }
98
99 /**
100 * Begining rasterization of a tile.
101 * \param x window X position of the tile, in pixels
102 * \param y window Y position of the tile, in pixels
103 */
104 static void
105 lp_rast_start_tile(struct lp_rasterizer_task *task,
106 unsigned x, unsigned y)
107 {
108 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
109
110 task->x = x;
111 task->y = y;
112 }
113
114
115 /**
116 * Clear the rasterizer's current color tile.
117 * This is a bin command called during bin processing.
118 */
119 void
120 lp_rast_clear_color(struct lp_rasterizer_task *task,
121 const union lp_rast_cmd_arg arg)
122 {
123 struct lp_rasterizer *rast = task->rast;
124 const uint8_t *clear_color = arg.clear_color;
125 uint8_t **color_tile = task->tile.color;
126 unsigned i;
127
128 LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
129 clear_color[0],
130 clear_color[1],
131 clear_color[2],
132 clear_color[3]);
133
134 if (clear_color[0] == clear_color[1] &&
135 clear_color[1] == clear_color[2] &&
136 clear_color[2] == clear_color[3]) {
137 /* clear to grayscale value {x, x, x, x} */
138 for (i = 0; i < rast->state.nr_cbufs; i++) {
139 memset(color_tile[i], clear_color[0], TILE_SIZE * TILE_SIZE * 4);
140 }
141 }
142 else {
143 /* Non-gray color.
144 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
145 * will need to change. It'll be pretty obvious when clearing no longer
146 * works.
147 */
148 const unsigned chunk = TILE_SIZE / 4;
149 for (i = 0; i < rast->state.nr_cbufs; i++) {
150 uint8_t *c = color_tile[i];
151 unsigned j;
152 for (j = 0; j < 4 * TILE_SIZE; j++) {
153 memset(c, clear_color[0], chunk);
154 c += chunk;
155 memset(c, clear_color[1], chunk);
156 c += chunk;
157 memset(c, clear_color[2], chunk);
158 c += chunk;
159 memset(c, clear_color[3], chunk);
160 c += chunk;
161 }
162 assert(c - color_tile[i] == TILE_SIZE * TILE_SIZE * 4);
163 }
164 }
165
166 LP_COUNT(nr_color_tile_clear);
167 }
168
169
170 /**
171 * Clear the rasterizer's current z/stencil tile.
172 * This is a bin command called during bin processing.
173 */
174 void
175 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
176 const union lp_rast_cmd_arg arg)
177 {
178 struct lp_rasterizer *rast = task->rast;
179 const unsigned tile_x = task->x;
180 const unsigned tile_y = task->y;
181 const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
182 const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
183 unsigned block_size = rast->zsbuf.blocksize;
184 uint8_t *dst;
185 unsigned dst_stride = rast->zsbuf.stride * TILE_VECTOR_HEIGHT;
186 unsigned i, j;
187
188 LP_DBG(DEBUG_RAST, "%s 0x%x\n", __FUNCTION__, arg.clear_zstencil);
189
190 assert(rast->zsbuf.map);
191 if (!rast->zsbuf.map)
192 return;
193
194 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
195
196 /*
197 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
198 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
199 *
200 * The swizzled depth format is such that the depths for
201 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
202 */
203
204 dst = lp_rast_depth_pointer(rast, tile_x, tile_y);
205
206 switch (block_size) {
207 case 1:
208 memset(dst, (uint8_t) arg.clear_zstencil, height * width);
209 break;
210 case 2:
211 for (i = 0; i < height; i++) {
212 uint16_t *row = (uint16_t *)dst;
213 for (j = 0; j < width; j++)
214 *row++ = (uint16_t) arg.clear_zstencil;
215 dst += dst_stride;
216 }
217 break;
218 case 4:
219 for (i = 0; i < height; i++) {
220 uint32_t *row = (uint32_t *)dst;
221 for (j = 0; j < width; j++)
222 *row++ = arg.clear_zstencil;
223 dst += dst_stride;
224 }
225 break;
226 default:
227 assert(0);
228 break;
229 }
230 }
231
232
233 /**
234 * Load tile color from the framebuffer surface.
235 * This is a bin command called during bin processing.
236 */
237 void
238 lp_rast_load_color(struct lp_rasterizer_task *task,
239 const union lp_rast_cmd_arg arg)
240 {
241 struct lp_rasterizer *rast = task->rast;
242 const unsigned x = task->x, y = task->y;
243 unsigned i;
244
245 LP_DBG(DEBUG_RAST, "%s at %u, %u\n", __FUNCTION__, x, y);
246
247 for (i = 0; i < rast->state.nr_cbufs; i++) {
248 if (x >= rast->cbuf[i].width || y >= rast->cbuf[i].height)
249 continue;
250
251 lp_tile_read_4ub(rast->cbuf[i].format,
252 task->tile.color[i],
253 rast->cbuf[i].map,
254 rast->cbuf[i].stride,
255 x, y,
256 TILE_SIZE, TILE_SIZE);
257
258 LP_COUNT(nr_color_tile_load);
259 }
260 }
261
262
263 void
264 lp_rast_set_state(struct lp_rasterizer_task *task,
265 const union lp_rast_cmd_arg arg)
266 {
267 const struct lp_rast_state *state = arg.set_state;
268
269 LP_DBG(DEBUG_RAST, "%s %p\n", __FUNCTION__, (void *) state);
270
271 /* just set the current state pointer for this rasterizer */
272 task->current_state = state;
273 }
274
275
276
277 /**
278 * Run the shader on all blocks in a tile. This is used when a tile is
279 * completely contained inside a triangle.
280 * This is a bin command called during bin processing.
281 */
282 void
283 lp_rast_shade_tile(struct lp_rasterizer_task *task,
284 const union lp_rast_cmd_arg arg)
285 {
286 struct lp_rasterizer *rast = task->rast;
287 const struct lp_rast_state *state = task->current_state;
288 struct lp_rast_tile *tile = &task->tile;
289 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
290 const unsigned tile_x = task->x, tile_y = task->y;
291 unsigned x, y;
292
293 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
294
295 /* render the whole 64x64 tile in 4x4 chunks */
296 for (y = 0; y < TILE_SIZE; y += 4){
297 for (x = 0; x < TILE_SIZE; x += 4) {
298 uint8_t *color[PIPE_MAX_COLOR_BUFS];
299 uint32_t *depth;
300 unsigned block_offset, i;
301
302 /* offset of the 16x16 pixel block within the tile */
303 block_offset = ((y / 4) * (16 * 16) + (x / 4) * 16);
304
305 /* color buffer */
306 for (i = 0; i < rast->state.nr_cbufs; i++)
307 color[i] = tile->color[i] + 4 * block_offset;
308
309 /* depth buffer */
310 depth = lp_rast_depth_pointer(rast, tile_x + x, tile_y + y);
311
312 /* run shader */
313 state->jit_function[0]( &state->jit_context,
314 tile_x + x, tile_y + y,
315 inputs->a0,
316 inputs->dadx,
317 inputs->dady,
318 color,
319 depth,
320 INT_MIN, INT_MIN, INT_MIN,
321 NULL, NULL, NULL );
322 }
323 }
324 }
325
326
327 /**
328 * Compute shading for a 4x4 block of pixels.
329 * This is a bin command called during bin processing.
330 */
331 void lp_rast_shade_quads( struct lp_rasterizer_task *task,
332 const struct lp_rast_shader_inputs *inputs,
333 unsigned x, unsigned y,
334 int32_t c1, int32_t c2, int32_t c3)
335 {
336 const struct lp_rast_state *state = task->current_state;
337 struct lp_rasterizer *rast = task->rast;
338 struct lp_rast_tile *tile = &task->tile;
339 uint8_t *color[PIPE_MAX_COLOR_BUFS];
340 void *depth;
341 unsigned i;
342 unsigned ix, iy;
343 int block_offset;
344
345 assert(state);
346
347 /* Sanity checks */
348 assert(x % TILE_VECTOR_WIDTH == 0);
349 assert(y % TILE_VECTOR_HEIGHT == 0);
350
351 assert((x % 4) == 0);
352 assert((y % 4) == 0);
353
354 ix = x % TILE_SIZE;
355 iy = y % TILE_SIZE;
356
357 /* offset of the 16x16 pixel block within the tile */
358 block_offset = ((iy / 4) * (16 * 16) + (ix / 4) * 16);
359
360 /* color buffer */
361 for (i = 0; i < rast->state.nr_cbufs; i++)
362 color[i] = tile->color[i] + 4 * block_offset;
363
364 /* depth buffer */
365 depth = lp_rast_depth_pointer(rast, x, y);
366
367
368 assert(lp_check_alignment(tile->color[0], 16));
369 assert(lp_check_alignment(state->jit_context.blend_color, 16));
370
371 assert(lp_check_alignment(inputs->step[0], 16));
372 assert(lp_check_alignment(inputs->step[1], 16));
373 assert(lp_check_alignment(inputs->step[2], 16));
374
375 /* run shader */
376 state->jit_function[1]( &state->jit_context,
377 x, y,
378 inputs->a0,
379 inputs->dadx,
380 inputs->dady,
381 color,
382 depth,
383 c1, c2, c3,
384 inputs->step[0], inputs->step[1], inputs->step[2]);
385 }
386
387
388 /**
389 * Set top row and left column of the tile's pixels to white. For debugging.
390 */
391 static void
392 outline_tile(uint8_t *tile)
393 {
394 const uint8_t val = 0xff;
395 unsigned i;
396
397 for (i = 0; i < TILE_SIZE; i++) {
398 TILE_PIXEL(tile, i, 0, 0) = val;
399 TILE_PIXEL(tile, i, 0, 1) = val;
400 TILE_PIXEL(tile, i, 0, 2) = val;
401 TILE_PIXEL(tile, i, 0, 3) = val;
402
403 TILE_PIXEL(tile, 0, i, 0) = val;
404 TILE_PIXEL(tile, 0, i, 1) = val;
405 TILE_PIXEL(tile, 0, i, 2) = val;
406 TILE_PIXEL(tile, 0, i, 3) = val;
407 }
408 }
409
410
411 /**
412 * Draw grid of gray lines at 16-pixel intervals across the tile to
413 * show the sub-tile boundaries. For debugging.
414 */
415 static void
416 outline_subtiles(uint8_t *tile)
417 {
418 const uint8_t val = 0x80;
419 const unsigned step = 16;
420 unsigned i, j;
421
422 for (i = 0; i < TILE_SIZE; i += step) {
423 for (j = 0; j < TILE_SIZE; j++) {
424 TILE_PIXEL(tile, i, j, 0) = val;
425 TILE_PIXEL(tile, i, j, 1) = val;
426 TILE_PIXEL(tile, i, j, 2) = val;
427 TILE_PIXEL(tile, i, j, 3) = val;
428
429 TILE_PIXEL(tile, j, i, 0) = val;
430 TILE_PIXEL(tile, j, i, 1) = val;
431 TILE_PIXEL(tile, j, i, 2) = val;
432 TILE_PIXEL(tile, j, i, 3) = val;
433 }
434 }
435
436 outline_tile(tile);
437 }
438
439
440
441 /**
442 * Write the rasterizer's color tile to the framebuffer.
443 */
444 static void
445 lp_rast_store_color(struct lp_rasterizer_task *task)
446 {
447 struct lp_rasterizer *rast = task->rast;
448 const unsigned x = task->x, y = task->y;
449 unsigned i;
450
451 for (i = 0; i < rast->state.nr_cbufs; i++) {
452 if (x >= rast->cbuf[i].width)
453 continue;
454
455 if (y >= rast->cbuf[i].height)
456 continue;
457
458 LP_DBG(DEBUG_RAST, "%s [%u] %d,%d\n", __FUNCTION__,
459 task->thread_index, x, y);
460
461 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
462 outline_subtiles(task->tile.color[i]);
463 else if (LP_DEBUG & DEBUG_SHOW_TILES)
464 outline_tile(task->tile.color[i]);
465
466 lp_tile_write_4ub(rast->cbuf[i].format,
467 task->tile.color[i],
468 rast->cbuf[i].map,
469 rast->cbuf[i].stride,
470 x, y,
471 TILE_SIZE, TILE_SIZE);
472
473 LP_COUNT(nr_color_tile_store);
474 }
475 }
476
477
478
479 /**
480 * Signal on a fence. This is called during bin execution/rasterization.
481 * Called per thread.
482 */
483 void
484 lp_rast_fence(struct lp_rasterizer_task *task,
485 const union lp_rast_cmd_arg arg)
486 {
487 struct lp_fence *fence = arg.fence;
488
489 pipe_mutex_lock( fence->mutex );
490
491 fence->count++;
492 assert(fence->count <= fence->rank);
493
494 LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
495 fence->count, fence->rank);
496
497 pipe_condvar_signal( fence->signalled );
498
499 pipe_mutex_unlock( fence->mutex );
500 }
501
502
503
504
505 /**
506 * Rasterize commands for a single bin.
507 * \param x, y position of the bin's tile in the framebuffer
508 * Must be called between lp_rast_begin() and lp_rast_end().
509 * Called per thread.
510 */
511 static void
512 rasterize_bin(struct lp_rasterizer_task *task,
513 const struct cmd_bin *bin,
514 int x, int y)
515 {
516 const struct cmd_block_list *commands = &bin->commands;
517 struct cmd_block *block;
518 unsigned k;
519
520 lp_rast_start_tile( task, x * TILE_SIZE, y * TILE_SIZE );
521
522 /* simply execute each of the commands in the block list */
523 for (block = commands->head; block; block = block->next) {
524 for (k = 0; k < block->count; k++) {
525 block->cmd[k]( task, block->arg[k] );
526 }
527 }
528
529 /* Write the rasterizer's tiles to the framebuffer.
530 */
531 if (task->rast->state.write_color)
532 lp_rast_store_color(task);
533
534 /* Free data for this bin.
535 */
536 lp_scene_bin_reset( task->rast->curr_scene, x, y);
537 }
538
539
540 #define RAST(x) { lp_rast_##x, #x }
541
542 static struct {
543 lp_rast_cmd cmd;
544 const char *name;
545 } cmd_names[] =
546 {
547 RAST(load_color),
548 RAST(clear_color),
549 RAST(clear_zstencil),
550 RAST(triangle),
551 RAST(shade_tile),
552 RAST(set_state),
553 RAST(fence),
554 };
555
556 static void
557 debug_bin( const struct cmd_bin *bin )
558 {
559 const struct cmd_block *head = bin->commands.head;
560 int i, j;
561
562 for (i = 0; i < head->count; i++) {
563 debug_printf("%d: ", i);
564 for (j = 0; j < Elements(cmd_names); j++) {
565 if (head->cmd[i] == cmd_names[j].cmd) {
566 debug_printf("%s\n", cmd_names[j].name);
567 break;
568 }
569 }
570 if (j == Elements(cmd_names))
571 debug_printf("...other\n");
572 }
573
574 }
575
576 /* An empty bin is one that just loads the contents of the tile and
577 * stores them again unchanged. This typically happens when bins have
578 * been flushed for some reason in the middle of a frame, or when
579 * incremental updates are being made to a render target.
580 *
581 * Try to avoid doing pointless work in this case.
582 */
583 static boolean
584 is_empty_bin( const struct cmd_bin *bin )
585 {
586 const struct cmd_block *head = bin->commands.head;
587 int i;
588
589 if (0)
590 debug_bin(bin);
591
592 /* We emit at most two load-tile commands at the start of the first
593 * command block. In addition we seem to emit a couple of
594 * set-state commands even in empty bins.
595 *
596 * As a heuristic, if a bin has more than 4 commands, consider it
597 * non-empty.
598 */
599 if (head->next != NULL ||
600 head->count > 4) {
601 return FALSE;
602 }
603
604 for (i = 0; i < head->count; i++)
605 if (head->cmd[i] != lp_rast_load_color &&
606 head->cmd[i] != lp_rast_set_state) {
607 return FALSE;
608 }
609
610 return TRUE;
611 }
612
613
614
615 /**
616 * Rasterize/execute all bins within a scene.
617 * Called per thread.
618 */
619 static void
620 rasterize_scene(struct lp_rasterizer_task *task,
621 struct lp_scene *scene)
622 {
623 /* loop over scene bins, rasterize each */
624 #if 0
625 {
626 unsigned i, j;
627 for (i = 0; i < scene->tiles_x; i++) {
628 for (j = 0; j < scene->tiles_y; j++) {
629 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
630 rasterize_bin(task, bin, i, j);
631 }
632 }
633 }
634 #else
635 {
636 struct cmd_bin *bin;
637 int x, y;
638
639 assert(scene);
640 while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
641 if (!is_empty_bin( bin ))
642 rasterize_bin(task, bin, x, y);
643 }
644 }
645 #endif
646 }
647
648
649 /**
650 * Called by setup module when it has something for us to render.
651 */
652 void
653 lp_rast_queue_scene( struct lp_rasterizer *rast,
654 struct lp_scene *scene)
655 {
656 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
657
658 if (rast->num_threads == 0) {
659 /* no threading */
660
661 lp_rast_begin( rast, scene );
662
663 rasterize_scene( &rast->tasks[0], scene );
664
665 lp_scene_reset( scene );
666 rast->curr_scene = NULL;
667 }
668 else {
669 /* threaded rendering! */
670 unsigned i;
671
672 lp_scene_enqueue( rast->full_scenes, scene );
673
674 /* signal the threads that there's work to do */
675 for (i = 0; i < rast->num_threads; i++) {
676 pipe_semaphore_signal(&rast->tasks[i].work_ready);
677 }
678 }
679
680 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
681 }
682
683
684 void
685 lp_rast_finish( struct lp_rasterizer *rast )
686 {
687 if (rast->num_threads == 0) {
688 /* nothing to do */
689 }
690 else {
691 int i;
692
693 /* wait for work to complete */
694 for (i = 0; i < rast->num_threads; i++) {
695 pipe_semaphore_wait(&rast->tasks[i].work_done);
696 }
697 }
698 }
699
700
701 /**
702 * This is the thread's main entrypoint.
703 * It's a simple loop:
704 * 1. wait for work
705 * 2. do work
706 * 3. signal that we're done
707 */
708 static PIPE_THREAD_ROUTINE( thread_func, init_data )
709 {
710 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
711 struct lp_rasterizer *rast = task->rast;
712 boolean debug = false;
713
714 while (1) {
715 /* wait for work */
716 if (debug)
717 debug_printf("thread %d waiting for work\n", task->thread_index);
718 pipe_semaphore_wait(&task->work_ready);
719
720 if (rast->exit_flag)
721 break;
722
723 if (task->thread_index == 0) {
724 /* thread[0]:
725 * - get next scene to rasterize
726 * - map the framebuffer surfaces
727 */
728 lp_rast_begin( rast,
729 lp_scene_dequeue( rast->full_scenes, TRUE ) );
730 }
731
732 /* Wait for all threads to get here so that threads[1+] don't
733 * get a null rast->curr_scene pointer.
734 */
735 pipe_barrier_wait( &rast->barrier );
736
737 /* do work */
738 if (debug)
739 debug_printf("thread %d doing work\n", task->thread_index);
740
741 rasterize_scene(task,
742 rast->curr_scene);
743
744 /* wait for all threads to finish with this scene */
745 pipe_barrier_wait( &rast->barrier );
746
747 /* XXX: shouldn't be necessary:
748 */
749 if (task->thread_index == 0) {
750 lp_rast_end( rast );
751 }
752
753 /* signal done with work */
754 if (debug)
755 debug_printf("thread %d done working\n", task->thread_index);
756
757 pipe_semaphore_signal(&task->work_done);
758 }
759
760 return NULL;
761 }
762
763
764 /**
765 * Initialize semaphores and spawn the threads.
766 */
767 static void
768 create_rast_threads(struct lp_rasterizer *rast)
769 {
770 unsigned i;
771
772 #ifdef PIPE_OS_WINDOWS
773 /* Multithreading not supported on windows until conditions and barriers are
774 * properly implemented. */
775 rast->num_threads = 0;
776 #else
777 rast->num_threads = util_cpu_caps.nr_cpus;
778 rast->num_threads = debug_get_num_option("LP_NUM_THREADS", rast->num_threads);
779 rast->num_threads = MIN2(rast->num_threads, MAX_THREADS);
780 #endif
781
782 /* NOTE: if num_threads is zero, we won't use any threads */
783 for (i = 0; i < rast->num_threads; i++) {
784 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
785 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
786 rast->threads[i] = pipe_thread_create(thread_func,
787 (void *) &rast->tasks[i]);
788 }
789 }
790
791
792
793 /**
794 * Create new lp_rasterizer.
795 * \param empty the queue to put empty scenes on after we've finished
796 * processing them.
797 */
798 struct lp_rasterizer *
799 lp_rast_create( void )
800 {
801 struct lp_rasterizer *rast;
802 unsigned i, cbuf;
803
804 rast = CALLOC_STRUCT(lp_rasterizer);
805 if(!rast)
806 return NULL;
807
808 rast->full_scenes = lp_scene_queue_create();
809
810 for (i = 0; i < Elements(rast->tasks); i++) {
811 struct lp_rasterizer_task *task = &rast->tasks[i];
812
813 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
814 task->tile.color[cbuf] = align_malloc(TILE_SIZE * TILE_SIZE * 4, 16);
815
816 task->rast = rast;
817 task->thread_index = i;
818 }
819
820 create_rast_threads(rast);
821
822 /* for synchronizing rasterization threads */
823 pipe_barrier_init( &rast->barrier, rast->num_threads );
824
825 return rast;
826 }
827
828
829 /* Shutdown:
830 */
831 void lp_rast_destroy( struct lp_rasterizer *rast )
832 {
833 unsigned i, cbuf;
834
835 for (i = 0; i < Elements(rast->tasks); i++) {
836 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
837 align_free(rast->tasks[i].tile.color[cbuf]);
838 }
839
840 /* Set exit_flag and signal each thread's work_ready semaphore.
841 * Each thread will be woken up, notice that the exit_flag is set and
842 * break out of its main loop. The thread will then exit.
843 */
844 rast->exit_flag = TRUE;
845 for (i = 0; i < rast->num_threads; i++) {
846 pipe_semaphore_signal(&rast->tasks[i].work_ready);
847 }
848
849 /* Wait for threads to terminate before cleaning up per-thread data */
850 for (i = 0; i < rast->num_threads; i++) {
851 pipe_thread_wait(rast->threads[i]);
852 }
853
854 /* Clean up per-thread data */
855 for (i = 0; i < rast->num_threads; i++) {
856 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
857 pipe_semaphore_destroy(&rast->tasks[i].work_done);
858 }
859
860 /* for synchronizing rasterization threads */
861 pipe_barrier_destroy( &rast->barrier );
862
863 FREE(rast);
864 }
865
866
867 /** Return number of rasterization threads */
868 unsigned
869 lp_rast_get_num_threads( struct lp_rasterizer *rast )
870 {
871 return rast->num_threads;
872 }