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