s/Tungsten Graphics/VMware/
[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 lp_rast_queue_scene(screen->rast, scene);
169 lp_rast_finish(screen->rast);
170 pipe_mutex_unlock(screen->rast_mutex);
171
172 lp_scene_end_rasterization(setup->scene);
173 lp_setup_reset( setup );
174
175 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
176 }
177
178
179
180 static boolean
181 begin_binning( struct lp_setup_context *setup )
182 {
183 struct lp_scene *scene = setup->scene;
184 boolean need_zsload = FALSE;
185 boolean ok;
186
187 assert(scene);
188 assert(scene->fence == NULL);
189
190 /* Always create a fence:
191 */
192 scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
193 if (!scene->fence)
194 return FALSE;
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 setup->clear.flags = 0;
235 setup->clear.zsmask = 0;
236 setup->clear.zsvalue = 0;
237
238 scene->had_queries = !!setup->active_binned_queries;
239
240 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
241 return TRUE;
242 }
243
244
245 /* This basically bins and then flushes any outstanding full-screen
246 * clears.
247 *
248 * TODO: fast path for fullscreen clears and no triangles.
249 */
250 static boolean
251 execute_clears( struct lp_setup_context *setup )
252 {
253 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
254
255 return begin_binning( setup );
256 }
257
258 const char *states[] = {
259 "FLUSHED",
260 "CLEARED",
261 "ACTIVE "
262 };
263
264
265 static boolean
266 set_scene_state( struct lp_setup_context *setup,
267 enum setup_state new_state,
268 const char *reason)
269 {
270 unsigned old_state = setup->state;
271
272 if (old_state == new_state)
273 return TRUE;
274
275 if (LP_DEBUG & DEBUG_SCENE) {
276 debug_printf("%s old %s new %s%s%s\n",
277 __FUNCTION__,
278 states[old_state],
279 states[new_state],
280 (new_state == SETUP_FLUSHED) ? ": " : "",
281 (new_state == SETUP_FLUSHED) ? reason : "");
282
283 if (new_state == SETUP_FLUSHED && setup->scene)
284 lp_debug_draw_bins_by_cmd_length(setup->scene);
285 }
286
287 /* wait for a free/empty scene
288 */
289 if (old_state == SETUP_FLUSHED)
290 lp_setup_get_empty_scene(setup);
291
292 switch (new_state) {
293 case SETUP_CLEARED:
294 break;
295
296 case SETUP_ACTIVE:
297 if (!begin_binning( setup ))
298 goto fail;
299 break;
300
301 case SETUP_FLUSHED:
302 if (old_state == SETUP_CLEARED)
303 if (!execute_clears( setup ))
304 goto fail;
305
306 lp_setup_rasterize_scene( setup );
307 assert(setup->scene == NULL);
308 break;
309
310 default:
311 assert(0 && "invalid setup state mode");
312 goto fail;
313 }
314
315 setup->state = new_state;
316 return TRUE;
317
318 fail:
319 if (setup->scene) {
320 lp_scene_end_rasterization(setup->scene);
321 setup->scene = NULL;
322 }
323
324 setup->state = SETUP_FLUSHED;
325 lp_setup_reset( setup );
326 return FALSE;
327 }
328
329
330 void
331 lp_setup_flush( struct lp_setup_context *setup,
332 struct pipe_fence_handle **fence,
333 const char *reason)
334 {
335 set_scene_state( setup, SETUP_FLUSHED, reason );
336
337 if (fence) {
338 lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
339 }
340 }
341
342
343 void
344 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
345 const struct pipe_framebuffer_state *fb )
346 {
347 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
348
349 /* Flush any old scene.
350 */
351 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
352
353 /*
354 * Ensure the old scene is not reused.
355 */
356 assert(!setup->scene);
357
358 /* Set new state. This will be picked up later when we next need a
359 * scene.
360 */
361 util_copy_framebuffer_state(&setup->fb, fb);
362 setup->framebuffer.x0 = 0;
363 setup->framebuffer.y0 = 0;
364 setup->framebuffer.x1 = fb->width-1;
365 setup->framebuffer.y1 = fb->height-1;
366 setup->dirty |= LP_SETUP_NEW_SCISSOR;
367 }
368
369
370 static boolean
371 lp_setup_try_clear( struct lp_setup_context *setup,
372 const union pipe_color_union *color,
373 double depth,
374 unsigned stencil,
375 unsigned flags )
376 {
377 uint64_t zsmask = 0;
378 uint64_t zsvalue = 0;
379 union lp_rast_cmd_arg color_arg;
380 unsigned i;
381
382 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
383
384 if (flags & PIPE_CLEAR_COLOR) {
385 for (i = 0; i < 4; i++)
386 color_arg.clear_color.i[i] = color->i[i];
387 }
388
389 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
390 uint32_t zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
391 uint8_t smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
392
393 zsvalue = util_pack64_z_stencil(setup->fb.zsbuf->format,
394 depth,
395 stencil);
396
397
398 zsmask = util_pack64_mask_z_stencil(setup->fb.zsbuf->format,
399 zmask,
400 smask);
401
402 zsvalue &= zsmask;
403 }
404
405 if (setup->state == SETUP_ACTIVE) {
406 struct lp_scene *scene = setup->scene;
407
408 /* Add the clear to existing scene. In the unusual case where
409 * both color and depth-stencil are being cleared when there's
410 * already been some rendering, we could discard the currently
411 * binned scene and start again, but I don't see that as being
412 * a common usage.
413 */
414 if (flags & PIPE_CLEAR_COLOR) {
415 if (!lp_scene_bin_everywhere( scene,
416 LP_RAST_OP_CLEAR_COLOR,
417 color_arg ))
418 return FALSE;
419 }
420
421 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
422 if (!lp_scene_bin_everywhere( scene,
423 LP_RAST_OP_CLEAR_ZSTENCIL,
424 lp_rast_arg_clearzs(zsvalue, zsmask) ))
425 return FALSE;
426 }
427 }
428 else {
429 /* Put ourselves into the 'pre-clear' state, specifically to try
430 * and accumulate multiple clears to color and depth_stencil
431 * buffers which the app or state-tracker might issue
432 * separately.
433 */
434 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
435
436 setup->clear.flags |= flags;
437
438 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
439 setup->clear.zsmask |= zsmask;
440 setup->clear.zsvalue =
441 (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
442 }
443
444 if (flags & PIPE_CLEAR_COLOR) {
445 memcpy(&setup->clear.color.clear_color,
446 &color_arg,
447 sizeof setup->clear.color.clear_color);
448 }
449 }
450
451 return TRUE;
452 }
453
454 void
455 lp_setup_clear( struct lp_setup_context *setup,
456 const union pipe_color_union *color,
457 double depth,
458 unsigned stencil,
459 unsigned flags )
460 {
461 if (!lp_setup_try_clear( setup, color, depth, stencil, flags )) {
462 lp_setup_flush(setup, NULL, __FUNCTION__);
463
464 if (!lp_setup_try_clear( setup, color, depth, stencil, flags ))
465 assert(0);
466 }
467 }
468
469
470
471
472
473 void
474 lp_setup_set_triangle_state( struct lp_setup_context *setup,
475 unsigned cull_mode,
476 boolean ccw_is_frontface,
477 boolean scissor,
478 boolean half_pixel_center,
479 boolean bottom_edge_rule)
480 {
481 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
482
483 setup->ccw_is_frontface = ccw_is_frontface;
484 setup->cullmode = cull_mode;
485 setup->triangle = first_triangle;
486 setup->pixel_offset = half_pixel_center ? 0.5f : 0.0f;
487 setup->bottom_edge_rule = bottom_edge_rule;
488
489 if (setup->scissor_test != scissor) {
490 setup->dirty |= LP_SETUP_NEW_SCISSOR;
491 setup->scissor_test = scissor;
492 }
493 }
494
495 void
496 lp_setup_set_line_state( struct lp_setup_context *setup,
497 float line_width)
498 {
499 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
500
501 setup->line_width = line_width;
502 }
503
504 void
505 lp_setup_set_point_state( struct lp_setup_context *setup,
506 float point_size,
507 boolean point_size_per_vertex,
508 uint sprite_coord_enable,
509 uint sprite_coord_origin)
510 {
511 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
512
513 setup->point_size = point_size;
514 setup->sprite_coord_enable = sprite_coord_enable;
515 setup->sprite_coord_origin = sprite_coord_origin;
516 setup->point_size_per_vertex = point_size_per_vertex;
517 }
518
519 void
520 lp_setup_set_setup_variant( struct lp_setup_context *setup,
521 const struct lp_setup_variant *variant)
522 {
523 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
524
525 setup->setup.variant = variant;
526 }
527
528 void
529 lp_setup_set_fs_variant( struct lp_setup_context *setup,
530 struct lp_fragment_shader_variant *variant)
531 {
532 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
533 variant);
534 /* FIXME: reference count */
535
536 setup->fs.current.variant = variant;
537 setup->dirty |= LP_SETUP_NEW_FS;
538 }
539
540 void
541 lp_setup_set_fs_constants(struct lp_setup_context *setup,
542 unsigned num,
543 struct pipe_constant_buffer *buffers)
544 {
545 unsigned i;
546
547 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);
548
549 assert(num <= Elements(setup->constants));
550
551 for (i = 0; i < num; ++i) {
552 util_copy_constant_buffer(&setup->constants[i].current, &buffers[i]);
553 }
554 for (; i < Elements(setup->constants); i++) {
555 util_copy_constant_buffer(&setup->constants[i].current, NULL);
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_scissors( struct lp_setup_context *setup,
604 const struct pipe_scissor_state *scissors )
605 {
606 unsigned i;
607 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
608
609 assert(scissors);
610
611 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
612 setup->scissors[i].x0 = scissors[i].minx;
613 setup->scissors[i].x1 = scissors[i].maxx-1;
614 setup->scissors[i].y0 = scissors[i].miny;
615 setup->scissors[i].y1 = scissors[i].maxy-1;
616 }
617 setup->dirty |= LP_SETUP_NEW_SCISSOR;
618 }
619
620
621 void
622 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
623 boolean flatshade_first )
624 {
625 setup->flatshade_first = flatshade_first;
626 }
627
628 void
629 lp_setup_set_rasterizer_discard( struct lp_setup_context *setup,
630 boolean rasterizer_discard )
631 {
632 if (setup->rasterizer_discard != rasterizer_discard) {
633 setup->rasterizer_discard = rasterizer_discard;
634 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
635 }
636 }
637
638 void
639 lp_setup_set_vertex_info( struct lp_setup_context *setup,
640 struct vertex_info *vertex_info )
641 {
642 /* XXX: just silently holding onto the pointer:
643 */
644 setup->vertex_info = vertex_info;
645 }
646
647
648 /**
649 * Called during state validation when LP_NEW_VIEWPORT is set.
650 */
651 void
652 lp_setup_set_viewports(struct lp_setup_context *setup,
653 unsigned num_viewports,
654 const struct pipe_viewport_state *viewports)
655 {
656 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
657 unsigned i;
658
659 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
660
661 assert(num_viewports <= PIPE_MAX_VIEWPORTS);
662 assert(viewports);
663
664 /*
665 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
666 */
667 for (i = 0; i < num_viewports; i++) {
668 float min_depth;
669 float max_depth;
670
671 if (lp->rasterizer->clip_halfz == 0) {
672 float half_depth = viewports[i].scale[2];
673 min_depth = viewports[i].translate[2] - half_depth;
674 max_depth = min_depth + half_depth * 2.0f;
675 } else {
676 min_depth = viewports[i].translate[2];
677 max_depth = min_depth + viewports[i].scale[2];
678 }
679
680 if (setup->viewports[i].min_depth != min_depth ||
681 setup->viewports[i].max_depth != max_depth) {
682 setup->viewports[i].min_depth = min_depth;
683 setup->viewports[i].max_depth = max_depth;
684 setup->dirty |= LP_SETUP_NEW_VIEWPORTS;
685 }
686 }
687 }
688
689
690 /**
691 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
692 */
693 void
694 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
695 unsigned num,
696 struct pipe_sampler_view **views)
697 {
698 unsigned i;
699
700 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
701
702 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
703
704 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
705 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
706
707 if (view) {
708 struct pipe_resource *res = view->texture;
709 struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);
710 struct lp_jit_texture *jit_tex;
711 jit_tex = &setup->fs.current.jit_context.textures[i];
712
713 /* We're referencing the texture's internal data, so save a
714 * reference to it.
715 */
716 pipe_resource_reference(&setup->fs.current_tex[i], res);
717
718 if (!lp_tex->dt) {
719 /* regular texture - setup array of mipmap level offsets */
720 void *mip_ptr;
721 int j;
722 unsigned first_level = 0;
723 unsigned last_level = 0;
724
725 if (llvmpipe_resource_is_texture(res)) {
726 first_level = view->u.tex.first_level;
727 last_level = view->u.tex.last_level;
728 assert(first_level <= last_level);
729 assert(last_level <= res->last_level);
730
731 /*
732 * The complexity here should no longer be necessary.
733 */
734 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, first_level,
735 LP_TEX_USAGE_READ);
736 jit_tex->base = lp_tex->linear_img.data;
737 }
738 else {
739 mip_ptr = lp_tex->data;
740 jit_tex->base = mip_ptr;
741 }
742
743 if ((LP_PERF & PERF_TEX_MEM) || !mip_ptr) {
744 /* out of memory - use dummy tile memory */
745 /* Note if using PERF_TEX_MEM will also skip tile conversion */
746 jit_tex->base = lp_dummy_tile;
747 jit_tex->width = TILE_SIZE/8;
748 jit_tex->height = TILE_SIZE/8;
749 jit_tex->depth = 1;
750 jit_tex->first_level = 0;
751 jit_tex->last_level = 0;
752 jit_tex->mip_offsets[0] = 0;
753 jit_tex->row_stride[0] = 0;
754 jit_tex->img_stride[0] = 0;
755 }
756 else {
757 jit_tex->width = res->width0;
758 jit_tex->height = res->height0;
759 jit_tex->depth = res->depth0;
760 jit_tex->first_level = first_level;
761 jit_tex->last_level = last_level;
762
763 if (llvmpipe_resource_is_texture(res)) {
764 for (j = first_level; j <= last_level; j++) {
765 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, j,
766 LP_TEX_USAGE_READ);
767 jit_tex->mip_offsets[j] = (uint8_t *)mip_ptr - (uint8_t *)jit_tex->base;
768 /*
769 * could get mip offset directly but need call above to
770 * invoke tiled->linear conversion.
771 */
772 assert(lp_tex->linear_mip_offsets[j] == jit_tex->mip_offsets[j]);
773 jit_tex->row_stride[j] = lp_tex->row_stride[j];
774 jit_tex->img_stride[j] = lp_tex->img_stride[j];
775 }
776
777 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
778 res->target == PIPE_TEXTURE_2D_ARRAY) {
779 /*
780 * For array textures, we don't have first_layer, instead
781 * adjust last_layer (stored as depth) plus the mip level offsets
782 * (as we have mip-first layout can't just adjust base ptr).
783 * XXX For mip levels, could do something similar.
784 */
785 jit_tex->depth = view->u.tex.last_layer - view->u.tex.first_layer + 1;
786 for (j = first_level; j <= last_level; j++) {
787 jit_tex->mip_offsets[j] += view->u.tex.first_layer *
788 lp_tex->img_stride[j];
789 }
790 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
791 assert(view->u.tex.last_layer < res->array_size);
792 }
793 }
794 else {
795 /*
796 * For buffers, we don't have first_element, instead adjust
797 * last_element (stored as width) plus the base pointer.
798 */
799 unsigned view_blocksize = util_format_get_blocksize(view->format);
800 /* probably don't really need to fill that out */
801 jit_tex->mip_offsets[0] = 0;
802 jit_tex->row_stride[0] = 0;
803 jit_tex->row_stride[0] = 0;
804
805 /* everything specified in number of elements here. */
806 jit_tex->width = view->u.buf.last_element - view->u.buf.first_element + 1;
807 jit_tex->base = (uint8_t *)jit_tex->base + view->u.buf.first_element *
808 view_blocksize;
809 /* XXX Unsure if we need to sanitize parameters? */
810 assert(view->u.buf.first_element <= view->u.buf.last_element);
811 assert(view->u.buf.last_element * view_blocksize < res->width0);
812 }
813 }
814 }
815 else {
816 /* display target texture/surface */
817 /*
818 * XXX: Where should this be unmapped?
819 */
820 struct llvmpipe_screen *screen = llvmpipe_screen(res->screen);
821 struct sw_winsys *winsys = screen->winsys;
822 jit_tex->base = winsys->displaytarget_map(winsys, lp_tex->dt,
823 PIPE_TRANSFER_READ);
824 jit_tex->row_stride[0] = lp_tex->row_stride[0];
825 jit_tex->img_stride[0] = lp_tex->img_stride[0];
826 jit_tex->mip_offsets[0] = 0;
827 jit_tex->width = res->width0;
828 jit_tex->height = res->height0;
829 jit_tex->depth = res->depth0;
830 jit_tex->first_level = jit_tex->last_level = 0;
831 assert(jit_tex->base);
832 }
833 }
834 }
835
836 setup->dirty |= LP_SETUP_NEW_FS;
837 }
838
839
840 /**
841 * Called during state validation when LP_NEW_SAMPLER is set.
842 */
843 void
844 lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
845 unsigned num,
846 struct pipe_sampler_state **samplers)
847 {
848 unsigned i;
849
850 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
851
852 assert(num <= PIPE_MAX_SAMPLERS);
853
854 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
855 const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
856
857 if (sampler) {
858 struct lp_jit_sampler *jit_sam;
859 jit_sam = &setup->fs.current.jit_context.samplers[i];
860
861 jit_sam->min_lod = sampler->min_lod;
862 jit_sam->max_lod = sampler->max_lod;
863 jit_sam->lod_bias = sampler->lod_bias;
864 COPY_4V(jit_sam->border_color, sampler->border_color.f);
865 }
866 }
867
868 setup->dirty |= LP_SETUP_NEW_FS;
869 }
870
871
872 /**
873 * Is the given texture referenced by any scene?
874 * Note: we have to check all scenes including any scenes currently
875 * being rendered and the current scene being built.
876 */
877 unsigned
878 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
879 const struct pipe_resource *texture )
880 {
881 unsigned i;
882
883 /* check the render targets */
884 for (i = 0; i < setup->fb.nr_cbufs; i++) {
885 if (setup->fb.cbufs[i] && setup->fb.cbufs[i]->texture == texture)
886 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
887 }
888 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
889 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
890 }
891
892 /* check textures referenced by the scene */
893 for (i = 0; i < Elements(setup->scenes); i++) {
894 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
895 return LP_REFERENCED_FOR_READ;
896 }
897 }
898
899 return LP_UNREFERENCED;
900 }
901
902
903 /**
904 * Called by vbuf code when we're about to draw something.
905 *
906 * This function stores all dirty state in the current scene's display list
907 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
908 * the JIT functions, as the JIT functions will be called later on, most likely
909 * on a different thread.
910 *
911 * When processing dirty state it is imperative that we don't refer to any
912 * pointers previously allocated with lp_scene_alloc() in this function (or any
913 * function) as they may belong to a scene freed since then.
914 */
915 static boolean
916 try_update_scene_state( struct lp_setup_context *setup )
917 {
918 boolean new_scene = (setup->fs.stored == NULL);
919 struct lp_scene *scene = setup->scene;
920 unsigned i;
921
922 assert(scene);
923
924 if (setup->dirty & LP_SETUP_NEW_VIEWPORTS) {
925 /*
926 * Record new depth range state for changes due to viewport updates.
927 *
928 * TODO: Collapse the existing viewport and depth range information
929 * into one structure, for access by JIT.
930 */
931 struct lp_jit_viewport *stored;
932
933 stored = (struct lp_jit_viewport *)
934 lp_scene_alloc(scene, sizeof setup->viewports);
935
936 if (!stored) {
937 assert(!new_scene);
938 return FALSE;
939 }
940
941 memcpy(stored, setup->viewports, sizeof setup->viewports);
942
943 setup->fs.current.jit_context.viewports = stored;
944 setup->dirty |= LP_SETUP_NEW_FS;
945 }
946
947 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
948 uint8_t *stored;
949 float* fstored;
950 unsigned i, j;
951 unsigned size;
952
953 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
954 size = 4 * 16 * sizeof(uint8_t);
955 size += (LP_MAX_VECTOR_LENGTH / 4) * sizeof(float);
956 stored = lp_scene_alloc_aligned(scene, size, LP_MAX_VECTOR_LENGTH);
957
958 if (!stored) {
959 assert(!new_scene);
960 return FALSE;
961 }
962
963 /* Store floating point colour */
964 fstored = (float*)(stored + 4*16);
965 for (i = 0; i < (LP_MAX_VECTOR_LENGTH / 4); ++i) {
966 fstored[i] = setup->blend_color.current.color[i % 4];
967 }
968
969 /* smear each blend color component across 16 ubyte elements */
970 for (i = 0; i < 4; ++i) {
971 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
972 for (j = 0; j < 16; ++j)
973 stored[i*16 + j] = c;
974 }
975
976 setup->blend_color.stored = stored;
977 setup->fs.current.jit_context.u8_blend_color = stored;
978 setup->fs.current.jit_context.f_blend_color = fstored;
979 setup->dirty |= LP_SETUP_NEW_FS;
980 }
981
982 if (setup->dirty & LP_SETUP_NEW_CONSTANTS) {
983 for (i = 0; i < Elements(setup->constants); ++i) {
984 struct pipe_resource *buffer = setup->constants[i].current.buffer;
985 const unsigned current_size = setup->constants[i].current.buffer_size;
986 const ubyte *current_data = NULL;
987 int num_constants;
988
989 if (buffer) {
990 /* resource buffer */
991 current_data = (ubyte *) llvmpipe_resource_data(buffer);
992 }
993 else if (setup->constants[i].current.user_buffer) {
994 /* user-space buffer */
995 current_data = (ubyte *) setup->constants[i].current.user_buffer;
996 }
997
998 if (current_data) {
999 current_data += setup->constants[i].current.buffer_offset;
1000
1001 /* TODO: copy only the actually used constants? */
1002
1003 if (setup->constants[i].stored_size != current_size ||
1004 !setup->constants[i].stored_data ||
1005 memcmp(setup->constants[i].stored_data,
1006 current_data,
1007 current_size) != 0) {
1008 void *stored;
1009
1010 stored = lp_scene_alloc(scene, current_size);
1011 if (!stored) {
1012 assert(!new_scene);
1013 return FALSE;
1014 }
1015
1016 memcpy(stored,
1017 current_data,
1018 current_size);
1019 setup->constants[i].stored_size = current_size;
1020 setup->constants[i].stored_data = stored;
1021 }
1022 }
1023 else {
1024 setup->constants[i].stored_size = 0;
1025 setup->constants[i].stored_data = NULL;
1026 }
1027
1028 setup->fs.current.jit_context.constants[i] =
1029 setup->constants[i].stored_data;
1030 num_constants =
1031 setup->constants[i].stored_size / (sizeof(float) * 4);
1032 setup->fs.current.jit_context.num_constants[i] = num_constants;
1033 setup->dirty |= LP_SETUP_NEW_FS;
1034 }
1035 }
1036
1037
1038 if (setup->dirty & LP_SETUP_NEW_FS) {
1039 if (!setup->fs.stored ||
1040 memcmp(setup->fs.stored,
1041 &setup->fs.current,
1042 sizeof setup->fs.current) != 0)
1043 {
1044 struct lp_rast_state *stored;
1045
1046 /* The fs state that's been stored in the scene is different from
1047 * the new, current state. So allocate a new lp_rast_state object
1048 * and append it to the bin's setup data buffer.
1049 */
1050 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
1051 if (!stored) {
1052 assert(!new_scene);
1053 return FALSE;
1054 }
1055
1056 memcpy(stored,
1057 &setup->fs.current,
1058 sizeof setup->fs.current);
1059 setup->fs.stored = stored;
1060
1061 /* The scene now references the textures in the rasterization
1062 * state record. Note that now.
1063 */
1064 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1065 if (setup->fs.current_tex[i]) {
1066 if (!lp_scene_add_resource_reference(scene,
1067 setup->fs.current_tex[i],
1068 new_scene)) {
1069 assert(!new_scene);
1070 return FALSE;
1071 }
1072 }
1073 }
1074 }
1075 }
1076
1077 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
1078 unsigned i;
1079 for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {
1080 setup->draw_regions[i] = setup->framebuffer;
1081 if (setup->scissor_test) {
1082 u_rect_possible_intersection(&setup->scissors[i],
1083 &setup->draw_regions[i]);
1084 }
1085 }
1086 }
1087
1088 setup->dirty = 0;
1089
1090 assert(setup->fs.stored);
1091 return TRUE;
1092 }
1093
1094 boolean
1095 lp_setup_update_state( struct lp_setup_context *setup,
1096 boolean update_scene )
1097 {
1098 /* Some of the 'draw' pipeline stages may have changed some driver state.
1099 * Make sure we've processed those state changes before anything else.
1100 *
1101 * XXX this is the only place where llvmpipe_context is used in the
1102 * setup code. This may get refactored/changed...
1103 */
1104 {
1105 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
1106 if (lp->dirty) {
1107 llvmpipe_update_derived(lp);
1108 }
1109
1110 if (lp->setup->dirty) {
1111 llvmpipe_update_setup(lp);
1112 }
1113
1114 assert(setup->setup.variant);
1115
1116 /* Will probably need to move this somewhere else, just need
1117 * to know about vertex shader point size attribute.
1118 */
1119 setup->psize = lp->psize_slot;
1120 setup->viewport_index_slot = lp->viewport_index_slot;
1121 setup->layer_slot = lp->layer_slot;
1122 setup->face_slot = lp->face_slot;
1123
1124 assert(lp->dirty == 0);
1125
1126 assert(lp->setup_variant.key.size ==
1127 setup->setup.variant->key.size);
1128
1129 assert(memcmp(&lp->setup_variant.key,
1130 &setup->setup.variant->key,
1131 setup->setup.variant->key.size) == 0);
1132 }
1133
1134 if (update_scene && setup->state != SETUP_ACTIVE) {
1135 if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))
1136 return FALSE;
1137 }
1138
1139 /* Only call into update_scene_state() if we already have a
1140 * scene:
1141 */
1142 if (update_scene && setup->scene) {
1143 assert(setup->state == SETUP_ACTIVE);
1144
1145 if (try_update_scene_state(setup))
1146 return TRUE;
1147
1148 /* Update failed, try to restart the scene.
1149 *
1150 * Cannot call lp_setup_flush_and_restart() directly here
1151 * because of potential recursion.
1152 */
1153 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1154 return FALSE;
1155
1156 if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))
1157 return FALSE;
1158
1159 if (!setup->scene)
1160 return FALSE;
1161
1162 return try_update_scene_state(setup);
1163 }
1164
1165 return TRUE;
1166 }
1167
1168
1169
1170 /* Only caller is lp_setup_vbuf_destroy()
1171 */
1172 void
1173 lp_setup_destroy( struct lp_setup_context *setup )
1174 {
1175 uint i;
1176
1177 lp_setup_reset( setup );
1178
1179 util_unreference_framebuffer_state(&setup->fb);
1180
1181 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1182 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
1183 }
1184
1185 for (i = 0; i < Elements(setup->constants); i++) {
1186 pipe_resource_reference(&setup->constants[i].current.buffer, NULL);
1187 }
1188
1189 /* free the scenes in the 'empty' queue */
1190 for (i = 0; i < Elements(setup->scenes); i++) {
1191 struct lp_scene *scene = setup->scenes[i];
1192
1193 if (scene->fence)
1194 lp_fence_wait(scene->fence);
1195
1196 lp_scene_destroy(scene);
1197 }
1198
1199 lp_fence_reference(&setup->last_fence, NULL);
1200
1201 FREE( setup );
1202 }
1203
1204
1205 /**
1206 * Create a new primitive tiling engine. Plug it into the backend of
1207 * the draw module. Currently also creates a rasterizer to use with
1208 * it.
1209 */
1210 struct lp_setup_context *
1211 lp_setup_create( struct pipe_context *pipe,
1212 struct draw_context *draw )
1213 {
1214 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
1215 struct lp_setup_context *setup;
1216 unsigned i;
1217
1218 setup = CALLOC_STRUCT(lp_setup_context);
1219 if (!setup) {
1220 goto no_setup;
1221 }
1222
1223 lp_setup_init_vbuf(setup);
1224
1225 /* Used only in update_state():
1226 */
1227 setup->pipe = pipe;
1228
1229
1230 setup->num_threads = screen->num_threads;
1231 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
1232 if (!setup->vbuf) {
1233 goto no_vbuf;
1234 }
1235
1236 draw_set_rasterize_stage(draw, setup->vbuf);
1237 draw_set_render(draw, &setup->base);
1238
1239 /* create some empty scenes */
1240 for (i = 0; i < MAX_SCENES; i++) {
1241 setup->scenes[i] = lp_scene_create( pipe );
1242 if (!setup->scenes[i]) {
1243 goto no_scenes;
1244 }
1245 }
1246
1247 setup->triangle = first_triangle;
1248 setup->line = first_line;
1249 setup->point = first_point;
1250
1251 setup->dirty = ~0;
1252
1253 return setup;
1254
1255 no_scenes:
1256 for (i = 0; i < MAX_SCENES; i++) {
1257 if (setup->scenes[i]) {
1258 lp_scene_destroy(setup->scenes[i]);
1259 }
1260 }
1261
1262 setup->vbuf->destroy(setup->vbuf);
1263 no_vbuf:
1264 FREE(setup);
1265 no_setup:
1266 return NULL;
1267 }
1268
1269
1270 /**
1271 * Put a BeginQuery command into all bins.
1272 */
1273 void
1274 lp_setup_begin_query(struct lp_setup_context *setup,
1275 struct llvmpipe_query *pq)
1276 {
1277
1278 set_scene_state(setup, SETUP_ACTIVE, "begin_query");
1279
1280 if (!(pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1281 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1282 pq->type == PIPE_QUERY_PIPELINE_STATISTICS))
1283 return;
1284
1285 /* init the query to its beginning state */
1286 assert(setup->active_binned_queries < LP_MAX_ACTIVE_BINNED_QUERIES);
1287 /* exceeding list size so just ignore the query */
1288 if (setup->active_binned_queries >= LP_MAX_ACTIVE_BINNED_QUERIES) {
1289 return;
1290 }
1291 assert(setup->active_queries[setup->active_binned_queries] == NULL);
1292 setup->active_queries[setup->active_binned_queries] = pq;
1293 setup->active_binned_queries++;
1294
1295 assert(setup->scene);
1296 if (setup->scene) {
1297 if (!lp_scene_bin_everywhere(setup->scene,
1298 LP_RAST_OP_BEGIN_QUERY,
1299 lp_rast_arg_query(pq))) {
1300
1301 if (!lp_setup_flush_and_restart(setup))
1302 return;
1303
1304 if (!lp_scene_bin_everywhere(setup->scene,
1305 LP_RAST_OP_BEGIN_QUERY,
1306 lp_rast_arg_query(pq))) {
1307 return;
1308 }
1309 }
1310 setup->scene->had_queries |= TRUE;
1311 }
1312 }
1313
1314
1315 /**
1316 * Put an EndQuery command into all bins.
1317 */
1318 void
1319 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1320 {
1321 set_scene_state(setup, SETUP_ACTIVE, "end_query");
1322
1323 assert(setup->scene);
1324 if (setup->scene) {
1325 /* pq->fence should be the fence of the *last* scene which
1326 * contributed to the query result.
1327 */
1328 lp_fence_reference(&pq->fence, setup->scene->fence);
1329
1330 if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1331 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1332 pq->type == PIPE_QUERY_PIPELINE_STATISTICS ||
1333 pq->type == PIPE_QUERY_TIMESTAMP) {
1334 if (pq->type == PIPE_QUERY_TIMESTAMP &&
1335 !(setup->scene->tiles_x | setup->scene->tiles_y)) {
1336 /*
1337 * If there's a zero width/height framebuffer, there's no bins and
1338 * hence no rast task is ever run. So fill in something here instead.
1339 */
1340 pq->end[0] = os_time_get_nano();
1341 }
1342
1343 if (!lp_scene_bin_everywhere(setup->scene,
1344 LP_RAST_OP_END_QUERY,
1345 lp_rast_arg_query(pq))) {
1346 if (!lp_setup_flush_and_restart(setup))
1347 goto fail;
1348
1349 if (!lp_scene_bin_everywhere(setup->scene,
1350 LP_RAST_OP_END_QUERY,
1351 lp_rast_arg_query(pq))) {
1352 goto fail;
1353 }
1354 }
1355 setup->scene->had_queries |= TRUE;
1356 }
1357 }
1358 else {
1359 lp_fence_reference(&pq->fence, setup->last_fence);
1360 }
1361
1362 fail:
1363 /* Need to do this now not earlier since it still needs to be marked as
1364 * active when binning it would cause a flush.
1365 */
1366 if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||
1367 pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1368 pq->type == PIPE_QUERY_PIPELINE_STATISTICS) {
1369 unsigned i;
1370
1371 /* remove from active binned query list */
1372 for (i = 0; i < setup->active_binned_queries; i++) {
1373 if (setup->active_queries[i] == pq)
1374 break;
1375 }
1376 assert(i < setup->active_binned_queries);
1377 if (i == setup->active_binned_queries)
1378 return;
1379 setup->active_binned_queries--;
1380 setup->active_queries[i] = setup->active_queries[setup->active_binned_queries];
1381 setup->active_queries[setup->active_binned_queries] = NULL;
1382 }
1383 }
1384
1385
1386 boolean
1387 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1388 {
1389 if (0) debug_printf("%s\n", __FUNCTION__);
1390
1391 assert(setup->state == SETUP_ACTIVE);
1392
1393 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1394 return FALSE;
1395
1396 if (!lp_setup_update_state(setup, TRUE))
1397 return FALSE;
1398
1399 return TRUE;
1400 }
1401
1402