lp_rast_priv.h revision d4b64167b56f780d0dea73193c345622888fbc16
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#ifndef LP_RAST_PRIV_H
29#define LP_RAST_PRIV_H
30
31#include "os/os_thread.h"
32#include "util/u_format.h"
33#include "gallivm/lp_bld_debug.h"
34#include "lp_memory.h"
35#include "lp_rast.h"
36#include "lp_scene.h"
37#include "lp_state.h"
38#include "lp_texture.h"
39#include "lp_tile_soa.h"
40#include "lp_limits.h"
41
42
43struct lp_rasterizer;
44
45
46/**
47 * Per-thread rasterization state
48 */
49struct lp_rasterizer_task
50{
51   unsigned x, y;          /**< Pos of this tile in framebuffer, in pixels */
52
53   uint8_t *color_tiles[PIPE_MAX_COLOR_BUFS];
54   uint8_t *depth_tile;
55
56   const struct lp_rast_state *current_state;
57
58   /** "back" pointer */
59   struct lp_rasterizer *rast;
60
61   /** "my" index */
62   unsigned thread_index;
63
64   /* occlude counter for visiable pixels */
65   uint32_t vis_counter;
66
67   pipe_semaphore work_ready;
68   pipe_semaphore work_done;
69};
70
71
72/**
73 * This is the state required while rasterizing tiles.
74 * Note that this contains per-thread information too.
75 * The tile size is TILE_SIZE x TILE_SIZE pixels.
76 */
77struct lp_rasterizer
78{
79   boolean exit_flag;
80
81   /* Framebuffer stuff
82    */
83   struct {
84      uint8_t *map;
85      unsigned stride;
86      unsigned blocksize;
87   } zsbuf;
88
89   struct {
90      unsigned nr_cbufs;
91      unsigned clear_color;
92      unsigned clear_depth;
93      char clear_stencil;
94   } state;
95
96   /** The incoming queue of scenes ready to rasterize */
97   struct lp_scene_queue *full_scenes;
98
99   /**
100    * The outgoing queue of processed scenes to return to setup module
101    *
102    * XXX: while scenes are per-context but the rasterizer is
103    * (potentially) shared, these empty scenes should be returned to
104    * the context which created them rather than retained here.
105    */
106   /*   struct lp_scene_queue *empty_scenes; */
107
108   /** The scene currently being rasterized by the threads */
109   struct lp_scene *curr_scene;
110
111   /** A task object for each rasterization thread */
112   struct lp_rasterizer_task tasks[LP_MAX_THREADS];
113
114   unsigned num_threads;
115   pipe_thread threads[LP_MAX_THREADS];
116
117   /** For synchronizing the rasterization threads */
118   pipe_barrier barrier;
119};
120
121
122void
123lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
124                         const struct lp_rast_shader_inputs *inputs,
125                         unsigned x, unsigned y,
126                         unsigned mask);
127
128
129
130/**
131 * Get the pointer to a 4x4 depth/stencil block.
132 * We'll map the z/stencil buffer on demand here.
133 * Note that this may be called even when there's no z/stencil buffer - return
134 * NULL in that case.
135 * \param x, y location of 4x4 block in window coords
136 */
137static INLINE void *
138lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task,
139                                unsigned x, unsigned y)
140{
141   const struct lp_rasterizer *rast = task->rast;
142   void *depth;
143
144   assert((x % TILE_VECTOR_WIDTH) == 0);
145   assert((y % TILE_VECTOR_HEIGHT) == 0);
146
147   if (!rast->zsbuf.map && (task->current_state->variant->key.depth.enabled ||
148                            task->current_state->variant->key.stencil[0].enabled)) {
149      /* out of memory - use dummy tile memory */
150      return lp_get_dummy_tile();
151   }
152
153   depth = (rast->zsbuf.map +
154            rast->zsbuf.stride * y +
155            rast->zsbuf.blocksize * x * TILE_VECTOR_HEIGHT);
156
157   assert(lp_check_alignment(depth, 16));
158   return depth;
159}
160
161
162/**
163 * Get pointer to the swizzled color tile
164 */
165static INLINE uint8_t *
166lp_rast_get_color_tile_pointer(struct lp_rasterizer_task *task,
167                               unsigned buf, enum lp_texture_usage usage)
168{
169   struct lp_rasterizer *rast = task->rast;
170
171   assert(task->x % TILE_SIZE == 0);
172   assert(task->y % TILE_SIZE == 0);
173   assert(buf < rast->state.nr_cbufs);
174
175   if (!task->color_tiles[buf]) {
176      struct pipe_surface *cbuf = rast->curr_scene->fb.cbufs[buf];
177      struct llvmpipe_resource *lpt;
178      assert(cbuf);
179      lpt = llvmpipe_resource(cbuf->texture);
180      task->color_tiles[buf] = llvmpipe_get_texture_tile(lpt,
181                                                         cbuf->face + cbuf->zslice,
182                                                         cbuf->level,
183                                                         usage,
184                                                         task->x,
185                                                         task->y);
186      if (!task->color_tiles[buf]) {
187         /* out of memory - use dummy tile memory */
188         return lp_get_dummy_tile();
189      }
190   }
191
192   return task->color_tiles[buf];
193}
194
195
196/**
197 * Get the pointer to a 4x4 color block (within a 64x64 tile).
198 * We'll map the color buffer on demand here.
199 * Note that this may be called even when there's no color buffers - return
200 * NULL in that case.
201 * \param x, y location of 4x4 block in window coords
202 */
203static INLINE uint8_t *
204lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task,
205                                unsigned buf, unsigned x, unsigned y)
206{
207   unsigned px, py, pixel_offset;
208   uint8_t *color;
209
210   assert((x % TILE_VECTOR_WIDTH) == 0);
211   assert((y % TILE_VECTOR_HEIGHT) == 0);
212
213   color = lp_rast_get_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
214   color = task->color_tiles[buf];
215   if (!color) {
216      /* out of memory - use dummy tile memory */
217      return lp_get_dummy_tile();
218   }
219
220   px = x % TILE_SIZE;
221   py = y % TILE_SIZE;
222   pixel_offset = tile_pixel_offset(px, py, 0);
223
224   color = color + pixel_offset;
225
226   assert(lp_check_alignment(color, 16));
227   return color;
228}
229
230
231
232/**
233 * Shade all pixels in a 4x4 block.  The fragment code omits the
234 * triangle in/out tests.
235 * \param x, y location of 4x4 block in window coords
236 */
237static INLINE void
238lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
239                         const struct lp_rast_shader_inputs *inputs,
240                         unsigned x, unsigned y )
241{
242   const struct lp_rasterizer *rast = task->rast;
243   const struct lp_rast_state *state = task->current_state;
244   struct lp_fragment_shader_variant *variant = state->variant;
245   uint8_t *color[PIPE_MAX_COLOR_BUFS];
246   void *depth;
247   unsigned i;
248
249   /* color buffer */
250   for (i = 0; i < rast->state.nr_cbufs; i++)
251      color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
252
253   depth = lp_rast_get_depth_block_pointer(task, x, y);
254
255   /* run shader on 4x4 block */
256   variant->jit_function[RAST_WHOLE]( &state->jit_context,
257                                      x, y,
258                                      inputs->facing,
259                                      inputs->a0,
260                                      inputs->dadx,
261                                      inputs->dady,
262                                      color,
263                                      depth,
264                                      0xffff,
265                                      &task->vis_counter );
266}
267
268
269#endif
270