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