Merge branch 'mesa_7_7_branch'
[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 "util/u_memory.h"
35 #include "util/u_math.h"
36 #include "draw_context.h"
37 #include "draw_vs.h"
38 #include "draw_gs.h"
39 #include "draw_pt.h"
40 #include "draw_pipe.h"
41
42
43 struct draw_context *draw_create( void )
44 {
45 struct draw_context *draw = CALLOC_STRUCT( draw_context );
46 if (draw == NULL)
47 goto fail;
48
49 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
50 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
51 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
52 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
53 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
54 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
55 draw->nr_planes = 6;
56
57
58 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
59
60
61 if (!draw_pipeline_init( draw ))
62 goto fail;
63
64 if (!draw_pt_init( draw ))
65 goto fail;
66
67 if (!draw_vs_init( draw ))
68 goto fail;
69
70 if (!draw_gs_init( draw ))
71 goto fail;
72
73 return draw;
74
75 fail:
76 draw_destroy( draw );
77 return NULL;
78 }
79
80
81 void draw_destroy( struct draw_context *draw )
82 {
83 if (!draw)
84 return;
85
86
87
88 /* Not so fast -- we're just borrowing this at the moment.
89 *
90 if (draw->render)
91 draw->render->destroy( draw->render );
92 */
93
94 draw_pipeline_destroy( draw );
95 draw_pt_destroy( draw );
96 draw_vs_destroy( draw );
97 draw_gs_destroy( draw );
98
99 FREE( draw );
100 }
101
102
103
104 void draw_flush( struct draw_context *draw )
105 {
106 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
107 }
108
109
110 /**
111 * Specify the Minimum Resolvable Depth factor for polygon offset.
112 * This factor potentially depends on the number of Z buffer bits,
113 * the rasterization algorithm and the arithmetic performed on Z
114 * values between vertex shading and rasterization. It will vary
115 * from one driver to another.
116 */
117 void draw_set_mrd(struct draw_context *draw, double mrd)
118 {
119 draw->mrd = mrd;
120 }
121
122
123 /**
124 * Register new primitive rasterization/rendering state.
125 * This causes the drawing pipeline to be rebuilt.
126 */
127 void draw_set_rasterizer_state( struct draw_context *draw,
128 const struct pipe_rasterizer_state *raster )
129 {
130 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
131
132 draw->rasterizer = raster;
133 draw->bypass_clipping =
134 ((draw->rasterizer && draw->rasterizer->bypass_vs_clip_and_viewport) ||
135 draw->driver.bypass_clipping);
136 }
137
138
139 void draw_set_driver_clipping( struct draw_context *draw,
140 boolean bypass_clipping )
141 {
142 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
143
144 draw->driver.bypass_clipping = bypass_clipping;
145 draw->bypass_clipping =
146 ((draw->rasterizer && draw->rasterizer->bypass_vs_clip_and_viewport) ||
147 draw->driver.bypass_clipping);
148 }
149
150
151 /**
152 * Plug in the primitive rendering/rasterization stage (which is the last
153 * stage in the drawing pipeline).
154 * This is provided by the device driver.
155 */
156 void draw_set_rasterize_stage( struct draw_context *draw,
157 struct draw_stage *stage )
158 {
159 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
160
161 draw->pipeline.rasterize = stage;
162 }
163
164
165 /**
166 * Set the draw module's clipping state.
167 */
168 void draw_set_clip_state( struct draw_context *draw,
169 const struct pipe_clip_state *clip )
170 {
171 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
172
173 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
174 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
175 draw->nr_planes = 6 + clip->nr;
176 }
177
178
179 /**
180 * Set the draw module's viewport state.
181 */
182 void draw_set_viewport_state( struct draw_context *draw,
183 const struct pipe_viewport_state *viewport )
184 {
185 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
186 draw->viewport = *viewport; /* struct copy */
187 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
188 viewport->scale[1] == 1.0f &&
189 viewport->scale[2] == 1.0f &&
190 viewport->scale[3] == 1.0f &&
191 viewport->translate[0] == 0.0f &&
192 viewport->translate[1] == 0.0f &&
193 viewport->translate[2] == 0.0f &&
194 viewport->translate[3] == 0.0f);
195
196 draw_vs_set_viewport( draw, viewport );
197 }
198
199
200
201 void
202 draw_set_vertex_buffers(struct draw_context *draw,
203 unsigned count,
204 const struct pipe_vertex_buffer *buffers)
205 {
206 assert(count <= PIPE_MAX_ATTRIBS);
207
208 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0]));
209 draw->pt.nr_vertex_buffers = count;
210 }
211
212
213 void
214 draw_set_vertex_elements(struct draw_context *draw,
215 unsigned count,
216 const struct pipe_vertex_element *elements)
217 {
218 assert(count <= PIPE_MAX_ATTRIBS);
219
220 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
221 draw->pt.nr_vertex_elements = count;
222 }
223
224
225 /**
226 * Tell drawing context where to find mapped vertex buffers.
227 */
228 void
229 draw_set_mapped_vertex_buffer(struct draw_context *draw,
230 unsigned attr, const void *buffer)
231 {
232 draw->pt.user.vbuffer[attr] = buffer;
233 }
234
235
236 void
237 draw_set_mapped_constant_buffer(struct draw_context *draw,
238 unsigned shader_type,
239 const void *buffer,
240 unsigned size )
241 {
242 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
243 shader_type == PIPE_SHADER_GEOMETRY);
244 if (shader_type == PIPE_SHADER_VERTEX) {
245 draw->pt.user.vs_constants = buffer;
246 draw_vs_set_constants( draw, (const float (*)[4])buffer, size );
247 } else if (shader_type == PIPE_SHADER_GEOMETRY) {
248 draw->pt.user.gs_constants = buffer;
249 draw_gs_set_constants( draw, (const float (*)[4])buffer, size );
250 }
251 }
252
253
254 /**
255 * Tells the draw module to draw points with triangles if their size
256 * is greater than this threshold.
257 */
258 void
259 draw_wide_point_threshold(struct draw_context *draw, float threshold)
260 {
261 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
262 draw->pipeline.wide_point_threshold = threshold;
263 }
264
265
266 /**
267 * Tells the draw module to draw lines with triangles if their width
268 * is greater than this threshold.
269 */
270 void
271 draw_wide_line_threshold(struct draw_context *draw, float threshold)
272 {
273 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
274 draw->pipeline.wide_line_threshold = threshold;
275 }
276
277
278 /**
279 * Tells the draw module whether or not to implement line stipple.
280 */
281 void
282 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
283 {
284 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
285 draw->pipeline.line_stipple = enable;
286 }
287
288
289 /**
290 * Tells draw module whether to convert points to quads for sprite mode.
291 */
292 void
293 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
294 {
295 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
296 draw->pipeline.point_sprite = enable;
297 }
298
299
300 void
301 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
302 {
303 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
304 draw->force_passthrough = enable;
305 }
306
307
308 /**
309 * Ask the draw module for the location/slot of the given vertex attribute in
310 * a post-transformed vertex.
311 *
312 * With this function, drivers that use the draw module should have no reason
313 * to track the current vertex/geometry shader.
314 *
315 * Note that the draw module may sometimes generate vertices with extra
316 * attributes (such as texcoords for AA lines). The driver can call this
317 * function to find those attributes.
318 *
319 * Zero is returned if the attribute is not found since this is
320 * a don't care / undefined situtation. Returning -1 would be a bit more
321 * work for the drivers.
322 */
323 int
324 draw_find_shader_output(const struct draw_context *draw,
325 uint semantic_name, uint semantic_index)
326 {
327 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
328 const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
329 uint i;
330 const struct tgsi_shader_info *info = &vs->info;
331
332 if (gs)
333 info = &gs->info;
334
335 for (i = 0; i < info->num_outputs; i++) {
336 if (info->output_semantic_name[i] == semantic_name &&
337 info->output_semantic_index[i] == semantic_index)
338 return i;
339 }
340
341 /* XXX there may be more than one extra vertex attrib.
342 * For example, simulated gl_FragCoord and gl_PointCoord.
343 */
344 if (draw->extra_shader_outputs.semantic_name == semantic_name &&
345 draw->extra_shader_outputs.semantic_index == semantic_index) {
346 return draw->extra_shader_outputs.slot;
347 }
348
349 return 0;
350 }
351
352
353 /**
354 * Return number of the shader outputs.
355 *
356 * If geometry shader is present, its output will be returned,
357 * if not vertex shader is used.
358 */
359 uint
360 draw_num_shader_outputs(const struct draw_context *draw)
361 {
362 uint count = draw->vs.vertex_shader->info.num_outputs;
363
364 /* if geometry shader is present, its outputs go to te
365 * driver, not the vertex shaders */
366 if (draw->gs.geometry_shader)
367 count = draw->gs.geometry_shader->info.num_outputs;
368
369 if (draw->extra_shader_outputs.slot > 0)
370 count++;
371 return count;
372 }
373
374
375 /**
376 * Provide TGSI sampler objects for vertex/geometry shaders that use texture fetches.
377 * This might only be used by software drivers for the time being.
378 */
379 void
380 draw_texture_samplers(struct draw_context *draw,
381 uint num_samplers,
382 struct tgsi_sampler **samplers)
383 {
384 draw->vs.num_samplers = num_samplers;
385 draw->vs.samplers = samplers;
386 draw->gs.num_samplers = num_samplers;
387 draw->gs.samplers = samplers;
388 }
389
390
391
392
393 void draw_set_render( struct draw_context *draw,
394 struct vbuf_render *render )
395 {
396 draw->render = render;
397 }
398
399
400
401 /**
402 * Tell the drawing context about the index/element buffer to use
403 * (ala glDrawElements)
404 * If no element buffer is to be used (i.e. glDrawArrays) then this
405 * should be called with eltSize=0 and elements=NULL.
406 *
407 * \param draw the drawing context
408 * \param eltSize size of each element (1, 2 or 4 bytes)
409 * \param elements the element buffer ptr
410 */
411 void
412 draw_set_mapped_element_buffer_range( struct draw_context *draw,
413 unsigned eltSize,
414 unsigned min_index,
415 unsigned max_index,
416 const void *elements )
417 {
418 draw->pt.user.elts = elements;
419 draw->pt.user.eltSize = eltSize;
420 draw->pt.user.min_index = min_index;
421 draw->pt.user.max_index = max_index;
422 }
423
424
425 void
426 draw_set_mapped_element_buffer( struct draw_context *draw,
427 unsigned eltSize,
428 const void *elements )
429 {
430 draw->pt.user.elts = elements;
431 draw->pt.user.eltSize = eltSize;
432 draw->pt.user.min_index = 0;
433 draw->pt.user.max_index = 0xffffffff;
434 }
435
436
437 /* Revamp me please:
438 */
439 void draw_do_flush( struct draw_context *draw, unsigned flags )
440 {
441 if (!draw->suspend_flushing)
442 {
443 assert(!draw->flushing); /* catch inadvertant recursion */
444
445 draw->flushing = TRUE;
446
447 draw_pipeline_flush( draw, flags );
448
449 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
450
451 draw->flushing = FALSE;
452 }
453 }
454
455
456 int draw_current_shader_outputs(struct draw_context *draw)
457 {
458 if (draw->gs.geometry_shader)
459 return draw->gs.num_gs_outputs;
460 return draw->vs.num_vs_outputs;
461 }
462
463 int draw_current_shader_position_output(struct draw_context *draw)
464 {
465 if (draw->gs.geometry_shader)
466 return draw->gs.position_output;
467 return draw->vs.position_output;
468 }