llvmpipe: fixup multisample coverage masks for covered tiles
[mesa.git] / src / gallium / drivers / llvmpipe / lp_scene.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_framebuffer.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/simple_list.h"
33 #include "util/format/u_format.h"
34 #include "lp_scene.h"
35 #include "lp_fence.h"
36 #include "lp_debug.h"
37
38
39 #define RESOURCE_REF_SZ 32
40
41 /** List of resource references */
42 struct resource_ref {
43 struct pipe_resource *resource[RESOURCE_REF_SZ];
44 int count;
45 struct resource_ref *next;
46 };
47
48
49 /**
50 * Create a new scene object.
51 * \param queue the queue to put newly rendered/emptied scenes into
52 */
53 struct lp_scene *
54 lp_scene_create( struct pipe_context *pipe )
55 {
56 struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
57 if (!scene)
58 return NULL;
59
60 scene->pipe = pipe;
61
62 scene->data.head =
63 CALLOC_STRUCT(data_block);
64
65 (void) mtx_init(&scene->mutex, mtx_plain);
66
67 #ifdef DEBUG
68 /* Do some scene limit sanity checks here */
69 {
70 size_t maxBins = TILES_X * TILES_Y;
71 size_t maxCommandBytes = sizeof(struct cmd_block) * maxBins;
72 size_t maxCommandPlusData = maxCommandBytes + DATA_BLOCK_SIZE;
73 /* We'll need at least one command block per bin. Make sure that's
74 * less than the max allowed scene size.
75 */
76 assert(maxCommandBytes < LP_SCENE_MAX_SIZE);
77 /* We'll also need space for at least one other data block */
78 assert(maxCommandPlusData <= LP_SCENE_MAX_SIZE);
79 }
80 #endif
81
82 return scene;
83 }
84
85
86 /**
87 * Free all data associated with the given scene, and the scene itself.
88 */
89 void
90 lp_scene_destroy(struct lp_scene *scene)
91 {
92 lp_fence_reference(&scene->fence, NULL);
93 mtx_destroy(&scene->mutex);
94 assert(scene->data.head->next == NULL);
95 FREE(scene->data.head);
96 FREE(scene);
97 }
98
99
100 /**
101 * Check if the scene's bins are all empty.
102 * For debugging purposes.
103 */
104 boolean
105 lp_scene_is_empty(struct lp_scene *scene )
106 {
107 unsigned x, y;
108
109 for (y = 0; y < TILES_Y; y++) {
110 for (x = 0; x < TILES_X; x++) {
111 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
112 if (bin->head) {
113 return FALSE;
114 }
115 }
116 }
117 return TRUE;
118 }
119
120
121 /* Returns true if there has ever been a failed allocation attempt in
122 * this scene. Used in triangle emit to avoid having to check success
123 * at each bin.
124 */
125 boolean
126 lp_scene_is_oom(struct lp_scene *scene)
127 {
128 return scene->alloc_failed;
129 }
130
131
132 /* Remove all commands from a bin. Tries to reuse some of the memory
133 * allocated to the bin, however.
134 */
135 void
136 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
137 {
138 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
139
140 bin->last_state = NULL;
141 bin->head = bin->tail;
142 if (bin->tail) {
143 bin->tail->next = NULL;
144 bin->tail->count = 0;
145 }
146 }
147
148
149 void
150 lp_scene_begin_rasterization(struct lp_scene *scene)
151 {
152 const struct pipe_framebuffer_state *fb = &scene->fb;
153 int i;
154
155 //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
156
157 for (i = 0; i < scene->fb.nr_cbufs; i++) {
158 struct pipe_surface *cbuf = scene->fb.cbufs[i];
159
160 if (!cbuf) {
161 scene->cbufs[i].stride = 0;
162 scene->cbufs[i].layer_stride = 0;
163 scene->cbufs[i].sample_stride = 0;
164 scene->cbufs[i].nr_samples = 0;
165 scene->cbufs[i].map = NULL;
166 continue;
167 }
168
169 if (llvmpipe_resource_is_texture(cbuf->texture)) {
170 scene->cbufs[i].stride = llvmpipe_resource_stride(cbuf->texture,
171 cbuf->u.tex.level);
172 scene->cbufs[i].layer_stride = llvmpipe_layer_stride(cbuf->texture,
173 cbuf->u.tex.level);
174 scene->cbufs[i].sample_stride = llvmpipe_sample_stride(cbuf->texture);
175
176 scene->cbufs[i].map = llvmpipe_resource_map(cbuf->texture,
177 cbuf->u.tex.level,
178 cbuf->u.tex.first_layer,
179 LP_TEX_USAGE_READ_WRITE);
180 scene->cbufs[i].format_bytes = util_format_get_blocksize(cbuf->format);
181 scene->cbufs[i].nr_samples = util_res_sample_count(cbuf->texture);
182 }
183 else {
184 struct llvmpipe_resource *lpr = llvmpipe_resource(cbuf->texture);
185 unsigned pixstride = util_format_get_blocksize(cbuf->format);
186 scene->cbufs[i].stride = cbuf->texture->width0;
187 scene->cbufs[i].layer_stride = 0;
188 scene->cbufs[i].sample_stride = 0;
189 scene->cbufs[i].nr_samples = 1;
190 scene->cbufs[i].map = lpr->data;
191 scene->cbufs[i].map += cbuf->u.buf.first_element * pixstride;
192 scene->cbufs[i].format_bytes = util_format_get_blocksize(cbuf->format);
193 }
194 }
195
196 if (fb->zsbuf) {
197 struct pipe_surface *zsbuf = scene->fb.zsbuf;
198 scene->zsbuf.stride = llvmpipe_resource_stride(zsbuf->texture, zsbuf->u.tex.level);
199 scene->zsbuf.layer_stride = llvmpipe_layer_stride(zsbuf->texture, zsbuf->u.tex.level);
200 scene->zsbuf.sample_stride = llvmpipe_sample_stride(zsbuf->texture);
201 scene->zsbuf.nr_samples = util_res_sample_count(zsbuf->texture);
202 scene->zsbuf.map = llvmpipe_resource_map(zsbuf->texture,
203 zsbuf->u.tex.level,
204 zsbuf->u.tex.first_layer,
205 LP_TEX_USAGE_READ_WRITE);
206 scene->zsbuf.format_bytes = util_format_get_blocksize(zsbuf->format);
207 }
208 }
209
210
211
212
213 /**
214 * Free all the temporary data in a scene.
215 */
216 void
217 lp_scene_end_rasterization(struct lp_scene *scene )
218 {
219 int i, j;
220
221 /* Unmap color buffers */
222 for (i = 0; i < scene->fb.nr_cbufs; i++) {
223 if (scene->cbufs[i].map) {
224 struct pipe_surface *cbuf = scene->fb.cbufs[i];
225 if (llvmpipe_resource_is_texture(cbuf->texture)) {
226 llvmpipe_resource_unmap(cbuf->texture,
227 cbuf->u.tex.level,
228 cbuf->u.tex.first_layer);
229 }
230 scene->cbufs[i].map = NULL;
231 }
232 }
233
234 /* Unmap z/stencil buffer */
235 if (scene->zsbuf.map) {
236 struct pipe_surface *zsbuf = scene->fb.zsbuf;
237 llvmpipe_resource_unmap(zsbuf->texture,
238 zsbuf->u.tex.level,
239 zsbuf->u.tex.first_layer);
240 scene->zsbuf.map = NULL;
241 }
242
243 /* Reset all command lists:
244 */
245 for (i = 0; i < scene->tiles_x; i++) {
246 for (j = 0; j < scene->tiles_y; j++) {
247 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
248 bin->head = NULL;
249 bin->tail = NULL;
250 bin->last_state = NULL;
251 }
252 }
253
254 /* If there are any bins which weren't cleared by the loop above,
255 * they will be caught (on debug builds at least) by this assert:
256 */
257 assert(lp_scene_is_empty(scene));
258
259 /* Decrement texture ref counts
260 */
261 {
262 struct resource_ref *ref;
263 int i, j = 0;
264
265 for (ref = scene->resources; ref; ref = ref->next) {
266 for (i = 0; i < ref->count; i++) {
267 if (LP_DEBUG & DEBUG_SETUP)
268 debug_printf("resource %d: %p %dx%d sz %d\n",
269 j,
270 (void *) ref->resource[i],
271 ref->resource[i]->width0,
272 ref->resource[i]->height0,
273 llvmpipe_resource_size(ref->resource[i]));
274 j++;
275 pipe_resource_reference(&ref->resource[i], NULL);
276 }
277 }
278
279 if (LP_DEBUG & DEBUG_SETUP)
280 debug_printf("scene %d resources, sz %d\n",
281 j, scene->resource_reference_size);
282 }
283
284 /* Free all scene data blocks:
285 */
286 {
287 struct data_block_list *list = &scene->data;
288 struct data_block *block, *tmp;
289
290 for (block = list->head->next; block; block = tmp) {
291 tmp = block->next;
292 FREE(block);
293 }
294
295 list->head->next = NULL;
296 list->head->used = 0;
297 }
298
299 lp_fence_reference(&scene->fence, NULL);
300
301 scene->resources = NULL;
302 scene->scene_size = 0;
303 scene->resource_reference_size = 0;
304
305 scene->alloc_failed = FALSE;
306
307 util_unreference_framebuffer_state( &scene->fb );
308 }
309
310
311
312
313
314
315 struct cmd_block *
316 lp_scene_new_cmd_block( struct lp_scene *scene,
317 struct cmd_bin *bin )
318 {
319 struct cmd_block *block = lp_scene_alloc(scene, sizeof(struct cmd_block));
320 if (block) {
321 if (bin->tail) {
322 bin->tail->next = block;
323 bin->tail = block;
324 }
325 else {
326 bin->head = block;
327 bin->tail = block;
328 }
329 //memset(block, 0, sizeof *block);
330 block->next = NULL;
331 block->count = 0;
332 }
333 return block;
334 }
335
336
337 struct data_block *
338 lp_scene_new_data_block( struct lp_scene *scene )
339 {
340 if (scene->scene_size + DATA_BLOCK_SIZE > LP_SCENE_MAX_SIZE) {
341 if (0) debug_printf("%s: failed\n", __FUNCTION__);
342 scene->alloc_failed = TRUE;
343 return NULL;
344 }
345 else {
346 struct data_block *block = MALLOC_STRUCT(data_block);
347 if (!block)
348 return NULL;
349
350 scene->scene_size += sizeof *block;
351
352 block->used = 0;
353 block->next = scene->data.head;
354 scene->data.head = block;
355
356 return block;
357 }
358 }
359
360
361 /**
362 * Return number of bytes used for all bin data within a scene.
363 * This does not include resources (textures) referenced by the scene.
364 */
365 static unsigned
366 lp_scene_data_size( const struct lp_scene *scene )
367 {
368 unsigned size = 0;
369 const struct data_block *block;
370 for (block = scene->data.head; block; block = block->next) {
371 size += block->used;
372 }
373 return size;
374 }
375
376
377
378 /**
379 * Add a reference to a resource by the scene.
380 */
381 boolean
382 lp_scene_add_resource_reference(struct lp_scene *scene,
383 struct pipe_resource *resource,
384 boolean initializing_scene)
385 {
386 struct resource_ref *ref, **last = &scene->resources;
387 int i;
388
389 /* Look at existing resource blocks:
390 */
391 for (ref = scene->resources; ref; ref = ref->next) {
392 last = &ref->next;
393
394 /* Search for this resource:
395 */
396 for (i = 0; i < ref->count; i++)
397 if (ref->resource[i] == resource)
398 return TRUE;
399
400 if (ref->count < RESOURCE_REF_SZ) {
401 /* If the block is half-empty, then append the reference here.
402 */
403 break;
404 }
405 }
406
407 /* Create a new block if no half-empty block was found.
408 */
409 if (!ref) {
410 assert(*last == NULL);
411 *last = lp_scene_alloc(scene, sizeof *ref);
412 if (*last == NULL)
413 return FALSE;
414
415 ref = *last;
416 memset(ref, 0, sizeof *ref);
417 }
418
419 /* Append the reference to the reference block.
420 */
421 pipe_resource_reference(&ref->resource[ref->count++], resource);
422 scene->resource_reference_size += llvmpipe_resource_size(resource);
423
424 /* Heuristic to advise scene flushes. This isn't helpful in the
425 * initial setup of the scene, but after that point flush on the
426 * next resource added which exceeds 64MB in referenced texture
427 * data.
428 */
429 if (!initializing_scene &&
430 scene->resource_reference_size >= LP_SCENE_MAX_RESOURCE_SIZE)
431 return FALSE;
432
433 return TRUE;
434 }
435
436
437 /**
438 * Does this scene have a reference to the given resource?
439 */
440 boolean
441 lp_scene_is_resource_referenced(const struct lp_scene *scene,
442 const struct pipe_resource *resource)
443 {
444 const struct resource_ref *ref;
445 int i;
446
447 for (ref = scene->resources; ref; ref = ref->next) {
448 for (i = 0; i < ref->count; i++)
449 if (ref->resource[i] == resource)
450 return TRUE;
451 }
452
453 return FALSE;
454 }
455
456
457
458
459 /** advance curr_x,y to the next bin */
460 static boolean
461 next_bin(struct lp_scene *scene)
462 {
463 scene->curr_x++;
464 if (scene->curr_x >= scene->tiles_x) {
465 scene->curr_x = 0;
466 scene->curr_y++;
467 }
468 if (scene->curr_y >= scene->tiles_y) {
469 /* no more bins */
470 return FALSE;
471 }
472 return TRUE;
473 }
474
475
476 void
477 lp_scene_bin_iter_begin( struct lp_scene *scene )
478 {
479 scene->curr_x = scene->curr_y = -1;
480 }
481
482
483 /**
484 * Return pointer to next bin to be rendered.
485 * The lp_scene::curr_x and ::curr_y fields will be advanced.
486 * Multiple rendering threads will call this function to get a chunk
487 * of work (a bin) to work on.
488 */
489 struct cmd_bin *
490 lp_scene_bin_iter_next( struct lp_scene *scene , int *x, int *y)
491 {
492 struct cmd_bin *bin = NULL;
493
494 mtx_lock(&scene->mutex);
495
496 if (scene->curr_x < 0) {
497 /* first bin */
498 scene->curr_x = 0;
499 scene->curr_y = 0;
500 }
501 else if (!next_bin(scene)) {
502 /* no more bins left */
503 goto end;
504 }
505
506 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
507 *x = scene->curr_x;
508 *y = scene->curr_y;
509
510 end:
511 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
512 mtx_unlock(&scene->mutex);
513 return bin;
514 }
515
516
517 void lp_scene_begin_binning(struct lp_scene *scene,
518 struct pipe_framebuffer_state *fb)
519 {
520 int i;
521 unsigned max_layer = ~0;
522
523 assert(lp_scene_is_empty(scene));
524
525 util_copy_framebuffer_state(&scene->fb, fb);
526
527 scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
528 scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
529 assert(scene->tiles_x <= TILES_X);
530 assert(scene->tiles_y <= TILES_Y);
531
532 /*
533 * Determine how many layers the fb has (used for clamping layer value).
534 * OpenGL (but not d3d10) permits different amount of layers per rt, however
535 * results are undefined if layer exceeds the amount of layers of ANY
536 * attachment hence don't need separate per cbuf and zsbuf max.
537 */
538 for (i = 0; i < scene->fb.nr_cbufs; i++) {
539 struct pipe_surface *cbuf = scene->fb.cbufs[i];
540 if (cbuf) {
541 if (llvmpipe_resource_is_texture(cbuf->texture)) {
542 max_layer = MIN2(max_layer,
543 cbuf->u.tex.last_layer - cbuf->u.tex.first_layer);
544 }
545 else {
546 max_layer = 0;
547 }
548 }
549 }
550 if (fb->zsbuf) {
551 struct pipe_surface *zsbuf = scene->fb.zsbuf;
552 max_layer = MIN2(max_layer, zsbuf->u.tex.last_layer - zsbuf->u.tex.first_layer);
553 }
554 scene->fb_max_layer = max_layer;
555 scene->fb_max_samples = util_framebuffer_get_num_samples(fb);
556 if (scene->fb_max_samples == 4) {
557 for (unsigned i = 0; i < 4; i++) {
558 scene->fixed_sample_pos[i][0] = util_iround(lp_sample_pos_4x[i][0] * FIXED_ONE);
559 scene->fixed_sample_pos[i][1] = util_iround(lp_sample_pos_4x[i][1] * FIXED_ONE);
560 }
561 }
562 }
563
564
565 void lp_scene_end_binning( struct lp_scene *scene )
566 {
567 if (LP_DEBUG & DEBUG_SCENE) {
568 debug_printf("rasterize scene:\n");
569 debug_printf(" scene_size: %u\n",
570 scene->scene_size);
571 debug_printf(" data size: %u\n",
572 lp_scene_data_size(scene));
573
574 if (0)
575 lp_debug_bins( scene );
576 }
577 }