Merge branch '7.8'
[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_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "util/u_simple_list.h"
32 #include "util/u_surface.h"
33 #include "lp_scene.h"
34 #include "lp_scene_queue.h"
35 #include "lp_debug.h"
36
37
38 struct lp_scene *
39 lp_scene_create( struct pipe_context *pipe,
40 struct lp_scene_queue *queue )
41 {
42 unsigned i, j;
43 struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
44 if (!scene)
45 return NULL;
46
47 scene->pipe = pipe;
48 scene->empty_queue = queue;
49
50 for (i = 0; i < TILES_X; i++) {
51 for (j = 0; j < TILES_Y; j++) {
52 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
53 bin->commands.head = bin->commands.tail = CALLOC_STRUCT(cmd_block);
54 }
55 }
56
57 scene->data.head =
58 scene->data.tail = CALLOC_STRUCT(data_block);
59
60 make_empty_list(&scene->textures);
61
62 pipe_mutex_init(scene->mutex);
63
64 return scene;
65 }
66
67
68 /**
69 * Free all data associated with the given scene, and free(scene).
70 */
71 void
72 lp_scene_destroy(struct lp_scene *scene)
73 {
74 unsigned i, j;
75
76 lp_scene_reset(scene);
77
78 for (i = 0; i < TILES_X; i++)
79 for (j = 0; j < TILES_Y; j++) {
80 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
81 assert(bin->commands.head == bin->commands.tail);
82 FREE(bin->commands.head);
83 bin->commands.head = NULL;
84 bin->commands.tail = NULL;
85 }
86
87 FREE(scene->data.head);
88 scene->data.head = NULL;
89
90 pipe_mutex_destroy(scene->mutex);
91
92 FREE(scene);
93 }
94
95
96 /**
97 * Check if the scene's bins are all empty.
98 * For debugging purposes.
99 */
100 boolean
101 lp_scene_is_empty(struct lp_scene *scene )
102 {
103 unsigned x, y;
104
105 for (y = 0; y < TILES_Y; y++) {
106 for (x = 0; x < TILES_X; x++) {
107 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
108 const struct cmd_block_list *list = &bin->commands;
109 if (list->head != list->tail || list->head->count > 0) {
110 return FALSE;
111 }
112 }
113 }
114 return TRUE;
115 }
116
117
118 /* Free data for one particular bin. May be called from the
119 * rasterizer thread(s).
120 */
121 void
122 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
123 {
124 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
125 struct cmd_block_list *list = &bin->commands;
126 struct cmd_block *block;
127 struct cmd_block *tmp;
128
129 assert(x < TILES_X);
130 assert(y < TILES_Y);
131
132 for (block = list->head; block != list->tail; block = tmp) {
133 tmp = block->next;
134 FREE(block);
135 }
136
137 assert(list->tail->next == NULL);
138 list->head = list->tail;
139 list->head->count = 0;
140 }
141
142
143 /**
144 * Free all the temporary data in a scene. May be called from the
145 * rasterizer thread(s).
146 */
147 void
148 lp_scene_reset(struct lp_scene *scene )
149 {
150 unsigned i, j;
151
152 /* Free all but last binner command lists:
153 */
154 for (i = 0; i < scene->tiles_x; i++) {
155 for (j = 0; j < scene->tiles_y; j++) {
156 lp_scene_bin_reset(scene, i, j);
157 }
158 }
159
160 assert(lp_scene_is_empty(scene));
161
162 /* Free all but last binned data block:
163 */
164 {
165 struct data_block_list *list = &scene->data;
166 struct data_block *block, *tmp;
167
168 for (block = list->head; block != list->tail; block = tmp) {
169 tmp = block->next;
170 FREE(block);
171 }
172
173 assert(list->tail->next == NULL);
174 list->head = list->tail;
175 list->head->used = 0;
176 }
177
178 /* Release texture refs
179 */
180 {
181 struct texture_ref *ref, *next, *ref_list = &scene->textures;
182 for (ref = ref_list->next; ref != ref_list; ref = next) {
183 next = next_elem(ref);
184 pipe_resource_reference(&ref->texture, NULL);
185 FREE(ref);
186 }
187 make_empty_list(ref_list);
188 }
189 }
190
191
192
193
194
195
196 void
197 lp_bin_new_cmd_block( struct cmd_block_list *list )
198 {
199 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
200 list->tail->next = block;
201 list->tail = block;
202 block->next = NULL;
203 block->count = 0;
204 }
205
206
207 void
208 lp_bin_new_data_block( struct data_block_list *list )
209 {
210 struct data_block *block = MALLOC_STRUCT(data_block);
211 list->tail->next = block;
212 list->tail = block;
213 block->next = NULL;
214 block->used = 0;
215 }
216
217
218 /** Return number of bytes used for all bin data within a scene */
219 unsigned
220 lp_scene_data_size( const struct lp_scene *scene )
221 {
222 unsigned size = 0;
223 const struct data_block *block;
224 for (block = scene->data.head; block; block = block->next) {
225 size += block->used;
226 }
227 return size;
228 }
229
230
231 /** Return number of bytes used for a single bin */
232 unsigned
233 lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y )
234 {
235 struct cmd_bin *bin = lp_scene_get_bin((struct lp_scene *) scene, x, y);
236 const struct cmd_block *cmd;
237 unsigned size = 0;
238 for (cmd = bin->commands.head; cmd; cmd = cmd->next) {
239 size += (cmd->count *
240 (sizeof(lp_rast_cmd) + sizeof(union lp_rast_cmd_arg)));
241 }
242 return size;
243 }
244
245
246 /**
247 * Add a reference to a texture by the scene.
248 */
249 void
250 lp_scene_texture_reference( struct lp_scene *scene,
251 struct pipe_resource *texture )
252 {
253 struct texture_ref *ref = CALLOC_STRUCT(texture_ref);
254 if (ref) {
255 struct texture_ref *ref_list = &scene->textures;
256 pipe_resource_reference(&ref->texture, texture);
257 insert_at_tail(ref_list, ref);
258 }
259 }
260
261
262 /**
263 * Does this scene have a reference to the given texture?
264 */
265 boolean
266 lp_scene_is_resource_referenced( const struct lp_scene *scene,
267 const struct pipe_resource *texture )
268 {
269 const struct texture_ref *ref_list = &scene->textures;
270 const struct texture_ref *ref;
271 foreach (ref, ref_list) {
272 if (ref->texture == texture)
273 return TRUE;
274 }
275 return FALSE;
276 }
277
278
279 /**
280 * Return last command in the bin
281 */
282 static lp_rast_cmd
283 lp_get_last_command( const struct cmd_bin *bin )
284 {
285 const struct cmd_block *tail = bin->commands.tail;
286 const unsigned i = tail->count;
287 if (i > 0)
288 return tail->cmd[i - 1];
289 else
290 return NULL;
291 }
292
293
294 /**
295 * Replace the arg of the last command in the bin.
296 */
297 static void
298 lp_replace_last_command_arg( struct cmd_bin *bin,
299 const union lp_rast_cmd_arg arg )
300 {
301 struct cmd_block *tail = bin->commands.tail;
302 const unsigned i = tail->count;
303 assert(i > 0);
304 tail->arg[i - 1] = arg;
305 }
306
307
308
309 /**
310 * Put a state-change command into all bins.
311 * If we find that the last command in a bin was also a state-change
312 * command, we can simply replace that one with the new one.
313 */
314 void
315 lp_scene_bin_state_command( struct lp_scene *scene,
316 lp_rast_cmd cmd,
317 const union lp_rast_cmd_arg arg )
318 {
319 unsigned i, j;
320 for (i = 0; i < scene->tiles_x; i++) {
321 for (j = 0; j < scene->tiles_y; j++) {
322 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
323 lp_rast_cmd last_cmd = lp_get_last_command(bin);
324 if (last_cmd == cmd) {
325 lp_replace_last_command_arg(bin, arg);
326 }
327 else {
328 lp_scene_bin_command( scene, i, j, cmd, arg );
329 }
330 }
331 }
332 }
333
334
335 /** advance curr_x,y to the next bin */
336 static boolean
337 next_bin(struct lp_scene *scene)
338 {
339 scene->curr_x++;
340 if (scene->curr_x >= scene->tiles_x) {
341 scene->curr_x = 0;
342 scene->curr_y++;
343 }
344 if (scene->curr_y >= scene->tiles_y) {
345 /* no more bins */
346 return FALSE;
347 }
348 return TRUE;
349 }
350
351
352 void
353 lp_scene_bin_iter_begin( struct lp_scene *scene )
354 {
355 scene->curr_x = scene->curr_y = -1;
356 }
357
358
359 /**
360 * Return pointer to next bin to be rendered.
361 * The lp_scene::curr_x and ::curr_y fields will be advanced.
362 * Multiple rendering threads will call this function to get a chunk
363 * of work (a bin) to work on.
364 */
365 struct cmd_bin *
366 lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y )
367 {
368 struct cmd_bin *bin = NULL;
369
370 pipe_mutex_lock(scene->mutex);
371
372 if (scene->curr_x < 0) {
373 /* first bin */
374 scene->curr_x = 0;
375 scene->curr_y = 0;
376 }
377 else if (!next_bin(scene)) {
378 /* no more bins left */
379 goto end;
380 }
381
382 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
383 *bin_x = scene->curr_x;
384 *bin_y = scene->curr_y;
385
386 end:
387 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
388 pipe_mutex_unlock(scene->mutex);
389 return bin;
390 }
391
392
393 /**
394 * Prepare this scene for the rasterizer.
395 * Map the framebuffer surfaces. Initialize the 'rast' state.
396 */
397 static boolean
398 lp_scene_map_buffers( struct lp_scene *scene )
399 {
400 struct pipe_surface *cbuf, *zsbuf;
401 unsigned usage;
402 int i;
403
404 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
405
406 /* XXX: try to improve on this:
407 */
408 usage = PIPE_TRANSFER_READ_WRITE;
409
410 /* Map all color buffers
411 */
412 for (i = 0; i < scene->fb.nr_cbufs; i++) {
413 cbuf = scene->fb.cbufs[i];
414 if (cbuf) {
415 scene->cbuf_map[i] = llvmpipe_resource_map(cbuf->texture,
416 usage,
417 cbuf->face,
418 cbuf->level,
419 cbuf->zslice);
420 if (!scene->cbuf_map[i])
421 goto fail;
422 }
423 }
424
425 /* Map the zsbuffer
426 */
427 zsbuf = scene->fb.zsbuf;
428 if (zsbuf) {
429 scene->zsbuf_map = llvmpipe_resource_map(zsbuf->texture,
430 usage,
431 zsbuf->face,
432 zsbuf->level,
433 zsbuf->zslice);
434 if (!scene->zsbuf_map)
435 goto fail;
436 }
437
438 return TRUE;
439
440 fail:
441 /* Unmap and release transfers?
442 */
443 return FALSE;
444 }
445
446
447
448 /**
449 * Called after rasterizer as finished rasterizing a scene.
450 *
451 * We want to call this from the pipe_context's current thread to
452 * avoid having to have mutexes on the transfer functions.
453 */
454 static void
455 lp_scene_unmap_buffers( struct lp_scene *scene )
456 {
457 unsigned i;
458
459 for (i = 0; i < scene->fb.nr_cbufs; i++) {
460 if (scene->cbuf_map[i]) {
461 struct pipe_surface *cbuf = scene->fb.cbufs[i];
462 llvmpipe_resource_unmap(cbuf->texture,
463 cbuf->face,
464 cbuf->level,
465 cbuf->zslice);
466 scene->cbuf_map[i] = NULL;
467 }
468 }
469
470 if (scene->zsbuf_map) {
471 struct pipe_surface *zsbuf = scene->fb.zsbuf;
472 llvmpipe_resource_unmap(zsbuf->texture,
473 zsbuf->face,
474 zsbuf->level,
475 zsbuf->zslice);
476 scene->zsbuf_map = NULL;
477 }
478
479 util_unreference_framebuffer_state( &scene->fb );
480 }
481
482
483 void lp_scene_begin_binning( struct lp_scene *scene,
484 struct pipe_framebuffer_state *fb )
485 {
486 assert(lp_scene_is_empty(scene));
487
488 util_copy_framebuffer_state(&scene->fb, fb);
489
490 scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
491 scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
492
493 assert(scene->tiles_x <= TILES_X);
494 assert(scene->tiles_y <= TILES_Y);
495 }
496
497
498 void lp_scene_rasterize( struct lp_scene *scene,
499 struct lp_rasterizer *rast,
500 boolean write_depth )
501 {
502 if (0) {
503 unsigned x, y;
504 debug_printf("rasterize scene:\n");
505 debug_printf(" data size: %u\n", lp_scene_data_size(scene));
506 for (y = 0; y < scene->tiles_y; y++) {
507 for (x = 0; x < scene->tiles_x; x++) {
508 debug_printf(" bin %u, %u size: %u\n", x, y,
509 lp_scene_bin_size(scene, x, y));
510 }
511 }
512 }
513
514
515 scene->write_depth = (scene->fb.zsbuf != NULL &&
516 write_depth);
517
518 lp_scene_map_buffers( scene );
519
520 /* Enqueue the scene for rasterization, then immediately wait for
521 * it to finish.
522 */
523 lp_rast_queue_scene( rast, scene );
524
525 /* Currently just wait for the rasterizer to finish. Some
526 * threading interactions need to be worked out, particularly once
527 * transfers become per-context:
528 */
529 lp_rast_finish( rast );
530 lp_scene_unmap_buffers( scene );
531 lp_scene_enqueue( scene->empty_queue, scene );
532 }