llvmpipe: get lp_setup compiling
[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 "lp_setup_context.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "pipe/p_defines.h"
40
41 static void set_state( struct setup_context *, unsigned );
42
43 void lp_setup_new_cmd_block( struct cmd_block_list *list )
44 {
45 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
46 list->tail->next = block;
47 list->tail = block;
48 block->next = NULL;
49 block->count = 0;
50 }
51
52 void lp_setup_new_data_block( struct data_block_list *list )
53 {
54 struct data_block *block = MALLOC_STRUCT(data_block);
55 list->tail->next = block;
56 list->tail = block;
57 block->next = NULL;
58 block->used = 0;
59 }
60
61
62 static void
63 first_triangle( struct setup_context *setup,
64 const float (*v0)[4],
65 const float (*v1)[4],
66 const float (*v2)[4])
67 {
68 set_state( setup, SETUP_ACTIVE );
69 lp_setup_choose_triangle( setup );
70 setup->triangle( setup, v0, v1, v2 );
71 }
72
73 static void
74 first_line( struct setup_context *setup,
75 const float (*v0)[4],
76 const float (*v1)[4])
77 {
78 set_state( setup, SETUP_ACTIVE );
79 lp_setup_choose_line( setup );
80 setup->line( setup, v0, v1 );
81 }
82
83 static void
84 first_point( struct setup_context *setup,
85 const float (*v0)[4])
86 {
87 set_state( setup, SETUP_ACTIVE );
88 lp_setup_choose_point( setup );
89 setup->point( setup, v0 );
90 }
91
92 static void reset_context( struct setup_context *setup )
93 {
94 unsigned i, j;
95
96 /* Free binner command lists:
97 */
98 for (i = 0; i < setup->tiles_x; i++) {
99 for (j = 0; j < setup->tiles_y; j++) {
100 struct cmd_block_list *list = &setup->tile[i][j];
101 struct cmd_block *block;
102 struct cmd_block *tmp;
103
104 for (block = list->head; block != list->tail; block = tmp) {
105 tmp = block->next;
106 FREE(block);
107 }
108
109 list->head = list->tail;
110 }
111 }
112
113 /* Free binned data:
114 */
115 {
116 struct data_block_list *list = &setup->data;
117 struct data_block *block, *tmp;
118
119 for (block = list->head; block != list->tail; block = tmp) {
120 tmp = block->next;
121 FREE(block);
122 }
123
124 list->head = list->tail;
125 }
126
127 /* Reset some state:
128 */
129 setup->clear.flags = 0;
130
131 /* Have an explicit "start-binning" call and get rid of this
132 * pointer twiddling?
133 */
134 setup->line = first_line;
135 setup->point = first_point;
136 setup->triangle = first_triangle;
137 }
138
139
140
141
142 /* Add a command to all active bins.
143 */
144 static void bin_everywhere( struct setup_context *setup,
145 lp_rast_cmd cmd,
146 const union lp_rast_cmd_arg *arg )
147 {
148 unsigned i, j;
149 for (i = 0; i < setup->tiles_x; i++)
150 for (j = 0; j < setup->tiles_y; j++)
151 bin_cmd( &setup->tile[i][j], cmd, arg );
152 }
153
154
155 static void
156 rasterize_bins( struct setup_context *setup,
157 boolean write_depth )
158 {
159 struct lp_rasterizer *rast = setup->rast;
160 struct cmd_block *block;
161 unsigned i,j,k;
162
163 lp_rast_bind_color( rast,
164 setup->fb.cbuf,
165 TRUE ); /* WRITE */
166
167 lp_rast_bind_depth( rast,
168 setup->fb.zsbuf,
169 write_depth ); /* WRITE */
170
171 for (i = 0; i < setup->tiles_x; i++) {
172 for (j = 0; j < setup->tiles_y; j++) {
173
174 lp_rast_start_tile( rast,
175 i * TILESIZE,
176 j * TILESIZE );
177
178 for (block = setup->tile[i][j].head; block; block = block->next) {
179 for (k = 0; k < block->count; k++) {
180 block->cmd[k]( rast, block->arg[k] );
181 }
182 }
183
184 lp_rast_end_tile( rast );
185 }
186 }
187
188 reset_context( setup );
189 }
190
191
192
193 static void
194 begin_binning( struct setup_context *setup )
195 {
196 if (setup->fb.cbuf) {
197 if (setup->clear.flags & PIPE_CLEAR_COLOR)
198 bin_everywhere( setup,
199 lp_rast_clear_color,
200 &setup->clear.color );
201 else
202 bin_everywhere( setup,
203 lp_rast_load_color,
204 NULL );
205 }
206
207 if (setup->fb.zsbuf) {
208 if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL)
209 bin_everywhere( setup,
210 lp_rast_clear_zstencil,
211 &setup->clear.zstencil );
212 else
213 bin_everywhere( setup,
214 lp_rast_load_zstencil,
215 NULL );
216 }
217 }
218
219
220 /* This basically bins and then flushes any outstanding full-screen
221 * clears.
222 *
223 * TODO: fast path for fullscreen clears and no triangles.
224 */
225 static void
226 execute_clears( struct setup_context *setup )
227 {
228 begin_binning( setup );
229 rasterize_bins( setup, TRUE );
230 }
231
232
233 static void
234 set_state( struct setup_context *setup,
235 unsigned new_state )
236 {
237 unsigned old_state = setup->state;
238
239 if (old_state == new_state)
240 return;
241
242 switch (new_state) {
243 case SETUP_ACTIVE:
244 if (old_state == SETUP_FLUSHED)
245 begin_binning( setup );
246 break;
247
248 case SETUP_CLEARED:
249 if (old_state == SETUP_ACTIVE) {
250 assert(0);
251 return;
252 }
253 break;
254
255 case SETUP_FLUSHED:
256 if (old_state == SETUP_CLEARED)
257 execute_clears( setup );
258 else
259 rasterize_bins( setup, TRUE );
260 break;
261 }
262
263 setup->state = new_state;
264 }
265
266
267 void
268 lp_setup_flush( struct setup_context *setup,
269 unsigned flags )
270 {
271 set_state( setup, SETUP_FLUSHED );
272 }
273
274
275 void
276 lp_setup_bind_framebuffer( struct setup_context *setup,
277 struct pipe_surface *color,
278 struct pipe_surface *zstencil )
279 {
280 unsigned width, height;
281
282 set_state( setup, SETUP_FLUSHED );
283
284 pipe_surface_reference( &setup->fb.cbuf, color );
285 pipe_surface_reference( &setup->fb.zsbuf, zstencil );
286
287 width = MAX2( color->width, zstencil->width );
288 height = MAX2( color->height, zstencil->height );
289
290 setup->tiles_x = align( width, TILESIZE ) / TILESIZE;
291 setup->tiles_y = align( height, TILESIZE ) / TILESIZE;
292 }
293
294 void
295 lp_setup_clear( struct setup_context *setup,
296 const float *color,
297 double depth,
298 unsigned stencil,
299 unsigned flags )
300 {
301 if (setup->state == SETUP_ACTIVE) {
302 /* Add the clear to existing bins. In the unusual case where
303 * both color and depth-stencilare being cleared, we could
304 * discard the currently binned scene and start again, but I
305 * don't see that as being a common usage.
306 */
307 if (flags & PIPE_CLEAR_COLOR) {
308 union lp_rast_cmd_arg *arg = get_data( &setup->data, sizeof *arg );
309
310 util_pack_color(color,
311 setup->fb.cbuf->format,
312 &arg->clear_color );
313
314 bin_everywhere(setup, lp_rast_clear_color, arg );
315 }
316
317 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
318 union lp_rast_cmd_arg *arg = get_data( &setup->data, sizeof *arg );
319
320 arg->clear_zstencil =
321 util_pack_z_stencil(setup->fb.zsbuf->format,
322 depth,
323 stencil);
324
325 bin_everywhere(setup, lp_rast_clear_zstencil, arg );
326 }
327 }
328 else {
329 /* Put ourselves into the 'pre-clear' state, specifically to try
330 * and accumulate multiple clears to color and depth_stencil
331 * buffers which the app or state-tracker might issue
332 * separately.
333 */
334 set_state( setup, SETUP_CLEARED );
335
336 setup->clear.flags |= flags;
337
338 if (flags & PIPE_CLEAR_COLOR) {
339 util_pack_color(color,
340 setup->fb.cbuf->format,
341 &setup->clear.color.clear_color );
342 }
343
344 if (flags & PIPE_CLEAR_DEPTHSTENCIL) {
345 setup->clear.zstencil.clear_zstencil =
346 util_pack_z_stencil(setup->fb.zsbuf->format,
347 depth,
348 stencil);
349 }
350 }
351 }
352
353
354 void
355 lp_setup_set_fs_inputs( struct setup_context *setup,
356 const struct lp_shader_input *input,
357 unsigned nr )
358 {
359 memcpy( setup->fs.input, input, nr * sizeof input[0] );
360 setup->fs.nr_inputs = nr;
361 }
362
363 void
364 lp_setup_set_shader_state( struct setup_context *setup,
365 const struct lp_jit_context *jc )
366 {
367
368 }
369
370
371
372
373
374 /* Stubs for lines & points for now:
375 */
376 void
377 lp_setup_point(struct setup_context *setup,
378 const float (*v0)[4])
379 {
380 setup->point( setup, v0 );
381 }
382
383 void
384 lp_setup_line(struct setup_context *setup,
385 const float (*v0)[4],
386 const float (*v1)[4])
387 {
388 setup->line( setup, v0, v1 );
389 }
390
391 void
392 lp_setup_tri(struct setup_context *setup,
393 const float (*v0)[4],
394 const float (*v1)[4],
395 const float (*v2)[4])
396 {
397 setup->triangle( setup, v0, v1, v2 );
398 }
399
400
401 void
402 lp_setup_destroy( struct setup_context *setup )
403 {
404 lp_rast_destroy( setup->rast );
405 FREE( setup );
406 }
407
408
409 /**
410 * Create a new primitive tiling engine. Currently also creates a
411 * rasterizer to use with it.
412 */
413 struct setup_context *
414 lp_setup_create( void )
415 {
416 struct setup_context *setup = CALLOC_STRUCT(setup_context);
417 unsigned i, j;
418
419 setup->rast = lp_rast_create();
420 if (!setup->rast)
421 goto fail;
422
423 for (i = 0; i < TILES_X; i++)
424 for (j = 0; j < TILES_Y; j++)
425 setup->tile[i][j].head =
426 setup->tile[i][j].tail = CALLOC_STRUCT(cmd_block);
427
428 return setup;
429
430 fail:
431 FREE(setup);
432 return NULL;
433 }
434