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 "util/u_memory.h"
35 #include "util/u_math.h"
36 #include "draw_context.h"
41 struct draw_context
*draw_create( void )
43 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
57 boolean
draw_init(struct draw_context
*draw
)
59 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
60 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
61 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
62 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
63 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
64 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
68 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
71 if (!draw_pipeline_init( draw
))
74 if (!draw_pt_init( draw
))
77 if (!draw_vs_init( draw
))
80 if (!draw_gs_init( draw
))
87 void draw_destroy( struct draw_context
*draw
)
94 /* Not so fast -- we're just borrowing this at the moment.
97 draw->render->destroy( draw->render );
100 draw_pipeline_destroy( draw
);
101 draw_pt_destroy( draw
);
102 draw_vs_destroy( draw
);
103 draw_gs_destroy( draw
);
110 void draw_flush( struct draw_context
*draw
)
112 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
117 * Specify the Minimum Resolvable Depth factor for polygon offset.
118 * This factor potentially depends on the number of Z buffer bits,
119 * the rasterization algorithm and the arithmetic performed on Z
120 * values between vertex shading and rasterization. It will vary
121 * from one driver to another.
123 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
130 * Register new primitive rasterization/rendering state.
131 * This causes the drawing pipeline to be rebuilt.
133 void draw_set_rasterizer_state( struct draw_context
*draw
,
134 const struct pipe_rasterizer_state
*raster
)
136 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
138 draw
->rasterizer
= raster
;
139 draw
->bypass_clipping
= draw
->driver
.bypass_clipping
;
143 void draw_set_driver_clipping( struct draw_context
*draw
,
144 boolean bypass_clipping
)
146 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
148 draw
->driver
.bypass_clipping
= bypass_clipping
;
149 draw
->bypass_clipping
= draw
->driver
.bypass_clipping
;
154 * Plug in the primitive rendering/rasterization stage (which is the last
155 * stage in the drawing pipeline).
156 * This is provided by the device driver.
158 void draw_set_rasterize_stage( struct draw_context
*draw
,
159 struct draw_stage
*stage
)
161 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
163 draw
->pipeline
.rasterize
= stage
;
168 * Set the draw module's clipping state.
170 void draw_set_clip_state( struct draw_context
*draw
,
171 const struct pipe_clip_state
*clip
)
173 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
175 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
176 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
177 draw
->nr_planes
= 6 + clip
->nr
;
182 * Set the draw module's viewport state.
184 void draw_set_viewport_state( struct draw_context
*draw
,
185 const struct pipe_viewport_state
*viewport
)
187 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
188 draw
->viewport
= *viewport
; /* struct copy */
189 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
190 viewport
->scale
[1] == 1.0f
&&
191 viewport
->scale
[2] == 1.0f
&&
192 viewport
->scale
[3] == 1.0f
&&
193 viewport
->translate
[0] == 0.0f
&&
194 viewport
->translate
[1] == 0.0f
&&
195 viewport
->translate
[2] == 0.0f
&&
196 viewport
->translate
[3] == 0.0f
);
198 draw_vs_set_viewport( draw
, viewport
);
204 draw_set_vertex_buffers(struct draw_context
*draw
,
206 const struct pipe_vertex_buffer
*buffers
)
208 assert(count
<= PIPE_MAX_ATTRIBS
);
210 memcpy(draw
->pt
.vertex_buffer
, buffers
, count
* sizeof(buffers
[0]));
211 draw
->pt
.nr_vertex_buffers
= count
;
216 draw_set_vertex_elements(struct draw_context
*draw
,
218 const struct pipe_vertex_element
*elements
)
220 assert(count
<= PIPE_MAX_ATTRIBS
);
222 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
223 draw
->pt
.nr_vertex_elements
= count
;
228 * Tell drawing context where to find mapped vertex buffers.
231 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
232 unsigned attr
, const void *buffer
)
234 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
239 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
240 unsigned shader_type
,
245 debug_assert(shader_type
== PIPE_SHADER_VERTEX
||
246 shader_type
== PIPE_SHADER_GEOMETRY
);
247 debug_assert(slot
< PIPE_MAX_CONSTANT_BUFFERS
);
249 if (shader_type
== PIPE_SHADER_VERTEX
) {
250 draw
->pt
.user
.vs_constants
[slot
] = buffer
;
251 draw_vs_set_constants(draw
, slot
, buffer
, size
);
252 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
253 draw
->pt
.user
.gs_constants
[slot
] = buffer
;
254 draw_gs_set_constants(draw
, slot
, buffer
, size
);
260 * Tells the draw module to draw points with triangles if their size
261 * is greater than this threshold.
264 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
266 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
267 draw
->pipeline
.wide_point_threshold
= threshold
;
272 * Tells the draw module to draw lines with triangles if their width
273 * is greater than this threshold.
276 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
278 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
279 draw
->pipeline
.wide_line_threshold
= threshold
;
284 * Tells the draw module whether or not to implement line stipple.
287 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
289 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
290 draw
->pipeline
.line_stipple
= enable
;
295 * Tells draw module whether to convert points to quads for sprite mode.
298 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
300 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
301 draw
->pipeline
.point_sprite
= enable
;
306 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
308 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
309 draw
->force_passthrough
= enable
;
314 * Ask the draw module for the location/slot of the given vertex attribute in
315 * a post-transformed vertex.
317 * With this function, drivers that use the draw module should have no reason
318 * to track the current vertex/geometry shader.
320 * Note that the draw module may sometimes generate vertices with extra
321 * attributes (such as texcoords for AA lines). The driver can call this
322 * function to find those attributes.
324 * Zero is returned if the attribute is not found since this is
325 * a don't care / undefined situtation. Returning -1 would be a bit more
326 * work for the drivers.
329 draw_find_shader_output(const struct draw_context
*draw
,
330 uint semantic_name
, uint semantic_index
)
332 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
333 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
335 const struct tgsi_shader_info
*info
= &vs
->info
;
340 for (i
= 0; i
< info
->num_outputs
; i
++) {
341 if (info
->output_semantic_name
[i
] == semantic_name
&&
342 info
->output_semantic_index
[i
] == semantic_index
)
346 /* XXX there may be more than one extra vertex attrib.
347 * For example, simulated gl_FragCoord and gl_PointCoord.
349 if (draw
->extra_shader_outputs
.semantic_name
== semantic_name
&&
350 draw
->extra_shader_outputs
.semantic_index
== semantic_index
) {
351 return draw
->extra_shader_outputs
.slot
;
359 * Return total number of the shader outputs. This function is similar to
360 * draw_current_shader_outputs() but this function also counts any extra
361 * vertex/geometry output attributes that may be filled in by some draw
362 * stages (such as AA point, AA line).
364 * If geometry shader is present, its output will be returned,
365 * if not vertex shader is used.
368 draw_num_shader_outputs(const struct draw_context
*draw
)
370 uint count
= draw
->vs
.vertex_shader
->info
.num_outputs
;
372 /* If a geometry shader is present, its outputs go to the
373 * driver, else the vertex shader's outputs.
375 if (draw
->gs
.geometry_shader
)
376 count
= draw
->gs
.geometry_shader
->info
.num_outputs
;
378 if (draw
->extra_shader_outputs
.slot
> 0)
385 * Provide TGSI sampler objects for vertex/geometry shaders that use
387 * This might only be used by software drivers for the time being.
390 draw_texture_samplers(struct draw_context
*draw
,
392 struct tgsi_sampler
**samplers
)
394 draw
->vs
.num_samplers
= num_samplers
;
395 draw
->vs
.samplers
= samplers
;
396 draw
->gs
.num_samplers
= num_samplers
;
397 draw
->gs
.samplers
= samplers
;
403 void draw_set_render( struct draw_context
*draw
,
404 struct vbuf_render
*render
)
406 draw
->render
= render
;
412 * Tell the drawing context about the index/element buffer to use
413 * (ala glDrawElements)
414 * If no element buffer is to be used (i.e. glDrawArrays) then this
415 * should be called with eltSize=0 and elements=NULL.
417 * \param draw the drawing context
418 * \param eltSize size of each element (1, 2 or 4 bytes)
419 * \param elements the element buffer ptr
422 draw_set_mapped_element_buffer_range( struct draw_context
*draw
,
426 const void *elements
)
428 draw
->pt
.user
.elts
= elements
;
429 draw
->pt
.user
.eltSize
= eltSize
;
430 draw
->pt
.user
.min_index
= min_index
;
431 draw
->pt
.user
.max_index
= max_index
;
436 draw_set_mapped_element_buffer( struct draw_context
*draw
,
438 const void *elements
)
440 draw
->pt
.user
.elts
= elements
;
441 draw
->pt
.user
.eltSize
= eltSize
;
442 draw
->pt
.user
.min_index
= 0;
443 draw
->pt
.user
.max_index
= 0xffffffff;
449 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
451 if (!draw
->suspend_flushing
)
453 assert(!draw
->flushing
); /* catch inadvertant recursion */
455 draw
->flushing
= TRUE
;
457 draw_pipeline_flush( draw
, flags
);
459 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
461 draw
->flushing
= FALSE
;
467 * Return the number of output attributes produced by the geometry
468 * shader, if present. If no geometry shader, return the number of
469 * outputs from the vertex shader.
470 * \sa draw_num_shader_outputs
473 draw_current_shader_outputs(const struct draw_context
*draw
)
475 if (draw
->gs
.geometry_shader
)
476 return draw
->gs
.num_gs_outputs
;
477 return draw
->vs
.num_vs_outputs
;
482 * Return the index of the shader output which will contain the
486 draw_current_shader_position_output(const struct draw_context
*draw
)
488 if (draw
->gs
.geometry_shader
)
489 return draw
->gs
.position_output
;
490 return draw
->vs
.position_output
;