1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Builds per-tile display lists and executes them on calls to
37 #include "pipe/p_defines.h"
38 #include "util/u_framebuffer.h"
39 #include "util/u_inlines.h"
40 #include "util/u_memory.h"
41 #include "util/u_pack_color.h"
42 #include "draw/draw_pipe.h"
43 #include "os/os_time.h"
44 #include "lp_context.h"
45 #include "lp_memory.h"
47 #include "lp_texture.h"
52 #include "lp_setup_context.h"
53 #include "lp_screen.h"
55 #include "state_tracker/sw_winsys.h"
57 #include "draw/draw_context.h"
58 #include "draw/draw_vbuf.h"
61 static boolean
set_scene_state( struct lp_setup_context
*, enum setup_state
,
63 static boolean
try_update_scene_state( struct lp_setup_context
*setup
);
67 lp_setup_get_empty_scene(struct lp_setup_context
*setup
)
69 assert(setup
->scene
== NULL
);
72 setup
->scene_idx
%= Elements(setup
->scenes
);
74 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
76 if (setup
->scene
->fence
) {
77 if (LP_DEBUG
& DEBUG_SETUP
)
78 debug_printf("%s: wait for scene %d\n",
79 __FUNCTION__
, setup
->scene
->fence
->id
);
81 lp_fence_wait(setup
->scene
->fence
);
84 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, setup
->rasterizer_discard
);
90 first_triangle( struct lp_setup_context
*setup
,
95 assert(setup
->state
== SETUP_ACTIVE
);
96 lp_setup_choose_triangle( setup
);
97 setup
->triangle( setup
, v0
, v1
, v2
);
101 first_line( struct lp_setup_context
*setup
,
102 const float (*v0
)[4],
103 const float (*v1
)[4])
105 assert(setup
->state
== SETUP_ACTIVE
);
106 lp_setup_choose_line( setup
);
107 setup
->line( setup
, v0
, v1
);
111 first_point( struct lp_setup_context
*setup
,
112 const float (*v0
)[4])
114 assert(setup
->state
== SETUP_ACTIVE
);
115 lp_setup_choose_point( setup
);
116 setup
->point( setup
, v0
);
119 void lp_setup_reset( struct lp_setup_context
*setup
)
123 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
125 /* Reset derived state */
126 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
127 setup
->constants
[i
].stored_size
= 0;
128 setup
->constants
[i
].stored_data
= NULL
;
130 setup
->fs
.stored
= NULL
;
138 memset(&setup
->clear
, 0, sizeof setup
->clear
);
140 /* Have an explicit "start-binning" call and get rid of this
143 setup
->line
= first_line
;
144 setup
->point
= first_point
;
145 setup
->triangle
= first_triangle
;
149 /** Rasterize all scene's bins */
151 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
153 struct lp_scene
*scene
= setup
->scene
;
154 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
156 scene
->num_active_queries
= setup
->active_binned_queries
;
157 memcpy(scene
->active_queries
, setup
->active_queries
,
158 scene
->num_active_queries
* sizeof(scene
->active_queries
[0]));
160 lp_scene_end_binning(scene
);
162 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
164 if (setup
->last_fence
)
165 setup
->last_fence
->issued
= TRUE
;
167 pipe_mutex_lock(screen
->rast_mutex
);
168 lp_rast_queue_scene(screen
->rast
, scene
);
169 lp_rast_finish(screen
->rast
);
170 pipe_mutex_unlock(screen
->rast_mutex
);
172 lp_scene_end_rasterization(setup
->scene
);
173 lp_setup_reset( setup
);
175 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
181 begin_binning( struct lp_setup_context
*setup
)
183 struct lp_scene
*scene
= setup
->scene
;
184 boolean need_zsload
= FALSE
;
188 assert(scene
->fence
== NULL
);
190 /* Always create a fence:
192 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
196 ok
= try_update_scene_state(setup
);
200 if (setup
->fb
.zsbuf
&&
201 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
202 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
205 LP_DBG(DEBUG_SETUP
, "%s color clear bufs: %x depth: %s\n", __FUNCTION__
,
206 setup
->clear
.flags
>> 2,
207 need_zsload
? "clear": "load");
209 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
211 for (cbuf
= 0; cbuf
< setup
->fb
.nr_cbufs
; cbuf
++) {
212 assert(PIPE_CLEAR_COLOR0
== 1 << 2);
213 if (setup
->clear
.flags
& (1 << (2 + cbuf
))) {
214 union lp_rast_cmd_arg clearrb_arg
;
215 struct lp_rast_clear_rb
*cc_scene
=
216 (struct lp_rast_clear_rb
*)
217 lp_scene_alloc(scene
, sizeof(struct lp_rast_clear_rb
));
223 cc_scene
->cbuf
= cbuf
;
224 cc_scene
->color_val
= setup
->clear
.color_val
[cbuf
];
225 clearrb_arg
.clear_rb
= cc_scene
;
227 if (!lp_scene_bin_everywhere(scene
,
228 LP_RAST_OP_CLEAR_COLOR
,
235 if (setup
->fb
.zsbuf
) {
236 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
237 ok
= lp_scene_bin_everywhere( scene
,
238 LP_RAST_OP_CLEAR_ZSTENCIL
,
240 setup
->clear
.zsvalue
,
241 setup
->clear
.zsmask
));
247 setup
->clear
.flags
= 0;
248 setup
->clear
.zsmask
= 0;
249 setup
->clear
.zsvalue
= 0;
251 scene
->had_queries
= !!setup
->active_binned_queries
;
253 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
258 /* This basically bins and then flushes any outstanding full-screen
261 * TODO: fast path for fullscreen clears and no triangles.
264 execute_clears( struct lp_setup_context
*setup
)
266 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
268 return begin_binning( setup
);
271 const char *states
[] = {
279 set_scene_state( struct lp_setup_context
*setup
,
280 enum setup_state new_state
,
283 unsigned old_state
= setup
->state
;
285 if (old_state
== new_state
)
288 if (LP_DEBUG
& DEBUG_SCENE
) {
289 debug_printf("%s old %s new %s%s%s\n",
293 (new_state
== SETUP_FLUSHED
) ? ": " : "",
294 (new_state
== SETUP_FLUSHED
) ? reason
: "");
296 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
297 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
300 /* wait for a free/empty scene
302 if (old_state
== SETUP_FLUSHED
)
303 lp_setup_get_empty_scene(setup
);
310 if (!begin_binning( setup
))
315 if (old_state
== SETUP_CLEARED
)
316 if (!execute_clears( setup
))
319 lp_setup_rasterize_scene( setup
);
320 assert(setup
->scene
== NULL
);
324 assert(0 && "invalid setup state mode");
328 setup
->state
= new_state
;
333 lp_scene_end_rasterization(setup
->scene
);
337 setup
->state
= SETUP_FLUSHED
;
338 lp_setup_reset( setup
);
344 lp_setup_flush( struct lp_setup_context
*setup
,
345 struct pipe_fence_handle
**fence
,
348 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
351 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
357 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
358 const struct pipe_framebuffer_state
*fb
)
360 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
362 /* Flush any old scene.
364 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
367 * Ensure the old scene is not reused.
369 assert(!setup
->scene
);
371 /* Set new state. This will be picked up later when we next need a
374 util_copy_framebuffer_state(&setup
->fb
, fb
);
375 setup
->framebuffer
.x0
= 0;
376 setup
->framebuffer
.y0
= 0;
377 setup
->framebuffer
.x1
= fb
->width
-1;
378 setup
->framebuffer
.y1
= fb
->height
-1;
379 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
384 * Try to clear one color buffer of the attached fb, either by binning a clear
385 * command or queuing up the clear for later (when binning is started).
388 lp_setup_try_clear_color_buffer(struct lp_setup_context
*setup
,
389 const union pipe_color_union
*color
,
392 union lp_rast_cmd_arg clearrb_arg
;
394 enum pipe_format format
= setup
->fb
.cbufs
[cbuf
]->format
;
396 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
398 if (util_format_is_pure_integer(format
)) {
400 * We expect int/uint clear values here, though some APIs
401 * might disagree (but in any case util_pack_color()
402 * couldn't handle it)...
404 if (util_format_is_pure_sint(format
)) {
405 util_format_write_4i(format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
408 assert(util_format_is_pure_uint(format
));
409 util_format_write_4ui(format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
413 util_pack_color(color
->f
, format
, &uc
);
416 if (setup
->state
== SETUP_ACTIVE
) {
417 struct lp_scene
*scene
= setup
->scene
;
419 /* Add the clear to existing scene. In the unusual case where
420 * both color and depth-stencil are being cleared when there's
421 * already been some rendering, we could discard the currently
422 * binned scene and start again, but I don't see that as being
425 struct lp_rast_clear_rb
*cc_scene
=
426 (struct lp_rast_clear_rb
*)
427 lp_scene_alloc_aligned(scene
, sizeof(struct lp_rast_clear_rb
), 8);
433 cc_scene
->cbuf
= cbuf
;
434 cc_scene
->color_val
= uc
;
435 clearrb_arg
.clear_rb
= cc_scene
;
437 if (!lp_scene_bin_everywhere(scene
,
438 LP_RAST_OP_CLEAR_COLOR
,
443 /* Put ourselves into the 'pre-clear' state, specifically to try
444 * and accumulate multiple clears to color and depth_stencil
445 * buffers which the app or state-tracker might issue
448 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
450 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
451 setup
->clear
.flags
|= 1 << (cbuf
+ 2);
452 setup
->clear
.color_val
[cbuf
] = uc
;
459 lp_setup_try_clear_zs(struct lp_setup_context
*setup
,
465 uint64_t zsvalue
= 0;
469 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
471 zmask32
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
472 smask8
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
474 zsvalue
= util_pack64_z_stencil(setup
->fb
.zsbuf
->format
,
478 zsmask
= util_pack64_mask_z_stencil(setup
->fb
.zsbuf
->format
,
484 if (setup
->state
== SETUP_ACTIVE
) {
485 struct lp_scene
*scene
= setup
->scene
;
487 /* Add the clear to existing scene. In the unusual case where
488 * both color and depth-stencil are being cleared when there's
489 * already been some rendering, we could discard the currently
490 * binned scene and start again, but I don't see that as being
493 if (!lp_scene_bin_everywhere(scene
,
494 LP_RAST_OP_CLEAR_ZSTENCIL
,
495 lp_rast_arg_clearzs(zsvalue
, zsmask
)))
499 /* Put ourselves into the 'pre-clear' state, specifically to try
500 * and accumulate multiple clears to color and depth_stencil
501 * buffers which the app or state-tracker might issue
504 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
506 setup
->clear
.flags
|= flags
;
508 setup
->clear
.zsmask
|= zsmask
;
509 setup
->clear
.zsvalue
=
510 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
517 lp_setup_clear( struct lp_setup_context
*setup
,
518 const union pipe_color_union
*color
,
526 * Note any of these (max 9) clears could fail (but at most there should
527 * be just one failure!). This avoids doing the previous succeeded
528 * clears again (we still clear tiles twice if a clear command succeeded
529 * partially for one buffer).
531 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
532 unsigned flagszs
= flags
& PIPE_CLEAR_DEPTHSTENCIL
;
533 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
)) {
534 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
536 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
))
541 if (flags
& PIPE_CLEAR_COLOR
) {
542 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
543 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
544 if ((flags
& (1 << (2 + i
))) && setup
->fb
.cbufs
[i
]) {
545 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
)) {
546 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
548 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
))
559 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
561 boolean ccw_is_frontface
,
563 boolean half_pixel_center
,
564 boolean bottom_edge_rule
)
566 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
568 setup
->ccw_is_frontface
= ccw_is_frontface
;
569 setup
->cullmode
= cull_mode
;
570 setup
->triangle
= first_triangle
;
571 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
572 setup
->bottom_edge_rule
= bottom_edge_rule
;
574 if (setup
->scissor_test
!= scissor
) {
575 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
576 setup
->scissor_test
= scissor
;
581 lp_setup_set_line_state( struct lp_setup_context
*setup
,
584 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
586 setup
->line_width
= line_width
;
590 lp_setup_set_point_state( struct lp_setup_context
*setup
,
592 boolean point_size_per_vertex
,
593 uint sprite_coord_enable
,
594 uint sprite_coord_origin
)
596 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
598 setup
->point_size
= point_size
;
599 setup
->sprite_coord_enable
= sprite_coord_enable
;
600 setup
->sprite_coord_origin
= sprite_coord_origin
;
601 setup
->point_size_per_vertex
= point_size_per_vertex
;
605 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
606 const struct lp_setup_variant
*variant
)
608 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
610 setup
->setup
.variant
= variant
;
614 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
615 struct lp_fragment_shader_variant
*variant
)
617 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
619 /* FIXME: reference count */
621 setup
->fs
.current
.variant
= variant
;
622 setup
->dirty
|= LP_SETUP_NEW_FS
;
626 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
628 struct pipe_constant_buffer
*buffers
)
632 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
634 assert(num
<= Elements(setup
->constants
));
636 for (i
= 0; i
< num
; ++i
) {
637 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
639 for (; i
< Elements(setup
->constants
); i
++) {
640 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
642 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
647 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
648 float alpha_ref_value
)
650 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
652 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
653 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
654 setup
->dirty
|= LP_SETUP_NEW_FS
;
659 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
660 const ubyte refs
[2] )
662 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
664 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
665 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
666 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
667 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
668 setup
->dirty
|= LP_SETUP_NEW_FS
;
673 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
674 const struct pipe_blend_color
*blend_color
)
676 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
680 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
681 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
682 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
688 lp_setup_set_scissors( struct lp_setup_context
*setup
,
689 const struct pipe_scissor_state
*scissors
)
692 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
696 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
697 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
698 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
699 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
700 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
702 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
707 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
708 boolean flatshade_first
)
710 setup
->flatshade_first
= flatshade_first
;
714 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
715 boolean rasterizer_discard
)
717 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
718 setup
->rasterizer_discard
= rasterizer_discard
;
719 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
724 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
725 struct vertex_info
*vertex_info
)
727 /* XXX: just silently holding onto the pointer:
729 setup
->vertex_info
= vertex_info
;
734 * Called during state validation when LP_NEW_VIEWPORT is set.
737 lp_setup_set_viewports(struct lp_setup_context
*setup
,
738 unsigned num_viewports
,
739 const struct pipe_viewport_state
*viewports
)
741 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
744 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
746 assert(num_viewports
<= PIPE_MAX_VIEWPORTS
);
750 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
752 for (i
= 0; i
< num_viewports
; i
++) {
756 if (lp
->rasterizer
->clip_halfz
== 0) {
757 float half_depth
= viewports
[i
].scale
[2];
758 min_depth
= viewports
[i
].translate
[2] - half_depth
;
759 max_depth
= min_depth
+ half_depth
* 2.0f
;
761 min_depth
= viewports
[i
].translate
[2];
762 max_depth
= min_depth
+ viewports
[i
].scale
[2];
765 if (setup
->viewports
[i
].min_depth
!= min_depth
||
766 setup
->viewports
[i
].max_depth
!= max_depth
) {
767 setup
->viewports
[i
].min_depth
= min_depth
;
768 setup
->viewports
[i
].max_depth
= max_depth
;
769 setup
->dirty
|= LP_SETUP_NEW_VIEWPORTS
;
776 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
779 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
781 struct pipe_sampler_view
**views
)
785 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
787 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
789 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
790 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
793 struct pipe_resource
*res
= view
->texture
;
794 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
795 struct lp_jit_texture
*jit_tex
;
796 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
798 /* We're referencing the texture's internal data, so save a
801 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
804 /* regular texture - setup array of mipmap level offsets */
807 unsigned first_level
= 0;
808 unsigned last_level
= 0;
810 if (llvmpipe_resource_is_texture(res
)) {
811 first_level
= view
->u
.tex
.first_level
;
812 last_level
= view
->u
.tex
.last_level
;
813 assert(first_level
<= last_level
);
814 assert(last_level
<= res
->last_level
);
817 * The complexity here should no longer be necessary.
819 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, first_level
,
821 jit_tex
->base
= lp_tex
->tex_data
;
824 mip_ptr
= lp_tex
->data
;
825 jit_tex
->base
= mip_ptr
;
828 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
829 /* out of memory - use dummy tile memory */
830 /* Note if using PERF_TEX_MEM will also skip tile conversion */
831 jit_tex
->base
= lp_dummy_tile
;
832 jit_tex
->width
= TILE_SIZE
/8;
833 jit_tex
->height
= TILE_SIZE
/8;
835 jit_tex
->first_level
= 0;
836 jit_tex
->last_level
= 0;
837 jit_tex
->mip_offsets
[0] = 0;
838 jit_tex
->row_stride
[0] = 0;
839 jit_tex
->img_stride
[0] = 0;
842 jit_tex
->width
= res
->width0
;
843 jit_tex
->height
= res
->height0
;
844 jit_tex
->depth
= res
->depth0
;
845 jit_tex
->first_level
= first_level
;
846 jit_tex
->last_level
= last_level
;
848 if (llvmpipe_resource_is_texture(res
)) {
849 for (j
= first_level
; j
<= last_level
; j
++) {
850 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
852 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
854 * could get mip offset directly but need call above to
855 * invoke tiled->linear conversion.
857 assert(lp_tex
->mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
858 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
859 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
862 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
863 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
865 * For array textures, we don't have first_layer, instead
866 * adjust last_layer (stored as depth) plus the mip level offsets
867 * (as we have mip-first layout can't just adjust base ptr).
868 * XXX For mip levels, could do something similar.
870 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
871 for (j
= first_level
; j
<= last_level
; j
++) {
872 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
873 lp_tex
->img_stride
[j
];
875 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
876 assert(view
->u
.tex
.last_layer
< res
->array_size
);
881 * For buffers, we don't have first_element, instead adjust
882 * last_element (stored as width) plus the base pointer.
884 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
885 /* probably don't really need to fill that out */
886 jit_tex
->mip_offsets
[0] = 0;
887 jit_tex
->row_stride
[0] = 0;
888 jit_tex
->row_stride
[0] = 0;
890 /* everything specified in number of elements here. */
891 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
892 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
894 /* XXX Unsure if we need to sanitize parameters? */
895 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
896 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
901 /* display target texture/surface */
903 * XXX: Where should this be unmapped?
905 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
906 struct sw_winsys
*winsys
= screen
->winsys
;
907 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
909 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
910 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
911 jit_tex
->mip_offsets
[0] = 0;
912 jit_tex
->width
= res
->width0
;
913 jit_tex
->height
= res
->height0
;
914 jit_tex
->depth
= res
->depth0
;
915 jit_tex
->first_level
= jit_tex
->last_level
= 0;
916 assert(jit_tex
->base
);
921 setup
->dirty
|= LP_SETUP_NEW_FS
;
926 * Called during state validation when LP_NEW_SAMPLER is set.
929 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
931 struct pipe_sampler_state
**samplers
)
935 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
937 assert(num
<= PIPE_MAX_SAMPLERS
);
939 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
940 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
943 struct lp_jit_sampler
*jit_sam
;
944 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
946 jit_sam
->min_lod
= sampler
->min_lod
;
947 jit_sam
->max_lod
= sampler
->max_lod
;
948 jit_sam
->lod_bias
= sampler
->lod_bias
;
949 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
953 setup
->dirty
|= LP_SETUP_NEW_FS
;
958 * Is the given texture referenced by any scene?
959 * Note: we have to check all scenes including any scenes currently
960 * being rendered and the current scene being built.
963 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
964 const struct pipe_resource
*texture
)
968 /* check the render targets */
969 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
970 if (setup
->fb
.cbufs
[i
] && setup
->fb
.cbufs
[i
]->texture
== texture
)
971 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
973 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
974 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
977 /* check textures referenced by the scene */
978 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
979 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
980 return LP_REFERENCED_FOR_READ
;
984 return LP_UNREFERENCED
;
989 * Called by vbuf code when we're about to draw something.
991 * This function stores all dirty state in the current scene's display list
992 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
993 * the JIT functions, as the JIT functions will be called later on, most likely
994 * on a different thread.
996 * When processing dirty state it is imperative that we don't refer to any
997 * pointers previously allocated with lp_scene_alloc() in this function (or any
998 * function) as they may belong to a scene freed since then.
1001 try_update_scene_state( struct lp_setup_context
*setup
)
1003 boolean new_scene
= (setup
->fs
.stored
== NULL
);
1004 struct lp_scene
*scene
= setup
->scene
;
1009 if (setup
->dirty
& LP_SETUP_NEW_VIEWPORTS
) {
1011 * Record new depth range state for changes due to viewport updates.
1013 * TODO: Collapse the existing viewport and depth range information
1014 * into one structure, for access by JIT.
1016 struct lp_jit_viewport
*stored
;
1018 stored
= (struct lp_jit_viewport
*)
1019 lp_scene_alloc(scene
, sizeof setup
->viewports
);
1026 memcpy(stored
, setup
->viewports
, sizeof setup
->viewports
);
1028 setup
->fs
.current
.jit_context
.viewports
= stored
;
1029 setup
->dirty
|= LP_SETUP_NEW_FS
;
1032 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
1038 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
1039 size
= 4 * 16 * sizeof(uint8_t);
1040 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
1041 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MIN_VECTOR_ALIGN
);
1048 /* Store floating point colour */
1049 fstored
= (float*)(stored
+ 4*16);
1050 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
1051 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
1054 /* smear each blend color component across 16 ubyte elements */
1055 for (i
= 0; i
< 4; ++i
) {
1056 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
1057 for (j
= 0; j
< 16; ++j
)
1058 stored
[i
*16 + j
] = c
;
1061 setup
->blend_color
.stored
= stored
;
1062 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
1063 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
1064 setup
->dirty
|= LP_SETUP_NEW_FS
;
1067 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
1068 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
1069 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
1070 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
1071 const ubyte
*current_data
= NULL
;
1075 /* resource buffer */
1076 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1078 else if (setup
->constants
[i
].current
.user_buffer
) {
1079 /* user-space buffer */
1080 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
1084 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
1086 /* TODO: copy only the actually used constants? */
1088 if (setup
->constants
[i
].stored_size
!= current_size
||
1089 !setup
->constants
[i
].stored_data
||
1090 memcmp(setup
->constants
[i
].stored_data
,
1092 current_size
) != 0) {
1095 stored
= lp_scene_alloc(scene
, current_size
);
1104 setup
->constants
[i
].stored_size
= current_size
;
1105 setup
->constants
[i
].stored_data
= stored
;
1109 setup
->constants
[i
].stored_size
= 0;
1110 setup
->constants
[i
].stored_data
= NULL
;
1113 setup
->fs
.current
.jit_context
.constants
[i
] =
1114 setup
->constants
[i
].stored_data
;
1116 setup
->constants
[i
].stored_size
/ (sizeof(float) * 4);
1117 setup
->fs
.current
.jit_context
.num_constants
[i
] = num_constants
;
1118 setup
->dirty
|= LP_SETUP_NEW_FS
;
1123 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
1124 if (!setup
->fs
.stored
||
1125 memcmp(setup
->fs
.stored
,
1127 sizeof setup
->fs
.current
) != 0)
1129 struct lp_rast_state
*stored
;
1131 /* The fs state that's been stored in the scene is different from
1132 * the new, current state. So allocate a new lp_rast_state object
1133 * and append it to the bin's setup data buffer.
1135 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
1143 sizeof setup
->fs
.current
);
1144 setup
->fs
.stored
= stored
;
1146 /* The scene now references the textures in the rasterization
1147 * state record. Note that now.
1149 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1150 if (setup
->fs
.current_tex
[i
]) {
1151 if (!lp_scene_add_resource_reference(scene
,
1152 setup
->fs
.current_tex
[i
],
1162 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1164 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1165 setup
->draw_regions
[i
] = setup
->framebuffer
;
1166 if (setup
->scissor_test
) {
1167 u_rect_possible_intersection(&setup
->scissors
[i
],
1168 &setup
->draw_regions
[i
]);
1175 assert(setup
->fs
.stored
);
1180 lp_setup_update_state( struct lp_setup_context
*setup
,
1181 boolean update_scene
)
1183 /* Some of the 'draw' pipeline stages may have changed some driver state.
1184 * Make sure we've processed those state changes before anything else.
1186 * XXX this is the only place where llvmpipe_context is used in the
1187 * setup code. This may get refactored/changed...
1190 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1192 llvmpipe_update_derived(lp
);
1195 if (lp
->setup
->dirty
) {
1196 llvmpipe_update_setup(lp
);
1199 assert(setup
->setup
.variant
);
1201 /* Will probably need to move this somewhere else, just need
1202 * to know about vertex shader point size attribute.
1204 setup
->psize
= lp
->psize_slot
;
1205 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1206 setup
->layer_slot
= lp
->layer_slot
;
1207 setup
->face_slot
= lp
->face_slot
;
1209 assert(lp
->dirty
== 0);
1211 assert(lp
->setup_variant
.key
.size
==
1212 setup
->setup
.variant
->key
.size
);
1214 assert(memcmp(&lp
->setup_variant
.key
,
1215 &setup
->setup
.variant
->key
,
1216 setup
->setup
.variant
->key
.size
) == 0);
1219 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1220 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1224 /* Only call into update_scene_state() if we already have a
1227 if (update_scene
&& setup
->scene
) {
1228 assert(setup
->state
== SETUP_ACTIVE
);
1230 if (try_update_scene_state(setup
))
1233 /* Update failed, try to restart the scene.
1235 * Cannot call lp_setup_flush_and_restart() directly here
1236 * because of potential recursion.
1238 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1241 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1247 return try_update_scene_state(setup
);
1255 /* Only caller is lp_setup_vbuf_destroy()
1258 lp_setup_destroy( struct lp_setup_context
*setup
)
1262 lp_setup_reset( setup
);
1264 util_unreference_framebuffer_state(&setup
->fb
);
1266 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1267 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1270 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1271 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1274 /* free the scenes in the 'empty' queue */
1275 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1276 struct lp_scene
*scene
= setup
->scenes
[i
];
1279 lp_fence_wait(scene
->fence
);
1281 lp_scene_destroy(scene
);
1284 lp_fence_reference(&setup
->last_fence
, NULL
);
1291 * Create a new primitive tiling engine. Plug it into the backend of
1292 * the draw module. Currently also creates a rasterizer to use with
1295 struct lp_setup_context
*
1296 lp_setup_create( struct pipe_context
*pipe
,
1297 struct draw_context
*draw
)
1299 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1300 struct lp_setup_context
*setup
;
1303 setup
= CALLOC_STRUCT(lp_setup_context
);
1308 lp_setup_init_vbuf(setup
);
1310 /* Used only in update_state():
1315 setup
->num_threads
= screen
->num_threads
;
1316 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1321 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1322 draw_set_render(draw
, &setup
->base
);
1324 /* create some empty scenes */
1325 for (i
= 0; i
< MAX_SCENES
; i
++) {
1326 setup
->scenes
[i
] = lp_scene_create( pipe
);
1327 if (!setup
->scenes
[i
]) {
1332 setup
->triangle
= first_triangle
;
1333 setup
->line
= first_line
;
1334 setup
->point
= first_point
;
1341 for (i
= 0; i
< MAX_SCENES
; i
++) {
1342 if (setup
->scenes
[i
]) {
1343 lp_scene_destroy(setup
->scenes
[i
]);
1347 setup
->vbuf
->destroy(setup
->vbuf
);
1356 * Put a BeginQuery command into all bins.
1359 lp_setup_begin_query(struct lp_setup_context
*setup
,
1360 struct llvmpipe_query
*pq
)
1363 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1365 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1366 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1367 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1370 /* init the query to its beginning state */
1371 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1372 /* exceeding list size so just ignore the query */
1373 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1376 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1377 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1378 setup
->active_binned_queries
++;
1380 assert(setup
->scene
);
1382 if (!lp_scene_bin_everywhere(setup
->scene
,
1383 LP_RAST_OP_BEGIN_QUERY
,
1384 lp_rast_arg_query(pq
))) {
1386 if (!lp_setup_flush_and_restart(setup
))
1389 if (!lp_scene_bin_everywhere(setup
->scene
,
1390 LP_RAST_OP_BEGIN_QUERY
,
1391 lp_rast_arg_query(pq
))) {
1395 setup
->scene
->had_queries
|= TRUE
;
1401 * Put an EndQuery command into all bins.
1404 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1406 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1408 assert(setup
->scene
);
1410 /* pq->fence should be the fence of the *last* scene which
1411 * contributed to the query result.
1413 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1415 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1416 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1417 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1418 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1419 if (pq
->type
== PIPE_QUERY_TIMESTAMP
&&
1420 !(setup
->scene
->tiles_x
| setup
->scene
->tiles_y
)) {
1422 * If there's a zero width/height framebuffer, there's no bins and
1423 * hence no rast task is ever run. So fill in something here instead.
1425 pq
->end
[0] = os_time_get_nano();
1428 if (!lp_scene_bin_everywhere(setup
->scene
,
1429 LP_RAST_OP_END_QUERY
,
1430 lp_rast_arg_query(pq
))) {
1431 if (!lp_setup_flush_and_restart(setup
))
1434 if (!lp_scene_bin_everywhere(setup
->scene
,
1435 LP_RAST_OP_END_QUERY
,
1436 lp_rast_arg_query(pq
))) {
1440 setup
->scene
->had_queries
|= TRUE
;
1444 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1448 /* Need to do this now not earlier since it still needs to be marked as
1449 * active when binning it would cause a flush.
1451 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1452 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1453 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1456 /* remove from active binned query list */
1457 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1458 if (setup
->active_queries
[i
] == pq
)
1461 assert(i
< setup
->active_binned_queries
);
1462 if (i
== setup
->active_binned_queries
)
1464 setup
->active_binned_queries
--;
1465 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1466 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1472 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1474 if (0) debug_printf("%s\n", __FUNCTION__
);
1476 assert(setup
->state
== SETUP_ACTIVE
);
1478 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1481 if (!lp_setup_update_state(setup
, TRUE
))