Add new files.
[mesa.git] / src / gallium / auxiliary / 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 "rtasm/rtasm_x86sse.h"
48 #include "tgsi/exec/tgsi_exec.h"
49
50
51 struct pipe_context;
52 struct gallivm_prog;
53 struct gallivm_cpu_engine;
54
55 /**
56 * Basic vertex info.
57 * Carry some useful information around with the vertices in the prim pipe.
58 */
59 struct vertex_header {
60 unsigned clipmask:12;
61 unsigned edgeflag:1;
62 unsigned pad:3;
63 unsigned vertex_id:16;
64
65 float clip[4];
66
67 float data[][4]; /* Note variable size */
68 };
69
70 /* NOTE: It should match vertex_id size above */
71 #define UNDEFINED_VERTEX_ID 0xffff
72
73 /* XXX This is too large */
74 #define MAX_VERTEX_SIZE ((2 + PIPE_MAX_SHADER_OUTPUTS) * 4 * sizeof(float))
75
76
77
78 /**
79 * Basic info for a point/line/triangle primitive.
80 */
81 struct prim_header {
82 float det; /**< front/back face determinant */
83 unsigned reset_line_stipple:1;
84 unsigned edgeflags:3;
85 unsigned pad:28;
86 struct vertex_header *v[3]; /**< 1 to 3 vertex pointers */
87 };
88
89
90
91 struct draw_context;
92
93 /**
94 * Base class for all primitive drawing stages.
95 */
96 struct draw_stage
97 {
98 struct draw_context *draw; /**< parent context */
99
100 struct draw_stage *next; /**< next stage in pipeline */
101
102 struct vertex_header **tmp; /**< temp vert storage, such as for clipping */
103 unsigned nr_tmps;
104
105 void (*point)( struct draw_stage *,
106 struct prim_header * );
107
108 void (*line)( struct draw_stage *,
109 struct prim_header * );
110
111 void (*tri)( struct draw_stage *,
112 struct prim_header * );
113
114 void (*flush)( struct draw_stage *,
115 unsigned flags );
116
117 void (*reset_stipple_counter)( struct draw_stage * );
118
119 void (*destroy)( struct draw_stage * );
120 };
121
122
123 #define PRIM_QUEUE_LENGTH 32
124 #define VCACHE_SIZE 32
125 #define VCACHE_OVERFLOW 4
126 #define VS_QUEUE_LENGTH (VCACHE_SIZE + VCACHE_OVERFLOW + 1) /* can never fill up */
127
128 /**
129 * Private version of the compiled vertex_shader
130 */
131 struct draw_vertex_shader {
132
133 /* This member will disappear shortly:
134 */
135 const struct pipe_shader_state *state;
136
137 void (*prepare)( struct draw_vertex_shader *shader,
138 struct draw_context *draw );
139
140 /* Run the shader - this interface will get cleaned up in the
141 * future:
142 */
143 void (*run)( struct draw_vertex_shader *shader,
144 struct draw_context *draw,
145 const unsigned *elts,
146 unsigned count,
147 struct vertex_header *vOut[] );
148
149
150 void (*delete)( struct draw_vertex_shader * );
151 };
152
153
154 /* Internal function for vertex fetch.
155 */
156 typedef void (*fetch_func)(const void *ptr, float *attrib);
157 typedef void (*full_fetch_func)( struct draw_context *draw,
158 struct tgsi_exec_machine *machine,
159 const unsigned *elts,
160 unsigned count );
161
162
163
164 /**
165 * Private context for the drawing module.
166 */
167 struct draw_context
168 {
169 /** Drawing/primitive pipeline stages */
170 struct {
171 struct draw_stage *first; /**< one of the following */
172
173 struct draw_stage *validate;
174
175 /* stages (in logical order) */
176 struct draw_stage *flatshade;
177 struct draw_stage *clip;
178 struct draw_stage *cull;
179 struct draw_stage *twoside;
180 struct draw_stage *offset;
181 struct draw_stage *unfilled;
182 struct draw_stage *stipple;
183 struct draw_stage *aapoint;
184 struct draw_stage *aaline;
185 struct draw_stage *pstipple;
186 struct draw_stage *wide;
187 struct draw_stage *rasterize;
188 } pipeline;
189
190 /* pipe state that we need: */
191 const struct pipe_rasterizer_state *rasterizer;
192 struct pipe_viewport_state viewport;
193 struct pipe_vertex_buffer vertex_buffer[PIPE_ATTRIB_MAX];
194 struct pipe_vertex_element vertex_element[PIPE_ATTRIB_MAX];
195 struct draw_vertex_shader *vertex_shader;
196
197 uint num_vs_outputs; /**< convenience, from vertex_shader */
198
199 /* user-space vertex data, buffers */
200 struct {
201 /** vertex element/index buffer (ex: glDrawElements) */
202 const void *elts;
203 /** bytes per index (0, 1, 2 or 4) */
204 unsigned eltSize;
205
206 /** vertex arrays */
207 const void *vbuffer[PIPE_ATTRIB_MAX];
208
209 /** constant buffer (for vertex shader) */
210 const void *constants;
211 } user;
212
213 /* Clip derived state:
214 */
215 float plane[12][4];
216 unsigned nr_planes;
217
218 boolean convert_wide_points; /**< convert wide points to tris? */
219 boolean convert_wide_lines; /**< convert wide lines to tris? */
220 boolean use_sse;
221
222 /* If a prim stage introduces new vertex attributes, they'll be stored here
223 */
224 struct {
225 uint semantic_name;
226 uint semantic_index;
227 int slot;
228 } extra_vp_outputs;
229
230 unsigned reduced_prim;
231
232 /** TGSI program interpreter runtime state */
233 struct tgsi_exec_machine machine;
234
235 /* Vertex fetch internal state
236 */
237 struct {
238 const ubyte *src_ptr[PIPE_ATTRIB_MAX];
239 unsigned pitch[PIPE_ATTRIB_MAX];
240 fetch_func fetch[PIPE_ATTRIB_MAX];
241 unsigned nr_attrs;
242 full_fetch_func fetch_func;
243 } vertex_fetch;
244
245 /* Post-tnl vertex cache:
246 */
247 struct {
248 unsigned referenced; /**< bitfield */
249
250 struct {
251 unsigned in; /* client array element */
252 unsigned out; /* index in vs queue/array */
253 } idx[VCACHE_SIZE + VCACHE_OVERFLOW];
254
255 unsigned overflow;
256
257 /** To find space in the vertex cache: */
258 struct vertex_header *(*get_vertex)( struct draw_context *draw,
259 unsigned i );
260 } vcache;
261
262 /* Vertex shader queue:
263 */
264 struct {
265 struct {
266 unsigned elt; /**< index into the user's vertex arrays */
267 struct vertex_header *vertex;
268 } queue[VS_QUEUE_LENGTH];
269 unsigned queue_nr;
270 unsigned post_nr;
271 } vs;
272
273 /**
274 * Run the vertex shader on all vertices in the vertex queue.
275 */
276 void (*shader_queue_flush)(struct draw_context *draw);
277
278 /* Prim pipeline queue:
279 */
280 struct {
281 /* Need to queue up primitives until their vertices have been
282 * transformed by a vs queue flush.
283 */
284 struct prim_header queue[PRIM_QUEUE_LENGTH];
285 unsigned queue_nr;
286 } pq;
287
288
289 /* This (and the tgsi_exec_machine struct) probably need to be moved somewhere private.
290 */
291 struct gallivm_cpu_engine *engine;
292 void *driver_private;
293 };
294
295
296
297 extern struct draw_stage *draw_unfilled_stage( struct draw_context *context );
298 extern struct draw_stage *draw_twoside_stage( struct draw_context *context );
299 extern struct draw_stage *draw_offset_stage( struct draw_context *context );
300 extern struct draw_stage *draw_clip_stage( struct draw_context *context );
301 extern struct draw_stage *draw_flatshade_stage( struct draw_context *context );
302 extern struct draw_stage *draw_cull_stage( struct draw_context *context );
303 extern struct draw_stage *draw_stipple_stage( struct draw_context *context );
304 extern struct draw_stage *draw_wide_stage( struct draw_context *context );
305 extern struct draw_stage *draw_validate_stage( struct draw_context *context );
306
307
308 extern void draw_free_temp_verts( struct draw_stage *stage );
309
310 extern void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr );
311
312 extern void draw_reset_vertex_ids( struct draw_context *draw );
313
314
315 extern int draw_vertex_cache_check_space( struct draw_context *draw,
316 unsigned nr_verts );
317
318 extern void draw_vertex_cache_invalidate( struct draw_context *draw );
319 extern void draw_vertex_cache_unreference( struct draw_context *draw );
320 extern void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw );
321
322 extern void draw_vertex_shader_queue_flush( struct draw_context *draw );
323
324 struct tgsi_exec_machine;
325
326 extern void draw_update_vertex_fetch( struct draw_context *draw );
327
328
329 #define DRAW_FLUSH_SHADER_QUEUE 0x1 /* sized not to overflow, never raised */
330 #define DRAW_FLUSH_PRIM_QUEUE 0x2
331 #define DRAW_FLUSH_VERTEX_CACHE 0x4
332 #define DRAW_FLUSH_STATE_CHANGE 0x8
333 #define DRAW_FLUSH_BACKEND 0x10
334
335
336 void draw_do_flush( struct draw_context *draw, unsigned flags );
337
338
339
340 /**
341 * Get a writeable copy of a vertex.
342 * \param stage drawing stage info
343 * \param vert the vertex to copy (source)
344 * \param idx index into stage's tmp[] array to put the copy (dest)
345 * \return pointer to the copied vertex
346 */
347 static INLINE struct vertex_header *
348 dup_vert( struct draw_stage *stage,
349 const struct vertex_header *vert,
350 unsigned idx )
351 {
352 struct vertex_header *tmp = stage->tmp[idx];
353 const uint vsize = sizeof(struct vertex_header)
354 + stage->draw->num_vs_outputs * 4 * sizeof(float);
355 memcpy(tmp, vert, vsize);
356 tmp->vertex_id = UNDEFINED_VERTEX_ID;
357 return tmp;
358 }
359
360 static INLINE float
361 dot4(const float *a, const float *b)
362 {
363 float result = (a[0]*b[0] +
364 a[1]*b[1] +
365 a[2]*b[2] +
366 a[3]*b[3]);
367
368 return result;
369 }
370
371 #endif /* DRAW_PRIVATE_H */