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