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