Merge the lp-surface-tiling branch into master.
[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
42 struct lp_scene_queue;
43
44 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
45 * Will need a 64-bit version for larger framebuffers.
46 */
47 #define MAXHEIGHT 2048
48 #define MAXWIDTH 2048
49 #define TILES_X (MAXWIDTH / TILE_SIZE)
50 #define TILES_Y (MAXHEIGHT / TILE_SIZE)
51
52
53 #define CMD_BLOCK_MAX 128
54 #define DATA_BLOCK_SIZE (16 * 1024 - sizeof(unsigned) - sizeof(void *))
55
56
57
58 /* switch to a non-pointer value for this:
59 */
60 typedef void (*lp_rast_cmd)( struct lp_rasterizer_task *,
61 const union lp_rast_cmd_arg );
62
63 struct cmd_block {
64 lp_rast_cmd cmd[CMD_BLOCK_MAX];
65 union lp_rast_cmd_arg arg[CMD_BLOCK_MAX];
66 unsigned count;
67 struct cmd_block *next;
68 };
69
70 struct data_block {
71 ubyte data[DATA_BLOCK_SIZE];
72 unsigned used;
73 struct data_block *next;
74 };
75
76 struct cmd_block_list {
77 struct cmd_block *head;
78 struct cmd_block *tail;
79 };
80
81 /**
82 * For each screen tile we have one of these bins.
83 */
84 struct cmd_bin {
85 struct cmd_block_list commands;
86 };
87
88
89 /**
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.
93 */
94 struct data_block_list {
95 struct data_block *head;
96 struct data_block *tail;
97 };
98
99
100 /** List of texture references */
101 struct texture_ref {
102 struct pipe_resource *texture;
103 struct texture_ref *prev, *next; /**< linked list w/ u_simple_list.h */
104 };
105
106
107 /**
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.
111 *
112 * When there are multiple threads, will want to double-buffer between
113 * scenes:
114 */
115 struct lp_scene {
116 struct pipe_context *pipe;
117
118 /** the framebuffer to render the scene into */
119 struct pipe_framebuffer_state fb;
120
121 /** list of textures referenced by the scene commands */
122 struct texture_ref textures;
123
124 boolean write_depth;
125 boolean has_color_clear;
126 boolean has_depth_clear;
127
128 /**
129 * Number of active tiles in each dimension.
130 * This basically the framebuffer size divided by tile size
131 */
132 unsigned tiles_x, tiles_y;
133
134 int curr_x, curr_y; /**< for iterating over bins */
135 pipe_mutex mutex;
136
137 /* Where to place this scene once it has been rasterized:
138 */
139 struct lp_scene_queue *empty_queue;
140
141 struct cmd_bin tile[TILES_X][TILES_Y];
142 struct data_block_list data;
143 };
144
145
146
147 struct lp_scene *lp_scene_create(struct pipe_context *pipe,
148 struct lp_scene_queue *empty_queue);
149
150 void lp_scene_destroy(struct lp_scene *scene);
151
152
153
154 boolean lp_scene_is_empty(struct lp_scene *scene );
155
156 void lp_scene_reset(struct lp_scene *scene );
157
158
159 void lp_bin_new_data_block( struct data_block_list *list );
160
161 void lp_bin_new_cmd_block( struct cmd_block_list *list );
162
163 unsigned lp_scene_data_size( const struct lp_scene *scene );
164
165 unsigned lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y );
166
167 void lp_scene_texture_reference( struct lp_scene *scene,
168 struct pipe_resource *texture );
169
170 boolean lp_scene_is_resource_referenced( const struct lp_scene *scene,
171 const struct pipe_resource *texture );
172
173
174 /**
175 * Allocate space for a command/data in the bin's data buffer.
176 * Grow the block list if needed.
177 */
178 static INLINE void *
179 lp_scene_alloc( struct lp_scene *scene, unsigned size)
180 {
181 struct data_block_list *list = &scene->data;
182
183 if (list->tail->used + size > DATA_BLOCK_SIZE) {
184 lp_bin_new_data_block( list );
185 }
186
187 {
188 struct data_block *tail = list->tail;
189 ubyte *data = tail->data + tail->used;
190 tail->used += size;
191 return data;
192 }
193 }
194
195
196 /**
197 * As above, but with specific alignment.
198 */
199 static INLINE void *
200 lp_scene_alloc_aligned( struct lp_scene *scene, unsigned size,
201 unsigned alignment )
202 {
203 struct data_block_list *list = &scene->data;
204
205 if (list->tail->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
206 lp_bin_new_data_block( list );
207 }
208
209 {
210 struct data_block *tail = list->tail;
211 ubyte *data = tail->data + tail->used;
212 unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
213 tail->used += offset + size;
214 return data + offset;
215 }
216 }
217
218
219 /* Put back data if we decide not to use it, eg. culled triangles.
220 */
221 static INLINE void
222 lp_scene_putback_data( struct lp_scene *scene, unsigned size)
223 {
224 struct data_block_list *list = &scene->data;
225 assert(list->tail->used >= size);
226 list->tail->used -= size;
227 }
228
229
230 /** Return pointer to a particular tile's bin. */
231 static INLINE struct cmd_bin *
232 lp_scene_get_bin(struct lp_scene *scene, unsigned x, unsigned y)
233 {
234 return &scene->tile[x][y];
235 }
236
237
238 /** Remove all commands from a bin */
239 void
240 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y);
241
242
243 /* Add a command to bin[x][y].
244 */
245 static INLINE void
246 lp_scene_bin_command( struct lp_scene *scene,
247 unsigned x, unsigned y,
248 lp_rast_cmd cmd,
249 union lp_rast_cmd_arg arg )
250 {
251 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
252 struct cmd_block_list *list = &bin->commands;
253
254 assert(x < scene->tiles_x);
255 assert(y < scene->tiles_y);
256
257 if (list->tail->count == CMD_BLOCK_MAX) {
258 lp_bin_new_cmd_block( list );
259 }
260
261 {
262 struct cmd_block *tail = list->tail;
263 unsigned i = tail->count;
264 tail->cmd[i] = cmd;
265 tail->arg[i] = arg;
266 tail->count++;
267 }
268 }
269
270
271 /* Add a command to all active bins.
272 */
273 static INLINE void
274 lp_scene_bin_everywhere( struct lp_scene *scene,
275 lp_rast_cmd cmd,
276 const union lp_rast_cmd_arg arg )
277 {
278 unsigned i, j;
279 for (i = 0; i < scene->tiles_x; i++)
280 for (j = 0; j < scene->tiles_y; j++)
281 lp_scene_bin_command( scene, i, j, cmd, arg );
282 }
283
284
285 void
286 lp_scene_bin_state_command( struct lp_scene *scene,
287 lp_rast_cmd cmd,
288 const union lp_rast_cmd_arg arg );
289
290
291 static INLINE unsigned
292 lp_scene_get_num_bins( const struct lp_scene *scene )
293 {
294 return scene->tiles_x * scene->tiles_y;
295 }
296
297
298 void
299 lp_scene_bin_iter_begin( struct lp_scene *scene );
300
301 struct cmd_bin *
302 lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y );
303
304
305 void
306 lp_scene_rasterize( struct lp_scene *scene,
307 struct lp_rasterizer *rast,
308 boolean write_depth );
309
310 void
311 lp_scene_begin_binning( struct lp_scene *scene,
312 struct pipe_framebuffer_state *fb );
313
314 #endif /* LP_BIN_H */