1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 "lp_context.h"
44 #include "lp_memory.h"
46 #include "lp_texture.h"
51 #include "lp_setup_context.h"
52 #include "lp_screen.h"
54 #include "state_tracker/sw_winsys.h"
56 #include "draw/draw_context.h"
57 #include "draw/draw_vbuf.h"
60 static boolean
set_scene_state( struct lp_setup_context
*, enum setup_state
,
62 static boolean
try_update_scene_state( struct lp_setup_context
*setup
);
66 lp_setup_get_empty_scene(struct lp_setup_context
*setup
)
68 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
69 boolean discard
= lp
->rasterizer
? lp
->rasterizer
->rasterizer_discard
: FALSE
;
71 assert(setup
->scene
== NULL
);
74 setup
->scene_idx
%= Elements(setup
->scenes
);
76 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
78 if (setup
->scene
->fence
) {
79 if (LP_DEBUG
& DEBUG_SETUP
)
80 debug_printf("%s: wait for scene %d\n",
81 __FUNCTION__
, setup
->scene
->fence
->id
);
83 lp_fence_wait(setup
->scene
->fence
);
86 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, discard
);
92 first_triangle( struct lp_setup_context
*setup
,
97 assert(setup
->state
== SETUP_ACTIVE
);
98 lp_setup_choose_triangle( setup
);
99 setup
->triangle( setup
, v0
, v1
, v2
);
103 first_line( struct lp_setup_context
*setup
,
104 const float (*v0
)[4],
105 const float (*v1
)[4])
107 assert(setup
->state
== SETUP_ACTIVE
);
108 lp_setup_choose_line( setup
);
109 setup
->line( setup
, v0
, v1
);
113 first_point( struct lp_setup_context
*setup
,
114 const float (*v0
)[4])
116 assert(setup
->state
== SETUP_ACTIVE
);
117 lp_setup_choose_point( setup
);
118 setup
->point( setup
, v0
);
121 void lp_setup_reset( struct lp_setup_context
*setup
)
125 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
127 /* Reset derived state */
128 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
129 setup
->constants
[i
].stored_size
= 0;
130 setup
->constants
[i
].stored_data
= NULL
;
132 setup
->fs
.stored
= NULL
;
140 memset(&setup
->clear
, 0, sizeof setup
->clear
);
142 /* Have an explicit "start-binning" call and get rid of this
145 setup
->line
= first_line
;
146 setup
->point
= first_point
;
147 setup
->triangle
= first_triangle
;
151 /** Rasterize all scene's bins */
153 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
155 struct lp_scene
*scene
= setup
->scene
;
156 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
158 lp_scene_end_binning(scene
);
160 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
162 if (setup
->last_fence
)
163 setup
->last_fence
->issued
= TRUE
;
165 pipe_mutex_lock(screen
->rast_mutex
);
166 lp_rast_queue_scene(screen
->rast
, scene
);
167 lp_rast_finish(screen
->rast
);
168 pipe_mutex_unlock(screen
->rast_mutex
);
170 lp_scene_end_rasterization(setup
->scene
);
171 lp_setup_reset( setup
);
173 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
179 begin_binning( struct lp_setup_context
*setup
)
181 struct lp_scene
*scene
= setup
->scene
;
182 boolean need_zsload
= FALSE
;
187 assert(scene
->fence
== NULL
);
189 /* Always create a fence:
191 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
195 /* Initialize the bin flags and x/y coords:
197 for (i
= 0; i
< scene
->tiles_x
; i
++) {
198 for (j
= 0; j
< scene
->tiles_y
; j
++) {
199 scene
->tile
[i
][j
].x
= i
;
200 scene
->tile
[i
][j
].y
= j
;
204 ok
= try_update_scene_state(setup
);
208 if (setup
->fb
.zsbuf
&&
209 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
210 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
213 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
214 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
215 need_zsload
? "clear": "load");
217 if (setup
->fb
.nr_cbufs
) {
218 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
219 ok
= lp_scene_bin_everywhere( scene
,
220 LP_RAST_OP_CLEAR_COLOR
,
221 setup
->clear
.color
);
227 if (setup
->fb
.zsbuf
) {
228 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
230 scene
->has_depthstencil_clear
= TRUE
;
232 ok
= lp_scene_bin_everywhere( scene
,
233 LP_RAST_OP_CLEAR_ZSTENCIL
,
235 setup
->clear
.zsvalue
,
236 setup
->clear
.zsmask
));
242 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
243 if (setup
->active_query
[i
]) {
244 ok
= lp_scene_bin_everywhere( scene
,
245 LP_RAST_OP_BEGIN_QUERY
,
246 lp_rast_arg_query(setup
->active_query
[i
]) );
252 setup
->clear
.flags
= 0;
253 setup
->clear
.zsmask
= 0;
254 setup
->clear
.zsvalue
= 0;
256 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
261 /* This basically bins and then flushes any outstanding full-screen
264 * TODO: fast path for fullscreen clears and no triangles.
267 execute_clears( struct lp_setup_context
*setup
)
269 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
271 return begin_binning( setup
);
274 const char *states
[] = {
282 set_scene_state( struct lp_setup_context
*setup
,
283 enum setup_state new_state
,
286 unsigned old_state
= setup
->state
;
288 if (old_state
== new_state
)
291 if (LP_DEBUG
& DEBUG_SCENE
) {
292 debug_printf("%s old %s new %s%s%s\n",
296 (new_state
== SETUP_FLUSHED
) ? ": " : "",
297 (new_state
== SETUP_FLUSHED
) ? reason
: "");
299 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
300 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
303 /* wait for a free/empty scene
305 if (old_state
== SETUP_FLUSHED
)
306 lp_setup_get_empty_scene(setup
);
313 if (!begin_binning( setup
))
318 if (old_state
== SETUP_CLEARED
)
319 if (!execute_clears( setup
))
322 lp_setup_rasterize_scene( setup
);
323 assert(setup
->scene
== NULL
);
327 assert(0 && "invalid setup state mode");
331 setup
->state
= new_state
;
336 lp_scene_end_rasterization(setup
->scene
);
340 setup
->state
= SETUP_FLUSHED
;
341 lp_setup_reset( setup
);
347 lp_setup_flush( struct lp_setup_context
*setup
,
348 struct pipe_fence_handle
**fence
,
351 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
354 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
360 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
361 const struct pipe_framebuffer_state
*fb
)
363 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
365 /* Flush any old scene.
367 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
370 * Ensure the old scene is not reused.
372 assert(!setup
->scene
);
374 /* Set new state. This will be picked up later when we next need a
377 util_copy_framebuffer_state(&setup
->fb
, fb
);
378 setup
->framebuffer
.x0
= 0;
379 setup
->framebuffer
.y0
= 0;
380 setup
->framebuffer
.x1
= fb
->width
-1;
381 setup
->framebuffer
.y1
= fb
->height
-1;
382 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
387 lp_setup_try_clear( struct lp_setup_context
*setup
,
388 const union pipe_color_union
*color
,
394 uint32_t zsvalue
= 0;
395 union lp_rast_cmd_arg color_arg
;
398 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
400 if (flags
& PIPE_CLEAR_COLOR
) {
401 for (i
= 0; i
< 4; i
++)
402 color_arg
.clear_color
.i
[i
] = color
->i
[i
];
405 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
406 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
407 uint32_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
409 zsvalue
= util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
414 zsmask
= util_pack_mask_z_stencil(setup
->fb
.zsbuf
->format
,
421 if (setup
->state
== SETUP_ACTIVE
) {
422 struct lp_scene
*scene
= setup
->scene
;
424 /* Add the clear to existing scene. In the unusual case where
425 * both color and depth-stencil are being cleared when there's
426 * already been some rendering, we could discard the currently
427 * binned scene and start again, but I don't see that as being
430 if (flags
& PIPE_CLEAR_COLOR
) {
431 if (!lp_scene_bin_everywhere( scene
,
432 LP_RAST_OP_CLEAR_COLOR
,
437 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
438 if (!lp_scene_bin_everywhere( scene
,
439 LP_RAST_OP_CLEAR_ZSTENCIL
,
440 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
445 /* Put ourselves into the 'pre-clear' state, specifically to try
446 * and accumulate multiple clears to color and depth_stencil
447 * buffers which the app or state-tracker might issue
450 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
452 setup
->clear
.flags
|= flags
;
454 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
455 setup
->clear
.zsmask
|= zsmask
;
456 setup
->clear
.zsvalue
=
457 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
460 if (flags
& PIPE_CLEAR_COLOR
) {
461 memcpy(&setup
->clear
.color
.clear_color
,
463 sizeof setup
->clear
.color
.clear_color
);
471 lp_setup_clear( struct lp_setup_context
*setup
,
472 const union pipe_color_union
*color
,
477 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
478 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
480 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
490 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
492 boolean ccw_is_frontface
,
494 boolean gl_rasterization_rules
)
496 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
498 setup
->ccw_is_frontface
= ccw_is_frontface
;
499 setup
->cullmode
= cull_mode
;
500 setup
->triangle
= first_triangle
;
501 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
503 if (setup
->scissor_test
!= scissor
) {
504 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
505 setup
->scissor_test
= scissor
;
510 lp_setup_set_line_state( struct lp_setup_context
*setup
,
513 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
515 setup
->line_width
= line_width
;
519 lp_setup_set_point_state( struct lp_setup_context
*setup
,
521 boolean point_size_per_vertex
,
522 uint sprite_coord_enable
,
523 uint sprite_coord_origin
)
525 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
527 setup
->point_size
= point_size
;
528 setup
->sprite_coord_enable
= sprite_coord_enable
;
529 setup
->sprite_coord_origin
= sprite_coord_origin
;
530 setup
->point_size_per_vertex
= point_size_per_vertex
;
534 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
535 const struct lp_setup_variant
*variant
)
537 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
539 setup
->setup
.variant
= variant
;
543 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
544 struct lp_fragment_shader_variant
*variant
)
546 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
548 /* FIXME: reference count */
550 setup
->fs
.current
.variant
= variant
;
551 setup
->dirty
|= LP_SETUP_NEW_FS
;
555 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
557 struct pipe_constant_buffer
*buffers
)
561 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
563 assert(num
<= Elements(setup
->constants
));
565 for (i
= 0; i
< num
; ++i
) {
566 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
568 for (; i
< Elements(setup
->constants
); i
++) {
569 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
571 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
576 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
577 float alpha_ref_value
)
579 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
581 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
582 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
583 setup
->dirty
|= LP_SETUP_NEW_FS
;
588 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
589 const ubyte refs
[2] )
591 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
593 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
594 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
595 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
596 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
597 setup
->dirty
|= LP_SETUP_NEW_FS
;
602 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
603 const struct pipe_blend_color
*blend_color
)
605 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
609 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
610 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
611 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
617 lp_setup_set_scissor( struct lp_setup_context
*setup
,
618 const struct pipe_scissor_state
*scissor
)
620 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
624 setup
->scissor
.x0
= scissor
->minx
;
625 setup
->scissor
.x1
= scissor
->maxx
-1;
626 setup
->scissor
.y0
= scissor
->miny
;
627 setup
->scissor
.y1
= scissor
->maxy
-1;
628 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
633 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
634 boolean flatshade_first
)
636 setup
->flatshade_first
= flatshade_first
;
640 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
641 boolean rasterizer_discard
)
643 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
644 setup
->rasterizer_discard
= rasterizer_discard
;
645 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
650 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
651 struct vertex_info
*vertex_info
)
653 /* XXX: just silently holding onto the pointer:
655 setup
->vertex_info
= vertex_info
;
660 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
663 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
665 struct pipe_sampler_view
**views
)
669 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
671 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
673 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
674 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
677 struct pipe_resource
*res
= view
->texture
;
678 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
679 struct lp_jit_texture
*jit_tex
;
680 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
682 /* We're referencing the texture's internal data, so save a
685 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
688 /* regular texture - setup array of mipmap level offsets */
691 unsigned first_level
= 0;
692 unsigned last_level
= 0;
694 if (llvmpipe_resource_is_texture(res
)) {
695 first_level
= view
->u
.tex
.first_level
;
696 last_level
= view
->u
.tex
.last_level
;
697 assert(first_level
<= last_level
);
698 assert(last_level
<= res
->last_level
);
701 * The complexity here is only necessary for depth textures which
704 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, first_level
,
706 LP_TEX_LAYOUT_LINEAR
);
707 jit_tex
->base
= lp_tex
->linear_img
.data
;
710 mip_ptr
= lp_tex
->data
;
711 jit_tex
->base
= mip_ptr
;
714 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
715 /* out of memory - use dummy tile memory */
716 /* Note if using PERF_TEX_MEM will also skip tile conversion */
717 jit_tex
->base
= lp_dummy_tile
;
718 jit_tex
->width
= TILE_SIZE
/8;
719 jit_tex
->height
= TILE_SIZE
/8;
721 jit_tex
->first_level
= 0;
722 jit_tex
->last_level
= 0;
723 jit_tex
->mip_offsets
[j
] = 0;
724 jit_tex
->row_stride
[j
] = 0;
725 jit_tex
->img_stride
[j
] = 0;
728 jit_tex
->width
= res
->width0
;
729 jit_tex
->height
= res
->height0
;
730 jit_tex
->depth
= res
->depth0
;
731 jit_tex
->first_level
= first_level
;
732 jit_tex
->last_level
= last_level
;
734 if (llvmpipe_resource_is_texture(res
)) {
735 for (j
= first_level
; j
<= last_level
; j
++) {
736 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
738 LP_TEX_LAYOUT_LINEAR
);
739 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
741 * could get mip offset directly but need call above to
742 * invoke tiled->linear conversion.
744 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
745 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
746 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
750 * We don't use anything like first_element (for buffers) or
751 * first_layer (for arrays), instead adjust the last_element
752 * (width) or last_layer (depth) plus the base pointer.
753 * Less parameters and faster at shader execution.
754 * XXX Could do the same for mip levels.
756 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
757 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
758 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
759 jit_tex
->base
= (uint8_t *)jit_tex
->base
+
760 view
->u
.tex
.first_layer
* lp_tex
->img_stride
[0];
761 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
762 assert(view
->u
.tex
.last_layer
< res
->array_size
);
766 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
767 /* probably don't really need to fill that out */
768 jit_tex
->mip_offsets
[0] = 0;
769 jit_tex
->row_stride
[0] = 0;
770 jit_tex
->row_stride
[0] = 0;
772 /* everything specified in number of elements here. */
773 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
774 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
776 /* XXX Unsure if we need to sanitize parameters? */
777 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
778 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
783 /* display target texture/surface */
785 * XXX: Where should this be unmapped?
787 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
788 struct sw_winsys
*winsys
= screen
->winsys
;
789 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
791 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
792 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
793 jit_tex
->mip_offsets
[0] = 0;
794 jit_tex
->width
= res
->width0
;
795 jit_tex
->height
= res
->height0
;
796 jit_tex
->depth
= res
->depth0
;
797 jit_tex
->first_level
= jit_tex
->last_level
= 0;
798 assert(jit_tex
->base
);
803 setup
->dirty
|= LP_SETUP_NEW_FS
;
808 * Called during state validation when LP_NEW_SAMPLER is set.
811 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
813 struct pipe_sampler_state
**samplers
)
817 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
819 assert(num
<= PIPE_MAX_SAMPLERS
);
821 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
822 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
825 struct lp_jit_sampler
*jit_sam
;
826 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
828 jit_sam
->min_lod
= sampler
->min_lod
;
829 jit_sam
->max_lod
= sampler
->max_lod
;
830 jit_sam
->lod_bias
= sampler
->lod_bias
;
831 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
835 setup
->dirty
|= LP_SETUP_NEW_FS
;
840 * Is the given texture referenced by any scene?
841 * Note: we have to check all scenes including any scenes currently
842 * being rendered and the current scene being built.
845 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
846 const struct pipe_resource
*texture
)
850 /* check the render targets */
851 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
852 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
853 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
855 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
856 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
859 /* check textures referenced by the scene */
860 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
861 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
862 return LP_REFERENCED_FOR_READ
;
866 return LP_UNREFERENCED
;
871 * Called by vbuf code when we're about to draw something.
874 try_update_scene_state( struct lp_setup_context
*setup
)
876 boolean new_scene
= (setup
->fs
.stored
== NULL
);
877 struct lp_scene
*scene
= setup
->scene
;
882 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
888 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
889 size
= 4 * 16 * sizeof(uint8_t);
890 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
891 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
898 /* Store floating point colour */
899 fstored
= (float*)(stored
+ 4*16);
900 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
901 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
904 /* smear each blend color component across 16 ubyte elements */
905 for (i
= 0; i
< 4; ++i
) {
906 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
907 for (j
= 0; j
< 16; ++j
)
908 stored
[i
*16 + j
] = c
;
911 setup
->blend_color
.stored
= stored
;
912 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
913 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
914 setup
->dirty
|= LP_SETUP_NEW_FS
;
917 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
918 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
919 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
920 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
921 const ubyte
*current_data
= NULL
;
924 /* resource buffer */
925 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
927 else if (setup
->constants
[i
].current
.user_buffer
) {
928 /* user-space buffer */
929 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
933 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
935 /* TODO: copy only the actually used constants? */
937 if (setup
->constants
[i
].stored_size
!= current_size
||
938 !setup
->constants
[i
].stored_data
||
939 memcmp(setup
->constants
[i
].stored_data
,
941 current_size
) != 0) {
944 stored
= lp_scene_alloc(scene
, current_size
);
953 setup
->constants
[i
].stored_size
= current_size
;
954 setup
->constants
[i
].stored_data
= stored
;
958 setup
->constants
[i
].stored_size
= 0;
959 setup
->constants
[i
].stored_data
= NULL
;
962 setup
->fs
.current
.jit_context
.constants
[i
] = setup
->constants
[i
].stored_data
;
963 setup
->dirty
|= LP_SETUP_NEW_FS
;
968 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
969 if (!setup
->fs
.stored
||
970 memcmp(setup
->fs
.stored
,
972 sizeof setup
->fs
.current
) != 0)
974 struct lp_rast_state
*stored
;
976 /* The fs state that's been stored in the scene is different from
977 * the new, current state. So allocate a new lp_rast_state object
978 * and append it to the bin's setup data buffer.
980 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
988 sizeof setup
->fs
.current
);
989 setup
->fs
.stored
= stored
;
991 /* The scene now references the textures in the rasterization
992 * state record. Note that now.
994 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
995 if (setup
->fs
.current_tex
[i
]) {
996 if (!lp_scene_add_resource_reference(scene
,
997 setup
->fs
.current_tex
[i
],
1007 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1008 setup
->draw_region
= setup
->framebuffer
;
1009 if (setup
->scissor_test
) {
1010 u_rect_possible_intersection(&setup
->scissor
,
1011 &setup
->draw_region
);
1017 assert(setup
->fs
.stored
);
1022 lp_setup_update_state( struct lp_setup_context
*setup
,
1023 boolean update_scene
)
1025 /* Some of the 'draw' pipeline stages may have changed some driver state.
1026 * Make sure we've processed those state changes before anything else.
1028 * XXX this is the only place where llvmpipe_context is used in the
1029 * setup code. This may get refactored/changed...
1032 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1034 llvmpipe_update_derived(lp
);
1037 if (lp
->setup
->dirty
) {
1038 llvmpipe_update_setup(lp
);
1041 assert(setup
->setup
.variant
);
1043 /* Will probably need to move this somewhere else, just need
1044 * to know about vertex shader point size attribute.
1046 setup
->psize
= lp
->psize_slot
;
1048 assert(lp
->dirty
== 0);
1050 assert(lp
->setup_variant
.key
.size
==
1051 setup
->setup
.variant
->key
.size
);
1053 assert(memcmp(&lp
->setup_variant
.key
,
1054 &setup
->setup
.variant
->key
,
1055 setup
->setup
.variant
->key
.size
) == 0);
1058 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1059 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1063 /* Only call into update_scene_state() if we already have a
1066 if (update_scene
&& setup
->scene
) {
1067 assert(setup
->state
== SETUP_ACTIVE
);
1069 if (try_update_scene_state(setup
))
1072 /* Update failed, try to restart the scene.
1074 * Cannot call lp_setup_flush_and_restart() directly here
1075 * because of potential recursion.
1077 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1080 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1086 return try_update_scene_state(setup
);
1094 /* Only caller is lp_setup_vbuf_destroy()
1097 lp_setup_destroy( struct lp_setup_context
*setup
)
1101 lp_setup_reset( setup
);
1103 util_unreference_framebuffer_state(&setup
->fb
);
1105 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1106 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1109 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1110 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1113 /* free the scenes in the 'empty' queue */
1114 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1115 struct lp_scene
*scene
= setup
->scenes
[i
];
1118 lp_fence_wait(scene
->fence
);
1120 lp_scene_destroy(scene
);
1123 lp_fence_reference(&setup
->last_fence
, NULL
);
1130 * Create a new primitive tiling engine. Plug it into the backend of
1131 * the draw module. Currently also creates a rasterizer to use with
1134 struct lp_setup_context
*
1135 lp_setup_create( struct pipe_context
*pipe
,
1136 struct draw_context
*draw
)
1138 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1139 struct lp_setup_context
*setup
;
1142 setup
= CALLOC_STRUCT(lp_setup_context
);
1147 lp_setup_init_vbuf(setup
);
1149 /* Used only in update_state():
1154 setup
->num_threads
= screen
->num_threads
;
1155 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1160 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1161 draw_set_render(draw
, &setup
->base
);
1163 /* create some empty scenes */
1164 for (i
= 0; i
< MAX_SCENES
; i
++) {
1165 setup
->scenes
[i
] = lp_scene_create( pipe
);
1166 if (!setup
->scenes
[i
]) {
1171 setup
->triangle
= first_triangle
;
1172 setup
->line
= first_line
;
1173 setup
->point
= first_point
;
1180 for (i
= 0; i
< MAX_SCENES
; i
++) {
1181 if (setup
->scenes
[i
]) {
1182 lp_scene_destroy(setup
->scenes
[i
]);
1186 setup
->vbuf
->destroy(setup
->vbuf
);
1195 * Put a BeginQuery command into all bins.
1198 lp_setup_begin_query(struct lp_setup_context
*setup
,
1199 struct llvmpipe_query
*pq
)
1201 /* init the query to its beginning state */
1202 assert(setup
->active_query
[pq
->type
] == NULL
);
1204 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1206 setup
->active_query
[pq
->type
] = pq
;
1208 /* XXX: It is possible that a query is created before the scene
1209 * has been created. This means that setup->scene == NULL resulting
1210 * in the query not being binned and thus is ignored.
1214 if (!lp_scene_bin_everywhere(setup
->scene
,
1215 LP_RAST_OP_BEGIN_QUERY
,
1216 lp_rast_arg_query(pq
))) {
1218 if (!lp_setup_flush_and_restart(setup
))
1221 if (!lp_scene_bin_everywhere(setup
->scene
,
1222 LP_RAST_OP_BEGIN_QUERY
,
1223 lp_rast_arg_query(pq
))) {
1232 * Put an EndQuery command into all bins.
1235 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1237 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1239 if (pq
->type
!= PIPE_QUERY_TIMESTAMP
) {
1240 assert(setup
->active_query
[pq
->type
] == pq
);
1241 setup
->active_query
[pq
->type
] = NULL
;
1244 /* Setup will automatically re-issue any query which carried over a
1245 * scene boundary, and the rasterizer automatically "ends" queries
1246 * which are active at the end of a scene, so there is no need to
1247 * retry this commands on failure.
1250 /* pq->fence should be the fence of the *last* scene which
1251 * contributed to the query result.
1253 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1255 if (!lp_scene_bin_everywhere(setup
->scene
,
1256 LP_RAST_OP_END_QUERY
,
1257 lp_rast_arg_query(pq
))) {
1258 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
1262 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1268 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1270 if (0) debug_printf("%s\n", __FUNCTION__
);
1272 assert(setup
->state
== SETUP_ACTIVE
);
1274 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1277 if (!lp_setup_update_state(setup
, TRUE
))