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