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 /** XXX temporary value, temporary here */
57 static void set_scene_state( struct setup_context
*, unsigned );
61 lp_setup_get_current_scene(struct setup_context
*setup
)
64 /* wait for a free/empty bin */
65 setup
->scene
= lp_scene_dequeue(setup
->empty_scenes
);
66 if(0)lp_scene_reset( setup
->scene
); /* XXX temporary? */
68 lp_scene_set_framebuffer_size(setup
->scene
,
77 first_triangle( struct 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 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 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 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 setup_context
*setup
,
135 boolean write_depth
)
137 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
139 lp_rasterize_scene(setup
->rast
,
144 reset_context( setup
);
146 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
152 begin_binning( struct 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
);
166 lp_scene_bin_everywhere( scene
,
168 lp_rast_arg_null() );
171 if (setup
->fb
.zsbuf
) {
172 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
173 lp_scene_bin_everywhere( scene
,
174 lp_rast_clear_zstencil
,
175 setup
->clear
.zstencil
);
177 lp_scene_bin_everywhere( scene
,
178 lp_rast_load_zstencil
,
179 lp_rast_arg_null() );
182 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
186 /* This basically bins and then flushes any outstanding full-screen
189 * TODO: fast path for fullscreen clears and no triangles.
192 execute_clears( struct setup_context
*setup
)
194 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
196 begin_binning( setup
);
197 lp_setup_rasterize_scene( setup
, TRUE
);
202 set_scene_state( struct setup_context
*setup
,
205 unsigned old_state
= setup
->state
;
207 if (old_state
== new_state
)
210 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
214 begin_binning( setup
);
218 if (old_state
== SETUP_ACTIVE
) {
225 if (old_state
== SETUP_CLEARED
)
226 execute_clears( setup
);
228 lp_setup_rasterize_scene( setup
, TRUE
);
232 setup
->state
= new_state
;
237 lp_setup_flush( struct setup_context
*setup
,
240 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
242 set_scene_state( setup
, SETUP_FLUSHED
);
247 lp_setup_bind_framebuffer( struct setup_context
*setup
,
248 const struct pipe_framebuffer_state
*fb
)
250 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
252 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
254 set_scene_state( setup
, SETUP_FLUSHED
);
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
)
345 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
347 setup
->ccw_is_frontface
= ccw_is_frontface
;
348 setup
->cullmode
= cull_mode
;
349 setup
->triangle
= first_triangle
;
355 lp_setup_set_fs_inputs( struct setup_context
*setup
,
356 const struct lp_shader_input
*input
,
359 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
361 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
362 setup
->fs
.nr_inputs
= nr
;
366 lp_setup_set_fs_function( struct setup_context
*setup
,
367 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
->dirty
|= LP_SETUP_NEW_FS
;
377 lp_setup_set_fs_constants(struct setup_context
*setup
,
378 struct pipe_buffer
*buffer
)
380 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
382 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
384 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
389 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
390 float alpha_ref_value
)
392 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
394 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
395 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
396 setup
->dirty
|= LP_SETUP_NEW_FS
;
401 lp_setup_set_blend_color( struct setup_context
*setup
,
402 const struct pipe_blend_color
*blend_color
)
404 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
408 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
409 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
410 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
416 lp_setup_set_flatshade_first( struct setup_context
*setup
,
417 boolean flatshade_first
)
419 setup
->flatshade_first
= flatshade_first
;
424 lp_setup_set_vertex_info( struct setup_context
*setup
,
425 struct vertex_info
*vertex_info
)
427 /* XXX: just silently holding onto the pointer:
429 setup
->vertex_info
= vertex_info
;
434 lp_setup_set_sampler_textures( struct setup_context
*setup
,
435 unsigned num
, struct pipe_texture
**texture
)
437 struct pipe_texture
*dummy
;
440 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
443 assert(num
<= PIPE_MAX_SAMPLERS
);
445 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
446 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
448 /* FIXME: hold on to the reference */
450 pipe_texture_reference(&dummy
, tex
);
453 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
454 struct lp_jit_texture
*jit_tex
;
455 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
456 jit_tex
->width
= tex
->width0
;
457 jit_tex
->height
= tex
->height0
;
458 jit_tex
->stride
= lp_tex
->stride
[0];
460 jit_tex
->data
= lp_tex
->data
;
462 /* FIXME: map the rendertarget */
467 setup
->dirty
|= LP_SETUP_NEW_FS
;
471 lp_setup_is_texture_referenced( struct setup_context
*setup
,
472 const struct pipe_texture
*texture
)
475 return PIPE_UNREFERENCED
;
480 lp_setup_update_state( struct setup_context
*setup
)
482 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
484 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
486 assert(setup
->fs
.current
.jit_function
);
488 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
492 stored
= lp_scene_alloc_aligned(scene
, 4 * 16, 16);
494 /* smear each blend color component across 16 ubyte elements */
495 for (i
= 0; i
< 4; ++i
) {
496 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
497 for (j
= 0; j
< 16; ++j
)
498 stored
[i
*16 + j
] = c
;
501 setup
->blend_color
.stored
= stored
;
503 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
504 setup
->dirty
|= LP_SETUP_NEW_FS
;
508 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
509 struct pipe_buffer
*buffer
= setup
->constants
.current
;
512 unsigned current_size
= buffer
->size
;
513 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
515 /* TODO: copy only the actually used constants? */
517 if(setup
->constants
.stored_size
!= current_size
||
518 !setup
->constants
.stored_data
||
519 memcmp(setup
->constants
.stored_data
,
521 current_size
) != 0) {
524 stored
= lp_scene_alloc(scene
, current_size
);
529 setup
->constants
.stored_size
= current_size
;
530 setup
->constants
.stored_data
= stored
;
535 setup
->constants
.stored_size
= 0;
536 setup
->constants
.stored_data
= NULL
;
539 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
540 setup
->dirty
|= LP_SETUP_NEW_FS
;
544 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
545 if(!setup
->fs
.stored
||
546 memcmp(setup
->fs
.stored
,
548 sizeof setup
->fs
.current
) != 0) {
549 /* The fs state that's been stored in the scene is different from
550 * the new, current state. So allocate a new lp_rast_state object
551 * and append it to the bin's setup data buffer.
553 struct lp_rast_state
*stored
=
554 (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
558 sizeof setup
->fs
.current
);
559 setup
->fs
.stored
= stored
;
561 /* put the state-set command into all bins */
562 lp_scene_bin_state_command( scene
,
564 lp_rast_arg_state(setup
->fs
.stored
) );
571 assert(setup
->fs
.stored
);
576 /* Only caller is lp_setup_vbuf_destroy()
579 lp_setup_destroy( struct setup_context
*setup
)
581 reset_context( setup
);
583 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
585 /* free the scenes in the 'empty' queue */
586 while (lp_scene_queue_count(setup
->empty_scenes
) > 0) {
587 struct lp_scene
*scene
= lp_scene_dequeue(setup
->empty_scenes
);
590 lp_scene_destroy(scene
);
593 lp_rast_destroy( setup
->rast
);
600 * Create a new primitive tiling engine. Plug it into the backend of
601 * the draw module. Currently also creates a rasterizer to use with
604 struct setup_context
*
605 lp_setup_create( struct pipe_screen
*screen
,
606 struct draw_context
*draw
)
609 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
614 lp_setup_init_vbuf(setup
);
616 setup
->empty_scenes
= lp_scene_queue_create();
617 if (!setup
->empty_scenes
)
620 setup
->rast
= lp_rast_create( screen
, setup
->empty_scenes
);
624 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
628 draw_set_rasterize_stage(draw
, setup
->vbuf
);
629 draw_set_render(draw
, &setup
->base
);
631 /* create some empty scenes */
632 for (i
= 0; i
< MAX_SCENES
; i
++) {
633 struct lp_scene
*scene
= lp_scene_create();
634 lp_scene_enqueue(setup
->empty_scenes
, scene
);
637 setup
->triangle
= first_triangle
;
638 setup
->line
= first_line
;
639 setup
->point
= first_point
;
647 lp_rast_destroy( setup
->rast
);
652 if (setup
->empty_scenes
)
653 lp_scene_queue_destroy(setup
->empty_scenes
);