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