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