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 assert(setup
->scene
== NULL
);
71 setup
->scene_idx
%= Elements(setup
->scenes
);
73 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
75 if (setup
->scene
->fence
) {
76 if (LP_DEBUG
& DEBUG_SETUP
)
77 debug_printf("%s: wait for scene %d\n",
78 __FUNCTION__
, setup
->scene
->fence
->id
);
80 lp_fence_wait(setup
->scene
->fence
);
83 lp_scene_begin_binning(setup
->scene
, &setup
->fb
);
89 first_triangle( struct lp_setup_context
*setup
,
94 assert(setup
->state
== SETUP_ACTIVE
);
95 lp_setup_choose_triangle( setup
);
96 setup
->triangle( setup
, v0
, v1
, v2
);
100 first_line( struct lp_setup_context
*setup
,
101 const float (*v0
)[4],
102 const float (*v1
)[4])
104 assert(setup
->state
== SETUP_ACTIVE
);
105 lp_setup_choose_line( setup
);
106 setup
->line( setup
, v0
, v1
);
110 first_point( struct lp_setup_context
*setup
,
111 const float (*v0
)[4])
113 assert(setup
->state
== SETUP_ACTIVE
);
114 lp_setup_choose_point( setup
);
115 setup
->point( setup
, v0
);
118 void lp_setup_reset( struct lp_setup_context
*setup
)
120 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
122 /* Reset derived state */
123 setup
->constants
.stored_size
= 0;
124 setup
->constants
.stored_data
= NULL
;
125 setup
->fs
.stored
= NULL
;
133 memset(&setup
->clear
, 0, sizeof setup
->clear
);
135 /* Have an explicit "start-binning" call and get rid of this
138 setup
->line
= first_line
;
139 setup
->point
= first_point
;
140 setup
->triangle
= first_triangle
;
144 /** Rasterize all scene's bins */
146 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
148 struct lp_scene
*scene
= setup
->scene
;
149 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
151 lp_scene_end_binning(scene
);
153 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
155 if (setup
->last_fence
)
156 setup
->last_fence
->issued
= TRUE
;
158 pipe_mutex_lock(screen
->rast_mutex
);
159 lp_rast_queue_scene(screen
->rast
, scene
);
160 lp_rast_finish(screen
->rast
);
161 pipe_mutex_unlock(screen
->rast_mutex
);
163 lp_scene_end_rasterization(setup
->scene
);
164 lp_setup_reset( setup
);
166 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
172 begin_binning( struct lp_setup_context
*setup
)
174 struct lp_scene
*scene
= setup
->scene
;
175 boolean need_zsload
= FALSE
;
180 assert(scene
->fence
== NULL
);
182 /* Always create a fence:
184 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
188 /* Initialize the bin flags and x/y coords:
190 for (i
= 0; i
< scene
->tiles_x
; i
++) {
191 for (j
= 0; j
< scene
->tiles_y
; j
++) {
192 scene
->tile
[i
][j
].x
= i
;
193 scene
->tile
[i
][j
].y
= j
;
197 ok
= try_update_scene_state(setup
);
201 if (setup
->fb
.zsbuf
&&
202 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
203 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
206 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
207 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
208 need_zsload
? "clear": "load");
210 if (setup
->fb
.nr_cbufs
) {
211 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
212 ok
= lp_scene_bin_everywhere( scene
,
213 LP_RAST_OP_CLEAR_COLOR
,
214 setup
->clear
.color
);
220 if (setup
->fb
.zsbuf
) {
221 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
223 scene
->has_depthstencil_clear
= TRUE
;
225 ok
= lp_scene_bin_everywhere( scene
,
226 LP_RAST_OP_CLEAR_ZSTENCIL
,
228 setup
->clear
.zsvalue
,
229 setup
->clear
.zsmask
));
235 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
236 if (setup
->active_query
[i
]) {
237 ok
= lp_scene_bin_everywhere( scene
,
238 LP_RAST_OP_BEGIN_QUERY
,
239 lp_rast_arg_query(setup
->active_query
[i
]) );
245 setup
->clear
.flags
= 0;
246 setup
->clear
.zsmask
= 0;
247 setup
->clear
.zsvalue
= 0;
249 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
254 /* This basically bins and then flushes any outstanding full-screen
257 * TODO: fast path for fullscreen clears and no triangles.
260 execute_clears( struct lp_setup_context
*setup
)
262 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
264 return begin_binning( setup
);
267 const char *states
[] = {
275 set_scene_state( struct lp_setup_context
*setup
,
276 enum setup_state new_state
,
279 unsigned old_state
= setup
->state
;
281 if (old_state
== new_state
)
284 if (LP_DEBUG
& DEBUG_SCENE
) {
285 debug_printf("%s old %s new %s%s%s\n",
289 (new_state
== SETUP_FLUSHED
) ? ": " : "",
290 (new_state
== SETUP_FLUSHED
) ? reason
: "");
292 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
293 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
296 /* wait for a free/empty scene
298 if (old_state
== SETUP_FLUSHED
)
299 lp_setup_get_empty_scene(setup
);
306 if (!begin_binning( setup
))
311 if (old_state
== SETUP_CLEARED
)
312 if (!execute_clears( setup
))
315 lp_setup_rasterize_scene( setup
);
316 assert(setup
->scene
== NULL
);
320 assert(0 && "invalid setup state mode");
324 setup
->state
= new_state
;
329 lp_scene_end_rasterization(setup
->scene
);
333 setup
->state
= SETUP_FLUSHED
;
334 lp_setup_reset( setup
);
340 lp_setup_flush( struct lp_setup_context
*setup
,
341 struct pipe_fence_handle
**fence
,
344 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
347 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
353 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
354 const struct pipe_framebuffer_state
*fb
)
356 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
358 /* Flush any old scene.
360 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
363 * Ensure the old scene is not reused.
365 assert(!setup
->scene
);
367 /* Set new state. This will be picked up later when we next need a
370 util_copy_framebuffer_state(&setup
->fb
, fb
);
371 setup
->framebuffer
.x0
= 0;
372 setup
->framebuffer
.y0
= 0;
373 setup
->framebuffer
.x1
= fb
->width
-1;
374 setup
->framebuffer
.y1
= fb
->height
-1;
375 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
380 lp_setup_try_clear( struct lp_setup_context
*setup
,
387 uint32_t zsvalue
= 0;
388 union lp_rast_cmd_arg color_arg
;
391 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
393 if (flags
& PIPE_CLEAR_COLOR
) {
394 for (i
= 0; i
< 4; i
++)
395 color_arg
.clear_color
[i
] = color
[i
];
398 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
399 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
400 uint32_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
402 zsvalue
= util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
407 zsmask
= util_pack_mask_z_stencil(setup
->fb
.zsbuf
->format
,
414 if (setup
->state
== SETUP_ACTIVE
) {
415 struct lp_scene
*scene
= setup
->scene
;
417 /* Add the clear to existing scene. In the unusual case where
418 * both color and depth-stencil are being cleared when there's
419 * already been some rendering, we could discard the currently
420 * binned scene and start again, but I don't see that as being
423 if (flags
& PIPE_CLEAR_COLOR
) {
424 if (!lp_scene_bin_everywhere( scene
,
425 LP_RAST_OP_CLEAR_COLOR
,
430 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
431 if (!lp_scene_bin_everywhere( scene
,
432 LP_RAST_OP_CLEAR_ZSTENCIL
,
433 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
438 /* Put ourselves into the 'pre-clear' state, specifically to try
439 * and accumulate multiple clears to color and depth_stencil
440 * buffers which the app or state-tracker might issue
443 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
445 setup
->clear
.flags
|= flags
;
447 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
448 setup
->clear
.zsmask
|= zsmask
;
449 setup
->clear
.zsvalue
=
450 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
453 if (flags
& PIPE_CLEAR_COLOR
) {
454 memcpy(setup
->clear
.color
.clear_color
,
456 sizeof setup
->clear
.color
.clear_color
);
464 lp_setup_clear( struct lp_setup_context
*setup
,
470 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
471 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
473 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
483 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
485 boolean ccw_is_frontface
,
487 boolean gl_rasterization_rules
)
489 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
491 setup
->ccw_is_frontface
= ccw_is_frontface
;
492 setup
->cullmode
= cull_mode
;
493 setup
->triangle
= first_triangle
;
494 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
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
,
549 struct pipe_resource
*buffer
)
551 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
553 pipe_resource_reference(&setup
->constants
.current
, buffer
);
555 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
560 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
561 float alpha_ref_value
)
563 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
565 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
566 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
567 setup
->dirty
|= LP_SETUP_NEW_FS
;
572 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
573 const ubyte refs
[2] )
575 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
577 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
578 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
579 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
580 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
581 setup
->dirty
|= LP_SETUP_NEW_FS
;
586 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
587 const struct pipe_blend_color
*blend_color
)
589 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
593 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
594 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
595 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
601 lp_setup_set_scissor( struct lp_setup_context
*setup
,
602 const struct pipe_scissor_state
*scissor
)
604 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
608 setup
->scissor
.x0
= scissor
->minx
;
609 setup
->scissor
.x1
= scissor
->maxx
-1;
610 setup
->scissor
.y0
= scissor
->miny
;
611 setup
->scissor
.y1
= scissor
->maxy
-1;
612 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
617 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
618 boolean flatshade_first
)
620 setup
->flatshade_first
= flatshade_first
;
625 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
626 struct vertex_info
*vertex_info
)
628 /* XXX: just silently holding onto the pointer:
630 setup
->vertex_info
= vertex_info
;
635 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
638 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
640 struct pipe_sampler_view
**views
)
644 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
646 assert(num
<= PIPE_MAX_SAMPLERS
);
648 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
649 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
652 struct pipe_resource
*tex
= view
->texture
;
653 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
654 struct lp_jit_texture
*jit_tex
;
655 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
656 jit_tex
->width
= tex
->width0
;
657 jit_tex
->height
= tex
->height0
;
658 jit_tex
->first_level
= view
->u
.tex
.first_level
;
659 jit_tex
->last_level
= tex
->last_level
;
661 if (tex
->target
== PIPE_TEXTURE_3D
) {
662 jit_tex
->depth
= tex
->depth0
;
665 jit_tex
->depth
= tex
->array_size
;
668 /* We're referencing the texture's internal data, so save a
671 pipe_resource_reference(&setup
->fs
.current_tex
[i
], tex
);
674 /* regular texture - setup array of mipmap level offsets */
678 * XXX this is messed up we don't want to accidentally trigger
679 * tiled->linear conversion for levels we don't need.
680 * So ask for first_level data (which will allocate all levels)
681 * then if successful get base ptr.
683 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, view
->u
.tex
.first_level
,
685 LP_TEX_LAYOUT_LINEAR
);
686 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
687 /* out of memory - use dummy tile memory */
688 jit_tex
->base
= lp_dummy_tile
;
689 jit_tex
->width
= TILE_SIZE
/8;
690 jit_tex
->height
= TILE_SIZE
/8;
692 jit_tex
->first_level
= 0;
693 jit_tex
->last_level
= 0;
696 jit_tex
->base
= lp_tex
->linear_img
.data
;
698 for (j
= view
->u
.tex
.first_level
; j
<= tex
->last_level
; j
++) {
699 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
701 LP_TEX_LAYOUT_LINEAR
);
702 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
704 * could get mip offset directly but need call above to
705 * invoke tiled->linear conversion.
707 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
708 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
709 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
711 if (jit_tex
->base
== lp_dummy_tile
) {
712 /* out of memory - use dummy tile memory */
713 jit_tex
->mip_offsets
[j
] = 0;
714 jit_tex
->row_stride
[j
] = 0;
715 jit_tex
->img_stride
[j
] = 0;
720 /* display target texture/surface */
722 * XXX: Where should this be unmapped?
724 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
725 struct sw_winsys
*winsys
= screen
->winsys
;
726 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
728 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
729 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
730 jit_tex
->mip_offsets
[0] = 0;
731 assert(jit_tex
->base
);
736 setup
->dirty
|= LP_SETUP_NEW_FS
;
741 * Called during state validation when LP_NEW_SAMPLER is set.
744 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
746 struct pipe_sampler_state
**samplers
)
750 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
752 assert(num
<= PIPE_MAX_SAMPLERS
);
754 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
755 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
758 struct lp_jit_texture
*jit_tex
;
759 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
761 jit_tex
->min_lod
= sampler
->min_lod
;
762 jit_tex
->max_lod
= sampler
->max_lod
;
763 jit_tex
->lod_bias
= sampler
->lod_bias
;
764 COPY_4V(jit_tex
->border_color
, sampler
->border_color
.f
);
768 setup
->dirty
|= LP_SETUP_NEW_FS
;
773 * Is the given texture referenced by any scene?
774 * Note: we have to check all scenes including any scenes currently
775 * being rendered and the current scene being built.
778 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
779 const struct pipe_resource
*texture
)
783 /* check the render targets */
784 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
785 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
786 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
788 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
789 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
792 /* check textures referenced by the scene */
793 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
794 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
795 return LP_REFERENCED_FOR_READ
;
799 return LP_UNREFERENCED
;
804 * Called by vbuf code when we're about to draw something.
807 try_update_scene_state( struct lp_setup_context
*setup
)
809 boolean new_scene
= (setup
->fs
.stored
== NULL
);
810 struct lp_scene
*scene
= setup
->scene
;
814 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
820 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
821 size
= 4 * 16 * sizeof(uint8_t);
822 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
823 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
830 /* Store floating point colour */
831 fstored
= (float*)(stored
+ 4*16);
832 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
833 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
836 /* smear each blend color component across 16 ubyte elements */
837 for (i
= 0; i
< 4; ++i
) {
838 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
839 for (j
= 0; j
< 16; ++j
)
840 stored
[i
*16 + j
] = c
;
843 setup
->blend_color
.stored
= stored
;
844 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
845 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
846 setup
->dirty
|= LP_SETUP_NEW_FS
;
849 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
850 struct pipe_resource
*buffer
= setup
->constants
.current
;
853 unsigned current_size
= buffer
->width0
;
854 const void *current_data
= llvmpipe_resource_data(buffer
);
856 /* TODO: copy only the actually used constants? */
858 if(setup
->constants
.stored_size
!= current_size
||
859 !setup
->constants
.stored_data
||
860 memcmp(setup
->constants
.stored_data
,
862 current_size
) != 0) {
865 stored
= lp_scene_alloc(scene
, current_size
);
874 setup
->constants
.stored_size
= current_size
;
875 setup
->constants
.stored_data
= stored
;
879 setup
->constants
.stored_size
= 0;
880 setup
->constants
.stored_data
= NULL
;
883 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
884 setup
->dirty
|= LP_SETUP_NEW_FS
;
888 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
889 if (!setup
->fs
.stored
||
890 memcmp(setup
->fs
.stored
,
892 sizeof setup
->fs
.current
) != 0)
894 struct lp_rast_state
*stored
;
897 /* The fs state that's been stored in the scene is different from
898 * the new, current state. So allocate a new lp_rast_state object
899 * and append it to the bin's setup data buffer.
901 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
909 sizeof setup
->fs
.current
);
910 setup
->fs
.stored
= stored
;
912 /* The scene now references the textures in the rasterization
913 * state record. Note that now.
915 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
916 if (setup
->fs
.current_tex
[i
]) {
917 if (!lp_scene_add_resource_reference(scene
,
918 setup
->fs
.current_tex
[i
],
928 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
929 setup
->draw_region
= setup
->framebuffer
;
930 if (setup
->scissor_test
) {
931 u_rect_possible_intersection(&setup
->scissor
,
932 &setup
->draw_region
);
938 assert(setup
->fs
.stored
);
943 lp_setup_update_state( struct lp_setup_context
*setup
,
944 boolean update_scene
)
946 /* Some of the 'draw' pipeline stages may have changed some driver state.
947 * Make sure we've processed those state changes before anything else.
949 * XXX this is the only place where llvmpipe_context is used in the
950 * setup code. This may get refactored/changed...
953 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
955 llvmpipe_update_derived(lp
);
958 if (lp
->setup
->dirty
) {
959 llvmpipe_update_setup(lp
);
962 assert(setup
->setup
.variant
);
964 /* Will probably need to move this somewhere else, just need
965 * to know about vertex shader point size attribute.
967 setup
->psize
= lp
->psize_slot
;
969 assert(lp
->dirty
== 0);
971 assert(lp
->setup_variant
.key
.size
==
972 setup
->setup
.variant
->key
.size
);
974 assert(memcmp(&lp
->setup_variant
.key
,
975 &setup
->setup
.variant
->key
,
976 setup
->setup
.variant
->key
.size
) == 0);
979 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
980 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
984 /* Only call into update_scene_state() if we already have a
987 if (update_scene
&& setup
->scene
) {
988 assert(setup
->state
== SETUP_ACTIVE
);
990 if (try_update_scene_state(setup
))
993 /* Update failed, try to restart the scene.
995 * Cannot call lp_setup_flush_and_restart() directly here
996 * because of potential recursion.
998 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1001 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1007 return try_update_scene_state(setup
);
1015 /* Only caller is lp_setup_vbuf_destroy()
1018 lp_setup_destroy( struct lp_setup_context
*setup
)
1022 lp_setup_reset( setup
);
1024 util_unreference_framebuffer_state(&setup
->fb
);
1026 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1027 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1030 pipe_resource_reference(&setup
->constants
.current
, NULL
);
1032 /* free the scenes in the 'empty' queue */
1033 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1034 struct lp_scene
*scene
= setup
->scenes
[i
];
1037 lp_fence_wait(scene
->fence
);
1039 lp_scene_destroy(scene
);
1042 lp_fence_reference(&setup
->last_fence
, NULL
);
1049 * Create a new primitive tiling engine. Plug it into the backend of
1050 * the draw module. Currently also creates a rasterizer to use with
1053 struct lp_setup_context
*
1054 lp_setup_create( struct pipe_context
*pipe
,
1055 struct draw_context
*draw
)
1057 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1058 struct lp_setup_context
*setup
;
1061 setup
= CALLOC_STRUCT(lp_setup_context
);
1066 lp_setup_init_vbuf(setup
);
1068 /* Used only in update_state():
1073 setup
->num_threads
= screen
->num_threads
;
1074 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1079 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1080 draw_set_render(draw
, &setup
->base
);
1082 /* create some empty scenes */
1083 for (i
= 0; i
< MAX_SCENES
; i
++) {
1084 setup
->scenes
[i
] = lp_scene_create( pipe
);
1085 if (!setup
->scenes
[i
]) {
1090 setup
->triangle
= first_triangle
;
1091 setup
->line
= first_line
;
1092 setup
->point
= first_point
;
1099 for (i
= 0; i
< MAX_SCENES
; i
++) {
1100 if (setup
->scenes
[i
]) {
1101 lp_scene_destroy(setup
->scenes
[i
]);
1105 setup
->vbuf
->destroy(setup
->vbuf
);
1114 * Put a BeginQuery command into all bins.
1117 lp_setup_begin_query(struct lp_setup_context
*setup
,
1118 struct llvmpipe_query
*pq
)
1120 /* init the query to its beginning state */
1121 assert(setup
->active_query
[pq
->type
] == NULL
);
1123 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1125 setup
->active_query
[pq
->type
] = pq
;
1127 /* XXX: It is possible that a query is created before the scene
1128 * has been created. This means that setup->scene == NULL resulting
1129 * in the query not being binned and thus is ignored.
1133 if (!lp_scene_bin_everywhere(setup
->scene
,
1134 LP_RAST_OP_BEGIN_QUERY
,
1135 lp_rast_arg_query(pq
))) {
1137 if (!lp_setup_flush_and_restart(setup
))
1140 if (!lp_scene_bin_everywhere(setup
->scene
,
1141 LP_RAST_OP_BEGIN_QUERY
,
1142 lp_rast_arg_query(pq
))) {
1151 * Put an EndQuery command into all bins.
1154 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1156 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1158 if (pq
->type
!= PIPE_QUERY_TIMESTAMP
) {
1159 assert(setup
->active_query
[pq
->type
] == pq
);
1160 setup
->active_query
[pq
->type
] = NULL
;
1163 /* Setup will automatically re-issue any query which carried over a
1164 * scene boundary, and the rasterizer automatically "ends" queries
1165 * which are active at the end of a scene, so there is no need to
1166 * retry this commands on failure.
1169 /* pq->fence should be the fence of the *last* scene which
1170 * contributed to the query result.
1172 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1174 if (!lp_scene_bin_everywhere(setup
->scene
,
1175 LP_RAST_OP_END_QUERY
,
1176 lp_rast_arg_query(pq
))) {
1177 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
1181 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1187 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1189 if (0) debug_printf("%s\n", __FUNCTION__
);
1191 assert(setup
->state
== SETUP_ACTIVE
);
1193 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1196 if (!lp_setup_update_state(setup
, TRUE
))