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 "lp_winsys.h"
51 #include "draw/draw_context.h"
52 #include "draw/draw_vbuf.h"
55 static void set_scene_state( struct setup_context
*, unsigned );
59 lp_setup_get_current_scene(struct setup_context
*setup
)
63 /* wait for a free/empty scene
65 setup
->scene
= lp_scene_dequeue(setup
->empty_scenes
, TRUE
);
67 if(0)lp_scene_reset( setup
->scene
); /* XXX temporary? */
69 lp_scene_set_framebuffer_size(setup
->scene
,
78 first_triangle( struct 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 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 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 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 setup_context
*setup
,
136 boolean write_depth
)
138 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
140 lp_rasterize_scene(setup
->rast
,
145 reset_context( setup
);
147 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
153 begin_binning( struct setup_context
*setup
)
155 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
157 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
158 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
159 (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) ? "clear": "load");
161 if (setup
->fb
.nr_cbufs
) {
162 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
163 lp_scene_bin_everywhere( scene
,
165 setup
->clear
.color
);
167 lp_scene_bin_everywhere( scene
,
169 lp_rast_arg_null() );
172 if (setup
->fb
.zsbuf
) {
173 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
174 lp_scene_bin_everywhere( scene
,
175 lp_rast_clear_zstencil
,
176 setup
->clear
.zstencil
);
179 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
183 /* This basically bins and then flushes any outstanding full-screen
186 * TODO: fast path for fullscreen clears and no triangles.
189 execute_clears( struct setup_context
*setup
)
191 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
193 begin_binning( setup
);
194 lp_setup_rasterize_scene( setup
, TRUE
);
199 set_scene_state( struct setup_context
*setup
,
202 unsigned old_state
= setup
->state
;
204 if (old_state
== new_state
)
207 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
211 begin_binning( setup
);
215 if (old_state
== SETUP_ACTIVE
) {
222 if (old_state
== SETUP_CLEARED
)
223 execute_clears( setup
);
225 lp_setup_rasterize_scene( setup
, TRUE
);
229 setup
->state
= new_state
;
234 lp_setup_flush( struct setup_context
*setup
,
237 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
239 set_scene_state( setup
, SETUP_FLUSHED
);
244 lp_setup_bind_framebuffer( struct setup_context
*setup
,
245 const struct pipe_framebuffer_state
*fb
)
247 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
249 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
251 set_scene_state( setup
, SETUP_FLUSHED
);
253 /* re-get scene pointer, may have a new scene after flushing */
254 scene
= lp_setup_get_current_scene(setup
);
256 util_copy_framebuffer_state(&setup
->fb
, fb
);
258 lp_scene_set_framebuffer_size(scene
, setup
->fb
.width
, setup
->fb
.height
);
263 lp_setup_clear( struct setup_context
*setup
,
269 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
272 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
275 if (flags
& PIPE_CLEAR_COLOR
) {
276 for (i
= 0; i
< 4; ++i
)
277 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
280 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
281 setup
->clear
.zstencil
.clear_zstencil
=
282 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
287 if (setup
->state
== SETUP_ACTIVE
) {
288 /* Add the clear to existing scene. In the unusual case where
289 * both color and depth-stencil are being cleared when there's
290 * already been some rendering, we could discard the currently
291 * binned scene and start again, but I don't see that as being
294 if (flags
& PIPE_CLEAR_COLOR
)
295 lp_scene_bin_everywhere( scene
,
297 setup
->clear
.color
);
299 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
300 lp_scene_bin_everywhere( scene
,
301 lp_rast_clear_zstencil
,
302 setup
->clear
.zstencil
);
305 /* Put ourselves into the 'pre-clear' state, specifically to try
306 * and accumulate multiple clears to color and depth_stencil
307 * buffers which the app or state-tracker might issue
310 set_scene_state( setup
, SETUP_CLEARED
);
312 setup
->clear
.flags
|= flags
;
320 struct pipe_fence_handle
*
321 lp_setup_fence( struct setup_context
*setup
)
323 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
324 const unsigned rank
= lp_scene_get_num_bins( scene
); /* xxx */
325 struct lp_fence
*fence
= lp_fence_create(rank
);
327 LP_DBG(DEBUG_SETUP
, "%s rank %u\n", __FUNCTION__
, rank
);
329 set_scene_state( setup
, SETUP_ACTIVE
);
331 /* insert the fence into all command bins */
332 lp_scene_bin_everywhere( scene
,
334 lp_rast_arg_fence(fence
) );
336 return (struct pipe_fence_handle
*) fence
;
341 lp_setup_set_triangle_state( struct setup_context
*setup
,
343 boolean ccw_is_frontface
,
346 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
348 setup
->ccw_is_frontface
= ccw_is_frontface
;
349 setup
->cullmode
= cull_mode
;
350 setup
->triangle
= first_triangle
;
351 setup
->scissor_test
= scissor
;
357 lp_setup_set_fs_inputs( struct setup_context
*setup
,
358 const struct lp_shader_input
*input
,
361 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
363 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
364 setup
->fs
.nr_inputs
= nr
;
368 lp_setup_set_fs_functions( struct setup_context
*setup
,
369 lp_jit_frag_func jit_function0
,
370 lp_jit_frag_func jit_function1
,
373 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) jit_function0
);
374 /* FIXME: reference count */
376 setup
->fs
.current
.jit_function
[0] = jit_function0
;
377 setup
->fs
.current
.jit_function
[1] = jit_function1
;
378 setup
->fs
.current
.opaque
= opaque
;
379 setup
->dirty
|= LP_SETUP_NEW_FS
;
383 lp_setup_set_fs_constants(struct setup_context
*setup
,
384 struct pipe_buffer
*buffer
)
386 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
388 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
390 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
395 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
396 float alpha_ref_value
)
398 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
400 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
401 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
402 setup
->dirty
|= LP_SETUP_NEW_FS
;
407 lp_setup_set_blend_color( struct setup_context
*setup
,
408 const struct pipe_blend_color
*blend_color
)
410 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
414 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
415 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
416 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
422 lp_setup_set_scissor( struct setup_context
*setup
,
423 const struct pipe_scissor_state
*scissor
)
425 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
429 if (memcmp(&setup
->scissor
.current
, scissor
, sizeof(*scissor
)) != 0) {
430 setup
->scissor
.current
= *scissor
; /* struct copy */
431 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
437 lp_setup_set_flatshade_first( struct setup_context
*setup
,
438 boolean flatshade_first
)
440 setup
->flatshade_first
= flatshade_first
;
445 lp_setup_set_vertex_info( struct setup_context
*setup
,
446 struct vertex_info
*vertex_info
)
448 /* XXX: just silently holding onto the pointer:
450 setup
->vertex_info
= vertex_info
;
455 * Called during state validation when LP_NEW_TEXTURE is set.
458 lp_setup_set_sampler_textures( struct setup_context
*setup
,
459 unsigned num
, struct pipe_texture
**texture
)
463 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
465 assert(num
<= PIPE_MAX_SAMPLERS
);
467 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
468 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
471 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
472 struct lp_jit_texture
*jit_tex
;
473 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
474 jit_tex
->width
= tex
->width0
;
475 jit_tex
->height
= tex
->height0
;
476 jit_tex
->stride
= lp_tex
->stride
[0];
478 jit_tex
->data
= lp_tex
->data
;
482 * XXX: Where should this be unmapped?
485 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
486 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
487 jit_tex
->data
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
488 PIPE_BUFFER_USAGE_CPU_READ
);
489 assert(jit_tex
->data
);
492 /* the scene references this texture */
494 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
495 lp_scene_texture_reference(scene
, tex
);
500 setup
->dirty
|= LP_SETUP_NEW_FS
;
505 * Is the given texture referenced by any scene?
506 * Note: we have to check all scenes including any scenes currently
507 * being rendered and the current scene being built.
510 lp_setup_is_texture_referenced( const struct setup_context
*setup
,
511 const struct pipe_texture
*texture
)
515 /* check the render targets */
516 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
517 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
518 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
520 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
521 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
524 /* check textures referenced by the scene */
525 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
526 if (lp_scene_is_texture_referenced(setup
->scenes
[i
], texture
)) {
527 return PIPE_REFERENCED_FOR_READ
;
531 return PIPE_UNREFERENCED
;
536 * Called by vbuf code when we're about to draw something.
539 lp_setup_update_state( struct setup_context
*setup
)
541 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
543 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
545 assert(setup
->fs
.current
.jit_function
);
547 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
551 stored
= lp_scene_alloc_aligned(scene
, 4 * 16, 16);
553 /* smear each blend color component across 16 ubyte elements */
554 for (i
= 0; i
< 4; ++i
) {
555 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
556 for (j
= 0; j
< 16; ++j
)
557 stored
[i
*16 + j
] = c
;
560 setup
->blend_color
.stored
= stored
;
562 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
563 setup
->dirty
|= LP_SETUP_NEW_FS
;
566 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
569 stored
= lp_scene_alloc_aligned(scene
, 4 * sizeof(int32_t), 16);
571 stored
[0] = (float) setup
->scissor
.current
.minx
;
572 stored
[1] = (float) setup
->scissor
.current
.miny
;
573 stored
[2] = (float) setup
->scissor
.current
.maxx
;
574 stored
[3] = (float) setup
->scissor
.current
.maxy
;
576 setup
->scissor
.stored
= stored
;
578 setup
->fs
.current
.jit_context
.scissor_xmin
= stored
[0];
579 setup
->fs
.current
.jit_context
.scissor_ymin
= stored
[1];
580 setup
->fs
.current
.jit_context
.scissor_xmax
= stored
[2];
581 setup
->fs
.current
.jit_context
.scissor_ymax
= stored
[3];
583 setup
->dirty
|= LP_SETUP_NEW_FS
;
586 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
587 struct pipe_buffer
*buffer
= setup
->constants
.current
;
590 unsigned current_size
= buffer
->size
;
591 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
593 /* TODO: copy only the actually used constants? */
595 if(setup
->constants
.stored_size
!= current_size
||
596 !setup
->constants
.stored_data
||
597 memcmp(setup
->constants
.stored_data
,
599 current_size
) != 0) {
602 stored
= lp_scene_alloc(scene
, current_size
);
607 setup
->constants
.stored_size
= current_size
;
608 setup
->constants
.stored_data
= stored
;
613 setup
->constants
.stored_size
= 0;
614 setup
->constants
.stored_data
= NULL
;
617 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
618 setup
->dirty
|= LP_SETUP_NEW_FS
;
622 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
623 if(!setup
->fs
.stored
||
624 memcmp(setup
->fs
.stored
,
626 sizeof setup
->fs
.current
) != 0) {
627 /* The fs state that's been stored in the scene is different from
628 * the new, current state. So allocate a new lp_rast_state object
629 * and append it to the bin's setup data buffer.
631 struct lp_rast_state
*stored
=
632 (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
636 sizeof setup
->fs
.current
);
637 setup
->fs
.stored
= stored
;
639 /* put the state-set command into all bins */
640 lp_scene_bin_state_command( scene
,
642 lp_rast_arg_state(setup
->fs
.stored
) );
649 assert(setup
->fs
.stored
);
654 /* Only caller is lp_setup_vbuf_destroy()
657 lp_setup_destroy( struct setup_context
*setup
)
659 reset_context( setup
);
661 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
663 /* free the scenes in the 'empty' queue */
665 struct lp_scene
*scene
= lp_scene_dequeue(setup
->empty_scenes
, FALSE
);
668 lp_scene_destroy(scene
);
671 lp_rast_destroy( setup
->rast
);
678 * Create a new primitive tiling engine. Plug it into the backend of
679 * the draw module. Currently also creates a rasterizer to use with
682 struct setup_context
*
683 lp_setup_create( struct pipe_screen
*screen
,
684 struct draw_context
*draw
)
687 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
692 lp_setup_init_vbuf(setup
);
694 setup
->empty_scenes
= lp_scene_queue_create();
695 if (!setup
->empty_scenes
)
698 setup
->rast
= lp_rast_create( screen
, setup
->empty_scenes
);
702 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
706 draw_set_rasterize_stage(draw
, setup
->vbuf
);
707 draw_set_render(draw
, &setup
->base
);
709 /* create some empty scenes */
710 for (i
= 0; i
< MAX_SCENES
; i
++) {
711 setup
->scenes
[i
] = lp_scene_create();
712 lp_scene_enqueue(setup
->empty_scenes
, setup
->scenes
[i
]);
715 setup
->triangle
= first_triangle
;
716 setup
->line
= first_line
;
717 setup
->point
= first_point
;
725 lp_rast_destroy( setup
->rast
);
730 if (setup
->empty_scenes
)
731 lp_scene_queue_destroy(setup
->empty_scenes
);