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