llvmpipe: checkpoint: more thread/queuing changes
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bin.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 bins while
32 * The "rast" code is concerned with consuming/executing bins.
33 */
34
35 #ifndef LP_BIN_H
36 #define LP_BIN_H
37
38 #include "pipe/p_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 *,
60 unsigned thread_index,
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.
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 /**
101 * All bins and bin data are contained here.
102 * Per-bin data goes into the 'tile' bins.
103 * Shared bin data goes into the 'data' buffer.
104 * When there are multiple threads, will want to double-buffer the
105 * bin arrays:
106 */
107 struct lp_bins {
108 struct cmd_bin tile[TILES_X][TILES_Y];
109 struct data_block_list data;
110
111 /** the framebuffer to render the bins into */
112 struct pipe_framebuffer_state fb;
113
114 boolean write_depth;
115
116 /**
117 * Number of active tiles in each dimension.
118 * This basically the framebuffer size divided by tile size
119 */
120 unsigned tiles_x, tiles_y;
121
122 int curr_x, curr_y; /**< for iterating over bins */
123 pipe_mutex mutex;
124 };
125
126
127
128 struct lp_bins *lp_bins_create(void);
129
130 void lp_bins_destroy(struct lp_bins *bins);
131
132
133 void lp_init_bins(struct lp_bins *bins);
134
135 void lp_reset_bins(struct lp_bins *bins );
136
137 void lp_free_bin_data(struct lp_bins *bins);
138
139 void lp_bin_set_framebuffer_size( struct lp_bins *bins,
140 unsigned width, unsigned height );
141
142 void lp_bin_new_data_block( struct data_block_list *list );
143
144 void lp_bin_new_cmd_block( struct cmd_block_list *list );
145
146 unsigned lp_bin_data_size( const struct lp_bins *bins );
147
148 unsigned lp_bin_cmd_size( const struct lp_bins *bins, unsigned x, unsigned y );
149
150
151 /**
152 * Allocate space for a command/data in the bin's data buffer.
153 * Grow the block list if needed.
154 */
155 static INLINE void *
156 lp_bin_alloc( struct lp_bins *bins, unsigned size)
157 {
158 struct data_block_list *list = &bins->data;
159
160 if (list->tail->used + size > DATA_BLOCK_SIZE) {
161 lp_bin_new_data_block( list );
162 }
163
164 {
165 struct data_block *tail = list->tail;
166 ubyte *data = tail->data + tail->used;
167 tail->used += size;
168 return data;
169 }
170 }
171
172
173 /**
174 * As above, but with specific alignment.
175 */
176 static INLINE void *
177 lp_bin_alloc_aligned( struct lp_bins *bins, unsigned size,
178 unsigned alignment )
179 {
180 struct data_block_list *list = &bins->data;
181
182 if (list->tail->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
183 lp_bin_new_data_block( list );
184 }
185
186 {
187 struct data_block *tail = list->tail;
188 ubyte *data = tail->data + tail->used;
189 unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
190 tail->used += offset + size;
191 return data + offset;
192 }
193 }
194
195
196 /* Put back data if we decide not to use it, eg. culled triangles.
197 */
198 static INLINE void
199 lp_bin_putback_data( struct lp_bins *bins, unsigned size)
200 {
201 struct data_block_list *list = &bins->data;
202 assert(list->tail->used >= size);
203 list->tail->used -= size;
204 }
205
206
207 /** Return pointer to a particular tile's bin. */
208 static INLINE struct cmd_bin *
209 lp_get_bin(struct lp_bins *bins, unsigned x, unsigned y)
210 {
211 return &bins->tile[x][y];
212 }
213
214
215
216 /* Add a command to bin[x][y].
217 */
218 static INLINE void
219 lp_bin_command( struct lp_bins *bins,
220 unsigned x, unsigned y,
221 lp_rast_cmd cmd,
222 union lp_rast_cmd_arg arg )
223 {
224 struct cmd_bin *bin = lp_get_bin(bins, x, y);
225 struct cmd_block_list *list = &bin->commands;
226
227 if (list->tail->count == CMD_BLOCK_MAX) {
228 lp_bin_new_cmd_block( list );
229 }
230
231 {
232 struct cmd_block *tail = list->tail;
233 unsigned i = tail->count;
234 tail->cmd[i] = cmd;
235 tail->arg[i] = arg;
236 tail->count++;
237 }
238 }
239
240
241 /* Add a command to all active bins.
242 */
243 static INLINE void
244 lp_bin_everywhere( struct lp_bins *bins,
245 lp_rast_cmd cmd,
246 const union lp_rast_cmd_arg arg )
247 {
248 unsigned i, j;
249 for (i = 0; i < bins->tiles_x; i++)
250 for (j = 0; j < bins->tiles_y; j++)
251 lp_bin_command( bins, i, j, cmd, arg );
252 }
253
254
255 void
256 lp_bin_state_command( struct lp_bins *bins,
257 lp_rast_cmd cmd,
258 const union lp_rast_cmd_arg arg );
259
260
261 void
262 lp_bin_iter_begin( struct lp_bins *bins );
263
264 struct cmd_bin *
265 lp_bin_iter_next( struct lp_bins *bins, int *bin_x, int *bin_y );
266
267
268 #endif /* LP_BIN_H */