Home | History | Annotate | Download | only in svga
      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       /** The following are all 32-bit bitmasks (per VS input) */
     54       unsigned adjust_attrib_range;
     55       unsigned attrib_is_pure_int;
     56       unsigned adjust_attrib_w_1;
     57       unsigned adjust_attrib_itof;
     58       unsigned adjust_attrib_utof;
     59       unsigned attrib_is_bgra;
     60       unsigned attrib_puint_to_snorm;
     61       unsigned attrib_puint_to_uscaled;
     62       unsigned attrib_puint_to_sscaled;
     63    } vs;
     64 
     65    /* geometry shader only */
     66    struct {
     67       uint64_t vs_generic_outputs;
     68       unsigned need_prescale:1;
     69       unsigned writes_psize:1;
     70       unsigned wide_point:1;
     71    } gs;
     72 
     73    /* fragment shader only */
     74    struct {
     75       uint64_t vs_generic_outputs;
     76       uint64_t gs_generic_outputs;
     77       unsigned light_twoside:1;
     78       unsigned front_ccw:1;
     79       unsigned white_fragments:1;
     80       unsigned flatshade:1;
     81       unsigned pstipple:1;
     82       unsigned alpha_func:4;  /**< SVGA3D_CMP_x */
     83       unsigned write_color0_to_n_cbufs:4;
     84       unsigned aa_point:1;
     85       int aa_point_coord_index;
     86       float alpha_ref;
     87    } fs;
     88 
     89    /* any shader type */
     90    int8_t generic_remap_table[MAX_GENERIC_VARYING];
     91    unsigned num_textures:8;
     92    unsigned num_unnormalized_coords:8;
     93    unsigned clip_plane_enable:PIPE_MAX_CLIP_PLANES;
     94    unsigned sprite_origin_lower_left:1;
     95    unsigned sprite_coord_enable;
     96    struct {
     97       unsigned compare_mode:1;
     98       unsigned compare_func:3;
     99       unsigned unnormalized:1;
    100       unsigned width_height_idx:5; /**< texture unit */
    101       unsigned is_array:1;
    102       unsigned sprite_texgen:1;
    103       unsigned swizzle_r:3;
    104       unsigned swizzle_g:3;
    105       unsigned swizzle_b:3;
    106       unsigned swizzle_a:3;
    107    } tex[PIPE_MAX_SAMPLERS];
    108    /* Note: svga_compile_keys_equal() depends on the variable-size
    109     * tex[] array being at the end of this structure.
    110     */
    111 };
    112 
    113 /* A key for a variant of token string of a shader */
    114 struct svga_token_key {
    115    struct {
    116       unsigned sprite_coord_enable:24;
    117       unsigned sprite_origin_upper_left:1;
    118       unsigned point_pos_stream_out:1;
    119       unsigned writes_psize:1;
    120       unsigned aa_point:1;
    121    } gs;
    122 };
    123 
    124 /**
    125  * A single TGSI shader may be compiled into different variants of
    126  * SVGA3D shaders depending on the compile key.  Each user shader
    127  * will have a linked list of these variants.
    128  */
    129 struct svga_shader_variant
    130 {
    131    const struct svga_shader *shader;
    132 
    133    /** Parameters used to generate this variant */
    134    struct svga_compile_key key;
    135 
    136    /* Compiled shader tokens:
    137     */
    138    const unsigned *tokens;
    139    unsigned nr_tokens;
    140 
    141    /** Per-context shader identifier used with SVGA_3D_CMD_SHADER_DEFINE,
    142     * SVGA_3D_CMD_SET_SHADER and SVGA_3D_CMD_SHADER_DESTROY.
    143     */
    144    unsigned id;
    145 
    146    /** Start of extra constants (number of float[4] constants) */
    147    unsigned extra_const_start;
    148 
    149    /* GB object buffer containing the bytecode */
    150    struct svga_winsys_gb_shader *gb_shader;
    151 
    152    boolean uses_flat_interp;   /** TRUE if flat interpolation qualifier is
    153                                 *  applied to any of the varyings.
    154                                 */
    155 
    156    /** Is the color output just a constant value? (fragment shader only) */
    157    boolean constant_color_output;
    158 
    159    /** For FS-based polygon stipple */
    160    unsigned pstipple_sampler_unit;
    161 
    162    /** Next variant */
    163    struct svga_shader_variant *next;
    164 };
    165 
    166 
    167 struct svga_shader
    168 {
    169    const struct tgsi_token *tokens;
    170    struct svga_token_key token_key;     /* token key for the token string */
    171    struct tgsi_shader_info info;
    172 
    173    /* List of shaders with tokens derived from the same token string */
    174    struct svga_shader *next;
    175    struct svga_shader *parent;   /* shader with the original token string */
    176 
    177    struct svga_stream_output *stream_output;
    178 
    179    /** Head of linked list of compiled variants */
    180    struct svga_shader_variant *variants;
    181 
    182    unsigned id;  /**< for debugging only */
    183 };
    184 
    185 
    186 struct svga_fragment_shader
    187 {
    188    struct svga_shader base;
    189 
    190    struct draw_fragment_shader *draw_shader;
    191 
    192    /** Mask of which generic varying variables are read by this shader */
    193    uint64_t generic_inputs;
    194 
    195    /** Table mapping original TGSI generic indexes to low integers */
    196    int8_t generic_remap_table[MAX_GENERIC_VARYING];
    197 };
    198 
    199 
    200 struct svga_vertex_shader
    201 {
    202    struct svga_shader base;
    203 
    204    struct draw_vertex_shader *draw_shader;
    205 
    206    /** Mask of which generic varying variables are written by this shader */
    207    uint64_t generic_outputs;
    208 
    209    /** Generated geometry shader that goes with this vertex shader */
    210    struct svga_geometry_shader *gs;
    211 };
    212 
    213 
    214 struct svga_geometry_shader
    215 {
    216    struct svga_shader base;
    217 
    218    struct draw_geometry_shader *draw_shader;
    219 
    220    /** Table mapping original TGSI generic indexes to low integers */
    221    int8_t generic_remap_table[MAX_GENERIC_VARYING];
    222    uint64_t generic_outputs;
    223 
    224    unsigned aa_point_coord_index; /* generic index for aa point coord */
    225 
    226    unsigned wide_point:1;      /* set if the shader emulates wide point */
    227 };
    228 
    229 
    230 static inline boolean
    231 svga_compile_keys_equal(const struct svga_compile_key *a,
    232                         const struct svga_compile_key *b)
    233 {
    234    unsigned key_size =
    235       (const char *) &a->tex[a->num_textures] - (const char *) a;
    236 
    237    return memcmp(a, b, key_size) == 0;
    238 }
    239 
    240 
    241 uint64_t
    242 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);
    243 
    244 uint64_t
    245 svga_get_generic_outputs_mask(const struct tgsi_shader_info *info);
    246 
    247 void
    248 svga_remap_generics(uint64_t generics_mask,
    249                     int8_t remap_table[MAX_GENERIC_VARYING]);
    250 
    251 int
    252 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
    253                          int generic_index);
    254 
    255 void
    256 svga_init_shader_key_common(const struct svga_context *svga,
    257                             enum pipe_shader_type shader,
    258                             struct svga_compile_key *key);
    259 
    260 struct svga_shader_variant *
    261 svga_search_shader_key(const struct svga_shader *shader,
    262                        const struct svga_compile_key *key);
    263 
    264 struct svga_shader *
    265 svga_search_shader_token_key(struct svga_shader *shader,
    266                              const struct svga_token_key *key);
    267 
    268 enum pipe_error
    269 svga_define_shader(struct svga_context *svga,
    270                    SVGA3dShaderType type,
    271                    struct svga_shader_variant *variant);
    272 
    273 enum pipe_error
    274 svga_set_shader(struct svga_context *svga,
    275                 SVGA3dShaderType type,
    276                 struct svga_shader_variant *variant);
    277 
    278 struct svga_shader_variant *
    279 svga_new_shader_variant(struct svga_context *svga);
    280 
    281 enum pipe_error
    282 svga_destroy_shader_variant(struct svga_context *svga,
    283                             SVGA3dShaderType type,
    284                             struct svga_shader_variant *variant);
    285 
    286 enum pipe_error
    287 svga_rebind_shaders(struct svga_context *svga);
    288 
    289 /**
    290  * Check if a shader's bytecode exceeds the device limits.
    291  */
    292 static inline boolean
    293 svga_shader_too_large(const struct svga_context *svga,
    294                       const struct svga_shader_variant *variant)
    295 {
    296    if (svga_have_gb_objects(svga)) {
    297       return FALSE;
    298    }
    299 
    300    if (variant->nr_tokens * sizeof(variant->tokens[0])
    301        + sizeof(SVGA3dCmdDefineShader) + sizeof(SVGA3dCmdHeader)
    302        < SVGA_CB_MAX_COMMAND_SIZE) {
    303       return FALSE;
    304    }
    305 
    306    return TRUE;
    307 }
    308 
    309 
    310 /**
    311  * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
    312  */
    313 static inline SVGA3dShaderType
    314 svga_shader_type(enum pipe_shader_type shader)
    315 {
    316    switch (shader) {
    317    case PIPE_SHADER_VERTEX:
    318       return SVGA3D_SHADERTYPE_VS;
    319    case PIPE_SHADER_GEOMETRY:
    320       return SVGA3D_SHADERTYPE_GS;
    321    case PIPE_SHADER_FRAGMENT:
    322       return SVGA3D_SHADERTYPE_PS;
    323    default:
    324       assert(!"Invalid shader type");
    325       return SVGA3D_SHADERTYPE_VS;
    326    }
    327 }
    328 
    329 
    330 /** Does the current VS have stream output? */
    331 static inline boolean
    332 svga_have_vs_streamout(const struct svga_context *svga)
    333 {
    334    return svga->curr.vs != NULL && svga->curr.vs->base.stream_output != NULL;
    335 }
    336 
    337 
    338 /** Does the current GS have stream output? */
    339 static inline boolean
    340 svga_have_gs_streamout(const struct svga_context *svga)
    341 {
    342    return svga->curr.gs != NULL && svga->curr.gs->base.stream_output != NULL;
    343 }
    344 
    345 
    346 #endif /* SVGA_SHADER_H */
    347