llvmpipe: bin state-change commands
[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_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_pack_color.h"
40 #include "lp_state.h"
41 #include "lp_buffer.h"
42 #include "lp_texture.h"
43 #include "lp_setup_context.h"
44
45 #define SETUP_DEBUG debug_printf
46
47 static void set_state( struct setup_context *, unsigned );
48
49 void lp_setup_new_cmd_block( struct cmd_block_list *list )
50 {
51 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
52 list->tail->next = block;
53 list->tail = block;
54 block->next = NULL;
55 block->count = 0;
56 }
57
58 void lp_setup_new_data_block( struct data_block_list *list )
59 {
60 struct data_block *block = MALLOC_STRUCT(data_block);
61 list->tail->next = block;
62 list->tail = block;
63 block->next = NULL;
64 block->used = 0;
65 }
66
67
68 static void
69 first_triangle( struct setup_context *setup,
70 const float (*v0)[4],
71 const float (*v1)[4],
72 const float (*v2)[4])
73 {
74 set_state( setup, SETUP_ACTIVE );
75 lp_setup_choose_triangle( setup );
76 setup->triangle( setup, v0, v1, v2 );
77 }
78
79 static void
80 first_line( struct setup_context *setup,
81 const float (*v0)[4],
82 const float (*v1)[4])
83 {
84 set_state( setup, SETUP_ACTIVE );
85 lp_setup_choose_line( setup );
86 setup->line( setup, v0, v1 );
87 }
88
89 static void
90 first_point( struct setup_context *setup,
91 const float (*v0)[4])
92 {
93 set_state( setup, SETUP_ACTIVE );
94 lp_setup_choose_point( setup );
95 setup->point( setup, v0 );
96 }
97
98 static void reset_context( struct setup_context *setup )
99 {
100 unsigned i, j;
101
102 SETUP_DEBUG("%s\n", __FUNCTION__);
103
104 /* Reset derived state */
105 setup->constants.stored_size = 0;
106 setup->constants.stored_data = NULL;
107 setup->fs.stored = NULL;
108 setup->dirty = ~0;
109
110 /* Free all but last binner command lists:
111 */
112 for (i = 0; i < setup->tiles_x; i++) {
113 for (j = 0; j < setup->tiles_y; j++) {
114 struct cmd_block_list *list = &setup->tile[i][j].commands;
115 struct cmd_block *block;
116 struct cmd_block *tmp;
117
118 for (block = list->head; block != list->tail; block = tmp) {
119 tmp = block->next;
120 FREE(block);
121 }
122
123 assert(list->tail->next == NULL);
124 list->head = list->tail;
125 list->head->count = 0;
126 }
127 }
128
129 /* Free all but last binned data block:
130 */
131 {
132 struct data_block_list *list = &setup->data;
133 struct data_block *block, *tmp;
134
135 for (block = list->head; block != list->tail; block = tmp) {
136 tmp = block->next;
137 FREE(block);
138 }
139
140 assert(list->tail->next == NULL);
141 list->head = list->tail;
142 list->head->used = 0;
143 }
144
145 /* Reset some state:
146 */
147 setup->clear.flags = 0;
148
149 /* Have an explicit "start-binning" call and get rid of this
150 * pointer twiddling?
151 */
152 setup->line = first_line;
153 setup->point = first_point;
154 setup->triangle = first_triangle;
155 }
156
157
158 /**
159 * Return last command in the bin
160 */
161 static lp_rast_cmd
162 lp_get_last_command( const struct cmd_bin *bin )
163 {
164 const struct cmd_block *tail = bin->commands.tail;
165 const unsigned i = tail->count;
166 if (i > 0)
167 return tail->cmd[i - 1];
168 else
169 return NULL;
170 }
171
172
173 /**
174 * Replace the arg of the last command in the bin.
175 */
176 static void
177 lp_replace_last_command_arg( struct cmd_bin *bin,
178 const union lp_rast_cmd_arg arg )
179 {
180 struct cmd_block *tail = bin->commands.tail;
181 const unsigned i = tail->count;
182 assert(i > 0);
183 tail->arg[i - 1] = arg;
184 }
185
186
187
188 /* Add a command to all active bins.
189 */
190 static void bin_everywhere( struct setup_context *setup,
191 lp_rast_cmd cmd,
192 const union lp_rast_cmd_arg arg )
193 {
194 unsigned i, j;
195 for (i = 0; i < setup->tiles_x; i++)
196 for (j = 0; j < setup->tiles_y; j++)
197 bin_command( &setup->tile[i][j], cmd, arg );
198 }
199
200
201 /**
202 * Put a state-change command into all bins.
203 * If we find that the last command in a bin was also a state-change
204 * command, we can simply replace that one with the new one.
205 */
206 static void
207 bin_state_command( struct setup_context *setup,
208 lp_rast_cmd cmd,
209 const union lp_rast_cmd_arg arg )
210 {
211 unsigned i, j;
212 for (i = 0; i < setup->tiles_x; i++) {
213 for (j = 0; j < setup->tiles_y; j++) {
214 struct cmd_bin *bin = &setup->tile[i][j];
215 lp_rast_cmd last_cmd = lp_get_last_command(bin);
216 if (last_cmd == cmd) {
217 lp_replace_last_command_arg(bin, arg);
218 }
219 else {
220 bin_command( bin, cmd, arg );
221 }
222 }
223 }
224 }
225
226
227 /** Rasterize commands for a single bin */
228 static void
229 rasterize_bin( struct lp_rasterizer *rast,
230 const struct cmd_bin *bin,
231 int x, int y)
232 {
233 const struct cmd_block_list *commands = &bin->commands;
234 struct cmd_block *block;
235 unsigned k;
236
237 lp_rast_start_tile( rast, x, y );
238
239 /* simply execute each of the commands in the block list */
240 for (block = commands->head; block; block = block->next) {
241 for (k = 0; k < block->count; k++) {
242 block->cmd[k]( rast, block->arg[k] );
243 }
244 }
245
246 lp_rast_end_tile( rast );
247 }
248
249
250 /** Rasterize all tile's bins */
251 static void
252 rasterize_bins( struct setup_context *setup,
253 boolean write_depth )
254 {
255 struct lp_rasterizer *rast = setup->rast;
256 unsigned i, j;
257
258 SETUP_DEBUG("%s\n", __FUNCTION__);
259
260 lp_rast_begin( rast,
261 setup->fb.cbuf,
262 setup->fb.zsbuf,
263 setup->fb.cbuf != NULL,
264 setup->fb.zsbuf != NULL && write_depth,
265 setup->fb.width,
266 setup->fb.height );
267
268 /* loop over tile bins, rasterize each */
269 for (i = 0; i < setup->tiles_x; i++) {
270 for (j = 0; j < setup->tiles_y; j++) {
271 rasterize_bin( rast, &setup->tile[i][j],
272 i * TILE_SIZE,
273 j * TILE_SIZE );
274 }
275 }
276
277 lp_rast_end( rast );
278
279 reset_context( setup );
280
281 SETUP_DEBUG("%s done \n", __FUNCTION__);
282 }
283
284
285
286 static void
287 begin_binning( struct setup_context *setup )
288 {
289 SETUP_DEBUG("%s\n", __FUNCTION__);
290
291 if (setup->fb.cbuf) {
292 if (setup->clear.flags & PIPE_CLEAR_COLOR)
293 bin_everywhere( setup,
294 lp_rast_clear_color,
295 setup->clear.color );
296 else
297 bin_everywhere( setup, lp_rast_load_color, lp_rast_arg_null() );
298 }
299
300 if (setup->fb.zsbuf) {
301 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
302 bin_everywhere( setup,
303 lp_rast_clear_zstencil,
304 setup->clear.zstencil );
305 else
306 bin_everywhere( setup, lp_rast_load_zstencil, lp_rast_arg_null() );
307 }
308
309 SETUP_DEBUG("%s done\n", __FUNCTION__);
310 }
311
312
313 /* This basically bins and then flushes any outstanding full-screen
314 * clears.
315 *
316 * TODO: fast path for fullscreen clears and no triangles.
317 */
318 static void
319 execute_clears( struct setup_context *setup )
320 {
321 SETUP_DEBUG("%s\n", __FUNCTION__);
322
323 begin_binning( setup );
324 rasterize_bins( setup, TRUE );
325 }
326
327
328 static void
329 set_state( struct setup_context *setup,
330 unsigned new_state )
331 {
332 unsigned old_state = setup->state;
333
334 if (old_state == new_state)
335 return;
336
337 SETUP_DEBUG("%s old %d new %d\n", __FUNCTION__, old_state, new_state);
338
339 switch (new_state) {
340 case SETUP_ACTIVE:
341 begin_binning( setup );
342 break;
343
344 case SETUP_CLEARED:
345 if (old_state == SETUP_ACTIVE) {
346 assert(0);
347 return;
348 }
349 break;
350
351 case SETUP_FLUSHED:
352 if (old_state == SETUP_CLEARED)
353 execute_clears( setup );
354 else
355 rasterize_bins( setup, TRUE );
356 break;
357 }
358
359 setup->state = new_state;
360 }
361
362
363 void
364 lp_setup_flush( struct setup_context *setup,
365 unsigned flags )
366 {
367 SETUP_DEBUG("%s\n", __FUNCTION__);
368
369 set_state( setup, SETUP_FLUSHED );
370 }
371
372
373 void
374 lp_setup_bind_framebuffer( struct setup_context *setup,
375 struct pipe_surface *color,
376 struct pipe_surface *zstencil )
377 {
378 SETUP_DEBUG("%s\n", __FUNCTION__);
379
380 set_state( setup, SETUP_FLUSHED );
381
382 pipe_surface_reference( &setup->fb.cbuf, color );
383 pipe_surface_reference( &setup->fb.zsbuf, zstencil );
384
385 if (!setup->fb.cbuf && !setup->fb.zsbuf) {
386 setup->fb.width = 0;
387 setup->fb.height = 0;
388 }
389 else if (!setup->fb.zsbuf) {
390 setup->fb.width = setup->fb.cbuf->width;
391 setup->fb.height = setup->fb.cbuf->height;
392 }
393 else if (!setup->fb.cbuf) {
394 setup->fb.width = setup->fb.zsbuf->width;
395 setup->fb.height = setup->fb.zsbuf->height;
396 }
397 else {
398 /* XXX: not sure what we're really supposed to do for
399 * mis-matched color & depth buffer sizes.
400 */
401 setup->fb.width = MIN2(setup->fb.cbuf->width,
402 setup->fb.zsbuf->width);
403 setup->fb.height = MIN2(setup->fb.cbuf->height,
404 setup->fb.zsbuf->height);
405 }
406
407 setup->tiles_x = align(setup->fb.width, TILE_SIZE) / TILE_SIZE;
408 setup->tiles_y = align(setup->fb.height, TILE_SIZE) / TILE_SIZE;
409 }
410
411
412 void
413 lp_setup_clear( struct setup_context *setup,
414 const float *color,
415 double depth,
416 unsigned stencil,
417 unsigned flags )
418 {
419 unsigned i;
420
421 SETUP_DEBUG("%s state %d\n", __FUNCTION__, setup->state);
422
423
424 if (flags & PIPE_CLEAR_COLOR) {
425 for (i = 0; i < 4; ++i)
426 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
427 }
428
429 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
430 setup->clear.zstencil.clear_zstencil =
431 util_pack_z_stencil(setup->fb.zsbuf->format,
432 depth,
433 stencil);
434 }
435
436 if (setup->state == SETUP_ACTIVE) {
437 /* Add the clear to existing bins. In the unusual case where
438 * both color and depth-stencilare being cleared, we could
439 * discard the currently binned scene and start again, but I
440 * don't see that as being a common usage.
441 */
442 if (flags & PIPE_CLEAR_COLOR)
443 bin_everywhere( setup,
444 lp_rast_clear_color,
445 setup->clear.color );
446
447 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
448 bin_everywhere( setup,
449 lp_rast_clear_zstencil,
450 setup->clear.zstencil );
451 }
452 else {
453 /* Put ourselves into the 'pre-clear' state, specifically to try
454 * and accumulate multiple clears to color and depth_stencil
455 * buffers which the app or state-tracker might issue
456 * separately.
457 */
458 set_state( setup, SETUP_CLEARED );
459
460 setup->clear.flags |= flags;
461 }
462 }
463
464
465
466 void
467 lp_setup_set_triangle_state( struct setup_context *setup,
468 unsigned cull_mode,
469 boolean ccw_is_frontface)
470 {
471 SETUP_DEBUG("%s\n", __FUNCTION__);
472
473 setup->ccw_is_frontface = ccw_is_frontface;
474 setup->cullmode = cull_mode;
475 setup->triangle = first_triangle;
476 }
477
478
479
480 void
481 lp_setup_set_fs_inputs( struct setup_context *setup,
482 const struct lp_shader_input *input,
483 unsigned nr )
484 {
485 SETUP_DEBUG("%s %p %u\n", __FUNCTION__, (void *) input, nr);
486
487 memcpy( setup->fs.input, input, nr * sizeof input[0] );
488 setup->fs.nr_inputs = nr;
489 }
490
491 void
492 lp_setup_set_fs( struct setup_context *setup,
493 struct lp_fragment_shader *fs )
494 {
495 SETUP_DEBUG("%s %p\n", __FUNCTION__, (void *) fs);
496 /* FIXME: reference count */
497
498 setup->fs.current.jit_function = fs ? fs->current->jit_function : NULL;
499 setup->dirty |= LP_SETUP_NEW_FS;
500 }
501
502 void
503 lp_setup_set_fs_constants(struct setup_context *setup,
504 struct pipe_buffer *buffer)
505 {
506 SETUP_DEBUG("%s %p\n", __FUNCTION__, (void *) buffer);
507
508 pipe_buffer_reference(&setup->constants.current, buffer);
509
510 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
511 }
512
513
514 void
515 lp_setup_set_alpha_ref_value( struct setup_context *setup,
516 float alpha_ref_value )
517 {
518 SETUP_DEBUG("%s %f\n", __FUNCTION__, alpha_ref_value);
519
520 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
521 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
522 setup->dirty |= LP_SETUP_NEW_FS;
523 }
524 }
525
526 void
527 lp_setup_set_blend_color( struct setup_context *setup,
528 const struct pipe_blend_color *blend_color )
529 {
530 SETUP_DEBUG("%s\n", __FUNCTION__);
531
532 assert(blend_color);
533
534 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
535 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
536 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
537 }
538 }
539
540 void
541 lp_setup_set_sampler_textures( struct setup_context *setup,
542 unsigned num, struct pipe_texture **texture)
543 {
544 struct pipe_texture *dummy;
545 unsigned i;
546
547 SETUP_DEBUG("%s\n", __FUNCTION__);
548
549
550 assert(num <= PIPE_MAX_SAMPLERS);
551
552 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
553 struct pipe_texture *tex = i < num ? texture[i] : NULL;
554
555 /* FIXME: hold on to the reference */
556 dummy = NULL;
557 pipe_texture_reference(&dummy, tex);
558
559 if(tex) {
560 struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
561 struct lp_jit_texture *jit_tex;
562 jit_tex = &setup->fs.current.jit_context.textures[i];
563 jit_tex->width = tex->width[0];
564 jit_tex->height = tex->height[0];
565 jit_tex->stride = lp_tex->stride[0];
566 if(!lp_tex->dt)
567 jit_tex->data = lp_tex->data;
568 else
569 /* FIXME: map the rendertarget */
570 assert(0);
571 }
572 }
573
574 setup->dirty |= LP_SETUP_NEW_FS;
575 }
576
577 boolean
578 lp_setup_is_texture_referenced( struct setup_context *setup,
579 const struct pipe_texture *texture )
580 {
581 /* FIXME */
582 return PIPE_UNREFERENCED;
583 }
584
585
586 static INLINE void
587 lp_setup_update_shader_state( struct setup_context *setup )
588 {
589 SETUP_DEBUG("%s\n", __FUNCTION__);
590
591 assert(setup->fs.current.jit_function);
592
593 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
594 uint8_t *stored;
595 unsigned i, j;
596
597 stored = get_data_aligned(&setup->data, 4 * 16, 16);
598
599 /* smear each blend color component across 16 ubyte elements */
600 for (i = 0; i < 4; ++i) {
601 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
602 for (j = 0; j < 16; ++j)
603 stored[i*16 + j] = c;
604 }
605
606 setup->blend_color.stored = stored;
607
608 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
609 setup->dirty |= LP_SETUP_NEW_FS;
610 }
611
612
613 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
614 struct pipe_buffer *buffer = setup->constants.current;
615
616 if(buffer) {
617 unsigned current_size = buffer->size;
618 const void *current_data = llvmpipe_buffer(buffer)->data;
619
620 /* TODO: copy only the actually used constants? */
621
622 if(setup->constants.stored_size != current_size ||
623 !setup->constants.stored_data ||
624 memcmp(setup->constants.stored_data,
625 current_data,
626 current_size) != 0) {
627 void *stored;
628
629 stored = get_data(&setup->data, current_size);
630 if(stored) {
631 memcpy(stored,
632 current_data,
633 current_size);
634 setup->constants.stored_size = current_size;
635 setup->constants.stored_data = stored;
636 }
637 }
638 }
639 else {
640 setup->constants.stored_size = 0;
641 setup->constants.stored_data = NULL;
642 }
643
644 setup->fs.current.jit_context.constants = setup->constants.stored_data;
645 setup->dirty |= LP_SETUP_NEW_FS;
646 }
647
648
649 if(setup->dirty & LP_SETUP_NEW_FS) {
650 if(!setup->fs.stored ||
651 memcmp(setup->fs.stored,
652 &setup->fs.current,
653 sizeof setup->fs.current) != 0) {
654 /* The fs state that's been stored in the bins is different from
655 * the new, current state. So allocate a new lp_rast_state object
656 * and append it to the bin's setup data buffer.
657 */
658 struct lp_rast_state *stored =
659 (struct lp_rast_state *) get_data(&setup->data, sizeof *stored);
660 if(stored) {
661 memcpy(stored,
662 &setup->fs.current,
663 sizeof setup->fs.current);
664 setup->fs.stored = stored;
665
666 /* put the state-set command into all bins */
667 bin_state_command( setup,
668 lp_rast_set_state,
669 lp_rast_arg_state(setup->fs.stored) );
670 }
671 }
672 }
673
674 setup->dirty = 0;
675
676 assert(setup->fs.stored);
677 }
678
679
680 /* Stubs for lines & points for now:
681 */
682 void
683 lp_setup_point(struct setup_context *setup,
684 const float (*v0)[4])
685 {
686 lp_setup_update_shader_state(setup);
687 setup->point( setup, v0 );
688 }
689
690 void
691 lp_setup_line(struct setup_context *setup,
692 const float (*v0)[4],
693 const float (*v1)[4])
694 {
695 lp_setup_update_shader_state(setup);
696 setup->line( setup, v0, v1 );
697 }
698
699 void
700 lp_setup_tri(struct setup_context *setup,
701 const float (*v0)[4],
702 const float (*v1)[4],
703 const float (*v2)[4])
704 {
705 SETUP_DEBUG("%s\n", __FUNCTION__);
706
707 lp_setup_update_shader_state(setup);
708 setup->triangle( setup, v0, v1, v2 );
709 }
710
711
712 void
713 lp_setup_destroy( struct setup_context *setup )
714 {
715 unsigned i, j;
716
717 reset_context( setup );
718
719 pipe_buffer_reference(&setup->constants.current, NULL);
720
721 for (i = 0; i < TILES_X; i++)
722 for (j = 0; j < TILES_Y; j++)
723 FREE(setup->tile[i][j].commands.head);
724
725 FREE(setup->data.head);
726
727 lp_rast_destroy( setup->rast );
728 FREE( setup );
729 }
730
731
732 /**
733 * Create a new primitive tiling engine. Currently also creates a
734 * rasterizer to use with it.
735 */
736 struct setup_context *
737 lp_setup_create( struct pipe_screen *screen )
738 {
739 struct setup_context *setup = CALLOC_STRUCT(setup_context);
740 unsigned i, j;
741
742 setup->rast = lp_rast_create( screen );
743 if (!setup->rast)
744 goto fail;
745
746 for (i = 0; i < TILES_X; i++)
747 for (j = 0; j < TILES_Y; j++)
748 setup->tile[i][j].commands.head =
749 setup->tile[i][j].commands.tail = CALLOC_STRUCT(cmd_block);
750
751 setup->data.head =
752 setup->data.tail = CALLOC_STRUCT(data_block);
753
754 setup->triangle = first_triangle;
755 setup->line = first_line;
756 setup->point = first_point;
757
758 setup->dirty = ~0;
759
760 return setup;
761
762 fail:
763 FREE(setup);
764 return NULL;
765 }
766