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