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