1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
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"
45 #include "gallivm/lp_bld_init.h"
46 #include "gallivm/lp_bld_limits.h"
47 #include "draw_llvm.h"
50 draw_get_option_use_llvm(void)
52 static boolean first = TRUE;
56 value = debug_get_bool_option("DRAW_USE_LLVM", TRUE);
60 /* require SSE2 due to LLVM PR6960. */
61 if (!util_cpu_caps.has_sse2)
71 * Create new draw module context with gallivm state for LLVM JIT.
73 static struct draw_context *
74 draw_create_context(struct pipe_context *pipe, boolean try_llvm)
76 struct draw_context *draw = CALLOC_STRUCT( draw_context );
81 if (try_llvm && draw_get_option_use_llvm()) {
82 draw->llvm = draw_llvm_create(draw);
103 * Create new draw module context, with LLVM JIT.
105 struct draw_context *
106 draw_create(struct pipe_context *pipe)
108 return draw_create_context(pipe, TRUE);
113 * Create a new draw context, without LLVM JIT.
115 struct draw_context *
116 draw_create_no_llvm(struct pipe_context *pipe)
118 return draw_create_context(pipe, FALSE);
122 boolean draw_init(struct draw_context *draw)
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.
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;
139 draw->pt.user.planes = (float (*) [DRAW_TOTAL_CLIP_PLANES][4]) &(draw->plane[0]);
141 if (!draw_pipeline_init( draw ))
144 if (!draw_pt_init( draw ))
147 if (!draw_vs_init( draw ))
150 if (!draw_gs_init( draw ))
153 draw->quads_always_flatshade_last = !draw->pipe->screen->get_param(
154 draw->pipe->screen, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION);
160 void draw_destroy( struct draw_context *draw )
162 struct pipe_context *pipe;
170 /* free any rasterizer CSOs that we may have created.
172 for (i = 0; i < 2; i++) {
173 for (j = 0; j < 2; j++) {
174 if (draw->rasterizer_no_cull[i][j]) {
175 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
180 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
181 pipe_resource_reference(&draw->pt.vertex_buffer[i].buffer, NULL);
184 /* Not so fast -- we're just borrowing this at the moment.
187 draw->render->destroy( draw->render );
190 draw_pipeline_destroy( draw );
191 draw_pt_destroy( draw );
192 draw_vs_destroy( draw );
193 draw_gs_destroy( draw );
196 draw_llvm_destroy( draw->llvm );
204 void draw_flush( struct draw_context *draw )
206 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
211 * Specify the Minimum Resolvable Depth factor for polygon offset.
212 * This factor potentially depends on the number of Z buffer bits,
213 * the rasterization algorithm and the arithmetic performed on Z
214 * values between vertex shading and rasterization. It will vary
215 * from one driver to another.
217 void draw_set_mrd(struct draw_context *draw, double mrd)
223 static void update_clip_flags( struct draw_context *draw )
225 draw->clip_xy = !draw->driver.bypass_clip_xy;
226 draw->guard_band_xy = (!draw->driver.bypass_clip_xy &&
227 draw->driver.guard_band_xy);
228 draw->clip_z = (!draw->driver.bypass_clip_z &&
229 draw->rasterizer && draw->rasterizer->depth_clip);
230 draw->clip_user = draw->rasterizer &&
231 draw->rasterizer->clip_plane_enable != 0;
235 * Register new primitive rasterization/rendering state.
236 * This causes the drawing pipeline to be rebuilt.
238 void draw_set_rasterizer_state( struct draw_context *draw,
239 const struct pipe_rasterizer_state *raster,
242 if (!draw->suspend_flushing) {
243 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
245 draw->rasterizer = raster;
246 draw->rast_handle = rast_handle;
247 update_clip_flags(draw);
251 /* With a little more work, llvmpipe will be able to turn this off and
252 * do its own x/y clipping.
254 * Some hardware can turn off clipping altogether - in particular any
255 * hardware with a TNL unit can do its own clipping, even if it is
256 * relying on the draw module for some other reason.
258 void draw_set_driver_clipping( struct draw_context *draw,
259 boolean bypass_clip_xy,
260 boolean bypass_clip_z,
261 boolean guard_band_xy)
263 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
265 draw->driver.bypass_clip_xy = bypass_clip_xy;
266 draw->driver.bypass_clip_z = bypass_clip_z;
267 draw->driver.guard_band_xy = guard_band_xy;
268 update_clip_flags(draw);
273 * Plug in the primitive rendering/rasterization stage (which is the last
274 * stage in the drawing pipeline).
275 * This is provided by the device driver.
277 void draw_set_rasterize_stage( struct draw_context *draw,
278 struct draw_stage *stage )
280 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
282 draw->pipeline.rasterize = stage;
287 * Set the draw module's clipping state.
289 void draw_set_clip_state( struct draw_context *draw,
290 const struct pipe_clip_state *clip )
292 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
294 memcpy(&draw->plane[6], clip->ucp, sizeof(clip->ucp));
299 * Set the draw module's viewport state.
301 void draw_set_viewport_state( struct draw_context *draw,
302 const struct pipe_viewport_state *viewport )
304 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
305 draw->viewport = *viewport; /* struct copy */
306 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
307 viewport->scale[1] == 1.0f &&
308 viewport->scale[2] == 1.0f &&
309 viewport->scale[3] == 1.0f &&
310 viewport->translate[0] == 0.0f &&
311 viewport->translate[1] == 0.0f &&
312 viewport->translate[2] == 0.0f &&
313 viewport->translate[3] == 0.0f);
315 draw_vs_set_viewport( draw, viewport );
321 draw_set_vertex_buffers(struct draw_context *draw,
322 unsigned start_slot, unsigned count,
323 const struct pipe_vertex_buffer *buffers)
325 assert(start_slot + count <= PIPE_MAX_ATTRIBS);
327 util_set_vertex_buffers_count(draw->pt.vertex_buffer,
328 &draw->pt.nr_vertex_buffers,
329 buffers, start_slot, count);
334 draw_set_vertex_elements(struct draw_context *draw,
336 const struct pipe_vertex_element *elements)
338 assert(count <= PIPE_MAX_ATTRIBS);
340 /* We could improve this by only flushing the frontend and the fetch part
341 * of the middle. This would avoid recalculating the emit keys.*/
342 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
344 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
345 draw->pt.nr_vertex_elements = count;
350 * Tell drawing context where to find mapped vertex buffers.
353 draw_set_mapped_vertex_buffer(struct draw_context *draw,
354 unsigned attr, const void *buffer)
356 draw->pt.user.vbuffer[attr] = buffer;
361 draw_set_mapped_constant_buffer(struct draw_context *draw,
362 unsigned shader_type,
367 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
368 shader_type == PIPE_SHADER_GEOMETRY);
369 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
371 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
373 switch (shader_type) {
374 case PIPE_SHADER_VERTEX:
375 draw->pt.user.vs_constants[slot] = buffer;
376 draw->pt.user.vs_constants_size[slot] = size;
378 case PIPE_SHADER_GEOMETRY:
379 draw->pt.user.gs_constants[slot] = buffer;
380 draw->pt.user.gs_constants_size[slot] = size;
383 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
389 * Tells the draw module to draw points with triangles if their size
390 * is greater than this threshold.
393 draw_wide_point_threshold(struct draw_context *draw, float threshold)
395 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
396 draw->pipeline.wide_point_threshold = threshold;
401 * Should the draw module handle point->quad conversion for drawing sprites?
404 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
406 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
407 draw->pipeline.wide_point_sprites = draw_sprite;
412 * Tells the draw module to draw lines with triangles if their width
413 * is greater than this threshold.
416 draw_wide_line_threshold(struct draw_context *draw, float threshold)
418 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
419 draw->pipeline.wide_line_threshold = roundf(threshold);
424 * Tells the draw module whether or not to implement line stipple.
427 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
429 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
430 draw->pipeline.line_stipple = enable;
435 * Tells draw module whether to convert points to quads for sprite mode.
438 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
440 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
441 draw->pipeline.point_sprite = enable;
446 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
448 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
449 draw->force_passthrough = enable;
455 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
458 * This is used by some of the optional draw module stages such
459 * as wide_point which may need to allocate additional generic/texcoord
463 draw_alloc_extra_vertex_attrib(struct draw_context *draw,
464 uint semantic_name, uint semantic_index)
470 slot = draw_find_shader_output(draw, semantic_name, semantic_index);
475 num_outputs = draw_current_shader_outputs(draw);
476 n = draw->extra_shader_outputs.num;
478 assert(n < Elements(draw->extra_shader_outputs.semantic_name));
480 draw->extra_shader_outputs.semantic_name[n] = semantic_name;
481 draw->extra_shader_outputs.semantic_index[n] = semantic_index;
482 draw->extra_shader_outputs.slot[n] = num_outputs + n;
483 draw->extra_shader_outputs.num++;
485 return draw->extra_shader_outputs.slot[n];
490 * Remove all extra vertex attributes that were allocated with
491 * draw_alloc_extra_vertex_attrib().
494 draw_remove_extra_vertex_attribs(struct draw_context *draw)
496 draw->extra_shader_outputs.num = 0;
501 * If a geometry shader is present, return its info, else the vertex shader's
504 struct tgsi_shader_info *
505 draw_get_shader_info(const struct draw_context *draw)
508 if (draw->gs.geometry_shader) {
509 return &draw->gs.geometry_shader->info;
511 return &draw->vs.vertex_shader->info;
517 * Ask the draw module for the location/slot of the given vertex attribute in
518 * a post-transformed vertex.
520 * With this function, drivers that use the draw module should have no reason
521 * to track the current vertex/geometry shader.
523 * Note that the draw module may sometimes generate vertices with extra
524 * attributes (such as texcoords for AA lines). The driver can call this
525 * function to find those attributes.
527 * Zero is returned if the attribute is not found since this is
528 * a don't care / undefined situtation. Returning -1 would be a bit more
529 * work for the drivers.
532 draw_find_shader_output(const struct draw_context *draw,
533 uint semantic_name, uint semantic_index)
535 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
538 for (i = 0; i < info->num_outputs; i++) {
539 if (info->output_semantic_name[i] == semantic_name &&
540 info->output_semantic_index[i] == semantic_index)
544 /* Search the extra vertex attributes */
545 for (i = 0; i < draw->extra_shader_outputs.num; i++) {
546 if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
547 draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
548 return draw->extra_shader_outputs.slot[i];
557 * Return total number of the shader outputs. This function is similar to
558 * draw_current_shader_outputs() but this function also counts any extra
559 * vertex/geometry output attributes that may be filled in by some draw
560 * stages (such as AA point, AA line).
562 * If geometry shader is present, its output will be returned,
563 * if not vertex shader is used.
566 draw_num_shader_outputs(const struct draw_context *draw)
568 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
571 count = info->num_outputs;
572 count += draw->extra_shader_outputs.num;
579 * Provide TGSI sampler objects for vertex/geometry shaders that use
580 * texture fetches. This state only needs to be set once per context.
581 * This might only be used by software drivers for the time being.
584 draw_texture_sampler(struct draw_context *draw,
586 struct tgsi_sampler *sampler)
588 if (shader == PIPE_SHADER_VERTEX) {
589 draw->vs.tgsi.sampler = sampler;
591 debug_assert(shader == PIPE_SHADER_GEOMETRY);
592 draw->gs.tgsi.sampler = sampler;
599 void draw_set_render( struct draw_context *draw,
600 struct vbuf_render *render )
602 draw->render = render;
607 * Tell the draw module where vertex indexes/elements are located, and
608 * their size (in bytes).
610 * Note: the caller must apply the pipe_index_buffer::offset value to
611 * the address. The draw module doesn't do that.
614 draw_set_indexes(struct draw_context *draw,
615 const void *elements, unsigned elem_size)
617 assert(elem_size == 0 ||
621 draw->pt.user.elts = elements;
622 draw->pt.user.eltSizeIB = elem_size;
628 void draw_do_flush( struct draw_context *draw, unsigned flags )
630 if (!draw->suspend_flushing)
632 assert(!draw->flushing); /* catch inadvertant recursion */
634 draw->flushing = TRUE;
636 draw_pipeline_flush( draw, flags );
638 draw_pt_flush( draw, flags );
640 draw->flushing = FALSE;
646 * Return the number of output attributes produced by the geometry
647 * shader, if present. If no geometry shader, return the number of
648 * outputs from the vertex shader.
649 * \sa draw_num_shader_outputs
652 draw_current_shader_outputs(const struct draw_context *draw)
654 if (draw->gs.geometry_shader)
655 return draw->gs.num_gs_outputs;
656 return draw->vs.num_vs_outputs;
661 * Return the index of the shader output which will contain the
665 draw_current_shader_position_output(const struct draw_context *draw)
667 if (draw->gs.geometry_shader)
668 return draw->gs.position_output;
669 return draw->vs.position_output;
674 * Return the index of the shader output which will contain the
678 draw_current_shader_clipvertex_output(const struct draw_context *draw)
680 return draw->vs.clipvertex_output;
684 draw_current_shader_clipdistance_output(const struct draw_context *draw, int index)
686 return draw->vs.clipdistance_output[index];
690 * Return a pointer/handle for a driver/CSO rasterizer object which
691 * disabled culling, stippling, unfilled tris, etc.
692 * This is used by some pipeline stages (such as wide_point, aa_line
693 * and aa_point) which convert points/lines into triangles. In those
694 * cases we don't want to accidentally cull the triangles.
696 * \param scissor should the rasterizer state enable scissoring?
697 * \param flatshade should the rasterizer state use flat shading?
698 * \return rasterizer CSO handle
701 draw_get_rasterizer_no_cull( struct draw_context *draw,
705 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
707 struct pipe_context *pipe = draw->pipe;
708 struct pipe_rasterizer_state rast;
710 memset(&rast, 0, sizeof(rast));
711 rast.scissor = scissor;
712 rast.flatshade = flatshade;
714 rast.gl_rasterization_rules = draw->rasterizer->gl_rasterization_rules;
715 rast.clip_halfz = draw->rasterizer->clip_halfz;
717 draw->rasterizer_no_cull[scissor][flatshade] =
718 pipe->create_rasterizer_state(pipe, &rast);
720 return draw->rasterizer_no_cull[scissor][flatshade];
724 draw_set_mapped_so_targets(struct draw_context *draw,
726 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
730 for (i = 0; i < num_targets; i++)
731 draw->so.targets[i] = targets[i];
732 for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
733 draw->so.targets[i] = NULL;
735 draw->so.num_targets = num_targets;
739 draw_set_sampler_views(struct draw_context *draw,
740 unsigned shader_stage,
741 struct pipe_sampler_view **views,
746 debug_assert(shader_stage < PIPE_SHADER_TYPES);
747 debug_assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
749 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
751 for (i = 0; i < num; ++i)
752 draw->sampler_views[shader_stage][i] = views[i];
753 for (i = num; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; ++i)
754 draw->sampler_views[shader_stage][i] = NULL;
756 draw->num_sampler_views[shader_stage] = num;
760 draw_set_samplers(struct draw_context *draw,
761 unsigned shader_stage,
762 struct pipe_sampler_state **samplers,
767 debug_assert(shader_stage < PIPE_SHADER_TYPES);
768 debug_assert(num <= PIPE_MAX_SAMPLERS);
770 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
772 for (i = 0; i < num; ++i)
773 draw->samplers[shader_stage][i] = samplers[i];
774 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
775 draw->samplers[shader_stage][i] = NULL;
777 draw->num_samplers[shader_stage] = num;
781 draw_llvm_set_sampler_state(draw, shader_stage);
786 draw_set_mapped_texture(struct draw_context *draw,
787 unsigned shader_stage,
789 uint32_t width, uint32_t height, uint32_t depth,
790 uint32_t first_level, uint32_t last_level,
791 const void *base_ptr,
792 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
793 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
794 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
798 draw_llvm_set_mapped_texture(draw,
801 width, height, depth, first_level,
802 last_level, base_ptr,
803 row_stride, img_stride, mip_offsets);
808 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
809 * different ways of setting textures, and drivers typically only support one.
812 draw_get_shader_param_no_llvm(unsigned shader, enum pipe_shader_cap param)
815 case PIPE_SHADER_VERTEX:
816 case PIPE_SHADER_GEOMETRY:
817 return tgsi_exec_get_shader_param(param);
824 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
825 * different ways of setting textures, and drivers typically only support one.
828 draw_get_shader_param(unsigned shader, enum pipe_shader_cap param)
832 if (draw_get_option_use_llvm()) {
834 case PIPE_SHADER_VERTEX:
835 case PIPE_SHADER_GEOMETRY:
836 return gallivm_get_shader_param(param);
843 return draw_get_shader_param_no_llvm(shader, param);
847 * Enables or disables collection of statistics.
849 * Draw module is capable of generating statistics for the vertex
850 * processing pipeline. Collection of that data isn't free and so
851 * it's disabled by default. The users of the module can enable
852 * (or disable) this functionality through this function.
853 * The actual data will be emitted through the VBUF interface,
854 * the 'pipeline_statistics' callback to be exact.
857 draw_collect_pipeline_statistics(struct draw_context *draw,
860 draw->collect_statistics = enable;