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