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"
41 #include "lp_scene_queue.h"
42 #include "lp_buffer.h"
43 #include "lp_texture.h"
47 #include "lp_setup_context.h"
48 #include "lp_screen.h"
49 #include "state_tracker/sw_winsys.h"
51 #include "draw/draw_context.h"
52 #include "draw/draw_vbuf.h"
55 static void set_scene_state( struct lp_setup_context
*, unsigned );
59 lp_setup_get_current_scene(struct lp_setup_context
*setup
)
63 /* wait for a free/empty scene
65 setup
->scene
= lp_scene_dequeue(setup
->empty_scenes
, TRUE
);
67 assert(lp_scene_is_empty(setup
->scene
));
69 lp_scene_begin_binning(setup
->scene
,
77 first_triangle( struct lp_setup_context
*setup
,
82 set_scene_state( setup
, SETUP_ACTIVE
);
83 lp_setup_choose_triangle( setup
);
84 setup
->triangle( setup
, v0
, v1
, v2
);
88 first_line( struct lp_setup_context
*setup
,
92 set_scene_state( setup
, SETUP_ACTIVE
);
93 lp_setup_choose_line( setup
);
94 setup
->line( setup
, v0
, v1
);
98 first_point( struct lp_setup_context
*setup
,
101 set_scene_state( setup
, SETUP_ACTIVE
);
102 lp_setup_choose_point( setup
);
103 setup
->point( setup
, v0
);
106 static void reset_context( struct lp_setup_context
*setup
)
108 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
110 /* Reset derived state */
111 setup
->constants
.stored_size
= 0;
112 setup
->constants
.stored_data
= NULL
;
113 setup
->fs
.stored
= NULL
;
121 setup
->clear
.flags
= 0;
123 /* Have an explicit "start-binning" call and get rid of this
126 setup
->line
= first_line
;
127 setup
->point
= first_point
;
128 setup
->triangle
= first_triangle
;
132 /** Rasterize all scene's bins */
134 lp_setup_rasterize_scene( struct lp_setup_context
*setup
,
135 boolean write_depth
)
137 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
139 lp_scene_rasterize(scene
,
143 reset_context( setup
);
145 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
151 begin_binning( struct lp_setup_context
*setup
)
153 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
155 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
156 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
157 (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) ? "clear": "load");
159 if (setup
->fb
.nr_cbufs
) {
160 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
161 lp_scene_bin_everywhere( scene
,
163 setup
->clear
.color
);
165 lp_scene_bin_everywhere( scene
,
167 lp_rast_arg_null() );
170 if (setup
->fb
.zsbuf
) {
171 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
172 lp_scene_bin_everywhere( scene
,
173 lp_rast_clear_zstencil
,
174 setup
->clear
.zstencil
);
177 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
181 /* This basically bins and then flushes any outstanding full-screen
184 * TODO: fast path for fullscreen clears and no triangles.
187 execute_clears( struct lp_setup_context
*setup
)
189 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
191 begin_binning( setup
);
192 lp_setup_rasterize_scene( setup
, TRUE
);
197 set_scene_state( struct lp_setup_context
*setup
,
200 unsigned old_state
= setup
->state
;
202 if (old_state
== new_state
)
205 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
209 begin_binning( setup
);
213 if (old_state
== SETUP_ACTIVE
) {
220 if (old_state
== SETUP_CLEARED
)
221 execute_clears( setup
);
223 lp_setup_rasterize_scene( setup
, TRUE
);
227 setup
->state
= new_state
;
232 lp_setup_flush( struct lp_setup_context
*setup
,
235 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
237 set_scene_state( setup
, SETUP_FLUSHED
);
242 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
243 const struct pipe_framebuffer_state
*fb
)
245 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
247 /* Flush any old scene.
249 set_scene_state( setup
, SETUP_FLUSHED
);
251 /* Set new state. This will be picked up later when we next need a
254 util_copy_framebuffer_state(&setup
->fb
, fb
);
259 lp_setup_clear( struct lp_setup_context
*setup
,
265 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
268 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
271 if (flags
& PIPE_CLEAR_COLOR
) {
272 for (i
= 0; i
< 4; ++i
)
273 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
276 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
277 setup
->clear
.zstencil
.clear_zstencil
=
278 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
283 if (setup
->state
== SETUP_ACTIVE
) {
284 /* Add the clear to existing scene. In the unusual case where
285 * both color and depth-stencil are being cleared when there's
286 * already been some rendering, we could discard the currently
287 * binned scene and start again, but I don't see that as being
290 if (flags
& PIPE_CLEAR_COLOR
)
291 lp_scene_bin_everywhere( scene
,
293 setup
->clear
.color
);
295 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
296 lp_scene_bin_everywhere( scene
,
297 lp_rast_clear_zstencil
,
298 setup
->clear
.zstencil
);
301 /* Put ourselves into the 'pre-clear' state, specifically to try
302 * and accumulate multiple clears to color and depth_stencil
303 * buffers which the app or state-tracker might issue
306 set_scene_state( setup
, SETUP_CLEARED
);
308 setup
->clear
.flags
|= flags
;
316 struct pipe_fence_handle
*
317 lp_setup_fence( struct lp_setup_context
*setup
)
319 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
320 const unsigned rank
= lp_scene_get_num_bins( scene
); /* xxx */
321 struct lp_fence
*fence
;
323 LP_DBG(DEBUG_SETUP
, "%s rank %u\n", __FUNCTION__
, rank
);
325 if (setup
->state
== SETUP_FLUSHED
) {
326 /* We're in a flushed state so there's nothing in the bins.
327 * No need to wait on a fence.
332 /* There's material in the bins. Emit the fence into the bins.
333 * When the rasterizer(s) find the fence, they'll signal on it.
335 fence
= lp_fence_create(rank
);
337 set_scene_state( setup
, SETUP_ACTIVE
);
339 /* insert the fence into all command bins */
340 lp_scene_bin_everywhere( scene
,
342 lp_rast_arg_fence(fence
) );
346 return (struct pipe_fence_handle
*) fence
;
351 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
353 boolean ccw_is_frontface
,
355 boolean gl_rasterization_rules
)
357 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
359 setup
->ccw_is_frontface
= ccw_is_frontface
;
360 setup
->cullmode
= cull_mode
;
361 setup
->triangle
= first_triangle
;
362 setup
->scissor_test
= scissor
;
363 setup
->pixel_offset
= gl_rasterization_rules
? 0.5f
: 0.0f
;
369 lp_setup_set_fs_inputs( struct lp_setup_context
*setup
,
370 const struct lp_shader_input
*input
,
373 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
375 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
376 setup
->fs
.nr_inputs
= nr
;
380 lp_setup_set_fs_functions( struct lp_setup_context
*setup
,
381 lp_jit_frag_func jit_function0
,
382 lp_jit_frag_func jit_function1
,
385 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) jit_function0
);
386 /* FIXME: reference count */
388 setup
->fs
.current
.jit_function
[0] = jit_function0
;
389 setup
->fs
.current
.jit_function
[1] = jit_function1
;
390 setup
->fs
.current
.opaque
= opaque
;
391 setup
->dirty
|= LP_SETUP_NEW_FS
;
395 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
396 struct pipe_buffer
*buffer
)
398 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
400 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
402 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
407 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
408 float alpha_ref_value
)
410 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
412 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
413 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
414 setup
->dirty
|= LP_SETUP_NEW_FS
;
419 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
420 const ubyte refs
[2] )
422 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
424 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
425 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
426 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
427 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
428 setup
->dirty
|= LP_SETUP_NEW_FS
;
433 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
434 const struct pipe_blend_color
*blend_color
)
436 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
440 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
441 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
442 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
448 lp_setup_set_scissor( struct lp_setup_context
*setup
,
449 const struct pipe_scissor_state
*scissor
)
451 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
455 if (memcmp(&setup
->scissor
.current
, scissor
, sizeof(*scissor
)) != 0) {
456 setup
->scissor
.current
= *scissor
; /* struct copy */
457 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
463 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
464 boolean flatshade_first
)
466 setup
->flatshade_first
= flatshade_first
;
471 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
472 struct vertex_info
*vertex_info
)
474 /* XXX: just silently holding onto the pointer:
476 setup
->vertex_info
= vertex_info
;
481 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
484 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
486 struct pipe_sampler_view
**views
)
490 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
492 assert(num
<= PIPE_MAX_SAMPLERS
);
494 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
495 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
498 struct pipe_texture
*tex
= view
->texture
;
499 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
500 struct lp_jit_texture
*jit_tex
;
501 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
502 jit_tex
->width
= tex
->width0
;
503 jit_tex
->height
= tex
->height0
;
504 jit_tex
->depth
= tex
->depth0
;
505 jit_tex
->last_level
= tex
->last_level
;
507 /* We're referencing the texture's internal data, so save a
510 pipe_texture_reference(&setup
->fs
.current_tex
[i
], tex
);
513 /* regular texture - setup array of mipmap level pointers */
515 for (j
= 0; j
<= tex
->last_level
; j
++) {
517 (ubyte
*) lp_tex
->data
+ lp_tex
->level_offset
[j
];
518 jit_tex
->row_stride
[j
] = lp_tex
->stride
[j
];
522 /* display target texture/surface */
524 * XXX: Where should this be unmapped?
527 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
528 struct sw_winsys
*winsys
= screen
->winsys
;
529 jit_tex
->data
[0] = winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
530 PIPE_BUFFER_USAGE_CPU_READ
);
531 jit_tex
->row_stride
[0] = lp_tex
->stride
[0];
532 assert(jit_tex
->data
[0]);
537 setup
->dirty
|= LP_SETUP_NEW_FS
;
542 * Is the given texture referenced by any scene?
543 * Note: we have to check all scenes including any scenes currently
544 * being rendered and the current scene being built.
547 lp_setup_is_texture_referenced( const struct lp_setup_context
*setup
,
548 const struct pipe_texture
*texture
)
552 /* check the render targets */
553 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
554 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
555 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
557 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
558 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
561 /* check textures referenced by the scene */
562 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
563 if (lp_scene_is_texture_referenced(setup
->scenes
[i
], texture
)) {
564 return PIPE_REFERENCED_FOR_READ
;
568 return PIPE_UNREFERENCED
;
573 * Called by vbuf code when we're about to draw something.
576 lp_setup_update_state( struct lp_setup_context
*setup
)
578 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
580 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
582 assert(setup
->fs
.current
.jit_function
);
584 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
588 stored
= lp_scene_alloc_aligned(scene
, 4 * 16, 16);
590 /* smear each blend color component across 16 ubyte elements */
591 for (i
= 0; i
< 4; ++i
) {
592 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
593 for (j
= 0; j
< 16; ++j
)
594 stored
[i
*16 + j
] = c
;
597 setup
->blend_color
.stored
= stored
;
599 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
600 setup
->dirty
|= LP_SETUP_NEW_FS
;
603 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
606 stored
= lp_scene_alloc_aligned(scene
, 4 * sizeof(int32_t), 16);
608 stored
[0] = (float) setup
->scissor
.current
.minx
;
609 stored
[1] = (float) setup
->scissor
.current
.miny
;
610 stored
[2] = (float) setup
->scissor
.current
.maxx
;
611 stored
[3] = (float) setup
->scissor
.current
.maxy
;
613 setup
->scissor
.stored
= stored
;
615 setup
->fs
.current
.jit_context
.scissor_xmin
= stored
[0];
616 setup
->fs
.current
.jit_context
.scissor_ymin
= stored
[1];
617 setup
->fs
.current
.jit_context
.scissor_xmax
= stored
[2];
618 setup
->fs
.current
.jit_context
.scissor_ymax
= stored
[3];
620 setup
->dirty
|= LP_SETUP_NEW_FS
;
623 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
624 struct pipe_buffer
*buffer
= setup
->constants
.current
;
627 unsigned current_size
= buffer
->size
;
628 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
630 /* TODO: copy only the actually used constants? */
632 if(setup
->constants
.stored_size
!= current_size
||
633 !setup
->constants
.stored_data
||
634 memcmp(setup
->constants
.stored_data
,
636 current_size
) != 0) {
639 stored
= lp_scene_alloc(scene
, current_size
);
644 setup
->constants
.stored_size
= current_size
;
645 setup
->constants
.stored_data
= stored
;
650 setup
->constants
.stored_size
= 0;
651 setup
->constants
.stored_data
= NULL
;
654 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
655 setup
->dirty
|= LP_SETUP_NEW_FS
;
659 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
660 if(!setup
->fs
.stored
||
661 memcmp(setup
->fs
.stored
,
663 sizeof setup
->fs
.current
) != 0) {
664 /* The fs state that's been stored in the scene is different from
665 * the new, current state. So allocate a new lp_rast_state object
666 * and append it to the bin's setup data buffer.
669 struct lp_rast_state
*stored
=
670 (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
674 sizeof setup
->fs
.current
);
675 setup
->fs
.stored
= stored
;
677 /* put the state-set command into all bins */
678 lp_scene_bin_state_command( scene
,
680 lp_rast_arg_state(setup
->fs
.stored
) );
683 /* The scene now references the textures in the rasterization
684 * state record. Note that now.
686 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
687 if (setup
->fs
.current_tex
[i
])
688 lp_scene_texture_reference(scene
, setup
->fs
.current_tex
[i
]);
695 assert(setup
->fs
.stored
);
700 /* Only caller is lp_setup_vbuf_destroy()
703 lp_setup_destroy( struct lp_setup_context
*setup
)
707 reset_context( setup
);
709 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
710 pipe_texture_reference(&setup
->fs
.current_tex
[i
], NULL
);
713 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
715 /* free the scenes in the 'empty' queue */
717 struct lp_scene
*scene
= lp_scene_dequeue(setup
->empty_scenes
, FALSE
);
720 lp_scene_destroy(scene
);
723 lp_rast_destroy( setup
->rast
);
730 * Create a new primitive tiling engine. Plug it into the backend of
731 * the draw module. Currently also creates a rasterizer to use with
734 struct lp_setup_context
*
735 lp_setup_create( struct pipe_context
*pipe
,
736 struct draw_context
*draw
)
739 struct lp_setup_context
*setup
= CALLOC_STRUCT(lp_setup_context
);
744 lp_setup_init_vbuf(setup
);
746 setup
->empty_scenes
= lp_scene_queue_create();
747 if (!setup
->empty_scenes
)
750 /* XXX: move this to the screen and share between contexts:
752 setup
->rast
= lp_rast_create();
756 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
760 draw_set_rasterize_stage(draw
, setup
->vbuf
);
761 draw_set_render(draw
, &setup
->base
);
763 /* create some empty scenes */
764 for (i
= 0; i
< MAX_SCENES
; i
++) {
765 setup
->scenes
[i
] = lp_scene_create( pipe
, setup
->empty_scenes
);
767 lp_scene_enqueue(setup
->empty_scenes
, setup
->scenes
[i
]);
770 setup
->triangle
= first_triangle
;
771 setup
->line
= first_line
;
772 setup
->point
= first_point
;
780 lp_rast_destroy( setup
->rast
);
785 if (setup
->empty_scenes
)
786 lp_scene_queue_destroy(setup
->empty_scenes
);