9fef34e77e051954303e1d3fab67d70b48a18dcb
[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 union pipe_color_union *color,
389 double depth,
390 unsigned stencil,
391 unsigned flags )
392 {
393 uint64_t zsmask = 0;
394 uint64_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[i] = color->i[i];
403 }
404
405 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
406 uint32_t zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
407 uint8_t smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
408
409 zsvalue = util_pack64_z_stencil(setup->fb.zsbuf->format,
410 depth,
411 stencil);
412
413
414 zsmask = util_pack64_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 union pipe_color_union *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 half_pixel_center,
495 boolean bottom_edge_rule)
496 {
497 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
498
499 setup->ccw_is_frontface = ccw_is_frontface;
500 setup->cullmode = cull_mode;
501 setup->triangle = first_triangle;
502 setup->pixel_offset = half_pixel_center ? 0.5f : 0.0f;
503 setup->bottom_edge_rule = bottom_edge_rule;
504
505 if (setup->scissor_test != scissor) {
506 setup->dirty |= LP_SETUP_NEW_SCISSOR;
507 setup->scissor_test = scissor;
508 }
509 }
510
511 void
512 lp_setup_set_line_state( struct lp_setup_context *setup,
513 float line_width)
514 {
515 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
516
517 setup->line_width = line_width;
518 }
519
520 void
521 lp_setup_set_point_state( struct lp_setup_context *setup,
522 float point_size,
523 boolean point_size_per_vertex,
524 uint sprite_coord_enable,
525 uint sprite_coord_origin)
526 {
527 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
528
529 setup->point_size = point_size;
530 setup->sprite_coord_enable = sprite_coord_enable;
531 setup->sprite_coord_origin = sprite_coord_origin;
532 setup->point_size_per_vertex = point_size_per_vertex;
533 }
534
535 void
536 lp_setup_set_setup_variant( struct lp_setup_context *setup,
537 const struct lp_setup_variant *variant)
538 {
539 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
540
541 setup->setup.variant = variant;
542 }
543
544 void
545 lp_setup_set_fs_variant( struct lp_setup_context *setup,
546 struct lp_fragment_shader_variant *variant)
547 {
548 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
549 variant);
550 /* FIXME: reference count */
551
552 setup->fs.current.variant = variant;
553 setup->dirty |= LP_SETUP_NEW_FS;
554 }
555
556 void
557 lp_setup_set_fs_constants(struct lp_setup_context *setup,
558 unsigned num,
559 struct pipe_constant_buffer *buffers)
560 {
561 unsigned i;
562
563 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);
564
565 assert(num <= Elements(setup->constants));
566
567 for (i = 0; i < num; ++i) {
568 util_copy_constant_buffer(&setup->constants[i].current, &buffers[i]);
569 }
570 for (; i < Elements(setup->constants); i++) {
571 util_copy_constant_buffer(&setup->constants[i].current, NULL);
572 }
573 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
574 }
575
576
577 void
578 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
579 float alpha_ref_value )
580 {
581 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
582
583 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
584 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
585 setup->dirty |= LP_SETUP_NEW_FS;
586 }
587 }
588
589 void
590 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
591 const ubyte refs[2] )
592 {
593 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
594
595 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
596 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
597 setup->fs.current.jit_context.stencil_ref_front = refs[0];
598 setup->fs.current.jit_context.stencil_ref_back = refs[1];
599 setup->dirty |= LP_SETUP_NEW_FS;
600 }
601 }
602
603 void
604 lp_setup_set_blend_color( struct lp_setup_context *setup,
605 const struct pipe_blend_color *blend_color )
606 {
607 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
608
609 assert(blend_color);
610
611 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
612 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
613 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
614 }
615 }
616
617
618 void
619 lp_setup_set_scissor( struct lp_setup_context *setup,
620 const struct pipe_scissor_state *scissor )
621 {
622 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
623
624 assert(scissor);
625
626 setup->scissor.x0 = scissor->minx;
627 setup->scissor.x1 = scissor->maxx-1;
628 setup->scissor.y0 = scissor->miny;
629 setup->scissor.y1 = scissor->maxy-1;
630 setup->dirty |= LP_SETUP_NEW_SCISSOR;
631 }
632
633
634 void
635 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
636 boolean flatshade_first )
637 {
638 setup->flatshade_first = flatshade_first;
639 }
640
641 void
642 lp_setup_set_rasterizer_discard( struct lp_setup_context *setup,
643 boolean rasterizer_discard )
644 {
645 if (setup->rasterizer_discard != rasterizer_discard) {
646 setup->rasterizer_discard = rasterizer_discard;
647 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
648 }
649 }
650
651 void
652 lp_setup_set_vertex_info( struct lp_setup_context *setup,
653 struct vertex_info *vertex_info )
654 {
655 /* XXX: just silently holding onto the pointer:
656 */
657 setup->vertex_info = vertex_info;
658 }
659
660
661 /**
662 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
663 */
664 void
665 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
666 unsigned num,
667 struct pipe_sampler_view **views)
668 {
669 unsigned i;
670
671 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
672
673 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
674
675 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
676 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
677
678 if (view) {
679 struct pipe_resource *res = view->texture;
680 struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);
681 struct lp_jit_texture *jit_tex;
682 jit_tex = &setup->fs.current.jit_context.textures[i];
683
684 /* We're referencing the texture's internal data, so save a
685 * reference to it.
686 */
687 pipe_resource_reference(&setup->fs.current_tex[i], res);
688
689 if (!lp_tex->dt) {
690 /* regular texture - setup array of mipmap level offsets */
691 void *mip_ptr;
692 int j;
693 unsigned first_level = 0;
694 unsigned last_level = 0;
695
696 if (llvmpipe_resource_is_texture(res)) {
697 first_level = view->u.tex.first_level;
698 last_level = view->u.tex.last_level;
699 assert(first_level <= last_level);
700 assert(last_level <= res->last_level);
701
702 /*
703 * The complexity here is only necessary for depth textures which
704 * still are tiled.
705 */
706 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, first_level,
707 LP_TEX_USAGE_READ,
708 LP_TEX_LAYOUT_LINEAR);
709 jit_tex->base = lp_tex->linear_img.data;
710 }
711 else {
712 mip_ptr = lp_tex->data;
713 jit_tex->base = mip_ptr;
714 }
715
716 if ((LP_PERF & PERF_TEX_MEM) || !mip_ptr) {
717 /* out of memory - use dummy tile memory */
718 /* Note if using PERF_TEX_MEM will also skip tile conversion */
719 jit_tex->base = lp_dummy_tile;
720 jit_tex->width = TILE_SIZE/8;
721 jit_tex->height = TILE_SIZE/8;
722 jit_tex->depth = 1;
723 jit_tex->first_level = 0;
724 jit_tex->last_level = 0;
725 jit_tex->mip_offsets[0] = 0;
726 jit_tex->row_stride[0] = 0;
727 jit_tex->img_stride[0] = 0;
728 }
729 else {
730 jit_tex->width = res->width0;
731 jit_tex->height = res->height0;
732 jit_tex->depth = res->depth0;
733 jit_tex->first_level = first_level;
734 jit_tex->last_level = last_level;
735
736 if (llvmpipe_resource_is_texture(res)) {
737 for (j = first_level; j <= last_level; j++) {
738 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, j,
739 LP_TEX_USAGE_READ,
740 LP_TEX_LAYOUT_LINEAR);
741 jit_tex->mip_offsets[j] = (uint8_t *)mip_ptr - (uint8_t *)jit_tex->base;
742 /*
743 * could get mip offset directly but need call above to
744 * invoke tiled->linear conversion.
745 */
746 assert(lp_tex->linear_mip_offsets[j] == jit_tex->mip_offsets[j]);
747 jit_tex->row_stride[j] = lp_tex->row_stride[j];
748 jit_tex->img_stride[j] = lp_tex->img_stride[j];
749 }
750
751 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
752 res->target == PIPE_TEXTURE_2D_ARRAY) {
753 /*
754 * For array textures, we don't have first_layer, instead
755 * adjust last_layer (stored as depth) plus the mip level offsets
756 * (as we have mip-first layout can't just adjust base ptr).
757 * XXX For mip levels, could do something similar.
758 */
759 jit_tex->depth = view->u.tex.last_layer - view->u.tex.first_layer + 1;
760 for (j = first_level; j <= last_level; j++) {
761 jit_tex->mip_offsets[j] += view->u.tex.first_layer *
762 lp_tex->img_stride[j];
763 }
764 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
765 assert(view->u.tex.last_layer < res->array_size);
766 }
767 }
768 else {
769 /*
770 * For buffers, we don't have first_element, instead adjust
771 * last_element (stored as width) plus the base pointer.
772 */
773 unsigned view_blocksize = util_format_get_blocksize(view->format);
774 /* probably don't really need to fill that out */
775 jit_tex->mip_offsets[0] = 0;
776 jit_tex->row_stride[0] = 0;
777 jit_tex->row_stride[0] = 0;
778
779 /* everything specified in number of elements here. */
780 jit_tex->width = view->u.buf.last_element - view->u.buf.first_element + 1;
781 jit_tex->base = (uint8_t *)jit_tex->base + view->u.buf.first_element *
782 view_blocksize;
783 /* XXX Unsure if we need to sanitize parameters? */
784 assert(view->u.buf.first_element <= view->u.buf.last_element);
785 assert(view->u.buf.last_element * view_blocksize < res->width0);
786 }
787 }
788 }
789 else {
790 /* display target texture/surface */
791 /*
792 * XXX: Where should this be unmapped?
793 */
794 struct llvmpipe_screen *screen = llvmpipe_screen(res->screen);
795 struct sw_winsys *winsys = screen->winsys;
796 jit_tex->base = winsys->displaytarget_map(winsys, lp_tex->dt,
797 PIPE_TRANSFER_READ);
798 jit_tex->row_stride[0] = lp_tex->row_stride[0];
799 jit_tex->img_stride[0] = lp_tex->img_stride[0];
800 jit_tex->mip_offsets[0] = 0;
801 jit_tex->width = res->width0;
802 jit_tex->height = res->height0;
803 jit_tex->depth = res->depth0;
804 jit_tex->first_level = jit_tex->last_level = 0;
805 assert(jit_tex->base);
806 }
807 }
808 }
809
810 setup->dirty |= LP_SETUP_NEW_FS;
811 }
812
813
814 /**
815 * Called during state validation when LP_NEW_SAMPLER is set.
816 */
817 void
818 lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,
819 unsigned num,
820 struct pipe_sampler_state **samplers)
821 {
822 unsigned i;
823
824 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
825
826 assert(num <= PIPE_MAX_SAMPLERS);
827
828 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
829 const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;
830
831 if (sampler) {
832 struct lp_jit_sampler *jit_sam;
833 jit_sam = &setup->fs.current.jit_context.samplers[i];
834
835 jit_sam->min_lod = sampler->min_lod;
836 jit_sam->max_lod = sampler->max_lod;
837 jit_sam->lod_bias = sampler->lod_bias;
838 COPY_4V(jit_sam->border_color, sampler->border_color.f);
839 }
840 }
841
842 setup->dirty |= LP_SETUP_NEW_FS;
843 }
844
845
846 /**
847 * Is the given texture referenced by any scene?
848 * Note: we have to check all scenes including any scenes currently
849 * being rendered and the current scene being built.
850 */
851 unsigned
852 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
853 const struct pipe_resource *texture )
854 {
855 unsigned i;
856
857 /* check the render targets */
858 for (i = 0; i < setup->fb.nr_cbufs; i++) {
859 if (setup->fb.cbufs[i]->texture == texture)
860 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
861 }
862 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
863 return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
864 }
865
866 /* check textures referenced by the scene */
867 for (i = 0; i < Elements(setup->scenes); i++) {
868 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
869 return LP_REFERENCED_FOR_READ;
870 }
871 }
872
873 return LP_UNREFERENCED;
874 }
875
876
877 /**
878 * Called by vbuf code when we're about to draw something.
879 */
880 static boolean
881 try_update_scene_state( struct lp_setup_context *setup )
882 {
883 boolean new_scene = (setup->fs.stored == NULL);
884 struct lp_scene *scene = setup->scene;
885 unsigned i;
886
887 assert(scene);
888
889 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
890 uint8_t *stored;
891 float* fstored;
892 unsigned i, j;
893 unsigned size;
894
895 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
896 size = 4 * 16 * sizeof(uint8_t);
897 size += (LP_MAX_VECTOR_LENGTH / 4) * sizeof(float);
898 stored = lp_scene_alloc_aligned(scene, size, LP_MAX_VECTOR_LENGTH);
899
900 if (!stored) {
901 assert(!new_scene);
902 return FALSE;
903 }
904
905 /* Store floating point colour */
906 fstored = (float*)(stored + 4*16);
907 for (i = 0; i < (LP_MAX_VECTOR_LENGTH / 4); ++i) {
908 fstored[i] = setup->blend_color.current.color[i % 4];
909 }
910
911 /* smear each blend color component across 16 ubyte elements */
912 for (i = 0; i < 4; ++i) {
913 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
914 for (j = 0; j < 16; ++j)
915 stored[i*16 + j] = c;
916 }
917
918 setup->blend_color.stored = stored;
919 setup->fs.current.jit_context.u8_blend_color = stored;
920 setup->fs.current.jit_context.f_blend_color = fstored;
921 setup->dirty |= LP_SETUP_NEW_FS;
922 }
923
924 if (setup->dirty & LP_SETUP_NEW_CONSTANTS) {
925 for (i = 0; i < Elements(setup->constants); ++i) {
926 struct pipe_resource *buffer = setup->constants[i].current.buffer;
927 const unsigned current_size = setup->constants[i].current.buffer_size;
928 const ubyte *current_data = NULL;
929
930 if (buffer) {
931 /* resource buffer */
932 current_data = (ubyte *) llvmpipe_resource_data(buffer);
933 }
934 else if (setup->constants[i].current.user_buffer) {
935 /* user-space buffer */
936 current_data = (ubyte *) setup->constants[i].current.user_buffer;
937 }
938
939 if (current_data) {
940 current_data += setup->constants[i].current.buffer_offset;
941
942 /* TODO: copy only the actually used constants? */
943
944 if (setup->constants[i].stored_size != current_size ||
945 !setup->constants[i].stored_data ||
946 memcmp(setup->constants[i].stored_data,
947 current_data,
948 current_size) != 0) {
949 void *stored;
950
951 stored = lp_scene_alloc(scene, current_size);
952 if (!stored) {
953 assert(!new_scene);
954 return FALSE;
955 }
956
957 memcpy(stored,
958 current_data,
959 current_size);
960 setup->constants[i].stored_size = current_size;
961 setup->constants[i].stored_data = stored;
962 }
963 }
964 else {
965 setup->constants[i].stored_size = 0;
966 setup->constants[i].stored_data = NULL;
967 }
968
969 setup->fs.current.jit_context.constants[i] = setup->constants[i].stored_data;
970 setup->dirty |= LP_SETUP_NEW_FS;
971 }
972 }
973
974
975 if (setup->dirty & LP_SETUP_NEW_FS) {
976 if (!setup->fs.stored ||
977 memcmp(setup->fs.stored,
978 &setup->fs.current,
979 sizeof setup->fs.current) != 0)
980 {
981 struct lp_rast_state *stored;
982
983 /* The fs state that's been stored in the scene is different from
984 * the new, current state. So allocate a new lp_rast_state object
985 * and append it to the bin's setup data buffer.
986 */
987 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
988 if (!stored) {
989 assert(!new_scene);
990 return FALSE;
991 }
992
993 memcpy(stored,
994 &setup->fs.current,
995 sizeof setup->fs.current);
996 setup->fs.stored = stored;
997
998 /* The scene now references the textures in the rasterization
999 * state record. Note that now.
1000 */
1001 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1002 if (setup->fs.current_tex[i]) {
1003 if (!lp_scene_add_resource_reference(scene,
1004 setup->fs.current_tex[i],
1005 new_scene)) {
1006 assert(!new_scene);
1007 return FALSE;
1008 }
1009 }
1010 }
1011 }
1012 }
1013
1014 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
1015 setup->draw_region = setup->framebuffer;
1016 if (setup->scissor_test) {
1017 u_rect_possible_intersection(&setup->scissor,
1018 &setup->draw_region);
1019 }
1020 /* If the framebuffer is large we have to think about fixed-point
1021 * integer overflow. For 2K by 2K images, coordinates need 15 bits
1022 * (2^11 + 4 subpixel bits). The product of two such numbers would
1023 * use 30 bits. Any larger and we could overflow a 32-bit int.
1024 *
1025 * To cope with this problem we check if triangles are large and
1026 * subdivide them if needed.
1027 */
1028 setup->subdivide_large_triangles = (setup->fb.width > 2048 &&
1029 setup->fb.height > 2048);
1030 }
1031
1032 setup->dirty = 0;
1033
1034 assert(setup->fs.stored);
1035 return TRUE;
1036 }
1037
1038 boolean
1039 lp_setup_update_state( struct lp_setup_context *setup,
1040 boolean update_scene )
1041 {
1042 /* Some of the 'draw' pipeline stages may have changed some driver state.
1043 * Make sure we've processed those state changes before anything else.
1044 *
1045 * XXX this is the only place where llvmpipe_context is used in the
1046 * setup code. This may get refactored/changed...
1047 */
1048 {
1049 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
1050 if (lp->dirty) {
1051 llvmpipe_update_derived(lp);
1052 }
1053
1054 if (lp->setup->dirty) {
1055 llvmpipe_update_setup(lp);
1056 }
1057
1058 assert(setup->setup.variant);
1059
1060 /* Will probably need to move this somewhere else, just need
1061 * to know about vertex shader point size attribute.
1062 */
1063 setup->psize = lp->psize_slot;
1064
1065 assert(lp->dirty == 0);
1066
1067 assert(lp->setup_variant.key.size ==
1068 setup->setup.variant->key.size);
1069
1070 assert(memcmp(&lp->setup_variant.key,
1071 &setup->setup.variant->key,
1072 setup->setup.variant->key.size) == 0);
1073 }
1074
1075 if (update_scene && setup->state != SETUP_ACTIVE) {
1076 if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))
1077 return FALSE;
1078 }
1079
1080 /* Only call into update_scene_state() if we already have a
1081 * scene:
1082 */
1083 if (update_scene && setup->scene) {
1084 assert(setup->state == SETUP_ACTIVE);
1085
1086 if (try_update_scene_state(setup))
1087 return TRUE;
1088
1089 /* Update failed, try to restart the scene.
1090 *
1091 * Cannot call lp_setup_flush_and_restart() directly here
1092 * because of potential recursion.
1093 */
1094 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1095 return FALSE;
1096
1097 if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))
1098 return FALSE;
1099
1100 if (!setup->scene)
1101 return FALSE;
1102
1103 return try_update_scene_state(setup);
1104 }
1105
1106 return TRUE;
1107 }
1108
1109
1110
1111 /* Only caller is lp_setup_vbuf_destroy()
1112 */
1113 void
1114 lp_setup_destroy( struct lp_setup_context *setup )
1115 {
1116 uint i;
1117
1118 lp_setup_reset( setup );
1119
1120 util_unreference_framebuffer_state(&setup->fb);
1121
1122 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
1123 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
1124 }
1125
1126 for (i = 0; i < Elements(setup->constants); i++) {
1127 pipe_resource_reference(&setup->constants[i].current.buffer, NULL);
1128 }
1129
1130 /* free the scenes in the 'empty' queue */
1131 for (i = 0; i < Elements(setup->scenes); i++) {
1132 struct lp_scene *scene = setup->scenes[i];
1133
1134 if (scene->fence)
1135 lp_fence_wait(scene->fence);
1136
1137 lp_scene_destroy(scene);
1138 }
1139
1140 lp_fence_reference(&setup->last_fence, NULL);
1141
1142 FREE( setup );
1143 }
1144
1145
1146 /**
1147 * Create a new primitive tiling engine. Plug it into the backend of
1148 * the draw module. Currently also creates a rasterizer to use with
1149 * it.
1150 */
1151 struct lp_setup_context *
1152 lp_setup_create( struct pipe_context *pipe,
1153 struct draw_context *draw )
1154 {
1155 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
1156 struct lp_setup_context *setup;
1157 unsigned i;
1158
1159 setup = CALLOC_STRUCT(lp_setup_context);
1160 if (!setup) {
1161 goto no_setup;
1162 }
1163
1164 lp_setup_init_vbuf(setup);
1165
1166 /* Used only in update_state():
1167 */
1168 setup->pipe = pipe;
1169
1170
1171 setup->num_threads = screen->num_threads;
1172 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
1173 if (!setup->vbuf) {
1174 goto no_vbuf;
1175 }
1176
1177 draw_set_rasterize_stage(draw, setup->vbuf);
1178 draw_set_render(draw, &setup->base);
1179
1180 /* create some empty scenes */
1181 for (i = 0; i < MAX_SCENES; i++) {
1182 setup->scenes[i] = lp_scene_create( pipe );
1183 if (!setup->scenes[i]) {
1184 goto no_scenes;
1185 }
1186 }
1187
1188 setup->triangle = first_triangle;
1189 setup->line = first_line;
1190 setup->point = first_point;
1191
1192 setup->dirty = ~0;
1193
1194 return setup;
1195
1196 no_scenes:
1197 for (i = 0; i < MAX_SCENES; i++) {
1198 if (setup->scenes[i]) {
1199 lp_scene_destroy(setup->scenes[i]);
1200 }
1201 }
1202
1203 setup->vbuf->destroy(setup->vbuf);
1204 no_vbuf:
1205 FREE(setup);
1206 no_setup:
1207 return NULL;
1208 }
1209
1210
1211 /**
1212 * Put a BeginQuery command into all bins.
1213 */
1214 void
1215 lp_setup_begin_query(struct lp_setup_context *setup,
1216 struct llvmpipe_query *pq)
1217 {
1218 /* init the query to its beginning state */
1219 assert(setup->active_query[pq->type] == NULL);
1220
1221 set_scene_state(setup, SETUP_ACTIVE, "begin_query");
1222
1223 setup->active_query[pq->type] = pq;
1224
1225 /* XXX: It is possible that a query is created before the scene
1226 * has been created. This means that setup->scene == NULL resulting
1227 * in the query not being binned and thus is ignored.
1228 */
1229
1230 if (setup->scene) {
1231 if (!lp_scene_bin_everywhere(setup->scene,
1232 LP_RAST_OP_BEGIN_QUERY,
1233 lp_rast_arg_query(pq))) {
1234
1235 if (!lp_setup_flush_and_restart(setup))
1236 return;
1237
1238 if (!lp_scene_bin_everywhere(setup->scene,
1239 LP_RAST_OP_BEGIN_QUERY,
1240 lp_rast_arg_query(pq))) {
1241 return;
1242 }
1243 }
1244 }
1245 }
1246
1247
1248 /**
1249 * Put an EndQuery command into all bins.
1250 */
1251 void
1252 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1253 {
1254 set_scene_state(setup, SETUP_ACTIVE, "end_query");
1255
1256 if (pq->type != PIPE_QUERY_TIMESTAMP) {
1257 assert(setup->active_query[pq->type] == pq);
1258 setup->active_query[pq->type] = NULL;
1259 }
1260
1261 /* Setup will automatically re-issue any query which carried over a
1262 * scene boundary, and the rasterizer automatically "ends" queries
1263 * which are active at the end of a scene, so there is no need to
1264 * retry this commands on failure.
1265 */
1266 if (setup->scene) {
1267 /* pq->fence should be the fence of the *last* scene which
1268 * contributed to the query result.
1269 */
1270 lp_fence_reference(&pq->fence, setup->scene->fence);
1271
1272 if (!lp_scene_bin_everywhere(setup->scene,
1273 LP_RAST_OP_END_QUERY,
1274 lp_rast_arg_query(pq))) {
1275 lp_setup_flush(setup, NULL, __FUNCTION__);
1276 }
1277 }
1278 else {
1279 lp_fence_reference(&pq->fence, setup->last_fence);
1280 }
1281 }
1282
1283
1284 boolean
1285 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1286 {
1287 if (0) debug_printf("%s\n", __FUNCTION__);
1288
1289 assert(setup->state == SETUP_ACTIVE);
1290
1291 if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))
1292 return FALSE;
1293
1294 if (!lp_setup_update_state(setup, TRUE))
1295 return FALSE;
1296
1297 return TRUE;
1298 }
1299
1300