eeb1a94138ecaa0b1579902895917466e1184cca
[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_soa.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 visiable pixels */
98 uint32_t vis_counter;
99 struct llvmpipe_query *query;
100
101 pipe_semaphore work_ready;
102 pipe_semaphore work_done;
103 };
104
105
106 /**
107 * This is the state required while rasterizing tiles.
108 * Note that this contains per-thread information too.
109 * The tile size is TILE_SIZE x TILE_SIZE pixels.
110 */
111 struct lp_rasterizer
112 {
113 boolean exit_flag;
114 boolean no_rast; /**< For debugging/profiling */
115
116 /** The incoming queue of scenes ready to rasterize */
117 struct lp_scene_queue *full_scenes;
118
119 /** The scene currently being rasterized by the threads */
120 struct lp_scene *curr_scene;
121
122 /** A task object for each rasterization thread */
123 struct lp_rasterizer_task tasks[LP_MAX_THREADS];
124
125 unsigned num_threads;
126 pipe_thread threads[LP_MAX_THREADS];
127
128 /** For synchronizing the rasterization threads */
129 pipe_barrier barrier;
130 };
131
132
133 void
134 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
135 const struct lp_rast_shader_inputs *inputs,
136 unsigned x, unsigned y,
137 unsigned mask);
138
139
140
141 /**
142 * Get the pointer to a 4x4 depth/stencil block.
143 * We'll map the z/stencil buffer on demand here.
144 * Note that this may be called even when there's no z/stencil buffer - return
145 * NULL in that case.
146 * \param x, y location of 4x4 block in window coords
147 */
148 static INLINE void *
149 lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task,
150 unsigned x, unsigned y)
151 {
152 const struct lp_scene *scene = task->scene;
153 void *depth;
154
155 assert(x < scene->tiles_x * TILE_SIZE);
156 assert(y < scene->tiles_y * TILE_SIZE);
157 assert((x % TILE_VECTOR_WIDTH) == 0);
158 assert((y % TILE_VECTOR_HEIGHT) == 0);
159
160 if (!scene->zsbuf.map) {
161 /* Either out of memory or no zsbuf. Can't tell without access
162 * to the state. Just use dummy tile memory, but don't print
163 * the oom warning as this most likely because there is no
164 * zsbuf.
165 */
166 return lp_dummy_tile;
167 }
168
169 depth = (scene->zsbuf.map +
170 scene->zsbuf.stride * y +
171 scene->zsbuf.blocksize * x * TILE_VECTOR_HEIGHT);
172
173 assert(lp_check_alignment(depth, 16));
174 return depth;
175 }
176
177
178 /**
179 * Get pointer to the swizzled color tile
180 */
181 static INLINE uint8_t *
182 lp_rast_get_color_tile_pointer(struct lp_rasterizer_task *task,
183 unsigned buf, enum lp_texture_usage usage)
184 {
185 const struct lp_scene *scene = task->scene;
186
187 assert(task->x < scene->tiles_x * TILE_SIZE);
188 assert(task->y < scene->tiles_y * TILE_SIZE);
189 assert(task->x % TILE_SIZE == 0);
190 assert(task->y % TILE_SIZE == 0);
191 assert(buf < scene->fb.nr_cbufs);
192 assert(scene->cbufs[buf].unswizzled == 0);
193
194 if (!task->color_tiles[buf]) {
195 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
196 struct llvmpipe_resource *lpt;
197 assert(cbuf);
198 lpt = llvmpipe_resource(cbuf->texture);
199 task->color_tiles[buf] = lp_swizzled_cbuf[task->thread_index][buf];
200
201 if (usage != LP_TEX_USAGE_WRITE_ALL) {
202 llvmpipe_swizzle_cbuf_tile(lpt,
203 cbuf->u.tex.first_layer,
204 cbuf->u.tex.level,
205 task->x, task->y,
206 task->color_tiles[buf]);
207 }
208 }
209
210 return task->color_tiles[buf];
211 }
212
213
214 /**
215 * Get pointer to the unswizzled color tile
216 */
217 static INLINE uint8_t *
218 lp_rast_get_unswizzled_color_tile_pointer(struct lp_rasterizer_task *task,
219 unsigned buf, enum lp_texture_usage usage)
220 {
221 const struct lp_scene *scene = task->scene;
222 unsigned format_bytes;
223
224 assert(task->x < scene->tiles_x * TILE_SIZE);
225 assert(task->y < scene->tiles_y * TILE_SIZE);
226 assert(task->x % TILE_SIZE == 0);
227 assert(task->y % TILE_SIZE == 0);
228 assert(buf < scene->fb.nr_cbufs);
229 assert(scene->cbufs[buf].unswizzled);
230
231 if (!task->color_tiles[buf]) {
232 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
233 assert(cbuf);
234
235 format_bytes = util_format_description(cbuf->format)->block.bits / 8;
236 task->color_tiles[buf] = scene->cbufs[buf].map + scene->cbufs[buf].stride * task->y + format_bytes * task->x;
237 }
238
239 return task->color_tiles[buf];
240 }
241
242
243 /**
244 * Get the pointer to a 4x4 color block (within a 64x64 tile).
245 * We'll map the color buffer on demand here.
246 * Note that this may be called even when there's no color buffers - return
247 * NULL in that case.
248 * \param x, y location of 4x4 block in window coords
249 */
250 static INLINE uint8_t *
251 lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task,
252 unsigned buf, unsigned x, unsigned y)
253 {
254 unsigned px, py, pixel_offset;
255 uint8_t *color;
256
257 assert(x < task->scene->tiles_x * TILE_SIZE);
258 assert(y < task->scene->tiles_y * TILE_SIZE);
259 assert((x % TILE_VECTOR_WIDTH) == 0);
260 assert((y % TILE_VECTOR_HEIGHT) == 0);
261 assert(buf < task->scene->fb.nr_cbufs);
262 assert(task->scene->cbufs[buf].unswizzled == 0);
263
264 color = lp_rast_get_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
265 assert(color);
266
267 px = x % TILE_SIZE;
268 py = y % TILE_SIZE;
269 pixel_offset = tile_pixel_offset(px, py, 0);
270
271 color = color + pixel_offset;
272
273 assert(lp_check_alignment(color, 16));
274 return color;
275 }
276
277
278 /**
279 * Get the pointer to an unswizzled 4x4 color block (within an unswizzled 64x64 tile).
280 * \param x, y location of 4x4 block in window coords
281 */
282 static INLINE uint8_t *
283 lp_rast_get_unswizzled_color_block_pointer(struct lp_rasterizer_task *task,
284 unsigned buf, unsigned x, unsigned y)
285 {
286 unsigned px, py, pixel_offset, format_bytes;
287 uint8_t *color;
288
289 assert(x < task->scene->tiles_x * TILE_SIZE);
290 assert(y < task->scene->tiles_y * TILE_SIZE);
291 assert((x % TILE_VECTOR_WIDTH) == 0);
292 assert((y % TILE_VECTOR_HEIGHT) == 0);
293 assert(buf < task->scene->fb.nr_cbufs);
294 assert(task->scene->cbufs[buf].unswizzled);
295
296 format_bytes = util_format_description(task->scene->fb.cbufs[buf]->format)->block.bits / 8;
297
298 color = lp_rast_get_unswizzled_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
299 assert(color);
300
301 px = x % TILE_SIZE;
302 py = y % TILE_SIZE;
303 pixel_offset = px * format_bytes + py * task->scene->cbufs[buf].stride;
304
305 color = color + pixel_offset;
306
307 assert(lp_check_alignment(color, llvmpipe_get_format_alignment(task->scene->fb.cbufs[buf]->format)));
308 return color;
309 }
310
311
312
313 /**
314 * Shade all pixels in a 4x4 block. The fragment code omits the
315 * triangle in/out tests.
316 * \param x, y location of 4x4 block in window coords
317 */
318 static INLINE void
319 lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
320 const struct lp_rast_shader_inputs *inputs,
321 unsigned x, unsigned y )
322 {
323 const struct lp_scene *scene = task->scene;
324 const struct lp_rast_state *state = task->state;
325 struct lp_fragment_shader_variant *variant = state->variant;
326 uint8_t *color[PIPE_MAX_COLOR_BUFS];
327 unsigned stride[PIPE_MAX_COLOR_BUFS];
328 void *depth;
329 unsigned i;
330
331 /* color buffer */
332 for (i = 0; i < scene->fb.nr_cbufs; i++) {
333 stride[i] = scene->cbufs[i].stride;
334
335 if (scene->cbufs[i].unswizzled) {
336 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, x, y);
337 } else {
338 color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
339 }
340 }
341
342 depth = lp_rast_get_depth_block_pointer(task, x, y);
343
344 /* run shader on 4x4 block */
345 BEGIN_JIT_CALL(state, task);
346 variant->jit_function[RAST_WHOLE]( &state->jit_context,
347 x, y,
348 inputs->frontfacing,
349 GET_A0(inputs),
350 GET_DADX(inputs),
351 GET_DADY(inputs),
352 color,
353 depth,
354 0xffff,
355 &task->vis_counter,
356 stride );
357 END_JIT_CALL();
358 }
359
360 void lp_rast_triangle_1( struct lp_rasterizer_task *,
361 const union lp_rast_cmd_arg );
362 void lp_rast_triangle_2( struct lp_rasterizer_task *,
363 const union lp_rast_cmd_arg );
364 void lp_rast_triangle_3( struct lp_rasterizer_task *,
365 const union lp_rast_cmd_arg );
366 void lp_rast_triangle_4( struct lp_rasterizer_task *,
367 const union lp_rast_cmd_arg );
368 void lp_rast_triangle_5( struct lp_rasterizer_task *,
369 const union lp_rast_cmd_arg );
370 void lp_rast_triangle_6( struct lp_rasterizer_task *,
371 const union lp_rast_cmd_arg );
372 void lp_rast_triangle_7( struct lp_rasterizer_task *,
373 const union lp_rast_cmd_arg );
374 void lp_rast_triangle_8( struct lp_rasterizer_task *,
375 const union lp_rast_cmd_arg );
376
377 void lp_rast_triangle_3_4(struct lp_rasterizer_task *,
378 const union lp_rast_cmd_arg );
379
380 void lp_rast_triangle_3_16( struct lp_rasterizer_task *,
381 const union lp_rast_cmd_arg );
382
383 void lp_rast_triangle_4_16( struct lp_rasterizer_task *,
384 const union lp_rast_cmd_arg );
385
386 void
387 lp_rast_set_state(struct lp_rasterizer_task *task,
388 const union lp_rast_cmd_arg arg);
389
390 void
391 lp_debug_bin( const struct cmd_bin *bin );
392
393 #endif