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"
40 #include "lp_bin_queue.h"
44 #include "lp_buffer.h"
45 #include "lp_texture.h"
46 #include "lp_setup_context.h"
49 /** XXX temporary value, temporary here */
53 static void set_state( struct setup_context
*, unsigned );
57 lp_setup_get_current_bins(struct setup_context
*setup
)
60 /* wait for a free/empty bin */
61 setup
->bins
= lp_bins_dequeue(setup
->empty_bins
);
62 if(0)lp_reset_bins( setup
->bins
); /* XXX temporary? */
65 lp_bin_set_framebuffer_size(setup
->bins
,
66 setup
->fb
->width
, setup
->fb
->height
);
74 first_triangle( struct setup_context
*setup
,
79 set_state( setup
, SETUP_ACTIVE
);
80 lp_setup_choose_triangle( setup
);
81 setup
->triangle( setup
, v0
, v1
, v2
);
85 first_line( struct setup_context
*setup
,
89 set_state( setup
, SETUP_ACTIVE
);
90 lp_setup_choose_line( setup
);
91 setup
->line( setup
, v0
, v1
);
95 first_point( struct setup_context
*setup
,
98 set_state( setup
, SETUP_ACTIVE
);
99 lp_setup_choose_point( setup
);
100 setup
->point( setup
, v0
);
103 static void reset_context( struct setup_context
*setup
)
105 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
107 /* Reset derived state */
108 setup
->constants
.stored_size
= 0;
109 setup
->constants
.stored_data
= NULL
;
110 setup
->fs
.stored
= NULL
;
118 setup
->clear
.flags
= 0;
120 /* Have an explicit "start-binning" call and get rid of this
123 setup
->line
= first_line
;
124 setup
->point
= first_point
;
125 setup
->triangle
= first_triangle
;
129 /** Rasterize all tile's bins */
131 rasterize_bins( struct setup_context
*setup
,
132 boolean write_depth
)
134 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
136 lp_rasterize_bins(setup
->rast
,
141 reset_context( setup
);
143 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
149 begin_binning( struct setup_context
*setup
)
151 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
153 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
155 if (setup
->fb
->cbufs
[0]) {
156 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
157 lp_bin_everywhere( bins
,
159 setup
->clear
.color
);
161 lp_bin_everywhere( bins
,
163 lp_rast_arg_null() );
166 if (setup
->fb
->zsbuf
) {
167 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
168 lp_bin_everywhere( bins
,
169 lp_rast_clear_zstencil
,
170 setup
->clear
.zstencil
);
172 lp_bin_everywhere( bins
,
173 lp_rast_load_zstencil
,
174 lp_rast_arg_null() );
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 setup_context
*setup
)
189 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
191 begin_binning( setup
);
192 rasterize_bins( setup
, TRUE
);
197 set_state( struct 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 rasterize_bins( setup
, TRUE
);
227 setup
->state
= new_state
;
232 lp_setup_flush( struct setup_context
*setup
,
235 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
237 set_state( setup
, SETUP_FLUSHED
);
242 lp_setup_bind_framebuffer( struct setup_context
*setup
,
243 const struct pipe_framebuffer_state
*fb
)
245 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
247 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
249 set_state( setup
, SETUP_FLUSHED
);
253 lp_bin_set_framebuffer_size(bins
, setup
->fb
->width
, setup
->fb
->height
);
258 lp_setup_clear( struct setup_context
*setup
,
264 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
267 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
270 if (flags
& PIPE_CLEAR_COLOR
) {
271 for (i
= 0; i
< 4; ++i
)
272 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
275 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
276 setup
->clear
.zstencil
.clear_zstencil
=
277 util_pack_z_stencil(setup
->fb
->zsbuf
->format
,
282 if (setup
->state
== SETUP_ACTIVE
) {
283 /* Add the clear to existing bins. In the unusual case where
284 * both color and depth-stencil are being cleared when there's
285 * already been some rendering, we could discard the currently
286 * binned scene and start again, but I don't see that as being
289 if (flags
& PIPE_CLEAR_COLOR
)
290 lp_bin_everywhere( bins
,
292 setup
->clear
.color
);
294 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
295 lp_bin_everywhere( bins
,
296 lp_rast_clear_zstencil
,
297 setup
->clear
.zstencil
);
300 /* Put ourselves into the 'pre-clear' state, specifically to try
301 * and accumulate multiple clears to color and depth_stencil
302 * buffers which the app or state-tracker might issue
305 set_state( setup
, SETUP_CLEARED
);
307 setup
->clear
.flags
|= flags
;
315 struct pipe_fence_handle
*
316 lp_setup_fence( struct setup_context
*setup
)
318 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
319 const unsigned rank
= lp_bin_get_num_bins( bins
);
320 struct lp_fence
*fence
= lp_fence_create(rank
);
322 LP_DBG(DEBUG_SETUP
, "%s rank %u\n", __FUNCTION__
, rank
);
324 set_state( setup
, SETUP_ACTIVE
);
326 /* insert the fence into all command bins */
327 lp_bin_everywhere( bins
,
329 lp_rast_arg_fence(fence
) );
331 return (struct pipe_fence_handle
*) fence
;
336 lp_setup_set_triangle_state( struct setup_context
*setup
,
338 boolean ccw_is_frontface
)
340 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
342 setup
->ccw_is_frontface
= ccw_is_frontface
;
343 setup
->cullmode
= cull_mode
;
344 setup
->triangle
= first_triangle
;
350 lp_setup_set_fs_inputs( struct setup_context
*setup
,
351 const struct lp_shader_input
*input
,
354 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
356 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
357 setup
->fs
.nr_inputs
= nr
;
361 lp_setup_set_fs( struct setup_context
*setup
,
362 struct lp_fragment_shader
*fs
)
364 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) fs
);
365 /* FIXME: reference count */
367 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
368 setup
->dirty
|= LP_SETUP_NEW_FS
;
372 lp_setup_set_fs_constants(struct setup_context
*setup
,
373 struct pipe_buffer
*buffer
)
375 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
377 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
379 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
384 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
385 float alpha_ref_value
)
387 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
389 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
390 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
391 setup
->dirty
|= LP_SETUP_NEW_FS
;
396 lp_setup_set_blend_color( struct setup_context
*setup
,
397 const struct pipe_blend_color
*blend_color
)
399 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
403 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
404 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
405 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
410 lp_setup_set_sampler_textures( struct setup_context
*setup
,
411 unsigned num
, struct pipe_texture
**texture
)
413 struct pipe_texture
*dummy
;
416 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
419 assert(num
<= PIPE_MAX_SAMPLERS
);
421 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
422 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
424 /* FIXME: hold on to the reference */
426 pipe_texture_reference(&dummy
, tex
);
429 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
430 struct lp_jit_texture
*jit_tex
;
431 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
432 jit_tex
->width
= tex
->width
[0];
433 jit_tex
->height
= tex
->height
[0];
434 jit_tex
->stride
= lp_tex
->stride
[0];
436 jit_tex
->data
= lp_tex
->data
;
438 /* FIXME: map the rendertarget */
443 setup
->dirty
|= LP_SETUP_NEW_FS
;
447 lp_setup_is_texture_referenced( struct setup_context
*setup
,
448 const struct pipe_texture
*texture
)
451 return PIPE_UNREFERENCED
;
456 lp_setup_update_shader_state( struct setup_context
*setup
)
458 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
460 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
462 assert(setup
->fs
.current
.jit_function
);
464 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
468 stored
= lp_bin_alloc_aligned(bins
, 4 * 16, 16);
470 /* smear each blend color component across 16 ubyte elements */
471 for (i
= 0; i
< 4; ++i
) {
472 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
473 for (j
= 0; j
< 16; ++j
)
474 stored
[i
*16 + j
] = c
;
477 setup
->blend_color
.stored
= stored
;
479 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
480 setup
->dirty
|= LP_SETUP_NEW_FS
;
484 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
485 struct pipe_buffer
*buffer
= setup
->constants
.current
;
488 unsigned current_size
= buffer
->size
;
489 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
491 /* TODO: copy only the actually used constants? */
493 if(setup
->constants
.stored_size
!= current_size
||
494 !setup
->constants
.stored_data
||
495 memcmp(setup
->constants
.stored_data
,
497 current_size
) != 0) {
500 stored
= lp_bin_alloc(bins
, current_size
);
505 setup
->constants
.stored_size
= current_size
;
506 setup
->constants
.stored_data
= stored
;
511 setup
->constants
.stored_size
= 0;
512 setup
->constants
.stored_data
= NULL
;
515 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
516 setup
->dirty
|= LP_SETUP_NEW_FS
;
520 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
521 if(!setup
->fs
.stored
||
522 memcmp(setup
->fs
.stored
,
524 sizeof setup
->fs
.current
) != 0) {
525 /* The fs state that's been stored in the bins is different from
526 * the new, current state. So allocate a new lp_rast_state object
527 * and append it to the bin's setup data buffer.
529 struct lp_rast_state
*stored
=
530 (struct lp_rast_state
*) lp_bin_alloc(bins
, sizeof *stored
);
534 sizeof setup
->fs
.current
);
535 setup
->fs
.stored
= stored
;
537 /* put the state-set command into all bins */
538 lp_bin_state_command( bins
,
540 lp_rast_arg_state(setup
->fs
.stored
) );
547 assert(setup
->fs
.stored
);
551 /* Stubs for lines & points for now:
554 lp_setup_point(struct setup_context
*setup
,
555 const float (*v0
)[4])
557 lp_setup_update_shader_state(setup
);
558 setup
->point( setup
, v0
);
562 lp_setup_line(struct setup_context
*setup
,
563 const float (*v0
)[4],
564 const float (*v1
)[4])
566 lp_setup_update_shader_state(setup
);
567 setup
->line( setup
, v0
, v1
);
571 lp_setup_tri(struct setup_context
*setup
,
572 const float (*v0
)[4],
573 const float (*v1
)[4],
574 const float (*v2
)[4])
576 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
578 lp_setup_update_shader_state(setup
);
579 setup
->triangle( setup
, v0
, v1
, v2
);
584 lp_setup_destroy( struct setup_context
*setup
)
586 reset_context( setup
);
588 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
590 /* free the bins in the 'empty' queue */
591 while (lp_bins_queue_size(setup
->empty_bins
) > 0) {
592 struct lp_bins
*bins
= lp_bins_dequeue(setup
->empty_bins
);
595 lp_bins_destroy(bins
);
598 lp_rast_destroy( setup
->rast
);
605 * Create a new primitive tiling engine. Currently also creates a
606 * rasterizer to use with it.
608 struct setup_context
*
609 lp_setup_create( struct pipe_screen
*screen
)
612 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
617 setup
->empty_bins
= lp_bins_queue_create();
618 if (!setup
->empty_bins
)
621 setup
->rast
= lp_rast_create( screen
, setup
->empty_bins
);
625 /* create some empty bins */
626 for (i
= 0; i
< MAX_BINS
; i
++) {
627 struct lp_bins
*bins
= lp_bins_create();
628 lp_bins_enqueue(setup
->empty_bins
, bins
);
631 setup
->triangle
= first_triangle
;
632 setup
->line
= first_line
;
633 setup
->point
= first_point
;
640 if (setup
->empty_bins
)
641 lp_bins_queue_destroy(setup
->empty_bins
);