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 */
806 unsigned first_level
= 0;
807 unsigned last_level
= 0;
809 if (llvmpipe_resource_is_texture(res
)) {
810 first_level
= view
->u
.tex
.first_level
;
811 last_level
= view
->u
.tex
.last_level
;
812 assert(first_level
<= last_level
);
813 assert(last_level
<= res
->last_level
);
814 jit_tex
->base
= lp_tex
->tex_data
;
817 jit_tex
->base
= lp_tex
->data
;
820 if (LP_PERF
& PERF_TEX_MEM
) {
821 /* use dummy tile memory */
822 jit_tex
->base
= lp_dummy_tile
;
823 jit_tex
->width
= TILE_SIZE
/8;
824 jit_tex
->height
= TILE_SIZE
/8;
826 jit_tex
->first_level
= 0;
827 jit_tex
->last_level
= 0;
828 jit_tex
->mip_offsets
[0] = 0;
829 jit_tex
->row_stride
[0] = 0;
830 jit_tex
->img_stride
[0] = 0;
833 jit_tex
->width
= res
->width0
;
834 jit_tex
->height
= res
->height0
;
835 jit_tex
->depth
= res
->depth0
;
836 jit_tex
->first_level
= first_level
;
837 jit_tex
->last_level
= last_level
;
839 if (llvmpipe_resource_is_texture(res
)) {
840 for (j
= first_level
; j
<= last_level
; j
++) {
841 jit_tex
->mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
842 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
843 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
846 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
847 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
848 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
850 * For array textures, we don't have first_layer, instead
851 * adjust last_layer (stored as depth) plus the mip level offsets
852 * (as we have mip-first layout can't just adjust base ptr).
853 * XXX For mip levels, could do something similar.
855 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
856 for (j
= first_level
; j
<= last_level
; j
++) {
857 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
858 lp_tex
->img_stride
[j
];
860 if (res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
861 assert(jit_tex
->depth
% 6 == 0);
863 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
864 assert(view
->u
.tex
.last_layer
< res
->array_size
);
869 * For buffers, we don't have first_element, instead adjust
870 * last_element (stored as width) plus the base pointer.
872 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
873 /* probably don't really need to fill that out */
874 jit_tex
->mip_offsets
[0] = 0;
875 jit_tex
->row_stride
[0] = 0;
876 jit_tex
->img_stride
[0] = 0;
878 /* everything specified in number of elements here. */
879 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
880 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
882 /* XXX Unsure if we need to sanitize parameters? */
883 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
884 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
889 /* display target texture/surface */
891 * XXX: Where should this be unmapped?
893 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
894 struct sw_winsys
*winsys
= screen
->winsys
;
895 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
897 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
898 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
899 jit_tex
->mip_offsets
[0] = 0;
900 jit_tex
->width
= res
->width0
;
901 jit_tex
->height
= res
->height0
;
902 jit_tex
->depth
= res
->depth0
;
903 jit_tex
->first_level
= jit_tex
->last_level
= 0;
904 assert(jit_tex
->base
);
909 setup
->dirty
|= LP_SETUP_NEW_FS
;
914 * Called during state validation when LP_NEW_SAMPLER is set.
917 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
919 struct pipe_sampler_state
**samplers
)
923 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
925 assert(num
<= PIPE_MAX_SAMPLERS
);
927 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
928 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
931 struct lp_jit_sampler
*jit_sam
;
932 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
934 jit_sam
->min_lod
= sampler
->min_lod
;
935 jit_sam
->max_lod
= sampler
->max_lod
;
936 jit_sam
->lod_bias
= sampler
->lod_bias
;
937 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
941 setup
->dirty
|= LP_SETUP_NEW_FS
;
946 * Is the given texture referenced by any scene?
947 * Note: we have to check all scenes including any scenes currently
948 * being rendered and the current scene being built.
951 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
952 const struct pipe_resource
*texture
)
956 /* check the render targets */
957 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
958 if (setup
->fb
.cbufs
[i
] && setup
->fb
.cbufs
[i
]->texture
== texture
)
959 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
961 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
962 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
965 /* check textures referenced by the scene */
966 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
967 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
968 return LP_REFERENCED_FOR_READ
;
972 return LP_UNREFERENCED
;
977 * Called by vbuf code when we're about to draw something.
979 * This function stores all dirty state in the current scene's display list
980 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
981 * the JIT functions, as the JIT functions will be called later on, most likely
982 * on a different thread.
984 * When processing dirty state it is imperative that we don't refer to any
985 * pointers previously allocated with lp_scene_alloc() in this function (or any
986 * function) as they may belong to a scene freed since then.
989 try_update_scene_state( struct lp_setup_context
*setup
)
991 boolean new_scene
= (setup
->fs
.stored
== NULL
);
992 struct lp_scene
*scene
= setup
->scene
;
997 if (setup
->dirty
& LP_SETUP_NEW_VIEWPORTS
) {
999 * Record new depth range state for changes due to viewport updates.
1001 * TODO: Collapse the existing viewport and depth range information
1002 * into one structure, for access by JIT.
1004 struct lp_jit_viewport
*stored
;
1006 stored
= (struct lp_jit_viewport
*)
1007 lp_scene_alloc(scene
, sizeof setup
->viewports
);
1014 memcpy(stored
, setup
->viewports
, sizeof setup
->viewports
);
1016 setup
->fs
.current
.jit_context
.viewports
= stored
;
1017 setup
->dirty
|= LP_SETUP_NEW_FS
;
1020 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
1026 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
1027 size
= 4 * 16 * sizeof(uint8_t);
1028 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
1029 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MIN_VECTOR_ALIGN
);
1036 /* Store floating point colour */
1037 fstored
= (float*)(stored
+ 4*16);
1038 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
1039 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
1042 /* smear each blend color component across 16 ubyte elements */
1043 for (i
= 0; i
< 4; ++i
) {
1044 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
1045 for (j
= 0; j
< 16; ++j
)
1046 stored
[i
*16 + j
] = c
;
1049 setup
->blend_color
.stored
= stored
;
1050 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
1051 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
1052 setup
->dirty
|= LP_SETUP_NEW_FS
;
1055 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
1056 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
1057 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
1058 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
1059 const ubyte
*current_data
= NULL
;
1063 /* resource buffer */
1064 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1066 else if (setup
->constants
[i
].current
.user_buffer
) {
1067 /* user-space buffer */
1068 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
1072 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
1074 /* TODO: copy only the actually used constants? */
1076 if (setup
->constants
[i
].stored_size
!= current_size
||
1077 !setup
->constants
[i
].stored_data
||
1078 memcmp(setup
->constants
[i
].stored_data
,
1080 current_size
) != 0) {
1083 stored
= lp_scene_alloc(scene
, current_size
);
1092 setup
->constants
[i
].stored_size
= current_size
;
1093 setup
->constants
[i
].stored_data
= stored
;
1097 setup
->constants
[i
].stored_size
= 0;
1098 setup
->constants
[i
].stored_data
= NULL
;
1101 setup
->fs
.current
.jit_context
.constants
[i
] =
1102 setup
->constants
[i
].stored_data
;
1104 setup
->constants
[i
].stored_size
/ (sizeof(float) * 4);
1105 setup
->fs
.current
.jit_context
.num_constants
[i
] = num_constants
;
1106 setup
->dirty
|= LP_SETUP_NEW_FS
;
1111 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
1112 if (!setup
->fs
.stored
||
1113 memcmp(setup
->fs
.stored
,
1115 sizeof setup
->fs
.current
) != 0)
1117 struct lp_rast_state
*stored
;
1119 /* The fs state that's been stored in the scene is different from
1120 * the new, current state. So allocate a new lp_rast_state object
1121 * and append it to the bin's setup data buffer.
1123 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
1131 sizeof setup
->fs
.current
);
1132 setup
->fs
.stored
= stored
;
1134 /* The scene now references the textures in the rasterization
1135 * state record. Note that now.
1137 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1138 if (setup
->fs
.current_tex
[i
]) {
1139 if (!lp_scene_add_resource_reference(scene
,
1140 setup
->fs
.current_tex
[i
],
1150 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1152 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1153 setup
->draw_regions
[i
] = setup
->framebuffer
;
1154 if (setup
->scissor_test
) {
1155 u_rect_possible_intersection(&setup
->scissors
[i
],
1156 &setup
->draw_regions
[i
]);
1163 assert(setup
->fs
.stored
);
1168 lp_setup_update_state( struct lp_setup_context
*setup
,
1169 boolean update_scene
)
1171 /* Some of the 'draw' pipeline stages may have changed some driver state.
1172 * Make sure we've processed those state changes before anything else.
1174 * XXX this is the only place where llvmpipe_context is used in the
1175 * setup code. This may get refactored/changed...
1178 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1180 llvmpipe_update_derived(lp
);
1183 if (lp
->setup
->dirty
) {
1184 llvmpipe_update_setup(lp
);
1187 assert(setup
->setup
.variant
);
1189 /* Will probably need to move this somewhere else, just need
1190 * to know about vertex shader point size attribute.
1192 setup
->psize
= lp
->psize_slot
;
1193 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1194 setup
->layer_slot
= lp
->layer_slot
;
1195 setup
->face_slot
= lp
->face_slot
;
1197 assert(lp
->dirty
== 0);
1199 assert(lp
->setup_variant
.key
.size
==
1200 setup
->setup
.variant
->key
.size
);
1202 assert(memcmp(&lp
->setup_variant
.key
,
1203 &setup
->setup
.variant
->key
,
1204 setup
->setup
.variant
->key
.size
) == 0);
1207 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1208 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1212 /* Only call into update_scene_state() if we already have a
1215 if (update_scene
&& setup
->scene
) {
1216 assert(setup
->state
== SETUP_ACTIVE
);
1218 if (try_update_scene_state(setup
))
1221 /* Update failed, try to restart the scene.
1223 * Cannot call lp_setup_flush_and_restart() directly here
1224 * because of potential recursion.
1226 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1229 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1235 return try_update_scene_state(setup
);
1243 /* Only caller is lp_setup_vbuf_destroy()
1246 lp_setup_destroy( struct lp_setup_context
*setup
)
1250 lp_setup_reset( setup
);
1252 util_unreference_framebuffer_state(&setup
->fb
);
1254 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1255 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1258 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1259 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1262 /* free the scenes in the 'empty' queue */
1263 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1264 struct lp_scene
*scene
= setup
->scenes
[i
];
1267 lp_fence_wait(scene
->fence
);
1269 lp_scene_destroy(scene
);
1272 lp_fence_reference(&setup
->last_fence
, NULL
);
1279 * Create a new primitive tiling engine. Plug it into the backend of
1280 * the draw module. Currently also creates a rasterizer to use with
1283 struct lp_setup_context
*
1284 lp_setup_create( struct pipe_context
*pipe
,
1285 struct draw_context
*draw
)
1287 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1288 struct lp_setup_context
*setup
;
1291 setup
= CALLOC_STRUCT(lp_setup_context
);
1296 lp_setup_init_vbuf(setup
);
1298 /* Used only in update_state():
1303 setup
->num_threads
= screen
->num_threads
;
1304 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1309 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1310 draw_set_render(draw
, &setup
->base
);
1312 /* create some empty scenes */
1313 for (i
= 0; i
< MAX_SCENES
; i
++) {
1314 setup
->scenes
[i
] = lp_scene_create( pipe
);
1315 if (!setup
->scenes
[i
]) {
1320 setup
->triangle
= first_triangle
;
1321 setup
->line
= first_line
;
1322 setup
->point
= first_point
;
1329 for (i
= 0; i
< MAX_SCENES
; i
++) {
1330 if (setup
->scenes
[i
]) {
1331 lp_scene_destroy(setup
->scenes
[i
]);
1335 setup
->vbuf
->destroy(setup
->vbuf
);
1344 * Put a BeginQuery command into all bins.
1347 lp_setup_begin_query(struct lp_setup_context
*setup
,
1348 struct llvmpipe_query
*pq
)
1351 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1353 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1354 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1355 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1358 /* init the query to its beginning state */
1359 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1360 /* exceeding list size so just ignore the query */
1361 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1364 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1365 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1366 setup
->active_binned_queries
++;
1368 assert(setup
->scene
);
1370 if (!lp_scene_bin_everywhere(setup
->scene
,
1371 LP_RAST_OP_BEGIN_QUERY
,
1372 lp_rast_arg_query(pq
))) {
1374 if (!lp_setup_flush_and_restart(setup
))
1377 if (!lp_scene_bin_everywhere(setup
->scene
,
1378 LP_RAST_OP_BEGIN_QUERY
,
1379 lp_rast_arg_query(pq
))) {
1383 setup
->scene
->had_queries
|= TRUE
;
1389 * Put an EndQuery command into all bins.
1392 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1394 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1396 assert(setup
->scene
);
1398 /* pq->fence should be the fence of the *last* scene which
1399 * contributed to the query result.
1401 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1403 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1404 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1405 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1406 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1407 if (pq
->type
== PIPE_QUERY_TIMESTAMP
&&
1408 !(setup
->scene
->tiles_x
| setup
->scene
->tiles_y
)) {
1410 * If there's a zero width/height framebuffer, there's no bins and
1411 * hence no rast task is ever run. So fill in something here instead.
1413 pq
->end
[0] = os_time_get_nano();
1416 if (!lp_scene_bin_everywhere(setup
->scene
,
1417 LP_RAST_OP_END_QUERY
,
1418 lp_rast_arg_query(pq
))) {
1419 if (!lp_setup_flush_and_restart(setup
))
1422 if (!lp_scene_bin_everywhere(setup
->scene
,
1423 LP_RAST_OP_END_QUERY
,
1424 lp_rast_arg_query(pq
))) {
1428 setup
->scene
->had_queries
|= TRUE
;
1432 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1436 /* Need to do this now not earlier since it still needs to be marked as
1437 * active when binning it would cause a flush.
1439 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1440 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1441 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1444 /* remove from active binned query list */
1445 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1446 if (setup
->active_queries
[i
] == pq
)
1449 assert(i
< setup
->active_binned_queries
);
1450 if (i
== setup
->active_binned_queries
)
1452 setup
->active_binned_queries
--;
1453 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1454 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1460 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1462 if (0) debug_printf("%s\n", __FUNCTION__
);
1464 assert(setup
->state
== SETUP_ACTIVE
);
1466 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1469 if (!lp_setup_update_state(setup
, TRUE
))