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