9984809ea24f74277c28d326149bfaa19c53c8bf
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_shader.h
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #ifndef IR3_SHADER_H_
30 #define IR3_SHADER_H_
31
32 #include "pipe/p_state.h"
33 #include "compiler/shader_enums.h"
34 #include "util/bitscan.h"
35
36 #include "ir3.h"
37 #include "disasm.h"
38
39 struct glsl_type;
40
41 /* driver param indices: */
42 enum ir3_driver_param {
43 /* compute shader driver params: */
44 IR3_DP_NUM_WORK_GROUPS_X = 0,
45 IR3_DP_NUM_WORK_GROUPS_Y = 1,
46 IR3_DP_NUM_WORK_GROUPS_Z = 2,
47 IR3_DP_CS_COUNT = 4, /* must be aligned to vec4 */
48
49 /* vertex shader driver params: */
50 IR3_DP_VTXID_BASE = 0,
51 IR3_DP_VTXCNT_MAX = 1,
52 /* user-clip-plane components, up to 8x vec4's: */
53 IR3_DP_UCP0_X = 4,
54 /* .... */
55 IR3_DP_UCP7_W = 35,
56 IR3_DP_VS_COUNT = 36 /* must be aligned to vec4 */
57 };
58
59 /* Configuration key used to identify a shader variant.. different
60 * shader variants can be used to implement features not supported
61 * in hw (two sided color), binning-pass vertex shader, etc.
62 */
63 struct ir3_shader_key {
64 union {
65 struct {
66 /*
67 * Combined Vertex/Fragment shader parameters:
68 */
69 unsigned ucp_enables : 8;
70
71 /* do we need to check {v,f}saturate_{s,t,r}? */
72 unsigned has_per_samp : 1;
73
74 /*
75 * Vertex shader variant parameters:
76 */
77 unsigned binning_pass : 1;
78 unsigned vclamp_color : 1;
79
80 /*
81 * Fragment shader variant parameters:
82 */
83 unsigned color_two_side : 1;
84 unsigned half_precision : 1;
85 /* used when shader needs to handle flat varyings (a4xx)
86 * for front/back color inputs to frag shader:
87 */
88 unsigned rasterflat : 1;
89 unsigned fclamp_color : 1;
90 };
91 uint32_t global;
92 };
93
94 /* bitmask of sampler which needs coords clamped for vertex
95 * shader:
96 */
97 uint16_t vsaturate_s, vsaturate_t, vsaturate_r;
98
99 /* bitmask of sampler which needs coords clamped for frag
100 * shader:
101 */
102 uint16_t fsaturate_s, fsaturate_t, fsaturate_r;
103
104 /* bitmask of samplers which need astc srgb workaround: */
105 uint16_t vastc_srgb, fastc_srgb;
106 };
107
108 static inline bool
109 ir3_shader_key_equal(struct ir3_shader_key *a, struct ir3_shader_key *b)
110 {
111 /* slow-path if we need to check {v,f}saturate_{s,t,r} */
112 if (a->has_per_samp || b->has_per_samp)
113 return memcmp(a, b, sizeof(struct ir3_shader_key)) == 0;
114 return a->global == b->global;
115 }
116
117 /* will the two keys produce different lowering for a fragment shader? */
118 static inline bool
119 ir3_shader_key_changes_fs(struct ir3_shader_key *key, struct ir3_shader_key *last_key)
120 {
121 if (last_key->has_per_samp || key->has_per_samp) {
122 if ((last_key->fsaturate_s != key->fsaturate_s) ||
123 (last_key->fsaturate_t != key->fsaturate_t) ||
124 (last_key->fsaturate_r != key->fsaturate_r) ||
125 (last_key->fastc_srgb != key->fastc_srgb))
126 return true;
127 }
128
129 if (last_key->fclamp_color != key->fclamp_color)
130 return true;
131
132 if (last_key->color_two_side != key->color_two_side)
133 return true;
134
135 if (last_key->half_precision != key->half_precision)
136 return true;
137
138 if (last_key->rasterflat != key->rasterflat)
139 return true;
140
141 if (last_key->ucp_enables != key->ucp_enables)
142 return true;
143
144 return false;
145 }
146
147 /* will the two keys produce different lowering for a vertex shader? */
148 static inline bool
149 ir3_shader_key_changes_vs(struct ir3_shader_key *key, struct ir3_shader_key *last_key)
150 {
151 if (last_key->has_per_samp || key->has_per_samp) {
152 if ((last_key->vsaturate_s != key->vsaturate_s) ||
153 (last_key->vsaturate_t != key->vsaturate_t) ||
154 (last_key->vsaturate_r != key->vsaturate_r) ||
155 (last_key->vastc_srgb != key->vastc_srgb))
156 return true;
157 }
158
159 if (last_key->vclamp_color != key->vclamp_color)
160 return true;
161
162 if (last_key->ucp_enables != key->ucp_enables)
163 return true;
164
165 return false;
166 }
167
168 struct ir3_shader_variant {
169 struct fd_bo *bo;
170
171 /* variant id (for debug) */
172 uint32_t id;
173
174 struct ir3_shader_key key;
175
176 struct ir3_info info;
177 struct ir3 *ir;
178
179 /* the instructions length is in units of instruction groups
180 * (4 instructions for a3xx, 16 instructions for a4xx.. each
181 * instruction is 2 dwords):
182 */
183 unsigned instrlen;
184
185 /* the constants length is in units of vec4's, and is the sum of
186 * the uniforms and the built-in compiler constants
187 */
188 unsigned constlen;
189
190 /* number of uniforms (in vec4), not including built-in compiler
191 * constants, etc.
192 */
193 unsigned num_uniforms;
194 unsigned num_ubos;
195
196 /* About Linkage:
197 * + Let the frag shader determine the position/compmask for the
198 * varyings, since it is the place where we know if the varying
199 * is actually used, and if so, which components are used. So
200 * what the hw calls "outloc" is taken from the "inloc" of the
201 * frag shader.
202 * + From the vert shader, we only need the output regid
203 */
204
205 /* for frag shader, pos_regid holds the frag_pos, ie. what is passed
206 * to bary.f instructions
207 */
208 uint8_t pos_regid;
209 bool frag_coord, frag_face, color0_mrt;
210
211 /* NOTE: for input/outputs, slot is:
212 * gl_vert_attrib - for VS inputs
213 * gl_varying_slot - for VS output / FS input
214 * gl_frag_result - for FS output
215 */
216
217 /* varyings/outputs: */
218 unsigned outputs_count;
219 struct {
220 uint8_t slot;
221 uint8_t regid;
222 } outputs[16 + 2]; /* +POSITION +PSIZE */
223 bool writes_pos, writes_psize;
224
225 /* attributes (VS) / varyings (FS):
226 * Note that sysval's should come *after* normal inputs.
227 */
228 unsigned inputs_count;
229 struct {
230 uint8_t slot;
231 uint8_t regid;
232 uint8_t compmask;
233 uint8_t ncomp;
234 /* location of input (ie. offset passed to bary.f, etc). This
235 * matches the SP_VS_VPC_DST_REG.OUTLOCn value (a3xx and a4xx
236 * have the OUTLOCn value offset by 8, presumably to account
237 * for gl_Position/gl_PointSize)
238 */
239 uint8_t inloc;
240 /* vertex shader specific: */
241 bool sysval : 1; /* slot is a gl_system_value */
242 /* fragment shader specific: */
243 bool bary : 1; /* fetched varying (vs one loaded into reg) */
244 bool rasterflat : 1; /* special handling for emit->rasterflat */
245 enum glsl_interp_mode interpolate;
246 } inputs[16 + 2]; /* +POSITION +FACE */
247
248 /* sum of input components (scalar). For frag shaders, it only counts
249 * the varying inputs:
250 */
251 unsigned total_in;
252
253 /* For frag shaders, the total number of inputs (not scalar,
254 * ie. SP_VS_PARAM_REG.TOTALVSOUTVAR)
255 */
256 unsigned varying_in;
257
258 /* do we have one or more texture sample instructions: */
259 bool has_samp;
260
261 /* do we have one or more SSBO instructions: */
262 bool has_ssbo;
263
264 /* do we have kill instructions: */
265 bool has_kill;
266
267 /* Layout of constant registers, each section (in vec4). Pointer size
268 * is 32b (a3xx, a4xx), or 64b (a5xx+), which effects the size of the
269 * UBO and stream-out consts.
270 */
271 struct {
272 /* user const start at zero */
273 unsigned ubo;
274 unsigned driver_param;
275 unsigned tfbo;
276 unsigned immediate;
277 } constbase;
278
279 unsigned immediates_count;
280 struct {
281 uint32_t val[4];
282 } immediates[64];
283
284 /* for astc srgb workaround, the number/base of additional
285 * alpha tex states we need, and index of original tex states
286 */
287 struct {
288 unsigned base, count;
289 unsigned orig_idx[16];
290 } astc_srgb;
291
292 /* shader variants form a linked list: */
293 struct ir3_shader_variant *next;
294
295 /* replicated here to avoid passing extra ptrs everywhere: */
296 enum shader_t type;
297 struct ir3_shader *shader;
298 };
299
300 typedef struct nir_shader nir_shader;
301
302 struct ir3_shader {
303 enum shader_t type;
304
305 /* shader id (for debug): */
306 uint32_t id;
307 uint32_t variant_count;
308
309 /* so we know when we can disable TGSI related hacks: */
310 bool from_tgsi;
311
312 struct ir3_compiler *compiler;
313
314 nir_shader *nir;
315 struct pipe_stream_output_info stream_output;
316
317 struct ir3_shader_variant *variants;
318 };
319
320 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id);
321
322 struct ir3_shader * ir3_shader_create(struct ir3_compiler *compiler,
323 const struct pipe_shader_state *cso, enum shader_t type,
324 struct pipe_debug_callback *debug);
325 struct ir3_shader *
326 ir3_shader_create_compute(struct ir3_compiler *compiler,
327 const struct pipe_compute_state *cso,
328 struct pipe_debug_callback *debug);
329 void ir3_shader_destroy(struct ir3_shader *shader);
330 struct ir3_shader_variant * ir3_shader_variant(struct ir3_shader *shader,
331 struct ir3_shader_key key, struct pipe_debug_callback *debug);
332 void ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin);
333 uint64_t ir3_shader_outputs(const struct ir3_shader *so);
334
335 struct fd_ringbuffer;
336 struct fd_context;
337 void ir3_emit_vs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
338 struct fd_context *ctx, const struct pipe_draw_info *info);
339 void ir3_emit_fs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
340 struct fd_context *ctx);
341 void ir3_emit_cs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
342 struct fd_context *ctx, const struct pipe_grid_info *info);
343
344 int
345 ir3_glsl_type_size(const struct glsl_type *type);
346
347 static inline const char *
348 ir3_shader_stage(struct ir3_shader *shader)
349 {
350 switch (shader->type) {
351 case SHADER_VERTEX: return "VERT";
352 case SHADER_FRAGMENT: return "FRAG";
353 case SHADER_COMPUTE: return "CL";
354 default:
355 unreachable("invalid type");
356 return NULL;
357 }
358 }
359
360 /*
361 * Helper/util:
362 */
363
364 #include "pipe/p_shader_tokens.h"
365
366 static inline int
367 ir3_find_output(const struct ir3_shader_variant *so, gl_varying_slot slot)
368 {
369 int j;
370
371 for (j = 0; j < so->outputs_count; j++)
372 if (so->outputs[j].slot == slot)
373 return j;
374
375 /* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
376 * in the vertex shader.. but the fragment shader doesn't know this
377 * so it will always have both IN.COLOR[n] and IN.BCOLOR[n]. So
378 * at link time if there is no matching OUT.BCOLOR[n], we must map
379 * OUT.COLOR[n] to IN.BCOLOR[n]. And visa versa if there is only
380 * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
381 */
382 if (slot == VARYING_SLOT_BFC0) {
383 slot = VARYING_SLOT_COL0;
384 } else if (slot == VARYING_SLOT_BFC1) {
385 slot = VARYING_SLOT_COL1;
386 } else if (slot == VARYING_SLOT_COL0) {
387 slot = VARYING_SLOT_BFC0;
388 } else if (slot == VARYING_SLOT_COL1) {
389 slot = VARYING_SLOT_BFC1;
390 } else {
391 return 0;
392 }
393
394 for (j = 0; j < so->outputs_count; j++)
395 if (so->outputs[j].slot == slot)
396 return j;
397
398 debug_assert(0);
399
400 return 0;
401 }
402
403 static inline int
404 ir3_next_varying(const struct ir3_shader_variant *so, int i)
405 {
406 while (++i < so->inputs_count)
407 if (so->inputs[i].compmask && so->inputs[i].bary)
408 break;
409 return i;
410 }
411
412 struct ir3_shader_linkage {
413 uint8_t max_loc;
414 uint8_t cnt;
415 struct {
416 uint8_t regid;
417 uint8_t compmask;
418 uint8_t loc;
419 } var[32];
420 };
421
422 static inline void
423 ir3_link_add(struct ir3_shader_linkage *l, uint8_t regid, uint8_t compmask, uint8_t loc)
424 {
425 int i = l->cnt++;
426
427 debug_assert(i < ARRAY_SIZE(l->var));
428
429 l->var[i].regid = regid;
430 l->var[i].compmask = compmask;
431 l->var[i].loc = loc;
432 l->max_loc = MAX2(l->max_loc, loc + util_last_bit(compmask));
433 }
434
435 static inline void
436 ir3_link_shaders(struct ir3_shader_linkage *l,
437 const struct ir3_shader_variant *vs,
438 const struct ir3_shader_variant *fs)
439 {
440 int j = -1, k;
441
442 while (l->cnt < ARRAY_SIZE(l->var)) {
443 j = ir3_next_varying(fs, j);
444
445 if (j >= fs->inputs_count)
446 break;
447
448 if (fs->inputs[j].inloc >= fs->total_in)
449 continue;
450
451 k = ir3_find_output(vs, fs->inputs[j].slot);
452
453 ir3_link_add(l, vs->outputs[k].regid,
454 fs->inputs[j].compmask, fs->inputs[j].inloc);
455 }
456 }
457
458 static inline uint32_t
459 ir3_find_output_regid(const struct ir3_shader_variant *so, unsigned slot)
460 {
461 int j;
462 for (j = 0; j < so->outputs_count; j++)
463 if (so->outputs[j].slot == slot)
464 return so->outputs[j].regid;
465 return regid(63, 0);
466 }
467
468 static inline uint32_t
469 ir3_find_sysval_regid(const struct ir3_shader_variant *so, unsigned slot)
470 {
471 int j;
472 for (j = 0; j < so->inputs_count; j++)
473 if (so->inputs[j].sysval && (so->inputs[j].slot == slot))
474 return so->inputs[j].regid;
475 return regid(63, 0);
476 }
477
478 #endif /* IR3_SHADER_H_ */