gallium: adapt to get_query_result interface change
[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
115 /** The incoming queue of scenes ready to rasterize */
116 struct lp_scene_queue *full_scenes;
117
118 /** The scene currently being rasterized by the threads */
119 struct lp_scene *curr_scene;
120
121 /** A task object for each rasterization thread */
122 struct lp_rasterizer_task tasks[LP_MAX_THREADS];
123
124 unsigned num_threads;
125 pipe_thread threads[LP_MAX_THREADS];
126
127 /** For synchronizing the rasterization threads */
128 pipe_barrier barrier;
129 };
130
131
132 void
133 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
134 const struct lp_rast_shader_inputs *inputs,
135 unsigned x, unsigned y,
136 unsigned mask);
137
138
139
140 /**
141 * Get the pointer to a 4x4 depth/stencil block.
142 * We'll map the z/stencil buffer on demand here.
143 * Note that this may be called even when there's no z/stencil buffer - return
144 * NULL in that case.
145 * \param x, y location of 4x4 block in window coords
146 */
147 static INLINE void *
148 lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task,
149 unsigned x, unsigned y)
150 {
151 const struct lp_scene *scene = task->scene;
152 void *depth;
153
154 assert(x < scene->tiles_x * TILE_SIZE);
155 assert(y < scene->tiles_y * TILE_SIZE);
156 assert((x % TILE_VECTOR_WIDTH) == 0);
157 assert((y % TILE_VECTOR_HEIGHT) == 0);
158
159 if (!scene->zsbuf.map) {
160 /* Either out of memory or no zsbuf. Can't tell without access
161 * to the state. Just use dummy tile memory, but don't print
162 * the oom warning as this most likely because there is no
163 * zsbuf.
164 */
165 return lp_dummy_tile;
166 }
167
168 depth = (scene->zsbuf.map +
169 scene->zsbuf.stride * y +
170 scene->zsbuf.blocksize * x * TILE_VECTOR_HEIGHT);
171
172 assert(lp_check_alignment(depth, 16));
173 return depth;
174 }
175
176
177 /**
178 * Get pointer to the swizzled color tile
179 */
180 static INLINE uint8_t *
181 lp_rast_get_color_tile_pointer(struct lp_rasterizer_task *task,
182 unsigned buf, enum lp_texture_usage usage)
183 {
184 const struct lp_scene *scene = task->scene;
185
186 assert(task->x < scene->tiles_x * TILE_SIZE);
187 assert(task->y < scene->tiles_y * TILE_SIZE);
188 assert(task->x % TILE_SIZE == 0);
189 assert(task->y % TILE_SIZE == 0);
190 assert(buf < scene->fb.nr_cbufs);
191
192 if (!task->color_tiles[buf]) {
193 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
194 struct llvmpipe_resource *lpt;
195 assert(cbuf);
196 lpt = llvmpipe_resource(cbuf->texture);
197 task->color_tiles[buf] = lp_swizzled_cbuf[task->thread_index][buf];
198
199 if (usage != LP_TEX_USAGE_WRITE_ALL) {
200 llvmpipe_swizzle_cbuf_tile(lpt,
201 cbuf->u.tex.first_layer,
202 cbuf->u.tex.level,
203 task->x, task->y,
204 task->color_tiles[buf]);
205 }
206 }
207
208 return task->color_tiles[buf];
209 }
210
211
212 /**
213 * Get the pointer to a 4x4 color block (within a 64x64 tile).
214 * We'll map the color buffer on demand here.
215 * Note that this may be called even when there's no color buffers - return
216 * NULL in that case.
217 * \param x, y location of 4x4 block in window coords
218 */
219 static INLINE uint8_t *
220 lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task,
221 unsigned buf, unsigned x, unsigned y)
222 {
223 unsigned px, py, pixel_offset;
224 uint8_t *color;
225
226 assert(x < task->scene->tiles_x * TILE_SIZE);
227 assert(y < task->scene->tiles_y * TILE_SIZE);
228 assert((x % TILE_VECTOR_WIDTH) == 0);
229 assert((y % TILE_VECTOR_HEIGHT) == 0);
230
231 color = lp_rast_get_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
232 assert(color);
233
234 px = x % TILE_SIZE;
235 py = y % TILE_SIZE;
236 pixel_offset = tile_pixel_offset(px, py, 0);
237
238 color = color + pixel_offset;
239
240 assert(lp_check_alignment(color, 16));
241 return color;
242 }
243
244
245
246 /**
247 * Shade all pixels in a 4x4 block. The fragment code omits the
248 * triangle in/out tests.
249 * \param x, y location of 4x4 block in window coords
250 */
251 static INLINE void
252 lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
253 const struct lp_rast_shader_inputs *inputs,
254 unsigned x, unsigned y )
255 {
256 const struct lp_scene *scene = task->scene;
257 const struct lp_rast_state *state = task->state;
258 struct lp_fragment_shader_variant *variant = state->variant;
259 uint8_t *color[PIPE_MAX_COLOR_BUFS];
260 void *depth;
261 unsigned i;
262
263 /* color buffer */
264 for (i = 0; i < scene->fb.nr_cbufs; i++)
265 color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
266
267 depth = lp_rast_get_depth_block_pointer(task, x, y);
268
269 /* run shader on 4x4 block */
270 BEGIN_JIT_CALL(state, task);
271 variant->jit_function[RAST_WHOLE]( &state->jit_context,
272 x, y,
273 inputs->frontfacing,
274 GET_A0(inputs),
275 GET_DADX(inputs),
276 GET_DADY(inputs),
277 color,
278 depth,
279 0xffff,
280 &task->vis_counter );
281 END_JIT_CALL();
282 }
283
284 void lp_rast_triangle_1( struct lp_rasterizer_task *,
285 const union lp_rast_cmd_arg );
286 void lp_rast_triangle_2( struct lp_rasterizer_task *,
287 const union lp_rast_cmd_arg );
288 void lp_rast_triangle_3( struct lp_rasterizer_task *,
289 const union lp_rast_cmd_arg );
290 void lp_rast_triangle_4( struct lp_rasterizer_task *,
291 const union lp_rast_cmd_arg );
292 void lp_rast_triangle_5( struct lp_rasterizer_task *,
293 const union lp_rast_cmd_arg );
294 void lp_rast_triangle_6( struct lp_rasterizer_task *,
295 const union lp_rast_cmd_arg );
296 void lp_rast_triangle_7( struct lp_rasterizer_task *,
297 const union lp_rast_cmd_arg );
298 void lp_rast_triangle_8( struct lp_rasterizer_task *,
299 const union lp_rast_cmd_arg );
300
301 void lp_rast_triangle_3_4(struct lp_rasterizer_task *,
302 const union lp_rast_cmd_arg );
303
304 void lp_rast_triangle_3_16( struct lp_rasterizer_task *,
305 const union lp_rast_cmd_arg );
306
307 void lp_rast_triangle_4_16( struct lp_rasterizer_task *,
308 const union lp_rast_cmd_arg );
309
310 void
311 lp_rast_set_state(struct lp_rasterizer_task *task,
312 const union lp_rast_cmd_arg arg);
313
314 void
315 lp_debug_bin( const struct cmd_bin *bin );
316
317 #endif