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