aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/svga/svga_shader.h
blob: efcac4086264ee16eda653ffd25cfabd1eb74f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**********************************************************
 * Copyright 2008-2012 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/

#ifndef SVGA_SHADER_H
#define SVGA_SHADER_H

#include "svga3d_reg.h"
#include "svga_context.h"
#include "svga_streamout.h"


/**
 * We use a 64-bit mask to keep track of the generic indexes.
 * This is the maximum semantic index for a TGSI GENERIC[i] register.
 */
#define MAX_GENERIC_VARYING 64


struct svga_context;


struct svga_compile_key
{
   /* vertex shader only */
   struct {
      uint64_t fs_generic_inputs;
      unsigned passthrough:1;
      unsigned need_prescale:1;
      unsigned undo_viewport:1;
      unsigned allow_psiz:1;
      /** The following are all 32-bit bitmasks (per VS input) */
      unsigned adjust_attrib_range;
      unsigned attrib_is_pure_int;
      unsigned adjust_attrib_w_1;
      unsigned adjust_attrib_itof;
      unsigned adjust_attrib_utof;
      unsigned attrib_is_bgra;
      unsigned attrib_puint_to_snorm;
      unsigned attrib_puint_to_uscaled;
      unsigned attrib_puint_to_sscaled;
   } vs;

   /* geometry shader only */
   struct {
      uint64_t vs_generic_outputs;
      unsigned need_prescale:1;
      unsigned writes_psize:1;
      unsigned wide_point:1;
   } gs;

   /* fragment shader only */
   struct {
      uint64_t vs_generic_outputs;
      uint64_t gs_generic_outputs;
      unsigned light_twoside:1;
      unsigned front_ccw:1;
      unsigned white_fragments:1;
      unsigned flatshade:1;
      unsigned pstipple:1;
      unsigned alpha_func:4;  /**< SVGA3D_CMP_x */
      unsigned write_color0_to_n_cbufs:4;
      unsigned aa_point:1;
      int aa_point_coord_index;
      float alpha_ref;
   } fs;

   /* any shader type */
   int8_t generic_remap_table[MAX_GENERIC_VARYING];
   unsigned num_textures:8;
   unsigned num_unnormalized_coords:8;
   unsigned clip_plane_enable:PIPE_MAX_CLIP_PLANES;
   unsigned sprite_origin_lower_left:1;
   unsigned sprite_coord_enable;
   struct {
      unsigned compare_mode:1;
      unsigned compare_func:3;
      unsigned unnormalized:1;
      unsigned width_height_idx:5; /**< texture unit */
      unsigned texture_target:4;   /**< PIPE_TEXTURE_x */
      unsigned texture_msaa:1;    /**< A multisample texture? */
      unsigned sprite_texgen:1;
      unsigned swizzle_r:3;
      unsigned swizzle_g:3;
      unsigned swizzle_b:3;
      unsigned swizzle_a:3;
      unsigned return_type:3;  /**< TGSI_RETURN_TYPE_x */
   } tex[PIPE_MAX_SAMPLERS];
   /* Note: svga_compile_keys_equal() depends on the variable-size
    * tex[] array being at the end of this structure.
    */
};

/* A key for a variant of token string of a shader */
struct svga_token_key {
   struct {
      unsigned sprite_coord_enable:24;
      unsigned sprite_origin_upper_left:1;
      unsigned point_pos_stream_out:1;
      unsigned writes_psize:1;
      unsigned aa_point:1;
   } gs;
};

/**
 * A single TGSI shader may be compiled into different variants of
 * SVGA3D shaders depending on the compile key.  Each user shader
 * will have a linked list of these variants.
 */
struct svga_shader_variant
{
   const struct svga_shader *shader;

   /** Parameters used to generate this variant */
   struct svga_compile_key key;

   /* Compiled shader tokens:
    */
   const unsigned *tokens;
   unsigned nr_tokens;

   /** Per-context shader identifier used with SVGA_3D_CMD_SHADER_DEFINE,
    * SVGA_3D_CMD_SET_SHADER and SVGA_3D_CMD_SHADER_DESTROY.
    */
   unsigned id;

   /** Start of extra constants (number of float[4] constants) */
   unsigned extra_const_start;

   /* GB object buffer containing the bytecode */
   struct svga_winsys_gb_shader *gb_shader;

   boolean uses_flat_interp;   /** TRUE if flat interpolation qualifier is
                                *  applied to any of the varyings.
                                */

   /** For FS-based polygon stipple */
   unsigned pstipple_sampler_unit;

   /** Next variant */
   struct svga_shader_variant *next;
};


