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