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