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
];
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
;
160 /* Add a command to all active bins.
162 static void bin_everywhere( struct setup_context
*setup
,
164 const union lp_rast_cmd_arg arg
)
167 for (i
= 0; i
< setup
->tiles_x
; i
++)
168 for (j
= 0; j
< setup
->tiles_y
; j
++)
169 bin_command( &setup
->tile
[i
][j
], cmd
, arg
);
173 /** Rasterize commands for a single bin */
175 rasterize_bin( struct lp_rasterizer
*rast
,
176 struct cmd_block_list
*commands
,
179 struct cmd_block
*block
;
182 lp_rast_start_tile( rast
, x
, y
);
184 /* simply execute each of the commands in the block list */
185 for (block
= commands
->head
; block
; block
= block
->next
) {
186 for (k
= 0; k
< block
->count
; k
++) {
187 block
->cmd
[k
]( rast
, block
->arg
[k
] );
191 lp_rast_end_tile( rast
);
195 /** Rasterize all tile's bins */
197 rasterize_bins( struct setup_context
*setup
,
198 boolean write_depth
)
200 struct lp_rasterizer
*rast
= setup
->rast
;
203 SETUP_DEBUG("%s\n", __FUNCTION__
);
208 setup
->fb
.cbuf
!= NULL
,
209 setup
->fb
.zsbuf
!= NULL
&& write_depth
,
213 /* loop over tile bins, rasterize each */
214 for (i
= 0; i
< setup
->tiles_x
; i
++) {
215 for (j
= 0; j
< setup
->tiles_y
; j
++) {
216 rasterize_bin( rast
, &setup
->tile
[i
][j
],
224 reset_context( setup
);
226 SETUP_DEBUG("%s done \n", __FUNCTION__
);
232 begin_binning( struct setup_context
*setup
)
234 SETUP_DEBUG("%s\n", __FUNCTION__
);
236 if (!setup
->fb
.cbuf
&& !setup
->fb
.zsbuf
) {
238 setup
->fb
.height
= 0;
240 else if (!setup
->fb
.zsbuf
) {
241 setup
->fb
.width
= setup
->fb
.cbuf
->width
;
242 setup
->fb
.height
= setup
->fb
.cbuf
->height
;
244 else if (!setup
->fb
.cbuf
) {
245 setup
->fb
.width
= setup
->fb
.zsbuf
->width
;
246 setup
->fb
.height
= setup
->fb
.zsbuf
->height
;
249 /* XXX: not sure what we're really supposed to do for
250 * mis-matched color & depth buffer sizes.
252 setup
->fb
.width
= MIN2(setup
->fb
.cbuf
->width
,
253 setup
->fb
.zsbuf
->width
);
254 setup
->fb
.height
= MIN2(setup
->fb
.cbuf
->height
,
255 setup
->fb
.zsbuf
->height
);
258 setup
->tiles_x
= align(setup
->fb
.width
, TILE_SIZE
) / TILE_SIZE
;
259 setup
->tiles_y
= align(setup
->fb
.height
, TILE_SIZE
) / TILE_SIZE
;
261 if (setup
->fb
.cbuf
) {
262 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
263 bin_everywhere( setup
,
265 setup
->clear
.color
);
267 bin_everywhere( setup
, lp_rast_load_color
, lp_rast_arg_null() );
270 if (setup
->fb
.zsbuf
) {
271 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
272 bin_everywhere( setup
,
273 lp_rast_clear_zstencil
,
274 setup
->clear
.zstencil
);
276 bin_everywhere( setup
, lp_rast_load_zstencil
, lp_rast_arg_null() );
279 SETUP_DEBUG("%s done\n", __FUNCTION__
);
283 /* This basically bins and then flushes any outstanding full-screen
286 * TODO: fast path for fullscreen clears and no triangles.
289 execute_clears( struct setup_context
*setup
)
291 SETUP_DEBUG("%s\n", __FUNCTION__
);
293 begin_binning( setup
);
294 rasterize_bins( setup
, TRUE
);
299 set_state( struct setup_context
*setup
,
302 unsigned old_state
= setup
->state
;
304 if (old_state
== new_state
)
307 SETUP_DEBUG("%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
311 begin_binning( setup
);
315 if (old_state
== SETUP_ACTIVE
) {
322 if (old_state
== SETUP_CLEARED
)
323 execute_clears( setup
);
325 rasterize_bins( setup
, TRUE
);
329 setup
->state
= new_state
;
334 lp_setup_flush( struct setup_context
*setup
,
337 SETUP_DEBUG("%s\n", __FUNCTION__
);
339 set_state( setup
, SETUP_FLUSHED
);
344 lp_setup_bind_framebuffer( struct setup_context
*setup
,
345 struct pipe_surface
*color
,
346 struct pipe_surface
*zstencil
)
348 SETUP_DEBUG("%s\n", __FUNCTION__
);
350 set_state( setup
, SETUP_FLUSHED
);
352 pipe_surface_reference( &setup
->fb
.cbuf
, color
);
353 pipe_surface_reference( &setup
->fb
.zsbuf
, zstencil
);
357 lp_setup_clear( struct setup_context
*setup
,
365 SETUP_DEBUG("%s state %d\n", __FUNCTION__
, setup
->state
);
368 if (flags
& PIPE_CLEAR_COLOR
) {
369 for (i
= 0; i
< 4; ++i
)
370 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
373 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
374 setup
->clear
.zstencil
.clear_zstencil
=
375 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
380 if (setup
->state
== SETUP_ACTIVE
) {
381 /* Add the clear to existing bins. In the unusual case where
382 * both color and depth-stencilare being cleared, we could
383 * discard the currently binned scene and start again, but I
384 * don't see that as being a common usage.
386 if (flags
& PIPE_CLEAR_COLOR
)
387 bin_everywhere( setup
,
389 setup
->clear
.color
);
391 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
392 bin_everywhere( setup
,
393 lp_rast_clear_zstencil
,
394 setup
->clear
.zstencil
);
397 /* Put ourselves into the 'pre-clear' state, specifically to try
398 * and accumulate multiple clears to color and depth_stencil
399 * buffers which the app or state-tracker might issue
402 set_state( setup
, SETUP_CLEARED
);
404 setup
->clear
.flags
|= flags
;
411 lp_setup_set_triangle_state( struct setup_context
*setup
,
413 boolean ccw_is_frontface
)
415 SETUP_DEBUG("%s\n", __FUNCTION__
);
417 setup
->ccw_is_frontface
= ccw_is_frontface
;
418 setup
->cullmode
= cull_mode
;
419 setup
->triangle
= first_triangle
;
425 lp_setup_set_fs_inputs( struct setup_context
*setup
,
426 const struct lp_shader_input
*input
,
429 SETUP_DEBUG("%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
431 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
432 setup
->fs
.nr_inputs
= nr
;
436 lp_setup_set_fs( struct setup_context
*setup
,
437 struct lp_fragment_shader
*fs
)
439 SETUP_DEBUG("%s %p\n", __FUNCTION__
, (void *) fs
);
440 /* FIXME: reference count */
442 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
446 lp_setup_set_fs_constants(struct setup_context
*setup
,
447 struct pipe_buffer
*buffer
)
449 SETUP_DEBUG("%s %p\n", __FUNCTION__
, (void *) buffer
);
451 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
453 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
458 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
459 float alpha_ref_value
)
461 SETUP_DEBUG("%s %f\n", __FUNCTION__
, alpha_ref_value
);
463 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
464 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
465 setup
->dirty
|= LP_SETUP_NEW_FS
;
470 lp_setup_set_blend_color( struct setup_context
*setup
,
471 const struct pipe_blend_color
*blend_color
)
473 SETUP_DEBUG("%s\n", __FUNCTION__
);
477 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
478 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
479 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
484 lp_setup_set_sampler_textures( struct setup_context
*setup
,
485 unsigned num
, struct pipe_texture
**texture
)
487 struct pipe_texture
*dummy
;
490 SETUP_DEBUG("%s\n", __FUNCTION__
);
493 assert(num
<= PIPE_MAX_SAMPLERS
);
495 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
496 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
498 /* FIXME: hold on to the reference */
500 pipe_texture_reference(&dummy
, tex
);
503 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
504 struct lp_jit_texture
*jit_tex
;
505 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
506 jit_tex
->width
= tex
->width
[0];
507 jit_tex
->height
= tex
->height
[0];
508 jit_tex
->stride
= lp_tex
->stride
[0];
510 jit_tex
->data
= lp_tex
->data
;
512 /* FIXME: map the rendertarget */
517 setup
->dirty
|= LP_SETUP_NEW_FS
;
521 lp_setup_is_texture_referenced( struct setup_context
*setup
,
522 const struct pipe_texture
*texture
)
525 return PIPE_UNREFERENCED
;
530 lp_setup_update_shader_state( struct setup_context
*setup
)
532 SETUP_DEBUG("%s\n", __FUNCTION__
);
534 assert(setup
->fs
.current
.jit_function
);
536 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
540 stored
= get_data_aligned(&setup
->data
, 4 * 16, 16);
542 for (i
= 0; i
< 4; ++i
) {
543 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
544 for (j
= 0; j
< 16; ++j
)
548 setup
->blend_color
.stored
= stored
;
550 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
551 setup
->dirty
|= LP_SETUP_NEW_FS
;
555 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
556 struct pipe_buffer
*buffer
= setup
->constants
.current
;
559 unsigned current_size
= buffer
->size
;
560 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
562 /* TODO: copy only the actually used constants? */
564 if(setup
->constants
.stored_size
!= current_size
||
565 !setup
->constants
.stored_data
||
566 memcmp(setup
->constants
.stored_data
,
568 current_size
) != 0) {
571 stored
= get_data(&setup
->data
, current_size
);
576 setup
->constants
.stored_size
= current_size
;
577 setup
->constants
.stored_data
= stored
;
582 setup
->constants
.stored_size
= 0;
583 setup
->constants
.stored_data
= NULL
;
586 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
587 setup
->dirty
|= LP_SETUP_NEW_FS
;
591 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
592 if(!setup
->fs
.stored
||
593 memcmp(setup
->fs
.stored
,
595 sizeof setup
->fs
.current
) != 0) {
596 struct lp_rast_state
*stored
;
598 stored
= get_data(&setup
->data
, sizeof *stored
);
602 sizeof setup
->fs
.current
);
603 setup
->fs
.stored
= stored
;
610 assert(setup
->fs
.stored
);
614 /* Stubs for lines & points for now:
617 lp_setup_point(struct setup_context
*setup
,
618 const float (*v0
)[4])
620 lp_setup_update_shader_state(setup
);
621 setup
->point( setup
, v0
);
625 lp_setup_line(struct setup_context
*setup
,
626 const float (*v0
)[4],
627 const float (*v1
)[4])
629 lp_setup_update_shader_state(setup
);
630 setup
->line( setup
, v0
, v1
);
634 lp_setup_tri(struct setup_context
*setup
,
635 const float (*v0
)[4],
636 const float (*v1
)[4],
637 const float (*v2
)[4])
639 SETUP_DEBUG("%s\n", __FUNCTION__
);
641 lp_setup_update_shader_state(setup
);
642 setup
->triangle( setup
, v0
, v1
, v2
);
647 lp_setup_destroy( struct setup_context
*setup
)
651 reset_context( setup
);
653 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
655 for (i
= 0; i
< TILES_X
; i
++)
656 for (j
= 0; j
< TILES_Y
; j
++)
657 FREE(setup
->tile
[i
][j
].head
);
659 FREE(setup
->data
.head
);
661 lp_rast_destroy( setup
->rast
);
667 * Create a new primitive tiling engine. Currently also creates a
668 * rasterizer to use with it.
670 struct setup_context
*
671 lp_setup_create( struct pipe_screen
*screen
)
673 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
676 setup
->rast
= lp_rast_create( screen
);
680 for (i
= 0; i
< TILES_X
; i
++)
681 for (j
= 0; j
< TILES_Y
; j
++)
682 setup
->tile
[i
][j
].head
=
683 setup
->tile
[i
][j
].tail
= CALLOC_STRUCT(cmd_block
);
686 setup
->data
.tail
= CALLOC_STRUCT(data_block
);
688 setup
->triangle
= first_triangle
;
689 setup
->line
= first_line
;
690 setup
->point
= first_point
;