llvmpipe: Obey rasterization rules.
[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 "state_tracker/sw_winsys.h"
50
51 #include "draw/draw_context.h"
52 #include "draw/draw_vbuf.h"
53
54
55 static void set_scene_state( struct lp_setup_context *, unsigned );
56
57
58 struct lp_scene *
59 lp_setup_get_current_scene(struct lp_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 lp_scene_begin_binning(setup->scene,
70 &setup->fb );
71 }
72 return setup->scene;
73 }
74
75
76 static void
77 first_triangle( struct lp_setup_context *setup,
78 const float (*v0)[4],
79 const float (*v1)[4],
80 const float (*v2)[4])
81 {
82 set_scene_state( setup, SETUP_ACTIVE );
83 lp_setup_choose_triangle( setup );
84 setup->triangle( setup, v0, v1, v2 );
85 }
86
87 static void
88 first_line( struct lp_setup_context *setup,
89 const float (*v0)[4],
90 const float (*v1)[4])
91 {
92 set_scene_state( setup, SETUP_ACTIVE );
93 lp_setup_choose_line( setup );
94 setup->line( setup, v0, v1 );
95 }
96
97 static void
98 first_point( struct lp_setup_context *setup,
99 const float (*v0)[4])
100 {
101 set_scene_state( setup, SETUP_ACTIVE );
102 lp_setup_choose_point( setup );
103 setup->point( setup, v0 );
104 }
105
106 static void reset_context( struct lp_setup_context *setup )
107 {
108 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
109
110 /* Reset derived state */
111 setup->constants.stored_size = 0;
112 setup->constants.stored_data = NULL;
113 setup->fs.stored = NULL;
114 setup->dirty = ~0;
115
116 /* no current bin */
117 setup->scene = NULL;
118
119 /* Reset some state:
120 */
121 setup->clear.flags = 0;
122
123 /* Have an explicit "start-binning" call and get rid of this
124 * pointer twiddling?
125 */
126 setup->line = first_line;
127 setup->point = first_point;
128 setup->triangle = first_triangle;
129 }
130
131
132 /** Rasterize all scene's bins */
133 static void
134 lp_setup_rasterize_scene( struct lp_setup_context *setup,
135 boolean write_depth )
136 {
137 struct lp_scene *scene = lp_setup_get_current_scene(setup);
138
139 lp_scene_rasterize(scene,
140 setup->rast,
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 lp_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 lp_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 lp_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 lp_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 lp_setup_context *setup,
243 const struct pipe_framebuffer_state *fb )
244 {
245 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
246
247 /* Flush any old scene.
248 */
249 set_scene_state( setup, SETUP_FLUSHED );
250
251 /* Set new state. This will be picked up later when we next need a
252 * scene.
253 */
254 util_copy_framebuffer_state(&setup->fb, fb);
255 }
256
257
258 void
259 lp_setup_clear( struct lp_setup_context *setup,
260 const float *color,
261 double depth,
262 unsigned stencil,
263 unsigned flags )
264 {
265 struct lp_scene *scene = lp_setup_get_current_scene(setup);
266 unsigned i;
267
268 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
269
270
271 if (flags & PIPE_CLEAR_COLOR) {
272 for (i = 0; i < 4; ++i)
273 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
274 }
275
276 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
277 setup->clear.zstencil.clear_zstencil =
278 util_pack_z_stencil(setup->fb.zsbuf->format,
279 depth,
280 stencil);
281 }
282
283 if (setup->state == SETUP_ACTIVE) {
284 /* Add the clear to existing scene. In the unusual case where
285 * both color and depth-stencil are being cleared when there's
286 * already been some rendering, we could discard the currently
287 * binned scene and start again, but I don't see that as being
288 * a common usage.
289 */
290 if (flags & PIPE_CLEAR_COLOR)
291 lp_scene_bin_everywhere( scene,
292 lp_rast_clear_color,
293 setup->clear.color );
294
295 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
296 lp_scene_bin_everywhere( scene,
297 lp_rast_clear_zstencil,
298 setup->clear.zstencil );
299 }
300 else {
301 /* Put ourselves into the 'pre-clear' state, specifically to try
302 * and accumulate multiple clears to color and depth_stencil
303 * buffers which the app or state-tracker might issue
304 * separately.
305 */
306 set_scene_state( setup, SETUP_CLEARED );
307
308 setup->clear.flags |= flags;
309 }
310 }
311
312
313 /**
314 * Emit a fence.
315 */
316 struct pipe_fence_handle *
317 lp_setup_fence( struct lp_setup_context *setup )
318 {
319 struct lp_scene *scene = lp_setup_get_current_scene(setup);
320 const unsigned rank = lp_scene_get_num_bins( scene ); /* xxx */
321 struct lp_fence *fence = lp_fence_create(rank);
322
323 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
324
325 set_scene_state( setup, SETUP_ACTIVE );
326
327 /* insert the fence into all command bins */
328 lp_scene_bin_everywhere( scene,
329 lp_rast_fence,
330 lp_rast_arg_fence(fence) );
331
332 return (struct pipe_fence_handle *) fence;
333 }
334
335
336 void
337 lp_setup_set_triangle_state( struct lp_setup_context *setup,
338 unsigned cull_mode,
339 boolean ccw_is_frontface,
340 boolean scissor,
341 boolean gl_rasterization_rules)
342 {
343 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
344
345 setup->ccw_is_frontface = ccw_is_frontface;
346 setup->cullmode = cull_mode;
347 setup->triangle = first_triangle;
348 setup->scissor_test = scissor;
349 setup->pixel_offset = gl_rasterization_rules ? 0.5f : 0.0f;
350 }
351
352
353
354 void
355 lp_setup_set_fs_inputs( struct lp_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 lp_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 lp_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 lp_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 lp_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 lp_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 lp_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 lp_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 lp_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->depth = tex->depth0;
475 jit_tex->last_level = tex->last_level;
476 if (!lp_tex->dt) {
477 /* regular texture - setup array of mipmap level pointers */
478 int j;
479 for (j = 0; j <= tex->last_level; j++) {
480 jit_tex->data[j] =
481 (ubyte *) lp_tex->data + lp_tex->level_offset[j];
482 jit_tex->row_stride[j] = lp_tex->stride[j];
483 }
484 }
485 else {
486 /* display target texture/surface */
487 /*
488 * XXX: Where should this be unmapped?
489 */
490
491 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
492 struct sw_winsys *winsys = screen->winsys;
493 jit_tex->data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
494 PIPE_BUFFER_USAGE_CPU_READ);
495 jit_tex->row_stride[0] = lp_tex->stride[0];
496 assert(jit_tex->data[0]);
497 }
498
499 /* the scene references this texture */
500 {
501 struct lp_scene *scene = lp_setup_get_current_scene(setup);
502 lp_scene_texture_reference(scene, tex);
503 }
504 }
505 }
506
507 setup->dirty |= LP_SETUP_NEW_FS;
508 }
509
510
511 /**
512 * Is the given texture referenced by any scene?
513 * Note: we have to check all scenes including any scenes currently
514 * being rendered and the current scene being built.
515 */
516 unsigned
517 lp_setup_is_texture_referenced( const struct lp_setup_context *setup,
518 const struct pipe_texture *texture )
519 {
520 unsigned i;
521
522 /* check the render targets */
523 for (i = 0; i < setup->fb.nr_cbufs; i++) {
524 if (setup->fb.cbufs[i]->texture == texture)
525 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
526 }
527 if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {
528 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
529 }
530
531 /* check textures referenced by the scene */
532 for (i = 0; i < Elements(setup->scenes); i++) {
533 if (lp_scene_is_texture_referenced(setup->scenes[i], texture)) {
534 return PIPE_REFERENCED_FOR_READ;
535 }
536 }
537
538 return PIPE_UNREFERENCED;
539 }
540
541
542 /**
543 * Called by vbuf code when we're about to draw something.
544 */
545 void
546 lp_setup_update_state( struct lp_setup_context *setup )
547 {
548 struct lp_scene *scene = lp_setup_get_current_scene(setup);
549
550 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
551
552 assert(setup->fs.current.jit_function);
553
554 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
555 uint8_t *stored;
556 unsigned i, j;
557
558 stored = lp_scene_alloc_aligned(scene, 4 * 16, 16);
559
560 /* smear each blend color component across 16 ubyte elements */
561 for (i = 0; i < 4; ++i) {
562 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
563 for (j = 0; j < 16; ++j)
564 stored[i*16 + j] = c;
565 }
566
567 setup->blend_color.stored = stored;
568
569 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
570 setup->dirty |= LP_SETUP_NEW_FS;
571 }
572
573 if (setup->dirty & LP_SETUP_NEW_SCISSOR) {
574 float *stored;
575
576 stored = lp_scene_alloc_aligned(scene, 4 * sizeof(int32_t), 16);
577
578 stored[0] = (float) setup->scissor.current.minx;
579 stored[1] = (float) setup->scissor.current.miny;
580 stored[2] = (float) setup->scissor.current.maxx;
581 stored[3] = (float) setup->scissor.current.maxy;
582
583 setup->scissor.stored = stored;
584
585 setup->fs.current.jit_context.scissor_xmin = stored[0];
586 setup->fs.current.jit_context.scissor_ymin = stored[1];
587 setup->fs.current.jit_context.scissor_xmax = stored[2];
588 setup->fs.current.jit_context.scissor_ymax = stored[3];
589
590 setup->dirty |= LP_SETUP_NEW_FS;
591 }
592
593 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
594 struct pipe_buffer *buffer = setup->constants.current;
595
596 if(buffer) {
597 unsigned current_size = buffer->size;
598 const void *current_data = llvmpipe_buffer(buffer)->data;
599
600 /* TODO: copy only the actually used constants? */
601
602 if(setup->constants.stored_size != current_size ||
603 !setup->constants.stored_data ||
604 memcmp(setup->constants.stored_data,
605 current_data,
606 current_size) != 0) {
607 void *stored;
608
609 stored = lp_scene_alloc(scene, current_size);
610 if(stored) {
611 memcpy(stored,
612 current_data,
613 current_size);
614 setup->constants.stored_size = current_size;
615 setup->constants.stored_data = stored;
616 }
617 }
618 }
619 else {
620 setup->constants.stored_size = 0;
621 setup->constants.stored_data = NULL;
622 }
623
624 setup->fs.current.jit_context.constants = setup->constants.stored_data;
625 setup->dirty |= LP_SETUP_NEW_FS;
626 }
627
628
629 if(setup->dirty & LP_SETUP_NEW_FS) {
630 if(!setup->fs.stored ||
631 memcmp(setup->fs.stored,
632 &setup->fs.current,
633 sizeof setup->fs.current) != 0) {
634 /* The fs state that's been stored in the scene is different from
635 * the new, current state. So allocate a new lp_rast_state object
636 * and append it to the bin's setup data buffer.
637 */
638 struct lp_rast_state *stored =
639 (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);
640 if(stored) {
641 memcpy(stored,
642 &setup->fs.current,
643 sizeof setup->fs.current);
644 setup->fs.stored = stored;
645
646 /* put the state-set command into all bins */
647 lp_scene_bin_state_command( scene,
648 lp_rast_set_state,
649 lp_rast_arg_state(setup->fs.stored) );
650 }
651 }
652 }
653
654 setup->dirty = 0;
655
656 assert(setup->fs.stored);
657 }
658
659
660
661 /* Only caller is lp_setup_vbuf_destroy()
662 */
663 void
664 lp_setup_destroy( struct lp_setup_context *setup )
665 {
666 reset_context( setup );
667
668 pipe_buffer_reference(&setup->constants.current, NULL);
669
670 /* free the scenes in the 'empty' queue */
671 while (1) {
672 struct lp_scene *scene = lp_scene_dequeue(setup->empty_scenes, FALSE);
673 if (!scene)
674 break;
675 lp_scene_destroy(scene);
676 }
677
678 lp_rast_destroy( setup->rast );
679
680 FREE( setup );
681 }
682
683
684 /**
685 * Create a new primitive tiling engine. Plug it into the backend of
686 * the draw module. Currently also creates a rasterizer to use with
687 * it.
688 */
689 struct lp_setup_context *
690 lp_setup_create( struct pipe_context *pipe,
691 struct draw_context *draw )
692 {
693 unsigned i;
694 struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
695
696 if (!setup)
697 return NULL;
698
699 lp_setup_init_vbuf(setup);
700
701 setup->empty_scenes = lp_scene_queue_create();
702 if (!setup->empty_scenes)
703 goto fail;
704
705 /* XXX: move this to the screen and share between contexts:
706 */
707 setup->rast = lp_rast_create();
708 if (!setup->rast)
709 goto fail;
710
711 setup->vbuf = draw_vbuf_stage(draw, &setup->base);
712 if (!setup->vbuf)
713 goto fail;
714
715 draw_set_rasterize_stage(draw, setup->vbuf);
716 draw_set_render(draw, &setup->base);
717
718 /* create some empty scenes */
719 for (i = 0; i < MAX_SCENES; i++) {
720 setup->scenes[i] = lp_scene_create( pipe, setup->empty_scenes );
721
722 lp_scene_enqueue(setup->empty_scenes, setup->scenes[i]);
723 }
724
725 setup->triangle = first_triangle;
726 setup->line = first_line;
727 setup->point = first_point;
728
729 setup->dirty = ~0;
730
731 return setup;
732
733 fail:
734 if (setup->rast)
735 lp_rast_destroy( setup->rast );
736
737 if (setup->vbuf)
738 ;
739
740 if (setup->empty_scenes)
741 lp_scene_queue_destroy(setup->empty_scenes);
742
743 FREE(setup);
744 return NULL;
745 }
746