llvmpipe: debug, crash fixes
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 /**
29 * Tiling engine.
30 *
31 * Builds per-tile display lists and executes them on calls to
32 * lp_setup_flush().
33 */
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_pack_color.h"
40 #include "lp_state.h"
41 #include "lp_buffer.h"
42 #include "lp_texture.h"
43 #include "lp_setup_context.h"
44
45 static void set_state( struct setup_context *, unsigned );
46
47 void lp_setup_new_cmd_block( struct cmd_block_list *list )
48 {
49 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
50 list->tail->next = block;
51 list->tail = block;
52 block->next = NULL;
53 block->count = 0;
54 }
55
56 void lp_setup_new_data_block( struct data_block_list *list )
57 {
58 struct data_block *block = MALLOC_STRUCT(data_block);
59 list->tail->next = block;
60 list->tail = block;
61 block->next = NULL;
62 block->used = 0;
63 }
64
65
66 static void
67 first_triangle( struct setup_context *setup,
68 const float (*v0)[4],
69 const float (*v1)[4],
70 const float (*v2)[4])
71 {
72 set_state( setup, SETUP_ACTIVE );
73 lp_setup_choose_triangle( setup );
74 setup->triangle( setup, v0, v1, v2 );
75 }
76
77 static void
78 first_line( struct setup_context *setup,
79 const float (*v0)[4],
80 const float (*v1)[4])
81 {
82 set_state( setup, SETUP_ACTIVE );
83 lp_setup_choose_line( setup );
84 setup->line( setup, v0, v1 );
85 }
86
87 static void
88 first_point( struct setup_context *setup,
89 const float (*v0)[4])
90 {
91 set_state( setup, SETUP_ACTIVE );
92 lp_setup_choose_point( setup );
93 setup->point( setup, v0 );
94 }
95
96 static void reset_context( struct setup_context *setup )
97 {
98 unsigned i, j;
99
100 /* Free binner command lists:
101 */
102 for (i = 0; i < setup->tiles_x; i++) {
103 for (j = 0; j < setup->tiles_y; j++) {
104 struct cmd_block_list *list = &setup->tile[i][j];
105 struct cmd_block *block;
106 struct cmd_block *tmp;
107
108 for (block = list->head; block != list->tail; block = tmp) {
109 tmp = block->next;
110 FREE(block);
111 }
112
113 list->head = list->tail;
114 }
115 }
116
117 /* Free binned data:
118 */
119 {
120 struct data_block_list *list = &setup->data;
121 struct data_block *block, *tmp;
122
123 for (block = list->head; block != list->tail; block = tmp) {
124 tmp = block->next;
125 FREE(block);
126 }
127
128 list->head = list->tail;
129 }
130
131 /* Reset some state:
132 */
133 setup->clear.flags = 0;
134
135 /* Have an explicit "start-binning" call and get rid of this
136 * pointer twiddling?
137 */
138 setup->line = first_line;
139 setup->point = first_point;
140 setup->triangle = first_triangle;
141 }
142
143
144
145
146 /* Add a command to all active bins.
147 */
148 static void bin_everywhere( struct setup_context *setup,
149 lp_rast_cmd cmd,
150 const union lp_rast_cmd_arg arg )
151 {
152 unsigned i, j;
153 for (i = 0; i < setup->tiles_x; i++)
154 for (j = 0; j < setup->tiles_y; j++)
155 bin_command( &setup->tile[i][j], cmd, arg );
156 }
157
158
159 static void
160 rasterize_bins( struct setup_context *setup,
161 boolean write_depth )
162 {
163 struct lp_rasterizer *rast = setup->rast;
164 struct cmd_block *block;
165 unsigned i,j,k;
166
167 lp_rast_begin( rast,
168 setup->fb.width,
169 setup->fb.height );
170
171 lp_rast_bind_color( rast,
172 setup->fb.cbuf,
173 setup->fb.cbuf != NULL );
174
175 lp_rast_bind_zstencil( rast,
176 setup->fb.zsbuf,
177 setup->fb.zsbuf != NULL && write_depth );
178
179 for (i = 0; i < setup->tiles_x; i++) {
180 for (j = 0; j < setup->tiles_y; j++) {
181
182 lp_rast_start_tile( rast,
183 i * TILESIZE,
184 j * TILESIZE );
185
186 for (block = setup->tile[i][j].head; block; block = block->next) {
187 for (k = 0; k < block->count; k++) {
188 block->cmd[k]( rast, block->arg[k] );
189 }
190 }
191
192 lp_rast_end_tile( rast );
193 }
194 }
195
196 reset_context( setup );
197 }
198
199
200
201 static void
202 begin_binning( struct setup_context *setup )
203 {
204 if (!setup->fb.cbuf && !setup->fb.zsbuf) {
205 setup->fb.width = 0;
206 setup->fb.height = 0;
207 }
208 else if (!setup->fb.zsbuf) {
209 setup->fb.width = setup->fb.cbuf->width;
210 setup->fb.height = setup->fb.cbuf->height;
211 }
212 else if (!setup->fb.cbuf) {
213 setup->fb.width = setup->fb.zsbuf->width;
214 setup->fb.height = setup->fb.zsbuf->height;
215 }
216 else {
217 /* XXX: not sure what we're really supposed to do for
218 * mis-matched color & depth buffer sizes.
219 */
220 setup->fb.width = MIN2(setup->fb.cbuf->width,
221 setup->fb.zsbuf->width);
222 setup->fb.height = MIN2(setup->fb.cbuf->height,
223 setup->fb.zsbuf->height);
224 }
225
226 setup->tiles_x = align(setup->fb.width, TILESIZE) / TILESIZE;
227 setup->tiles_y = align(setup->fb.height, TILESIZE) / TILESIZE;
228
229 if (setup->fb.cbuf) {
230 if (setup->clear.flags & PIPE_CLEAR_COLOR)
231 bin_everywhere( setup,
232 lp_rast_clear_color,
233 setup->clear.color );
234 else
235 bin_everywhere( setup, lp_rast_load_color, lp_rast_arg_null() );
236 }
237
238 if (setup->fb.zsbuf) {
239 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
240 bin_everywhere( setup,
241 lp_rast_clear_zstencil,
242 setup->clear.zstencil );
243 else
244 bin_everywhere( setup, lp_rast_load_zstencil, lp_rast_arg_null() );
245 }
246 }
247
248
249 /* This basically bins and then flushes any outstanding full-screen
250 * clears.
251 *
252 * TODO: fast path for fullscreen clears and no triangles.
253 */
254 static void
255 execute_clears( struct setup_context *setup )
256 {
257 begin_binning( setup );
258 rasterize_bins( setup, TRUE );
259 }
260
261
262 static void
263 set_state( struct setup_context *setup,
264 unsigned new_state )
265 {
266 unsigned old_state = setup->state;
267
268 if (old_state == new_state)
269 return;
270
271 switch (new_state) {
272 case SETUP_ACTIVE:
273 if (old_state == SETUP_FLUSHED)
274 begin_binning( setup );
275 break;
276
277 case SETUP_CLEARED:
278 if (old_state == SETUP_ACTIVE) {
279 assert(0);
280 return;
281 }
282 break;
283
284 case SETUP_FLUSHED:
285 if (old_state == SETUP_CLEARED)
286 execute_clears( setup );
287 else
288 rasterize_bins( setup, TRUE );
289 break;
290 }
291
292 setup->state = new_state;
293 }
294
295
296 void
297 lp_setup_flush( struct setup_context *setup,
298 unsigned flags )
299 {
300 set_state( setup, SETUP_FLUSHED );
301 }
302
303
304 void
305 lp_setup_bind_framebuffer( struct setup_context *setup,
306 struct pipe_surface *color,
307 struct pipe_surface *zstencil )
308 {
309 set_state( setup, SETUP_FLUSHED );
310
311 pipe_surface_reference( &setup->fb.cbuf, color );
312 pipe_surface_reference( &setup->fb.zsbuf, zstencil );
313 }
314
315 void
316 lp_setup_clear( struct setup_context *setup,
317 const float *color,
318 double depth,
319 unsigned stencil,
320 unsigned flags )
321 {
322 if (flags & PIPE_CLEAR_COLOR) {
323 util_pack_color(color,
324 setup->fb.cbuf->format,
325 &setup->clear.color.clear_color );
326 }
327
328 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
329 setup->clear.zstencil.clear_zstencil =
330 util_pack_z_stencil(setup->fb.zsbuf->format,
331 depth,
332 stencil);
333 }
334
335 if (setup->state == SETUP_ACTIVE) {
336 /* Add the clear to existing bins. In the unusual case where
337 * both color and depth-stencilare being cleared, we could
338 * discard the currently binned scene and start again, but I
339 * don't see that as being a common usage.
340 */
341 if (flags & PIPE_CLEAR_COLOR)
342 bin_everywhere( setup,
343 lp_rast_clear_color,
344 setup->clear.color );
345
346 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
347 bin_everywhere( setup,
348 lp_rast_clear_zstencil,
349 setup->clear.zstencil );
350 }
351 else {
352 /* Put ourselves into the 'pre-clear' state, specifically to try
353 * and accumulate multiple clears to color and depth_stencil
354 * buffers which the app or state-tracker might issue
355 * separately.
356 */
357 set_state( setup, SETUP_CLEARED );
358
359 setup->clear.flags |= flags;
360 }
361 }
362
363
364
365 void
366 lp_setup_set_triangle_state( struct setup_context *setup,
367 unsigned cull_mode,
368 boolean ccw_is_frontface)
369 {
370 setup->ccw_is_frontface = ccw_is_frontface;
371 setup->cullmode = cull_mode;
372 setup->triangle = first_triangle;
373 }
374
375
376
377 void
378 lp_setup_set_fs_inputs( struct setup_context *setup,
379 const struct lp_shader_input *input,
380 unsigned nr )
381 {
382 memcpy( setup->fs.input, input, nr * sizeof input[0] );
383 setup->fs.nr_inputs = nr;
384 }
385
386 void
387 lp_setup_set_fs( struct setup_context *setup,
388 struct lp_fragment_shader *fs )
389 {
390 /* FIXME: reference count */
391
392 setup->fs.jit_function = fs->current->jit_function;
393 }
394
395 void
396 lp_setup_set_fs_constants(struct setup_context *setup,
397 struct pipe_buffer *buffer)
398 {
399 const void *data = buffer ? llvmpipe_buffer(buffer)->data : NULL;
400 struct pipe_buffer *dummy;
401
402 /* FIXME: hold on to the reference */
403 dummy = NULL;
404 pipe_buffer_reference(&dummy, buffer);
405
406 setup->fs.jit_context.constants = data;
407
408 setup->fs.jit_context_dirty = TRUE;
409 }
410
411
412 void
413 lp_setup_set_alpha_ref_value( struct setup_context *setup,
414 float alpha_ref_value )
415 {
416 if(setup->fs.jit_context.alpha_ref_value != alpha_ref_value) {
417 setup->fs.jit_context.alpha_ref_value = alpha_ref_value;
418 setup->fs.jit_context_dirty = TRUE;
419 }
420 }
421
422 void
423 lp_setup_set_blend_color( struct setup_context *setup,
424 const struct pipe_blend_color *blend_color )
425 {
426 unsigned i, j;
427
428 if(!setup->fs.jit_context.blend_color)
429 setup->fs.jit_context.blend_color = align_malloc(4 * 16, 16);
430
431 for (i = 0; i < 4; ++i) {
432 uint8_t c = float_to_ubyte(blend_color->color[i]);
433 for (j = 0; j < 16; ++j)
434 setup->fs.jit_context.blend_color[i*4 + j] = c;
435 }
436
437 setup->fs.jit_context_dirty = TRUE;
438 }
439
440 void
441 lp_setup_set_sampler_textures( struct setup_context *setup,
442 unsigned num, struct pipe_texture **texture)
443 {
444 struct pipe_texture *dummy;
445 unsigned i;
446
447 assert(num <= PIPE_MAX_SAMPLERS);
448
449 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
450 struct pipe_texture *tex = i < num ? texture[i] : NULL;
451
452 /* FIXME: hold on to the reference */
453 dummy = NULL;
454 pipe_texture_reference(&dummy, tex);
455
456 if(tex) {
457 struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
458 struct lp_jit_texture *jit_tex = &setup->fs.jit_context.textures[i];
459 jit_tex->width = tex->width[0];
460 jit_tex->height = tex->height[0];
461 jit_tex->stride = lp_tex->stride[0];
462 if(!lp_tex->dt)
463 jit_tex->data = lp_tex->data;
464 else
465 /* FIXME: map the rendertarget */
466 assert(0);
467 }
468 }
469
470 setup->fs.jit_context_dirty = TRUE;
471 }
472
473 boolean
474 lp_setup_is_texture_referenced( struct setup_context *setup,
475 const struct pipe_texture *texture )
476 {
477 /* FIXME */
478 return PIPE_UNREFERENCED;
479 }
480
481
482 /* Stubs for lines & points for now:
483 */
484 void
485 lp_setup_point(struct setup_context *setup,
486 const float (*v0)[4])
487 {
488 setup->point( setup, v0 );
489 }
490
491 void
492 lp_setup_line(struct setup_context *setup,
493 const float (*v0)[4],
494 const float (*v1)[4])
495 {
496 setup->line( setup, v0, v1 );
497 }
498
499 void
500 lp_setup_tri(struct setup_context *setup,
501 const float (*v0)[4],
502 const float (*v1)[4],
503 const float (*v2)[4])
504 {
505 setup->triangle( setup, v0, v1, v2 );
506 }
507
508
509 void
510 lp_setup_destroy( struct setup_context *setup )
511 {
512 unsigned i, j;
513
514 reset_context( setup );
515
516 for (i = 0; i < TILES_X; i++)
517 for (j = 0; j < TILES_Y; j++)
518 FREE(setup->tile[i][j].head);
519
520 lp_rast_destroy( setup->rast );
521 FREE( setup );
522 }
523
524
525 /**
526 * Create a new primitive tiling engine. Currently also creates a
527 * rasterizer to use with it.
528 */
529 struct setup_context *
530 lp_setup_create( void )
531 {
532 struct setup_context *setup = CALLOC_STRUCT(setup_context);
533 unsigned i, j;
534
535 setup->rast = lp_rast_create();
536 if (!setup->rast)
537 goto fail;
538
539 for (i = 0; i < TILES_X; i++)
540 for (j = 0; j < TILES_Y; j++)
541 setup->tile[i][j].head =
542 setup->tile[i][j].tail = CALLOC_STRUCT(cmd_block);
543
544 return setup;
545
546 fail:
547 FREE(setup);
548 return NULL;
549 }
550