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