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