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