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 ok
= try_update_scene_state(setup
);
199 if (setup
->fb
.zsbuf
&&
200 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
201 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
204 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
205 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
206 need_zsload
? "clear": "load");
208 if (setup
->fb
.nr_cbufs
) {
209 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
210 ok
= lp_scene_bin_everywhere( scene
,
211 LP_RAST_OP_CLEAR_COLOR
,
212 setup
->clear
.color
);
218 if (setup
->fb
.zsbuf
) {
219 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
221 scene
->has_depthstencil_clear
= TRUE
;
223 ok
= lp_scene_bin_everywhere( scene
,
224 LP_RAST_OP_CLEAR_ZSTENCIL
,
226 setup
->clear
.zsvalue
,
227 setup
->clear
.zsmask
));
233 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
234 if (setup
->active_query
[i
]) {
235 ok
= lp_scene_bin_everywhere( scene
,
236 LP_RAST_OP_BEGIN_QUERY
,
237 lp_rast_arg_query(setup
->active_query
[i
]) );
243 setup
->clear
.flags
= 0;
244 setup
->clear
.zsmask
= 0;
245 setup
->clear
.zsvalue
= 0;
247 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
252 /* This basically bins and then flushes any outstanding full-screen
255 * TODO: fast path for fullscreen clears and no triangles.
258 execute_clears( struct lp_setup_context
*setup
)
260 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
262 return begin_binning( setup
);
265 const char *states
[] = {
273 set_scene_state( struct lp_setup_context
*setup
,
274 enum setup_state new_state
,
277 unsigned old_state
= setup
->state
;
279 if (old_state
== new_state
)
282 if (LP_DEBUG
& DEBUG_SCENE
) {
283 debug_printf("%s old %s new %s%s%s\n",
287 (new_state
== SETUP_FLUSHED
) ? ": " : "",
288 (new_state
== SETUP_FLUSHED
) ? reason
: "");
290 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
291 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
294 /* wait for a free/empty scene
296 if (old_state
== SETUP_FLUSHED
)
297 lp_setup_get_empty_scene(setup
);
304 if (!begin_binning( setup
))
309 if (old_state
== SETUP_CLEARED
)
310 if (!execute_clears( setup
))
313 lp_setup_rasterize_scene( setup
);
314 assert(setup
->scene
== NULL
);
318 assert(0 && "invalid setup state mode");
322 setup
->state
= new_state
;
327 lp_scene_end_rasterization(setup
->scene
);
331 setup
->state
= SETUP_FLUSHED
;
332 lp_setup_reset( setup
);
338 lp_setup_flush( struct lp_setup_context
*setup
,
339 struct pipe_fence_handle
**fence
,
342 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
345 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
351 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
352 const struct pipe_framebuffer_state
*fb
)
354 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
356 /* Flush any old scene.
358 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
361 * Ensure the old scene is not reused.
363 assert(!setup
->scene
);
365 /* Set new state. This will be picked up later when we next need a
368 util_copy_framebuffer_state(&setup
->fb
, fb
);
369 setup
->framebuffer
.x0
= 0;
370 setup
->framebuffer
.y0
= 0;
371 setup
->framebuffer
.x1
= fb
->width
-1;
372 setup
->framebuffer
.y1
= fb
->height
-1;
373 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
378 lp_setup_try_clear( struct lp_setup_context
*setup
,
379 const union pipe_color_union
*color
,
385 uint64_t zsvalue
= 0;
386 union lp_rast_cmd_arg color_arg
;
389 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
391 if (flags
& PIPE_CLEAR_COLOR
) {
392 for (i
= 0; i
< 4; i
++)
393 color_arg
.clear_color
.i
[i
] = color
->i
[i
];
396 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
397 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
398 uint8_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
400 zsvalue
= util_pack64_z_stencil(setup
->fb
.zsbuf
->format
,
405 zsmask
= util_pack64_mask_z_stencil(setup
->fb
.zsbuf
->format
,
412 if (setup
->state
== SETUP_ACTIVE
) {
413 struct lp_scene
*scene
= setup
->scene
;
415 /* Add the clear to existing scene. In the unusual case where
416 * both color and depth-stencil are being cleared when there's
417 * already been some rendering, we could discard the currently
418 * binned scene and start again, but I don't see that as being
421 if (flags
& PIPE_CLEAR_COLOR
) {
422 if (!lp_scene_bin_everywhere( scene
,
423 LP_RAST_OP_CLEAR_COLOR
,
428 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
429 if (!lp_scene_bin_everywhere( scene
,
430 LP_RAST_OP_CLEAR_ZSTENCIL
,
431 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
436 /* Put ourselves into the 'pre-clear' state, specifically to try
437 * and accumulate multiple clears to color and depth_stencil
438 * buffers which the app or state-tracker might issue
441 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
443 setup
->clear
.flags
|= flags
;
445 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
446 setup
->clear
.zsmask
|= zsmask
;
447 setup
->clear
.zsvalue
=
448 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
451 if (flags
& PIPE_CLEAR_COLOR
) {
452 memcpy(&setup
->clear
.color
.clear_color
,
454 sizeof setup
->clear
.color
.clear_color
);
462 lp_setup_clear( struct lp_setup_context
*setup
,
463 const union pipe_color_union
*color
,
468 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
469 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
471 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
481 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
483 boolean ccw_is_frontface
,
485 boolean half_pixel_center
,
486 boolean bottom_edge_rule
)
488 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
490 setup
->ccw_is_frontface
= ccw_is_frontface
;
491 setup
->cullmode
= cull_mode
;
492 setup
->triangle
= first_triangle
;
493 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
494 setup
->bottom_edge_rule
= bottom_edge_rule
;
496 if (setup
->scissor_test
!= scissor
) {
497 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
498 setup
->scissor_test
= scissor
;
503 lp_setup_set_line_state( struct lp_setup_context
*setup
,
506 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
508 setup
->line_width
= line_width
;
512 lp_setup_set_point_state( struct lp_setup_context
*setup
,
514 boolean point_size_per_vertex
,
515 uint sprite_coord_enable
,
516 uint sprite_coord_origin
)
518 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
520 setup
->point_size
= point_size
;
521 setup
->sprite_coord_enable
= sprite_coord_enable
;
522 setup
->sprite_coord_origin
= sprite_coord_origin
;
523 setup
->point_size_per_vertex
= point_size_per_vertex
;
527 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
528 const struct lp_setup_variant
*variant
)
530 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
532 setup
->setup
.variant
= variant
;
536 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
537 struct lp_fragment_shader_variant
*variant
)
539 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
541 /* FIXME: reference count */
543 setup
->fs
.current
.variant
= variant
;
544 setup
->dirty
|= LP_SETUP_NEW_FS
;
548 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
550 struct pipe_constant_buffer
*buffers
)
554 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
556 assert(num
<= Elements(setup
->constants
));
558 for (i
= 0; i
< num
; ++i
) {
559 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
561 for (; i
< Elements(setup
->constants
); i
++) {
562 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
564 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
569 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
570 float alpha_ref_value
)
572 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
574 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
575 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
576 setup
->dirty
|= LP_SETUP_NEW_FS
;
581 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
582 const ubyte refs
[2] )
584 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
586 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
587 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
588 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
589 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
590 setup
->dirty
|= LP_SETUP_NEW_FS
;
595 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
596 const struct pipe_blend_color
*blend_color
)
598 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
602 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
603 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
604 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
610 lp_setup_set_scissors( struct lp_setup_context
*setup
,
611 const struct pipe_scissor_state
*scissors
)
614 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
618 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
619 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
620 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
621 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
622 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
624 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
629 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
630 boolean flatshade_first
)
632 setup
->flatshade_first
= flatshade_first
;
636 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
637 boolean rasterizer_discard
)
639 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
640 setup
->rasterizer_discard
= rasterizer_discard
;
641 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
646 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
647 struct vertex_info
*vertex_info
)
649 /* XXX: just silently holding onto the pointer:
651 setup
->vertex_info
= vertex_info
;
656 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
659 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
661 struct pipe_sampler_view
**views
)
665 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
667 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
669 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
670 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
673 struct pipe_resource
*res
= view
->texture
;
674 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
675 struct lp_jit_texture
*jit_tex
;
676 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
678 /* We're referencing the texture's internal data, so save a
681 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
684 /* regular texture - setup array of mipmap level offsets */
687 unsigned first_level
= 0;
688 unsigned last_level
= 0;
690 if (llvmpipe_resource_is_texture(res
)) {
691 first_level
= view
->u
.tex
.first_level
;
692 last_level
= view
->u
.tex
.last_level
;
693 assert(first_level
<= last_level
);
694 assert(last_level
<= res
->last_level
);
697 * The complexity here should no longer be necessary.
699 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, first_level
,
701 jit_tex
->base
= lp_tex
->linear_img
.data
;
704 mip_ptr
= lp_tex
->data
;
705 jit_tex
->base
= mip_ptr
;
708 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
709 /* out of memory - use dummy tile memory */
710 /* Note if using PERF_TEX_MEM will also skip tile conversion */
711 jit_tex
->base
= lp_dummy_tile
;
712 jit_tex
->width
= TILE_SIZE
/8;
713 jit_tex
->height
= TILE_SIZE
/8;
715 jit_tex
->first_level
= 0;
716 jit_tex
->last_level
= 0;
717 jit_tex
->mip_offsets
[0] = 0;
718 jit_tex
->row_stride
[0] = 0;
719 jit_tex
->img_stride
[0] = 0;
722 jit_tex
->width
= res
->width0
;
723 jit_tex
->height
= res
->height0
;
724 jit_tex
->depth
= res
->depth0
;
725 jit_tex
->first_level
= first_level
;
726 jit_tex
->last_level
= last_level
;
728 if (llvmpipe_resource_is_texture(res
)) {
729 for (j
= first_level
; j
<= last_level
; j
++) {
730 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
732 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
734 * could get mip offset directly but need call above to
735 * invoke tiled->linear conversion.
737 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
738 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
739 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
742 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
743 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
745 * For array textures, we don't have first_layer, instead
746 * adjust last_layer (stored as depth) plus the mip level offsets
747 * (as we have mip-first layout can't just adjust base ptr).
748 * XXX For mip levels, could do something similar.
750 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
751 for (j
= first_level
; j
<= last_level
; j
++) {
752 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
753 lp_tex
->img_stride
[j
];
755 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
756 assert(view
->u
.tex
.last_layer
< res
->array_size
);
761 * For buffers, we don't have first_element, instead adjust
762 * last_element (stored as width) plus the base pointer.
764 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
765 /* probably don't really need to fill that out */
766 jit_tex
->mip_offsets
[0] = 0;
767 jit_tex
->row_stride
[0] = 0;
768 jit_tex
->row_stride
[0] = 0;
770 /* everything specified in number of elements here. */
771 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
772 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
774 /* XXX Unsure if we need to sanitize parameters? */
775 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
776 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
781 /* display target texture/surface */
783 * XXX: Where should this be unmapped?
785 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
786 struct sw_winsys
*winsys
= screen
->winsys
;
787 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
789 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
790 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
791 jit_tex
->mip_offsets
[0] = 0;
792 jit_tex
->width
= res
->width0
;
793 jit_tex
->height
= res
->height0
;
794 jit_tex
->depth
= res
->depth0
;
795 jit_tex
->first_level
= jit_tex
->last_level
= 0;
796 assert(jit_tex
->base
);
801 setup
->dirty
|= LP_SETUP_NEW_FS
;
806 * Called during state validation when LP_NEW_SAMPLER is set.
809 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
811 struct pipe_sampler_state
**samplers
)
815 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
817 assert(num
<= PIPE_MAX_SAMPLERS
);
819 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
820 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
823 struct lp_jit_sampler
*jit_sam
;
824 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
826 jit_sam
->min_lod
= sampler
->min_lod
;
827 jit_sam
->max_lod
= sampler
->max_lod
;
828 jit_sam
->lod_bias
= sampler
->lod_bias
;
829 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
833 setup
->dirty
|= LP_SETUP_NEW_FS
;
838 * Is the given texture referenced by any scene?
839 * Note: we have to check all scenes including any scenes currently
840 * being rendered and the current scene being built.
843 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
844 const struct pipe_resource
*texture
)
848 /* check the render targets */
849 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
850 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
851 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
853 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
854 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
857 /* check textures referenced by the scene */
858 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
859 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
860 return LP_REFERENCED_FOR_READ
;
864 return LP_UNREFERENCED
;
869 * Called by vbuf code when we're about to draw something.
872 try_update_scene_state( struct lp_setup_context
*setup
)
874 boolean new_scene
= (setup
->fs
.stored
== NULL
);
875 struct lp_scene
*scene
= setup
->scene
;
880 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
886 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
887 size
= 4 * 16 * sizeof(uint8_t);
888 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
889 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
896 /* Store floating point colour */
897 fstored
= (float*)(stored
+ 4*16);
898 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
899 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
902 /* smear each blend color component across 16 ubyte elements */
903 for (i
= 0; i
< 4; ++i
) {
904 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
905 for (j
= 0; j
< 16; ++j
)
906 stored
[i
*16 + j
] = c
;
909 setup
->blend_color
.stored
= stored
;
910 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
911 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
912 setup
->dirty
|= LP_SETUP_NEW_FS
;
915 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
916 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
917 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
918 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
919 const ubyte
*current_data
= NULL
;
922 /* resource buffer */
923 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
925 else if (setup
->constants
[i
].current
.user_buffer
) {
926 /* user-space buffer */
927 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
931 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
933 /* TODO: copy only the actually used constants? */
935 if (setup
->constants
[i
].stored_size
!= current_size
||
936 !setup
->constants
[i
].stored_data
||
937 memcmp(setup
->constants
[i
].stored_data
,
939 current_size
) != 0) {
942 stored
= lp_scene_alloc(scene
, current_size
);
951 setup
->constants
[i
].stored_size
= current_size
;
952 setup
->constants
[i
].stored_data
= stored
;
956 setup
->constants
[i
].stored_size
= 0;
957 setup
->constants
[i
].stored_data
= NULL
;
960 setup
->fs
.current
.jit_context
.constants
[i
] = setup
->constants
[i
].stored_data
;
961 setup
->dirty
|= LP_SETUP_NEW_FS
;
966 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
967 if (!setup
->fs
.stored
||
968 memcmp(setup
->fs
.stored
,
970 sizeof setup
->fs
.current
) != 0)
972 struct lp_rast_state
*stored
;
974 /* The fs state that's been stored in the scene is different from
975 * the new, current state. So allocate a new lp_rast_state object
976 * and append it to the bin's setup data buffer.
978 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
986 sizeof setup
->fs
.current
);
987 setup
->fs
.stored
= stored
;
989 /* The scene now references the textures in the rasterization
990 * state record. Note that now.
992 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
993 if (setup
->fs
.current_tex
[i
]) {
994 if (!lp_scene_add_resource_reference(scene
,
995 setup
->fs
.current_tex
[i
],
1005 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1007 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1008 setup
->draw_regions
[i
] = setup
->framebuffer
;
1009 if (setup
->scissor_test
) {
1010 u_rect_possible_intersection(&setup
->scissors
[i
],
1011 &setup
->draw_regions
[i
]);
1014 /* If the framebuffer is large we have to think about fixed-point
1015 * integer overflow. For 2K by 2K images, coordinates need 15 bits
1016 * (2^11 + 4 subpixel bits). The product of two such numbers would
1017 * use 30 bits. Any larger and we could overflow a 32-bit int.
1019 * To cope with this problem we check if triangles are large and
1020 * subdivide them if needed.
1022 setup
->subdivide_large_triangles
= (setup
->fb
.width
> 2048 &&
1023 setup
->fb
.height
> 2048);
1028 assert(setup
->fs
.stored
);
1033 lp_setup_update_state( struct lp_setup_context
*setup
,
1034 boolean update_scene
)
1036 /* Some of the 'draw' pipeline stages may have changed some driver state.
1037 * Make sure we've processed those state changes before anything else.
1039 * XXX this is the only place where llvmpipe_context is used in the
1040 * setup code. This may get refactored/changed...
1043 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1045 llvmpipe_update_derived(lp
);
1048 if (lp
->setup
->dirty
) {
1049 llvmpipe_update_setup(lp
);
1052 assert(setup
->setup
.variant
);
1054 /* Will probably need to move this somewhere else, just need
1055 * to know about vertex shader point size attribute.
1057 setup
->psize
= lp
->psize_slot
;
1058 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1059 setup
->layer_slot
= lp
->layer_slot
;
1061 assert(lp
->dirty
== 0);
1063 assert(lp
->setup_variant
.key
.size
==
1064 setup
->setup
.variant
->key
.size
);
1066 assert(memcmp(&lp
->setup_variant
.key
,
1067 &setup
->setup
.variant
->key
,
1068 setup
->setup
.variant
->key
.size
) == 0);
1071 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1072 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1076 /* Only call into update_scene_state() if we already have a
1079 if (update_scene
&& setup
->scene
) {
1080 assert(setup
->state
== SETUP_ACTIVE
);
1082 if (try_update_scene_state(setup
))
1085 /* Update failed, try to restart the scene.
1087 * Cannot call lp_setup_flush_and_restart() directly here
1088 * because of potential recursion.
1090 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1093 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1099 return try_update_scene_state(setup
);
1107 /* Only caller is lp_setup_vbuf_destroy()
1110 lp_setup_destroy( struct lp_setup_context
*setup
)
1114 lp_setup_reset( setup
);
1116 util_unreference_framebuffer_state(&setup
->fb
);
1118 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1119 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1122 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1123 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1126 /* free the scenes in the 'empty' queue */
1127 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1128 struct lp_scene
*scene
= setup
->scenes
[i
];
1131 lp_fence_wait(scene
->fence
);
1133 lp_scene_destroy(scene
);
1136 lp_fence_reference(&setup
->last_fence
, NULL
);
1143 * Create a new primitive tiling engine. Plug it into the backend of
1144 * the draw module. Currently also creates a rasterizer to use with
1147 struct lp_setup_context
*
1148 lp_setup_create( struct pipe_context
*pipe
,
1149 struct draw_context
*draw
)
1151 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1152 struct lp_setup_context
*setup
;
1155 setup
= CALLOC_STRUCT(lp_setup_context
);
1160 lp_setup_init_vbuf(setup
);
1162 /* Used only in update_state():
1167 setup
->num_threads
= screen
->num_threads
;
1168 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1173 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1174 draw_set_render(draw
, &setup
->base
);
1176 /* create some empty scenes */
1177 for (i
= 0; i
< MAX_SCENES
; i
++) {
1178 setup
->scenes
[i
] = lp_scene_create( pipe
);
1179 if (!setup
->scenes
[i
]) {
1184 setup
->triangle
= first_triangle
;
1185 setup
->line
= first_line
;
1186 setup
->point
= first_point
;
1193 for (i
= 0; i
< MAX_SCENES
; i
++) {
1194 if (setup
->scenes
[i
]) {
1195 lp_scene_destroy(setup
->scenes
[i
]);
1199 setup
->vbuf
->destroy(setup
->vbuf
);
1208 * Put a BeginQuery command into all bins.
1211 lp_setup_begin_query(struct lp_setup_context
*setup
,
1212 struct llvmpipe_query
*pq
)
1214 /* init the query to its beginning state */
1215 assert(setup
->active_query
[pq
->type
] == NULL
);
1217 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1219 setup
->active_query
[pq
->type
] = pq
;
1221 /* XXX: It is possible that a query is created before the scene
1222 * has been created. This means that setup->scene == NULL resulting
1223 * in the query not being binned and thus is ignored.
1227 if (!lp_scene_bin_everywhere(setup
->scene
,
1228 LP_RAST_OP_BEGIN_QUERY
,
1229 lp_rast_arg_query(pq
))) {
1231 if (!lp_setup_flush_and_restart(setup
))
1234 if (!lp_scene_bin_everywhere(setup
->scene
,
1235 LP_RAST_OP_BEGIN_QUERY
,
1236 lp_rast_arg_query(pq
))) {
1245 * Put an EndQuery command into all bins.
1248 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1250 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1252 if (pq
->type
!= PIPE_QUERY_TIMESTAMP
&& pq
->type
!= PIPE_QUERY_GPU_FINISHED
) {
1253 assert(setup
->active_query
[pq
->type
] == pq
);
1254 setup
->active_query
[pq
->type
] = NULL
;
1257 /* Setup will automatically re-issue any query which carried over a
1258 * scene boundary, and the rasterizer automatically "ends" queries
1259 * which are active at the end of a scene, so there is no need to
1260 * retry this commands on failure.
1263 /* pq->fence should be the fence of the *last* scene which
1264 * contributed to the query result.
1266 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1268 if (!lp_scene_bin_everywhere(setup
->scene
,
1269 LP_RAST_OP_END_QUERY
,
1270 lp_rast_arg_query(pq
))) {
1271 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
1275 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1281 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1283 if (0) debug_printf("%s\n", __FUNCTION__
);
1285 assert(setup
->state
== SETUP_ACTIVE
);
1287 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1290 if (!lp_setup_update_state(setup
, TRUE
))