llvmpipe: get rid of depth swizzling.
[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_image.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 struct lp_rasterizer_task;
49 extern int jit_line;
50 extern const struct lp_rast_state *jit_state;
51 extern const struct lp_rasterizer_task *jit_task;
52
53 #define BEGIN_JIT_CALL(state, task) \
54 do { \
55 jit_line = __LINE__; \
56 jit_state = state; \
57 jit_task = task; \
58 } while (0)
59
60 #define END_JIT_CALL() \
61 do { \
62 jit_line = 0; \
63 jit_state = NULL; \
64 } while (0)
65
66 #else
67
68 #define BEGIN_JIT_CALL(X, Y)
69 #define END_JIT_CALL()
70
71 #endif
72
73
74 struct lp_rasterizer;
75 struct cmd_bin;
76
77 /**
78 * Per-thread rasterization state
79 */
80 struct lp_rasterizer_task
81 {
82 const struct cmd_bin *bin;
83 const struct lp_rast_state *state;
84
85 struct lp_scene *scene;
86 unsigned x, y; /**< Pos of this tile in framebuffer, in pixels */
87
88 uint8_t *color_tiles[PIPE_MAX_COLOR_BUFS];
89 uint8_t *depth_tile;
90
91 /** "back" pointer */
92 struct lp_rasterizer *rast;
93
94 /** "my" index */
95 unsigned thread_index;
96
97 /* occlude counter for visible pixels */
98 struct lp_jit_thread_data thread_data;
99 uint64_t query_start;
100 struct llvmpipe_query *query[PIPE_QUERY_TYPES];
101
102 pipe_semaphore work_ready;
103 pipe_semaphore work_done;
104 };
105
106
107 /**
108 * This is the state required while rasterizing tiles.
109 * Note that this contains per-thread information too.
110 * The tile size is TILE_SIZE x TILE_SIZE pixels.
111 */
112 struct lp_rasterizer
113 {
114 boolean exit_flag;
115 boolean no_rast; /**< For debugging/profiling */
116
117 /** The incoming queue of scenes ready to rasterize */
118 struct lp_scene_queue *full_scenes;
119
120 /** The scene currently being rasterized by the threads */
121 struct lp_scene *curr_scene;
122
123 /** A task object for each rasterization thread */
124 struct lp_rasterizer_task tasks[LP_MAX_THREADS];
125
126 unsigned num_threads;
127 pipe_thread threads[LP_MAX_THREADS];
128
129 /** For synchronizing the rasterization threads */
130 pipe_barrier barrier;
131 };
132
133
134 void
135 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
136 const struct lp_rast_shader_inputs *inputs,
137 unsigned x, unsigned y,
138 unsigned mask);
139
140
141
142 /**
143 * Get pointer to the unswizzled color tile
144 */
145 static INLINE uint8_t *
146 lp_rast_get_unswizzled_color_tile_pointer(struct lp_rasterizer_task *task,
147 unsigned buf, enum lp_texture_usage usage)
148 {
149 const struct lp_scene *scene = task->scene;
150 unsigned format_bytes;
151
152 assert(task->x < scene->tiles_x * TILE_SIZE);
153 assert(task->y < scene->tiles_y * TILE_SIZE);
154 assert(task->x % TILE_SIZE == 0);
155 assert(task->y % TILE_SIZE == 0);
156 assert(buf < scene->fb.nr_cbufs);
157
158 if (!task->color_tiles[buf]) {
159 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
160 assert(cbuf);
161
162 format_bytes = util_format_get_blocksize(cbuf->format);
163 task->color_tiles[buf] = scene->cbufs[buf].map + scene->cbufs[buf].stride * task->y + format_bytes * task->x;
164 }
165
166 return task->color_tiles[buf];
167 }
168
169
170 /**
171 * Get pointer to the unswizzled depth tile
172 */
173 static INLINE uint8_t *
174 lp_rast_get_unswizzled_depth_tile_pointer(struct lp_rasterizer_task *task,
175 enum lp_texture_usage usage)
176 {
177 const struct lp_scene *scene = task->scene;
178 unsigned format_bytes;
179
180 assert(task->x < scene->tiles_x * TILE_SIZE);
181 assert(task->y < scene->tiles_y * TILE_SIZE);
182 assert(task->x % TILE_SIZE == 0);
183 assert(task->y % TILE_SIZE == 0);
184
185 if (!task->depth_tile) {
186 struct pipe_surface *dbuf = scene->fb.zsbuf;
187 assert(dbuf);
188
189 format_bytes = util_format_get_blocksize(dbuf->format);
190 task->depth_tile = scene->zsbuf.map + scene->zsbuf.stride * task->y + format_bytes * task->x;
191 }
192
193 return task->depth_tile;
194 }
195
196
197 /**
198 * Get the pointer to an unswizzled 4x4 color block (within an unswizzled 64x64 tile).
199 * \param x, y location of 4x4 block in window coords
200 */
201 static INLINE uint8_t *
202 lp_rast_get_unswizzled_color_block_pointer(struct lp_rasterizer_task *task,
203 unsigned buf, unsigned x, unsigned y)
204 {
205 unsigned px, py, pixel_offset, format_bytes;
206 uint8_t *color;
207
208 assert(x < task->scene->tiles_x * TILE_SIZE);
209 assert(y < task->scene->tiles_y * TILE_SIZE);
210 assert((x % TILE_VECTOR_WIDTH) == 0);
211 assert((y % TILE_VECTOR_HEIGHT) == 0);
212 assert(buf < task->scene->fb.nr_cbufs);
213
214 format_bytes = util_format_get_blocksize(task->scene->fb.cbufs[buf]->format);
215
216 color = lp_rast_get_unswizzled_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
217 assert(color);
218
219 px = x % TILE_SIZE;
220 py = y % TILE_SIZE;
221 pixel_offset = px * format_bytes + py * task->scene->cbufs[buf].stride;
222
223 color = color + pixel_offset;
224
225 assert(lp_check_alignment(color, llvmpipe_get_format_alignment(task->scene->fb.cbufs[buf]->format)));
226 return color;
227 }
228
229
230 /**
231 * Get the pointer to an unswizzled 4x4 depth block (within an unswizzled 64x64 tile).
232 * \param x, y location of 4x4 block in window coords
233 */
234 static INLINE uint8_t *
235 lp_rast_get_unswizzled_depth_block_pointer(struct lp_rasterizer_task *task,
236 unsigned x, unsigned y)
237 {
238 unsigned px, py, pixel_offset, format_bytes;
239 uint8_t *depth;
240
241 assert(x < task->scene->tiles_x * TILE_SIZE);
242 assert(y < task->scene->tiles_y * TILE_SIZE);
243 assert((x % TILE_VECTOR_WIDTH) == 0);
244 assert((y % TILE_VECTOR_HEIGHT) == 0);
245
246 format_bytes = util_format_get_blocksize(task->scene->fb.zsbuf->format);
247
248 depth = lp_rast_get_unswizzled_depth_tile_pointer(task, LP_TEX_USAGE_READ_WRITE);
249 assert(depth);
250
251 px = x % TILE_SIZE;
252 py = y % TILE_SIZE;
253 pixel_offset = px * format_bytes + py * task->scene->zsbuf.stride;
254
255 depth = depth + pixel_offset;
256
257 assert(lp_check_alignment(depth, llvmpipe_get_format_alignment(task->scene->fb.zsbuf->format)));
258 return depth;
259 }
260
261
262
263 /**
264 * Shade all pixels in a 4x4 block. The fragment code omits the
265 * triangle in/out tests.
266 * \param x, y location of 4x4 block in window coords
267 */
268 static INLINE void
269 lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
270 const struct lp_rast_shader_inputs *inputs,
271 unsigned x, unsigned y )
272 {
273 const struct lp_scene *scene = task->scene;
274 const struct lp_rast_state *state = task->state;
275 struct lp_fragment_shader_variant *variant = state->variant;
276 uint8_t *color[PIPE_MAX_COLOR_BUFS];
277 unsigned stride[PIPE_MAX_COLOR_BUFS];
278 void *depth = NULL;
279 unsigned depth_stride = 0;
280 unsigned i;
281
282 /* color buffer */
283 for (i = 0; i < scene->fb.nr_cbufs; i++) {
284 stride[i] = scene->cbufs[i].stride;
285
286 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, x, y);
287 }
288
289 if (scene->zsbuf.map) {
290 depth = lp_rast_get_unswizzled_depth_block_pointer(task, x, y);
291 depth_stride = scene->zsbuf.stride;
292 }
293
294 /* run shader on 4x4 block */
295 BEGIN_JIT_CALL(state, task);
296 variant->jit_function[RAST_WHOLE]( &state->jit_context,
297 x, y,
298 inputs->frontfacing,
299 GET_A0(inputs),
300 GET_DADX(inputs),
301 GET_DADY(inputs),
302 color,
303 depth,
304 0xffff,
305 &task->thread_data,
306 stride,
307 depth_stride);
308 END_JIT_CALL();
309 }
310
311 void lp_rast_triangle_1( struct lp_rasterizer_task *,
312 const union lp_rast_cmd_arg );
313 void lp_rast_triangle_2( struct lp_rasterizer_task *,
314 const union lp_rast_cmd_arg );
315 void lp_rast_triangle_3( struct lp_rasterizer_task *,
316 const union lp_rast_cmd_arg );
317 void lp_rast_triangle_4( struct lp_rasterizer_task *,
318 const union lp_rast_cmd_arg );
319 void lp_rast_triangle_5( struct lp_rasterizer_task *,
320 const union lp_rast_cmd_arg );
321 void lp_rast_triangle_6( struct lp_rasterizer_task *,
322 const union lp_rast_cmd_arg );
323 void lp_rast_triangle_7( struct lp_rasterizer_task *,
324 const union lp_rast_cmd_arg );
325 void lp_rast_triangle_8( struct lp_rasterizer_task *,
326 const union lp_rast_cmd_arg );
327
328 void lp_rast_triangle_3_4(struct lp_rasterizer_task *,
329 const union lp_rast_cmd_arg );
330
331 void lp_rast_triangle_3_16( struct lp_rasterizer_task *,
332 const union lp_rast_cmd_arg );
333
334 void lp_rast_triangle_4_16( struct lp_rasterizer_task *,
335 const union lp_rast_cmd_arg );
336
337 void
338 lp_rast_set_state(struct lp_rasterizer_task *task,
339 const union lp_rast_cmd_arg arg);
340
341 void
342 lp_debug_bin( const struct cmd_bin *bin );
343
344 #endif