Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / pipe / draw / draw_private.h
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 * Private data structures, etc for the draw module.
30 */
31
32
33 /**
34 * Authors:
35 * Keith Whitwell <keith@tungstengraphics.com>
36 * Brian Paul
37 */
38
39
40 #ifndef DRAW_PRIVATE_H
41 #define DRAW_PRIVATE_H
42
43
44 #include "pipe/p_state.h"
45 #include "pipe/p_defines.h"
46
47 #include "draw_vertex.h"
48
49 #include "x86/rtasm/x86sse.h"
50 #include "pipe/tgsi/exec/tgsi_exec.h"
51
52
53 struct gallivm_prog;
54 struct gallivm_cpu_engine;
55
56 /**
57 * Basic vertex info.
58 * Carry some useful information around with the vertices in the prim pipe.
59 */
60 struct vertex_header {
61 unsigned clipmask:12;
62 unsigned edgeflag:1;
63 unsigned pad:3;
64 unsigned vertex_id:16;
65
66 float clip[4];
67
68 float data[][4]; /* Note variable size */
69 };
70
71 /* NOTE: It should match vertex_id size above */
72 #define UNDEFINED_VERTEX_ID 0xffff
73
74 /* XXX This is too large */
75 #define MAX_VERTEX_SIZE ((2 + PIPE_MAX_SHADER_OUTPUTS) * 4 * sizeof(float))
76
77
78
79 /**
80 * Basic info for a point/line/triangle primitive.
81 */
82 struct prim_header {
83 float det; /**< front/back face determinant */
84 unsigned reset_line_stipple:1;
85 unsigned edgeflags:3;
86 unsigned pad:28;
87 struct vertex_header *v[3]; /**< 1 to 3 vertex pointers */
88 };
89
90
91
92 struct draw_context;
93
94 /**
95 * Base class for all primitive drawing stages.
96 */
97 struct draw_stage
98 {
99 struct draw_context *draw; /**< parent context */
100
101 struct draw_stage *next; /**< next stage in pipeline */
102
103 struct vertex_header **tmp; /**< temp vert storage, such as for clipping */
104 unsigned nr_tmps;
105
106 void (*begin)( struct draw_stage * );
107
108 void (*point)( struct draw_stage *,
109 struct prim_header * );
110
111 void (*line)( struct draw_stage *,
112 struct prim_header * );
113
114 void (*tri)( struct draw_stage *,
115 struct prim_header * );
116
117 void (*end)( struct draw_stage * );
118
119 /**
120 * Reset temporary vertex ids in this stage
121 *
122 * draw_free_tmps will be called instead if null.
123 */
124 void (*reset_tmps)( struct draw_stage * );
125
126 void (*reset_stipple_counter)( struct draw_stage * );
127 };
128
129
130 #define PRIM_QUEUE_LENGTH 16
131 #define VCACHE_SIZE 32
132 #define VCACHE_OVERFLOW 4
133 #define VS_QUEUE_LENGTH (VCACHE_SIZE + VCACHE_OVERFLOW + 1) /* can never fill up */
134
135 /**
136 * Private version of the compiled vertex_shader
137 */
138 struct draw_vertex_shader {
139 const struct pipe_shader_state *state;
140 #if defined(__i386__) || defined(__386__)
141 struct x86_function sse2_program;
142 #endif
143 #ifdef MESA_LLVM
144 struct gallivm_prog *llvm_prog;
145 #endif
146 };
147
148 /**
149 * Private context for the drawing module.
150 */
151 struct draw_context
152 {
153 /** Drawing/primitive pipeline stages */
154 struct {
155 struct draw_stage *first; /**< one of the following */
156
157 struct draw_stage *validate;
158
159 /* stages (in logical order) */
160 struct draw_stage *feedback;
161 struct draw_stage *flatshade;
162 struct draw_stage *clip;
163 struct draw_stage *cull;
164 struct draw_stage *twoside;
165 struct draw_stage *offset;
166 struct draw_stage *unfilled;
167 struct draw_stage *wide;
168 struct draw_stage *rasterize;
169 } pipeline;
170
171 /* pipe state that we need: */
172 const struct pipe_rasterizer_state *rasterizer;
173 struct pipe_feedback_state feedback;
174 struct pipe_viewport_state viewport;
175 struct pipe_vertex_buffer vertex_buffer[PIPE_ATTRIB_MAX];
176 struct pipe_vertex_element vertex_element[PIPE_ATTRIB_MAX];
177 const struct draw_vertex_shader *vertex_shader;
178 struct pipe_vertex_buffer feedback_buffer[PIPE_ATTRIB_MAX];
179 struct pipe_vertex_element feedback_element[PIPE_ATTRIB_MAX];
180
181 /* user-space vertex data, buffers */
182 struct {
183 /** vertex element/index buffer (ex: glDrawElements) */
184 const void *elts;
185 /** bytes per index (0, 1, 2 or 4) */
186 unsigned eltSize;
187
188 /** vertex arrays */
189 const void *vbuffer[PIPE_ATTRIB_MAX];
190
191 /** constant buffer (for vertex shader) */
192 const void *constants;
193
194 /** The vertex feedback buffer */
195 void *feedback_buffer[PIPE_MAX_FEEDBACK_ATTRIBS];
196 uint feedback_buffer_size[PIPE_MAX_FEEDBACK_ATTRIBS]; /* in bytes */
197 } user;
198
199 /* Clip derived state:
200 */
201 float plane[12][4];
202 unsigned nr_planes;
203
204 /** Describes the layout of post-transformation vertices */
205 struct vertex_info vertex_info;
206 /** Two-sided attributes: */
207 uint attrib_front0, attrib_back0;
208 uint attrib_front1, attrib_back1;
209
210 boolean drawing; /**< do we presently have something queued for drawing? */
211 unsigned prim; /**< current prim type: PIPE_PRIM_x */
212 unsigned reduced_prim;
213
214 /** TGSI program interpreter runtime state */
215 struct tgsi_exec_machine machine;
216
217 /* Post-tnl vertex cache:
218 */
219 struct {
220 unsigned referenced; /**< bitfield */
221 unsigned idx[VCACHE_SIZE + VCACHE_OVERFLOW];
222 struct vertex_header *vertex[VCACHE_SIZE + VCACHE_OVERFLOW];
223 unsigned overflow;
224
225 /** To find space in the vertex cache: */
226 struct vertex_header *(*get_vertex)( struct draw_context *draw,
227 unsigned i );
228 } vcache;
229
230 /* Vertex shader queue:
231 */
232 struct {
233 struct {
234 unsigned elt; /**< index into the user's vertex arrays */
235 struct vertex_header *dest; /**< points into vcache.vertex[] array */
236 } queue[VS_QUEUE_LENGTH];
237 unsigned queue_nr;
238 } vs;
239
240 /* Prim pipeline queue:
241 */
242 struct {
243 /* Need to queue up primitives until their vertices have been
244 * transformed by a vs queue flush.
245 */
246 struct prim_header queue[PRIM_QUEUE_LENGTH];
247 unsigned queue_nr;
248 } pq;
249
250 int use_sse : 1;
251 #ifdef MESA_LLVM
252 struct gallivm_cpu_engine *engine;
253 #endif
254 };
255
256
257
258 extern struct draw_stage *draw_feedback_stage( struct draw_context *context );
259 extern struct draw_stage *draw_unfilled_stage( struct draw_context *context );
260 extern struct draw_stage *draw_twoside_stage( struct draw_context *context );
261 extern struct draw_stage *draw_offset_stage( struct draw_context *context );
262 extern struct draw_stage *draw_clip_stage( struct draw_context *context );
263 extern struct draw_stage *draw_flatshade_stage( struct draw_context *context );
264 extern struct draw_stage *draw_cull_stage( struct draw_context *context );
265 extern struct draw_stage *draw_wide_stage( struct draw_context *context );
266 extern struct draw_stage *draw_validate_stage( struct draw_context *context );
267
268
269 extern void draw_free_tmps( struct draw_stage *stage );
270 extern void draw_reset_tmps( struct draw_stage *stage );
271 extern void draw_alloc_tmps( struct draw_stage *stage, unsigned nr );
272
273 extern void draw_reset_vertex_ids( struct draw_context *draw );
274
275
276 extern int draw_vertex_cache_check_space( struct draw_context *draw,
277 unsigned nr_verts );
278
279 extern void draw_vertex_cache_invalidate( struct draw_context *draw );
280 extern void draw_vertex_cache_unreference( struct draw_context *draw );
281 extern void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw );
282
283
284 extern void draw_vertex_shader_queue_flush( struct draw_context *draw );
285 #ifdef MESA_LLVM
286 extern void draw_vertex_shader_queue_flush_llvm( struct draw_context *draw );
287 #endif
288
289 struct tgsi_exec_machine;
290
291 extern void draw_vertex_fetch( struct draw_context *draw,
292 struct tgsi_exec_machine *machine,
293 const unsigned *elts,
294 unsigned count );
295
296
297 #define DRAW_FLUSH_PRIM_QUEUE 0x1
298 #define DRAW_FLUSH_VERTEX_CACHE_INVALIDATE 0x2
299 #define DRAW_FLUSH_DRAW 0x4
300
301
302 void draw_do_flush( struct draw_context *draw,
303 unsigned flags );
304
305
306
307
308 /**
309 * Get a writeable copy of a vertex.
310 * \param stage drawing stage info
311 * \param vert the vertex to copy (source)
312 * \param idx index into stage's tmp[] array to put the copy (dest)
313 * \return pointer to the copied vertex
314 */
315 static INLINE struct vertex_header *
316 dup_vert( struct draw_stage *stage,
317 const struct vertex_header *vert,
318 unsigned idx )
319 {
320 struct vertex_header *tmp = stage->tmp[idx];
321 memcpy(tmp, vert, stage->draw->vertex_info.size * sizeof(float) );
322 tmp->vertex_id = UNDEFINED_VERTEX_ID;
323 return tmp;
324 }
325
326 static INLINE float
327 dot4(const float *a, const float *b)
328 {
329 float result = (a[0]*b[0] +
330 a[1]*b[1] +
331 a[2]*b[2] +
332 a[3]*b[3]);
333
334 return result;
335 }
336
337 #endif /* DRAW_PRIVATE_H */