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