use the new macro
[mesa.git] / src / gallium / auxiliary / draw / draw_context.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 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "pipe/p_util.h"
35 #include "draw_context.h"
36 #include "draw_private.h"
37 #include "draw_vbuf.h"
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43 if (draw == NULL)
44 goto fail;
45
46 #if defined(__i386__) || defined(__386__)
47 draw->use_sse = GETENV( "GALLIUM_NOSSE" ) == NULL;
48 #else
49 draw->use_sse = FALSE;
50 #endif
51
52 draw->use_pt_shaders = GETENV( "GALLIUM_PT_SHADERS" ) != NULL;
53
54 /* create pipeline stages */
55 draw->pipeline.wide_line = draw_wide_line_stage( draw );
56 draw->pipeline.wide_point = draw_wide_point_stage( draw );
57 draw->pipeline.stipple = draw_stipple_stage( draw );
58 draw->pipeline.unfilled = draw_unfilled_stage( draw );
59 draw->pipeline.twoside = draw_twoside_stage( draw );
60 draw->pipeline.offset = draw_offset_stage( draw );
61 draw->pipeline.clip = draw_clip_stage( draw );
62 draw->pipeline.flatshade = draw_flatshade_stage( draw );
63 draw->pipeline.cull = draw_cull_stage( draw );
64 draw->pipeline.validate = draw_validate_stage( draw );
65 draw->pipeline.first = draw->pipeline.validate;
66
67 if (!draw->pipeline.wide_line ||
68 !draw->pipeline.wide_point ||
69 !draw->pipeline.stipple ||
70 !draw->pipeline.unfilled ||
71 !draw->pipeline.twoside ||
72 !draw->pipeline.offset ||
73 !draw->pipeline.clip ||
74 !draw->pipeline.flatshade ||
75 !draw->pipeline.cull ||
76 !draw->pipeline.validate)
77 goto fail;
78
79
80 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
81 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
82 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
83 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
84 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
85 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
86 draw->nr_planes = 6;
87
88 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
89 */
90 {
91 char *tmp = align_malloc(VS_QUEUE_LENGTH * MAX_VERTEX_ALLOCATION, 16);
92 if (!tmp)
93 goto fail;
94
95 draw->vs.vertex_cache = tmp;
96 }
97
98 draw->shader_queue_flush = draw_vertex_shader_queue_flush;
99
100 /* these defaults are oriented toward the needs of softpipe */
101 draw->wide_point_threshold = 1000000.0; /* infinity */
102 draw->wide_line_threshold = 1.0;
103 draw->line_stipple = TRUE;
104 draw->point_sprite = TRUE;
105
106 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
107
108 draw_vertex_cache_invalidate( draw );
109 draw_set_mapped_element_buffer( draw, 0, NULL );
110
111 if (!draw_pt_init( draw ))
112 goto fail;
113
114 return draw;
115
116 fail:
117 draw_destroy( draw );
118 return NULL;
119 }
120
121
122 void draw_destroy( struct draw_context *draw )
123 {
124 if (!draw)
125 return;
126
127 if (draw->pipeline.wide_line)
128 draw->pipeline.wide_line->destroy( draw->pipeline.wide_line );
129 if (draw->pipeline.wide_point)
130 draw->pipeline.wide_point->destroy( draw->pipeline.wide_point );
131 if (draw->pipeline.stipple)
132 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
133 if (draw->pipeline.unfilled)
134 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
135 if (draw->pipeline.twoside)
136 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
137 if (draw->pipeline.offset)
138 draw->pipeline.offset->destroy( draw->pipeline.offset );
139 if (draw->pipeline.clip)
140 draw->pipeline.clip->destroy( draw->pipeline.clip );
141 if (draw->pipeline.flatshade)
142 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
143 if (draw->pipeline.cull)
144 draw->pipeline.cull->destroy( draw->pipeline.cull );
145 if (draw->pipeline.validate)
146 draw->pipeline.validate->destroy( draw->pipeline.validate );
147 if (draw->pipeline.aaline)
148 draw->pipeline.aaline->destroy( draw->pipeline.aaline );
149 if (draw->pipeline.aapoint)
150 draw->pipeline.aapoint->destroy( draw->pipeline.aapoint );
151 if (draw->pipeline.pstipple)
152 draw->pipeline.pstipple->destroy( draw->pipeline.pstipple );
153 if (draw->pipeline.rasterize)
154 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
155
156 tgsi_exec_machine_free_data(&draw->machine);
157
158 if (draw->vs.vertex_cache)
159 align_free( draw->vs.vertex_cache ); /* Frees all the vertices. */
160
161 /* Not so fast -- we're just borrowing this at the moment.
162 *
163 if (draw->render)
164 draw->render->destroy( draw->render );
165 */
166
167 draw_pt_destroy( draw );
168
169 FREE( draw );
170 }
171
172
173
174 void draw_flush( struct draw_context *draw )
175 {
176 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
177 }
178
179
180
181 /**
182 * Register new primitive rasterization/rendering state.
183 * This causes the drawing pipeline to be rebuilt.
184 */
185 void draw_set_rasterizer_state( struct draw_context *draw,
186 const struct pipe_rasterizer_state *raster )
187 {
188 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
189
190 draw->rasterizer = raster;
191 }
192
193
194 /**
195 * Plug in the primitive rendering/rasterization stage (which is the last
196 * stage in the drawing pipeline).
197 * This is provided by the device driver.
198 */
199 void draw_set_rasterize_stage( struct draw_context *draw,
200 struct draw_stage *stage )
201 {
202 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
203
204 draw->pipeline.rasterize = stage;
205 }
206
207
208 /**
209 * Set the draw module's clipping state.
210 */
211 void draw_set_clip_state( struct draw_context *draw,
212 const struct pipe_clip_state *clip )
213 {
214 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
215
216 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
217 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
218 draw->nr_planes = 6 + clip->nr;
219 }
220
221
222 /**
223 * Set the draw module's viewport state.
224 */
225 void draw_set_viewport_state( struct draw_context *draw,
226 const struct pipe_viewport_state *viewport )
227 {
228 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
229 draw->viewport = *viewport; /* struct copy */
230 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
231 viewport->scale[1] == 1.0f &&
232 viewport->scale[2] == 1.0f &&
233 viewport->scale[3] == 1.0f &&
234 viewport->translate[0] == 0.0f &&
235 viewport->translate[1] == 0.0f &&
236 viewport->translate[2] == 0.0f &&
237 viewport->translate[3] == 0.0f);
238 }
239
240
241
242 void
243 draw_set_vertex_buffers(struct draw_context *draw,
244 unsigned count,
245 const struct pipe_vertex_buffer *buffers)
246 {
247 assert(count <= PIPE_MAX_ATTRIBS);
248
249 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
250
251 memcpy(draw->vertex_buffer, buffers, count * sizeof(buffers[0]));
252 }
253
254
255 void
256 draw_set_vertex_elements(struct draw_context *draw,
257 unsigned count,
258 const struct pipe_vertex_element *elements)
259 {
260 assert(count <= PIPE_MAX_ATTRIBS);
261
262 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
263
264 memcpy(draw->vertex_element, elements, count * sizeof(elements[0]));
265 }
266
267
268 /**
269 * Tell drawing context where to find mapped vertex buffers.
270 */
271 void
272 draw_set_mapped_vertex_buffer(struct draw_context *draw,
273 unsigned attr, const void *buffer)
274 {
275 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
276 draw->user.vbuffer[attr] = buffer;
277 }
278
279
280 void
281 draw_set_mapped_constant_buffer(struct draw_context *draw,
282 const void *buffer)
283 {
284 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
285 draw->user.constants = buffer;
286 }
287
288
289 /**
290 * Tells the draw module to draw points with triangles if their size
291 * is greater than this threshold.
292 */
293 void
294 draw_wide_point_threshold(struct draw_context *draw, float threshold)
295 {
296 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
297 draw->wide_point_threshold = threshold;
298 }
299
300
301 /**
302 * Tells the draw module to draw lines with triangles if their width
303 * is greater than this threshold.
304 */
305 void
306 draw_wide_line_threshold(struct draw_context *draw, float threshold)
307 {
308 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
309 draw->wide_line_threshold = threshold;
310 }
311
312
313 /**
314 * Tells the draw module whether or not to implement line stipple.
315 */
316 void
317 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
318 {
319 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
320 draw->line_stipple = enable;
321 }
322
323
324 /**
325 * Tells draw module whether to convert points to quads for sprite mode.
326 */
327 void
328 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
329 {
330 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
331 draw->point_sprite = enable;
332 }
333
334
335 /**
336 * Ask the draw module for the location/slot of the given vertex attribute in
337 * a post-transformed vertex.
338 *
339 * With this function, drivers that use the draw module should have no reason
340 * to track the current vertex shader.
341 *
342 * Note that the draw module may sometimes generate vertices with extra
343 * attributes (such as texcoords for AA lines). The driver can call this
344 * function to find those attributes.
345 *
346 * Zero is returned if the attribute is not found since this is
347 * a don't care / undefined situtation. Returning -1 would be a bit more
348 * work for the drivers.
349 */
350 int
351 draw_find_vs_output(struct draw_context *draw,
352 uint semantic_name, uint semantic_index)
353 {
354 const struct draw_vertex_shader *vs = draw->vertex_shader;
355 uint i;
356 for (i = 0; i < vs->info.num_outputs; i++) {
357 if (vs->info.output_semantic_name[i] == semantic_name &&
358 vs->info.output_semantic_index[i] == semantic_index)
359 return i;
360 }
361
362 /* XXX there may be more than one extra vertex attrib.
363 * For example, simulated gl_FragCoord and gl_PointCoord.
364 */
365 if (draw->extra_vp_outputs.semantic_name == semantic_name &&
366 draw->extra_vp_outputs.semantic_index == semantic_index) {
367 return draw->extra_vp_outputs.slot;
368 }
369 return 0;
370 }
371
372
373 /**
374 * Return number of vertex shader outputs.
375 */
376 uint
377 draw_num_vs_outputs(struct draw_context *draw)
378 {
379 uint count = draw->vertex_shader->info.num_outputs;
380 if (draw->extra_vp_outputs.slot > 0)
381 count++;
382 return count;
383 }
384
385
386 /**
387 * Allocate space for temporary post-transform vertices, such as for clipping.
388 */
389 void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr )
390 {
391 assert(!stage->tmp);
392
393 stage->nr_tmps = nr;
394
395 if (nr) {
396 ubyte *store = (ubyte *) MALLOC( MAX_VERTEX_SIZE * nr );
397 unsigned i;
398
399 stage->tmp = (struct vertex_header **) MALLOC( sizeof(struct vertex_header *) * nr );
400
401 for (i = 0; i < nr; i++)
402 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
403 }
404 }
405
406
407 void draw_free_temp_verts( struct draw_stage *stage )
408 {
409 if (stage->tmp) {
410 FREE( stage->tmp[0] );
411 FREE( stage->tmp );
412 stage->tmp = NULL;
413 }
414 }
415
416
417 boolean draw_use_sse(struct draw_context *draw)
418 {
419 return (boolean) draw->use_sse;
420 }
421
422
423 void draw_reset_vertex_ids(struct draw_context *draw)
424 {
425 struct draw_stage *stage = draw->pipeline.first;
426
427 while (stage) {
428 unsigned i;
429
430 for (i = 0; i < stage->nr_tmps; i++)
431 stage->tmp[i]->vertex_id = UNDEFINED_VERTEX_ID;
432
433 stage = stage->next;
434 }
435
436 draw_vertex_cache_reset_vertex_ids(draw); /* going away soon */
437 draw_pt_reset_vertex_ids(draw);
438 }
439
440
441 void draw_set_render( struct draw_context *draw,
442 struct vbuf_render *render )
443 {
444 draw->render = render;
445 }
446
447 void draw_set_edgeflags( struct draw_context *draw,
448 const unsigned *edgeflag )
449 {
450 draw->user.edgeflag = edgeflag;
451 }
452
453
454 boolean draw_get_edgeflag( struct draw_context *draw,
455 unsigned idx )
456 {
457 if (draw->user.edgeflag)
458 return (draw->user.edgeflag[idx/32] & (1 << (idx%32))) != 0;
459 else
460 return 1;
461 }