OSDN Git Service

fa5bbcaf013a518b7c4a686524aa3bc9b3d8805d
[android-x86/external-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_rast.h"
40 #include "lp_debug.h"
41
42 struct lp_scene_queue;
43 struct lp_rast_state;
44
45 /* We're limited to 2K by 2K for 32bit fixed point rasterization.
46  * Will need a 64-bit version for larger framebuffers.
47  */
48 #define TILES_X (LP_MAX_WIDTH / TILE_SIZE)
49 #define TILES_Y (LP_MAX_HEIGHT / TILE_SIZE)
50
51
52 /* Commands per command block (ideally so sizeof(cmd_block) is a power of
53  * two in size.)
54  */
55 #define CMD_BLOCK_MAX 29
56
57 /* Bytes per data block.
58  */
59 #define DATA_BLOCK_SIZE (64 * 1024)
60
61 /* Scene temporary storage is clamped to this size:
62  */
63 #define LP_SCENE_MAX_SIZE (9*1024*1024)
64
65 /* The maximum amount of texture storage referenced by a scene is
66  * clamped ot this size:
67  */
68 #define LP_SCENE_MAX_RESOURCE_SIZE (64*1024*1024)
69
70
71 /* switch to a non-pointer value for this:
72  */
73 typedef void (*lp_rast_cmd_func)( struct lp_rasterizer_task *,
74                                   const union lp_rast_cmd_arg );
75
76    
77 struct cmd_block {
78    uint8_t cmd[CMD_BLOCK_MAX];
79    union lp_rast_cmd_arg arg[CMD_BLOCK_MAX];
80    unsigned count;
81    struct cmd_block *next;
82 };
83
84
85 struct data_block {
86    ubyte data[DATA_BLOCK_SIZE];
87    unsigned used;
88    struct data_block *next;
89 };
90
91
92
93 /**
94  * For each screen tile we have one of these bins.
95  */
96 struct cmd_bin {
97    const struct lp_rast_state *last_state;       /* most recent state set in bin */
98    struct cmd_block *head;
99    struct cmd_block *tail;
100 };
101    
102
103 /**
104  * This stores bulk data which is used for all memory allocations
105  * within a scene.
106  *
107  * Examples include triangle data and state data.  The commands in
108  * the per-tile bins will point to chunks of data in this structure.
109  *
110  * Include the first block of data statically to ensure we can always
111  * initiate a scene without relying on malloc succeeding.
112  */
113 struct data_block_list {
114    struct data_block first;
115    struct data_block *head;
116 };
117
118 struct resource_ref;
119
120 /**
121  * All bins and bin data are contained here.
122  * Per-bin data goes into the 'tile' bins.
123  * Shared data goes into the 'data' buffer.
124  *
125  * When there are multiple threads, will want to double-buffer between
126  * scenes:
127  */
128 struct lp_scene {
129    struct pipe_context *pipe;
130    struct lp_fence *fence;
131
132    /* Framebuffer mappings - valid only between begin_rasterization()
133     * and end_rasterization().
134     */
135    struct {
136       uint8_t *map;
137       unsigned stride;
138       unsigned blocksize;
139    } zsbuf, cbufs[PIPE_MAX_COLOR_BUFS];
140    
141    /** the framebuffer to render the scene into */
142    struct pipe_framebuffer_state fb;
143
144    /** list of resources referenced by the scene commands */
145    struct resource_ref *resources;
146
147    /** Total memory used by the scene (in bytes).  This sums all the
148     * data blocks and counts all bins, state, resource references and
149     * other random allocations within the scene.
150     */
151    unsigned scene_size;
152
153    /** Sum of sizes of all resources referenced by the scene.  Sums
154     * all the textures read by the scene:
155     */
156    unsigned resource_reference_size;
157
158    boolean alloc_failed;
159    boolean has_depthstencil_clear;
160    boolean discard;
161    /**
162     * Number of active tiles in each dimension.
163     * This basically the framebuffer size divided by tile size
164     */
165    unsigned tiles_x, tiles_y;
166
167    int curr_x, curr_y;  /**< for iterating over bins */
168    pipe_mutex mutex;
169
170    struct cmd_bin tile[TILES_X][TILES_Y];
171    struct data_block_list data;
172 };
173
174
175
176 struct lp_scene *lp_scene_create(struct pipe_context *pipe);
177
178 void lp_scene_destroy(struct lp_scene *scene);
179
180 boolean lp_scene_is_empty(struct lp_scene *scene );
181 boolean lp_scene_is_oom(struct lp_scene *scene );
182
183
184 struct data_block *lp_scene_new_data_block( struct lp_scene *scene );
185
186 struct cmd_block *lp_scene_new_cmd_block( struct lp_scene *scene,
187                                           struct cmd_bin *bin );
188
189 boolean lp_scene_add_resource_reference(struct lp_scene *scene,
190                                         struct pipe_resource *resource,
191                                         boolean initializing_scene);
192
193 boolean lp_scene_is_resource_referenced(const struct lp_scene *scene,
194                                         const struct pipe_resource *resource );
195
196
197 /**
198  * Allocate space for a command/data in the bin's data buffer.
199  * Grow the block list if needed.
200  */
201 static INLINE void *
202 lp_scene_alloc( struct lp_scene *scene, unsigned size)
203 {
204    struct data_block_list *list = &scene->data;
205    struct data_block *block = list->head;
206
207    assert(size <= DATA_BLOCK_SIZE);
208    assert(block != NULL);
209
210    if (LP_DEBUG & DEBUG_MEM)
211       debug_printf("alloc %u block %u/%u tot %u/%u\n",
212                    size, block->used, DATA_BLOCK_SIZE,
213                    scene->scene_size, LP_SCENE_MAX_SIZE);
214
215    if (block->used + size > DATA_BLOCK_SIZE) {
216       block = lp_scene_new_data_block( scene );
217       if (!block) {
218          /* out of memory */
219          return NULL;
220       }
221    }
222
223    {
224       ubyte *data = block->data + block->used;
225       block->used += size;
226       return data;
227    }
228 }
229
230
231 /**
232  * As above, but with specific alignment.
233  */
234 static INLINE void *
235 lp_scene_alloc_aligned( struct lp_scene *scene, unsigned size,
236                         unsigned alignment )
237 {
238    struct data_block_list *list = &scene->data;
239    struct data_block *block = list->head;
240
241    assert(block != NULL);
242
243    if (LP_DEBUG & DEBUG_MEM)
244       debug_printf("alloc %u block %u/%u tot %u/%u\n",
245                    size + alignment - 1,
246                    block->used, DATA_BLOCK_SIZE,
247                    scene->scene_size, LP_SCENE_MAX_SIZE);
248        
249    if (block->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
250       block = lp_scene_new_data_block( scene );
251       if (!block)
252          return NULL;
253    }
254
255    {
256       ubyte *data = block->data + block->used;
257       unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
258       block->used += offset + size;
259       return data + offset;
260    }
261 }
262
263
264 /* Put back data if we decide not to use it, eg. culled triangles.
265  */
266 static INLINE void
267 lp_scene_putback_data( struct lp_scene *scene, unsigned size)
268 {
269    struct data_block_list *list = &scene->data;
270    assert(list->head && list->head->used >= size);
271    list->head->used -= size;
272 }
273
274
275 /** Return pointer to a particular tile's bin. */
276 static INLINE struct cmd_bin *
277 lp_scene_get_bin(struct lp_scene *scene, unsigned x, unsigned y)
278 {
279    return &scene->tile[x][y];
280 }
281
282
283 /** Remove all commands from a bin */
284 void
285 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y);
286
287
288 /* Add a command to bin[x][y].
289  */
290 static INLINE boolean
291 lp_scene_bin_command( struct lp_scene *scene,
292                       unsigned x, unsigned y,
293                       unsigned cmd,
294                       union lp_rast_cmd_arg arg )
295 {
296    struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
297    struct cmd_block *tail = bin->tail;
298
299    assert(x < scene->tiles_x);
300    assert(y < scene->tiles_y);
301    assert(cmd < LP_RAST_OP_MAX);
302
303    if (tail == NULL || tail->count == CMD_BLOCK_MAX) {
304       tail = lp_scene_new_cmd_block( scene, bin );
305       if (!tail) {
306          return FALSE;
307       }
308       assert(tail->count == 0);
309    }
310
311    {
312       unsigned i = tail->count;
313       tail->cmd[i] = cmd & LP_RAST_OP_MASK;
314       tail->arg[i] = arg;
315       tail->count++;
316    }
317    
318    return TRUE;
319 }
320
321
322 static INLINE boolean
323 lp_scene_bin_cmd_with_state( struct lp_scene *scene,
324                              unsigned x, unsigned y,
325                              const struct lp_rast_state *state,
326                              unsigned cmd,
327                              union lp_rast_cmd_arg arg )
328 {
329    struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
330
331    if (state != bin->last_state) {
332       bin->last_state = state;
333       if (!lp_scene_bin_command(scene, x, y,
334                                 LP_RAST_OP_SET_STATE,
335                                 lp_rast_arg_state(state)))
336          return FALSE;
337    }
338
339    if (!lp_scene_bin_command( scene, x, y, cmd, arg ))
340       return FALSE;
341
342    return TRUE;
343 }
344
345
346 /* Add a command to all active bins.
347  */
348 static INLINE boolean
349 lp_scene_bin_everywhere( struct lp_scene *scene,
350                          unsigned cmd,
351                          const union lp_rast_cmd_arg arg )
352 {
353    unsigned i, j;
354    for (i = 0; i < scene->tiles_x; i++) {
355       for (j = 0; j < scene->tiles_y; j++) {
356          if (!lp_scene_bin_command( scene, i, j, cmd, arg ))
357             return FALSE;
358       }
359    }
360
361    return TRUE;
362 }
363
364
365 static INLINE unsigned
366 lp_scene_get_num_bins( const struct lp_scene *scene )
367 {
368    return scene->tiles_x * scene->tiles_y;
369 }
370
371
372 void
373 lp_scene_bin_iter_begin( struct lp_scene *scene );
374
375 struct cmd_bin *
376 lp_scene_bin_iter_next( struct lp_scene *scene, int *x, int *y );
377
378
379
380 /* Begin/end binning of a scene
381  */
382 void
383 lp_scene_begin_binning( struct lp_scene *scene,
384                         struct pipe_framebuffer_state *fb,
385                         boolean discard );
386
387 void
388 lp_scene_end_binning( struct lp_scene *scene );
389
390
391 /* Begin/end rasterization of a scene
392  */
393 void
394 lp_scene_begin_rasterization(struct lp_scene *scene);
395
396 void
397 lp_scene_end_rasterization(struct lp_scene *scene );
398
399
400
401
402
403 #endif /* LP_BIN_H */