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