1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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 "util/u_prim.h"
41 #include "util/format/u_format.h"
42 #include "draw_context.h"
43 #include "draw_pipe.h"
44 #include "draw_prim_assembler.h"
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_limits.h"
51 #include "draw_llvm.h"
54 draw_get_option_use_llvm(void)
56 return debug_get_bool_option("DRAW_USE_LLVM", TRUE
);
60 draw_get_option_use_llvm(void)
70 return draw_get_option_use_llvm();
77 * Create new draw module context with gallivm state for LLVM JIT.
79 static struct draw_context
*
80 draw_create_context(struct pipe_context
*pipe
, void *context
,
83 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
87 /* we need correct cpu caps for disabling denorms in draw_vbo() */
91 if (try_llvm
&& draw_get_option_use_llvm()) {
92 draw
->llvm
= draw_llvm_create(draw
, (LLVMContextRef
)context
);
101 draw
->ia
= draw_prim_assembler_create(draw
);
108 draw_destroy( draw
);
115 * Create new draw module context, with LLVM JIT.
117 struct draw_context
*
118 draw_create(struct pipe_context
*pipe
)
120 return draw_create_context(pipe
, NULL
, TRUE
);
124 #ifdef LLVM_AVAILABLE
125 struct draw_context
*
126 draw_create_with_llvm_context(struct pipe_context
*pipe
,
129 return draw_create_context(pipe
, context
, TRUE
);
134 * Create a new draw context, without LLVM JIT.
136 struct draw_context
*
137 draw_create_no_llvm(struct pipe_context
*pipe
)
139 return draw_create_context(pipe
, NULL
, FALSE
);
143 boolean
draw_init(struct draw_context
*draw
)
146 * Note that several functions compute the clipmask of the predefined
147 * formats with hardcoded formulas instead of using these. So modifications
148 * here must be reflected there too.
151 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
152 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
153 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
154 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
155 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
156 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
157 draw
->clip_xy
= TRUE
;
160 draw
->pt
.user
.planes
= (float (*) [DRAW_TOTAL_CLIP_PLANES
][4]) &(draw
->plane
[0]);
161 draw
->pt
.user
.eltMax
= ~0;
163 if (!draw_pipeline_init( draw
))
166 if (!draw_pt_init( draw
))
169 if (!draw_vs_init( draw
))
172 if (!draw_gs_init( draw
))
175 draw
->quads_always_flatshade_last
= !draw
->pipe
->screen
->get_param(
176 draw
->pipe
->screen
, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION
);
178 draw
->floating_point_depth
= false;
184 * Called whenever we're starting to draw a new instance.
185 * Some internal structures don't want to have to reset internal
186 * members on each invocation (because their state might have to persist
187 * between multiple primitive restart rendering call) but might have to
188 * for each new instance.
189 * This is particularly the case for primitive id's in geometry shader.
191 void draw_new_instance(struct draw_context
*draw
)
193 draw_geometry_shader_new_instance(draw
->gs
.geometry_shader
);
194 draw_prim_assembler_new_instance(draw
->ia
);
198 void draw_destroy( struct draw_context
*draw
)
200 struct pipe_context
*pipe
;
208 /* free any rasterizer CSOs that we may have created.
210 for (i
= 0; i
< 2; i
++) {
211 for (j
= 0; j
< 2; j
++) {
212 if (draw
->rasterizer_no_cull
[i
][j
]) {
213 pipe
->delete_rasterizer_state(pipe
, draw
->rasterizer_no_cull
[i
][j
]);
218 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++)
219 pipe_vertex_buffer_unreference(&draw
->pt
.vertex_buffer
[i
]);
221 /* Not so fast -- we're just borrowing this at the moment.
224 draw->render->destroy( draw->render );
227 draw_prim_assembler_destroy(draw
->ia
);
228 draw_pipeline_destroy( draw
);
229 draw_pt_destroy( draw
);
230 draw_vs_destroy( draw
);
231 draw_gs_destroy( draw
);
232 #ifdef LLVM_AVAILABLE
234 draw_llvm_destroy( draw
->llvm
);
242 void draw_flush( struct draw_context
*draw
)
244 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
249 * Specify the depth stencil format for the draw pipeline. This function
250 * determines the Minimum Resolvable Depth factor for polygon offset.
251 * This factor potentially depends on the number of Z buffer bits,
252 * the rasterization algorithm and the arithmetic performed on Z
253 * values between vertex shading and rasterization.
255 void draw_set_zs_format(struct draw_context
*draw
, enum pipe_format format
)
257 const struct util_format_description
*desc
= util_format_description(format
);
259 draw
->floating_point_depth
=
260 (util_get_depth_format_type(desc
) == UTIL_FORMAT_TYPE_FLOAT
);
262 draw
->mrd
= util_get_depth_format_mrd(desc
);
267 draw_is_vs_window_space(struct draw_context
*draw
)
269 if (draw
->vs
.vertex_shader
) {
270 struct tgsi_shader_info
*info
= &draw
->vs
.vertex_shader
->info
;
272 return info
->properties
[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
] != 0;
279 draw_update_clip_flags(struct draw_context
*draw
)
281 bool window_space
= draw_is_vs_window_space(draw
);
283 draw
->clip_xy
= !draw
->driver
.bypass_clip_xy
&& !window_space
;
284 draw
->guard_band_xy
= (!draw
->driver
.bypass_clip_xy
&&
285 draw
->driver
.guard_band_xy
);
286 draw
->clip_z
= (!draw
->driver
.bypass_clip_z
&&
287 draw
->rasterizer
&& draw
->rasterizer
->depth_clip_near
) &&
289 draw
->clip_user
= draw
->rasterizer
&&
290 draw
->rasterizer
->clip_plane_enable
!= 0 &&
292 draw
->guard_band_points_xy
= draw
->guard_band_xy
||
293 (draw
->driver
.bypass_clip_points
&&
295 draw
->rasterizer
->point_tri_clip
));
300 draw_update_viewport_flags(struct draw_context
*draw
)
302 bool window_space
= draw_is_vs_window_space(draw
);
304 draw
->bypass_viewport
= window_space
|| draw
->identity_viewport
;
309 * Register new primitive rasterization/rendering state.
310 * This causes the drawing pipeline to be rebuilt.
312 void draw_set_rasterizer_state( struct draw_context
*draw
,
313 const struct pipe_rasterizer_state
*raster
,
316 if (!draw
->suspend_flushing
) {
317 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
319 draw
->rasterizer
= raster
;
320 draw
->rast_handle
= rast_handle
;
321 draw_update_clip_flags(draw
);
325 /* With a little more work, llvmpipe will be able to turn this off and
326 * do its own x/y clipping.
328 * Some hardware can turn off clipping altogether - in particular any
329 * hardware with a TNL unit can do its own clipping, even if it is
330 * relying on the draw module for some other reason.
331 * Setting bypass_clip_points to achieve d3d-style point clipping (the driver
332 * will need to do the "vp scissoring") _requires_ the driver to implement
333 * wide points / point sprites itself (points will still be clipped if rasterizer
334 * point_tri_clip isn't set). Only relevant if bypass_clip_xy isn't set.
336 void draw_set_driver_clipping( struct draw_context
*draw
,
337 boolean bypass_clip_xy
,
338 boolean bypass_clip_z
,
339 boolean guard_band_xy
,
340 boolean bypass_clip_points
)
342 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
344 draw
->driver
.bypass_clip_xy
= bypass_clip_xy
;
345 draw
->driver
.bypass_clip_z
= bypass_clip_z
;
346 draw
->driver
.guard_band_xy
= guard_band_xy
;
347 draw
->driver
.bypass_clip_points
= bypass_clip_points
;
348 draw_update_clip_flags(draw
);
353 * Plug in the primitive rendering/rasterization stage (which is the last
354 * stage in the drawing pipeline).
355 * This is provided by the device driver.
357 void draw_set_rasterize_stage( struct draw_context
*draw
,
358 struct draw_stage
*stage
)
360 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
362 draw
->pipeline
.rasterize
= stage
;
367 * Set the draw module's clipping state.
369 void draw_set_clip_state( struct draw_context
*draw
,
370 const struct pipe_clip_state
*clip
)
372 draw_do_flush(draw
, DRAW_FLUSH_PARAMETER_CHANGE
);
374 memcpy(&draw
->plane
[6], clip
->ucp
, sizeof(clip
->ucp
));
379 * Set the draw module's viewport state.
381 void draw_set_viewport_states( struct draw_context
*draw
,
383 unsigned num_viewports
,
384 const struct pipe_viewport_state
*vps
)
386 const struct pipe_viewport_state
*viewport
= vps
;
387 draw_do_flush(draw
, DRAW_FLUSH_PARAMETER_CHANGE
);
389 debug_assert(start_slot
< PIPE_MAX_VIEWPORTS
);
390 debug_assert((start_slot
+ num_viewports
) <= PIPE_MAX_VIEWPORTS
);
392 memcpy(draw
->viewports
+ start_slot
, vps
,
393 sizeof(struct pipe_viewport_state
) * num_viewports
);
395 draw
->identity_viewport
= (num_viewports
== 1) &&
396 (viewport
->scale
[0] == 1.0f
&&
397 viewport
->scale
[1] == 1.0f
&&
398 viewport
->scale
[2] == 1.0f
&&
399 viewport
->translate
[0] == 0.0f
&&
400 viewport
->translate
[1] == 0.0f
&&
401 viewport
->translate
[2] == 0.0f
);
402 draw_update_viewport_flags(draw
);
408 draw_set_vertex_buffers(struct draw_context
*draw
,
409 unsigned start_slot
, unsigned count
,
410 const struct pipe_vertex_buffer
*buffers
)
412 assert(start_slot
+ count
<= PIPE_MAX_ATTRIBS
);
414 util_set_vertex_buffers_count(draw
->pt
.vertex_buffer
,
415 &draw
->pt
.nr_vertex_buffers
,
416 buffers
, start_slot
, count
);
421 draw_set_vertex_elements(struct draw_context
*draw
,
423 const struct pipe_vertex_element
*elements
)
425 assert(count
<= PIPE_MAX_ATTRIBS
);
427 /* We could improve this by only flushing the frontend and the fetch part
428 * of the middle. This would avoid recalculating the emit keys.*/
429 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
431 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
432 draw
->pt
.nr_vertex_elements
= count
;
437 * Tell drawing context where to find mapped vertex buffers.
440 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
441 unsigned attr
, const void *buffer
,
444 draw
->pt
.user
.vbuffer
[attr
].map
= buffer
;
445 draw
->pt
.user
.vbuffer
[attr
].size
= size
;
450 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
451 enum pipe_shader_type shader_type
,
456 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
457 shader_type
== PIPE_SHADER_GEOMETRY
);
458 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
460 draw_do_flush(draw
, DRAW_FLUSH_PARAMETER_CHANGE
);
462 switch (shader_type
) {
463 case PIPE_SHADER_VERTEX
:
464 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
465 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
467 case PIPE_SHADER_GEOMETRY
:
468 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
469 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
472 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
477 draw_set_mapped_shader_buffer(struct draw_context
*draw
,
478 enum pipe_shader_type shader_type
,
483 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
484 shader_type
== PIPE_SHADER_GEOMETRY
);
485 debug_assert(slot
< PIPE_MAX_SHADER_BUFFERS
);
487 draw_do_flush(draw
, DRAW_FLUSH_PARAMETER_CHANGE
);
489 switch (shader_type
) {
490 case PIPE_SHADER_VERTEX
:
491 draw
->pt
.user
.vs_ssbos
[slot
] = buffer
;
492 draw
->pt
.user
.vs_ssbos_size
[slot
] = size
;
494 case PIPE_SHADER_GEOMETRY
:
495 draw
->pt
.user
.gs_ssbos
[slot
] = buffer
;
496 draw
->pt
.user
.gs_ssbos_size
[slot
] = size
;
499 assert(0 && "invalid shader type in draw_set_mapped_shader_buffer");
504 * Tells the draw module to draw points with triangles if their size
505 * is greater than this threshold.
508 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
510 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
511 draw
->pipeline
.wide_point_threshold
= threshold
;
516 * Should the draw module handle point->quad conversion for drawing sprites?
519 draw_wide_point_sprites(struct draw_context
*draw
, boolean draw_sprite
)
521 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
522 draw
->pipeline
.wide_point_sprites
= draw_sprite
;
527 * Tells the draw module to draw lines with triangles if their width
528 * is greater than this threshold.
531 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
533 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
534 draw
->pipeline
.wide_line_threshold
= roundf(threshold
);
539 * Tells the draw module whether or not to implement line stipple.
542 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
544 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
545 draw
->pipeline
.line_stipple
= enable
;
550 * Tells draw module whether to convert points to quads for sprite mode.
553 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
555 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
556 draw
->pipeline
.point_sprite
= enable
;
561 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
563 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
564 draw
->force_passthrough
= enable
;
570 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
573 * This is used by some of the optional draw module stages such
574 * as wide_point which may need to allocate additional generic/texcoord
578 draw_alloc_extra_vertex_attrib(struct draw_context
*draw
,
579 uint semantic_name
, uint semantic_index
)
585 slot
= draw_find_shader_output(draw
, semantic_name
, semantic_index
);
590 num_outputs
= draw_current_shader_outputs(draw
);
591 n
= draw
->extra_shader_outputs
.num
;
593 assert(n
< ARRAY_SIZE(draw
->extra_shader_outputs
.semantic_name
));
595 draw
->extra_shader_outputs
.semantic_name
[n
] = semantic_name
;
596 draw
->extra_shader_outputs
.semantic_index
[n
] = semantic_index
;
597 draw
->extra_shader_outputs
.slot
[n
] = num_outputs
+ n
;
598 draw
->extra_shader_outputs
.num
++;
600 return draw
->extra_shader_outputs
.slot
[n
];
605 * Remove all extra vertex attributes that were allocated with
606 * draw_alloc_extra_vertex_attrib().
609 draw_remove_extra_vertex_attribs(struct draw_context
*draw
)
611 draw
->extra_shader_outputs
.num
= 0;
616 * If a geometry shader is present, return its info, else the vertex shader's
619 struct tgsi_shader_info
*
620 draw_get_shader_info(const struct draw_context
*draw
)
623 if (draw
->gs
.geometry_shader
) {
624 return &draw
->gs
.geometry_shader
->info
;
626 return &draw
->vs
.vertex_shader
->info
;
631 * Prepare outputs slots from the draw module
633 * Certain parts of the draw module can emit additional
634 * outputs that can be quite useful to the backends, a good
635 * example of it is the process of decomposing primitives
636 * into wireframes (aka. lines) which normally would lose
637 * the face-side information, but using this method we can
638 * inject another shader output which passes the original
639 * face side information to the backend.
642 draw_prepare_shader_outputs(struct draw_context
*draw
)
644 draw_remove_extra_vertex_attribs(draw
);
645 draw_prim_assembler_prepare_outputs(draw
->ia
);
646 draw_unfilled_prepare_outputs(draw
, draw
->pipeline
.unfilled
);
647 if (draw
->pipeline
.aapoint
)
648 draw_aapoint_prepare_outputs(draw
, draw
->pipeline
.aapoint
);
649 if (draw
->pipeline
.aaline
)
650 draw_aaline_prepare_outputs(draw
, draw
->pipeline
.aaline
);
654 * Ask the draw module for the location/slot of the given vertex attribute in
655 * a post-transformed vertex.
657 * With this function, drivers that use the draw module should have no reason
658 * to track the current vertex/geometry shader.
660 * Note that the draw module may sometimes generate vertices with extra
661 * attributes (such as texcoords for AA lines). The driver can call this
662 * function to find those attributes.
664 * -1 is returned if the attribute is not found since this is
665 * an undefined situation. Note, that zero is valid and can
666 * be used by any of the attributes, because position is not
667 * required to be attribute 0 or even at all present.
670 draw_find_shader_output(const struct draw_context
*draw
,
671 uint semantic_name
, uint semantic_index
)
673 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
676 for (i
= 0; i
< info
->num_outputs
; i
++) {
677 if (info
->output_semantic_name
[i
] == semantic_name
&&
678 info
->output_semantic_index
[i
] == semantic_index
)
682 /* Search the extra vertex attributes */
683 for (i
= 0; i
< draw
->extra_shader_outputs
.num
; i
++) {
684 if (draw
->extra_shader_outputs
.semantic_name
[i
] == semantic_name
&&
685 draw
->extra_shader_outputs
.semantic_index
[i
] == semantic_index
) {
686 return draw
->extra_shader_outputs
.slot
[i
];
695 * Return total number of the shader outputs. This function is similar to
696 * draw_current_shader_outputs() but this function also counts any extra
697 * vertex/geometry output attributes that may be filled in by some draw
698 * stages (such as AA point, AA line).
700 * If geometry shader is present, its output will be returned,
701 * if not vertex shader is used.
704 draw_num_shader_outputs(const struct draw_context
*draw
)
706 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
709 count
= info
->num_outputs
;
710 count
+= draw
->extra_shader_outputs
.num
;
717 * Return total number of the vertex shader outputs. This function
718 * also counts any extra vertex output attributes that may
719 * be filled in by some draw stages (such as AA point, AA line,
723 draw_total_vs_outputs(const struct draw_context
*draw
)
725 const struct tgsi_shader_info
*info
= &draw
->vs
.vertex_shader
->info
;
727 return info
->num_outputs
+ draw
->extra_shader_outputs
.num
;
731 * Return total number of the geometry shader outputs. This function
732 * also counts any extra geometry output attributes that may
733 * be filled in by some draw stages (such as AA point, AA line, front
737 draw_total_gs_outputs(const struct draw_context
*draw
)
739 const struct tgsi_shader_info
*info
;
741 if (!draw
->gs
.geometry_shader
)
744 info
= &draw
->gs
.geometry_shader
->info
;
746 return info
->num_outputs
+ draw
->extra_shader_outputs
.num
;
751 * Provide TGSI sampler objects for vertex/geometry shaders that use
752 * texture fetches. This state only needs to be set once per context.
753 * This might only be used by software drivers for the time being.
756 draw_texture_sampler(struct draw_context
*draw
,
757 enum pipe_shader_type shader
,
758 struct tgsi_sampler
*sampler
)
760 if (shader
== PIPE_SHADER_VERTEX
) {
761 draw
->vs
.tgsi
.sampler
= sampler
;
763 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
764 draw
->gs
.tgsi
.sampler
= sampler
;
769 * Provide TGSI image objects for vertex/geometry shaders that use
770 * texture fetches. This state only needs to be set once per context.
771 * This might only be used by software drivers for the time being.
774 draw_image(struct draw_context
*draw
,
775 enum pipe_shader_type shader
,
776 struct tgsi_image
*image
)
778 if (shader
== PIPE_SHADER_VERTEX
) {
779 draw
->vs
.tgsi
.image
= image
;
781 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
782 draw
->gs
.tgsi
.image
= image
;
787 * Provide TGSI buffer objects for vertex/geometry shaders that use
788 * load/store/atomic ops. This state only needs to be set once per context.
789 * This might only be used by software drivers for the time being.
792 draw_buffer(struct draw_context
*draw
,
793 enum pipe_shader_type shader
,
794 struct tgsi_buffer
*buffer
)
796 if (shader
== PIPE_SHADER_VERTEX
) {
797 draw
->vs
.tgsi
.buffer
= buffer
;
799 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
800 draw
->gs
.tgsi
.buffer
= buffer
;
805 void draw_set_render( struct draw_context
*draw
,
806 struct vbuf_render
*render
)
808 draw
->render
= render
;
813 * Tell the draw module where vertex indexes/elements are located, and
814 * their size (in bytes).
817 draw_set_indexes(struct draw_context
*draw
,
818 const void *elements
, unsigned elem_size
,
819 unsigned elem_buffer_space
)
821 assert(elem_size
== 0 ||
825 draw
->pt
.user
.elts
= elements
;
826 draw
->pt
.user
.eltSizeIB
= elem_size
;
828 draw
->pt
.user
.eltMax
= elem_buffer_space
/ elem_size
;
830 draw
->pt
.user
.eltMax
= 0;
836 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
838 if (!draw
->suspend_flushing
)
840 assert(!draw
->flushing
); /* catch inadvertant recursion */
842 draw
->flushing
= TRUE
;
844 draw_pipeline_flush( draw
, flags
);
846 draw_pt_flush( draw
, flags
);
848 draw
->flushing
= FALSE
;
854 * Return the number of output attributes produced by the geometry
855 * shader, if present. If no geometry shader, return the number of
856 * outputs from the vertex shader.
857 * \sa draw_num_shader_outputs
860 draw_current_shader_outputs(const struct draw_context
*draw
)
862 if (draw
->gs
.geometry_shader
)
863 return draw
->gs
.num_gs_outputs
;
864 return draw
->vs
.num_vs_outputs
;
869 * Return the index of the shader output which will contain the
873 draw_current_shader_position_output(const struct draw_context
*draw
)
875 if (draw
->gs
.geometry_shader
)
876 return draw
->gs
.position_output
;
877 return draw
->vs
.position_output
;
882 * Return the index of the shader output which will contain the
886 draw_current_shader_viewport_index_output(const struct draw_context
*draw
)
888 if (draw
->gs
.geometry_shader
)
889 return draw
->gs
.geometry_shader
->viewport_index_output
;
890 return draw
->vs
.vertex_shader
->viewport_index_output
;
894 * Returns true if there's a geometry shader bound and the geometry
895 * shader writes out a viewport index.
898 draw_current_shader_uses_viewport_index(const struct draw_context
*draw
)
900 if (draw
->gs
.geometry_shader
)
901 return draw
->gs
.geometry_shader
->info
.writes_viewport_index
;
902 return draw
->vs
.vertex_shader
->info
.writes_viewport_index
;
907 * Return the index of the shader output which will contain the
908 * clip vertex position.
909 * Note we don't support clipvertex output in the gs. For clipping
910 * to work correctly hence we return ordinary position output instead.
913 draw_current_shader_clipvertex_output(const struct draw_context
*draw
)
915 if (draw
->gs
.geometry_shader
)
916 return draw
->gs
.position_output
;
917 return draw
->vs
.clipvertex_output
;
921 draw_current_shader_ccdistance_output(const struct draw_context
*draw
, int index
)
923 debug_assert(index
< PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT
);
924 if (draw
->gs
.geometry_shader
)
925 return draw
->gs
.geometry_shader
->ccdistance_output
[index
];
926 return draw
->vs
.ccdistance_output
[index
];
931 draw_current_shader_num_written_clipdistances(const struct draw_context
*draw
)
933 if (draw
->gs
.geometry_shader
)
934 return draw
->gs
.geometry_shader
->info
.num_written_clipdistance
;
935 return draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
939 draw_current_shader_num_written_culldistances(const struct draw_context
*draw
)
941 if (draw
->gs
.geometry_shader
)
942 return draw
->gs
.geometry_shader
->info
.num_written_culldistance
;
943 return draw
->vs
.vertex_shader
->info
.num_written_culldistance
;
947 * Return a pointer/handle for a driver/CSO rasterizer object which
948 * disabled culling, stippling, unfilled tris, etc.
949 * This is used by some pipeline stages (such as wide_point, aa_line
950 * and aa_point) which convert points/lines into triangles. In those
951 * cases we don't want to accidentally cull the triangles.
953 * \param scissor should the rasterizer state enable scissoring?
954 * \param flatshade should the rasterizer state use flat shading?
955 * \return rasterizer CSO handle
958 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
962 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
964 struct pipe_context
*pipe
= draw
->pipe
;
965 struct pipe_rasterizer_state rast
;
967 memset(&rast
, 0, sizeof(rast
));
968 rast
.scissor
= scissor
;
969 rast
.flatshade
= flatshade
;
971 rast
.half_pixel_center
= draw
->rasterizer
->half_pixel_center
;
972 rast
.bottom_edge_rule
= draw
->rasterizer
->bottom_edge_rule
;
973 rast
.clip_halfz
= draw
->rasterizer
->clip_halfz
;
975 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
976 pipe
->create_rasterizer_state(pipe
, &rast
);
978 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
982 draw_set_mapped_so_targets(struct draw_context
*draw
,
984 struct draw_so_target
*targets
[PIPE_MAX_SO_BUFFERS
])
988 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
990 for (i
= 0; i
< num_targets
; i
++)
991 draw
->so
.targets
[i
] = targets
[i
];
992 for (i
= num_targets
; i
< PIPE_MAX_SO_BUFFERS
; i
++)
993 draw
->so
.targets
[i
] = NULL
;
995 draw
->so
.num_targets
= num_targets
;
999 draw_set_sampler_views(struct draw_context
*draw
,
1000 enum pipe_shader_type shader_stage
,
1001 struct pipe_sampler_view
**views
,
1006 debug_assert(shader_stage
< PIPE_SHADER_TYPES
);
1007 debug_assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
1009 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
1011 for (i
= 0; i
< num
; ++i
)
1012 draw
->sampler_views
[shader_stage
][i
] = views
[i
];
1013 for (i
= num
; i
< draw
->num_sampler_views
[shader_stage
]; ++i
)
1014 draw
->sampler_views
[shader_stage
][i
] = NULL
;
1016 draw
->num_sampler_views
[shader_stage
] = num
;
1020 draw_set_samplers(struct draw_context
*draw
,
1021 enum pipe_shader_type shader_stage
,
1022 struct pipe_sampler_state
**samplers
,
1027 debug_assert(shader_stage
< PIPE_SHADER_TYPES
);
1028 debug_assert(num
<= PIPE_MAX_SAMPLERS
);
1030 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
1032 for (i
= 0; i
< num
; ++i
)
1033 draw
->samplers
[shader_stage
][i
] = samplers
[i
];
1034 for (i
= num
; i
< PIPE_MAX_SAMPLERS
; ++i
)
1035 draw
->samplers
[shader_stage
][i
] = NULL
;
1037 draw
->num_samplers
[shader_stage
] = num
;
1039 #ifdef LLVM_AVAILABLE
1041 draw_llvm_set_sampler_state(draw
, shader_stage
);
1046 draw_set_images(struct draw_context
*draw
,
1047 enum pipe_shader_type shader_stage
,
1048 struct pipe_image_view
*views
,
1053 debug_assert(shader_stage
< PIPE_SHADER_TYPES
);
1054 debug_assert(num
<= PIPE_MAX_SHADER_IMAGES
);
1056 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
1058 for (i
= 0; i
< num
; ++i
)
1059 draw
->images
[shader_stage
][i
] = &views
[i
];
1060 for (i
= num
; i
< draw
->num_sampler_views
[shader_stage
]; ++i
)
1061 draw
->images
[shader_stage
][i
] = NULL
;
1063 draw
->num_images
[shader_stage
] = num
;
1067 draw_set_mapped_texture(struct draw_context
*draw
,
1068 enum pipe_shader_type shader_stage
,
1070 uint32_t width
, uint32_t height
, uint32_t depth
,
1071 uint32_t first_level
, uint32_t last_level
,
1072 const void *base_ptr
,
1073 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1074 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1075 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1077 #ifdef LLVM_AVAILABLE
1079 draw_llvm_set_mapped_texture(draw
,
1082 width
, height
, depth
, first_level
,
1083 last_level
, base_ptr
,
1084 row_stride
, img_stride
, mip_offsets
);
1089 draw_set_mapped_image(struct draw_context
*draw
,
1090 enum pipe_shader_type shader_stage
,
1092 uint32_t width
, uint32_t height
, uint32_t depth
,
1093 const void *base_ptr
,
1094 uint32_t row_stride
,
1095 uint32_t img_stride
)
1097 #ifdef LLVM_AVAILABLE
1099 draw_llvm_set_mapped_image(draw
,
1102 width
, height
, depth
,
1104 row_stride
, img_stride
);
1109 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1110 * different ways of setting textures, and drivers typically only support one.
1113 draw_get_shader_param_no_llvm(enum pipe_shader_type shader
,
1114 enum pipe_shader_cap param
)
1117 case PIPE_SHADER_VERTEX
:
1118 case PIPE_SHADER_GEOMETRY
:
1119 return tgsi_exec_get_shader_param(param
);
1126 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1127 * different ways of setting textures, and drivers typically only support one.
1128 * Drivers requesting a draw context explicitly without llvm must call
1129 * draw_get_shader_param_no_llvm instead.
1132 draw_get_shader_param(enum pipe_shader_type shader
, enum pipe_shader_cap param
)
1135 #ifdef LLVM_AVAILABLE
1136 if (draw_get_option_use_llvm()) {
1138 case PIPE_SHADER_VERTEX
:
1139 case PIPE_SHADER_GEOMETRY
:
1140 return gallivm_get_shader_param(param
);
1147 return draw_get_shader_param_no_llvm(shader
, param
);
1151 * Enables or disables collection of statistics.
1153 * Draw module is capable of generating statistics for the vertex
1154 * processing pipeline. Collection of that data isn't free and so
1155 * it's disabled by default. The users of the module can enable
1156 * (or disable) this functionality through this function.
1157 * The actual data will be emitted through the VBUF interface,
1158 * the 'pipeline_statistics' callback to be exact.
1161 draw_collect_pipeline_statistics(struct draw_context
*draw
,
1164 draw
->collect_statistics
= enable
;
1168 * Enable/disable primitives generated gathering.
1170 void draw_collect_primitives_generated(struct draw_context
*draw
,
1173 draw
->collect_primgen
= enable
;
1177 * Computes clipper invocation statistics.
1179 * Figures out how many primitives would have been
1180 * sent to the clipper given the specified
1184 draw_stats_clipper_primitives(struct draw_context
*draw
,
1185 const struct draw_prim_info
*prim_info
)
1187 if (draw
->collect_statistics
) {
1189 for (i
= 0; i
< prim_info
->primitive_count
; i
++) {
1190 draw
->statistics
.c_invocations
+=
1191 u_decomposed_prims_for_vertices(prim_info
->prim
,
1192 prim_info
->primitive_lengths
[i
]);
1199 * Returns true if the draw module will inject the frontface
1200 * info into the outputs.
1202 * Given the specified primitive and rasterizer state
1203 * the function will figure out if the draw module
1204 * will inject the front-face information into shader
1205 * outputs. This is done to preserve the front-facing
1206 * info when decomposing primitives into wireframes.
1209 draw_will_inject_frontface(const struct draw_context
*draw
)
1211 unsigned reduced_prim
= u_reduced_prim(draw
->pt
.prim
);
1212 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
1214 if (reduced_prim
!= PIPE_PRIM_TRIANGLES
) {
1219 (rast
->fill_front
!= PIPE_POLYGON_MODE_FILL
||
1220 rast
->fill_back
!= PIPE_POLYGON_MODE_FILL
));