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