draw: add support for passing images to vs/gs shaders.
[mesa.git] / src / gallium / auxiliary / draw / draw_private.h
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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 "tgsi/tgsi_scan.h"
48
49 #ifdef HAVE_LLVM
50 struct gallivm_state;
51 #endif
52
53
54 /** Sum of frustum planes and user-defined planes */
55 #define DRAW_TOTAL_CLIP_PLANES (6 + PIPE_MAX_CLIP_PLANES)
56
57 /**
58 * The largest possible index of a vertex that can be fetched.
59 */
60 #define DRAW_MAX_FETCH_IDX 0xffffffff
61
62 struct pipe_context;
63 struct draw_vertex_shader;
64 struct draw_context;
65 struct draw_stage;
66 struct vbuf_render;
67 struct tgsi_exec_machine;
68 struct tgsi_sampler;
69 struct tgsi_image;
70 struct draw_pt_front_end;
71 struct draw_assembler;
72 struct draw_llvm;
73
74
75 /**
76 * Represents the mapped vertex buffer.
77 */
78 struct draw_vertex_buffer {
79 const void *map;
80 uint32_t size;
81 };
82
83 /**
84 * Basic vertex info.
85 * Carry some useful information around with the vertices in the prim pipe.
86 */
87 struct vertex_header {
88 unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
89 unsigned edgeflag:1;
90 unsigned pad:1;
91 unsigned vertex_id:16;
92
93 float clip_pos[4];
94
95 /* This will probably become float (*data)[4] soon:
96 */
97 float data[][4];
98 };
99
100 /* NOTE: It should match vertex_id size above */
101 #define UNDEFINED_VERTEX_ID 0xffff
102
103
104 /* maximum number of shader variants we can cache */
105 #define DRAW_MAX_SHADER_VARIANTS 128
106
107 /**
108 * Private context for the drawing module.
109 */
110 struct draw_context
111 {
112 struct pipe_context *pipe;
113
114 /** Drawing/primitive pipeline stages */
115 struct {
116 struct draw_stage *first; /**< one of the following */
117
118 struct draw_stage *validate;
119
120 /* stages (in logical order) */
121 struct draw_stage *flatshade;
122 struct draw_stage *clip;
123 struct draw_stage *cull;
124 struct draw_stage *twoside;
125 struct draw_stage *offset;
126 struct draw_stage *unfilled;
127 struct draw_stage *stipple;
128 struct draw_stage *aapoint;
129 struct draw_stage *aaline;
130 struct draw_stage *pstipple;
131 struct draw_stage *wide_line;
132 struct draw_stage *wide_point;
133 struct draw_stage *rasterize;
134
135 float wide_point_threshold; /**< convert pnts to tris if larger than this */
136 float wide_line_threshold; /**< convert lines to tris if wider than this */
137 boolean wide_point_sprites; /**< convert points to tris for sprite mode */
138 boolean line_stipple; /**< do line stipple? */
139 boolean point_sprite; /**< convert points to quads for sprites? */
140
141 /* Temporary storage while the pipeline is being run:
142 */
143 char *verts;
144 unsigned vertex_stride;
145 unsigned vertex_count;
146 } pipeline;
147
148
149 struct vbuf_render *render;
150
151 /* Support prototype passthrough path:
152 */
153 struct {
154 /* Current active frontend */
155 struct draw_pt_front_end *frontend;
156 unsigned prim;
157 unsigned opt; /**< bitmask of PT_x flags */
158 unsigned eltSize; /* saved eltSize for flushing */
159
160 boolean rebind_parameters;
161
162 struct {
163 struct draw_pt_middle_end *fetch_emit;
164 struct draw_pt_middle_end *fetch_shade_emit;
165 struct draw_pt_middle_end *general;
166 struct draw_pt_middle_end *llvm;
167 } middle;
168
169 struct {
170 struct draw_pt_front_end *vsplit;
171 } front;
172
173 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
174 unsigned nr_vertex_buffers;
175
176 /*
177 * This is the largest legal index value for the current set of
178 * bound vertex buffers. Regardless of any other consideration,
179 * all vertex lookups need to be clamped to 0..max_index to
180 * prevent out-of-bound access.
181 */
182 unsigned max_index;
183
184 struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
185 unsigned nr_vertex_elements;
186
187 /* user-space vertex data, buffers */
188 struct {
189 /** vertex element/index buffer (ex: glDrawElements) */
190 const void *elts;
191 /** bytes per index (0, 1, 2 or 4) */
192 unsigned eltSizeIB;
193 unsigned eltSize;
194 unsigned eltMax;
195 int eltBias;
196 unsigned min_index;
197 unsigned max_index;
198
199 /** vertex arrays */
200 struct draw_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
201
202 /** constant buffers (for vertex/geometry shader) */
203 const void *vs_constants[PIPE_MAX_CONSTANT_BUFFERS];
204 unsigned vs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
205 const void *gs_constants[PIPE_MAX_CONSTANT_BUFFERS];
206 unsigned gs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
207
208 /* pointer to planes */
209 float (*planes)[DRAW_TOTAL_CLIP_PLANES][4];
210 } user;
211
212 boolean test_fse; /* enable FSE even though its not correct (eg for softpipe) */
213 boolean no_fse; /* disable FSE even when it is correct */
214 } pt;
215
216 struct {
217 boolean bypass_clip_xy;
218 boolean bypass_clip_z;
219 boolean guard_band_xy;
220 boolean bypass_clip_points;
221 } driver;
222
223 boolean quads_always_flatshade_last;
224
225 boolean flushing; /**< debugging/sanity */
226 boolean suspend_flushing; /**< internally set */
227
228 /* Flags set if API requires clipping in these planes and the
229 * driver doesn't indicate that it can do it for us.
230 */
231 boolean clip_xy;
232 boolean clip_z;
233 boolean clip_user;
234 boolean guard_band_xy;
235 boolean guard_band_points_xy;
236
237 boolean force_passthrough; /**< never clip or shade */
238
239 boolean dump_vs;
240
241 /** Depth format and bias related settings. */
242 boolean floating_point_depth;
243 double mrd; /**< minimum resolvable depth value, for polygon offset */
244
245 /** Current rasterizer state given to us by the driver */
246 const struct pipe_rasterizer_state *rasterizer;
247 /** Driver CSO handle for the current rasterizer state */
248 void *rast_handle;
249
250 /** Rasterizer CSOs without culling/stipple/etc */
251 void *rasterizer_no_cull[2][2];
252
253 struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];
254 boolean identity_viewport;
255 boolean bypass_viewport;
256
257 /** Vertex shader state */
258 struct {
259 struct draw_vertex_shader *vertex_shader;
260 uint num_vs_outputs; /**< convenience, from vertex_shader */
261 uint position_output;
262 uint edgeflag_output;
263 uint clipvertex_output;
264 uint clipdistance_output[2];
265
266 /** Fields for TGSI interpreter / execution */
267 struct {
268 struct tgsi_exec_machine *machine;
269
270 struct tgsi_sampler *sampler;
271 struct tgsi_image *image;
272 } tgsi;
273
274 struct translate *fetch;
275 struct translate_cache *fetch_cache;
276 struct translate *emit;
277 struct translate_cache *emit_cache;
278 } vs;
279
280 /** Geometry shader state */
281 struct {
282 struct draw_geometry_shader *geometry_shader;
283 uint num_gs_outputs; /**< convenience, from geometry_shader */
284 uint position_output;
285
286 /** Fields for TGSI interpreter / execution */
287 struct {
288 struct tgsi_exec_machine *machine;
289
290 struct tgsi_sampler *sampler;
291 struct tgsi_image *image;
292 } tgsi;
293
294 } gs;
295
296 /** Fragment shader state */
297 struct {
298 struct draw_fragment_shader *fragment_shader;
299 } fs;
300
301 /** Stream output (vertex feedback) state */
302 struct {
303 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
304 uint num_targets;
305 } so;
306
307 /* Clip derived state:
308 */
309 float plane[DRAW_TOTAL_CLIP_PLANES][4];
310
311 /* If a prim stage introduces new vertex attributes, they'll be stored here
312 */
313 struct {
314 uint num;
315 uint semantic_name[10];
316 uint semantic_index[10];
317 uint slot[10];
318 } extra_shader_outputs;
319
320 unsigned instance_id;
321 unsigned start_instance;
322 unsigned start_index;
323
324 struct draw_llvm *llvm;
325
326 /** Texture sampler and sampler view state.
327 * Note that we have arrays indexed by shader type. At this time
328 * we only handle vertex and geometry shaders in the draw module, but
329 * there may be more in the future (ex: hull and tessellation).
330 */
331 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
332 unsigned num_sampler_views[PIPE_SHADER_TYPES];
333 const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
334 unsigned num_samplers[PIPE_SHADER_TYPES];
335
336 struct pipe_query_data_pipeline_statistics statistics;
337 boolean collect_statistics;
338
339 struct draw_assembler *ia;
340
341 void *driver_private;
342 };
343
344
345 struct draw_fetch_info {
346 boolean linear;
347 unsigned start;
348 const unsigned *elts;
349 unsigned count;
350 };
351
352 struct draw_vertex_info {
353 struct vertex_header *verts;
354 unsigned vertex_size;
355 unsigned stride;
356 unsigned count;
357 };
358
359 /* these flags are set if the primitive is a segment of a larger one */
360 #define DRAW_SPLIT_BEFORE 0x1
361 #define DRAW_SPLIT_AFTER 0x2
362 #define DRAW_LINE_LOOP_AS_STRIP 0x4
363
364 struct draw_prim_info {
365 boolean linear;
366 unsigned start;
367
368 const ushort *elts;
369 unsigned count;
370
371 unsigned prim;
372 unsigned flags;
373 unsigned *primitive_lengths;
374 unsigned primitive_count;
375 };
376
377
378 /*******************************************************************************
379 * Draw common initialization code
380 */
381 boolean draw_init(struct draw_context *draw);
382 void draw_new_instance(struct draw_context *draw);
383
384 /*******************************************************************************
385 * Vertex shader code:
386 */
387 boolean draw_vs_init( struct draw_context *draw );
388 void draw_vs_destroy( struct draw_context *draw );
389
390
391 /*******************************************************************************
392 * Geometry shading code:
393 */
394 boolean draw_gs_init( struct draw_context *draw );
395
396
397 void draw_gs_destroy( struct draw_context *draw );
398
399 /*******************************************************************************
400 * Common shading code:
401 */
402 uint draw_current_shader_outputs(const struct draw_context *draw);
403 uint draw_current_shader_position_output(const struct draw_context *draw);
404 uint draw_current_shader_viewport_index_output(const struct draw_context *draw);
405 uint draw_current_shader_clipvertex_output(const struct draw_context *draw);
406 uint draw_current_shader_clipdistance_output(const struct draw_context *draw, int index);
407 uint draw_current_shader_num_written_clipdistances(const struct draw_context *draw);
408 uint draw_current_shader_culldistance_output(const struct draw_context *draw, int index);
409 uint draw_current_shader_num_written_culldistances(const struct draw_context *draw);
410 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
411 uint semantic_name, uint semantic_index);
412 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
413 boolean draw_current_shader_uses_viewport_index(
414 const struct draw_context *draw);
415
416
417 /*******************************************************************************
418 * Vertex processing (was passthrough) code:
419 */
420 boolean draw_pt_init( struct draw_context *draw );
421 void draw_pt_destroy( struct draw_context *draw );
422 void draw_pt_reset_vertex_ids( struct draw_context *draw );
423 void draw_pt_flush( struct draw_context *draw, unsigned flags );
424
425
426 /*******************************************************************************
427 * Primitive processing (pipeline) code:
428 */
429
430 boolean draw_pipeline_init( struct draw_context *draw );
431 void draw_pipeline_destroy( struct draw_context *draw );
432
433
434
435
436
437 /*
438 * These flags are used by the pipeline when unfilled and/or line stipple modes
439 * are operational.
440 */
441 #define DRAW_PIPE_EDGE_FLAG_0 0x1
442 #define DRAW_PIPE_EDGE_FLAG_1 0x2
443 #define DRAW_PIPE_EDGE_FLAG_2 0x4
444 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
445 #define DRAW_PIPE_RESET_STIPPLE 0x8
446
447 void draw_pipeline_run( struct draw_context *draw,
448 const struct draw_vertex_info *vert,
449 const struct draw_prim_info *prim);
450
451 void draw_pipeline_run_linear( struct draw_context *draw,
452 const struct draw_vertex_info *vert,
453 const struct draw_prim_info *prim);
454
455
456
457
458 void draw_pipeline_flush( struct draw_context *draw,
459 unsigned flags );
460
461
462
463 /*******************************************************************************
464 * Flushing
465 */
466
467 #define DRAW_FLUSH_PARAMETER_CHANGE 0x1 /**< Constants, viewport, etc */
468 #define DRAW_FLUSH_STATE_CHANGE 0x2 /**< Other/heavy state changes */
469 #define DRAW_FLUSH_BACKEND 0x4 /**< Flush the output buffer */
470
471
472 void draw_do_flush( struct draw_context *draw, unsigned flags );
473
474
475
476 void *
477 draw_get_rasterizer_no_cull( struct draw_context *draw,
478 boolean scissor,
479 boolean flatshade );
480
481 void
482 draw_stats_clipper_primitives(struct draw_context *draw,
483 const struct draw_prim_info *prim_info);
484
485 void draw_update_clip_flags(struct draw_context *draw);
486 void draw_update_viewport_flags(struct draw_context *draw);
487
488 /**
489 * Return index i from the index buffer.
490 * If the index buffer would overflow we return the
491 * maximum possible index.
492 */
493 #define DRAW_GET_IDX(_elts, _i) \
494 (((_i) >= draw->pt.user.eltMax) ? DRAW_MAX_FETCH_IDX : (_elts)[_i])
495
496 /**
497 * Return index of the given viewport clamping it
498 * to be between 0 <= and < PIPE_MAX_VIEWPORTS
499 */
500 static inline unsigned
501 draw_clamp_viewport_idx(int idx)
502 {
503 return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0);
504 }
505
506 /**
507 * Adds two unsigned integers and if the addition
508 * overflows then it returns the value from
509 * the overflow_value variable.
510 */
511 static inline unsigned
512 draw_overflow_uadd(unsigned a, unsigned b,
513 unsigned overflow_value)
514 {
515 unsigned res = a + b;
516 if (res < a || res < b) {
517 res = overflow_value;
518 }
519 return res;
520 }
521
522 #endif /* DRAW_PRIVATE_H */