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