struct svga_shader
{
   const struct tgsi_token *tokens;
   struct svga_token_key token_key;     /* token key for the token string */
   struct tgsi_shader_info info;

   /* List of shaders with tokens derived from the same token string */
   struct svga_shader *next;
   struct svga_shader *parent;   /* shader with the original token string */

   struct svga_stream_output *stream_output;

   /** Head of linked list of compiled variants */
   struct svga_shader_variant *variants;

   unsigned id;  /**< for debugging only */
};


struct svga_fragment_shader
{
   struct svga_shader base;

   struct draw_fragment_shader *draw_shader;

   /** Mask of which generic varying variables are read by this shader */
   uint64_t generic_inputs;

   /** Table mapping original TGSI generic indexes to low integers */
   int8_t generic_remap_table[MAX_GENERIC_VARYING];
};


struct svga_vertex_shader
{
   struct svga_shader base;

   struct draw_vertex_shader *draw_shader;

   /** Mask of which generic varying variables are written by this shader */
   uint64_t generic_outputs;

   /** Generated geometry shader that goes with this vertex shader */
   struct svga_geometry_shader *gs;
};


struct svga_geometry_shader
{
   struct svga_shader base;

   struct draw_geometry_shader *draw_shader;

   /** Table mapping original TGSI generic indexes to low integers */
   int8_t generic_remap_table[MAX_GENERIC_VARYING];
   uint64_t generic_outputs;

   unsigned aa_point_coord_index; /* generic index for aa point coord */

   unsigned wide_point:1;      /* set if the shader emulates wide point */
};


static inline boolean
svga_compile_keys_equal(const struct svga_compile_key *a,
                        const struct svga_compile_key *b)
{
   unsigned key_size =
      (const char *) &a->tex[a->num_textures] - (const char *) a;

   return memcmp(a, b, key_size) == 0;
}


uint64_t
svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);

uint64_t
svga_get_generic_outputs_mask(const struct tgsi_shader_info *info);

void
svga_remap_generics(uint64_t generics_mask,
                    int8_t remap_table[MAX_GENERIC_VARYING]);

int
svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
                         int generic_index);

void
svga_init_shader_key_common(const struct svga_context *svga, unsigned shader,
                            struct svga_compile_key *key);

struct svga_shader_variant *
svga_search_shader_key(const struct svga_shader *shader,
                       const struct svga_compile_key *key);

struct svga_shader *
svga_search_shader_token_key(struct svga_shader *shader,
                             const struct svga_token_key *key);

enum pipe_error
svga_define_shader(struct svga_context *svga,
                   SVGA3dShaderType type,
                   struct svga_shader_variant *variant);

enum pipe_error
svga_set_shader(struct svga_context *svga,
                SVGA3dShaderType type,
                struct svga_shader_variant *variant);

struct svga_shader_variant *
svga_new_shader_variant(struct svga_context *svga);

enum pipe_error
svga_destroy_shader_variant(struct svga_context *svga,
                            SVGA3dShaderType type,
                            struct svga_shader_variant *variant);

enum pipe_error
svga_rebind_shaders(struct svga_context *svga);

/**
 * Check if a shader's bytecode exceeds the device limits.
 */
static inline boolean
svga_shader_too_large(const struct svga_context *svga,
                      const struct svga_shader_variant *variant)
{
   if (svga_have_gb_objects(svga)) {
      return FALSE;
   }

   if (variant->nr_tokens * sizeof(variant->tokens[0])
       + sizeof(SVGA3dCmdDefineShader) + sizeof(SVGA3dCmdHeader)
       < SVGA_CB_MAX_COMMAND_SIZE) {
      return FALSE;
   }

   return TRUE;
}


/**
 * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
 */
static inline SVGA3dShaderType
svga_shader_type(unsigned shader)
{
   switch (shader) {
   case PIPE_SHADER_VERTEX:
      return SVGA3D_SHADERTYPE_VS;
   case PIPE_SHADER_GEOMETRY:
      return SVGA3D_SHADERTYPE_GS;
   case PIPE_SHADER_FRAGMENT:
      return SVGA3D_SHADERTYPE_PS;
   default:
      assert(!"Invalid shader type");
      return SVGA3D_SHADERTYPE_VS;
   }
}


/** Does the current VS have stream output? */
static inline boolean
svga_have_vs_streamout(const struct svga_context *svga)
{
   return svga->curr.vs != NULL && svga->curr.vs->base.stream_output != NULL;
}


/** Does the current GS have stream output? */
static inline boolean
svga_have_gs_streamout(const struct svga_context *svga)
{
   return svga->curr.gs != NULL && svga->curr.gs->base.stream_output != NULL;
}


#endif /* SVGA_SHADER_H */