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