draw: keep record of number of active vertex buffers
[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 #include "tgsi/util/tgsi_scan.h"
50
51
52 struct pipe_context;
53 struct gallivm_prog;
54 struct gallivm_cpu_engine;
55
56 struct draw_pt_middle_end;
57 struct draw_pt_front_end;
58
59 #define MAX_SHADER_VERTICES 128
60
61 /**
62 * Basic vertex info.
63 * Carry some useful information around with the vertices in the prim pipe.
64 */
65 struct vertex_header {
66 unsigned clipmask:12;
67 unsigned edgeflag:1;
68 unsigned pad:3;
69 unsigned vertex_id:16;
70
71 float clip[4];
72
73 float data[][4]; /* Note variable size */
74 };
75
76 /* NOTE: It should match vertex_id size above */
77 #define UNDEFINED_VERTEX_ID 0xffff
78
79 /* XXX This is too large */
80 #define MAX_VERTEX_SIZE ((2 + PIPE_MAX_SHADER_OUTPUTS) * 4 * sizeof(float))
81 #define MAX_VERTEX_ALLOCATION ((MAX_VERTEX_SIZE + 0x0f) & ~0x0f)
82
83
84
85 /**
86 * Basic info for a point/line/triangle primitive.
87 */
88 struct prim_header {
89 float det; /**< front/back face determinant */
90 unsigned reset_line_stipple:1;
91 unsigned edgeflags:3;
92 unsigned pad:28;
93 struct vertex_header *v[3]; /**< 1 to 3 vertex pointers */
94 };
95
96
97
98 struct draw_context;
99
100 /**
101 * Base class for all primitive drawing stages.
102 */
103 struct draw_stage
104 {
105 struct draw_context *draw; /**< parent context */
106
107 struct draw_stage *next; /**< next stage in pipeline */
108
109 struct vertex_header **tmp; /**< temp vert storage, such as for clipping */
110 unsigned nr_tmps;
111
112 void (*point)( struct draw_stage *,
113 struct prim_header * );
114
115 void (*line)( struct draw_stage *,
116 struct prim_header * );
117
118 void (*tri)( struct draw_stage *,
119 struct prim_header * );
120
121 void (*flush)( struct draw_stage *,
122 unsigned flags );
123
124 void (*reset_stipple_counter)( struct draw_stage * );
125
126 void (*destroy)( struct draw_stage * );
127 };
128
129
130 #define PRIM_QUEUE_LENGTH 32
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
140 /* This member will disappear shortly:
141 */
142 struct pipe_shader_state state;
143
144 struct tgsi_shader_info info;
145
146 void (*prepare)( struct draw_vertex_shader *shader,
147 struct draw_context *draw );
148
149 /* Run the shader - this interface will get cleaned up in the
150 * future:
151 */
152 boolean (*run)( struct draw_vertex_shader *shader,
153 struct draw_context *draw,
154 const unsigned *elts,
155 unsigned count,
156 void *out,
157 unsigned vertex_size);
158
159
160 void (*delete)( struct draw_vertex_shader * );
161 };
162
163
164 /* Internal function for vertex fetch.
165 */
166 typedef void (*fetch_func)(const void *ptr, float *attrib);
167
168 fetch_func draw_get_fetch_func( enum pipe_format format );
169
170
171
172 typedef void (*full_fetch_func)( struct draw_context *draw,
173 struct tgsi_exec_machine *machine,
174 const unsigned *elts,
175 unsigned count );
176
177 typedef void (*pt_fetch_func)( struct draw_context *draw,
178 float *out,
179 unsigned start,
180 unsigned count );
181
182
183 struct vbuf_render;
184
185
186 #define PT_SHADE 0x1
187 #define PT_CLIPTEST 0x2
188 #define PT_PIPELINE 0x4
189 #define PT_MAX_MIDDLE 0x8
190
191 /**
192 * Private context for the drawing module.
193 */
194 struct draw_context
195 {
196 /** Drawing/primitive pipeline stages */
197 struct {
198 struct draw_stage *first; /**< one of the following */
199
200 struct draw_stage *validate;
201
202 /* stages (in logical order) */
203 struct draw_stage *flatshade;
204 struct draw_stage *clip;
205 struct draw_stage *cull;
206 struct draw_stage *twoside;
207 struct draw_stage *offset;
208 struct draw_stage *unfilled;
209 struct draw_stage *stipple;
210 struct draw_stage *aapoint;
211 struct draw_stage *aaline;
212 struct draw_stage *pstipple;
213 struct draw_stage *wide_line;
214 struct draw_stage *wide_point;
215 struct draw_stage *rasterize;
216 } pipeline;
217
218
219 struct vbuf_render *render;
220
221 /* Support prototype passthrough path:
222 */
223 struct {
224 unsigned prim; /* XXX: to be removed */
225 unsigned hw_vertex_size; /* XXX: to be removed */
226
227 struct {
228 struct draw_pt_middle_end *opt[PT_MAX_MIDDLE];
229 } middle;
230
231 struct {
232 struct draw_pt_front_end *vcache;
233 } front;
234
235 struct {
236 char *verts;
237 unsigned vertex_stride;
238 unsigned vertex_count;
239 } pipeline;
240
241 } pt;
242
243 boolean flushing;
244
245 /* pipe state that we need: */
246 const struct pipe_rasterizer_state *rasterizer;
247 struct pipe_viewport_state viewport;
248
249 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
250 unsigned nr_vertex_buffers;
251
252 struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
253 struct draw_vertex_shader *vertex_shader;
254
255 boolean identity_viewport;
256
257 uint num_vs_outputs; /**< convenience, from vertex_shader */
258
259 /* user-space vertex data, buffers */
260 struct {
261 const unsigned *edgeflag;
262
263 /** vertex element/index buffer (ex: glDrawElements) */
264 const void *elts;
265 /** bytes per index (0, 1, 2 or 4) */
266 unsigned eltSize;
267
268 /** vertex arrays */
269 const void *vbuffer[PIPE_MAX_ATTRIBS];
270
271 /** constant buffer (for vertex shader) */
272 const void *constants;
273 } user;
274
275 /* Clip derived state:
276 */
277 float plane[12][4];
278 unsigned nr_planes;
279
280 float wide_point_threshold; /**< convert pnts to tris if larger than this */
281 float wide_line_threshold; /**< convert lines to tris if wider than this */
282 boolean line_stipple; /**< do line stipple? */
283 boolean point_sprite; /**< convert points to quads for sprites? */
284 boolean use_sse;
285 boolean use_pt_shaders; /* temporary flag to switch on pt shader paths */
286
287 /* If a prim stage introduces new vertex attributes, they'll be stored here
288 */
289 struct {
290 uint semantic_name;
291 uint semantic_index;
292 int slot;
293 } extra_vp_outputs;
294
295 unsigned reduced_prim;
296
297 /** TGSI program interpreter runtime state */
298 struct tgsi_exec_machine machine;
299
300 /* Vertex fetch internal state
301 */
302 struct {
303 const ubyte *src_ptr[PIPE_MAX_ATTRIBS];
304 unsigned pitch[PIPE_MAX_ATTRIBS];
305 fetch_func fetch[PIPE_MAX_ATTRIBS];
306 unsigned nr_attrs;
307 full_fetch_func fetch_func;
308 pt_fetch_func pt_fetch;
309 } vertex_fetch;
310
311 /* Post-tnl vertex cache:
312 */
313 struct {
314 unsigned referenced; /**< bitfield */
315
316 struct {
317 unsigned in; /* client array element */
318 unsigned out; /* index in vs queue/array */
319 } idx[VCACHE_SIZE + VCACHE_OVERFLOW];
320
321 unsigned overflow;
322
323 /** To find space in the vertex cache: */
324 struct vertex_header *(*get_vertex)( struct draw_context *draw,
325 unsigned i );
326 } vcache;
327
328 /* Vertex shader queue:
329 */
330 struct {
331 unsigned elts[VS_QUEUE_LENGTH]; /**< index into the user's vertex arrays */
332 char *vertex_cache;
333 unsigned queue_nr;
334 unsigned post_nr;
335 } vs;
336
337 /**
338 * Run the vertex shader on all vertices in the vertex queue.
339 */
340 void (*shader_queue_flush)(struct draw_context *draw);
341
342 /* Prim pipeline queue:
343 */
344 struct {
345 /* Need to queue up primitives until their vertices have been
346 * transformed by a vs queue flush.
347 */
348 struct prim_header queue[PRIM_QUEUE_LENGTH];
349 unsigned queue_nr;
350 } pq;
351
352
353 /* This (and the tgsi_exec_machine struct) probably need to be moved somewhere private.
354 */
355 struct gallivm_cpu_engine *engine;
356 void *driver_private;
357 };
358
359
360
361 extern struct draw_stage *draw_unfilled_stage( struct draw_context *context );
362 extern struct draw_stage *draw_twoside_stage( struct draw_context *context );
363 extern struct draw_stage *draw_offset_stage( struct draw_context *context );
364 extern struct draw_stage *draw_clip_stage( struct draw_context *context );
365 extern struct draw_stage *draw_flatshade_stage( struct draw_context *context );
366 extern struct draw_stage *draw_cull_stage( struct draw_context *context );
367 extern struct draw_stage *draw_stipple_stage( struct draw_context *context );
368 extern struct draw_stage *draw_wide_line_stage( struct draw_context *context );
369 extern struct draw_stage *draw_wide_point_stage( struct draw_context *context );
370 extern struct draw_stage *draw_validate_stage( struct draw_context *context );
371
372
373 extern void draw_free_temp_verts( struct draw_stage *stage );
374
375 extern void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr );
376
377 extern void draw_reset_vertex_ids( struct draw_context *draw );
378
379
380 extern int draw_vertex_cache_check_space( struct draw_context *draw,
381 unsigned nr_verts );
382
383 extern void draw_vertex_cache_invalidate( struct draw_context *draw );
384 extern void draw_vertex_cache_unreference( struct draw_context *draw );
385 extern void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw );
386
387 extern void draw_vertex_shader_queue_flush( struct draw_context *draw );
388
389 extern void draw_update_vertex_fetch( struct draw_context *draw );
390
391 extern boolean draw_need_pipeline(const struct draw_context *draw,
392 unsigned prim );
393
394
395 /* Passthrough mode (second attempt):
396 */
397 boolean draw_pt_init( struct draw_context *draw );
398 void draw_pt_destroy( struct draw_context *draw );
399 boolean draw_pt_arrays( struct draw_context *draw,
400 unsigned prim,
401 unsigned start,
402 unsigned count );
403
404 void draw_pt_reset_vertex_ids( struct draw_context *draw );
405 void draw_pt_run_pipeline( struct draw_context *draw,
406 unsigned prim,
407 char *verts,
408 unsigned vertex_stride,
409 unsigned vertex_count,
410 const ushort *elts,
411 unsigned count );
412
413
414 #define DRAW_FLUSH_SHADER_QUEUE 0x1 /* sized not to overflow, never raised */
415 #define DRAW_FLUSH_PRIM_QUEUE 0x2
416 #define DRAW_FLUSH_VERTEX_CACHE 0x4
417 #define DRAW_FLUSH_STATE_CHANGE 0x8
418 #define DRAW_FLUSH_BACKEND 0x10
419
420
421 void draw_do_flush( struct draw_context *draw, unsigned flags );
422
423 boolean draw_get_edgeflag( struct draw_context *draw,
424 unsigned idx );
425
426
427 /**
428 * Get a writeable copy of a vertex.
429 * \param stage drawing stage info
430 * \param vert the vertex to copy (source)
431 * \param idx index into stage's tmp[] array to put the copy (dest)
432 * \return pointer to the copied vertex
433 */
434 static INLINE struct vertex_header *
435 dup_vert( struct draw_stage *stage,
436 const struct vertex_header *vert,
437 unsigned idx )
438 {
439 struct vertex_header *tmp = stage->tmp[idx];
440 const uint vsize = sizeof(struct vertex_header)
441 + stage->draw->num_vs_outputs * 4 * sizeof(float);
442 memcpy(tmp, vert, vsize);
443 tmp->vertex_id = UNDEFINED_VERTEX_ID;
444 return tmp;
445 }
446
447 static INLINE float
448 dot4(const float *a, const float *b)
449 {
450 float result = (a[0]*b[0] +
451 a[1]*b[1] +
452 a[2]*b[2] +
453 a[3]*b[3]);
454
455 return result;
456 }
457
458 static INLINE struct vertex_header *
459 draw_header_from_block(char *block, int size, int num)
460 {
461 return (struct vertex_header*)(block + num * size);
462 }
463
464 #endif /* DRAW_PRIVATE_H */