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