draw: subclass vertex shaders according to execution method
[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_private.h"
37
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43
44 #if defined(__i386__) || defined(__386__)
45 draw->use_sse = GETENV( "GALLIUM_NOSSE" ) == NULL;
46 #else
47 draw->use_sse = FALSE;
48 #endif
49
50 /* create pipeline stages */
51 draw->pipeline.wide = draw_wide_stage( draw );
52 draw->pipeline.stipple = draw_stipple_stage( draw );
53 draw->pipeline.unfilled = draw_unfilled_stage( draw );
54 draw->pipeline.twoside = draw_twoside_stage( draw );
55 draw->pipeline.offset = draw_offset_stage( draw );
56 draw->pipeline.clip = draw_clip_stage( draw );
57 draw->pipeline.flatshade = draw_flatshade_stage( draw );
58 draw->pipeline.cull = draw_cull_stage( draw );
59 draw->pipeline.validate = draw_validate_stage( draw );
60 draw->pipeline.first = draw->pipeline.validate;
61
62 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
63 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
64 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
65 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
66 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
67 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
68 draw->nr_planes = 6;
69
70 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
71 */
72 {
73 uint i;
74 const unsigned size = (MAX_VERTEX_SIZE + 0x0f) & ~0x0f;
75 char *tmp = align_malloc(Elements(draw->vcache.vertex) * size, 16);
76
77 for (i = 0; i < Elements(draw->vcache.vertex); i++)
78 draw->vcache.vertex[i] = (struct vertex_header *)(tmp + i * size);
79 }
80
81 draw->shader_queue_flush = draw_vertex_shader_queue_flush;
82
83 draw->convert_wide_points = TRUE;
84 draw->convert_wide_lines = TRUE;
85
86 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
87
88 draw_vertex_cache_invalidate( draw );
89 draw_set_mapped_element_buffer( draw, 0, NULL );
90
91 return draw;
92 }
93
94
95 void draw_destroy( struct draw_context *draw )
96 {
97 draw->pipeline.wide->destroy( draw->pipeline.wide );
98 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
99 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
100 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
101 draw->pipeline.offset->destroy( draw->pipeline.offset );
102 draw->pipeline.clip->destroy( draw->pipeline.clip );
103 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
104 draw->pipeline.cull->destroy( draw->pipeline.cull );
105 draw->pipeline.validate->destroy( draw->pipeline.validate );
106 if (draw->pipeline.rasterize)
107 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
108 tgsi_exec_machine_free_data(&draw->machine);
109 align_free( draw->vcache.vertex[0] ); /* Frees all the vertices. */
110 FREE( draw );
111 }
112
113
114
115 void draw_flush( struct draw_context *draw )
116 {
117 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
118 }
119
120
121
122 /**
123 * Register new primitive rasterization/rendering state.
124 * This causes the drawing pipeline to be rebuilt.
125 */
126 void draw_set_rasterizer_state( struct draw_context *draw,
127 const struct pipe_rasterizer_state *raster )
128 {
129 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
130
131 draw->rasterizer = raster;
132 }
133
134
135 /**
136 * Plug in the primitive rendering/rasterization stage (which is the last
137 * stage in the drawing pipeline).
138 * This is provided by the device driver.
139 */
140 void draw_set_rasterize_stage( struct draw_context *draw,
141 struct draw_stage *stage )
142 {
143 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
144
145 draw->pipeline.rasterize = stage;
146 }
147
148
149 /**
150 * Set the draw module's clipping state.
151 */
152 void draw_set_clip_state( struct draw_context *draw,
153 const struct pipe_clip_state *clip )
154 {
155 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
156
157 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
158 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
159 draw->nr_planes = 6 + clip->nr;
160 }
161
162
163 /**
164 * Set the draw module's viewport state.
165 */
166 void draw_set_viewport_state( struct draw_context *draw,
167 const struct pipe_viewport_state *viewport )
168 {
169 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
170 draw->viewport = *viewport; /* struct copy */
171 }
172
173
174
175 void
176 draw_set_vertex_buffer(struct draw_context *draw,
177 unsigned attr,
178 const struct pipe_vertex_buffer *buffer)
179 {
180 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
181 assert(attr < PIPE_ATTRIB_MAX);
182 draw->vertex_buffer[attr] = *buffer;
183 }
184
185
186 void
187 draw_set_vertex_element(struct draw_context *draw,
188 unsigned attr,
189 const struct pipe_vertex_element *element)
190 {
191 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
192 assert(attr < PIPE_ATTRIB_MAX);
193 draw->vertex_element[attr] = *element;
194 }
195
196
197 /**
198 * Tell drawing context where to find mapped vertex buffers.
199 */
200 void
201 draw_set_mapped_vertex_buffer(struct draw_context *draw,
202 unsigned attr, const void *buffer)
203 {
204 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
205 draw->user.vbuffer[attr] = buffer;
206 }
207
208
209 void
210 draw_set_mapped_constant_buffer(struct draw_context *draw,
211 const void *buffer)
212 {
213 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
214 draw->user.constants = buffer;
215 }
216
217
218 /**
219 * Tells the draw module whether to convert wide points (size != 1)
220 * into triangles.
221 */
222 void
223 draw_convert_wide_points(struct draw_context *draw, boolean enable)
224 {
225 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
226 draw->convert_wide_points = enable;
227 }
228
229
230 /**
231 * Tells the draw module whether to convert wide lines (width != 1)
232 * into triangles.
233 */
234 void
235 draw_convert_wide_lines(struct draw_context *draw, boolean enable)
236 {
237 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
238 draw->convert_wide_lines = enable;
239 }
240
241
242 /**
243 * Allocate space for temporary post-transform vertices, such as for clipping.
244 */
245 void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr )
246 {
247 assert(!stage->tmp);
248
249 stage->nr_tmps = nr;
250
251 if (nr) {
252 ubyte *store = (ubyte *) MALLOC( MAX_VERTEX_SIZE * nr );
253 unsigned i;
254
255 stage->tmp = (struct vertex_header **) MALLOC( sizeof(struct vertex_header *) * nr );
256
257 for (i = 0; i < nr; i++)
258 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
259 }
260 }
261
262
263 void draw_free_temp_verts( struct draw_stage *stage )
264 {
265 if (stage->tmp) {
266 FREE( stage->tmp[0] );
267 FREE( stage->tmp );
268 stage->tmp = NULL;
269 }
270 }
271
272
273 boolean draw_use_sse(struct draw_context *draw)
274 {
275 return (boolean) draw->use_sse;
276 }
277
278
279 void draw_reset_vertex_ids(struct draw_context *draw)
280 {
281 struct draw_stage *stage = draw->pipeline.first;
282
283 while (stage) {
284 unsigned i;
285
286 for (i = 0; i < stage->nr_tmps; i++)
287 stage->tmp[i]->vertex_id = UNDEFINED_VERTEX_ID;
288
289 stage = stage->next;
290 }
291
292 draw_vertex_cache_reset_vertex_ids(draw);
293 }