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