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"
39 #include "lp_tile_soa.h"
42 struct lp_scene_queue
;
44 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
45 * Will need a 64-bit version for larger framebuffers.
47 #define TILES_X (LP_MAX_WIDTH / TILE_SIZE)
48 #define TILES_Y (LP_MAX_HEIGHT / TILE_SIZE)
51 #define CMD_BLOCK_MAX 128
52 #define DATA_BLOCK_SIZE (16 * 1024 - sizeof(unsigned) - sizeof(void *))
56 /* switch to a non-pointer value for this:
58 typedef void (*lp_rast_cmd
)( struct lp_rasterizer_task
*,
59 const union lp_rast_cmd_arg
);
62 lp_rast_cmd cmd
[CMD_BLOCK_MAX
];
63 union lp_rast_cmd_arg arg
[CMD_BLOCK_MAX
];
65 struct cmd_block
*next
;
69 ubyte data
[DATA_BLOCK_SIZE
];
71 struct data_block
*next
;
74 struct cmd_block_list
{
75 struct cmd_block
*head
;
76 struct cmd_block
*tail
;
80 * For each screen tile we have one of these bins.
83 struct cmd_block_list commands
;
88 * This stores bulk data which is shared by all bins within a scene.
89 * Examples include triangle data and state data. The commands in
90 * the per-tile bins will point to chunks of data in this structure.
92 struct data_block_list
{
93 struct data_block
*head
;
94 struct data_block
*tail
;
98 /** List of resource references */
100 struct pipe_resource
*resource
;
101 struct resource_ref
*prev
, *next
; /**< linked list w/ u_simple_list.h */
106 * All bins and bin data are contained here.
107 * Per-bin data goes into the 'tile' bins.
108 * Shared data goes into the 'data' buffer.
110 * When there are multiple threads, will want to double-buffer between
114 struct pipe_context
*pipe
;
115 struct lp_fence
*fence
;
117 /** the framebuffer to render the scene into */
118 struct pipe_framebuffer_state fb
;
120 /** list of resources referenced by the scene commands */
121 struct resource_ref resources
;
123 /** Approx memory used by the scene (in bytes). This includes the
124 * shared and per-tile bins plus any referenced resources/textures.
128 boolean has_color_clear
;
129 boolean has_depthstencil_clear
;
132 * Number of active tiles in each dimension.
133 * This basically the framebuffer size divided by tile size
135 unsigned tiles_x
, tiles_y
;
137 int curr_x
, curr_y
; /**< for iterating over bins */
140 /* Where to place this scene once it has been rasterized:
142 struct lp_scene_queue
*empty_queue
;
144 struct cmd_bin tile
[TILES_X
][TILES_Y
];
145 struct data_block_list data
;
150 struct lp_scene
*lp_scene_create(struct pipe_context
*pipe
,
151 struct lp_scene_queue
*empty_queue
);
153 void lp_scene_destroy(struct lp_scene
*scene
);
157 boolean
lp_scene_is_empty(struct lp_scene
*scene
);
159 void lp_scene_reset(struct lp_scene
*scene
);
162 struct data_block
*lp_bin_new_data_block( struct data_block_list
*list
);
164 struct cmd_block
*lp_bin_new_cmd_block( struct cmd_block_list
*list
);
166 unsigned lp_scene_data_size( const struct lp_scene
*scene
);
168 unsigned lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
);
170 void lp_scene_add_resource_reference(struct lp_scene
*scene
,
171 struct pipe_resource
*resource
);
173 boolean
lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
174 const struct pipe_resource
*resource
);
178 * Allocate space for a command/data in the bin's data buffer.
179 * Grow the block list if needed.
182 lp_scene_alloc( struct lp_scene
*scene
, unsigned size
)
184 struct data_block_list
*list
= &scene
->data
;
185 struct data_block
*tail
= list
->tail
;
187 if (tail
->used
+ size
> DATA_BLOCK_SIZE
) {
188 tail
= lp_bin_new_data_block( list
);
195 scene
->scene_size
+= size
;
198 ubyte
*data
= tail
->data
+ tail
->used
;
206 * As above, but with specific alignment.
209 lp_scene_alloc_aligned( struct lp_scene
*scene
, unsigned size
,
212 struct data_block_list
*list
= &scene
->data
;
213 struct data_block
*tail
= list
->tail
;
215 if (tail
->used
+ size
+ alignment
- 1 > DATA_BLOCK_SIZE
) {
216 tail
= lp_bin_new_data_block( list
);
221 scene
->scene_size
+= size
;
224 ubyte
*data
= tail
->data
+ tail
->used
;
225 unsigned offset
= (((uintptr_t)data
+ alignment
- 1) & ~(alignment
- 1)) - (uintptr_t)data
;
226 tail
->used
+= offset
+ size
;
227 return data
+ offset
;
232 /* Put back data if we decide not to use it, eg. culled triangles.
235 lp_scene_putback_data( struct lp_scene
*scene
, unsigned size
)
237 struct data_block_list
*list
= &scene
->data
;
238 scene
->scene_size
-= size
;
239 assert(list
->tail
->used
>= size
);
240 list
->tail
->used
-= size
;
244 /** Return pointer to a particular tile's bin. */
245 static INLINE
struct cmd_bin
*
246 lp_scene_get_bin(struct lp_scene
*scene
, unsigned x
, unsigned y
)
248 return &scene
->tile
[x
][y
];
252 /** Remove all commands from a bin */
254 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
);
257 /* Add a command to bin[x][y].
260 lp_scene_bin_command( struct lp_scene
*scene
,
261 unsigned x
, unsigned y
,
263 union lp_rast_cmd_arg arg
)
265 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
266 struct cmd_block_list
*list
= &bin
->commands
;
267 struct cmd_block
*tail
= list
->tail
;
269 assert(x
< scene
->tiles_x
);
270 assert(y
< scene
->tiles_y
);
272 if (tail
->count
== CMD_BLOCK_MAX
) {
273 tail
= lp_bin_new_cmd_block( list
);
275 /* out of memory - simply ignore this command (for now) */
278 assert(tail
->count
== 0);
282 unsigned i
= tail
->count
;
290 /* Add a command to all active bins.
293 lp_scene_bin_everywhere( struct lp_scene
*scene
,
295 const union lp_rast_cmd_arg arg
)
298 for (i
= 0; i
< scene
->tiles_x
; i
++)
299 for (j
= 0; j
< scene
->tiles_y
; j
++)
300 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
305 lp_scene_bin_state_command( struct lp_scene
*scene
,
307 const union lp_rast_cmd_arg arg
);
310 static INLINE
unsigned
311 lp_scene_get_num_bins( const struct lp_scene
*scene
)
313 return scene
->tiles_x
* scene
->tiles_y
;
318 lp_scene_bin_iter_begin( struct lp_scene
*scene
);
321 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
);
325 lp_scene_rasterize( struct lp_scene
*scene
,
326 struct lp_rasterizer
*rast
);
329 lp_scene_begin_binning( struct lp_scene
*scene
,
330 struct pipe_framebuffer_state
*fb
);
333 static INLINE
unsigned
334 lp_scene_get_size(const struct lp_scene
*scene
)
336 return scene
->scene_size
;
340 #endif /* LP_BIN_H */