xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_shader.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #ifndef SVGA_SHADER_H
9 #define SVGA_SHADER_H
10 
11 #include "svga3d_reg.h"
12 #include "svga_context.h"
13 #include "svga_streamout.h"
14 #include "compiler/shader_enums.h"
15 #include "tgsi/tgsi_scan.h"
16 
17 
18 /**
19  * We use a 64-bit mask to keep track of the generic indexes.
20  * This is the maximum semantic index for a TGSI GENERIC[i] register.
21  */
22 #define MAX_GENERIC_VARYING 64
23 
24 
25 struct svga_context;
26 
27 
28 struct svga_compile_key
29 {
30    /* vertex shader only */
31    struct {
32       uint64_t fs_generic_inputs;
33       unsigned passthrough:1;
34       unsigned need_prescale:1;
35       unsigned undo_viewport:1;
36       unsigned allow_psiz:1;
37       unsigned need_vertex_id_bias:1;
38 
39       /** The following are all 32-bit bitmasks (per VS input) */
40       unsigned adjust_attrib_range;
41       unsigned attrib_is_pure_int;
42       unsigned adjust_attrib_w_1;
43       unsigned adjust_attrib_itof;
44       unsigned adjust_attrib_utof;
45       unsigned attrib_is_bgra;
46       unsigned attrib_puint_to_snorm;
47       unsigned attrib_puint_to_uscaled;
48       unsigned attrib_puint_to_sscaled;
49    } vs;
50 
51    /* geometry shader only */
52    struct {
53       uint64_t vs_generic_outputs;
54       unsigned need_prescale:1;
55       unsigned writes_psize:1;
56       unsigned wide_point:1;
57       unsigned writes_viewport_index:1;
58       unsigned num_prescale:5;
59    } gs;
60 
61    /* fragment shader only */
62    struct {
63       uint64_t vs_generic_outputs;
64       uint64_t gs_generic_outputs;
65       unsigned light_twoside:1;
66       unsigned front_ccw:1;
67       unsigned white_fragments:1;
68       unsigned alpha_to_one:1;
69       unsigned flatshade:1;
70       unsigned pstipple:1;
71       unsigned alpha_func:4;  /**< SVGA3D_CMP_x */
72       unsigned write_color0_to_n_cbufs:4;
73       unsigned aa_point:1;
74       unsigned layer_to_zero:1;
75       int aa_point_coord_index;
76       float alpha_ref;
77    } fs;
78 
79    /* tessellation control shader */
80    struct {
81       unsigned vertices_per_patch:8;
82       unsigned vertices_out:8;
83       enum mesa_prim prim_mode:8;
84       enum pipe_tess_spacing spacing:3;
85       unsigned vertices_order_cw:1;
86       unsigned point_mode:1;
87       unsigned passthrough:1;
88    } tcs;
89 
90    /* tessellation evaluation shader */
91    struct {
92       unsigned vertices_per_patch:8;
93       unsigned tessfactor_index:8;
94       unsigned need_prescale:1;
95       unsigned need_tessouter:1;
96       unsigned need_tessinner:1;
97    } tes;
98 
99    /* compute shader */
100    struct {
101       unsigned grid_size[3];
102       unsigned mem_size;
103    } cs;
104 
105    /* any shader type */
106    int8_t generic_remap_table[MAX_GENERIC_VARYING];
107    unsigned num_textures:8;
108    unsigned num_samplers:8;
109    unsigned num_unnormalized_coords:8;
110    unsigned clip_plane_enable:PIPE_MAX_CLIP_PLANES;
111    unsigned last_vertex_stage:1;
112    unsigned clamp_vertex_color:1;
113    unsigned sampler_state_mapping:1;    /* Set if use sampler state mapping */
114    unsigned sprite_origin_lower_left:1;
115    uint16_t sprite_coord_enable;
116    struct {
117       unsigned compare_mode:1;
118       unsigned compare_func:3;
119       unsigned compare_in_shader:1;
120       unsigned unnormalized:1;
121       unsigned texel_bias:1;
122       unsigned width_height_idx:5; /**< texture unit */
123       unsigned is_array:1;
124       unsigned swizzle_r:3;
125       unsigned swizzle_g:3;
126       unsigned swizzle_b:3;
127       unsigned swizzle_a:3;
128       unsigned num_samples:5;   /**< Up to 16 samples */
129       unsigned target:4;
130       unsigned sampler_return_type:4;
131       unsigned sampler_view:1;
132       unsigned sampler_index:5;
133    } tex[PIPE_MAX_SAMPLERS];
134 
135    unsigned uav_splice_index:4;      /* starting uav index */
136    unsigned srv_raw_constbuf_index:8;   /* start index for srv raw buffers */
137    unsigned srv_raw_shaderbuf_index:8;  /* start index for srv raw shader bufs */
138    unsigned image_size_used:1;
139 
140    uint16_t raw_constbufs;           /* bitmask of raw constant buffers */
141    uint64_t raw_shaderbufs;          /* bitmask of raw shader buffers */
142 
143    struct {
144       enum tgsi_return_type return_type;
145       enum pipe_texture_target resource_target;
146       unsigned is_array:1;
147       unsigned is_single_layer:1;
148       unsigned uav_index;
149    } images[PIPE_MAX_SHADER_IMAGES];
150 
151    uint32_t shader_buf_uav_index[PIPE_MAX_SHADER_BUFFERS];
152    uint32_t atomic_buf_uav_index[PIPE_MAX_HW_ATOMIC_BUFFERS];
153 };
154 
155 /* A key for a variant of token string of a shader */
156 struct svga_token_key {
157    struct {
158       unsigned sprite_coord_enable:24;
159       unsigned sprite_origin_upper_left:1;
160       unsigned point_pos_stream_out:1;
161       unsigned writes_psize:1;
162       unsigned aa_point:1;
163    } gs;
164    struct {
165       unsigned write_position:1;
166    } vs;
167    unsigned dynamic_indexing:1;
168 };
169 
170 /**
171  * A single TGSI shader may be compiled into different variants of
172  * SVGA3D shaders depending on the compile key.  Each user shader
173  * will have a linked list of these variants.
174  */
175 struct svga_shader_variant
176 {
177    const struct svga_shader *shader;
178 
179    /** Parameters used to generate this variant */
180    struct svga_compile_key key;
181 
182    /* svga shader type */
183    SVGA3dShaderType type;
184 
185    /* Compiled shader tokens:
186     */
187    const unsigned *tokens;
188    unsigned nr_tokens;
189 
190    /* shader signature */
191    unsigned signatureLen;
192    SVGA3dDXShaderSignatureHeader *signature;
193 
194    /** Per-context shader identifier used with SVGA_3D_CMD_SHADER_DEFINE,
195     * SVGA_3D_CMD_SET_SHADER and SVGA_3D_CMD_SHADER_DESTROY.
196     */
197    unsigned id;
198 
199    /** Start of extra constants (number of float[4] constants) */
200    unsigned extra_const_start;
201 
202    /* GB object buffer containing the bytecode */
203    struct svga_winsys_gb_shader *gb_shader;
204 
205    /** Next variant */
206    struct svga_shader_variant *next;
207 };
208 
209 
210 /**
211  * Shader variant for fragment shader
212  */
213 struct svga_fs_variant
214 {
215    struct svga_shader_variant base;
216 
217    bool uses_flat_interp;   /** TRUE if flat interpolation qualifier is
218                                 *  applied to any of the varyings.
219                                 */
220 
221    /** Is the color output just a constant value? (fragment shader only) */
222    bool constant_color_output;
223 
224    /** Bitmask indicating which texture units are doing the shadow
225     * comparison test in the shader rather than the sampler state.
226     */
227    unsigned fs_shadow_compare_units;
228 
229    /** For FS-based polygon stipple */
230    unsigned pstipple_sampler_unit:8;
231    unsigned pstipple_sampler_state_index:8;
232 };
233 
234 
235 /**
236  * Shader variant for geometry shader
237  */
238 struct svga_gs_variant
239 {
240    struct svga_shader_variant base;
241 };
242 
243 
244 /**
245  * Shader variant for vertex shader
246  */
247 struct svga_vs_variant
248 {
249    struct svga_shader_variant base;
250 };
251 
252 
253 /**
254  * Shader variant for tessellation evaluation shader
255  */
256 struct svga_tes_variant
257 {
258    struct svga_shader_variant base;
259 
260    enum mesa_prim prim_mode:8;
261    enum pipe_tess_spacing spacing:3;
262    unsigned vertices_order_cw:1;
263    unsigned point_mode:1;
264 };
265 
266 
267 /**
268  * Shader variant for tessellation control shader
269  */
270 struct svga_tcs_variant
271 {
272    struct svga_shader_variant base;
273 };
274 
275 
276 /**
277  * Shader variant for compute shader
278  */
279 struct svga_cs_variant
280 {
281    struct svga_shader_variant base;
282 };
283 
284 
285 struct svga_shader_info
286 {
287    uint8_t num_inputs;
288    uint8_t num_outputs;
289 
290    uint8_t input_semantic_name[PIPE_MAX_SHADER_INPUTS];
291    uint8_t input_semantic_index[PIPE_MAX_SHADER_INPUTS];
292    uint8_t input_usage_mask[PIPE_MAX_SHADER_INPUTS];
293    uint8_t output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
294    uint8_t output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
295    uint8_t output_usage_mask[PIPE_MAX_SHADER_OUTPUTS];
296 
297    uint64_t generic_inputs_mask;
298    uint64_t generic_outputs_mask;
299 
300    bool writes_edgeflag;
301    bool writes_layer;
302    bool writes_position;
303    bool writes_psize;
304    bool writes_viewport_index;
305 
306    bool uses_grid_size;
307    bool uses_const_buffers;
308    bool uses_hw_atomic;
309    bool uses_images;
310    bool uses_image_size;
311    bool uses_shader_buffers;
312    bool uses_samplers;
313 
314    unsigned const_buffers_declared;  /* bitmask of declared const buffers */
315    unsigned constbuf0_num_uniforms;  /* number of uniforms in constbuf0 */
316 
317    unsigned shader_buffers_declared;  /* bitmask of declared shader buffers */
318 
319    struct {
320       bool color0_writes_all_cbufs;
321    } fs;
322 
323   struct {
324       enum mesa_prim in_prim;
325       enum mesa_prim out_prim;
326    } gs;
327 
328    struct {
329       unsigned vertices_out;        /* number of vertices in tcs patch */
330       bool writes_tess_factor;
331    } tcs;
332 
333    struct {
334       enum mesa_prim prim_mode;
335       bool reads_control_point;
336    } tes;
337 };
338 
339 
340 struct svga_shader
341 {
342    enum pipe_shader_ir type;            /* IR type */
343    enum pipe_shader_type stage;         /* shader stage */
344 
345    struct svga_shader_info info;        /* shader info */
346 
347    /* TGSI */
348    const struct tgsi_token *tokens;
349    struct svga_token_key token_key;     /* token key for the token string */
350    struct tgsi_shader_info tgsi_info;
351 
352    /* List of shaders with tokens derived from the same token string */
353    struct svga_shader *next;
354    struct svga_shader *parent;   /* shader with the original token string */
355 
356    struct svga_stream_output *stream_output;
357 
358    /** Head of linked list of compiled variants */
359    struct svga_shader_variant *variants;
360 
361    /* Get dummy shader variant */
362    struct svga_shader_variant *(*get_dummy_shader)(struct svga_context *,
363                                                    struct svga_shader *,
364                                                    const struct svga_compile_key *);
365 
366    unsigned id;  /**< for debugging only */
367 };
368 
369 
370 struct svga_fragment_shader
371 {
372    struct svga_shader base;
373 
374    struct draw_fragment_shader *draw_shader;
375 
376    /** Mask of which generic varying variables are read by this shader */
377    uint64_t generic_inputs;
378 
379    /** Table mapping original TGSI generic indexes to low integers */
380    int8_t generic_remap_table[MAX_GENERIC_VARYING];
381 };
382 
383 
384 struct svga_vertex_shader
385 {
386    struct svga_shader base;
387 
388    struct draw_vertex_shader *draw_shader;
389 
390    /** Mask of which generic varying variables are written by this shader */
391    uint64_t generic_outputs;
392 
393    /** Generated geometry shader that goes with this vertex shader */
394    struct svga_geometry_shader *gs;
395 };
396 
397 
398 struct svga_geometry_shader
399 {
400    struct svga_shader base;
401 
402    struct draw_geometry_shader *draw_shader;
403 
404    /** Table mapping original TGSI generic indexes to low integers */
405    int8_t generic_remap_table[MAX_GENERIC_VARYING];
406    uint64_t generic_outputs;
407 
408    unsigned aa_point_coord_index; /* generic index for aa point coord */
409 
410    unsigned wide_point:1;      /* set if the shader emulates wide point */
411 };
412 
413 
414 struct svga_tcs_shader
415 {
416    struct svga_shader base;
417 
418    /** Mask of which generic varying variables are written by this shader */
419    uint64_t generic_outputs;
420 };
421 
422 
423 struct svga_tes_shader
424 {
425    struct svga_shader base;
426 
427    /** Mask of which generic varying variables are written by this shader */
428    uint64_t generic_inputs;
429 };
430 
431 
432 struct svga_compute_shader
433 {
434    struct svga_shader base;
435    unsigned shared_mem_size;
436 };
437 
438 
439 static inline bool
svga_compile_keys_equal(const struct svga_compile_key * a,const struct svga_compile_key * b)440 svga_compile_keys_equal(const struct svga_compile_key *a,
441                         const struct svga_compile_key *b)
442 {
443    unsigned key_size = sizeof(*a);
444 
445    return memcmp(a, b, key_size) == 0;
446 }
447 
448 
449 uint64_t
450 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);
451 
452 uint64_t
453 svga_get_generic_outputs_mask(const struct tgsi_shader_info *info);
454 
455 void
456 svga_remap_generics(uint64_t generics_mask,
457                     int8_t remap_table[MAX_GENERIC_VARYING]);
458 
459 int
460 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
461                          int generic_index);
462 
463 void
464 svga_init_shader_key_common(const struct svga_context *svga,
465                             enum pipe_shader_type shader_type,
466                             const struct svga_shader *shader,
467                             struct svga_compile_key *key);
468 
469 struct svga_shader_variant *
470 svga_search_shader_key(const struct svga_shader *shader,
471                        const struct svga_compile_key *key);
472 
473 struct svga_shader *
474 svga_search_shader_token_key(struct svga_shader *shader,
475                              const struct svga_token_key *key);
476 
477 struct svga_shader *
478 svga_create_shader(struct pipe_context *pipe,
479                    const struct pipe_shader_state *templ,
480                    enum pipe_shader_type stage,
481                    unsigned len);
482 
483 enum pipe_error
484 svga_compile_shader(struct svga_context *svga,
485                     struct svga_shader *shader,
486                     const struct svga_compile_key *key,
487                     struct svga_shader_variant **out_variant);
488 
489 enum pipe_error
490 svga_define_shader(struct svga_context *svga,
491                    struct svga_shader_variant *variant);
492 
493 enum pipe_error
494 svga_set_shader(struct svga_context *svga,
495                 SVGA3dShaderType type,
496                 struct svga_shader_variant *variant);
497 
498 struct svga_shader_variant *
499 svga_new_shader_variant(struct svga_context *svga, enum pipe_shader_type type);
500 
501 void
502 svga_destroy_shader_variant(struct svga_context *svga,
503                             struct svga_shader_variant *variant);
504 
505 enum pipe_error
506 svga_rebind_shaders(struct svga_context *svga);
507 
508 /**
509  * Check if a shader's bytecode exceeds the device limits.
510  */
511 static inline bool
svga_shader_too_large(const struct svga_context * svga,const struct svga_shader_variant * variant)512 svga_shader_too_large(const struct svga_context *svga,
513                       const struct svga_shader_variant *variant)
514 {
515    if (svga_have_gb_objects(svga)) {
516       return false;
517    }
518 
519    if (variant->nr_tokens * sizeof(variant->tokens[0])
520        + sizeof(SVGA3dCmdDefineShader) + sizeof(SVGA3dCmdHeader)
521        < SVGA_CB_MAX_COMMAND_SIZE) {
522       return false;
523    }
524 
525    return true;
526 }
527 
528 
529 /**
530  * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
531  */
532 static inline SVGA3dShaderType
svga_shader_type(enum pipe_shader_type shader)533 svga_shader_type(enum pipe_shader_type shader)
534 {
535    switch (shader) {
536    case PIPE_SHADER_VERTEX:
537       return SVGA3D_SHADERTYPE_VS;
538    case PIPE_SHADER_GEOMETRY:
539       return SVGA3D_SHADERTYPE_GS;
540    case PIPE_SHADER_FRAGMENT:
541       return SVGA3D_SHADERTYPE_PS;
542    case PIPE_SHADER_TESS_CTRL:
543       return SVGA3D_SHADERTYPE_HS;
544    case PIPE_SHADER_TESS_EVAL:
545       return SVGA3D_SHADERTYPE_DS;
546    case PIPE_SHADER_COMPUTE:
547       return SVGA3D_SHADERTYPE_CS;
548    default:
549       assert(!"Invalid shader type");
550       return SVGA3D_SHADERTYPE_VS;
551    }
552 }
553 
554 
555 /** Does the current VS have stream output? */
556 static inline bool
svga_have_vs_streamout(const struct svga_context * svga)557 svga_have_vs_streamout(const struct svga_context *svga)
558 {
559    return svga->curr.vs != NULL && svga->curr.vs->base.stream_output != NULL;
560 }
561 
562 
563 /** Does the current GS have stream output? */
564 static inline bool
svga_have_gs_streamout(const struct svga_context * svga)565 svga_have_gs_streamout(const struct svga_context *svga)
566 {
567    return svga->curr.gs != NULL && svga->curr.gs->base.stream_output != NULL;
568 }
569 
570 
571 static inline struct svga_fs_variant *
svga_fs_variant(struct svga_shader_variant * variant)572 svga_fs_variant(struct svga_shader_variant *variant)
573 {
574    assert(!variant || variant->type == SVGA3D_SHADERTYPE_PS);
575    return (struct svga_fs_variant *)variant;
576 }
577 
578 
579 static inline struct svga_tes_variant *
svga_tes_variant(struct svga_shader_variant * variant)580 svga_tes_variant(struct svga_shader_variant *variant)
581 {
582    assert(!variant || variant->type == SVGA3D_SHADERTYPE_DS);
583    return (struct svga_tes_variant *)variant;
584 }
585 
586 
587 static inline struct svga_cs_variant *
svga_cs_variant(struct svga_shader_variant * variant)588 svga_cs_variant(struct svga_shader_variant *variant)
589 {
590    assert(!variant || variant->type == SVGA3D_SHADERTYPE_CS);
591    return (struct svga_cs_variant *)variant;
592 }
593 
594 
595 /* Returns TRUE if we are currently using flat shading.
596  */
597 static inline bool
svga_is_using_flat_shading(const struct svga_context * svga)598 svga_is_using_flat_shading(const struct svga_context *svga)
599 {
600    return
601       svga->state.hw_draw.fs ?
602          svga_fs_variant(svga->state.hw_draw.fs)->uses_flat_interp : false;
603 }
604 
605 struct svga_shader_variant *
606 svga_get_compiled_dummy_vertex_shader(struct svga_context *svga,
607                                       struct svga_shader *shader,
608                                       const struct svga_compile_key *key);
609 
610 
611 struct svga_shader_variant *
612 svga_get_compiled_dummy_fragment_shader(struct svga_context *svga,
613                                         struct svga_shader *shader,
614                                         const struct svga_compile_key *key);
615 
616 struct svga_shader_variant *
617 svga_get_compiled_dummy_geometry_shader(struct svga_context *svga,
618                                         struct svga_shader *shader,
619                                         const struct svga_compile_key *key);
620 
621 static inline bool
svga_shader_use_samplers(struct svga_shader * shader)622 svga_shader_use_samplers(struct svga_shader *shader)
623 {
624    return shader ? (shader->info.uses_samplers != 0) : false;
625 }
626 
627 #endif /* SVGA_SHADER_H */
628