b7116297ece0b01ad8c2acb5e2eb5c55d52d1984
[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 "lp_scene.h"
33
34
35 struct lp_scene *
36 lp_scene_create(void)
37 {
38 struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
39 if (scene)
40 lp_scene_init(scene);
41 return scene;
42 }
43
44
45 void
46 lp_scene_destroy(struct lp_scene *scene)
47 {
48 lp_scene_reset(scene);
49 lp_scene_free_bin_data(scene);
50 FREE(scene);
51 }
52
53
54 void
55 lp_scene_init(struct lp_scene *scene)
56 {
57 unsigned i, j;
58 for (i = 0; i < TILES_X; i++)
59 for (j = 0; j < TILES_Y; j++) {
60 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
61 bin->commands.head = bin->commands.tail = CALLOC_STRUCT(cmd_block);
62 }
63
64 scene->data.head =
65 scene->data.tail = CALLOC_STRUCT(data_block);
66
67 make_empty_list(&scene->textures);
68
69 pipe_mutex_init(scene->mutex);
70 }
71
72
73 /**
74 * Check if the scene's bins are all empty.
75 * For debugging purposes.
76 */
77 boolean
78 lp_scene_is_empty(struct lp_scene *scene )
79 {
80 unsigned x, y;
81
82 for (y = 0; y < TILES_Y; y++) {
83 for (x = 0; x < TILES_X; x++) {
84 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
85 const struct cmd_block_list *list = &bin->commands;
86 if (list->head != list->tail || list->head->count > 0) {
87 return FALSE;
88 }
89 }
90 }
91 return TRUE;
92 }
93
94
95 void
96 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
97 {
98 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
99 struct cmd_block_list *list = &bin->commands;
100 struct cmd_block *block;
101 struct cmd_block *tmp;
102
103 for (block = list->head; block != list->tail; block = tmp) {
104 tmp = block->next;
105 FREE(block);
106 }
107
108 assert(list->tail->next == NULL);
109 list->head = list->tail;
110 list->head->count = 0;
111 }
112
113
114 /**
115 * Set scene to empty state.
116 */
117 void
118 lp_scene_reset(struct lp_scene *scene )
119 {
120 unsigned i, j;
121
122 /* Free all but last binner command lists:
123 */
124 for (i = 0; i < scene->tiles_x; i++) {
125 for (j = 0; j < scene->tiles_y; j++) {
126 lp_scene_bin_reset(scene, i, j);
127 }
128 }
129
130 assert(lp_scene_is_empty(scene));
131
132 /* Free all but last binned data block:
133 */
134 {
135 struct data_block_list *list = &scene->data;
136 struct data_block *block, *tmp;
137
138 for (block = list->head; block != list->tail; block = tmp) {
139 tmp = block->next;
140 FREE(block);
141 }
142
143 assert(list->tail->next == NULL);
144 list->head = list->tail;
145 list->head->used = 0;
146 }
147
148 /* Release texture refs
149 */
150 {
151 struct texture_ref *ref, *next, *ref_list = &scene->textures;
152 for (ref = ref_list->next; ref != ref_list; ref = next) {
153 next = next_elem(ref);
154 pipe_texture_reference(&ref->texture, NULL);
155 FREE(ref);
156 }
157 make_empty_list(ref_list);
158 }
159 }
160
161
162 /**
163 * Free all data associated with the given bin, but don't free(scene).
164 */
165 void
166 lp_scene_free_bin_data(struct lp_scene *scene)
167 {
168 unsigned i, j;
169
170 for (i = 0; i < TILES_X; i++)
171 for (j = 0; j < TILES_Y; j++) {
172 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
173 /* lp_reset_scene() should have been already called */
174 assert(bin->commands.head == bin->commands.tail);
175 FREE(bin->commands.head);
176 bin->commands.head = NULL;
177 bin->commands.tail = NULL;
178 }
179
180 FREE(scene->data.head);
181 scene->data.head = NULL;
182
183 pipe_mutex_destroy(scene->mutex);
184 }
185
186
187 void
188 lp_scene_set_framebuffer_size( struct lp_scene *scene,
189 unsigned width, unsigned height )
190 {
191 assert(lp_scene_is_empty(scene));
192
193 scene->tiles_x = align(width, TILE_SIZE) / TILE_SIZE;
194 scene->tiles_y = align(height, TILE_SIZE) / TILE_SIZE;
195 }
196
197
198 void
199 lp_bin_new_cmd_block( struct cmd_block_list *list )
200 {
201 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
202 list->tail->next = block;
203 list->tail = block;
204 block->next = NULL;
205 block->count = 0;
206 }
207
208
209 void
210 lp_bin_new_data_block( struct data_block_list *list )
211 {
212 struct data_block *block = MALLOC_STRUCT(data_block);
213 list->tail->next = block;
214 list->tail = block;
215 block->next = NULL;
216 block->used = 0;
217 }
218
219
220 /** Return number of bytes used for all bin data within a scene */
221 unsigned
222 lp_scene_data_size( const struct lp_scene *scene )
223 {
224 unsigned size = 0;
225 const struct data_block *block;
226 for (block = scene->data.head; block; block = block->next) {
227 size += block->used;
228 }
229 return size;
230 }
231
232
233 /** Return number of bytes used for a single bin */
234 unsigned
235 lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y )
236 {
237 struct cmd_bin *bin = lp_scene_get_bin((struct lp_scene *) scene, x, y);
238 const struct cmd_block *cmd;
239 unsigned size = 0;
240 for (cmd = bin->commands.head; cmd; cmd = cmd->next) {
241 size += (cmd->count *
242 (sizeof(lp_rast_cmd) + sizeof(union lp_rast_cmd_arg)));
243 }
244 return size;
245 }
246
247
248 /**
249 * Add a reference to a texture by the scene.
250 */
251 void
252 lp_scene_texture_reference( struct lp_scene *scene,
253 struct pipe_texture *texture )
254 {
255 struct texture_ref *ref = CALLOC_STRUCT(texture_ref);
256 if (ref) {
257 struct texture_ref *ref_list = &scene->textures;
258 pipe_texture_reference(&ref->texture, texture);
259 insert_at_tail(ref_list, ref);
260 }
261 }
262
263
264 /**
265 * Does this scene have a reference to the given texture?
266 */
267 boolean
268 lp_scene_is_texture_referenced( const struct lp_scene *scene,
269 const struct pipe_texture *texture )
270 {
271 const struct texture_ref *ref_list = &scene->textures;
272 const struct texture_ref *ref;
273 foreach (ref, ref_list) {
274 if (ref->texture == texture)
275 return TRUE;
276 }
277 return FALSE;
278 }
279
280
281 /**
282 * Return last command in the bin
283 */
284 static lp_rast_cmd
285 lp_get_last_command( const struct cmd_bin *bin )
286 {
287 const struct cmd_block *tail = bin->commands.tail;
288 const unsigned i = tail->count;
289 if (i > 0)
290 return tail->cmd[i - 1];
291 else
292 return NULL;
293 }
294
295
296 /**
297 * Replace the arg of the last command in the bin.
298 */
299 static void
300 lp_replace_last_command_arg( struct cmd_bin *bin,
301 const union lp_rast_cmd_arg arg )
302 {
303 struct cmd_block *tail = bin->commands.tail;
304 const unsigned i = tail->count;
305 assert(i > 0);
306 tail->arg[i - 1] = arg;
307 }
308
309
310
311 /**
312 * Put a state-change command into all bins.
313 * If we find that the last command in a bin was also a state-change
314 * command, we can simply replace that one with the new one.
315 */
316 void
317 lp_scene_bin_state_command( struct lp_scene *scene,
318 lp_rast_cmd cmd,
319 const union lp_rast_cmd_arg arg )
320 {
321 unsigned i, j;
322 for (i = 0; i < scene->tiles_x; i++) {
323 for (j = 0; j < scene->tiles_y; j++) {
324 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
325 lp_rast_cmd last_cmd = lp_get_last_command(bin);
326 if (last_cmd == cmd) {
327 lp_replace_last_command_arg(bin, arg);
328 }
329 else {
330 lp_scene_bin_command( scene, i, j, cmd, arg );
331 }
332 }
333 }
334 }
335
336
337 /** advance curr_x,y to the next bin */
338 static boolean
339 next_bin(struct lp_scene *scene)
340 {
341 scene->curr_x++;
342 if (scene->curr_x >= scene->tiles_x) {
343 scene->curr_x = 0;
344 scene->curr_y++;
345 }
346 if (scene->curr_y >= scene->tiles_y) {
347 /* no more bins */
348 return FALSE;
349 }
350 return TRUE;
351 }
352
353
354 void
355 lp_scene_bin_iter_begin( struct lp_scene *scene )
356 {
357 scene->curr_x = scene->curr_y = -1;
358 }
359
360
361 /**
362 * Return pointer to next bin to be rendered.
363 * The lp_scene::curr_x and ::curr_y fields will be advanced.
364 * Multiple rendering threads will call this function to get a chunk
365 * of work (a bin) to work on.
366 */
367 struct cmd_bin *
368 lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y )
369 {
370 struct cmd_bin *bin = NULL;
371
372 pipe_mutex_lock(scene->mutex);
373
374 if (scene->curr_x < 0) {
375 /* first bin */
376 scene->curr_x = 0;
377 scene->curr_y = 0;
378 }
379 else if (!next_bin(scene)) {
380 /* no more bins left */
381 goto end;
382 }
383
384 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
385 *bin_x = scene->curr_x;
386 *bin_y = scene->curr_y;
387
388 end:
389 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
390 pipe_mutex_unlock(scene->mutex);
391 return bin;
392 }