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"
43 #include "lp_buffer.h"
44 #include "lp_texture.h"
45 #include "lp_setup_context.h"
48 /** XXX temporary value, temporary here */
52 static void set_state( struct setup_context
*, unsigned );
56 lp_setup_get_current_bins(struct setup_context
*setup
)
59 /* wait for a free/empty bin */
60 setup
->bins
= lp_bins_dequeue(setup
->empty_bins
);
61 if(0)lp_reset_bins( setup
->bins
); /* XXX temporary? */
64 lp_bin_set_framebuffer_size(setup
->bins
,
65 setup
->fb
->width
, setup
->fb
->height
);
73 first_triangle( struct setup_context
*setup
,
78 set_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_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_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 tile's bins */
130 rasterize_bins( struct setup_context
*setup
,
131 boolean write_depth
)
133 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
135 lp_rasterize_bins(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_bins
*bins
= lp_setup_get_current_bins(setup
);
152 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
154 if (setup
->fb
->cbufs
[0]) {
155 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
156 lp_bin_everywhere( bins
,
158 setup
->clear
.color
);
160 lp_bin_everywhere( bins
,
162 lp_rast_arg_null() );
165 if (setup
->fb
->zsbuf
) {
166 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
167 lp_bin_everywhere( bins
,
168 lp_rast_clear_zstencil
,
169 setup
->clear
.zstencil
);
171 lp_bin_everywhere( bins
,
172 lp_rast_load_zstencil
,
173 lp_rast_arg_null() );
176 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
180 /* This basically bins and then flushes any outstanding full-screen
183 * TODO: fast path for fullscreen clears and no triangles.
186 execute_clears( struct setup_context
*setup
)
188 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
190 begin_binning( setup
);
191 rasterize_bins( setup
, TRUE
);
196 set_state( struct setup_context
*setup
,
199 unsigned old_state
= setup
->state
;
201 if (old_state
== new_state
)
204 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
208 begin_binning( setup
);
212 if (old_state
== SETUP_ACTIVE
) {
219 if (old_state
== SETUP_CLEARED
)
220 execute_clears( setup
);
222 rasterize_bins( setup
, TRUE
);
226 setup
->state
= new_state
;
231 lp_setup_flush( struct setup_context
*setup
,
234 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
236 set_state( setup
, SETUP_FLUSHED
);
241 lp_setup_bind_framebuffer( struct setup_context
*setup
,
242 const struct pipe_framebuffer_state
*fb
)
244 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
246 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
248 set_state( setup
, SETUP_FLUSHED
);
252 lp_bin_set_framebuffer_size(bins
, setup
->fb
->width
, setup
->fb
->height
);
257 lp_setup_clear( struct setup_context
*setup
,
263 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
266 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
269 if (flags
& PIPE_CLEAR_COLOR
) {
270 for (i
= 0; i
< 4; ++i
)
271 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
274 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
275 setup
->clear
.zstencil
.clear_zstencil
=
276 util_pack_z_stencil(setup
->fb
->zsbuf
->format
,
281 if (setup
->state
== SETUP_ACTIVE
) {
282 /* Add the clear to existing bins. In the unusual case where
283 * both color and depth-stencilare being cleared, we could
284 * discard the currently binned scene and start again, but I
285 * don't see that as being a common usage.
287 if (flags
& PIPE_CLEAR_COLOR
)
288 lp_bin_everywhere( bins
,
290 setup
->clear
.color
);
292 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
293 lp_bin_everywhere( bins
,
294 lp_rast_clear_zstencil
,
295 setup
->clear
.zstencil
);
298 /* Put ourselves into the 'pre-clear' state, specifically to try
299 * and accumulate multiple clears to color and depth_stencil
300 * buffers which the app or state-tracker might issue
303 set_state( setup
, SETUP_CLEARED
);
305 setup
->clear
.flags
|= flags
;
312 lp_setup_set_triangle_state( struct setup_context
*setup
,
314 boolean ccw_is_frontface
)
316 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
318 setup
->ccw_is_frontface
= ccw_is_frontface
;
319 setup
->cullmode
= cull_mode
;
320 setup
->triangle
= first_triangle
;
326 lp_setup_set_fs_inputs( struct setup_context
*setup
,
327 const struct lp_shader_input
*input
,
330 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
332 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
333 setup
->fs
.nr_inputs
= nr
;
337 lp_setup_set_fs( struct setup_context
*setup
,
338 struct lp_fragment_shader
*fs
)
340 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) fs
);
341 /* FIXME: reference count */
343 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
344 setup
->dirty
|= LP_SETUP_NEW_FS
;
348 lp_setup_set_fs_constants(struct setup_context
*setup
,
349 struct pipe_buffer
*buffer
)
351 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
353 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
355 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
360 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
361 float alpha_ref_value
)
363 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
365 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
366 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
367 setup
->dirty
|= LP_SETUP_NEW_FS
;
372 lp_setup_set_blend_color( struct setup_context
*setup
,
373 const struct pipe_blend_color
*blend_color
)
375 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
379 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
380 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
381 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
386 lp_setup_set_sampler_textures( struct setup_context
*setup
,
387 unsigned num
, struct pipe_texture
**texture
)
389 struct pipe_texture
*dummy
;
392 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
395 assert(num
<= PIPE_MAX_SAMPLERS
);
397 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
398 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
400 /* FIXME: hold on to the reference */
402 pipe_texture_reference(&dummy
, tex
);
405 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
406 struct lp_jit_texture
*jit_tex
;
407 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
408 jit_tex
->width
= tex
->width
[0];
409 jit_tex
->height
= tex
->height
[0];
410 jit_tex
->stride
= lp_tex
->stride
[0];
412 jit_tex
->data
= lp_tex
->data
;
414 /* FIXME: map the rendertarget */
419 setup
->dirty
|= LP_SETUP_NEW_FS
;
423 lp_setup_is_texture_referenced( struct setup_context
*setup
,
424 const struct pipe_texture
*texture
)
427 return PIPE_UNREFERENCED
;
432 lp_setup_update_shader_state( struct setup_context
*setup
)
434 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
436 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
438 assert(setup
->fs
.current
.jit_function
);
440 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
444 stored
= lp_bin_alloc_aligned(bins
, 4 * 16, 16);
446 /* smear each blend color component across 16 ubyte elements */
447 for (i
= 0; i
< 4; ++i
) {
448 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
449 for (j
= 0; j
< 16; ++j
)
450 stored
[i
*16 + j
] = c
;
453 setup
->blend_color
.stored
= stored
;
455 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
456 setup
->dirty
|= LP_SETUP_NEW_FS
;
460 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
461 struct pipe_buffer
*buffer
= setup
->constants
.current
;
464 unsigned current_size
= buffer
->size
;
465 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
467 /* TODO: copy only the actually used constants? */
469 if(setup
->constants
.stored_size
!= current_size
||
470 !setup
->constants
.stored_data
||
471 memcmp(setup
->constants
.stored_data
,
473 current_size
) != 0) {
476 stored
= lp_bin_alloc(bins
, current_size
);
481 setup
->constants
.stored_size
= current_size
;
482 setup
->constants
.stored_data
= stored
;
487 setup
->constants
.stored_size
= 0;
488 setup
->constants
.stored_data
= NULL
;
491 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
492 setup
->dirty
|= LP_SETUP_NEW_FS
;
496 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
497 if(!setup
->fs
.stored
||
498 memcmp(setup
->fs
.stored
,
500 sizeof setup
->fs
.current
) != 0) {
501 /* The fs state that's been stored in the bins is different from
502 * the new, current state. So allocate a new lp_rast_state object
503 * and append it to the bin's setup data buffer.
505 struct lp_rast_state
*stored
=
506 (struct lp_rast_state
*) lp_bin_alloc(bins
, sizeof *stored
);
510 sizeof setup
->fs
.current
);
511 setup
->fs
.stored
= stored
;
513 /* put the state-set command into all bins */
514 lp_bin_state_command( bins
,
516 lp_rast_arg_state(setup
->fs
.stored
) );
523 assert(setup
->fs
.stored
);
527 /* Stubs for lines & points for now:
530 lp_setup_point(struct setup_context
*setup
,
531 const float (*v0
)[4])
533 lp_setup_update_shader_state(setup
);
534 setup
->point( setup
, v0
);
538 lp_setup_line(struct setup_context
*setup
,
539 const float (*v0
)[4],
540 const float (*v1
)[4])
542 lp_setup_update_shader_state(setup
);
543 setup
->line( setup
, v0
, v1
);
547 lp_setup_tri(struct setup_context
*setup
,
548 const float (*v0
)[4],
549 const float (*v1
)[4],
550 const float (*v2
)[4])
552 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
554 lp_setup_update_shader_state(setup
);
555 setup
->triangle( setup
, v0
, v1
, v2
);
560 lp_setup_destroy( struct setup_context
*setup
)
562 reset_context( setup
);
564 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
566 /* free the bins in the 'empty' queue */
567 while (lp_bins_queue_size(setup
->empty_bins
) > 0) {
568 struct lp_bins
*bins
= lp_bins_dequeue(setup
->empty_bins
);
571 lp_bins_destroy(bins
);
574 lp_rast_destroy( setup
->rast
);
581 * Create a new primitive tiling engine. Currently also creates a
582 * rasterizer to use with it.
584 struct setup_context
*
585 lp_setup_create( struct pipe_screen
*screen
)
588 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
593 setup
->empty_bins
= lp_bins_queue_create();
594 if (!setup
->empty_bins
)
597 setup
->rast
= lp_rast_create( screen
, setup
->empty_bins
);
601 /* create some empty bins */
602 for (i
= 0; i
< MAX_BINS
; i
++) {
603 struct lp_bins
*bins
= lp_bins_create();
604 lp_bins_enqueue(setup
->empty_bins
, bins
);
607 setup
->triangle
= first_triangle
;
608 setup
->line
= first_line
;
609 setup
->point
= first_point
;
616 if (setup
->empty_bins
)
617 lp_bins_queue_destroy(setup
->empty_bins
);