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"
37 #include "draw_vbuf.h"
40 #include "draw_pipe.h"
43 struct draw_context
*draw_create( void )
45 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
49 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
50 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
51 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
52 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
53 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
54 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
58 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
61 if (!draw_pipeline_init( draw
))
64 if (!draw_pt_init( draw
))
67 if (!draw_vs_init( draw
))
78 void draw_destroy( struct draw_context
*draw
)
85 /* Not so fast -- we're just borrowing this at the moment.
88 draw->render->destroy( draw->render );
91 draw_pipeline_destroy( draw
);
92 draw_pt_destroy( draw
);
93 draw_vs_destroy( draw
);
100 void draw_flush( struct draw_context
*draw
)
102 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
107 * Specify the Minimum Resolvable Depth factor for polygon offset.
108 * This factor potentially depends on the number of Z buffer bits,
109 * the rasterization algorithm and the arithmetic performed on Z
110 * values between vertex shading and rasterization. It will vary
111 * from one driver to another.
113 void draw_set_mrd(struct draw_context
*draw
, double mrd
)
120 * Register new primitive rasterization/rendering state.
121 * This causes the drawing pipeline to be rebuilt.
123 void draw_set_rasterizer_state( struct draw_context
*draw
,
124 const struct pipe_rasterizer_state
*raster
)
126 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
128 draw
->rasterizer
= raster
;
129 draw
->bypass_clipping
=
130 ((draw
->rasterizer
&& draw
->rasterizer
->bypass_vs_clip_and_viewport
) ||
131 draw
->driver
.bypass_clipping
);
135 void draw_set_driver_clipping( struct draw_context
*draw
,
136 boolean bypass_clipping
)
138 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
140 draw
->driver
.bypass_clipping
= bypass_clipping
;
141 draw
->bypass_clipping
=
142 ((draw
->rasterizer
&& draw
->rasterizer
->bypass_vs_clip_and_viewport
) ||
143 draw
->driver
.bypass_clipping
);
148 * Plug in the primitive rendering/rasterization stage (which is the last
149 * stage in the drawing pipeline).
150 * This is provided by the device driver.
152 void draw_set_rasterize_stage( struct draw_context
*draw
,
153 struct draw_stage
*stage
)
155 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
157 draw
->pipeline
.rasterize
= stage
;
162 * Set the draw module's clipping state.
164 void draw_set_clip_state( struct draw_context
*draw
,
165 const struct pipe_clip_state
*clip
)
167 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
169 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
170 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
171 draw
->nr_planes
= 6 + clip
->nr
;
176 * Set the draw module's viewport state.
178 void draw_set_viewport_state( struct draw_context
*draw
,
179 const struct pipe_viewport_state
*viewport
)
181 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
182 draw
->viewport
= *viewport
; /* struct copy */
183 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
184 viewport
->scale
[1] == 1.0f
&&
185 viewport
->scale
[2] == 1.0f
&&
186 viewport
->scale
[3] == 1.0f
&&
187 viewport
->translate
[0] == 0.0f
&&
188 viewport
->translate
[1] == 0.0f
&&
189 viewport
->translate
[2] == 0.0f
&&
190 viewport
->translate
[3] == 0.0f
);
192 draw_vs_set_viewport( draw
, viewport
);
198 draw_set_vertex_buffers(struct draw_context
*draw
,
200 const struct pipe_vertex_buffer
*buffers
)
202 assert(count
<= PIPE_MAX_ATTRIBS
);
204 memcpy(draw
->pt
.vertex_buffer
, buffers
, count
* sizeof(buffers
[0]));
205 draw
->pt
.nr_vertex_buffers
= count
;
210 draw_set_vertex_elements(struct draw_context
*draw
,
212 const struct pipe_vertex_element
*elements
)
214 assert(count
<= PIPE_MAX_ATTRIBS
);
216 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
217 draw
->pt
.nr_vertex_elements
= count
;
222 * Tell drawing context where to find mapped vertex buffers.
225 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
226 unsigned attr
, const void *buffer
)
228 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
233 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
237 draw
->pt
.user
.constants
= buffer
;
238 draw_vs_set_constants( draw
, (const float (*)[4])buffer
, size
);
243 * Tells the draw module to draw points with triangles if their size
244 * is greater than this threshold.
247 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
249 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
250 draw
->pipeline
.wide_point_threshold
= threshold
;
255 * Tells the draw module to draw lines with triangles if their width
256 * is greater than this threshold.
259 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
261 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
262 draw
->pipeline
.wide_line_threshold
= threshold
;
267 * Tells the draw module whether or not to implement line stipple.
270 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
272 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
273 draw
->pipeline
.line_stipple
= enable
;
278 * Tells draw module whether to convert points to quads for sprite mode.
281 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
283 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
284 draw
->pipeline
.point_sprite
= enable
;
289 draw_set_force_passthrough( struct draw_context
*draw
, boolean enable
)
291 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
292 draw
->force_passthrough
= enable
;
297 * Ask the draw module for the location/slot of the given vertex attribute in
298 * a post-transformed vertex.
300 * With this function, drivers that use the draw module should have no reason
301 * to track the current vertex shader.
303 * Note that the draw module may sometimes generate vertices with extra
304 * attributes (such as texcoords for AA lines). The driver can call this
305 * function to find those attributes.
307 * Zero is returned if the attribute is not found since this is
308 * a don't care / undefined situtation. Returning -1 would be a bit more
309 * work for the drivers.
312 draw_find_vs_output(const struct draw_context
*draw
,
313 uint semantic_name
, uint semantic_index
)
315 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
317 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
318 if (vs
->info
.output_semantic_name
[i
] == semantic_name
&&
319 vs
->info
.output_semantic_index
[i
] == semantic_index
)
323 /* XXX there may be more than one extra vertex attrib.
324 * For example, simulated gl_FragCoord and gl_PointCoord.
326 if (draw
->extra_vp_outputs
.semantic_name
== semantic_name
&&
327 draw
->extra_vp_outputs
.semantic_index
== semantic_index
) {
328 return draw
->extra_vp_outputs
.slot
;
335 * Return number of vertex shader outputs.
338 draw_num_vs_outputs(const struct draw_context
*draw
)
340 uint count
= draw
->vs
.vertex_shader
->info
.num_outputs
;
341 if (draw
->extra_vp_outputs
.slot
> 0)
348 * Provide TGSI sampler objects for vertex shaders that use texture fetches.
349 * This might only be used by software drivers for the time being.
352 draw_texture_samplers(struct draw_context
*draw
,
354 struct tgsi_sampler
**samplers
)
356 draw
->vs
.num_samplers
= num_samplers
;
357 draw
->vs
.samplers
= samplers
;
363 void draw_set_render( struct draw_context
*draw
,
364 struct vbuf_render
*render
)
366 draw
->render
= render
;
369 void draw_set_edgeflags( struct draw_context
*draw
,
370 const unsigned *edgeflag
)
372 draw
->pt
.user
.edgeflag
= edgeflag
;
379 * Tell the drawing context about the index/element buffer to use
380 * (ala glDrawElements)
381 * If no element buffer is to be used (i.e. glDrawArrays) then this
382 * should be called with eltSize=0 and elements=NULL.
384 * \param draw the drawing context
385 * \param eltSize size of each element (1, 2 or 4 bytes)
386 * \param elements the element buffer ptr
389 draw_set_mapped_element_buffer_range( struct draw_context
*draw
,
393 const void *elements
)
395 draw
->pt
.user
.elts
= elements
;
396 draw
->pt
.user
.eltSize
= eltSize
;
397 draw
->pt
.user
.min_index
= min_index
;
398 draw
->pt
.user
.max_index
= max_index
;
403 draw_set_mapped_element_buffer( struct draw_context
*draw
,
405 const void *elements
)
407 draw
->pt
.user
.elts
= elements
;
408 draw
->pt
.user
.eltSize
= eltSize
;
409 draw
->pt
.user
.min_index
= 0;
410 draw
->pt
.user
.max_index
= 0xffffffff;
416 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
418 if (!draw
->suspend_flushing
)
420 assert(!draw
->flushing
); /* catch inadvertant recursion */
422 draw
->flushing
= TRUE
;
424 draw_pipeline_flush( draw
, flags
);
426 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
428 draw
->flushing
= FALSE
;