llvmpipe: use opcodes instead of function pointers in bins
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * Tiling engine.
30 *
31 * Builds per-tile display lists and executes them on calls to
32 * lp_setup_flush().
33 */
34
35 #include <limits.h>
36
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 "lp_context.h"
43 #include "lp_memory.h"
44 #include "lp_scene.h"
45 #include "lp_texture.h"
46 #include "lp_debug.h"
47 #include "lp_fence.h"
48 #include "lp_query.h"
49 #include "lp_rast.h"
50 #include "lp_setup_context.h"
51 #include "lp_screen.h"
52 #include "lp_state.h"
53 #include "state_tracker/sw_winsys.h"
54
55 #include "draw/draw_context.h"
56 #include "draw/draw_vbuf.h"
57
58
59 static void set_scene_state( struct lp_setup_context *, enum setup_state,
60 const char *reason);
61 static boolean try_update_scene_state( struct lp_setup_context *setup );
62
63
64 static void
65 lp_setup_get_empty_scene(struct lp_setup_context *setup)
66 {
67 assert(setup->scene == NULL);
68
69 setup->scene_idx++;
70 setup->scene_idx %= Elements(setup->scenes);
71
72 setup->scene = setup->scenes[setup->scene_idx];
73
74 if (setup->scene->fence) {
75 if (LP_DEBUG & DEBUG_SETUP)
76 debug_printf("%s: wait for scene %d\n",
77 __FUNCTION__, setup->scene->fence->id);
78
79 lp_fence_wait(setup->scene->fence);
80 }
81
82 lp_scene_begin_binning(setup->scene, &setup->fb);
83
84 }
85
86
87 static void
88 first_triangle( struct lp_setup_context *setup,
89 const float (*v0)[4],
90 const float (*v1)[4],
91 const float (*v2)[4])
92 {
93 assert(setup->state == SETUP_ACTIVE);
94 lp_setup_choose_triangle( setup );
95 setup->triangle( setup, v0, v1, v2 );
96 }
97
98 static void
99 first_line( struct lp_setup_context *setup,
100 const float (*v0)[4],
101 const float (*v1)[4])
102 {
103 assert(setup->state == SETUP_ACTIVE);
104 lp_setup_choose_line( setup );
105 setup->line( setup, v0, v1 );
106 }
107
108 static void
109 first_point( struct lp_setup_context *setup,
110 const float (*v0)[4])
111 {
112 assert(setup->state == SETUP_ACTIVE);
113 lp_setup_choose_point( setup );
114 setup->point( setup, v0 );
115 }
116
117 static void lp_setup_reset( struct lp_setup_context *setup )
118 {
119 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
120
121 /* Reset derived state */
122 setup->constants.stored_size = 0;
123 setup->constants.stored_data = NULL;
124 setup->fs.stored = NULL;
125 setup->dirty = ~0;
126
127 /* no current bin */
128 setup->scene = NULL;
129
130 /* Reset some state:
131 */
132 memset(&setup->clear, 0, sizeof setup->clear);
133
134 /* Have an explicit "start-binning" call and get rid of this
135 * pointer twiddling?
136 */
137 setup->line = first_line;
138 setup->point = first_point;
139 setup->triangle = first_triangle;
140 }
141
142
143 /** Rasterize all scene's bins */
144 static void
145 lp_setup_rasterize_scene( struct lp_setup_context *setup )
146 {
147 struct lp_scene *scene = setup->scene;
148 struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);
149
150 lp_scene_end_binning(scene);
151
152 lp_fence_reference(&setup->last_fence, scene->fence);
153
154 if (setup->last_fence)
155 setup->last_fence->issued = TRUE;
156
157 pipe_mutex_lock(screen->rast_mutex);
158 lp_rast_queue_scene(screen->rast, scene);
159 lp_rast_finish(screen->rast);
160 pipe_mutex_unlock(screen->rast_mutex);
161
162 lp_scene_end_rasterization(setup->scene);
163 lp_setup_reset( setup );
164
165 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
166 }
167
168
169
170 static void
171 begin_binning( struct lp_setup_context *setup )
172 {
173 struct lp_scene *scene = setup->scene;
174 boolean need_zsload = FALSE;
175 boolean ok;
176 unsigned i, j;
177
178 assert(scene);
179 assert(scene->fence == NULL);
180
181 /* Always create a fence:
182 */
183 scene->fence = lp_fence_create(MAX2(1, setup->num_threads));
184
185 /* Initialize the bin flags and x/y coords:
186 */
187 for (i = 0; i < scene->tiles_x; i++) {
188 for (j = 0; j < scene->tiles_y; j++) {
189 scene->tile[i][j].x = i;
190 scene->tile[i][j].y = j;
191 }
192 }
193
194 ok = try_update_scene_state(setup);
195 assert(ok);
196
197 if (setup->fb.zsbuf &&
198 ((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
199 util_format_is_depth_and_stencil(setup->fb.zsbuf->format))
200 need_zsload = TRUE;
201
202 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
203 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
204 need_zsload ? "clear": "load");
205
206 if (setup->fb.nr_cbufs) {
207 if (setup->clear.flags & PIPE_CLEAR_COLOR) {
208 ok = lp_scene_bin_everywhere( scene,
209 LP_RAST_OP_CLEAR_COLOR,
210 setup->clear.color );
211 assert(ok);
212 }
213 }
214
215 if (setup->fb.zsbuf) {
216 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {
217 if (!need_zsload)
218 scene->has_depthstencil_clear = TRUE;
219 ok = lp_scene_bin_everywhere( scene,
220 LP_RAST_OP_CLEAR_ZSTENCIL,
221 lp_rast_arg_clearzs(
222 setup->clear.zsvalue,
223 setup->clear.zsmask));
224 assert(ok);
225 }
226 }
227
228 if (setup->active_query) {
229 ok = lp_scene_bin_everywhere( scene,
230 LP_RAST_OP_BEGIN_QUERY,
231 lp_rast_arg_query(setup->active_query) );
232 assert(ok);
233 }
234
235
236 setup->clear.flags = 0;
237 setup->clear.zsmask = 0;
238 setup->clear.zsvalue = 0;
239
240 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
241 }
242
243
244 /* This basically bins and then flushes any outstanding full-screen
245 * clears.
246 *
247 * TODO: fast path for fullscreen clears and no triangles.
248 */
249 static void
250 execute_clears( struct lp_setup_context *setup )
251 {
252 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
253
254 begin_binning( setup );
255 }
256
257 const char *states[] = {
258 "FLUSHED",
259 "EMPTY ",
260 "CLEARED",
261 "ACTIVE "
262 };
263
264
265 static void
266 set_scene_state( struct lp_setup_context *setup,
267 enum setup_state new_state,
268 const char *reason)
269 {
270 unsigned old_state = setup->state;
271
272 if (old_state == new_state)
273 return;
274
275 if (LP_DEBUG & DEBUG_SETUP) {
276 debug_printf("%s old %s new %s%s%s\n",
277 __FUNCTION__,
278 states[old_state],
279 states[new_state],
280 (new_state == SETUP_FLUSHED) ? ": " : "",
281 (new_state == SETUP_FLUSHED) ? reason : "");
282 }
283
284 /* wait for a free/empty scene
285 */
286 if (old_state == SETUP_FLUSHED)
287 lp_setup_get_empty_scene(setup);
288
289 switch (new_state) {
290 case SETUP_CLEARED:
291 break;
292
293 case SETUP_ACTIVE:
294 begin_binning( setup );
295 break;
296
297 case SETUP_FLUSHED:
298 if (old_state == SETUP_CLEARED)
299 execute_clears( setup );
300
301 lp_setup_rasterize_scene( setup );
302 assert(setup->scene == NULL);
303 break;
304
305 default:
306 assert(0 && "invalid setup state mode");
307 }
308
309 setup->state = new_state;
310 }
311
312
313 /**
314 * \param flags bitmask of PIPE_FLUSH_x flags
315 */
316 void
317 lp_setup_flush( struct lp_setup_context *setup,
318 unsigned flags,
319 struct pipe_fence_handle **fence,
320 const char *reason)
321 {
322 set_scene_state( setup, SETUP_FLUSHED, reason );
323
324 if (fence) {
325 lp_fence_reference((struct lp_fence **)fence, setup->last_fence);
326 }
327 }
328
329
330 void
331 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
332 const struct pipe_framebuffer_state *fb )
333 {
334 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
335
336 /* Flush any old scene.
337 */
338 set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );
339
340 /*
341 * Ensure the old scene is not reused.
342 */
343 assert(!setup->scene);
344
345 /* Set new state. This will be picked up later when we next need a
346 * scene.
347 */
348 util_copy_framebuffer_state(&setup->fb, fb);
349 setup->framebuffer.x0 = 0;
350 setup->framebuffer.y0 = 0;
351 setup->framebuffer.x1 = fb->width-1;
352 setup->framebuffer.y1 = fb->height-1;
353 setup->dirty |= LP_SETUP_NEW_SCISSOR;
354 }
355
356
357 static boolean
358 lp_setup_try_clear( struct lp_setup_context *setup,
359 const float *color,
360 double depth,
361 unsigned stencil,
362 unsigned flags )
363 {
364 uint32_t zsmask = 0;
365 uint32_t zsvalue = 0;
366 union lp_rast_cmd_arg color_arg;
367 unsigned i;
368
369 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
370
371 if (flags & PIPE_CLEAR_COLOR) {
372 for (i = 0; i < 4; i++)
373 color_arg.clear_color[i] = float_to_ubyte(color[i]);
374 }
375
376 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
377 unsigned zmask = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;
378 unsigned smask = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;
379
380 zsvalue = util_pack_z_stencil(setup->fb.zsbuf->format,
381 depth,
382 stencil);
383
384 zsmask = util_pack_uint_z_stencil(setup->fb.zsbuf->format,
385 zmask,
386 smask);
387 }
388
389 if (setup->state == SETUP_ACTIVE) {
390 struct lp_scene *scene = setup->scene;
391
392 /* Add the clear to existing scene. In the unusual case where
393 * both color and depth-stencil are being cleared when there's
394 * already been some rendering, we could discard the currently
395 * binned scene and start again, but I don't see that as being
396 * a common usage.
397 */
398 if (flags & PIPE_CLEAR_COLOR) {
399 if (!lp_scene_bin_everywhere( scene,
400 LP_RAST_OP_CLEAR_COLOR,
401 color_arg ))
402 return FALSE;
403 }
404
405 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
406 if (!lp_scene_bin_everywhere( scene,
407 LP_RAST_OP_CLEAR_ZSTENCIL,
408 lp_rast_arg_clearzs(zsvalue, zsmask) ))
409 return FALSE;
410 }
411 }
412 else {
413 /* Put ourselves into the 'pre-clear' state, specifically to try
414 * and accumulate multiple clears to color and depth_stencil
415 * buffers which the app or state-tracker might issue
416 * separately.
417 */
418 set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );
419
420 setup->clear.flags |= flags;
421
422 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
423 setup->clear.zsmask |= zsmask;
424 setup->clear.zsvalue =
425 (setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);
426 }
427
428 if (flags & PIPE_CLEAR_COLOR) {
429 memcpy(setup->clear.color.clear_color,
430 &color_arg,
431 sizeof color_arg);
432 }
433 }
434
435 return TRUE;
436 }
437
438 void
439 lp_setup_clear( struct lp_setup_context *setup,
440 const float *color,
441 double depth,
442 unsigned stencil,
443 unsigned flags )
444 {
445 if (!lp_setup_try_clear( setup, color, depth, stencil, flags )) {
446 lp_setup_flush(setup, 0, NULL, __FUNCTION__);
447
448 if (!lp_setup_try_clear( setup, color, depth, stencil, flags ))
449 assert(0);
450 }
451 }
452
453
454
455
456
457 void
458 lp_setup_set_triangle_state( struct lp_setup_context *setup,
459 unsigned cull_mode,
460 boolean ccw_is_frontface,
461 boolean scissor,
462 boolean gl_rasterization_rules)
463 {
464 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
465
466 setup->ccw_is_frontface = ccw_is_frontface;
467 setup->cullmode = cull_mode;
468 setup->triangle = first_triangle;
469 setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
470
471 if (setup->scissor_test != scissor) {
472 setup->dirty |= LP_SETUP_NEW_SCISSOR;
473 setup->scissor_test = scissor;
474 }
475 }
476
477 void
478 lp_setup_set_line_state( struct lp_setup_context *setup,
479 float line_width)
480 {
481 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
482
483 setup->line_width = line_width;
484 }
485
486 void
487 lp_setup_set_point_state( struct lp_setup_context *setup,
488 float point_size,
489 boolean point_size_per_vertex,
490 uint sprite)
491 {
492 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
493
494 setup->point_size = point_size;
495 setup->sprite = sprite;
496 setup->point_size_per_vertex = point_size_per_vertex;
497 }
498
499 void
500 lp_setup_set_fs_inputs( struct lp_setup_context *setup,
501 const struct lp_shader_input *input,
502 unsigned nr )
503 {
504 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
505
506 memcpy( setup->fs.input, input, nr * sizeof input[0] );
507 setup->fs.nr_inputs = nr;
508 }
509
510 void
511 lp_setup_set_fs_variant( struct lp_setup_context *setup,
512 struct lp_fragment_shader_variant *variant)
513 {
514 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,
515 variant);
516 /* FIXME: reference count */
517
518 setup->fs.current.variant = variant;
519 setup->dirty |= LP_SETUP_NEW_FS;
520 }
521
522 void
523 lp_setup_set_fs_constants(struct lp_setup_context *setup,
524 struct pipe_resource *buffer)
525 {
526 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
527
528 pipe_resource_reference(&setup->constants.current, buffer);
529
530 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
531 }
532
533
534 void
535 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
536 float alpha_ref_value )
537 {
538 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
539
540 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
541 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
542 setup->dirty |= LP_SETUP_NEW_FS;
543 }
544 }
545
546 void
547 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
548 const ubyte refs[2] )
549 {
550 LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
551
552 if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
553 setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
554 setup->fs.current.jit_context.stencil_ref_front = refs[0];
555 setup->fs.current.jit_context.stencil_ref_back = refs[1];
556 setup->dirty |= LP_SETUP_NEW_FS;
557 }
558 }
559
560 void
561 lp_setup_set_blend_color( struct lp_setup_context *setup,
562 const struct pipe_blend_color *blend_color )
563 {
564 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
565
566 assert(blend_color);
567
568 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
569 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
570 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
571 }
572 }
573
574
575 void
576 lp_setup_set_scissor( struct lp_setup_context *setup,
577 const struct pipe_scissor_state *scissor )
578 {
579 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
580
581 assert(scissor);
582
583 setup->scissor.x0 = scissor->minx;
584 setup->scissor.x1 = scissor->maxx-1;
585 setup->scissor.y0 = scissor->miny;
586 setup->scissor.y1 = scissor->maxy-1;
587 setup->dirty |= LP_SETUP_NEW_SCISSOR;
588 }
589
590
591 void
592 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
593 boolean flatshade_first )
594 {
595 setup->flatshade_first = flatshade_first;
596 }
597
598
599 void
600 lp_setup_set_vertex_info( struct lp_setup_context *setup,
601 struct vertex_info *vertex_info )
602 {
603 /* XXX: just silently holding onto the pointer:
604 */
605 setup->vertex_info = vertex_info;
606 }
607
608
609 /**
610 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
611 */
612 void
613 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
614 unsigned num,
615 struct pipe_sampler_view **views)
616 {
617 unsigned i;
618
619 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
620
621 assert(num <= PIPE_MAX_SAMPLERS);
622
623 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
624 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
625
626 if(view) {
627 struct pipe_resource *tex = view->texture;
628 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
629 struct lp_jit_texture *jit_tex;
630 jit_tex = &setup->fs.current.jit_context.textures[i];
631 jit_tex->width = tex->width0;
632 jit_tex->height = tex->height0;
633 jit_tex->depth = tex->depth0;
634 jit_tex->last_level = tex->last_level;
635
636 /* We're referencing the texture's internal data, so save a
637 * reference to it.
638 */
639 pipe_resource_reference(&setup->fs.current_tex[i], tex);
640
641 if (!lp_tex->dt) {
642 /* regular texture - setup array of mipmap level pointers */
643 int j;
644 for (j = 0; j <= tex->last_level; j++) {
645 jit_tex->data[j] =
646 llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
647 LP_TEX_LAYOUT_LINEAR);
648 jit_tex->row_stride[j] = lp_tex->row_stride[j];
649 jit_tex->img_stride[j] = lp_tex->img_stride[j];
650
651 if (!jit_tex->data[j]) {
652 /* out of memory - use dummy tile memory */
653 jit_tex->data[j] = lp_dummy_tile;
654 jit_tex->width = TILE_SIZE;
655 jit_tex->height = TILE_SIZE;
656 jit_tex->depth = 1;
657 jit_tex->last_level = 0;
658 jit_tex->row_stride[j] = 0;
659 jit_tex->img_stride[j] = 0;
660 }
661 }
662 }
663 else {
664 /* display target texture/surface */
665 /*
666 * XXX: Where should this be unmapped?
667 */
668 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
669 struct sw_winsys *winsys = screen->winsys;
670 jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
671 PIPE_TRANSFER_READ);
672 jit_tex->row_stride[0] = lp_tex->row_stride[0];
673 jit_tex->img_stride[0] = lp_tex->img_stride[0];
674 assert(jit_tex->data[0]);
675 }
676 }
677 }
678
679 setup->dirty |= LP_SETUP_NEW_FS;
680 }
681
682
683 /**
684 * Is the given texture referenced by any scene?
685 * Note: we have to check all scenes including any scenes currently
686 * being rendered and the current scene being built.
687 */
688 unsigned
689 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
690 const struct pipe_resource *texture )
691 {
692 unsigned i;
693
694 /* check the render targets */
695 for (i = 0; i < setup->fb.nr_cbufs; i++) {
696 if (setup->fb.cbufs[i]->texture == texture)
697 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
698 }
699 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
700 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
701 }
702
703 /* check textures referenced by the scene */
704 for (i = 0; i < Elements(setup->scenes); i++) {
705 if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
706 return PIPE_REFERENCED_FOR_READ;
707 }
708 }
709
710 return PIPE_UNREFERENCED;
711 }
712
713
714 /**
715 * Called by vbuf code when we're about to draw something.
716 */
717 static boolean
718 try_update_scene_state( struct lp_setup_context *setup )
719 {
720 boolean new_scene = (setup->fs.stored == NULL);
721 struct lp_scene *scene = setup->scene;
722
723 assert(scene);
724
725 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
726 uint8_t *stored;
727 unsigned i, j;
728
729 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
730 if (!stored) {
731 assert(!new_scene);
732 return FALSE;
733 }
734
735 /* smear each blend color component across 16 ubyte elements */
736 for (i = 0; i < 4; ++i) {
737 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
738 for (j = 0; j < 16; ++j)
739 stored[i*16 + j] = c;
740 }
741
742 setup->blend_color.stored = stored;
743 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
744 setup->dirty |= LP_SETUP_NEW_FS;
745 }
746
747 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
748 struct pipe_resource *buffer = setup->constants.current;
749
750 if(buffer) {
751 unsigned current_size = buffer->width0;
752 const void *current_data = llvmpipe_resource_data(buffer);
753
754 /* TODO: copy only the actually used constants? */
755
756 if(setup->constants.stored_size != current_size ||
757 !setup->constants.stored_data ||
758 memcmp(setup->constants.stored_data,
759 current_data,
760 current_size) != 0) {
761 void *stored;
762
763 stored = lp_scene_alloc(scene, current_size);
764 if (!stored) {
765 assert(!new_scene);
766 return FALSE;
767 }
768
769 memcpy(stored,
770 current_data,
771 current_size);
772 setup->constants.stored_size = current_size;
773 setup->constants.stored_data = stored;
774 }
775 }
776 else {
777 setup->constants.stored_size = 0;
778 setup->constants.stored_data = NULL;
779 }
780
781 setup->fs.current.jit_context.constants = setup->constants.stored_data;
782 setup->dirty |= LP_SETUP_NEW_FS;
783 }
784
785
786 if (setup->dirty & LP_SETUP_NEW_FS) {
787 if (!setup->fs.stored ||
788 memcmp(setup->fs.stored,
789 &setup->fs.current,
790 sizeof setup->fs.current) != 0)
791 {
792 struct lp_rast_state *stored;
793 uint i;
794
795 /* The fs state that's been stored in the scene is different from
796 * the new, current state. So allocate a new lp_rast_state object
797 * and append it to the bin's setup data buffer.
798 */
799 stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
800 if (!stored) {
801 assert(!new_scene);
802 return FALSE;
803 }
804
805 memcpy(stored,
806 &setup->fs.current,
807 sizeof setup->fs.current);
808 setup->fs.stored = stored;
809
810 /* The scene now references the textures in the rasterization
811 * state record. Note that now.
812 */
813 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
814 if (setup->fs.current_tex[i]) {
815 if (!lp_scene_add_resource_reference(scene,
816 setup->fs.current_tex[i],
817 new_scene)) {
818 assert(!new_scene);
819 return FALSE;
820 }
821 }
822 }
823 }
824 }
825
826 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
827 setup->draw_region = setup->framebuffer;
828 if (setup->scissor_test) {
829 u_rect_possible_intersection(&setup->scissor,
830 &setup->draw_region);
831 }
832 }
833
834 setup->dirty = 0;
835
836 assert(setup->fs.stored);
837 return TRUE;
838 }
839
840 void
841 lp_setup_update_state( struct lp_setup_context *setup,
842 boolean update_scene )
843 {
844 /* Some of the 'draw' pipeline stages may have changed some driver state.
845 * Make sure we've processed those state changes before anything else.
846 *
847 * XXX this is the only place where llvmpipe_context is used in the
848 * setup code. This may get refactored/changed...
849 */
850 {
851 struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);
852 if (lp->dirty) {
853 llvmpipe_update_derived(lp);
854 }
855
856 /* Will probably need to move this somewhere else, just need
857 * to know about vertex shader point size attribute.
858 */
859 setup->psize = lp->psize_slot;
860
861 assert(lp->dirty == 0);
862 }
863
864 if (update_scene)
865 set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ );
866
867 /* Only call into update_scene_state() if we already have a
868 * scene:
869 */
870 if (update_scene && setup->scene) {
871 assert(setup->state == SETUP_ACTIVE);
872 if (!try_update_scene_state(setup)) {
873 lp_setup_flush_and_restart(setup);
874 if (!try_update_scene_state(setup))
875 assert(0);
876 }
877 }
878 }
879
880
881
882 /* Only caller is lp_setup_vbuf_destroy()
883 */
884 void
885 lp_setup_destroy( struct lp_setup_context *setup )
886 {
887 uint i;
888
889 lp_setup_reset( setup );
890
891 util_unreference_framebuffer_state(&setup->fb);
892
893 for (i = 0; i < Elements(setup->fs.current_tex); i++) {
894 pipe_resource_reference(&setup->fs.current_tex[i], NULL);
895 }
896
897 pipe_resource_reference(&setup->constants.current, NULL);
898
899 /* free the scenes in the 'empty' queue */
900 for (i = 0; i < Elements(setup->scenes); i++) {
901 struct lp_scene *scene = setup->scenes[i];
902
903 if (scene->fence)
904 lp_fence_wait(scene->fence);
905
906 lp_scene_destroy(scene);
907 }
908
909 FREE( setup );
910 }
911
912
913 /**
914 * Create a new primitive tiling engine. Plug it into the backend of
915 * the draw module. Currently also creates a rasterizer to use with
916 * it.
917 */
918 struct lp_setup_context *
919 lp_setup_create( struct pipe_context *pipe,
920 struct draw_context *draw )
921 {
922 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
923 struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
924 unsigned i;
925
926 if (!setup)
927 return NULL;
928
929 lp_setup_init_vbuf(setup);
930
931 /* Used only in update_state():
932 */
933 setup->pipe = pipe;
934
935
936 setup->num_threads = screen->num_threads;
937 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
938 if (!setup->vbuf)
939 goto fail;
940
941 draw_set_rasterize_stage(draw, setup->vbuf);
942 draw_set_render(draw, &setup->base);
943
944 /* create some empty scenes */
945 for (i = 0; i < MAX_SCENES; i++) {
946 setup->scenes[i] = lp_scene_create( pipe );
947 }
948
949 setup->triangle = first_triangle;
950 setup->line = first_line;
951 setup->point = first_point;
952
953 setup->dirty = ~0;
954
955 return setup;
956
957 fail:
958 if (setup->vbuf)
959 ;
960
961 FREE(setup);
962 return NULL;
963 }
964
965
966 /**
967 * Put a BeginQuery command into all bins.
968 */
969 void
970 lp_setup_begin_query(struct lp_setup_context *setup,
971 struct llvmpipe_query *pq)
972 {
973 /* init the query to its beginning state */
974 assert(setup->active_query == NULL);
975
976 if (setup->scene) {
977 if (!lp_scene_bin_everywhere(setup->scene,
978 LP_RAST_OP_BEGIN_QUERY,
979 lp_rast_arg_query(pq))) {
980
981 lp_setup_flush_and_restart(setup);
982
983 if (!lp_scene_bin_everywhere(setup->scene,
984 LP_RAST_OP_BEGIN_QUERY,
985 lp_rast_arg_query(pq))) {
986 assert(0);
987 return;
988 }
989 }
990 }
991
992 setup->active_query = pq;
993 }
994
995
996 /**
997 * Put an EndQuery command into all bins.
998 */
999 void
1000 lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)
1001 {
1002 union lp_rast_cmd_arg dummy = { 0 };
1003
1004 assert(setup->active_query == pq);
1005 setup->active_query = NULL;
1006
1007 /* Setup will automatically re-issue any query which carried over a
1008 * scene boundary, and the rasterizer automatically "ends" queries
1009 * which are active at the end of a scene, so there is no need to
1010 * retry this commands on failure.
1011 */
1012 if (setup->scene) {
1013 /* pq->fence should be the fence of the *last* scene which
1014 * contributed to the query result.
1015 */
1016 lp_fence_reference(&pq->fence, setup->scene->fence);
1017
1018 if (!lp_scene_bin_everywhere(setup->scene,
1019 LP_RAST_OP_END_QUERY,
1020 dummy)) {
1021 lp_setup_flush(setup, 0, NULL, __FUNCTION__);
1022 }
1023 }
1024 else {
1025 lp_fence_reference(&pq->fence, setup->last_fence);
1026 }
1027 }
1028
1029
1030 void
1031 lp_setup_flush_and_restart(struct lp_setup_context *setup)
1032 {
1033 if (0) debug_printf("%s\n", __FUNCTION__);
1034
1035 assert(setup->state == SETUP_ACTIVE);
1036 set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__);
1037 lp_setup_update_state(setup, TRUE);
1038 }
1039
1040