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