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