Merge commit '381d5e209815235911c4aab516037c868c8f695f'
[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 "util/u_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "util/u_surface.h"
40 #include "lp_scene.h"
41 #include "lp_scene_queue.h"
42 #include "lp_buffer.h"
43 #include "lp_texture.h"
44 #include "lp_debug.h"
45 #include "lp_fence.h"
46 #include "lp_rast.h"
47 #include "lp_setup_context.h"
48 #include "lp_screen.h"
49 #include "lp_winsys.h"
50
51 #include "draw/draw_context.h"
52 #include "draw/draw_vbuf.h"
53
54
55 static void set_scene_state( struct setup_context *, unsigned );
56
57
58 struct lp_scene *
59 lp_setup_get_current_scene(struct setup_context *setup)
60 {
61 if (!setup->scene) {
62
63 /* wait for a free/empty scene
64 */
65 setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
66
67 if(0)lp_scene_reset( setup->scene ); /* XXX temporary? */
68
69 lp_scene_set_framebuffer_size(setup->scene,
70 setup->fb.width,
71 setup->fb.height);
72 }
73 return setup->scene;
74 }
75
76
77 static void
78 first_triangle( struct setup_context *setup,
79 const float (*v0)[4],
80 const float (*v1)[4],
81 const float (*v2)[4])
82 {
83 set_scene_state( setup, SETUP_ACTIVE );
84 lp_setup_choose_triangle( setup );
85 setup->triangle( setup, v0, v1, v2 );
86 }
87
88 static void
89 first_line( struct setup_context *setup,
90 const float (*v0)[4],
91 const float (*v1)[4])
92 {
93 set_scene_state( setup, SETUP_ACTIVE );
94 lp_setup_choose_line( setup );
95 setup->line( setup, v0, v1 );
96 }
97
98 static void
99 first_point( struct setup_context *setup,
100 const float (*v0)[4])
101 {
102 set_scene_state( setup, SETUP_ACTIVE );
103 lp_setup_choose_point( setup );
104 setup->point( setup, v0 );
105 }
106
107 static void reset_context( struct setup_context *setup )
108 {
109 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
110
111 /* Reset derived state */
112 setup->constants.stored_size = 0;
113 setup->constants.stored_data = NULL;
114 setup->fs.stored = NULL;
115 setup->dirty = ~0;
116
117 /* no current bin */
118 setup->scene = NULL;
119
120 /* Reset some state:
121 */
122 setup->clear.flags = 0;
123
124 /* Have an explicit "start-binning" call and get rid of this
125 * pointer twiddling?
126 */
127 setup->line = first_line;
128 setup->point = first_point;
129 setup->triangle = first_triangle;
130 }
131
132
133 /** Rasterize all scene's bins */
134 static void
135 lp_setup_rasterize_scene( struct setup_context *setup,
136 boolean write_depth )
137 {
138 struct lp_scene *scene = lp_setup_get_current_scene(setup);
139
140 lp_rasterize_scene(setup->rast,
141 scene,
142 &setup->fb,
143 write_depth);
144
145 reset_context( setup );
146
147 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
148 }
149
150
151
152 static void
153 begin_binning( struct setup_context *setup )
154 {
155 struct lp_scene *scene = lp_setup_get_current_scene(setup);
156
157 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
158 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
159 (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
160
161 if (setup->fb.nr_cbufs) {
162 if (setup->clear.flags & PIPE_CLEAR_COLOR)
163 lp_scene_bin_everywhere( scene,
164 lp_rast_clear_color,
165 setup->clear.color );
166 else
167 lp_scene_bin_everywhere( scene,
168 lp_rast_load_color,
169 lp_rast_arg_null() );
170 }
171
172 if (setup->fb.zsbuf) {
173 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
174 lp_scene_bin_everywhere( scene,
175 lp_rast_clear_zstencil,
176 setup->clear.zstencil );
177 }
178
179 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
180 }
181
182
183 /* This basically bins and then flushes any outstanding full-screen
184 * clears.
185 *
186 * TODO: fast path for fullscreen clears and no triangles.
187 */
188 static void
189 execute_clears( struct setup_context *setup )
190 {
191 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
192
193 begin_binning( setup );
194 lp_setup_rasterize_scene( setup, TRUE );
195 }
196
197
198 static void
199 set_scene_state( struct setup_context *setup,
200 unsigned new_state )
201 {
202 unsigned old_state = setup->state;
203
204 if (old_state == new_state)
205 return;
206
207 LP_DBG(DEBUG_SETUP, "%s old %d new %d\n", __FUNCTION__, old_state, new_state);
208
209 switch (new_state) {
210 case SETUP_ACTIVE:
211 begin_binning( setup );
212 break;
213
214 case SETUP_CLEARED:
215 if (old_state == SETUP_ACTIVE) {
216 assert(0);
217 return;
218 }
219 break;
220
221 case SETUP_FLUSHED:
222 if (old_state == SETUP_CLEARED)
223 execute_clears( setup );
224 else
225 lp_setup_rasterize_scene( setup, TRUE );
226 break;
227 }
228
229 setup->state = new_state;
230 }
231
232
233 void
234 lp_setup_flush( struct setup_context *setup,
235 unsigned flags )
236 {
237 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
238
239 set_scene_state( setup, SETUP_FLUSHED );
240 }
241
242
243 void
244 lp_setup_bind_framebuffer( struct setup_context *setup,
245 const struct pipe_framebuffer_state *fb )
246 {
247 struct lp_scene *scene = lp_setup_get_current_scene(setup);
248
249 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
250
251 set_scene_state( setup, SETUP_FLUSHED );
252
253 /* re-get scene pointer, may have a new scene after flushing */
254 (void) scene;
255 scene = lp_setup_get_current_scene(setup);
256
257 util_copy_framebuffer_state(&setup->fb, fb);
258
259 lp_scene_set_framebuffer_size(scene, setup->fb.width, setup->fb.height);
260 }
261
262
263 void
264 lp_setup_clear( struct setup_context *setup,
265 const float *color,
266 double depth,
267 unsigned stencil,
268 unsigned flags )
269 {
270 struct lp_scene *scene = lp_setup_get_current_scene(setup);
271 unsigned i;
272
273 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
274
275
276 if (flags & PIPE_CLEAR_COLOR) {
277 for (i = 0; i < 4; ++i)
278 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
279 }
280
281 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
282 setup->clear.zstencil.clear_zstencil =
283 util_pack_z_stencil(setup->fb.zsbuf->format,
284 depth,
285 stencil);
286 }
287
288 if (setup->state == SETUP_ACTIVE) {
289 /* Add the clear to existing scene. In the unusual case where
290 * both color and depth-stencil are being cleared when there's
291 * already been some rendering, we could discard the currently
292 * binned scene and start again, but I don't see that as being
293 * a common usage.
294 */
295 if (flags & PIPE_CLEAR_COLOR)
296 lp_scene_bin_everywhere( scene,
297 lp_rast_clear_color,
298 setup->clear.color );
299
300 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
301 lp_scene_bin_everywhere( scene,
302 lp_rast_clear_zstencil,
303 setup->clear.zstencil );
304 }
305 else {
306 /* Put ourselves into the 'pre-clear' state, specifically to try
307 * and accumulate multiple clears to color and depth_stencil
308 * buffers which the app or state-tracker might issue
309 * separately.
310 */
311 set_scene_state( setup, SETUP_CLEARED );
312
313 setup->clear.flags |= flags;
314 }
315 }
316
317
318 /**
319 * Emit a fence.
320 */
321 struct pipe_fence_handle *
322 lp_setup_fence( struct setup_context *setup )
323 {
324 struct lp_scene *scene = lp_setup_get_current_scene(setup);
325 const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
326 struct lp_fence *fence = lp_fence_create(rank);
327
328 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
329
330 set_scene_state( setup, SETUP_ACTIVE );
331
332 /* insert the fence into all command bins */
333 lp_scene_bin_everywhere( scene,
334 lp_rast_fence,
335 lp_rast_arg_fence(fence) );
336
337 return (struct pipe_fence_handle *) fence;
338 }
339
340
341 void
342 lp_setup_set_triangle_state( struct setup_context *setup,
343 unsigned cull_mode,
344 boolean ccw_is_frontface,
345 boolean scissor )
346 {
347 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
348
349 setup->ccw_is_frontface = ccw_is_frontface;
350 setup->cullmode = cull_mode;
351 setup->triangle = first_triangle;
352 setup->scissor_test = scissor;
353 }
354
355
356
357 void
358 lp_setup_set_fs_inputs( struct setup_context *setup,
359 const struct lp_shader_input *input,
360 unsigned nr )
361 {
362 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
363
364 memcpy( setup->fs.input, input, nr * sizeof input[0] );
365 setup->fs.nr_inputs = nr;
366 }
367
368 void
369 lp_setup_set_fs_functions( struct setup_context *setup,
370 lp_jit_frag_func jit_function0,
371 lp_jit_frag_func jit_function1,
372 boolean opaque )
373 {
374 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
375 /* FIXME: reference count */
376
377 setup->fs.current.jit_function[0] = jit_function0;
378 setup->fs.current.jit_function[1] = jit_function1;
379 setup->fs.current.opaque = opaque;
380 setup->dirty |= LP_SETUP_NEW_FS;
381 }
382
383 void
384 lp_setup_set_fs_constants(struct setup_context *setup,
385 struct pipe_buffer *buffer)
386 {
387 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
388
389 pipe_buffer_reference(&setup->constants.current, buffer);
390
391 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
392 }
393
394
395 void
396 lp_setup_set_alpha_ref_value( struct setup_context *setup,
397 float alpha_ref_value )
398 {
399 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
400
401 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
402 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
403 setup->dirty |= LP_SETUP_NEW_FS;
404 }
405 }
406
407 void
408 lp_setup_set_blend_color( struct setup_context *setup,
409 const struct pipe_blend_color *blend_color )
410 {
411 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
412
413 assert(blend_color);
414
415 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
416 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
417 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
418 }
419 }
420
421
422 void
423 lp_setup_set_scissor( struct setup_context *setup,
424 const struct pipe_scissor_state *scissor )
425 {
426 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
427
428 assert(scissor);
429
430 if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
431 setup->scissor.current = *scissor; /* struct copy */
432 setup->dirty |= LP_SETUP_NEW_SCISSOR;
433 }
434 }
435
436
437 void
438 lp_setup_set_flatshade_first( struct setup_context *setup,
439 boolean flatshade_first )
440 {
441 setup->flatshade_first = flatshade_first;
442 }
443
444
445 void
446 lp_setup_set_vertex_info( struct setup_context *setup,
447 struct vertex_info *vertex_info )
448 {
449 /* XXX: just silently holding onto the pointer:
450 */
451 setup->vertex_info = vertex_info;
452 }
453
454
455 /**
456 * Called during state validation when LP_NEW_TEXTURE is set.
457 */
458 void
459 lp_setup_set_sampler_textures( struct setup_context *setup,
460 unsigned num, struct pipe_texture **texture)
461 {
462 unsigned i;
463
464 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
465
466 assert(num <= PIPE_MAX_SAMPLERS);
467
468 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
469 struct pipe_texture *tex = i < num ? texture[i] : NULL;
470
471 if(tex) {
472 struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
473 struct lp_jit_texture *jit_tex;
474 jit_tex = &setup->fs.current.jit_context.textures[i];
475 jit_tex->width = tex->width0;
476 jit_tex->height = tex->height0;
477 jit_tex->stride = lp_tex->stride[0];
478 if(!lp_tex->dt) {
479 jit_tex->data = lp_tex->data;
480 }
481 else {
482 /*
483 * XXX: Where should this be unmapped?
484 */
485
486 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
487 struct llvmpipe_winsys *winsys = screen->winsys;
488 jit_tex->data = winsys->displaytarget_map(winsys, lp_tex->dt,
489 PIPE_BUFFER_USAGE_CPU_READ);
490 assert(jit_tex->data);
491 }
492
493 /* the scene references this texture */
494 {
495 struct lp_scene *scene = lp_setup_get_current_scene(setup);
496 lp_scene_texture_reference(scene, tex);
497 }
498 }
499 }
500
501 setup->dirty |= LP_SETUP_NEW_FS;
502 }
503
504
505 /**
506 * Is the given texture referenced by any scene?
507 * Note: we have to check all scenes including any scenes currently
508 * being rendered and the current scene being built.
509 */
510 unsigned
511 lp_setup_is_texture_referenced( const struct setup_context *setup,
512 const struct pipe_texture *texture )
513 {
514 unsigned i;
515
516 /* check the render targets */
517 for (i = 0; i < setup->fb.nr_cbufs; i++) {
518 if (setup->fb.cbufs[i]->texture == texture)
519 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
520 }
521 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
522 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
523 }
524
525 /* check textures referenced by the scene */
526 for (i = 0; i < Elements(setup->scenes); i++) {
527 if (lp_scene_is_texture_referenced(setup->scenes[i], texture)) {
528 return PIPE_REFERENCED_FOR_READ;
529 }
530 }
531
532 return PIPE_UNREFERENCED;
533 }
534
535
536 /**
537 * Called by vbuf code when we're about to draw something.
538 */
539 void
540 lp_setup_update_state( struct setup_context *setup )
541 {
542 struct lp_scene *scene = lp_setup_get_current_scene(setup);
543
544 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
545
546 assert(setup->fs.current.jit_function);
547
548 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
549 uint8_t *stored;
550 unsigned i, j;
551
552 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
553
554 /* smear each blend color component across 16 ubyte elements */
555 for (i = 0; i < 4; ++i) {
556 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
557 for (j = 0; j < 16; ++j)
558 stored[i*16 + j] = c;
559 }
560
561 setup->blend_color.stored = stored;
562
563 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
564 setup->dirty |= LP_SETUP_NEW_FS;
565 }
566
567 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
568 float *stored;
569
570 stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
571
572 stored[0] = (float) setup->scissor.current.minx;
573 stored[1] = (float) setup->scissor.current.miny;
574 stored[2] = (float) setup->scissor.current.maxx;
575 stored[3] = (float) setup->scissor.current.maxy;
576
577 setup->scissor.stored = stored;
578
579 setup->fs.current.jit_context.scissor_xmin = stored[0];
580 setup->fs.current.jit_context.scissor_ymin = stored[1];
581 setup->fs.current.jit_context.scissor_xmax = stored[2];
582 setup->fs.current.jit_context.scissor_ymax = stored[3];
583
584 setup->dirty |= LP_SETUP_NEW_FS;
585 }
586
587 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
588 struct pipe_buffer *buffer = setup->constants.current;
589
590 if(buffer) {
591 unsigned current_size = buffer->size;
592 const void *current_data = llvmpipe_buffer(buffer)->data;
593
594 /* TODO: copy only the actually used constants? */
595
596 if(setup->constants.stored_size != current_size ||
597 !setup->constants.stored_data ||
598 memcmp(setup->constants.stored_data,
599 current_data,
600 current_size) != 0) {
601 void *stored;
602
603 stored = lp_scene_alloc(scene, current_size);
604 if(stored) {
605 memcpy(stored,
606 current_data,
607 current_size);
608 setup->constants.stored_size = current_size;
609 setup->constants.stored_data = stored;
610 }
611 }
612 }
613 else {
614 setup->constants.stored_size = 0;
615 setup->constants.stored_data = NULL;
616 }
617
618 setup->fs.current.jit_context.constants = setup->constants.stored_data;
619 setup->dirty |= LP_SETUP_NEW_FS;
620 }
621
622
623 if(setup->dirty & LP_SETUP_NEW_FS) {
624 if(!setup->fs.stored ||
625 memcmp(setup->fs.stored,
626 &setup->fs.current,
627 sizeof setup->fs.current) != 0) {
628 /* The fs state that's been stored in the scene is different from
629 * the new, current state. So allocate a new lp_rast_state object
630 * and append it to the bin's setup data buffer.
631 */
632 struct lp_rast_state *stored =
633 (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
634 if(stored) {
635 memcpy(stored,
636 &setup->fs.current,
637 sizeof setup->fs.current);
638 setup->fs.stored = stored;
639
640 /* put the state-set command into all bins */
641 lp_scene_bin_state_command( scene,
642 lp_rast_set_state,
643 lp_rast_arg_state(setup->fs.stored) );
644 }
645 }
646 }
647
648 setup->dirty = 0;
649
650 assert(setup->fs.stored);
651 }
652
653
654
655 /* Only caller is lp_setup_vbuf_destroy()
656 */
657 void
658 lp_setup_destroy( struct setup_context *setup )
659 {
660 reset_context( setup );
661
662 pipe_buffer_reference(&setup->constants.current, NULL);
663
664 /* free the scenes in the 'empty' queue */
665 while (1) {
666 struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
667 if (!scene)
668 break;
669 lp_scene_destroy(scene);
670 }
671
672 lp_rast_destroy( setup->rast );
673
674 FREE( setup );
675 }
676
677
678 /**
679 * Create a new primitive tiling engine. Plug it into the backend of
680 * the draw module. Currently also creates a rasterizer to use with
681 * it.
682 */
683 struct setup_context *
684 lp_setup_create( struct pipe_screen *screen,
685 struct draw_context *draw )
686 {
687 unsigned i;
688 struct setup_context *setup = CALLOC_STRUCT(setup_context);
689
690 if (!setup)
691 return NULL;
692
693 lp_setup_init_vbuf(setup);
694
695 setup->empty_scenes = lp_scene_queue_create();
696 if (!setup->empty_scenes)
697 goto fail;
698
699 setup->rast = lp_rast_create( screen, setup->empty_scenes );
700 if (!setup->rast)
701 goto fail;
702
703 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
704 if (!setup->vbuf)
705 goto fail;
706
707 draw_set_rasterize_stage(draw, setup->vbuf);
708 draw_set_render(draw, &setup->base);
709
710 /* create some empty scenes */
711 for (i = 0; i < MAX_SCENES; i++) {
712 setup->scenes[i] = lp_scene_create();
713 lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
714 }
715
716 setup->triangle = first_triangle;
717 setup->line = first_line;
718 setup->point = first_point;
719
720 setup->dirty = ~0;
721
722 return setup;
723
724 fail:
725 if (setup->rast)
726 lp_rast_destroy( setup->rast );
727
728 if (setup->vbuf)
729 ;
730
731 if (setup->empty_scenes)
732 lp_scene_queue_destroy(setup->empty_scenes);
733
734 FREE(setup);
735 return NULL;
736 }
737