llvmpipe: handle more queries
[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 "draw/draw_pipe.h"
43 #include "lp_context.h"
44 #include "lp_memory.h"
45 #include "lp_scene.h"
46 #include "lp_texture.h"
47 #include "lp_debug.h"
48 #include "lp_fence.h"
49 #include "lp_query.h"
50 #include "lp_rast.h"
51 #include "lp_setup_context.h"
52 #include "lp_screen.h"
53 #include "lp_state.h"
54 #include "state_tracker/sw_winsys.h"
55
56 #include "draw/draw_context.h"
57 #include "draw/draw_vbuf.h"
58
59
60 static boolean set_scene_state( struct lp_setup_context *, enum setup_state,
61 const char *reason);
62 static boolean try_update_scene_state( struct lp_setup_context *setup );
63
64
65 static void
66 lp_setup_get_empty_scene(struct lp_setup_context *setup)
67 {
68 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
69 boolean discard = lp->rasterizer ? lp->rasterizer->rasterizer_discard : FALSE;
70
71 assert(setup->scene == NULL);
72
73 setup->scene_idx++;
74 setup->scene_idx %= Elements(setup->scenes);
75
76 setup->scene = setup->scenes[setup->scene_idx];
77
78 if (setup->scene->fence) {
79 if (LP_DEBUG & DEBUG_SETUP)
80 debug_printf("%s: wait for scene %d\n",
81 __FUNCTION__, setup->scene->fence->id);
82
83 lp_fence_wait(setup->scene->fence);
84 }
85
86 lp_scene_begin_binning(setup->scene, &setup->fb, discard);
87
88 }
89
90
91 static void
92 first_triangle( struct lp_setup_context *setup,
93 const float (*v0)[4],
94 const float (*v1)[4],
95 const float (*v2)[4])
96 {
97 assert(setup->state == SETUP_ACTIVE);
98 lp_setup_choose_triangle( setup );
99 setup->triangle( setup, v0, v1, v2 );
100 }
101
102 static void
103 first_line( struct lp_setup_context *setup,
104 const float (*v0)[4],
105 const float (*v1)[4])
106 {
107 assert(setup->state == SETUP_ACTIVE);
108 lp_setup_choose_line( setup );
109 setup->line( setup, v0, v1 );
110 }
111
112 static void
113 first_point( struct lp_setup_context *setup,
114 const float (*v0)[4])
115 {
116 assert(setup->state == SETUP_ACTIVE);
117 lp_setup_choose_point( setup );
118 setup->point( setup, v0 );
119 }
120
121 void lp_setup_reset( struct lp_setup_context *setup )
122 {
123 unsigned i;
124
125 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
126
127 /* Reset derived state */
128 for (i = 0; i < Elements(setup->constants); ++i) {
129 setup->constants[i].stored_size = 0;
130 setup->constants[i].stored_data = NULL;
131 }
132 setup->fs.stored = NULL;
133 setup->dirty = ~0;
134
135 /* no current bin */
136 setup->scene = NULL;
137
138 /* Reset some state:
139 */
140 memset(&setup->clear, 0, sizeof setup->clear);
141
142 /* Have an explicit "start-binning" call and get rid of this
143 * pointer twiddling?
144 */
145 setup->line = first_line;
146 setup->point = first_point;
147 setup->triangle = first_triangle;
148 }
149
150
151 /** Rasterize all scene's bins */
152 static void
153 lp_setup_rasterize_scene( struct lp_setup_context *setup )
154 {
155 struct lp_scene *scene = setup->scene;
156 struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
157
158 lp_scene_end_binning(scene);
159
160 lp_fence_reference(&setup->last_fence, scene->fence);
161
162 if (setup->last_fence)
163 setup->last_fence->issued = TRUE;
164
165 pipe_mutex_lock(screen->rast_mutex);
166 lp_rast_queue_scene(screen->rast, scene);
167 lp_rast_finish(screen->rast);
168 pipe_mutex_unlock(screen->rast_mutex);
169
170 lp_scene_end_rasterization(setup->scene);
171 lp_setup_reset( setup );
172
173 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
174 }
175
176
177
178 static boolean
179 begin_binning( struct lp_setup_context *setup )
180 {
181 struct lp_scene *scene = setup->scene;
182 boolean need_zsload = FALSE;
183 boolean ok;
184 unsigned i;
185
186 assert(scene);
187 assert(scene->fence == NULL);
188
189 /* Always create a fence:
190 */
191 scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
192 if (!scene->fence)
193 return FALSE;
194
195 ok = try_update_scene_state(setup);
196 if (!ok)
197 return FALSE;
198
199 if (setup->fb.zsbuf &&
200 ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
201 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
202 need_zsload = TRUE;
203
204 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
205 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
206 need_zsload ? "clear": "load");
207
208 if (setup->fb.nr_cbufs) {
209 if (setup->clear.flags & PIPE_CLEAR_COLOR) {
210 ok = lp_scene_bin_everywhere( scene,
211 LP_RAST_OP_CLEAR_COLOR,
212 setup->clear.color );
213 if (!ok)
214 return FALSE;
215 }
216 }
217
218 if (setup->fb.zsbuf) {
219 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
220 if (!need_zsload)
221 scene->has_depthstencil_clear = TRUE;
222
223 ok = lp_scene_bin_everywhere( scene,
224 LP_RAST_OP_CLEAR_ZSTENCIL,
225 lp_rast_arg_clearzs(
226 setup->clear.zsvalue,
227 setup->clear.zsmask));
228 if (!ok)
229 return FALSE;
230 }
231 }
232
233 for (i = 0; i < PIPE_QUERY_TYPES; ++i) {
234 if (setup->active_query[i]) {
235 ok = lp_scene_bin_everywhere( scene,
236 LP_RAST_OP_BEGIN_QUERY,
237 lp_rast_arg_query(setup->active_query[i]) );
238 if (!ok)
239 return FALSE;
240 }
241 }
242
243 setup->clear.flags = 0;
244 setup->clear.zsmask = 0;
245 setup->clear.zsvalue = 0;
246
247 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
248 return TRUE;
249 }
250
251
252 /* This basically bins and then flushes any outstanding full-screen
253 * clears.
254 *
255 * TODO: fast path for fullscreen clears and no triangles.
256 */
257 static boolean
258 execute_clears( struct lp_setup_context *setup )
259 {
260 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
261
262 return begin_binning( setup );
263 }
264
265 const char *states[] = {
266 "FLUSHED",
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 void
338 lp_setup_flush( struct lp_setup_context *setup,
339 struct pipe_fence_handle **fence,
340 const char *reason)
341 {
342 set_scene_state( setup, SETUP_FLUSHED, reason );
343
344 if (fence) {
345 lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
346 }
347 }
348
349
350 void
351 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
352 const struct pipe_framebuffer_state *fb )
353 {
354 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
355
356 /* Flush any old scene.
357 */
358 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
359
360 /*
361 * Ensure the old scene is not reused.
362 */
363 assert(!setup->scene);
364
365 /* Set new state. This will be picked up later when we next need a
366 * scene.
367 */
368 util_copy_framebuffer_state(&setup->fb, fb);
369 setup->framebuffer.x0 = 0;
370 setup->framebuffer.y0 = 0;
371 setup->framebuffer.x1 = fb->width-1;
372 setup->framebuffer.y1 = fb->height-1;
373 setup->dirty |= LP_SETUP_NEW_SCISSOR;
374 }
375
376
377 static boolean
378 lp_setup_try_clear( struct lp_setup_context *setup,
379 const union pipe_color_union *color,
380 double depth,
381 unsigned stencil,
382 unsigned flags )
383 {
384 uint64_t zsmask = 0;
385 uint64_t zsvalue = 0;
386 union lp_rast_cmd_arg color_arg;
387 unsigned i;
388
389 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
390
391 if (flags & PIPE_CLEAR_COLOR) {
392 for (i = 0; i < 4; i++)
393 color_arg.clear_color.i[i] = color->i[i];
394 }
395
396 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
397 uint32_t zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
398 uint8_t smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
399
400 zsvalue = util_pack64_z_stencil(setup->fb.zsbuf->format,
401 depth,
402 stencil);
403
404
405 zsmask = util_pack64_mask_z_stencil(setup->fb.zsbuf->format,
406 zmask,
407 smask);
408
409 zsvalue &= zsmask;
410 }
411
412 if (setup->state == SETUP_ACTIVE) {
413 struct lp_scene *scene = setup->scene;
414
415 /* Add the clear to existing scene. In the unusual case where
416 * both color and depth-stencil are being cleared when there's
417 * already been some rendering, we could discard the currently
418 * binned scene and start again, but I don't see that as being
419 * a common usage.
420 */
421 if (flags & PIPE_CLEAR_COLOR) {
422 if (!lp_scene_bin_everywhere( scene,
423 LP_RAST_OP_CLEAR_COLOR,
424 color_arg ))
425 return FALSE;
426 }
427
428 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
429 if (!lp_scene_bin_everywhere( scene,
430 LP_RAST_OP_CLEAR_ZSTENCIL,
431 lp_rast_arg_clearzs(zsvalue, zsmask) ))
432 return FALSE;
433 }
434 }
435 else {
436 /* Put ourselves into the 'pre-clear' state, specifically to try
437 * and accumulate multiple clears to color and depth_stencil
438 * buffers which the app or state-tracker might issue
439 * separately.
440 */
441 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
442
443 setup->clear.flags |= flags;
444
445 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
446 setup->clear.zsmask |= zsmask;
447 setup->clear.zsvalue =
448 (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
449 }
450
451 if (flags & PIPE_CLEAR_COLOR) {
452 memcpy(&setup->clear.color.clear_color,
453 &color_arg,
454 sizeof setup->clear.color.clear_color);
455 }
456 }
457
458 return TRUE;
459 }
460
461 void
462 lp_setup_clear( struct lp_setup_context *setup,
463 const union pipe_color_union *color,
464 double depth,
465 unsigned stencil,
466 unsigned flags )
467 {
468 if (!lp_setup_try_clear( setup, color, depth, stencil, flags )) {
469 lp_setup_flush(setup, NULL, __FUNCTION__);
470
471 if (!lp_setup_try_clear( setup, color, depth, stencil, flags ))
472 assert(0);
473 }
474 }
475
476
477
478
479
480 void
481 lp_setup_set_triangle_state( struct lp_setup_context *setup,
482 unsigned cull_mode,
483 boolean ccw_is_frontface,
484 boolean scissor,
485 boolean half_pixel_center,
486 boolean bottom_edge_rule)
487 {
488 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
489
490 setup->ccw_is_frontface = ccw_is_frontface;
491 setup->cullmode = cull_mode;
492 setup->triangle = first_triangle;
493 setup->pixel_offset = half_pixel_center ? 0.5f : 0.0f;
494 setup->bottom_edge_rule = bottom_edge_rule;
495
496 if (setup->scissor_test != scissor) {
497 setup->dirty |= LP_SETUP_NEW_SCISSOR;
498 setup->scissor_test = scissor;
499 }
500 }
501
502 void
503 lp_setup_set_line_state( struct lp_setup_context *setup,
504 float line_width)
505 {
506 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
507
508 setup->line_width = line_width;
509 }
510
511 void
512 lp_setup_set_point_state( struct lp_setup_context *setup,
513 float point_size,
514 boolean point_size_per_vertex,
515 uint sprite_coord_enable,
516 uint sprite_coord_origin)
517 {
518 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
519
520 setup->point_size = point_size;
521 setup->sprite_coord_enable = sprite_coord_enable;
522 setup->sprite_coord_origin = sprite_coord_origin;
523 setup->point_size_per_vertex = point_size_per_vertex;
524 }
525
526 void
527 lp_setup_set_setup_variant( struct lp_setup_context *setup,
528 const struct lp_setup_variant *variant)
529 {
530 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
531
532 setup->setup.variant = variant;
533 }
534
535 void
536 lp_setup_set_fs_variant( struct lp_setup_context *setup,
537 struct lp_fragment_shader_variant *variant)
538 {
539 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
540 variant);
541 /* FIXME: reference count */
542
543 setup->fs.current.variant = variant;
544 setup->dirty |= LP_SETUP_NEW_FS;
545 }
546
547 void
548 lp_setup_set_fs_constants(struct lp_setup_context *setup,
549 unsigned num,
550 struct pipe_constant_buffer *buffers)
551 {
552 unsigned i;
553
554 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);
555
556 assert(num <= Elements(setup->constants));
557
558 for (i = 0; i < num; ++i) {
559 util_copy_constant_buffer(&setup->constants[i].current, &buffers[i]);
560 }
561 for (; i < Elements(setup->constants); i++) {
562 util_copy_constant_buffer(&setup->constants[i].current, NULL);
563 }
564 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
565 }
566
567
568 void
569 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
570 float alpha_ref_value )
571 {
572 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
573
574 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
575 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
576 setup->dirty |= LP_SETUP_NEW_FS;
577 }
578 }
579
580 void
581 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
582 const ubyte refs[2] )
583 {
584 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
585
586 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
587 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
588 setup->fs.current.jit_context.stencil_ref_front = refs[0];
589 setup->fs.current.jit_context.stencil_ref_back = refs[1];
590 setup->dirty |= LP_SETUP_NEW_FS;
591 }
592 }
593
594 void
595 lp_setup_set_blend_color( struct lp_setup_context *setup,
596 const struct pipe_blend_color *blend_color )
597 {
598 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
599
600 assert(blend_color);
601
602 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
603 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
604 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
605 }
606 }
607
608
609 void
610 lp_setup_set_scissors( struct lp_setup_context *setup,
611 const struct pipe_scissor_state *scissors )
612 {
613 unsigned i;
614 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
615
616 assert(scissors);
617
618 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
619 setup->scissors[i].x0 = scissors[i].minx;
620 setup->scissors[i].x1 = scissors[i].maxx-1;
621 setup->scissors[i].y0 = scissors[i].miny;
622 setup->scissors[i].y1 = scissors[i].maxy-1;
623 }
624 setup->dirty |= LP_SETUP_NEW_SCISSOR;
625 }
626
627
628 void
629 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
630 boolean flatshade_first )
631 {
632 setup->flatshade_first = flatshade_first;
633 }
634
635 void
636 lp_setup_set_rasterizer_discard( struct lp_setup_context *setup,
637 boolean rasterizer_discard )
638 {
639 if (setup->rasterizer_discard != rasterizer_discard) {
640 setup->rasterizer_discard = rasterizer_discard;
641 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
642 }
643 }
644
645 void
646 lp_setup_set_vertex_info( struct lp_setup_context *setup,
647 struct vertex_info *vertex_info )
648 {
649 /* XXX: just silently holding onto the pointer:
650 */
651 setup->vertex_info = vertex_info;
652 }
653
654
655 /**
656 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
657 */
658 void
659 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
660 unsigned num,
661 struct pipe_sampler_view **views)
662 {
663 unsigned i;
664
665 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
666
667 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
668
669 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
670 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
671
672 if (view) {
673 struct pipe_resource *res = view->texture;
674 struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);
675 struct lp_jit_texture *jit_tex;
676 jit_tex = &setup->fs.current.jit_context.textures[i];
677
678 /* We're referencing the texture's internal data, so save a
679 * reference to it.
680 */
681 pipe_resource_reference(&setup->fs.current_tex[i], res);
682
683 if (!lp_tex->dt) {
684 /* regular texture - setup array of mipmap level offsets */
685 void *mip_ptr;
686 int j;
687 unsigned first_level = 0;
688 unsigned last_level = 0;
689
690 if (llvmpipe_resource_is_texture(res)) {
691 first_level = view->u.tex.first_level;
692 last_level = view->u.tex.last_level;
693 assert(first_level <= last_level);
694 assert(last_level <= res->last_level);
695
696 /*
697 * The complexity here should no longer be necessary.
698 */
699 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, first_level,
700 LP_TEX_USAGE_READ);
701 jit_tex->base = lp_tex->linear_img.data;
702 }
703 else {
704 mip_ptr = lp_tex->data;
705 jit_tex->base = mip_ptr;
706 }
707
708 if ((LP_PERF & PERF_TEX_MEM) || !mip_ptr) {
709 /* out of memory - use dummy tile memory */
710 /* Note if using PERF_TEX_MEM will also skip tile conversion */
711 jit_tex->base = lp_dummy_tile;
712 jit_tex->width = TILE_SIZE/8;
713 jit_tex->height = TILE_SIZE/8;
714 jit_tex->depth = 1;
715 jit_tex->first_level = 0;
716 jit_tex->last_level = 0;
717 jit_tex->mip_offsets[0] = 0;
718 jit_tex->row_stride[0] = 0;
719 jit_tex->img_stride[0] = 0;
720 }
721 else {
722 jit_tex->width = res->width0;
723 jit_tex->height = res->height0;
724 jit_tex->depth = res->depth0;
725 jit_tex->first_level = first_level;
726 jit_tex->last_level = last_level;
727
728 if (llvmpipe_resource_is_texture(res)) {
729 for (j = first_level; j <= last_level; j++) {
730 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, j,
731 LP_TEX_USAGE_READ);
732 jit_tex->mip_offsets[j] = (uint8_t *)mip_ptr - (uint8_t *)jit_tex->base;
733 /*
734 * could get mip offset directly but need call above to
735 * invoke tiled->linear conversion.
736 */
737 assert(lp_tex->linear_mip_offsets[j] == jit_tex->mip_offsets[j]);
738 jit_tex->row_stride[j] = lp_tex->row_stride[j];
739 jit_tex->img_stride[j] = lp_tex->img_stride[j];
740 }
741
742 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
743 res->target == PIPE_TEXTURE_2D_ARRAY) {
744 /*
745 * For array textures, we don't have first_layer, instead
746 * adjust last_layer (stored as depth) plus the mip level offsets
747 * (as we have mip-first layout can't just adjust base ptr).
748 * XXX For mip levels, could do something similar.
749 */
750 jit_tex->depth = view->u.tex.last_layer - view->u.tex.first_layer + 1;
751 for (j = first_level; j <= last_level; j++) {
752 jit_tex->mip_offsets[j] += view->u.tex.first_layer *
753 lp_tex->img_stride[j];
754 }
755 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
756 assert(view->u.tex.last_layer < res->array_size);
757 }
758 }
759 else {
760 /*
761 * For buffers, we don't have first_element, instead adjust
762 * last_element (stored as width) plus the base pointer.
763 */
764 unsigned view_blocksize = util_format_get_blocksize(view->format);
765 /* probably don't really need to fill that out */
766 jit_tex->mip_offsets[0] = 0;
767 jit_tex->row_stride[0] = 0;
768 jit_tex->row_stride[0] = 0;
769
770 /* everything specified in number of elements here. */
771 jit_tex->width = view->u.buf.last_element - view->u.buf.first_element + 1;
772 jit_tex->base = (uint8_t *)jit_tex->base + view->u.buf.first_element *
773 view_blocksize;
774 /* XXX Unsure if we need to sanitize parameters? */
775 assert(view->u.buf.first_element <= view->u.buf.last_element);
776 assert(view->u.buf.last_element * view_blocksize < res->width0);
777 }
778 }
779 }
780 else {
781 /* display target texture/surface */
782 /*
783 * XXX: Where should this be unmapped?
784 */
785 struct llvmpipe_screen *screen = llvmpipe_screen(res->screen);
786 struct sw_winsys *winsys = screen->winsys;
787 jit_tex->base = winsys->displaytarget_map(winsys, lp_tex->dt,
788 PIPE_TRANSFER_READ);
789 jit_tex->row_stride[0] = lp_tex->row_stride[0];
790 jit_tex->img_stride[0] = lp_tex->img_stride[0];
791 jit_tex->mip_offsets[0] = 0;
792 jit_tex->width = res->width0;
793 jit_tex->height = res->height0;
794 jit_tex->depth = res->depth0;
795 jit_tex->first_level = jit_tex->last_level = 0;
796 assert(jit_tex->base);
797 }
798 }
799 }
800
801 setup->dirty |= LP_SETUP_NEW_FS;
802 }
803
804
805 /**
806 * Called during state validation when LP_NEW_SAMPLER is set.
807 */
808 void
809 lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
810 unsigned num,
811 struct pipe_sampler_state **samplers)
812 {
813 unsigned i;
814
815 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
816
817 assert(num <= PIPE_MAX_SAMPLERS);
818
819 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
820 const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
821
822 if (sampler) {
823 struct lp_jit_sampler *jit_sam;
824 jit_sam = &setup->fs.current.jit_context.samplers[i];
825
826 jit_sam->min_lod = sampler->min_lod;
827 jit_sam->max_lod = sampler->max_lod;
828 jit_sam->lod_bias = sampler->lod_bias;
829 COPY_4V(jit_sam->border_color, sampler->border_color.f);
830 }
831 }
832
833 setup->dirty |= LP_SETUP_NEW_FS;
834 }
835
836
837 /**
838 * Is the given texture referenced by any scene?
839 * Note: we have to check all scenes including any scenes currently
840 * being rendered and the current scene being built.
841 */
842 unsigned
843 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
844 const struct pipe_resource *texture )
845 {
846 unsigned i;
847
848 /* check the render targets */
849 for (i = 0; i < setup->fb.nr_cbufs; i++) {
850 if (setup->fb.cbufs[i]->texture == texture)
851 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
852 }
853 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
854 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
855 }
856
857 /* check textures referenced by the scene */
858 for (i = 0; i < Elements(setup->scenes); i++) {
859 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
860 return LP_REFERENCED_FOR_READ;
861 }
862 }
863
864 return LP_UNREFERENCED;
865 }
866
867
868 /**
869 * Called by vbuf code when we're about to draw something.
870 */
871 static boolean
872 try_update_scene_state( struct lp_setup_context *setup )
873 {
874 boolean new_scene = (setup->fs.stored == NULL);
875 struct lp_scene *scene = setup->scene;
876 unsigned i;
877
878 assert(scene);
879
880 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
881 uint8_t *stored;
882 float* fstored;
883 unsigned i, j;
884 unsigned size;
885
886 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
887 size = 4 * 16 * sizeof(uint8_t);
888 size += (LP_MAX_VECTOR_LENGTH / 4) * sizeof(float);
889 stored = lp_scene_alloc_aligned(scene, size, LP_MAX_VECTOR_LENGTH);
890
891 if (!stored) {
892 assert(!new_scene);
893 return FALSE;
894 }
895
896 /* Store floating point colour */
897 fstored = (float*)(stored + 4*16);
898 for (i = 0; i < (LP_MAX_VECTOR_LENGTH / 4); ++i) {
899 fstored[i] = setup->blend_color.current.color[i % 4];
900 }
901
902 /* smear each blend color component across 16 ubyte elements */
903 for (i = 0; i < 4; ++i) {
904 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
905 for (j = 0; j < 16; ++j)
906 stored[i*16 + j] = c;
907 }
908
909 setup->blend_color.stored = stored;
910 setup->fs.current.jit_context.u8_blend_color = stored;
911 setup->fs.current.jit_context.f_blend_color = fstored;
912 setup->dirty |= LP_SETUP_NEW_FS;
913 }
914
915 if (setup->dirty & LP_SETUP_NEW_CONSTANTS) {
916 for (i = 0; i < Elements(setup->constants); ++i) {
917 struct pipe_resource *buffer = setup->constants[i].current.buffer;
918 const unsigned current_size = setup->constants[i].current.buffer_size;
919 const ubyte *current_data = NULL;
920
921 if (buffer) {
922 /* resource buffer */
923 current_data = (ubyte *) llvmpipe_resource_data(buffer);
924 }
925 else if (setup->constants[i].current.user_buffer) {
926 /* user-space buffer */
927 current_data = (ubyte *) setup->constants[i].current.user_buffer;
928 }
929
930 if (current_data) {
931 current_data += setup->constants[i].current.buffer_offset;
932
933 /* TODO: copy only the actually used constants? */
934
935 if (setup->constants[i].stored_size != current_size ||
936 !setup->constants[i].stored_data ||
937 memcmp(setup->constants[i].stored_data,
938 current_data,
939 current_size) != 0) {
940 void *stored;
941
942 stored = lp_scene_alloc(scene, current_size);
943 if (!stored) {
944 assert(!new_scene);
945 return FALSE;
946 }
947
948 memcpy(stored,
949 current_data,
950 current_size);
951 setup->constants[i].stored_size = current_size;
952 setup->constants[i].stored_data = stored;
953 }
954 }
955 else {
956 setup->constants[i].stored_size = 0;
957 setup->constants[i].stored_data = NULL;
958 }
959
960 setup->fs.current.jit_context.constants[i] = setup->constants[i].stored_data;
961 setup->dirty |= LP_SETUP_NEW_FS;
962 }
963 }
964
965
966 if (setup->dirty & LP_SETUP_NEW_FS) {
967 if (!setup->fs.stored ||
968 memcmp(setup->fs.stored,
969 &setup->fs.current,
970 sizeof setup->fs.current) != 0)
971 {
972 struct lp_rast_state *stored;
973
974 /* The fs state that's been stored in the scene is different from
975 * the new, current state. So allocate a new lp_rast_state object
976 * and append it to the bin's setup data buffer.
977 */
978 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
979 if (!stored) {
980 assert(!new_scene);
981 return FALSE;
982 }
983
984 memcpy(stored,
985 &setup->fs.current,
986 sizeof setup->fs.current);
987 setup->fs.stored = stored;
988
989 /* The scene now references the textures in the rasterization
990 * state record. Note that now.
991 */
992 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
993 if (setup->fs.current_tex[i]) {
994 if (!lp_scene_add_resource_reference(scene,
995 setup->fs.current_tex[i],
996 new_scene)) {
997 assert(!new_scene);
998 return FALSE;
999 }
1000 }
1001 }
1002 }
1003 }
1004
1005 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
1006 unsigned i;
1007 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
1008 setup->draw_regions[i] = setup->framebuffer;
1009 if (setup->scissor_test) {
1010 u_rect_possible_intersection(&setup->scissors[i],
1011 &setup->draw_regions[i]);
1012 }
1013 }
1014 /* If the framebuffer is large we have to think about fixed-point
1015 * integer overflow. For 2K by 2K images, coordinates need 15 bits
1016 * (2^11 + 4 subpixel bits). The product of two such numbers would
1017 * use 30 bits. Any larger and we could overflow a 32-bit int.
1018 *
1019 * To cope with this problem we check if triangles are large and
1020 * subdivide them if needed.
1021 */
1022 setup->subdivide_large_triangles = (setup->fb.width > 2048 &&
1023 setup->fb.height > 2048);
1024 }
1025
1026 setup->dirty = 0;
1027
1028 assert(setup->fs.stored);
1029 return TRUE;
1030 }
1031
1032 boolean
1033 lp_setup_update_state( struct lp_setup_context *setup,
1034 boolean update_scene )
1035 {
1036 /* Some of the 'draw' pipeline stages may have changed some driver state.
1037 * Make sure we've processed those state changes before anything else.
1038 *
1039 * XXX this is the only place where llvmpipe_context is used in the
1040 * setup code. This may get refactored/changed...
1041 */
1042 {
1043 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
1044 if (lp->dirty) {
1045 llvmpipe_update_derived(lp);
1046 }
1047
1048 if (lp->setup->dirty) {
1049 llvmpipe_update_setup(lp);
1050 }
1051
1052 assert(setup->setup.variant);
1053
1054 /* Will probably need to move this somewhere else, just need
1055 * to know about vertex shader point size attribute.
1056 */
1057 setup->psize = lp->psize_slot;
1058 setup->viewport_index_slot = lp->viewport_index_slot;
1059 setup->layer_slot = lp->layer_slot;
1060
1061 assert(lp->dirty == 0);
1062
1063 assert(lp->setup_variant.key.size ==
1064 setup->setup.variant->key.size);
1065
1066 assert(memcmp(&lp->setup_variant.key,
1067 &setup->setup.variant->key,
1068 setup->setup.variant->key.size) == 0);
1069 }
1070
1071 if (update_scene && setup->state != SETUP_ACTIVE) {
1072 if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))
1073 return FALSE;
1074 }
1075
1076 /* Only call into update_scene_state() if we already have a
1077 * scene:
1078 */
1079 if (update_scene && setup->scene) {
1080 assert(setup->state == SETUP_ACTIVE);
1081
1082 if (try_update_scene_state(setup))
1083 return TRUE;
1084
1085 /* Update failed, try to restart the scene.
1086 *
1087 * Cannot call lp_setup_flush_and_restart() directly here
1088 * because of potential recursion.
1089 */
1090 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1091 return FALSE;
1092
1093 if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))
1094 return FALSE;
1095
1096 if (!setup->scene)
1097 return FALSE;
1098
1099 return try_update_scene_state(setup);
1100 }
1101
1102 return TRUE;
1103 }
1104
1105
1106
1107 /* Only caller is lp_setup_vbuf_destroy()
1108 */
1109 void
1110 lp_setup_destroy( struct lp_setup_context *setup )
1111 {
1112 uint i;
1113
1114 lp_setup_reset( setup );
1115
1116 util_unreference_framebuffer_state(&setup->fb);
1117
1118 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1119 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
1120 }
1121
1122 for (i = 0; i < Elements(setup->constants); i++) {
1123 pipe_resource_reference(&setup->constants[i].current.buffer, NULL);
1124 }
1125
1126 /* free the scenes in the 'empty' queue */
1127 for (i = 0; i < Elements(setup->scenes); i++) {
1128 struct lp_scene *scene = setup->scenes[i];
1129
1130 if (scene->fence)
1131 lp_fence_wait(scene->fence);
1132
1133 lp_scene_destroy(scene);
1134 }
1135
1136 lp_fence_reference(&setup->last_fence, NULL);
1137
1138 FREE( setup );
1139 }
1140
1141
1142 /**
1143 * Create a new primitive tiling engine. Plug it into the backend of
1144 * the draw module. Currently also creates a rasterizer to use with
1145 * it.
1146 */
1147 struct lp_setup_context *
1148 lp_setup_create( struct pipe_context *pipe,
1149 struct draw_context *draw )
1150 {
1151 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
1152 struct lp_setup_context *setup;
1153 unsigned i;
1154
1155 setup = CALLOC_STRUCT(lp_setup_context);
1156 if (!setup) {
1157 goto no_setup;
1158 }
1159
1160 lp_setup_init_vbuf(setup);
1161
1162 /* Used only in update_state():
1163 */
1164 setup->pipe = pipe;
1165
1166
1167 setup->num_threads = screen->num_threads;
1168 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
1169 if (!setup->vbuf) {
1170 goto no_vbuf;
1171 }
1172
1173 draw_set_rasterize_stage(draw, setup->vbuf);
1174 draw_set_render(draw, &setup->base);
1175
1176 /* create some empty scenes */
1177 for (i = 0; i < MAX_SCENES; i++) {
1178 setup->scenes[i] = lp_scene_create( pipe );
1179 if (!setup->scenes[i]) {
1180 goto no_scenes;
1181 }
1182 }
1183
1184 setup->triangle = first_triangle;
1185 setup->line = first_line;
1186 setup->point = first_point;
1187
1188 setup->dirty = ~0;
1189
1190 return setup;
1191
1192 no_scenes:
1193 for (i = 0; i < MAX_SCENES; i++) {
1194 if (setup->scenes[i]) {
1195 lp_scene_destroy(setup->scenes[i]);
1196 }
1197 }
1198
1199 setup->vbuf->destroy(setup->vbuf);
1200 no_vbuf:
1201 FREE(setup);
1202 no_setup:
1203 return NULL;
1204 }
1205
1206
1207 /**
1208 * Put a BeginQuery command into all bins.
1209 */
1210 void
1211 lp_setup_begin_query(struct lp_setup_context *setup,
1212 struct llvmpipe_query *pq)
1213 {
1214 /* init the query to its beginning state */
1215 assert(setup->active_query[pq->type] == NULL);
1216
1217 set_scene_state(setup, SETUP_ACTIVE, "begin_query");
1218
1219 setup->active_query[pq->type] = pq;
1220
1221 /* XXX: It is possible that a query is created before the scene
1222 * has been created. This means that setup->scene == NULL resulting
1223 * in the query not being binned and thus is ignored.
1224 */
1225
1226 if (setup->scene) {
1227 if (!lp_scene_bin_everywhere(setup->scene,
1228 LP_RAST_OP_BEGIN_QUERY,
1229 lp_rast_arg_query(pq))) {
1230
1231 if (!lp_setup_flush_and_restart(setup))
1232 return;
1233
1234 if (!lp_scene_bin_everywhere(setup->scene,
1235 LP_RAST_OP_BEGIN_QUERY,
1236 lp_rast_arg_query(pq))) {
1237 return;
1238 }
1239 }
1240 }
1241 }
1242
1243
1244 /**
1245 * Put an EndQuery command into all bins.
1246 */
1247 void
1248 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1249 {
1250 set_scene_state(setup, SETUP_ACTIVE, "end_query");
1251
1252 if (pq->type != PIPE_QUERY_TIMESTAMP && pq->type != PIPE_QUERY_GPU_FINISHED) {
1253 assert(setup->active_query[pq->type] == pq);
1254 setup->active_query[pq->type] = NULL;
1255 }
1256
1257 /* Setup will automatically re-issue any query which carried over a
1258 * scene boundary, and the rasterizer automatically "ends" queries
1259 * which are active at the end of a scene, so there is no need to
1260 * retry this commands on failure.
1261 */
1262 if (setup->scene) {
1263 /* pq->fence should be the fence of the *last* scene which
1264 * contributed to the query result.
1265 */
1266 lp_fence_reference(&pq->fence, setup->scene->fence);
1267
1268 if (!lp_scene_bin_everywhere(setup->scene,
1269 LP_RAST_OP_END_QUERY,
1270 lp_rast_arg_query(pq))) {
1271 lp_setup_flush(setup, NULL, __FUNCTION__);
1272 }
1273 }
1274 else {
1275 lp_fence_reference(&pq->fence, setup->last_fence);
1276 }
1277 }
1278
1279
1280 boolean
1281 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1282 {
1283 if (0) debug_printf("%s\n", __FUNCTION__);
1284
1285 assert(setup->state == SETUP_ACTIVE);
1286
1287 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1288 return FALSE;
1289
1290 if (!lp_setup_update_state(setup, TRUE))
1291 return FALSE;
1292
1293 return TRUE;
1294 }
1295
1296