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 /* Free binner command lists:
106 for (i
= 0; i
< setup
->tiles_x
; i
++) {
107 for (j
= 0; j
< setup
->tiles_y
; j
++) {
108 struct cmd_block_list
*list
= &setup
->tile
[i
][j
];
109 struct cmd_block
*block
;
110 struct cmd_block
*tmp
;
112 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
117 list
->head
= list
->tail
;
124 struct data_block_list
*list
= &setup
->data
;
125 struct data_block
*block
, *tmp
;
127 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
132 list
->head
= list
->tail
;
137 setup
->clear
.flags
= 0;
139 /* Have an explicit "start-binning" call and get rid of this
142 setup
->line
= first_line
;
143 setup
->point
= first_point
;
144 setup
->triangle
= first_triangle
;
150 /* Add a command to all active bins.
152 static void bin_everywhere( struct setup_context
*setup
,
154 const union lp_rast_cmd_arg arg
)
157 for (i
= 0; i
< setup
->tiles_x
; i
++)
158 for (j
= 0; j
< setup
->tiles_y
; j
++)
159 bin_command( &setup
->tile
[i
][j
], cmd
, arg
);
164 rasterize_bins( struct setup_context
*setup
,
165 boolean write_depth
)
167 struct lp_rasterizer
*rast
= setup
->rast
;
168 struct cmd_block
*block
;
171 SETUP_DEBUG("%s\n", __FUNCTION__
);
176 setup
->fb
.cbuf
!= NULL
,
177 setup
->fb
.zsbuf
!= NULL
&& write_depth
,
183 for (i
= 0; i
< setup
->tiles_x
; i
++) {
184 for (j
= 0; j
< setup
->tiles_y
; j
++) {
186 lp_rast_start_tile( rast
,
190 for (block
= setup
->tile
[i
][j
].head
; block
; block
= block
->next
) {
191 for (k
= 0; k
< block
->count
; k
++) {
192 block
->cmd
[k
]( rast
, block
->arg
[k
] );
196 lp_rast_end_tile( rast
);
202 reset_context( setup
);
208 begin_binning( struct setup_context
*setup
)
210 SETUP_DEBUG("%s\n", __FUNCTION__
);
212 if (!setup
->fb
.cbuf
&& !setup
->fb
.zsbuf
) {
214 setup
->fb
.height
= 0;
216 else if (!setup
->fb
.zsbuf
) {
217 setup
->fb
.width
= setup
->fb
.cbuf
->width
;
218 setup
->fb
.height
= setup
->fb
.cbuf
->height
;
220 else if (!setup
->fb
.cbuf
) {
221 setup
->fb
.width
= setup
->fb
.zsbuf
->width
;
222 setup
->fb
.height
= setup
->fb
.zsbuf
->height
;
225 /* XXX: not sure what we're really supposed to do for
226 * mis-matched color & depth buffer sizes.
228 setup
->fb
.width
= MIN2(setup
->fb
.cbuf
->width
,
229 setup
->fb
.zsbuf
->width
);
230 setup
->fb
.height
= MIN2(setup
->fb
.cbuf
->height
,
231 setup
->fb
.zsbuf
->height
);
234 setup
->tiles_x
= align(setup
->fb
.width
, TILESIZE
) / TILESIZE
;
235 setup
->tiles_y
= align(setup
->fb
.height
, TILESIZE
) / TILESIZE
;
237 if (setup
->fb
.cbuf
) {
238 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
)
239 bin_everywhere( setup
,
241 setup
->clear
.color
);
243 bin_everywhere( setup
, lp_rast_load_color
, lp_rast_arg_null() );
246 if (setup
->fb
.zsbuf
) {
247 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
248 bin_everywhere( setup
,
249 lp_rast_clear_zstencil
,
250 setup
->clear
.zstencil
);
252 bin_everywhere( setup
, lp_rast_load_zstencil
, lp_rast_arg_null() );
257 /* This basically bins and then flushes any outstanding full-screen
260 * TODO: fast path for fullscreen clears and no triangles.
263 execute_clears( struct setup_context
*setup
)
265 SETUP_DEBUG("%s\n", __FUNCTION__
);
267 begin_binning( setup
);
268 rasterize_bins( setup
, TRUE
);
273 set_state( struct setup_context
*setup
,
276 unsigned old_state
= setup
->state
;
278 if (old_state
== new_state
)
281 SETUP_DEBUG("%s old %d new %d\n", __FUNCTION__
, old_state
, new_state
);
285 begin_binning( setup
);
289 if (old_state
== SETUP_ACTIVE
) {
296 if (old_state
== SETUP_CLEARED
)
297 execute_clears( setup
);
299 rasterize_bins( setup
, TRUE
);
303 setup
->state
= new_state
;
308 lp_setup_flush( struct setup_context
*setup
,
311 SETUP_DEBUG("%s\n", __FUNCTION__
);
313 set_state( setup
, SETUP_FLUSHED
);
318 lp_setup_bind_framebuffer( struct setup_context
*setup
,
319 struct pipe_surface
*color
,
320 struct pipe_surface
*zstencil
)
322 SETUP_DEBUG("%s\n", __FUNCTION__
);
324 set_state( setup
, SETUP_FLUSHED
);
326 pipe_surface_reference( &setup
->fb
.cbuf
, color
);
327 pipe_surface_reference( &setup
->fb
.zsbuf
, zstencil
);
331 lp_setup_clear( struct setup_context
*setup
,
339 SETUP_DEBUG("%s state %d\n", __FUNCTION__
, setup
->state
);
342 if (flags
& PIPE_CLEAR_COLOR
) {
343 for (i
= 0; i
< 4; ++i
)
344 setup
->clear
.color
.clear_color
[i
] = float_to_ubyte(color
[i
]);
347 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
348 setup
->clear
.zstencil
.clear_zstencil
=
349 util_pack_z_stencil(setup
->fb
.zsbuf
->format
,
354 if (setup
->state
== SETUP_ACTIVE
) {
355 /* Add the clear to existing bins. In the unusual case where
356 * both color and depth-stencilare being cleared, we could
357 * discard the currently binned scene and start again, but I
358 * don't see that as being a common usage.
360 if (flags
& PIPE_CLEAR_COLOR
)
361 bin_everywhere( setup
,
363 setup
->clear
.color
);
365 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
)
366 bin_everywhere( setup
,
367 lp_rast_clear_zstencil
,
368 setup
->clear
.zstencil
);
371 /* Put ourselves into the 'pre-clear' state, specifically to try
372 * and accumulate multiple clears to color and depth_stencil
373 * buffers which the app or state-tracker might issue
376 set_state( setup
, SETUP_CLEARED
);
378 setup
->clear
.flags
|= flags
;
385 lp_setup_set_triangle_state( struct setup_context
*setup
,
387 boolean ccw_is_frontface
)
389 SETUP_DEBUG("%s\n", __FUNCTION__
);
391 setup
->ccw_is_frontface
= ccw_is_frontface
;
392 setup
->cullmode
= cull_mode
;
393 setup
->triangle
= first_triangle
;
399 lp_setup_set_fs_inputs( struct setup_context
*setup
,
400 const struct lp_shader_input
*input
,
403 SETUP_DEBUG("%s\n", __FUNCTION__
);
405 memcpy( setup
->fs
.input
, input
, nr
* sizeof input
[0] );
406 setup
->fs
.nr_inputs
= nr
;
410 lp_setup_set_fs( struct setup_context
*setup
,
411 struct lp_fragment_shader
*fs
)
413 SETUP_DEBUG("%s\n", __FUNCTION__
);
414 /* FIXME: reference count */
416 setup
->fs
.jit_function
= fs
->current
->jit_function
;
420 lp_setup_set_fs_constants(struct setup_context
*setup
,
421 struct pipe_buffer
*buffer
)
423 const void *data
= buffer
? llvmpipe_buffer(buffer
)->data
: NULL
;
424 struct pipe_buffer
*dummy
;
426 SETUP_DEBUG("%s\n", __FUNCTION__
);
428 /* FIXME: hold on to the reference */
430 pipe_buffer_reference(&dummy
, buffer
);
432 setup
->fs
.jit_context
.constants
= data
;
434 setup
->fs
.jit_context_dirty
= TRUE
;
439 lp_setup_set_alpha_ref_value( struct setup_context
*setup
,
440 float alpha_ref_value
)
442 SETUP_DEBUG("%s\n", __FUNCTION__
);
444 if(setup
->fs
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
445 setup
->fs
.jit_context
.alpha_ref_value
= alpha_ref_value
;
446 setup
->fs
.jit_context_dirty
= TRUE
;
451 lp_setup_set_blend_color( struct setup_context
*setup
,
452 const struct pipe_blend_color
*blend_color
)
456 SETUP_DEBUG("%s\n", __FUNCTION__
);
458 if(!setup
->fs
.jit_context
.blend_color
)
459 setup
->fs
.jit_context
.blend_color
= align_malloc(4 * 16, 16);
461 for (i
= 0; i
< 4; ++i
) {
462 uint8_t c
= float_to_ubyte(blend_color
->color
[i
]);
463 for (j
= 0; j
< 16; ++j
)
464 setup
->fs
.jit_context
.blend_color
[i
*4 + j
] = c
;
467 setup
->fs
.jit_context_dirty
= TRUE
;
471 lp_setup_set_sampler_textures( struct setup_context
*setup
,
472 unsigned num
, struct pipe_texture
**texture
)
474 struct pipe_texture
*dummy
;
477 SETUP_DEBUG("%s\n", __FUNCTION__
);
480 assert(num
<= PIPE_MAX_SAMPLERS
);
482 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
483 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
485 /* FIXME: hold on to the reference */
487 pipe_texture_reference(&dummy
, tex
);
490 struct llvmpipe_texture
*lp_tex
= llvmpipe_texture(tex
);
491 struct lp_jit_texture
*jit_tex
= &setup
->fs
.jit_context
.textures
[i
];
492 jit_tex
->width
= tex
->width
[0];
493 jit_tex
->height
= tex
->height
[0];
494 jit_tex
->stride
= lp_tex
->stride
[0];
496 jit_tex
->data
= lp_tex
->data
;
498 /* FIXME: map the rendertarget */
503 setup
->fs
.jit_context_dirty
= TRUE
;
507 lp_setup_is_texture_referenced( struct setup_context
*setup
,
508 const struct pipe_texture
*texture
)
511 return PIPE_UNREFERENCED
;
516 lp_setup_update_shader_state( struct setup_context
*setup
)
518 SETUP_DEBUG("%s\n", __FUNCTION__
);
520 if(setup
->fs
.jit_context_dirty
) {
521 if(!setup
->fs
.last_jc
||
522 memcmp(setup
->fs
.last_jc
, &setup
->fs
.jit_context
, sizeof *setup
->fs
.last_jc
)) {
523 struct lp_jit_context
*jc
;
525 jc
= get_data(&setup
->data
, sizeof *jc
);
527 memcpy(jc
, &setup
->fs
.jit_context
, sizeof *jc
);
528 setup
->fs
.last_jc
= jc
;
532 setup
->fs
.jit_context_dirty
= FALSE
;
535 assert(setup
->fs
.last_jc
);
539 /* Stubs for lines & points for now:
542 lp_setup_point(struct setup_context
*setup
,
543 const float (*v0
)[4])
545 lp_setup_update_shader_state(setup
);
546 setup
->point( setup
, v0
);
550 lp_setup_line(struct setup_context
*setup
,
551 const float (*v0
)[4],
552 const float (*v1
)[4])
554 lp_setup_update_shader_state(setup
);
555 setup
->line( setup
, v0
, v1
);
559 lp_setup_tri(struct setup_context
*setup
,
560 const float (*v0
)[4],
561 const float (*v1
)[4],
562 const float (*v2
)[4])
564 SETUP_DEBUG("%s\n", __FUNCTION__
);
566 lp_setup_update_shader_state(setup
);
567 setup
->triangle( setup
, v0
, v1
, v2
);
572 lp_setup_destroy( struct setup_context
*setup
)
576 reset_context( setup
);
578 for (i
= 0; i
< TILES_X
; i
++)
579 for (j
= 0; j
< TILES_Y
; j
++)
580 FREE(setup
->tile
[i
][j
].head
);
582 lp_rast_destroy( setup
->rast
);
588 * Create a new primitive tiling engine. Currently also creates a
589 * rasterizer to use with it.
591 struct setup_context
*
592 lp_setup_create( struct pipe_screen
*screen
)
594 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
597 setup
->rast
= lp_rast_create( screen
);
601 for (i
= 0; i
< TILES_X
; i
++)
602 for (j
= 0; j
< TILES_Y
; j
++)
603 setup
->tile
[i
][j
].head
=
604 setup
->tile
[i
][j
].tail
= CALLOC_STRUCT(cmd_block
);
607 setup
->data
.tail
= CALLOC_STRUCT(data_block
);
609 setup
->triangle
= first_triangle
;
610 setup
->line
= first_line
;
611 setup
->point
= first_point
;