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_util.h"
35 #include "draw_context.h"
36 #include "draw_vbuf.h"
39 #include "draw_pipe.h"
42 struct draw_context
*draw_create( void )
44 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
48 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
49 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
50 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
51 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
52 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
53 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
57 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
59 tgsi_exec_machine_init(&draw
->machine
);
61 /* FIXME: give this machine thing a proper constructor:
63 draw
->machine
.Inputs
= align_malloc(PIPE_MAX_ATTRIBS
* sizeof(struct tgsi_exec_vector
), 16);
64 draw
->machine
.Outputs
= align_malloc(PIPE_MAX_ATTRIBS
* sizeof(struct tgsi_exec_vector
), 16);
66 if (!draw_pipeline_init( draw
))
69 if (!draw_pt_init( draw
))
80 void draw_destroy( struct draw_context
*draw
)
86 if (draw
->machine
.Inputs
)
87 align_free(draw
->machine
.Inputs
);
89 if (draw
->machine
.Outputs
)
90 align_free(draw
->machine
.Outputs
);
92 tgsi_exec_machine_free_data(&draw
->machine
);
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
);
108 void draw_flush( struct draw_context
*draw
)
110 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
116 * Register new primitive rasterization/rendering state.
117 * This causes the drawing pipeline to be rebuilt.
119 void draw_set_rasterizer_state( struct draw_context
*draw
,
120 const struct pipe_rasterizer_state
*raster
)
122 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
124 draw
->rasterizer
= raster
;
129 * Plug in the primitive rendering/rasterization stage (which is the last
130 * stage in the drawing pipeline).
131 * This is provided by the device driver.
133 void draw_set_rasterize_stage( struct draw_context
*draw
,
134 struct draw_stage
*stage
)
136 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
138 draw
->pipeline
.rasterize
= stage
;
143 * Set the draw module's clipping state.
145 void draw_set_clip_state( struct draw_context
*draw
,
146 const struct pipe_clip_state
*clip
)
148 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
150 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
151 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
152 draw
->nr_planes
= 6 + clip
->nr
;
157 * Set the draw module's viewport state.
159 void draw_set_viewport_state( struct draw_context
*draw
,
160 const struct pipe_viewport_state
*viewport
)
162 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
163 draw
->viewport
= *viewport
; /* struct copy */
164 draw
->identity_viewport
= (viewport
->scale
[0] == 1.0f
&&
165 viewport
->scale
[1] == 1.0f
&&
166 viewport
->scale
[2] == 1.0f
&&
167 viewport
->scale
[3] == 1.0f
&&
168 viewport
->translate
[0] == 0.0f
&&
169 viewport
->translate
[1] == 0.0f
&&
170 viewport
->translate
[2] == 0.0f
&&
171 viewport
->translate
[3] == 0.0f
);
177 draw_set_vertex_buffers(struct draw_context
*draw
,
179 const struct pipe_vertex_buffer
*buffers
)
181 assert(count
<= PIPE_MAX_ATTRIBS
);
183 memcpy(draw
->pt
.vertex_buffer
, buffers
, count
* sizeof(buffers
[0]));
184 draw
->pt
.nr_vertex_buffers
= count
;
189 draw_set_vertex_elements(struct draw_context
*draw
,
191 const struct pipe_vertex_element
*elements
)
193 assert(count
<= PIPE_MAX_ATTRIBS
);
195 memcpy(draw
->pt
.vertex_element
, elements
, count
* sizeof(elements
[0]));
196 draw
->pt
.nr_vertex_elements
= count
;
201 * Tell drawing context where to find mapped vertex buffers.
204 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
205 unsigned attr
, const void *buffer
)
207 draw
->pt
.user
.vbuffer
[attr
] = buffer
;
212 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
215 draw
->pt
.user
.constants
= buffer
;
220 * Tells the draw module to draw points with triangles if their size
221 * is greater than this threshold.
224 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
226 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
227 draw
->pipeline
.wide_point_threshold
= threshold
;
232 * Tells the draw module to draw lines with triangles if their width
233 * is greater than this threshold.
236 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
238 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
239 draw
->pipeline
.wide_line_threshold
= threshold
;
244 * Tells the draw module whether or not to implement line stipple.
247 draw_enable_line_stipple(struct draw_context
*draw
, boolean enable
)
249 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
250 draw
->pipeline
.line_stipple
= enable
;
255 * Tells draw module whether to convert points to quads for sprite mode.
258 draw_enable_point_sprites(struct draw_context
*draw
, boolean enable
)
260 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
261 draw
->pipeline
.point_sprite
= enable
;
266 * Ask the draw module for the location/slot of the given vertex attribute in
267 * a post-transformed vertex.
269 * With this function, drivers that use the draw module should have no reason
270 * to track the current vertex shader.
272 * Note that the draw module may sometimes generate vertices with extra
273 * attributes (such as texcoords for AA lines). The driver can call this
274 * function to find those attributes.
276 * Zero is returned if the attribute is not found since this is
277 * a don't care / undefined situtation. Returning -1 would be a bit more
278 * work for the drivers.
281 draw_find_vs_output(struct draw_context
*draw
,
282 uint semantic_name
, uint semantic_index
)
284 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
286 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
287 if (vs
->info
.output_semantic_name
[i
] == semantic_name
&&
288 vs
->info
.output_semantic_index
[i
] == semantic_index
)
292 /* XXX there may be more than one extra vertex attrib.
293 * For example, simulated gl_FragCoord and gl_PointCoord.
295 if (draw
->extra_vp_outputs
.semantic_name
== semantic_name
&&
296 draw
->extra_vp_outputs
.semantic_index
== semantic_index
) {
297 return draw
->extra_vp_outputs
.slot
;
304 * Return number of vertex shader outputs.
307 draw_num_vs_outputs(struct draw_context
*draw
)
309 uint count
= draw
->vertex_shader
->info
.num_outputs
;
310 if (draw
->extra_vp_outputs
.slot
> 0)
317 void draw_set_render( struct draw_context
*draw
,
318 struct vbuf_render
*render
)
320 draw
->render
= render
;
323 void draw_set_edgeflags( struct draw_context
*draw
,
324 const unsigned *edgeflag
)
326 draw
->pt
.user
.edgeflag
= edgeflag
;
333 * Tell the drawing context about the index/element buffer to use
334 * (ala glDrawElements)
335 * If no element buffer is to be used (i.e. glDrawArrays) then this
336 * should be called with eltSize=0 and elements=NULL.
338 * \param draw the drawing context
339 * \param eltSize size of each element (1, 2 or 4 bytes)
340 * \param elements the element buffer ptr
343 draw_set_mapped_element_buffer( struct draw_context
*draw
,
344 unsigned eltSize
, void *elements
)
346 draw
->pt
.user
.elts
= elements
;
347 draw
->pt
.user
.eltSize
= eltSize
;
354 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
358 draw
->flushing
= TRUE
;
360 draw_pipeline_flush( draw
, flags
);
362 draw
->reduced_prim
= ~0; /* is reduced_prim needed any more? */
364 draw
->flushing
= FALSE
;