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_STATE_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_STATE_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 switch (shader_type
) {
372 case PIPE_SHADER_VERTEX
:
373 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
374 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
375 draw_vs_set_constants(draw
, slot
, buffer
, size
);
377 case PIPE_SHADER_GEOMETRY
:
378 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
379 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
380 draw_gs_set_constants(draw
, slot
, buffer
, 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_samplers(struct draw_context
*draw
,
587 struct tgsi_sampler
**samplers
)
589 if (shader
== PIPE_SHADER_VERTEX
) {
590 draw
->vs
.tgsi
.num_samplers
= num_samplers
;
591 draw
->vs
.tgsi
.samplers
= samplers
;
593 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
594 draw
->gs
.tgsi
.num_samplers
= num_samplers
;
595 draw
->gs
.tgsi
.samplers
= samplers
;
602 void draw_set_render( struct draw_context
*draw
,
603 struct vbuf_render
*render
)
605 draw
->render
= render
;
610 * Tell the draw module where vertex indexes/elements are located, and
611 * their size (in bytes).
613 * Note: the caller must apply the pipe_index_buffer::offset value to
614 * the address. The draw module doesn't do that.
617 draw_set_indexes(struct draw_context
*draw
,
618 const void *elements
, unsigned elem_size
)
620 assert(elem_size
== 0 ||
624 draw
->pt
.user
.elts
= elements
;
625 draw
->pt
.user
.eltSizeIB
= elem_size
;
631 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
633 if (!draw
->suspend_flushing
)
635 assert(!draw
->flushing
); /* catch inadvertant recursion */
637 draw
->flushing
= TRUE
;
639 draw_pipeline_flush( draw
, flags
);
641 draw_pt_flush( draw
, flags
);
643 draw
->flushing
= FALSE
;
649 * Return the number of output attributes produced by the geometry
650 * shader, if present. If no geometry shader, return the number of
651 * outputs from the vertex shader.
652 * \sa draw_num_shader_outputs
655 draw_current_shader_outputs(const struct draw_context
*draw
)
657 if (draw
->gs
.geometry_shader
)
658 return draw
->gs
.num_gs_outputs
;
659 return draw
->vs
.num_vs_outputs
;
664 * Return the index of the shader output which will contain the
668 draw_current_shader_position_output(const struct draw_context
*draw
)
670 if (draw
->gs
.geometry_shader
)
671 return draw
->gs
.position_output
;
672 return draw
->vs
.position_output
;
677 * Return the index of the shader output which will contain the
681 draw_current_shader_clipvertex_output(const struct draw_context
*draw
)
683 return draw
->vs
.clipvertex_output
;
687 draw_current_shader_clipdistance_output(const struct draw_context
*draw
, int index
)
689 return draw
->vs
.clipdistance_output
[index
];
693 * Return a pointer/handle for a driver/CSO rasterizer object which
694 * disabled culling, stippling, unfilled tris, etc.
695 * This is used by some pipeline stages (such as wide_point, aa_line
696 * and aa_point) which convert points/lines into triangles. In those
697 * cases we don't want to accidentally cull the triangles.
699 * \param scissor should the rasterizer state enable scissoring?
700 * \param flatshade should the rasterizer state use flat shading?
701 * \return rasterizer CSO handle
704 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
708 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
710 struct pipe_context
*pipe
= draw
->pipe
;
711 struct pipe_rasterizer_state rast
;
713 memset(&rast
, 0, sizeof(rast
));
714 rast
.scissor
= scissor
;
715 rast
.flatshade
= flatshade
;
717 rast
.gl_rasterization_rules
= draw
->rasterizer
->gl_rasterization_rules
;
719 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
720 pipe
->create_rasterizer_state(pipe
, &rast
);
722 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
726 draw_set_mapped_so_targets(struct draw_context
*draw
,
728 struct draw_so_target
*targets
[PIPE_MAX_SO_BUFFERS
])
732 for (i
= 0; i
< num_targets
; i
++)
733 draw
->so
.targets
[i
] = targets
[i
];
734 for (i
= num_targets
; i
< PIPE_MAX_SO_BUFFERS
; i
++)
735 draw
->so
.targets
[i
] = NULL
;
737 draw
->so
.num_targets
= num_targets
;
741 draw_set_mapped_so_buffers(struct draw_context
*draw
,
742 void *buffers
[PIPE_MAX_SO_BUFFERS
],
743 unsigned num_buffers
)
748 draw_set_so_state(struct draw_context
*draw
,
749 struct pipe_stream_output_info
*state
)
751 memcpy(&draw
->so
.state
,
753 sizeof(struct pipe_stream_output_info
));
757 draw_set_sampler_views(struct draw_context
*draw
,
758 unsigned shader_stage
,
759 struct pipe_sampler_view
**views
,
764 debug_assert(shader_stage
< PIPE_SHADER_TYPES
);
765 debug_assert(num
<= PIPE_MAX_SAMPLERS
);
767 for (i
= 0; i
< num
; ++i
)
768 draw
->sampler_views
[shader_stage
][i
] = views
[i
];
769 for (i
= num
; i
< PIPE_MAX_SAMPLERS
; ++i
)
770 draw
->sampler_views
[shader_stage
][i
] = NULL
;
772 draw
->num_sampler_views
[shader_stage
] = num
;
776 draw_set_samplers(struct draw_context
*draw
,
777 unsigned shader_stage
,
778 struct pipe_sampler_state
**samplers
,
783 debug_assert(shader_stage
< PIPE_SHADER_TYPES
);
784 debug_assert(num
<= PIPE_MAX_SAMPLERS
);
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
;
791 draw
->num_samplers
[shader_stage
] = num
;
794 if (draw
->llvm
&& shader_stage
== PIPE_SHADER_VERTEX
)
795 draw_llvm_set_sampler_state(draw
);
800 draw_set_mapped_texture(struct draw_context
*draw
,
801 unsigned shader_stage
,
802 unsigned sampler_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
])
810 if (shader_stage
== PIPE_SHADER_VERTEX
) {
813 draw_llvm_set_mapped_texture(draw
,
815 width
, height
, depth
, first_level
,
816 last_level
, base_ptr
,
817 row_stride
, img_stride
, mip_offsets
);
823 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
824 * different ways of setting textures, and drivers typically only support one.
827 draw_get_shader_param_no_llvm(unsigned shader
, enum pipe_shader_cap param
)
830 case PIPE_SHADER_VERTEX
:
831 case PIPE_SHADER_GEOMETRY
:
832 return tgsi_exec_get_shader_param(param
);
839 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
840 * different ways of setting textures, and drivers typically only support one.
843 draw_get_shader_param(unsigned shader
, enum pipe_shader_cap param
)
847 if (draw_get_option_use_llvm()) {
849 case PIPE_SHADER_VERTEX
:
850 case PIPE_SHADER_GEOMETRY
:
851 return gallivm_get_shader_param(param
);
858 return draw_get_shader_param_no_llvm(shader
, param
);