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