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;
145       unsigned eltSize; /* saved eltSize for flushing */
146 
147       struct {
148          struct draw_pt_middle_end *fetch_emit;
149          struct draw_pt_middle_end *fetch_shade_emit;
150          struct draw_pt_middle_end *general;
151          struct draw_pt_middle_end *llvm;
152       } middle;
153 
154       struct {
155          struct draw_pt_front_end *vsplit;
156       } front;
157 
158       struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
159       unsigned nr_vertex_buffers;
160 
161       /*
162        * This is the largest legal index value for the current set of
163        * bound vertex buffers.  Regardless of any other consideration,
164        * all vertex lookups need to be clamped to 0..max_index to
165        * prevent out-of-bound access.
166        */
167       unsigned max_index;
168 
169       struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
170       unsigned nr_vertex_elements;
171 
172       /* user-space vertex data, buffers */
173       struct {
174          /** vertex element/index buffer (ex: glDrawElements) */
175          const void *elts;
176          /** bytes per index (0, 1, 2 or 4) */
177          unsigned eltSizeIB;
178          unsigned eltSize;
179          int eltBias;
180          unsigned min_index;
181          unsigned max_index;
182 
183          /** vertex arrays */
184          const void *vbuffer[PIPE_MAX_ATTRIBS];
185 
186          /** constant buffers (for vertex/geometry shader) */
187          const void *vs_constants[PIPE_MAX_CONSTANT_BUFFERS];
188          unsigned vs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
189          const void *gs_constants[PIPE_MAX_CONSTANT_BUFFERS];
190          unsigned gs_constants_size[PIPE_MAX_CONSTANT_BUFFERS];
191 
192          /* pointer to planes */
193          float (*planes)[DRAW_TOTAL_CLIP_PLANES][4];
194       } user;
195 
196       boolean test_fse;         /* enable FSE even though its not correct (eg for softpipe) */
197       boolean no_fse;           /* disable FSE even when it is correct */
198    } pt;
199 
200    struct {
201       boolean bypass_clip_xy;
202       boolean bypass_clip_z;
203       boolean guard_band_xy;
204    } driver;
205 
206    boolean quads_always_flatshade_last;
207 
208    boolean flushing;         /**< debugging/sanity */
209    boolean suspend_flushing; /**< internally set */
210 
211    /* Flags set if API requires clipping in these planes and the
212     * driver doesn't indicate that it can do it for us.
213     */
214    boolean clip_xy;
215    boolean clip_z;
216    boolean clip_user;
217    boolean guard_band_xy;
218 
219    boolean force_passthrough; /**< never clip or shade */
220 
221    boolean dump_vs;
222 
223    double mrd;  /**< minimum resolvable depth value, for polygon offset */
224 
225    /** Current rasterizer state given to us by the driver */
226    const struct pipe_rasterizer_state *rasterizer;
227    /** Driver CSO handle for the current rasterizer state */
228    void *rast_handle;
229 
230    /** Rasterizer CSOs without culling/stipple/etc */
231    void *rasterizer_no_cull[2][2];
232 
233    struct pipe_viewport_state viewport;
234    boolean identity_viewport;
235 
236    /** Vertex shader state */
237    struct {
238       struct draw_vertex_shader *vertex_shader;
239       uint num_vs_outputs;  /**< convenience, from vertex_shader */
240       uint position_output;
241       uint edgeflag_output;
242       uint clipvertex_output;
243       uint clipdistance_output[2];
244 
245       /** Fields for TGSI interpreter / execution */
246       struct {
247          struct tgsi_exec_machine *machine;
248 
249          struct tgsi_sampler **samplers;
250          uint num_samplers;
251       } tgsi;
252 
253       const void *aligned_constants[PIPE_MAX_CONSTANT_BUFFERS];
254 
255       const void *aligned_constant_storage[PIPE_MAX_CONSTANT_BUFFERS];
256       unsigned const_storage_size[PIPE_MAX_CONSTANT_BUFFERS];
257 
258 
259       struct translate *fetch;
260       struct translate_cache *fetch_cache;
261       struct translate *emit;
262       struct translate_cache *emit_cache;
263    } vs;
264 
265    /** Geometry shader state */
266    struct {
267       struct draw_geometry_shader *geometry_shader;
268       uint num_gs_outputs;  /**< convenience, from geometry_shader */
269       uint position_output;
270 
271       /** Fields for TGSI interpreter / execution */
272       struct {
273          struct tgsi_exec_machine *machine;
274 
275          struct tgsi_sampler **samplers;
276          uint num_samplers;
277       } tgsi;
278 
279    } gs;
280 
281    /** Fragment shader state */
282    struct {
283       struct draw_fragment_shader *fragment_shader;
284    } fs;
285 
286    /** Stream output (vertex feedback) state */
287    struct {
288       struct pipe_stream_output_info state;
289       struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS];
290       uint num_targets;
291    } so;
292 
293    /* Clip derived state:
294     */
295    float plane[DRAW_TOTAL_CLIP_PLANES][4];
296 
297    /* If a prim stage introduces new vertex attributes, they'll be stored here
298     */
299    struct {
300       uint num;
301       uint semantic_name[10];
302       uint semantic_index[10];
303       uint slot[10];
304    } extra_shader_outputs;
305 
306    unsigned instance_id;
307 
308 #ifdef HAVE_LLVM
309    struct draw_llvm *llvm;
310 #endif
311 
312    /** Texture sampler and sampler view state.
313     * Note that we have arrays indexed by shader type.  At this time
314     * we only handle vertex and geometry shaders in the draw module, but
315     * there may be more in the future (ex: hull and tessellation).
316     */
317    struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
318    unsigned num_sampler_views[PIPE_SHADER_TYPES];
319    const struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
320    unsigned num_samplers[PIPE_SHADER_TYPES];
321 
322    void *driver_private;
323 };
324 
325 
326 struct draw_fetch_info {
327    boolean linear;
328    unsigned start;
329    const unsigned *elts;
330    unsigned count;
331 };
332 
333 struct draw_vertex_info {
334    struct vertex_header *verts;
335    unsigned vertex_size;
336    unsigned stride;
337    unsigned count;
338 };
339 
340 /* these flags are set if the primitive is a segment of a larger one */
341 #define DRAW_SPLIT_BEFORE 0x1
342 #define DRAW_SPLIT_AFTER  0x2
343 
344 struct draw_prim_info {
345    boolean linear;
346    unsigned start;
347 
348    const ushort *elts;
349    unsigned count;
350 
351    unsigned prim;
352    unsigned flags;
353    unsigned *primitive_lengths;
354    unsigned primitive_count;
355 };
356 
357 
358 /*******************************************************************************
359  * Draw common initialization code
360  */
361 boolean draw_init(struct draw_context *draw);
362 
363 /*******************************************************************************
364  * Vertex shader code:
365  */
366 boolean draw_vs_init( struct draw_context *draw );
367 void draw_vs_destroy( struct draw_context *draw );
368 
369 void draw_vs_set_viewport( struct draw_context *,
370                            const struct pipe_viewport_state * );
371 
372 void
373 draw_vs_set_constants(struct draw_context *,
374                       unsigned slot,
375                       const void *constants,
376                       unsigned size);
377 
378 
379 
380 /*******************************************************************************
381  * Geometry shading code:
382  */
383 boolean draw_gs_init( struct draw_context *draw );
384 
385 void
386 draw_gs_set_constants(struct draw_context *,
387                       unsigned slot,
388                       const void *constants,
389                       unsigned size);
390 
391 void draw_gs_destroy( struct draw_context *draw );
392 
393 /*******************************************************************************
394  * Common shading code:
395  */
396 uint draw_current_shader_outputs(const struct draw_context *draw);
397 uint draw_current_shader_position_output(const struct draw_context *draw);
398 uint draw_current_shader_clipvertex_output(const struct draw_context *draw);
399 uint draw_current_shader_clipdistance_output(const struct draw_context *draw, int index);
400 int draw_alloc_extra_vertex_attrib(struct draw_context *draw,
401                                    uint semantic_name, uint semantic_index);
402 void draw_remove_extra_vertex_attribs(struct draw_context *draw);
403 
404 
405 /*******************************************************************************
406  * Vertex processing (was passthrough) code:
407  */
408 boolean draw_pt_init( struct draw_context *draw );
409 void draw_pt_destroy( struct draw_context *draw );
410 void draw_pt_reset_vertex_ids( struct draw_context *draw );
411 void draw_pt_flush( struct draw_context *draw, unsigned flags );
412 
413 
414 /*******************************************************************************
415  * Primitive processing (pipeline) code:
416  */
417 
418 boolean draw_pipeline_init( struct draw_context *draw );
419 void draw_pipeline_destroy( struct draw_context *draw );
420 
421 
422 
423 
424 
425 /*
426  * These flags are used by the pipeline when unfilled and/or line stipple modes
427  * are operational.
428  */
429 #define DRAW_PIPE_EDGE_FLAG_0   0x1
430 #define DRAW_PIPE_EDGE_FLAG_1   0x2
431 #define DRAW_PIPE_EDGE_FLAG_2   0x4
432 #define DRAW_PIPE_EDGE_FLAG_ALL 0x7
433 #define DRAW_PIPE_RESET_STIPPLE 0x8
434 
435 void draw_pipeline_run( struct draw_context *draw,
436                         const struct draw_vertex_info *vert,
437                         const struct draw_prim_info *prim);
438 
439 void draw_pipeline_run_linear( struct draw_context *draw,
440                                const struct draw_vertex_info *vert,
441                                const struct draw_prim_info *prim);
442 
443 
444 
445 
446 void draw_pipeline_flush( struct draw_context *draw,
447                           unsigned flags );
448 
449 
450 
451 /*******************************************************************************
452  * Flushing
453  */
454 
455 #define DRAW_FLUSH_STATE_CHANGE              0x8
456 #define DRAW_FLUSH_BACKEND                   0x10
457 
458 
459 void draw_do_flush( struct draw_context *draw, unsigned flags );
460 
461 
462 
463 void *
464 draw_get_rasterizer_no_cull( struct draw_context *draw,
465                              boolean scissor,
466                              boolean flatshade );
467 
468 
469 #endif /* DRAW_PRIVATE_H */
470