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
%= ARRAY_SIZE(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
< ARRAY_SIZE(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
);
169 /* FIXME: We enqueue the scene then wait on the rasterizer to finish.
170 * This means we never actually run any vertex stuff in parallel to
171 * rasterization (not in the same context at least) which is what the
172 * multiple scenes per setup is about - when we get a new empty scene
173 * any old one is already empty again because we waited here for
174 * raster tasks to be finished. Ideally, we shouldn't need to wait here
175 * and rely on fences elsewhere when waiting is necessary.
176 * Certainly, lp_scene_end_rasterization() would need to be deferred too
177 * and there's probably other bits why this doesn't actually work.
179 lp_rast_queue_scene(screen
->rast
, scene
);
180 lp_rast_finish(screen
->rast
);
181 pipe_mutex_unlock(screen
->rast_mutex
);
183 lp_scene_end_rasterization(setup
->scene
);
184 lp_setup_reset( setup
);
186 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
192 begin_binning( struct lp_setup_context
*setup
)
194 struct lp_scene
*scene
= setup
->scene
;
195 boolean need_zsload
= FALSE
;
199 assert(scene
->fence
== NULL
);
201 /* Always create a fence:
203 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
207 ok
= try_update_scene_state(setup
);
211 if (setup
->fb
.zsbuf
&&
212 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
213 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
216 LP_DBG(DEBUG_SETUP
, "%s color clear bufs: %x depth: %s\n", __FUNCTION__
,
217 setup
->clear
.flags
>> 2,
218 need_zsload
? "clear": "load");
220 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
222 for (cbuf
= 0; cbuf
< setup
->fb
.nr_cbufs
; cbuf
++) {
223 assert(PIPE_CLEAR_COLOR0
== 1 << 2);
224 if (setup
->clear
.flags
& (1 << (2 + cbuf
))) {
225 union lp_rast_cmd_arg clearrb_arg
;
226 struct lp_rast_clear_rb
*cc_scene
=
227 (struct lp_rast_clear_rb
*)
228 lp_scene_alloc(scene
, sizeof(struct lp_rast_clear_rb
));
234 cc_scene
->cbuf
= cbuf
;
235 cc_scene
->color_val
= setup
->clear
.color_val
[cbuf
];
236 clearrb_arg
.clear_rb
= cc_scene
;
238 if (!lp_scene_bin_everywhere(scene
,
239 LP_RAST_OP_CLEAR_COLOR
,
246 if (setup
->fb
.zsbuf
) {
247 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
248 ok
= lp_scene_bin_everywhere( scene
,
249 LP_RAST_OP_CLEAR_ZSTENCIL
,
251 setup
->clear
.zsvalue
,
252 setup
->clear
.zsmask
));
258 setup
->clear
.flags
= 0;
259 setup
->clear
.zsmask
= 0;
260 setup
->clear
.zsvalue
= 0;
262 scene
->had_queries
= !!setup
->active_binned_queries
;
264 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
269 /* This basically bins and then flushes any outstanding full-screen
272 * TODO: fast path for fullscreen clears and no triangles.
275 execute_clears( struct lp_setup_context
*setup
)
277 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
279 return begin_binning( setup
);
282 const char *states
[] = {
290 set_scene_state( struct lp_setup_context
*setup
,
291 enum setup_state new_state
,
294 unsigned old_state
= setup
->state
;
296 if (old_state
== new_state
)
299 if (LP_DEBUG
& DEBUG_SCENE
) {
300 debug_printf("%s old %s new %s%s%s\n",
304 (new_state
== SETUP_FLUSHED
) ? ": " : "",
305 (new_state
== SETUP_FLUSHED
) ? reason
: "");
307 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
308 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
311 /* wait for a free/empty scene
313 if (old_state
== SETUP_FLUSHED
)
314 lp_setup_get_empty_scene(setup
);
321 if (!begin_binning( setup
))
326 if (old_state
== SETUP_CLEARED
)
327 if (!execute_clears( setup
))
330 lp_setup_rasterize_scene( setup
);
331 assert(setup
->scene
== NULL
);
335 assert(0 && "invalid setup state mode");
339 setup
->state
= new_state
;
344 lp_scene_end_rasterization(setup
->scene
);
348 setup
->state
= SETUP_FLUSHED
;
349 lp_setup_reset( setup
);
355 lp_setup_flush( struct lp_setup_context
*setup
,
356 struct pipe_fence_handle
**fence
,
359 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
362 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
368 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
369 const struct pipe_framebuffer_state
*fb
)
371 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
373 /* Flush any old scene.
375 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
378 * Ensure the old scene is not reused.
380 assert(!setup
->scene
);
382 /* Set new state. This will be picked up later when we next need a
385 util_copy_framebuffer_state(&setup
->fb
, fb
);
386 setup
->framebuffer
.x0
= 0;
387 setup
->framebuffer
.y0
= 0;
388 setup
->framebuffer
.x1
= fb
->width
-1;
389 setup
->framebuffer
.y1
= fb
->height
-1;
390 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
395 * Try to clear one color buffer of the attached fb, either by binning a clear
396 * command or queuing up the clear for later (when binning is started).
399 lp_setup_try_clear_color_buffer(struct lp_setup_context
*setup
,
400 const union pipe_color_union
*color
,
403 union lp_rast_cmd_arg clearrb_arg
;
405 enum pipe_format format
= setup
->fb
.cbufs
[cbuf
]->format
;
407 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
409 if (util_format_is_pure_integer(format
)) {
411 * We expect int/uint clear values here, though some APIs
412 * might disagree (but in any case util_pack_color()
413 * couldn't handle it)...
415 if (util_format_is_pure_sint(format
)) {
416 util_format_write_4i(format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
419 assert(util_format_is_pure_uint(format
));
420 util_format_write_4ui(format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
424 util_pack_color(color
->f
, format
, &uc
);
427 if (setup
->state
== SETUP_ACTIVE
) {
428 struct lp_scene
*scene
= setup
->scene
;
430 /* Add the clear to existing scene. In the unusual case where
431 * both color and depth-stencil are being cleared when there's
432 * already been some rendering, we could discard the currently
433 * binned scene and start again, but I don't see that as being
436 struct lp_rast_clear_rb
*cc_scene
=
437 (struct lp_rast_clear_rb
*)
438 lp_scene_alloc_aligned(scene
, sizeof(struct lp_rast_clear_rb
), 8);
444 cc_scene
->cbuf
= cbuf
;
445 cc_scene
->color_val
= uc
;
446 clearrb_arg
.clear_rb
= cc_scene
;
448 if (!lp_scene_bin_everywhere(scene
,
449 LP_RAST_OP_CLEAR_COLOR
,
454 /* Put ourselves into the 'pre-clear' state, specifically to try
455 * and accumulate multiple clears to color and depth_stencil
456 * buffers which the app or state-tracker might issue
459 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
461 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
462 setup
->clear
.flags
|= 1 << (cbuf
+ 2);
463 setup
->clear
.color_val
[cbuf
] = uc
;
470 lp_setup_try_clear_zs(struct lp_setup_context
*setup
,
476 uint64_t zsvalue
= 0;
479 enum pipe_format format
= setup
->fb
.zsbuf
->format
;
481 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
483 zmask32
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
484 smask8
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
486 zsvalue
= util_pack64_z_stencil(format
, depth
, stencil
);
488 zsmask
= util_pack64_mask_z_stencil(format
, zmask32
, smask8
);
492 if (format
== PIPE_FORMAT_Z24X8_UNORM
||
493 format
== PIPE_FORMAT_X8Z24_UNORM
) {
495 * Make full mask if there's "X" bits so we can do full
496 * clear (without rmw).
498 uint32_t zsmask_full
= 0;
499 zsmask_full
= util_pack_mask_z_stencil(format
, ~0, ~0);
500 zsmask
|= ~zsmask_full
;
503 if (setup
->state
== SETUP_ACTIVE
) {
504 struct lp_scene
*scene
= setup
->scene
;
506 /* Add the clear to existing scene. In the unusual case where
507 * both color and depth-stencil are being cleared when there's
508 * already been some rendering, we could discard the currently
509 * binned scene and start again, but I don't see that as being
512 if (!lp_scene_bin_everywhere(scene
,
513 LP_RAST_OP_CLEAR_ZSTENCIL
,
514 lp_rast_arg_clearzs(zsvalue
, zsmask
)))
518 /* Put ourselves into the 'pre-clear' state, specifically to try
519 * and accumulate multiple clears to color and depth_stencil
520 * buffers which the app or state-tracker might issue
523 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
525 setup
->clear
.flags
|= flags
;
527 setup
->clear
.zsmask
|= zsmask
;
528 setup
->clear
.zsvalue
=
529 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
536 lp_setup_clear( struct lp_setup_context
*setup
,
537 const union pipe_color_union
*color
,
545 * Note any of these (max 9) clears could fail (but at most there should
546 * be just one failure!). This avoids doing the previous succeeded
547 * clears again (we still clear tiles twice if a clear command succeeded
548 * partially for one buffer).
550 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
551 unsigned flagszs
= flags
& PIPE_CLEAR_DEPTHSTENCIL
;
552 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
)) {
553 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
555 if (!lp_setup_try_clear_zs(setup
, depth
, stencil
, flagszs
))
560 if (flags
& PIPE_CLEAR_COLOR
) {
561 assert(PIPE_CLEAR_COLOR0
== (1 << 2));
562 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
563 if ((flags
& (1 << (2 + i
))) && setup
->fb
.cbufs
[i
]) {
564 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
)) {
565 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
567 if (!lp_setup_try_clear_color_buffer(setup
, color
, i
))
578 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
580 boolean ccw_is_frontface
,
582 boolean half_pixel_center
,
583 boolean bottom_edge_rule
)
585 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
587 setup
->ccw_is_frontface
= ccw_is_frontface
;
588 setup
->cullmode
= cull_mode
;
589 setup
->triangle
= first_triangle
;
590 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
591 setup
->bottom_edge_rule
= bottom_edge_rule
;
593 if (setup
->scissor_test
!= scissor
) {
594 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
595 setup
->scissor_test
= scissor
;
600 lp_setup_set_line_state( struct lp_setup_context
*setup
,
603 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
605 setup
->line_width
= line_width
;
609 lp_setup_set_point_state( struct lp_setup_context
*setup
,
611 boolean point_size_per_vertex
,
612 uint sprite_coord_enable
,
613 uint sprite_coord_origin
)
615 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
617 setup
->point_size
= point_size
;
618 setup
->sprite_coord_enable
= sprite_coord_enable
;
619 setup
->sprite_coord_origin
= sprite_coord_origin
;
620 setup
->point_size_per_vertex
= point_size_per_vertex
;
624 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
625 const struct lp_setup_variant
*variant
)
627 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
629 setup
->setup
.variant
= variant
;
633 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
634 struct lp_fragment_shader_variant
*variant
)
636 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
638 /* FIXME: reference count */
640 setup
->fs
.current
.variant
= variant
;
641 setup
->dirty
|= LP_SETUP_NEW_FS
;
645 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
647 struct pipe_constant_buffer
*buffers
)
651 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
653 assert(num
<= ARRAY_SIZE(setup
->constants
));
655 for (i
= 0; i
< num
; ++i
) {
656 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
658 for (; i
< ARRAY_SIZE(setup
->constants
); i
++) {
659 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
661 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
666 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
667 float alpha_ref_value
)
669 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
671 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
672 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
673 setup
->dirty
|= LP_SETUP_NEW_FS
;
678 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
679 const ubyte refs
[2] )
681 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
683 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
684 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
685 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
686 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
687 setup
->dirty
|= LP_SETUP_NEW_FS
;
692 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
693 const struct pipe_blend_color
*blend_color
)
695 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
699 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
700 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
701 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
707 lp_setup_set_scissors( struct lp_setup_context
*setup
,
708 const struct pipe_scissor_state
*scissors
)
711 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
715 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
716 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
717 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
718 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
719 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
721 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
726 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
727 boolean flatshade_first
)
729 setup
->flatshade_first
= flatshade_first
;
733 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
734 boolean rasterizer_discard
)
736 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
737 setup
->rasterizer_discard
= rasterizer_discard
;
738 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
743 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
744 struct vertex_info
*vertex_info
)
746 /* XXX: just silently holding onto the pointer:
748 setup
->vertex_info
= vertex_info
;
753 * Called during state validation when LP_NEW_VIEWPORT is set.
756 lp_setup_set_viewports(struct lp_setup_context
*setup
,
757 unsigned num_viewports
,
758 const struct pipe_viewport_state
*viewports
)
760 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
763 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
765 assert(num_viewports
<= PIPE_MAX_VIEWPORTS
);
769 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
771 for (i
= 0; i
< num_viewports
; i
++) {
775 if (lp
->rasterizer
->clip_halfz
== 0) {
776 float half_depth
= viewports
[i
].scale
[2];
777 min_depth
= viewports
[i
].translate
[2] - half_depth
;
778 max_depth
= min_depth
+ half_depth
* 2.0f
;
780 min_depth
= viewports
[i
].translate
[2];
781 max_depth
= min_depth
+ viewports
[i
].scale
[2];
784 if (setup
->viewports
[i
].min_depth
!= min_depth
||
785 setup
->viewports
[i
].max_depth
!= max_depth
) {
786 setup
->viewports
[i
].min_depth
= min_depth
;
787 setup
->viewports
[i
].max_depth
= max_depth
;
788 setup
->dirty
|= LP_SETUP_NEW_VIEWPORTS
;
795 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
798 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
800 struct pipe_sampler_view
**views
)
802 unsigned i
, max_tex_num
;
804 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
806 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
808 max_tex_num
= MAX2(num
, setup
->fs
.current_tex_num
);
810 for (i
= 0; i
< max_tex_num
; i
++) {
811 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
814 struct pipe_resource
*res
= view
->texture
;
815 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
816 struct lp_jit_texture
*jit_tex
;
817 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
819 /* We're referencing the texture's internal data, so save a
822 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
825 /* regular texture - setup array of mipmap level offsets */
827 unsigned first_level
= 0;
828 unsigned last_level
= 0;
830 if (llvmpipe_resource_is_texture(res
)) {
831 first_level
= view
->u
.tex
.first_level
;
832 last_level
= view
->u
.tex
.last_level
;
833 assert(first_level
<= last_level
);
834 assert(last_level
<= res
->last_level
);
835 jit_tex
->base
= lp_tex
->tex_data
;
838 jit_tex
->base
= lp_tex
->data
;
841 if (LP_PERF
& PERF_TEX_MEM
) {
842 /* use dummy tile memory */
843 jit_tex
->base
= lp_dummy_tile
;
844 jit_tex
->width
= TILE_SIZE
/8;
845 jit_tex
->height
= TILE_SIZE
/8;
847 jit_tex
->first_level
= 0;
848 jit_tex
->last_level
= 0;
849 jit_tex
->mip_offsets
[0] = 0;
850 jit_tex
->row_stride
[0] = 0;
851 jit_tex
->img_stride
[0] = 0;
854 jit_tex
->width
= res
->width0
;
855 jit_tex
->height
= res
->height0
;
856 jit_tex
->depth
= res
->depth0
;
857 jit_tex
->first_level
= first_level
;
858 jit_tex
->last_level
= last_level
;
860 if (llvmpipe_resource_is_texture(res
)) {
861 for (j
= first_level
; j
<= last_level
; j
++) {
862 jit_tex
->mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
863 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
864 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
867 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
868 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
869 res
->target
== PIPE_TEXTURE_CUBE
||
870 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
872 * For array textures, we don't have first_layer, instead
873 * adjust last_layer (stored as depth) plus the mip level offsets
874 * (as we have mip-first layout can't just adjust base ptr).
875 * XXX For mip levels, could do something similar.
877 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
878 for (j
= first_level
; j
<= last_level
; j
++) {
879 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
880 lp_tex
->img_stride
[j
];
882 if (view
->target
== PIPE_TEXTURE_CUBE
||
883 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
884 assert(jit_tex
->depth
% 6 == 0);
886 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
887 assert(view
->u
.tex
.last_layer
< res
->array_size
);
892 * For buffers, we don't have "offset", instead adjust
893 * the size (stored as width) plus the base pointer.
895 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
896 /* probably don't really need to fill that out */
897 jit_tex
->mip_offsets
[0] = 0;
898 jit_tex
->row_stride
[0] = 0;
899 jit_tex
->img_stride
[0] = 0;
901 /* everything specified in number of elements here. */
902 jit_tex
->width
= view
->u
.buf
.size
/ view_blocksize
;
903 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.offset
;
904 /* XXX Unsure if we need to sanitize parameters? */
905 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
910 /* display target texture/surface */
912 * XXX: Where should this be unmapped?
914 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
915 struct sw_winsys
*winsys
= screen
->winsys
;
916 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
918 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
919 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
920 jit_tex
->mip_offsets
[0] = 0;
921 jit_tex
->width
= res
->width0
;
922 jit_tex
->height
= res
->height0
;
923 jit_tex
->depth
= res
->depth0
;
924 jit_tex
->first_level
= jit_tex
->last_level
= 0;
925 assert(jit_tex
->base
);
929 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
932 setup
->fs
.current_tex_num
= num
;
934 setup
->dirty
|= LP_SETUP_NEW_FS
;
939 * Called during state validation when LP_NEW_SAMPLER is set.
942 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
944 struct pipe_sampler_state
**samplers
)
948 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
950 assert(num
<= PIPE_MAX_SAMPLERS
);
952 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
953 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
956 struct lp_jit_sampler
*jit_sam
;
957 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
959 jit_sam
->min_lod
= sampler
->min_lod
;
960 jit_sam
->max_lod
= sampler
->max_lod
;
961 jit_sam
->lod_bias
= sampler
->lod_bias
;
962 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
966 setup
->dirty
|= LP_SETUP_NEW_FS
;
971 * Is the given texture referenced by any scene?
972 * Note: we have to check all scenes including any scenes currently
973 * being rendered and the current scene being built.
976 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
977 const struct pipe_resource
*texture
)
981 /* check the render targets */
982 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
983 if (setup
->fb
.cbufs
[i
] && setup
->fb
.cbufs
[i
]->texture
== texture
)
984 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
986 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
987 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
990 /* check textures referenced by the scene */
991 for (i
= 0; i
< ARRAY_SIZE(setup
->scenes
); i
++) {
992 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
993 return LP_REFERENCED_FOR_READ
;
997 return LP_UNREFERENCED
;
1002 * Called by vbuf code when we're about to draw something.
1004 * This function stores all dirty state in the current scene's display list
1005 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
1006 * the JIT functions, as the JIT functions will be called later on, most likely
1007 * on a different thread.
1009 * When processing dirty state it is imperative that we don't refer to any
1010 * pointers previously allocated with lp_scene_alloc() in this function (or any
1011 * function) as they may belong to a scene freed since then.
1014 try_update_scene_state( struct lp_setup_context
*setup
)
1016 static const float fake_const_buf
[4];
1017 boolean new_scene
= (setup
->fs
.stored
== NULL
);
1018 struct lp_scene
*scene
= setup
->scene
;
1023 if (setup
->dirty
& LP_SETUP_NEW_VIEWPORTS
) {
1025 * Record new depth range state for changes due to viewport updates.
1027 * TODO: Collapse the existing viewport and depth range information
1028 * into one structure, for access by JIT.
1030 struct lp_jit_viewport
*stored
;
1032 stored
= (struct lp_jit_viewport
*)
1033 lp_scene_alloc(scene
, sizeof setup
->viewports
);
1040 memcpy(stored
, setup
->viewports
, sizeof setup
->viewports
);
1042 setup
->fs
.current
.jit_context
.viewports
= stored
;
1043 setup
->dirty
|= LP_SETUP_NEW_FS
;
1046 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
1052 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
1053 size
= 4 * 16 * sizeof(uint8_t);
1054 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
1055 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MIN_VECTOR_ALIGN
);
1062 /* Store floating point colour */
1063 fstored
= (float*)(stored
+ 4*16);
1064 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
1065 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
1068 /* smear each blend color component across 16 ubyte elements */
1069 for (i
= 0; i
< 4; ++i
) {
1070 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
1071 for (j
= 0; j
< 16; ++j
)
1072 stored
[i
*16 + j
] = c
;
1075 setup
->blend_color
.stored
= stored
;
1076 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
1077 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
1078 setup
->dirty
|= LP_SETUP_NEW_FS
;
1081 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
1082 for (i
= 0; i
< ARRAY_SIZE(setup
->constants
); ++i
) {
1083 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
1084 const unsigned current_size
= MIN2(setup
->constants
[i
].current
.buffer_size
,
1085 LP_MAX_TGSI_CONST_BUFFER_SIZE
);
1086 const ubyte
*current_data
= NULL
;
1089 STATIC_ASSERT(DATA_BLOCK_SIZE
>= LP_MAX_TGSI_CONST_BUFFER_SIZE
);
1092 /* resource buffer */
1093 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1095 else if (setup
->constants
[i
].current
.user_buffer
) {
1096 /* user-space buffer */
1097 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
1101 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
1103 /* TODO: copy only the actually used constants? */
1105 if (setup
->constants
[i
].stored_size
!= current_size
||
1106 !setup
->constants
[i
].stored_data
||
1107 memcmp(setup
->constants
[i
].stored_data
,
1109 current_size
) != 0) {
1112 stored
= lp_scene_alloc(scene
, current_size
);
1121 setup
->constants
[i
].stored_size
= current_size
;
1122 setup
->constants
[i
].stored_data
= stored
;
1124 setup
->fs
.current
.jit_context
.constants
[i
] =
1125 setup
->constants
[i
].stored_data
;
1128 setup
->constants
[i
].stored_size
= 0;
1129 setup
->constants
[i
].stored_data
= NULL
;
1130 setup
->fs
.current
.jit_context
.constants
[i
] = fake_const_buf
;
1134 setup
->constants
[i
].stored_size
/ (sizeof(float) * 4);
1135 setup
->fs
.current
.jit_context
.num_constants
[i
] = num_constants
;
1136 setup
->dirty
|= LP_SETUP_NEW_FS
;
1141 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
1142 if (!setup
->fs
.stored
||
1143 memcmp(setup
->fs
.stored
,
1145 sizeof setup
->fs
.current
) != 0)
1147 struct lp_rast_state
*stored
;
1149 /* The fs state that's been stored in the scene is different from
1150 * the new, current state. So allocate a new lp_rast_state object
1151 * and append it to the bin's setup data buffer.
1153 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
1161 sizeof setup
->fs
.current
);
1162 setup
->fs
.stored
= stored
;
1164 /* The scene now references the textures in the rasterization
1165 * state record. Note that now.
1167 for (i
= 0; i
< ARRAY_SIZE(setup
->fs
.current_tex
); i
++) {
1168 if (setup
->fs
.current_tex
[i
]) {
1169 if (!lp_scene_add_resource_reference(scene
,
1170 setup
->fs
.current_tex
[i
],
1180 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1182 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1183 setup
->draw_regions
[i
] = setup
->framebuffer
;
1184 if (setup
->scissor_test
) {
1185 u_rect_possible_intersection(&setup
->scissors
[i
],
1186 &setup
->draw_regions
[i
]);
1193 assert(setup
->fs
.stored
);
1198 lp_setup_update_state( struct lp_setup_context
*setup
,
1199 boolean update_scene
)
1201 /* Some of the 'draw' pipeline stages may have changed some driver state.
1202 * Make sure we've processed those state changes before anything else.
1204 * XXX this is the only place where llvmpipe_context is used in the
1205 * setup code. This may get refactored/changed...
1208 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1210 llvmpipe_update_derived(lp
);
1213 if (lp
->setup
->dirty
) {
1214 llvmpipe_update_setup(lp
);
1217 assert(setup
->setup
.variant
);
1219 /* Will probably need to move this somewhere else, just need
1220 * to know about vertex shader point size attribute.
1222 setup
->psize_slot
= lp
->psize_slot
;
1223 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1224 setup
->layer_slot
= lp
->layer_slot
;
1225 setup
->face_slot
= lp
->face_slot
;
1227 assert(lp
->dirty
== 0);
1229 assert(lp
->setup_variant
.key
.size
==
1230 setup
->setup
.variant
->key
.size
);
1232 assert(memcmp(&lp
->setup_variant
.key
,
1233 &setup
->setup
.variant
->key
,
1234 setup
->setup
.variant
->key
.size
) == 0);
1237 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1238 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1242 /* Only call into update_scene_state() if we already have a
1245 if (update_scene
&& setup
->scene
) {
1246 assert(setup
->state
== SETUP_ACTIVE
);
1248 if (try_update_scene_state(setup
))
1251 /* Update failed, try to restart the scene.
1253 * Cannot call lp_setup_flush_and_restart() directly here
1254 * because of potential recursion.
1256 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1259 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1265 return try_update_scene_state(setup
);
1273 /* Only caller is lp_setup_vbuf_destroy()
1276 lp_setup_destroy( struct lp_setup_context
*setup
)
1280 lp_setup_reset( setup
);
1282 util_unreference_framebuffer_state(&setup
->fb
);
1284 for (i
= 0; i
< ARRAY_SIZE(setup
->fs
.current_tex
); i
++) {
1285 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1288 for (i
= 0; i
< ARRAY_SIZE(setup
->constants
); i
++) {
1289 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1292 /* free the scenes in the 'empty' queue */
1293 for (i
= 0; i
< ARRAY_SIZE(setup
->scenes
); i
++) {
1294 struct lp_scene
*scene
= setup
->scenes
[i
];
1297 lp_fence_wait(scene
->fence
);
1299 lp_scene_destroy(scene
);
1302 lp_fence_reference(&setup
->last_fence
, NULL
);
1309 * Create a new primitive tiling engine. Plug it into the backend of
1310 * the draw module. Currently also creates a rasterizer to use with
1313 struct lp_setup_context
*
1314 lp_setup_create( struct pipe_context
*pipe
,
1315 struct draw_context
*draw
)
1317 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1318 struct lp_setup_context
*setup
;
1321 setup
= CALLOC_STRUCT(lp_setup_context
);
1326 lp_setup_init_vbuf(setup
);
1328 /* Used only in update_state():
1333 setup
->num_threads
= screen
->num_threads
;
1334 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1339 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1340 draw_set_render(draw
, &setup
->base
);
1342 /* create some empty scenes */
1343 for (i
= 0; i
< MAX_SCENES
; i
++) {
1344 setup
->scenes
[i
] = lp_scene_create( pipe
);
1345 if (!setup
->scenes
[i
]) {
1350 setup
->triangle
= first_triangle
;
1351 setup
->line
= first_line
;
1352 setup
->point
= first_point
;
1359 for (i
= 0; i
< MAX_SCENES
; i
++) {
1360 if (setup
->scenes
[i
]) {
1361 lp_scene_destroy(setup
->scenes
[i
]);
1365 setup
->vbuf
->destroy(setup
->vbuf
);
1374 * Put a BeginQuery command into all bins.
1377 lp_setup_begin_query(struct lp_setup_context
*setup
,
1378 struct llvmpipe_query
*pq
)
1381 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1383 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1384 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1385 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1388 /* init the query to its beginning state */
1389 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1390 /* exceeding list size so just ignore the query */
1391 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1394 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1395 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1396 setup
->active_binned_queries
++;
1398 assert(setup
->scene
);
1400 if (!lp_scene_bin_everywhere(setup
->scene
,
1401 LP_RAST_OP_BEGIN_QUERY
,
1402 lp_rast_arg_query(pq
))) {
1404 if (!lp_setup_flush_and_restart(setup
))
1407 if (!lp_scene_bin_everywhere(setup
->scene
,
1408 LP_RAST_OP_BEGIN_QUERY
,
1409 lp_rast_arg_query(pq
))) {
1413 setup
->scene
->had_queries
|= TRUE
;
1419 * Put an EndQuery command into all bins.
1422 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1424 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1426 assert(setup
->scene
);
1428 /* pq->fence should be the fence of the *last* scene which
1429 * contributed to the query result.
1431 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1433 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1434 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1435 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1436 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1437 if (pq
->type
== PIPE_QUERY_TIMESTAMP
&&
1438 !(setup
->scene
->tiles_x
| setup
->scene
->tiles_y
)) {
1440 * If there's a zero width/height framebuffer, there's no bins and
1441 * hence no rast task is ever run. So fill in something here instead.
1443 pq
->end
[0] = os_time_get_nano();
1446 if (!lp_scene_bin_everywhere(setup
->scene
,
1447 LP_RAST_OP_END_QUERY
,
1448 lp_rast_arg_query(pq
))) {
1449 if (!lp_setup_flush_and_restart(setup
))
1452 if (!lp_scene_bin_everywhere(setup
->scene
,
1453 LP_RAST_OP_END_QUERY
,
1454 lp_rast_arg_query(pq
))) {
1458 setup
->scene
->had_queries
|= TRUE
;
1462 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1466 /* Need to do this now not earlier since it still needs to be marked as
1467 * active when binning it would cause a flush.
1469 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1470 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1471 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1474 /* remove from active binned query list */
1475 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1476 if (setup
->active_queries
[i
] == pq
)
1479 assert(i
< setup
->active_binned_queries
);
1480 if (i
== setup
->active_binned_queries
)
1482 setup
->active_binned_queries
--;
1483 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1484 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1490 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1492 if (0) debug_printf("%s\n", __FUNCTION__
);
1494 assert(setup
->state
== SETUP_ACTIVE
);
1496 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1499 if (!lp_setup_update_state(setup
, TRUE
))