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