Merge branch 'gallium-no-rhw-position'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * Tiling engine.
30 *
31 * Builds per-tile display lists and executes them on calls to
32 * lp_setup_flush().
33 */
34
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "util/u_surface.h"
40 #include "lp_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 #include "lp_screen.h"
49 #include "lp_winsys.h"
50
51 #include "draw/draw_context.h"
52 #include "draw/draw_vbuf.h"
53
54
55 static void set_scene_state( struct setup_context *, unsigned );
56
57
58 struct lp_scene *
59 lp_setup_get_current_scene(struct setup_context *setup)
60 {
61 if (!setup->scene) {
62
63 /* wait for a free/empty scene
64 */
65 setup->scene = lp_scene_dequeue(setup->empty_scenes, TRUE);
66
67 assert(lp_scene_is_empty(setup->scene));
68
69 if(0)lp_scene_reset( setup->scene ); /* XXX temporary? */
70
71 lp_scene_set_framebuffer_size(setup->scene,
72 setup->fb.width,
73 setup->fb.height);
74 }
75 return setup->scene;
76 }
77
78
79 static void
80 first_triangle( struct setup_context *setup,
81 const float (*v0)[4],
82 const float (*v1)[4],
83 const float (*v2)[4])
84 {
85 set_scene_state( setup, SETUP_ACTIVE );
86 lp_setup_choose_triangle( setup );
87 setup->triangle( setup, v0, v1, v2 );
88 }
89
90 static void
91 first_line( struct setup_context *setup,
92 const float (*v0)[4],
93 const float (*v1)[4])
94 {
95 set_scene_state( setup, SETUP_ACTIVE );
96 lp_setup_choose_line( setup );
97 setup->line( setup, v0, v1 );
98 }
99
100 static void
101 first_point( struct setup_context *setup,
102 const float (*v0)[4])
103 {
104 set_scene_state( setup, SETUP_ACTIVE );
105 lp_setup_choose_point( setup );
106 setup->point( setup, v0 );
107 }
108
109 static void reset_context( struct setup_context *setup )
110 {
111 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
112
113 /* Reset derived state */
114 setup->constants.stored_size = 0;
115 setup->constants.stored_data = NULL;
116 setup->fs.stored = NULL;
117 setup->dirty = ~0;
118
119 /* no current bin */
120 setup->scene = NULL;
121
122 /* Reset some state:
123 */
124 setup->clear.flags = 0;
125
126 /* Have an explicit "start-binning" call and get rid of this
127 * pointer twiddling?
128 */
129 setup->line = first_line;
130 setup->point = first_point;
131 setup->triangle = first_triangle;
132 }
133
134
135 /** Rasterize all scene's bins */
136 static void
137 lp_setup_rasterize_scene( struct setup_context *setup,
138 boolean write_depth )
139 {
140 struct lp_scene *scene = lp_setup_get_current_scene(setup);
141
142 lp_rasterize_scene(setup->rast,
143 scene,
144 &setup->fb,
145 write_depth);
146
147 reset_context( setup );
148
149 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
150 }
151
152
153
154 static void
155 begin_binning( struct setup_context *setup )
156 {
157 struct lp_scene *scene = lp_setup_get_current_scene(setup);
158
159 LP_DBG(DEBUG_SETUP, "%s color: %s depth: %s\n", __FUNCTION__,
160 (setup->clear.flags & PIPE_CLEAR_COLOR) ? "clear": "load",
161 (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) ? "clear": "load");
162
163 if (setup->fb.nr_cbufs) {
164 if (setup->clear.flags & PIPE_CLEAR_COLOR)
165 lp_scene_bin_everywhere( scene,
166 lp_rast_clear_color,
167 setup->clear.color );
168 else
169 lp_scene_bin_everywhere( scene,
170 lp_rast_load_color,
171 lp_rast_arg_null() );
172 }
173
174 if (setup->fb.zsbuf) {
175 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
176 lp_scene_bin_everywhere( scene,
177 lp_rast_clear_zstencil,
178 setup->clear.zstencil );
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 (void) scene;
257 scene = lp_setup_get_current_scene(setup);
258
259 util_copy_framebuffer_state(&setup->fb, fb);
260
261 lp_scene_set_framebuffer_size(scene, setup->fb.width, setup->fb.height);
262 }
263
264
265 void
266 lp_setup_clear( struct setup_context *setup,
267 const float *color,
268 double depth,
269 unsigned stencil,
270 unsigned flags )
271 {
272 struct lp_scene *scene = lp_setup_get_current_scene(setup);
273 unsigned i;
274
275 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
276
277
278 if (flags & PIPE_CLEAR_COLOR) {
279 for (i = 0; i < 4; ++i)
280 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
281 }
282
283 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
284 setup->clear.zstencil.clear_zstencil =
285 util_pack_z_stencil(setup->fb.zsbuf->format,
286 depth,
287 stencil);
288 }
289
290 if (setup->state == SETUP_ACTIVE) {
291 /* Add the clear to existing scene. In the unusual case where
292 * both color and depth-stencil are being cleared when there's
293 * already been some rendering, we could discard the currently
294 * binned scene and start again, but I don't see that as being
295 * a common usage.
296 */
297 if (flags & PIPE_CLEAR_COLOR)
298 lp_scene_bin_everywhere( scene,
299 lp_rast_clear_color,
300 setup->clear.color );
301
302 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
303 lp_scene_bin_everywhere( scene,
304 lp_rast_clear_zstencil,
305 setup->clear.zstencil );
306 }
307 else {
308 /* Put ourselves into the 'pre-clear' state, specifically to try
309 * and accumulate multiple clears to color and depth_stencil
310 * buffers which the app or state-tracker might issue
311 * separately.
312 */
313 set_scene_state( setup, SETUP_CLEARED );
314
315 setup->clear.flags |= flags;
316 }
317 }
318
319
320 /**
321 * Emit a fence.
322 */
323 struct pipe_fence_handle *
324 lp_setup_fence( struct setup_context *setup )
325 {
326 struct lp_scene *scene = lp_setup_get_current_scene(setup);
327 const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
328 struct lp_fence *fence = lp_fence_create(rank);
329
330 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
331
332 set_scene_state( setup, SETUP_ACTIVE );
333
334 /* insert the fence into all command bins */
335 lp_scene_bin_everywhere( scene,
336 lp_rast_fence,
337 lp_rast_arg_fence(fence) );
338
339 return (struct pipe_fence_handle *) fence;
340 }
341
342
343 void
344 lp_setup_set_triangle_state( struct setup_context *setup,
345 unsigned cull_mode,
346 boolean ccw_is_frontface,
347 boolean scissor )
348 {
349 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
350
351 setup->ccw_is_frontface = ccw_is_frontface;
352 setup->cullmode = cull_mode;
353 setup->triangle = first_triangle;
354 setup->scissor_test = scissor;
355 }
356
357
358
359 void
360 lp_setup_set_fs_inputs( struct setup_context *setup,
361 const struct lp_shader_input *input,
362 unsigned nr )
363 {
364 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
365
366 memcpy( setup->fs.input, input, nr * sizeof input[0] );
367 setup->fs.nr_inputs = nr;
368 }
369
370 void
371 lp_setup_set_fs_functions( struct setup_context *setup,
372 lp_jit_frag_func jit_function0,
373 lp_jit_frag_func jit_function1,
374 boolean opaque )
375 {
376 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) jit_function0);
377 /* FIXME: reference count */
378
379 setup->fs.current.jit_function[0] = jit_function0;
380 setup->fs.current.jit_function[1] = jit_function1;
381 setup->fs.current.opaque = opaque;
382 setup->dirty |= LP_SETUP_NEW_FS;
383 }
384
385 void
386 lp_setup_set_fs_constants(struct setup_context *setup,
387 struct pipe_buffer *buffer)
388 {
389 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
390
391 pipe_buffer_reference(&setup->constants.current, buffer);
392
393 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
394 }
395
396
397 void
398 lp_setup_set_alpha_ref_value( struct setup_context *setup,
399 float alpha_ref_value )
400 {
401 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
402
403 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
404 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
405 setup->dirty |= LP_SETUP_NEW_FS;
406 }
407 }
408
409 void
410 lp_setup_set_blend_color( struct setup_context *setup,
411 const struct pipe_blend_color *blend_color )
412 {
413 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
414
415 assert(blend_color);
416
417 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
418 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
419 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
420 }
421 }
422
423
424 void
425 lp_setup_set_scissor( struct setup_context *setup,
426 const struct pipe_scissor_state *scissor )
427 {
428 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
429
430 assert(scissor);
431
432 if (memcmp(&setup->scissor.current, scissor, sizeof(*scissor)) != 0) {
433 setup->scissor.current = *scissor; /* struct copy */
434 setup->dirty |= LP_SETUP_NEW_SCISSOR;
435 }
436 }
437
438
439 void
440 lp_setup_set_flatshade_first( struct setup_context *setup,
441 boolean flatshade_first )
442 {
443 setup->flatshade_first = flatshade_first;
444 }
445
446
447 void
448 lp_setup_set_vertex_info( struct setup_context *setup,
449 struct vertex_info *vertex_info )
450 {
451 /* XXX: just silently holding onto the pointer:
452 */
453 setup->vertex_info = vertex_info;
454 }
455
456
457 /**
458 * Called during state validation when LP_NEW_TEXTURE is set.
459 */
460 void
461 lp_setup_set_sampler_textures( struct setup_context *setup,
462 unsigned num, struct pipe_texture **texture)
463 {
464 unsigned i;
465
466 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
467
468 assert(num <= PIPE_MAX_SAMPLERS);
469
470 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
471 struct pipe_texture *tex = i < num ? texture[i] : NULL;
472
473 if(tex) {
474 struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
475 struct lp_jit_texture *jit_tex;
476 jit_tex = &setup->fs.current.jit_context.textures[i];
477 jit_tex->width = tex->width0;
478 jit_tex->height = tex->height0;
479 jit_tex->stride = lp_tex->stride[0];
480 if(!lp_tex->dt) {
481 jit_tex->data = lp_tex->data;
482 }
483 else {
484 /*
485 * XXX: Where should this be unmapped?
486 */
487
488 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
489 struct llvmpipe_winsys *winsys = screen->winsys;
490 jit_tex->data = winsys->displaytarget_map(winsys, lp_tex->dt,
491 PIPE_BUFFER_USAGE_CPU_READ);
492 assert(jit_tex->data);
493 }
494
495 /* the scene references this texture */
496 {
497 struct lp_scene *scene = lp_setup_get_current_scene(setup);
498 lp_scene_texture_reference(scene, tex);
499 }
500 }
501 }
502
503 setup->dirty |= LP_SETUP_NEW_FS;
504 }
505
506
507 /**
508 * Is the given texture referenced by any scene?
509 * Note: we have to check all scenes including any scenes currently
510 * being rendered and the current scene being built.
511 */
512 unsigned
513 lp_setup_is_texture_referenced( const struct setup_context *setup,
514 const struct pipe_texture *texture )
515 {
516 unsigned i;
517
518 /* check the render targets */
519 for (i = 0; i < setup->fb.nr_cbufs; i++) {
520 if (setup->fb.cbufs[i]->texture == texture)
521 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
522 }
523 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
524 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
525 }
526
527 /* check textures referenced by the scene */
528 for (i = 0; i < Elements(setup->scenes); i++) {
529 if (lp_scene_is_texture_referenced(setup->scenes[i], texture)) {
530 return PIPE_REFERENCED_FOR_READ;
531 }
532 }
533
534 return PIPE_UNREFERENCED;
535 }
536
537
538 /**
539 * Called by vbuf code when we're about to draw something.
540 */
541 void
542 lp_setup_update_state( struct setup_context *setup )
543 {
544 struct lp_scene *scene = lp_setup_get_current_scene(setup);
545
546 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
547
548 assert(setup->fs.current.jit_function);
549
550 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
551 uint8_t *stored;
552 unsigned i, j;
553
554 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
555
556 /* smear each blend color component across 16 ubyte elements */
557 for (i = 0; i < 4; ++i) {
558 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
559 for (j = 0; j < 16; ++j)
560 stored[i*16 + j] = c;
561 }
562
563 setup->blend_color.stored = stored;
564
565 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
566 setup->dirty |= LP_SETUP_NEW_FS;
567 }
568
569 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
570 float *stored;
571
572 stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
573
574 stored[0] = (float) setup->scissor.current.minx;
575 stored[1] = (float) setup->scissor.current.miny;
576 stored[2] = (float) setup->scissor.current.maxx;
577 stored[3] = (float) setup->scissor.current.maxy;
578
579 setup->scissor.stored = stored;
580
581 setup->fs.current.jit_context.scissor_xmin = stored[0];
582 setup->fs.current.jit_context.scissor_ymin = stored[1];
583 setup->fs.current.jit_context.scissor_xmax = stored[2];
584 setup->fs.current.jit_context.scissor_ymax = stored[3];
585
586 setup->dirty |= LP_SETUP_NEW_FS;
587 }
588
589 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
590 struct pipe_buffer *buffer = setup->constants.current;
591
592 if(buffer) {
593 unsigned current_size = buffer->size;
594 const void *current_data = llvmpipe_buffer(buffer)->data;
595
596 /* TODO: copy only the actually used constants? */
597
598 if(setup->constants.stored_size != current_size ||
599 !setup->constants.stored_data ||
600 memcmp(setup->constants.stored_data,
601 current_data,
602 current_size) != 0) {
603 void *stored;
604
605 stored = lp_scene_alloc(scene, current_size);
606 if(stored) {
607 memcpy(stored,
608 current_data,
609 current_size);
610 setup->constants.stored_size = current_size;
611 setup->constants.stored_data = stored;
612 }
613 }
614 }
615 else {
616 setup->constants.stored_size = 0;
617 setup->constants.stored_data = NULL;
618 }
619
620 setup->fs.current.jit_context.constants = setup->constants.stored_data;
621 setup->dirty |= LP_SETUP_NEW_FS;
622 }
623
624
625 if(setup->dirty & LP_SETUP_NEW_FS) {
626 if(!setup->fs.stored ||
627 memcmp(setup->fs.stored,
628 &setup->fs.current,
629 sizeof setup->fs.current) != 0) {
630 /* The fs state that's been stored in the scene is different from
631 * the new, current state. So allocate a new lp_rast_state object
632 * and append it to the bin's setup data buffer.
633 */
634 struct lp_rast_state *stored =
635 (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
636 if(stored) {
637 memcpy(stored,
638 &setup->fs.current,
639 sizeof setup->fs.current);
640 setup->fs.stored = stored;
641
642 /* put the state-set command into all bins */
643 lp_scene_bin_state_command( scene,
644 lp_rast_set_state,
645 lp_rast_arg_state(setup->fs.stored) );
646 }
647 }
648 }
649
650 setup->dirty = 0;
651
652 assert(setup->fs.stored);
653 }
654
655
656
657 /* Only caller is lp_setup_vbuf_destroy()
658 */
659 void
660 lp_setup_destroy( struct setup_context *setup )
661 {
662 reset_context( setup );
663
664 pipe_buffer_reference(&setup->constants.current, NULL);
665
666 /* free the scenes in the 'empty' queue */
667 while (1) {
668 struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
669 if (!scene)
670 break;
671 lp_scene_destroy(scene);
672 }
673
674 lp_rast_destroy( setup->rast );
675
676 FREE( setup );
677 }
678
679
680 /**
681 * Create a new primitive tiling engine. Plug it into the backend of
682 * the draw module. Currently also creates a rasterizer to use with
683 * it.
684 */
685 struct setup_context *
686 lp_setup_create( struct pipe_screen *screen,
687 struct draw_context *draw )
688 {
689 unsigned i;
690 struct setup_context *setup = CALLOC_STRUCT(setup_context);
691
692 if (!setup)
693 return NULL;
694
695 lp_setup_init_vbuf(setup);
696
697 setup->empty_scenes = lp_scene_queue_create();
698 if (!setup->empty_scenes)
699 goto fail;
700
701 setup->rast = lp_rast_create( screen, setup->empty_scenes );
702 if (!setup->rast)
703 goto fail;
704
705 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
706 if (!setup->vbuf)
707 goto fail;
708
709 draw_set_rasterize_stage(draw, setup->vbuf);
710 draw_set_render(draw, &setup->base);
711
712 /* create some empty scenes */
713 for (i = 0; i < MAX_SCENES; i++) {
714 setup->scenes[i] = lp_scene_create();
715 lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
716 }
717
718 setup->triangle = first_triangle;
719 setup->line = first_line;
720 setup->point = first_point;
721
722 setup->dirty = ~0;
723
724 return setup;
725
726 fail:
727 if (setup->rast)
728 lp_rast_destroy( setup->rast );
729
730 if (setup->vbuf)
731 ;
732
733 if (setup->empty_scenes)
734 lp_scene_queue_destroy(setup->empty_scenes);
735
736 FREE(setup);
737 return NULL;
738 }
739