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_private.h"
37 #include "draw_vbuf.h"
41 struct draw_context
*draw_create( void )
43 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
45 #if defined(__i386__) || defined(__386__)
46 draw
->use_sse
= GETENV( "GALLIUM_NOSSE" ) == NULL
;
48 draw
->use_sse
= FALSE
;
51 /* create pipeline stages */
52 draw
->pipeline
.wide_line
= draw_wide_line_stage( draw
);
53 draw
->pipeline
.wide_point
= draw_wide_point_stage( draw
);
54 draw
->pipeline
.stipple
= draw_stipple_stage( draw
);
55 draw
->pipeline
.unfilled
= draw_unfilled_stage( draw
);
56 draw
->pipeline
.twoside
= draw_twoside_stage( draw
);
57 draw
->pipeline
.offset
= draw_offset_stage( draw
);
58 draw
->pipeline
.clip
= draw_clip_stage( draw
);
59 draw
->pipeline
.flatshade
= draw_flatshade_stage( draw
);
60 draw
->pipeline
.cull
= draw_cull_stage( draw
);
61 draw
->pipeline
.validate
= draw_validate_stage( draw
);
62 draw
->pipeline
.first
= draw
->pipeline
.validate
;
64 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
65 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
66 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
67 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
68 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
69 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
72 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
76 const unsigned size
= (MAX_VERTEX_SIZE
+ 0x0f) & ~0x0f;
77 char *tmp
= align_malloc(Elements(draw
->vs
.queue
) * size
, 16);
79 for (i
= 0; i
< Elements(draw
->vs
.queue
); i
++)
80 draw
->vs
.queue
[i
].vertex
= (struct vertex_header
*)(tmp
+ i
* size
);
83 draw
->shader_queue_flush
= draw_vertex_shader_queue_flush
;
85 /* these defaults are oriented toward the needs of softpipe */
86 draw
->wide_point_threshold
= 1000000.0; /* infinity */
87 draw
->wide_line_threshold
= 1.0;
89 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
91 draw_vertex_cache_invalidate( draw
);
92 draw_set_mapped_element_buffer( draw
, 0, NULL
);
98 void draw_destroy( struct draw_context
*draw
)
100 draw
->pipeline
.wide_line
->destroy( draw
->pipeline
.wide_line
);
101 draw
->pipeline
.wide_point
->destroy( draw
->pipeline
.wide_point
);
102 draw
->pipeline
.stipple
->destroy( draw
->pipeline
.stipple
);
103 draw
->pipeline
.unfilled
->destroy( draw
->pipeline
.unfilled
);
104 draw
->pipeline
.twoside
->destroy( draw
->pipeline
.twoside
);
105 draw
->pipeline
.offset
->destroy( draw
->pipeline
.offset
);
106 draw
->pipeline
.clip
->destroy( draw
->pipeline
.clip
);
107 draw
->pipeline
.flatshade
->destroy( draw
->pipeline
.flatshade
);
108 draw
->pipeline
.cull
->destroy( draw
->pipeline
.cull
);
109 draw
->pipeline
.validate
->destroy( draw
->pipeline
.validate
);
110 if (draw
->pipeline
.aaline
)
111 draw
->pipeline
.aaline
->destroy( draw
->pipeline
.aaline
);
112 if (draw
->pipeline
.aapoint
)
113 draw
->pipeline
.aapoint
->destroy( draw
->pipeline
.aapoint
);
114 if (draw
->pipeline
.rasterize
)
115 draw
->pipeline
.rasterize
->destroy( draw
->pipeline
.rasterize
);
116 tgsi_exec_machine_free_data(&draw
->machine
);
117 align_free( draw
->vs
.queue
[0].vertex
); /* Frees all the vertices. */
119 /* Not so fast -- we're just borrowing this at the moment.
122 draw->render->destroy( draw->render );
130 void draw_flush( struct draw_context
*draw
)
132 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
138 * Register new primitive rasterization/rendering state.
139 * This causes the drawing pipeline to be rebuilt.
141 void draw_set_rasterizer_state( struct draw_context
*draw
,
142 const struct pipe_rasterizer_state
*raster
)
144 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
146 draw
->rasterizer
= raster
;
151 * Plug in the primitive rendering/rasterization stage (which is the last
152 * stage in the drawing pipeline).
153 * This is provided by the device driver.
155 void draw_set_rasterize_stage( struct draw_context
*draw
,
156 struct draw_stage
*stage
)
158 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
160 draw
->pipeline
.rasterize
= stage
;
165 * Set the draw module's clipping state.
167 void draw_set_clip_state( struct draw_context
*draw
,
168 const struct pipe_clip_state
*clip
)
170 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
172 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
173 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
174 draw
->nr_planes
= 6 + clip
->nr
;
179 * Set the draw module's viewport state.
181 void draw_set_viewport_state( struct draw_context
*draw
,
182 const struct pipe_viewport_state
*viewport
)
184 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
185 draw
->viewport
= *viewport
; /* struct copy */
191 draw_set_vertex_buffer(struct draw_context
*draw
,
193 const struct pipe_vertex_buffer
*buffer
)
195 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
196 assert(attr
< PIPE_ATTRIB_MAX
);
197 draw
->vertex_buffer
[attr
] = *buffer
;
202 draw_set_vertex_element(struct draw_context
*draw
,
204 const struct pipe_vertex_element
*element
)
206 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
207 assert(attr
< PIPE_ATTRIB_MAX
);
208 draw
->vertex_element
[attr
] = *element
;
213 * Tell drawing context where to find mapped vertex buffers.
216 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
217 unsigned attr
, const void *buffer
)
219 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
220 draw
->user
.vbuffer
[attr
] = buffer
;
225 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
228 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
229 draw
->user
.constants
= buffer
;
234 * Tells the draw module to draw points with triangles if their size
235 * is greater than this threshold.
238 draw_wide_point_threshold(struct draw_context
*draw
, float threshold
)
240 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
241 draw
->wide_point_threshold
= threshold
;
246 * Tells the draw module to draw lines with triangles if their width
247 * is greater than this threshold.
250 draw_wide_line_threshold(struct draw_context
*draw
, float threshold
)
252 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
253 draw
->wide_line_threshold
= threshold
;
258 * Ask the draw module for the location/slot of the given vertex attribute in
259 * a post-transformed vertex.
261 * With this function, drivers that use the draw module should have no reason
262 * to track the current vertex shader.
264 * Note that the draw module may sometimes generate vertices with extra
265 * attributes (such as texcoords for AA lines). The driver can call this
266 * function to find those attributes.
268 * Zero is returned if the attribute is not found since this is
269 * a don't care / undefined situtation. Returning -1 would be a bit more
270 * work for the drivers.
273 draw_find_vs_output(struct draw_context
*draw
,
274 uint semantic_name
, uint semantic_index
)
276 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
278 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
279 if (vs
->info
.output_semantic_name
[i
] == semantic_name
&&
280 vs
->info
.output_semantic_index
[i
] == semantic_index
)
284 /* XXX there may be more than one extra vertex attrib.
285 * For example, simulated gl_FragCoord and gl_PointCoord.
287 if (draw
->extra_vp_outputs
.semantic_name
== semantic_name
&&
288 draw
->extra_vp_outputs
.semantic_index
== semantic_index
) {
289 return draw
->extra_vp_outputs
.slot
;
296 * Return number of vertex shader outputs.
299 draw_num_vs_outputs(struct draw_context
*draw
)
301 uint count
= draw
->vertex_shader
->info
.num_outputs
;
302 if (draw
->extra_vp_outputs
.slot
>= 0)
309 * Allocate space for temporary post-transform vertices, such as for clipping.
311 void draw_alloc_temp_verts( struct draw_stage
*stage
, unsigned nr
)
318 ubyte
*store
= (ubyte
*) MALLOC( MAX_VERTEX_SIZE
* nr
);
321 stage
->tmp
= (struct vertex_header
**) MALLOC( sizeof(struct vertex_header
*) * nr
);
323 for (i
= 0; i
< nr
; i
++)
324 stage
->tmp
[i
] = (struct vertex_header
*)(store
+ i
* MAX_VERTEX_SIZE
);
329 void draw_free_temp_verts( struct draw_stage
*stage
)
332 FREE( stage
->tmp
[0] );
339 boolean
draw_use_sse(struct draw_context
*draw
)
341 return (boolean
) draw
->use_sse
;
345 void draw_reset_vertex_ids(struct draw_context
*draw
)
347 struct draw_stage
*stage
= draw
->pipeline
.first
;
352 for (i
= 0; i
< stage
->nr_tmps
; i
++)
353 stage
->tmp
[i
]->vertex_id
= UNDEFINED_VERTEX_ID
;
358 draw_vertex_cache_reset_vertex_ids(draw
);
362 void draw_set_render( struct draw_context
*draw
,
363 struct vbuf_render
*render
)
365 draw
->render
= render
;