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