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