Merge remote branch 'upstream/gallium-0.2' into nouveau-gallium-0.2
[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_vbuf.h"
38 #include "draw_vs.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 #ifdef PIPE_ARCH_X86
68 if (!draw_vs_init( draw ))
69 goto fail;
70 #endif
71
72 return draw;
73
74 fail:
75 draw_destroy( draw );
76 return NULL;
77 }
78
79
80 void draw_destroy( struct draw_context *draw )
81 {
82 if (!draw)
83 return;
84
85
86
87 /* Not so fast -- we're just borrowing this at the moment.
88 *
89 if (draw->render)
90 draw->render->destroy( draw->render );
91 */
92
93 draw_pipeline_destroy( draw );
94 draw_pt_destroy( draw );
95 draw_vs_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 /**
110 * Register new primitive rasterization/rendering state.
111 * This causes the drawing pipeline to be rebuilt.
112 */
113 void draw_set_rasterizer_state( struct draw_context *draw,
114 const struct pipe_rasterizer_state *raster )
115 {
116 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
117
118 draw->rasterizer = raster;
119 draw->bypass_clipping =
120 ((draw->rasterizer && draw->rasterizer->bypass_clipping) ||
121 draw->driver.bypass_clipping);
122 }
123
124
125 void draw_set_driver_clipping( struct draw_context *draw,
126 boolean bypass_clipping )
127 {
128 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
129
130 draw->driver.bypass_clipping = bypass_clipping;
131 draw->bypass_clipping = (draw->rasterizer->bypass_clipping ||
132 draw->driver.bypass_clipping);
133 }
134
135
136 /**
137 * Plug in the primitive rendering/rasterization stage (which is the last
138 * stage in the drawing pipeline).
139 * This is provided by the device driver.
140 */
141 void draw_set_rasterize_stage( struct draw_context *draw,
142 struct draw_stage *stage )
143 {
144 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
145
146 draw->pipeline.rasterize = stage;
147 }
148
149
150 /**
151 * Set the draw module's clipping state.
152 */
153 void draw_set_clip_state( struct draw_context *draw,
154 const struct pipe_clip_state *clip )
155 {
156 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
157
158 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
159 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
160 draw->nr_planes = 6 + clip->nr;
161 }
162
163
164 /**
165 * Set the draw module's viewport state.
166 */
167 void draw_set_viewport_state( struct draw_context *draw,
168 const struct pipe_viewport_state *viewport )
169 {
170 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
171 draw->viewport = *viewport; /* struct copy */
172 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
173 viewport->scale[1] == 1.0f &&
174 viewport->scale[2] == 1.0f &&
175 viewport->scale[3] == 1.0f &&
176 viewport->translate[0] == 0.0f &&
177 viewport->translate[1] == 0.0f &&
178 viewport->translate[2] == 0.0f &&
179 viewport->translate[3] == 0.0f);
180
181 draw_vs_set_viewport( draw, viewport );
182 }
183
184
185
186 void
187 draw_set_vertex_buffers(struct draw_context *draw,
188 unsigned count,
189 const struct pipe_vertex_buffer *buffers)
190 {
191 assert(count <= PIPE_MAX_ATTRIBS);
192
193 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0]));
194 draw->pt.nr_vertex_buffers = count;
195 }
196
197
198 void
199 draw_set_vertex_elements(struct draw_context *draw,
200 unsigned count,
201 const struct pipe_vertex_element *elements)
202 {
203 assert(count <= PIPE_MAX_ATTRIBS);
204
205 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
206 draw->pt.nr_vertex_elements = count;
207 }
208
209
210 /**
211 * Tell drawing context where to find mapped vertex buffers.
212 */
213 void
214 draw_set_mapped_vertex_buffer(struct draw_context *draw,
215 unsigned attr, const void *buffer)
216 {
217 draw->pt.user.vbuffer[attr] = buffer;
218 }
219
220
221 void
222 draw_set_mapped_constant_buffer(struct draw_context *draw,
223 const void *buffer,
224 unsigned size )
225 {
226 draw->pt.user.constants = buffer;
227 draw_vs_set_constants( draw, (const float (*)[4])buffer, size );
228 }
229
230
231 /**
232 * Tells the draw module to draw points with triangles if their size
233 * is greater than this threshold.
234 */
235 void
236 draw_wide_point_threshold(struct draw_context *draw, float threshold)
237 {
238 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
239 draw->pipeline.wide_point_threshold = threshold;
240 }
241
242
243 /**
244 * Tells the draw module to draw lines with triangles if their width
245 * is greater than this threshold.
246 */
247 void
248 draw_wide_line_threshold(struct draw_context *draw, float threshold)
249 {
250 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
251 draw->pipeline.wide_line_threshold = threshold;
252 }
253
254
255 /**
256 * Tells the draw module whether or not to implement line stipple.
257 */
258 void
259 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
260 {
261 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
262 draw->pipeline.line_stipple = enable;
263 }
264
265
266 /**
267 * Tells draw module whether to convert points to quads for sprite mode.
268 */
269 void
270 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
271 {
272 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
273 draw->pipeline.point_sprite = enable;
274 }
275
276
277 /**
278 * Ask the draw module for the location/slot of the given vertex attribute in
279 * a post-transformed vertex.
280 *
281 * With this function, drivers that use the draw module should have no reason
282 * to track the current vertex shader.
283 *
284 * Note that the draw module may sometimes generate vertices with extra
285 * attributes (such as texcoords for AA lines). The driver can call this
286 * function to find those attributes.
287 *
288 * Zero is returned if the attribute is not found since this is
289 * a don't care / undefined situtation. Returning -1 would be a bit more
290 * work for the drivers.
291 */
292 int
293 draw_find_vs_output(const struct draw_context *draw,
294 uint semantic_name, uint semantic_index)
295 {
296 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
297 uint i;
298 for (i = 0; i < vs->info.num_outputs; i++) {
299 if (vs->info.output_semantic_name[i] == semantic_name &&
300 vs->info.output_semantic_index[i] == semantic_index)
301 return i;
302 }
303
304 /* XXX there may be more than one extra vertex attrib.
305 * For example, simulated gl_FragCoord and gl_PointCoord.
306 */
307 if (draw->extra_vp_outputs.semantic_name == semantic_name &&
308 draw->extra_vp_outputs.semantic_index == semantic_index) {
309 return draw->extra_vp_outputs.slot;
310 }
311 return 0;
312 }
313
314
315 /**
316 * Return number of vertex shader outputs.
317 */
318 uint
319 draw_num_vs_outputs(const struct draw_context *draw)
320 {
321 uint count = draw->vs.vertex_shader->info.num_outputs;
322 if (draw->extra_vp_outputs.slot > 0)
323 count++;
324 return count;
325 }
326
327
328
329 void draw_set_render( struct draw_context *draw,
330 struct vbuf_render *render )
331 {
332 draw->render = render;
333 }
334
335 void draw_set_edgeflags( struct draw_context *draw,
336 const unsigned *edgeflag )
337 {
338 draw->pt.user.edgeflag = edgeflag;
339 }
340
341
342
343
344 /**
345 * Tell the drawing context about the index/element buffer to use
346 * (ala glDrawElements)
347 * If no element buffer is to be used (i.e. glDrawArrays) then this
348 * should be called with eltSize=0 and elements=NULL.
349 *
350 * \param draw the drawing context
351 * \param eltSize size of each element (1, 2 or 4 bytes)
352 * \param elements the element buffer ptr
353 */
354 void
355 draw_set_mapped_element_buffer_range( struct draw_context *draw,
356 unsigned eltSize,
357 unsigned min_index,
358 unsigned max_index,
359 void *elements )
360 {
361 draw->pt.user.elts = elements;
362 draw->pt.user.eltSize = eltSize;
363 draw->pt.user.min_index = min_index;
364 draw->pt.user.max_index = max_index;
365 }
366
367
368 void
369 draw_set_mapped_element_buffer( struct draw_context *draw,
370 unsigned eltSize,
371 void *elements )
372 {
373 draw->pt.user.elts = elements;
374 draw->pt.user.eltSize = eltSize;
375 draw->pt.user.min_index = 0;
376 draw->pt.user.max_index = 0xffffffff;
377 }
378
379
380 /* Revamp me please:
381 */
382 void draw_do_flush( struct draw_context *draw, unsigned flags )
383 {
384 if (!draw->suspend_flushing)
385 {
386 assert(!draw->flushing); /* catch inadvertant recursion */
387
388 draw->flushing = TRUE;
389
390 draw_pipeline_flush( draw, flags );
391
392 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
393
394 draw->flushing = FALSE;
395 }
396 }