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