801829d93d2f5c642726a36826249779433ff071
[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_rast.h"
40 #include "lp_debug.h"
41
42 struct lp_scene_queue;
43 struct lp_rast_state;
44
45 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
46 * Will need a 64-bit version for larger framebuffers.
47 */
48 #define TILES_X (LP_MAX_WIDTH / TILE_SIZE)
49 #define TILES_Y (LP_MAX_HEIGHT / TILE_SIZE)
50
51
52 /* Commands per command block (ideally so sizeof(cmd_block) is a power of
53 * two in size.)
54 */
55 #define CMD_BLOCK_MAX 29
56
57 /* Bytes per data block.
58 */
59 #define DATA_BLOCK_SIZE (64 * 1024)
60
61 /* Scene temporary storage is clamped to this size:
62 */
63 #define LP_SCENE_MAX_SIZE (9*1024*1024)
64
65 /* The maximum amount of texture storage referenced by a scene is
66 * clamped ot this size:
67 */
68 #define LP_SCENE_MAX_RESOURCE_SIZE (64*1024*1024)
69
70
71 /* switch to a non-pointer value for this:
72 */
73 typedef void (*lp_rast_cmd_func)( struct lp_rasterizer_task *,
74 const union lp_rast_cmd_arg );
75
76
77 struct cmd_block {
78 uint8_t cmd[CMD_BLOCK_MAX];
79 union lp_rast_cmd_arg arg[CMD_BLOCK_MAX];
80 unsigned count;
81 struct cmd_block *next;
82 };
83
84 struct cmd_block_list {
85 struct cmd_block *head;
86 struct cmd_block *tail;
87 };
88
89 struct data_block {
90 ubyte data[DATA_BLOCK_SIZE];
91 unsigned used;
92 struct data_block *next;
93 };
94
95
96
97 /**
98 * For each screen tile we have one of these bins.
99 */
100 struct cmd_bin {
101 ushort x;
102 ushort y;
103 const struct lp_rast_state *last_state; /* most recent state set in bin */
104 struct cmd_block *head;
105 struct cmd_block *tail;
106 };
107
108
109 /**
110 * This stores bulk data which is used for all memory allocations
111 * within a scene.
112 *
113 * Examples include triangle data and state data. The commands in
114 * the per-tile bins will point to chunks of data in this structure.
115 *
116 * Include the first block of data statically to ensure we can always
117 * initiate a scene without relying on malloc succeeding.
118 */
119 struct data_block_list {
120 struct data_block first;
121 struct data_block *head;
122 };
123
124 struct resource_ref;
125
126 /**
127 * All bins and bin data are contained here.
128 * Per-bin data goes into the 'tile' bins.
129 * Shared data goes into the 'data' buffer.
130 *
131 * When there are multiple threads, will want to double-buffer between
132 * scenes:
133 */
134 struct lp_scene {
135 struct pipe_context *pipe;
136 struct lp_fence *fence;
137
138 /* Framebuffer mappings - valid only between begin_rasterization()
139 * and end_rasterization().
140 */
141 struct {
142 uint8_t *map;
143 unsigned stride;
144 unsigned blocksize;
145 } zsbuf, cbufs[PIPE_MAX_COLOR_BUFS];
146
147 /** the framebuffer to render the scene into */
148 struct pipe_framebuffer_state fb;
149
150 /** list of resources referenced by the scene commands */
151 struct resource_ref *resources;
152
153 /** Total memory used by the scene (in bytes). This sums all the
154 * data blocks and counts all bins, state, resource references and
155 * other random allocations within the scene.
156 */
157 unsigned scene_size;
158
159 /** Sum of sizes of all resources referenced by the scene. Sums
160 * all the textures read by the scene:
161 */
162 unsigned resource_reference_size;
163
164 boolean alloc_failed;
165 boolean has_depthstencil_clear;
166 boolean discard;
167 /**
168 * Number of active tiles in each dimension.
169 * This basically the framebuffer size divided by tile size
170 */
171 unsigned tiles_x, tiles_y;
172
173 int curr_x, curr_y; /**< for iterating over bins */
174 pipe_mutex mutex;
175
176 struct cmd_bin tile[TILES_X][TILES_Y];
177 struct data_block_list data;
178 };
179
180
181
182 struct lp_scene *lp_scene_create(struct pipe_context *pipe);
183
184 void lp_scene_destroy(struct lp_scene *scene);
185
186 boolean lp_scene_is_empty(struct lp_scene *scene );
187 boolean lp_scene_is_oom(struct lp_scene *scene );
188
189
190 struct data_block *lp_scene_new_data_block( struct lp_scene *scene );
191
192 struct cmd_block *lp_scene_new_cmd_block( struct lp_scene *scene,
193 struct cmd_bin *bin );
194
195 boolean lp_scene_add_resource_reference(struct lp_scene *scene,
196 struct pipe_resource *resource,
197 boolean initializing_scene);
198
199 boolean lp_scene_is_resource_referenced(const struct lp_scene *scene,
200 const struct pipe_resource *resource );
201
202
203 /**
204 * Allocate space for a command/data in the bin's data buffer.
205 * Grow the block list if needed.
206 */
207 static INLINE void *
208 lp_scene_alloc( struct lp_scene *scene, unsigned size)
209 {
210 struct data_block_list *list = &scene->data;
211 struct data_block *block = list->head;
212
213 assert(size <= DATA_BLOCK_SIZE);
214 assert(block != NULL);
215
216 if (LP_DEBUG & DEBUG_MEM)
217 debug_printf("alloc %u block %u/%u tot %u/%u\n",
218 size, block->used, DATA_BLOCK_SIZE,
219 scene->scene_size, LP_SCENE_MAX_SIZE);
220
221 if (block->used + size > DATA_BLOCK_SIZE) {
222 block = lp_scene_new_data_block( scene );
223 if (!block) {
224 /* out of memory */
225 return NULL;
226 }
227 }
228
229 {
230 ubyte *data = block->data + block->used;
231 block->used += size;
232 return data;
233 }
234 }
235
236
237 /**
238 * As above, but with specific alignment.
239 */
240 static INLINE void *
241 lp_scene_alloc_aligned( struct lp_scene *scene, unsigned size,
242 unsigned alignment )
243 {
244 struct data_block_list *list = &scene->data;
245 struct data_block *block = list->head;
246
247 assert(block != NULL);
248
249 if (LP_DEBUG & DEBUG_MEM)
250 debug_printf("alloc %u block %u/%u tot %u/%u\n",
251 size + alignment - 1,
252 block->used, DATA_BLOCK_SIZE,
253 scene->scene_size, LP_SCENE_MAX_SIZE);
254
255 if (block->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
256 block = lp_scene_new_data_block( scene );
257 if (!block)
258 return NULL;
259 }
260
261 {
262 ubyte *data = block->data + block->used;
263 unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
264 block->used += offset + size;
265 return data + offset;
266 }
267 }
268
269
270 /* Put back data if we decide not to use it, eg. culled triangles.
271 */
272 static INLINE void
273 lp_scene_putback_data( struct lp_scene *scene, unsigned size)
274 {
275 struct data_block_list *list = &scene->data;
276 assert(list->head && list->head->used >= size);
277 list->head->used -= size;
278 }
279
280
281 /** Return pointer to a particular tile's bin. */
282 static INLINE struct cmd_bin *
283 lp_scene_get_bin(struct lp_scene *scene, unsigned x, unsigned y)
284 {
285 return &scene->tile[x][y];
286 }
287
288
289 /** Remove all commands from a bin */
290 void
291 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y);
292
293
294 /* Add a command to bin[x][y].
295 */
296 static INLINE boolean
297 lp_scene_bin_command( struct lp_scene *scene,
298 unsigned x, unsigned y,
299 unsigned cmd,
300 union lp_rast_cmd_arg arg )
301 {
302 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
303 struct cmd_block *tail = bin->tail;
304
305 assert(x < scene->tiles_x);
306 assert(y < scene->tiles_y);
307 assert(cmd < LP_RAST_OP_MAX);
308
309 if (tail == NULL || tail->count == CMD_BLOCK_MAX) {
310 tail = lp_scene_new_cmd_block( scene, bin );
311 if (!tail) {
312 return FALSE;
313 }
314 assert(tail->count == 0);
315 }
316
317 {
318 unsigned i = tail->count;
319 tail->cmd[i] = cmd & LP_RAST_OP_MASK;
320 tail->arg[i] = arg;
321 tail->count++;
322 }
323
324 return TRUE;
325 }
326
327
328 static INLINE boolean
329 lp_scene_bin_cmd_with_state( struct lp_scene *scene,
330 unsigned x, unsigned y,
331 const struct lp_rast_state *state,
332 unsigned cmd,
333 union lp_rast_cmd_arg arg )
334 {
335 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
336
337 if (state != bin->last_state) {
338 bin->last_state = state;
339 if (!lp_scene_bin_command(scene, x, y,
340 LP_RAST_OP_SET_STATE,
341 lp_rast_arg_state(state)))
342 return FALSE;
343 }
344
345 if (!lp_scene_bin_command( scene, x, y, cmd, arg ))
346 return FALSE;
347
348 return TRUE;
349 }
350
351
352 /* Add a command to all active bins.
353 */
354 static INLINE boolean
355 lp_scene_bin_everywhere( struct lp_scene *scene,
356 unsigned cmd,
357 const union lp_rast_cmd_arg arg )
358 {
359 unsigned i, j;
360 for (i = 0; i < scene->tiles_x; i++) {
361 for (j = 0; j < scene->tiles_y; j++) {
362 if (!lp_scene_bin_command( scene, i, j, cmd, arg ))
363 return FALSE;
364 }
365 }
366
367 return TRUE;
368 }
369
370
371 static INLINE unsigned
372 lp_scene_get_num_bins( const struct lp_scene *scene )
373 {
374 return scene->tiles_x * scene->tiles_y;
375 }
376
377
378 void
379 lp_scene_bin_iter_begin( struct lp_scene *scene );
380
381 struct cmd_bin *
382 lp_scene_bin_iter_next( struct lp_scene *scene );
383
384
385
386 /* Begin/end binning of a scene
387 */
388 void
389 lp_scene_begin_binning( struct lp_scene *scene,
390 struct pipe_framebuffer_state *fb,
391 boolean discard );
392
393 void
394 lp_scene_end_binning( struct lp_scene *scene );
395
396
397 /* Begin/end rasterization of a scene
398 */
399 void
400 lp_scene_begin_rasterization(struct lp_scene *scene);
401
402 void
403 lp_scene_end_rasterization(struct lp_scene *scene );
404
405
406
407
408
409 #endif /* LP_BIN_H */