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