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 "util/u_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "util/u_surface.h"
41 #include "lp_scene_queue.h"
42 #include "lp_texture.h"
46 #include "lp_setup_context.h"
47 #include "lp_screen.h"
48 #include "state_tracker/sw_winsys.h"
50 #include "draw/draw_context.h"
51 #include "draw/draw_vbuf.h"
54 static void set_scene_state( struct lp_setup_context *, unsigned );
58 lp_setup_get_current_scene(struct lp_setup_context *setup)
62 /* wait for a free/empty scene
64 setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
66 assert(lp_scene_is_empty(setup->scene));
68 lp_scene_begin_binning(setup->scene,
76 first_triangle( struct lp_setup_context *setup,
81 set_scene_state( setup, SETUP_ACTIVE );
82 lp_setup_choose_triangle( setup );
83 setup->triangle( setup, v0, v1, v2 );
87 first_line( struct lp_setup_context *setup,
91 set_scene_state( setup, SETUP_ACTIVE );
92 lp_setup_choose_line( setup );
93 setup->line( setup, v0, v1 );
97 first_point( struct lp_setup_context *setup,
100 set_scene_state( setup, SETUP_ACTIVE );
101 lp_setup_choose_point( setup );
102 setup->point( setup, v0 );
105 static void reset_context( struct lp_setup_context *setup )
107 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
109 /* Reset derived state */
110 setup->constants.stored_size = 0;
111 setup->constants.stored_data = NULL;
112 setup->fs.stored = NULL;
120 setup->clear.flags = 0;
122 /* Have an explicit "start-binning" call and get rid of this
125 setup->line = first_line;
126 setup->point = first_point;
127 setup->triangle = first_triangle;
131 /** Rasterize all scene's bins */
133 lp_setup_rasterize_scene( struct lp_setup_context *setup,
134 boolean write_depth )
136 struct lp_scene *scene = lp_setup_get_current_scene(setup);
138 lp_scene_rasterize(scene,
142 reset_context( setup );
144 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
150 begin_binning( struct lp_setup_context *setup )
152 struct lp_scene *scene = lp_setup_get_current_scene(setup);
154 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
155 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
156 (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
158 if (setup->fb.nr_cbufs) {
159 if (setup->clear.flags & PIPE_CLEAR_COLOR)
160 lp_scene_bin_everywhere( scene,
162 setup->clear.color );
164 lp_scene_bin_everywhere( scene,
166 lp_rast_arg_null() );
169 if (setup->fb.zsbuf) {
170 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
171 lp_scene_bin_everywhere( scene,
172 lp_rast_clear_zstencil,
173 setup->clear.zstencil );
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 lp_setup_context *setup )
188 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
190 begin_binning( setup );
191 lp_setup_rasterize_scene( setup, TRUE );
196 set_scene_state( struct lp_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 lp_setup_rasterize_scene( setup, TRUE );
226 setup->state = new_state;
231 lp_setup_flush( struct lp_setup_context *setup,
234 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
236 set_scene_state( setup, SETUP_FLUSHED );
241 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
242 const struct pipe_framebuffer_state *fb )
244 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
246 /* Flush any old scene.
248 set_scene_state( setup, SETUP_FLUSHED );
250 /* Set new state. This will be picked up later when we next need a
253 util_copy_framebuffer_state(&setup->fb, fb);
258 lp_setup_clear( struct lp_setup_context *setup,
264 struct lp_scene *scene = lp_setup_get_current_scene(setup);
267 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
270 if (flags & PIPE_CLEAR_COLOR) {
271 for (i = 0; i < 4; ++i)
272 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
275 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
276 setup->clear.zstencil.clear_zstencil =
277 util_pack_z_stencil(setup->fb.zsbuf->format,
282 if (setup->state == SETUP_ACTIVE) {
283 /* Add the clear to existing scene. In the unusual case where
284 * both color and depth-stencil are being cleared when there's
285 * already been some rendering, we could discard the currently
286 * binned scene and start again, but I don't see that as being
289 if (flags & PIPE_CLEAR_COLOR)
290 lp_scene_bin_everywhere( scene,
292 setup->clear.color );
294 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
295 lp_scene_bin_everywhere( scene,
296 lp_rast_clear_zstencil,
297 setup->clear.zstencil );
300 /* Put ourselves into the 'pre-clear' state, specifically to try
301 * and accumulate multiple clears to color and depth_stencil
302 * buffers which the app or state-tracker might issue
305 set_scene_state( setup, SETUP_CLEARED );
307 setup->clear.flags |= flags;
315 struct pipe_fence_handle *
316 lp_setup_fence( struct lp_setup_context *setup )
318 struct lp_scene *scene = lp_setup_get_current_scene(setup);
319 const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
320 struct lp_fence *fence = lp_fence_create(rank);
322 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
324 set_scene_state( setup, SETUP_ACTIVE );
326 /* insert the fence into all command bins */
327 lp_scene_bin_everywhere( scene,
329 lp_rast_arg_fence(fence) );
331 return (struct pipe_fence_handle *) fence;
336 lp_setup_set_triangle_state( struct lp_setup_context *setup,
338 boolean ccw_is_frontface,
340 boolean gl_rasterization_rules)
342 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
344 setup->ccw_is_frontface = ccw_is_frontface;
345 setup->cullmode = cull_mode;
346 setup->triangle = first_triangle;
347 setup->scissor_test = scissor;
348 setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
354 lp_setup_set_fs_inputs( struct lp_setup_context *setup,
355 const struct lp_shader_input *input,
358 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
360 memcpy( setup->fs.input, input, nr * sizeof input[0] );
361 setup->fs.nr_inputs = nr;
365 lp_setup_set_fs_functions( struct lp_setup_context *setup,
366 lp_jit_frag_func jit_function0,
367 lp_jit_frag_func jit_function1,
370 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
371 /* FIXME: reference count */
373 setup->fs.current.jit_function[0] = jit_function0;
374 setup->fs.current.jit_function[1] = jit_function1;
375 setup->fs.current.opaque = opaque;
376 setup->dirty |= LP_SETUP_NEW_FS;
380 lp_setup_set_fs_constants(struct lp_setup_context *setup,
381 struct pipe_resource *buffer)
383 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
385 pipe_resource_reference(&setup->constants.current, buffer);
387 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
392 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
393 float alpha_ref_value )
395 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
397 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
398 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
399 setup->dirty |= LP_SETUP_NEW_FS;
404 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
405 const ubyte refs[2] )
407 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
409 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
410 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
411 setup->fs.current.jit_context.stencil_ref_front = refs[0];
412 setup->fs.current.jit_context.stencil_ref_back = refs[1];
413 setup->dirty |= LP_SETUP_NEW_FS;
418 lp_setup_set_blend_color( struct lp_setup_context *setup,
419 const struct pipe_blend_color *blend_color )
421 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
425 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
426 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
427 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
433 lp_setup_set_scissor( struct lp_setup_context *setup,
434 const struct pipe_scissor_state *scissor )
436 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
440 if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
441 setup->scissor.current = *scissor; /* struct copy */
442 setup->dirty |= LP_SETUP_NEW_SCISSOR;
448 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
449 boolean flatshade_first )
451 setup->flatshade_first = flatshade_first;
456 lp_setup_set_vertex_info( struct lp_setup_context *setup,
457 struct vertex_info *vertex_info )
459 /* XXX: just silently holding onto the pointer:
461 setup->vertex_info = vertex_info;
466 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
469 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
471 struct pipe_sampler_view **views)
475 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
477 assert(num <= PIPE_MAX_SAMPLERS);
479 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
480 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
483 struct pipe_resource *tex = view->texture;
484 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
485 struct lp_jit_texture *jit_tex;
486 jit_tex = &setup->fs.current.jit_context.textures[i];
487 jit_tex->width = tex->width0;
488 jit_tex->height = tex->height0;
489 jit_tex->depth = tex->depth0;
490 jit_tex->last_level = tex->last_level;
492 /* We're referencing the texture's internal data, so save a
495 pipe_resource_reference(&setup->fs.current_tex[i], tex);
498 /* regular texture - setup array of mipmap level pointers */
500 for (j = 0; j <= tex->last_level; j++) {
502 (ubyte *) lp_tex->data + lp_tex->level_offset[j];
503 jit_tex->row_stride[j] = lp_tex->stride[j];
507 /* display target texture/surface */
509 * XXX: Where should this be unmapped?
512 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
513 struct sw_winsys *winsys = screen->winsys;
514 jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
516 jit_tex->row_stride[0] = lp_tex->stride[0];
517 assert(jit_tex->data[0]);
522 setup->dirty |= LP_SETUP_NEW_FS;
527 * Is the given texture referenced by any scene?
528 * Note: we have to check all scenes including any scenes currently
529 * being rendered and the current scene being built.
532 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
533 const struct pipe_resource *texture )
537 /* check the render targets */
538 for (i = 0; i < setup->fb.nr_cbufs; i++) {
539 if (setup->fb.cbufs[i]->texture == texture)
540 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
542 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
543 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
546 /* check textures referenced by the scene */
547 for (i = 0; i < Elements(setup->scenes); i++) {
548 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
549 return PIPE_REFERENCED_FOR_READ;
553 return PIPE_UNREFERENCED;
558 * Called by vbuf code when we're about to draw something.
561 lp_setup_update_state( struct lp_setup_context *setup )
563 struct lp_scene *scene = lp_setup_get_current_scene(setup);
565 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
567 assert(setup->fs.current.jit_function);
569 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
573 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
575 /* smear each blend color component across 16 ubyte elements */
576 for (i = 0; i < 4; ++i) {
577 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
578 for (j = 0; j < 16; ++j)
579 stored[i*16 + j] = c;
582 setup->blend_color.stored = stored;
584 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
585 setup->dirty |= LP_SETUP_NEW_FS;
588 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
591 stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
593 stored[0] = (float) setup->scissor.current.minx;
594 stored[1] = (float) setup->scissor.current.miny;
595 stored[2] = (float) setup->scissor.current.maxx;
596 stored[3] = (float) setup->scissor.current.maxy;
598 setup->scissor.stored = stored;
600 setup->fs.current.jit_context.scissor_xmin = stored[0];
601 setup->fs.current.jit_context.scissor_ymin = stored[1];
602 setup->fs.current.jit_context.scissor_xmax = stored[2];
603 setup->fs.current.jit_context.scissor_ymax = stored[3];
605 setup->dirty |= LP_SETUP_NEW_FS;
608 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
609 struct pipe_resource *buffer = setup->constants.current;
612 unsigned current_size = buffer->width0;
613 const void *current_data = llvmpipe_resource(buffer)->data;
615 /* TODO: copy only the actually used constants? */
617 if(setup->constants.stored_size != current_size ||
618 !setup->constants.stored_data ||
619 memcmp(setup->constants.stored_data,
621 current_size) != 0) {
624 stored = lp_scene_alloc(scene, current_size);
629 setup->constants.stored_size = current_size;
630 setup->constants.stored_data = stored;
635 setup->constants.stored_size = 0;
636 setup->constants.stored_data = NULL;
639 setup->fs.current.jit_context.constants = setup->constants.stored_data;
640 setup->dirty |= LP_SETUP_NEW_FS;
644 if(setup->dirty & LP_SETUP_NEW_FS) {
645 if(!setup->fs.stored ||
646 memcmp(setup->fs.stored,
648 sizeof setup->fs.current) != 0) {
649 /* The fs state that's been stored in the scene is different from
650 * the new, current state. So allocate a new lp_rast_state object
651 * and append it to the bin's setup data buffer.
654 struct lp_rast_state *stored =
655 (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
659 sizeof setup->fs.current);
660 setup->fs.stored = stored;
662 /* put the state-set command into all bins */
663 lp_scene_bin_state_command( scene,
665 lp_rast_arg_state(setup->fs.stored) );
668 /* The scene now references the textures in the rasterization
669 * state record. Note that now.
671 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
672 if (setup->fs.current_tex[i])
673 lp_scene_texture_reference(scene, setup->fs.current_tex[i]);
680 assert(setup->fs.stored);
685 /* Only caller is lp_setup_vbuf_destroy()
688 lp_setup_destroy( struct lp_setup_context *setup )
692 reset_context( setup );
694 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
695 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
698 pipe_resource_reference(&setup->constants.current, NULL);
700 /* free the scenes in the 'empty' queue */
702 struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
705 lp_scene_destroy(scene);
708 lp_rast_destroy( setup->rast );
715 * Create a new primitive tiling engine. Plug it into the backend of
716 * the draw module. Currently also creates a rasterizer to use with
719 struct lp_setup_context *
720 lp_setup_create( struct pipe_context *pipe,
721 struct draw_context *draw )
724 struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
729 lp_setup_init_vbuf(setup);
731 setup->empty_scenes = lp_scene_queue_create();
732 if (!setup->empty_scenes)
735 /* XXX: move this to the screen and share between contexts:
737 setup->rast = lp_rast_create();
741 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
745 draw_set_rasterize_stage(draw, setup->vbuf);
746 draw_set_render(draw, &setup->base);
748 /* create some empty scenes */
749 for (i = 0; i < MAX_SCENES; i++) {
750 setup->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
752 lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
755 setup->triangle = first_triangle;
756 setup->line = first_line;
757 setup->point = first_point;
765 lp_rast_destroy( setup->rast );
770 if (setup->empty_scenes)
771 lp_scene_queue_destroy(setup->empty_scenes);