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 assert(setup
->scene
== NULL
);
70 boolean discard
= lp
->rasterizer
? lp
->rasterizer
->rasterizer_discard
: FALSE
;
73 setup
->scene_idx
%= Elements(setup
->scenes
);
75 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
77 if (setup
->scene
->fence
) {
78 if (LP_DEBUG
& DEBUG_SETUP
)
79 debug_printf("%s: wait for scene %d\n",
80 __FUNCTION__
, setup
->scene
->fence
->id
);
82 lp_fence_wait(setup
->scene
->fence
);
85 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, discard
);
91 first_triangle( struct lp_setup_context
*setup
,
96 assert(setup
->state
== SETUP_ACTIVE
);
97 lp_setup_choose_triangle( setup
);
98 setup
->triangle( setup
, v0
, v1
, v2
);
102 first_line( struct lp_setup_context
*setup
,
103 const float (*v0
)[4],
104 const float (*v1
)[4])
106 assert(setup
->state
== SETUP_ACTIVE
);
107 lp_setup_choose_line( setup
);
108 setup
->line( setup
, v0
, v1
);
112 first_point( struct lp_setup_context
*setup
,
113 const float (*v0
)[4])
115 assert(setup
->state
== SETUP_ACTIVE
);
116 lp_setup_choose_point( setup
);
117 setup
->point( setup
, v0
);
120 void lp_setup_reset( struct lp_setup_context
*setup
)
122 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
124 /* Reset derived state */
125 setup
->constants
.stored_size
= 0;
126 setup
->constants
.stored_data
= NULL
;
127 setup
->fs
.stored
= NULL
;
135 memset(&setup
->clear
, 0, sizeof setup
->clear
);
137 /* Have an explicit "start-binning" call and get rid of this
140 setup
->line
= first_line
;
141 setup
->point
= first_point
;
142 setup
->triangle
= first_triangle
;
146 /** Rasterize all scene's bins */
148 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
150 struct lp_scene
*scene
= setup
->scene
;
151 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
153 lp_scene_end_binning(scene
);
155 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
157 if (setup
->last_fence
)
158 setup
->last_fence
->issued
= TRUE
;
160 pipe_mutex_lock(screen
->rast_mutex
);
161 lp_rast_queue_scene(screen
->rast
, scene
);
162 lp_rast_finish(screen
->rast
);
163 pipe_mutex_unlock(screen
->rast_mutex
);
165 lp_scene_end_rasterization(setup
->scene
);
166 lp_setup_reset( setup
);
168 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
174 begin_binning( struct lp_setup_context
*setup
)
176 struct lp_scene
*scene
= setup
->scene
;
177 boolean need_zsload
= FALSE
;
182 assert(scene
->fence
== NULL
);
184 /* Always create a fence:
186 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
190 /* Initialize the bin flags and x/y coords:
192 for (i
= 0; i
< scene
->tiles_x
; i
++) {
193 for (j
= 0; j
< scene
->tiles_y
; j
++) {
194 scene
->tile
[i
][j
].x
= i
;
195 scene
->tile
[i
][j
].y
= j
;
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 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
238 if (setup
->active_query
[i
]) {
239 ok
= lp_scene_bin_everywhere( scene
,
240 LP_RAST_OP_BEGIN_QUERY
,
241 lp_rast_arg_query(setup
->active_query
[i
]) );
247 setup
->clear
.flags
= 0;
248 setup
->clear
.zsmask
= 0;
249 setup
->clear
.zsvalue
= 0;
251 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
256 /* This basically bins and then flushes any outstanding full-screen
259 * TODO: fast path for fullscreen clears and no triangles.
262 execute_clears( struct lp_setup_context
*setup
)
264 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
266 return begin_binning( setup
);
269 const char *states
[] = {
277 set_scene_state( struct lp_setup_context
*setup
,
278 enum setup_state new_state
,
281 unsigned old_state
= setup
->state
;
283 if (old_state
== new_state
)
286 if (LP_DEBUG
& DEBUG_SCENE
) {
287 debug_printf("%s old %s new %s%s%s\n",
291 (new_state
== SETUP_FLUSHED
) ? ": " : "",
292 (new_state
== SETUP_FLUSHED
) ? reason
: "");
294 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
295 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
298 /* wait for a free/empty scene
300 if (old_state
== SETUP_FLUSHED
)
301 lp_setup_get_empty_scene(setup
);
308 if (!begin_binning( setup
))
313 if (old_state
== SETUP_CLEARED
)
314 if (!execute_clears( setup
))
317 lp_setup_rasterize_scene( setup
);
318 assert(setup
->scene
== NULL
);
322 assert(0 && "invalid setup state mode");
326 setup
->state
= new_state
;
331 lp_scene_end_rasterization(setup
->scene
);
335 setup
->state
= SETUP_FLUSHED
;
336 lp_setup_reset( setup
);
342 lp_setup_flush( struct lp_setup_context
*setup
,
343 struct pipe_fence_handle
**fence
,
346 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
349 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
355 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
356 const struct pipe_framebuffer_state
*fb
)
358 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
360 /* Flush any old scene.
362 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
365 * Ensure the old scene is not reused.
367 assert(!setup
->scene
);
369 /* Set new state. This will be picked up later when we next need a
372 util_copy_framebuffer_state(&setup
->fb
, fb
);
373 setup
->framebuffer
.x0
= 0;
374 setup
->framebuffer
.y0
= 0;
375 setup
->framebuffer
.x1
= fb
->width
-1;
376 setup
->framebuffer
.y1
= fb
->height
-1;
377 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
382 lp_setup_try_clear( struct lp_setup_context
*setup
,
389 uint32_t zsvalue
= 0;
390 union lp_rast_cmd_arg color_arg
;
393 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
395 if (flags
& PIPE_CLEAR_COLOR
) {
396 for (i
= 0; i
< 4; i
++)
397 color_arg
.clear_color
[i
] = color
[i
];
400 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
401 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
402 uint32_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
404 zsvalue
= util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
409 zsmask
= util_pack_mask_z_stencil(setup
->fb
.zsbuf
->format
,
416 if (setup
->state
== SETUP_ACTIVE
) {
417 struct lp_scene
*scene
= setup
->scene
;
419 /* Add the clear to existing scene. In the unusual case where
420 * both color and depth-stencil are being cleared when there's
421 * already been some rendering, we could discard the currently
422 * binned scene and start again, but I don't see that as being
425 if (flags
& PIPE_CLEAR_COLOR
) {
426 if (!lp_scene_bin_everywhere( scene
,
427 LP_RAST_OP_CLEAR_COLOR
,
432 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
433 if (!lp_scene_bin_everywhere( scene
,
434 LP_RAST_OP_CLEAR_ZSTENCIL
,
435 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
440 /* Put ourselves into the 'pre-clear' state, specifically to try
441 * and accumulate multiple clears to color and depth_stencil
442 * buffers which the app or state-tracker might issue
445 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
447 setup
->clear
.flags
|= flags
;
449 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
450 setup
->clear
.zsmask
|= zsmask
;
451 setup
->clear
.zsvalue
=
452 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
455 if (flags
& PIPE_CLEAR_COLOR
) {
456 memcpy(setup
->clear
.color
.clear_color
,
458 sizeof setup
->clear
.color
.clear_color
);
466 lp_setup_clear( struct lp_setup_context
*setup
,
472 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
473 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
475 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
485 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
487 boolean ccw_is_frontface
,
489 boolean gl_rasterization_rules
)
491 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
493 setup
->ccw_is_frontface
= ccw_is_frontface
;
494 setup
->cullmode
= cull_mode
;
495 setup
->triangle
= first_triangle
;
496 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
498 if (setup
->scissor_test
!= scissor
) {
499 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
500 setup
->scissor_test
= scissor
;
505 lp_setup_set_line_state( struct lp_setup_context
*setup
,
508 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
510 setup
->line_width
= line_width
;
514 lp_setup_set_point_state( struct lp_setup_context
*setup
,
516 boolean point_size_per_vertex
,
517 uint sprite_coord_enable
,
518 uint sprite_coord_origin
)
520 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
522 setup
->point_size
= point_size
;
523 setup
->sprite_coord_enable
= sprite_coord_enable
;
524 setup
->sprite_coord_origin
= sprite_coord_origin
;
525 setup
->point_size_per_vertex
= point_size_per_vertex
;
529 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
530 const struct lp_setup_variant
*variant
)
532 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
534 setup
->setup
.variant
= variant
;
538 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
539 struct lp_fragment_shader_variant
*variant
)
541 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
543 /* FIXME: reference count */
545 setup
->fs
.current
.variant
= variant
;
546 setup
->dirty
|= LP_SETUP_NEW_FS
;
550 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
551 struct pipe_resource
*buffer
)
553 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
555 pipe_resource_reference(&setup
->constants
.current
, buffer
);
557 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
562 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
563 float alpha_ref_value
)
565 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
567 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
568 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
569 setup
->dirty
|= LP_SETUP_NEW_FS
;
574 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
575 const ubyte refs
[2] )
577 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
579 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
580 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
581 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
582 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
583 setup
->dirty
|= LP_SETUP_NEW_FS
;
588 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
589 const struct pipe_blend_color
*blend_color
)
591 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
595 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
596 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
597 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
603 lp_setup_set_scissor( struct lp_setup_context
*setup
,
604 const struct pipe_scissor_state
*scissor
)
606 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
610 setup
->scissor
.x0
= scissor
->minx
;
611 setup
->scissor
.x1
= scissor
->maxx
-1;
612 setup
->scissor
.y0
= scissor
->miny
;
613 setup
->scissor
.y1
= scissor
->maxy
-1;
614 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
619 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
620 boolean flatshade_first
)
622 setup
->flatshade_first
= flatshade_first
;
626 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
627 boolean rasterizer_discard
)
629 setup
->rasterizer_discard
= rasterizer_discard
;
630 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
634 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
635 struct vertex_info
*vertex_info
)
637 /* XXX: just silently holding onto the pointer:
639 setup
->vertex_info
= vertex_info
;
644 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
647 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
649 struct pipe_sampler_view
**views
)
653 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
655 assert(num
<= PIPE_MAX_SAMPLERS
);
657 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
658 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
661 struct pipe_resource
*tex
= view
->texture
;
662 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
663 struct lp_jit_texture
*jit_tex
;
664 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
665 jit_tex
->width
= tex
->width0
;
666 jit_tex
->height
= tex
->height0
;
667 jit_tex
->first_level
= view
->u
.tex
.first_level
;
668 jit_tex
->last_level
= tex
->last_level
;
670 if (tex
->target
== PIPE_TEXTURE_3D
) {
671 jit_tex
->depth
= tex
->depth0
;
674 jit_tex
->depth
= tex
->array_size
;
677 /* We're referencing the texture's internal data, so save a
680 pipe_resource_reference(&setup
->fs
.current_tex
[i
], tex
);
683 /* regular texture - setup array of mipmap level offsets */
687 * XXX this is messed up we don't want to accidentally trigger
688 * tiled->linear conversion for levels we don't need.
689 * So ask for first_level data (which will allocate all levels)
690 * then if successful get base ptr.
692 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, view
->u
.tex
.first_level
,
694 LP_TEX_LAYOUT_LINEAR
);
695 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
696 /* out of memory - use dummy tile memory */
697 jit_tex
->base
= lp_dummy_tile
;
698 jit_tex
->width
= TILE_SIZE
/8;
699 jit_tex
->height
= TILE_SIZE
/8;
701 jit_tex
->first_level
= 0;
702 jit_tex
->last_level
= 0;
705 jit_tex
->base
= lp_tex
->linear_img
.data
;
707 for (j
= view
->u
.tex
.first_level
; j
<= tex
->last_level
; j
++) {
708 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
710 LP_TEX_LAYOUT_LINEAR
);
711 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
713 * could get mip offset directly but need call above to
714 * invoke tiled->linear conversion.
716 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
717 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
718 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
720 if (jit_tex
->base
== lp_dummy_tile
) {
721 /* out of memory - use dummy tile memory */
722 jit_tex
->mip_offsets
[j
] = 0;
723 jit_tex
->row_stride
[j
] = 0;
724 jit_tex
->img_stride
[j
] = 0;
729 /* display target texture/surface */
731 * XXX: Where should this be unmapped?
733 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
734 struct sw_winsys
*winsys
= screen
->winsys
;
735 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
737 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
738 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
739 jit_tex
->mip_offsets
[0] = 0;
740 assert(jit_tex
->base
);
745 setup
->dirty
|= LP_SETUP_NEW_FS
;
750 * Called during state validation when LP_NEW_SAMPLER is set.
753 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
755 struct pipe_sampler_state
**samplers
)
759 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
761 assert(num
<= PIPE_MAX_SAMPLERS
);
763 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
764 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
767 struct lp_jit_texture
*jit_tex
;
768 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
770 jit_tex
->min_lod
= sampler
->min_lod
;
771 jit_tex
->max_lod
= sampler
->max_lod
;
772 jit_tex
->lod_bias
= sampler
->lod_bias
;
773 COPY_4V(jit_tex
->border_color
, sampler
->border_color
.f
);
777 setup
->dirty
|= LP_SETUP_NEW_FS
;
782 * Is the given texture referenced by any scene?
783 * Note: we have to check all scenes including any scenes currently
784 * being rendered and the current scene being built.
787 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
788 const struct pipe_resource
*texture
)
792 /* check the render targets */
793 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
794 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
795 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
797 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
798 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
801 /* check textures referenced by the scene */
802 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
803 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
804 return LP_REFERENCED_FOR_READ
;
808 return LP_UNREFERENCED
;
813 * Called by vbuf code when we're about to draw something.
816 try_update_scene_state( struct lp_setup_context
*setup
)
818 boolean new_scene
= (setup
->fs
.stored
== NULL
);
819 struct lp_scene
*scene
= setup
->scene
;
823 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
829 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
830 size
= 4 * 16 * sizeof(uint8_t);
831 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
832 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
839 /* Store floating point colour */
840 fstored
= (float*)(stored
+ 4*16);
841 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
842 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
845 /* smear each blend color component across 16 ubyte elements */
846 for (i
= 0; i
< 4; ++i
) {
847 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
848 for (j
= 0; j
< 16; ++j
)
849 stored
[i
*16 + j
] = c
;
852 setup
->blend_color
.stored
= stored
;
853 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
854 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
855 setup
->dirty
|= LP_SETUP_NEW_FS
;
858 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
859 struct pipe_resource
*buffer
= setup
->constants
.current
;
862 unsigned current_size
= buffer
->width0
;
863 const void *current_data
= llvmpipe_resource_data(buffer
);
865 /* TODO: copy only the actually used constants? */
867 if(setup
->constants
.stored_size
!= current_size
||
868 !setup
->constants
.stored_data
||
869 memcmp(setup
->constants
.stored_data
,
871 current_size
) != 0) {
874 stored
= lp_scene_alloc(scene
, current_size
);
883 setup
->constants
.stored_size
= current_size
;
884 setup
->constants
.stored_data
= stored
;
888 setup
->constants
.stored_size
= 0;
889 setup
->constants
.stored_data
= NULL
;
892 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
893 setup
->dirty
|= LP_SETUP_NEW_FS
;
897 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
898 if (!setup
->fs
.stored
||
899 memcmp(setup
->fs
.stored
,
901 sizeof setup
->fs
.current
) != 0)
903 struct lp_rast_state
*stored
;
906 /* The fs state that's been stored in the scene is different from
907 * the new, current state. So allocate a new lp_rast_state object
908 * and append it to the bin's setup data buffer.
910 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
918 sizeof setup
->fs
.current
);
919 setup
->fs
.stored
= stored
;
921 /* The scene now references the textures in the rasterization
922 * state record. Note that now.
924 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
925 if (setup
->fs
.current_tex
[i
]) {
926 if (!lp_scene_add_resource_reference(scene
,
927 setup
->fs
.current_tex
[i
],
937 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
938 setup
->draw_region
= setup
->framebuffer
;
939 if (setup
->scissor_test
) {
940 u_rect_possible_intersection(&setup
->scissor
,
941 &setup
->draw_region
);
947 assert(setup
->fs
.stored
);
952 lp_setup_update_state( struct lp_setup_context
*setup
,
953 boolean update_scene
)
955 /* Some of the 'draw' pipeline stages may have changed some driver state.
956 * Make sure we've processed those state changes before anything else.
958 * XXX this is the only place where llvmpipe_context is used in the
959 * setup code. This may get refactored/changed...
962 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
964 llvmpipe_update_derived(lp
);
967 if (lp
->setup
->dirty
) {
968 llvmpipe_update_setup(lp
);
971 assert(setup
->setup
.variant
);
973 /* Will probably need to move this somewhere else, just need
974 * to know about vertex shader point size attribute.
976 setup
->psize
= lp
->psize_slot
;
978 assert(lp
->dirty
== 0);
980 assert(lp
->setup_variant
.key
.size
==
981 setup
->setup
.variant
->key
.size
);
983 assert(memcmp(&lp
->setup_variant
.key
,
984 &setup
->setup
.variant
->key
,
985 setup
->setup
.variant
->key
.size
) == 0);
988 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
989 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
993 /* Only call into update_scene_state() if we already have a
996 if (update_scene
&& setup
->scene
) {
997 assert(setup
->state
== SETUP_ACTIVE
);
999 if (try_update_scene_state(setup
))
1002 /* Update failed, try to restart the scene.
1004 * Cannot call lp_setup_flush_and_restart() directly here
1005 * because of potential recursion.
1007 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1010 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1016 return try_update_scene_state(setup
);
1024 /* Only caller is lp_setup_vbuf_destroy()
1027 lp_setup_destroy( struct lp_setup_context
*setup
)
1031 lp_setup_reset( setup
);
1033 util_unreference_framebuffer_state(&setup
->fb
);
1035 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1036 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1039 pipe_resource_reference(&setup
->constants
.current
, NULL
);
1041 /* free the scenes in the 'empty' queue */
1042 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1043 struct lp_scene
*scene
= setup
->scenes
[i
];
1046 lp_fence_wait(scene
->fence
);
1048 lp_scene_destroy(scene
);
1051 lp_fence_reference(&setup
->last_fence
, NULL
);
1058 * Create a new primitive tiling engine. Plug it into the backend of
1059 * the draw module. Currently also creates a rasterizer to use with
1062 struct lp_setup_context
*
1063 lp_setup_create( struct pipe_context
*pipe
,
1064 struct draw_context
*draw
)
1066 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1067 struct lp_setup_context
*setup
;
1070 setup
= CALLOC_STRUCT(lp_setup_context
);
1075 lp_setup_init_vbuf(setup
);
1077 /* Used only in update_state():
1082 setup
->num_threads
= screen
->num_threads
;
1083 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1088 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1089 draw_set_render(draw
, &setup
->base
);
1091 /* create some empty scenes */
1092 for (i
= 0; i
< MAX_SCENES
; i
++) {
1093 setup
->scenes
[i
] = lp_scene_create( pipe
);
1094 if (!setup
->scenes
[i
]) {
1099 setup
->triangle
= first_triangle
;
1100 setup
->line
= first_line
;
1101 setup
->point
= first_point
;
1108 for (i
= 0; i
< MAX_SCENES
; i
++) {
1109 if (setup
->scenes
[i
]) {
1110 lp_scene_destroy(setup
->scenes
[i
]);
1114 setup
->vbuf
->destroy(setup
->vbuf
);
1123 * Put a BeginQuery command into all bins.
1126 lp_setup_begin_query(struct lp_setup_context
*setup
,
1127 struct llvmpipe_query
*pq
)
1129 /* init the query to its beginning state */
1130 assert(setup
->active_query
[pq
->type
] == NULL
);
1132 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1134 setup
->active_query
[pq
->type
] = pq
;
1136 /* XXX: It is possible that a query is created before the scene
1137 * has been created. This means that setup->scene == NULL resulting
1138 * in the query not being binned and thus is ignored.
1142 if (!lp_scene_bin_everywhere(setup
->scene
,
1143 LP_RAST_OP_BEGIN_QUERY
,
1144 lp_rast_arg_query(pq
))) {
1146 if (!lp_setup_flush_and_restart(setup
))
1149 if (!lp_scene_bin_everywhere(setup
->scene
,
1150 LP_RAST_OP_BEGIN_QUERY
,
1151 lp_rast_arg_query(pq
))) {
1160 * Put an EndQuery command into all bins.
1163 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1165 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1167 if (pq
->type
!= PIPE_QUERY_TIMESTAMP
) {
1168 assert(setup
->active_query
[pq
->type
] == pq
);
1169 setup
->active_query
[pq
->type
] = NULL
;
1172 /* Setup will automatically re-issue any query which carried over a
1173 * scene boundary, and the rasterizer automatically "ends" queries
1174 * which are active at the end of a scene, so there is no need to
1175 * retry this commands on failure.
1178 /* pq->fence should be the fence of the *last* scene which
1179 * contributed to the query result.
1181 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1183 if (!lp_scene_bin_everywhere(setup
->scene
,
1184 LP_RAST_OP_END_QUERY
,
1185 lp_rast_arg_query(pq
))) {
1186 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
1190 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1196 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1198 if (0) debug_printf("%s\n", __FUNCTION__
);
1200 assert(setup
->state
== SETUP_ACTIVE
);
1202 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1205 if (!lp_setup_update_state(setup
, TRUE
))