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