Squashed commit of the following:
[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 "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "util/u_surface.h"
40 #include "lp_scene.h"
41 #include "lp_scene_queue.h"
42 #include "lp_texture.h"
43 #include "lp_debug.h"
44 #include "lp_fence.h"
45 #include "lp_rast.h"
46 #include "lp_setup_context.h"
47 #include "lp_screen.h"
48 #include "state_tracker/sw_winsys.h"
49
50 #include "draw/draw_context.h"
51 #include "draw/draw_vbuf.h"
52
53
54 static void set_scene_state( struct lp_setup_context *, unsigned );
55
56
57 struct lp_scene *
58 lp_setup_get_current_scene(struct lp_setup_context *setup)
59 {
60    if (!setup->scene) {
61
62       /* wait for a free/empty scene
63        */
64       setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
65
66       assert(lp_scene_is_empty(setup->scene));
67
68       lp_scene_begin_binning(setup->scene,
69                              &setup->fb );
70    }
71    return setup->scene;
72 }
73
74
75 static void
76 first_triangle( struct lp_setup_context *setup,
77                 const float (*v0)[4],
78                 const float (*v1)[4],
79                 const float (*v2)[4])
80 {
81    set_scene_state( setup, SETUP_ACTIVE );
82    lp_setup_choose_triangle( setup );
83    setup->triangle( setup, v0, v1, v2 );
84 }
85
86 static void
87 first_line( struct lp_setup_context *setup,
88             const float (*v0)[4],
89             const float (*v1)[4])
90 {
91    set_scene_state( setup, SETUP_ACTIVE );
92    lp_setup_choose_line( setup );
93    setup->line( setup, v0, v1 );
94 }
95
96 static void
97 first_point( struct lp_setup_context *setup,
98              const float (*v0)[4])
99 {
100    set_scene_state( setup, SETUP_ACTIVE );
101    lp_setup_choose_point( setup );
102    setup->point( setup, v0 );
103 }
104
105 static void reset_context( struct lp_setup_context *setup )
106 {
107    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
108
109    /* Reset derived state */
110    setup->constants.stored_size = 0;
111    setup->constants.stored_data = NULL;
112    setup->fs.stored = NULL;
113    setup->dirty = ~0;
114
115    /* no current bin */
116    setup->scene = NULL;
117
118    /* Reset some state:
119     */
120    setup->clear.flags = 0;
121
122    /* Have an explicit "start-binning" call and get rid of this
123     * pointer twiddling?
124     */
125    setup->line = first_line;
126    setup->point = first_point;
127    setup->triangle = first_triangle;
128 }
129
130
131 /** Rasterize all scene's bins */
132 static void
133 lp_setup_rasterize_scene( struct lp_setup_context *setup,
134                           boolean write_depth )
135 {
136    struct lp_scene *scene = lp_setup_get_current_scene(setup);
137
138    lp_scene_rasterize(scene,
139                       setup->rast,
140                       write_depth);
141
142    reset_context( setup );
143
144    LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
145 }
146
147
148
149 static void
150 begin_binning( struct lp_setup_context *setup )
151 {
152    struct lp_scene *scene = lp_setup_get_current_scene(setup);
153
154    LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
155           (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
156           (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
157
158    if (setup->fb.nr_cbufs) {
159       if (setup->clear.flags & PIPE_CLEAR_COLOR)
160          lp_scene_bin_everywhere( scene, 
161                                   lp_rast_clear_color, 
162                                   setup->clear.color );
163       else
164          lp_scene_bin_everywhere( scene,
165                                   lp_rast_load_color,
166                                   lp_rast_arg_null() );
167    }
168
169    if (setup->fb.zsbuf) {
170       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
171          lp_scene_bin_everywhere( scene, 
172                                   lp_rast_clear_zstencil, 
173                                   setup->clear.zstencil );
174    }
175
176    LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
177 }
178
179
180 /* This basically bins and then flushes any outstanding full-screen
181  * clears.  
182  *
183  * TODO: fast path for fullscreen clears and no triangles.
184  */
185 static void
186 execute_clears( struct lp_setup_context *setup )
187 {
188    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
189
190    begin_binning( setup );
191    lp_setup_rasterize_scene( setup, TRUE );
192 }
193
194
195 static void
196 set_scene_state( struct lp_setup_context *setup,
197            unsigned new_state )
198 {
199    unsigned old_state = setup->state;
200
201    if (old_state == new_state)
202       return;
203        
204    LP_DBG(DEBUG_SETUP, "%s old %d new %d\n", __FUNCTION__, old_state, new_state);
205
206    switch (new_state) {
207    case SETUP_ACTIVE:
208       begin_binning( setup );
209       break;
210
211    case SETUP_CLEARED:
212       if (old_state == SETUP_ACTIVE) {
213          assert(0);
214          return;
215       }
216       break;
217       
218    case SETUP_FLUSHED:
219       if (old_state == SETUP_CLEARED)
220          execute_clears( setup );
221       else
222          lp_setup_rasterize_scene( setup, TRUE );
223       break;
224    }
225
226    setup->state = new_state;
227 }
228
229
230 void
231 lp_setup_flush( struct lp_setup_context *setup,
232                 unsigned flags )
233 {
234    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
235
236    set_scene_state( setup, SETUP_FLUSHED );
237 }
238
239
240 void
241 lp_setup_bind_framebuffer( struct lp_setup_context *setup,
242                            const struct pipe_framebuffer_state *fb )
243 {
244    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
245
246    /* Flush any old scene.
247     */
248    set_scene_state( setup, SETUP_FLUSHED );
249
250    /* Set new state.  This will be picked up later when we next need a
251     * scene.
252     */
253    util_copy_framebuffer_state(&setup->fb, fb);
254 }
255
256
257 void
258 lp_setup_clear( struct lp_setup_context *setup,
259                 const float *color,
260                 double depth,
261                 unsigned stencil,
262                 unsigned flags )
263 {
264    struct lp_scene *scene = lp_setup_get_current_scene(setup);
265    unsigned i;
266
267    LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
268
269
270    if (flags & PIPE_CLEAR_COLOR) {
271       for (i = 0; i < 4; ++i)
272          setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
273    }
274
275    if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
276       setup->clear.zstencil.clear_zstencil = 
277          util_pack_z_stencil(setup->fb.zsbuf->format, 
278                              depth,
279                              stencil);
280    }
281
282    if (setup->state == SETUP_ACTIVE) {
283       /* Add the clear to existing scene.  In the unusual case where
284        * both color and depth-stencil are being cleared when there's
285        * already been some rendering, we could discard the currently
286        * binned scene and start again, but I don't see that as being
287        * a common usage.
288        */
289       if (flags & PIPE_CLEAR_COLOR)
290          lp_scene_bin_everywhere( scene, 
291                                   lp_rast_clear_color,
292                                   setup->clear.color );
293
294       if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
295          lp_scene_bin_everywhere( scene, 
296                                   lp_rast_clear_zstencil,
297                                   setup->clear.zstencil );
298    }
299    else {
300       /* Put ourselves into the 'pre-clear' state, specifically to try
301        * and accumulate multiple clears to color and depth_stencil
302        * buffers which the app or state-tracker might issue
303        * separately.
304        */
305       set_scene_state( setup, SETUP_CLEARED );
306
307       setup->clear.flags |= flags;
308    }
309 }
310
311
312 /**
313  * Emit a fence.
314  */
315 struct pipe_fence_handle *
316 lp_setup_fence( struct lp_setup_context *setup )
317 {
318    struct lp_scene *scene = lp_setup_get_current_scene(setup);
319    const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
320    struct lp_fence *fence = lp_fence_create(rank);
321
322    LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
323
324    set_scene_state( setup, SETUP_ACTIVE );
325
326    /* insert the fence into all command bins */
327    lp_scene_bin_everywhere( scene,
328                             lp_rast_fence,
329                             lp_rast_arg_fence(fence) );
330
331    return (struct pipe_fence_handle *) fence;
332 }
333
334
335 void 
336 lp_setup_set_triangle_state( struct lp_setup_context *setup,
337                              unsigned cull_mode,
338                              boolean ccw_is_frontface,
339                              boolean scissor,
340                              boolean gl_rasterization_rules)
341 {
342    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
343
344    setup->ccw_is_frontface = ccw_is_frontface;
345    setup->cullmode = cull_mode;
346    setup->triangle = first_triangle;
347    setup->scissor_test = scissor;
348    setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
349 }
350
351
352
353 void
354 lp_setup_set_fs_inputs( struct lp_setup_context *setup,
355                         const struct lp_shader_input *input,
356                         unsigned nr )
357 {
358    LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
359
360    memcpy( setup->fs.input, input, nr * sizeof input[0] );
361    setup->fs.nr_inputs = nr;
362 }
363
364 void
365 lp_setup_set_fs_functions( struct lp_setup_context *setup,
366                            lp_jit_frag_func jit_function0,
367                            lp_jit_frag_func jit_function1,
368                            boolean opaque )
369 {
370    LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
371    /* FIXME: reference count */
372
373    setup->fs.current.jit_function[0] = jit_function0;
374    setup->fs.current.jit_function[1] = jit_function1;
375    setup->fs.current.opaque = opaque;
376    setup->dirty |= LP_SETUP_NEW_FS;
377 }
378
379 void
380 lp_setup_set_fs_constants(struct lp_setup_context *setup,
381                           struct pipe_resource *buffer)
382 {
383    LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
384
385    pipe_resource_reference(&setup->constants.current, buffer);
386
387    setup->dirty |= LP_SETUP_NEW_CONSTANTS;
388 }
389
390
391 void
392 lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,
393                               float alpha_ref_value )
394 {
395    LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
396
397    if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
398       setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
399       setup->dirty |= LP_SETUP_NEW_FS;
400    }
401 }
402
403 void
404 lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,
405                                  const ubyte refs[2] )
406 {
407    LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);
408
409    if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||
410        setup->fs.current.jit_context.stencil_ref_back != refs[1]) {
411       setup->fs.current.jit_context.stencil_ref_front = refs[0];
412       setup->fs.current.jit_context.stencil_ref_back = refs[1];
413       setup->dirty |= LP_SETUP_NEW_FS;
414    }
415 }
416
417 void
418 lp_setup_set_blend_color( struct lp_setup_context *setup,
419                           const struct pipe_blend_color *blend_color )
420 {
421    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
422
423    assert(blend_color);
424
425    if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
426       memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
427       setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
428    }
429 }
430
431
432 void
433 lp_setup_set_scissor( struct lp_setup_context *setup,
434                       const struct pipe_scissor_state *scissor )
435 {
436    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
437
438    assert(scissor);
439
440    if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
441       setup->scissor.current = *scissor; /* struct copy */
442       setup->dirty |= LP_SETUP_NEW_SCISSOR;
443    }
444 }
445
446
447 void 
448 lp_setup_set_flatshade_first( struct lp_setup_context *setup,
449                               boolean flatshade_first )
450 {
451    setup->flatshade_first = flatshade_first;
452 }
453
454
455 void 
456 lp_setup_set_vertex_info( struct lp_setup_context *setup,
457                           struct vertex_info *vertex_info )
458 {
459    /* XXX: just silently holding onto the pointer:
460     */
461    setup->vertex_info = vertex_info;
462 }
463
464
465 /**
466  * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
467  */
468 void
469 lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
470                                     unsigned num,
471                                     struct pipe_sampler_view **views)
472 {
473    unsigned i;
474
475    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
476
477    assert(num <= PIPE_MAX_SAMPLERS);
478
479    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
480       struct pipe_sampler_view *view = i < num ? views[i] : NULL;
481
482       if(view) {
483          struct pipe_resource *tex = view->texture;
484          struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
485          struct lp_jit_texture *jit_tex;
486          jit_tex = &setup->fs.current.jit_context.textures[i];
487          jit_tex->width = tex->width0;
488          jit_tex->height = tex->height0;
489          jit_tex->depth = tex->depth0;
490          jit_tex->last_level = tex->last_level;
491
492          /* We're referencing the texture's internal data, so save a
493           * reference to it.
494           */
495          pipe_resource_reference(&setup->fs.current_tex[i], tex);
496
497          if (!lp_tex->dt) {
498             /* regular texture - setup array of mipmap level pointers */
499             int j;
500             for (j = 0; j <= tex->last_level; j++) {
501                jit_tex->data[j] =
502                   (ubyte *) lp_tex->data + lp_tex->level_offset[j];
503                jit_tex->row_stride[j] = lp_tex->stride[j];
504             }
505          }
506          else {
507             /* display target texture/surface */
508             /*
509              * XXX: Where should this be unmapped?
510              */
511
512             struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
513             struct sw_winsys *winsys = screen->winsys;
514             jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
515                                                          PIPE_TRANSFER_READ);
516             jit_tex->row_stride[0] = lp_tex->stride[0];
517             assert(jit_tex->data[0]);
518          }
519       }
520    }
521
522    setup->dirty |= LP_SETUP_NEW_FS;
523 }
524
525
526 /**
527  * Is the given texture referenced by any scene?
528  * Note: we have to check all scenes including any scenes currently
529  * being rendered and the current scene being built.
530  */
531 unsigned
532 lp_setup_is_resource_referenced( const struct lp_setup_context *setup,
533                                 const struct pipe_resource *texture )
534 {
535    unsigned i;
536
537    /* check the render targets */
538    for (i = 0; i < setup->fb.nr_cbufs; i++) {
539       if (setup->fb.cbufs[i]->texture == texture)
540          return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
541    }
542    if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
543       return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
544    }
545
546    /* check textures referenced by the scene */
547    for (i = 0; i < Elements(setup->scenes); i++) {
548       if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {
549          return PIPE_REFERENCED_FOR_READ;
550       }
551    }
552
553    return PIPE_UNREFERENCED;
554 }
555
556
557 /**
558  * Called by vbuf code when we're about to draw something.
559  */
560 void
561 lp_setup_update_state( struct lp_setup_context *setup )
562 {
563    struct lp_scene *scene = lp_setup_get_current_scene(setup);
564
565    LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
566
567    assert(setup->fs.current.jit_function);
568
569    if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
570       uint8_t *stored;
571       unsigned i, j;
572
573       stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
574
575       /* smear each blend color component across 16 ubyte elements */
576       for (i = 0; i < 4; ++i) {
577          uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
578          for (j = 0; j < 16; ++j)
579             stored[i*16 + j] = c;
580       }
581
582       setup->blend_color.stored = stored;
583
584       setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
585       setup->dirty |= LP_SETUP_NEW_FS;
586    }
587
588    if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
589       float *stored;
590
591       stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
592
593       stored[0] = (float) setup->scissor.current.minx;
594       stored[1] = (float) setup->scissor.current.miny;
595       stored[2] = (float) setup->scissor.current.maxx;
596       stored[3] = (float) setup->scissor.current.maxy;
597
598       setup->scissor.stored = stored;
599
600       setup->fs.current.jit_context.scissor_xmin = stored[0];
601       setup->fs.current.jit_context.scissor_ymin = stored[1];
602       setup->fs.current.jit_context.scissor_xmax = stored[2];
603       setup->fs.current.jit_context.scissor_ymax = stored[3];
604
605       setup->dirty |= LP_SETUP_NEW_FS;
606    }
607
608    if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
609       struct pipe_resource *buffer = setup->constants.current;
610
611       if(buffer) {
612          unsigned current_size = buffer->width0;
613          const void *current_data = llvmpipe_resource(buffer)->data;
614
615          /* TODO: copy only the actually used constants? */
616
617          if(setup->constants.stored_size != current_size ||
618             !setup->constants.stored_data ||
619             memcmp(setup->constants.stored_data,
620                    current_data,
621                    current_size) != 0) {
622             void *stored;
623
624             stored = lp_scene_alloc(scene, current_size);
625             if(stored) {
626                memcpy(stored,
627                       current_data,
628                       current_size);
629                setup->constants.stored_size = current_size;
630                setup->constants.stored_data = stored;
631             }
632          }
633       }
634       else {
635          setup->constants.stored_size = 0;
636          setup->constants.stored_data = NULL;
637       }
638
639       setup->fs.current.jit_context.constants = setup->constants.stored_data;
640       setup->dirty |= LP_SETUP_NEW_FS;
641    }
642
643
644    if(setup->dirty & LP_SETUP_NEW_FS) {
645       if(!setup->fs.stored ||
646          memcmp(setup->fs.stored,
647                 &setup->fs.current,
648                 sizeof setup->fs.current) != 0) {
649          /* The fs state that's been stored in the scene is different from
650           * the new, current state.  So allocate a new lp_rast_state object
651           * and append it to the bin's setup data buffer.
652           */
653          uint i;
654          struct lp_rast_state *stored =
655             (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
656          if(stored) {
657             memcpy(stored,
658                    &setup->fs.current,
659                    sizeof setup->fs.current);
660             setup->fs.stored = stored;
661
662             /* put the state-set command into all bins */
663             lp_scene_bin_state_command( scene,
664                                         lp_rast_set_state, 
665                                         lp_rast_arg_state(setup->fs.stored) );
666          }
667
668          /* The scene now references the textures in the rasterization
669           * state record.  Note that now.
670           */
671          for (i = 0; i < Elements(setup->fs.current_tex); i++) {
672             if (setup->fs.current_tex[i])
673                lp_scene_texture_reference(scene, setup->fs.current_tex[i]);
674          }
675       }
676    }
677
678    setup->dirty = 0;
679
680    assert(setup->fs.stored);
681 }
682
683
684
685 /* Only caller is lp_setup_vbuf_destroy()
686  */
687 void 
688 lp_setup_destroy( struct lp_setup_context *setup )
689 {
690    uint i;
691
692    reset_context( setup );
693
694    for (i = 0; i < Elements(setup->fs.current_tex); i++) {
695       pipe_resource_reference(&setup->fs.current_tex[i], NULL);
696    }
697
698    pipe_resource_reference(&setup->constants.current, NULL);
699
700    /* free the scenes in the 'empty' queue */
701    while (1) {
702       struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
703       if (!scene)
704          break;
705       lp_scene_destroy(scene);
706    }
707
708    lp_rast_destroy( setup->rast );
709
710    FREE( setup );
711 }
712
713
714 /**
715  * Create a new primitive tiling engine.  Plug it into the backend of
716  * the draw module.  Currently also creates a rasterizer to use with
717  * it.
718  */
719 struct lp_setup_context *
720 lp_setup_create( struct pipe_context *pipe,
721                  struct draw_context *draw )
722 {
723    unsigned i;
724    struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
725
726    if (!setup)
727       return NULL;
728
729    lp_setup_init_vbuf(setup);
730
731    setup->empty_scenes = lp_scene_queue_create();
732    if (!setup->empty_scenes)
733       goto fail;
734
735    /* XXX: move this to the screen and share between contexts:
736     */
737    setup->rast = lp_rast_create();
738    if (!setup->rast) 
739       goto fail;
740
741    setup->vbuf = draw_vbuf_stage(draw, &setup->base);
742    if (!setup->vbuf)
743       goto fail;
744
745    draw_set_rasterize_stage(draw, setup->vbuf);
746    draw_set_render(draw, &setup->base);
747
748    /* create some empty scenes */
749    for (i = 0; i < MAX_SCENES; i++) {
750       setup->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
751
752       lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
753    }
754
755    setup->triangle = first_triangle;
756    setup->line     = first_line;
757    setup->point    = first_point;
758    
759    setup->dirty = ~0;
760
761    return setup;
762
763 fail:
764    if (setup->rast)
765       lp_rast_destroy( setup->rast );
766    
767    if (setup->vbuf)
768       ;
769
770    if (setup->empty_scenes)
771       lp_scene_queue_destroy(setup->empty_scenes);
772
773    FREE(setup);
774    return NULL;
775 }
776