d976934a5dd47a9757f1b7a72465438b10e67298
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-stencil are being cleared when there's
284 * already been some rendering, we could discard the currently
285 * binned scene and start again, but I don't see that as being
288 if (flags
& PIPE_CLEAR_COLOR
)
289 lp_bin_everywhere( bins
,
291 setup
->clear
.color
);
293 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
294 lp_bin_everywhere( bins
,
295 lp_rast_clear_zstencil
,
296 setup
->clear
.zstencil
);
299 /* Put ourselves into the 'pre-clear' state, specifically to try
300 * and accumulate multiple clears to color and depth_stencil
301 * buffers which the app or state-tracker might issue
304 set_state( setup
, SETUP_CLEARED
);
306 setup
->clear
.flags
|= flags
;
313 lp_setup_set_triangle_state( struct setup_context
*setup
,
315 boolean ccw_is_frontface
)
317 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
319 setup
->ccw_is_frontface
= ccw_is_frontface
;
320 setup
->cullmode
= cull_mode
;
321 setup
->triangle
= first_triangle
;
327 lp_setup_set_fs_inputs( struct setup_context
*setup
,
328 const struct lp_shader_input
*input
,
331 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
333 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
334 setup
->fs
.nr_inputs
= nr
;
338 lp_setup_set_fs( struct setup_context
*setup
,
339 struct lp_fragment_shader
*fs
)
341 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) fs
);
342 /* FIXME: reference count */
344 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
345 setup
->dirty
|= LP_SETUP_NEW_FS
;
349 lp_setup_set_fs_constants(struct setup_context
*setup
,
350 struct pipe_buffer
*buffer
)
352 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
354 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
356 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
361 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
362 float alpha_ref_value
)
364 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
366 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
367 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
368 setup
->dirty
|= LP_SETUP_NEW_FS
;
373 lp_setup_set_blend_color( struct setup_context
*setup
,
374 const struct pipe_blend_color
*blend_color
)
376 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
380 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
381 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
382 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
387 lp_setup_set_sampler_textures( struct setup_context
*setup
,
388 unsigned num
, struct pipe_texture
**texture
)
390 struct pipe_texture
*dummy
;
393 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
396 assert(num
<= PIPE_MAX_SAMPLERS
);
398 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
399 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
401 /* FIXME: hold on to the reference */
403 pipe_texture_reference(&dummy
, tex
);
406 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
407 struct lp_jit_texture
*jit_tex
;
408 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
409 jit_tex
->width
= tex
->width
[0];
410 jit_tex
->height
= tex
->height
[0];
411 jit_tex
->stride
= lp_tex
->stride
[0];
413 jit_tex
->data
= lp_tex
->data
;
415 /* FIXME: map the rendertarget */
420 setup
->dirty
|= LP_SETUP_NEW_FS
;
424 lp_setup_is_texture_referenced( struct setup_context
*setup
,
425 const struct pipe_texture
*texture
)
428 return PIPE_UNREFERENCED
;
433 lp_setup_update_shader_state( struct setup_context
*setup
)
435 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
437 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
439 assert(setup
->fs
.current
.jit_function
);
441 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
445 stored
= lp_bin_alloc_aligned(bins
, 4 * 16, 16);
447 /* smear each blend color component across 16 ubyte elements */
448 for (i
= 0; i
< 4; ++i
) {
449 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
450 for (j
= 0; j
< 16; ++j
)
451 stored
[i
*16 + j
] = c
;
454 setup
->blend_color
.stored
= stored
;
456 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
457 setup
->dirty
|= LP_SETUP_NEW_FS
;
461 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
462 struct pipe_buffer
*buffer
= setup
->constants
.current
;
465 unsigned current_size
= buffer
->size
;
466 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
468 /* TODO: copy only the actually used constants? */
470 if(setup
->constants
.stored_size
!= current_size
||
471 !setup
->constants
.stored_data
||
472 memcmp(setup
->constants
.stored_data
,
474 current_size
) != 0) {
477 stored
= lp_bin_alloc(bins
, current_size
);
482 setup
->constants
.stored_size
= current_size
;
483 setup
->constants
.stored_data
= stored
;
488 setup
->constants
.stored_size
= 0;
489 setup
->constants
.stored_data
= NULL
;
492 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
493 setup
->dirty
|= LP_SETUP_NEW_FS
;
497 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
498 if(!setup
->fs
.stored
||
499 memcmp(setup
->fs
.stored
,
501 sizeof setup
->fs
.current
) != 0) {
502 /* The fs state that's been stored in the bins is different from
503 * the new, current state. So allocate a new lp_rast_state object
504 * and append it to the bin's setup data buffer.
506 struct lp_rast_state
*stored
=
507 (struct lp_rast_state
*) lp_bin_alloc(bins
, sizeof *stored
);
511 sizeof setup
->fs
.current
);
512 setup
->fs
.stored
= stored
;
514 /* put the state-set command into all bins */
515 lp_bin_state_command( bins
,
517 lp_rast_arg_state(setup
->fs
.stored
) );
524 assert(setup
->fs
.stored
);
528 /* Stubs for lines & points for now:
531 lp_setup_point(struct setup_context
*setup
,
532 const float (*v0
)[4])
534 lp_setup_update_shader_state(setup
);
535 setup
->point( setup
, v0
);
539 lp_setup_line(struct setup_context
*setup
,
540 const float (*v0
)[4],
541 const float (*v1
)[4])
543 lp_setup_update_shader_state(setup
);
544 setup
->line( setup
, v0
, v1
);
548 lp_setup_tri(struct setup_context
*setup
,
549 const float (*v0
)[4],
550 const float (*v1
)[4],
551 const float (*v2
)[4])
553 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
555 lp_setup_update_shader_state(setup
);
556 setup
->triangle( setup
, v0
, v1
, v2
);
561 lp_setup_destroy( struct setup_context
*setup
)
563 reset_context( setup
);
565 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
567 /* free the bins in the 'empty' queue */
568 while (lp_bins_queue_size(setup
->empty_bins
) > 0) {
569 struct lp_bins
*bins
= lp_bins_dequeue(setup
->empty_bins
);
572 lp_bins_destroy(bins
);
575 lp_rast_destroy( setup
->rast
);
582 * Create a new primitive tiling engine. Currently also creates a
583 * rasterizer to use with it.
585 struct setup_context
*
586 lp_setup_create( struct pipe_screen
*screen
)
589 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
594 setup
->empty_bins
= lp_bins_queue_create();
595 if (!setup
->empty_bins
)
598 setup
->rast
= lp_rast_create( screen
, setup
->empty_bins
);
602 /* create some empty bins */
603 for (i
= 0; i
< MAX_BINS
; i
++) {
604 struct lp_bins
*bins
= lp_bins_create();
605 lp_bins_enqueue(setup
->empty_bins
, bins
);
608 setup
->triangle
= first_triangle
;
609 setup
->line
= first_line
;
610 setup
->point
= first_point
;
617 if (setup
->empty_bins
)
618 lp_bins_queue_destroy(setup
->empty_bins
);