e2521489037fefb438296ba1438963a52bbf0bc9
[mesa.git] / src / mesa / pipe / draw / draw_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "pipe/p_util.h"
35 #include "draw_context.h"
36 #include "draw_private.h"
37
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43
44 #if defined(__i386__) || defined(__386__)
45 draw->use_sse = getenv("GALLIUM_SSE") != NULL;
46 #else
47 draw->use_sse = false;
48 #endif
49
50 /* create pipeline stages */
51 draw->pipeline.unfilled = draw_unfilled_stage( draw );
52 draw->pipeline.twoside = draw_twoside_stage( draw );
53 draw->pipeline.offset = draw_offset_stage( draw );
54 draw->pipeline.clip = draw_clip_stage( draw );
55 draw->pipeline.flatshade = draw_flatshade_stage( draw );
56 draw->pipeline.cull = draw_cull_stage( draw );
57 draw->pipeline.feedback = draw_feedback_stage( draw );
58 draw->pipeline.validate = draw_validate_stage( draw );
59 draw->pipeline.first = draw->pipeline.validate;
60
61 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
62 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
63 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
64 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
65 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
66 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
67 draw->nr_planes = 6;
68
69 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
70 */
71 {
72 int i;
73 char *tmp = malloc(Elements(draw->vcache.vertex) * MAX_VERTEX_SIZE);
74
75 for (i = 0; i < Elements(draw->vcache.vertex); i++)
76 draw->vcache.vertex[i] = (struct vertex_header *)(tmp + i * MAX_VERTEX_SIZE);
77 }
78
79 draw->attrib_front0 = -1;
80 draw->attrib_back0 = -1;
81 draw->attrib_front1 = -1;
82 draw->attrib_back1 = -1;
83
84 draw->prim = ~0; /* != any of PIPE_PRIM_x */
85
86 draw_vertex_cache_invalidate( draw );
87
88 return draw;
89 }
90
91
92 void draw_destroy( struct draw_context *draw )
93 {
94 free( draw->vcache.vertex[0] ); /* Frees all the vertices. */
95 free( draw );
96 }
97
98
99
100 void draw_flush( struct draw_context *draw )
101 {
102 if (draw->drawing)
103 draw_do_flush( draw, DRAW_FLUSH_DRAW );
104 }
105
106
107 void draw_set_feedback_state( struct draw_context *draw,
108 const struct pipe_feedback_state *feedback )
109 {
110 draw_flush( draw );
111 draw->feedback = *feedback;
112 }
113
114
115 /**
116 * Register new primitive rasterization/rendering state.
117 * This causes the drawing pipeline to be rebuilt.
118 */
119 void draw_set_rasterizer_state( struct draw_context *draw,
120 const struct pipe_rasterizer_state *raster )
121 {
122 draw_flush( draw );
123 draw->rasterizer = raster;
124 }
125
126
127 /**
128 * Plug in the primitive rendering/rasterization stage.
129 * This is provided by the device driver.
130 */
131 void draw_set_rasterize_stage( struct draw_context *draw,
132 struct draw_stage *stage )
133 {
134 draw_flush( draw );
135 draw->pipeline.rasterize = stage;
136 }
137
138
139 /**
140 * Set the draw module's clipping state.
141 */
142 void draw_set_clip_state( struct draw_context *draw,
143 const struct pipe_clip_state *clip )
144 {
145 draw_flush( draw );
146
147 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
148 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
149 draw->nr_planes = 6 + clip->nr;
150 }
151
152
153 /**
154 * Set the draw module's viewport state.
155 */
156 void draw_set_viewport_state( struct draw_context *draw,
157 const struct pipe_viewport_state *viewport )
158 {
159 draw_flush( draw );
160 draw->viewport = *viewport; /* struct copy */
161 }
162
163
164
165 void
166 draw_set_vertex_buffer(struct draw_context *draw,
167 unsigned attr,
168 const struct pipe_vertex_buffer *buffer)
169 {
170 draw_flush( draw );
171
172 assert(attr < PIPE_ATTRIB_MAX);
173 draw->vertex_buffer[attr] = *buffer;
174 }
175
176
177 void
178 draw_set_vertex_element(struct draw_context *draw,
179 unsigned attr,
180 const struct pipe_vertex_element *element)
181 {
182 draw_flush( draw );
183
184 assert(attr < PIPE_ATTRIB_MAX);
185 draw->vertex_element[attr] = *element;
186 }
187
188
189 /**
190 * Tell drawing context where to find mapped vertex buffers.
191 */
192 void
193 draw_set_mapped_vertex_buffer(struct draw_context *draw,
194 unsigned attr, const void *buffer)
195 {
196 draw_flush( draw );
197
198 draw->mapped_vbuffer[attr] = buffer;
199 }
200
201
202 void
203 draw_set_mapped_constant_buffer(struct draw_context *draw,
204 const void *buffer)
205 {
206 draw_flush( draw );
207
208 draw->mapped_constants = buffer;
209 }
210
211
212 void
213 draw_set_mapped_feedback_buffer(struct draw_context *draw, uint index,
214 void *buffer, uint size)
215 {
216 draw_flush( draw );
217
218 assert(index < PIPE_MAX_FEEDBACK_ATTRIBS);
219 draw->mapped_feedback_buffer[index] = buffer;
220 draw->mapped_feedback_buffer_size[index] = size; /* in bytes */
221 }
222
223
224
225
226
227 /**
228 * Allocate space for temporary post-transform vertices, such as for clipping.
229 */
230 void draw_alloc_tmps( struct draw_stage *stage, unsigned nr )
231 {
232 stage->nr_tmps = nr;
233
234 if (nr) {
235 ubyte *store = (ubyte *) malloc(MAX_VERTEX_SIZE * nr);
236 unsigned i;
237
238 stage->tmp = (struct vertex_header **) malloc(sizeof(struct vertex_header *) * nr);
239
240 for (i = 0; i < nr; i++)
241 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
242 }
243 }
244
245 void draw_free_tmps( struct draw_stage *stage )
246 {
247 if (stage->tmp) {
248 free(stage->tmp[0]);
249 free(stage->tmp);
250 }
251 }
252
253 boolean draw_use_sse(struct draw_context *draw)
254 {
255 return draw->use_sse;
256 }
257
258