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