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
)
123 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
125 /* Reset derived state */
126 setup
->constants
.stored_size
= 0;
127 setup
->constants
.stored_data
= NULL
;
128 setup
->fs
.stored
= NULL
;
136 memset(&setup
->clear
, 0, sizeof setup
->clear
);
138 /* Have an explicit "start-binning" call and get rid of this
141 setup
->line
= first_line
;
142 setup
->point
= first_point
;
143 setup
->triangle
= first_triangle
;
147 /** Rasterize all scene's bins */
149 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
151 struct lp_scene
*scene
= setup
->scene
;
152 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
154 lp_scene_end_binning(scene
);
156 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
158 if (setup
->last_fence
)
159 setup
->last_fence
->issued
= TRUE
;
161 pipe_mutex_lock(screen
->rast_mutex
);
162 lp_rast_queue_scene(screen
->rast
, scene
);
163 lp_rast_finish(screen
->rast
);
164 pipe_mutex_unlock(screen
->rast_mutex
);
166 lp_scene_end_rasterization(setup
->scene
);
167 lp_setup_reset( setup
);
169 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
175 begin_binning( struct lp_setup_context
*setup
)
177 struct lp_scene
*scene
= setup
->scene
;
178 boolean need_zsload
= FALSE
;
183 assert(scene
->fence
== NULL
);
185 /* Always create a fence:
187 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
191 /* Initialize the bin flags and x/y coords:
193 for (i
= 0; i
< scene
->tiles_x
; i
++) {
194 for (j
= 0; j
< scene
->tiles_y
; j
++) {
195 scene
->tile
[i
][j
].x
= i
;
196 scene
->tile
[i
][j
].y
= j
;
200 ok
= try_update_scene_state(setup
);
204 if (setup
->fb
.zsbuf
&&
205 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
206 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
209 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
210 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
211 need_zsload
? "clear": "load");
213 if (setup
->fb
.nr_cbufs
) {
214 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
215 ok
= lp_scene_bin_everywhere( scene
,
216 LP_RAST_OP_CLEAR_COLOR
,
217 setup
->clear
.color
);
223 if (setup
->fb
.zsbuf
) {
224 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
226 scene
->has_depthstencil_clear
= TRUE
;
228 ok
= lp_scene_bin_everywhere( scene
,
229 LP_RAST_OP_CLEAR_ZSTENCIL
,
231 setup
->clear
.zsvalue
,
232 setup
->clear
.zsmask
));
238 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
239 if (setup
->active_query
[i
]) {
240 ok
= lp_scene_bin_everywhere( scene
,
241 LP_RAST_OP_BEGIN_QUERY
,
242 lp_rast_arg_query(setup
->active_query
[i
]) );
248 setup
->clear
.flags
= 0;
249 setup
->clear
.zsmask
= 0;
250 setup
->clear
.zsvalue
= 0;
252 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
257 /* This basically bins and then flushes any outstanding full-screen
260 * TODO: fast path for fullscreen clears and no triangles.
263 execute_clears( struct lp_setup_context
*setup
)
265 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
267 return begin_binning( setup
);
270 const char *states
[] = {
278 set_scene_state( struct lp_setup_context
*setup
,
279 enum setup_state new_state
,
282 unsigned old_state
= setup
->state
;
284 if (old_state
== new_state
)
287 if (LP_DEBUG
& DEBUG_SCENE
) {
288 debug_printf("%s old %s new %s%s%s\n",
292 (new_state
== SETUP_FLUSHED
) ? ": " : "",
293 (new_state
== SETUP_FLUSHED
) ? reason
: "");
295 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
296 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
299 /* wait for a free/empty scene
301 if (old_state
== SETUP_FLUSHED
)
302 lp_setup_get_empty_scene(setup
);
309 if (!begin_binning( setup
))
314 if (old_state
== SETUP_CLEARED
)
315 if (!execute_clears( setup
))
318 lp_setup_rasterize_scene( setup
);
319 assert(setup
->scene
== NULL
);
323 assert(0 && "invalid setup state mode");
327 setup
->state
= new_state
;
332 lp_scene_end_rasterization(setup
->scene
);
336 setup
->state
= SETUP_FLUSHED
;
337 lp_setup_reset( setup
);
343 lp_setup_flush( struct lp_setup_context
*setup
,
344 struct pipe_fence_handle
**fence
,
347 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
350 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
356 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
357 const struct pipe_framebuffer_state
*fb
)
359 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
361 /* Flush any old scene.
363 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
366 * Ensure the old scene is not reused.
368 assert(!setup
->scene
);
370 /* Set new state. This will be picked up later when we next need a
373 util_copy_framebuffer_state(&setup
->fb
, fb
);
374 setup
->framebuffer
.x0
= 0;
375 setup
->framebuffer
.y0
= 0;
376 setup
->framebuffer
.x1
= fb
->width
-1;
377 setup
->framebuffer
.y1
= fb
->height
-1;
378 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
383 lp_setup_try_clear( struct lp_setup_context
*setup
,
390 uint32_t zsvalue
= 0;
391 union lp_rast_cmd_arg color_arg
;
394 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
396 if (flags
& PIPE_CLEAR_COLOR
) {
397 for (i
= 0; i
< 4; i
++)
398 color_arg
.clear_color
[i
] = color
[i
];
401 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
402 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
403 uint32_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
405 zsvalue
= util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
410 zsmask
= util_pack_mask_z_stencil(setup
->fb
.zsbuf
->format
,
417 if (setup
->state
== SETUP_ACTIVE
) {
418 struct lp_scene
*scene
= setup
->scene
;
420 /* Add the clear to existing scene. In the unusual case where
421 * both color and depth-stencil are being cleared when there's
422 * already been some rendering, we could discard the currently
423 * binned scene and start again, but I don't see that as being
426 if (flags
& PIPE_CLEAR_COLOR
) {
427 if (!lp_scene_bin_everywhere( scene
,
428 LP_RAST_OP_CLEAR_COLOR
,
433 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
434 if (!lp_scene_bin_everywhere( scene
,
435 LP_RAST_OP_CLEAR_ZSTENCIL
,
436 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
441 /* Put ourselves into the 'pre-clear' state, specifically to try
442 * and accumulate multiple clears to color and depth_stencil
443 * buffers which the app or state-tracker might issue
446 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
448 setup
->clear
.flags
|= flags
;
450 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
451 setup
->clear
.zsmask
|= zsmask
;
452 setup
->clear
.zsvalue
=
453 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
456 if (flags
& PIPE_CLEAR_COLOR
) {
457 memcpy(setup
->clear
.color
.clear_color
,
459 sizeof setup
->clear
.color
.clear_color
);
467 lp_setup_clear( struct lp_setup_context
*setup
,
473 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
474 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
476 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
486 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
488 boolean ccw_is_frontface
,
490 boolean gl_rasterization_rules
)
492 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
494 setup
->ccw_is_frontface
= ccw_is_frontface
;
495 setup
->cullmode
= cull_mode
;
496 setup
->triangle
= first_triangle
;
497 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
499 if (setup
->scissor_test
!= scissor
) {
500 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
501 setup
->scissor_test
= scissor
;
506 lp_setup_set_line_state( struct lp_setup_context
*setup
,
509 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
511 setup
->line_width
= line_width
;
515 lp_setup_set_point_state( struct lp_setup_context
*setup
,
517 boolean point_size_per_vertex
,
518 uint sprite_coord_enable
,
519 uint sprite_coord_origin
)
521 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
523 setup
->point_size
= point_size
;
524 setup
->sprite_coord_enable
= sprite_coord_enable
;
525 setup
->sprite_coord_origin
= sprite_coord_origin
;
526 setup
->point_size_per_vertex
= point_size_per_vertex
;
530 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
531 const struct lp_setup_variant
*variant
)
533 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
535 setup
->setup
.variant
= variant
;
539 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
540 struct lp_fragment_shader_variant
*variant
)
542 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
544 /* FIXME: reference count */
546 setup
->fs
.current
.variant
= variant
;
547 setup
->dirty
|= LP_SETUP_NEW_FS
;
551 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
552 struct pipe_resource
*buffer
)
554 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
556 pipe_resource_reference(&setup
->constants
.current
, buffer
);
558 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
563 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
564 float alpha_ref_value
)
566 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
568 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
569 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
570 setup
->dirty
|= LP_SETUP_NEW_FS
;
575 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
576 const ubyte refs
[2] )
578 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
580 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
581 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
582 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
583 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
584 setup
->dirty
|= LP_SETUP_NEW_FS
;
589 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
590 const struct pipe_blend_color
*blend_color
)
592 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
596 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
597 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
598 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
604 lp_setup_set_scissor( struct lp_setup_context
*setup
,
605 const struct pipe_scissor_state
*scissor
)
607 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
611 setup
->scissor
.x0
= scissor
->minx
;
612 setup
->scissor
.x1
= scissor
->maxx
-1;
613 setup
->scissor
.y0
= scissor
->miny
;
614 setup
->scissor
.y1
= scissor
->maxy
-1;
615 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
620 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
621 boolean flatshade_first
)
623 setup
->flatshade_first
= flatshade_first
;
627 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
628 boolean rasterizer_discard
)
630 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
631 setup
->rasterizer_discard
= rasterizer_discard
;
632 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
637 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
638 struct vertex_info
*vertex_info
)
640 /* XXX: just silently holding onto the pointer:
642 setup
->vertex_info
= vertex_info
;
647 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
650 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
652 struct pipe_sampler_view
**views
)
656 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
658 assert(num
<= PIPE_MAX_SAMPLERS
);
660 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
661 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
664 struct pipe_resource
*tex
= view
->texture
;
665 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
666 struct lp_jit_texture
*jit_tex
;
667 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
668 jit_tex
->width
= tex
->width0
;
669 jit_tex
->height
= tex
->height0
;
670 jit_tex
->first_level
= view
->u
.tex
.first_level
;
671 jit_tex
->last_level
= tex
->last_level
;
673 if (tex
->target
== PIPE_TEXTURE_3D
) {
674 jit_tex
->depth
= tex
->depth0
;
677 jit_tex
->depth
= tex
->array_size
;
680 /* We're referencing the texture's internal data, so save a
683 pipe_resource_reference(&setup
->fs
.current_tex
[i
], tex
);
686 /* regular texture - setup array of mipmap level offsets */
690 * XXX this is messed up we don't want to accidentally trigger
691 * tiled->linear conversion for levels we don't need.
692 * So ask for first_level data (which will allocate all levels)
693 * then if successful get base ptr.
695 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, view
->u
.tex
.first_level
,
697 LP_TEX_LAYOUT_LINEAR
);
698 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
699 /* out of memory - use dummy tile memory */
700 jit_tex
->base
= lp_dummy_tile
;
701 jit_tex
->width
= TILE_SIZE
/8;
702 jit_tex
->height
= TILE_SIZE
/8;
704 jit_tex
->first_level
= 0;
705 jit_tex
->last_level
= 0;
708 jit_tex
->base
= lp_tex
->linear_img
.data
;
710 for (j
= view
->u
.tex
.first_level
; j
<= tex
->last_level
; j
++) {
711 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
713 LP_TEX_LAYOUT_LINEAR
);
714 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
716 * could get mip offset directly but need call above to
717 * invoke tiled->linear conversion.
719 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
720 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
721 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
723 if (jit_tex
->base
== lp_dummy_tile
) {
724 /* out of memory - use dummy tile memory */
725 jit_tex
->mip_offsets
[j
] = 0;
726 jit_tex
->row_stride
[j
] = 0;
727 jit_tex
->img_stride
[j
] = 0;
732 /* display target texture/surface */
734 * XXX: Where should this be unmapped?
736 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
737 struct sw_winsys
*winsys
= screen
->winsys
;
738 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
740 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
741 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
742 jit_tex
->mip_offsets
[0] = 0;
743 assert(jit_tex
->base
);
748 setup
->dirty
|= LP_SETUP_NEW_FS
;
753 * Called during state validation when LP_NEW_SAMPLER is set.
756 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
758 struct pipe_sampler_state
**samplers
)
762 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
764 assert(num
<= PIPE_MAX_SAMPLERS
);
766 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
767 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
770 struct lp_jit_texture
*jit_tex
;
771 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
773 jit_tex
->min_lod
= sampler
->min_lod
;
774 jit_tex
->max_lod
= sampler
->max_lod
;
775 jit_tex
->lod_bias
= sampler
->lod_bias
;
776 COPY_4V(jit_tex
->border_color
, sampler
->border_color
.f
);
780 setup
->dirty
|= LP_SETUP_NEW_FS
;
785 * Is the given texture referenced by any scene?
786 * Note: we have to check all scenes including any scenes currently
787 * being rendered and the current scene being built.
790 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
791 const struct pipe_resource
*texture
)
795 /* check the render targets */
796 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
797 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
798 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
800 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
801 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
804 /* check textures referenced by the scene */
805 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
806 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
807 return LP_REFERENCED_FOR_READ
;
811 return LP_UNREFERENCED
;
816 * Called by vbuf code when we're about to draw something.
819 try_update_scene_state( struct lp_setup_context
*setup
)
821 boolean new_scene
= (setup
->fs
.stored
== NULL
);
822 struct lp_scene
*scene
= setup
->scene
;
826 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
832 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
833 size
= 4 * 16 * sizeof(uint8_t);
834 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
835 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
842 /* Store floating point colour */
843 fstored
= (float*)(stored
+ 4*16);
844 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
845 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
848 /* smear each blend color component across 16 ubyte elements */
849 for (i
= 0; i
< 4; ++i
) {
850 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
851 for (j
= 0; j
< 16; ++j
)
852 stored
[i
*16 + j
] = c
;
855 setup
->blend_color
.stored
= stored
;
856 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
857 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
858 setup
->dirty
|= LP_SETUP_NEW_FS
;
861 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
862 struct pipe_resource
*buffer
= setup
->constants
.current
;
865 unsigned current_size
= buffer
->width0
;
866 const void *current_data
= llvmpipe_resource_data(buffer
);
868 /* TODO: copy only the actually used constants? */
870 if(setup
->constants
.stored_size
!= current_size
||
871 !setup
->constants
.stored_data
||
872 memcmp(setup
->constants
.stored_data
,
874 current_size
) != 0) {
877 stored
= lp_scene_alloc(scene
, current_size
);
886 setup
->constants
.stored_size
= current_size
;
887 setup
->constants
.stored_data
= stored
;
891 setup
->constants
.stored_size
= 0;
892 setup
->constants
.stored_data
= NULL
;
895 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
896 setup
->dirty
|= LP_SETUP_NEW_FS
;
900 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
901 if (!setup
->fs
.stored
||
902 memcmp(setup
->fs
.stored
,
904 sizeof setup
->fs
.current
) != 0)
906 struct lp_rast_state
*stored
;
909 /* The fs state that's been stored in the scene is different from
910 * the new, current state. So allocate a new lp_rast_state object
911 * and append it to the bin's setup data buffer.
913 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
921 sizeof setup
->fs
.current
);
922 setup
->fs
.stored
= stored
;
924 /* The scene now references the textures in the rasterization
925 * state record. Note that now.
927 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
928 if (setup
->fs
.current_tex
[i
]) {
929 if (!lp_scene_add_resource_reference(scene
,
930 setup
->fs
.current_tex
[i
],
940 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
941 setup
->draw_region
= setup
->framebuffer
;
942 if (setup
->scissor_test
) {
943 u_rect_possible_intersection(&setup
->scissor
,
944 &setup
->draw_region
);
950 assert(setup
->fs
.stored
);
955 lp_setup_update_state( struct lp_setup_context
*setup
,
956 boolean update_scene
)
958 /* Some of the 'draw' pipeline stages may have changed some driver state.
959 * Make sure we've processed those state changes before anything else.
961 * XXX this is the only place where llvmpipe_context is used in the
962 * setup code. This may get refactored/changed...
965 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
967 llvmpipe_update_derived(lp
);
970 if (lp
->setup
->dirty
) {
971 llvmpipe_update_setup(lp
);
974 assert(setup
->setup
.variant
);
976 /* Will probably need to move this somewhere else, just need
977 * to know about vertex shader point size attribute.
979 setup
->psize
= lp
->psize_slot
;
981 assert(lp
->dirty
== 0);
983 assert(lp
->setup_variant
.key
.size
==
984 setup
->setup
.variant
->key
.size
);
986 assert(memcmp(&lp
->setup_variant
.key
,
987 &setup
->setup
.variant
->key
,
988 setup
->setup
.variant
->key
.size
) == 0);
991 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
992 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
996 /* Only call into update_scene_state() if we already have a
999 if (update_scene
&& setup
->scene
) {
1000 assert(setup
->state
== SETUP_ACTIVE
);
1002 if (try_update_scene_state(setup
))
1005 /* Update failed, try to restart the scene.
1007 * Cannot call lp_setup_flush_and_restart() directly here
1008 * because of potential recursion.
1010 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1013 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1019 return try_update_scene_state(setup
);
1027 /* Only caller is lp_setup_vbuf_destroy()
1030 lp_setup_destroy( struct lp_setup_context
*setup
)
1034 lp_setup_reset( setup
);
1036 util_unreference_framebuffer_state(&setup
->fb
);
1038 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1039 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1042 pipe_resource_reference(&setup
->constants
.current
, NULL
);
1044 /* free the scenes in the 'empty' queue */
1045 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1046 struct lp_scene
*scene
= setup
->scenes
[i
];
1049 lp_fence_wait(scene
->fence
);
1051 lp_scene_destroy(scene
);
1054 lp_fence_reference(&setup
->last_fence
, NULL
);
1061 * Create a new primitive tiling engine. Plug it into the backend of
1062 * the draw module. Currently also creates a rasterizer to use with
1065 struct lp_setup_context
*
1066 lp_setup_create( struct pipe_context
*pipe
,
1067 struct draw_context
*draw
)
1069 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1070 struct lp_setup_context
*setup
;
1073 setup
= CALLOC_STRUCT(lp_setup_context
);
1078 lp_setup_init_vbuf(setup
);
1080 /* Used only in update_state():
1085 setup
->num_threads
= screen
->num_threads
;
1086 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1091 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1092 draw_set_render(draw
, &setup
->base
);
1094 /* create some empty scenes */
1095 for (i
= 0; i
< MAX_SCENES
; i
++) {
1096 setup
->scenes
[i
] = lp_scene_create( pipe
);
1097 if (!setup
->scenes
[i
]) {
1102 setup
->triangle
= first_triangle
;
1103 setup
->line
= first_line
;
1104 setup
->point
= first_point
;
1111 for (i
= 0; i
< MAX_SCENES
; i
++) {
1112 if (setup
->scenes
[i
]) {
1113 lp_scene_destroy(setup
->scenes
[i
]);
1117 setup
->vbuf
->destroy(setup
->vbuf
);
1126 * Put a BeginQuery command into all bins.
1129 lp_setup_begin_query(struct lp_setup_context
*setup
,
1130 struct llvmpipe_query
*pq
)
1132 /* init the query to its beginning state */
1133 assert(setup
->active_query
[pq
->type
] == NULL
);
1135 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1137 setup
->active_query
[pq
->type
] = pq
;
1139 /* XXX: It is possible that a query is created before the scene
1140 * has been created. This means that setup->scene == NULL resulting
1141 * in the query not being binned and thus is ignored.
1145 if (!lp_scene_bin_everywhere(setup
->scene
,
1146 LP_RAST_OP_BEGIN_QUERY
,
1147 lp_rast_arg_query(pq
))) {
1149 if (!lp_setup_flush_and_restart(setup
))
1152 if (!lp_scene_bin_everywhere(setup
->scene
,
1153 LP_RAST_OP_BEGIN_QUERY
,
1154 lp_rast_arg_query(pq
))) {
1163 * Put an EndQuery command into all bins.
1166 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1168 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1170 if (pq
->type
!= PIPE_QUERY_TIMESTAMP
) {
1171 assert(setup
->active_query
[pq
->type
] == pq
);
1172 setup
->active_query
[pq
->type
] = NULL
;
1175 /* Setup will automatically re-issue any query which carried over a
1176 * scene boundary, and the rasterizer automatically "ends" queries
1177 * which are active at the end of a scene, so there is no need to
1178 * retry this commands on failure.
1181 /* pq->fence should be the fence of the *last* scene which
1182 * contributed to the query result.
1184 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1186 if (!lp_scene_bin_everywhere(setup
->scene
,
1187 LP_RAST_OP_END_QUERY
,
1188 lp_rast_arg_query(pq
))) {
1189 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
1193 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1199 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1201 if (0) debug_printf("%s\n", __FUNCTION__
);
1203 assert(setup
->state
== SETUP_ACTIVE
);
1205 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1208 if (!lp_setup_update_state(setup
, TRUE
))