Merge remote branch 'origin/opengl-es-v2'
[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_gs.h"
40 #include "draw_pt.h"
41 #include "draw_pipe.h"
42
43
44 struct draw_context *draw_create( void )
45 {
46 struct draw_context *draw = CALLOC_STRUCT( draw_context );
47 if (draw == NULL)
48 goto fail;
49
50 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
51 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
52 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
53 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
54 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
55 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
56 draw->nr_planes = 6;
57
58
59 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
60
61
62 if (!draw_pipeline_init( draw ))
63 goto fail;
64
65 if (!draw_pt_init( draw ))
66 goto fail;
67
68 if (!draw_vs_init( draw ))
69 goto fail;
70
71 if (!draw_gs_init( draw ))
72 goto fail;
73
74 return draw;
75
76 fail:
77 draw_destroy( draw );
78 return NULL;
79 }
80
81
82 void draw_destroy( struct draw_context *draw )
83 {
84 if (!draw)
85 return;
86
87
88
89 /* Not so fast -- we're just borrowing this at the moment.
90 *
91 if (draw->render)
92 draw->render->destroy( draw->render );
93 */
94
95 draw_pipeline_destroy( draw );
96 draw_pt_destroy( draw );
97 draw_vs_destroy( draw );
98 draw_gs_destroy( draw );
99
100 FREE( draw );
101 }
102
103
104
105 void draw_flush( struct draw_context *draw )
106 {
107 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
108 }
109
110
111 /**
112 * Specify the Minimum Resolvable Depth factor for polygon offset.
113 * This factor potentially depends on the number of Z buffer bits,
114 * the rasterization algorithm and the arithmetic performed on Z
115 * values between vertex shading and rasterization. It will vary
116 * from one driver to another.
117 */
118 void draw_set_mrd(struct draw_context *draw, double mrd)
119 {
120 draw->mrd = mrd;
121 }
122
123
124 /**
125 * Register new primitive rasterization/rendering state.
126 * This causes the drawing pipeline to be rebuilt.
127 */
128 void draw_set_rasterizer_state( struct draw_context *draw,
129 const struct pipe_rasterizer_state *raster )
130 {
131 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
132
133 draw->rasterizer = raster;
134 draw->bypass_clipping =
135 ((draw->rasterizer && draw->rasterizer->bypass_vs_clip_and_viewport) ||
136 draw->driver.bypass_clipping);
137 }
138
139
140 void draw_set_driver_clipping( struct draw_context *draw,
141 boolean bypass_clipping )
142 {
143 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
144
145 draw->driver.bypass_clipping = bypass_clipping;
146 draw->bypass_clipping =
147 ((draw->rasterizer && draw->rasterizer->bypass_vs_clip_and_viewport) ||
148 draw->driver.bypass_clipping);
149 }
150
151
152 /**
153 * Plug in the primitive rendering/rasterization stage (which is the last
154 * stage in the drawing pipeline).
155 * This is provided by the device driver.
156 */
157 void draw_set_rasterize_stage( struct draw_context *draw,
158 struct draw_stage *stage )
159 {
160 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
161
162 draw->pipeline.rasterize = stage;
163 }
164
165
166 /**
167 * Set the draw module's clipping state.
168 */
169 void draw_set_clip_state( struct draw_context *draw,
170 const struct pipe_clip_state *clip )
171 {
172 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
173
174 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
175 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
176 draw->nr_planes = 6 + clip->nr;
177 }
178
179
180 /**
181 * Set the draw module's viewport state.
182 */
183 void draw_set_viewport_state( struct draw_context *draw,
184 const struct pipe_viewport_state *viewport )
185 {
186 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
187 draw->viewport = *viewport; /* struct copy */
188 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
189 viewport->scale[1] == 1.0f &&
190 viewport->scale[2] == 1.0f &&
191 viewport->scale[3] == 1.0f &&
192 viewport->translate[0] == 0.0f &&
193 viewport->translate[1] == 0.0f &&
194 viewport->translate[2] == 0.0f &&
195 viewport->translate[3] == 0.0f);
196
197 draw_vs_set_viewport( draw, viewport );
198 }
199
200
201
202 void
203 draw_set_vertex_buffers(struct draw_context *draw,
204 unsigned count,
205 const struct pipe_vertex_buffer *buffers)
206 {
207 assert(count <= PIPE_MAX_ATTRIBS);
208
209 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0]));
210 draw->pt.nr_vertex_buffers = count;
211 }
212
213
214 void
215 draw_set_vertex_elements(struct draw_context *draw,
216 unsigned count,
217 const struct pipe_vertex_element *elements)
218 {
219 assert(count <= PIPE_MAX_ATTRIBS);
220
221 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
222 draw->pt.nr_vertex_elements = count;
223 }
224
225
226 /**
227 * Tell drawing context where to find mapped vertex buffers.
228 */
229 void
230 draw_set_mapped_vertex_buffer(struct draw_context *draw,
231 unsigned attr, const void *buffer)
232 {
233 draw->pt.user.vbuffer[attr] = buffer;
234 }
235
236
237 void
238 draw_set_mapped_constant_buffer(struct draw_context *draw,
239 unsigned shader_type,
240 const void *buffer,
241 unsigned size )
242 {
243 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
244 shader_type == PIPE_SHADER_GEOMETRY);
245 if (shader_type == PIPE_SHADER_VERTEX) {
246 draw->pt.user.vs_constants = buffer;
247 draw_vs_set_constants( draw, (const float (*)[4])buffer, size );
248 } else if (shader_type == PIPE_SHADER_GEOMETRY) {
249 draw->pt.user.gs_constants = buffer;
250 draw_gs_set_constants( draw, (const float (*)[4])buffer, size );
251 }
252 }
253
254
255 /**
256 * Tells the draw module to draw points with triangles if their size
257 * is greater than this threshold.
258 */
259 void
260 draw_wide_point_threshold(struct draw_context *draw, float threshold)
261 {
262 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
263 draw->pipeline.wide_point_threshold = threshold;
264 }
265
266
267 /**
268 * Tells the draw module to draw lines with triangles if their width
269 * is greater than this threshold.
270 */
271 void
272 draw_wide_line_threshold(struct draw_context *draw, float threshold)
273 {
274 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
275 draw->pipeline.wide_line_threshold = threshold;
276 }
277
278
279 /**
280 * Tells the draw module whether or not to implement line stipple.
281 */
282 void
283 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
284 {
285 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
286 draw->pipeline.line_stipple = enable;
287 }
288
289
290 /**
291 * Tells draw module whether to convert points to quads for sprite mode.
292 */
293 void
294 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
295 {
296 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
297 draw->pipeline.point_sprite = enable;
298 }
299
300
301 void
302 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
303 {
304 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
305 draw->force_passthrough = enable;
306 }
307
308
309 /**
310 * Ask the draw module for the location/slot of the given vertex attribute in
311 * a post-transformed vertex.
312 *
313 * With this function, drivers that use the draw module should have no reason
314 * to track the current vertex/geometry shader.
315 *
316 * Note that the draw module may sometimes generate vertices with extra
317 * attributes (such as texcoords for AA lines). The driver can call this
318 * function to find those attributes.
319 *
320 * Zero is returned if the attribute is not found since this is
321 * a don't care / undefined situtation. Returning -1 would be a bit more
322 * work for the drivers.
323 */
324 int
325 draw_find_shader_output(const struct draw_context *draw,
326 uint semantic_name, uint semantic_index)
327 {
328 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
329 const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
330 uint i;
331 const struct tgsi_shader_info *info = &vs->info;
332
333 if (gs)
334 info = &gs->info;
335
336 for (i = 0; i < info->num_outputs; i++) {
337 if (info->output_semantic_name[i] == semantic_name &&
338 info->output_semantic_index[i] == semantic_index)
339 return i;
340 }
341
342 /* XXX there may be more than one extra vertex attrib.
343 * For example, simulated gl_FragCoord and gl_PointCoord.
344 */
345 if (draw->extra_shader_outputs.semantic_name == semantic_name &&
346 draw->extra_shader_outputs.semantic_index == semantic_index) {
347 return draw->extra_shader_outputs.slot;
348 }
349
350 return 0;
351 }
352
353
354 /**
355 * Return number of the shader outputs.
356 *
357 * If geometry shader is present, its output will be returned,
358 * if not vertex shader is used.
359 */
360 uint
361 draw_num_shader_outputs(const struct draw_context *draw)
362 {
363 uint count = draw->vs.vertex_shader->info.num_outputs;
364
365 /* if geometry shader is present, its outputs go to te
366 * driver, not the vertex shaders */
367 if (draw->gs.geometry_shader)
368 count = draw->gs.geometry_shader->info.num_outputs;
369
370 if (draw->extra_shader_outputs.slot > 0)
371 count++;
372 return count;
373 }
374
375
376 /**
377 * Provide TGSI sampler objects for vertex/geometry shaders that use texture fetches.
378 * This might only be used by software drivers for the time being.
379 */
380 void
381 draw_texture_samplers(struct draw_context *draw,
382 uint num_samplers,
383 struct tgsi_sampler **samplers)
384 {
385 draw->vs.num_samplers = num_samplers;
386 draw->vs.samplers = samplers;
387 draw->gs.num_samplers = num_samplers;
388 draw->gs.samplers = samplers;
389 }
390
391
392
393
394 void draw_set_render( struct draw_context *draw,
395 struct vbuf_render *render )
396 {
397 draw->render = render;
398 }
399
400
401
402 /**
403 * Tell the drawing context about the index/element buffer to use
404 * (ala glDrawElements)
405 * If no element buffer is to be used (i.e. glDrawArrays) then this
406 * should be called with eltSize=0 and elements=NULL.
407 *
408 * \param draw the drawing context
409 * \param eltSize size of each element (1, 2 or 4 bytes)
410 * \param elements the element buffer ptr
411 */
412 void
413 draw_set_mapped_element_buffer_range( struct draw_context *draw,
414 unsigned eltSize,
415 unsigned min_index,
416 unsigned max_index,
417 const void *elements )
418 {
419 draw->pt.user.elts = elements;
420 draw->pt.user.eltSize = eltSize;
421 draw->pt.user.min_index = min_index;
422 draw->pt.user.max_index = max_index;
423 }
424
425
426 void
427 draw_set_mapped_element_buffer( struct draw_context *draw,
428 unsigned eltSize,
429 const void *elements )
430 {
431 draw->pt.user.elts = elements;
432 draw->pt.user.eltSize = eltSize;
433 draw->pt.user.min_index = 0;
434 draw->pt.user.max_index = 0xffffffff;
435 }
436
437
438 /* Revamp me please:
439 */
440 void draw_do_flush( struct draw_context *draw, unsigned flags )
441 {
442 if (!draw->suspend_flushing)
443 {
444 assert(!draw->flushing); /* catch inadvertant recursion */
445
446 draw->flushing = TRUE;
447
448 draw_pipeline_flush( draw, flags );
449
450 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
451
452 draw->flushing = FALSE;
453 }
454 }
455
456
457 int draw_current_shader_outputs(struct draw_context *draw)
458 {
459 if (draw->gs.geometry_shader)
460 return draw->gs.num_gs_outputs;
461 return draw->vs.num_vs_outputs;
462 }
463
464 int draw_current_shader_position_output(struct draw_context *draw)
465 {
466 if (draw->gs.geometry_shader)
467 return draw->gs.position_output;
468 return draw->vs.position_output;
469 }