Merge remote-tracking branch 'mattst88/nir-lower-pack-unpack' into vulkan
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 "draw/draw_pipe.h"
43 #include "os/os_time.h"
44 #include "lp_context.h"
45 #include "lp_memory.h"
46 #include "lp_scene.h"
47 #include "lp_texture.h"
48 #include "lp_debug.h"
49 #include "lp_fence.h"
50 #include "lp_query.h"
51 #include "lp_rast.h"
52 #include "lp_setup_context.h"
53 #include "lp_screen.h"
54 #include "lp_state.h"
55 #include "state_tracker/sw_winsys.h"
56
57 #include "draw/draw_context.h"
58 #include "draw/draw_vbuf.h"
59
60
61 static boolean set_scene_state( struct lp_setup_context *, enum setup_state,
62 const char *reason);
63 static boolean try_update_scene_state( struct lp_setup_context *setup );
64
65
66 static void
67 lp_setup_get_empty_scene(struct lp_setup_context *setup)
68 {
69 assert(setup->scene == NULL);
70
71 setup->scene_idx++;
72 setup->scene_idx %= Elements(setup->scenes);
73
74 setup->scene = setup->scenes[setup->scene_idx];
75
76 if (setup->scene->fence) {
77 if (LP_DEBUG & DEBUG_SETUP)
78 debug_printf("%s: wait for scene %d\n",
79 __FUNCTION__, setup->scene->fence->id);
80
81 lp_fence_wait(setup->scene->fence);
82 }
83
84 lp_scene_begin_binning(setup->scene, &setup->fb, setup->rasterizer_discard);
85
86 }
87
88
89 static void
90 first_triangle( struct lp_setup_context *setup,
91 const float (*v0)[4],
92 const float (*v1)[4],
93 const float (*v2)[4])
94 {
95 assert(setup->state == SETUP_ACTIVE);
96 lp_setup_choose_triangle( setup );
97 setup->triangle( setup, v0, v1, v2 );
98 }
99
100 static void
101 first_line( struct lp_setup_context *setup,
102 const float (*v0)[4],
103 const float (*v1)[4])
104 {
105 assert(setup->state == SETUP_ACTIVE);
106 lp_setup_choose_line( setup );
107 setup->line( setup, v0, v1 );
108 }
109
110 static void
111 first_point( struct lp_setup_context *setup,
112 const float (*v0)[4])
113 {
114 assert(setup->state == SETUP_ACTIVE);
115 lp_setup_choose_point( setup );
116 setup->point( setup, v0 );
117 }
118
119 void lp_setup_reset( struct lp_setup_context *setup )
120 {
121 unsigned i;
122
123 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
124
125 /* Reset derived state */
126 for (i = 0; i < Elements(setup->constants); ++i) {
127 setup->constants[i].stored_size = 0;
128 setup->constants[i].stored_data = NULL;
129 }
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 memset(&setup->clear, 0, sizeof setup->clear);
139
140 /* Have an explicit "start-binning" call and get rid of this
141 * pointer twiddling?
142 */
143 setup->line = first_line;
144 setup->point = first_point;
145 setup->triangle = first_triangle;
146 }
147
148
149 /** Rasterize all scene's bins */
150 static void
151 lp_setup_rasterize_scene( struct lp_setup_context *setup )
152 {
153 struct lp_scene *scene = setup->scene;
154 struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
155
156 scene->num_active_queries = setup->active_binned_queries;
157 memcpy(scene->active_queries, setup->active_queries,
158 scene->num_active_queries * sizeof(scene->active_queries[0]));
159
160 lp_scene_end_binning(scene);
161
162 lp_fence_reference(&setup->last_fence, scene->fence);
163
164 if (setup->last_fence)
165 setup->last_fence->issued = TRUE;
166
167 pipe_mutex_lock(screen->rast_mutex);
168
169 /* FIXME: We enqueue the scene then wait on the rasterizer to finish.
170 * This means we never actually run any vertex stuff in parallel to
171 * rasterization (not in the same context at least) which is what the
172 * multiple scenes per setup is about - when we get a new empty scene
173 * any old one is already empty again because we waited here for
174 * raster tasks to be finished. Ideally, we shouldn't need to wait here
175 * and rely on fences elsewhere when waiting is necessary.
176 * Certainly, lp_scene_end_rasterization() would need to be deferred too
177 * and there's probably other bits why this doesn't actually work.
178 */
179 lp_rast_queue_scene(screen->rast, scene);
180 lp_rast_finish(screen->rast);
181 pipe_mutex_unlock(screen->rast_mutex);
182
183 lp_scene_end_rasterization(setup->scene);
184 lp_setup_reset( setup );
185
186 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
187 }
188
189
190
191 static boolean
192 begin_binning( struct lp_setup_context *setup )
193 {
194 struct lp_scene *scene = setup->scene;
195 boolean need_zsload = FALSE;
196 boolean ok;
197
198 assert(scene);
199 assert(scene->fence == NULL);
200
201 /* Always create a fence:
202 */
203 scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
204 if (!scene->fence)
205 return FALSE;
206
207 ok = try_update_scene_state(setup);
208 if (!ok)
209 return FALSE;
210
211 if (setup->fb.zsbuf &&
212 ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
213 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
214 need_zsload = TRUE;
215
216 LP_DBG(DEBUG_SETUP, "%s color clear bufs: %x depth: %s\n", __FUNCTION__,
217 setup->clear.flags >> 2,
218 need_zsload ? "clear": "load");
219
220 if (setup->clear.flags & PIPE_CLEAR_COLOR) {
221 unsigned cbuf;
222 for (cbuf = 0; cbuf < setup->fb.nr_cbufs; cbuf++) {
223 assert(PIPE_CLEAR_COLOR0 == 1 << 2);
224 if (setup->clear.flags & (1 << (2 + cbuf))) {
225 union lp_rast_cmd_arg clearrb_arg;
226 struct lp_rast_clear_rb *cc_scene =
227 (struct lp_rast_clear_rb *)
228 lp_scene_alloc(scene, sizeof(struct lp_rast_clear_rb));
229
230 if (!cc_scene) {
231 return FALSE;
232 }
233
234 cc_scene->cbuf = cbuf;
235 cc_scene->color_val = setup->clear.color_val[cbuf];
236 clearrb_arg.clear_rb = cc_scene;
237
238 if (!lp_scene_bin_everywhere(scene,
239 LP_RAST_OP_CLEAR_COLOR,
240 clearrb_arg))
241 return FALSE;
242 }
243 }
244 }
245
246 if (setup->fb.zsbuf) {
247 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
248 ok = lp_scene_bin_everywhere( scene,
249 LP_RAST_OP_CLEAR_ZSTENCIL,
250 lp_rast_arg_clearzs(
251 setup->clear.zsvalue,
252 setup->clear.zsmask));
253 if (!ok)
254 return FALSE;
255 }
256 }
257
258 setup->clear.flags = 0;
259 setup->clear.zsmask = 0;
260 setup->clear.zsvalue = 0;
261
262 scene->had_queries = !!setup->active_binned_queries;
263
264 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
265 return TRUE;
266 }
267
268
269 /* This basically bins and then flushes any outstanding full-screen
270 * clears.
271 *
272 * TODO: fast path for fullscreen clears and no triangles.
273 */
274 static boolean
275 execute_clears( struct lp_setup_context *setup )
276 {
277 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
278
279 return begin_binning( setup );
280 }
281
282 const char *states[] = {
283 "FLUSHED",
284 "CLEARED",
285 "ACTIVE "
286 };
287
288
289 static boolean
290 set_scene_state( struct lp_setup_context *setup,
291 enum setup_state new_state,
292 const char *reason)
293 {
294 unsigned old_state = setup->state;
295
296 if (old_state == new_state)
297 return TRUE;
298
299 if (LP_DEBUG & DEBUG_SCENE) {
300 debug_printf("%s old %s new %s%s%s\n",
301 __FUNCTION__,
302 states[old_state],
303 states[new_state],
304 (new_state == SETUP_FLUSHED) ? ": " : "",
305 (new_state == SETUP_FLUSHED) ? reason : "");
306
307 if (new_state == SETUP_FLUSHED && setup->scene)
308 lp_debug_draw_bins_by_cmd_length(setup->scene);
309 }
310
311 /* wait for a free/empty scene
312 */
313 if (old_state == SETUP_FLUSHED)
314 lp_setup_get_empty_scene(setup);
315
316 switch (new_state) {
317 case SETUP_CLEARED:
318 break;
319
320 case SETUP_ACTIVE:
321 if (!begin_binning( setup ))
322 goto fail;
323 break;
324
325 case SETUP_FLUSHED:
326 if (old_state == SETUP_CLEARED)
327 if (!execute_clears( setup ))
328 goto fail;
329
330 lp_setup_rasterize_scene( setup );
331 assert(setup->scene == NULL);
332 break;
333
334 default:
335 assert(0 && "invalid setup state mode");
336 goto fail;
337 }
338
339 setup->state = new_state;
340 return TRUE;
341
342 fail:
343 if (setup->scene) {
344 lp_scene_end_rasterization(setup->scene);
345 setup->scene = NULL;
346 }
347
348 setup->state = SETUP_FLUSHED;
349 lp_setup_reset( setup );
350 return FALSE;
351 }
352
353
354 void
355 lp_setup_flush( struct lp_setup_context *setup,
356 struct pipe_fence_handle **fence,
357 const char *reason)
358 {
359 set_scene_state( setup, SETUP_FLUSHED, reason );
360
361 if (fence) {
362 lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
363 }
364 }
365
366
367 void
368 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
369 const struct pipe_framebuffer_state *fb )
370 {
371 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
372
373 /* Flush any old scene.
374 */
375 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
376
377 /*
378 * Ensure the old scene is not reused.
379 */
380 assert(!setup->scene);
381
382 /* Set new state. This will be picked up later when we next need a
383 * scene.
384 */
385 util_copy_framebuffer_state(&setup->fb, fb);
386 setup->framebuffer.x0 = 0;
387 setup->framebuffer.y0 = 0;
388 setup->framebuffer.x1 = fb->width-1;
389 setup->framebuffer.y1 = fb->height-1;
390 setup->dirty |= LP_SETUP_NEW_SCISSOR;
391 }
392
393
394 /*
395 * Try to clear one color buffer of the attached fb, either by binning a clear
396 * command or queuing up the clear for later (when binning is started).
397 */
398 static boolean
399 lp_setup_try_clear_color_buffer(struct lp_setup_context *setup,
400 const union pipe_color_union *color,
401 unsigned cbuf)
402 {
403 union lp_rast_cmd_arg clearrb_arg;
404 union util_color uc;
405 enum pipe_format format = setup->fb.cbufs[cbuf]->format;
406
407 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
408
409 if (util_format_is_pure_integer(format)) {
410 /*
411 * We expect int/uint clear values here, though some APIs
412 * might disagree (but in any case util_pack_color()
413 * couldn't handle it)...
414 */
415 if (util_format_is_pure_sint(format)) {
416 util_format_write_4i(format, color->i, 0, &uc, 0, 0, 0, 1, 1);
417 }
418 else {
419 assert(util_format_is_pure_uint(format));
420 util_format_write_4ui(format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
421 }
422 }
423 else {
424 util_pack_color(color->f, format, &uc);
425 }
426
427 if (setup->state == SETUP_ACTIVE) {
428 struct lp_scene *scene = setup->scene;
429
430 /* Add the clear to existing scene. In the unusual case where
431 * both color and depth-stencil are being cleared when there's
432 * already been some rendering, we could discard the currently
433 * binned scene and start again, but I don't see that as being
434 * a common usage.
435 */
436 struct lp_rast_clear_rb *cc_scene =
437 (struct lp_rast_clear_rb *)
438 lp_scene_alloc_aligned(scene, sizeof(struct lp_rast_clear_rb), 8);
439
440 if (!cc_scene) {
441 return FALSE;
442 }
443
444 cc_scene->cbuf = cbuf;
445 cc_scene->color_val = uc;
446 clearrb_arg.clear_rb = cc_scene;
447
448 if (!lp_scene_bin_everywhere(scene,
449 LP_RAST_OP_CLEAR_COLOR,
450 clearrb_arg))
451 return FALSE;
452 }
453 else {
454 /* Put ourselves into the 'pre-clear' state, specifically to try
455 * and accumulate multiple clears to color and depth_stencil
456 * buffers which the app or state-tracker might issue
457 * separately.
458 */
459 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
460
461 assert(PIPE_CLEAR_COLOR0 == (1 << 2));
462 setup->clear.flags |= 1 << (cbuf + 2);
463 setup->clear.color_val[cbuf] = uc;
464 }
465
466 return TRUE;
467 }
468
469 static boolean
470 lp_setup_try_clear_zs(struct lp_setup_context *setup,
471 double depth,
472 unsigned stencil,
473 unsigned flags)
474 {
475 uint64_t zsmask = 0;
476 uint64_t zsvalue = 0;
477 uint32_t zmask32;
478 uint8_t smask8;
479 enum pipe_format format = setup->fb.zsbuf->format;
480
481 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
482
483 zmask32 = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
484 smask8 = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
485
486 zsvalue = util_pack64_z_stencil(format, depth, stencil);
487
488 zsmask = util_pack64_mask_z_stencil(format, zmask32, smask8);
489
490 zsvalue &= zsmask;
491
492 if (format == PIPE_FORMAT_Z24X8_UNORM ||
493 format == PIPE_FORMAT_X8Z24_UNORM) {
494 /*
495 * Make full mask if there's "X" bits so we can do full
496 * clear (without rmw).
497 */
498 uint32_t zsmask_full = 0;
499 zsmask_full = util_pack_mask_z_stencil(format, ~0, ~0);
500 zsmask |= ~zsmask_full;
501 }
502
503 if (setup->state == SETUP_ACTIVE) {
504 struct lp_scene *scene = setup->scene;
505
506 /* Add the clear to existing scene. In the unusual case where
507 * both color and depth-stencil are being cleared when there's
508 * already been some rendering, we could discard the currently
509 * binned scene and start again, but I don't see that as being
510 * a common usage.
511 */
512 if (!lp_scene_bin_everywhere(scene,
513 LP_RAST_OP_CLEAR_ZSTENCIL,
514 lp_rast_arg_clearzs(zsvalue, zsmask)))
515 return FALSE;
516 }
517 else {
518 /* Put ourselves into the 'pre-clear' state, specifically to try
519 * and accumulate multiple clears to color and depth_stencil
520 * buffers which the app or state-tracker might issue
521 * separately.
522 */
523 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
524
525 setup->clear.flags |= flags;
526
527 setup->clear.zsmask |= zsmask;
528 setup->clear.zsvalue =
529 (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
530 }
531
532 return TRUE;
533 }
534
535 void
536 lp_setup_clear( struct lp_setup_context *setup,
537 const union pipe_color_union *color,
538 double depth,
539 unsigned stencil,
540 unsigned flags )
541 {
542 unsigned i;
543
544 /*
545 * Note any of these (max 9) clears could fail (but at most there should
546 * be just one failure!). This avoids doing the previous succeeded
547 * clears again (we still clear tiles twice if a clear command succeeded
548 * partially for one buffer).
549 */
550 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
551 unsigned flagszs = flags & PIPE_CLEAR_DEPTHSTENCIL;
552 if (!lp_setup_try_clear_zs(setup, depth, stencil, flagszs)) {
553 lp_setup_flush(setup, NULL, __FUNCTION__);
554
555 if (!lp_setup_try_clear_zs(setup, depth, stencil, flagszs))
556 assert(0);
557 }
558 }
559
560 if (flags & PIPE_CLEAR_COLOR) {
561 assert(PIPE_CLEAR_COLOR0 == (1 << 2));
562 for (i = 0; i < setup->fb.nr_cbufs; i++) {
563 if ((flags & (1 << (2 + i))) && setup->fb.cbufs[i]) {
564 if (!lp_setup_try_clear_color_buffer(setup, color, i)) {
565 lp_setup_flush(setup, NULL, __FUNCTION__);
566
567 if (!lp_setup_try_clear_color_buffer(setup, color, i))
568 assert(0);
569 }
570 }
571 }
572 }
573 }
574
575
576
577 void
578 lp_setup_set_triangle_state( struct lp_setup_context *setup,
579 unsigned cull_mode,
580 boolean ccw_is_frontface,
581 boolean scissor,
582 boolean half_pixel_center,
583 boolean bottom_edge_rule)
584 {
585 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
586
587 setup->ccw_is_frontface = ccw_is_frontface;
588 setup->cullmode = cull_mode;
589 setup->triangle = first_triangle;
590 setup->pixel_offset = half_pixel_center ? 0.5f : 0.0f;
591 setup->bottom_edge_rule = bottom_edge_rule;
592
593 if (setup->scissor_test != scissor) {
594 setup->dirty |= LP_SETUP_NEW_SCISSOR;
595 setup->scissor_test = scissor;
596 }
597 }
598
599 void
600 lp_setup_set_line_state( struct lp_setup_context *setup,
601 float line_width)
602 {
603 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
604
605 setup->line_width = line_width;
606 }
607
608 void
609 lp_setup_set_point_state( struct lp_setup_context *setup,
610 float point_size,
611 boolean point_size_per_vertex,
612 uint sprite_coord_enable,
613 uint sprite_coord_origin)
614 {
615 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
616
617 setup->point_size = point_size;
618 setup->sprite_coord_enable = sprite_coord_enable;
619 setup->sprite_coord_origin = sprite_coord_origin;
620 setup->point_size_per_vertex = point_size_per_vertex;
621 }
622
623 void
624 lp_setup_set_setup_variant( struct lp_setup_context *setup,
625 const struct lp_setup_variant *variant)
626 {
627 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
628
629 setup->setup.variant = variant;
630 }
631
632 void
633 lp_setup_set_fs_variant( struct lp_setup_context *setup,
634 struct lp_fragment_shader_variant *variant)
635 {
636 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
637 variant);
638 /* FIXME: reference count */
639
640 setup->fs.current.variant = variant;
641 setup->dirty |= LP_SETUP_NEW_FS;
642 }
643
644 void
645 lp_setup_set_fs_constants(struct lp_setup_context *setup,
646 unsigned num,
647 struct pipe_constant_buffer *buffers)
648 {
649 unsigned i;
650
651 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);
652
653 assert(num <= Elements(setup->constants));
654
655 for (i = 0; i < num; ++i) {
656 util_copy_constant_buffer(&setup->constants[i].current, &buffers[i]);
657 }
658 for (; i < Elements(setup->constants); i++) {
659 util_copy_constant_buffer(&setup->constants[i].current, NULL);
660 }
661 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
662 }
663
664
665 void
666 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
667 float alpha_ref_value )
668 {
669 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
670
671 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
672 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
673 setup->dirty |= LP_SETUP_NEW_FS;
674 }
675 }
676
677 void
678 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
679 const ubyte refs[2] )
680 {
681 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
682
683 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
684 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
685 setup->fs.current.jit_context.stencil_ref_front = refs[0];
686 setup->fs.current.jit_context.stencil_ref_back = refs[1];
687 setup->dirty |= LP_SETUP_NEW_FS;
688 }
689 }
690
691 void
692 lp_setup_set_blend_color( struct lp_setup_context *setup,
693 const struct pipe_blend_color *blend_color )
694 {
695 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
696
697 assert(blend_color);
698
699 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
700 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
701 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
702 }
703 }
704
705
706 void
707 lp_setup_set_scissors( struct lp_setup_context *setup,
708 const struct pipe_scissor_state *scissors )
709 {
710 unsigned i;
711 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
712
713 assert(scissors);
714
715 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
716 setup->scissors[i].x0 = scissors[i].minx;
717 setup->scissors[i].x1 = scissors[i].maxx-1;
718 setup->scissors[i].y0 = scissors[i].miny;
719 setup->scissors[i].y1 = scissors[i].maxy-1;
720 }
721 setup->dirty |= LP_SETUP_NEW_SCISSOR;
722 }
723
724
725 void
726 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
727 boolean flatshade_first )
728 {
729 setup->flatshade_first = flatshade_first;
730 }
731
732 void
733 lp_setup_set_rasterizer_discard( struct lp_setup_context *setup,
734 boolean rasterizer_discard )
735 {
736 if (setup->rasterizer_discard != rasterizer_discard) {
737 setup->rasterizer_discard = rasterizer_discard;
738 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
739 }
740 }
741
742 void
743 lp_setup_set_vertex_info( struct lp_setup_context *setup,
744 struct vertex_info *vertex_info )
745 {
746 /* XXX: just silently holding onto the pointer:
747 */
748 setup->vertex_info = vertex_info;
749 }
750
751
752 /**
753 * Called during state validation when LP_NEW_VIEWPORT is set.
754 */
755 void
756 lp_setup_set_viewports(struct lp_setup_context *setup,
757 unsigned num_viewports,
758 const struct pipe_viewport_state *viewports)
759 {
760 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
761 unsigned i;
762
763 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
764
765 assert(num_viewports <= PIPE_MAX_VIEWPORTS);
766 assert(viewports);
767
768 /*
769 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
770 */
771 for (i = 0; i < num_viewports; i++) {
772 float min_depth;
773 float max_depth;
774
775 if (lp->rasterizer->clip_halfz == 0) {
776 float half_depth = viewports[i].scale[2];
777 min_depth = viewports[i].translate[2] - half_depth;
778 max_depth = min_depth + half_depth * 2.0f;
779 } else {
780 min_depth = viewports[i].translate[2];
781 max_depth = min_depth + viewports[i].scale[2];
782 }
783
784 if (setup->viewports[i].min_depth != min_depth ||
785 setup->viewports[i].max_depth != max_depth) {
786 setup->viewports[i].min_depth = min_depth;
787 setup->viewports[i].max_depth = max_depth;
788 setup->dirty |= LP_SETUP_NEW_VIEWPORTS;
789 }
790 }
791 }
792
793
794 /**
795 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
796 */
797 void
798 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
799 unsigned num,
800 struct pipe_sampler_view **views)
801 {
802 unsigned i, max_tex_num;
803
804 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
805
806 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
807
808 max_tex_num = MAX2(num, setup->fs.current_tex_num);
809
810 for (i = 0; i < max_tex_num; i++) {
811 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
812
813 if (view) {
814 struct pipe_resource *res = view->texture;
815 struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);
816 struct lp_jit_texture *jit_tex;
817 jit_tex = &setup->fs.current.jit_context.textures[i];
818
819 /* We're referencing the texture's internal data, so save a
820 * reference to it.
821 */
822 pipe_resource_reference(&setup->fs.current_tex[i], res);
823
824 if (!lp_tex->dt) {
825 /* regular texture - setup array of mipmap level offsets */
826 int j;
827 unsigned first_level = 0;
828 unsigned last_level = 0;
829
830 if (llvmpipe_resource_is_texture(res)) {
831 first_level = view->u.tex.first_level;
832 last_level = view->u.tex.last_level;
833 assert(first_level <= last_level);
834 assert(last_level <= res->last_level);
835 jit_tex->base = lp_tex->tex_data;
836 }
837 else {
838 jit_tex->base = lp_tex->data;
839 }
840
841 if (LP_PERF & PERF_TEX_MEM) {
842 /* use dummy tile memory */
843 jit_tex->base = lp_dummy_tile;
844 jit_tex->width = TILE_SIZE/8;
845 jit_tex->height = TILE_SIZE/8;
846 jit_tex->depth = 1;
847 jit_tex->first_level = 0;
848 jit_tex->last_level = 0;
849 jit_tex->mip_offsets[0] = 0;
850 jit_tex->row_stride[0] = 0;
851 jit_tex->img_stride[0] = 0;
852 }
853 else {
854 jit_tex->width = res->width0;
855 jit_tex->height = res->height0;
856 jit_tex->depth = res->depth0;
857 jit_tex->first_level = first_level;
858 jit_tex->last_level = last_level;
859
860 if (llvmpipe_resource_is_texture(res)) {
861 for (j = first_level; j <= last_level; j++) {
862 jit_tex->mip_offsets[j] = lp_tex->mip_offsets[j];
863 jit_tex->row_stride[j] = lp_tex->row_stride[j];
864 jit_tex->img_stride[j] = lp_tex->img_stride[j];
865 }
866
867 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
868 res->target == PIPE_TEXTURE_2D_ARRAY ||
869 res->target == PIPE_TEXTURE_CUBE ||
870 res->target == PIPE_TEXTURE_CUBE_ARRAY) {
871 /*
872 * For array textures, we don't have first_layer, instead
873 * adjust last_layer (stored as depth) plus the mip level offsets
874 * (as we have mip-first layout can't just adjust base ptr).
875 * XXX For mip levels, could do something similar.
876 */
877 jit_tex->depth = view->u.tex.last_layer - view->u.tex.first_layer + 1;
878 for (j = first_level; j <= last_level; j++) {
879 jit_tex->mip_offsets[j] += view->u.tex.first_layer *
880 lp_tex->img_stride[j];
881 }
882 if (view->target == PIPE_TEXTURE_CUBE ||
883 view->target == PIPE_TEXTURE_CUBE_ARRAY) {
884 assert(jit_tex->depth % 6 == 0);
885 }
886 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
887 assert(view->u.tex.last_layer < res->array_size);
888 }
889 }
890 else {
891 /*
892 * For buffers, we don't have first_element, instead adjust
893 * last_element (stored as width) plus the base pointer.
894 */
895 unsigned view_blocksize = util_format_get_blocksize(view->format);
896 /* probably don't really need to fill that out */
897 jit_tex->mip_offsets[0] = 0;
898 jit_tex->row_stride[0] = 0;
899 jit_tex->img_stride[0] = 0;
900
901 /* everything specified in number of elements here. */
902 jit_tex->width = view->u.buf.last_element - view->u.buf.first_element + 1;
903 jit_tex->base = (uint8_t *)jit_tex->base + view->u.buf.first_element *
904 view_blocksize;
905 /* XXX Unsure if we need to sanitize parameters? */
906 assert(view->u.buf.first_element <= view->u.buf.last_element);
907 assert(view->u.buf.last_element * view_blocksize < res->width0);
908 }
909 }
910 }
911 else {
912 /* display target texture/surface */
913 /*
914 * XXX: Where should this be unmapped?
915 */
916 struct llvmpipe_screen *screen = llvmpipe_screen(res->screen);
917 struct sw_winsys *winsys = screen->winsys;
918 jit_tex->base = winsys->displaytarget_map(winsys, lp_tex->dt,
919 PIPE_TRANSFER_READ);
920 jit_tex->row_stride[0] = lp_tex->row_stride[0];
921 jit_tex->img_stride[0] = lp_tex->img_stride[0];
922 jit_tex->mip_offsets[0] = 0;
923 jit_tex->width = res->width0;
924 jit_tex->height = res->height0;
925 jit_tex->depth = res->depth0;
926 jit_tex->first_level = jit_tex->last_level = 0;
927 assert(jit_tex->base);
928 }
929 }
930 else {
931 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
932 }
933 }
934 setup->fs.current_tex_num = num;
935
936 setup->dirty |= LP_SETUP_NEW_FS;
937 }
938
939
940 /**
941 * Called during state validation when LP_NEW_SAMPLER is set.
942 */
943 void
944 lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
945 unsigned num,
946 struct pipe_sampler_state **samplers)
947 {
948 unsigned i;
949
950 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
951
952 assert(num <= PIPE_MAX_SAMPLERS);
953
954 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
955 const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
956
957 if (sampler) {
958 struct lp_jit_sampler *jit_sam;
959 jit_sam = &setup->fs.current.jit_context.samplers[i];
960
961 jit_sam->min_lod = sampler->min_lod;
962 jit_sam->max_lod = sampler->max_lod;
963 jit_sam->lod_bias = sampler->lod_bias;
964 COPY_4V(jit_sam->border_color, sampler->border_color.f);
965 }
966 }
967
968 setup->dirty |= LP_SETUP_NEW_FS;
969 }
970
971
972 /**
973 * Is the given texture referenced by any scene?
974 * Note: we have to check all scenes including any scenes currently
975 * being rendered and the current scene being built.
976 */
977 unsigned
978 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
979 const struct pipe_resource *texture )
980 {
981 unsigned i;
982
983 /* check the render targets */
984 for (i = 0; i < setup->fb.nr_cbufs; i++) {
985 if (setup->fb.cbufs[i] && setup->fb.cbufs[i]->texture == texture)
986 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
987 }
988 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
989 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
990 }
991
992 /* check textures referenced by the scene */
993 for (i = 0; i < Elements(setup->scenes); i++) {
994 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
995 return LP_REFERENCED_FOR_READ;
996 }
997 }
998
999 return LP_UNREFERENCED;
1000 }
1001
1002
1003 /**
1004 * Called by vbuf code when we're about to draw something.
1005 *
1006 * This function stores all dirty state in the current scene's display list
1007 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
1008 * the JIT functions, as the JIT functions will be called later on, most likely
1009 * on a different thread.
1010 *
1011 * When processing dirty state it is imperative that we don't refer to any
1012 * pointers previously allocated with lp_scene_alloc() in this function (or any
1013 * function) as they may belong to a scene freed since then.
1014 */
1015 static boolean
1016 try_update_scene_state( struct lp_setup_context *setup )
1017 {
1018 static const float fake_const_buf[4];
1019 boolean new_scene = (setup->fs.stored == NULL);
1020 struct lp_scene *scene = setup->scene;
1021 unsigned i;
1022
1023 assert(scene);
1024
1025 if (setup->dirty & LP_SETUP_NEW_VIEWPORTS) {
1026 /*
1027 * Record new depth range state for changes due to viewport updates.
1028 *
1029 * TODO: Collapse the existing viewport and depth range information
1030 * into one structure, for access by JIT.
1031 */
1032 struct lp_jit_viewport *stored;
1033
1034 stored = (struct lp_jit_viewport *)
1035 lp_scene_alloc(scene, sizeof setup->viewports);
1036
1037 if (!stored) {
1038 assert(!new_scene);
1039 return FALSE;
1040 }
1041
1042 memcpy(stored, setup->viewports, sizeof setup->viewports);
1043
1044 setup->fs.current.jit_context.viewports = stored;
1045 setup->dirty |= LP_SETUP_NEW_FS;
1046 }
1047
1048 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
1049 uint8_t *stored;
1050 float* fstored;
1051 unsigned i, j;
1052 unsigned size;
1053
1054 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
1055 size = 4 * 16 * sizeof(uint8_t);
1056 size += (LP_MAX_VECTOR_LENGTH / 4) * sizeof(float);
1057 stored = lp_scene_alloc_aligned(scene, size, LP_MIN_VECTOR_ALIGN);
1058
1059 if (!stored) {
1060 assert(!new_scene);
1061 return FALSE;
1062 }
1063
1064 /* Store floating point colour */
1065 fstored = (float*)(stored + 4*16);
1066 for (i = 0; i < (LP_MAX_VECTOR_LENGTH / 4); ++i) {
1067 fstored[i] = setup->blend_color.current.color[i % 4];
1068 }
1069
1070 /* smear each blend color component across 16 ubyte elements */
1071 for (i = 0; i < 4; ++i) {
1072 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
1073 for (j = 0; j < 16; ++j)
1074 stored[i*16 + j] = c;
1075 }
1076
1077 setup->blend_color.stored = stored;
1078 setup->fs.current.jit_context.u8_blend_color = stored;
1079 setup->fs.current.jit_context.f_blend_color = fstored;
1080 setup->dirty |= LP_SETUP_NEW_FS;
1081 }
1082
1083 if (setup->dirty & LP_SETUP_NEW_CONSTANTS) {
1084 for (i = 0; i < Elements(setup->constants); ++i) {
1085 struct pipe_resource *buffer = setup->constants[i].current.buffer;
1086 const unsigned current_size = MIN2(setup->constants[i].current.buffer_size,
1087 LP_MAX_TGSI_CONST_BUFFER_SIZE);
1088 const ubyte *current_data = NULL;
1089 int num_constants;
1090
1091 STATIC_ASSERT(DATA_BLOCK_SIZE >= LP_MAX_TGSI_CONST_BUFFER_SIZE);
1092
1093 if (buffer) {
1094 /* resource buffer */
1095 current_data = (ubyte *) llvmpipe_resource_data(buffer);
1096 }
1097 else if (setup->constants[i].current.user_buffer) {
1098 /* user-space buffer */
1099 current_data = (ubyte *) setup->constants[i].current.user_buffer;
1100 }
1101
1102 if (current_data) {
1103 current_data += setup->constants[i].current.buffer_offset;
1104
1105 /* TODO: copy only the actually used constants? */
1106
1107 if (setup->constants[i].stored_size != current_size ||
1108 !setup->constants[i].stored_data ||
1109 memcmp(setup->constants[i].stored_data,
1110 current_data,
1111 current_size) != 0) {
1112 void *stored;
1113
1114 stored = lp_scene_alloc(scene, current_size);
1115 if (!stored) {
1116 assert(!new_scene);
1117 return FALSE;
1118 }
1119
1120 memcpy(stored,
1121 current_data,
1122 current_size);
1123 setup->constants[i].stored_size = current_size;
1124 setup->constants[i].stored_data = stored;
1125 }
1126 setup->fs.current.jit_context.constants[i] =
1127 setup->constants[i].stored_data;
1128 }
1129 else {
1130 setup->constants[i].stored_size = 0;
1131 setup->constants[i].stored_data = NULL;
1132 setup->fs.current.jit_context.constants[i] = fake_const_buf;
1133 }
1134
1135 num_constants =
1136 setup->constants[i].stored_size / (sizeof(float) * 4);
1137 setup->fs.current.jit_context.num_constants[i] = num_constants;
1138 setup->dirty |= LP_SETUP_NEW_FS;
1139 }
1140 }
1141
1142
1143 if (setup->dirty & LP_SETUP_NEW_FS) {
1144 if (!setup->fs.stored ||
1145 memcmp(setup->fs.stored,
1146 &setup->fs.current,
1147 sizeof setup->fs.current) != 0)
1148 {
1149 struct lp_rast_state *stored;
1150
1151 /* The fs state that's been stored in the scene is different from
1152 * the new, current state. So allocate a new lp_rast_state object
1153 * and append it to the bin's setup data buffer.
1154 */
1155 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
1156 if (!stored) {
1157 assert(!new_scene);
1158 return FALSE;
1159 }
1160
1161 memcpy(stored,
1162 &setup->fs.current,
1163 sizeof setup->fs.current);
1164 setup->fs.stored = stored;
1165
1166 /* The scene now references the textures in the rasterization
1167 * state record. Note that now.
1168 */
1169 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1170 if (setup->fs.current_tex[i]) {
1171 if (!lp_scene_add_resource_reference(scene,
1172 setup->fs.current_tex[i],
1173 new_scene)) {
1174 assert(!new_scene);
1175 return FALSE;
1176 }
1177 }
1178 }
1179 }
1180 }
1181
1182 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
1183 unsigned i;
1184 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
1185 setup->draw_regions[i] = setup->framebuffer;
1186 if (setup->scissor_test) {
1187 u_rect_possible_intersection(&setup->scissors[i],
1188 &setup->draw_regions[i]);
1189 }
1190 }
1191 }
1192
1193 setup->dirty = 0;
1194
1195 assert(setup->fs.stored);
1196 return TRUE;
1197 }
1198
1199 boolean
1200 lp_setup_update_state( struct lp_setup_context *setup,
1201 boolean update_scene )
1202 {
1203 /* Some of the 'draw' pipeline stages may have changed some driver state.
1204 * Make sure we've processed those state changes before anything else.
1205 *
1206 * XXX this is the only place where llvmpipe_context is used in the
1207 * setup code. This may get refactored/changed...
1208 */
1209 {
1210 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
1211 if (lp->dirty) {
1212 llvmpipe_update_derived(lp);
1213 }
1214
1215 if (lp->setup->dirty) {
1216 llvmpipe_update_setup(lp);
1217 }
1218
1219 assert(setup->setup.variant);
1220
1221 /* Will probably need to move this somewhere else, just need
1222 * to know about vertex shader point size attribute.
1223 */
1224 setup->psize_slot = lp->psize_slot;
1225 setup->viewport_index_slot = lp->viewport_index_slot;
1226 setup->layer_slot = lp->layer_slot;
1227 setup->face_slot = lp->face_slot;
1228
1229 assert(lp->dirty == 0);
1230
1231 assert(lp->setup_variant.key.size ==
1232 setup->setup.variant->key.size);
1233
1234 assert(memcmp(&lp->setup_variant.key,
1235 &setup->setup.variant->key,
1236 setup->setup.variant->key.size) == 0);
1237 }
1238
1239 if (update_scene && setup->state != SETUP_ACTIVE) {
1240 if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))
1241 return FALSE;
1242 }
1243
1244 /* Only call into update_scene_state() if we already have a
1245 * scene:
1246 */
1247 if (update_scene && setup->scene) {
1248 assert(setup->state == SETUP_ACTIVE);
1249
1250 if (try_update_scene_state(setup))
1251 return TRUE;
1252
1253 /* Update failed, try to restart the scene.
1254 *
1255 * Cannot call lp_setup_flush_and_restart() directly here
1256 * because of potential recursion.
1257 */
1258 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1259 return FALSE;
1260
1261 if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))
1262 return FALSE;
1263
1264 if (!setup->scene)
1265 return FALSE;
1266
1267 return try_update_scene_state(setup);
1268 }
1269
1270 return TRUE;
1271 }
1272
1273
1274
1275 /* Only caller is lp_setup_vbuf_destroy()
1276 */
1277 void
1278 lp_setup_destroy( struct lp_setup_context *setup )
1279 {
1280 uint i;
1281
1282 lp_setup_reset( setup );
1283
1284 util_unreference_framebuffer_state(&setup->fb);
1285
1286 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1287 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
1288 }
1289
1290 for (i = 0; i < Elements(setup->constants); i++) {
1291 pipe_resource_reference(&setup->constants[i].current.buffer, NULL);
1292 }
1293
1294 /* free the scenes in the 'empty' queue */
1295 for (i = 0; i < Elements(setup->scenes); i++) {
1296 struct lp_scene *scene = setup->scenes[i];
1297
1298 if (scene->fence)
1299 lp_fence_wait(scene->fence);
1300
1301 lp_scene_destroy(scene);
1302 }
1303
1304 lp_fence_reference(&setup->last_fence, NULL);
1305
1306 FREE( setup );
1307 }
1308
1309
1310 /**
1311 * Create a new primitive tiling engine. Plug it into the backend of
1312 * the draw module. Currently also creates a rasterizer to use with
1313 * it.
1314 */
1315 struct lp_setup_context *
1316 lp_setup_create( struct pipe_context *pipe,
1317 struct draw_context *draw )
1318 {
1319 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
1320 struct lp_setup_context *setup;
1321 unsigned i;
1322
1323 setup = CALLOC_STRUCT(lp_setup_context);
1324 if (!setup) {
1325 goto no_setup;
1326 }
1327
1328 lp_setup_init_vbuf(setup);
1329
1330 /* Used only in update_state():
1331 */
1332 setup->pipe = pipe;
1333
1334
1335 setup->num_threads = screen->num_threads;
1336 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
1337 if (!setup->vbuf) {
1338 goto no_vbuf;
1339 }
1340
1341 draw_set_rasterize_stage(draw, setup->vbuf);
1342 draw_set_render(draw, &setup->base);
1343
1344 /* create some empty scenes */
1345 for (i = 0; i < MAX_SCENES; i++) {
1346 setup->scenes[i] = lp_scene_create( pipe );
1347 if (!setup->scenes[i]) {
1348 goto no_scenes;
1349 }
1350 }
1351
1352 setup->triangle = first_triangle;
1353 setup->line = first_line;
1354 setup->point = first_point;
1355
1356 setup->dirty = ~0;
1357
1358 return setup;
1359
1360 no_scenes:
1361 for (i = 0; i < MAX_SCENES; i++) {
1362 if (setup->scenes[i]) {
1363 lp_scene_destroy(setup->scenes[i]);
1364 }
1365 }
1366
1367 setup->vbuf->destroy(setup->vbuf);
1368 no_vbuf:
1369 FREE(setup);
1370 no_setup:
1371 return NULL;
1372 }
1373
1374
1375 /**
1376 * Put a BeginQuery command into all bins.
1377 */
1378 void
1379 lp_setup_begin_query(struct lp_setup_context *setup,
1380 struct llvmpipe_query *pq)
1381 {
1382
1383 set_scene_state(setup, SETUP_ACTIVE, "begin_query");
1384
1385 if (!(pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1386 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1387 pq->type == PIPE_QUERY_PIPELINE_STATISTICS))
1388 return;
1389
1390 /* init the query to its beginning state */
1391 assert(setup->active_binned_queries < LP_MAX_ACTIVE_BINNED_QUERIES);
1392 /* exceeding list size so just ignore the query */
1393 if (setup->active_binned_queries >= LP_MAX_ACTIVE_BINNED_QUERIES) {
1394 return;
1395 }
1396 assert(setup->active_queries[setup->active_binned_queries] == NULL);
1397 setup->active_queries[setup->active_binned_queries] = pq;
1398 setup->active_binned_queries++;
1399
1400 assert(setup->scene);
1401 if (setup->scene) {
1402 if (!lp_scene_bin_everywhere(setup->scene,
1403 LP_RAST_OP_BEGIN_QUERY,
1404 lp_rast_arg_query(pq))) {
1405
1406 if (!lp_setup_flush_and_restart(setup))
1407 return;
1408
1409 if (!lp_scene_bin_everywhere(setup->scene,
1410 LP_RAST_OP_BEGIN_QUERY,
1411 lp_rast_arg_query(pq))) {
1412 return;
1413 }
1414 }
1415 setup->scene->had_queries |= TRUE;
1416 }
1417 }
1418
1419
1420 /**
1421 * Put an EndQuery command into all bins.
1422 */
1423 void
1424 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1425 {
1426 set_scene_state(setup, SETUP_ACTIVE, "end_query");
1427
1428 assert(setup->scene);
1429 if (setup->scene) {
1430 /* pq->fence should be the fence of the *last* scene which
1431 * contributed to the query result.
1432 */
1433 lp_fence_reference(&pq->fence, setup->scene->fence);
1434
1435 if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1436 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1437 pq->type == PIPE_QUERY_PIPELINE_STATISTICS ||
1438 pq->type == PIPE_QUERY_TIMESTAMP) {
1439 if (pq->type == PIPE_QUERY_TIMESTAMP &&
1440 !(setup->scene->tiles_x | setup->scene->tiles_y)) {
1441 /*
1442 * If there's a zero width/height framebuffer, there's no bins and
1443 * hence no rast task is ever run. So fill in something here instead.
1444 */
1445 pq->end[0] = os_time_get_nano();
1446 }
1447
1448 if (!lp_scene_bin_everywhere(setup->scene,
1449 LP_RAST_OP_END_QUERY,
1450 lp_rast_arg_query(pq))) {
1451 if (!lp_setup_flush_and_restart(setup))
1452 goto fail;
1453
1454 if (!lp_scene_bin_everywhere(setup->scene,
1455 LP_RAST_OP_END_QUERY,
1456 lp_rast_arg_query(pq))) {
1457 goto fail;
1458 }
1459 }
1460 setup->scene->had_queries |= TRUE;
1461 }
1462 }
1463 else {
1464 lp_fence_reference(&pq->fence, setup->last_fence);
1465 }
1466
1467 fail:
1468 /* Need to do this now not earlier since it still needs to be marked as
1469 * active when binning it would cause a flush.
1470 */
1471 if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1472 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1473 pq->type == PIPE_QUERY_PIPELINE_STATISTICS) {
1474 unsigned i;
1475
1476 /* remove from active binned query list */
1477 for (i = 0; i < setup->active_binned_queries; i++) {
1478 if (setup->active_queries[i] == pq)
1479 break;
1480 }
1481 assert(i < setup->active_binned_queries);
1482 if (i == setup->active_binned_queries)
1483 return;
1484 setup->active_binned_queries--;
1485 setup->active_queries[i] = setup->active_queries[setup->active_binned_queries];
1486 setup->active_queries[setup->active_binned_queries] = NULL;
1487 }
1488 }
1489
1490
1491 boolean
1492 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1493 {
1494 if (0) debug_printf("%s\n", __FUNCTION__);
1495
1496 assert(setup->state == SETUP_ACTIVE);
1497
1498 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1499 return FALSE;
1500
1501 if (!lp_setup_update_state(setup, TRUE))
1502 return FALSE;
1503
1504 return TRUE;
1505 }
1506
1507