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