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