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
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "util/u_surface.h"
40 #include "lp_context.h"
42 #include "lp_scene_queue.h"
43 #include "lp_texture.h"
47 #include "lp_setup_context.h"
48 #include "lp_screen.h"
50 #include "state_tracker/sw_winsys.h"
52 #include "draw/draw_context.h"
53 #include "draw/draw_vbuf.h"
56 static void set_scene_state( struct lp_setup_context
*, enum setup_state
);
60 lp_setup_get_current_scene(struct lp_setup_context
*setup
)
64 /* wait for a free/empty scene
66 setup
->scene
= lp_scene_dequeue(setup
->empty_scenes
, TRUE
);
68 assert(lp_scene_is_empty(setup
->scene
));
70 lp_scene_begin_binning(setup
->scene
,
78 first_triangle( struct lp_setup_context
*setup
,
83 set_scene_state( setup
, SETUP_ACTIVE
);
84 lp_setup_choose_triangle( setup
);
85 setup
->triangle( setup
, v0
, v1
, v2
);
89 first_line( struct lp_setup_context
*setup
,
93 set_scene_state( setup
, SETUP_ACTIVE
);
94 lp_setup_choose_line( setup
);
95 setup
->line( setup
, v0
, v1
);
99 first_point( struct lp_setup_context
*setup
,
100 const float (*v0
)[4])
102 set_scene_state( setup
, SETUP_ACTIVE
);
103 lp_setup_choose_point( setup
);
104 setup
->point( setup
, v0
);
107 static void reset_context( struct lp_setup_context
*setup
)
109 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
111 /* Reset derived state */
112 setup
->constants
.stored_size
= 0;
113 setup
->constants
.stored_data
= NULL
;
114 setup
->fs
.stored
= NULL
;
122 setup
->clear
.flags
= 0;
124 /* Have an explicit "start-binning" call and get rid of this
127 setup
->line
= first_line
;
128 setup
->point
= first_point
;
129 setup
->triangle
= first_triangle
;
133 /** Rasterize all scene's bins */
135 lp_setup_rasterize_scene( struct lp_setup_context
*setup
,
136 boolean write_depth
)
138 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
140 lp_scene_rasterize(scene
,
144 reset_context( setup
);
146 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
152 begin_binning( struct lp_setup_context
*setup
)
154 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
156 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
157 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
158 (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) ? "clear": "load");
160 if (setup
->fb
.nr_cbufs
) {
161 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
162 lp_scene_bin_everywhere( scene
,
164 setup
->clear
.color
);
165 scene
->has_color_clear
= TRUE
;
169 if (setup
->fb
.zsbuf
) {
170 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
171 lp_scene_bin_everywhere( scene
,
172 lp_rast_clear_zstencil
,
173 setup
->clear
.zstencil
);
174 scene
->has_depth_clear
= TRUE
;
178 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
182 /* This basically bins and then flushes any outstanding full-screen
185 * TODO: fast path for fullscreen clears and no triangles.
188 execute_clears( struct lp_setup_context
*setup
)
190 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
192 begin_binning( setup
);
193 lp_setup_rasterize_scene( setup
, TRUE
);
198 set_scene_state( struct lp_setup_context
*setup
,
199 enum setup_state new_state
)
201 unsigned old_state
= setup
->state
;
203 if (old_state
== new_state
)
206 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
210 begin_binning( setup
);
214 if (old_state
== SETUP_ACTIVE
) {
221 if (old_state
== SETUP_CLEARED
)
222 execute_clears( setup
);
224 lp_setup_rasterize_scene( setup
, TRUE
);
228 assert(0 && "invalid setup state mode");
231 setup
->state
= new_state
;
236 * \param flags bitmask of PIPE_FLUSH_x flags
239 lp_setup_flush( struct lp_setup_context
*setup
,
242 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
245 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
246 union lp_rast_cmd_arg dummy
;
248 if (flags
& (PIPE_FLUSH_SWAPBUFFERS
|
250 /* Store colors in the linear color buffer(s).
251 * If we don't do this here, we'll end up converting the tiled
252 * data to linear in the texture_unmap() function, which will
253 * not be a parallel/threaded operation as here.
255 lp_scene_bin_everywhere(scene
, lp_rast_store_color
, dummy
);
259 set_scene_state( setup
, SETUP_FLUSHED
);
264 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
265 const struct pipe_framebuffer_state
*fb
)
267 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
269 /* Flush any old scene.
271 set_scene_state( setup
, SETUP_FLUSHED
);
273 /* Set new state. This will be picked up later when we next need a
276 util_copy_framebuffer_state(&setup
->fb
, fb
);
281 lp_setup_clear( struct lp_setup_context
*setup
,
287 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
290 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
293 if (flags
& PIPE_CLEAR_COLOR
) {
294 for (i
= 0; i
< 4; ++i
)
295 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
298 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
299 setup
->clear
.zstencil
.clear_zstencil
=
300 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
305 if (setup
->state
== SETUP_ACTIVE
) {
306 /* Add the clear to existing scene. In the unusual case where
307 * both color and depth-stencil are being cleared when there's
308 * already been some rendering, we could discard the currently
309 * binned scene and start again, but I don't see that as being
312 if (flags
& PIPE_CLEAR_COLOR
) {
313 lp_scene_bin_everywhere( scene
,
315 setup
->clear
.color
);
316 scene
->has_color_clear
= TRUE
;
319 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
320 lp_scene_bin_everywhere( scene
,
321 lp_rast_clear_zstencil
,
322 setup
->clear
.zstencil
);
323 scene
->has_depth_clear
= TRUE
;
328 /* Put ourselves into the 'pre-clear' state, specifically to try
329 * and accumulate multiple clears to color and depth_stencil
330 * buffers which the app or state-tracker might issue
333 set_scene_state( setup
, SETUP_CLEARED
);
335 setup
->clear
.flags
|= flags
;
343 struct pipe_fence_handle
*
344 lp_setup_fence( struct lp_setup_context
*setup
)
346 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
347 const unsigned rank
= lp_scene_get_num_bins( scene
); /* xxx */
348 struct lp_fence
*fence
= lp_fence_create(rank
);
350 LP_DBG(DEBUG_SETUP
, "%s rank %u\n", __FUNCTION__
, rank
);
352 set_scene_state( setup
, SETUP_ACTIVE
);
354 /* insert the fence into all command bins */
355 lp_scene_bin_everywhere( scene
,
357 lp_rast_arg_fence(fence
) );
359 return (struct pipe_fence_handle
*) fence
;
364 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
366 boolean ccw_is_frontface
,
368 boolean gl_rasterization_rules
)
370 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
372 setup
->ccw_is_frontface
= ccw_is_frontface
;
373 setup
->cullmode
= cull_mode
;
374 setup
->triangle
= first_triangle
;
375 setup
->scissor_test
= scissor
;
376 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
382 lp_setup_set_fs_inputs( struct lp_setup_context
*setup
,
383 const struct lp_shader_input
*input
,
386 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
388 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
389 setup
->fs
.nr_inputs
= nr
;
393 lp_setup_set_fs_functions( struct lp_setup_context
*setup
,
394 lp_jit_frag_func jit_function0
,
395 lp_jit_frag_func jit_function1
,
398 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) jit_function0
);
399 /* FIXME: reference count */
401 setup
->fs
.current
.jit_function
[0] = jit_function0
;
402 setup
->fs
.current
.jit_function
[1] = jit_function1
;
403 setup
->fs
.current
.opaque
= opaque
;
404 setup
->dirty
|= LP_SETUP_NEW_FS
;
408 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
409 struct pipe_resource
*buffer
)
411 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
413 pipe_resource_reference(&setup
->constants
.current
, buffer
);
415 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
420 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
421 float alpha_ref_value
)
423 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
425 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
426 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
427 setup
->dirty
|= LP_SETUP_NEW_FS
;
432 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
433 const ubyte refs
[2] )
435 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
437 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
438 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
439 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
440 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
441 setup
->dirty
|= LP_SETUP_NEW_FS
;
446 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
447 const struct pipe_blend_color
*blend_color
)
449 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
453 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
454 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
455 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
461 lp_setup_set_scissor( struct lp_setup_context
*setup
,
462 const struct pipe_scissor_state
*scissor
)
464 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
468 if (memcmp(&setup
->scissor
.current
, scissor
, sizeof(*scissor
)) != 0) {
469 setup
->scissor
.current
= *scissor
; /* struct copy */
470 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
476 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
477 boolean flatshade_first
)
479 setup
->flatshade_first
= flatshade_first
;
484 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
485 struct vertex_info
*vertex_info
)
487 /* XXX: just silently holding onto the pointer:
489 setup
->vertex_info
= vertex_info
;
494 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
497 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
499 struct pipe_sampler_view
**views
)
503 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
505 assert(num
<= PIPE_MAX_SAMPLERS
);
507 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
508 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
511 struct pipe_resource
*tex
= view
->texture
;
512 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
513 struct lp_jit_texture
*jit_tex
;
514 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
515 jit_tex
->width
= tex
->width0
;
516 jit_tex
->height
= tex
->height0
;
517 jit_tex
->depth
= tex
->depth0
;
518 jit_tex
->last_level
= tex
->last_level
;
520 /* We're referencing the texture's internal data, so save a
523 pipe_resource_reference(&setup
->fs
.current_tex
[i
], tex
);
526 /* regular texture - setup array of mipmap level pointers */
528 for (j
= 0; j
<= tex
->last_level
; j
++) {
531 (ubyte
*) lp_tex
->data
+ lp_tex
->level_offset
[j
];
534 llvmpipe_get_texture_image_all(lp_tex
, j
, LP_TEX_USAGE_READ
,
535 LP_TEX_LAYOUT_LINEAR
);
537 jit_tex
->row_stride
[j
] = lp_tex
->stride
[j
];
541 /* display target texture/surface */
543 * XXX: Where should this be unmapped?
546 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
547 struct sw_winsys
*winsys
= screen
->winsys
;
548 jit_tex
->data
[0] = winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
550 jit_tex
->row_stride
[0] = lp_tex
->stride
[0];
551 assert(jit_tex
->data
[0]);
556 setup
->dirty
|= LP_SETUP_NEW_FS
;
561 * Is the given texture referenced by any scene?
562 * Note: we have to check all scenes including any scenes currently
563 * being rendered and the current scene being built.
566 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
567 const struct pipe_resource
*texture
)
571 /* check the render targets */
572 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
573 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
574 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
576 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
577 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
580 /* check textures referenced by the scene */
581 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
582 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
583 return PIPE_REFERENCED_FOR_READ
;
587 return PIPE_UNREFERENCED
;
592 * Called by vbuf code when we're about to draw something.
595 lp_setup_update_state( struct lp_setup_context
*setup
)
597 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
599 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
601 assert(setup
->fs
.current
.jit_function
);
603 /* Some of the 'draw' pipeline stages may have changed some driver state.
604 * Make sure we've processed those state changes before anything else.
606 * XXX this is the only place where llvmpipe_context is used in the
607 * setup code. This may get refactored/changed...
610 struct llvmpipe_context
*lp
= llvmpipe_context(scene
->pipe
);
612 llvmpipe_update_derived(lp
);
614 assert(lp
->dirty
== 0);
617 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
621 stored
= lp_scene_alloc_aligned(scene
, 4 * 16, 16);
623 /* smear each blend color component across 16 ubyte elements */
624 for (i
= 0; i
< 4; ++i
) {
625 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
626 for (j
= 0; j
< 16; ++j
)
627 stored
[i
*16 + j
] = c
;
630 setup
->blend_color
.stored
= stored
;
632 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
633 setup
->dirty
|= LP_SETUP_NEW_FS
;
636 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
639 stored
= lp_scene_alloc_aligned(scene
, 4 * sizeof(int32_t), 16);
641 stored
[0] = (float) setup
->scissor
.current
.minx
;
642 stored
[1] = (float) setup
->scissor
.current
.miny
;
643 stored
[2] = (float) setup
->scissor
.current
.maxx
;
644 stored
[3] = (float) setup
->scissor
.current
.maxy
;
646 setup
->scissor
.stored
= stored
;
648 setup
->fs
.current
.jit_context
.scissor_xmin
= stored
[0];
649 setup
->fs
.current
.jit_context
.scissor_ymin
= stored
[1];
650 setup
->fs
.current
.jit_context
.scissor_xmax
= stored
[2];
651 setup
->fs
.current
.jit_context
.scissor_ymax
= stored
[3];
653 setup
->dirty
|= LP_SETUP_NEW_FS
;
656 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
657 struct pipe_resource
*buffer
= setup
->constants
.current
;
660 unsigned current_size
= buffer
->width0
;
661 const void *current_data
= llvmpipe_resource_data(buffer
);
663 /* TODO: copy only the actually used constants? */
665 if(setup
->constants
.stored_size
!= current_size
||
666 !setup
->constants
.stored_data
||
667 memcmp(setup
->constants
.stored_data
,
669 current_size
) != 0) {
672 stored
= lp_scene_alloc(scene
, current_size
);
677 setup
->constants
.stored_size
= current_size
;
678 setup
->constants
.stored_data
= stored
;
683 setup
->constants
.stored_size
= 0;
684 setup
->constants
.stored_data
= NULL
;
687 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
688 setup
->dirty
|= LP_SETUP_NEW_FS
;
692 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
693 if(!setup
->fs
.stored
||
694 memcmp(setup
->fs
.stored
,
696 sizeof setup
->fs
.current
) != 0) {
697 /* The fs state that's been stored in the scene is different from
698 * the new, current state. So allocate a new lp_rast_state object
699 * and append it to the bin's setup data buffer.
702 struct lp_rast_state
*stored
=
703 (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
707 sizeof setup
->fs
.current
);
708 setup
->fs
.stored
= stored
;
710 /* put the state-set command into all bins */
711 lp_scene_bin_state_command( scene
,
713 lp_rast_arg_state(setup
->fs
.stored
) );
716 /* The scene now references the textures in the rasterization
717 * state record. Note that now.
719 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
720 if (setup
->fs
.current_tex
[i
])
721 lp_scene_texture_reference(scene
, setup
->fs
.current_tex
[i
]);
728 assert(setup
->fs
.stored
);
733 /* Only caller is lp_setup_vbuf_destroy()
736 lp_setup_destroy( struct lp_setup_context
*setup
)
740 reset_context( setup
);
742 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
743 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
746 pipe_resource_reference(&setup
->constants
.current
, NULL
);
748 /* free the scenes in the 'empty' queue */
750 struct lp_scene
*scene
= lp_scene_dequeue(setup
->empty_scenes
, FALSE
);
753 lp_scene_destroy(scene
);
756 lp_rast_destroy( setup
->rast
);
763 * Create a new primitive tiling engine. Plug it into the backend of
764 * the draw module. Currently also creates a rasterizer to use with
767 struct lp_setup_context
*
768 lp_setup_create( struct pipe_context
*pipe
,
769 struct draw_context
*draw
)
772 struct lp_setup_context
*setup
= CALLOC_STRUCT(lp_setup_context
);
777 lp_setup_init_vbuf(setup
);
779 setup
->empty_scenes
= lp_scene_queue_create();
780 if (!setup
->empty_scenes
)
783 /* XXX: move this to the screen and share between contexts:
785 setup
->rast
= lp_rast_create();
789 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
793 draw_set_rasterize_stage(draw
, setup
->vbuf
);
794 draw_set_render(draw
, &setup
->base
);
796 /* create some empty scenes */
797 for (i
= 0; i
< MAX_SCENES
; i
++) {
798 setup
->scenes
[i
] = lp_scene_create( pipe
, setup
->empty_scenes
);
800 lp_scene_enqueue(setup
->empty_scenes
, setup
->scenes
[i
]);
803 setup
->triangle
= first_triangle
;
804 setup
->line
= first_line
;
805 setup
->point
= first_point
;
813 lp_rast_destroy( setup
->rast
);
818 if (setup
->empty_scenes
)
819 lp_scene_queue_destroy(setup
->empty_scenes
);