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"
43 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
44 * Will need a 64-bit version for larger framebuffers.
46 #define MAXHEIGHT 2048
48 #define TILES_X (MAXWIDTH / TILE_SIZE)
49 #define TILES_Y (MAXHEIGHT / TILE_SIZE)
52 #define CMD_BLOCK_MAX 128
53 #define DATA_BLOCK_SIZE (16 * 1024 - sizeof(unsigned) - sizeof(void *))
57 /* switch to a non-pointer value for this:
59 typedef void (*lp_rast_cmd
)( struct lp_rasterizer
*,
60 unsigned thread_index
,
61 const union lp_rast_cmd_arg
);
64 lp_rast_cmd cmd
[CMD_BLOCK_MAX
];
65 union lp_rast_cmd_arg arg
[CMD_BLOCK_MAX
];
67 struct cmd_block
*next
;
71 ubyte data
[DATA_BLOCK_SIZE
];
73 struct data_block
*next
;
76 struct cmd_block_list
{
77 struct cmd_block
*head
;
78 struct cmd_block
*tail
;
82 * For each screen tile we have one of these bins.
85 struct cmd_block_list commands
;
90 * This stores bulk data which is shared by all bins within a scene.
91 * Examples include triangle data and state data. The commands in
92 * the per-tile bins will point to chunks of data in this structure.
94 struct data_block_list
{
95 struct data_block
*head
;
96 struct data_block
*tail
;
100 /** List of texture references */
102 struct pipe_texture
*texture
;
103 struct texture_ref
*prev
, *next
; /**< linked list w/ u_simple_list.h */
108 * All bins and bin data are contained here.
109 * Per-bin data goes into the 'tile' bins.
110 * Shared data goes into the 'data' buffer.
112 * When there are multiple threads, will want to double-buffer between
116 struct cmd_bin tile
[TILES_X
][TILES_Y
];
117 struct data_block_list data
;
119 /** the framebuffer to render the scene into */
120 struct pipe_framebuffer_state fb
;
122 /** list of textures referenced by the scene commands */
123 struct texture_ref textures
;
128 * Number of active tiles in each dimension.
129 * This basically the framebuffer size divided by tile size
131 unsigned tiles_x
, tiles_y
;
133 int curr_x
, curr_y
; /**< for iterating over bins */
139 struct lp_scene
*lp_scene_create(void);
141 void lp_scene_destroy(struct lp_scene
*scene
);
144 void lp_scene_init(struct lp_scene
*scene
);
146 boolean
lp_scene_is_empty(struct lp_scene
*scene
);
148 void lp_scene_reset(struct lp_scene
*scene
);
150 void lp_scene_free_bin_data(struct lp_scene
*scene
);
152 void lp_scene_set_framebuffer_size( struct lp_scene
*scene
,
153 unsigned width
, unsigned height
);
155 void lp_bin_new_data_block( struct data_block_list
*list
);
157 void lp_bin_new_cmd_block( struct cmd_block_list
*list
);
159 unsigned lp_scene_data_size( const struct lp_scene
*scene
);
161 unsigned lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
);
163 void lp_scene_texture_reference( struct lp_scene
*scene
,
164 struct pipe_texture
*texture
);
166 boolean
lp_scene_is_textured_referenced( const struct lp_scene
*scene
,
167 const struct pipe_texture
*texture
);
171 * Allocate space for a command/data in the bin's data buffer.
172 * Grow the block list if needed.
175 lp_scene_alloc( struct lp_scene
*scene
, unsigned size
)
177 struct data_block_list
*list
= &scene
->data
;
179 if (list
->tail
->used
+ size
> DATA_BLOCK_SIZE
) {
180 lp_bin_new_data_block( list
);
184 struct data_block
*tail
= list
->tail
;
185 ubyte
*data
= tail
->data
+ tail
->used
;
193 * As above, but with specific alignment.
196 lp_scene_alloc_aligned( struct lp_scene
*scene
, unsigned size
,
199 struct data_block_list
*list
= &scene
->data
;
201 if (list
->tail
->used
+ size
+ alignment
- 1 > DATA_BLOCK_SIZE
) {
202 lp_bin_new_data_block( list
);
206 struct data_block
*tail
= list
->tail
;
207 ubyte
*data
= tail
->data
+ tail
->used
;
208 unsigned offset
= (((uintptr_t)data
+ alignment
- 1) & ~(alignment
- 1)) - (uintptr_t)data
;
209 tail
->used
+= offset
+ size
;
210 return data
+ offset
;
215 /* Put back data if we decide not to use it, eg. culled triangles.
218 lp_scene_putback_data( struct lp_scene
*scene
, unsigned size
)
220 struct data_block_list
*list
= &scene
->data
;
221 assert(list
->tail
->used
>= size
);
222 list
->tail
->used
-= size
;
226 /** Return pointer to a particular tile's bin. */
227 static INLINE
struct cmd_bin
*
228 lp_scene_get_bin(struct lp_scene
*scene
, unsigned x
, unsigned y
)
230 return &scene
->tile
[x
][y
];
234 /** Remove all commands from a bin */
236 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
);
239 /* Add a command to bin[x][y].
242 lp_scene_bin_command( struct lp_scene
*scene
,
243 unsigned x
, unsigned y
,
245 union lp_rast_cmd_arg arg
)
247 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
248 struct cmd_block_list
*list
= &bin
->commands
;
250 assert(x
< scene
->tiles_x
);
251 assert(y
< scene
->tiles_y
);
253 if (list
->tail
->count
== CMD_BLOCK_MAX
) {
254 lp_bin_new_cmd_block( list
);
258 struct cmd_block
*tail
= list
->tail
;
259 unsigned i
= tail
->count
;
267 /* Add a command to all active bins.
270 lp_scene_bin_everywhere( struct lp_scene
*scene
,
272 const union lp_rast_cmd_arg arg
)
275 for (i
= 0; i
< scene
->tiles_x
; i
++)
276 for (j
= 0; j
< scene
->tiles_y
; j
++)
277 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
282 lp_scene_bin_state_command( struct lp_scene
*scene
,
284 const union lp_rast_cmd_arg arg
);
287 static INLINE
unsigned
288 lp_scene_get_num_bins( const struct lp_scene
*scene
)
290 return scene
->tiles_x
* scene
->tiles_y
;
295 lp_scene_bin_iter_begin( struct lp_scene
*scene
);
298 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
);
301 #endif /* LP_BIN_H */