1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
38 #include "os/os_thread.h"
42 struct lp_scene_queue
;
45 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
46 * Will need a 64-bit version for larger framebuffers.
48 #define TILES_X (LP_MAX_WIDTH / TILE_SIZE)
49 #define TILES_Y (LP_MAX_HEIGHT / TILE_SIZE)
52 /* Commands per command block (ideally so sizeof(cmd_block) is a power of
55 #define CMD_BLOCK_MAX 29
57 /* Bytes per data block.
59 #define DATA_BLOCK_SIZE (64 * 1024)
61 /* Scene temporary storage is clamped to this size:
63 #define LP_SCENE_MAX_SIZE (36*1024*1024)
65 /* The maximum amount of texture storage referenced by a scene is
66 * clamped to this size:
68 #define LP_SCENE_MAX_RESOURCE_SIZE (64*1024*1024)
71 /* switch to a non-pointer value for this:
73 typedef void (*lp_rast_cmd_func
)( struct lp_rasterizer_task
*,
74 const union lp_rast_cmd_arg
);
78 uint8_t cmd
[CMD_BLOCK_MAX
];
79 union lp_rast_cmd_arg arg
[CMD_BLOCK_MAX
];
81 struct cmd_block
*next
;
86 ubyte data
[DATA_BLOCK_SIZE
];
88 struct data_block
*next
;
94 * For each screen tile we have one of these bins.
97 const struct lp_rast_state
*last_state
; /* most recent state set in bin */
98 struct cmd_block
*head
;
99 struct cmd_block
*tail
;
104 * This stores bulk data which is used for all memory allocations
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.
110 * Include the first block of data statically to ensure we can always
111 * initiate a scene without relying on malloc succeeding.
113 struct data_block_list
{
114 struct data_block first
;
115 struct data_block
*head
;
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.
125 * When there are multiple threads, will want to double-buffer between
129 struct pipe_context
*pipe
;
130 struct lp_fence
*fence
;
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 */
138 /* Framebuffer mappings - valid only between begin_rasterization()
139 * and end_rasterization().
144 unsigned layer_stride
;
145 unsigned format_bytes
;
146 unsigned sample_stride
;
148 } zsbuf
, cbufs
[PIPE_MAX_COLOR_BUFS
];
150 /* The amount of layers in the fb (minimum of all attachments) */
151 unsigned fb_max_layer
;
153 /* fixed point sample positions. */
154 int32_t fixed_sample_pos
[LP_MAX_SAMPLES
][2];
156 /* max samples for bound framebuffer */
157 unsigned fb_max_samples
;
159 /** the framebuffer to render the scene into */
160 struct pipe_framebuffer_state fb
;
162 /** list of resources referenced by the scene commands */
163 struct resource_ref
*resources
;
165 /** Total memory used by the scene (in bytes). This sums all the
166 * data blocks and counts all bins, state, resource references and
167 * other random allocations within the scene.
171 /** Sum of sizes of all resources referenced by the scene. Sums
172 * all the textures read by the scene:
174 unsigned resource_reference_size
;
176 boolean alloc_failed
;
178 * Number of active tiles in each dimension.
179 * This basically the framebuffer size divided by tile size
181 unsigned tiles_x
, tiles_y
;
183 int curr_x
, curr_y
; /**< for iterating over bins */
186 struct cmd_bin tile
[TILES_X
][TILES_Y
];
187 struct data_block_list data
;
192 struct lp_scene
*lp_scene_create(struct pipe_context
*pipe
);
194 void lp_scene_destroy(struct lp_scene
*scene
);
196 boolean
lp_scene_is_empty(struct lp_scene
*scene
);
197 boolean
lp_scene_is_oom(struct lp_scene
*scene
);
200 struct data_block
*lp_scene_new_data_block( struct lp_scene
*scene
);
202 struct cmd_block
*lp_scene_new_cmd_block( struct lp_scene
*scene
,
203 struct cmd_bin
*bin
);
205 boolean
lp_scene_add_resource_reference(struct lp_scene
*scene
,
206 struct pipe_resource
*resource
,
207 boolean initializing_scene
);
209 boolean
lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
210 const struct pipe_resource
*resource
);
214 * Allocate space for a command/data in the bin's data buffer.
215 * Grow the block list if needed.
218 lp_scene_alloc( struct lp_scene
*scene
, unsigned size
)
220 struct data_block_list
*list
= &scene
->data
;
221 struct data_block
*block
= list
->head
;
223 assert(size
<= DATA_BLOCK_SIZE
);
224 assert(block
!= NULL
);
226 if (LP_DEBUG
& DEBUG_MEM
)
227 debug_printf("alloc %u block %u/%u tot %u/%u\n",
228 size
, block
->used
, DATA_BLOCK_SIZE
,
229 scene
->scene_size
, LP_SCENE_MAX_SIZE
);
231 if (block
->used
+ size
> DATA_BLOCK_SIZE
) {
232 block
= lp_scene_new_data_block( scene
);
240 ubyte
*data
= block
->data
+ block
->used
;
248 * As above, but with specific alignment.
251 lp_scene_alloc_aligned( struct lp_scene
*scene
, unsigned size
,
254 struct data_block_list
*list
= &scene
->data
;
255 struct data_block
*block
= list
->head
;
257 assert(block
!= NULL
);
259 if (LP_DEBUG
& DEBUG_MEM
)
260 debug_printf("alloc %u block %u/%u tot %u/%u\n",
261 size
+ alignment
- 1,
262 block
->used
, DATA_BLOCK_SIZE
,
263 scene
->scene_size
, LP_SCENE_MAX_SIZE
);
265 if (block
->used
+ size
+ alignment
- 1 > DATA_BLOCK_SIZE
) {
266 block
= lp_scene_new_data_block( scene
);
272 ubyte
*data
= block
->data
+ block
->used
;
273 unsigned offset
= (((uintptr_t)data
+ alignment
- 1) & ~(alignment
- 1)) - (uintptr_t)data
;
274 block
->used
+= offset
+ size
;
275 return data
+ offset
;
280 /* Put back data if we decide not to use it, eg. culled triangles.
283 lp_scene_putback_data( struct lp_scene
*scene
, unsigned size
)
285 struct data_block_list
*list
= &scene
->data
;
286 assert(list
->head
&& list
->head
->used
>= size
);
287 list
->head
->used
-= size
;
291 /** Return pointer to a particular tile's bin. */
292 static inline struct cmd_bin
*
293 lp_scene_get_bin(struct lp_scene
*scene
, unsigned x
, unsigned y
)
295 return &scene
->tile
[x
][y
];
299 /** Remove all commands from a bin */
301 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
);
304 /* Add a command to bin[x][y].
306 static inline boolean
307 lp_scene_bin_command( struct lp_scene
*scene
,
308 unsigned x
, unsigned y
,
310 union lp_rast_cmd_arg arg
)
312 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
313 struct cmd_block
*tail
= bin
->tail
;
315 assert(x
< scene
->tiles_x
);
316 assert(y
< scene
->tiles_y
);
317 assert(cmd
< LP_RAST_OP_MAX
);
319 if (tail
== NULL
|| tail
->count
== CMD_BLOCK_MAX
) {
320 tail
= lp_scene_new_cmd_block( scene
, bin
);
324 assert(tail
->count
== 0);
328 unsigned i
= tail
->count
;
329 tail
->cmd
[i
] = cmd
& LP_RAST_OP_MASK
;
338 static inline boolean
339 lp_scene_bin_cmd_with_state( struct lp_scene
*scene
,
340 unsigned x
, unsigned y
,
341 const struct lp_rast_state
*state
,
343 union lp_rast_cmd_arg arg
)
345 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
347 if (state
!= bin
->last_state
) {
348 bin
->last_state
= state
;
349 if (!lp_scene_bin_command(scene
, x
, y
,
350 LP_RAST_OP_SET_STATE
,
351 lp_rast_arg_state(state
)))
355 if (!lp_scene_bin_command( scene
, x
, y
, cmd
, arg
))
362 /* Add a command to all active bins.
364 static inline boolean
365 lp_scene_bin_everywhere( struct lp_scene
*scene
,
367 const union lp_rast_cmd_arg arg
)
370 for (i
= 0; i
< scene
->tiles_x
; i
++) {
371 for (j
= 0; j
< scene
->tiles_y
; j
++) {
372 if (!lp_scene_bin_command( scene
, i
, j
, cmd
, arg
))
381 static inline unsigned
382 lp_scene_get_num_bins( const struct lp_scene
*scene
)
384 return scene
->tiles_x
* scene
->tiles_y
;
389 lp_scene_bin_iter_begin( struct lp_scene
*scene
);
392 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *x
, int *y
);
396 /* Begin/end binning of a scene
399 lp_scene_begin_binning(struct lp_scene
*scene
,
400 struct pipe_framebuffer_state
*fb
);
403 lp_scene_end_binning(struct lp_scene
*scene
);
406 /* Begin/end rasterization of a scene
409 lp_scene_begin_rasterization(struct lp_scene
*scene
);
412 lp_scene_end_rasterization(struct lp_scene
*scene
);
418 #endif /* LP_BIN_H */