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