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 "draw_context.h"
42 #include "gallivm/lp_bld_init.h"
43 #include "draw_llvm.h"
46 struct draw_context
*draw_create( struct pipe_context
*pipe
)
48 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
54 assert(lp_build_engine
);
55 draw
->engine
= lp_build_engine
;
56 draw
->llvm
= draw_llvm_create(draw
);
71 boolean
draw_init(struct draw_context
*draw
)
74 * Note that several functions compute the clipmask of the predefined
75 * formats with hardcoded formulas instead of using these. So modifications
76 * here must be reflected there too.
79 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
80 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
81 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
82 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
83 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
84 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
88 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
91 if (!draw_pipeline_init( draw
))
94 if (!draw_pt_init( draw
))
97 if (!draw_vs_init( draw
))
100 if (!draw_gs_init( draw
))
107 void draw_destroy( struct draw_context
*draw
)
109 struct pipe_context
*pipe
;
117 /* free any rasterizer CSOs that we may have created.
119 for (i
= 0; i
< 2; i
++) {
120 for (j
= 0; j
< 2; j
++) {
121 if (draw
->rasterizer_no_cull
[i
][j
]) {
122 pipe
->delete_rasterizer_state(pipe
, draw
->rasterizer_no_cull
[i
][j
]);
127 /* Not so fast -- we're just borrowing this at the moment.
130 draw->render->destroy( draw->render );
133 draw_pipeline_destroy( draw
);
134 draw_pt_destroy( draw
);
135 draw_vs_destroy( draw
);
136 draw_gs_destroy( draw
);
138 draw_llvm_destroy( draw
->llvm
);
146 void draw_flush( struct draw_context
*draw
)
148 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
153 * Specify the Minimum Resolvable Depth factor for polygon offset.
154 * This factor potentially depends on the number of Z buffer bits,
155 * the rasterization algorithm and the arithmetic performed on Z
156 * values between vertex shading and rasterization. It will vary
157 * from one driver to another.
159 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
166 * Register new primitive rasterization/rendering state.
167 * This causes the drawing pipeline to be rebuilt.
169 void draw_set_rasterizer_state( struct draw_context
*draw
,
170 const struct pipe_rasterizer_state
*raster
,
173 if (!draw
->suspend_flushing
) {
174 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
176 draw
->rasterizer
= raster
;
177 draw
->rast_handle
= rast_handle
;
179 draw
->bypass_clipping
= draw
->driver
.bypass_clipping
;
184 void draw_set_driver_clipping( struct draw_context
*draw
,
185 boolean bypass_clipping
)
187 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
189 draw
->driver
.bypass_clipping
= bypass_clipping
;
190 draw
->bypass_clipping
= draw
->driver
.bypass_clipping
;
195 * Plug in the primitive rendering/rasterization stage (which is the last
196 * stage in the drawing pipeline).
197 * This is provided by the device driver.
199 void draw_set_rasterize_stage( struct draw_context
*draw
,
200 struct draw_stage
*stage
)
202 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
204 draw
->pipeline
.rasterize
= stage
;
209 * Set the draw module's clipping state.
211 void draw_set_clip_state( struct draw_context
*draw
,
212 const struct pipe_clip_state
*clip
)
214 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
216 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
217 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
218 draw
->nr_planes
= 6 + clip
->nr
;
219 draw
->depth_clamp
= clip
->depth_clamp
;
224 * Set the draw module's viewport state.
226 void draw_set_viewport_state( struct draw_context
*draw
,
227 const struct pipe_viewport_state
*viewport
)
229 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
230 draw
->viewport
= *viewport
; /* struct copy */
231 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
232 viewport
->scale
[1] == 1.0f
&&
233 viewport
->scale
[2] == 1.0f
&&
234 viewport
->scale
[3] == 1.0f
&&
235 viewport
->translate
[0] == 0.0f
&&
236 viewport
->translate
[1] == 0.0f
&&
237 viewport
->translate
[2] == 0.0f
&&
238 viewport
->translate
[3] == 0.0f
);
240 draw_vs_set_viewport( draw
, viewport
);
246 draw_set_vertex_buffers(struct draw_context
*draw
,
248 const struct pipe_vertex_buffer
*buffers
)
250 assert(count
<= PIPE_MAX_ATTRIBS
);
252 memcpy(draw
->pt
.vertex_buffer
, buffers
, count
* sizeof(buffers
[0]));
253 draw
->pt
.nr_vertex_buffers
= count
;
258 draw_set_vertex_elements(struct draw_context
*draw
,
260 const struct pipe_vertex_element
*elements
)
262 assert(count
<= PIPE_MAX_ATTRIBS
);
264 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
265 draw
->pt
.nr_vertex_elements
= count
;
270 * Tell drawing context where to find mapped vertex buffers.
273 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
274 unsigned attr
, const void *buffer
)
276 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
281 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
282 unsigned shader_type
,
287 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
288 shader_type
== PIPE_SHADER_GEOMETRY
);
289 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
291 switch (shader_type
) {
292 case PIPE_SHADER_VERTEX
:
293 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
294 draw
->pt
.user
.vs_constants_size
[slot
] = size
;
295 draw_vs_set_constants(draw
, slot
, buffer
, size
);
297 case PIPE_SHADER_GEOMETRY
:
298 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
299 draw
->pt
.user
.gs_constants_size
[slot
] = size
;
300 draw_gs_set_constants(draw
, slot
, buffer
, size
);
303 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
309 * Tells the draw module to draw points with triangles if their size
310 * is greater than this threshold.
313 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
315 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
316 draw
->pipeline
.wide_point_threshold
= threshold
;
321 * Should the draw module handle point->quad conversion for drawing sprites?
324 draw_wide_point_sprites(struct draw_context
*draw
, boolean draw_sprite
)
326 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
327 draw
->pipeline
.wide_point_sprites
= draw_sprite
;
332 * Tells the draw module to draw lines with triangles if their width
333 * is greater than this threshold.
336 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
338 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
339 draw
->pipeline
.wide_line_threshold
= threshold
;
344 * Tells the draw module whether or not to implement line stipple.
347 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
349 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
350 draw
->pipeline
.line_stipple
= enable
;
355 * Tells draw module whether to convert points to quads for sprite mode.
358 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
360 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
361 draw
->pipeline
.point_sprite
= enable
;
366 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
368 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
369 draw
->force_passthrough
= enable
;
374 * Ask the draw module for the location/slot of the given vertex attribute in
375 * a post-transformed vertex.
377 * With this function, drivers that use the draw module should have no reason
378 * to track the current vertex/geometry shader.
380 * Note that the draw module may sometimes generate vertices with extra
381 * attributes (such as texcoords for AA lines). The driver can call this
382 * function to find those attributes.
384 * Zero is returned if the attribute is not found since this is
385 * a don't care / undefined situtation. Returning -1 would be a bit more
386 * work for the drivers.
389 draw_find_shader_output(const struct draw_context
*draw
,
390 uint semantic_name
, uint semantic_index
)
392 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
393 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
395 const struct tgsi_shader_info
*info
= &vs
->info
;
400 for (i
= 0; i
< info
->num_outputs
; i
++) {
401 if (info
->output_semantic_name
[i
] == semantic_name
&&
402 info
->output_semantic_index
[i
] == semantic_index
)
406 /* XXX there may be more than one extra vertex attrib.
407 * For example, simulated gl_FragCoord and gl_PointCoord.
409 if (draw
->extra_shader_outputs
.semantic_name
== semantic_name
&&
410 draw
->extra_shader_outputs
.semantic_index
== semantic_index
) {
411 return draw
->extra_shader_outputs
.slot
;
419 * Return total number of the shader outputs. This function is similar to
420 * draw_current_shader_outputs() but this function also counts any extra
421 * vertex/geometry output attributes that may be filled in by some draw
422 * stages (such as AA point, AA line).
424 * If geometry shader is present, its output will be returned,
425 * if not vertex shader is used.
428 draw_num_shader_outputs(const struct draw_context
*draw
)
430 uint count
= draw
->vs
.vertex_shader
->info
.num_outputs
;
432 /* If a geometry shader is present, its outputs go to the
433 * driver, else the vertex shader's outputs.
435 if (draw
->gs
.geometry_shader
)
436 count
= draw
->gs
.geometry_shader
->info
.num_outputs
;
438 if (draw
->extra_shader_outputs
.slot
> 0)
445 * Provide TGSI sampler objects for vertex/geometry shaders that use
447 * This might only be used by software drivers for the time being.
450 draw_texture_samplers(struct draw_context
*draw
,
453 struct tgsi_sampler
**samplers
)
455 if (shader
== PIPE_SHADER_VERTEX
) {
456 draw
->vs
.num_samplers
= num_samplers
;
457 draw
->vs
.samplers
= samplers
;
459 debug_assert(shader
== PIPE_SHADER_GEOMETRY
);
460 draw
->gs
.num_samplers
= num_samplers
;
461 draw
->gs
.samplers
= samplers
;
468 void draw_set_render( struct draw_context
*draw
,
469 struct vbuf_render
*render
)
471 draw
->render
= render
;
477 * Tell the drawing context about the index/element buffer to use
478 * (ala glDrawElements)
479 * If no element buffer is to be used (i.e. glDrawArrays) then this
480 * should be called with eltSize=0 and elements=NULL.
482 * \param draw the drawing context
483 * \param eltSize size of each element (1, 2 or 4 bytes)
484 * \param elements the element buffer ptr
487 draw_set_mapped_element_buffer_range( struct draw_context
*draw
,
492 const void *elements
)
494 draw
->pt
.user
.elts
= elements
;
495 draw
->pt
.user
.eltSize
= eltSize
;
496 draw
->pt
.user
.eltBias
= eltBias
;
497 draw
->pt
.user
.min_index
= min_index
;
498 draw
->pt
.user
.max_index
= max_index
;
503 draw_set_mapped_element_buffer( struct draw_context
*draw
,
506 const void *elements
)
508 draw
->pt
.user
.elts
= elements
;
509 draw
->pt
.user
.eltSize
= eltSize
;
510 draw
->pt
.user
.eltBias
= eltBias
;
511 draw
->pt
.user
.min_index
= 0;
512 draw
->pt
.user
.max_index
= 0xffffffff;
518 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
520 if (!draw
->suspend_flushing
)
522 assert(!draw
->flushing
); /* catch inadvertant recursion */
524 draw
->flushing
= TRUE
;
526 draw_pipeline_flush( draw
, flags
);
528 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
530 draw
->flushing
= FALSE
;
536 * Return the number of output attributes produced by the geometry
537 * shader, if present. If no geometry shader, return the number of
538 * outputs from the vertex shader.
539 * \sa draw_num_shader_outputs
542 draw_current_shader_outputs(const struct draw_context
*draw
)
544 if (draw
->gs
.geometry_shader
)
545 return draw
->gs
.num_gs_outputs
;
546 return draw
->vs
.num_vs_outputs
;
551 * Return the index of the shader output which will contain the
555 draw_current_shader_position_output(const struct draw_context
*draw
)
557 if (draw
->gs
.geometry_shader
)
558 return draw
->gs
.position_output
;
559 return draw
->vs
.position_output
;
564 * Return a pointer/handle for a driver/CSO rasterizer object which
565 * disabled culling, stippling, unfilled tris, etc.
566 * This is used by some pipeline stages (such as wide_point, aa_line
567 * and aa_point) which convert points/lines into triangles. In those
568 * cases we don't want to accidentally cull the triangles.
570 * \param scissor should the rasterizer state enable scissoring?
571 * \param flatshade should the rasterizer state use flat shading?
572 * \return rasterizer CSO handle
575 draw_get_rasterizer_no_cull( struct draw_context
*draw
,
579 if (!draw
->rasterizer_no_cull
[scissor
][flatshade
]) {
581 struct pipe_context
*pipe
= draw
->pipe
;
582 struct pipe_rasterizer_state rast
;
584 memset(&rast
, 0, sizeof(rast
));
585 rast
.scissor
= scissor
;
586 rast
.flatshade
= flatshade
;
588 rast
.gl_rasterization_rules
= draw
->rasterizer
->gl_rasterization_rules
;
590 draw
->rasterizer_no_cull
[scissor
][flatshade
] =
591 pipe
->create_rasterizer_state(pipe
, &rast
);
593 return draw
->rasterizer_no_cull
[scissor
][flatshade
];
597 draw_set_mapped_so_buffers(struct draw_context
*draw
,
598 void *buffers
[PIPE_MAX_SO_BUFFERS
],
599 unsigned num_buffers
)
603 for (i
= 0; i
< num_buffers
; ++i
) {
604 draw
->so
.buffers
[i
] = buffers
[i
];
606 draw
->so
.num_buffers
= num_buffers
;
610 draw_set_so_state(struct draw_context
*draw
,
611 struct pipe_stream_output_state
*state
)
613 memcpy(&draw
->so
.state
,
615 sizeof(struct pipe_stream_output_state
));
619 draw_set_sampler_views(struct draw_context
*draw
,
620 struct pipe_sampler_view
**views
,
625 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
627 for (i
= 0; i
< num
; ++i
)
628 draw
->sampler_views
[i
] = views
[i
];
629 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
630 draw
->sampler_views
[i
] = NULL
;
632 draw
->num_sampler_views
= num
;
636 draw_set_samplers(struct draw_context
*draw
,
637 struct pipe_sampler_state
**samplers
,
642 debug_assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
644 for (i
= 0; i
< num
; ++i
)
645 draw
->samplers
[i
] = samplers
[i
];
646 for (i
= num
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
647 draw
->samplers
[i
] = NULL
;
649 draw
->num_samplers
= num
;
653 draw_set_mapped_texture(struct draw_context
*draw
,
654 unsigned sampler_idx
,
655 uint32_t width
, uint32_t height
, uint32_t depth
,
657 uint32_t row_stride
[DRAW_MAX_TEXTURE_LEVELS
],
658 uint32_t img_stride
[DRAW_MAX_TEXTURE_LEVELS
],
659 const void *data
[DRAW_MAX_TEXTURE_LEVELS
])
662 draw_llvm_set_mapped_texture(draw
,
664 width
, height
, depth
, last_level
,
665 row_stride
, img_stride
, data
);