llvmpipe: Unswizzled rendering.
[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/u_simple_list.h"
33 #include "util/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 pipe_mutex_init(scene->mutex);
66
67 return scene;
68 }
69
70
71 /**
72 * Free all data associated with the given scene, and the scene itself.
73 */
74 void
75 lp_scene_destroy(struct lp_scene *scene)
76 {
77 lp_fence_reference(&scene->fence, NULL);
78 pipe_mutex_destroy(scene->mutex);
79 assert(scene->data.head->next == NULL);
80 FREE(scene->data.head);
81 FREE(scene);
82 }
83
84
85 /**
86 * Check if the scene's bins are all empty.
87 * For debugging purposes.
88 */
89 boolean
90 lp_scene_is_empty(struct lp_scene *scene )
91 {
92 unsigned x, y;
93
94 for (y = 0; y < TILES_Y; y++) {
95 for (x = 0; x < TILES_X; x++) {
96 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
97 if (bin->head) {
98 return FALSE;
99 }
100 }
101 }
102 return TRUE;
103 }
104
105
106 /* Returns true if there has ever been a failed allocation attempt in
107 * this scene. Used in triangle emit to avoid having to check success
108 * at each bin.
109 */
110 boolean
111 lp_scene_is_oom(struct lp_scene *scene)
112 {
113 return scene->alloc_failed;
114 }
115
116
117 /* Remove all commands from a bin. Tries to reuse some of the memory
118 * allocated to the bin, however.
119 */
120 void
121 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
122 {
123 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
124
125 bin->last_state = NULL;
126 bin->head = bin->tail;
127 if (bin->tail) {
128 bin->tail->next = NULL;
129 bin->tail->count = 0;
130 }
131 }
132
133
134 void
135 lp_scene_begin_rasterization(struct lp_scene *scene)
136 {
137 const struct pipe_framebuffer_state *fb = &scene->fb;
138 int i;
139
140 //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
141
142 for (i = 0; i < scene->fb.nr_cbufs; i++) {
143 struct pipe_surface *cbuf = scene->fb.cbufs[i];
144 assert(cbuf->u.tex.first_layer == cbuf->u.tex.last_layer);
145 scene->cbufs[i].stride = llvmpipe_resource_stride(cbuf->texture,
146 cbuf->u.tex.level);
147
148 scene->cbufs[i].map = llvmpipe_resource_map(cbuf->texture,
149 cbuf->u.tex.level,
150 cbuf->u.tex.first_layer,
151 LP_TEX_USAGE_READ_WRITE,
152 LP_TEX_LAYOUT_LINEAR);
153
154 scene->cbufs[i].unswizzled = llvmpipe_is_format_unswizzled(cbuf->format);
155 }
156
157 if (fb->zsbuf) {
158 struct pipe_surface *zsbuf = scene->fb.zsbuf;
159 assert(zsbuf->u.tex.first_layer == zsbuf->u.tex.last_layer);
160 scene->zsbuf.stride = llvmpipe_resource_stride(zsbuf->texture, zsbuf->u.tex.level);
161 scene->zsbuf.blocksize =
162 util_format_get_blocksize(zsbuf->texture->format);
163
164 scene->zsbuf.map = llvmpipe_resource_map(zsbuf->texture,
165 zsbuf->u.tex.level,
166 zsbuf->u.tex.first_layer,
167 LP_TEX_USAGE_READ_WRITE,
168 LP_TEX_LAYOUT_NONE);
169 }
170 }
171
172
173
174
175 /**
176 * Free all the temporary data in a scene.
177 */
178 void
179 lp_scene_end_rasterization(struct lp_scene *scene )
180 {
181 int i, j;
182
183 /* Unmap color buffers */
184 for (i = 0; i < scene->fb.nr_cbufs; i++) {
185 if (scene->cbufs[i].map) {
186 struct pipe_surface *cbuf = scene->fb.cbufs[i];
187 llvmpipe_resource_unmap(cbuf->texture,
188 cbuf->u.tex.level,
189 cbuf->u.tex.first_layer);
190 scene->cbufs[i].map = NULL;
191 }
192 }
193
194 /* Unmap z/stencil buffer */
195 if (scene->zsbuf.map) {
196 struct pipe_surface *zsbuf = scene->fb.zsbuf;
197 llvmpipe_resource_unmap(zsbuf->texture,
198 zsbuf->u.tex.level,
199 zsbuf->u.tex.first_layer);
200 scene->zsbuf.map = NULL;
201 }
202
203 /* Reset all command lists:
204 */
205 for (i = 0; i < scene->tiles_x; i++) {
206 for (j = 0; j < scene->tiles_y; j++) {
207 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
208 bin->head = NULL;
209 bin->tail = NULL;
210 bin->last_state = NULL;
211 }
212 }
213
214 /* If there are any bins which weren't cleared by the loop above,
215 * they will be caught (on debug builds at least) by this assert:
216 */
217 assert(lp_scene_is_empty(scene));
218
219 /* Decrement texture ref counts
220 */
221 {
222 struct resource_ref *ref;
223 int i, j = 0;
224
225 for (ref = scene->resources; ref; ref = ref->next) {
226 for (i = 0; i < ref->count; i++) {
227 if (LP_DEBUG & DEBUG_SETUP)
228 debug_printf("resource %d: %p %dx%d sz %d\n",
229 j,
230 (void *) ref->resource[i],
231 ref->resource[i]->width0,
232 ref->resource[i]->height0,
233 llvmpipe_resource_size(ref->resource[i]));
234 j++;
235 pipe_resource_reference(&ref->resource[i], NULL);
236 }
237 }
238
239 if (LP_DEBUG & DEBUG_SETUP)
240 debug_printf("scene %d resources, sz %d\n",
241 j, scene->resource_reference_size);
242 }
243
244 /* Free all scene data blocks:
245 */
246 {
247 struct data_block_list *list = &scene->data;
248 struct data_block *block, *tmp;
249
250 for (block = list->head->next; block; block = tmp) {
251 tmp = block->next;
252 FREE(block);
253 }
254
255 list->head->next = NULL;
256 list->head->used = 0;
257 }
258
259 lp_fence_reference(&scene->fence, NULL);
260
261 scene->resources = NULL;
262 scene->scene_size = 0;
263 scene->resource_reference_size = 0;
264
265 scene->has_depthstencil_clear = FALSE;
266 scene->alloc_failed = FALSE;
267
268 util_unreference_framebuffer_state( &scene->fb );
269 }
270
271
272
273
274
275
276 struct cmd_block *
277 lp_scene_new_cmd_block( struct lp_scene *scene,
278 struct cmd_bin *bin )
279 {
280 struct cmd_block *block = lp_scene_alloc(scene, sizeof(struct cmd_block));
281 if (block) {
282 if (bin->tail) {
283 bin->tail->next = block;
284 bin->tail = block;
285 }
286 else {
287 bin->head = block;
288 bin->tail = block;
289 }
290 //memset(block, 0, sizeof *block);
291 block->next = NULL;
292 block->count = 0;
293 }
294 return block;
295 }
296
297
298 struct data_block *
299 lp_scene_new_data_block( struct lp_scene *scene )
300 {
301 if (scene->scene_size + DATA_BLOCK_SIZE > LP_SCENE_MAX_SIZE) {
302 if (0) debug_printf("%s: failed\n", __FUNCTION__);
303 scene->alloc_failed = TRUE;
304 return NULL;
305 }
306 else {
307 struct data_block *block = MALLOC_STRUCT(data_block);
308 if (block == NULL)
309 return NULL;
310
311 scene->scene_size += sizeof *block;
312
313 block->used = 0;
314 block->next = scene->data.head;
315 scene->data.head = block;
316
317 return block;
318 }
319 }
320
321
322 /**
323 * Return number of bytes used for all bin data within a scene.
324 * This does not include resources (textures) referenced by the scene.
325 */
326 static unsigned
327 lp_scene_data_size( const struct lp_scene *scene )
328 {
329 unsigned size = 0;
330 const struct data_block *block;
331 for (block = scene->data.head; block; block = block->next) {
332 size += block->used;
333 }
334 return size;
335 }
336
337
338
339 /**
340 * Add a reference to a resource by the scene.
341 */
342 boolean
343 lp_scene_add_resource_reference(struct lp_scene *scene,
344 struct pipe_resource *resource,
345 boolean initializing_scene)
346 {
347 struct resource_ref *ref, **last = &scene->resources;
348 int i;
349
350 /* Look at existing resource blocks:
351 */
352 for (ref = scene->resources; ref; ref = ref->next) {
353 last = &ref->next;
354
355 /* Search for this resource:
356 */
357 for (i = 0; i < ref->count; i++)
358 if (ref->resource[i] == resource)
359 return TRUE;
360
361 if (ref->count < RESOURCE_REF_SZ) {
362 /* If the block is half-empty, then append the reference here.
363 */
364 break;
365 }
366 }
367
368 /* Create a new block if no half-empty block was found.
369 */
370 if (!ref) {
371 assert(*last == NULL);
372 *last = lp_scene_alloc(scene, sizeof *ref);
373 if (*last == NULL)
374 return FALSE;
375
376 ref = *last;
377 memset(ref, 0, sizeof *ref);
378 }
379
380 /* Append the reference to the reference block.
381 */
382 pipe_resource_reference(&ref->resource[ref->count++], resource);
383 scene->resource_reference_size += llvmpipe_resource_size(resource);
384
385 /* Heuristic to advise scene flushes. This isn't helpful in the
386 * initial setup of the scene, but after that point flush on the
387 * next resource added which exceeds 64MB in referenced texture
388 * data.
389 */
390 if (!initializing_scene &&
391 scene->resource_reference_size >= LP_SCENE_MAX_RESOURCE_SIZE)
392 return FALSE;
393
394 return TRUE;
395 }
396
397
398 /**
399 * Does this scene have a reference to the given resource?
400 */
401 boolean
402 lp_scene_is_resource_referenced(const struct lp_scene *scene,
403 const struct pipe_resource *resource)
404 {
405 const struct resource_ref *ref;
406 int i;
407
408 for (ref = scene->resources; ref; ref = ref->next) {
409 for (i = 0; i < ref->count; i++)
410 if (ref->resource[i] == resource)
411 return TRUE;
412 }
413
414 return FALSE;
415 }
416
417
418
419
420 /** advance curr_x,y to the next bin */
421 static boolean
422 next_bin(struct lp_scene *scene)
423 {
424 scene->curr_x++;
425 if (scene->curr_x >= scene->tiles_x) {
426 scene->curr_x = 0;
427 scene->curr_y++;
428 }
429 if (scene->curr_y >= scene->tiles_y) {
430 /* no more bins */
431 return FALSE;
432 }
433 return TRUE;
434 }
435
436
437 void
438 lp_scene_bin_iter_begin( struct lp_scene *scene )
439 {
440 scene->curr_x = scene->curr_y = -1;
441 }
442
443
444 /**
445 * Return pointer to next bin to be rendered.
446 * The lp_scene::curr_x and ::curr_y fields will be advanced.
447 * Multiple rendering threads will call this function to get a chunk
448 * of work (a bin) to work on.
449 */
450 struct cmd_bin *
451 lp_scene_bin_iter_next( struct lp_scene *scene )
452 {
453 struct cmd_bin *bin = NULL;
454
455 pipe_mutex_lock(scene->mutex);
456
457 if (scene->curr_x < 0) {
458 /* first bin */
459 scene->curr_x = 0;
460 scene->curr_y = 0;
461 }
462 else if (!next_bin(scene)) {
463 /* no more bins left */
464 goto end;
465 }
466
467 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
468
469 end:
470 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
471 pipe_mutex_unlock(scene->mutex);
472 return bin;
473 }
474
475
476 void lp_scene_begin_binning( struct lp_scene *scene,
477 struct pipe_framebuffer_state *fb )
478 {
479 assert(lp_scene_is_empty(scene));
480
481 util_copy_framebuffer_state(&scene->fb, fb);
482
483 scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
484 scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
485
486 assert(scene->tiles_x <= TILES_X);
487 assert(scene->tiles_y <= TILES_Y);
488 }
489
490
491 void lp_scene_end_binning( struct lp_scene *scene )
492 {
493 if (LP_DEBUG & DEBUG_SCENE) {
494 debug_printf("rasterize scene:\n");
495 debug_printf(" scene_size: %u\n",
496 scene->scene_size);
497 debug_printf(" data size: %u\n",
498 lp_scene_data_size(scene));
499
500 if (0)
501 lp_debug_bins( scene );
502 }
503 }