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