draw_context.c revision 507fbe2d327efb8d608ce8e07436b97321560808
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
42struct draw_context *draw_create( void )
43{
44   struct draw_context *draw = CALLOC_STRUCT( draw_context );
45   if (draw == NULL)
46      goto fail;
47
48#if defined(__i386__) || defined(__386__)
49   draw->use_sse = GETENV( "GALLIUM_NOSSE" ) == NULL;
50#else
51   draw->use_sse = FALSE;
52#endif
53
54   ASSIGN_4V( draw->plane[0], -1,  0,  0, 1 );
55   ASSIGN_4V( draw->plane[1],  1,  0,  0, 1 );
56   ASSIGN_4V( draw->plane[2],  0, -1,  0, 1 );
57   ASSIGN_4V( draw->plane[3],  0,  1,  0, 1 );
58   ASSIGN_4V( draw->plane[4],  0,  0,  1, 1 ); /* yes these are correct */
59   ASSIGN_4V( draw->plane[5],  0,  0, -1, 1 ); /* mesa's a bit wonky */
60   draw->nr_planes = 6;
61
62
63   draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
64
65   draw_set_mapped_element_buffer( draw, 0, NULL );
66
67   tgsi_exec_machine_init(&draw->machine);
68
69   /* FIXME: give this machine thing a proper constructor:
70    */
71   draw->machine.Inputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
72   draw->machine.Outputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
73
74   if (!draw_pipeline_init( draw ))
75      goto fail;
76
77   if (!draw_pt_init( draw ))
78      goto fail;
79
80   return draw;
81
82fail:
83   draw_destroy( draw );
84   return NULL;
85}
86
87
88void draw_destroy( struct draw_context *draw )
89{
90   if (!draw)
91      return;
92
93
94   if (draw->machine.Inputs)
95      align_free(draw->machine.Inputs);
96
97   if (draw->machine.Outputs)
98      align_free(draw->machine.Outputs);
99
100   tgsi_exec_machine_free_data(&draw->machine);
101
102   /* Not so fast -- we're just borrowing this at the moment.
103    *
104   if (draw->render)
105      draw->render->destroy( draw->render );
106   */
107
108   draw_pipeline_destroy( draw );
109   draw_pt_destroy( draw );
110
111   FREE( draw );
112}
113
114
115
116void draw_flush( struct draw_context *draw )
117{
118   draw_do_flush( draw, DRAW_FLUSH_BACKEND );
119}
120
121
122
123/**
124 * Register new primitive rasterization/rendering state.
125 * This causes the drawing pipeline to be rebuilt.
126 */
127void draw_set_rasterizer_state( struct draw_context *draw,
128                                const struct pipe_rasterizer_state *raster )
129{
130   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
131
132   draw->rasterizer = raster;
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 */
141void 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 */
153void 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 */
167void 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
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->vertex_buffer, buffers, count * sizeof(buffers[0]));
192   draw->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->vertex_element, elements, count * sizeof(elements[0]));
204   draw->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->user.vbuffer[attr] = buffer;
216}
217
218
219void
220draw_set_mapped_constant_buffer(struct draw_context *draw,
221                                const void *buffer)
222{
223   draw->user.constants = buffer;
224}
225
226
227/**
228 * Tells the draw module to draw points with triangles if their size
229 * is greater than this threshold.
230 */
231void
232draw_wide_point_threshold(struct draw_context *draw, float threshold)
233{
234   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
235   draw->pipeline.wide_point_threshold = threshold;
236}
237
238
239/**
240 * Tells the draw module to draw lines with triangles if their width
241 * is greater than this threshold.
242 */
243void
244draw_wide_line_threshold(struct draw_context *draw, float threshold)
245{
246   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
247   draw->pipeline.wide_line_threshold = threshold;
248}
249
250
251/**
252 * Tells the draw module whether or not to implement line stipple.
253 */
254void
255draw_enable_line_stipple(struct draw_context *draw, boolean enable)
256{
257   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
258   draw->pipeline.line_stipple = enable;
259}
260
261
262/**
263 * Tells draw module whether to convert points to quads for sprite mode.
264 */
265void
266draw_enable_point_sprites(struct draw_context *draw, boolean enable)
267{
268   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
269   draw->pipeline.point_sprite = enable;
270}
271
272
273/**
274 * Ask the draw module for the location/slot of the given vertex attribute in
275 * a post-transformed vertex.
276 *
277 * With this function, drivers that use the draw module should have no reason
278 * to track the current vertex shader.
279 *
280 * Note that the draw module may sometimes generate vertices with extra
281 * attributes (such as texcoords for AA lines).  The driver can call this
282 * function to find those attributes.
283 *
284 * Zero is returned if the attribute is not found since this is
285 * a don't care / undefined situtation.  Returning -1 would be a bit more
286 * work for the drivers.
287 */
288int
289draw_find_vs_output(struct draw_context *draw,
290                    uint semantic_name, uint semantic_index)
291{
292   const struct draw_vertex_shader *vs = draw->vertex_shader;
293   uint i;
294   for (i = 0; i < vs->info.num_outputs; i++) {
295      if (vs->info.output_semantic_name[i] == semantic_name &&
296          vs->info.output_semantic_index[i] == semantic_index)
297         return i;
298   }
299
300   /* XXX there may be more than one extra vertex attrib.
301    * For example, simulated gl_FragCoord and gl_PointCoord.
302    */
303   if (draw->extra_vp_outputs.semantic_name == semantic_name &&
304       draw->extra_vp_outputs.semantic_index == semantic_index) {
305      return draw->extra_vp_outputs.slot;
306   }
307   return 0;
308}
309
310
311/**
312 * Return number of vertex shader outputs.
313 */
314uint
315draw_num_vs_outputs(struct draw_context *draw)
316{
317   uint count = draw->vertex_shader->info.num_outputs;
318   if (draw->extra_vp_outputs.slot > 0)
319      count++;
320   return count;
321}
322
323
324
325boolean draw_use_sse(struct draw_context *draw)
326{
327   return (boolean) draw->use_sse;
328}
329
330
331void draw_set_render( struct draw_context *draw,
332		      struct vbuf_render *render )
333{
334   draw->render = render;
335}
336
337void draw_set_edgeflags( struct draw_context *draw,
338                         const unsigned *edgeflag )
339{
340   draw->user.edgeflag = edgeflag;
341}
342
343
344boolean draw_get_edgeflag( struct draw_context *draw,
345                           unsigned idx )
346{
347   if (draw->user.edgeflag)
348      return (draw->user.edgeflag[idx/32] & (1 << (idx%32))) != 0;
349   else
350      return 1;
351}
352
353
354/**
355 * Tell the drawing context about the index/element buffer to use
356 * (ala glDrawElements)
357 * If no element buffer is to be used (i.e. glDrawArrays) then this
358 * should be called with eltSize=0 and elements=NULL.
359 *
360 * \param draw  the drawing context
361 * \param eltSize  size of each element (1, 2 or 4 bytes)
362 * \param elements  the element buffer ptr
363 */
364void
365draw_set_mapped_element_buffer( struct draw_context *draw,
366                                unsigned eltSize, void *elements )
367{
368   draw->user.elts = elements;
369   draw->user.eltSize = eltSize;
370}
371
372
373
374/* Revamp me please:
375 */
376void draw_do_flush( struct draw_context *draw, unsigned flags )
377{
378   if (!draw->flushing)
379   {
380      draw->flushing = TRUE;
381
382      if (flags >= DRAW_FLUSH_STATE_CHANGE) {
383	 draw->pipeline.first->flush( draw->pipeline.first, flags );
384	 draw->pipeline.first = draw->pipeline.validate;
385	 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
386      }
387
388      draw->flushing = FALSE;
389   }
390}
391