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 unsigned target:4;
142 unsigned sampler_return_type:4;
143 unsigned sampler_view:1;
144 } tex[PIPE_MAX_SAMPLERS];
145 /* Note: svga_compile_keys_equal() depends on the variable-size
146 * tex[] array being at the end of this structure.
147 */
148 };
149
150 /* A key for a variant of token string of a shader */
151 struct svga_token_key {
152 struct {
153 unsigned sprite_coord_enable:24;
154 unsigned sprite_origin_upper_left:1;
155 unsigned point_pos_stream_out:1;
156 unsigned writes_psize:1;
157 unsigned aa_point:1;
158 } gs;
159 struct {
160 unsigned write_position:1;
161 } vs;
162 unsigned dynamic_indexing:1;
163 };
164
165 /**
166 * A single TGSI shader may be compiled into different variants of
167 * SVGA3D shaders depending on the compile key. Each user shader
168 * will have a linked list of these variants.
169 */
170 struct svga_shader_variant
171 {
172 const struct svga_shader *shader;
173
174 /** Parameters used to generate this variant */
175 struct svga_compile_key key;
176
177 /* svga shader type */
178 SVGA3dShaderType type;
179
180 /* Compiled shader tokens:
181 */
182 const unsigned *tokens;
183 unsigned nr_tokens;
184
185 /* shader signature */
186 unsigned signatureLen;
187 SVGA3dDXShaderSignatureHeader *signature;
188
189 /** Per-context shader identifier used with SVGA_3D_CMD_SHADER_DEFINE,
190 * SVGA_3D_CMD_SET_SHADER and SVGA_3D_CMD_SHADER_DESTROY.
191 */
192 unsigned id;
193
194 /** Start of extra constants (number of float[4] constants) */
195 unsigned extra_const_start;
196
197 /* GB object buffer containing the bytecode */
198 struct svga_winsys_gb_shader *gb_shader;
199
200 /** Next variant */
201 struct svga_shader_variant *next;
202 };
203
204
205 /**
206 * Shader variant for fragment shader
207 */
208 struct svga_fs_variant
209 {
210 struct svga_shader_variant base;
211
212 boolean uses_flat_interp; /** TRUE if flat interpolation qualifier is
213 * applied to any of the varyings.
214 */
215
216 /** Is the color output just a constant value? (fragment shader only) */
217 boolean constant_color_output;
218
219 /** Bitmask indicating which texture units are doing the shadow
220 * comparison test in the shader rather than the sampler state.
221 */
222 unsigned fs_shadow_compare_units;
223
224 /** For FS-based polygon stipple */
225 unsigned pstipple_sampler_unit;
226 };
227
228
229 /**
230 * Shader variant for geometry shader
231 */
232 struct svga_gs_variant
233 {
234 struct svga_shader_variant base;
235 };
236
237
238 /**
239 * Shader variant for vertex shader
240 */
241 struct svga_vs_variant
242 {
243 struct svga_shader_variant base;
244 };
245
246
247 /**
248 * Shader variant for tessellation evaluation shader
249 */
250 struct svga_tes_variant
251 {
252 struct svga_shader_variant base;
253
254 enum pipe_prim_type prim_mode:8;
255 enum pipe_tess_spacing spacing:3;
256 unsigned vertices_order_cw:1;
257 unsigned point_mode:1;
258 };
259
260
261 /**
262 * Shader variant for tessellation control shader
263 */
264 struct svga_tcs_variant
265 {
266 struct svga_shader_variant base;
267 };
268
269
270 /**
271 * Shader variant for compute shader
272 */
273 struct svga_cs_variant
274 {
275 struct svga_shader_variant base;
276 };
277
278
279 struct svga_shader
280 {
281 const struct tgsi_token *tokens;
282 struct svga_token_key token_key; /* token key for the token string */
283 struct tgsi_shader_info info;
284
285 /* List of shaders with tokens derived from the same token string */
286 struct svga_shader *next;
287 struct svga_shader *parent; /* shader with the original token string */
288
289 struct svga_stream_output *stream_output;
290
291 /** Head of linked list of compiled variants */
292 struct svga_shader_variant *variants;
293
294 unsigned id; /**< for debugging only */
295 };
296
297
298 struct svga_fragment_shader
299 {
300 struct svga_shader base;
301
302 struct draw_fragment_shader *draw_shader;
303
304 /** Mask of which generic varying variables are read by this shader */
305 uint64_t generic_inputs;
306
307 /** Table mapping original TGSI generic indexes to low integers */
308 int8_t generic_remap_table[MAX_GENERIC_VARYING];
309 };
310
311
312 struct svga_vertex_shader
313 {
314 struct svga_shader base;
315
316 struct draw_vertex_shader *draw_shader;
317
318 /** Mask of which generic varying variables are written by this shader */
319 uint64_t generic_outputs;
320
321 /** Generated geometry shader that goes with this vertex shader */
322 struct svga_geometry_shader *gs;
323 };
324
325
326 struct svga_geometry_shader
327 {
328 struct svga_shader base;
329
330 struct draw_geometry_shader *draw_shader;
331
332 /** Table mapping original TGSI generic indexes to low integers */
333 int8_t generic_remap_table[MAX_GENERIC_VARYING];
334 uint64_t generic_outputs;
335
336 unsigned aa_point_coord_index; /* generic index for aa point coord */
337
338 unsigned wide_point:1; /* set if the shader emulates wide point */
339 };
340
341
342 struct svga_tcs_shader
343 {
344 struct svga_shader base;
345
346 /** Mask of which generic varying variables are written by this shader */
347 uint64_t generic_outputs;
348 };
349
350
351 struct svga_tes_shader
352 {
353 struct svga_shader base;
354
355 /** Mask of which generic varying variables are written by this shader */
356 uint64_t generic_inputs;
357 };
358
359
360 struct svga_compute_shader
361 {
362 struct svga_shader base;
363 };
364
365
366 static inline boolean
svga_compile_keys_equal(const struct svga_compile_key * a,const struct svga_compile_key * b)367 svga_compile_keys_equal(const struct svga_compile_key *a,
368 const struct svga_compile_key *b)
369 {
370 unsigned key_size =
371 (const char *) &a->tex[a->num_textures] - (const char *) a;
372
373 return memcmp(a, b, key_size) == 0;
374 }
375
376
377 uint64_t
378 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);
379
380 uint64_t
381 svga_get_generic_outputs_mask(const struct tgsi_shader_info *info);
382
383 void
384 svga_remap_generics(uint64_t generics_mask,
385 int8_t remap_table[MAX_GENERIC_VARYING]);
386
387 int
388 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
389 int generic_index);
390
391 void
392 svga_init_shader_key_common(const struct svga_context *svga,
393 enum pipe_shader_type shader_type,
394 const struct svga_shader *shader,
395 struct svga_compile_key *key);
396
397 struct svga_shader_variant *
398 svga_search_shader_key(const struct svga_shader *shader,
399 const struct svga_compile_key *key);
400
401 struct svga_shader *
402 svga_search_shader_token_key(struct svga_shader *shader,
403 const struct svga_token_key *key);
404
405 enum pipe_error
406 svga_define_shader(struct svga_context *svga,
407 struct svga_shader_variant *variant);
408
409 enum pipe_error
410 svga_set_shader(struct svga_context *svga,
411 SVGA3dShaderType type,
412 struct svga_shader_variant *variant);
413
414 struct svga_shader_variant *
415 svga_new_shader_variant(struct svga_context *svga, enum pipe_shader_type type);
416
417 void
418 svga_destroy_shader_variant(struct svga_context *svga,
419 struct svga_shader_variant *variant);
420
421 enum pipe_error
422 svga_rebind_shaders(struct svga_context *svga);
423
424 /**
425 * Check if a shader's bytecode exceeds the device limits.
426 */
427 static inline boolean
svga_shader_too_large(const struct svga_context * svga,const struct svga_shader_variant * variant)428 svga_shader_too_large(const struct svga_context *svga,
429 const struct svga_shader_variant *variant)
430 {
431 if (svga_have_gb_objects(svga)) {
432 return FALSE;
433 }
434
435 if (variant->nr_tokens * sizeof(variant->tokens[0])
436 + sizeof(SVGA3dCmdDefineShader) + sizeof(SVGA3dCmdHeader)
437 < SVGA_CB_MAX_COMMAND_SIZE) {
438 return FALSE;
439 }
440
441 return TRUE;
442 }
443
444
445 /**
446 * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
447 */
448 static inline SVGA3dShaderType
svga_shader_type(enum pipe_shader_type shader)449 svga_shader_type(enum pipe_shader_type shader)
450 {
451 switch (shader) {
452 case PIPE_SHADER_VERTEX:
453 return SVGA3D_SHADERTYPE_VS;
454 case PIPE_SHADER_GEOMETRY:
455 return SVGA3D_SHADERTYPE_GS;
456 case PIPE_SHADER_FRAGMENT:
457 return SVGA3D_SHADERTYPE_PS;
458 case PIPE_SHADER_TESS_CTRL:
459 return SVGA3D_SHADERTYPE_HS;
460 case PIPE_SHADER_TESS_EVAL:
461 return SVGA3D_SHADERTYPE_DS;
462 case PIPE_SHADER_COMPUTE:
463 return SVGA3D_SHADERTYPE_CS;
464 default:
465 assert(!"Invalid shader type");
466 return SVGA3D_SHADERTYPE_VS;
467 }
468 }
469
470
471 /** Does the current VS have stream output? */
472 static inline boolean
svga_have_vs_streamout(const struct svga_context * svga)473 svga_have_vs_streamout(const struct svga_context *svga)
474 {
475 return svga->curr.vs != NULL && svga->curr.vs->base.stream_output != NULL;
476 }
477
478
479 /** Does the current GS have stream output? */
480 static inline boolean
svga_have_gs_streamout(const struct svga_context * svga)481 svga_have_gs_streamout(const struct svga_context *svga)
482 {
483 return svga->curr.gs != NULL && svga->curr.gs->base.stream_output != NULL;
484 }
485
486
487 static inline struct svga_fs_variant *
svga_fs_variant(struct svga_shader_variant * variant)488 svga_fs_variant(struct svga_shader_variant *variant)
489 {
490 assert(!variant || variant->type == SVGA3D_SHADERTYPE_PS);
491 return (struct svga_fs_variant *)variant;
492 }
493
494
495 static inline struct svga_tes_variant *
svga_tes_variant(struct svga_shader_variant * variant)496 svga_tes_variant(struct svga_shader_variant *variant)
497 {
498 assert(!variant || variant->type == SVGA3D_SHADERTYPE_DS);
499 return (struct svga_tes_variant *)variant;
500 }
501
502
503 static inline struct svga_cs_variant *
svga_cs_variant(struct svga_shader_variant * variant)504 svga_cs_variant(struct svga_shader_variant *variant)
505 {
506 assert(!variant || variant->type == SVGA3D_SHADERTYPE_CS);
507 return (struct svga_cs_variant *)variant;
508 }
509
510
511 /* Returns TRUE if we are currently using flat shading.
512 */
513 static inline boolean
svga_is_using_flat_shading(const struct svga_context * svga)514 svga_is_using_flat_shading(const struct svga_context *svga)
515 {
516 return
517 svga->state.hw_draw.fs ?
518 svga_fs_variant(svga->state.hw_draw.fs)->uses_flat_interp : FALSE;
519 }
520
521
522 #endif /* SVGA_SHADER_H */
523