Remove src/mesa and src/mesa/main from gallium source include paths.
[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 16
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 *aaline;
184 struct draw_stage *wide;
185 struct draw_stage *rasterize;
186 } pipeline;
187
188 /* pipe state that we need: */
189 const struct pipe_rasterizer_state *rasterizer;
190 struct pipe_viewport_state viewport;
191 struct pipe_vertex_buffer vertex_buffer[PIPE_ATTRIB_MAX];
192 struct pipe_vertex_element vertex_element[PIPE_ATTRIB_MAX];
193 struct draw_vertex_shader *vertex_shader;
194
195 uint num_vs_outputs; /**< convenience, from vertex_shader */
196
197 /* user-space vertex data, buffers */
198 struct {
199 /** vertex element/index buffer (ex: glDrawElements) */
200 const void *elts;
201 /** bytes per index (0, 1, 2 or 4) */
202 unsigned eltSize;
203
204 /** vertex arrays */
205 const void *vbuffer[PIPE_ATTRIB_MAX];
206
207 /** constant buffer (for vertex shader) */
208 const void *constants;
209 } user;
210
211 /* Clip derived state:
212 */
213 float plane[12][4];
214 unsigned nr_planes;
215
216 boolean convert_wide_points; /**< convert wide points to tris? */
217 boolean convert_wide_lines; /**< convert wide lines to tris? */
218 boolean use_sse;
219
220 /* If a prim stage introduces new vertex attributes, they'll be stored here
221 */
222 struct {
223 uint semantic_name;
224 uint semantic_index;
225 int slot;
226 } extra_vp_outputs;
227
228 unsigned reduced_prim;
229
230 /** TGSI program interpreter runtime state */
231 struct tgsi_exec_machine machine;
232
233 /* Vertex fetch internal state
234 */
235 struct {
236 const ubyte *src_ptr[PIPE_ATTRIB_MAX];
237 unsigned pitch[PIPE_ATTRIB_MAX];
238 fetch_func fetch[PIPE_ATTRIB_MAX];
239 unsigned nr_attrs;
240 full_fetch_func fetch_func;
241 } vertex_fetch;
242
243 /* Post-tnl vertex cache:
244 */
245 struct {
246 unsigned referenced; /**< bitfield */
247 unsigned idx[VCACHE_SIZE + VCACHE_OVERFLOW];
248 struct vertex_header *vertex[VCACHE_SIZE + VCACHE_OVERFLOW];
249 unsigned overflow;
250
251 /** To find space in the vertex cache: */
252 struct vertex_header *(*get_vertex)( struct draw_context *draw,
253 unsigned i );
254 } vcache;
255
256 /* Vertex shader queue:
257 */
258 struct {
259 struct {
260 unsigned elt; /**< index into the user's vertex arrays */
261 struct vertex_header *dest; /**< points into vcache.vertex[] array */
262 } queue[VS_QUEUE_LENGTH];
263 unsigned queue_nr;
264 } vs;
265
266 /**
267 * Run the vertex shader on all vertices in the vertex queue.
268 */
269 void (*shader_queue_flush)(struct draw_context *draw);
270
271 /* Prim pipeline queue:
272 */
273 struct {
274 /* Need to queue up primitives until their vertices have been
275 * transformed by a vs queue flush.
276 */
277 struct prim_header queue[PRIM_QUEUE_LENGTH];
278 unsigned queue_nr;
279 } pq;
280
281
282 /* This (and the tgsi_exec_machine struct) probably need to be moved somewhere private.
283 */
284 struct gallivm_cpu_engine *engine;
285 void *driver_private;
286 };
287
288
289
290 extern struct draw_stage *draw_unfilled_stage( struct draw_context *context );
291 extern struct draw_stage *draw_twoside_stage( struct draw_context *context );
292 extern struct draw_stage *draw_offset_stage( struct draw_context *context );
293 extern struct draw_stage *draw_clip_stage( struct draw_context *context );
294 extern struct draw_stage *draw_flatshade_stage( struct draw_context *context );
295 extern struct draw_stage *draw_cull_stage( struct draw_context *context );
296 extern struct draw_stage *draw_stipple_stage( struct draw_context *context );
297 extern struct draw_stage *draw_wide_stage( struct draw_context *context );
298 extern struct draw_stage *draw_validate_stage( struct draw_context *context );
299
300
301 extern void draw_free_temp_verts( struct draw_stage *stage );
302
303 extern void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr );
304
305 extern void draw_reset_vertex_ids( struct draw_context *draw );
306
307
308 extern int draw_vertex_cache_check_space( struct draw_context *draw,
309 unsigned nr_verts );
310
311 extern void draw_vertex_cache_invalidate( struct draw_context *draw );
312 extern void draw_vertex_cache_unreference( struct draw_context *draw );
313 extern void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw );
314
315 extern void draw_vertex_shader_queue_flush( struct draw_context *draw );
316
317 struct tgsi_exec_machine;
318
319 extern void draw_update_vertex_fetch( struct draw_context *draw );
320
321
322 #define DRAW_FLUSH_SHADER_QUEUE 0x1 /* sized not to overflow, never raised */
323 #define DRAW_FLUSH_PRIM_QUEUE 0x2
324 #define DRAW_FLUSH_VERTEX_CACHE 0x4
325 #define DRAW_FLUSH_STATE_CHANGE 0x8
326 #define DRAW_FLUSH_BACKEND 0x10
327
328
329 void draw_do_flush( struct draw_context *draw, unsigned flags );
330
331
332
333 /**
334 * Get a writeable copy of a vertex.
335 * \param stage drawing stage info
336 * \param vert the vertex to copy (source)
337 * \param idx index into stage's tmp[] array to put the copy (dest)
338 * \return pointer to the copied vertex
339 */
340 static INLINE struct vertex_header *
341 dup_vert( struct draw_stage *stage,
342 const struct vertex_header *vert,
343 unsigned idx )
344 {
345 struct vertex_header *tmp = stage->tmp[idx];
346 const uint vsize = sizeof(struct vertex_header)
347 + stage->draw->num_vs_outputs * 4 * sizeof(float);
348 memcpy(tmp, vert, vsize);
349 tmp->vertex_id = UNDEFINED_VERTEX_ID;
350 return tmp;
351 }
352
353 static INLINE float
354 dot4(const float *a, const float *b)
355 {
356 float result = (a[0]*b[0] +
357 a[1]*b[1] +
358 a[2]*b[2] +
359 a[3]*b[3]);
360
361 return result;
362 }
363
364 #endif /* DRAW_PRIVATE_H */