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