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