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