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 "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 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
70 boolean discard
= lp
->rasterizer
? lp
->rasterizer
->rasterizer_discard
: FALSE
;
72 assert(setup
->scene
== NULL
);
75 setup
->scene_idx
%= Elements(setup
->scenes
);
77 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
79 if (setup
->scene
->fence
) {
80 if (LP_DEBUG
& DEBUG_SETUP
)
81 debug_printf("%s: wait for scene %d\n",
82 __FUNCTION__
, setup
->scene
->fence
->id
);
84 lp_fence_wait(setup
->scene
->fence
);
87 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, discard
);
93 first_triangle( struct lp_setup_context
*setup
,
98 assert(setup
->state
== SETUP_ACTIVE
);
99 lp_setup_choose_triangle( setup
);
100 setup
->triangle( setup
, v0
, v1
, v2
);
104 first_line( struct lp_setup_context
*setup
,
105 const float (*v0
)[4],
106 const float (*v1
)[4])
108 assert(setup
->state
== SETUP_ACTIVE
);
109 lp_setup_choose_line( setup
);
110 setup
->line( setup
, v0
, v1
);
114 first_point( struct lp_setup_context
*setup
,
115 const float (*v0
)[4])
117 assert(setup
->state
== SETUP_ACTIVE
);
118 lp_setup_choose_point( setup
);
119 setup
->point( setup
, v0
);
122 void lp_setup_reset( struct lp_setup_context
*setup
)
126 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
128 /* Reset derived state */
129 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
130 setup
->constants
[i
].stored_size
= 0;
131 setup
->constants
[i
].stored_data
= NULL
;
133 setup
->fs
.stored
= NULL
;
141 memset(&setup
->clear
, 0, sizeof setup
->clear
);
143 /* Have an explicit "start-binning" call and get rid of this
146 setup
->line
= first_line
;
147 setup
->point
= first_point
;
148 setup
->triangle
= first_triangle
;
152 /** Rasterize all scene's bins */
154 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
156 struct lp_scene
*scene
= setup
->scene
;
157 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
159 scene
->num_active_queries
= setup
->active_binned_queries
;
160 memcpy(scene
->active_queries
, setup
->active_queries
,
161 scene
->num_active_queries
* sizeof(scene
->active_queries
[0]));
163 lp_scene_end_binning(scene
);
165 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
167 if (setup
->last_fence
)
168 setup
->last_fence
->issued
= TRUE
;
170 pipe_mutex_lock(screen
->rast_mutex
);
171 lp_rast_queue_scene(screen
->rast
, scene
);
172 lp_rast_finish(screen
->rast
);
173 pipe_mutex_unlock(screen
->rast_mutex
);
175 lp_scene_end_rasterization(setup
->scene
);
176 lp_setup_reset( setup
);
178 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
184 begin_binning( struct lp_setup_context
*setup
)
186 struct lp_scene
*scene
= setup
->scene
;
187 boolean need_zsload
= FALSE
;
191 assert(scene
->fence
== NULL
);
193 /* Always create a fence:
195 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
199 ok
= try_update_scene_state(setup
);
203 if (setup
->fb
.zsbuf
&&
204 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
205 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
208 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
209 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
210 need_zsload
? "clear": "load");
212 if (setup
->fb
.nr_cbufs
) {
213 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
214 ok
= lp_scene_bin_everywhere( scene
,
215 LP_RAST_OP_CLEAR_COLOR
,
216 setup
->clear
.color
);
222 if (setup
->fb
.zsbuf
) {
223 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
225 scene
->has_depthstencil_clear
= TRUE
;
227 ok
= lp_scene_bin_everywhere( scene
,
228 LP_RAST_OP_CLEAR_ZSTENCIL
,
230 setup
->clear
.zsvalue
,
231 setup
->clear
.zsmask
));
237 setup
->clear
.flags
= 0;
238 setup
->clear
.zsmask
= 0;
239 setup
->clear
.zsvalue
= 0;
241 scene
->had_queries
= !!setup
->active_binned_queries
;
243 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
248 /* This basically bins and then flushes any outstanding full-screen
251 * TODO: fast path for fullscreen clears and no triangles.
254 execute_clears( struct lp_setup_context
*setup
)
256 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
258 return begin_binning( setup
);
261 const char *states
[] = {
269 set_scene_state( struct lp_setup_context
*setup
,
270 enum setup_state new_state
,
273 unsigned old_state
= setup
->state
;
275 if (old_state
== new_state
)
278 if (LP_DEBUG
& DEBUG_SCENE
) {
279 debug_printf("%s old %s new %s%s%s\n",
283 (new_state
== SETUP_FLUSHED
) ? ": " : "",
284 (new_state
== SETUP_FLUSHED
) ? reason
: "");
286 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
287 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
290 /* wait for a free/empty scene
292 if (old_state
== SETUP_FLUSHED
)
293 lp_setup_get_empty_scene(setup
);
300 if (!begin_binning( setup
))
305 if (old_state
== SETUP_CLEARED
)
306 if (!execute_clears( setup
))
309 lp_setup_rasterize_scene( setup
);
310 assert(setup
->scene
== NULL
);
314 assert(0 && "invalid setup state mode");
318 setup
->state
= new_state
;
323 lp_scene_end_rasterization(setup
->scene
);
327 setup
->state
= SETUP_FLUSHED
;
328 lp_setup_reset( setup
);
334 lp_setup_flush( struct lp_setup_context
*setup
,
335 struct pipe_fence_handle
**fence
,
338 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
341 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
347 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
348 const struct pipe_framebuffer_state
*fb
)
350 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
352 /* Flush any old scene.
354 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
357 * Ensure the old scene is not reused.
359 assert(!setup
->scene
);
361 /* Set new state. This will be picked up later when we next need a
364 util_copy_framebuffer_state(&setup
->fb
, fb
);
365 setup
->framebuffer
.x0
= 0;
366 setup
->framebuffer
.y0
= 0;
367 setup
->framebuffer
.x1
= fb
->width
-1;
368 setup
->framebuffer
.y1
= fb
->height
-1;
369 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
374 lp_setup_try_clear( struct lp_setup_context
*setup
,
375 const union pipe_color_union
*color
,
381 uint64_t zsvalue
= 0;
382 union lp_rast_cmd_arg color_arg
;
385 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
387 if (flags
& PIPE_CLEAR_COLOR
) {
388 for (i
= 0; i
< 4; i
++)
389 color_arg
.clear_color
.i
[i
] = color
->i
[i
];
392 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
393 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
394 uint8_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
396 zsvalue
= util_pack64_z_stencil(setup
->fb
.zsbuf
->format
,
401 zsmask
= util_pack64_mask_z_stencil(setup
->fb
.zsbuf
->format
,
408 if (setup
->state
== SETUP_ACTIVE
) {
409 struct lp_scene
*scene
= setup
->scene
;
411 /* Add the clear to existing scene. In the unusual case where
412 * both color and depth-stencil are being cleared when there's
413 * already been some rendering, we could discard the currently
414 * binned scene and start again, but I don't see that as being
417 if (flags
& PIPE_CLEAR_COLOR
) {
418 if (!lp_scene_bin_everywhere( scene
,
419 LP_RAST_OP_CLEAR_COLOR
,
424 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
425 if (!lp_scene_bin_everywhere( scene
,
426 LP_RAST_OP_CLEAR_ZSTENCIL
,
427 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
432 /* Put ourselves into the 'pre-clear' state, specifically to try
433 * and accumulate multiple clears to color and depth_stencil
434 * buffers which the app or state-tracker might issue
437 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
439 setup
->clear
.flags
|= flags
;
441 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
442 setup
->clear
.zsmask
|= zsmask
;
443 setup
->clear
.zsvalue
=
444 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
447 if (flags
& PIPE_CLEAR_COLOR
) {
448 memcpy(&setup
->clear
.color
.clear_color
,
450 sizeof setup
->clear
.color
.clear_color
);
458 lp_setup_clear( struct lp_setup_context
*setup
,
459 const union pipe_color_union
*color
,
464 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
465 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
467 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
477 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
479 boolean ccw_is_frontface
,
481 boolean half_pixel_center
,
482 boolean bottom_edge_rule
)
484 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
486 setup
->ccw_is_frontface
= ccw_is_frontface
;
487 setup
->cullmode
= cull_mode
;
488 setup
->triangle
= first_triangle
;
489 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
490 setup
->bottom_edge_rule
= bottom_edge_rule
;
492 if (setup
->scissor_test
!= scissor
) {
493 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
494 setup
->scissor_test
= scissor
;
499 lp_setup_set_line_state( struct lp_setup_context
*setup
,
502 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
504 setup
->line_width
= line_width
;
508 lp_setup_set_point_state( struct lp_setup_context
*setup
,
510 boolean point_size_per_vertex
,
511 uint sprite_coord_enable
,
512 uint sprite_coord_origin
)
514 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
516 setup
->point_size
= point_size
;
517 setup
->sprite_coord_enable
= sprite_coord_enable
;
518 setup
->sprite_coord_origin
= sprite_coord_origin
;
519 setup
->point_size_per_vertex
= point_size_per_vertex
;
523 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
524 const struct lp_setup_variant
*variant
)
526 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
528 setup
->setup
.variant
= variant
;
532 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
533 struct lp_fragment_shader_variant
*variant
)
535 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
537 /* FIXME: reference count */
539 setup
->fs
.current
.variant
= variant
;
540 setup
->dirty
|= LP_SETUP_NEW_FS
;
544 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
546 struct pipe_constant_buffer
*buffers
)
550 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
552 assert(num
<= Elements(setup
->constants
));
554 for (i
= 0; i
< num
; ++i
) {
555 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
557 for (; i
< Elements(setup
->constants
); i
++) {
558 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
560 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
565 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
566 float alpha_ref_value
)
568 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
570 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
571 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
572 setup
->dirty
|= LP_SETUP_NEW_FS
;
577 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
578 const ubyte refs
[2] )
580 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
582 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
583 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
584 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
585 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
586 setup
->dirty
|= LP_SETUP_NEW_FS
;
591 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
592 const struct pipe_blend_color
*blend_color
)
594 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
598 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
599 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
600 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
606 lp_setup_set_scissors( struct lp_setup_context
*setup
,
607 const struct pipe_scissor_state
*scissors
)
610 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
614 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
615 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
616 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
617 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
618 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
620 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
625 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
626 boolean flatshade_first
)
628 setup
->flatshade_first
= flatshade_first
;
632 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
633 boolean rasterizer_discard
)
635 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
636 setup
->rasterizer_discard
= rasterizer_discard
;
637 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
642 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
643 struct vertex_info
*vertex_info
)
645 /* XXX: just silently holding onto the pointer:
647 setup
->vertex_info
= vertex_info
;
652 * Called during state validation when LP_NEW_VIEWPORT is set.
655 lp_setup_set_viewports(struct lp_setup_context
*setup
,
656 unsigned num_viewports
,
657 const struct pipe_viewport_state
*viewports
)
659 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
662 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
664 assert(num_viewports
<= PIPE_MAX_VIEWPORTS
);
668 * For use in lp_state_fs.c, propagate the viewport values for all viewports.
670 for (i
= 0; i
< num_viewports
; i
++) {
674 if (lp
->rasterizer
->clip_halfz
== 0) {
675 float half_depth
= viewports
[i
].scale
[2];
676 min_depth
= viewports
[i
].translate
[2] - half_depth
;
677 max_depth
= min_depth
+ half_depth
* 2.0f
;
679 min_depth
= viewports
[i
].translate
[2];
680 max_depth
= min_depth
+ viewports
[i
].scale
[2];
683 if (setup
->viewports
[i
].min_depth
!= min_depth
||
684 setup
->viewports
[i
].max_depth
!= max_depth
) {
685 setup
->viewports
[i
].min_depth
= min_depth
;
686 setup
->viewports
[i
].max_depth
= max_depth
;
687 setup
->dirty
|= LP_SETUP_NEW_VIEWPORTS
;
694 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
697 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
699 struct pipe_sampler_view
**views
)
703 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
705 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
707 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
708 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
711 struct pipe_resource
*res
= view
->texture
;
712 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
713 struct lp_jit_texture
*jit_tex
;
714 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
716 /* We're referencing the texture's internal data, so save a
719 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
722 /* regular texture - setup array of mipmap level offsets */
725 unsigned first_level
= 0;
726 unsigned last_level
= 0;
728 if (llvmpipe_resource_is_texture(res
)) {
729 first_level
= view
->u
.tex
.first_level
;
730 last_level
= view
->u
.tex
.last_level
;
731 assert(first_level
<= last_level
);
732 assert(last_level
<= res
->last_level
);
735 * The complexity here should no longer be necessary.
737 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, first_level
,
739 jit_tex
->base
= lp_tex
->linear_img
.data
;
742 mip_ptr
= lp_tex
->data
;
743 jit_tex
->base
= mip_ptr
;
746 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
747 /* out of memory - use dummy tile memory */
748 /* Note if using PERF_TEX_MEM will also skip tile conversion */
749 jit_tex
->base
= lp_dummy_tile
;
750 jit_tex
->width
= TILE_SIZE
/8;
751 jit_tex
->height
= TILE_SIZE
/8;
753 jit_tex
->first_level
= 0;
754 jit_tex
->last_level
= 0;
755 jit_tex
->mip_offsets
[0] = 0;
756 jit_tex
->row_stride
[0] = 0;
757 jit_tex
->img_stride
[0] = 0;
760 jit_tex
->width
= res
->width0
;
761 jit_tex
->height
= res
->height0
;
762 jit_tex
->depth
= res
->depth0
;
763 jit_tex
->first_level
= first_level
;
764 jit_tex
->last_level
= last_level
;
766 if (llvmpipe_resource_is_texture(res
)) {
767 for (j
= first_level
; j
<= last_level
; j
++) {
768 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
770 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
772 * could get mip offset directly but need call above to
773 * invoke tiled->linear conversion.
775 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
776 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
777 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
780 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
781 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
783 * For array textures, we don't have first_layer, instead
784 * adjust last_layer (stored as depth) plus the mip level offsets
785 * (as we have mip-first layout can't just adjust base ptr).
786 * XXX For mip levels, could do something similar.
788 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
789 for (j
= first_level
; j
<= last_level
; j
++) {
790 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
791 lp_tex
->img_stride
[j
];
793 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
794 assert(view
->u
.tex
.last_layer
< res
->array_size
);
799 * For buffers, we don't have first_element, instead adjust
800 * last_element (stored as width) plus the base pointer.
802 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
803 /* probably don't really need to fill that out */
804 jit_tex
->mip_offsets
[0] = 0;
805 jit_tex
->row_stride
[0] = 0;
806 jit_tex
->row_stride
[0] = 0;
808 /* everything specified in number of elements here. */
809 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
810 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
812 /* XXX Unsure if we need to sanitize parameters? */
813 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
814 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
819 /* display target texture/surface */
821 * XXX: Where should this be unmapped?
823 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
824 struct sw_winsys
*winsys
= screen
->winsys
;
825 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
827 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
828 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
829 jit_tex
->mip_offsets
[0] = 0;
830 jit_tex
->width
= res
->width0
;
831 jit_tex
->height
= res
->height0
;
832 jit_tex
->depth
= res
->depth0
;
833 jit_tex
->first_level
= jit_tex
->last_level
= 0;
834 assert(jit_tex
->base
);
839 setup
->dirty
|= LP_SETUP_NEW_FS
;
844 * Called during state validation when LP_NEW_SAMPLER is set.
847 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
849 struct pipe_sampler_state
**samplers
)
853 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
855 assert(num
<= PIPE_MAX_SAMPLERS
);
857 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
858 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
861 struct lp_jit_sampler
*jit_sam
;
862 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
864 jit_sam
->min_lod
= sampler
->min_lod
;
865 jit_sam
->max_lod
= sampler
->max_lod
;
866 jit_sam
->lod_bias
= sampler
->lod_bias
;
867 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
871 setup
->dirty
|= LP_SETUP_NEW_FS
;
876 * Is the given texture referenced by any scene?
877 * Note: we have to check all scenes including any scenes currently
878 * being rendered and the current scene being built.
881 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
882 const struct pipe_resource
*texture
)
886 /* check the render targets */
887 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
888 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
889 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
891 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
892 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
895 /* check textures referenced by the scene */
896 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
897 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
898 return LP_REFERENCED_FOR_READ
;
902 return LP_UNREFERENCED
;
907 * Called by vbuf code when we're about to draw something.
909 * This function stores all dirty state in the current scene's display list
910 * memory, via lp_scene_alloc(). We can not pass pointers of mutable state to
911 * the JIT functions, as the JIT functions will be called later on, most likely
912 * on a different thread.
914 * When processing dirty state it is imperative that we don't refer to any
915 * pointers previously allocated with lp_scene_alloc() in this function (or any
916 * function) as they may belong to a scene freed since then.
919 try_update_scene_state( struct lp_setup_context
*setup
)
921 boolean new_scene
= (setup
->fs
.stored
== NULL
);
922 struct lp_scene
*scene
= setup
->scene
;
927 if (setup
->dirty
& LP_SETUP_NEW_VIEWPORTS
) {
929 * Record new depth range state for changes due to viewport updates.
931 * TODO: Collapse the existing viewport and depth range information
932 * into one structure, for access by JIT.
934 struct lp_jit_viewport
*stored
;
936 stored
= (struct lp_jit_viewport
*)
937 lp_scene_alloc(scene
, sizeof setup
->viewports
);
944 memcpy(stored
, setup
->viewports
, sizeof setup
->viewports
);
946 setup
->fs
.current
.jit_context
.viewports
= stored
;
947 setup
->dirty
|= LP_SETUP_NEW_FS
;
950 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
956 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
957 size
= 4 * 16 * sizeof(uint8_t);
958 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
959 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
966 /* Store floating point colour */
967 fstored
= (float*)(stored
+ 4*16);
968 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
969 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
972 /* smear each blend color component across 16 ubyte elements */
973 for (i
= 0; i
< 4; ++i
) {
974 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
975 for (j
= 0; j
< 16; ++j
)
976 stored
[i
*16 + j
] = c
;
979 setup
->blend_color
.stored
= stored
;
980 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
981 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
982 setup
->dirty
|= LP_SETUP_NEW_FS
;
985 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
986 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
987 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
988 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
989 const ubyte
*current_data
= NULL
;
992 /* resource buffer */
993 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
995 else if (setup
->constants
[i
].current
.user_buffer
) {
996 /* user-space buffer */
997 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
1001 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
1003 /* TODO: copy only the actually used constants? */
1005 if (setup
->constants
[i
].stored_size
!= current_size
||
1006 !setup
->constants
[i
].stored_data
||
1007 memcmp(setup
->constants
[i
].stored_data
,
1009 current_size
) != 0) {
1012 stored
= lp_scene_alloc(scene
, current_size
);
1021 setup
->constants
[i
].stored_size
= current_size
;
1022 setup
->constants
[i
].stored_data
= stored
;
1026 setup
->constants
[i
].stored_size
= 0;
1027 setup
->constants
[i
].stored_data
= NULL
;
1030 setup
->fs
.current
.jit_context
.constants
[i
] = setup
->constants
[i
].stored_data
;
1031 setup
->dirty
|= LP_SETUP_NEW_FS
;
1036 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
1037 if (!setup
->fs
.stored
||
1038 memcmp(setup
->fs
.stored
,
1040 sizeof setup
->fs
.current
) != 0)
1042 struct lp_rast_state
*stored
;
1044 /* The fs state that's been stored in the scene is different from
1045 * the new, current state. So allocate a new lp_rast_state object
1046 * and append it to the bin's setup data buffer.
1048 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
1056 sizeof setup
->fs
.current
);
1057 setup
->fs
.stored
= stored
;
1059 /* The scene now references the textures in the rasterization
1060 * state record. Note that now.
1062 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1063 if (setup
->fs
.current_tex
[i
]) {
1064 if (!lp_scene_add_resource_reference(scene
,
1065 setup
->fs
.current_tex
[i
],
1075 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1077 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1078 setup
->draw_regions
[i
] = setup
->framebuffer
;
1079 if (setup
->scissor_test
) {
1080 u_rect_possible_intersection(&setup
->scissors
[i
],
1081 &setup
->draw_regions
[i
]);
1085 * Subdivide triangles if the framebuffer is larger than the
1088 setup
->subdivide_large_triangles
= (setup
->fb
.width
> MAX_FIXED_LENGTH
||
1089 setup
->fb
.height
> MAX_FIXED_LENGTH
);
1094 assert(setup
->fs
.stored
);
1099 lp_setup_update_state( struct lp_setup_context
*setup
,
1100 boolean update_scene
)
1102 /* Some of the 'draw' pipeline stages may have changed some driver state.
1103 * Make sure we've processed those state changes before anything else.
1105 * XXX this is the only place where llvmpipe_context is used in the
1106 * setup code. This may get refactored/changed...
1109 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1111 llvmpipe_update_derived(lp
);
1114 if (lp
->setup
->dirty
) {
1115 llvmpipe_update_setup(lp
);
1118 assert(setup
->setup
.variant
);
1120 /* Will probably need to move this somewhere else, just need
1121 * to know about vertex shader point size attribute.
1123 setup
->psize
= lp
->psize_slot
;
1124 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1125 setup
->layer_slot
= lp
->layer_slot
;
1126 setup
->face_slot
= lp
->face_slot
;
1128 assert(lp
->dirty
== 0);
1130 assert(lp
->setup_variant
.key
.size
==
1131 setup
->setup
.variant
->key
.size
);
1133 assert(memcmp(&lp
->setup_variant
.key
,
1134 &setup
->setup
.variant
->key
,
1135 setup
->setup
.variant
->key
.size
) == 0);
1138 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1139 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1143 /* Only call into update_scene_state() if we already have a
1146 if (update_scene
&& setup
->scene
) {
1147 assert(setup
->state
== SETUP_ACTIVE
);
1149 if (try_update_scene_state(setup
))
1152 /* Update failed, try to restart the scene.
1154 * Cannot call lp_setup_flush_and_restart() directly here
1155 * because of potential recursion.
1157 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1160 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1166 return try_update_scene_state(setup
);
1174 /* Only caller is lp_setup_vbuf_destroy()
1177 lp_setup_destroy( struct lp_setup_context
*setup
)
1181 lp_setup_reset( setup
);
1183 util_unreference_framebuffer_state(&setup
->fb
);
1185 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1186 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1189 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1190 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1193 /* free the scenes in the 'empty' queue */
1194 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1195 struct lp_scene
*scene
= setup
->scenes
[i
];
1198 lp_fence_wait(scene
->fence
);
1200 lp_scene_destroy(scene
);
1203 lp_fence_reference(&setup
->last_fence
, NULL
);
1210 * Create a new primitive tiling engine. Plug it into the backend of
1211 * the draw module. Currently also creates a rasterizer to use with
1214 struct lp_setup_context
*
1215 lp_setup_create( struct pipe_context
*pipe
,
1216 struct draw_context
*draw
)
1218 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1219 struct lp_setup_context
*setup
;
1222 setup
= CALLOC_STRUCT(lp_setup_context
);
1227 lp_setup_init_vbuf(setup
);
1229 /* Used only in update_state():
1234 setup
->num_threads
= screen
->num_threads
;
1235 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1240 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1241 draw_set_render(draw
, &setup
->base
);
1243 /* create some empty scenes */
1244 for (i
= 0; i
< MAX_SCENES
; i
++) {
1245 setup
->scenes
[i
] = lp_scene_create( pipe
);
1246 if (!setup
->scenes
[i
]) {
1251 setup
->triangle
= first_triangle
;
1252 setup
->line
= first_line
;
1253 setup
->point
= first_point
;
1260 for (i
= 0; i
< MAX_SCENES
; i
++) {
1261 if (setup
->scenes
[i
]) {
1262 lp_scene_destroy(setup
->scenes
[i
]);
1266 setup
->vbuf
->destroy(setup
->vbuf
);
1275 * Put a BeginQuery command into all bins.
1278 lp_setup_begin_query(struct lp_setup_context
*setup
,
1279 struct llvmpipe_query
*pq
)
1282 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1284 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1285 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1286 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1289 /* init the query to its beginning state */
1290 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1291 /* exceeding list size so just ignore the query */
1292 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1295 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1296 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1297 setup
->active_binned_queries
++;
1299 assert(setup
->scene
);
1301 if (!lp_scene_bin_everywhere(setup
->scene
,
1302 LP_RAST_OP_BEGIN_QUERY
,
1303 lp_rast_arg_query(pq
))) {
1305 if (!lp_setup_flush_and_restart(setup
))
1308 if (!lp_scene_bin_everywhere(setup
->scene
,
1309 LP_RAST_OP_BEGIN_QUERY
,
1310 lp_rast_arg_query(pq
))) {
1314 setup
->scene
->had_queries
|= TRUE
;
1320 * Put an EndQuery command into all bins.
1323 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1325 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1327 assert(setup
->scene
);
1329 /* pq->fence should be the fence of the *last* scene which
1330 * contributed to the query result.
1332 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1334 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1335 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1336 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1337 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1338 if (pq
->type
== PIPE_QUERY_TIMESTAMP
&&
1339 !(setup
->scene
->tiles_x
| setup
->scene
->tiles_y
)) {
1341 * If there's a zero width/height framebuffer, there's no bins and
1342 * hence no rast task is ever run. So fill in something here instead.
1344 pq
->end
[0] = os_time_get_nano();
1347 if (!lp_scene_bin_everywhere(setup
->scene
,
1348 LP_RAST_OP_END_QUERY
,
1349 lp_rast_arg_query(pq
))) {
1350 if (!lp_setup_flush_and_restart(setup
))
1353 if (!lp_scene_bin_everywhere(setup
->scene
,
1354 LP_RAST_OP_END_QUERY
,
1355 lp_rast_arg_query(pq
))) {
1359 setup
->scene
->had_queries
|= TRUE
;
1363 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1367 /* Need to do this now not earlier since it still needs to be marked as
1368 * active when binning it would cause a flush.
1370 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1371 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1372 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1375 /* remove from active binned query list */
1376 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1377 if (setup
->active_queries
[i
] == pq
)
1380 assert(i
< setup
->active_binned_queries
);
1381 if (i
== setup
->active_binned_queries
)
1383 setup
->active_binned_queries
--;
1384 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1385 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1391 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1393 if (0) debug_printf("%s\n", __FUNCTION__
);
1395 assert(setup
->state
== SETUP_ACTIVE
);
1397 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1400 if (!lp_setup_update_state(setup
, TRUE
))