1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #ifndef LP_RAST_PRIV_H
29 #define LP_RAST_PRIV_H
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "gallivm/lp_bld_debug.h"
34 #include "lp_memory.h"
38 #include "lp_texture.h"
39 #include "lp_limits.h"
42 #define TILE_VECTOR_HEIGHT 4
43 #define TILE_VECTOR_WIDTH 4
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.
50 struct lp_rasterizer_task
;
52 extern const struct lp_rast_state
*jit_state
;
53 extern const struct lp_rasterizer_task
*jit_task
;
55 #define BEGIN_JIT_CALL(state, task) \
57 jit_line = __LINE__; \
62 #define END_JIT_CALL() \
70 #define BEGIN_JIT_CALL(X, Y)
71 #define END_JIT_CALL()
80 * Per-thread rasterization state
82 struct lp_rasterizer_task
84 const struct cmd_bin
*bin
;
85 const struct lp_rast_state
*state
;
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 */
91 uint8_t *color_tiles
[PIPE_MAX_COLOR_BUFS
];
95 struct lp_rasterizer
*rast
;
98 unsigned thread_index
;
100 /** Non-interpolated passthru state and occlude counter for visible pixels */
101 struct lp_jit_thread_data thread_data
;
102 uint64_t ps_invocations
;
103 uint8_t ps_inv_multiplier
;
105 pipe_semaphore work_ready
;
106 pipe_semaphore work_done
;
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.
118 boolean no_rast
; /**< For debugging/profiling */
120 /** The incoming queue of scenes ready to rasterize */
121 struct lp_scene_queue
*full_scenes
;
123 /** The scene currently being rasterized by the threads */
124 struct lp_scene
*curr_scene
;
126 /** A task object for each rasterization thread */
127 struct lp_rasterizer_task tasks
[LP_MAX_THREADS
];
129 unsigned num_threads
;
130 pipe_thread threads
[LP_MAX_THREADS
];
132 /** For synchronizing the rasterization threads */
133 pipe_barrier barrier
;
138 lp_rast_shade_quads_mask(struct lp_rasterizer_task
*task
,
139 const struct lp_rast_shader_inputs
*inputs
,
140 unsigned x
, unsigned y
,
146 * Get pointer to the color tile
148 static INLINE
uint8_t *
149 lp_rast_get_color_tile_pointer(struct lp_rasterizer_task
*task
,
150 unsigned buf
, enum lp_texture_usage usage
)
152 const struct lp_scene
*scene
= task
->scene
;
153 unsigned format_bytes
;
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
);
161 if (!task
->color_tiles
[buf
]) {
162 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[buf
];
165 format_bytes
= util_format_get_blocksize(cbuf
->format
);
166 task
->color_tiles
[buf
] = scene
->cbufs
[buf
].map
+ scene
->cbufs
[buf
].stride
* task
->y
+
167 format_bytes
* task
->x
;
170 return task
->color_tiles
[buf
];
175 * Get pointer to the depth tile
177 static INLINE
uint8_t *
178 lp_rast_get_depth_tile_pointer(struct lp_rasterizer_task
*task
,
179 enum lp_texture_usage usage
)
181 const struct lp_scene
*scene
= task
->scene
;
182 unsigned format_bytes
;
184 assert(task
->x
< scene
->tiles_x
* TILE_SIZE
);
185 assert(task
->y
< scene
->tiles_y
* TILE_SIZE
);
186 assert(task
->x
% TILE_SIZE
== 0);
187 assert(task
->y
% TILE_SIZE
== 0);
189 if (!task
->depth_tile
) {
190 struct pipe_surface
*dbuf
= scene
->fb
.zsbuf
;
193 format_bytes
= util_format_get_blocksize(dbuf
->format
);
194 task
->depth_tile
= scene
->zsbuf
.map
+ scene
->zsbuf
.stride
* task
->y
+
195 format_bytes
* task
->x
;
198 return task
->depth_tile
;
203 * Get the pointer to a 4x4 color block (within a 64x64 tile).
204 * \param x, y location of 4x4 block in window coords
206 static INLINE
uint8_t *
207 lp_rast_get_color_block_pointer(struct lp_rasterizer_task
*task
,
208 unsigned buf
, unsigned x
, unsigned y
,
211 unsigned px
, py
, pixel_offset
, format_bytes
;
214 assert(x
< task
->scene
->tiles_x
* TILE_SIZE
);
215 assert(y
< task
->scene
->tiles_y
* TILE_SIZE
);
216 assert((x
% TILE_VECTOR_WIDTH
) == 0);
217 assert((y
% TILE_VECTOR_HEIGHT
) == 0);
218 assert(buf
< task
->scene
->fb
.nr_cbufs
);
220 format_bytes
= util_format_get_blocksize(task
->scene
->fb
.cbufs
[buf
]->format
);
222 color
= lp_rast_get_color_tile_pointer(task
, buf
, LP_TEX_USAGE_READ_WRITE
);
227 pixel_offset
= px
* format_bytes
+ py
* task
->scene
->cbufs
[buf
].stride
;
229 color
= color
+ pixel_offset
;
232 color
+= layer
* task
->scene
->cbufs
[buf
].layer_stride
;
235 assert(lp_check_alignment(color
, llvmpipe_get_format_alignment(task
->scene
->fb
.cbufs
[buf
]->format
)));
241 * Get the pointer to a 4x4 depth block (within a 64x64 tile).
242 * \param x, y location of 4x4 block in window coords
244 static INLINE
uint8_t *
245 lp_rast_get_depth_block_pointer(struct lp_rasterizer_task
*task
,
246 unsigned x
, unsigned y
, unsigned layer
)
248 unsigned px
, py
, pixel_offset
, format_bytes
;
251 assert(x
< task
->scene
->tiles_x
* TILE_SIZE
);
252 assert(y
< task
->scene
->tiles_y
* TILE_SIZE
);
253 assert((x
% TILE_VECTOR_WIDTH
) == 0);
254 assert((y
% TILE_VECTOR_HEIGHT
) == 0);
256 format_bytes
= util_format_get_blocksize(task
->scene
->fb
.zsbuf
->format
);
258 depth
= lp_rast_get_depth_tile_pointer(task
, LP_TEX_USAGE_READ_WRITE
);
263 pixel_offset
= px
* format_bytes
+ py
* task
->scene
->zsbuf
.stride
;
265 depth
= depth
+ pixel_offset
;
268 depth
+= layer
* task
->scene
->zsbuf
.layer_stride
;
271 assert(lp_check_alignment(depth
, llvmpipe_get_format_alignment(task
->scene
->fb
.zsbuf
->format
)));
278 * Shade all pixels in a 4x4 block. The fragment code omits the
279 * triangle in/out tests.
280 * \param x, y location of 4x4 block in window coords
283 lp_rast_shade_quads_all( struct lp_rasterizer_task
*task
,
284 const struct lp_rast_shader_inputs
*inputs
,
285 unsigned x
, unsigned y
)
287 const struct lp_scene
*scene
= task
->scene
;
288 const struct lp_rast_state
*state
= task
->state
;
289 struct lp_fragment_shader_variant
*variant
= state
->variant
;
290 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
291 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
292 uint8_t *depth
= NULL
;
293 unsigned depth_stride
= 0;
297 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
298 if (scene
->fb
.cbufs
[i
]) {
299 stride
[i
] = scene
->cbufs
[i
].stride
;
300 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
,
309 if (scene
->zsbuf
.map
) {
310 depth
= lp_rast_get_depth_block_pointer(task
, x
, y
, inputs
->layer
);
311 depth_stride
= scene
->zsbuf
.stride
;
315 * The rasterizer may produce fragments outside our
316 * allocated 4x4 blocks hence need to filter them out here.
318 if ((x
% TILE_SIZE
) < task
->width
&& (y
% TILE_SIZE
) < task
->height
) {
319 /* not very accurate would need a popcount on the mask */
320 /* always count this not worth bothering? */
321 task
->ps_invocations
+= 1 * variant
->ps_inv_multiplier
;
323 /* Propagate non-interpolated raster state. */
324 task
->thread_data
.raster_state
.viewport_index
= inputs
->viewport_index
;
326 /* run shader on 4x4 block */
327 BEGIN_JIT_CALL(state
, task
);
328 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
344 void lp_rast_triangle_1( struct lp_rasterizer_task
*,
345 const union lp_rast_cmd_arg
);
346 void lp_rast_triangle_2( struct lp_rasterizer_task
*,
347 const union lp_rast_cmd_arg
);
348 void lp_rast_triangle_3( struct lp_rasterizer_task
*,
349 const union lp_rast_cmd_arg
);
350 void lp_rast_triangle_4( struct lp_rasterizer_task
*,
351 const union lp_rast_cmd_arg
);
352 void lp_rast_triangle_5( struct lp_rasterizer_task
*,
353 const union lp_rast_cmd_arg
);
354 void lp_rast_triangle_6( struct lp_rasterizer_task
*,
355 const union lp_rast_cmd_arg
);
356 void lp_rast_triangle_7( struct lp_rasterizer_task
*,
357 const union lp_rast_cmd_arg
);
358 void lp_rast_triangle_8( struct lp_rasterizer_task
*,
359 const union lp_rast_cmd_arg
);
361 void lp_rast_triangle_3_4(struct lp_rasterizer_task
*,
362 const union lp_rast_cmd_arg
);
364 void lp_rast_triangle_3_16( struct lp_rasterizer_task
*,
365 const union lp_rast_cmd_arg
);
367 void lp_rast_triangle_4_16( struct lp_rasterizer_task
*,
368 const union lp_rast_cmd_arg
);
371 void lp_rast_triangle_32_1( struct lp_rasterizer_task
*,
372 const union lp_rast_cmd_arg
);
373 void lp_rast_triangle_32_2( struct lp_rasterizer_task
*,
374 const union lp_rast_cmd_arg
);
375 void lp_rast_triangle_32_3( struct lp_rasterizer_task
*,
376 const union lp_rast_cmd_arg
);
377 void lp_rast_triangle_32_4( struct lp_rasterizer_task
*,
378 const union lp_rast_cmd_arg
);
379 void lp_rast_triangle_32_5( struct lp_rasterizer_task
*,
380 const union lp_rast_cmd_arg
);
381 void lp_rast_triangle_32_6( struct lp_rasterizer_task
*,
382 const union lp_rast_cmd_arg
);
383 void lp_rast_triangle_32_7( struct lp_rasterizer_task
*,
384 const union lp_rast_cmd_arg
);
385 void lp_rast_triangle_32_8( struct lp_rasterizer_task
*,
386 const union lp_rast_cmd_arg
);
388 void lp_rast_triangle_32_3_4(struct lp_rasterizer_task
*,
389 const union lp_rast_cmd_arg
);
391 void lp_rast_triangle_32_3_16( struct lp_rasterizer_task
*,
392 const union lp_rast_cmd_arg
);
394 void lp_rast_triangle_32_4_16( struct lp_rasterizer_task
*,
395 const union lp_rast_cmd_arg
);
398 lp_rast_set_state(struct lp_rasterizer_task
*task
,
399 const union lp_rast_cmd_arg arg
);
402 lp_debug_bin( const struct cmd_bin
*bin
, int x
, int y
);