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