d4f7b047c2a2b749fdff073e583803437df199c2
[mesa.git] / src / gallium / drivers / llvmpipe / lp_scene.h
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
29 /**
30 * Binner data structures and bin-related functions.
31 * Note: the "setup" code is concerned with building scenes while
32 * The "rast" code is concerned with consuming/executing scenes.
33 */
34
35 #ifndef LP_SCENE_H
36 #define LP_SCENE_H
37
38 #include "os/os_thread.h"
39 #include "lp_tile_soa.h"
40 #include "lp_rast.h"
41
42 struct lp_scene_queue;
43
44 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
45 * Will need a 64-bit version for larger framebuffers.
46 */
47 #define TILES_X (LP_MAX_WIDTH / TILE_SIZE)
48 #define TILES_Y (LP_MAX_HEIGHT / TILE_SIZE)
49
50
51 #define CMD_BLOCK_MAX 16
52 #define DATA_BLOCK_SIZE (64 * 1024 - 2 * sizeof(void *))
53
54 /* Scene temporary storage is clamped to this size:
55 */
56 #define LP_SCENE_MAX_SIZE (1024*1024)
57
58 /* The maximum amount of texture storage referenced by a scene is
59 * clamped ot this size:
60 */
61 #define LP_SCENE_MAX_RESOURCE_SIZE (64*1024*1024)
62
63
64 /* switch to a non-pointer value for this:
65 */
66 typedef void (*lp_rast_cmd)( struct lp_rasterizer_task *,
67 const union lp_rast_cmd_arg );
68
69 struct cmd_block {
70 lp_rast_cmd cmd[CMD_BLOCK_MAX];
71 union lp_rast_cmd_arg arg[CMD_BLOCK_MAX];
72 unsigned count;
73 struct cmd_block *next;
74 };
75
76 struct cmd_block_list {
77 struct cmd_block *head;
78 struct cmd_block *tail;
79 };
80
81 struct data_block {
82 ubyte data[DATA_BLOCK_SIZE];
83 unsigned used;
84 struct data_block *next;
85 };
86
87
88
89 /**
90 * For each screen tile we have one of these bins.
91 */
92 struct cmd_bin {
93 struct cmd_block_list commands;
94 };
95
96
97 /**
98 * This stores bulk data which is used for all memory allocations
99 * within a scene.
100 *
101 * Examples include triangle data and state data. The commands in
102 * the per-tile bins will point to chunks of data in this structure.
103 */
104 struct data_block_list {
105 struct data_block *head;
106 };
107
108 struct resource_ref;
109
110 /**
111 * All bins and bin data are contained here.
112 * Per-bin data goes into the 'tile' bins.
113 * Shared data goes into the 'data' buffer.
114 *
115 * When there are multiple threads, will want to double-buffer between
116 * scenes:
117 */
118 struct lp_scene {
119 struct pipe_context *pipe;
120 struct lp_fence *fence;
121
122 /** the framebuffer to render the scene into */
123 struct pipe_framebuffer_state fb;
124
125 /** list of resources referenced by the scene commands */
126 struct resource_ref *resources;
127
128 /** Total memory used by the scene (in bytes). This sums all the
129 * data blocks and counts all bins, state, resource references and
130 * other random allocations within the scene.
131 */
132 unsigned scene_size;
133
134 /** Sum of sizes of all resources referenced by the scene. Sums
135 * all the textures read by the scene:
136 */
137 unsigned resource_reference_size;
138
139 boolean alloc_failed;
140
141 boolean has_depthstencil_clear;
142
143 /**
144 * Number of active tiles in each dimension.
145 * This basically the framebuffer size divided by tile size
146 */
147 unsigned tiles_x, tiles_y;
148
149 int curr_x, curr_y; /**< for iterating over bins */
150 pipe_mutex mutex;
151
152 /* Where to place this scene once it has been rasterized:
153 */
154 struct lp_scene_queue *empty_queue;
155
156 struct cmd_bin tile[TILES_X][TILES_Y];
157 struct data_block_list data;
158 };
159
160
161
162 struct lp_scene *lp_scene_create(struct pipe_context *pipe,
163 struct lp_scene_queue *empty_queue);
164
165 void lp_scene_destroy(struct lp_scene *scene);
166
167 boolean lp_scene_is_empty(struct lp_scene *scene );
168 boolean lp_scene_is_oom(struct lp_scene *scene );
169
170 void lp_scene_reset(struct lp_scene *scene );
171
172
173 struct data_block *lp_scene_new_data_block( struct lp_scene *scene );
174
175 struct cmd_block *lp_scene_new_cmd_block( struct lp_scene *scene,
176 struct cmd_bin *bin );
177
178 boolean lp_scene_add_resource_reference(struct lp_scene *scene,
179 struct pipe_resource *resource);
180
181 boolean lp_scene_is_resource_referenced(const struct lp_scene *scene,
182 const struct pipe_resource *resource );
183
184
185 /**
186 * Allocate space for a command/data in the bin's data buffer.
187 * Grow the block list if needed.
188 */
189 static INLINE void *
190 lp_scene_alloc( struct lp_scene *scene, unsigned size)
191 {
192 struct data_block_list *list = &scene->data;
193 struct data_block *block = list->head;
194
195 assert(size <= DATA_BLOCK_SIZE);
196
197 if (block == NULL || block->used + size > DATA_BLOCK_SIZE) {
198 block = lp_scene_new_data_block( scene );
199 if (!block) {
200 /* out of memory */
201 return NULL;
202 }
203 }
204
205 {
206 ubyte *data = block->data + block->used;
207 block->used += size;
208 return data;
209 }
210 }
211
212
213 /**
214 * As above, but with specific alignment.
215 */
216 static INLINE void *
217 lp_scene_alloc_aligned( struct lp_scene *scene, unsigned size,
218 unsigned alignment )
219 {
220 struct data_block_list *list = &scene->data;
221 struct data_block *block = list->head;
222
223 if (block == NULL ||
224 block->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
225 block = lp_scene_new_data_block( scene );
226 if (!block)
227 return NULL;
228 }
229
230 {
231 ubyte *data = block->data + block->used;
232 unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
233 block->used += offset + size;
234 return data + offset;
235 }
236 }
237
238
239 /* Put back data if we decide not to use it, eg. culled triangles.
240 */
241 static INLINE void
242 lp_scene_putback_data( struct lp_scene *scene, unsigned size)
243 {
244 struct data_block_list *list = &scene->data;
245 assert(list->head && list->head->used >= size);
246 list->head->used -= size;
247 }
248
249
250 /** Return pointer to a particular tile's bin. */
251 static INLINE struct cmd_bin *
252 lp_scene_get_bin(struct lp_scene *scene, unsigned x, unsigned y)
253 {
254 return &scene->tile[x][y];
255 }
256
257
258 /** Remove all commands from a bin */
259 void
260 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y);
261
262
263 /* Add a command to bin[x][y].
264 */
265 static INLINE boolean
266 lp_scene_bin_command( struct lp_scene *scene,
267 unsigned x, unsigned y,
268 lp_rast_cmd cmd,
269 union lp_rast_cmd_arg arg )
270 {
271 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
272 struct cmd_block *tail = bin->commands.tail;
273
274 assert(x < scene->tiles_x);
275 assert(y < scene->tiles_y);
276
277 if (tail == NULL || tail->count == CMD_BLOCK_MAX) {
278 tail = lp_scene_new_cmd_block( scene, bin );
279 if (!tail) {
280 return FALSE;
281 }
282 assert(tail->count == 0);
283 }
284
285 {
286 unsigned i = tail->count;
287 tail->cmd[i] = cmd;
288 tail->arg[i] = arg;
289 tail->count++;
290 }
291
292 return TRUE;
293 }
294
295
296 /* Add a command to all active bins.
297 */
298 static INLINE boolean
299 lp_scene_bin_everywhere( struct lp_scene *scene,
300 lp_rast_cmd cmd,
301 const union lp_rast_cmd_arg arg )
302 {
303 unsigned i, j;
304 for (i = 0; i < scene->tiles_x; i++) {
305 for (j = 0; j < scene->tiles_y; j++) {
306 if (!lp_scene_bin_command( scene, i, j, cmd, arg ))
307 return FALSE;
308 }
309 }
310
311 return TRUE;
312 }
313
314
315 static INLINE unsigned
316 lp_scene_get_num_bins( const struct lp_scene *scene )
317 {
318 return scene->tiles_x * scene->tiles_y;
319 }
320
321
322 void
323 lp_scene_bin_iter_begin( struct lp_scene *scene );
324
325 struct cmd_bin *
326 lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y );
327
328
329 void
330 lp_scene_rasterize( struct lp_scene *scene,
331 struct lp_rasterizer *rast );
332
333 void
334 lp_scene_begin_binning( struct lp_scene *scene,
335 struct pipe_framebuffer_state *fb );
336
337
338
339 #endif /* LP_BIN_H */