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