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