llvmpipe: checkpoint: plug in the new fencing 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_memory.h"
38 #include "util/u_pack_color.h"
39 #include "lp_bin.h"
40 #include "lp_bin_queue.h"
41 #include "lp_debug.h"
42 #include "lp_fence.h"
43 #include "lp_state.h"
44 #include "lp_buffer.h"
45 #include "lp_texture.h"
46 #include "lp_setup_context.h"
47
48
49 /** XXX temporary value, temporary here */
50 #define MAX_BINS 2
51
52
53 static void set_state( struct setup_context *, unsigned );
54
55
56 struct lp_bins *
57 lp_setup_get_current_bins(struct setup_context *setup)
58 {
59 if (!setup->bins) {
60 /* wait for a free/empty bin */
61 setup->bins = lp_bins_dequeue(setup->empty_bins);
62 if(0)lp_reset_bins( setup->bins ); /* XXX temporary? */
63
64 if (setup->fb) {
65 lp_bin_set_framebuffer_size(setup->bins,
66 setup->fb->width, setup->fb->height);
67 }
68 }
69 return setup->bins;
70 }
71
72
73 static void
74 first_triangle( struct setup_context *setup,
75 const float (*v0)[4],
76 const float (*v1)[4],
77 const float (*v2)[4])
78 {
79 set_state( setup, SETUP_ACTIVE );
80 lp_setup_choose_triangle( setup );
81 setup->triangle( setup, v0, v1, v2 );
82 }
83
84 static void
85 first_line( struct setup_context *setup,
86 const float (*v0)[4],
87 const float (*v1)[4])
88 {
89 set_state( setup, SETUP_ACTIVE );
90 lp_setup_choose_line( setup );
91 setup->line( setup, v0, v1 );
92 }
93
94 static void
95 first_point( struct setup_context *setup,
96 const float (*v0)[4])
97 {
98 set_state( setup, SETUP_ACTIVE );
99 lp_setup_choose_point( setup );
100 setup->point( setup, v0 );
101 }
102
103 static void reset_context( struct setup_context *setup )
104 {
105 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
106
107 /* Reset derived state */
108 setup->constants.stored_size = 0;
109 setup->constants.stored_data = NULL;
110 setup->fs.stored = NULL;
111 setup->dirty = ~0;
112
113 /* no current bin */
114 setup->bins = NULL;
115
116 /* Reset some state:
117 */
118 setup->clear.flags = 0;
119
120 /* Have an explicit "start-binning" call and get rid of this
121 * pointer twiddling?
122 */
123 setup->line = first_line;
124 setup->point = first_point;
125 setup->triangle = first_triangle;
126 }
127
128
129 /** Rasterize all tile's bins */
130 static void
131 rasterize_bins( struct setup_context *setup,
132 boolean write_depth )
133 {
134 struct lp_bins *bins = lp_setup_get_current_bins(setup);
135
136 lp_rasterize_bins(setup->rast,
137 bins,
138 setup->fb,
139 write_depth);
140
141 reset_context( setup );
142
143 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
144 }
145
146
147
148 static void
149 begin_binning( struct setup_context *setup )
150 {
151 struct lp_bins *bins = lp_setup_get_current_bins(setup);
152
153 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
154
155 if (setup->fb->cbufs[0]) {
156 if (setup->clear.flags & PIPE_CLEAR_COLOR)
157 lp_bin_everywhere( bins,
158 lp_rast_clear_color,
159 setup->clear.color );
160 else
161 lp_bin_everywhere( bins,
162 lp_rast_load_color,
163 lp_rast_arg_null() );
164 }
165
166 if (setup->fb->zsbuf) {
167 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
168 lp_bin_everywhere( bins,
169 lp_rast_clear_zstencil,
170 setup->clear.zstencil );
171 else
172 lp_bin_everywhere( bins,
173 lp_rast_load_zstencil,
174 lp_rast_arg_null() );
175 }
176
177 LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);
178 }
179
180
181 /* This basically bins and then flushes any outstanding full-screen
182 * clears.
183 *
184 * TODO: fast path for fullscreen clears and no triangles.
185 */
186 static void
187 execute_clears( struct setup_context *setup )
188 {
189 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
190
191 begin_binning( setup );
192 rasterize_bins( setup, TRUE );
193 }
194
195
196 static void
197 set_state( struct setup_context *setup,
198 unsigned new_state )
199 {
200 unsigned old_state = setup->state;
201
202 if (old_state == new_state)
203 return;
204
205 LP_DBG(DEBUG_SETUP, "%s old %d new %d\n", __FUNCTION__, old_state, new_state);
206
207 switch (new_state) {
208 case SETUP_ACTIVE:
209 begin_binning( setup );
210 break;
211
212 case SETUP_CLEARED:
213 if (old_state == SETUP_ACTIVE) {
214 assert(0);
215 return;
216 }
217 break;
218
219 case SETUP_FLUSHED:
220 if (old_state == SETUP_CLEARED)
221 execute_clears( setup );
222 else
223 rasterize_bins( setup, TRUE );
224 break;
225 }
226
227 setup->state = new_state;
228 }
229
230
231 void
232 lp_setup_flush( struct setup_context *setup,
233 unsigned flags )
234 {
235 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
236
237 set_state( setup, SETUP_FLUSHED );
238 }
239
240
241 void
242 lp_setup_bind_framebuffer( struct setup_context *setup,
243 const struct pipe_framebuffer_state *fb )
244 {
245 struct lp_bins *bins = lp_setup_get_current_bins(setup);
246
247 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
248
249 set_state( setup, SETUP_FLUSHED );
250
251 setup->fb = fb;
252
253 lp_bin_set_framebuffer_size(bins, setup->fb->width, setup->fb->height);
254 }
255
256
257 void
258 lp_setup_clear( struct setup_context *setup,
259 const float *color,
260 double depth,
261 unsigned stencil,
262 unsigned flags )
263 {
264 struct lp_bins *bins = lp_setup_get_current_bins(setup);
265 unsigned i;
266
267 LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);
268
269
270 if (flags & PIPE_CLEAR_COLOR) {
271 for (i = 0; i < 4; ++i)
272 setup->clear.color.clear_color[i] = float_to_ubyte(color[i]);
273 }
274
275 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
276 setup->clear.zstencil.clear_zstencil =
277 util_pack_z_stencil(setup->fb->zsbuf->format,
278 depth,
279 stencil);
280 }
281
282 if (setup->state == SETUP_ACTIVE) {
283 /* Add the clear to existing bins. In the unusual case where
284 * both color and depth-stencil are being cleared when there's
285 * already been some rendering, we could discard the currently
286 * binned scene and start again, but I don't see that as being
287 * a common usage.
288 */
289 if (flags & PIPE_CLEAR_COLOR)
290 lp_bin_everywhere( bins,
291 lp_rast_clear_color,
292 setup->clear.color );
293
294 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
295 lp_bin_everywhere( bins,
296 lp_rast_clear_zstencil,
297 setup->clear.zstencil );
298 }
299 else {
300 /* Put ourselves into the 'pre-clear' state, specifically to try
301 * and accumulate multiple clears to color and depth_stencil
302 * buffers which the app or state-tracker might issue
303 * separately.
304 */
305 set_state( setup, SETUP_CLEARED );
306
307 setup->clear.flags |= flags;
308 }
309 }
310
311
312 /**
313 * Emit a fence.
314 */
315 struct pipe_fence_handle *
316 lp_setup_fence( struct setup_context *setup )
317 {
318 struct lp_bins *bins = lp_setup_get_current_bins(setup);
319 const unsigned rank = lp_bin_get_num_bins( bins );
320 struct lp_fence *fence = lp_fence_create(rank);
321
322 LP_DBG(DEBUG_SETUP, "%s rank %u\n", __FUNCTION__, rank);
323
324 set_state( setup, SETUP_ACTIVE );
325
326 /* insert the fence into all command bins */
327 lp_bin_everywhere( bins,
328 lp_rast_fence,
329 lp_rast_arg_fence(fence) );
330
331 return (struct pipe_fence_handle *) fence;
332 }
333
334
335 void
336 lp_setup_set_triangle_state( struct setup_context *setup,
337 unsigned cull_mode,
338 boolean ccw_is_frontface)
339 {
340 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
341
342 setup->ccw_is_frontface = ccw_is_frontface;
343 setup->cullmode = cull_mode;
344 setup->triangle = first_triangle;
345 }
346
347
348
349 void
350 lp_setup_set_fs_inputs( struct setup_context *setup,
351 const struct lp_shader_input *input,
352 unsigned nr )
353 {
354 LP_DBG(DEBUG_SETUP, "%s %p %u\n", __FUNCTION__, (void *) input, nr);
355
356 memcpy( setup->fs.input, input, nr * sizeof input[0] );
357 setup->fs.nr_inputs = nr;
358 }
359
360 void
361 lp_setup_set_fs( struct setup_context *setup,
362 struct lp_fragment_shader *fs )
363 {
364 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) fs);
365 /* FIXME: reference count */
366
367 setup->fs.current.jit_function = fs ? fs->current->jit_function : NULL;
368 setup->dirty |= LP_SETUP_NEW_FS;
369 }
370
371 void
372 lp_setup_set_fs_constants(struct setup_context *setup,
373 struct pipe_buffer *buffer)
374 {
375 LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffer);
376
377 pipe_buffer_reference(&setup->constants.current, buffer);
378
379 setup->dirty |= LP_SETUP_NEW_CONSTANTS;
380 }
381
382
383 void
384 lp_setup_set_alpha_ref_value( struct setup_context *setup,
385 float alpha_ref_value )
386 {
387 LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);
388
389 if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {
390 setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;
391 setup->dirty |= LP_SETUP_NEW_FS;
392 }
393 }
394
395 void
396 lp_setup_set_blend_color( struct setup_context *setup,
397 const struct pipe_blend_color *blend_color )
398 {
399 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
400
401 assert(blend_color);
402
403 if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {
404 memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);
405 setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;
406 }
407 }
408
409 void
410 lp_setup_set_sampler_textures( struct setup_context *setup,
411 unsigned num, struct pipe_texture **texture)
412 {
413 struct pipe_texture *dummy;
414 unsigned i;
415
416 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
417
418
419 assert(num <= PIPE_MAX_SAMPLERS);
420
421 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
422 struct pipe_texture *tex = i < num ? texture[i] : NULL;
423
424 /* FIXME: hold on to the reference */
425 dummy = NULL;
426 pipe_texture_reference(&dummy, tex);
427
428 if(tex) {
429 struct llvmpipe_texture *lp_tex = llvmpipe_texture(tex);
430 struct lp_jit_texture *jit_tex;
431 jit_tex = &setup->fs.current.jit_context.textures[i];
432 jit_tex->width = tex->width[0];
433 jit_tex->height = tex->height[0];
434 jit_tex->stride = lp_tex->stride[0];
435 if(!lp_tex->dt)
436 jit_tex->data = lp_tex->data;
437 else
438 /* FIXME: map the rendertarget */
439 assert(0);
440 }
441 }
442
443 setup->dirty |= LP_SETUP_NEW_FS;
444 }
445
446 boolean
447 lp_setup_is_texture_referenced( struct setup_context *setup,
448 const struct pipe_texture *texture )
449 {
450 /* FIXME */
451 return PIPE_UNREFERENCED;
452 }
453
454
455 static INLINE void
456 lp_setup_update_shader_state( struct setup_context *setup )
457 {
458 struct lp_bins *bins = lp_setup_get_current_bins(setup);
459
460 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
461
462 assert(setup->fs.current.jit_function);
463
464 if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {
465 uint8_t *stored;
466 unsigned i, j;
467
468 stored = lp_bin_alloc_aligned(bins, 4 * 16, 16);
469
470 /* smear each blend color component across 16 ubyte elements */
471 for (i = 0; i < 4; ++i) {
472 uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);
473 for (j = 0; j < 16; ++j)
474 stored[i*16 + j] = c;
475 }
476
477 setup->blend_color.stored = stored;
478
479 setup->fs.current.jit_context.blend_color = setup->blend_color.stored;
480 setup->dirty |= LP_SETUP_NEW_FS;
481 }
482
483
484 if(setup->dirty & LP_SETUP_NEW_CONSTANTS) {
485 struct pipe_buffer *buffer = setup->constants.current;
486
487 if(buffer) {
488 unsigned current_size = buffer->size;
489 const void *current_data = llvmpipe_buffer(buffer)->data;
490
491 /* TODO: copy only the actually used constants? */
492
493 if(setup->constants.stored_size != current_size ||
494 !setup->constants.stored_data ||
495 memcmp(setup->constants.stored_data,
496 current_data,
497 current_size) != 0) {
498 void *stored;
499
500 stored = lp_bin_alloc(bins, current_size);
501 if(stored) {
502 memcpy(stored,
503 current_data,
504 current_size);
505 setup->constants.stored_size = current_size;
506 setup->constants.stored_data = stored;
507 }
508 }
509 }
510 else {
511 setup->constants.stored_size = 0;
512 setup->constants.stored_data = NULL;
513 }
514
515 setup->fs.current.jit_context.constants = setup->constants.stored_data;
516 setup->dirty |= LP_SETUP_NEW_FS;
517 }
518
519
520 if(setup->dirty & LP_SETUP_NEW_FS) {
521 if(!setup->fs.stored ||
522 memcmp(setup->fs.stored,
523 &setup->fs.current,
524 sizeof setup->fs.current) != 0) {
525 /* The fs state that's been stored in the bins is different from
526 * the new, current state. So allocate a new lp_rast_state object
527 * and append it to the bin's setup data buffer.
528 */
529 struct lp_rast_state *stored =
530 (struct lp_rast_state *) lp_bin_alloc(bins, sizeof *stored);
531 if(stored) {
532 memcpy(stored,
533 &setup->fs.current,
534 sizeof setup->fs.current);
535 setup->fs.stored = stored;
536
537 /* put the state-set command into all bins */
538 lp_bin_state_command( bins,
539 lp_rast_set_state,
540 lp_rast_arg_state(setup->fs.stored) );
541 }
542 }
543 }
544
545 setup->dirty = 0;
546
547 assert(setup->fs.stored);
548 }
549
550
551 /* Stubs for lines & points for now:
552 */
553 void
554 lp_setup_point(struct setup_context *setup,
555 const float (*v0)[4])
556 {
557 lp_setup_update_shader_state(setup);
558 setup->point( setup, v0 );
559 }
560
561 void
562 lp_setup_line(struct setup_context *setup,
563 const float (*v0)[4],
564 const float (*v1)[4])
565 {
566 lp_setup_update_shader_state(setup);
567 setup->line( setup, v0, v1 );
568 }
569
570 void
571 lp_setup_tri(struct setup_context *setup,
572 const float (*v0)[4],
573 const float (*v1)[4],
574 const float (*v2)[4])
575 {
576 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
577
578 lp_setup_update_shader_state(setup);
579 setup->triangle( setup, v0, v1, v2 );
580 }
581
582
583 void
584 lp_setup_destroy( struct setup_context *setup )
585 {
586 reset_context( setup );
587
588 pipe_buffer_reference(&setup->constants.current, NULL);
589
590 /* free the bins in the 'empty' queue */
591 while (lp_bins_queue_size(setup->empty_bins) > 0) {
592 struct lp_bins *bins = lp_bins_dequeue(setup->empty_bins);
593 if (!bins)
594 break;
595 lp_bins_destroy(bins);
596 }
597
598 lp_rast_destroy( setup->rast );
599
600 FREE( setup );
601 }
602
603
604 /**
605 * Create a new primitive tiling engine. Currently also creates a
606 * rasterizer to use with it.
607 */
608 struct setup_context *
609 lp_setup_create( struct pipe_screen *screen )
610 {
611 unsigned i;
612 struct setup_context *setup = CALLOC_STRUCT(setup_context);
613
614 if (!setup)
615 return NULL;
616
617 setup->empty_bins = lp_bins_queue_create();
618 if (!setup->empty_bins)
619 goto fail;
620
621 setup->rast = lp_rast_create( screen, setup->empty_bins );
622 if (!setup->rast)
623 goto fail;
624
625 /* create some empty bins */
626 for (i = 0; i < MAX_BINS; i++) {
627 struct lp_bins *bins = lp_bins_create();
628 lp_bins_enqueue(setup->empty_bins, bins);
629 }
630
631 setup->triangle = first_triangle;
632 setup->line = first_line;
633 setup->point = first_point;
634
635 setup->dirty = ~0;
636
637 return setup;
638
639 fail:
640 if (setup->empty_bins)
641 lp_bins_queue_destroy(setup->empty_bins);
642
643 FREE(setup);
644 return NULL;
645 }
646