• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**********************************************************
2  * Copyright 2008-2012 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 
33 
34 /**
35  * We use a 64-bit mask to keep track of the generic indexes.
36  * This is the maximum semantic index for a TGSI GENERIC[i] register.
37  */
38 #define MAX_GENERIC_VARYING 64
39 
40 
41 struct svga_context;
42 
43 
44 struct svga_compile_key
45 {
46    /* vertex shader only */
47    struct {
48       uint64_t fs_generic_inputs;
49       unsigned passthrough:1;
50       unsigned need_prescale:1;
51       unsigned undo_viewport:1;
52       unsigned allow_psiz:1;
53       unsigned need_vertex_id_bias:1;
54 
55       /** The following are all 32-bit bitmasks (per VS input) */
56       unsigned adjust_attrib_range;
57       unsigned attrib_is_pure_int;
58       unsigned adjust_attrib_w_1;
59       unsigned adjust_attrib_itof;
60       unsigned adjust_attrib_utof;
61       unsigned attrib_is_bgra;
62       unsigned attrib_puint_to_snorm;
63       unsigned attrib_puint_to_uscaled;
64       unsigned attrib_puint_to_sscaled;
65    } vs;
66 
67    /* geometry shader only */
68    struct {
69       uint64_t vs_generic_outputs;
70       unsigned need_prescale:1;
71       unsigned writes_psize:1;
72       unsigned wide_point:1;
73       unsigned writes_viewport_index:1;
74       unsigned num_prescale:5;
75    } gs;
76 
77    /* fragment shader only */
78    struct {
79       uint64_t vs_generic_outputs;
80       uint64_t gs_generic_outputs;
81       unsigned light_twoside:1;
82       unsigned front_ccw:1;
83       unsigned white_fragments:1;
84       unsigned alpha_to_one:1;
85       unsigned flatshade:1;
86       unsigned pstipple:1;
87       unsigned alpha_func:4;  /**< SVGA3D_CMP_x */
88       unsigned write_color0_to_n_cbufs:4;
89       unsigned aa_point:1;
90       unsigned layer_to_zero:1;
91       int aa_point_coord_index;
92       float alpha_ref;
93    } fs;
94 
95    /* tessellation control shader */
96    struct {
97       unsigned vertices_per_patch:8;
98       unsigned vertices_out:8;
99       enum pipe_prim_type prim_mode:8;
100       enum pipe_tess_spacing spacing:3;
101       unsigned vertices_order_cw:1;
102       unsigned point_mode:1;
103       unsigned passthrough:1;
104    } tcs;
105 
106    /* tessellation evaluation shader */
107    struct {
108       unsigned vertices_per_patch:8;
109       unsigned tessfactor_index:8;
110       unsigned need_prescale:1;
111       unsigned need_tessouter:1;
112       unsigned need_tessinner:1;
113    } tes;
114 
115    /* compute shader */
116    struct {
117       unsigned grid_size[3];
118    } cs;
119 
120    /* any shader type */
121    int8_t generic_remap_table[MAX_GENERIC_VARYING];
122    unsigned num_textures:8;
123    unsigned num_unnormalized_coords:8;
124    unsigned clip_plane_enable:PIPE_MAX_CLIP_PLANES;
125    unsigned last_vertex_stage:1;
126    unsigned clamp_vertex_color:1;
127    unsigned sprite_origin_lower_left:1;
128    uint16_t sprite_coord_enable;
129    struct {
130       unsigned compare_mode:1;
131       unsigned compare_func:3;
132       unsigned unnormalized:1;
133       unsigned texel_bias:1;
134       unsigned width_height_idx:5; /**< texture unit */
135       unsigned is_array:1;
136       unsigned swizzle_r:3;
137       unsigned swizzle_g:3;
138       unsigned swizzle_b:3;
139       unsigned swizzle_a:3;
140       unsigned num_samples:5;   /**< Up to 16 samples */
141    } tex[PIPE_MAX_SAMPLERS];
142    /* Note: svga_compile_keys_equal() depends on the variable-size
143     * tex[] array being at the end of this structure.
144     */
145 };
146 
147 /* A key for a variant of token string of a shader */
148 struct svga_token_key {
149    struct {
150       unsigned sprite_coord_enable:24;
151       unsigned sprite_origin_upper_left:1;
152       unsigned point_pos_stream_out:1;
153       unsigned writes_psize:1;
154       unsigned aa_point:1;
155    } gs;
156    struct {
157       unsigned write_position:1;
158    } vs;
159    unsigned dynamic_indexing:1;
160 };
161 
162 /**
163  * A single TGSI shader may be compiled into different variants of
164  * SVGA3D shaders depending on the compile key.  Each user shader
165  * will have a linked list of these variants.
166  */
167 struct svga_shader_variant
168 {
169    const struct svga_shader *shader;
170 
171    /** Parameters used to generate this variant */
172    struct svga_compile_key key;
173 
174    /* svga shader type */
175    SVGA3dShaderType type;
176 
177    /* Compiled shader tokens:
178     */
179    const unsigned *tokens;
180    unsigned nr_tokens;
181 
182    /* shader signature */
183    unsigned signatureLen;
184    SVGA3dDXShaderSignatureHeader *signature;
185 
186    /** Per-context shader identifier used with SVGA_3D_CMD_SHADER_DEFINE,
187     * SVGA_3D_CMD_SET_SHADER and SVGA_3D_CMD_SHADER_DESTROY.
188     */
189    unsigned id;
190 
191    /** Start of extra constants (number of float[4] constants) */
192    unsigned extra_const_start;
193 
194    /* GB object buffer containing the bytecode */
195    struct svga_winsys_gb_shader *gb_shader;
196 
197    /** Next variant */
198    struct svga_shader_variant *next;
199 };
200 
201 
202 /**
203  * Shader variant for fragment shader
204  */
205 struct svga_fs_variant
206 {
207    struct svga_shader_variant base;
208 
209    boolean uses_flat_interp;   /** TRUE if flat interpolation qualifier is
210                                 *  applied to any of the varyings.
211                                 */
212 
213    /** Is the color output just a constant value? (fragment shader only) */
214    boolean constant_color_output;
215 
216    /** Bitmask indicating which texture units are doing the shadow
217     * comparison test in the shader rather than the sampler state.
218     */
219    unsigned fs_shadow_compare_units;
220 
221    /** For FS-based polygon stipple */
222    unsigned pstipple_sampler_unit;
223 };
224 
225 
226 /**
227  * Shader variant for geometry shader
228  */
229 struct svga_gs_variant
230 {
231    struct svga_shader_variant base;
232 };
233 
234 
235 /**
236  * Shader variant for vertex shader
237  */
238 struct svga_vs_variant
239 {
240    struct svga_shader_variant base;
241 };
242 
243 
244 /**
245  * Shader variant for tessellation evaluation shader
246  */
247 struct svga_tes_variant
248 {
249    struct svga_shader_variant base;
250 
251    enum pipe_prim_type prim_mode:8;
252    enum pipe_tess_spacing spacing:3;
253    unsigned vertices_order_cw:1;
254    unsigned point_mode:1;
255 };
256 
257 
258 /**
259  * Shader variant for tessellation control shader
260  */
261 struct svga_tcs_variant
262 {
263    struct svga_shader_variant base;
264 };
265 
266 
267 /**
268  * Shader variant for compute shader
269  */
270 struct svga_cs_variant
271 {
272    struct svga_shader_variant base;
273 };
274 
275 
276 struct svga_shader
277 {
278    const struct tgsi_token *tokens;
279    struct svga_token_key token_key;     /* token key for the token string */
280    struct tgsi_shader_info info;
281 
282    /* List of shaders with tokens derived from the same token string */
283    struct svga_shader *next;
284    struct svga_shader *parent;   /* shader with the original token string */
285 
286    struct svga_stream_output *stream_output;
287 
288    /** Head of linked list of compiled variants */
289    struct svga_shader_variant *variants;
290 
291    unsigned id;  /**< for debugging only */
292 };
293 
294 
295 struct svga_fragment_shader
296 {
297    struct svga_shader base;
298 
299    struct draw_fragment_shader *draw_shader;
300 
301    /** Mask of which generic varying variables are read by this shader */
302    uint64_t generic_inputs;
303 
304    /** Table mapping original TGSI generic indexes to low integers */
305    int8_t generic_remap_table[MAX_GENERIC_VARYING];
306 };
307 
308 
309 struct svga_vertex_shader
310 {
311    struct svga_shader base;
312 
313    struct draw_vertex_shader *draw_shader;
314 
315    /** Mask of which generic varying variables are written by this shader */
316    uint64_t generic_outputs;
317 
318    /** Generated geometry shader that goes with this vertex shader */
319    struct svga_geometry_shader *gs;
320 };
321 
322 
323 struct svga_geometry_shader
324 {
325    struct svga_shader base;
326 
327    struct draw_geometry_shader *draw_shader;
328 
329    /** Table mapping original TGSI generic indexes to low integers */
330    int8_t generic_remap_table[MAX_GENERIC_VARYING];
331    uint64_t generic_outputs;
332 
333    unsigned aa_point_coord_index; /* generic index for aa point coord */
334 
335    unsigned wide_point:1;      /* set if the shader emulates wide point */
336 };
337 
338 
339 struct svga_tcs_shader
340 {
341    struct svga_shader base;
342 
343    /** Mask of which generic varying variables are written by this shader */
344    uint64_t generic_outputs;
345 };
346 
347 
348 struct svga_tes_shader
349 {
350    struct svga_shader base;
351 
352    /** Mask of which generic varying variables are written by this shader */
353    uint64_t generic_inputs;
354 };
355 
356 
357 struct svga_compute_shader
358 {
359    struct svga_shader base;
360 };
361 
362 
363 static inline boolean
svga_compile_keys_equal(const struct svga_compile_key * a,const struct svga_compile_key * b)364 svga_compile_keys_equal(const struct svga_compile_key *a,
365                         const struct svga_compile_key *b)
366 {
367    unsigned key_size =
368       (const char *) &a->tex[a->num_textures] - (const char *) a;
369 
370    return memcmp(a, b, key_size) == 0;
371 }
372 
373 
374 uint64_t
375 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);
376 
377 uint64_t
378 svga_get_generic_outputs_mask(const struct tgsi_shader_info *info);
379 
380 void
381 svga_remap_generics(uint64_t generics_mask,
382                     int8_t remap_table[MAX_GENERIC_VARYING]);
383 
384 int
385 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
386                          int generic_index);
387 
388 void
389 svga_init_shader_key_common(const struct svga_context *svga,
390                             enum pipe_shader_type shader_type,
391                             const struct svga_shader *shader,
392                             struct svga_compile_key *key);
393 
394 struct svga_shader_variant *
395 svga_search_shader_key(const struct svga_shader *shader,
396                        const struct svga_compile_key *key);
397 
398 struct svga_shader *
399 svga_search_shader_token_key(struct svga_shader *shader,
400                              const struct svga_token_key *key);
401 
402 enum pipe_error
403 svga_define_shader(struct svga_context *svga,
404                    struct svga_shader_variant *variant);
405 
406 enum pipe_error
407 svga_set_shader(struct svga_context *svga,
408                 SVGA3dShaderType type,
409                 struct svga_shader_variant *variant);
410 
411 struct svga_shader_variant *
412 svga_new_shader_variant(struct svga_context *svga, enum pipe_shader_type type);
413 
414 void
415 svga_destroy_shader_variant(struct svga_context *svga,
416                             struct svga_shader_variant *variant);
417 
418 enum pipe_error
419 svga_rebind_shaders(struct svga_context *svga);
420 
421 /**
422  * Check if a shader's bytecode exceeds the device limits.
423  */
424 static inline boolean
svga_shader_too_large(const struct svga_context * svga,const struct svga_shader_variant * variant)425 svga_shader_too_large(const struct svga_context *svga,
426                       const struct svga_shader_variant *variant)
427 {
428    if (svga_have_gb_objects(svga)) {
429       return FALSE;
430    }
431 
432    if (variant->nr_tokens * sizeof(variant->tokens[0])
433        + sizeof(SVGA3dCmdDefineShader) + sizeof(SVGA3dCmdHeader)
434        < SVGA_CB_MAX_COMMAND_SIZE) {
435       return FALSE;
436    }
437 
438    return TRUE;
439 }
440 
441 
442 /**
443  * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
444  */
445 static inline SVGA3dShaderType
svga_shader_type(enum pipe_shader_type shader)446 svga_shader_type(enum pipe_shader_type shader)
447 {
448    switch (shader) {
449    case PIPE_SHADER_VERTEX:
450       return SVGA3D_SHADERTYPE_VS;
451    case PIPE_SHADER_GEOMETRY:
452       return SVGA3D_SHADERTYPE_GS;
453    case PIPE_SHADER_FRAGMENT:
454       return SVGA3D_SHADERTYPE_PS;
455    case PIPE_SHADER_TESS_CTRL:
456       return SVGA3D_SHADERTYPE_HS;
457    case PIPE_SHADER_TESS_EVAL:
458       return SVGA3D_SHADERTYPE_DS;
459    case PIPE_SHADER_COMPUTE:
460       return SVGA3D_SHADERTYPE_CS;
461    default:
462       assert(!"Invalid shader type");
463       return SVGA3D_SHADERTYPE_VS;
464    }
465 }
466 
467 
468 /** Does the current VS have stream output? */
469 static inline boolean
svga_have_vs_streamout(const struct svga_context * svga)470 svga_have_vs_streamout(const struct svga_context *svga)
471 {
472    return svga->curr.vs != NULL && svga->curr.vs->base.stream_output != NULL;
473 }
474 
475 
476 /** Does the current GS have stream output? */
477 static inline boolean
svga_have_gs_streamout(const struct svga_context * svga)478 svga_have_gs_streamout(const struct svga_context *svga)
479 {
480    return svga->curr.gs != NULL && svga->curr.gs->base.stream_output != NULL;
481 }
482 
483 
484 static inline struct svga_fs_variant *
svga_fs_variant(struct svga_shader_variant * variant)485 svga_fs_variant(struct svga_shader_variant *variant)
486 {
487    assert(!variant || variant->type == SVGA3D_SHADERTYPE_PS);
488    return (struct svga_fs_variant *)variant;
489 }
490 
491 
492 static inline struct svga_tes_variant *
svga_tes_variant(struct svga_shader_variant * variant)493 svga_tes_variant(struct svga_shader_variant *variant)
494 {
495    assert(!variant || variant->type == SVGA3D_SHADERTYPE_DS);
496    return (struct svga_tes_variant *)variant;
497 }
498 
499 
500 static inline struct svga_cs_variant *
svga_cs_variant(struct svga_shader_variant * variant)501 svga_cs_variant(struct svga_shader_variant *variant)
502 {
503    assert(!variant || variant->type == SVGA3D_SHADERTYPE_CS);
504    return (struct svga_cs_variant *)variant;
505 }
506 
507 
508 /* Returns TRUE if we are currently using flat shading.
509  */
510 static inline boolean
svga_is_using_flat_shading(const struct svga_context * svga)511 svga_is_using_flat_shading(const struct svga_context *svga)
512 {
513    return
514       svga->state.hw_draw.fs ?
515          svga_fs_variant(svga->state.hw_draw.fs)->uses_flat_interp : FALSE;
516 }
517 
518 
519 #endif /* SVGA_SHADER_H */
520