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 "pipe/p_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"
49 #include "draw/draw_context.h"
50 #include "draw/draw_vbuf.h"
53 static void set_scene_state( struct setup_context
*, unsigned );
57 lp_setup_get_current_scene(struct setup_context
*setup
)
60 /* wait for a free/empty bin */
61 setup
->scene
= lp_scene_dequeue(setup
->empty_scenes
);
62 if(0)lp_scene_reset( setup
->scene
); /* XXX temporary? */
64 lp_scene_set_framebuffer_size(setup
->scene
,
73 first_triangle( struct setup_context
*setup
,
78 set_scene_state( setup
, SETUP_ACTIVE
);
79 lp_setup_choose_triangle( setup
);
80 setup
->triangle( setup
, v0
, v1
, v2
);
84 first_line( struct setup_context
*setup
,
88 set_scene_state( setup
, SETUP_ACTIVE
);
89 lp_setup_choose_line( setup
);
90 setup
->line( setup
, v0
, v1
);
94 first_point( struct setup_context
*setup
,
97 set_scene_state( setup
, SETUP_ACTIVE
);
98 lp_setup_choose_point( setup
);
99 setup
->point( setup
, v0
);
102 static void reset_context( struct setup_context
*setup
)
104 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
106 /* Reset derived state */
107 setup
->constants
.stored_size
= 0;
108 setup
->constants
.stored_data
= NULL
;
109 setup
->fs
.stored
= NULL
;
117 setup
->clear
.flags
= 0;
119 /* Have an explicit "start-binning" call and get rid of this
122 setup
->line
= first_line
;
123 setup
->point
= first_point
;
124 setup
->triangle
= first_triangle
;
128 /** Rasterize all scene's bins */
130 lp_setup_rasterize_scene( struct setup_context
*setup
,
131 boolean write_depth
)
133 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
135 lp_rasterize_scene(setup
->rast
,
140 reset_context( setup
);
142 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
148 begin_binning( struct setup_context
*setup
)
150 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
152 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
153 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
154 (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) ? "clear": "load");
156 if (setup
->fb
.nr_cbufs
) {
157 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
158 lp_scene_bin_everywhere( scene
,
160 setup
->clear
.color
);
162 lp_scene_bin_everywhere( scene
,
164 lp_rast_arg_null() );
167 if (setup
->fb
.zsbuf
) {
168 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
169 lp_scene_bin_everywhere( scene
,
170 lp_rast_clear_zstencil
,
171 setup
->clear
.zstencil
);
173 lp_scene_bin_everywhere( scene
,
174 lp_rast_load_zstencil
,
175 lp_rast_arg_null() );
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 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 setup_context
*setup
,
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 setup
->state
= new_state
;
233 lp_setup_flush( struct setup_context
*setup
,
236 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
238 set_scene_state( setup
, SETUP_FLUSHED
);
243 lp_setup_bind_framebuffer( struct setup_context
*setup
,
244 const struct pipe_framebuffer_state
*fb
)
246 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
248 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
250 set_scene_state( setup
, SETUP_FLUSHED
);
252 /* re-get scene pointer, may have a new scene after flushing */
253 scene
= lp_setup_get_current_scene(setup
);
255 util_copy_framebuffer_state(&setup
->fb
, fb
);
257 lp_scene_set_framebuffer_size(scene
, setup
->fb
.width
, setup
->fb
.height
);
262 lp_setup_clear( struct setup_context
*setup
,
268 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
271 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
274 if (flags
& PIPE_CLEAR_COLOR
) {
275 for (i
= 0; i
< 4; ++i
)
276 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
279 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
280 setup
->clear
.zstencil
.clear_zstencil
=
281 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
286 if (setup
->state
== SETUP_ACTIVE
) {
287 /* Add the clear to existing scene. In the unusual case where
288 * both color and depth-stencil are being cleared when there's
289 * already been some rendering, we could discard the currently
290 * binned scene and start again, but I don't see that as being
293 if (flags
& PIPE_CLEAR_COLOR
)
294 lp_scene_bin_everywhere( scene
,
296 setup
->clear
.color
);
298 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
299 lp_scene_bin_everywhere( scene
,
300 lp_rast_clear_zstencil
,
301 setup
->clear
.zstencil
);
304 /* Put ourselves into the 'pre-clear' state, specifically to try
305 * and accumulate multiple clears to color and depth_stencil
306 * buffers which the app or state-tracker might issue
309 set_scene_state( setup
, SETUP_CLEARED
);
311 setup
->clear
.flags
|= flags
;
319 struct pipe_fence_handle
*
320 lp_setup_fence( struct setup_context
*setup
)
322 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
323 const unsigned rank
= lp_scene_get_num_bins( scene
); /* xxx */
324 struct lp_fence
*fence
= lp_fence_create(rank
);
326 LP_DBG(DEBUG_SETUP
, "%s rank %u\n", __FUNCTION__
, rank
);
328 set_scene_state( setup
, SETUP_ACTIVE
);
330 /* insert the fence into all command bins */
331 lp_scene_bin_everywhere( scene
,
333 lp_rast_arg_fence(fence
) );
335 return (struct pipe_fence_handle
*) fence
;
340 lp_setup_set_triangle_state( struct setup_context
*setup
,
342 boolean ccw_is_frontface
)
344 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
346 setup
->ccw_is_frontface
= ccw_is_frontface
;
347 setup
->cullmode
= cull_mode
;
348 setup
->triangle
= first_triangle
;
354 lp_setup_set_fs_inputs( struct setup_context
*setup
,
355 const struct lp_shader_input
*input
,
358 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
360 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
361 setup
->fs
.nr_inputs
= nr
;
365 lp_setup_set_fs_function( struct setup_context
*setup
,
366 lp_jit_frag_func jit_function
,
369 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) jit_function
);
370 /* FIXME: reference count */
372 setup
->fs
.current
.jit_function
= jit_function
;
373 setup
->fs
.current
.opaque
= opaque
;
374 setup
->dirty
|= LP_SETUP_NEW_FS
;
378 lp_setup_set_fs_constants(struct setup_context
*setup
,
379 struct pipe_buffer
*buffer
)
381 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
383 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
385 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
390 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
391 float alpha_ref_value
)
393 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
395 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
396 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
397 setup
->dirty
|= LP_SETUP_NEW_FS
;
402 lp_setup_set_blend_color( struct setup_context
*setup
,
403 const struct pipe_blend_color
*blend_color
)
405 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
409 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
410 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
411 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
417 lp_setup_set_flatshade_first( struct setup_context
*setup
,
418 boolean flatshade_first
)
420 setup
->flatshade_first
= flatshade_first
;
425 lp_setup_set_vertex_info( struct setup_context
*setup
,
426 struct vertex_info
*vertex_info
)
428 /* XXX: just silently holding onto the pointer:
430 setup
->vertex_info
= vertex_info
;
435 * Called during state validation when LP_NEW_TEXTURE is set.
438 lp_setup_set_sampler_textures( struct setup_context
*setup
,
439 unsigned num
, struct pipe_texture
**texture
)
443 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
445 assert(num
<= PIPE_MAX_SAMPLERS
);
447 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
448 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
451 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
452 struct lp_jit_texture
*jit_tex
;
453 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
454 jit_tex
->width
= tex
->width0
;
455 jit_tex
->height
= tex
->height0
;
456 jit_tex
->stride
= lp_tex
->stride
[0];
458 jit_tex
->data
= lp_tex
->data
;
460 /* FIXME: map the rendertarget */
463 /* the scene references this texture */
465 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
466 lp_scene_texture_reference(scene
, tex
);
471 setup
->dirty
|= LP_SETUP_NEW_FS
;
476 * Is the given texture referenced by any scene?
477 * Note: we have to check all scenes including any scenes currently
478 * being rendered and the current scene being built.
481 lp_setup_is_texture_referenced( const struct setup_context
*setup
,
482 const struct pipe_texture
*texture
)
485 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
486 if (lp_scene_is_textured_referenced(setup
->scenes
[i
], texture
)) {
487 return PIPE_REFERENCED_FOR_READ
;
490 return PIPE_UNREFERENCED
;
495 * Called by vbuf code when we're about to draw something.
498 lp_setup_update_state( struct setup_context
*setup
)
500 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
502 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
504 assert(setup
->fs
.current
.jit_function
);
506 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
510 stored
= lp_scene_alloc_aligned(scene
, 4 * 16, 16);
512 /* smear each blend color component across 16 ubyte elements */
513 for (i
= 0; i
< 4; ++i
) {
514 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
515 for (j
= 0; j
< 16; ++j
)
516 stored
[i
*16 + j
] = c
;
519 setup
->blend_color
.stored
= stored
;
521 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
522 setup
->dirty
|= LP_SETUP_NEW_FS
;
526 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
527 struct pipe_buffer
*buffer
= setup
->constants
.current
;
530 unsigned current_size
= buffer
->size
;
531 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
533 /* TODO: copy only the actually used constants? */
535 if(setup
->constants
.stored_size
!= current_size
||
536 !setup
->constants
.stored_data
||
537 memcmp(setup
->constants
.stored_data
,
539 current_size
) != 0) {
542 stored
= lp_scene_alloc(scene
, current_size
);
547 setup
->constants
.stored_size
= current_size
;
548 setup
->constants
.stored_data
= stored
;
553 setup
->constants
.stored_size
= 0;
554 setup
->constants
.stored_data
= NULL
;
557 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
558 setup
->dirty
|= LP_SETUP_NEW_FS
;
562 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
563 if(!setup
->fs
.stored
||
564 memcmp(setup
->fs
.stored
,
566 sizeof setup
->fs
.current
) != 0) {
567 /* The fs state that's been stored in the scene is different from
568 * the new, current state. So allocate a new lp_rast_state object
569 * and append it to the bin's setup data buffer.
571 struct lp_rast_state
*stored
=
572 (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
576 sizeof setup
->fs
.current
);
577 setup
->fs
.stored
= stored
;
579 /* put the state-set command into all bins */
580 lp_scene_bin_state_command( scene
,
582 lp_rast_arg_state(setup
->fs
.stored
) );
589 assert(setup
->fs
.stored
);
594 /* Only caller is lp_setup_vbuf_destroy()
597 lp_setup_destroy( struct setup_context
*setup
)
599 reset_context( setup
);
601 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
603 /* free the scenes in the 'empty' queue */
604 while (lp_scene_queue_count(setup
->empty_scenes
) > 0) {
605 struct lp_scene
*scene
= lp_scene_dequeue(setup
->empty_scenes
);
608 lp_scene_destroy(scene
);
611 lp_rast_destroy( setup
->rast
);
618 * Create a new primitive tiling engine. Plug it into the backend of
619 * the draw module. Currently also creates a rasterizer to use with
622 struct setup_context
*
623 lp_setup_create( struct pipe_screen
*screen
,
624 struct draw_context
*draw
)
627 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
632 lp_setup_init_vbuf(setup
);
634 setup
->empty_scenes
= lp_scene_queue_create();
635 if (!setup
->empty_scenes
)
638 setup
->rast
= lp_rast_create( screen
, setup
->empty_scenes
);
642 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
646 draw_set_rasterize_stage(draw
, setup
->vbuf
);
647 draw_set_render(draw
, &setup
->base
);
649 /* create some empty scenes */
650 for (i
= 0; i
< MAX_SCENES
; i
++) {
651 setup
->scenes
[i
] = lp_scene_create();
652 lp_scene_enqueue(setup
->empty_scenes
, setup
->scenes
[i
]);
655 setup
->triangle
= first_triangle
;
656 setup
->line
= first_line
;
657 setup
->point
= first_point
;
665 lp_rast_destroy( setup
->rast
);
670 if (setup
->empty_scenes
)
671 lp_scene_queue_destroy(setup
->empty_scenes
);