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
37 #include "pipe/p_defines.h"
38 #include "util/u_framebuffer.h"
39 #include "util/u_inlines.h"
40 #include "util/u_memory.h"
41 #include "util/u_pack_color.h"
42 #include "draw/draw_pipe.h"
43 #include "lp_context.h"
44 #include "lp_memory.h"
46 #include "lp_texture.h"
51 #include "lp_setup_context.h"
52 #include "lp_screen.h"
54 #include "state_tracker/sw_winsys.h"
56 #include "draw/draw_context.h"
57 #include "draw/draw_vbuf.h"
60 static boolean
set_scene_state( struct lp_setup_context
*, enum setup_state
,
62 static boolean
try_update_scene_state( struct lp_setup_context
*setup
);
66 lp_setup_get_empty_scene(struct lp_setup_context
*setup
)
68 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
69 boolean discard
= lp
->rasterizer
? lp
->rasterizer
->rasterizer_discard
: FALSE
;
71 assert(setup
->scene
== NULL
);
74 setup
->scene_idx
%= Elements(setup
->scenes
);
76 setup
->scene
= setup
->scenes
[setup
->scene_idx
];
78 if (setup
->scene
->fence
) {
79 if (LP_DEBUG
& DEBUG_SETUP
)
80 debug_printf("%s: wait for scene %d\n",
81 __FUNCTION__
, setup
->scene
->fence
->id
);
83 lp_fence_wait(setup
->scene
->fence
);
86 lp_scene_begin_binning(setup
->scene
, &setup
->fb
, discard
);
92 first_triangle( struct lp_setup_context
*setup
,
97 assert(setup
->state
== SETUP_ACTIVE
);
98 lp_setup_choose_triangle( setup
);
99 setup
->triangle( setup
, v0
, v1
, v2
);
103 first_line( struct lp_setup_context
*setup
,
104 const float (*v0
)[4],
105 const float (*v1
)[4])
107 assert(setup
->state
== SETUP_ACTIVE
);
108 lp_setup_choose_line( setup
);
109 setup
->line( setup
, v0
, v1
);
113 first_point( struct lp_setup_context
*setup
,
114 const float (*v0
)[4])
116 assert(setup
->state
== SETUP_ACTIVE
);
117 lp_setup_choose_point( setup
);
118 setup
->point( setup
, v0
);
121 void lp_setup_reset( struct lp_setup_context
*setup
)
125 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
127 /* Reset derived state */
128 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
129 setup
->constants
[i
].stored_size
= 0;
130 setup
->constants
[i
].stored_data
= NULL
;
132 setup
->fs
.stored
= NULL
;
140 memset(&setup
->clear
, 0, sizeof setup
->clear
);
142 /* Have an explicit "start-binning" call and get rid of this
145 setup
->line
= first_line
;
146 setup
->point
= first_point
;
147 setup
->triangle
= first_triangle
;
151 /** Rasterize all scene's bins */
153 lp_setup_rasterize_scene( struct lp_setup_context
*setup
)
155 struct lp_scene
*scene
= setup
->scene
;
156 struct llvmpipe_screen
*screen
= llvmpipe_screen(scene
->pipe
->screen
);
158 scene
->num_active_queries
= setup
->active_binned_queries
;
159 memcpy(scene
->active_queries
, setup
->active_queries
,
160 scene
->num_active_queries
* sizeof(scene
->active_queries
[0]));
162 lp_scene_end_binning(scene
);
164 lp_fence_reference(&setup
->last_fence
, scene
->fence
);
166 if (setup
->last_fence
)
167 setup
->last_fence
->issued
= TRUE
;
169 pipe_mutex_lock(screen
->rast_mutex
);
170 lp_rast_queue_scene(screen
->rast
, scene
);
171 lp_rast_finish(screen
->rast
);
172 pipe_mutex_unlock(screen
->rast_mutex
);
174 lp_scene_end_rasterization(setup
->scene
);
175 lp_setup_reset( setup
);
177 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
183 begin_binning( struct lp_setup_context
*setup
)
185 struct lp_scene
*scene
= setup
->scene
;
186 boolean need_zsload
= FALSE
;
190 assert(scene
->fence
== NULL
);
192 /* Always create a fence:
194 scene
->fence
= lp_fence_create(MAX2(1, setup
->num_threads
));
198 ok
= try_update_scene_state(setup
);
202 if (setup
->fb
.zsbuf
&&
203 ((setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
204 util_format_is_depth_and_stencil(setup
->fb
.zsbuf
->format
))
207 LP_DBG(DEBUG_SETUP
, "%s color: %s depth: %s\n", __FUNCTION__
,
208 (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) ? "clear": "load",
209 need_zsload
? "clear": "load");
211 if (setup
->fb
.nr_cbufs
) {
212 if (setup
->clear
.flags
& PIPE_CLEAR_COLOR
) {
213 ok
= lp_scene_bin_everywhere( scene
,
214 LP_RAST_OP_CLEAR_COLOR
,
215 setup
->clear
.color
);
221 if (setup
->fb
.zsbuf
) {
222 if (setup
->clear
.flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
224 scene
->has_depthstencil_clear
= TRUE
;
226 ok
= lp_scene_bin_everywhere( scene
,
227 LP_RAST_OP_CLEAR_ZSTENCIL
,
229 setup
->clear
.zsvalue
,
230 setup
->clear
.zsmask
));
236 setup
->clear
.flags
= 0;
237 setup
->clear
.zsmask
= 0;
238 setup
->clear
.zsvalue
= 0;
240 LP_DBG(DEBUG_SETUP
, "%s done\n", __FUNCTION__
);
245 /* This basically bins and then flushes any outstanding full-screen
248 * TODO: fast path for fullscreen clears and no triangles.
251 execute_clears( struct lp_setup_context
*setup
)
253 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
255 return begin_binning( setup
);
258 const char *states
[] = {
266 set_scene_state( struct lp_setup_context
*setup
,
267 enum setup_state new_state
,
270 unsigned old_state
= setup
->state
;
272 if (old_state
== new_state
)
275 if (LP_DEBUG
& DEBUG_SCENE
) {
276 debug_printf("%s old %s new %s%s%s\n",
280 (new_state
== SETUP_FLUSHED
) ? ": " : "",
281 (new_state
== SETUP_FLUSHED
) ? reason
: "");
283 if (new_state
== SETUP_FLUSHED
&& setup
->scene
)
284 lp_debug_draw_bins_by_cmd_length(setup
->scene
);
287 /* wait for a free/empty scene
289 if (old_state
== SETUP_FLUSHED
)
290 lp_setup_get_empty_scene(setup
);
297 if (!begin_binning( setup
))
302 if (old_state
== SETUP_CLEARED
)
303 if (!execute_clears( setup
))
306 lp_setup_rasterize_scene( setup
);
307 assert(setup
->scene
== NULL
);
311 assert(0 && "invalid setup state mode");
315 setup
->state
= new_state
;
320 lp_scene_end_rasterization(setup
->scene
);
324 setup
->state
= SETUP_FLUSHED
;
325 lp_setup_reset( setup
);
331 lp_setup_flush( struct lp_setup_context
*setup
,
332 struct pipe_fence_handle
**fence
,
335 set_scene_state( setup
, SETUP_FLUSHED
, reason
);
338 lp_fence_reference((struct lp_fence
**)fence
, setup
->last_fence
);
344 lp_setup_bind_framebuffer( struct lp_setup_context
*setup
,
345 const struct pipe_framebuffer_state
*fb
)
347 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
349 /* Flush any old scene.
351 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
354 * Ensure the old scene is not reused.
356 assert(!setup
->scene
);
358 /* Set new state. This will be picked up later when we next need a
361 util_copy_framebuffer_state(&setup
->fb
, fb
);
362 setup
->framebuffer
.x0
= 0;
363 setup
->framebuffer
.y0
= 0;
364 setup
->framebuffer
.x1
= fb
->width
-1;
365 setup
->framebuffer
.y1
= fb
->height
-1;
366 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
371 lp_setup_try_clear( struct lp_setup_context
*setup
,
372 const union pipe_color_union
*color
,
378 uint64_t zsvalue
= 0;
379 union lp_rast_cmd_arg color_arg
;
382 LP_DBG(DEBUG_SETUP
, "%s state %d\n", __FUNCTION__
, setup
->state
);
384 if (flags
& PIPE_CLEAR_COLOR
) {
385 for (i
= 0; i
< 4; i
++)
386 color_arg
.clear_color
.i
[i
] = color
->i
[i
];
389 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
390 uint32_t zmask
= (flags
& PIPE_CLEAR_DEPTH
) ? ~0 : 0;
391 uint8_t smask
= (flags
& PIPE_CLEAR_STENCIL
) ? ~0 : 0;
393 zsvalue
= util_pack64_z_stencil(setup
->fb
.zsbuf
->format
,
398 zsmask
= util_pack64_mask_z_stencil(setup
->fb
.zsbuf
->format
,
405 if (setup
->state
== SETUP_ACTIVE
) {
406 struct lp_scene
*scene
= setup
->scene
;
408 /* Add the clear to existing scene. In the unusual case where
409 * both color and depth-stencil are being cleared when there's
410 * already been some rendering, we could discard the currently
411 * binned scene and start again, but I don't see that as being
414 if (flags
& PIPE_CLEAR_COLOR
) {
415 if (!lp_scene_bin_everywhere( scene
,
416 LP_RAST_OP_CLEAR_COLOR
,
421 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
422 if (!lp_scene_bin_everywhere( scene
,
423 LP_RAST_OP_CLEAR_ZSTENCIL
,
424 lp_rast_arg_clearzs(zsvalue
, zsmask
) ))
429 /* Put ourselves into the 'pre-clear' state, specifically to try
430 * and accumulate multiple clears to color and depth_stencil
431 * buffers which the app or state-tracker might issue
434 set_scene_state( setup
, SETUP_CLEARED
, __FUNCTION__
);
436 setup
->clear
.flags
|= flags
;
438 if (flags
& PIPE_CLEAR_DEPTHSTENCIL
) {
439 setup
->clear
.zsmask
|= zsmask
;
440 setup
->clear
.zsvalue
=
441 (setup
->clear
.zsvalue
& ~zsmask
) | (zsvalue
& zsmask
);
444 if (flags
& PIPE_CLEAR_COLOR
) {
445 memcpy(&setup
->clear
.color
.clear_color
,
447 sizeof setup
->clear
.color
.clear_color
);
455 lp_setup_clear( struct lp_setup_context
*setup
,
456 const union pipe_color_union
*color
,
461 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
)) {
462 lp_setup_flush(setup
, NULL
, __FUNCTION__
);
464 if (!lp_setup_try_clear( setup
, color
, depth
, stencil
, flags
))
474 lp_setup_set_triangle_state( struct lp_setup_context
*setup
,
476 boolean ccw_is_frontface
,
478 boolean half_pixel_center
,
479 boolean bottom_edge_rule
)
481 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
483 setup
->ccw_is_frontface
= ccw_is_frontface
;
484 setup
->cullmode
= cull_mode
;
485 setup
->triangle
= first_triangle
;
486 setup
->pixel_offset
= half_pixel_center
? 0.5f
: 0.0f
;
487 setup
->bottom_edge_rule
= bottom_edge_rule
;
489 if (setup
->scissor_test
!= scissor
) {
490 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
491 setup
->scissor_test
= scissor
;
496 lp_setup_set_line_state( struct lp_setup_context
*setup
,
499 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
501 setup
->line_width
= line_width
;
505 lp_setup_set_point_state( struct lp_setup_context
*setup
,
507 boolean point_size_per_vertex
,
508 uint sprite_coord_enable
,
509 uint sprite_coord_origin
)
511 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
513 setup
->point_size
= point_size
;
514 setup
->sprite_coord_enable
= sprite_coord_enable
;
515 setup
->sprite_coord_origin
= sprite_coord_origin
;
516 setup
->point_size_per_vertex
= point_size_per_vertex
;
520 lp_setup_set_setup_variant( struct lp_setup_context
*setup
,
521 const struct lp_setup_variant
*variant
)
523 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
525 setup
->setup
.variant
= variant
;
529 lp_setup_set_fs_variant( struct lp_setup_context
*setup
,
530 struct lp_fragment_shader_variant
*variant
)
532 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
,
534 /* FIXME: reference count */
536 setup
->fs
.current
.variant
= variant
;
537 setup
->dirty
|= LP_SETUP_NEW_FS
;
541 lp_setup_set_fs_constants(struct lp_setup_context
*setup
,
543 struct pipe_constant_buffer
*buffers
)
547 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
549 assert(num
<= Elements(setup
->constants
));
551 for (i
= 0; i
< num
; ++i
) {
552 util_copy_constant_buffer(&setup
->constants
[i
].current
, &buffers
[i
]);
554 for (; i
< Elements(setup
->constants
); i
++) {
555 util_copy_constant_buffer(&setup
->constants
[i
].current
, NULL
);
557 setup
->dirty
|= LP_SETUP_NEW_CONSTANTS
;
562 lp_setup_set_alpha_ref_value( struct lp_setup_context
*setup
,
563 float alpha_ref_value
)
565 LP_DBG(DEBUG_SETUP
, "%s %f\n", __FUNCTION__
, alpha_ref_value
);
567 if(setup
->fs
.current
.jit_context
.alpha_ref_value
!= alpha_ref_value
) {
568 setup
->fs
.current
.jit_context
.alpha_ref_value
= alpha_ref_value
;
569 setup
->dirty
|= LP_SETUP_NEW_FS
;
574 lp_setup_set_stencil_ref_values( struct lp_setup_context
*setup
,
575 const ubyte refs
[2] )
577 LP_DBG(DEBUG_SETUP
, "%s %d %d\n", __FUNCTION__
, refs
[0], refs
[1]);
579 if (setup
->fs
.current
.jit_context
.stencil_ref_front
!= refs
[0] ||
580 setup
->fs
.current
.jit_context
.stencil_ref_back
!= refs
[1]) {
581 setup
->fs
.current
.jit_context
.stencil_ref_front
= refs
[0];
582 setup
->fs
.current
.jit_context
.stencil_ref_back
= refs
[1];
583 setup
->dirty
|= LP_SETUP_NEW_FS
;
588 lp_setup_set_blend_color( struct lp_setup_context
*setup
,
589 const struct pipe_blend_color
*blend_color
)
591 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
595 if(memcmp(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
) != 0) {
596 memcpy(&setup
->blend_color
.current
, blend_color
, sizeof *blend_color
);
597 setup
->dirty
|= LP_SETUP_NEW_BLEND_COLOR
;
603 lp_setup_set_scissors( struct lp_setup_context
*setup
,
604 const struct pipe_scissor_state
*scissors
)
607 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
611 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
612 setup
->scissors
[i
].x0
= scissors
[i
].minx
;
613 setup
->scissors
[i
].x1
= scissors
[i
].maxx
-1;
614 setup
->scissors
[i
].y0
= scissors
[i
].miny
;
615 setup
->scissors
[i
].y1
= scissors
[i
].maxy
-1;
617 setup
->dirty
|= LP_SETUP_NEW_SCISSOR
;
622 lp_setup_set_flatshade_first( struct lp_setup_context
*setup
,
623 boolean flatshade_first
)
625 setup
->flatshade_first
= flatshade_first
;
629 lp_setup_set_rasterizer_discard( struct lp_setup_context
*setup
,
630 boolean rasterizer_discard
)
632 if (setup
->rasterizer_discard
!= rasterizer_discard
) {
633 setup
->rasterizer_discard
= rasterizer_discard
;
634 set_scene_state( setup
, SETUP_FLUSHED
, __FUNCTION__
);
639 lp_setup_set_vertex_info( struct lp_setup_context
*setup
,
640 struct vertex_info
*vertex_info
)
642 /* XXX: just silently holding onto the pointer:
644 setup
->vertex_info
= vertex_info
;
649 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
652 lp_setup_set_fragment_sampler_views(struct lp_setup_context
*setup
,
654 struct pipe_sampler_view
**views
)
658 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
660 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
662 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
663 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
666 struct pipe_resource
*res
= view
->texture
;
667 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
668 struct lp_jit_texture
*jit_tex
;
669 jit_tex
= &setup
->fs
.current
.jit_context
.textures
[i
];
671 /* We're referencing the texture's internal data, so save a
674 pipe_resource_reference(&setup
->fs
.current_tex
[i
], res
);
677 /* regular texture - setup array of mipmap level offsets */
680 unsigned first_level
= 0;
681 unsigned last_level
= 0;
683 if (llvmpipe_resource_is_texture(res
)) {
684 first_level
= view
->u
.tex
.first_level
;
685 last_level
= view
->u
.tex
.last_level
;
686 assert(first_level
<= last_level
);
687 assert(last_level
<= res
->last_level
);
690 * The complexity here should no longer be necessary.
692 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, first_level
,
694 jit_tex
->base
= lp_tex
->linear_img
.data
;
697 mip_ptr
= lp_tex
->data
;
698 jit_tex
->base
= mip_ptr
;
701 if ((LP_PERF
& PERF_TEX_MEM
) || !mip_ptr
) {
702 /* out of memory - use dummy tile memory */
703 /* Note if using PERF_TEX_MEM will also skip tile conversion */
704 jit_tex
->base
= lp_dummy_tile
;
705 jit_tex
->width
= TILE_SIZE
/8;
706 jit_tex
->height
= TILE_SIZE
/8;
708 jit_tex
->first_level
= 0;
709 jit_tex
->last_level
= 0;
710 jit_tex
->mip_offsets
[0] = 0;
711 jit_tex
->row_stride
[0] = 0;
712 jit_tex
->img_stride
[0] = 0;
715 jit_tex
->width
= res
->width0
;
716 jit_tex
->height
= res
->height0
;
717 jit_tex
->depth
= res
->depth0
;
718 jit_tex
->first_level
= first_level
;
719 jit_tex
->last_level
= last_level
;
721 if (llvmpipe_resource_is_texture(res
)) {
722 for (j
= first_level
; j
<= last_level
; j
++) {
723 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
725 jit_tex
->mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)jit_tex
->base
;
727 * could get mip offset directly but need call above to
728 * invoke tiled->linear conversion.
730 assert(lp_tex
->linear_mip_offsets
[j
] == jit_tex
->mip_offsets
[j
]);
731 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
732 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
735 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
736 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
738 * For array textures, we don't have first_layer, instead
739 * adjust last_layer (stored as depth) plus the mip level offsets
740 * (as we have mip-first layout can't just adjust base ptr).
741 * XXX For mip levels, could do something similar.
743 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
744 for (j
= first_level
; j
<= last_level
; j
++) {
745 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
746 lp_tex
->img_stride
[j
];
748 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
749 assert(view
->u
.tex
.last_layer
< res
->array_size
);
754 * For buffers, we don't have first_element, instead adjust
755 * last_element (stored as width) plus the base pointer.
757 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
758 /* probably don't really need to fill that out */
759 jit_tex
->mip_offsets
[0] = 0;
760 jit_tex
->row_stride
[0] = 0;
761 jit_tex
->row_stride
[0] = 0;
763 /* everything specified in number of elements here. */
764 jit_tex
->width
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
765 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.first_element
*
767 /* XXX Unsure if we need to sanitize parameters? */
768 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
769 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
774 /* display target texture/surface */
776 * XXX: Where should this be unmapped?
778 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
779 struct sw_winsys
*winsys
= screen
->winsys
;
780 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
782 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
783 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
784 jit_tex
->mip_offsets
[0] = 0;
785 jit_tex
->width
= res
->width0
;
786 jit_tex
->height
= res
->height0
;
787 jit_tex
->depth
= res
->depth0
;
788 jit_tex
->first_level
= jit_tex
->last_level
= 0;
789 assert(jit_tex
->base
);
794 setup
->dirty
|= LP_SETUP_NEW_FS
;
799 * Called during state validation when LP_NEW_SAMPLER is set.
802 lp_setup_set_fragment_sampler_state(struct lp_setup_context
*setup
,
804 struct pipe_sampler_state
**samplers
)
808 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
810 assert(num
<= PIPE_MAX_SAMPLERS
);
812 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
813 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
816 struct lp_jit_sampler
*jit_sam
;
817 jit_sam
= &setup
->fs
.current
.jit_context
.samplers
[i
];
819 jit_sam
->min_lod
= sampler
->min_lod
;
820 jit_sam
->max_lod
= sampler
->max_lod
;
821 jit_sam
->lod_bias
= sampler
->lod_bias
;
822 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
826 setup
->dirty
|= LP_SETUP_NEW_FS
;
831 * Is the given texture referenced by any scene?
832 * Note: we have to check all scenes including any scenes currently
833 * being rendered and the current scene being built.
836 lp_setup_is_resource_referenced( const struct lp_setup_context
*setup
,
837 const struct pipe_resource
*texture
)
841 /* check the render targets */
842 for (i
= 0; i
< setup
->fb
.nr_cbufs
; i
++) {
843 if (setup
->fb
.cbufs
[i
]->texture
== texture
)
844 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
846 if (setup
->fb
.zsbuf
&& setup
->fb
.zsbuf
->texture
== texture
) {
847 return LP_REFERENCED_FOR_READ
| LP_REFERENCED_FOR_WRITE
;
850 /* check textures referenced by the scene */
851 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
852 if (lp_scene_is_resource_referenced(setup
->scenes
[i
], texture
)) {
853 return LP_REFERENCED_FOR_READ
;
857 return LP_UNREFERENCED
;
862 * Called by vbuf code when we're about to draw something.
865 try_update_scene_state( struct lp_setup_context
*setup
)
867 boolean new_scene
= (setup
->fs
.stored
== NULL
);
868 struct lp_scene
*scene
= setup
->scene
;
873 if(setup
->dirty
& LP_SETUP_NEW_BLEND_COLOR
) {
879 /* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */
880 size
= 4 * 16 * sizeof(uint8_t);
881 size
+= (LP_MAX_VECTOR_LENGTH
/ 4) * sizeof(float);
882 stored
= lp_scene_alloc_aligned(scene
, size
, LP_MAX_VECTOR_LENGTH
);
889 /* Store floating point colour */
890 fstored
= (float*)(stored
+ 4*16);
891 for (i
= 0; i
< (LP_MAX_VECTOR_LENGTH
/ 4); ++i
) {
892 fstored
[i
] = setup
->blend_color
.current
.color
[i
% 4];
895 /* smear each blend color component across 16 ubyte elements */
896 for (i
= 0; i
< 4; ++i
) {
897 uint8_t c
= float_to_ubyte(setup
->blend_color
.current
.color
[i
]);
898 for (j
= 0; j
< 16; ++j
)
899 stored
[i
*16 + j
] = c
;
902 setup
->blend_color
.stored
= stored
;
903 setup
->fs
.current
.jit_context
.u8_blend_color
= stored
;
904 setup
->fs
.current
.jit_context
.f_blend_color
= fstored
;
905 setup
->dirty
|= LP_SETUP_NEW_FS
;
908 if (setup
->dirty
& LP_SETUP_NEW_CONSTANTS
) {
909 for (i
= 0; i
< Elements(setup
->constants
); ++i
) {
910 struct pipe_resource
*buffer
= setup
->constants
[i
].current
.buffer
;
911 const unsigned current_size
= setup
->constants
[i
].current
.buffer_size
;
912 const ubyte
*current_data
= NULL
;
915 /* resource buffer */
916 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
918 else if (setup
->constants
[i
].current
.user_buffer
) {
919 /* user-space buffer */
920 current_data
= (ubyte
*) setup
->constants
[i
].current
.user_buffer
;
924 current_data
+= setup
->constants
[i
].current
.buffer_offset
;
926 /* TODO: copy only the actually used constants? */
928 if (setup
->constants
[i
].stored_size
!= current_size
||
929 !setup
->constants
[i
].stored_data
||
930 memcmp(setup
->constants
[i
].stored_data
,
932 current_size
) != 0) {
935 stored
= lp_scene_alloc(scene
, current_size
);
944 setup
->constants
[i
].stored_size
= current_size
;
945 setup
->constants
[i
].stored_data
= stored
;
949 setup
->constants
[i
].stored_size
= 0;
950 setup
->constants
[i
].stored_data
= NULL
;
953 setup
->fs
.current
.jit_context
.constants
[i
] = setup
->constants
[i
].stored_data
;
954 setup
->dirty
|= LP_SETUP_NEW_FS
;
959 if (setup
->dirty
& LP_SETUP_NEW_FS
) {
960 if (!setup
->fs
.stored
||
961 memcmp(setup
->fs
.stored
,
963 sizeof setup
->fs
.current
) != 0)
965 struct lp_rast_state
*stored
;
967 /* The fs state that's been stored in the scene is different from
968 * the new, current state. So allocate a new lp_rast_state object
969 * and append it to the bin's setup data buffer.
971 stored
= (struct lp_rast_state
*) lp_scene_alloc(scene
, sizeof *stored
);
979 sizeof setup
->fs
.current
);
980 setup
->fs
.stored
= stored
;
982 /* The scene now references the textures in the rasterization
983 * state record. Note that now.
985 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
986 if (setup
->fs
.current_tex
[i
]) {
987 if (!lp_scene_add_resource_reference(scene
,
988 setup
->fs
.current_tex
[i
],
998 if (setup
->dirty
& LP_SETUP_NEW_SCISSOR
) {
1000 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; ++i
) {
1001 setup
->draw_regions
[i
] = setup
->framebuffer
;
1002 if (setup
->scissor_test
) {
1003 u_rect_possible_intersection(&setup
->scissors
[i
],
1004 &setup
->draw_regions
[i
]);
1007 /* If the framebuffer is large we have to think about fixed-point
1008 * integer overflow. For 2K by 2K images, coordinates need 15 bits
1009 * (2^11 + 4 subpixel bits). The product of two such numbers would
1010 * use 30 bits. Any larger and we could overflow a 32-bit int.
1012 * To cope with this problem we check if triangles are large and
1013 * subdivide them if needed.
1015 setup
->subdivide_large_triangles
= (setup
->fb
.width
> 2048 &&
1016 setup
->fb
.height
> 2048);
1021 assert(setup
->fs
.stored
);
1026 lp_setup_update_state( struct lp_setup_context
*setup
,
1027 boolean update_scene
)
1029 /* Some of the 'draw' pipeline stages may have changed some driver state.
1030 * Make sure we've processed those state changes before anything else.
1032 * XXX this is the only place where llvmpipe_context is used in the
1033 * setup code. This may get refactored/changed...
1036 struct llvmpipe_context
*lp
= llvmpipe_context(setup
->pipe
);
1038 llvmpipe_update_derived(lp
);
1041 if (lp
->setup
->dirty
) {
1042 llvmpipe_update_setup(lp
);
1045 assert(setup
->setup
.variant
);
1047 /* Will probably need to move this somewhere else, just need
1048 * to know about vertex shader point size attribute.
1050 setup
->psize
= lp
->psize_slot
;
1051 setup
->viewport_index_slot
= lp
->viewport_index_slot
;
1052 setup
->layer_slot
= lp
->layer_slot
;
1054 assert(lp
->dirty
== 0);
1056 assert(lp
->setup_variant
.key
.size
==
1057 setup
->setup
.variant
->key
.size
);
1059 assert(memcmp(&lp
->setup_variant
.key
,
1060 &setup
->setup
.variant
->key
,
1061 setup
->setup
.variant
->key
.size
) == 0);
1064 if (update_scene
&& setup
->state
!= SETUP_ACTIVE
) {
1065 if (!set_scene_state( setup
, SETUP_ACTIVE
, __FUNCTION__
))
1069 /* Only call into update_scene_state() if we already have a
1072 if (update_scene
&& setup
->scene
) {
1073 assert(setup
->state
== SETUP_ACTIVE
);
1075 if (try_update_scene_state(setup
))
1078 /* Update failed, try to restart the scene.
1080 * Cannot call lp_setup_flush_and_restart() directly here
1081 * because of potential recursion.
1083 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1086 if (!set_scene_state(setup
, SETUP_ACTIVE
, __FUNCTION__
))
1092 return try_update_scene_state(setup
);
1100 /* Only caller is lp_setup_vbuf_destroy()
1103 lp_setup_destroy( struct lp_setup_context
*setup
)
1107 lp_setup_reset( setup
);
1109 util_unreference_framebuffer_state(&setup
->fb
);
1111 for (i
= 0; i
< Elements(setup
->fs
.current_tex
); i
++) {
1112 pipe_resource_reference(&setup
->fs
.current_tex
[i
], NULL
);
1115 for (i
= 0; i
< Elements(setup
->constants
); i
++) {
1116 pipe_resource_reference(&setup
->constants
[i
].current
.buffer
, NULL
);
1119 /* free the scenes in the 'empty' queue */
1120 for (i
= 0; i
< Elements(setup
->scenes
); i
++) {
1121 struct lp_scene
*scene
= setup
->scenes
[i
];
1124 lp_fence_wait(scene
->fence
);
1126 lp_scene_destroy(scene
);
1129 lp_fence_reference(&setup
->last_fence
, NULL
);
1136 * Create a new primitive tiling engine. Plug it into the backend of
1137 * the draw module. Currently also creates a rasterizer to use with
1140 struct lp_setup_context
*
1141 lp_setup_create( struct pipe_context
*pipe
,
1142 struct draw_context
*draw
)
1144 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1145 struct lp_setup_context
*setup
;
1148 setup
= CALLOC_STRUCT(lp_setup_context
);
1153 lp_setup_init_vbuf(setup
);
1155 /* Used only in update_state():
1160 setup
->num_threads
= screen
->num_threads
;
1161 setup
->vbuf
= draw_vbuf_stage(draw
, &setup
->base
);
1166 draw_set_rasterize_stage(draw
, setup
->vbuf
);
1167 draw_set_render(draw
, &setup
->base
);
1169 /* create some empty scenes */
1170 for (i
= 0; i
< MAX_SCENES
; i
++) {
1171 setup
->scenes
[i
] = lp_scene_create( pipe
);
1172 if (!setup
->scenes
[i
]) {
1177 setup
->triangle
= first_triangle
;
1178 setup
->line
= first_line
;
1179 setup
->point
= first_point
;
1186 for (i
= 0; i
< MAX_SCENES
; i
++) {
1187 if (setup
->scenes
[i
]) {
1188 lp_scene_destroy(setup
->scenes
[i
]);
1192 setup
->vbuf
->destroy(setup
->vbuf
);
1201 * Put a BeginQuery command into all bins.
1204 lp_setup_begin_query(struct lp_setup_context
*setup
,
1205 struct llvmpipe_query
*pq
)
1208 set_scene_state(setup
, SETUP_ACTIVE
, "begin_query");
1210 if (!(pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1211 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1212 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
))
1215 /* init the query to its beginning state */
1216 assert(setup
->active_binned_queries
< LP_MAX_ACTIVE_BINNED_QUERIES
);
1217 /* exceeding list size so just ignore the query */
1218 if (setup
->active_binned_queries
>= LP_MAX_ACTIVE_BINNED_QUERIES
) {
1221 assert(setup
->active_queries
[setup
->active_binned_queries
] == NULL
);
1222 setup
->active_queries
[setup
->active_binned_queries
] = pq
;
1223 setup
->active_binned_queries
++;
1225 assert(setup
->scene
);
1227 if (!lp_scene_bin_everywhere(setup
->scene
,
1228 LP_RAST_OP_BEGIN_QUERY
,
1229 lp_rast_arg_query(pq
))) {
1231 if (!lp_setup_flush_and_restart(setup
))
1234 if (!lp_scene_bin_everywhere(setup
->scene
,
1235 LP_RAST_OP_BEGIN_QUERY
,
1236 lp_rast_arg_query(pq
))) {
1245 * Put an EndQuery command into all bins.
1248 lp_setup_end_query(struct lp_setup_context
*setup
, struct llvmpipe_query
*pq
)
1250 set_scene_state(setup
, SETUP_ACTIVE
, "end_query");
1252 assert(setup
->scene
);
1254 /* pq->fence should be the fence of the *last* scene which
1255 * contributed to the query result.
1257 lp_fence_reference(&pq
->fence
, setup
->scene
->fence
);
1259 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1260 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1261 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
||
1262 pq
->type
== PIPE_QUERY_TIMESTAMP
) {
1263 if (!lp_scene_bin_everywhere(setup
->scene
,
1264 LP_RAST_OP_END_QUERY
,
1265 lp_rast_arg_query(pq
))) {
1266 if (!lp_setup_flush_and_restart(setup
))
1269 if (!lp_scene_bin_everywhere(setup
->scene
,
1270 LP_RAST_OP_END_QUERY
,
1271 lp_rast_arg_query(pq
))) {
1278 lp_fence_reference(&pq
->fence
, setup
->last_fence
);
1282 /* Need to do this now not earlier since it still needs to be marked as
1283 * active when binning it would cause a flush.
1285 if (pq
->type
== PIPE_QUERY_OCCLUSION_COUNTER
||
1286 pq
->type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1287 pq
->type
== PIPE_QUERY_PIPELINE_STATISTICS
) {
1290 /* remove from active binned query list */
1291 for (i
= 0; i
< setup
->active_binned_queries
; i
++) {
1292 if (setup
->active_queries
[i
] == pq
)
1295 assert(i
< setup
->active_binned_queries
);
1296 if (i
== setup
->active_binned_queries
)
1298 setup
->active_binned_queries
--;
1299 setup
->active_queries
[i
] = setup
->active_queries
[setup
->active_binned_queries
];
1300 setup
->active_queries
[setup
->active_binned_queries
] = NULL
;
1306 lp_setup_flush_and_restart(struct lp_setup_context
*setup
)
1308 if (0) debug_printf("%s\n", __FUNCTION__
);
1310 assert(setup
->state
== SETUP_ACTIVE
);
1312 if (!set_scene_state(setup
, SETUP_FLUSHED
, __FUNCTION__
))
1315 if (!lp_setup_update_state(setup
, TRUE
))