llvmpipe: fence debugging, add llvmpipe_finish
[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 <limits.h>
36
37 #include "pipe/p_defines.h"
38 #include "util/u_framebuffer.h"
39 #include "util/u_inlines.h"
40 #include "util/u_memory.h"
41 #include "util/u_pack_color.h"
42 #include "lp_context.h"
43 #include "lp_memory.h"
44 #include "lp_scene.h"
45 #include "lp_scene_queue.h"
46 #include "lp_texture.h"
47 #include "lp_debug.h"
48 #include "lp_fence.h"
49 #include "lp_query.h"
50 #include "lp_rast.h"
51 #include "lp_setup_context.h"
52 #include "lp_screen.h"
53 #include "lp_state.h"
54 #include "state_tracker/sw_winsys.h"
55
56 #include "draw/draw_context.h"
57 #include "draw/draw_vbuf.h"
58
59
60 static void set_scene_state( struct lp_setup_context *, enum setup_state );
61
62
63 struct lp_scene *
64 lp_setup_get_current_scene(struct lp_setup_context *setup)
65 {
66 if (!setup->scene) {
67 set_scene_state( setup, SETUP_EMPTY );
68 }
69 return setup->scene;
70 }
71
72
73 /**
74 * Check if the size of the current scene has exceeded the limit.
75 * If so, flush/render it.
76 */
77 static void
78 setup_check_scene_size_and_flush(struct lp_setup_context *setup)
79 {
80 if (setup->scene) {
81 struct lp_scene *scene = lp_setup_get_current_scene(setup);
82 unsigned size = lp_scene_get_size(scene);
83
84 if (size > LP_MAX_SCENE_SIZE) {
85 /*printf("LLVMPIPE: scene size = %u, flushing.\n", size);*/
86 set_scene_state( setup, SETUP_FLUSHED );
87 /*assert(lp_scene_get_size(scene) == 0);*/
88 }
89 }
90 }
91
92
93 static void
94 first_triangle( struct lp_setup_context *setup,
95 const float (*v0)[4],
96 const float (*v1)[4],
97 const float (*v2)[4])
98 {
99 set_scene_state( setup, SETUP_ACTIVE );
100 lp_setup_choose_triangle( setup );
101 setup->triangle( setup, v0, v1, v2 );
102 }
103
104 static void
105 first_line( struct lp_setup_context *setup,
106 const float (*v0)[4],
107 const float (*v1)[4])
108 {
109 set_scene_state( setup, SETUP_ACTIVE );
110 lp_setup_choose_line( setup );
111 setup->line( setup, v0, v1 );
112 }
113
114 static void
115 first_point( struct lp_setup_context *setup,
116 const float (*v0)[4])
117 {
118 set_scene_state( setup, SETUP_ACTIVE );
119 lp_setup_choose_point( setup );
120 setup->point( setup, v0 );
121 }
122
123 static void reset_context( struct lp_setup_context *setup )
124 {
125 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
126
127 /* Reset derived state */
128 setup->constants.stored_size = 0;
129 setup->constants.stored_data = NULL;
130 setup->fs.stored = NULL;
131 setup->dirty = ~0;
132
133 /* no current bin */
134 setup->scene = NULL;
135
136 /* Reset some state:
137 */
138 setup->clear.flags = 0;
139 setup->clear.clearzs.clearzs_mask = 0;
140
141 /* Have an explicit "start-binning" call and get rid of this
142 * pointer twiddling?
143 */
144 setup->line = first_line;
145 setup->point = first_point;
146 setup->triangle = first_triangle;
147 }
148
149
150 /** Rasterize all scene's bins */
151 static void
152 lp_setup_rasterize_scene( struct lp_setup_context *setup )
153 {
154 struct lp_scene *scene = lp_setup_get_current_scene(setup);
155 struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
156
157 pipe_mutex_lock(screen->rast_mutex);
158 lp_scene_rasterize(scene, screen->rast);
159 pipe_mutex_unlock(screen->rast_mutex);
160
161 reset_context( setup );
162
163 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
164 }
165
166
167
168 static void
169 begin_binning( struct lp_setup_context *setup )
170 {
171 struct lp_scene *scene = lp_setup_get_current_scene(setup);
172 boolean need_zsload = FALSE;
173 if (setup->fb.zsbuf &&
174 ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
175 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
176 need_zsload = TRUE;
177
178 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
179 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
180 need_zsload ? "clear": "load");
181
182 if (setup->fb.nr_cbufs) {
183 if (setup->clear.flags & PIPE_CLEAR_COLOR) {
184 lp_scene_bin_everywhere( scene,
185 lp_rast_clear_color,
186 setup->clear.color );
187 scene->has_color_clear = TRUE;
188 }
189 }
190
191 if (setup->fb.zsbuf) {
192 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
193 if (!need_zsload)
194 scene->has_depthstencil_clear = TRUE;
195 lp_scene_bin_everywhere( scene,
196 lp_rast_clear_zstencil,
197 lp_rast_arg_clearzs(&setup->clear.clearzs) );
198 }
199 }
200
201 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
202 }
203
204
205 /* This basically bins and then flushes any outstanding full-screen
206 * clears.
207 *
208 * TODO: fast path for fullscreen clears and no triangles.
209 */
210 static void
211 execute_clears( struct lp_setup_context *setup )
212 {
213 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
214
215 begin_binning( setup );
216 lp_setup_rasterize_scene( setup );
217 }
218
219
220 static void
221 set_scene_state( struct lp_setup_context *setup,
222 enum setup_state new_state )
223 {
224 unsigned old_state = setup->state;
225
226 if (old_state == new_state)
227 return;
228
229 LP_DBG(DEBUG_SETUP, "%s old %d new %d\n", __FUNCTION__, old_state, new_state);
230
231 switch (new_state) {
232 case SETUP_EMPTY:
233 assert(old_state == SETUP_FLUSHED);
234 assert(setup->scene == NULL);
235
236 /* wait for a free/empty scene
237 */
238 setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
239 assert(lp_scene_is_empty(setup->scene));
240 lp_scene_begin_binning(setup->scene,
241 &setup->fb );
242 break;
243
244 case SETUP_CLEARED:
245 assert(old_state == SETUP_EMPTY);
246 assert(setup->scene != NULL);
247 break;
248
249 case SETUP_ACTIVE:
250 assert(old_state == SETUP_EMPTY ||
251 old_state == SETUP_CLEARED);
252 assert(setup->scene != NULL);
253 begin_binning( setup );
254 break;
255
256 case SETUP_FLUSHED:
257 if (old_state == SETUP_CLEARED)
258 execute_clears( setup );
259 else
260 lp_setup_rasterize_scene( setup );
261 assert(setup->scene == NULL);
262 break;
263
264 default:
265 assert(0 && "invalid setup state mode");
266 }
267
268 setup->state = new_state;
269 }
270
271
272 /**
273 * \param flags bitmask of PIPE_FLUSH_x flags
274 */
275 void
276 lp_setup_flush( struct lp_setup_context *setup,
277 unsigned flags,
278 struct pipe_fence_handle **fence,
279 const char *reason)
280 {
281 LP_DBG(DEBUG_SETUP, "%s %s\n", __FUNCTION__, reason);
282
283 if (setup->scene) {
284 if (fence) {
285 /* if we're going to flush the setup/rasterization modules, emit
286 * a fence.
287 */
288 *fence = lp_setup_fence( setup );
289 }
290
291 }
292
293 set_scene_state( setup, SETUP_FLUSHED );
294 }
295
296
297 void
298 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
299 const struct pipe_framebuffer_state *fb )
300 {
301 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
302
303 /* Flush any old scene.
304 */
305 set_scene_state( setup, SETUP_FLUSHED );
306
307 /*
308 * Ensure the old scene is not reused.
309 */
310 assert(!setup->scene);
311
312 /* Set new state. This will be picked up later when we next need a
313 * scene.
314 */
315 util_copy_framebuffer_state(&setup->fb, fb);
316 }
317
318
319 void
320 lp_setup_clear( struct lp_setup_context *setup,
321 const float *color,
322 double depth,
323 unsigned stencil,
324 unsigned flags )
325 {
326 struct lp_scene *scene = lp_setup_get_current_scene(setup);
327 unsigned i;
328 boolean full_zs_clear = TRUE;
329 uint32_t mask = 0;
330
331 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
332
333
334 if (flags & PIPE_CLEAR_COLOR) {
335 for (i = 0; i < 4; ++i)
336 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
337 }
338
339 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
340 if (setup->fb.zsbuf &&
341 ((flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
342 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
343 full_zs_clear = FALSE;
344
345 if (full_zs_clear) {
346 setup->clear.clearzs.clearzs_value =
347 util_pack_z_stencil(setup->fb.zsbuf->format,
348 depth,
349 stencil);
350 setup->clear.clearzs.clearzs_mask = 0xffffffff;
351 }
352 else {
353 /* hmm */
354 uint32_t tmpval;
355 if (flags & PIPE_CLEAR_DEPTH) {
356 tmpval = util_pack_z(setup->fb.zsbuf->format,
357 depth);
358 switch (setup->fb.zsbuf->format) {
359 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
360 mask = 0xffffff;
361 break;
362 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
363 mask = 0xffffff00;
364 break;
365 default:
366 assert(0);
367 }
368 }
369 else {
370 switch (setup->fb.zsbuf->format) {
371 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
372 mask = 0xff000000;
373 tmpval = stencil << 24;
374 break;
375 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
376 mask = 0xff;
377 tmpval = stencil;
378 break;
379 default:
380 assert(0);
381 tmpval = 0;
382 }
383 }
384 setup->clear.clearzs.clearzs_mask |= mask;
385 setup->clear.clearzs.clearzs_value =
386 (setup->clear.clearzs.clearzs_value & ~mask) | (tmpval & mask);
387 }
388 }
389
390 if (setup->state == SETUP_ACTIVE) {
391 /* Add the clear to existing scene. In the unusual case where
392 * both color and depth-stencil are being cleared when there's
393 * already been some rendering, we could discard the currently
394 * binned scene and start again, but I don't see that as being
395 * a common usage.
396 */
397 if (flags & PIPE_CLEAR_COLOR) {
398 lp_scene_bin_everywhere( scene,
399 lp_rast_clear_color,
400 setup->clear.color );
401 scene->has_color_clear = TRUE;
402 }
403
404 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
405 if (full_zs_clear)
406 scene->has_depthstencil_clear = TRUE;
407 else
408 setup->clear.clearzs.clearzs_mask = mask;
409 lp_scene_bin_everywhere( scene,
410 lp_rast_clear_zstencil,
411 lp_rast_arg_clearzs(&setup->clear.clearzs) );
412
413
414 }
415
416 }
417 else {
418 /* Put ourselves into the 'pre-clear' state, specifically to try
419 * and accumulate multiple clears to color and depth_stencil
420 * buffers which the app or state-tracker might issue
421 * separately.
422 */
423 set_scene_state( setup, SETUP_CLEARED );
424
425 setup->clear.flags |= flags;
426 }
427 }
428
429
430 /**
431 * Emit a fence.
432 */
433 struct pipe_fence_handle *
434 lp_setup_fence( struct lp_setup_context *setup )
435 {
436 if (setup->scene == NULL)
437 return NULL;
438 else if (setup->num_threads == 0)
439 return NULL;
440 else
441 {
442 struct lp_scene *scene = lp_setup_get_current_scene(setup);
443 const unsigned rank = setup->num_threads;
444
445 set_scene_state( setup, SETUP_ACTIVE );
446
447 assert(scene->fence == NULL);
448
449 /* The caller gets a reference, we keep a copy too, so need to
450 * bump the refcount:
451 */
452 lp_fence_reference(&scene->fence, lp_fence_create(rank));
453
454 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
455
456 return (struct pipe_fence_handle *) scene->fence;
457 }
458 }
459
460
461 void
462 lp_setup_set_triangle_state( struct lp_setup_context *setup,
463 unsigned cull_mode,
464 boolean ccw_is_frontface,
465 boolean scissor,
466 boolean gl_rasterization_rules)
467 {
468 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
469
470 setup->ccw_is_frontface = ccw_is_frontface;
471 setup->cullmode = cull_mode;
472 setup->triangle = first_triangle;
473 setup->scissor_test = scissor;
474 setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
475 }
476
477
478
479 void
480 lp_setup_set_fs_inputs( struct lp_setup_context *setup,
481 const struct lp_shader_input *input,
482 unsigned nr )
483 {
484 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
485
486 memcpy( setup->fs.input, input, nr * sizeof input[0] );
487 setup->fs.nr_inputs = nr;
488 }
489
490 void
491 lp_setup_set_fs_variant( struct lp_setup_context *setup,
492 struct lp_fragment_shader_variant *variant)
493 {
494 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
495 variant);
496 /* FIXME: reference count */
497
498 setup->fs.current.variant = variant;
499 setup->dirty |= LP_SETUP_NEW_FS;
500 }
501
502 void
503 lp_setup_set_fs_constants(struct lp_setup_context *setup,
504 struct pipe_resource *buffer)
505 {
506 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
507
508 pipe_resource_reference(&setup->constants.current, buffer);
509
510 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
511 }
512
513
514 void
515 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
516 float alpha_ref_value )
517 {
518 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
519
520 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
521 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
522 setup->dirty |= LP_SETUP_NEW_FS;
523 }
524 }
525
526 void
527 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
528 const ubyte refs[2] )
529 {
530 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
531
532 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
533 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
534 setup->fs.current.jit_context.stencil_ref_front = refs[0];
535 setup->fs.current.jit_context.stencil_ref_back = refs[1];
536 setup->dirty |= LP_SETUP_NEW_FS;
537 }
538 }
539
540 void
541 lp_setup_set_blend_color( struct lp_setup_context *setup,
542 const struct pipe_blend_color *blend_color )
543 {
544 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
545
546 assert(blend_color);
547
548 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
549 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
550 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
551 }
552 }
553
554
555 void
556 lp_setup_set_scissor( struct lp_setup_context *setup,
557 const struct pipe_scissor_state *scissor )
558 {
559 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
560
561 assert(scissor);
562
563 if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
564 setup->scissor.current = *scissor; /* struct copy */
565 setup->dirty |= LP_SETUP_NEW_SCISSOR;
566 }
567 }
568
569
570 void
571 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
572 boolean flatshade_first )
573 {
574 setup->flatshade_first = flatshade_first;
575 }
576
577
578 void
579 lp_setup_set_vertex_info( struct lp_setup_context *setup,
580 struct vertex_info *vertex_info )
581 {
582 /* XXX: just silently holding onto the pointer:
583 */
584 setup->vertex_info = vertex_info;
585 }
586
587
588 /**
589 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
590 */
591 void
592 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
593 unsigned num,
594 struct pipe_sampler_view **views)
595 {
596 unsigned i;
597
598 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
599
600 assert(num <= PIPE_MAX_SAMPLERS);
601
602 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
603 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
604
605 if(view) {
606 struct pipe_resource *tex = view->texture;
607 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
608 struct lp_jit_texture *jit_tex;
609 jit_tex = &setup->fs.current.jit_context.textures[i];
610 jit_tex->width = tex->width0;
611 jit_tex->height = tex->height0;
612 jit_tex->depth = tex->depth0;
613 jit_tex->last_level = tex->last_level;
614
615 /* We're referencing the texture's internal data, so save a
616 * reference to it.
617 */
618 pipe_resource_reference(&setup->fs.current_tex[i], tex);
619
620 if (!lp_tex->dt) {
621 /* regular texture - setup array of mipmap level pointers */
622 int j;
623 for (j = 0; j <= tex->last_level; j++) {
624 jit_tex->data[j] =
625 llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
626 LP_TEX_LAYOUT_LINEAR);
627 jit_tex->row_stride[j] = lp_tex->row_stride[j];
628 jit_tex->img_stride[j] = lp_tex->img_stride[j];
629
630 if (!jit_tex->data[j]) {
631 /* out of memory - use dummy tile memory */
632 jit_tex->data[j] = lp_dummy_tile;
633 jit_tex->width = TILE_SIZE;
634 jit_tex->height = TILE_SIZE;
635 jit_tex->depth = 1;
636 jit_tex->last_level = 0;
637 jit_tex->row_stride[j] = 0;
638 jit_tex->img_stride[j] = 0;
639 }
640 }
641 }
642 else {
643 /* display target texture/surface */
644 /*
645 * XXX: Where should this be unmapped?
646 */
647 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
648 struct sw_winsys *winsys = screen->winsys;
649 jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
650 PIPE_TRANSFER_READ);
651 jit_tex->row_stride[0] = lp_tex->row_stride[0];
652 jit_tex->img_stride[0] = lp_tex->img_stride[0];
653 assert(jit_tex->data[0]);
654 }
655 }
656 }
657
658 setup->dirty |= LP_SETUP_NEW_FS;
659 }
660
661
662 /**
663 * Is the given texture referenced by any scene?
664 * Note: we have to check all scenes including any scenes currently
665 * being rendered and the current scene being built.
666 */
667 unsigned
668 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
669 const struct pipe_resource *texture )
670 {
671 unsigned i;
672
673 /* check the render targets */
674 for (i = 0; i < setup->fb.nr_cbufs; i++) {
675 if (setup->fb.cbufs[i]->texture == texture)
676 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
677 }
678 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
679 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
680 }
681
682 /* check textures referenced by the scene */
683 for (i = 0; i < Elements(setup->scenes); i++) {
684 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
685 return PIPE_REFERENCED_FOR_READ;
686 }
687 }
688
689 return PIPE_UNREFERENCED;
690 }
691
692
693 /**
694 * Called by vbuf code when we're about to draw something.
695 */
696 void
697 lp_setup_update_state( struct lp_setup_context *setup )
698 {
699 struct lp_scene *scene;
700
701 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
702
703 setup_check_scene_size_and_flush(setup);
704
705 scene = lp_setup_get_current_scene(setup);
706
707 assert(setup->fs.current.variant);
708
709 /* Some of the 'draw' pipeline stages may have changed some driver state.
710 * Make sure we've processed those state changes before anything else.
711 *
712 * XXX this is the only place where llvmpipe_context is used in the
713 * setup code. This may get refactored/changed...
714 */
715 {
716 struct llvmpipe_context *lp = llvmpipe_context(scene->pipe);
717 if (lp->dirty) {
718 llvmpipe_update_derived(lp);
719 }
720 assert(lp->dirty == 0);
721 }
722
723 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
724 uint8_t *stored;
725 unsigned i, j;
726
727 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
728
729 if (stored) {
730 /* smear each blend color component across 16 ubyte elements */
731 for (i = 0; i < 4; ++i) {
732 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
733 for (j = 0; j < 16; ++j)
734 stored[i*16 + j] = c;
735 }
736
737 setup->blend_color.stored = stored;
738
739 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
740 }
741
742 setup->dirty |= LP_SETUP_NEW_FS;
743 }
744
745 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
746 struct pipe_resource *buffer = setup->constants.current;
747
748 if(buffer) {
749 unsigned current_size = buffer->width0;
750 const void *current_data = llvmpipe_resource_data(buffer);
751
752 /* TODO: copy only the actually used constants? */
753
754 if(setup->constants.stored_size != current_size ||
755 !setup->constants.stored_data ||
756 memcmp(setup->constants.stored_data,
757 current_data,
758 current_size) != 0) {
759 void *stored;
760
761 stored = lp_scene_alloc(scene, current_size);
762 if(stored) {
763 memcpy(stored,
764 current_data,
765 current_size);
766 setup->constants.stored_size = current_size;
767 setup->constants.stored_data = stored;
768 }
769 }
770 }
771 else {
772 setup->constants.stored_size = 0;
773 setup->constants.stored_data = NULL;
774 }
775
776 setup->fs.current.jit_context.constants = setup->constants.stored_data;
777 setup->dirty |= LP_SETUP_NEW_FS;
778 }
779
780
781 if(setup->dirty & LP_SETUP_NEW_FS) {
782 if(!setup->fs.stored ||
783 memcmp(setup->fs.stored,
784 &setup->fs.current,
785 sizeof setup->fs.current) != 0) {
786 /* The fs state that's been stored in the scene is different from
787 * the new, current state. So allocate a new lp_rast_state object
788 * and append it to the bin's setup data buffer.
789 */
790 uint i;
791 struct lp_rast_state *stored =
792 (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
793 if(stored) {
794 memcpy(stored,
795 &setup->fs.current,
796 sizeof setup->fs.current);
797 setup->fs.stored = stored;
798 }
799
800 /* The scene now references the textures in the rasterization
801 * state record. Note that now.
802 */
803 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
804 if (setup->fs.current_tex[i])
805 lp_scene_add_resource_reference(scene, setup->fs.current_tex[i]);
806 }
807 }
808 }
809
810 setup->dirty = 0;
811
812 assert(setup->fs.stored);
813 }
814
815
816
817 /* Only caller is lp_setup_vbuf_destroy()
818 */
819 void
820 lp_setup_destroy( struct lp_setup_context *setup )
821 {
822 uint i;
823
824 reset_context( setup );
825
826 util_unreference_framebuffer_state(&setup->fb);
827
828 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
829 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
830 }
831
832 pipe_resource_reference(&setup->constants.current, NULL);
833
834 /* free the scenes in the 'empty' queue */
835 while (1) {
836 struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
837 if (!scene)
838 break;
839 lp_scene_destroy(scene);
840 }
841
842 lp_scene_queue_destroy(setup->empty_scenes);
843
844 FREE( setup );
845 }
846
847
848 /**
849 * Create a new primitive tiling engine. Plug it into the backend of
850 * the draw module. Currently also creates a rasterizer to use with
851 * it.
852 */
853 struct lp_setup_context *
854 lp_setup_create( struct pipe_context *pipe,
855 struct draw_context *draw )
856 {
857 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
858 struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
859 unsigned i;
860
861 if (!setup)
862 return NULL;
863
864 lp_setup_init_vbuf(setup);
865
866 setup->empty_scenes = lp_scene_queue_create();
867 if (!setup->empty_scenes)
868 goto fail;
869
870 setup->num_threads = screen->num_threads;
871 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
872 if (!setup->vbuf)
873 goto fail;
874
875 draw_set_rasterize_stage(draw, setup->vbuf);
876 draw_set_render(draw, &setup->base);
877
878 /* create some empty scenes */
879 for (i = 0; i < MAX_SCENES; i++) {
880 setup->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
881
882 lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
883 }
884
885 setup->triangle = first_triangle;
886 setup->line = first_line;
887 setup->point = first_point;
888
889 setup->dirty = ~0;
890
891 return setup;
892
893 fail:
894 if (setup->vbuf)
895 ;
896
897 if (setup->empty_scenes)
898 lp_scene_queue_destroy(setup->empty_scenes);
899
900 FREE(setup);
901 return NULL;
902 }
903
904
905 /**
906 * Put a BeginQuery command into all bins.
907 */
908 void
909 lp_setup_begin_query(struct lp_setup_context *setup,
910 struct llvmpipe_query *pq)
911 {
912 struct lp_scene * scene = lp_setup_get_current_scene(setup);
913 union lp_rast_cmd_arg cmd_arg;
914
915 /* init the query to its beginning state */
916 pq->done = FALSE;
917 pq->tile_count = 0;
918 pq->num_tiles = scene->tiles_x * scene->tiles_y;
919 assert(pq->num_tiles > 0);
920
921 memset(pq->count, 0, sizeof(pq->count)); /* reset all counters */
922
923 set_scene_state( setup, SETUP_ACTIVE );
924
925 cmd_arg.query_obj = pq;
926 lp_scene_bin_everywhere(scene, lp_rast_begin_query, cmd_arg);
927 pq->binned = TRUE;
928 }
929
930
931 /**
932 * Put an EndQuery command into all bins.
933 */
934 void
935 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
936 {
937 struct lp_scene * scene = lp_setup_get_current_scene(setup);
938 union lp_rast_cmd_arg cmd_arg;
939
940 set_scene_state( setup, SETUP_ACTIVE );
941
942 cmd_arg.query_obj = pq;
943 lp_scene_bin_everywhere(scene, lp_rast_end_query, cmd_arg);
944 }