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 "util/u_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. This effectively limits the maximum constant buffer
58 * size.
59 */
60 #define DATA_BLOCK_SIZE (64 * 1024)
61
62 /* Scene temporary storage is clamped to this size:
63 */
64 #define LP_SCENE_MAX_SIZE (36*1024*1024)
65
66 /* The maximum amount of texture storage referenced by a scene is
67 * clamped to this size:
68 */
69 #define LP_SCENE_MAX_RESOURCE_SIZE (64*1024*1024)
70
71
72 /* switch to a non-pointer value for this:
73 */
74 typedef void (*lp_rast_cmd_func)(struct lp_rasterizer_task *,
75 const union lp_rast_cmd_arg);
76
77
78 struct cmd_block {
79 uint8_t cmd[CMD_BLOCK_MAX]; // LP_RAST_OP_x
80 union lp_rast_cmd_arg arg[CMD_BLOCK_MAX];
81 unsigned count;
82 struct cmd_block *next;
83 };
84
85
86 struct data_block {
87 uint8_t data[DATA_BLOCK_SIZE];
88 unsigned used;
89 struct data_block *next;
90 };
91
92
93
94 /**
95 * For each screen tile we have one of these bins.
96 */
97 struct cmd_bin {
98 const struct lp_rast_state *last_state; /* most recent state set in bin */
99 struct cmd_block *head;
100 struct cmd_block *tail;
101 };
102
103
104 /**
105 * This stores bulk data which is used for all memory allocations
106 * within a scene.
107 *
108 * Examples include triangle data and state data. The commands in
109 * the per-tile bins will point to chunks of data in this structure.
110 *
111 * Include the first block of data statically to ensure we can always
112 * initiate a scene without relying on malloc succeeding.
113 */
114 struct data_block_list {
115 struct data_block first;
116 struct data_block *head;
117 };
118
119 struct resource_ref;
120
121 struct shader_ref;
122
123 struct lp_scene_surface {
124 uint8_t *map;
125 unsigned stride;
126 unsigned layer_stride;
127 unsigned format_bytes;
128 unsigned sample_stride;
129 unsigned nr_samples;
130 unsigned base_layer;
131 unsigned layer_count;
132 };
133
134
135 /**
136 * All bins and bin data are contained here.
137 * Per-bin data goes into the 'tile' bins.
138 * Shared data goes into the 'data' buffer.
139 *
140 * When there are multiple threads, will want to double-buffer between
141 * scenes:
142 */
143 struct lp_scene {
144 struct pipe_context *pipe;
145 struct lp_fence *fence;
146 struct lp_setup_context *setup;
147
148 /* The queries still active at end of scene */
149 struct llvmpipe_query *active_queries[LP_MAX_ACTIVE_BINNED_QUERIES];
150 unsigned num_active_queries;
151 /* If queries were either active or there were begin/end query commands */
152 bool had_queries;
153
154 /* Framebuffer mappings - valid only between begin_rasterization()
155 * and end_rasterization().
156 */
157 struct lp_scene_surface zsbuf, cbufs[PIPE_MAX_COLOR_BUFS];
158
159 /* The amount of layers in the fb (minimum of all attachments) */
160 unsigned fb_max_layer;
161
162 /* fixed point sample positions. */
163 int32_t fixed_sample_pos[LP_MAX_SAMPLES][2];
164
165 /* max samples for bound framebuffer */
166 unsigned fb_max_samples;
167
168 /** the framebuffer to render the scene into */
169 struct pipe_framebuffer_state fb;
170
171 /** list of resources referenced by the scene commands */
172 struct resource_ref *resources;
173
174 /** list of writable resources referenced by the scene commands */
175 struct resource_ref *writeable_resources;
176
177 /** list of frag shaders referenced by the scene commands */
178 struct shader_ref *frag_shaders;
179
180 /** Total memory used by the scene (in bytes). This sums all the
181 * data blocks and counts all bins, state, resource references and
182 * other random allocations within the scene.
183 */
184 unsigned scene_size;
185
186 /** Sum of sizes of all resources referenced by the scene. Sums
187 * all the textures read by the scene:
188 */
189 unsigned resource_reference_size;
190
191 bool alloc_failed;
192 bool permit_linear_rasterizer;
193
194 /**
195 * Number of active tiles in each dimension.
196 * This basically the framebuffer size divided by tile size
197 */
198 unsigned tiles_x, tiles_y;
199
200 int curr_x, curr_y; /**< for iterating over bins */
201 mtx_t mutex;
202
203 unsigned num_alloced_tiles;
204 struct cmd_bin *tiles;
205 struct data_block_list data;
206 };
207
208
209
210 struct lp_scene *lp_scene_create(struct lp_setup_context *setup);
211
212 void lp_scene_destroy(struct lp_scene *scene);
213
214 bool lp_scene_is_empty(struct lp_scene *scene);
215
216 bool lp_scene_is_oom(struct lp_scene *scene);
217
218 struct data_block *lp_scene_new_data_block(struct lp_scene *scene);
219
220 struct cmd_block *lp_scene_new_cmd_block(struct lp_scene *scene,
221 struct cmd_bin *bin);
222
223 bool lp_scene_add_resource_reference(struct lp_scene *scene,
224 struct pipe_resource *resource,
225 bool initializing_scene,
226 bool writeable);
227
228 unsigned lp_scene_is_resource_referenced(const struct lp_scene *scene,
229 const struct pipe_resource *resource);
230
231 bool lp_scene_add_frag_shader_reference(struct lp_scene *scene,
232 struct lp_fragment_shader_variant *variant);
233
234
235
236 /**
237 * Allocate space for a command/data in the bin's data buffer.
238 * Grow the block list if needed.
239 */
240 static inline void *
lp_scene_alloc(struct lp_scene * scene,unsigned size)241 lp_scene_alloc(struct lp_scene *scene, unsigned size)
242 {
243 struct data_block_list *list = &scene->data;
244 struct data_block *block = list->head;
245
246 assert(size <= DATA_BLOCK_SIZE);
247 assert(block != NULL);
248
249 if (LP_DEBUG & DEBUG_MEM)
250 debug_printf("alloc %u block %u/%u tot %u/%u\n",
251 size, block->used, (unsigned)DATA_BLOCK_SIZE,
252 scene->scene_size, LP_SCENE_MAX_SIZE);
253
254 if (block->used + size > DATA_BLOCK_SIZE) {
255 block = lp_scene_new_data_block(scene);
256 if (!block) {
257 /* out of memory */
258 return NULL;
259 }
260 }
261
262 {
263 uint8_t *data = block->data + block->used;
264 block->used += size;
265 return data;
266 }
267 }
268
269
270 /**
271 * As above, but with specific alignment.
272 */
273 static inline void *
lp_scene_alloc_aligned(struct lp_scene * scene,unsigned size,unsigned alignment)274 lp_scene_alloc_aligned(struct lp_scene *scene, unsigned size,
275 unsigned alignment)
276 {
277 struct data_block_list *list = &scene->data;
278 struct data_block *block = list->head;
279
280 assert(block != NULL);
281
282 if (LP_DEBUG & DEBUG_MEM)
283 debug_printf("alloc %u block %u/%u tot %u/%u\n",
284 size + alignment - 1,
285 block->used, (unsigned)DATA_BLOCK_SIZE,
286 scene->scene_size, LP_SCENE_MAX_SIZE);
287
288 if (block->used + size + alignment - 1 > DATA_BLOCK_SIZE) {
289 block = lp_scene_new_data_block(scene);
290 if (!block)
291 return NULL;
292 }
293
294 {
295 uint8_t *data = block->data + block->used;
296 unsigned offset = (((uintptr_t)data + alignment - 1) & ~(alignment - 1)) - (uintptr_t)data;
297 block->used += offset + size;
298 return data + offset;
299 }
300 }
301
302
303 /** Return pointer to a particular tile's bin. */
304 static inline struct cmd_bin *
lp_scene_get_bin(struct lp_scene * scene,unsigned x,unsigned y)305 lp_scene_get_bin(struct lp_scene *scene, unsigned x, unsigned y)
306 {
307 unsigned idx = scene->tiles_x * y + x;
308 return &scene->tiles[idx];
309 }
310
311
312 /** Remove all commands from a bin */
313 void
314 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y);
315
316
317 /* Add a command to bin[x][y].
318 */
319 static inline bool
lp_scene_bin_command(struct lp_scene * scene,unsigned x,unsigned y,enum lp_rast_op cmd,union lp_rast_cmd_arg arg)320 lp_scene_bin_command(struct lp_scene *scene,
321 unsigned x, unsigned y,
322 enum lp_rast_op cmd,
323 union lp_rast_cmd_arg arg)
324 {
325 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
326 struct cmd_block *tail = bin->tail;
327
328 assert(x < scene->tiles_x);
329 assert(y < scene->tiles_y);
330 assert(cmd < LP_RAST_OP_MAX);
331
332 if (tail == NULL || tail->count == CMD_BLOCK_MAX) {
333 tail = lp_scene_new_cmd_block(scene, bin);
334 if (!tail) {
335 return false;
336 }
337 assert(tail->count == 0);
338 }
339
340 {
341 unsigned i = tail->count;
342 tail->cmd[i] = cmd & LP_RAST_OP_MASK;
343 tail->arg[i] = arg;
344 tail->count++;
345 }
346
347 return true;
348 }
349
350
351 static inline bool
lp_scene_bin_cmd_with_state(struct lp_scene * scene,unsigned x,unsigned y,const struct lp_rast_state * state,enum lp_rast_op cmd,union lp_rast_cmd_arg arg)352 lp_scene_bin_cmd_with_state(struct lp_scene *scene,
353 unsigned x, unsigned y,
354 const struct lp_rast_state *state,
355 enum lp_rast_op cmd,
356 union lp_rast_cmd_arg arg)
357 {
358 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
359
360 if (state != bin->last_state) {
361 bin->last_state = state;
362 if (!lp_scene_bin_command(scene, x, y,
363 LP_RAST_OP_SET_STATE,
364 lp_rast_arg_state(state)))
365 return false;
366 }
367
368 if (!lp_scene_bin_command(scene, x, y, cmd, arg))
369 return false;
370
371 return true;
372 }
373
374
375 /* Add a command to all active bins.
376 */
377 static inline bool
lp_scene_bin_everywhere(struct lp_scene * scene,enum lp_rast_op cmd,const union lp_rast_cmd_arg arg)378 lp_scene_bin_everywhere(struct lp_scene *scene,
379 enum lp_rast_op cmd,
380 const union lp_rast_cmd_arg arg)
381 {
382 for (unsigned i = 0; i < scene->tiles_x; i++) {
383 for (unsigned j = 0; j < scene->tiles_y; j++) {
384 if (!lp_scene_bin_command(scene, i, j, cmd, arg))
385 return false;
386 }
387 }
388
389 return true;
390 }
391
392
393 static inline unsigned
lp_scene_get_num_bins(const struct lp_scene * scene)394 lp_scene_get_num_bins(const struct lp_scene *scene)
395 {
396 return scene->tiles_x * scene->tiles_y;
397 }
398
399
400 void
401 lp_scene_bin_iter_begin(struct lp_scene *scene);
402
403 struct cmd_bin *
404 lp_scene_bin_iter_next(struct lp_scene *scene, int *x, int *y);
405
406
407
408 /* Begin/end binning of a scene
409 */
410 void
411 lp_scene_begin_binning(struct lp_scene *scene,
412 struct pipe_framebuffer_state *fb);
413
414 void
415 lp_scene_end_binning(struct lp_scene *scene);
416
417
418 /* Begin/end rasterization of a scene
419 */
420 void
421 lp_scene_begin_rasterization(struct lp_scene *scene);
422
423 void
424 lp_scene_end_rasterization(struct lp_scene *scene);
425
426
427 #endif /* LP_SCENE_H */
428