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