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