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
)
70 * Create new draw module context.
73 draw_create(struct pipe_context
*pipe
)
75 return draw_create_gallivm(pipe
, NULL
);
81 * Create new draw module context with gallivm state for LLVM JIT.
84 draw_create_gallivm(struct pipe_context
*pipe
, struct gallivm_state
*gallivm
)
86 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
91 if (draw_get_option_use_llvm()) {
93 gallivm
= gallivm_create();
94 draw
->own_gallivm
= gallivm
;
98 draw
->llvm
= draw_llvm_create(draw
, gallivm
);
102 if (!draw_init(draw
))
110 draw_destroy( draw
);
116 boolean
draw_init(struct draw_context
*draw
)
119 * Note that several functions compute the clipmask of the predefined
120 * formats with hardcoded formulas instead of using these. So modifications
121 * here must be reflected there too.
124 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
125 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
126 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
127 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
128 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
129 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
131 draw
->clip_xy
= TRUE
;
135 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
138 if (!draw_pipeline_init( draw
))
141 if (!draw_pt_init( draw
))
144 if (!draw_vs_init( draw
))
147 if (!draw_gs_init( draw
))
154 void draw_destroy( struct draw_context
*draw
)
156 struct pipe_context
*pipe
;
164 /* free any rasterizer CSOs that we may have created.
166 for (i
= 0; i
< 2; i
++) {
167 for (j
= 0; j
< 2; j
++) {
168 if (draw
->rasterizer_no_cull
[i
][j
]) {
169 pipe
->delete_rasterizer_state(pipe
, draw
->rasterizer_no_cull
[i
][j
]);
174 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
175 pipe_resource_reference(&draw
->pt
.vertex_buffer
[i
].buffer
, NULL
);
178 /* Not so fast -- we're just borrowing this at the moment.
181 draw->render->destroy( draw->render );
184 draw_pipeline_destroy( draw
);
185 draw_pt_destroy( draw
);
186 draw_vs_destroy( draw
);
187 draw_gs_destroy( draw
);
190 draw_llvm_destroy( draw
->llvm
);
192 if (draw
->own_gallivm
)
193 gallivm_destroy(draw
->own_gallivm
);
201 void draw_flush( struct draw_context
*draw
)
203 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
208 * Specify the Minimum Resolvable Depth factor for polygon offset.
209 * This factor potentially depends on the number of Z buffer bits,
210 * the rasterization algorithm and the arithmetic performed on Z
211 * values between vertex shading and rasterization. It will vary
212 * from one driver to another.
214 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
220 static void update_clip_flags( struct draw_context
*draw
)
222 draw
->clip_xy
= !draw
->driver
.bypass_clip_xy
;
223 draw
->guard_band_xy
= (!draw
->driver
.bypass_clip_xy
&&
224 draw
->driver
.guard_band_xy
);
225 draw
->clip_z
= (!draw
->driver
.bypass_clip_z
&&
227 draw
->clip_user
= (draw
->nr_planes
> 6);
231 * Register new primitive rasterization/rendering state.
232 * This causes the drawing pipeline to be rebuilt.
234 void draw_set_rasterizer_state( struct draw_context
*draw
,
235 const struct pipe_rasterizer_state
*raster
,
238 if (!draw
->suspend_flushing
) {
239 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
241 draw
->rasterizer
= raster
;
242 draw
->rast_handle
= rast_handle
;
247 /* With a little more work, llvmpipe will be able to turn this off and
248 * do its own x/y clipping.
250 * Some hardware can turn off clipping altogether - in particular any
251 * hardware with a TNL unit can do its own clipping, even if it is
252 * relying on the draw module for some other reason.
254 void draw_set_driver_clipping( struct draw_context
*draw
,
255 boolean bypass_clip_xy
,
256 boolean bypass_clip_z
,
257 boolean guard_band_xy
)
259 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
261 draw
->driver
.bypass_clip_xy
= bypass_clip_xy
;
262 draw
->driver
.bypass_clip_z
= bypass_clip_z
;
263 draw
->driver
.guard_band_xy
= guard_band_xy
;
264 update_clip_flags(draw
);
269 * Plug in the primitive rendering/rasterization stage (which is the last
270 * stage in the drawing pipeline).
271 * This is provided by the device driver.
273 void draw_set_rasterize_stage( struct draw_context
*draw
,
274 struct draw_stage
*stage
)
276 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
278 draw
->pipeline
.rasterize
= stage
;
283 * Set the draw module's clipping state.
285 void draw_set_clip_state( struct draw_context
*draw
,
286 const struct pipe_clip_state
*clip
)
288 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
290 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
291 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
292 draw
->nr_planes
= 6 + clip
->nr
;
293 draw
->depth_clamp
= clip
->depth_clamp
;
295 update_clip_flags(draw
);
300 * Set the draw module's viewport state.
302 void draw_set_viewport_state( struct draw_context
*draw
,
303 const struct pipe_viewport_state
*viewport
)
305 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
306 draw
->viewport
= *viewport
; /* struct copy */
307 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
308 viewport
->scale
[1] == 1.0f
&&
309 viewport
->scale
[2] == 1.0f
&&
310 viewport
->scale
[3] == 1.0f
&&
311 viewport
->translate
[0] == 0.0f
&&
312 viewport
->translate
[1] == 0.0f
&&
313 viewport
->translate
[2] == 0.0f
&&
314 viewport
->translate
[3] == 0.0f
);
316 draw_vs_set_viewport( draw
, viewport
);
322 draw_set_vertex_buffers(struct draw_context
*draw
,
324 const struct pipe_vertex_buffer
*buffers
)
326 assert(count
<= PIPE_MAX_ATTRIBS
);
328 util_copy_vertex_buffers(draw
->pt
.vertex_buffer
,
329 &draw
->pt
.nr_vertex_buffers
,
335 draw_set_vertex_elements(struct draw_context
*draw
,
337 const struct pipe_vertex_element
*elements
)
339 assert(count
<= PIPE_MAX_ATTRIBS
);
341 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
342 draw
->pt
.nr_vertex_elements
= count
;
347 * Tell drawing context where to find mapped vertex buffers.
350 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
351 unsigned attr
, const void *buffer
)
353 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
358 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
359 unsigned shader_type
,
364 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
365 shader_type
== PIPE_SHADER_GEOMETRY
);
366 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
368 switch (shader_type
) {
369 case PIPE_SHADER_VERTEX
:
370 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
371 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
372 draw
->pt
.user
.planes
= (float (*) [DRAW_TOTAL_CLIP_PLANES
][4]) &(draw
->plane
[0]);
373 draw_vs_set_constants(draw
, slot
, buffer
, size
);
375 case PIPE_SHADER_GEOMETRY
:
376 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
377 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
378 draw_gs_set_constants(draw
, slot
, buffer
, size
);
381 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
387 * Tells the draw module to draw points with triangles if their size
388 * is greater than this threshold.
391 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
393 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
394 draw
->pipeline
.wide_point_threshold
= threshold
;
399 * Should the draw module handle point->quad conversion for drawing sprites?
402 draw_wide_point_sprites(struct draw_context
*draw
, boolean draw_sprite
)
404 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
405 draw
->pipeline
.wide_point_sprites
= draw_sprite
;
410 * Tells the draw module to draw lines with triangles if their width
411 * is greater than this threshold.
414 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
416 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
417 draw
->pipeline
.wide_line_threshold
= roundf(threshold
);
422 * Tells the draw module whether or not to implement line stipple.
425 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
427 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
428 draw
->pipeline
.line_stipple
= enable
;
433 * Tells draw module whether to convert points to quads for sprite mode.
436 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
438 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
439 draw
->pipeline
.point_sprite
= enable
;
444 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
446 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
447 draw
->force_passthrough
= enable
;
453 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
456 * This is used by some of the optional draw module stages such
457 * as wide_point which may need to allocate additional generic/texcoord
461 draw_alloc_extra_vertex_attrib(struct draw_context
*draw
,
462 uint semantic_name
, uint semantic_index
)
468 slot
= draw_find_shader_output(draw
, semantic_name
, semantic_index
);
473 num_outputs
= draw_current_shader_outputs(draw
);
474 n
= draw
->extra_shader_outputs
.num
;
476 assert(n
< Elements(draw
->extra_shader_outputs
.semantic_name
));
478 draw
->extra_shader_outputs
.semantic_name
[n
] = semantic_name
;
479 draw
->extra_shader_outputs
.semantic_index
[n
] = semantic_index
;
480 draw
->extra_shader_outputs
.slot
[n
] = num_outputs
+ n
;
481 draw
->extra_shader_outputs
.num
++;
483 return draw
->extra_shader_outputs
.slot
[n
];
488 * Remove all extra vertex attributes that were allocated with
489 * draw_alloc_extra_vertex_attrib().
492 draw_remove_extra_vertex_attribs(struct draw_context
*draw
)
494 draw
->extra_shader_outputs
.num
= 0;
499 * If a geometry shader is present, return its info, else the vertex shader's
502 struct tgsi_shader_info
*
503 draw_get_shader_info(const struct draw_context
*draw
)
506 if (draw
->gs
.geometry_shader
) {
507 return &draw
->gs
.geometry_shader
->info
;
509 return &draw
->vs
.vertex_shader
->info
;
515 * Ask the draw module for the location/slot of the given vertex attribute in
516 * a post-transformed vertex.
518 * With this function, drivers that use the draw module should have no reason
519 * to track the current vertex/geometry shader.
521 * Note that the draw module may sometimes generate vertices with extra
522 * attributes (such as texcoords for AA lines). The driver can call this
523 * function to find those attributes.
525 * Zero is returned if the attribute is not found since this is
526 * a don't care / undefined situtation. Returning -1 would be a bit more
527 * work for the drivers.
530 draw_find_shader_output(const struct draw_context
*draw
,
531 uint semantic_name
, uint semantic_index
)
533 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
536 for (i
= 0; i
< info
->num_outputs
; i
++) {
537 if (info
->output_semantic_name
[i
] == semantic_name
&&
538 info
->output_semantic_index
[i
] == semantic_index
)
542 /* Search the extra vertex attributes */
543 for (i
= 0; i
< draw
->extra_shader_outputs
.num
; i
++) {
544 if (draw
->extra_shader_outputs
.semantic_name
[i
] == semantic_name
&&
545 draw
->extra_shader_outputs
.semantic_index
[i
] == semantic_index
) {
546 return draw
->extra_shader_outputs
.slot
[i
];
555 * Return total number of the shader outputs. This function is similar to
556 * draw_current_shader_outputs() but this function also counts any extra
557 * vertex/geometry output attributes that may be filled in by some draw
558 * stages (such as AA point, AA line).
560 * If geometry shader is present, its output will be returned,
561 * if not vertex shader is used.
564 draw_num_shader_outputs(const struct draw_context
*draw
)
566 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
569 count
= info
->num_outputs
;
570 count
+= draw
->extra_shader_outputs
.num
;
577 * Provide TGSI sampler objects for vertex/geometry shaders that use
579 * This might only be used by software drivers for the time being.
582 draw_texture_samplers(struct draw_context
*draw
,
585 struct tgsi_sampler
**samplers
)
587 if (shader
== PIPE_SHADER_VERTEX
) {
588 draw
->vs
.num_samplers
= num_samplers
;
589 draw
->vs
.samplers
= samplers
;
591 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
592 draw
->gs
.num_samplers
= num_samplers
;
593 draw
->gs
.samplers
= samplers
;
600 void draw_set_render( struct draw_context
*draw
,
601 struct vbuf_render
*render
)
603 draw
->render
= render
;
608 draw_set_index_buffer(struct draw_context
*draw
,
609 const struct pipe_index_buffer
*ib
)
612 memcpy(&draw
->pt
.index_buffer
, ib
, sizeof(draw
->pt
.index_buffer
));
614 memset(&draw
->pt
.index_buffer
, 0, sizeof(draw
->pt
.index_buffer
));
619 * Tell drawing context where to find mapped index/element buffer.
622 draw_set_mapped_index_buffer(struct draw_context
*draw
,
623 const void *elements
)
625 draw
->pt
.user
.elts
= elements
;
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
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
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 a pointer/handle for a driver/CSO rasterizer object which
678 * disabled culling, stippling, unfilled tris, etc.
679 * This is used by some pipeline stages (such as wide_point, aa_line
680 * and aa_point) which convert points/lines into triangles. In those
681 * cases we don't want to accidentally cull the triangles.
683 * \param scissor should the rasterizer state enable scissoring?
684 * \param flatshade should the rasterizer state use flat shading?
685 * \return rasterizer CSO handle
688 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
692 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
694 struct pipe_context
*pipe
= draw
->pipe
;
695 struct pipe_rasterizer_state rast
;
697 memset(&rast
, 0, sizeof(rast
));
698 rast
.scissor
= scissor
;
699 rast
.flatshade
= flatshade
;
701 rast
.gl_rasterization_rules
= draw
->rasterizer
->gl_rasterization_rules
;
703 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
704 pipe
->create_rasterizer_state(pipe
, &rast
);
706 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
710 draw_set_mapped_so_buffers(struct draw_context
*draw
,
711 void *buffers
[PIPE_MAX_SO_BUFFERS
],
712 unsigned num_buffers
)
716 for (i
= 0; i
< num_buffers
; ++i
) {
717 draw
->so
.buffers
[i
] = buffers
[i
];
719 draw
->so
.num_buffers
= num_buffers
;
723 draw_set_so_state(struct draw_context
*draw
,
724 struct pipe_stream_output_state
*state
)
726 memcpy(&draw
->so
.state
,
728 sizeof(struct pipe_stream_output_state
));
732 draw_set_sampler_views(struct draw_context
*draw
,
733 struct pipe_sampler_view
**views
,
738 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
740 for (i
= 0; i
< num
; ++i
)
741 draw
->sampler_views
[i
] = views
[i
];
742 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
743 draw
->sampler_views
[i
] = NULL
;
745 draw
->num_sampler_views
= num
;
749 draw_set_samplers(struct draw_context
*draw
,
750 struct pipe_sampler_state
**samplers
,
755 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
757 for (i
= 0; i
< num
; ++i
)
758 draw
->samplers
[i
] = samplers
[i
];
759 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
760 draw
->samplers
[i
] = NULL
;
762 draw
->num_samplers
= num
;
766 draw_llvm_set_sampler_state(draw
);
771 draw_set_mapped_texture(struct draw_context
*draw
,
772 unsigned sampler_idx
,
773 uint32_t width
, uint32_t height
, uint32_t depth
,
774 uint32_t first_level
, uint32_t last_level
,
775 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
776 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
777 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
781 draw_llvm_set_mapped_texture(draw
,
783 width
, height
, depth
, first_level
, last_level
,
784 row_stride
, img_stride
, data
);