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_screen.h"
51 #include "lp_tex_sample.h"
52
53 #ifdef _WIN32
54 #include <windows.h>
55 #endif
56
57 #if MESA_DEBUG
58 int jit_line = 0;
59 const struct lp_rast_state *jit_state = NULL;
60 const struct lp_rasterizer_task *jit_task = NULL;
61 #endif
62
63 const float lp_sample_pos_4x[4][2] = { { 0.375, 0.125 },
64 { 0.875, 0.375 },
65 { 0.125, 0.625 },
66 { 0.625, 0.875 } };
67
68 /**
69 * Begin rasterizing a scene.
70 * Called once per scene by one thread.
71 */
72 static void
lp_rast_begin(struct lp_rasterizer * rast,struct lp_scene * scene)73 lp_rast_begin(struct lp_rasterizer *rast,
74 struct lp_scene *scene)
75 {
76 rast->curr_scene = scene;
77
78 LP_DBG(DEBUG_RAST, "%s\n", __func__);
79
80 lp_scene_begin_rasterization(scene);
81 lp_scene_bin_iter_begin(scene);
82 }
83
84
85 static void
lp_rast_end(struct lp_rasterizer * rast)86 lp_rast_end(struct lp_rasterizer *rast)
87 {
88 rast->curr_scene = NULL;
89 }
90
91
92 /**
93 * Beginning rasterization of a tile.
94 * \param x window X position of the tile, in pixels
95 * \param y window Y position of the tile, in pixels
96 */
97 static void
lp_rast_tile_begin(struct lp_rasterizer_task * task,const struct cmd_bin * bin,int x,int y)98 lp_rast_tile_begin(struct lp_rasterizer_task *task,
99 const struct cmd_bin *bin,
100 int x, int y)
101 {
102 struct lp_scene *scene = task->scene;
103
104 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __func__, x, y);
105
106 task->bin = bin;
107 task->x = x * TILE_SIZE;
108 task->y = y * TILE_SIZE;
109 task->width = TILE_SIZE + x * TILE_SIZE > scene->fb.width ?
110 scene->fb.width - x * TILE_SIZE : TILE_SIZE;
111 task->height = TILE_SIZE + y * TILE_SIZE > scene->fb.height ?
112 scene->fb.height - y * TILE_SIZE : TILE_SIZE;
113
114 task->thread_data.vis_counter = 0;
115 task->thread_data.ps_invocations = 0;
116
117 for (unsigned i = 0; i < scene->fb.nr_cbufs; i++) {
118 if (scene->fb.cbufs[i]) {
119 task->color_tiles[i] = scene->cbufs[i].map +
120 scene->cbufs[i].stride * task->y +
121 scene->cbufs[i].format_bytes * task->x;
122 }
123 }
124 if (scene->fb.zsbuf) {
125 task->depth_tile = scene->zsbuf.map +
126 scene->zsbuf.stride * task->y +
127 scene->zsbuf.format_bytes * task->x;
128 }
129 }
130
131
132 /**
133 * Clear the rasterizer's current color tile.
134 * This is a bin command called during bin processing.
135 * Clear commands always clear all bound layers.
136 */
137 static void
lp_rast_clear_color(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)138 lp_rast_clear_color(struct lp_rasterizer_task *task,
139 const union lp_rast_cmd_arg arg)
140 {
141 const struct lp_scene *scene = task->scene;
142 const unsigned cbuf = arg.clear_rb->cbuf;
143
144 /* we never bin clear commands for non-existing buffers */
145 assert(cbuf < scene->fb.nr_cbufs);
146 assert(scene->fb.cbufs[cbuf]);
147
148 const enum pipe_format format = scene->fb.cbufs[cbuf]->format;
149 union util_color uc = arg.clear_rb->color_val;
150
151 /*
152 * this is pretty rough since we have target format (bunch of bytes...)
153 * here. dump it as raw 4 dwords.
154 */
155 LP_DBG(DEBUG_RAST,
156 "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",
157 __func__, 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
161 + scene->cbufs[cbuf].sample_stride * s;
162 util_fill_box(map,
163 format,
164 scene->cbufs[cbuf].stride,
165 scene->cbufs[cbuf].layer_stride,
166 task->x,
167 task->y,
168 0,
169 task->width,
170 task->height,
171 scene->cbufs[cbuf].layer_count,
172 &uc);
173 }
174
175 /* this will increase for each rb which probably doesn't mean much */
176 LP_COUNT(nr_color_tile_clear);
177 }
178
179
180 /**
181 * Clear the rasterizer's current z/stencil tile.
182 * This is a bin command called during bin processing.
183 * Clear commands always clear all bound layers.
184 */
185 static void
lp_rast_clear_zstencil(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)186 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
187 const union lp_rast_cmd_arg arg)
188 {
189 const struct lp_scene *scene = task->scene;
190 uint64_t clear_value64 = arg.clear_zstencil.value;
191 uint64_t clear_mask64 = arg.clear_zstencil.mask;
192 uint32_t clear_value = (uint32_t) clear_value64;
193 uint32_t clear_mask = (uint32_t) clear_mask64;
194 const unsigned height = task->height;
195 const unsigned width = task->width;
196 const unsigned dst_stride = scene->zsbuf.stride;
197
198 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
199 __func__, 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 for (unsigned s = 0; s < scene->zsbuf.nr_samples; s++) {
207 uint8_t *dst_layer =
208 task->depth_tile + (s * scene->zsbuf.sample_stride);
209 const unsigned block_size =
210 util_format_get_blocksize(scene->fb.zsbuf->format);
211
212 clear_value &= clear_mask;
213
214 for (unsigned layer = 0; layer <= scene->fb_max_layer; layer++) {
215 uint8_t *dst = dst_layer;
216
217 switch (block_size) {
218 case 1:
219 assert(clear_mask == 0xff);
220 for (unsigned i = 0; i < height; i++) {
221 uint8_t *row = (uint8_t *)dst;
222 memset(row, (uint8_t) clear_value, width);
223 dst += dst_stride;
224 }
225 break;
226 case 2:
227 if (clear_mask == 0xffff) {
228 for (unsigned i = 0; i < height; i++) {
229 uint16_t *row = (uint16_t *)dst;
230 for (unsigned j = 0; j < width; j++)
231 *row++ = (uint16_t) clear_value;
232 dst += dst_stride;
233 }
234 } else {
235 for (unsigned i = 0; i < height; i++) {
236 uint16_t *row = (uint16_t *)dst;
237 for (unsigned j = 0; j < width; j++) {
238 uint16_t tmp = ~clear_mask & *row;
239 *row++ = clear_value | tmp;
240 }
241 dst += dst_stride;
242 }
243 }
244 break;
245 case 4:
246 if (clear_mask == 0xffffffff) {
247 for (unsigned i = 0; i < height; i++) {
248 util_memset32(dst, clear_value, width);
249 dst += dst_stride;
250 }
251 } else {
252 for (unsigned i = 0; i < height; i++) {
253 uint32_t *row = (uint32_t *)dst;
254 for (unsigned 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 (unsigned i = 0; i < height; i++) {
266 util_memset64(dst, clear_value64, width);
267 dst += dst_stride;
268 }
269 } else {
270 for (unsigned i = 0; i < height; i++) {
271 uint64_t *row = (uint64_t *)dst;
272 for (unsigned j = 0; j < width; j++) {
273 uint64_t tmp = ~clear_mask64 & *row;
274 *row++ = clear_value64 | tmp;
275 }
276 dst += dst_stride;
277 }
278 }
279 break;
280
281 default:
282 assert(0);
283 break;
284 }
285 dst_layer += scene->zsbuf.layer_stride;
286 }
287 }
288 }
289 }
290
291
292 /**
293 * Run the shader on all blocks in a tile. This is used when a tile is
294 * completely contained inside a triangle.
295 * This is a bin command called during bin processing.
296 */
297 static void
lp_rast_shade_tile(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)298 lp_rast_shade_tile(struct lp_rasterizer_task *task,
299 const union lp_rast_cmd_arg arg)
300 {
301 const struct lp_scene *scene = task->scene;
302 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
303 const unsigned tile_x = task->x, tile_y = task->y;
304
305 if (inputs->disable) {
306 /* This command was partially binned and has been disabled */
307 return;
308 }
309
310 LP_DBG(DEBUG_RAST, "%s\n", __func__);
311
312 const struct lp_rast_state *state = task->state;
313 assert(state);
314 if (!state) {
315 return;
316 }
317
318 const struct lp_fragment_shader_variant *variant = state->variant;
319
320 unsigned view_index = inputs->view_index;
321 /* render the whole 64x64 tile in 4x4 chunks */
322 for (unsigned y = 0; y < task->height; y += 4){
323 for (unsigned x = 0; x < task->width; x += 4) {
324 /* color buffer */
325 uint8_t *color[PIPE_MAX_COLOR_BUFS];
326 unsigned stride[PIPE_MAX_COLOR_BUFS];
327 unsigned sample_stride[PIPE_MAX_COLOR_BUFS];
328 for (unsigned i = 0; i < scene->fb.nr_cbufs; i++){
329 if (scene->fb.cbufs[i]) {
330 stride[i] = scene->cbufs[i].stride;
331 sample_stride[i] = scene->cbufs[i].sample_stride;
332 color[i] = lp_rast_get_color_block_pointer(task, i, tile_x + x,
333 tile_y + y,
334 inputs->layer, view_index);
335 } else {
336 stride[i] = 0;
337 sample_stride[i] = 0;
338 color[i] = NULL;
339 }
340 }
341
342 /* depth buffer */
343 uint8_t *depth = NULL;
344 unsigned depth_stride = 0;
345 unsigned depth_sample_stride = 0;
346 if (scene->zsbuf.map) {
347 depth = lp_rast_get_depth_block_pointer(task, tile_x + x,
348 tile_y + y,
349 inputs->layer, view_index);
350 depth_stride = scene->zsbuf.stride;
351 depth_sample_stride = scene->zsbuf.sample_stride;
352 }
353
354 uint64_t mask = 0;
355 for (unsigned i = 0; i < scene->fb_max_samples; i++)
356 mask |= (uint64_t)(0xffff) << (16 * i);
357
358 /* Propagate non-interpolated raster state. */
359 task->thread_data.raster_state.viewport_index = inputs->viewport_index;
360 task->thread_data.raster_state.view_index = inputs->view_index;
361
362 /* run shader on 4x4 block */
363 BEGIN_JIT_CALL(state, task);
364 variant->jit_function[RAST_WHOLE](&state->jit_context,
365 &state->jit_resources,
366 tile_x + x, tile_y + y,
367 inputs->frontfacing,
368 GET_A0(inputs),
369 GET_DADX(inputs),
370 GET_DADY(inputs),
371 color,
372 depth,
373 mask,
374 &task->thread_data,
375 stride,
376 depth_stride,
377 sample_stride,
378 depth_sample_stride);
379 END_JIT_CALL();
380 }
381 }
382 }
383
384
385 /**
386 * Run the shader on all blocks in a tile. This is used when a tile is
387 * completely contained inside a triangle, and the shader is opaque.
388 * This is a bin command called during bin processing.
389 */
390 static void
lp_rast_shade_tile_opaque(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)391 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
392 const union lp_rast_cmd_arg arg)
393 {
394 LP_DBG(DEBUG_RAST, "%s\n", __func__);
395
396 assert(task->state);
397 if (!task->state) {
398 return;
399 }
400
401 lp_rast_shade_tile(task, arg);
402 }
403
404
405 /**
406 * Compute shading for a 4x4 block of pixels inside a triangle.
407 * This is a bin command called during bin processing.
408 * \param x X position of quad in window coords
409 * \param y Y position of quad in window coords
410 */
411 void
lp_rast_shade_quads_mask_sample(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned x,unsigned y,uint64_t mask)412 lp_rast_shade_quads_mask_sample(struct lp_rasterizer_task *task,
413 const struct lp_rast_shader_inputs *inputs,
414 unsigned x, unsigned y,
415 uint64_t mask)
416 {
417 const struct lp_rast_state *state = task->state;
418 const struct lp_fragment_shader_variant *variant = state->variant;
419 const struct lp_scene *scene = task->scene;
420
421 assert(state);
422
423 /* Sanity checks */
424 assert(x < scene->tiles_x * TILE_SIZE);
425 assert(y < scene->tiles_y * TILE_SIZE);
426 assert(x % TILE_VECTOR_WIDTH == 0);
427 assert(y % TILE_VECTOR_HEIGHT == 0);
428
429 assert((x % 4) == 0);
430 assert((y % 4) == 0);
431
432 /* color buffer */
433 uint8_t *color[PIPE_MAX_COLOR_BUFS];
434 unsigned stride[PIPE_MAX_COLOR_BUFS];
435 unsigned sample_stride[PIPE_MAX_COLOR_BUFS];
436 unsigned view_index = inputs->view_index;
437 for (unsigned i = 0; i < scene->fb.nr_cbufs; i++) {
438 if (scene->fb.cbufs[i]) {
439 stride[i] = scene->cbufs[i].stride;
440 sample_stride[i] = scene->cbufs[i].sample_stride;
441 color[i] = lp_rast_get_color_block_pointer(task, i, x, y,
442 inputs->layer, view_index);
443 } else {
444 stride[i] = 0;
445 sample_stride[i] = 0;
446 color[i] = NULL;
447 }
448 }
449
450 /* depth buffer */
451 uint8_t *depth = NULL;
452 unsigned depth_stride = 0;
453 unsigned depth_sample_stride = 0;
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, view_index);
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 task->thread_data.raster_state.view_index = inputs->view_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 &state->jit_resources,
475 x, y,
476 inputs->frontfacing,
477 GET_A0(inputs),
478 GET_DADX(inputs),
479 GET_DADY(inputs),
480 color,
481 depth,
482 mask,
483 &task->thread_data,
484 stride,
485 depth_stride,
486 sample_stride,
487 depth_sample_stride);
488 END_JIT_CALL();
489 }
490 }
491
492
493 void
lp_rast_shade_quads_mask(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned x,unsigned y,unsigned mask)494 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
495 const struct lp_rast_shader_inputs *inputs,
496 unsigned x, unsigned y,
497 unsigned mask)
498 {
499 uint64_t new_mask = 0;
500 for (unsigned i = 0; i < task->scene->fb_max_samples; i++)
501 new_mask |= ((uint64_t)mask) << (16 * i);
502 lp_rast_shade_quads_mask_sample(task, inputs, x, y, new_mask);
503 }
504
505
506 /**
507 * Directly copy pixels from a texture to the destination color buffer.
508 * This is a bin command called during bin processing.
509 */
510 static void
lp_rast_blit_tile_to_dest(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)511 lp_rast_blit_tile_to_dest(struct lp_rasterizer_task *task,
512 const union lp_rast_cmd_arg arg)
513 {
514 const struct lp_scene *scene = task->scene;
515 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
516 const struct lp_rast_state *state = task->state;
517 struct lp_fragment_shader_variant *variant = state->variant;
518 const struct lp_jit_texture *texture = &state->jit_resources.textures[0];
519 struct pipe_surface *cbuf = scene->fb.cbufs[0];
520 const unsigned face_slice = cbuf->u.tex.first_layer;
521 const unsigned level = cbuf->u.tex.level;
522 struct llvmpipe_resource *lpt = llvmpipe_resource(cbuf->texture);
523
524 LP_DBG(DEBUG_RAST, "%s\n", __func__);
525
526 if (inputs->disable) {
527 /* This command was partially binned and has been disabled */
528 return;
529 }
530
531 uint8_t *dst = llvmpipe_get_texture_image_address(lpt, face_slice, level);
532 if (!dst)
533 return;
534
535 const unsigned dst_stride = lpt->row_stride[level];
536
537 const uint8_t *src = texture->base;
538 const unsigned src_stride = texture->row_stride[0];
539
540 int src_x = util_iround(GET_A0(inputs)[1][0]*texture->width - 0.5f);
541 int src_y = util_iround(GET_A0(inputs)[1][1]*texture->height - 0.5f);
542
543 src_x += task->x;
544 src_y += task->y;
545
546 if (0) {
547 union util_color uc;
548 uc.ui[0] = 0xff0000ff;
549 util_fill_rect(dst,
550 cbuf->format,
551 dst_stride,
552 task->x,
553 task->y,
554 task->width,
555 task->height,
556 &uc);
557 return;
558 }
559
560 if (src_x >= 0 &&
561 src_y >= 0 &&
562 src_x + task->width <= texture->width &&
563 src_y + task->height <= texture->height) {
564
565 if (variant->shader->kind == LP_FS_KIND_BLIT_RGBA ||
566 (variant->shader->kind == LP_FS_KIND_BLIT_RGB1 &&
567 cbuf->format == PIPE_FORMAT_B8G8R8X8_UNORM)) {
568 util_copy_rect(dst,
569 cbuf->format,
570 dst_stride,
571 task->x, task->y,
572 task->width, task->height,
573 src, src_stride,
574 src_x, src_y);
575 return;
576 }
577
578 if (variant->shader->kind == LP_FS_KIND_BLIT_RGB1) {
579 if (cbuf->format == PIPE_FORMAT_B8G8R8A8_UNORM) {
580 dst += task->x * 4;
581 src += src_x * 4;
582 dst += task->y * dst_stride;
583 src += src_y * src_stride;
584
585 for (int y = 0; y < task->height; ++y) {
586 const uint32_t *src_row = (const uint32_t *)src;
587 uint32_t *dst_row = (uint32_t *)dst;
588
589 for (int x = 0; x < task->width; ++x) {
590 *dst_row++ = *src_row++ | 0xff000000;
591 }
592 dst += dst_stride;
593 src += src_stride;
594 }
595
596 return;
597 }
598 }
599
600 }
601
602 /*
603 * Fall back to the jit shaders.
604 */
605
606 lp_rast_shade_tile_opaque(task, arg);
607 }
608
609
610 static void
lp_rast_blit_tile(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)611 lp_rast_blit_tile(struct lp_rasterizer_task *task,
612 const union lp_rast_cmd_arg arg)
613 {
614 /* This kindof just works, but isn't efficient:
615 */
616 lp_rast_blit_tile_to_dest(task, arg);
617 }
618
619
620 /**
621 * Begin a new occlusion query.
622 * This is a bin command put in all bins.
623 * Called per thread.
624 */
625 static void
lp_rast_begin_query(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)626 lp_rast_begin_query(struct lp_rasterizer_task *task,
627 const union lp_rast_cmd_arg arg)
628 {
629 struct llvmpipe_query *pq = arg.query_obj;
630
631 switch (pq->type) {
632 case PIPE_QUERY_OCCLUSION_COUNTER:
633 case PIPE_QUERY_OCCLUSION_PREDICATE:
634 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
635 pq->start[task->thread_index] = task->thread_data.vis_counter;
636 break;
637 case PIPE_QUERY_PIPELINE_STATISTICS:
638 pq->start[task->thread_index] = task->thread_data.ps_invocations;
639 break;
640 case PIPE_QUERY_TIME_ELAPSED:
641 pq->start[task->thread_index] = os_time_get_nano();
642 break;
643 default:
644 assert(0);
645 break;
646 }
647 }
648
649
650 /**
651 * End the current occlusion query.
652 * This is a bin command put in all bins.
653 * Called per thread.
654 */
655 static void
lp_rast_end_query(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)656 lp_rast_end_query(struct lp_rasterizer_task *task,
657 const union lp_rast_cmd_arg arg)
658 {
659 struct llvmpipe_query *pq = arg.query_obj;
660
661 switch (pq->type) {
662 case PIPE_QUERY_OCCLUSION_COUNTER:
663 case PIPE_QUERY_OCCLUSION_PREDICATE:
664 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
665 pq->end[task->thread_index] +=
666 task->thread_data.vis_counter - pq->start[task->thread_index];
667 pq->start[task->thread_index] = 0;
668 break;
669 case PIPE_QUERY_TIMESTAMP:
670 case PIPE_QUERY_TIME_ELAPSED:
671 pq->end[task->thread_index] = os_time_get_nano();
672 break;
673 case PIPE_QUERY_PIPELINE_STATISTICS:
674 pq->end[task->thread_index] +=
675 task->thread_data.ps_invocations - pq->start[task->thread_index];
676 pq->start[task->thread_index] = 0;
677 break;
678 default:
679 assert(0);
680 break;
681 }
682 }
683
684
685 void
lp_rast_set_state(struct lp_rasterizer_task * task,const union lp_rast_cmd_arg arg)686 lp_rast_set_state(struct lp_rasterizer_task *task,
687 const union lp_rast_cmd_arg arg)
688 {
689 task->state = arg.set_state;
690 }
691
692
693 /**
694 * Called when we're done writing to a color tile.
695 */
696 static void
lp_rast_tile_end(struct lp_rasterizer_task * task)697 lp_rast_tile_end(struct lp_rasterizer_task *task)
698 {
699
700 for (unsigned i = 0; i < task->scene->num_active_queries; ++i) {
701 lp_rast_end_query(task,
702 lp_rast_arg_query(task->scene->active_queries[i]));
703 }
704
705 /* debug */
706 memset(task->color_tiles, 0, sizeof(task->color_tiles));
707 task->depth_tile = NULL;
708 task->bin = NULL;
709 }
710
711
712 /* Currently have two rendering paths only - the general case triangle
713 * path and the super-specialized blit/clear path.
714 */
715 #define TRI ((LP_RAST_FLAGS_TRI <<1)-1) /* general case */
716 #define RECT ((LP_RAST_FLAGS_RECT<<1)-1) /* direct rectangle rasterizer */
717 #define BLIT ((LP_RAST_FLAGS_BLIT<<1)-1) /* write direct-to-dest */
718
719 static const unsigned
720 rast_flags[] = {
721 BLIT, /* clear color */
722 TRI, /* clear zstencil */
723 TRI, /* triangle_1 */
724 TRI, /* triangle_2 */
725 TRI, /* triangle_3 */
726 TRI, /* triangle_4 */
727 TRI, /* triangle_5 */
728 TRI, /* triangle_6 */
729 TRI, /* triangle_7 */
730 TRI, /* triangle_8 */
731 TRI, /* triangle_3_4 */
732 TRI, /* triangle_3_16 */
733 TRI, /* triangle_4_16 */
734 RECT, /* shade_tile */
735 RECT, /* shade_tile_opaque */
736 TRI, /* begin_query */
737 TRI, /* end_query */
738 BLIT, /* set_state, */
739 TRI, /* lp_rast_triangle_32_1 */
740 TRI, /* lp_rast_triangle_32_2 */
741 TRI, /* lp_rast_triangle_32_3 */
742 TRI, /* lp_rast_triangle_32_4 */
743 TRI, /* lp_rast_triangle_32_5 */
744 TRI, /* lp_rast_triangle_32_6 */
745 TRI, /* lp_rast_triangle_32_7 */
746 TRI, /* lp_rast_triangle_32_8 */
747 TRI, /* lp_rast_triangle_32_3_4 */
748 TRI, /* lp_rast_triangle_32_3_16 */
749 TRI, /* lp_rast_triangle_32_4_16 */
750 TRI, /* lp_rast_triangle_ms_1 */
751 TRI, /* lp_rast_triangle_ms_2 */
752 TRI, /* lp_rast_triangle_ms_3 */
753 TRI, /* lp_rast_triangle_ms_4 */
754 TRI, /* lp_rast_triangle_ms_5 */
755 TRI, /* lp_rast_triangle_ms_6 */
756 TRI, /* lp_rast_triangle_ms_7 */
757 TRI, /* lp_rast_triangle_ms_8 */
758 TRI, /* lp_rast_triangle_ms_3_4 */
759 TRI, /* lp_rast_triangle_ms_3_16 */
760 TRI, /* lp_rast_triangle_ms_4_16 */
761 RECT, /* rectangle */
762 BLIT, /* blit */
763 };
764
765 /*
766 */
767 static const lp_rast_cmd_func
768 dispatch_blit[] = {
769 lp_rast_clear_color,
770 NULL, /* clear_zstencil */
771 NULL, /* triangle_1 */
772 NULL, /* triangle_2 */
773 NULL, /* triangle_3 */
774 NULL, /* triangle_4 */
775 NULL, /* triangle_5 */
776 NULL, /* triangle_6 */
777 NULL, /* triangle_7 */
778 NULL, /* triangle_8 */
779 NULL, /* triangle_3_4 */
780 NULL, /* triangle_3_16 */
781 NULL, /* triangle_4_16 */
782 NULL, /* shade_tile */
783 NULL, /* shade_tile_opaque */
784 NULL, /* begin_query */
785 NULL, /* end_query */
786 lp_rast_set_state, /* set_state */
787 NULL, /* lp_rast_triangle_32_1 */
788 NULL, /* lp_rast_triangle_32_2 */
789 NULL, /* lp_rast_triangle_32_3 */
790 NULL, /* lp_rast_triangle_32_4 */
791 NULL, /* lp_rast_triangle_32_5 */
792 NULL, /* lp_rast_triangle_32_6 */
793 NULL, /* lp_rast_triangle_32_7 */
794 NULL, /* lp_rast_triangle_32_8 */
795 NULL, /* lp_rast_triangle_32_3_4 */
796 NULL, /* lp_rast_triangle_32_3_16 */
797 NULL, /* lp_rast_triangle_32_4_16 */
798 NULL, /* lp_rast_triangle_ms_1 */
799 NULL, /* lp_rast_triangle_ms_2 */
800 NULL, /* lp_rast_triangle_ms_3 */
801 NULL, /* lp_rast_triangle_ms_4 */
802 NULL, /* lp_rast_triangle_ms_5 */
803 NULL, /* lp_rast_triangle_ms_6 */
804 NULL, /* lp_rast_triangle_ms_7 */
805 NULL, /* lp_rast_triangle_ms_8 */
806 NULL, /* lp_rast_triangle_ms_3_4 */
807 NULL, /* lp_rast_triangle_ms_3_16 */
808 NULL, /* lp_rast_triangle_ms_4_16 */
809 NULL, /* rectangle */
810 lp_rast_blit_tile_to_dest,
811 };
812
813
814
815 /* Triangle and general case rasterization: Use the SOA llvm shaders,
816 * an active swizzled tile for each color buf, etc. Don't blit/clear
817 * directly to destination surface as we know there are swizzled
818 * operations coming.
819 */
820 static const lp_rast_cmd_func
821 dispatch_tri[] = {
822 lp_rast_clear_color,
823 lp_rast_clear_zstencil,
824 lp_rast_triangle_1,
825 lp_rast_triangle_2,
826 lp_rast_triangle_3,
827 lp_rast_triangle_4,
828 lp_rast_triangle_5,
829 lp_rast_triangle_6,
830 lp_rast_triangle_7,
831 lp_rast_triangle_8,
832 lp_rast_triangle_3_4,
833 lp_rast_triangle_3_16,
834 lp_rast_triangle_4_16,
835 lp_rast_shade_tile,
836 lp_rast_shade_tile_opaque,
837 lp_rast_begin_query,
838 lp_rast_end_query,
839 lp_rast_set_state,
840 lp_rast_triangle_32_1,
841 lp_rast_triangle_32_2,
842 lp_rast_triangle_32_3,
843 lp_rast_triangle_32_4,
844 lp_rast_triangle_32_5,
845 lp_rast_triangle_32_6,
846 lp_rast_triangle_32_7,
847 lp_rast_triangle_32_8,
848 lp_rast_triangle_32_3_4,
849 lp_rast_triangle_32_3_16,
850 lp_rast_triangle_32_4_16,
851 lp_rast_triangle_ms_1,
852 lp_rast_triangle_ms_2,
853 lp_rast_triangle_ms_3,
854 lp_rast_triangle_ms_4,
855 lp_rast_triangle_ms_5,
856 lp_rast_triangle_ms_6,
857 lp_rast_triangle_ms_7,
858 lp_rast_triangle_ms_8,
859 lp_rast_triangle_ms_3_4,
860 lp_rast_triangle_ms_3_16,
861 lp_rast_triangle_ms_4_16,
862 lp_rast_rectangle,
863 lp_rast_blit_tile,
864 };
865
866
867 /* Debug rasterization with most fastpaths disabled.
868 */
869 static const lp_rast_cmd_func
870 dispatch_tri_debug[] =
871 {
872 lp_rast_clear_color,
873 lp_rast_clear_zstencil,
874 lp_rast_triangle_1,
875 lp_rast_triangle_2,
876 lp_rast_triangle_3,
877 lp_rast_triangle_4,
878 lp_rast_triangle_5,
879 lp_rast_triangle_6,
880 lp_rast_triangle_7,
881 lp_rast_triangle_8,
882 lp_rast_triangle_3_4,
883 lp_rast_triangle_3_16,
884 lp_rast_triangle_4_16,
885 lp_rast_shade_tile,
886 lp_rast_shade_tile,
887 lp_rast_begin_query,
888 lp_rast_end_query,
889 lp_rast_set_state,
890 lp_rast_triangle_32_1,
891 lp_rast_triangle_32_2,
892 lp_rast_triangle_32_3,
893 lp_rast_triangle_32_4,
894 lp_rast_triangle_32_5,
895 lp_rast_triangle_32_6,
896 lp_rast_triangle_32_7,
897 lp_rast_triangle_32_8,
898 lp_rast_triangle_32_3_4,
899 lp_rast_triangle_32_3_16,
900 lp_rast_triangle_32_4_16,
901 lp_rast_triangle_ms_1,
902 lp_rast_triangle_ms_2,
903 lp_rast_triangle_ms_3,
904 lp_rast_triangle_ms_4,
905 lp_rast_triangle_ms_5,
906 lp_rast_triangle_ms_6,
907 lp_rast_triangle_ms_7,
908 lp_rast_triangle_ms_8,
909 lp_rast_triangle_ms_3_4,
910 lp_rast_triangle_ms_3_16,
911 lp_rast_triangle_ms_4_16,
912 lp_rast_rectangle,
913 lp_rast_shade_tile,
914 };
915
916
917 struct lp_bin_info
lp_characterize_bin(const struct cmd_bin * bin)918 lp_characterize_bin(const struct cmd_bin *bin)
919 {
920 unsigned andflags = ~0, j = 0;
921
922 STATIC_ASSERT(ARRAY_SIZE(rast_flags) == LP_RAST_OP_MAX);
923
924 for (const struct cmd_block *block = bin->head; block; block = block->next) {
925 for (unsigned k = 0; k < block->count; k++, j++) {
926 andflags &= rast_flags[block->cmd[k]];
927 }
928 }
929
930 struct lp_bin_info info;
931 info.type = andflags;
932 info.count = j;
933
934 return info;
935 }
936
937
938 static void
blit_rasterize_bin(struct lp_rasterizer_task * task,const struct cmd_bin * bin)939 blit_rasterize_bin(struct lp_rasterizer_task *task,
940 const struct cmd_bin *bin)
941 {
942 STATIC_ASSERT(ARRAY_SIZE(dispatch_blit) == LP_RAST_OP_MAX);
943
944 if (0) debug_printf("%s\n", __func__);
945 for (const struct cmd_block *block = bin->head; block; block = block->next) {
946 for (unsigned k = 0; k < block->count; k++) {
947 dispatch_blit[block->cmd[k]](task, block->arg[k]);
948 }
949 }
950 }
951
952
953 static void
tri_rasterize_bin(struct lp_rasterizer_task * task,const struct cmd_bin * bin,int x,int y)954 tri_rasterize_bin(struct lp_rasterizer_task *task,
955 const struct cmd_bin *bin,
956 int x, int y)
957 {
958 STATIC_ASSERT(ARRAY_SIZE(dispatch_tri) == LP_RAST_OP_MAX);
959
960 for (const struct cmd_block *block = bin->head; block; block = block->next) {
961 for (unsigned k = 0; k < block->count; k++) {
962 dispatch_tri[block->cmd[k]](task, block->arg[k]);
963 }
964 }
965 }
966
967
968 static void
debug_rasterize_bin(struct lp_rasterizer_task * task,const struct cmd_bin * bin)969 debug_rasterize_bin(struct lp_rasterizer_task *task,
970 const struct cmd_bin *bin)
971 {
972 STATIC_ASSERT(ARRAY_SIZE(dispatch_tri_debug) == LP_RAST_OP_MAX);
973
974 for (const struct cmd_block *block = bin->head; block; block = block->next) {
975 for (unsigned k = 0; k < block->count; k++) {
976 dispatch_tri_debug[block->cmd[k]](task, block->arg[k]);
977 }
978 }
979 }
980
981
982 /**
983 * Rasterize commands for a single bin.
984 * \param x, y position of the bin's tile in the framebuffer
985 * Must be called between lp_rast_begin() and lp_rast_end().
986 * Called per thread.
987 */
988 static void
rasterize_bin(struct lp_rasterizer_task * task,const struct cmd_bin * bin,int x,int y)989 rasterize_bin(struct lp_rasterizer_task *task,
990 const struct cmd_bin *bin, int x, int y)
991 {
992 struct lp_bin_info info = lp_characterize_bin(bin);
993
994 lp_rast_tile_begin(task, bin, x, y);
995
996 if (LP_DEBUG & DEBUG_NO_FASTPATH) {
997 debug_rasterize_bin(task, bin);
998 } else if (info.type & LP_RAST_FLAGS_BLIT) {
999 blit_rasterize_bin(task, bin);
1000 } else if (task->scene->permit_linear_rasterizer &&
1001 !(LP_PERF & PERF_NO_RAST_LINEAR) &&
1002 (info.type & LP_RAST_FLAGS_RECT)) {
1003 lp_linear_rasterize_bin(task, bin);
1004 } else {
1005 tri_rasterize_bin(task, bin, x, y);
1006 }
1007
1008 lp_rast_tile_end(task);
1009
1010 #if MESA_DEBUG
1011 /* Debug/Perf flags:
1012 */
1013 if (bin->head->count == 1) {
1014 if (bin->head->cmd[0] == LP_RAST_OP_BLIT)
1015 LP_COUNT(nr_pure_blit_64);
1016 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
1017 LP_COUNT(nr_pure_shade_opaque_64);
1018 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
1019 LP_COUNT(nr_pure_shade_64);
1020 }
1021 #endif
1022 }
1023
1024
1025 /* An empty bin is one that just loads the contents of the tile and
1026 * stores them again unchanged. This typically happens when bins have
1027 * been flushed for some reason in the middle of a frame, or when
1028 * incremental updates are being made to a render target.
1029 *
1030 * Try to avoid doing pointless work in this case.
1031 */
1032 static bool
is_empty_bin(const struct cmd_bin * bin)1033 is_empty_bin(const struct cmd_bin *bin)
1034 {
1035 return bin->head == NULL;
1036 }
1037
1038
1039 /**
1040 * Rasterize/execute all bins within a scene.
1041 * Called per thread.
1042 */
1043 static void
rasterize_scene(struct lp_rasterizer_task * task,struct lp_scene * scene)1044 rasterize_scene(struct lp_rasterizer_task *task,
1045 struct lp_scene *scene)
1046 {
1047 task->scene = scene;
1048
1049 /* Clear the cache tags. This should not always be necessary but
1050 * simpler for now.
1051 */
1052 #if LP_USE_TEXTURE_CACHE
1053 memset(task->thread_data.cache->cache_tags, 0,
1054 sizeof(task->thread_data.cache->cache_tags));
1055 #if LP_BUILD_FORMAT_CACHE_DEBUG
1056 task->thread_data.cache->cache_access_total = 0;
1057 task->thread_data.cache->cache_access_miss = 0;
1058 #endif
1059 #endif
1060
1061 if (!task->rast->no_rast) {
1062 /* loop over scene bins, rasterize each */
1063 struct cmd_bin *bin;
1064 int i, j;
1065
1066 assert(scene);
1067 while ((bin = lp_scene_bin_iter_next(scene, &i, &j))) {
1068 if (!is_empty_bin(bin))
1069 rasterize_bin(task, bin, i, j);
1070 }
1071 }
1072
1073 #if LP_BUILD_FORMAT_CACHE_DEBUG
1074 {
1075 uint64_t total, miss;
1076 total = task->thread_data.cache->cache_access_total;
1077 miss = task->thread_data.cache->cache_access_miss;
1078 if (total) {
1079 debug_printf("thread %d cache access %llu miss %llu hit rate %f\n",
1080 task->thread_index, (long long unsigned)total,
1081 (long long unsigned)miss,
1082 (float)(total - miss)/(float)total);
1083 }
1084 }
1085 #endif
1086
1087 if (scene->fence) {
1088 lp_fence_signal(scene->fence);
1089 }
1090
1091 task->scene = NULL;
1092 }
1093
1094
1095 /**
1096 * Called by setup module when it has something for us to render.
1097 */
1098 void
lp_rast_queue_scene(struct lp_rasterizer * rast,struct lp_scene * scene)1099 lp_rast_queue_scene(struct lp_rasterizer *rast,
1100 struct lp_scene *scene)
1101 {
1102 LP_DBG(DEBUG_SETUP, "%s\n", __func__);
1103
1104 lp_fence_reference(&rast->last_fence, scene->fence);
1105 if (rast->last_fence)
1106 rast->last_fence->issued = true;
1107
1108 if (rast->num_threads == 0) {
1109 /* no threading */
1110 unsigned fpstate = util_fpstate_get();
1111
1112 /* Make sure that denorms are treated like zeros. This is
1113 * the behavior required by D3D10. OpenGL doesn't care.
1114 */
1115 util_fpstate_set_denorms_to_zero(fpstate);
1116
1117 lp_rast_begin(rast, scene);
1118
1119 rasterize_scene(&rast->tasks[0], scene);
1120
1121 lp_rast_end(rast);
1122
1123 util_fpstate_set(fpstate);
1124
1125 rast->curr_scene = NULL;
1126 } else {
1127 /* threaded rendering! */
1128 lp_scene_enqueue(rast->full_scenes, scene);
1129
1130 /* signal the threads that there's work to do */
1131 for (unsigned i = 0; i < rast->num_threads; i++) {
1132 util_semaphore_signal(&rast->tasks[i].work_ready);
1133 }
1134 }
1135
1136 LP_DBG(DEBUG_SETUP, "%s done \n", __func__);
1137 }
1138
1139
1140 void
lp_rast_finish(struct lp_rasterizer * rast)1141 lp_rast_finish(struct lp_rasterizer *rast)
1142 {
1143 if (rast->num_threads == 0) {
1144 /* nothing to do */
1145 } else {
1146 /* wait for work to complete */
1147 for (unsigned i = 0; i < rast->num_threads; i++) {
1148 util_semaphore_wait(&rast->tasks[i].work_done);
1149 }
1150 }
1151 }
1152
1153
1154 /**
1155 * This is the thread's main entrypoint.
1156 * It's a simple loop:
1157 * 1. wait for work
1158 * 2. do work
1159 * 3. signal that we're done
1160 */
1161 static int
thread_function(void * init_data)1162 thread_function(void *init_data)
1163 {
1164 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
1165 struct lp_rasterizer *rast = task->rast;
1166 bool debug = false;
1167 char thread_name[16];
1168
1169 snprintf(thread_name, sizeof thread_name, "llvmpipe-%u", task->thread_index);
1170 u_thread_setname(thread_name);
1171
1172 /* Make sure that denorms are treated like zeros. This is
1173 * the behavior required by D3D10. OpenGL doesn't care.
1174 */
1175 unsigned fpstate = util_fpstate_get();
1176 util_fpstate_set_denorms_to_zero(fpstate);
1177
1178 while (1) {
1179 /* wait for work */
1180 if (debug)
1181 debug_printf("thread %d waiting for work\n", task->thread_index);
1182 util_semaphore_wait(&task->work_ready);
1183
1184 if (rast->exit_flag)
1185 break;
1186
1187 if (task->thread_index == 0) {
1188 /* thread[0]:
1189 * - get next scene to rasterize
1190 * - map the framebuffer surfaces
1191 */
1192 lp_rast_begin(rast, lp_scene_dequeue(rast->full_scenes, true));
1193 }
1194
1195 /* Wait for all threads to get here so that threads[1+] don't
1196 * get a null rast->curr_scene pointer.
1197 */
1198 util_barrier_wait(&rast->barrier);
1199
1200 /* do work */
1201 if (debug)
1202 debug_printf("thread %d doing work\n", task->thread_index);
1203
1204 rasterize_scene(task, rast->curr_scene);
1205
1206 /* wait for all threads to finish with this scene */
1207 util_barrier_wait(&rast->barrier);
1208
1209 /* XXX: shouldn't be necessary:
1210 */
1211 if (task->thread_index == 0) {
1212 lp_rast_end(rast);
1213 }
1214
1215 /* signal done with work */
1216 if (debug)
1217 debug_printf("thread %d done working\n", task->thread_index);
1218
1219 util_semaphore_signal(&task->work_done);
1220 }
1221
1222 #ifdef _WIN32
1223 util_semaphore_signal(&task->exited);
1224 #endif
1225
1226 return 0;
1227 }
1228
1229
1230 /**
1231 * Initialize semaphores and spawn the threads.
1232 */
1233 static void
create_rast_threads(struct lp_rasterizer * rast)1234 create_rast_threads(struct lp_rasterizer *rast)
1235 {
1236 /* NOTE: if num_threads is zero, we won't use any threads */
1237 for (unsigned i = 0; i < rast->num_threads; i++) {
1238 util_semaphore_init(&rast->tasks[i].work_ready, 0);
1239 util_semaphore_init(&rast->tasks[i].work_done, 0);
1240 #ifdef _WIN32
1241 util_semaphore_init(&rast->tasks[i].exited, 0);
1242 #endif
1243 if (thrd_success != u_thread_create(rast->threads + i, thread_function,
1244 (void *) &rast->tasks[i])) {
1245 rast->num_threads = i; /* previous thread is max */
1246 break;
1247 }
1248 }
1249 }
1250
1251
1252 /**
1253 * Create new lp_rasterizer. If num_threads is zero, don't create any
1254 * new threads, do rendering synchronously.
1255 * \param num_threads number of rasterizer threads to create
1256 */
1257 struct lp_rasterizer *
lp_rast_create(unsigned num_threads)1258 lp_rast_create(unsigned num_threads)
1259 {
1260 struct lp_rasterizer *rast = CALLOC_STRUCT(lp_rasterizer);
1261 if (!rast) {
1262 goto no_rast;
1263 }
1264
1265 rast->full_scenes = lp_scene_queue_create();
1266 if (!rast->full_scenes) {
1267 goto no_full_scenes;
1268 }
1269
1270 for (unsigned i = 0; i < MAX2(1, num_threads); i++) {
1271 struct lp_rasterizer_task *task = &rast->tasks[i];
1272 task->rast = rast;
1273 task->thread_index = i;
1274 task->thread_data.cache =
1275 align_malloc(sizeof(struct lp_build_format_cache), 16);
1276 if (!task->thread_data.cache) {
1277 goto no_thread_data_cache;
1278 }
1279 }
1280
1281 rast->num_threads = num_threads;
1282
1283 rast->no_rast = debug_get_bool_option("LP_NO_RAST", false);
1284
1285 create_rast_threads(rast);
1286
1287 /* for synchronizing rasterization threads */
1288 if (rast->num_threads > 0) {
1289 util_barrier_init(&rast->barrier, rast->num_threads);
1290 }
1291
1292 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
1293
1294 return rast;
1295
1296 no_thread_data_cache:
1297 for (unsigned i = 0; i < MAX2(1, rast->num_threads); i++) {
1298 if (rast->tasks[i].thread_data.cache) {
1299 align_free(rast->tasks[i].thread_data.cache);
1300 }
1301 }
1302
1303 lp_scene_queue_destroy(rast->full_scenes);
1304 no_full_scenes:
1305 FREE(rast);
1306 no_rast:
1307 return NULL;
1308 }
1309
1310
1311 /* Shutdown:
1312 */
1313 void
lp_rast_destroy(struct lp_rasterizer * rast)1314 lp_rast_destroy(struct lp_rasterizer *rast)
1315 {
1316 /* Set exit_flag and signal each thread's work_ready semaphore.
1317 * Each thread will be woken up, notice that the exit_flag is set and
1318 * break out of its main loop. The thread will then exit.
1319 */
1320 rast->exit_flag = true;
1321 for (unsigned i = 0; i < rast->num_threads; i++) {
1322 util_semaphore_signal(&rast->tasks[i].work_ready);
1323 }
1324
1325 /* Wait for threads to terminate before cleaning up per-thread data.
1326 * We don't actually call pipe_thread_wait to avoid dead lock on Windows
1327 * per https://bugs.freedesktop.org/show_bug.cgi?id=76252 */
1328 for (unsigned i = 0; i < rast->num_threads; i++) {
1329 #ifdef _WIN32
1330 /* Threads might already be dead - Windows apparently terminates
1331 * other threads when returning from main.
1332 */
1333 DWORD exit_code = STILL_ACTIVE;
1334 if (GetExitCodeThread(rast->threads[i].handle, &exit_code) &&
1335 exit_code == STILL_ACTIVE) {
1336 util_semaphore_wait(&rast->tasks[i].exited);
1337 }
1338 #else
1339 thrd_join(rast->threads[i], NULL);
1340 #endif
1341 }
1342
1343 /* Clean up per-thread data */
1344 for (unsigned i = 0; i < rast->num_threads; i++) {
1345 util_semaphore_destroy(&rast->tasks[i].work_ready);
1346 util_semaphore_destroy(&rast->tasks[i].work_done);
1347 #ifdef _WIN32
1348 util_semaphore_destroy(&rast->tasks[i].exited);
1349 #endif
1350 }
1351 for (unsigned i = 0; i < MAX2(1, rast->num_threads); i++) {
1352 align_free(rast->tasks[i].thread_data.cache);
1353 }
1354
1355 lp_fence_reference(&rast->last_fence, NULL);
1356
1357 /* for synchronizing rasterization threads */
1358 if (rast->num_threads > 0) {
1359 util_barrier_destroy(&rast->barrier);
1360 }
1361
1362 lp_scene_queue_destroy(rast->full_scenes);
1363
1364 FREE(rast);
1365 }
1366
1367
1368 void
lp_rast_fence(struct lp_rasterizer * rast,struct lp_fence ** fence)1369 lp_rast_fence(struct lp_rasterizer *rast,
1370 struct lp_fence **fence)
1371 {
1372 if (fence)
1373 lp_fence_reference((struct lp_fence **)fence, rast->last_fence);
1374 }
1375