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