llvmpipe: reorganize transfer usage
[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 #ifdef DEBUG
346 assert(state);
347
348 /* Sanity checks */
349 assert(x % TILE_VECTOR_WIDTH == 0);
350 assert(y % TILE_VECTOR_HEIGHT == 0);
351
352 assert((x % 4) == 0);
353 assert((y % 4) == 0);
354 #endif
355
356 ix = x % TILE_SIZE;
357 iy = y % TILE_SIZE;
358
359 /* offset of the 16x16 pixel block within the tile */
360 block_offset = ((iy / 4) * (16 * 16) + (ix / 4) * 16);
361
362 /* color buffer */
363 for (i = 0; i < rast->state.nr_cbufs; i++)
364 color[i] = tile->color[i] + 4 * block_offset;
365
366 /* depth buffer */
367 depth = lp_rast_depth_pointer(rast, x, y);
368
369
370
371 #ifdef DEBUG
372 assert(lp_check_alignment(tile->color[0], 16));
373 assert(lp_check_alignment(state->jit_context.blend_color, 16));
374
375 assert(lp_check_alignment(inputs->step[0], 16));
376 assert(lp_check_alignment(inputs->step[1], 16));
377 assert(lp_check_alignment(inputs->step[2], 16));
378 #endif
379
380 /* run shader */
381 state->jit_function[1]( &state->jit_context,
382 x, y,
383 inputs->a0,
384 inputs->dadx,
385 inputs->dady,
386 color,
387 depth,
388 c1, c2, c3,
389 inputs->step[0], inputs->step[1], inputs->step[2]);
390 }
391
392
393 /**
394 * Set top row and left column of the tile's pixels to white. For debugging.
395 */
396 static void
397 outline_tile(uint8_t *tile)
398 {
399 const uint8_t val = 0xff;
400 unsigned i;
401
402 for (i = 0; i < TILE_SIZE; i++) {
403 TILE_PIXEL(tile, i, 0, 0) = val;
404 TILE_PIXEL(tile, i, 0, 1) = val;
405 TILE_PIXEL(tile, i, 0, 2) = val;
406 TILE_PIXEL(tile, i, 0, 3) = val;
407
408 TILE_PIXEL(tile, 0, i, 0) = val;
409 TILE_PIXEL(tile, 0, i, 1) = val;
410 TILE_PIXEL(tile, 0, i, 2) = val;
411 TILE_PIXEL(tile, 0, i, 3) = val;
412 }
413 }
414
415
416 /**
417 * Draw grid of gray lines at 16-pixel intervals across the tile to
418 * show the sub-tile boundaries. For debugging.
419 */
420 static void
421 outline_subtiles(uint8_t *tile)
422 {
423 const uint8_t val = 0x80;
424 const unsigned step = 16;
425 unsigned i, j;
426
427 for (i = 0; i < TILE_SIZE; i += step) {
428 for (j = 0; j < TILE_SIZE; j++) {
429 TILE_PIXEL(tile, i, j, 0) = val;
430 TILE_PIXEL(tile, i, j, 1) = val;
431 TILE_PIXEL(tile, i, j, 2) = val;
432 TILE_PIXEL(tile, i, j, 3) = val;
433
434 TILE_PIXEL(tile, j, i, 0) = val;
435 TILE_PIXEL(tile, j, i, 1) = val;
436 TILE_PIXEL(tile, j, i, 2) = val;
437 TILE_PIXEL(tile, j, i, 3) = val;
438 }
439 }
440
441 outline_tile(tile);
442 }
443
444
445
446 /**
447 * Write the rasterizer's color tile to the framebuffer.
448 */
449 static void
450 lp_rast_store_color(struct lp_rasterizer_task *task)
451 {
452 struct lp_rasterizer *rast = task->rast;
453 const unsigned x = task->x, y = task->y;
454 unsigned i;
455
456 for (i = 0; i < rast->state.nr_cbufs; i++) {
457 if (x >= rast->cbuf[i].width)
458 continue;
459
460 if (y >= rast->cbuf[i].height)
461 continue;
462
463 LP_DBG(DEBUG_RAST, "%s [%u] %d,%d\n", __FUNCTION__,
464 task->thread_index, x, y);
465
466 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
467 outline_subtiles(task->tile.color[i]);
468 else if (LP_DEBUG & DEBUG_SHOW_TILES)
469 outline_tile(task->tile.color[i]);
470
471 lp_tile_write_4ub(rast->cbuf[i].format,
472 task->tile.color[i],
473 rast->cbuf[i].map,
474 rast->cbuf[i].stride,
475 x, y,
476 TILE_SIZE, TILE_SIZE);
477
478 LP_COUNT(nr_color_tile_store);
479 }
480 }
481
482
483
484 /**
485 * Signal on a fence. This is called during bin execution/rasterization.
486 * Called per thread.
487 */
488 void
489 lp_rast_fence(struct lp_rasterizer_task *task,
490 const union lp_rast_cmd_arg arg)
491 {
492 struct lp_fence *fence = arg.fence;
493
494 pipe_mutex_lock( fence->mutex );
495
496 fence->count++;
497 assert(fence->count <= fence->rank);
498
499 LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
500 fence->count, fence->rank);
501
502 pipe_condvar_signal( fence->signalled );
503
504 pipe_mutex_unlock( fence->mutex );
505 }
506
507
508
509
510 /**
511 * Rasterize commands for a single bin.
512 * \param x, y position of the bin's tile in the framebuffer
513 * Must be called between lp_rast_begin() and lp_rast_end().
514 * Called per thread.
515 */
516 static void
517 rasterize_bin(struct lp_rasterizer_task *task,
518 const struct cmd_bin *bin,
519 int x, int y)
520 {
521 const struct cmd_block_list *commands = &bin->commands;
522 struct cmd_block *block;
523 unsigned k;
524
525 lp_rast_start_tile( task, x * TILE_SIZE, y * TILE_SIZE );
526
527 /* simply execute each of the commands in the block list */
528 for (block = commands->head; block; block = block->next) {
529 for (k = 0; k < block->count; k++) {
530 block->cmd[k]( task, block->arg[k] );
531 }
532 }
533
534 /* Write the rasterizer's tiles to the framebuffer.
535 */
536 if (task->rast->state.write_color)
537 lp_rast_store_color(task);
538
539 /* Free data for this bin.
540 */
541 lp_scene_bin_reset( task->rast->curr_scene, x, y);
542 }
543
544
545 #define RAST(x) { lp_rast_##x, #x }
546
547 static struct {
548 lp_rast_cmd cmd;
549 const char *name;
550 } cmd_names[] =
551 {
552 RAST(load_color),
553 RAST(clear_color),
554 RAST(clear_zstencil),
555 RAST(triangle),
556 RAST(shade_tile),
557 RAST(set_state),
558 RAST(fence),
559 };
560
561 static void
562 debug_bin( const struct cmd_bin *bin )
563 {
564 const struct cmd_block *head = bin->commands.head;
565 int i, j;
566
567 for (i = 0; i < head->count; i++) {
568 debug_printf("%d: ", i);
569 for (j = 0; j < Elements(cmd_names); j++) {
570 if (head->cmd[i] == cmd_names[j].cmd) {
571 debug_printf("%s\n", cmd_names[j].name);
572 break;
573 }
574 }
575 if (j == Elements(cmd_names))
576 debug_printf("...other\n");
577 }
578
579 }
580
581 /* An empty bin is one that just loads the contents of the tile and
582 * stores them again unchanged. This typically happens when bins have
583 * been flushed for some reason in the middle of a frame, or when
584 * incremental updates are being made to a render target.
585 *
586 * Try to avoid doing pointless work in this case.
587 */
588 static boolean
589 is_empty_bin( const struct cmd_bin *bin )
590 {
591 const struct cmd_block *head = bin->commands.head;
592 int i;
593
594 if (0)
595 debug_bin(bin);
596
597 /* We emit at most two load-tile commands at the start of the first
598 * command block. In addition we seem to emit a couple of
599 * set-state commands even in empty bins.
600 *
601 * As a heuristic, if a bin has more than 4 commands, consider it
602 * non-empty.
603 */
604 if (head->next != NULL ||
605 head->count > 4) {
606 return FALSE;
607 }
608
609 for (i = 0; i < head->count; i++)
610 if (head->cmd[i] != lp_rast_load_color &&
611 head->cmd[i] != lp_rast_set_state) {
612 return FALSE;
613 }
614
615 return TRUE;
616 }
617
618
619
620 /**
621 * Rasterize/execute all bins within a scene.
622 * Called per thread.
623 */
624 static void
625 rasterize_scene(struct lp_rasterizer_task *task,
626 struct lp_scene *scene)
627 {
628 /* loop over scene bins, rasterize each */
629 #if 0
630 {
631 unsigned i, j;
632 for (i = 0; i < scene->tiles_x; i++) {
633 for (j = 0; j < scene->tiles_y; j++) {
634 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
635 rasterize_bin(task, bin, i, j);
636 }
637 }
638 }
639 #else
640 {
641 struct cmd_bin *bin;
642 int x, y;
643
644 assert(scene);
645 while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
646 if (!is_empty_bin( bin ))
647 rasterize_bin(task, bin, x, y);
648 }
649 }
650 #endif
651 }
652
653
654 /**
655 * Called by setup module when it has something for us to render.
656 */
657 void
658 lp_rast_queue_scene( struct lp_rasterizer *rast,
659 struct lp_scene *scene)
660 {
661 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
662
663 if (rast->num_threads == 0) {
664 /* no threading */
665
666 lp_rast_begin( rast, scene );
667
668 rasterize_scene( &rast->tasks[0], scene );
669
670 lp_scene_reset( scene );
671 rast->curr_scene = NULL;
672 }
673 else {
674 /* threaded rendering! */
675 unsigned i;
676
677 lp_scene_enqueue( rast->full_scenes, scene );
678
679 /* signal the threads that there's work to do */
680 for (i = 0; i < rast->num_threads; i++) {
681 pipe_semaphore_signal(&rast->tasks[i].work_ready);
682 }
683 }
684
685 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
686 }
687
688
689 void
690 lp_rast_finish( struct lp_rasterizer *rast )
691 {
692 if (rast->num_threads == 0) {
693 /* nothing to do */
694 }
695 else {
696 int i;
697
698 /* wait for work to complete */
699 for (i = 0; i < rast->num_threads; i++) {
700 pipe_semaphore_wait(&rast->tasks[i].work_done);
701 }
702 }
703 }
704
705
706 /**
707 * This is the thread's main entrypoint.
708 * It's a simple loop:
709 * 1. wait for work
710 * 2. do work
711 * 3. signal that we're done
712 */
713 static PIPE_THREAD_ROUTINE( thread_func, init_data )
714 {
715 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
716 struct lp_rasterizer *rast = task->rast;
717 boolean debug = false;
718
719 while (1) {
720 /* wait for work */
721 if (debug)
722 debug_printf("thread %d waiting for work\n", task->thread_index);
723 pipe_semaphore_wait(&task->work_ready);
724
725 if (rast->exit_flag)
726 break;
727
728 if (task->thread_index == 0) {
729 /* thread[0]:
730 * - get next scene to rasterize
731 * - map the framebuffer surfaces
732 */
733 lp_rast_begin( rast,
734 lp_scene_dequeue( rast->full_scenes, TRUE ) );
735 }
736
737 /* Wait for all threads to get here so that threads[1+] don't
738 * get a null rast->curr_scene pointer.
739 */
740 pipe_barrier_wait( &rast->barrier );
741
742 /* do work */
743 if (debug)
744 debug_printf("thread %d doing work\n", task->thread_index);
745
746 rasterize_scene(task,
747 rast->curr_scene);
748
749 /* wait for all threads to finish with this scene */
750 pipe_barrier_wait( &rast->barrier );
751
752 /* XXX: shouldn't be necessary:
753 */
754 if (task->thread_index == 0) {
755 lp_rast_end( rast );
756 }
757
758 /* signal done with work */
759 if (debug)
760 debug_printf("thread %d done working\n", task->thread_index);
761
762 pipe_semaphore_signal(&task->work_done);
763 }
764
765 return NULL;
766 }
767
768
769 /**
770 * Initialize semaphores and spawn the threads.
771 */
772 static void
773 create_rast_threads(struct lp_rasterizer *rast)
774 {
775 unsigned i;
776
777 #ifdef PIPE_OS_WINDOWS
778 /* Multithreading not supported on windows until conditions and barriers are
779 * properly implemented. */
780 rast->num_threads = 0;
781 #else
782 rast->num_threads = util_cpu_caps.nr_cpus;
783 rast->num_threads = debug_get_num_option("LP_NUM_THREADS", rast->num_threads);
784 rast->num_threads = MIN2(rast->num_threads, MAX_THREADS);
785 #endif
786
787 /* NOTE: if num_threads is zero, we won't use any threads */
788 for (i = 0; i < rast->num_threads; i++) {
789 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
790 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
791 rast->threads[i] = pipe_thread_create(thread_func,
792 (void *) &rast->tasks[i]);
793 }
794 }
795
796
797
798 /**
799 * Create new lp_rasterizer.
800 * \param empty the queue to put empty scenes on after we've finished
801 * processing them.
802 */
803 struct lp_rasterizer *
804 lp_rast_create( void )
805 {
806 struct lp_rasterizer *rast;
807 unsigned i, cbuf;
808
809 rast = CALLOC_STRUCT(lp_rasterizer);
810 if(!rast)
811 return NULL;
812
813 rast->full_scenes = lp_scene_queue_create();
814
815 for (i = 0; i < Elements(rast->tasks); i++) {
816 struct lp_rasterizer_task *task = &rast->tasks[i];
817
818 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
819 task->tile.color[cbuf] = align_malloc(TILE_SIZE * TILE_SIZE * 4, 16);
820
821 task->rast = rast;
822 task->thread_index = i;
823 }
824
825 create_rast_threads(rast);
826
827 /* for synchronizing rasterization threads */
828 pipe_barrier_init( &rast->barrier, rast->num_threads );
829
830 return rast;
831 }
832
833
834 /* Shutdown:
835 */
836 void lp_rast_destroy( struct lp_rasterizer *rast )
837 {
838 unsigned i, cbuf;
839
840 for (i = 0; i < Elements(rast->tasks); i++) {
841 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
842 align_free(rast->tasks[i].tile.color[cbuf]);
843 }
844
845 /* Set exit_flag and signal each thread's work_ready semaphore.
846 * Each thread will be woken up, notice that the exit_flag is set and
847 * break out of its main loop. The thread will then exit.
848 */
849 rast->exit_flag = TRUE;
850 for (i = 0; i < rast->num_threads; i++) {
851 pipe_semaphore_signal(&rast->tasks[i].work_ready);
852 }
853
854 for (i = 0; i < rast->num_threads; i++) {
855 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
856 pipe_semaphore_destroy(&rast->tasks[i].work_done);
857 }
858
859 /* for synchronizing rasterization threads */
860 pipe_barrier_destroy( &rast->barrier );
861
862 FREE(rast);
863 }
864
865
866 /** Return number of rasterization threads */
867 unsigned
868 lp_rast_get_num_threads( struct lp_rasterizer *rast )
869 {
870 return rast->num_threads;
871 }