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