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 "draw_context.h"
44 #include "gallivm/lp_bld_init.h"
45 #include "draw_llvm.h"
48 draw_get_option_use_llvm(void)
50 static boolean first
= TRUE
;
54 value
= debug_get_bool_option("DRAW_USE_LLVM", TRUE
);
58 /* require SSE2 due to LLVM PR6960. */
59 if (!util_cpu_caps
.has_sse2
)
69 * Create new draw module context with gallivm state for LLVM JIT.
71 static struct draw_context
*
72 draw_create_context(struct pipe_context
*pipe
, boolean try_llvm
,
73 struct gallivm_state
*gallivm
)
75 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
80 if (try_llvm
&& draw_get_option_use_llvm()) {
82 gallivm
= gallivm_create();
83 draw
->own_gallivm
= gallivm
;
89 draw
->llvm
= draw_llvm_create(draw
, gallivm
);
104 draw_destroy( draw
);
111 * Create new draw module context, with LLVM JIT.
113 struct draw_context
*
114 draw_create(struct pipe_context
*pipe
)
116 return draw_create_context(pipe
, TRUE
, NULL
);
121 * Create a new draw context, without LLVM JIT.
123 struct draw_context
*
124 draw_create_no_llvm(struct pipe_context
*pipe
)
126 return draw_create_context(pipe
, FALSE
, NULL
);
131 * Create new draw module context with gallivm state for LLVM JIT.
133 struct draw_context
*
134 draw_create_gallivm(struct pipe_context
*pipe
, struct gallivm_state
*gallivm
)
136 return draw_create_context(pipe
, TRUE
, gallivm
);
140 boolean
draw_init(struct draw_context
*draw
)
143 * Note that several functions compute the clipmask of the predefined
144 * formats with hardcoded formulas instead of using these. So modifications
145 * here must be reflected there too.
148 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
149 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
150 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
151 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
152 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
153 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
154 draw
->clip_xy
= TRUE
;
157 draw
->pt
.user
.planes
= (float (*) [DRAW_TOTAL_CLIP_PLANES
][4]) &(draw
->plane
[0]);
158 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
161 if (!draw_pipeline_init( draw
))
164 if (!draw_pt_init( draw
))
167 if (!draw_vs_init( draw
))
170 if (!draw_gs_init( draw
))
177 void draw_destroy( struct draw_context
*draw
)
179 struct pipe_context
*pipe
;
187 /* free any rasterizer CSOs that we may have created.
189 for (i
= 0; i
< 2; i
++) {
190 for (j
= 0; j
< 2; j
++) {
191 if (draw
->rasterizer_no_cull
[i
][j
]) {
192 pipe
->delete_rasterizer_state(pipe
, draw
->rasterizer_no_cull
[i
][j
]);
197 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
198 pipe_resource_reference(&draw
->pt
.vertex_buffer
[i
].buffer
, NULL
);
201 /* Not so fast -- we're just borrowing this at the moment.
204 draw->render->destroy( draw->render );
207 draw_pipeline_destroy( draw
);
208 draw_pt_destroy( draw
);
209 draw_vs_destroy( draw
);
210 draw_gs_destroy( draw
);
213 draw_llvm_destroy( draw
->llvm
);
215 if (draw
->own_gallivm
)
216 gallivm_destroy(draw
->own_gallivm
);
224 void draw_flush( struct draw_context
*draw
)
226 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
231 * Specify the Minimum Resolvable Depth factor for polygon offset.
232 * This factor potentially depends on the number of Z buffer bits,
233 * the rasterization algorithm and the arithmetic performed on Z
234 * values between vertex shading and rasterization. It will vary
235 * from one driver to another.
237 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
243 static void update_clip_flags( struct draw_context
*draw
)
245 draw
->clip_xy
= !draw
->driver
.bypass_clip_xy
;
246 draw
->guard_band_xy
= (!draw
->driver
.bypass_clip_xy
&&
247 draw
->driver
.guard_band_xy
);
248 draw
->clip_z
= (!draw
->driver
.bypass_clip_z
&&
249 draw
->rasterizer
&& draw
->rasterizer
->depth_clip
);
250 draw
->clip_user
= draw
->rasterizer
&&
251 draw
->rasterizer
->clip_plane_enable
!= 0;
255 * Register new primitive rasterization/rendering state.
256 * This causes the drawing pipeline to be rebuilt.
258 void draw_set_rasterizer_state( struct draw_context
*draw
,
259 const struct pipe_rasterizer_state
*raster
,
262 if (!draw
->suspend_flushing
) {
263 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
265 draw
->rasterizer
= raster
;
266 draw
->rast_handle
= rast_handle
;
267 update_clip_flags(draw
);
271 /* With a little more work, llvmpipe will be able to turn this off and
272 * do its own x/y clipping.
274 * Some hardware can turn off clipping altogether - in particular any
275 * hardware with a TNL unit can do its own clipping, even if it is
276 * relying on the draw module for some other reason.
278 void draw_set_driver_clipping( struct draw_context
*draw
,
279 boolean bypass_clip_xy
,
280 boolean bypass_clip_z
,
281 boolean guard_band_xy
)
283 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
285 draw
->driver
.bypass_clip_xy
= bypass_clip_xy
;
286 draw
->driver
.bypass_clip_z
= bypass_clip_z
;
287 draw
->driver
.guard_band_xy
= guard_band_xy
;
288 update_clip_flags(draw
);
293 * Plug in the primitive rendering/rasterization stage (which is the last
294 * stage in the drawing pipeline).
295 * This is provided by the device driver.
297 void draw_set_rasterize_stage( struct draw_context
*draw
,
298 struct draw_stage
*stage
)
300 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
302 draw
->pipeline
.rasterize
= stage
;
307 * Set the draw module's clipping state.
309 void draw_set_clip_state( struct draw_context
*draw
,
310 const struct pipe_clip_state
*clip
)
312 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
314 memcpy(&draw
->plane
[6], clip
->ucp
, sizeof(clip
->ucp
));
319 * Set the draw module's viewport state.
321 void draw_set_viewport_state( struct draw_context
*draw
,
322 const struct pipe_viewport_state
*viewport
)
324 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
325 draw
->viewport
= *viewport
; /* struct copy */
326 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
327 viewport
->scale
[1] == 1.0f
&&
328 viewport
->scale
[2] == 1.0f
&&
329 viewport
->scale
[3] == 1.0f
&&
330 viewport
->translate
[0] == 0.0f
&&
331 viewport
->translate
[1] == 0.0f
&&
332 viewport
->translate
[2] == 0.0f
&&
333 viewport
->translate
[3] == 0.0f
);
335 draw_vs_set_viewport( draw
, viewport
);
341 draw_set_vertex_buffers(struct draw_context
*draw
,
343 const struct pipe_vertex_buffer
*buffers
)
345 assert(count
<= PIPE_MAX_ATTRIBS
);
347 util_copy_vertex_buffers(draw
->pt
.vertex_buffer
,
348 &draw
->pt
.nr_vertex_buffers
,
354 draw_set_vertex_elements(struct draw_context
*draw
,
356 const struct pipe_vertex_element
*elements
)
358 assert(count
<= PIPE_MAX_ATTRIBS
);
360 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
361 draw
->pt
.nr_vertex_elements
= count
;
366 * Tell drawing context where to find mapped vertex buffers.
369 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
370 unsigned attr
, const void *buffer
)
372 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
377 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
378 unsigned shader_type
,
383 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
384 shader_type
== PIPE_SHADER_GEOMETRY
);
385 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
387 switch (shader_type
) {
388 case PIPE_SHADER_VERTEX
:
389 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
390 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
391 draw_vs_set_constants(draw
, slot
, buffer
, size
);
393 case PIPE_SHADER_GEOMETRY
:
394 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
395 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
396 draw_gs_set_constants(draw
, slot
, buffer
, size
);
399 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
405 * Tells the draw module to draw points with triangles if their size
406 * is greater than this threshold.
409 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
411 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
412 draw
->pipeline
.wide_point_threshold
= threshold
;
417 * Should the draw module handle point->quad conversion for drawing sprites?
420 draw_wide_point_sprites(struct draw_context
*draw
, boolean draw_sprite
)
422 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
423 draw
->pipeline
.wide_point_sprites
= draw_sprite
;
428 * Tells the draw module to draw lines with triangles if their width
429 * is greater than this threshold.
432 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
434 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
435 draw
->pipeline
.wide_line_threshold
= roundf(threshold
);
440 * Tells the draw module whether or not to implement line stipple.
443 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
445 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
446 draw
->pipeline
.line_stipple
= enable
;
451 * Tells draw module whether to convert points to quads for sprite mode.
454 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
456 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
457 draw
->pipeline
.point_sprite
= enable
;
462 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
464 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
465 draw
->force_passthrough
= enable
;
471 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
474 * This is used by some of the optional draw module stages such
475 * as wide_point which may need to allocate additional generic/texcoord
479 draw_alloc_extra_vertex_attrib(struct draw_context
*draw
,
480 uint semantic_name
, uint semantic_index
)
486 slot
= draw_find_shader_output(draw
, semantic_name
, semantic_index
);
491 num_outputs
= draw_current_shader_outputs(draw
);
492 n
= draw
->extra_shader_outputs
.num
;
494 assert(n
< Elements(draw
->extra_shader_outputs
.semantic_name
));
496 draw
->extra_shader_outputs
.semantic_name
[n
] = semantic_name
;
497 draw
->extra_shader_outputs
.semantic_index
[n
] = semantic_index
;
498 draw
->extra_shader_outputs
.slot
[n
] = num_outputs
+ n
;
499 draw
->extra_shader_outputs
.num
++;
501 return draw
->extra_shader_outputs
.slot
[n
];
506 * Remove all extra vertex attributes that were allocated with
507 * draw_alloc_extra_vertex_attrib().
510 draw_remove_extra_vertex_attribs(struct draw_context
*draw
)
512 draw
->extra_shader_outputs
.num
= 0;
517 * If a geometry shader is present, return its info, else the vertex shader's
520 struct tgsi_shader_info
*
521 draw_get_shader_info(const struct draw_context
*draw
)
524 if (draw
->gs
.geometry_shader
) {
525 return &draw
->gs
.geometry_shader
->info
;
527 return &draw
->vs
.vertex_shader
->info
;
533 * Ask the draw module for the location/slot of the given vertex attribute in
534 * a post-transformed vertex.
536 * With this function, drivers that use the draw module should have no reason
537 * to track the current vertex/geometry shader.
539 * Note that the draw module may sometimes generate vertices with extra
540 * attributes (such as texcoords for AA lines). The driver can call this
541 * function to find those attributes.
543 * Zero is returned if the attribute is not found since this is
544 * a don't care / undefined situtation. Returning -1 would be a bit more
545 * work for the drivers.
548 draw_find_shader_output(const struct draw_context
*draw
,
549 uint semantic_name
, uint semantic_index
)
551 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
554 for (i
= 0; i
< info
->num_outputs
; i
++) {
555 if (info
->output_semantic_name
[i
] == semantic_name
&&
556 info
->output_semantic_index
[i
] == semantic_index
)
560 /* Search the extra vertex attributes */
561 for (i
= 0; i
< draw
->extra_shader_outputs
.num
; i
++) {
562 if (draw
->extra_shader_outputs
.semantic_name
[i
] == semantic_name
&&
563 draw
->extra_shader_outputs
.semantic_index
[i
] == semantic_index
) {
564 return draw
->extra_shader_outputs
.slot
[i
];
573 * Return total number of the shader outputs. This function is similar to
574 * draw_current_shader_outputs() but this function also counts any extra
575 * vertex/geometry output attributes that may be filled in by some draw
576 * stages (such as AA point, AA line).
578 * If geometry shader is present, its output will be returned,
579 * if not vertex shader is used.
582 draw_num_shader_outputs(const struct draw_context
*draw
)
584 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
587 count
= info
->num_outputs
;
588 count
+= draw
->extra_shader_outputs
.num
;
595 * Provide TGSI sampler objects for vertex/geometry shaders that use
597 * This might only be used by software drivers for the time being.
600 draw_texture_samplers(struct draw_context
*draw
,
603 struct tgsi_sampler
**samplers
)
605 if (shader
== PIPE_SHADER_VERTEX
) {
606 draw
->vs
.num_samplers
= num_samplers
;
607 draw
->vs
.samplers
= samplers
;
609 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
610 draw
->gs
.num_samplers
= num_samplers
;
611 draw
->gs
.samplers
= samplers
;
618 void draw_set_render( struct draw_context
*draw
,
619 struct vbuf_render
*render
)
621 draw
->render
= render
;
626 draw_set_index_buffer(struct draw_context
*draw
,
627 const struct pipe_index_buffer
*ib
)
630 memcpy(&draw
->pt
.index_buffer
, ib
, sizeof(draw
->pt
.index_buffer
));
632 memset(&draw
->pt
.index_buffer
, 0, sizeof(draw
->pt
.index_buffer
));
637 * Tell drawing context where to find mapped index/element buffer.
640 draw_set_mapped_index_buffer(struct draw_context
*draw
,
641 const void *elements
)
643 draw
->pt
.user
.elts
= elements
;
649 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
651 if (!draw
->suspend_flushing
)
653 assert(!draw
->flushing
); /* catch inadvertant recursion */
655 draw
->flushing
= TRUE
;
657 draw_pipeline_flush( draw
, flags
);
659 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
661 draw
->flushing
= FALSE
;
667 * Return the number of output attributes produced by the geometry
668 * shader, if present. If no geometry shader, return the number of
669 * outputs from the vertex shader.
670 * \sa draw_num_shader_outputs
673 draw_current_shader_outputs(const struct draw_context
*draw
)
675 if (draw
->gs
.geometry_shader
)
676 return draw
->gs
.num_gs_outputs
;
677 return draw
->vs
.num_vs_outputs
;
682 * Return the index of the shader output which will contain the
686 draw_current_shader_position_output(const struct draw_context
*draw
)
688 if (draw
->gs
.geometry_shader
)
689 return draw
->gs
.position_output
;
690 return draw
->vs
.position_output
;
695 * Return the index of the shader output which will contain the
699 draw_current_shader_clipvertex_output(const struct draw_context
*draw
)
701 return draw
->vs
.clipvertex_output
;
705 draw_current_shader_clipdistance_output(const struct draw_context
*draw
, int index
)
707 return draw
->vs
.clipdistance_output
[index
];
711 * Return a pointer/handle for a driver/CSO rasterizer object which
712 * disabled culling, stippling, unfilled tris, etc.
713 * This is used by some pipeline stages (such as wide_point, aa_line
714 * and aa_point) which convert points/lines into triangles. In those
715 * cases we don't want to accidentally cull the triangles.
717 * \param scissor should the rasterizer state enable scissoring?
718 * \param flatshade should the rasterizer state use flat shading?
719 * \return rasterizer CSO handle
722 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
726 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
728 struct pipe_context
*pipe
= draw
->pipe
;
729 struct pipe_rasterizer_state rast
;
731 memset(&rast
, 0, sizeof(rast
));
732 rast
.scissor
= scissor
;
733 rast
.flatshade
= flatshade
;
735 rast
.gl_rasterization_rules
= draw
->rasterizer
->gl_rasterization_rules
;
737 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
738 pipe
->create_rasterizer_state(pipe
, &rast
);
740 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
744 draw_set_mapped_so_targets(struct draw_context
*draw
,
746 struct draw_so_target
*targets
[PIPE_MAX_SO_BUFFERS
])
750 for (i
= 0; i
< num_targets
; i
++)
751 draw
->so
.targets
[i
] = targets
[i
];
752 for (i
= num_targets
; i
< PIPE_MAX_SO_BUFFERS
; i
++)
753 draw
->so
.targets
[i
] = NULL
;
755 draw
->so
.num_targets
= num_targets
;
759 draw_set_mapped_so_buffers(struct draw_context
*draw
,
760 void *buffers
[PIPE_MAX_SO_BUFFERS
],
761 unsigned num_buffers
)
766 draw_set_so_state(struct draw_context
*draw
,
767 struct pipe_stream_output_info
*state
)
769 memcpy(&draw
->so
.state
,
771 sizeof(struct pipe_stream_output_info
));
775 draw_set_sampler_views(struct draw_context
*draw
,
776 struct pipe_sampler_view
**views
,
781 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
783 for (i
= 0; i
< num
; ++i
)
784 draw
->sampler_views
[i
] = views
[i
];
785 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
786 draw
->sampler_views
[i
] = NULL
;
788 draw
->num_sampler_views
= num
;
792 draw_set_samplers(struct draw_context
*draw
,
793 struct pipe_sampler_state
**samplers
,
798 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
800 for (i
= 0; i
< num
; ++i
)
801 draw
->samplers
[i
] = samplers
[i
];
802 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
803 draw
->samplers
[i
] = NULL
;
805 draw
->num_samplers
= num
;
809 draw_llvm_set_sampler_state(draw
);
814 draw_set_mapped_texture(struct draw_context
*draw
,
815 unsigned sampler_idx
,
816 uint32_t width
, uint32_t height
, uint32_t depth
,
817 uint32_t first_level
, uint32_t last_level
,
818 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
819 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
820 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
824 draw_llvm_set_mapped_texture(draw
,
826 width
, height
, depth
, first_level
, last_level
,
827 row_stride
, img_stride
, data
);