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_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 unsigned tiles_x
= align(setup
->fb
->width
, TILE_SIZE
) / TILE_SIZE
;
66 unsigned tiles_y
= align(setup
->fb
->height
, TILE_SIZE
) / TILE_SIZE
;
67 lp_bin_set_num_bins(setup
->bins
, tiles_x
, tiles_y
);
75 first_triangle( struct setup_context
*setup
,
80 set_state( setup
, SETUP_ACTIVE
);
81 lp_setup_choose_triangle( setup
);
82 setup
->triangle( setup
, v0
, v1
, v2
);
86 first_line( struct setup_context
*setup
,
90 set_state( setup
, SETUP_ACTIVE
);
91 lp_setup_choose_line( setup
);
92 setup
->line( setup
, v0
, v1
);
96 first_point( struct setup_context
*setup
,
99 set_state( setup
, SETUP_ACTIVE
);
100 lp_setup_choose_point( setup
);
101 setup
->point( setup
, v0
);
104 static void reset_context( struct setup_context
*setup
)
106 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
108 /* Reset derived state */
109 setup
->constants
.stored_size
= 0;
110 setup
->constants
.stored_data
= NULL
;
111 setup
->fs
.stored
= NULL
;
119 setup
->clear
.flags
= 0;
121 /* Have an explicit "start-binning" call and get rid of this
124 setup
->line
= first_line
;
125 setup
->point
= first_point
;
126 setup
->triangle
= first_triangle
;
130 /** Rasterize all tile's bins */
132 rasterize_bins( struct setup_context
*setup
,
133 boolean write_depth
)
135 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
137 lp_rasterize_bins(setup
->rast
,
142 reset_context( setup
);
144 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
150 begin_binning( struct setup_context
*setup
)
152 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
154 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
156 if (setup
->fb
->cbufs
[0]) {
157 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
158 lp_bin_everywhere( bins
,
160 setup
->clear
.color
);
162 lp_bin_everywhere( bins
,
164 lp_rast_arg_null() );
167 if (setup
->fb
->zsbuf
) {
168 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
169 lp_bin_everywhere( bins
,
170 lp_rast_clear_zstencil
,
171 setup
->clear
.zstencil
);
173 lp_bin_everywhere( bins
,
174 lp_rast_load_zstencil
,
175 lp_rast_arg_null() );
178 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
182 /* This basically bins and then flushes any outstanding full-screen
185 * TODO: fast path for fullscreen clears and no triangles.
188 execute_clears( struct setup_context
*setup
)
190 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
192 begin_binning( setup
);
193 rasterize_bins( setup
, TRUE
);
198 set_state( struct setup_context
*setup
,
201 unsigned old_state
= setup
->state
;
203 if (old_state
== new_state
)
206 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
210 begin_binning( setup
);
214 if (old_state
== SETUP_ACTIVE
) {
221 if (old_state
== SETUP_CLEARED
)
222 execute_clears( setup
);
224 rasterize_bins( setup
, TRUE
);
228 setup
->state
= new_state
;
233 lp_setup_flush( struct setup_context
*setup
,
236 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
238 set_state( setup
, SETUP_FLUSHED
);
243 lp_setup_bind_framebuffer( struct setup_context
*setup
,
244 const struct pipe_framebuffer_state
*fb
)
246 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
247 unsigned tiles_x
, tiles_y
;
249 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
251 set_state( setup
, SETUP_FLUSHED
);
255 tiles_x
= align(setup
->fb
->width
, TILE_SIZE
) / TILE_SIZE
;
256 tiles_y
= align(setup
->fb
->height
, TILE_SIZE
) / TILE_SIZE
;
258 lp_bin_set_num_bins(bins
, tiles_x
, tiles_y
);
263 lp_setup_clear( struct setup_context
*setup
,
269 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
272 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
275 if (flags
& PIPE_CLEAR_COLOR
) {
276 for (i
= 0; i
< 4; ++i
)
277 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
280 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
281 setup
->clear
.zstencil
.clear_zstencil
=
282 util_pack_z_stencil(setup
->fb
->zsbuf
->format
,
287 if (setup
->state
== SETUP_ACTIVE
) {
288 /* Add the clear to existing bins. In the unusual case where
289 * both color and depth-stencilare being cleared, we could
290 * discard the currently binned scene and start again, but I
291 * don't see that as being a common usage.
293 if (flags
& PIPE_CLEAR_COLOR
)
294 lp_bin_everywhere( bins
,
296 setup
->clear
.color
);
298 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
299 lp_bin_everywhere( bins
,
300 lp_rast_clear_zstencil
,
301 setup
->clear
.zstencil
);
304 /* Put ourselves into the 'pre-clear' state, specifically to try
305 * and accumulate multiple clears to color and depth_stencil
306 * buffers which the app or state-tracker might issue
309 set_state( setup
, SETUP_CLEARED
);
311 setup
->clear
.flags
|= flags
;
318 lp_setup_set_triangle_state( struct setup_context
*setup
,
320 boolean ccw_is_frontface
)
322 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
324 setup
->ccw_is_frontface
= ccw_is_frontface
;
325 setup
->cullmode
= cull_mode
;
326 setup
->triangle
= first_triangle
;
332 lp_setup_set_fs_inputs( struct setup_context
*setup
,
333 const struct lp_shader_input
*input
,
336 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
338 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
339 setup
->fs
.nr_inputs
= nr
;
343 lp_setup_set_fs( struct setup_context
*setup
,
344 struct lp_fragment_shader
*fs
)
346 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) fs
);
347 /* FIXME: reference count */
349 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
350 setup
->dirty
|= LP_SETUP_NEW_FS
;
354 lp_setup_set_fs_constants(struct setup_context
*setup
,
355 struct pipe_buffer
*buffer
)
357 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
359 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
361 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
366 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
367 float alpha_ref_value
)
369 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
371 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
372 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
373 setup
->dirty
|= LP_SETUP_NEW_FS
;
378 lp_setup_set_blend_color( struct setup_context
*setup
,
379 const struct pipe_blend_color
*blend_color
)
381 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
385 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
386 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
387 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
392 lp_setup_set_sampler_textures( struct setup_context
*setup
,
393 unsigned num
, struct pipe_texture
**texture
)
395 struct pipe_texture
*dummy
;
398 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
401 assert(num
<= PIPE_MAX_SAMPLERS
);
403 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
404 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
406 /* FIXME: hold on to the reference */
408 pipe_texture_reference(&dummy
, tex
);
411 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
412 struct lp_jit_texture
*jit_tex
;
413 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
414 jit_tex
->width
= tex
->width
[0];
415 jit_tex
->height
= tex
->height
[0];
416 jit_tex
->stride
= lp_tex
->stride
[0];
418 jit_tex
->data
= lp_tex
->data
;
420 /* FIXME: map the rendertarget */
425 setup
->dirty
|= LP_SETUP_NEW_FS
;
429 lp_setup_is_texture_referenced( struct setup_context
*setup
,
430 const struct pipe_texture
*texture
)
433 return PIPE_UNREFERENCED
;
438 lp_setup_update_shader_state( struct setup_context
*setup
)
440 struct lp_bins
*bins
= lp_setup_get_current_bins(setup
);
442 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
444 assert(setup
->fs
.current
.jit_function
);
446 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
450 stored
= lp_bin_alloc_aligned(bins
, 4 * 16, 16);
452 /* smear each blend color component across 16 ubyte elements */
453 for (i
= 0; i
< 4; ++i
) {
454 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
455 for (j
= 0; j
< 16; ++j
)
456 stored
[i
*16 + j
] = c
;
459 setup
->blend_color
.stored
= stored
;
461 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
462 setup
->dirty
|= LP_SETUP_NEW_FS
;
466 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
467 struct pipe_buffer
*buffer
= setup
->constants
.current
;
470 unsigned current_size
= buffer
->size
;
471 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
473 /* TODO: copy only the actually used constants? */
475 if(setup
->constants
.stored_size
!= current_size
||
476 !setup
->constants
.stored_data
||
477 memcmp(setup
->constants
.stored_data
,
479 current_size
) != 0) {
482 stored
= lp_bin_alloc(bins
, current_size
);
487 setup
->constants
.stored_size
= current_size
;
488 setup
->constants
.stored_data
= stored
;
493 setup
->constants
.stored_size
= 0;
494 setup
->constants
.stored_data
= NULL
;
497 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
498 setup
->dirty
|= LP_SETUP_NEW_FS
;
502 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
503 if(!setup
->fs
.stored
||
504 memcmp(setup
->fs
.stored
,
506 sizeof setup
->fs
.current
) != 0) {
507 /* The fs state that's been stored in the bins is different from
508 * the new, current state. So allocate a new lp_rast_state object
509 * and append it to the bin's setup data buffer.
511 struct lp_rast_state
*stored
=
512 (struct lp_rast_state
*) lp_bin_alloc(bins
, sizeof *stored
);
516 sizeof setup
->fs
.current
);
517 setup
->fs
.stored
= stored
;
519 /* put the state-set command into all bins */
520 lp_bin_state_command( bins
,
522 lp_rast_arg_state(setup
->fs
.stored
) );
529 assert(setup
->fs
.stored
);
533 /* Stubs for lines & points for now:
536 lp_setup_point(struct setup_context
*setup
,
537 const float (*v0
)[4])
539 lp_setup_update_shader_state(setup
);
540 setup
->point( setup
, v0
);
544 lp_setup_line(struct setup_context
*setup
,
545 const float (*v0
)[4],
546 const float (*v1
)[4])
548 lp_setup_update_shader_state(setup
);
549 setup
->line( setup
, v0
, v1
);
553 lp_setup_tri(struct setup_context
*setup
,
554 const float (*v0
)[4],
555 const float (*v1
)[4],
556 const float (*v2
)[4])
558 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
560 lp_setup_update_shader_state(setup
);
561 setup
->triangle( setup
, v0
, v1
, v2
);
566 lp_setup_destroy( struct setup_context
*setup
)
568 reset_context( setup
);
570 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
572 /* free the bins in the 'empty' queue */
573 while (lp_bins_queue_size(setup
->empty_bins
) > 0) {
574 struct lp_bins
*bins
= lp_bins_dequeue(setup
->empty_bins
);
577 lp_bins_destroy(bins
);
580 lp_rast_destroy( setup
->rast
);
587 * Create a new primitive tiling engine. Currently also creates a
588 * rasterizer to use with it.
590 struct setup_context
*
591 lp_setup_create( struct pipe_screen
*screen
)
594 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
599 setup
->empty_bins
= lp_bins_queue_create();
600 if (!setup
->empty_bins
)
603 setup
->rast
= lp_rast_create( screen
, setup
->empty_bins
);
607 /* create some empty bins */
608 for (i
= 0; i
< MAX_BINS
; i
++) {
609 struct lp_bins
*bins
= lp_bins_create();
610 lp_bins_enqueue(setup
->empty_bins
, bins
);
613 setup
->triangle
= first_triangle
;
614 setup
->line
= first_line
;
615 setup
->point
= first_point
;
622 if (setup
->empty_bins
)
623 lp_bins_queue_destroy(setup
->empty_bins
);