freedreno/a4xx: better workaround for astc+srgb
[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
35 #include "ir3.h"
36 #include "disasm.h"
37
38 /* driver param indices: */
39 enum ir3_driver_param {
40 IR3_DP_VTXID_BASE = 0,
41 IR3_DP_VTXCNT_MAX = 1,
42 /* user-clip-plane components, up to 8x vec4's: */
43 IR3_DP_UCP0_X = 4,
44 /* .... */
45 IR3_DP_UCP7_W = 35,
46 IR3_DP_COUNT = 36 /* must be aligned to vec4 */
47 };
48
49 /* Layout of constant registers:
50 *
51 * num_uniform * vec4 - user consts
52 * 4 * vec4 - UBO addresses
53 * if (vertex shader) {
54 * N * vec4 - driver params (IR3_DP_*)
55 * 1 * vec4 - stream-out addresses
56 * }
57 *
58 * TODO this could be made more dynamic, to at least skip sections
59 * that we don't need..
60 */
61 #define IR3_UBOS_OFF 0 /* UBOs after user consts */
62 #define IR3_DRIVER_PARAM_OFF 4 /* driver params after UBOs */
63 #define IR3_TFBOS_OFF (IR3_DRIVER_PARAM_OFF + IR3_DP_COUNT/4)
64
65 /* Configuration key used to identify a shader variant.. different
66 * shader variants can be used to implement features not supported
67 * in hw (two sided color), binning-pass vertex shader, etc.
68 */
69 struct ir3_shader_key {
70 union {
71 struct {
72 /*
73 * Combined Vertex/Fragment shader parameters:
74 */
75 unsigned ucp_enables : 8;
76
77 /* do we need to check {v,f}saturate_{s,t,r}? */
78 unsigned has_per_samp : 1;
79
80 /*
81 * Vertex shader variant parameters:
82 */
83 unsigned binning_pass : 1;
84
85 /*
86 * Fragment shader variant parameters:
87 */
88 unsigned color_two_side : 1;
89 unsigned half_precision : 1;
90 /* used when shader needs to handle flat varyings (a4xx)
91 * for front/back color inputs to frag shader:
92 */
93 unsigned rasterflat : 1;
94 };
95 uint32_t global;
96 };
97
98 /* bitmask of sampler which needs coords clamped for vertex
99 * shader:
100 */
101 uint16_t vsaturate_s, vsaturate_t, vsaturate_r;
102
103 /* bitmask of sampler which needs coords clamped for frag
104 * shader:
105 */
106 uint16_t fsaturate_s, fsaturate_t, fsaturate_r;
107
108 /* bitmask of samplers which need astc srgb workaround: */
109 uint16_t vastc_srgb, fastc_srgb;
110 };
111
112 static inline bool
113 ir3_shader_key_equal(struct ir3_shader_key *a, struct ir3_shader_key *b)
114 {
115 /* slow-path if we need to check {v,f}saturate_{s,t,r} */
116 if (a->has_per_samp || b->has_per_samp)
117 return memcmp(a, b, sizeof(struct ir3_shader_key)) == 0;
118 return a->global == b->global;
119 }
120
121 struct ir3_shader_variant {
122 struct fd_bo *bo;
123
124 /* variant id (for debug) */
125 uint32_t id;
126
127 struct ir3_shader_key key;
128
129 struct ir3_info info;
130 struct ir3 *ir;
131
132 /* the instructions length is in units of instruction groups
133 * (4 instructions for a3xx, 16 instructions for a4xx.. each
134 * instruction is 2 dwords):
135 */
136 unsigned instrlen;
137
138 /* the constants length is in units of vec4's, and is the sum of
139 * the uniforms and the built-in compiler constants
140 */
141 unsigned constlen;
142
143 /* About Linkage:
144 * + Let the frag shader determine the position/compmask for the
145 * varyings, since it is the place where we know if the varying
146 * is actually used, and if so, which components are used. So
147 * what the hw calls "outloc" is taken from the "inloc" of the
148 * frag shader.
149 * + From the vert shader, we only need the output regid
150 */
151
152 /* for frag shader, pos_regid holds the frag_pos, ie. what is passed
153 * to bary.f instructions
154 */
155 uint8_t pos_regid;
156 bool frag_coord, frag_face, color0_mrt;
157
158 /* NOTE: for input/outputs, slot is:
159 * gl_vert_attrib - for VS inputs
160 * gl_varying_slot - for VS output / FS input
161 * gl_frag_result - for FS output
162 */
163
164 /* varyings/outputs: */
165 unsigned outputs_count;
166 struct {
167 uint8_t slot;
168 uint8_t regid;
169 } outputs[16 + 2]; /* +POSITION +PSIZE */
170 bool writes_pos, writes_psize;
171
172 /* attributes (VS) / varyings (FS):
173 * Note that sysval's should come *after* normal inputs.
174 */
175 unsigned inputs_count;
176 struct {
177 uint8_t slot;
178 uint8_t regid;
179 uint8_t compmask;
180 uint8_t ncomp;
181 /* In theory inloc of fs should match outloc of vs. Or
182 * rather the outloc of the vs is 8 plus the offset passed
183 * to bary.f. Presumably that +8 is to account for
184 * gl_Position/gl_PointSize?
185 *
186 * NOTE inloc is currently aligned to 4 (we don't try
187 * to pack varyings). Changing this would likely break
188 * assumptions in few places (like setting up of flat
189 * shading in fd3_program) so be sure to check all the
190 * spots where inloc is used.
191 */
192 uint8_t inloc;
193 /* vertex shader specific: */
194 bool sysval : 1; /* slot is a gl_system_value */
195 /* fragment shader specific: */
196 bool bary : 1; /* fetched varying (vs one loaded into reg) */
197 bool rasterflat : 1; /* special handling for emit->rasterflat */
198 enum glsl_interp_qualifier interpolate;
199 } inputs[16 + 2]; /* +POSITION +FACE */
200
201 /* sum of input components (scalar). For frag shaders, it only counts
202 * the varying inputs:
203 */
204 unsigned total_in;
205
206 /* For frag shaders, the total number of inputs (not scalar,
207 * ie. SP_VS_PARAM_REG.TOTALVSOUTVAR)
208 */
209 unsigned varying_in;
210
211 /* do we have one or more texture sample instructions: */
212 bool has_samp;
213
214 /* do we have kill instructions: */
215 bool has_kill;
216
217 /* const reg # of first immediate, ie. 1 == c1
218 * (not regid, because TGSI thinks in terms of vec4 registers,
219 * not scalar registers)
220 */
221 unsigned first_driver_param;
222 unsigned first_immediate;
223 unsigned immediates_count;
224 struct {
225 uint32_t val[4];
226 } immediates[64];
227
228 /* for astc srgb workaround, the number/base of additional
229 * alpha tex states we need, and index of original tex states
230 */
231 struct {
232 unsigned base, count;
233 unsigned orig_idx[16];
234 } astc_srgb;
235
236 /* shader variants form a linked list: */
237 struct ir3_shader_variant *next;
238
239 /* replicated here to avoid passing extra ptrs everywhere: */
240 enum shader_t type;
241 struct ir3_shader *shader;
242 };
243
244 typedef struct nir_shader nir_shader;
245
246 struct ir3_shader {
247 enum shader_t type;
248
249 /* shader id (for debug): */
250 uint32_t id;
251 uint32_t variant_count;
252
253 struct ir3_compiler *compiler;
254
255 nir_shader *nir;
256 struct pipe_stream_output_info stream_output;
257
258 struct ir3_shader_variant *variants;
259 };
260
261 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id);
262
263 struct ir3_shader * ir3_shader_create(struct ir3_compiler *compiler,
264 const struct pipe_shader_state *cso, enum shader_t type);
265 void ir3_shader_destroy(struct ir3_shader *shader);
266 struct ir3_shader_variant * ir3_shader_variant(struct ir3_shader *shader,
267 struct ir3_shader_key key);
268 void ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin);
269
270 struct fd_ringbuffer;
271 struct fd_context;
272 void ir3_emit_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
273 struct fd_context *ctx, const struct pipe_draw_info *info, uint32_t dirty);
274
275 static inline const char *
276 ir3_shader_stage(struct ir3_shader *shader)
277 {
278 switch (shader->type) {
279 case SHADER_VERTEX: return "VERT";
280 case SHADER_FRAGMENT: return "FRAG";
281 case SHADER_COMPUTE: return "CL";
282 default:
283 unreachable("invalid type");
284 return NULL;
285 }
286 }
287
288 /*
289 * Helper/util:
290 */
291
292 #include "pipe/p_shader_tokens.h"
293
294 static inline int
295 ir3_find_output(const struct ir3_shader_variant *so, gl_varying_slot slot)
296 {
297 int j;
298
299 for (j = 0; j < so->outputs_count; j++)
300 if (so->outputs[j].slot == slot)
301 return j;
302
303 /* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
304 * in the vertex shader.. but the fragment shader doesn't know this
305 * so it will always have both IN.COLOR[n] and IN.BCOLOR[n]. So
306 * at link time if there is no matching OUT.BCOLOR[n], we must map
307 * OUT.COLOR[n] to IN.BCOLOR[n]. And visa versa if there is only
308 * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
309 */
310 if (slot == VARYING_SLOT_BFC0) {
311 slot = VARYING_SLOT_COL0;
312 } else if (slot == VARYING_SLOT_BFC1) {
313 slot = VARYING_SLOT_COL1;
314 } else if (slot == VARYING_SLOT_COL0) {
315 slot = VARYING_SLOT_BFC0;
316 } else if (slot == VARYING_SLOT_COL1) {
317 slot = VARYING_SLOT_BFC1;
318 } else {
319 return 0;
320 }
321
322 for (j = 0; j < so->outputs_count; j++)
323 if (so->outputs[j].slot == slot)
324 return j;
325
326 debug_assert(0);
327
328 return 0;
329 }
330
331 static inline int
332 ir3_next_varying(const struct ir3_shader_variant *so, int i)
333 {
334 while (++i < so->inputs_count)
335 if (so->inputs[i].compmask && so->inputs[i].bary)
336 break;
337 return i;
338 }
339
340 static inline uint32_t
341 ir3_find_output_regid(const struct ir3_shader_variant *so, unsigned slot)
342 {
343 int j;
344 for (j = 0; j < so->outputs_count; j++)
345 if (so->outputs[j].slot == slot)
346 return so->outputs[j].regid;
347 return regid(63, 0);
348 }
349
350 #endif /* IR3_SHADER_H_ */