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