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