• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "util/format/u_format.h"
32 #include "util/u_thread.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    /** Non-interpolated passthru state and occlude counter for visible pixels */
101    struct lp_jit_thread_data thread_data;
102 
103    pipe_semaphore work_ready;
104    pipe_semaphore work_done;
105 };
106 
107 
108 /**
109  * This is the state required while rasterizing tiles.
110  * Note that this contains per-thread information too.
111  * The tile size is TILE_SIZE x TILE_SIZE pixels.
112  */
113 struct lp_rasterizer
114 {
115    boolean exit_flag;
116    boolean no_rast;  /**< For debugging/profiling */
117 
118    /** The incoming queue of scenes ready to rasterize */
119    struct lp_scene_queue *full_scenes;
120 
121    /** The scene currently being rasterized by the threads */
122    struct lp_scene *curr_scene;
123 
124    /** A task object for each rasterization thread */
125    struct lp_rasterizer_task tasks[LP_MAX_THREADS];
126 
127    unsigned num_threads;
128    thrd_t threads[LP_MAX_THREADS];
129 
130    /** For synchronizing the rasterization threads */
131    util_barrier barrier;
132 
133    struct lp_fence *last_fence;
134 };
135 
136 
137 void
138 lp_rast_shade_quads_mask_sample(struct lp_rasterizer_task *task,
139                                 const struct lp_rast_shader_inputs *inputs,
140                                 unsigned x, unsigned y,
141                                 uint64_t mask);
142 
143 void
144 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
145                          const struct lp_rast_shader_inputs *inputs,
146                          unsigned x, unsigned y,
147                          unsigned mask);
148 
149 
150 /**
151  * Get the pointer to a 4x4 color block (within a 64x64 tile).
152  * \param x, y location of 4x4 block in window coords
153  */
154 static inline uint8_t *
lp_rast_get_color_block_pointer(struct lp_rasterizer_task * task,unsigned buf,unsigned x,unsigned y,unsigned layer)155 lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task,
156                                 unsigned buf, unsigned x, unsigned y,
157                                 unsigned layer)
158 {
159    assert(x < task->scene->tiles_x * TILE_SIZE);
160    assert(y < task->scene->tiles_y * TILE_SIZE);
161    assert((x % TILE_VECTOR_WIDTH) == 0);
162    assert((y % TILE_VECTOR_HEIGHT) == 0);
163    assert(buf < task->scene->fb.nr_cbufs);
164    assert(task->color_tiles[buf]);
165 
166    /*
167     * We don't actually benefit from having per tile cbuf/zsbuf pointers,
168     * it's just extra work - the mul/add would be exactly the same anyway.
169     * Fortunately the extra work (modulo) here is very cheap at least...
170     */
171    unsigned px = x % TILE_SIZE;
172    unsigned py = y % TILE_SIZE;
173 
174    unsigned pixel_offset = px * task->scene->cbufs[buf].format_bytes +
175                            py * task->scene->cbufs[buf].stride;
176    uint8_t *color = task->color_tiles[buf] + pixel_offset;
177 
178    if (layer) {
179       assert(layer <= task->scene->fb_max_layer);
180       color += layer * task->scene->cbufs[buf].layer_stride;
181    }
182 
183    assert(lp_check_alignment(color, llvmpipe_get_format_alignment(task->scene->fb.cbufs[buf]->format)));
184    return color;
185 }
186 
187 
188 /**
189  * Get the pointer to a 4x4 depth block (within a 64x64 tile).
190  * \param x, y location of 4x4 block in window coords
191  */
192 static inline uint8_t *
lp_rast_get_depth_block_pointer(struct lp_rasterizer_task * task,unsigned x,unsigned y,unsigned layer)193 lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task,
194                                 unsigned x, unsigned y, unsigned layer)
195 {
196    assert(x < task->scene->tiles_x * TILE_SIZE);
197    assert(y < task->scene->tiles_y * TILE_SIZE);
198    assert((x % TILE_VECTOR_WIDTH) == 0);
199    assert((y % TILE_VECTOR_HEIGHT) == 0);
200    assert(task->depth_tile);
201 
202    unsigned px = x % TILE_SIZE;
203    unsigned py = y % TILE_SIZE;
204 
205    unsigned pixel_offset = px * task->scene->zsbuf.format_bytes +
206                            py * task->scene->zsbuf.stride;
207    uint8_t *depth = task->depth_tile + pixel_offset;
208 
209    if (layer) {
210       depth += layer * task->scene->zsbuf.layer_stride;
211    }
212 
213    assert(lp_check_alignment(depth, llvmpipe_get_format_alignment(task->scene->fb.zsbuf->format)));
214    return depth;
215 }
216 
217 
218 
219 /**
220  * Shade all pixels in a 4x4 block.  The fragment code omits the
221  * triangle in/out tests.
222  * \param x, y location of 4x4 block in window coords
223  */
224 static inline void
lp_rast_shade_quads_all(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned x,unsigned y)225 lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
226                          const struct lp_rast_shader_inputs *inputs,
227                          unsigned x, unsigned y )
228 {
229    const struct lp_scene *scene = task->scene;
230    const struct lp_rast_state *state = task->state;
231    struct lp_fragment_shader_variant *variant = state->variant;
232    uint8_t *color[PIPE_MAX_COLOR_BUFS];
233    unsigned stride[PIPE_MAX_COLOR_BUFS];
234    unsigned sample_stride[PIPE_MAX_COLOR_BUFS];
235    uint8_t *depth = NULL;
236    unsigned depth_stride = 0;
237    unsigned depth_sample_stride = 0;
238 
239    /* color buffer */
240    for (unsigned i = 0; i < scene->fb.nr_cbufs; i++) {
241       if (scene->fb.cbufs[i]) {
242          stride[i] = scene->cbufs[i].stride;
243          sample_stride[i] = scene->cbufs[i].sample_stride;
244          color[i] = lp_rast_get_color_block_pointer(task, i, x, y,
245                                                     inputs->layer + inputs->view_index);
246       }
247       else {
248          stride[i] = 0;
249          sample_stride[i] = 0;
250          color[i] = NULL;
251       }
252    }
253 
254    if (scene->zsbuf.map) {
255       depth = lp_rast_get_depth_block_pointer(task, x, y, inputs->layer + inputs->view_index);
256       depth_sample_stride = scene->zsbuf.sample_stride;
257       depth_stride = scene->zsbuf.stride;
258    }
259 
260    uint64_t mask = 0;
261    for (unsigned i = 0; i < scene->fb_max_samples; i++)
262       mask |= (uint64_t)0xffff << (16 * i);
263 
264    /*
265     * The rasterizer may produce fragments outside our
266     * allocated 4x4 blocks hence need to filter them out here.
267     */
268    if ((x % TILE_SIZE) < task->width && (y % TILE_SIZE) < task->height) {
269       /* Propagate non-interpolated raster state. */
270       task->thread_data.raster_state.viewport_index = inputs->viewport_index;
271       task->thread_data.raster_state.view_index = inputs->view_index;
272 
273       /* run shader on 4x4 block */
274       BEGIN_JIT_CALL(state, task);
275       variant->jit_function[RAST_WHOLE]( &state->jit_context,
276                                          x, y,
277                                          inputs->frontfacing,
278                                          GET_A0(inputs),
279                                          GET_DADX(inputs),
280                                          GET_DADY(inputs),
281                                          color,
282                                          depth,
283                                          mask,
284                                          &task->thread_data,
285                                          stride,
286                                          depth_stride,
287                                          sample_stride,
288                                          depth_sample_stride);
289       END_JIT_CALL();
290    }
291 }
292 
293 void
294 lp_rast_triangle_1(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
295 
296 void
297 lp_rast_triangle_2(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
298 
299 void
300 lp_rast_triangle_3(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
301 
302 void
303 lp_rast_triangle_4(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
304 
305 void
306 lp_rast_triangle_5(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
307 
308 void
309 lp_rast_triangle_6(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
310 
311 void
312 lp_rast_triangle_7(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
313 
314 void
315 lp_rast_triangle_8(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
316 
317 void
318 lp_rast_triangle_3_4(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
319 
320 void
321 lp_rast_triangle_3_16(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
322 
323 void
324 lp_rast_triangle_4_16(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
325 
326 void
327 lp_rast_triangle_32_1(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
328 
329 void
330 lp_rast_triangle_32_2(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
331 
332 void
333 lp_rast_triangle_32_3(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
334 
335 void
336 lp_rast_triangle_32_4(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
337 
338 void
339 lp_rast_triangle_32_5(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
340 
341 void
342 lp_rast_triangle_32_6(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
343 
344 void
345 lp_rast_triangle_32_7(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
346 
347 void
348 lp_rast_triangle_32_8(struct lp_rasterizer_task *, const union lp_rast_cmd_arg);
349 
350 void
351 lp_rast_triangle_32_3_4(struct lp_rasterizer_task *,
352                         const union lp_rast_cmd_arg);
353 
354 void
355 lp_rast_triangle_32_3_16(struct lp_rasterizer_task *,
356                          const union lp_rast_cmd_arg);
357 
358 void
359 lp_rast_triangle_32_4_16(struct lp_rasterizer_task *,
360                          const union lp_rast_cmd_arg);
361 
362 void
363 lp_rast_rectangle(struct lp_rasterizer_task *,
364                   const union lp_rast_cmd_arg);
365 
366 void
367 lp_rast_triangle_ms_1(struct lp_rasterizer_task *,
368                       const union lp_rast_cmd_arg);
369 
370 void
371 lp_rast_triangle_ms_2(struct lp_rasterizer_task *,
372                       const union lp_rast_cmd_arg);
373 
374 void
375 lp_rast_triangle_ms_3(struct lp_rasterizer_task *,
376                       const union lp_rast_cmd_arg);
377 
378 void
379 lp_rast_triangle_ms_4(struct lp_rasterizer_task *,
380                       const union lp_rast_cmd_arg);
381 
382 void
383 lp_rast_triangle_ms_5(struct lp_rasterizer_task *,
384                       const union lp_rast_cmd_arg);
385 
386 void
387 lp_rast_triangle_ms_6(struct lp_rasterizer_task *,
388                       const union lp_rast_cmd_arg);
389 
390 void
391 lp_rast_triangle_ms_7(struct lp_rasterizer_task *,
392                       const union lp_rast_cmd_arg);
393 
394 void
395 lp_rast_triangle_ms_8(struct lp_rasterizer_task *,
396                       const union lp_rast_cmd_arg);
397 
398 
399 void
400 lp_rast_triangle_ms_3_4(struct lp_rasterizer_task *,
401                         const union lp_rast_cmd_arg);
402 
403 
404 void
405 lp_rast_triangle_ms_3_16(struct lp_rasterizer_task *,
406                          const union lp_rast_cmd_arg);
407 
408 
409 void
410 lp_rast_triangle_ms_4_16(struct lp_rasterizer_task *,
411                          const union lp_rast_cmd_arg);
412 
413 
414 void
415 lp_rast_triangle_ms_32_1(struct lp_rasterizer_task *,
416                          const union lp_rast_cmd_arg);
417 
418 void
419 lp_rast_triangle_ms_32_2(struct lp_rasterizer_task *,
420                          const union lp_rast_cmd_arg);
421 
422 void
423 lp_rast_triangle_ms_32_3(struct lp_rasterizer_task *,
424                          const union lp_rast_cmd_arg);
425 
426 void
427 lp_rast_triangle_ms_32_4(struct lp_rasterizer_task *,
428                          const union lp_rast_cmd_arg);
429 
430 void
431 lp_rast_triangle_ms_32_5(struct lp_rasterizer_task *,
432                          const union lp_rast_cmd_arg);
433 
434 void
435 lp_rast_triangle_ms_32_6(struct lp_rasterizer_task *,
436                          const union lp_rast_cmd_arg);
437 
438 void
439 lp_rast_triangle_ms_32_7(struct lp_rasterizer_task *,
440                          const union lp_rast_cmd_arg);
441 
442 void
443 lp_rast_triangle_ms_32_8(struct lp_rasterizer_task *,
444                          const union lp_rast_cmd_arg);
445 
446 void
447 lp_rast_triangle_ms_32_3_4(struct lp_rasterizer_task *,
448                            const union lp_rast_cmd_arg);
449 
450 void
451 lp_rast_triangle_ms_32_3_16(struct lp_rasterizer_task *,
452                             const union lp_rast_cmd_arg);
453 
454 void
455 lp_rast_triangle_ms_32_4_16(struct lp_rasterizer_task *,
456                             const union lp_rast_cmd_arg);
457 
458 void
459 lp_rast_set_state(struct lp_rasterizer_task *task,
460                   const union lp_rast_cmd_arg arg);
461 
462 void
463 lp_debug_bin(const struct cmd_bin *bin, int x, int y);
464 
465 void
466 lp_linear_rasterize_bin(struct lp_rasterizer_task *task,
467                         const struct cmd_bin *bin);
468 
469 void
470 lp_rast_linear_rect_fallback(struct lp_rasterizer_task *task,
471                              const struct lp_rast_shader_inputs *inputs,
472                              const struct u_rect *box);
473 
474 #endif
475