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