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