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