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 "util/u_viewport.h"
43 #include "draw/draw_pipe.h"
44 #include "os/os_time.h"
45 #include "lp_context.h"
46 #include "lp_memory.h"
48 #include "lp_texture.h"
53 #include "lp_setup_context.h"
54 #include "lp_screen.h"
56 #include "state_tracker/sw_winsys.h"
58 #include "draw/draw_context.h"
59 #include "draw/draw_vbuf.h"
62 static boolean
set_scene_state( struct lp_setup_context
*, enum setup_state
,
64 static boolean
try_update_scene_state( struct lp_setup_context
*setup
);
68 lp_setup_get_empty_scene(struct lp_setup_context
*setup
)
70 assert(setup
->scene
== NULL
);
73 setup
->scene_idx
%= ARRAY_SIZE(setup
->scenes
);
75 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
77 if (setup
->scene
->fence
) {
78 if (LP_DEBUG
& DEBUG_SETUP
)
79 debug_printf("%s: wait for scene %d\n",
80 __FUNCTION__
, setup
->scene
->fence
->id
);
82 lp_fence_wait(setup
->scene
->fence
);
85 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, setup
->rasterizer_discard
);
91 first_triangle( struct lp_setup_context
*setup
,
96 assert(setup
->state
== SETUP_ACTIVE
);
97 lp_setup_choose_triangle( setup
);
98 setup
->triangle( setup
, v0
, v1
, v2
);
102 first_line( struct lp_setup_context
*setup
,
103 const float (*v0
)[4],
104 const float (*v1
)[4])
106 assert(setup
->state
== SETUP_ACTIVE
);
107 lp_setup_choose_line( setup
);
108 setup
->line( setup
, v0
, v1
);
112 first_point( struct lp_setup_context
*setup
,
113 const float (*v0
)[4])
115 assert(setup
->state
== SETUP_ACTIVE
);
116 lp_setup_choose_point( setup
);
117 setup
->point( setup
, v0
);
120 void lp_setup_reset( struct lp_setup_context
*setup
)
124 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
126 /* Reset derived state */
127 for (i
= 0; i
< ARRAY_SIZE(setup
->constants
); ++i
) {
128 setup
->constants
[i
].stored_size
= 0;
129 setup
->constants
[i
].stored_data
= NULL
;
131 setup
->fs
.stored
= NULL
;
139 memset(&setup
->clear
, 0, sizeof setup
->clear
);
141 /* Have an explicit "start-binning" call and get rid of this
144 setup
->line
= first_line
;
145 setup
->point
= first_point
;
146 setup
->triangle
= first_triangle
;
150 /** Rasterize all scene's bins */
152 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
154 struct lp_scene
*scene
= setup
->scene
;
155 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
157 scene
->num_active_queries
= setup
->active_binned_queries
;
158 memcpy(scene
->active_queries
, setup
->active_queries
,
159 scene
->num_active_queries
* sizeof(scene
->active_queries
[0]));
161 lp_scene_end_binning(scene
);
163 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
165 if (setup
->last_fence
)
166 setup
->last_fence
->issued
= TRUE
;
168 mtx_lock(&screen
->rast_mutex
);
170 /* FIXME: We enqueue the scene then wait on the rasterizer to finish.
171 * This means we never actually run any vertex stuff in parallel to
172 * rasterization (not in the same context at least) which is what the
173 * multiple scenes per setup is about - when we get a new empty scene
174 * any old one is already empty again because we waited here for
175 * raster tasks to be finished. Ideally, we shouldn't need to wait here
176 * and rely on fences elsewhere when waiting is necessary.
177 * Certainly, lp_scene_end_rasterization() would need to be deferred too
178 * and there's probably other bits why this doesn't actually work.
180 lp_rast_queue_scene(screen
->rast
, scene
);
181 lp_rast_finish(screen
->rast
);
182 mtx_unlock(&screen
->rast_mutex
);
184 lp_scene_end_rasterization(setup
->scene
);
185 lp_setup_reset( setup
);
187 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
193 begin_binning( struct lp_setup_context
*setup
)
195 struct lp_scene
*scene
= setup
->scene
;
196 boolean need_zsload
= FALSE
;
200 assert(scene
->fence
== NULL
);
202 /* Always create a fence:
204 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
208 ok
= try_update_scene_state(setup
);
212 if (setup
->fb
.zsbuf
&&
213 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
214 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
217 LP_DBG(DEBUG_SETUP
, "%s color clear bufs: %x depth: %s\n", __FUNCTION__
,
218 setup
->clear
.flags
>> 2,
219 need_zsload
? "clear": "load");
221 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
223 for (cbuf
= 0; cbuf
< setup
->fb
.nr_cbufs
; cbuf
++) {
224 assert(PIPE_CLEAR_COLOR0
== 1 << 2);
225 if (setup
->clear
.flags
& (1 << (2 + cbuf
))) {
226 union lp_rast_cmd_arg clearrb_arg
;
227 struct lp_rast_clear_rb
*cc_scene
=
228 (struct lp_rast_clear_rb
*)
229 lp_scene_alloc(scene
, sizeof(struct lp_rast_clear_rb
));
235 cc_scene
->cbuf
= cbuf
;
236 cc_scene
->color_val
= setup
->clear
.color_val
[cbuf
];
237 clearrb_arg
.clear_rb
= cc_scene
;
239 if (!lp_scene_bin_everywhere(scene
,
240 LP_RAST_OP_CLEAR_COLOR
,
247 if (setup
->fb
.zsbuf
) {
248 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
249 ok
= lp_scene_bin_everywhere( scene
,
250 LP_RAST_OP_CLEAR_ZSTENCIL
,
252 setup
->clear
.zsvalue
,
253 setup
->clear
.zsmask
));
259 setup
->clear
.flags
= 0;
260 setup
->clear
.zsmask
= 0;
261 setup
->clear
.zsvalue
= 0;
263 scene
->had_queries
= !!setup
->active_binned_queries
;
265 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
270 /* This basically bins and then flushes any outstanding full-screen
273 * TODO: fast path for fullscreen clears and no triangles.
276 execute_clears( struct lp_setup_context
*setup
)
278 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
280 return begin_binning( setup
);
283 const char *states
[] = {
291 set_scene_state( struct lp_setup_context
*setup
,
292 enum setup_state new_state
,
295 unsigned old_state
= setup
->state
;
297 if (old_state
== new_state
)
300 if (LP_DEBUG
& DEBUG_SCENE
) {
301 debug_printf("%s old %s new %s%s%s\n",
305 (new_state
== SETUP_FLUSHED
) ? ": " : "",
306 (new_state
== SETUP_FLUSHED
) ? reason
: "");
308 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
309 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
312 /* wait for a free/empty scene
314 if (old_state
== SETUP_FLUSHED
)
315 lp_setup_get_empty_scene(setup
);
322 if (!begin_binning( setup
))
327 if (old_state
== SETUP_CLEARED
)
328 if (!execute_clears( setup
))
331 lp_setup_rasterize_scene( setup
);
332 assert(setup
->scene
== NULL
);
336 assert(0 && "invalid setup state mode");
340 setup
->state
= new_state
;
345 lp_scene_end_rasterization(setup
->scene
);
349 setup
->state
= SETUP_FLUSHED
;
350 lp_setup_reset( setup
);
356 lp_setup_flush( struct lp_setup_context
*setup
,
357 struct pipe_fence_handle
**fence
,
360 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
363 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
369 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
370 const struct pipe_framebuffer_state
*fb
)
372 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
374 /* Flush any old scene.
376 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
379 * Ensure the old scene is not reused.
381 assert(!setup
->scene
);
383 /* Set new state. This will be picked up later when we next need a
386 util_copy_framebuffer_state(&setup
->fb
, fb
);
387 setup
->framebuffer
.x0
= 0;
388 setup
->framebuffer
.y0
= 0;
389 setup
->framebuffer
.x1
= fb
->width
-1;
390 setup
->framebuffer
.y1
= fb
->height
-1;
391 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
396 * Try to clear one color buffer of the attached fb, either by binning a clear
397 * command or queuing up the clear for later (when binning is started).
400 lp_setup_try_clear_color_buffer(struct lp_setup_context
*setup
,
401 const union pipe_color_union
*color
,
404 union lp_rast_cmd_arg clearrb_arg
;
406 enum pipe_format format
= setup
->fb
.cbufs
[cbuf
]->format
;
408 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
410 if (util_format_is_pure_integer(format
)) {
412 * We expect int/uint clear values here, though some APIs
413 * might disagree (but in any case util_pack_color()
414 * couldn't handle it)...
416 if (util_format_is_pure_sint(format
)) {
417 util_format_write_4i(format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
420 assert(util_format_is_pure_uint(format
));
421 util_format_write_4ui(format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
425 util_pack_color(color
->f
, format
, &uc
);
428 if (setup
->state
== SETUP_ACTIVE
) {
429 struct lp_scene
*scene
= setup
->scene
;
431 /* Add the clear to existing scene. In the unusual case where
432 * both color and depth-stencil are being cleared when there's
433 * already been some rendering, we could discard the currently
434 * binned scene and start again, but I don't see that as being
437 struct lp_rast_clear_rb
*cc_scene
=
438 (struct lp_rast_clear_rb
*)
439 lp_scene_alloc_aligned(scene
, sizeof(struct lp_rast_clear_rb
), 8);
445 cc_scene
->cbuf
= cbuf
;
446 cc_scene
->color_val
= uc
;
447 clearrb_arg
.clear_rb
= cc_scene
;
449 if (!lp_scene_bin_everywhere(scene
,
450 LP_RAST_OP_CLEAR_COLOR
,
455 /* Put ourselves into the 'pre-clear' state, specifically to try
456 * and accumulate multiple clears to color and depth_stencil
457 * buffers which the app or state-tracker might issue
460 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
462 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
463 setup
->clear
.flags
|= 1 << (cbuf
+ 2);
464 setup
->clear
.color_val
[cbuf
] = uc
;
471 lp_setup_try_clear_zs(struct lp_setup_context
*setup
,
477 uint64_t zsvalue
= 0;
480 enum pipe_format format
= setup
->fb
.zsbuf
->format
;
482 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
484 zmask32
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
485 smask8
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
487 zsvalue
= util_pack64_z_stencil(format
, depth
, stencil
);
489 zsmask
= util_pack64_mask_z_stencil(format
, zmask32
, smask8
);
493 if (format
== PIPE_FORMAT_Z24X8_UNORM
||
494 format
== PIPE_FORMAT_X8Z24_UNORM
) {
496 * Make full mask if there's "X" bits so we can do full
497 * clear (without rmw).
499 uint32_t zsmask_full
= 0;
500 zsmask_full
= util_pack_mask_z_stencil(format
, ~0, ~0);
501 zsmask
|= ~zsmask_full
;
504 if (setup
->state
== SETUP_ACTIVE
) {
505 struct lp_scene
*scene
= setup
->scene
;
507 /* Add the clear to existing scene. In the unusual case where
508 * both color and depth-stencil are being cleared when there's
509 * already been some rendering, we could discard the currently
510 * binned scene and start again, but I don't see that as being
513 if (!lp_scene_bin_everywhere(scene
,
514 LP_RAST_OP_CLEAR_ZSTENCIL
,
515 lp_rast_arg_clearzs(zsvalue
, zsmask
)))
519 /* Put ourselves into the 'pre-clear' state, specifically to try
520 * and accumulate multiple clears to color and depth_stencil
521 * buffers which the app or state-tracker might issue
524 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
526 setup
->clear
.flags
|= flags
;
528 setup
->clear
.zsmask
|= zsmask
;
529 setup
->clear
.zsvalue
=
530 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
537 lp_setup_clear( struct lp_setup_context
*setup
,
538 const union pipe_color_union
*color
,
546 * Note any of these (max 9) clears could fail (but at most there should
547 * be just one failure!). This avoids doing the previous succeeded
548 * clears again (we still clear tiles twice if a clear command succeeded
549 * partially for one buffer).
551 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
552 unsigned flagszs
= flags
& PIPE_CLEAR_DEPTHSTENCIL
;
553 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
)) {
554 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
556 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
))
561 if (flags
& PIPE_CLEAR_COLOR
) {
562 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
563 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
564 if ((flags
& (1 << (2 + i
))) && setup
->fb
.cbufs
[i
]) {
565 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
)) {
566 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
568 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
))
579 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
581 boolean ccw_is_frontface
,
583 boolean half_pixel_center
,
584 boolean bottom_edge_rule
)
586 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
588 setup
->ccw_is_frontface
= ccw_is_frontface
;
589 setup
->cullmode
= cull_mode
;
590 setup
->triangle
= first_triangle
;
591 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
592 setup
->bottom_edge_rule
= bottom_edge_rule
;
594 if (setup
->scissor_test
!= scissor
) {
595 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
596 setup
->scissor_test
= scissor
;
601 lp_setup_set_line_state( struct lp_setup_context
*setup
,
604 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
606 setup
->line_width
= line_width
;
610 lp_setup_set_point_state( struct lp_setup_context
*setup
,
612 boolean point_size_per_vertex
,
613 uint sprite_coord_enable
,
614 uint sprite_coord_origin
)
616 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
618 setup
->point_size
= point_size
;
619 setup
->sprite_coord_enable
= sprite_coord_enable
;
620 setup
->sprite_coord_origin
= sprite_coord_origin
;
621 setup
->point_size_per_vertex
= point_size_per_vertex
;
625 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
626 const struct lp_setup_variant
*variant
)
628 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
630 setup
->setup
.variant
= variant
;
634 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
635 struct lp_fragment_shader_variant
*variant
)
637 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
639 /* FIXME: reference count */
641 setup
->fs
.current
.variant
= variant
;
642 setup
->dirty
|= LP_SETUP_NEW_FS
;
646 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
648 struct pipe_constant_buffer
*buffers
)
652 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
654 assert(num
<= ARRAY_SIZE(setup
->constants
));
656 for (i
= 0; i
< num
; ++i
) {
657 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
659 for (; i
< ARRAY_SIZE(setup
->constants
); i
++) {
660 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
662 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
667 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
668 float alpha_ref_value
)
670 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
672 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
673 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
674 setup
->dirty
|= LP_SETUP_NEW_FS
;
679 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
680 const ubyte refs
[2] )
682 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
684 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
685 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
686 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
687 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
688 setup
->dirty
|= LP_SETUP_NEW_FS
;
693 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
694 const struct pipe_blend_color
*blend_color
)
696 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
700 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
701 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
702 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
708 lp_setup_set_scissors( struct lp_setup_context
*setup
,
709 const struct pipe_scissor_state
*scissors
)
712 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
716 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
717 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
718 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
719 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
720 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
722 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
727 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
728 boolean flatshade_first
)
730 setup
->flatshade_first
= flatshade_first
;
734 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
735 boolean rasterizer_discard
)
737 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
738 setup
->rasterizer_discard
= rasterizer_discard
;
739 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
744 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
745 struct vertex_info
*vertex_info
)
747 /* XXX: just silently holding onto the pointer:
749 setup
->vertex_info
= vertex_info
;
754 * Called during state validation when LP_NEW_VIEWPORT is set.
757 lp_setup_set_viewports(struct lp_setup_context
*setup
,
758 unsigned num_viewports
,
759 const struct pipe_viewport_state
*viewports
)
761 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
764 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
766 assert(num_viewports
<= PIPE_MAX_VIEWPORTS
);
770 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
772 for (i
= 0; i
< num_viewports
; i
++) {
775 util_viewport_zmin_zmax(&viewports
[i
], lp
->rasterizer
->clip_halfz
,
776 &min_depth
, &max_depth
);
778 if (setup
->viewports
[i
].min_depth
!= min_depth
||
779 setup
->viewports
[i
].max_depth
!= max_depth
) {
780 setup
->viewports
[i
].min_depth
= min_depth
;
781 setup
->viewports
[i
].max_depth
= max_depth
;
782 setup
->dirty
|= LP_SETUP_NEW_VIEWPORTS
;
789 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
792 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
794 struct pipe_sampler_view
**views
)
796 unsigned i
, max_tex_num
;
798 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
800 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
802 max_tex_num
= MAX2(num
, setup
->fs
.current_tex_num
);
804 for (i
= 0; i
< max_tex_num
; i
++) {
805 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
808 struct pipe_resource
*res
= view
->texture
;
809 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
810 struct lp_jit_texture
*jit_tex
;
811 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
813 /* We're referencing the texture's internal data, so save a
816 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
819 /* regular texture - setup array of mipmap level offsets */
821 unsigned first_level
= 0;
822 unsigned last_level
= 0;
824 if (llvmpipe_resource_is_texture(res
)) {
825 first_level
= view
->u
.tex
.first_level
;
826 last_level
= view
->u
.tex
.last_level
;
827 assert(first_level
<= last_level
);
828 assert(last_level
<= res
->last_level
);
829 jit_tex
->base
= lp_tex
->tex_data
;
832 jit_tex
->base
= lp_tex
->data
;
835 if (LP_PERF
& PERF_TEX_MEM
) {
836 /* use dummy tile memory */
837 jit_tex
->base
= lp_dummy_tile
;
838 jit_tex
->width
= TILE_SIZE
/8;
839 jit_tex
->height
= TILE_SIZE
/8;
841 jit_tex
->first_level
= 0;
842 jit_tex
->last_level
= 0;
843 jit_tex
->mip_offsets
[0] = 0;
844 jit_tex
->row_stride
[0] = 0;
845 jit_tex
->img_stride
[0] = 0;
848 jit_tex
->width
= res
->width0
;
849 jit_tex
->height
= res
->height0
;
850 jit_tex
->depth
= res
->depth0
;
851 jit_tex
->first_level
= first_level
;
852 jit_tex
->last_level
= last_level
;
854 if (llvmpipe_resource_is_texture(res
)) {
855 for (j
= first_level
; j
<= last_level
; j
++) {
856 jit_tex
->mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
857 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
858 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
861 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
862 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
863 res
->target
== PIPE_TEXTURE_CUBE
||
864 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
866 * For array textures, we don't have first_layer, instead
867 * adjust last_layer (stored as depth) plus the mip level offsets
868 * (as we have mip-first layout can't just adjust base ptr).
869 * XXX For mip levels, could do something similar.
871 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
872 for (j
= first_level
; j
<= last_level
; j
++) {
873 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
874 lp_tex
->img_stride
[j
];
876 if (view
->target
== PIPE_TEXTURE_CUBE
||
877 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
878 assert(jit_tex
->depth
% 6 == 0);
880 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
881 assert(view
->u
.tex
.last_layer
< res
->array_size
);
886 * For buffers, we don't have "offset", instead adjust
887 * the size (stored as width) plus the base pointer.
889 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
890 /* probably don't really need to fill that out */
891 jit_tex
->mip_offsets
[0] = 0;
892 jit_tex
->row_stride
[0] = 0;
893 jit_tex
->img_stride
[0] = 0;
895 /* everything specified in number of elements here. */
896 jit_tex
->width
= view
->u
.buf
.size
/ view_blocksize
;
897 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.offset
;
898 /* XXX Unsure if we need to sanitize parameters? */
899 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
904 /* display target texture/surface */
906 * XXX: Where should this be unmapped?
908 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
909 struct sw_winsys
*winsys
= screen
->winsys
;
910 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
912 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
913 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
914 jit_tex
->mip_offsets
[0] = 0;
915 jit_tex
->width
= res
->width0
;
916 jit_tex
->height
= res
->height0
;
917 jit_tex
->depth
= res
->depth0
;
918 jit_tex
->first_level
= jit_tex
->last_level
= 0;
919 assert(jit_tex
->base
);
923 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
926 setup
->fs
.current_tex_num
= num
;
928 setup
->dirty
|= LP_SETUP_NEW_FS
;
933 * Called during state validation when LP_NEW_SAMPLER is set.
936 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
938 struct pipe_sampler_state
**samplers
)
942 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
944 assert(num
<= PIPE_MAX_SAMPLERS
);
946 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
947 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
950 struct lp_jit_sampler
*jit_sam
;
951 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
953 jit_sam
->min_lod
= sampler
->min_lod
;
954 jit_sam
->max_lod
= sampler
->max_lod
;
955 jit_sam
->lod_bias
= sampler
->lod_bias
;
956 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
960 setup
->dirty
|= LP_SETUP_NEW_FS
;
965 * Is the given texture referenced by any scene?
966 * Note: we have to check all scenes including any scenes currently
967 * being rendered and the current scene being built.
970 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
971 const struct pipe_resource
*texture
)
975 /* check the render targets */
976 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
977 if (setup
->fb
.cbufs
[i
] && setup
->fb
.cbufs
[i
]->texture
== texture
)
978 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
980 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
981 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
984 /* check textures referenced by the scene */
985 for (i
= 0; i
< ARRAY_SIZE(setup
->scenes
); i
++) {
986 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
987 return LP_REFERENCED_FOR_READ
;
991 return LP_UNREFERENCED
;
996 * Called by vbuf code when we're about to draw something.
998 * This function stores all dirty state in the current scene's display list
999 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
1000 * the JIT functions, as the JIT functions will be called later on, most likely
1001 * on a different thread.
1003 * When processing dirty state it is imperative that we don't refer to any
1004 * pointers previously allocated with lp_scene_alloc() in this function (or any
1005 * function) as they may belong to a scene freed since then.
1008 try_update_scene_state( struct lp_setup_context
*setup
)
1010 static const float fake_const_buf
[4];
1011 boolean new_scene
= (setup
->fs
.stored
== NULL
);
1012 struct lp_scene
*scene
= setup
->scene
;
1017 if (setup
->dirty
& LP_SETUP_NEW_VIEWPORTS
) {
1019 * Record new depth range state for changes due to viewport updates.
1021 * TODO: Collapse the existing viewport and depth range information
1022 * into one structure, for access by JIT.
1024 struct lp_jit_viewport
*stored
;
1026 stored
= (struct lp_jit_viewport
*)
1027 lp_scene_alloc(scene
, sizeof setup
->viewports
);
1034 memcpy(stored
, setup
->viewports
, sizeof setup
->viewports
);
1036 setup
->fs
.current
.jit_context
.viewports
= stored
;
1037 setup
->dirty
|= LP_SETUP_NEW_FS
;
1040 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
1046 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
1047 size
= 4 * 16 * sizeof(uint8_t);
1048 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
1049 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MIN_VECTOR_ALIGN
);
1056 /* Store floating point colour */
1057 fstored
= (float*)(stored
+ 4*16);
1058 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
1059 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
1062 /* smear each blend color component across 16 ubyte elements */
1063 for (i
= 0; i
< 4; ++i
) {
1064 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
1065 for (j
= 0; j
< 16; ++j
)
1066 stored
[i
*16 + j
] = c
;
1069 setup
->blend_color
.stored
= stored
;
1070 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
1071 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
1072 setup
->dirty
|= LP_SETUP_NEW_FS
;
1075 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
1076 for (i
= 0; i
< ARRAY_SIZE(setup
->constants
); ++i
) {
1077 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
1078 const unsigned current_size
= MIN2(setup
->constants
[i
].current
.buffer_size
,
1079 LP_MAX_TGSI_CONST_BUFFER_SIZE
);
1080 const ubyte
*current_data
= NULL
;
1083 STATIC_ASSERT(DATA_BLOCK_SIZE
>= LP_MAX_TGSI_CONST_BUFFER_SIZE
);
1086 /* resource buffer */
1087 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1089 else if (setup
->constants
[i
].current
.user_buffer
) {
1090 /* user-space buffer */
1091 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
1095 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
1097 /* TODO: copy only the actually used constants? */
1099 if (setup
->constants
[i
].stored_size
!= current_size
||
1100 !setup
->constants
[i
].stored_data
||
1101 memcmp(setup
->constants
[i
].stored_data
,
1103 current_size
) != 0) {
1106 stored
= lp_scene_alloc(scene
, current_size
);
1115 setup
->constants
[i
].stored_size
= current_size
;
1116 setup
->constants
[i
].stored_data
= stored
;
1118 setup
->fs
.current
.jit_context
.constants
[i
] =
1119 setup
->constants
[i
].stored_data
;
1122 setup
->constants
[i
].stored_size
= 0;
1123 setup
->constants
[i
].stored_data
= NULL
;
1124 setup
->fs
.current
.jit_context
.constants
[i
] = fake_const_buf
;
1128 setup
->constants
[i
].stored_size
/ (sizeof(float) * 4);
1129 setup
->fs
.current
.jit_context
.num_constants
[i
] = num_constants
;
1130 setup
->dirty
|= LP_SETUP_NEW_FS
;
1135 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
1136 if (!setup
->fs
.stored
||
1137 memcmp(setup
->fs
.stored
,
1139 sizeof setup
->fs
.current
) != 0)
1141 struct lp_rast_state
*stored
;
1143 /* The fs state that's been stored in the scene is different from
1144 * the new, current state. So allocate a new lp_rast_state object
1145 * and append it to the bin's setup data buffer.
1147 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
1155 sizeof setup
->fs
.current
);
1156 setup
->fs
.stored
= stored
;
1158 /* The scene now references the textures in the rasterization
1159 * state record. Note that now.
1161 for (i
= 0; i
< ARRAY_SIZE(setup
->fs
.current_tex
); i
++) {
1162 if (setup
->fs
.current_tex
[i
]) {
1163 if (!lp_scene_add_resource_reference(scene
,
1164 setup
->fs
.current_tex
[i
],
1174 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1176 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1177 setup
->draw_regions
[i
] = setup
->framebuffer
;
1178 if (setup
->scissor_test
) {
1179 u_rect_possible_intersection(&setup
->scissors
[i
],
1180 &setup
->draw_regions
[i
]);
1187 assert(setup
->fs
.stored
);
1192 lp_setup_update_state( struct lp_setup_context
*setup
,
1193 boolean update_scene
)
1195 /* Some of the 'draw' pipeline stages may have changed some driver state.
1196 * Make sure we've processed those state changes before anything else.
1198 * XXX this is the only place where llvmpipe_context is used in the
1199 * setup code. This may get refactored/changed...
1202 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1204 llvmpipe_update_derived(lp
);
1207 if (lp
->setup
->dirty
) {
1208 llvmpipe_update_setup(lp
);
1211 assert(setup
->setup
.variant
);
1213 /* Will probably need to move this somewhere else, just need
1214 * to know about vertex shader point size attribute.
1216 setup
->psize_slot
= lp
->psize_slot
;
1217 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1218 setup
->layer_slot
= lp
->layer_slot
;
1219 setup
->face_slot
= lp
->face_slot
;
1221 assert(lp
->dirty
== 0);
1223 assert(lp
->setup_variant
.key
.size
==
1224 setup
->setup
.variant
->key
.size
);
1226 assert(memcmp(&lp
->setup_variant
.key
,
1227 &setup
->setup
.variant
->key
,
1228 setup
->setup
.variant
->key
.size
) == 0);
1231 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1232 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1236 /* Only call into update_scene_state() if we already have a
1239 if (update_scene
&& setup
->scene
) {
1240 assert(setup
->state
== SETUP_ACTIVE
);
1242 if (try_update_scene_state(setup
))
1245 /* Update failed, try to restart the scene.
1247 * Cannot call lp_setup_flush_and_restart() directly here
1248 * because of potential recursion.
1250 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1253 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1259 return try_update_scene_state(setup
);
1267 /* Only caller is lp_setup_vbuf_destroy()
1270 lp_setup_destroy( struct lp_setup_context
*setup
)
1274 lp_setup_reset( setup
);
1276 util_unreference_framebuffer_state(&setup
->fb
);
1278 for (i
= 0; i
< ARRAY_SIZE(setup
->fs
.current_tex
); i
++) {
1279 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1282 for (i
= 0; i
< ARRAY_SIZE(setup
->constants
); i
++) {
1283 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1286 /* free the scenes in the 'empty' queue */
1287 for (i
= 0; i
< ARRAY_SIZE(setup
->scenes
); i
++) {
1288 struct lp_scene
*scene
= setup
->scenes
[i
];
1291 lp_fence_wait(scene
->fence
);
1293 lp_scene_destroy(scene
);
1296 lp_fence_reference(&setup
->last_fence
, NULL
);
1303 * Create a new primitive tiling engine. Plug it into the backend of
1304 * the draw module. Currently also creates a rasterizer to use with
1307 struct lp_setup_context
*
1308 lp_setup_create( struct pipe_context
*pipe
,
1309 struct draw_context
*draw
)
1311 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1312 struct lp_setup_context
*setup
;
1315 setup
= CALLOC_STRUCT(lp_setup_context
);
1320 lp_setup_init_vbuf(setup
);
1322 /* Used only in update_state():
1327 setup
->num_threads
= screen
->num_threads
;
1328 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1333 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1334 draw_set_render(draw
, &setup
->base
);
1336 /* create some empty scenes */
1337 for (i
= 0; i
< MAX_SCENES
; i
++) {
1338 setup
->scenes
[i
] = lp_scene_create( pipe
);
1339 if (!setup
->scenes
[i
]) {
1344 setup
->triangle
= first_triangle
;
1345 setup
->line
= first_line
;
1346 setup
->point
= first_point
;
1353 for (i
= 0; i
< MAX_SCENES
; i
++) {
1354 if (setup
->scenes
[i
]) {
1355 lp_scene_destroy(setup
->scenes
[i
]);
1359 setup
->vbuf
->destroy(setup
->vbuf
);
1368 * Put a BeginQuery command into all bins.
1371 lp_setup_begin_query(struct lp_setup_context
*setup
,
1372 struct llvmpipe_query
*pq
)
1375 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1377 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1378 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1379 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1382 /* init the query to its beginning state */
1383 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1384 /* exceeding list size so just ignore the query */
1385 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1388 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1389 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1390 setup
->active_binned_queries
++;
1392 assert(setup
->scene
);
1394 if (!lp_scene_bin_everywhere(setup
->scene
,
1395 LP_RAST_OP_BEGIN_QUERY
,
1396 lp_rast_arg_query(pq
))) {
1398 if (!lp_setup_flush_and_restart(setup
))
1401 if (!lp_scene_bin_everywhere(setup
->scene
,
1402 LP_RAST_OP_BEGIN_QUERY
,
1403 lp_rast_arg_query(pq
))) {
1407 setup
->scene
->had_queries
|= TRUE
;
1413 * Put an EndQuery command into all bins.
1416 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1418 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1420 assert(setup
->scene
);
1422 /* pq->fence should be the fence of the *last* scene which
1423 * contributed to the query result.
1425 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1427 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1428 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1429 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1430 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1431 if (pq
->type
== PIPE_QUERY_TIMESTAMP
&&
1432 !(setup
->scene
->tiles_x
| setup
->scene
->tiles_y
)) {
1434 * If there's a zero width/height framebuffer, there's no bins and
1435 * hence no rast task is ever run. So fill in something here instead.
1437 pq
->end
[0] = os_time_get_nano();
1440 if (!lp_scene_bin_everywhere(setup
->scene
,
1441 LP_RAST_OP_END_QUERY
,
1442 lp_rast_arg_query(pq
))) {
1443 if (!lp_setup_flush_and_restart(setup
))
1446 if (!lp_scene_bin_everywhere(setup
->scene
,
1447 LP_RAST_OP_END_QUERY
,
1448 lp_rast_arg_query(pq
))) {
1452 setup
->scene
->had_queries
|= TRUE
;
1456 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1460 /* Need to do this now not earlier since it still needs to be marked as
1461 * active when binning it would cause a flush.
1463 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1464 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1465 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1468 /* remove from active binned query list */
1469 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1470 if (setup
->active_queries
[i
] == pq
)
1473 assert(i
< setup
->active_binned_queries
);
1474 if (i
== setup
->active_binned_queries
)
1476 setup
->active_binned_queries
--;
1477 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1478 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1484 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1486 if (0) debug_printf("%s\n", __FUNCTION__
);
1488 assert(setup
->state
== SETUP_ACTIVE
);
1490 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1493 if (!lp_setup_update_state(setup
, TRUE
))