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"
42 #include "lp_buffer.h"
43 #include "lp_texture.h"
44 #include "lp_setup_context.h"
47 static void set_state( struct setup_context
*, unsigned );
52 first_triangle( struct setup_context
*setup
,
57 set_state( setup
, SETUP_ACTIVE
);
58 lp_setup_choose_triangle( setup
);
59 setup
->triangle( setup
, v0
, v1
, v2
);
63 first_line( struct setup_context
*setup
,
67 set_state( setup
, SETUP_ACTIVE
);
68 lp_setup_choose_line( setup
);
69 setup
->line( setup
, v0
, v1
);
73 first_point( struct setup_context
*setup
,
76 set_state( setup
, SETUP_ACTIVE
);
77 lp_setup_choose_point( setup
);
78 setup
->point( setup
, v0
);
81 static void reset_context( struct setup_context
*setup
)
83 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
85 /* Reset derived state */
86 setup
->constants
.stored_size
= 0;
87 setup
->constants
.stored_data
= NULL
;
88 setup
->fs
.stored
= NULL
;
91 lp_reset_bins( &setup
->bins
);
95 setup
->clear
.flags
= 0;
97 /* Have an explicit "start-binning" call and get rid of this
100 setup
->line
= first_line
;
101 setup
->point
= first_point
;
102 setup
->triangle
= first_triangle
;
106 /** Rasterize all tile's bins */
108 rasterize_bins( struct setup_context
*setup
,
109 boolean write_depth
)
111 lp_rasterize_bins(setup
->rast
,
116 reset_context( setup
);
118 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
124 begin_binning( struct setup_context
*setup
)
126 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
128 if (setup
->fb
->cbufs
[0]) {
129 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
130 lp_bin_everywhere( &setup
->bins
,
132 setup
->clear
.color
);
134 lp_bin_everywhere( &setup
->bins
,
136 lp_rast_arg_null() );
139 if (setup
->fb
->zsbuf
) {
140 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
141 lp_bin_everywhere( &setup
->bins
,
142 lp_rast_clear_zstencil
,
143 setup
->clear
.zstencil
);
145 lp_bin_everywhere( &setup
->bins
,
146 lp_rast_load_zstencil
,
147 lp_rast_arg_null() );
150 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
154 /* This basically bins and then flushes any outstanding full-screen
157 * TODO: fast path for fullscreen clears and no triangles.
160 execute_clears( struct setup_context
*setup
)
162 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
164 begin_binning( setup
);
165 rasterize_bins( setup
, TRUE
);
170 set_state( struct setup_context
*setup
,
173 unsigned old_state
= setup
->state
;
175 if (old_state
== new_state
)
178 LP_DBG(DEBUG_SETUP
, "%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
182 begin_binning( setup
);
186 if (old_state
== SETUP_ACTIVE
) {
193 if (old_state
== SETUP_CLEARED
)
194 execute_clears( setup
);
196 rasterize_bins( setup
, TRUE
);
200 setup
->state
= new_state
;
205 lp_setup_flush( struct setup_context
*setup
,
208 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
210 set_state( setup
, SETUP_FLUSHED
);
215 lp_setup_bind_framebuffer( struct setup_context
*setup
,
216 const struct pipe_framebuffer_state
*fb
)
218 unsigned tiles_x
, tiles_y
;
220 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
222 set_state( setup
, SETUP_FLUSHED
);
226 tiles_x
= align(setup
->fb
->width
, TILE_SIZE
) / TILE_SIZE
;
227 tiles_y
= align(setup
->fb
->height
, TILE_SIZE
) / TILE_SIZE
;
229 lp_bin_set_num_bins(&setup
->bins
, tiles_x
, tiles_y
);
234 lp_setup_clear( struct setup_context
*setup
,
242 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
245 if (flags
& PIPE_CLEAR_COLOR
) {
246 for (i
= 0; i
< 4; ++i
)
247 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
250 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
251 setup
->clear
.zstencil
.clear_zstencil
=
252 util_pack_z_stencil(setup
->fb
->zsbuf
->format
,
257 if (setup
->state
== SETUP_ACTIVE
) {
258 /* Add the clear to existing bins. In the unusual case where
259 * both color and depth-stencilare being cleared, we could
260 * discard the currently binned scene and start again, but I
261 * don't see that as being a common usage.
263 if (flags
& PIPE_CLEAR_COLOR
)
264 lp_bin_everywhere( &setup
->bins
,
266 setup
->clear
.color
);
268 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
269 lp_bin_everywhere( &setup
->bins
,
270 lp_rast_clear_zstencil
,
271 setup
->clear
.zstencil
);
274 /* Put ourselves into the 'pre-clear' state, specifically to try
275 * and accumulate multiple clears to color and depth_stencil
276 * buffers which the app or state-tracker might issue
279 set_state( setup
, SETUP_CLEARED
);
281 setup
->clear
.flags
|= flags
;
288 lp_setup_set_triangle_state( struct setup_context
*setup
,
290 boolean ccw_is_frontface
)
292 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
294 setup
->ccw_is_frontface
= ccw_is_frontface
;
295 setup
->cullmode
= cull_mode
;
296 setup
->triangle
= first_triangle
;
302 lp_setup_set_fs_inputs( struct setup_context
*setup
,
303 const struct lp_shader_input
*input
,
306 LP_DBG(DEBUG_SETUP
, "%s %p %u\n", __FUNCTION__
, (void *) input
, nr
);
308 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
309 setup
->fs
.nr_inputs
= nr
;
313 lp_setup_set_fs( struct setup_context
*setup
,
314 struct lp_fragment_shader
*fs
)
316 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) fs
);
317 /* FIXME: reference count */
319 setup
->fs
.current
.jit_function
= fs
? fs
->current
->jit_function
: NULL
;
320 setup
->dirty
|= LP_SETUP_NEW_FS
;
324 lp_setup_set_fs_constants(struct setup_context
*setup
,
325 struct pipe_buffer
*buffer
)
327 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffer
);
329 pipe_buffer_reference(&setup
->constants
.current
, buffer
);
331 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
336 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
337 float alpha_ref_value
)
339 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
341 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
342 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
343 setup
->dirty
|= LP_SETUP_NEW_FS
;
348 lp_setup_set_blend_color( struct setup_context
*setup
,
349 const struct pipe_blend_color
*blend_color
)
351 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
355 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
356 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
357 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
362 lp_setup_set_sampler_textures( struct setup_context
*setup
,
363 unsigned num
, struct pipe_texture
**texture
)
365 struct pipe_texture
*dummy
;
368 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
371 assert(num
<= PIPE_MAX_SAMPLERS
);
373 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
374 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
376 /* FIXME: hold on to the reference */
378 pipe_texture_reference(&dummy
, tex
);
381 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
382 struct lp_jit_texture
*jit_tex
;
383 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
384 jit_tex
->width
= tex
->width
[0];
385 jit_tex
->height
= tex
->height
[0];
386 jit_tex
->stride
= lp_tex
->stride
[0];
388 jit_tex
->data
= lp_tex
->data
;
390 /* FIXME: map the rendertarget */
395 setup
->dirty
|= LP_SETUP_NEW_FS
;
399 lp_setup_is_texture_referenced( struct setup_context
*setup
,
400 const struct pipe_texture
*texture
)
403 return PIPE_UNREFERENCED
;
408 lp_setup_update_shader_state( struct setup_context
*setup
)
410 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
412 assert(setup
->fs
.current
.jit_function
);
414 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
418 stored
= lp_bin_alloc_aligned(&setup
->bins
, 4 * 16, 16);
420 /* smear each blend color component across 16 ubyte elements */
421 for (i
= 0; i
< 4; ++i
) {
422 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
423 for (j
= 0; j
< 16; ++j
)
424 stored
[i
*16 + j
] = c
;
427 setup
->blend_color
.stored
= stored
;
429 setup
->fs
.current
.jit_context
.blend_color
= setup
->blend_color
.stored
;
430 setup
->dirty
|= LP_SETUP_NEW_FS
;
434 if(setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
435 struct pipe_buffer
*buffer
= setup
->constants
.current
;
438 unsigned current_size
= buffer
->size
;
439 const void *current_data
= llvmpipe_buffer(buffer
)->data
;
441 /* TODO: copy only the actually used constants? */
443 if(setup
->constants
.stored_size
!= current_size
||
444 !setup
->constants
.stored_data
||
445 memcmp(setup
->constants
.stored_data
,
447 current_size
) != 0) {
450 stored
= lp_bin_alloc(&setup
->bins
, current_size
);
455 setup
->constants
.stored_size
= current_size
;
456 setup
->constants
.stored_data
= stored
;
461 setup
->constants
.stored_size
= 0;
462 setup
->constants
.stored_data
= NULL
;
465 setup
->fs
.current
.jit_context
.constants
= setup
->constants
.stored_data
;
466 setup
->dirty
|= LP_SETUP_NEW_FS
;
470 if(setup
->dirty
& LP_SETUP_NEW_FS
) {
471 if(!setup
->fs
.stored
||
472 memcmp(setup
->fs
.stored
,
474 sizeof setup
->fs
.current
) != 0) {
475 /* The fs state that's been stored in the bins is different from
476 * the new, current state. So allocate a new lp_rast_state object
477 * and append it to the bin's setup data buffer.
479 struct lp_rast_state
*stored
=
480 (struct lp_rast_state
*) lp_bin_alloc(&setup
->bins
, sizeof *stored
);
484 sizeof setup
->fs
.current
);
485 setup
->fs
.stored
= stored
;
487 /* put the state-set command into all bins */
488 lp_bin_state_command( &setup
->bins
,
490 lp_rast_arg_state(setup
->fs
.stored
) );
497 assert(setup
->fs
.stored
);
501 /* Stubs for lines & points for now:
504 lp_setup_point(struct setup_context
*setup
,
505 const float (*v0
)[4])
507 lp_setup_update_shader_state(setup
);
508 setup
->point( setup
, v0
);
512 lp_setup_line(struct setup_context
*setup
,
513 const float (*v0
)[4],
514 const float (*v1
)[4])
516 lp_setup_update_shader_state(setup
);
517 setup
->line( setup
, v0
, v1
);
521 lp_setup_tri(struct setup_context
*setup
,
522 const float (*v0
)[4],
523 const float (*v1
)[4],
524 const float (*v2
)[4])
526 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
528 lp_setup_update_shader_state(setup
);
529 setup
->triangle( setup
, v0
, v1
, v2
);
534 lp_setup_destroy( struct setup_context
*setup
)
536 reset_context( setup
);
538 pipe_buffer_reference(&setup
->constants
.current
, NULL
);
540 lp_free_bin_data(&setup
->bins
);
542 lp_rast_destroy( setup
->rast
);
548 * Create a new primitive tiling engine. Currently also creates a
549 * rasterizer to use with it.
551 struct setup_context
*
552 lp_setup_create( struct pipe_screen
*screen
)
554 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
556 setup
->rast
= lp_rast_create( screen
);
560 lp_init_bins(&setup
->bins
);
562 setup
->triangle
= first_triangle
;
563 setup
->line
= first_line
;
564 setup
->point
= first_point
;