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