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