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