llvmpipe: Plug fence leaks.
[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_texture.h"
46 #include "lp_debug.h"
47 #include "lp_fence.h"
48 #include "lp_query.h"
49 #include "lp_rast.h"
50 #include "lp_setup_context.h"
51 #include "lp_screen.h"
52 #include "lp_state.h"
53 #include "state_tracker/sw_winsys.h"
54
55 #include "draw/draw_context.h"
56 #include "draw/draw_vbuf.h"
57
58
59 static boolean set_scene_state( struct lp_setup_context *, enum setup_state,
60 const char *reason);
61 static boolean try_update_scene_state( struct lp_setup_context *setup );
62
63
64 static void
65 lp_setup_get_empty_scene(struct lp_setup_context *setup)
66 {
67 assert(setup->scene == NULL);
68
69 setup->scene_idx++;
70 setup->scene_idx %= Elements(setup->scenes);
71
72 setup->scene = setup->scenes[setup->scene_idx];
73
74 if (setup->scene->fence) {
75 if (LP_DEBUG & DEBUG_SETUP)
76 debug_printf("%s: wait for scene %d\n",
77 __FUNCTION__, setup->scene->fence->id);
78
79 lp_fence_wait(setup->scene->fence);
80 }
81
82 lp_scene_begin_binning(setup->scene, &setup->fb);
83
84 }
85
86
87 static void
88 first_triangle( struct lp_setup_context *setup,
89 const float (*v0)[4],
90 const float (*v1)[4],
91 const float (*v2)[4])
92 {
93 assert(setup->state == SETUP_ACTIVE);
94 lp_setup_choose_triangle( setup );
95 setup->triangle( setup, v0, v1, v2 );
96 }
97
98 static void
99 first_line( struct lp_setup_context *setup,
100 const float (*v0)[4],
101 const float (*v1)[4])
102 {
103 assert(setup->state == SETUP_ACTIVE);
104 lp_setup_choose_line( setup );
105 setup->line( setup, v0, v1 );
106 }
107
108 static void
109 first_point( struct lp_setup_context *setup,
110 const float (*v0)[4])
111 {
112 assert(setup->state == SETUP_ACTIVE);
113 lp_setup_choose_point( setup );
114 setup->point( setup, v0 );
115 }
116
117 void lp_setup_reset( struct lp_setup_context *setup )
118 {
119 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
120
121 /* Reset derived state */
122 setup->constants.stored_size = 0;
123 setup->constants.stored_data = NULL;
124 setup->fs.stored = NULL;
125 setup->dirty = ~0;
126
127 /* no current bin */
128 setup->scene = NULL;
129
130 /* Reset some state:
131 */
132 memset(&setup->clear, 0, sizeof setup->clear);
133
134 /* Have an explicit "start-binning" call and get rid of this
135 * pointer twiddling?
136 */
137 setup->line = first_line;
138 setup->point = first_point;
139 setup->triangle = first_triangle;
140 }
141
142
143 /** Rasterize all scene's bins */
144 static void
145 lp_setup_rasterize_scene( struct lp_setup_context *setup )
146 {
147 struct lp_scene *scene = setup->scene;
148 struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
149
150 lp_scene_end_binning(scene);
151
152 lp_fence_reference(&setup->last_fence, scene->fence);
153
154 if (setup->last_fence)
155 setup->last_fence->issued = TRUE;
156
157 pipe_mutex_lock(screen->rast_mutex);
158 lp_rast_queue_scene(screen->rast, scene);
159 lp_rast_finish(screen->rast);
160 pipe_mutex_unlock(screen->rast_mutex);
161
162 lp_scene_end_rasterization(setup->scene);
163 lp_setup_reset( setup );
164
165 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
166 }
167
168
169
170 static boolean
171 begin_binning( struct lp_setup_context *setup )
172 {
173 struct lp_scene *scene = setup->scene;
174 boolean need_zsload = FALSE;
175 boolean ok;
176 unsigned i, j;
177
178 assert(scene);
179 assert(scene->fence == NULL);
180
181 /* Always create a fence:
182 */
183 scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
184 if (!scene->fence)
185 return FALSE;
186
187 /* Initialize the bin flags and x/y coords:
188 */
189 for (i = 0; i < scene->tiles_x; i++) {
190 for (j = 0; j < scene->tiles_y; j++) {
191 scene->tile[i][j].x = i;
192 scene->tile[i][j].y = j;
193 }
194 }
195
196 ok = try_update_scene_state(setup);
197 if (!ok)
198 return FALSE;
199
200 if (setup->fb.zsbuf &&
201 ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
202 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
203 need_zsload = TRUE;
204
205 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
206 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
207 need_zsload ? "clear": "load");
208
209 if (setup->fb.nr_cbufs) {
210 if (setup->clear.flags & PIPE_CLEAR_COLOR) {
211 ok = lp_scene_bin_everywhere( scene,
212 LP_RAST_OP_CLEAR_COLOR,
213 setup->clear.color );
214 if (!ok)
215 return FALSE;
216 }
217 }
218
219 if (setup->fb.zsbuf) {
220 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
221 if (!need_zsload)
222 scene->has_depthstencil_clear = TRUE;
223
224 ok = lp_scene_bin_everywhere( scene,
225 LP_RAST_OP_CLEAR_ZSTENCIL,
226 lp_rast_arg_clearzs(
227 setup->clear.zsvalue,
228 setup->clear.zsmask));
229 if (!ok)
230 return FALSE;
231 }
232 }
233
234 if (setup->active_query) {
235 ok = lp_scene_bin_everywhere( scene,
236 LP_RAST_OP_BEGIN_QUERY,
237 lp_rast_arg_query(setup->active_query) );
238 if (!ok)
239 return FALSE;
240 }
241
242 setup->clear.flags = 0;
243 setup->clear.zsmask = 0;
244 setup->clear.zsvalue = 0;
245
246 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
247 return TRUE;
248 }
249
250
251 /* This basically bins and then flushes any outstanding full-screen
252 * clears.
253 *
254 * TODO: fast path for fullscreen clears and no triangles.
255 */
256 static boolean
257 execute_clears( struct lp_setup_context *setup )
258 {
259 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
260
261 return begin_binning( setup );
262 }
263
264 const char *states[] = {
265 "FLUSHED",
266 "CLEARED",
267 "ACTIVE "
268 };
269
270
271 static boolean
272 set_scene_state( struct lp_setup_context *setup,
273 enum setup_state new_state,
274 const char *reason)
275 {
276 unsigned old_state = setup->state;
277
278 if (old_state == new_state)
279 return TRUE;
280
281 if (LP_DEBUG & DEBUG_SCENE) {
282 debug_printf("%s old %s new %s%s%s\n",
283 __FUNCTION__,
284 states[old_state],
285 states[new_state],
286 (new_state == SETUP_FLUSHED) ? ": " : "",
287 (new_state == SETUP_FLUSHED) ? reason : "");
288
289 if (new_state == SETUP_FLUSHED && setup->scene)
290 lp_debug_draw_bins_by_cmd_length(setup->scene);
291 }
292
293 /* wait for a free/empty scene
294 */
295 if (old_state == SETUP_FLUSHED)
296 lp_setup_get_empty_scene(setup);
297
298 switch (new_state) {
299 case SETUP_CLEARED:
300 break;
301
302 case SETUP_ACTIVE:
303 if (!begin_binning( setup ))
304 goto fail;
305 break;
306
307 case SETUP_FLUSHED:
308 if (old_state == SETUP_CLEARED)
309 if (!execute_clears( setup ))
310 goto fail;
311
312 lp_setup_rasterize_scene( setup );
313 assert(setup->scene == NULL);
314 break;
315
316 default:
317 assert(0 && "invalid setup state mode");
318 goto fail;
319 }
320
321 setup->state = new_state;
322 return TRUE;
323
324 fail:
325 if (setup->scene) {
326 lp_scene_end_rasterization(setup->scene);
327 setup->scene = NULL;
328 }
329
330 setup->state = SETUP_FLUSHED;
331 lp_setup_reset( setup );
332 return FALSE;
333 }
334
335
336 /**
337 * \param flags bitmask of PIPE_FLUSH_x flags
338 */
339 void
340 lp_setup_flush( struct lp_setup_context *setup,
341 unsigned flags,
342 struct pipe_fence_handle **fence,
343 const char *reason)
344 {
345 set_scene_state( setup, SETUP_FLUSHED, reason );
346
347 if (fence) {
348 lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
349 }
350 }
351
352
353 void
354 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
355 const struct pipe_framebuffer_state *fb )
356 {
357 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
358
359 /* Flush any old scene.
360 */
361 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
362
363 /*
364 * Ensure the old scene is not reused.
365 */
366 assert(!setup->scene);
367
368 /* Set new state. This will be picked up later when we next need a
369 * scene.
370 */
371 util_copy_framebuffer_state(&setup->fb, fb);
372 setup->framebuffer.x0 = 0;
373 setup->framebuffer.y0 = 0;
374 setup->framebuffer.x1 = fb->width-1;
375 setup->framebuffer.y1 = fb->height-1;
376 setup->dirty |= LP_SETUP_NEW_SCISSOR;
377 }
378
379
380 static boolean
381 lp_setup_try_clear( struct lp_setup_context *setup,
382 const float *color,
383 double depth,
384 unsigned stencil,
385 unsigned flags )
386 {
387 uint32_t zsmask = 0;
388 uint32_t zsvalue = 0;
389 union lp_rast_cmd_arg color_arg;
390 unsigned i;
391
392 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
393
394 if (flags & PIPE_CLEAR_COLOR) {
395 for (i = 0; i < 4; i++)
396 color_arg.clear_color[i] = float_to_ubyte(color[i]);
397 }
398
399 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
400 uint32_t zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
401 uint32_t smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
402
403 zsvalue = util_pack_z_stencil(setup->fb.zsbuf->format,
404 depth,
405 stencil);
406
407
408 zsmask = util_pack_mask_z_stencil(setup->fb.zsbuf->format,
409 zmask,
410 smask);
411
412 zsvalue &= zsmask;
413 }
414
415 if (setup->state == SETUP_ACTIVE) {
416 struct lp_scene *scene = setup->scene;
417
418 /* Add the clear to existing scene. In the unusual case where
419 * both color and depth-stencil are being cleared when there's
420 * already been some rendering, we could discard the currently
421 * binned scene and start again, but I don't see that as being
422 * a common usage.
423 */
424 if (flags & PIPE_CLEAR_COLOR) {
425 if (!lp_scene_bin_everywhere( scene,
426 LP_RAST_OP_CLEAR_COLOR,
427 color_arg ))
428 return FALSE;
429 }
430
431 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
432 if (!lp_scene_bin_everywhere( scene,
433 LP_RAST_OP_CLEAR_ZSTENCIL,
434 lp_rast_arg_clearzs(zsvalue, zsmask) ))
435 return FALSE;
436 }
437 }
438 else {
439 /* Put ourselves into the 'pre-clear' state, specifically to try
440 * and accumulate multiple clears to color and depth_stencil
441 * buffers which the app or state-tracker might issue
442 * separately.
443 */
444 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
445
446 setup->clear.flags |= flags;
447
448 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
449 setup->clear.zsmask |= zsmask;
450 setup->clear.zsvalue =
451 (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
452 }
453
454 if (flags & PIPE_CLEAR_COLOR) {
455 memcpy(setup->clear.color.clear_color,
456 &color_arg,
457 sizeof setup->clear.color.clear_color);
458 }
459 }
460
461 return TRUE;
462 }
463
464 void
465 lp_setup_clear( struct lp_setup_context *setup,
466 const float *color,
467 double depth,
468 unsigned stencil,
469 unsigned flags )
470 {
471 if (!lp_setup_try_clear( setup, color, depth, stencil, flags )) {
472 lp_setup_flush(setup, 0, NULL, __FUNCTION__);
473
474 if (!lp_setup_try_clear( setup, color, depth, stencil, flags ))
475 assert(0);
476 }
477 }
478
479
480
481
482
483 void
484 lp_setup_set_triangle_state( struct lp_setup_context *setup,
485 unsigned cull_mode,
486 boolean ccw_is_frontface,
487 boolean scissor,
488 boolean gl_rasterization_rules)
489 {
490 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
491
492 setup->ccw_is_frontface = ccw_is_frontface;
493 setup->cullmode = cull_mode;
494 setup->triangle = first_triangle;
495 setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
496
497 if (setup->scissor_test != scissor) {
498 setup->dirty |= LP_SETUP_NEW_SCISSOR;
499 setup->scissor_test = scissor;
500 }
501 }
502
503 void
504 lp_setup_set_line_state( struct lp_setup_context *setup,
505 float line_width)
506 {
507 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
508
509 setup->line_width = line_width;
510 }
511
512 void
513 lp_setup_set_point_state( struct lp_setup_context *setup,
514 float point_size,
515 boolean point_size_per_vertex,
516 uint sprite_coord_enable,
517 uint sprite_coord_origin)
518 {
519 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
520
521 setup->point_size = point_size;
522 setup->sprite_coord_enable = sprite_coord_enable;
523 setup->sprite_coord_origin = sprite_coord_origin;
524 setup->point_size_per_vertex = point_size_per_vertex;
525 }
526
527 void
528 lp_setup_set_setup_variant( struct lp_setup_context *setup,
529 const struct lp_setup_variant *variant)
530 {
531 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
532
533 setup->setup.variant = variant;
534 }
535
536 void
537 lp_setup_set_fs_variant( struct lp_setup_context *setup,
538 struct lp_fragment_shader_variant *variant)
539 {
540 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
541 variant);
542 /* FIXME: reference count */
543
544 setup->fs.current.variant = variant;
545 setup->dirty |= LP_SETUP_NEW_FS;
546 }
547
548 void
549 lp_setup_set_fs_constants(struct lp_setup_context *setup,
550 struct pipe_resource *buffer)
551 {
552 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
553
554 pipe_resource_reference(&setup->constants.current, buffer);
555
556 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
557 }
558
559
560 void
561 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
562 float alpha_ref_value )
563 {
564 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
565
566 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
567 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
568 setup->dirty |= LP_SETUP_NEW_FS;
569 }
570 }
571
572 void
573 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
574 const ubyte refs[2] )
575 {
576 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
577
578 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
579 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
580 setup->fs.current.jit_context.stencil_ref_front = refs[0];
581 setup->fs.current.jit_context.stencil_ref_back = refs[1];
582 setup->dirty |= LP_SETUP_NEW_FS;
583 }
584 }
585
586 void
587 lp_setup_set_blend_color( struct lp_setup_context *setup,
588 const struct pipe_blend_color *blend_color )
589 {
590 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
591
592 assert(blend_color);
593
594 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
595 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
596 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
597 }
598 }
599
600
601 void
602 lp_setup_set_scissor( struct lp_setup_context *setup,
603 const struct pipe_scissor_state *scissor )
604 {
605 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
606
607 assert(scissor);
608
609 setup->scissor.x0 = scissor->minx;
610 setup->scissor.x1 = scissor->maxx-1;
611 setup->scissor.y0 = scissor->miny;
612 setup->scissor.y1 = scissor->maxy-1;
613 setup->dirty |= LP_SETUP_NEW_SCISSOR;
614 }
615
616
617 void
618 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
619 boolean flatshade_first )
620 {
621 setup->flatshade_first = flatshade_first;
622 }
623
624
625 void
626 lp_setup_set_vertex_info( struct lp_setup_context *setup,
627 struct vertex_info *vertex_info )
628 {
629 /* XXX: just silently holding onto the pointer:
630 */
631 setup->vertex_info = vertex_info;
632 }
633
634
635 /**
636 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
637 */
638 void
639 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
640 unsigned num,
641 struct pipe_sampler_view **views)
642 {
643 unsigned i;
644
645 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
646
647 assert(num <= PIPE_MAX_SAMPLERS);
648
649 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
650 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
651
652 if (view) {
653 struct pipe_resource *tex = view->texture;
654 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
655 struct lp_jit_texture *jit_tex;
656 jit_tex = &setup->fs.current.jit_context.textures[i];
657 jit_tex->width = tex->width0;
658 jit_tex->height = tex->height0;
659 jit_tex->depth = tex->depth0;
660 jit_tex->last_level = tex->last_level;
661
662 /* We're referencing the texture's internal data, so save a
663 * reference to it.
664 */
665 pipe_resource_reference(&setup->fs.current_tex[i], tex);
666
667 if (!lp_tex->dt) {
668 /* regular texture - setup array of mipmap level pointers */
669 int j;
670 for (j = 0; j <= tex->last_level; j++) {
671 jit_tex->data[j] =
672 llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
673 LP_TEX_LAYOUT_LINEAR);
674 jit_tex->row_stride[j] = lp_tex->row_stride[j];
675 jit_tex->img_stride[j] = lp_tex->img_stride[j];
676
677 if ((LP_PERF & PERF_TEX_MEM) ||
678 !jit_tex->data[j]) {
679 /* out of memory - use dummy tile memory */
680 jit_tex->data[j] = lp_dummy_tile;
681 jit_tex->width = TILE_SIZE/8;
682 jit_tex->height = TILE_SIZE/8;
683 jit_tex->depth = 1;
684 jit_tex->last_level = 0;
685 jit_tex->row_stride[j] = 0;
686 jit_tex->img_stride[j] = 0;
687 }
688 }
689 }
690 else {
691 /* display target texture/surface */
692 /*
693 * XXX: Where should this be unmapped?
694 */
695 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
696 struct sw_winsys *winsys = screen->winsys;
697 jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
698 PIPE_TRANSFER_READ);
699 jit_tex->row_stride[0] = lp_tex->row_stride[0];
700 jit_tex->img_stride[0] = lp_tex->img_stride[0];
701 assert(jit_tex->data[0]);
702 }
703 }
704 }
705
706 setup->dirty |= LP_SETUP_NEW_FS;
707 }
708
709
710 /**
711 * Called during state validation when LP_NEW_SAMPLER is set.
712 */
713 void
714 lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
715 unsigned num,
716 const struct pipe_sampler_state **samplers)
717 {
718 unsigned i;
719
720 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
721
722 assert(num <= PIPE_MAX_SAMPLERS);
723
724 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
725 const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
726
727 if (sampler) {
728 struct lp_jit_texture *jit_tex;
729 jit_tex = &setup->fs.current.jit_context.textures[i];
730
731 jit_tex->min_lod = sampler->min_lod;
732 jit_tex->max_lod = sampler->max_lod;
733 jit_tex->lod_bias = sampler->lod_bias;
734 COPY_4V(jit_tex->border_color, sampler->border_color);
735 }
736 }
737
738 setup->dirty |= LP_SETUP_NEW_FS;
739 }
740
741
742 /**
743 * Is the given texture referenced by any scene?
744 * Note: we have to check all scenes including any scenes currently
745 * being rendered and the current scene being built.
746 */
747 unsigned
748 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
749 const struct pipe_resource *texture )
750 {
751 unsigned i;
752
753 /* check the render targets */
754 for (i = 0; i < setup->fb.nr_cbufs; i++) {
755 if (setup->fb.cbufs[i]->texture == texture)
756 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
757 }
758 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
759 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
760 }
761
762 /* check textures referenced by the scene */
763 for (i = 0; i < Elements(setup->scenes); i++) {
764 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
765 return PIPE_REFERENCED_FOR_READ;
766 }
767 }
768
769 return PIPE_UNREFERENCED;
770 }
771
772
773 /**
774 * Called by vbuf code when we're about to draw something.
775 */
776 static boolean
777 try_update_scene_state( struct lp_setup_context *setup )
778 {
779 boolean new_scene = (setup->fs.stored == NULL);
780 struct lp_scene *scene = setup->scene;
781
782 assert(scene);
783
784 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
785 uint8_t *stored;
786 unsigned i, j;
787
788 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
789 if (!stored) {
790 assert(!new_scene);
791 return FALSE;
792 }
793
794 /* smear each blend color component across 16 ubyte elements */
795 for (i = 0; i < 4; ++i) {
796 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
797 for (j = 0; j < 16; ++j)
798 stored[i*16 + j] = c;
799 }
800
801 setup->blend_color.stored = stored;
802 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
803 setup->dirty |= LP_SETUP_NEW_FS;
804 }
805
806 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
807 struct pipe_resource *buffer = setup->constants.current;
808
809 if(buffer) {
810 unsigned current_size = buffer->width0;
811 const void *current_data = llvmpipe_resource_data(buffer);
812
813 /* TODO: copy only the actually used constants? */
814
815 if(setup->constants.stored_size != current_size ||
816 !setup->constants.stored_data ||
817 memcmp(setup->constants.stored_data,
818 current_data,
819 current_size) != 0) {
820 void *stored;
821
822 stored = lp_scene_alloc(scene, current_size);
823 if (!stored) {
824 assert(!new_scene);
825 return FALSE;
826 }
827
828 memcpy(stored,
829 current_data,
830 current_size);
831 setup->constants.stored_size = current_size;
832 setup->constants.stored_data = stored;
833 }
834 }
835 else {
836 setup->constants.stored_size = 0;
837 setup->constants.stored_data = NULL;
838 }
839
840 setup->fs.current.jit_context.constants = setup->constants.stored_data;
841 setup->dirty |= LP_SETUP_NEW_FS;
842 }
843
844
845 if (setup->dirty & LP_SETUP_NEW_FS) {
846 if (!setup->fs.stored ||
847 memcmp(setup->fs.stored,
848 &setup->fs.current,
849 sizeof setup->fs.current) != 0)
850 {
851 struct lp_rast_state *stored;
852 uint i;
853
854 /* The fs state that's been stored in the scene is different from
855 * the new, current state. So allocate a new lp_rast_state object
856 * and append it to the bin's setup data buffer.
857 */
858 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
859 if (!stored) {
860 assert(!new_scene);
861 return FALSE;
862 }
863
864 memcpy(stored,
865 &setup->fs.current,
866 sizeof setup->fs.current);
867 setup->fs.stored = stored;
868
869 /* The scene now references the textures in the rasterization
870 * state record. Note that now.
871 */
872 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
873 if (setup->fs.current_tex[i]) {
874 if (!lp_scene_add_resource_reference(scene,
875 setup->fs.current_tex[i],
876 new_scene)) {
877 assert(!new_scene);
878 return FALSE;
879 }
880 }
881 }
882 }
883 }
884
885 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
886 setup->draw_region = setup->framebuffer;
887 if (setup->scissor_test) {
888 u_rect_possible_intersection(&setup->scissor,
889 &setup->draw_region);
890 }
891 }
892
893 setup->dirty = 0;
894
895 assert(setup->fs.stored);
896 return TRUE;
897 }
898
899 boolean
900 lp_setup_update_state( struct lp_setup_context *setup,
901 boolean update_scene )
902 {
903 /* Some of the 'draw' pipeline stages may have changed some driver state.
904 * Make sure we've processed those state changes before anything else.
905 *
906 * XXX this is the only place where llvmpipe_context is used in the
907 * setup code. This may get refactored/changed...
908 */
909 {
910 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
911 if (lp->dirty) {
912 llvmpipe_update_derived(lp);
913 }
914
915 if (lp->setup->dirty) {
916 llvmpipe_update_setup(lp);
917 }
918
919 assert(setup->setup.variant);
920
921 /* Will probably need to move this somewhere else, just need
922 * to know about vertex shader point size attribute.
923 */
924 setup->psize = lp->psize_slot;
925
926 assert(lp->dirty == 0);
927
928 assert(lp->setup_variant.key.size ==
929 setup->setup.variant->key.size);
930
931 assert(memcmp(&lp->setup_variant.key,
932 &setup->setup.variant->key,
933 setup->setup.variant->key.size) == 0);
934 }
935
936 if (update_scene && setup->state != SETUP_ACTIVE) {
937 if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))
938 return FALSE;
939 }
940
941 /* Only call into update_scene_state() if we already have a
942 * scene:
943 */
944 if (update_scene && setup->scene) {
945 assert(setup->state == SETUP_ACTIVE);
946
947 if (try_update_scene_state(setup))
948 return TRUE;
949
950 /* Update failed, try to restart the scene.
951 *
952 * Cannot call lp_setup_flush_and_restart() directly here
953 * because of potential recursion.
954 */
955 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
956 return FALSE;
957
958 if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))
959 return FALSE;
960
961 if (!setup->scene)
962 return FALSE;
963
964 return try_update_scene_state(setup);
965 }
966
967 return TRUE;
968 }
969
970
971
972 /* Only caller is lp_setup_vbuf_destroy()
973 */
974 void
975 lp_setup_destroy( struct lp_setup_context *setup )
976 {
977 uint i;
978
979 lp_setup_reset( setup );
980
981 util_unreference_framebuffer_state(&setup->fb);
982
983 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
984 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
985 }
986
987 pipe_resource_reference(&setup->constants.current, NULL);
988
989 /* free the scenes in the 'empty' queue */
990 for (i = 0; i < Elements(setup->scenes); i++) {
991 struct lp_scene *scene = setup->scenes[i];
992
993 if (scene->fence)
994 lp_fence_wait(scene->fence);
995
996 lp_scene_destroy(scene);
997 }
998
999 lp_fence_reference(&setup->last_fence, NULL);
1000
1001 FREE( setup );
1002 }
1003
1004
1005 /**
1006 * Create a new primitive tiling engine. Plug it into the backend of
1007 * the draw module. Currently also creates a rasterizer to use with
1008 * it.
1009 */
1010 struct lp_setup_context *
1011 lp_setup_create( struct pipe_context *pipe,
1012 struct draw_context *draw )
1013 {
1014 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
1015 struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
1016 unsigned i;
1017
1018 if (!setup)
1019 return NULL;
1020
1021 lp_setup_init_vbuf(setup);
1022
1023 /* Used only in update_state():
1024 */
1025 setup->pipe = pipe;
1026
1027
1028 setup->num_threads = screen->num_threads;
1029 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
1030 if (!setup->vbuf)
1031 goto fail;
1032
1033 draw_set_rasterize_stage(draw, setup->vbuf);
1034 draw_set_render(draw, &setup->base);
1035
1036 /* create some empty scenes */
1037 for (i = 0; i < MAX_SCENES; i++) {
1038 setup->scenes[i] = lp_scene_create( pipe );
1039 }
1040
1041 setup->triangle = first_triangle;
1042 setup->line = first_line;
1043 setup->point = first_point;
1044
1045 setup->dirty = ~0;
1046
1047 return setup;
1048
1049 fail:
1050 if (setup->vbuf)
1051 ;
1052
1053 FREE(setup);
1054 return NULL;
1055 }
1056
1057
1058 /**
1059 * Put a BeginQuery command into all bins.
1060 */
1061 void
1062 lp_setup_begin_query(struct lp_setup_context *setup,
1063 struct llvmpipe_query *pq)
1064 {
1065 /* init the query to its beginning state */
1066 assert(setup->active_query == NULL);
1067
1068 if (setup->scene) {
1069 if (!lp_scene_bin_everywhere(setup->scene,
1070 LP_RAST_OP_BEGIN_QUERY,
1071 lp_rast_arg_query(pq))) {
1072
1073 if (!lp_setup_flush_and_restart(setup))
1074 return;
1075
1076 if (!lp_scene_bin_everywhere(setup->scene,
1077 LP_RAST_OP_BEGIN_QUERY,
1078 lp_rast_arg_query(pq))) {
1079 return;
1080 }
1081 }
1082 }
1083
1084 setup->active_query = pq;
1085 }
1086
1087
1088 /**
1089 * Put an EndQuery command into all bins.
1090 */
1091 void
1092 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1093 {
1094 union lp_rast_cmd_arg dummy = { 0 };
1095
1096 assert(setup->active_query == pq);
1097 setup->active_query = NULL;
1098
1099 /* Setup will automatically re-issue any query which carried over a
1100 * scene boundary, and the rasterizer automatically "ends" queries
1101 * which are active at the end of a scene, so there is no need to
1102 * retry this commands on failure.
1103 */
1104 if (setup->scene) {
1105 /* pq->fence should be the fence of the *last* scene which
1106 * contributed to the query result.
1107 */
1108 lp_fence_reference(&pq->fence, setup->scene->fence);
1109
1110 if (!lp_scene_bin_everywhere(setup->scene,
1111 LP_RAST_OP_END_QUERY,
1112 dummy)) {
1113 lp_setup_flush(setup, 0, NULL, __FUNCTION__);
1114 }
1115 }
1116 else {
1117 lp_fence_reference(&pq->fence, setup->last_fence);
1118 }
1119 }
1120
1121
1122 boolean
1123 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1124 {
1125 if (0) debug_printf("%s\n", __FUNCTION__);
1126
1127 assert(setup->state == SETUP_ACTIVE);
1128
1129 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1130 return FALSE;
1131
1132 if (!lp_setup_update_state(setup, TRUE))
1133 return FALSE;
1134
1135 return TRUE;
1136 }
1137
1138