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