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_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_pack_color.h"
41 #include "lp_buffer.h"
42 #include "lp_texture.h"
43 #include "lp_setup_context.h"
45 #define SETUP_DEBUG debug_printf
47 static void set_state( struct setup_context
*, unsigned );
49 void lp_setup_new_cmd_block( struct cmd_block_list
*list
)
51 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
52 list
->tail
->next
= block
;
58 void lp_setup_new_data_block( struct data_block_list
*list
)
60 struct data_block
*block
= MALLOC_STRUCT(data_block
);
61 list
->tail
->next
= block
;
69 first_triangle( struct setup_context
*setup
,
74 set_state( setup
, SETUP_ACTIVE
);
75 lp_setup_choose_triangle( setup
);
76 setup
->triangle( setup
, v0
, v1
, v2
);
80 first_line( struct setup_context
*setup
,
84 set_state( setup
, SETUP_ACTIVE
);
85 lp_setup_choose_line( setup
);
86 setup
->line( setup
, v0
, v1
);
90 first_point( struct setup_context
*setup
,
93 set_state( setup
, SETUP_ACTIVE
);
94 lp_setup_choose_point( setup
);
95 setup
->point( setup
, v0
);
98 static void reset_context( struct setup_context
*setup
)
102 SETUP_DEBUG("%s\n", __FUNCTION__
);
104 /* Reset derived state */
105 setup
->constants
.stored_size
= 0;
106 setup
->constants
.stored_data
= NULL
;
107 setup
->fs
.stored
= NULL
;
110 /* Free all but last binner command lists:
112 for (i
= 0; i
< setup
->tiles_x
; i
++) {
113 for (j
= 0; j
< setup
->tiles_y
; j
++) {
114 struct cmd_block_list
*list
= &setup
->tile
[i
][j
].commands
;
115 struct cmd_block
*block
;
116 struct cmd_block
*tmp
;
118 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
123 assert(list
->tail
->next
== NULL
);
124 list
->head
= list
->tail
;
125 list
->head
->count
= 0;
129 /* Free all but last binned data block:
132 struct data_block_list
*list
= &setup
->data
;
133 struct data_block
*block
, *tmp
;
135 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
140 assert(list
->tail
->next
== NULL
);
141 list
->head
= list
->tail
;
142 list
->head
->used
= 0;
147 setup
->clear
.flags
= 0;
149 /* Have an explicit "start-binning" call and get rid of this
152 setup
->line
= first_line
;
153 setup
->point
= first_point
;
154 setup
->triangle
= first_triangle
;
159 * Return last command in the bin
162 lp_get_last_command( const struct cmd_bin
*bin
)
164 const struct cmd_block
*tail
= bin
->commands
.tail
;
165 const unsigned i
= tail
->count
;
167 return tail
->cmd
[i
- 1];
174 * Replace the arg of the last command in the bin.
177 lp_replace_last_command_arg( struct cmd_bin
*bin
,
178 const union lp_rast_cmd_arg arg
)
180 struct cmd_block
*tail
= bin
->commands
.tail
;
181 const unsigned i
= tail
->count
;
183 tail
->arg
[i
- 1] = arg
;
188 /* Add a command to all active bins.
190 static void bin_everywhere( struct setup_context
*setup
,
192 const union lp_rast_cmd_arg arg
)
195 for (i
= 0; i
< setup
->tiles_x
; i
++)
196 for (j
= 0; j
< setup
->tiles_y
; j
++)
197 bin_command( &setup
->tile
[i
][j
], cmd
, arg
);
202 * Put a state-change command into all bins.
203 * If we find that the last command in a bin was also a state-change
204 * command, we can simply replace that one with the new one.
207 bin_state_command( struct setup_context
*setup
,
209 const union lp_rast_cmd_arg arg
)
212 for (i
= 0; i
< setup
->tiles_x
; i
++) {
213 for (j
= 0; j
< setup
->tiles_y
; j
++) {
214 struct cmd_bin
*bin
= &setup
->tile
[i
][j
];
215 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
216 if (last_cmd
== cmd
) {
217 lp_replace_last_command_arg(bin
, arg
);
220 bin_command( bin
, cmd
, arg
);
227 /** Rasterize commands for a single bin */
229 rasterize_bin( struct lp_rasterizer
*rast
,
230 const struct cmd_bin
*bin
,
233 const struct cmd_block_list
*commands
= &bin
->commands
;
234 struct cmd_block
*block
;
237 lp_rast_start_tile( rast
, x
, y
);
239 /* simply execute each of the commands in the block list */
240 for (block
= commands
->head
; block
; block
= block
->next
) {
241 for (k
= 0; k
< block
->count
; k
++) {
242 block
->cmd
[k
]( rast
, block
->arg
[k
] );
246 lp_rast_end_tile( rast
);
250 /** Rasterize all tile's bins */
252 rasterize_bins( struct setup_context
*setup
,
253 boolean write_depth
)
255 struct lp_rasterizer
*rast
= setup
->rast
;
258 SETUP_DEBUG("%s\n", __FUNCTION__
);
263 setup
->fb
.cbuf
!= NULL
,
264 setup
->fb
.zsbuf
!= NULL
&& write_depth
,
268 /* loop over tile bins, rasterize each */
269 for (i
= 0; i
< setup
->tiles_x
; i
++) {
270 for (j
= 0; j
< setup
->tiles_y
; j
++) {
271 rasterize_bin( rast
, &setup
->tile
[i
][j
],
279 reset_context( setup
);
281 SETUP_DEBUG("%s done \n", __FUNCTION__
);
287 begin_binning( struct setup_context
*setup
)
289 SETUP_DEBUG("%s\n", __FUNCTION__
);
291 if (setup
->fb
.cbuf
) {
292 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
293 bin_everywhere( setup
,
295 setup
->clear
.color
);
297 bin_everywhere( setup
, lp_rast_load_color
, lp_rast_arg_null() );
300 if (setup
->fb
.zsbuf
) {
301 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
302 bin_everywhere( setup
,
303 lp_rast_clear_zstencil
,
304 setup
->clear
.zstencil
);
306 bin_everywhere( setup
, lp_rast_load_zstencil
, lp_rast_arg_null() );
309 SETUP_DEBUG("%s done\n", __FUNCTION__
);
313 /* This basically bins and then flushes any outstanding full-screen
316 * TODO: fast path for fullscreen clears and no triangles.
319 execute_clears( struct setup_context
*setup
)
321 SETUP_DEBUG("%s\n", __FUNCTION__
);
323 begin_binning( setup
);
324 rasterize_bins( setup
, TRUE
);
329 set_state( struct setup_context
*setup
,
332 unsigned old_state
= setup
->state
;
334 if (old_state
== new_state
)
337 SETUP_DEBUG("%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
341 begin_binning( setup
);
345 if (old_state
== SETUP_ACTIVE
) {
352 if (old_state
== SETUP_CLEARED
)
353 execute_clears( setup
);
355 rasterize_bins( setup
, TRUE
);
359 setup
->state
= new_state
;
364 lp_setup_flush( struct setup_context
*setup
,
367 SETUP_DEBUG("%s\n", __FUNCTION__
);
369 set_state( setup
, SETUP_FLUSHED
);
374 lp_setup_bind_framebuffer( struct setup_context
*setup
,
375 struct pipe_surface
*color
,
376 struct pipe_surface
*zstencil
)
378 SETUP_DEBUG("%s\n", __FUNCTION__
);
380 set_state( setup
, SETUP_FLUSHED
);
382 pipe_surface_reference( &setup
->fb
.cbuf
, color
);
383 pipe_surface_reference( &setup
->fb
.zsbuf
, zstencil
);
385 if (!setup
->fb
.cbuf
&& !setup
->fb
.zsbuf
) {
387 setup
->fb
.height
= 0;
389 else if (!setup
->fb
.zsbuf
) {
390 setup
->fb
.width
= setup
->fb
.cbuf
->width
;
391 setup
->fb
.height
= setup
->fb
.cbuf
->height
;
393 else if (!setup
->fb
.cbuf
) {
394 setup
->fb
.width
= setup
->fb
.zsbuf
->width
;
395 setup
->fb
.height
= setup
->fb
.zsbuf
->height
;
398 /* XXX: not sure what we're really supposed to do for
399 * mis-matched color & depth buffer sizes.
401 setup
->fb
.width
= MIN2(setup
->fb
.cbuf
->width
,
402 setup
->fb
.zsbuf
->width
);
403 setup
->fb
.height
= MIN2(setup
->fb
.cbuf
->height
,
404 setup
->fb
.zsbuf
->height
);
407 setup
->tiles_x
= align(setup
->fb
.width
, TILE_SIZE
) / TILE_SIZE
;
408 setup
->tiles_y
= align(setup
->fb
.height
, TILE_SIZE
) / TILE_SIZE
;
413 lp_setup_clear( struct setup_context
*setup
,
421 SETUP_DEBUG("%s state %d\n", __FUNCTION__
, setup
->state
);
424 if (flags
& PIPE_CLEAR_COLOR
) {
425 for (i
= 0; i
< 4; ++i
)
426 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
429 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
430 setup
->clear
.zstencil
.clear_zstencil
=
431 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
436 if (setup
->state
== SETUP_ACTIVE
) {
437 /* Add the clear to existing bins. In the unusual case where
438 * both color and depth-stencilare being cleared, we could
439 * discard the currently binned scene and start again, but I
440 * don't see that as being a common usage.
442 if (flags
& PIPE_CLEAR_COLOR
)
443 bin_everywhere( setup
,
445 setup
->clear
.color
);
447 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
448 bin_everywhere( setup
,
449 lp_rast_clear_zstencil
,
450 setup
->clear
.zstencil
);
453 /* Put ourselves into the 'pre-clear' state, specifically to try
454 * and accumulate multiple clears to color and depth_stencil
455 * buffers which the app or state-tracker might issue
458 set_state( setup
, SETUP_CLEARED
);
460 setup
->clear
.flags
|= flags
;
467 lp_setup_set_triangle_state( struct setup_context
*setup
,
469 boolean ccw_is_frontface
)
471 SETUP_DEBUG("%s\n", __FUNCTION__
);
473 setup
->ccw_is_frontface
= ccw_is_frontface
;
474 setup
->cullmode
= cull_mode
;
475 setup
->triangle
= first_triangle
;
481 lp_setup_set_fs_inputs( struct setup_context
*setup
,
482 const struct lp_shader_input
*input
,
485 SETUP_DEBUG("%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
487 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
488 setup
->fs
.nr_inputs
= nr
;
492 lp_setup_set_fs( struct setup_context
*setup
,
493 struct lp_fragment_shader
*fs
)
495 SETUP_DEBUG("%s %p\n", __FUNCTION__
, (void *) fs
);
496 /* FIXME: reference count */
498 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
499 setup
->dirty
|= LP_SETUP_NEW_FS
;
503 lp_setup_set_fs_constants(struct setup_context
*setup
,
504 struct pipe_buffer
*buffer
)
506 SETUP_DEBUG("%s %p\n", __FUNCTION__
, (void *) buffer
);
508 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
510 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
515 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
516 float alpha_ref_value
)
518 SETUP_DEBUG("%s %f\n", __FUNCTION__
, alpha_ref_value
);
520 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
521 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
522 setup
->dirty
|= LP_SETUP_NEW_FS
;
527 lp_setup_set_blend_color( struct setup_context
*setup
,
528 const struct pipe_blend_color
*blend_color
)
530 SETUP_DEBUG("%s\n", __FUNCTION__
);
534 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
535 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
536 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
541 lp_setup_set_sampler_textures( struct setup_context
*setup
,
542 unsigned num
, struct pipe_texture
**texture
)
544 struct pipe_texture
*dummy
;
547 SETUP_DEBUG("%s\n", __FUNCTION__
);
550 assert(num
<= PIPE_MAX_SAMPLERS
);
552 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
553 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
555 /* FIXME: hold on to the reference */
557 pipe_texture_reference(&dummy
, tex
);
560 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
561 struct lp_jit_texture
*jit_tex
;
562 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
563 jit_tex
->width
= tex
->width
[0];
564 jit_tex
->height
= tex
->height
[0];
565 jit_tex
->stride
= lp_tex
->stride
[0];
567 jit_tex
->data
= lp_tex
->data
;
569 /* FIXME: map the rendertarget */
574 setup
->dirty
|= LP_SETUP_NEW_FS
;
578 lp_setup_is_texture_referenced( struct setup_context
*setup
,
579 const struct pipe_texture
*texture
)
582 return PIPE_UNREFERENCED
;
587 lp_setup_update_shader_state( struct setup_context
*setup
)
589 SETUP_DEBUG("%s\n", __FUNCTION__
);
591 assert(setup
->fs
.current
.jit_function
);
593 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
597 stored
= get_data_aligned(&setup
->data
, 4 * 16, 16);
599 /* smear each blend color component across 16 ubyte elements */
600 for (i
= 0; i
< 4; ++i
) {
601 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
602 for (j
= 0; j
< 16; ++j
)
603 stored
[i
*16 + j
] = c
;
606 setup
->blend_color
.stored
= stored
;
608 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
609 setup
->dirty
|= LP_SETUP_NEW_FS
;
613 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
614 struct pipe_buffer
*buffer
= setup
->constants
.current
;
617 unsigned current_size
= buffer
->size
;
618 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
620 /* TODO: copy only the actually used constants? */
622 if(setup
->constants
.stored_size
!= current_size
||
623 !setup
->constants
.stored_data
||
624 memcmp(setup
->constants
.stored_data
,
626 current_size
) != 0) {
629 stored
= get_data(&setup
->data
, current_size
);
634 setup
->constants
.stored_size
= current_size
;
635 setup
->constants
.stored_data
= stored
;
640 setup
->constants
.stored_size
= 0;
641 setup
->constants
.stored_data
= NULL
;
644 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
645 setup
->dirty
|= LP_SETUP_NEW_FS
;
649 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
650 if(!setup
->fs
.stored
||
651 memcmp(setup
->fs
.stored
,
653 sizeof setup
->fs
.current
) != 0) {
654 /* The fs state that's been stored in the bins is different from
655 * the new, current state. So allocate a new lp_rast_state object
656 * and append it to the bin's setup data buffer.
658 struct lp_rast_state
*stored
=
659 (struct lp_rast_state
*) get_data(&setup
->data
, sizeof *stored
);
663 sizeof setup
->fs
.current
);
664 setup
->fs
.stored
= stored
;
666 /* put the state-set command into all bins */
667 bin_state_command( setup
,
669 lp_rast_arg_state(setup
->fs
.stored
) );
676 assert(setup
->fs
.stored
);
680 /* Stubs for lines & points for now:
683 lp_setup_point(struct setup_context
*setup
,
684 const float (*v0
)[4])
686 lp_setup_update_shader_state(setup
);
687 setup
->point( setup
, v0
);
691 lp_setup_line(struct setup_context
*setup
,
692 const float (*v0
)[4],
693 const float (*v1
)[4])
695 lp_setup_update_shader_state(setup
);
696 setup
->line( setup
, v0
, v1
);
700 lp_setup_tri(struct setup_context
*setup
,
701 const float (*v0
)[4],
702 const float (*v1
)[4],
703 const float (*v2
)[4])
705 SETUP_DEBUG("%s\n", __FUNCTION__
);
707 lp_setup_update_shader_state(setup
);
708 setup
->triangle( setup
, v0
, v1
, v2
);
713 lp_setup_destroy( struct setup_context
*setup
)
717 reset_context( setup
);
719 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
721 for (i
= 0; i
< TILES_X
; i
++)
722 for (j
= 0; j
< TILES_Y
; j
++)
723 FREE(setup
->tile
[i
][j
].commands
.head
);
725 FREE(setup
->data
.head
);
727 lp_rast_destroy( setup
->rast
);
733 * Create a new primitive tiling engine. Currently also creates a
734 * rasterizer to use with it.
736 struct setup_context
*
737 lp_setup_create( struct pipe_screen
*screen
)
739 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
742 setup
->rast
= lp_rast_create( screen
);
746 for (i
= 0; i
< TILES_X
; i
++)
747 for (j
= 0; j
< TILES_Y
; j
++)
748 setup
->tile
[i
][j
].commands
.head
=
749 setup
->tile
[i
][j
].commands
.tail
= CALLOC_STRUCT(cmd_block
);
752 setup
->data
.tail
= CALLOC_STRUCT(data_block
);
754 setup
->triangle
= first_triangle
;
755 setup
->line
= first_line
;
756 setup
->point
= first_point
;