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