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 "draw_context.h"
43 #include "gallivm/lp_bld_init.h"
44 #include "draw_llvm.h"
47 draw_get_option_use_llvm(void)
49 static boolean first
= TRUE
;
53 value
= debug_get_bool_option("DRAW_USE_LLVM", TRUE
);
57 /* require SSE2 due to LLVM PR6960. */
58 if (!util_cpu_caps
.has_sse2
)
66 struct draw_context
*draw_create( struct pipe_context
*pipe
)
68 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
73 if(draw_get_option_use_llvm())
76 assert(lp_build_engine
);
77 draw
->engine
= lp_build_engine
;
78 draw
->llvm
= draw_llvm_create(draw
);
94 boolean
draw_init(struct draw_context
*draw
)
97 * Note that several functions compute the clipmask of the predefined
98 * formats with hardcoded formulas instead of using these. So modifications
99 * here must be reflected there too.
102 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
103 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
104 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
105 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
106 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
107 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
113 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
116 if (!draw_pipeline_init( draw
))
119 if (!draw_pt_init( draw
))
122 if (!draw_vs_init( draw
))
125 if (!draw_gs_init( draw
))
132 void draw_destroy( struct draw_context
*draw
)
134 struct pipe_context
*pipe
;
142 /* free any rasterizer CSOs that we may have created.
144 for (i
= 0; i
< 2; i
++) {
145 for (j
= 0; j
< 2; j
++) {
146 if (draw
->rasterizer_no_cull
[i
][j
]) {
147 pipe
->delete_rasterizer_state(pipe
, draw
->rasterizer_no_cull
[i
][j
]);
152 /* Not so fast -- we're just borrowing this at the moment.
155 draw->render->destroy( draw->render );
158 draw_pipeline_destroy( draw
);
159 draw_pt_destroy( draw
);
160 draw_vs_destroy( draw
);
161 draw_gs_destroy( draw
);
164 draw_llvm_destroy( draw
->llvm
);
172 void draw_flush( struct draw_context
*draw
)
174 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
179 * Specify the Minimum Resolvable Depth factor for polygon offset.
180 * This factor potentially depends on the number of Z buffer bits,
181 * the rasterization algorithm and the arithmetic performed on Z
182 * values between vertex shading and rasterization. It will vary
183 * from one driver to another.
185 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
191 static void update_clip_flags( struct draw_context
*draw
)
193 draw
->clip_xy
= !draw
->driver
.bypass_clip_xy
;
194 draw
->clip_z
= (!draw
->driver
.bypass_clip_z
&&
196 draw
->clip_user
= (draw
->nr_planes
> 6);
200 * Register new primitive rasterization/rendering state.
201 * This causes the drawing pipeline to be rebuilt.
203 void draw_set_rasterizer_state( struct draw_context
*draw
,
204 const struct pipe_rasterizer_state
*raster
,
207 if (!draw
->suspend_flushing
) {
208 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
210 draw
->rasterizer
= raster
;
211 draw
->rast_handle
= rast_handle
;
216 /* With a little more work, llvmpipe will be able to turn this off and
217 * do its own x/y clipping.
219 * Some hardware can turn off clipping altogether - in particular any
220 * hardware with a TNL unit can do its own clipping, even if it is
221 * relying on the draw module for some other reason.
223 void draw_set_driver_clipping( struct draw_context
*draw
,
224 boolean bypass_clip_xy
,
225 boolean bypass_clip_z
)
227 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
229 draw
->driver
.bypass_clip_xy
= bypass_clip_xy
;
230 draw
->driver
.bypass_clip_z
= bypass_clip_z
;
231 update_clip_flags(draw
);
236 * Plug in the primitive rendering/rasterization stage (which is the last
237 * stage in the drawing pipeline).
238 * This is provided by the device driver.
240 void draw_set_rasterize_stage( struct draw_context
*draw
,
241 struct draw_stage
*stage
)
243 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
245 draw
->pipeline
.rasterize
= stage
;
250 * Set the draw module's clipping state.
252 void draw_set_clip_state( struct draw_context
*draw
,
253 const struct pipe_clip_state
*clip
)
255 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
257 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
258 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
259 draw
->nr_planes
= 6 + clip
->nr
;
260 draw
->depth_clamp
= clip
->depth_clamp
;
262 update_clip_flags(draw
);
267 * Set the draw module's viewport state.
269 void draw_set_viewport_state( struct draw_context
*draw
,
270 const struct pipe_viewport_state
*viewport
)
272 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
273 draw
->viewport
= *viewport
; /* struct copy */
274 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
275 viewport
->scale
[1] == 1.0f
&&
276 viewport
->scale
[2] == 1.0f
&&
277 viewport
->scale
[3] == 1.0f
&&
278 viewport
->translate
[0] == 0.0f
&&
279 viewport
->translate
[1] == 0.0f
&&
280 viewport
->translate
[2] == 0.0f
&&
281 viewport
->translate
[3] == 0.0f
);
283 draw_vs_set_viewport( draw
, viewport
);
289 draw_set_vertex_buffers(struct draw_context
*draw
,
291 const struct pipe_vertex_buffer
*buffers
)
293 assert(count
<= PIPE_MAX_ATTRIBS
);
295 memcpy(draw
->pt
.vertex_buffer
, buffers
, count
* sizeof(buffers
[0]));
296 draw
->pt
.nr_vertex_buffers
= count
;
301 draw_set_vertex_elements(struct draw_context
*draw
,
303 const struct pipe_vertex_element
*elements
)
305 assert(count
<= PIPE_MAX_ATTRIBS
);
307 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
308 draw
->pt
.nr_vertex_elements
= count
;
313 * Tell drawing context where to find mapped vertex buffers.
316 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
317 unsigned attr
, const void *buffer
)
319 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
324 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
325 unsigned shader_type
,
330 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
331 shader_type
== PIPE_SHADER_GEOMETRY
);
332 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
334 switch (shader_type
) {
335 case PIPE_SHADER_VERTEX
:
336 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
337 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
338 draw_vs_set_constants(draw
, slot
, buffer
, size
);
340 case PIPE_SHADER_GEOMETRY
:
341 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
342 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
343 draw_gs_set_constants(draw
, slot
, buffer
, size
);
346 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
352 * Tells the draw module to draw points with triangles if their size
353 * is greater than this threshold.
356 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
358 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
359 draw
->pipeline
.wide_point_threshold
= threshold
;
364 * Should the draw module handle point->quad conversion for drawing sprites?
367 draw_wide_point_sprites(struct draw_context
*draw
, boolean draw_sprite
)
369 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
370 draw
->pipeline
.wide_point_sprites
= draw_sprite
;
375 * Tells the draw module to draw lines with triangles if their width
376 * is greater than this threshold.
379 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
381 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
382 draw
->pipeline
.wide_line_threshold
= threshold
;
387 * Tells the draw module whether or not to implement line stipple.
390 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
392 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
393 draw
->pipeline
.line_stipple
= enable
;
398 * Tells draw module whether to convert points to quads for sprite mode.
401 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
403 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
404 draw
->pipeline
.point_sprite
= enable
;
409 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
411 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
412 draw
->force_passthrough
= enable
;
418 * Allocate an extra vertex/geometry shader vertex attribute.
419 * This is used by some of the optional draw module stages such
420 * as wide_point which may need to allocate additional generic/texcoord
424 draw_alloc_extra_vertex_attrib(struct draw_context
*draw
,
425 uint semantic_name
, uint semantic_index
)
427 const int num_outputs
= draw_current_shader_outputs(draw
);
428 const int n
= draw
->extra_shader_outputs
.num
;
430 assert(n
< Elements(draw
->extra_shader_outputs
.semantic_name
));
432 draw
->extra_shader_outputs
.semantic_name
[n
] = semantic_name
;
433 draw
->extra_shader_outputs
.semantic_index
[n
] = semantic_index
;
434 draw
->extra_shader_outputs
.slot
[n
] = num_outputs
+ n
;
435 draw
->extra_shader_outputs
.num
++;
437 return draw
->extra_shader_outputs
.slot
[n
];
442 * Remove all extra vertex attributes that were allocated with
443 * draw_alloc_extra_vertex_attrib().
446 draw_remove_extra_vertex_attribs(struct draw_context
*draw
)
448 draw
->extra_shader_outputs
.num
= 0;
453 * Ask the draw module for the location/slot of the given vertex attribute in
454 * a post-transformed vertex.
456 * With this function, drivers that use the draw module should have no reason
457 * to track the current vertex/geometry shader.
459 * Note that the draw module may sometimes generate vertices with extra
460 * attributes (such as texcoords for AA lines). The driver can call this
461 * function to find those attributes.
463 * Zero is returned if the attribute is not found since this is
464 * a don't care / undefined situtation. Returning -1 would be a bit more
465 * work for the drivers.
468 draw_find_shader_output(const struct draw_context
*draw
,
469 uint semantic_name
, uint semantic_index
)
471 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
472 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
474 const struct tgsi_shader_info
*info
= &vs
->info
;
479 for (i
= 0; i
< info
->num_outputs
; i
++) {
480 if (info
->output_semantic_name
[i
] == semantic_name
&&
481 info
->output_semantic_index
[i
] == semantic_index
)
485 /* Search the extra vertex attributes */
486 for (i
= 0; i
< draw
->extra_shader_outputs
.num
; i
++) {
487 if (draw
->extra_shader_outputs
.semantic_name
[i
] == semantic_name
&&
488 draw
->extra_shader_outputs
.semantic_index
[i
] == semantic_index
) {
489 return draw
->extra_shader_outputs
.slot
[i
];
498 * Return total number of the shader outputs. This function is similar to
499 * draw_current_shader_outputs() but this function also counts any extra
500 * vertex/geometry output attributes that may be filled in by some draw
501 * stages (such as AA point, AA line).
503 * If geometry shader is present, its output will be returned,
504 * if not vertex shader is used.
507 draw_num_shader_outputs(const struct draw_context
*draw
)
511 /* If a geometry shader is present, its outputs go to the
512 * driver, else the vertex shader's outputs.
514 if (draw
->gs
.geometry_shader
)
515 count
= draw
->gs
.geometry_shader
->info
.num_outputs
;
517 count
= draw
->vs
.vertex_shader
->info
.num_outputs
;
519 count
+= draw
->extra_shader_outputs
.num
;
526 * Provide TGSI sampler objects for vertex/geometry shaders that use
528 * This might only be used by software drivers for the time being.
531 draw_texture_samplers(struct draw_context
*draw
,
534 struct tgsi_sampler
**samplers
)
536 if (shader
== PIPE_SHADER_VERTEX
) {
537 draw
->vs
.num_samplers
= num_samplers
;
538 draw
->vs
.samplers
= samplers
;
540 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
541 draw
->gs
.num_samplers
= num_samplers
;
542 draw
->gs
.samplers
= samplers
;
549 void draw_set_render( struct draw_context
*draw
,
550 struct vbuf_render
*render
)
552 draw
->render
= render
;
557 draw_set_index_buffer(struct draw_context
*draw
,
558 const struct pipe_index_buffer
*ib
)
561 memcpy(&draw
->pt
.index_buffer
, ib
, sizeof(draw
->pt
.index_buffer
));
563 memset(&draw
->pt
.index_buffer
, 0, sizeof(draw
->pt
.index_buffer
));
568 * Tell drawing context where to find mapped index/element buffer.
571 draw_set_mapped_index_buffer(struct draw_context
*draw
,
572 const void *elements
)
574 draw
->pt
.user
.elts
= elements
;
580 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
582 if (!draw
->suspend_flushing
)
584 assert(!draw
->flushing
); /* catch inadvertant recursion */
586 draw
->flushing
= TRUE
;
588 draw_pipeline_flush( draw
, flags
);
590 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
592 draw
->flushing
= FALSE
;
598 * Return the number of output attributes produced by the geometry
599 * shader, if present. If no geometry shader, return the number of
600 * outputs from the vertex shader.
601 * \sa draw_num_shader_outputs
604 draw_current_shader_outputs(const struct draw_context
*draw
)
606 if (draw
->gs
.geometry_shader
)
607 return draw
->gs
.num_gs_outputs
;
608 return draw
->vs
.num_vs_outputs
;
613 * Return the index of the shader output which will contain the
617 draw_current_shader_position_output(const struct draw_context
*draw
)
619 if (draw
->gs
.geometry_shader
)
620 return draw
->gs
.position_output
;
621 return draw
->vs
.position_output
;
626 * Return a pointer/handle for a driver/CSO rasterizer object which
627 * disabled culling, stippling, unfilled tris, etc.
628 * This is used by some pipeline stages (such as wide_point, aa_line
629 * and aa_point) which convert points/lines into triangles. In those
630 * cases we don't want to accidentally cull the triangles.
632 * \param scissor should the rasterizer state enable scissoring?
633 * \param flatshade should the rasterizer state use flat shading?
634 * \return rasterizer CSO handle
637 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
641 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
643 struct pipe_context
*pipe
= draw
->pipe
;
644 struct pipe_rasterizer_state rast
;
646 memset(&rast
, 0, sizeof(rast
));
647 rast
.scissor
= scissor
;
648 rast
.flatshade
= flatshade
;
650 rast
.gl_rasterization_rules
= draw
->rasterizer
->gl_rasterization_rules
;
652 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
653 pipe
->create_rasterizer_state(pipe
, &rast
);
655 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
659 draw_set_mapped_so_buffers(struct draw_context
*draw
,
660 void *buffers
[PIPE_MAX_SO_BUFFERS
],
661 unsigned num_buffers
)
665 for (i
= 0; i
< num_buffers
; ++i
) {
666 draw
->so
.buffers
[i
] = buffers
[i
];
668 draw
->so
.num_buffers
= num_buffers
;
672 draw_set_so_state(struct draw_context
*draw
,
673 struct pipe_stream_output_state
*state
)
675 memcpy(&draw
->so
.state
,
677 sizeof(struct pipe_stream_output_state
));
681 draw_set_sampler_views(struct draw_context
*draw
,
682 struct pipe_sampler_view
**views
,
687 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
689 for (i
= 0; i
< num
; ++i
)
690 draw
->sampler_views
[i
] = views
[i
];
691 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
692 draw
->sampler_views
[i
] = NULL
;
694 draw
->num_sampler_views
= num
;
698 draw_set_samplers(struct draw_context
*draw
,
699 struct pipe_sampler_state
**samplers
,
704 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
706 for (i
= 0; i
< num
; ++i
)
707 draw
->samplers
[i
] = samplers
[i
];
708 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
709 draw
->samplers
[i
] = NULL
;
711 draw
->num_samplers
= num
;
715 draw_set_mapped_texture(struct draw_context
*draw
,
716 unsigned sampler_idx
,
717 uint32_t width
, uint32_t height
, uint32_t depth
,
719 uint32_t row_stride
[DRAW_MAX_TEXTURE_LEVELS
],
720 uint32_t img_stride
[DRAW_MAX_TEXTURE_LEVELS
],
721 const void *data
[DRAW_MAX_TEXTURE_LEVELS
])
725 draw_llvm_set_mapped_texture(draw
,
727 width
, height
, depth
, last_level
,
728 row_stride
, img_stride
, data
);