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