llvmpipe: record sample info for color/depth buffers in scene
[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_rect.h"
32 #include "util/u_surface.h"
33 #include "util/u_pack_color.h"
34 #include "util/u_string.h"
35 #include "util/u_thread.h"
36 #include "util/u_memset.h"
37 #include "util/os_time.h"
38
39 #include "lp_scene_queue.h"
40 #include "lp_context.h"
41 #include "lp_debug.h"
42 #include "lp_fence.h"
43 #include "lp_perf.h"
44 #include "lp_query.h"
45 #include "lp_rast.h"
46 #include "lp_rast_priv.h"
47 #include "gallivm/lp_bld_format.h"
48 #include "gallivm/lp_bld_debug.h"
49 #include "lp_scene.h"
50 #include "lp_tex_sample.h"
51
52
53 #ifdef DEBUG
54 int jit_line = 0;
55 const struct lp_rast_state *jit_state = NULL;
56 const struct lp_rasterizer_task *jit_task = NULL;
57 #endif
58
59 const float lp_sample_pos_4x[4][2] = { { 0.375, 0.125 },
60 { 0.875, 0.375 },
61 { 0.125, 0.625 },
62 { 0.625, 0.875 } };
63
64 /**
65 * Begin rasterizing a scene.
66 * Called once per scene by one thread.
67 */
68 static void
69 lp_rast_begin( struct lp_rasterizer *rast,
70 struct lp_scene *scene )
71 {
72 rast->curr_scene = scene;
73
74 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
75
76 lp_scene_begin_rasterization( scene );
77 lp_scene_bin_iter_begin( scene );
78 }
79
80
81 static void
82 lp_rast_end( struct lp_rasterizer *rast )
83 {
84 lp_scene_end_rasterization( rast->curr_scene );
85
86 rast->curr_scene = NULL;
87 }
88
89
90 /**
91 * Beginning rasterization of a tile.
92 * \param x window X position of the tile, in pixels
93 * \param y window Y position of the tile, in pixels
94 */
95 static void
96 lp_rast_tile_begin(struct lp_rasterizer_task *task,
97 const struct cmd_bin *bin,
98 int x, int y)
99 {
100 unsigned i;
101 struct lp_scene *scene = task->scene;
102
103 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
104
105 task->bin = bin;
106 task->x = x * TILE_SIZE;
107 task->y = y * TILE_SIZE;
108 task->width = TILE_SIZE + x * TILE_SIZE > task->scene->fb.width ?
109 task->scene->fb.width - x * TILE_SIZE : TILE_SIZE;
110 task->height = TILE_SIZE + y * TILE_SIZE > task->scene->fb.height ?
111 task->scene->fb.height - y * TILE_SIZE : TILE_SIZE;
112
113 task->thread_data.vis_counter = 0;
114 task->thread_data.ps_invocations = 0;
115
116 for (i = 0; i < task->scene->fb.nr_cbufs; i++) {
117 if (task->scene->fb.cbufs[i]) {
118 task->color_tiles[i] = scene->cbufs[i].map +
119 scene->cbufs[i].stride * task->y +
120 scene->cbufs[i].format_bytes * task->x;
121 }
122 }
123 if (task->scene->fb.zsbuf) {
124 task->depth_tile = scene->zsbuf.map +
125 scene->zsbuf.stride * task->y +
126 scene->zsbuf.format_bytes * task->x;
127 }
128 }
129
130
131 /**
132 * Clear the rasterizer's current color tile.
133 * This is a bin command called during bin processing.
134 * Clear commands always clear all bound layers.
135 */
136 static void
137 lp_rast_clear_color(struct lp_rasterizer_task *task,
138 const union lp_rast_cmd_arg arg)
139 {
140 const struct lp_scene *scene = task->scene;
141 unsigned cbuf = arg.clear_rb->cbuf;
142 union util_color uc;
143 enum pipe_format format;
144
145 /* we never bin clear commands for non-existing buffers */
146 assert(cbuf < scene->fb.nr_cbufs);
147 assert(scene->fb.cbufs[cbuf]);
148
149 format = scene->fb.cbufs[cbuf]->format;
150 uc = arg.clear_rb->color_val;
151
152 /*
153 * this is pretty rough since we have target format (bunch of bytes...) here.
154 * dump it as raw 4 dwords.
155 */
156 LP_DBG(DEBUG_RAST, "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",
157 __FUNCTION__, format, uc.ui[0], uc.ui[1], uc.ui[2], uc.ui[3]);
158
159
160 util_fill_box(scene->cbufs[cbuf].map,
161 format,
162 scene->cbufs[cbuf].stride,
163 scene->cbufs[cbuf].layer_stride,
164 task->x,
165 task->y,
166 0,
167 task->width,
168 task->height,
169 scene->fb_max_layer + 1,
170 &uc);
171
172 /* this will increase for each rb which probably doesn't mean much */
173 LP_COUNT(nr_color_tile_clear);
174 }
175
176
177 /**
178 * Clear the rasterizer's current z/stencil tile.
179 * This is a bin command called during bin processing.
180 * Clear commands always clear all bound layers.
181 */
182 static void
183 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
184 const union lp_rast_cmd_arg arg)
185 {
186 const struct lp_scene *scene = task->scene;
187 uint64_t clear_value64 = arg.clear_zstencil.value;
188 uint64_t clear_mask64 = arg.clear_zstencil.mask;
189 uint32_t clear_value = (uint32_t) clear_value64;
190 uint32_t clear_mask = (uint32_t) clear_mask64;
191 const unsigned height = task->height;
192 const unsigned width = task->width;
193 const unsigned dst_stride = scene->zsbuf.stride;
194 uint8_t *dst;
195 unsigned i, j;
196 unsigned block_size;
197
198 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
199 __FUNCTION__, clear_value, clear_mask);
200
201 /*
202 * Clear the area of the depth/depth buffer matching this tile.
203 */
204
205 if (scene->fb.zsbuf) {
206 unsigned layer;
207 uint8_t *dst_layer = task->depth_tile;
208 block_size = util_format_get_blocksize(scene->fb.zsbuf->format);
209
210 clear_value &= clear_mask;
211
212 for (layer = 0; layer <= scene->fb_max_layer; layer++) {
213 dst = dst_layer;
214
215 switch (block_size) {
216 case 1:
217 assert(clear_mask == 0xff);
218 for (i = 0; i < height; i++) {
219 uint8_t *row = (uint8_t *)dst;
220 memset(row, (uint8_t) clear_value, width);
221 dst += dst_stride;
222 }
223 break;
224 case 2:
225 if (clear_mask == 0xffff) {
226 for (i = 0; i < height; i++) {
227 uint16_t *row = (uint16_t *)dst;
228 for (j = 0; j < width; j++)
229 *row++ = (uint16_t) clear_value;
230 dst += dst_stride;
231 }
232 }
233 else {
234 for (i = 0; i < height; i++) {
235 uint16_t *row = (uint16_t *)dst;
236 for (j = 0; j < width; j++) {
237 uint16_t tmp = ~clear_mask & *row;
238 *row++ = clear_value | tmp;
239 }
240 dst += dst_stride;
241 }
242 }
243 break;
244 case 4:
245 if (clear_mask == 0xffffffff) {
246 for (i = 0; i < height; i++) {
247 util_memset32(dst, clear_value, width);
248 dst += dst_stride;
249 }
250 }
251 else {
252 for (i = 0; i < height; i++) {
253 uint32_t *row = (uint32_t *)dst;
254 for (j = 0; j < width; j++) {
255 uint32_t tmp = ~clear_mask & *row;
256 *row++ = clear_value | tmp;
257 }
258 dst += dst_stride;
259 }
260 }
261 break;
262 case 8:
263 clear_value64 &= clear_mask64;
264 if (clear_mask64 == 0xffffffffffULL) {
265 for (i = 0; i < height; i++) {
266 uint64_t *row = (uint64_t *)dst;
267 for (j = 0; j < width; j++)
268 *row++ = clear_value64;
269 dst += dst_stride;
270 }
271 }
272 else {
273 for (i = 0; i < height; i++) {
274 uint64_t *row = (uint64_t *)dst;
275 for (j = 0; j < width; j++) {
276 uint64_t tmp = ~clear_mask64 & *row;
277 *row++ = clear_value64 | tmp;
278 }
279 dst += dst_stride;
280 }
281 }
282 break;
283
284 default:
285 assert(0);
286 break;
287 }
288 dst_layer += scene->zsbuf.layer_stride;
289 }
290 }
291 }
292
293
294
295 /**
296 * Run the shader on all blocks in a tile. This is used when a tile is
297 * completely contained inside a triangle.
298 * This is a bin command called during bin processing.
299 */
300 static void
301 lp_rast_shade_tile(struct lp_rasterizer_task *task,
302 const union lp_rast_cmd_arg arg)
303 {
304 const struct lp_scene *scene = task->scene;
305 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
306 const struct lp_rast_state *state;
307 struct lp_fragment_shader_variant *variant;
308 const unsigned tile_x = task->x, tile_y = task->y;
309 unsigned x, y;
310
311 if (inputs->disable) {
312 /* This command was partially binned and has been disabled */
313 return;
314 }
315
316 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
317
318 state = task->state;
319 assert(state);
320 if (!state) {
321 return;
322 }
323 variant = state->variant;
324
325 /* render the whole 64x64 tile in 4x4 chunks */
326 for (y = 0; y < task->height; y += 4){
327 for (x = 0; x < task->width; x += 4) {
328 uint8_t *color[PIPE_MAX_COLOR_BUFS];
329 unsigned stride[PIPE_MAX_COLOR_BUFS];
330 unsigned sample_stride[PIPE_MAX_COLOR_BUFS];
331 uint8_t *depth = NULL;
332 unsigned depth_stride = 0;
333 unsigned depth_sample_stride = 0;
334 unsigned i;
335
336 /* color buffer */
337 for (i = 0; i < scene->fb.nr_cbufs; i++){
338 if (scene->fb.cbufs[i]) {
339 stride[i] = scene->cbufs[i].stride;
340 sample_stride[i] = scene->cbufs[i].sample_stride;
341 color[i] = lp_rast_get_color_block_pointer(task, i, tile_x + x,
342 tile_y + y, inputs->layer);
343 }
344 else {
345 stride[i] = 0;
346 sample_stride[i] = 0;
347 color[i] = NULL;
348 }
349 }
350
351 /* depth buffer */
352 if (scene->zsbuf.map) {
353 depth = lp_rast_get_depth_block_pointer(task, tile_x + x,
354 tile_y + y, inputs->layer);
355 depth_stride = scene->zsbuf.stride;
356 depth_sample_stride = scene->zsbuf.sample_stride;
357 }
358
359 /* Propagate non-interpolated raster state. */
360 task->thread_data.raster_state.viewport_index = inputs->viewport_index;
361
362 /* run shader on 4x4 block */
363 BEGIN_JIT_CALL(state, task);
364 variant->jit_function[RAST_WHOLE]( &state->jit_context,
365 tile_x + x, tile_y + y,
366 inputs->frontfacing,
367 GET_A0(inputs),
368 GET_DADX(inputs),
369 GET_DADY(inputs),
370 color,
371 depth,
372 0xffff,
373 &task->thread_data,
374 stride,
375 depth_stride,
376 sample_stride,
377 depth_sample_stride);
378 END_JIT_CALL();
379 }
380 }
381 }
382
383
384 /**
385 * Run the shader on all blocks in a tile. This is used when a tile is
386 * completely contained inside a triangle, and the shader is opaque.
387 * This is a bin command called during bin processing.
388 */
389 static void
390 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
391 const union lp_rast_cmd_arg arg)
392 {
393 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
394
395 assert(task->state);
396 if (!task->state) {
397 return;
398 }
399
400 lp_rast_shade_tile(task, arg);
401 }
402
403
404 /**
405 * Compute shading for a 4x4 block of pixels inside a triangle.
406 * This is a bin command called during bin processing.
407 * \param x X position of quad in window coords
408 * \param y Y position of quad in window coords
409 */
410 void
411 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
412 const struct lp_rast_shader_inputs *inputs,
413 unsigned x, unsigned y,
414 unsigned mask)
415 {
416 const struct lp_rast_state *state = task->state;
417 struct lp_fragment_shader_variant *variant = state->variant;
418 const struct lp_scene *scene = task->scene;
419 uint8_t *color[PIPE_MAX_COLOR_BUFS];
420 unsigned stride[PIPE_MAX_COLOR_BUFS];
421 unsigned sample_stride[PIPE_MAX_COLOR_BUFS];
422 uint8_t *depth = NULL;
423 unsigned depth_stride = 0;
424 unsigned depth_sample_stride = 0;
425 unsigned i;
426
427 assert(state);
428
429 /* Sanity checks */
430 assert(x < scene->tiles_x * TILE_SIZE);
431 assert(y < scene->tiles_y * TILE_SIZE);
432 assert(x % TILE_VECTOR_WIDTH == 0);
433 assert(y % TILE_VECTOR_HEIGHT == 0);
434
435 assert((x % 4) == 0);
436 assert((y % 4) == 0);
437
438 /* color buffer */
439 for (i = 0; i < scene->fb.nr_cbufs; i++) {
440 if (scene->fb.cbufs[i]) {
441 stride[i] = scene->cbufs[i].stride;
442 sample_stride[i] = scene->cbufs[i].sample_stride;
443 color[i] = lp_rast_get_color_block_pointer(task, i, x, y,
444 inputs->layer);
445 }
446 else {
447 stride[i] = 0;
448 sample_stride[i] = 0;
449 color[i] = NULL;
450 }
451 }
452
453 /* depth buffer */
454 if (scene->zsbuf.map) {
455 depth_stride = scene->zsbuf.stride;
456 depth_sample_stride = scene->zsbuf.sample_stride;
457 depth = lp_rast_get_depth_block_pointer(task, x, y, inputs->layer);
458 }
459
460 assert(lp_check_alignment(state->jit_context.u8_blend_color, 16));
461
462 /*
463 * The rasterizer may produce fragments outside our
464 * allocated 4x4 blocks hence need to filter them out here.
465 */
466 if ((x % TILE_SIZE) < task->width && (y % TILE_SIZE) < task->height) {
467 /* Propagate non-interpolated raster state. */
468 task->thread_data.raster_state.viewport_index = inputs->viewport_index;
469
470 /* run shader on 4x4 block */
471 BEGIN_JIT_CALL(state, task);
472 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
473 x, y,
474 inputs->frontfacing,
475 GET_A0(inputs),
476 GET_DADX(inputs),
477 GET_DADY(inputs),
478 color,
479 depth,
480 mask,
481 &task->thread_data,
482 stride,
483 depth_stride,
484 sample_stride,
485 depth_sample_stride);
486 END_JIT_CALL();
487 }
488 }
489
490
491
492 /**
493 * Begin a new occlusion query.
494 * This is a bin command put in all bins.
495 * Called per thread.
496 */
497 static void
498 lp_rast_begin_query(struct lp_rasterizer_task *task,
499 const union lp_rast_cmd_arg arg)
500 {
501 struct llvmpipe_query *pq = arg.query_obj;
502
503 switch (pq->type) {
504 case PIPE_QUERY_OCCLUSION_COUNTER:
505 case PIPE_QUERY_OCCLUSION_PREDICATE:
506 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
507 pq->start[task->thread_index] = task->thread_data.vis_counter;
508 break;
509 case PIPE_QUERY_PIPELINE_STATISTICS:
510 pq->start[task->thread_index] = task->thread_data.ps_invocations;
511 break;
512 default:
513 assert(0);
514 break;
515 }
516 }
517
518
519 /**
520 * End the current occlusion query.
521 * This is a bin command put in all bins.
522 * Called per thread.
523 */
524 static void
525 lp_rast_end_query(struct lp_rasterizer_task *task,
526 const union lp_rast_cmd_arg arg)
527 {
528 struct llvmpipe_query *pq = arg.query_obj;
529
530 switch (pq->type) {
531 case PIPE_QUERY_OCCLUSION_COUNTER:
532 case PIPE_QUERY_OCCLUSION_PREDICATE:
533 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
534 pq->end[task->thread_index] +=
535 task->thread_data.vis_counter - pq->start[task->thread_index];
536 pq->start[task->thread_index] = 0;
537 break;
538 case PIPE_QUERY_TIMESTAMP:
539 pq->end[task->thread_index] = os_time_get_nano();
540 break;
541 case PIPE_QUERY_PIPELINE_STATISTICS:
542 pq->end[task->thread_index] +=
543 task->thread_data.ps_invocations - pq->start[task->thread_index];
544 pq->start[task->thread_index] = 0;
545 break;
546 default:
547 assert(0);
548 break;
549 }
550 }
551
552
553 void
554 lp_rast_set_state(struct lp_rasterizer_task *task,
555 const union lp_rast_cmd_arg arg)
556 {
557 task->state = arg.state;
558 }
559
560
561
562 /**
563 * Called when we're done writing to a color tile.
564 */
565 static void
566 lp_rast_tile_end(struct lp_rasterizer_task *task)
567 {
568 unsigned i;
569
570 for (i = 0; i < task->scene->num_active_queries; ++i) {
571 lp_rast_end_query(task, lp_rast_arg_query(task->scene->active_queries[i]));
572 }
573
574 /* debug */
575 memset(task->color_tiles, 0, sizeof(task->color_tiles));
576 task->depth_tile = NULL;
577
578 task->bin = NULL;
579 }
580
581 static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
582 {
583 lp_rast_clear_color,
584 lp_rast_clear_zstencil,
585 lp_rast_triangle_1,
586 lp_rast_triangle_2,
587 lp_rast_triangle_3,
588 lp_rast_triangle_4,
589 lp_rast_triangle_5,
590 lp_rast_triangle_6,
591 lp_rast_triangle_7,
592 lp_rast_triangle_8,
593 lp_rast_triangle_3_4,
594 lp_rast_triangle_3_16,
595 lp_rast_triangle_4_16,
596 lp_rast_shade_tile,
597 lp_rast_shade_tile_opaque,
598 lp_rast_begin_query,
599 lp_rast_end_query,
600 lp_rast_set_state,
601 lp_rast_triangle_32_1,
602 lp_rast_triangle_32_2,
603 lp_rast_triangle_32_3,
604 lp_rast_triangle_32_4,
605 lp_rast_triangle_32_5,
606 lp_rast_triangle_32_6,
607 lp_rast_triangle_32_7,
608 lp_rast_triangle_32_8,
609 lp_rast_triangle_32_3_4,
610 lp_rast_triangle_32_3_16,
611 lp_rast_triangle_32_4_16
612 };
613
614
615 static void
616 do_rasterize_bin(struct lp_rasterizer_task *task,
617 const struct cmd_bin *bin,
618 int x, int y)
619 {
620 const struct cmd_block *block;
621 unsigned k;
622
623 if (0)
624 lp_debug_bin(bin, x, y);
625
626 for (block = bin->head; block; block = block->next) {
627 for (k = 0; k < block->count; k++) {
628 dispatch[block->cmd[k]]( task, block->arg[k] );
629 }
630 }
631 }
632
633
634
635 /**
636 * Rasterize commands for a single bin.
637 * \param x, y position of the bin's tile in the framebuffer
638 * Must be called between lp_rast_begin() and lp_rast_end().
639 * Called per thread.
640 */
641 static void
642 rasterize_bin(struct lp_rasterizer_task *task,
643 const struct cmd_bin *bin, int x, int y )
644 {
645 lp_rast_tile_begin( task, bin, x, y );
646
647 do_rasterize_bin(task, bin, x, y);
648
649 lp_rast_tile_end(task);
650
651 #ifdef DEBUG
652 /* Debug/Perf flags:
653 */
654 if (bin->head->count == 1) {
655 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
656 LP_COUNT(nr_pure_shade_opaque_64);
657 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
658 LP_COUNT(nr_pure_shade_64);
659 }
660 #endif
661 }
662
663
664 /* An empty bin is one that just loads the contents of the tile and
665 * stores them again unchanged. This typically happens when bins have
666 * been flushed for some reason in the middle of a frame, or when
667 * incremental updates are being made to a render target.
668 *
669 * Try to avoid doing pointless work in this case.
670 */
671 static boolean
672 is_empty_bin( const struct cmd_bin *bin )
673 {
674 return bin->head == NULL;
675 }
676
677
678 /**
679 * Rasterize/execute all bins within a scene.
680 * Called per thread.
681 */
682 static void
683 rasterize_scene(struct lp_rasterizer_task *task,
684 struct lp_scene *scene)
685 {
686 task->scene = scene;
687
688 /* Clear the cache tags. This should not always be necessary but
689 simpler for now. */
690 #if LP_USE_TEXTURE_CACHE
691 memset(task->thread_data.cache->cache_tags, 0,
692 sizeof(task->thread_data.cache->cache_tags));
693 #if LP_BUILD_FORMAT_CACHE_DEBUG
694 task->thread_data.cache->cache_access_total = 0;
695 task->thread_data.cache->cache_access_miss = 0;
696 #endif
697 #endif
698
699 if (!task->rast->no_rast) {
700 /* loop over scene bins, rasterize each */
701 {
702 struct cmd_bin *bin;
703 int i, j;
704
705 assert(scene);
706 while ((bin = lp_scene_bin_iter_next(scene, &i, &j))) {
707 if (!is_empty_bin( bin ))
708 rasterize_bin(task, bin, i, j);
709 }
710 }
711 }
712
713
714 #if LP_BUILD_FORMAT_CACHE_DEBUG
715 {
716 uint64_t total, miss;
717 total = task->thread_data.cache->cache_access_total;
718 miss = task->thread_data.cache->cache_access_miss;
719 if (total) {
720 debug_printf("thread %d cache access %llu miss %llu hit rate %f\n",
721 task->thread_index, (long long unsigned)total,
722 (long long unsigned)miss,
723 (float)(total - miss)/(float)total);
724 }
725 }
726 #endif
727
728 if (scene->fence) {
729 lp_fence_signal(scene->fence);
730 }
731
732 task->scene = NULL;
733 }
734
735
736 /**
737 * Called by setup module when it has something for us to render.
738 */
739 void
740 lp_rast_queue_scene( struct lp_rasterizer *rast,
741 struct lp_scene *scene)
742 {
743 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
744
745 if (rast->num_threads == 0) {
746 /* no threading */
747 unsigned fpstate = util_fpstate_get();
748
749 /* Make sure that denorms are treated like zeros. This is
750 * the behavior required by D3D10. OpenGL doesn't care.
751 */
752 util_fpstate_set_denorms_to_zero(fpstate);
753
754 lp_rast_begin( rast, scene );
755
756 rasterize_scene( &rast->tasks[0], scene );
757
758 lp_rast_end( rast );
759
760 util_fpstate_set(fpstate);
761
762 rast->curr_scene = NULL;
763 }
764 else {
765 /* threaded rendering! */
766 unsigned i;
767
768 lp_scene_enqueue( rast->full_scenes, scene );
769
770 /* signal the threads that there's work to do */
771 for (i = 0; i < rast->num_threads; i++) {
772 pipe_semaphore_signal(&rast->tasks[i].work_ready);
773 }
774 }
775
776 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
777 }
778
779
780 void
781 lp_rast_finish( struct lp_rasterizer *rast )
782 {
783 if (rast->num_threads == 0) {
784 /* nothing to do */
785 }
786 else {
787 int i;
788
789 /* wait for work to complete */
790 for (i = 0; i < rast->num_threads; i++) {
791 pipe_semaphore_wait(&rast->tasks[i].work_done);
792 }
793 }
794 }
795
796
797 /**
798 * This is the thread's main entrypoint.
799 * It's a simple loop:
800 * 1. wait for work
801 * 2. do work
802 * 3. signal that we're done
803 */
804 static int
805 thread_function(void *init_data)
806 {
807 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
808 struct lp_rasterizer *rast = task->rast;
809 boolean debug = false;
810 char thread_name[16];
811 unsigned fpstate;
812
813 snprintf(thread_name, sizeof thread_name, "llvmpipe-%u", task->thread_index);
814 u_thread_setname(thread_name);
815
816 /* Make sure that denorms are treated like zeros. This is
817 * the behavior required by D3D10. OpenGL doesn't care.
818 */
819 fpstate = util_fpstate_get();
820 util_fpstate_set_denorms_to_zero(fpstate);
821
822 while (1) {
823 /* wait for work */
824 if (debug)
825 debug_printf("thread %d waiting for work\n", task->thread_index);
826 pipe_semaphore_wait(&task->work_ready);
827
828 if (rast->exit_flag)
829 break;
830
831 if (task->thread_index == 0) {
832 /* thread[0]:
833 * - get next scene to rasterize
834 * - map the framebuffer surfaces
835 */
836 lp_rast_begin( rast,
837 lp_scene_dequeue( rast->full_scenes, TRUE ) );
838 }
839
840 /* Wait for all threads to get here so that threads[1+] don't
841 * get a null rast->curr_scene pointer.
842 */
843 util_barrier_wait( &rast->barrier );
844
845 /* do work */
846 if (debug)
847 debug_printf("thread %d doing work\n", task->thread_index);
848
849 rasterize_scene(task,
850 rast->curr_scene);
851
852 /* wait for all threads to finish with this scene */
853 util_barrier_wait( &rast->barrier );
854
855 /* XXX: shouldn't be necessary:
856 */
857 if (task->thread_index == 0) {
858 lp_rast_end( rast );
859 }
860
861 /* signal done with work */
862 if (debug)
863 debug_printf("thread %d done working\n", task->thread_index);
864
865 pipe_semaphore_signal(&task->work_done);
866 }
867
868 #ifdef _WIN32
869 pipe_semaphore_signal(&task->work_done);
870 #endif
871
872 return 0;
873 }
874
875
876 /**
877 * Initialize semaphores and spawn the threads.
878 */
879 static void
880 create_rast_threads(struct lp_rasterizer *rast)
881 {
882 unsigned i;
883
884 /* NOTE: if num_threads is zero, we won't use any threads */
885 for (i = 0; i < rast->num_threads; i++) {
886 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
887 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
888 rast->threads[i] = u_thread_create(thread_function,
889 (void *) &rast->tasks[i]);
890 if (!rast->threads[i]) {
891 rast->num_threads = i; /* previous thread is max */
892 break;
893 }
894 }
895 }
896
897
898
899 /**
900 * Create new lp_rasterizer. If num_threads is zero, don't create any
901 * new threads, do rendering synchronously.
902 * \param num_threads number of rasterizer threads to create
903 */
904 struct lp_rasterizer *
905 lp_rast_create( unsigned num_threads )
906 {
907 struct lp_rasterizer *rast;
908 unsigned i;
909
910 rast = CALLOC_STRUCT(lp_rasterizer);
911 if (!rast) {
912 goto no_rast;
913 }
914
915 rast->full_scenes = lp_scene_queue_create();
916 if (!rast->full_scenes) {
917 goto no_full_scenes;
918 }
919
920 for (i = 0; i < MAX2(1, num_threads); i++) {
921 struct lp_rasterizer_task *task = &rast->tasks[i];
922 task->rast = rast;
923 task->thread_index = i;
924 task->thread_data.cache = align_malloc(sizeof(struct lp_build_format_cache),
925 16);
926 if (!task->thread_data.cache) {
927 goto no_thread_data_cache;
928 }
929 }
930
931 rast->num_threads = num_threads;
932
933 rast->no_rast = debug_get_bool_option("LP_NO_RAST", FALSE);
934
935 create_rast_threads(rast);
936
937 /* for synchronizing rasterization threads */
938 if (rast->num_threads > 0) {
939 util_barrier_init( &rast->barrier, rast->num_threads );
940 }
941
942 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
943
944 return rast;
945
946 no_thread_data_cache:
947 for (i = 0; i < MAX2(1, rast->num_threads); i++) {
948 if (rast->tasks[i].thread_data.cache) {
949 align_free(rast->tasks[i].thread_data.cache);
950 }
951 }
952
953 lp_scene_queue_destroy(rast->full_scenes);
954 no_full_scenes:
955 FREE(rast);
956 no_rast:
957 return NULL;
958 }
959
960
961 /* Shutdown:
962 */
963 void lp_rast_destroy( struct lp_rasterizer *rast )
964 {
965 unsigned i;
966
967 /* Set exit_flag and signal each thread's work_ready semaphore.
968 * Each thread will be woken up, notice that the exit_flag is set and
969 * break out of its main loop. The thread will then exit.
970 */
971 rast->exit_flag = TRUE;
972 for (i = 0; i < rast->num_threads; i++) {
973 pipe_semaphore_signal(&rast->tasks[i].work_ready);
974 }
975
976 /* Wait for threads to terminate before cleaning up per-thread data.
977 * We don't actually call pipe_thread_wait to avoid dead lock on Windows
978 * per https://bugs.freedesktop.org/show_bug.cgi?id=76252 */
979 for (i = 0; i < rast->num_threads; i++) {
980 #ifdef _WIN32
981 pipe_semaphore_wait(&rast->tasks[i].work_done);
982 #else
983 thrd_join(rast->threads[i], NULL);
984 #endif
985 }
986
987 /* Clean up per-thread data */
988 for (i = 0; i < rast->num_threads; i++) {
989 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
990 pipe_semaphore_destroy(&rast->tasks[i].work_done);
991 }
992 for (i = 0; i < MAX2(1, rast->num_threads); i++) {
993 align_free(rast->tasks[i].thread_data.cache);
994 }
995
996 /* for synchronizing rasterization threads */
997 if (rast->num_threads > 0) {
998 util_barrier_destroy( &rast->barrier );
999 }
1000
1001 lp_scene_queue_destroy(rast->full_scenes);
1002
1003 FREE(rast);
1004 }
1005
1006