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