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