freedreno/a6xx: Drop the WFI in the program update stateobj.
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_program.c
1 /*
2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 #include "pipe/p_state.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_format.h"
33 #include "util/bitset.h"
34
35 #include "freedreno_program.h"
36
37 #include "fd6_program.h"
38 #include "fd6_emit.h"
39 #include "fd6_texture.h"
40 #include "fd6_format.h"
41
42 static struct ir3_shader *
43 create_shader_stateobj(struct pipe_context *pctx, const struct pipe_shader_state *cso,
44 gl_shader_stage type)
45 {
46 struct fd_context *ctx = fd_context(pctx);
47 struct ir3_compiler *compiler = ctx->screen->compiler;
48 return ir3_shader_create(compiler, cso, type, &ctx->debug, pctx->screen);
49 }
50
51 static void *
52 fd6_fp_state_create(struct pipe_context *pctx,
53 const struct pipe_shader_state *cso)
54 {
55 return create_shader_stateobj(pctx, cso, MESA_SHADER_FRAGMENT);
56 }
57
58 static void
59 fd6_fp_state_delete(struct pipe_context *pctx, void *hwcso)
60 {
61 struct ir3_shader *so = hwcso;
62 struct fd_context *ctx = fd_context(pctx);
63 ir3_cache_invalidate(fd6_context(ctx)->shader_cache, hwcso);
64 ir3_shader_destroy(so);
65 }
66
67 static void *
68 fd6_vp_state_create(struct pipe_context *pctx,
69 const struct pipe_shader_state *cso)
70 {
71 return create_shader_stateobj(pctx, cso, MESA_SHADER_VERTEX);
72 }
73
74 static void
75 fd6_vp_state_delete(struct pipe_context *pctx, void *hwcso)
76 {
77 struct ir3_shader *so = hwcso;
78 struct fd_context *ctx = fd_context(pctx);
79 ir3_cache_invalidate(fd6_context(ctx)->shader_cache, hwcso);
80 ir3_shader_destroy(so);
81 }
82
83 void
84 fd6_emit_shader(struct fd_ringbuffer *ring, const struct ir3_shader_variant *so)
85 {
86 enum a6xx_state_block sb = fd6_stage2shadersb(so->type);
87
88 OUT_PKT7(ring, fd6_stage2opcode(so->type), 3);
89 OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
90 CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
91 CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
92 CP_LOAD_STATE6_0_STATE_BLOCK(sb) |
93 CP_LOAD_STATE6_0_NUM_UNIT(so->instrlen));
94 OUT_RELOCD(ring, so->bo, 0, 0, 0);
95 }
96
97 /* Add any missing varyings needed for stream-out. Otherwise varyings not
98 * used by fragment shader will be stripped out.
99 */
100 static void
101 link_stream_out(struct ir3_shader_linkage *l, const struct ir3_shader_variant *v)
102 {
103 const struct ir3_stream_output_info *strmout = &v->shader->stream_output;
104
105 /*
106 * First, any stream-out varyings not already in linkage map (ie. also
107 * consumed by frag shader) need to be added:
108 */
109 for (unsigned i = 0; i < strmout->num_outputs; i++) {
110 const struct ir3_stream_output *out = &strmout->output[i];
111 unsigned k = out->register_index;
112 unsigned compmask =
113 (1 << (out->num_components + out->start_component)) - 1;
114 unsigned idx, nextloc = 0;
115
116 /* psize/pos need to be the last entries in linkage map, and will
117 * get added link_stream_out, so skip over them:
118 */
119 if ((v->outputs[k].slot == VARYING_SLOT_PSIZ) ||
120 (v->outputs[k].slot == VARYING_SLOT_POS))
121 continue;
122
123 for (idx = 0; idx < l->cnt; idx++) {
124 if (l->var[idx].regid == v->outputs[k].regid)
125 break;
126 nextloc = MAX2(nextloc, l->var[idx].loc + 4);
127 }
128
129 /* add if not already in linkage map: */
130 if (idx == l->cnt)
131 ir3_link_add(l, v->outputs[k].regid, compmask, nextloc);
132
133 /* expand component-mask if needed, ie streaming out all components
134 * but frag shader doesn't consume all components:
135 */
136 if (compmask & ~l->var[idx].compmask) {
137 l->var[idx].compmask |= compmask;
138 l->max_loc = MAX2(l->max_loc,
139 l->var[idx].loc + util_last_bit(l->var[idx].compmask));
140 }
141 }
142 }
143
144 static void
145 setup_stream_out(struct fd6_program_state *state, const struct ir3_shader_variant *v,
146 struct ir3_shader_linkage *l)
147 {
148 const struct ir3_stream_output_info *strmout = &v->shader->stream_output;
149 struct fd6_streamout_state *tf = &state->tf;
150
151 memset(tf, 0, sizeof(*tf));
152
153 tf->prog_count = align(l->max_loc, 2) / 2;
154
155 debug_assert(tf->prog_count < ARRAY_SIZE(tf->prog));
156
157 for (unsigned i = 0; i < strmout->num_outputs; i++) {
158 const struct ir3_stream_output *out = &strmout->output[i];
159 unsigned k = out->register_index;
160 unsigned idx;
161
162 tf->ncomp[out->output_buffer] += out->num_components;
163
164 /* linkage map sorted by order frag shader wants things, so
165 * a bit less ideal here..
166 */
167 for (idx = 0; idx < l->cnt; idx++)
168 if (l->var[idx].regid == v->outputs[k].regid)
169 break;
170
171 debug_assert(idx < l->cnt);
172
173 for (unsigned j = 0; j < out->num_components; j++) {
174 unsigned c = j + out->start_component;
175 unsigned loc = l->var[idx].loc + c;
176 unsigned off = j + out->dst_offset; /* in dwords */
177
178 if (loc & 1) {
179 tf->prog[loc/2] |= A6XX_VPC_SO_PROG_B_EN |
180 A6XX_VPC_SO_PROG_B_BUF(out->output_buffer) |
181 A6XX_VPC_SO_PROG_B_OFF(off * 4);
182 } else {
183 tf->prog[loc/2] |= A6XX_VPC_SO_PROG_A_EN |
184 A6XX_VPC_SO_PROG_A_BUF(out->output_buffer) |
185 A6XX_VPC_SO_PROG_A_OFF(off * 4);
186 }
187 }
188 }
189
190 tf->vpc_so_buf_cntl = A6XX_VPC_SO_BUF_CNTL_ENABLE |
191 COND(tf->ncomp[0] > 0, A6XX_VPC_SO_BUF_CNTL_BUF0) |
192 COND(tf->ncomp[1] > 0, A6XX_VPC_SO_BUF_CNTL_BUF1) |
193 COND(tf->ncomp[2] > 0, A6XX_VPC_SO_BUF_CNTL_BUF2) |
194 COND(tf->ncomp[3] > 0, A6XX_VPC_SO_BUF_CNTL_BUF3);
195 }
196
197 static void
198 setup_config_stateobj(struct fd_ringbuffer *ring, struct fd6_program_state *state)
199 {
200 OUT_PKT4(ring, REG_A6XX_HLSQ_UPDATE_CNTL, 1);
201 OUT_RING(ring, 0xff); /* XXX */
202
203 debug_assert(state->vs->constlen >= state->bs->constlen);
204
205 OUT_PKT4(ring, REG_A6XX_HLSQ_VS_CNTL, 4);
206 OUT_RING(ring, A6XX_HLSQ_VS_CNTL_CONSTLEN(align(state->vs->constlen, 4)) |
207 A6XX_HLSQ_VS_CNTL_ENABLED);
208 OUT_RING(ring, A6XX_HLSQ_HS_CNTL_CONSTLEN(0));
209 OUT_RING(ring, A6XX_HLSQ_DS_CNTL_CONSTLEN(0));
210 OUT_RING(ring, A6XX_HLSQ_GS_CNTL_CONSTLEN(0));
211
212 OUT_PKT4(ring, REG_A6XX_HLSQ_FS_CNTL, 1);
213 OUT_RING(ring, A6XX_HLSQ_FS_CNTL_CONSTLEN(align(state->fs->constlen, 4)) |
214 A6XX_HLSQ_FS_CNTL_ENABLED);
215
216 OUT_PKT4(ring, REG_A6XX_SP_VS_CONFIG, 1);
217 OUT_RING(ring, COND(state->vs, A6XX_SP_VS_CONFIG_ENABLED) |
218 A6XX_SP_VS_CONFIG_NIBO(state->vs->image_mapping.num_ibo) |
219 A6XX_SP_VS_CONFIG_NTEX(state->vs->num_samp) |
220 A6XX_SP_VS_CONFIG_NSAMP(state->vs->num_samp));
221
222 OUT_PKT4(ring, REG_A6XX_SP_FS_CONFIG, 1);
223 OUT_RING(ring, COND(state->fs, A6XX_SP_FS_CONFIG_ENABLED) |
224 A6XX_SP_FS_CONFIG_NIBO(state->fs->image_mapping.num_ibo) |
225 A6XX_SP_FS_CONFIG_NTEX(state->fs->num_samp) |
226 A6XX_SP_FS_CONFIG_NSAMP(state->fs->num_samp));
227
228 OUT_PKT4(ring, REG_A6XX_SP_HS_CONFIG, 1);
229 OUT_RING(ring, COND(false, A6XX_SP_HS_CONFIG_ENABLED));
230
231 OUT_PKT4(ring, REG_A6XX_SP_DS_CONFIG, 1);
232 OUT_RING(ring, COND(false, A6XX_SP_DS_CONFIG_ENABLED));
233
234 OUT_PKT4(ring, REG_A6XX_SP_GS_CONFIG, 1);
235 OUT_RING(ring, COND(false, A6XX_SP_GS_CONFIG_ENABLED));
236
237 OUT_PKT4(ring, REG_A6XX_SP_IBO_COUNT, 1);
238 OUT_RING(ring, state->fs->image_mapping.num_ibo);
239 }
240
241 #define VALIDREG(r) ((r) != regid(63,0))
242 #define CONDREG(r, val) COND(VALIDREG(r), (val))
243
244 static inline uint32_t
245 next_regid(uint32_t reg, uint32_t increment)
246 {
247 if (VALIDREG(reg))
248 return reg + increment;
249 else
250 return regid(63,0);
251 }
252
253 static void
254 setup_stateobj(struct fd_ringbuffer *ring, struct fd6_program_state *state,
255 const struct ir3_shader_key *key, bool binning_pass)
256 {
257 uint32_t pos_regid, psize_regid, color_regid[8], posz_regid;
258 uint32_t face_regid, coord_regid, zwcoord_regid, samp_id_regid;
259 uint32_t smask_in_regid, smask_regid;
260 uint32_t vertex_regid, instance_regid;
261 uint32_t ij_pix_regid, ij_samp_regid, ij_cent_regid, ij_size_regid;
262 enum a3xx_threadsize fssz;
263 uint8_t psize_loc = ~0;
264 int i, j;
265
266 static const struct ir3_shader_variant dummy_fs = {0};
267 const struct ir3_shader_variant *vs = binning_pass ? state->bs : state->vs;
268 const struct ir3_shader_variant *fs = binning_pass ? &dummy_fs : state->fs;
269
270 bool sample_shading = fs->per_samp | key->sample_shading;
271
272 fssz = FOUR_QUADS;
273
274 pos_regid = ir3_find_output_regid(vs, VARYING_SLOT_POS);
275 psize_regid = ir3_find_output_regid(vs, VARYING_SLOT_PSIZ);
276 vertex_regid = ir3_find_sysval_regid(vs, SYSTEM_VALUE_VERTEX_ID);
277 instance_regid = ir3_find_sysval_regid(vs, SYSTEM_VALUE_INSTANCE_ID);
278
279 if (fs->color0_mrt) {
280 color_regid[0] = color_regid[1] = color_regid[2] = color_regid[3] =
281 color_regid[4] = color_regid[5] = color_regid[6] = color_regid[7] =
282 ir3_find_output_regid(fs, FRAG_RESULT_COLOR);
283 } else {
284 color_regid[0] = ir3_find_output_regid(fs, FRAG_RESULT_DATA0);
285 color_regid[1] = ir3_find_output_regid(fs, FRAG_RESULT_DATA1);
286 color_regid[2] = ir3_find_output_regid(fs, FRAG_RESULT_DATA2);
287 color_regid[3] = ir3_find_output_regid(fs, FRAG_RESULT_DATA3);
288 color_regid[4] = ir3_find_output_regid(fs, FRAG_RESULT_DATA4);
289 color_regid[5] = ir3_find_output_regid(fs, FRAG_RESULT_DATA5);
290 color_regid[6] = ir3_find_output_regid(fs, FRAG_RESULT_DATA6);
291 color_regid[7] = ir3_find_output_regid(fs, FRAG_RESULT_DATA7);
292 }
293
294 samp_id_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_SAMPLE_ID);
295 smask_in_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_SAMPLE_MASK_IN);
296 face_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_FRONT_FACE);
297 coord_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_FRAG_COORD);
298 zwcoord_regid = next_regid(coord_regid, 2);
299 ij_pix_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_BARYCENTRIC_PIXEL);
300 ij_samp_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_BARYCENTRIC_SAMPLE);
301 ij_cent_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_BARYCENTRIC_CENTROID);
302 ij_size_regid = ir3_find_sysval_regid(fs, SYSTEM_VALUE_BARYCENTRIC_SIZE);
303 posz_regid = ir3_find_output_regid(fs, FRAG_RESULT_DEPTH);
304 smask_regid = ir3_find_output_regid(fs, FRAG_RESULT_SAMPLE_MASK);
305
306 /* we can't write gl_SampleMask for !msaa.. if b0 is zero then we
307 * end up masking the single sample!!
308 */
309 if (!key->msaa)
310 smask_regid = regid(63, 0);
311
312 /* we could probably divide this up into things that need to be
313 * emitted if frag-prog is dirty vs if vert-prog is dirty..
314 */
315
316 OUT_PKT4(ring, REG_A6XX_SP_VS_INSTRLEN, 1);
317 OUT_RING(ring, vs->instrlen); /* SP_VS_INSTRLEN */
318
319 OUT_PKT4(ring, REG_A6XX_SP_HS_UNKNOWN_A831, 1);
320 OUT_RING(ring, 0);
321
322 OUT_PKT4(ring, REG_A6XX_SP_HS_INSTRLEN, 1);
323 OUT_RING(ring, 0); /* SP_HS_INSTRLEN */
324
325 OUT_PKT4(ring, REG_A6XX_SP_DS_INSTRLEN, 1);
326 OUT_RING(ring, 0); /* SP_DS_INSTRLEN */
327
328 OUT_PKT4(ring, REG_A6XX_SP_GS_UNKNOWN_A871, 1);
329 OUT_RING(ring, 0);
330
331 OUT_PKT4(ring, REG_A6XX_SP_GS_INSTRLEN, 1);
332 OUT_RING(ring, 0); /* SP_GS_INSTRLEN */
333
334 /* I believe this is related to pre-dispatch texture fetch.. we probably
335 * should't turn it on by accident:
336 */
337 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_A99E, 1);
338 OUT_RING(ring, 0x0);
339
340 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_A9A8, 1);
341 OUT_RING(ring, 0);
342
343 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_AB00, 1);
344 OUT_RING(ring, 0x5);
345
346 OUT_PKT4(ring, REG_A6XX_SP_FS_INSTRLEN, 1);
347 OUT_RING(ring, fs->instrlen); /* SP_FS_INSTRLEN */
348
349 OUT_PKT4(ring, REG_A6XX_SP_FS_OUTPUT_CNTL0, 1);
350 OUT_RING(ring, A6XX_SP_FS_OUTPUT_CNTL0_DEPTH_REGID(posz_regid) |
351 A6XX_SP_FS_OUTPUT_CNTL0_SAMPMASK_REGID(smask_regid) |
352 0xfc000000);
353
354 OUT_PKT4(ring, REG_A6XX_SP_VS_CTRL_REG0, 1);
355 OUT_RING(ring, A6XX_SP_VS_CTRL_REG0_THREADSIZE(fssz) |
356 A6XX_SP_VS_CTRL_REG0_FULLREGFOOTPRINT(vs->info.max_reg + 1) |
357 A6XX_SP_VS_CTRL_REG0_MERGEDREGS |
358 A6XX_SP_VS_CTRL_REG0_BRANCHSTACK(vs->branchstack) |
359 COND(vs->need_pixlod, A6XX_SP_VS_CTRL_REG0_PIXLODENABLE));
360
361 struct ir3_shader_linkage l = {0};
362 ir3_link_shaders(&l, vs, fs);
363
364 if ((vs->shader->stream_output.num_outputs > 0) && !binning_pass)
365 link_stream_out(&l, vs);
366
367 BITSET_DECLARE(varbs, 128) = {0};
368 uint32_t *varmask = (uint32_t *)varbs;
369
370 for (i = 0; i < l.cnt; i++)
371 for (j = 0; j < util_last_bit(l.var[i].compmask); j++)
372 BITSET_SET(varbs, l.var[i].loc + j);
373
374 OUT_PKT4(ring, REG_A6XX_VPC_VAR_DISABLE(0), 4);
375 OUT_RING(ring, ~varmask[0]); /* VPC_VAR[0].DISABLE */
376 OUT_RING(ring, ~varmask[1]); /* VPC_VAR[1].DISABLE */
377 OUT_RING(ring, ~varmask[2]); /* VPC_VAR[2].DISABLE */
378 OUT_RING(ring, ~varmask[3]); /* VPC_VAR[3].DISABLE */
379
380 /* a6xx appends pos/psize to end of the linkage map: */
381 if (VALIDREG(pos_regid))
382 ir3_link_add(&l, pos_regid, 0xf, l.max_loc);
383
384 if (VALIDREG(psize_regid)) {
385 psize_loc = l.max_loc;
386 ir3_link_add(&l, psize_regid, 0x1, l.max_loc);
387 }
388
389 if ((vs->shader->stream_output.num_outputs > 0) && !binning_pass) {
390 setup_stream_out(state, vs, &l);
391 }
392
393 for (i = 0, j = 0; (i < 16) && (j < l.cnt); i++) {
394 uint32_t reg = 0;
395
396 OUT_PKT4(ring, REG_A6XX_SP_VS_OUT_REG(i), 1);
397
398 reg |= A6XX_SP_VS_OUT_REG_A_REGID(l.var[j].regid);
399 reg |= A6XX_SP_VS_OUT_REG_A_COMPMASK(l.var[j].compmask);
400 j++;
401
402 reg |= A6XX_SP_VS_OUT_REG_B_REGID(l.var[j].regid);
403 reg |= A6XX_SP_VS_OUT_REG_B_COMPMASK(l.var[j].compmask);
404 j++;
405
406 OUT_RING(ring, reg);
407 }
408
409 for (i = 0, j = 0; (i < 8) && (j < l.cnt); i++) {
410 uint32_t reg = 0;
411
412 OUT_PKT4(ring, REG_A6XX_SP_VS_VPC_DST_REG(i), 1);
413
414 reg |= A6XX_SP_VS_VPC_DST_REG_OUTLOC0(l.var[j++].loc);
415 reg |= A6XX_SP_VS_VPC_DST_REG_OUTLOC1(l.var[j++].loc);
416 reg |= A6XX_SP_VS_VPC_DST_REG_OUTLOC2(l.var[j++].loc);
417 reg |= A6XX_SP_VS_VPC_DST_REG_OUTLOC3(l.var[j++].loc);
418
419 OUT_RING(ring, reg);
420 }
421
422 OUT_PKT4(ring, REG_A6XX_SP_VS_OBJ_START_LO, 2);
423 OUT_RELOC(ring, vs->bo, 0, 0, 0); /* SP_VS_OBJ_START_LO/HI */
424
425 if (vs->instrlen)
426 fd6_emit_shader(ring, vs);
427
428
429 OUT_PKT4(ring, REG_A6XX_SP_PRIMITIVE_CNTL, 1);
430 OUT_RING(ring, A6XX_SP_PRIMITIVE_CNTL_VSOUT(l.cnt));
431
432 bool enable_varyings = fs->total_in > 0;
433
434 OUT_PKT4(ring, REG_A6XX_VPC_CNTL_0, 1);
435 OUT_RING(ring, A6XX_VPC_CNTL_0_NUMNONPOSVAR(fs->total_in) |
436 COND(enable_varyings, A6XX_VPC_CNTL_0_VARYING) |
437 0xff00ff00);
438
439 OUT_PKT4(ring, REG_A6XX_PC_PRIMITIVE_CNTL_1, 1);
440 OUT_RING(ring, A6XX_PC_PRIMITIVE_CNTL_1_STRIDE_IN_VPC(l.max_loc) |
441 CONDREG(psize_regid, 0x100));
442
443 if (binning_pass) {
444 OUT_PKT4(ring, REG_A6XX_SP_FS_OBJ_START_LO, 2);
445 OUT_RING(ring, 0x00000000); /* SP_FS_OBJ_START_LO */
446 OUT_RING(ring, 0x00000000); /* SP_FS_OBJ_START_HI */
447 } else {
448 OUT_PKT4(ring, REG_A6XX_SP_FS_OBJ_START_LO, 2);
449 OUT_RELOC(ring, fs->bo, 0, 0, 0); /* SP_FS_OBJ_START_LO/HI */
450 }
451
452 OUT_PKT4(ring, REG_A6XX_HLSQ_CONTROL_1_REG, 5);
453 OUT_RING(ring, 0x7); /* XXX */
454 OUT_RING(ring, A6XX_HLSQ_CONTROL_2_REG_FACEREGID(face_regid) |
455 A6XX_HLSQ_CONTROL_2_REG_SAMPLEID(samp_id_regid) |
456 A6XX_HLSQ_CONTROL_2_REG_SAMPLEMASK(smask_in_regid) |
457 A6XX_HLSQ_CONTROL_2_REG_SIZE(ij_size_regid));
458 OUT_RING(ring, A6XX_HLSQ_CONTROL_3_REG_BARY_IJ_PIXEL(ij_pix_regid) |
459 A6XX_HLSQ_CONTROL_3_REG_BARY_IJ_CENTROID(ij_cent_regid) |
460 0xfc00fc00); /* XXX */
461 OUT_RING(ring, A6XX_HLSQ_CONTROL_4_REG_XYCOORDREGID(coord_regid) |
462 A6XX_HLSQ_CONTROL_4_REG_ZWCOORDREGID(zwcoord_regid) |
463 A6XX_HLSQ_CONTROL_4_REG_BARY_IJ_PIXEL_PERSAMP(ij_samp_regid) |
464 0x0000fc00); /* XXX */
465 OUT_RING(ring, 0xfc); /* XXX */
466
467 OUT_PKT4(ring, REG_A6XX_HLSQ_UNKNOWN_B980, 1);
468 OUT_RING(ring, enable_varyings ? 3 : 1);
469
470 OUT_PKT4(ring, REG_A6XX_SP_FS_CTRL_REG0, 1);
471 OUT_RING(ring, A6XX_SP_FS_CTRL_REG0_THREADSIZE(fssz) |
472 COND(enable_varyings, A6XX_SP_FS_CTRL_REG0_VARYING) |
473 COND(fs->frag_coord, A6XX_SP_FS_CTRL_REG0_VARYING) |
474 0x1000000 |
475 A6XX_SP_FS_CTRL_REG0_FULLREGFOOTPRINT(fs->info.max_reg + 1) |
476 A6XX_SP_FS_CTRL_REG0_MERGEDREGS |
477 A6XX_SP_FS_CTRL_REG0_BRANCHSTACK(fs->branchstack) |
478 COND(fs->need_pixlod, A6XX_SP_FS_CTRL_REG0_PIXLODENABLE));
479
480 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_A982, 1);
481 OUT_RING(ring, 0); /* XXX */
482
483 OUT_PKT4(ring, REG_A6XX_VPC_GS_SIV_CNTL, 1);
484 OUT_RING(ring, 0x0000ffff); /* XXX */
485
486 OUT_PKT4(ring, REG_A6XX_GRAS_CNTL, 1);
487 OUT_RING(ring,
488 CONDREG(ij_pix_regid, A6XX_GRAS_CNTL_VARYING) |
489 CONDREG(ij_cent_regid, A6XX_GRAS_CNTL_CENTROID) |
490 CONDREG(ij_samp_regid, A6XX_GRAS_CNTL_PERSAMP_VARYING) |
491 COND(VALIDREG(ij_size_regid) && !sample_shading, A6XX_GRAS_CNTL_SIZE) |
492 COND(VALIDREG(ij_size_regid) && sample_shading, A6XX_GRAS_CNTL_SIZE_PERSAMP) |
493 COND(fs->frag_coord,
494 A6XX_GRAS_CNTL_SIZE |
495 A6XX_GRAS_CNTL_XCOORD |
496 A6XX_GRAS_CNTL_YCOORD |
497 A6XX_GRAS_CNTL_ZCOORD |
498 A6XX_GRAS_CNTL_WCOORD) |
499 COND(fs->frag_face, A6XX_GRAS_CNTL_SIZE));
500
501 OUT_PKT4(ring, REG_A6XX_RB_RENDER_CONTROL0, 2);
502 OUT_RING(ring,
503 CONDREG(ij_pix_regid, A6XX_RB_RENDER_CONTROL0_VARYING) |
504 CONDREG(ij_cent_regid, A6XX_RB_RENDER_CONTROL0_CENTROID) |
505 CONDREG(ij_samp_regid, A6XX_RB_RENDER_CONTROL0_PERSAMP_VARYING) |
506 COND(enable_varyings, A6XX_RB_RENDER_CONTROL0_UNK10) |
507 COND(VALIDREG(ij_size_regid) && !sample_shading, A6XX_RB_RENDER_CONTROL0_SIZE) |
508 COND(VALIDREG(ij_size_regid) && sample_shading, A6XX_RB_RENDER_CONTROL0_SIZE_PERSAMP) |
509 COND(fs->frag_coord,
510 A6XX_RB_RENDER_CONTROL0_SIZE |
511 A6XX_RB_RENDER_CONTROL0_XCOORD |
512 A6XX_RB_RENDER_CONTROL0_YCOORD |
513 A6XX_RB_RENDER_CONTROL0_ZCOORD |
514 A6XX_RB_RENDER_CONTROL0_WCOORD) |
515 COND(fs->frag_face, A6XX_RB_RENDER_CONTROL0_SIZE));
516
517 OUT_RING(ring,
518 CONDREG(smask_in_regid, A6XX_RB_RENDER_CONTROL1_SAMPLEMASK) |
519 CONDREG(samp_id_regid, A6XX_RB_RENDER_CONTROL1_SAMPLEID) |
520 CONDREG(ij_size_regid, A6XX_RB_RENDER_CONTROL1_SIZE) |
521 COND(fs->frag_face, A6XX_RB_RENDER_CONTROL1_FACENESS));
522
523 OUT_PKT4(ring, REG_A6XX_RB_SAMPLE_CNTL, 1);
524 OUT_RING(ring, COND(sample_shading, A6XX_RB_SAMPLE_CNTL_PER_SAMP_MODE));
525
526 OUT_PKT4(ring, REG_A6XX_GRAS_UNKNOWN_8101, 1);
527 OUT_RING(ring, COND(sample_shading, 0x6)); // XXX
528
529 OUT_PKT4(ring, REG_A6XX_GRAS_SAMPLE_CNTL, 1);
530 OUT_RING(ring, COND(sample_shading, A6XX_GRAS_SAMPLE_CNTL_PER_SAMP_MODE));
531
532 OUT_PKT4(ring, REG_A6XX_SP_FS_OUTPUT_REG(0), 8);
533 for (i = 0; i < 8; i++) {
534 OUT_RING(ring, A6XX_SP_FS_OUTPUT_REG_REGID(color_regid[i]) |
535 COND(color_regid[i] & HALF_REG_ID, A6XX_SP_FS_OUTPUT_REG_HALF_PRECISION));
536 }
537
538 OUT_PKT4(ring, REG_A6XX_VPC_PACK, 1);
539 OUT_RING(ring, A6XX_VPC_PACK_NUMNONPOSVAR(fs->total_in) |
540 A6XX_VPC_PACK_PSIZELOC(psize_loc) |
541 A6XX_VPC_PACK_STRIDE_IN_VPC(l.max_loc));
542
543 if (!binning_pass) {
544 /* figure out VARYING_INTERP / VARYING_PS_REPL register values: */
545 for (j = -1; (j = ir3_next_varying(fs, j)) < (int)fs->inputs_count; ) {
546 /* NOTE: varyings are packed, so if compmask is 0xb
547 * then first, third, and fourth component occupy
548 * three consecutive varying slots:
549 */
550 unsigned compmask = fs->inputs[j].compmask;
551
552 uint32_t inloc = fs->inputs[j].inloc;
553
554 if (fs->inputs[j].interpolate == INTERP_MODE_FLAT) {
555 uint32_t loc = inloc;
556
557 for (i = 0; i < 4; i++) {
558 if (compmask & (1 << i)) {
559 state->vinterp[loc / 16] |= 1 << ((loc % 16) * 2);
560 loc++;
561 }
562 }
563 }
564 }
565 }
566
567 if (!binning_pass)
568 if (fs->instrlen)
569 fd6_emit_shader(ring, fs);
570
571 OUT_PKT4(ring, REG_A6XX_VFD_CONTROL_1, 6);
572 OUT_RING(ring, A6XX_VFD_CONTROL_1_REGID4VTX(vertex_regid) |
573 A6XX_VFD_CONTROL_1_REGID4INST(instance_regid) |
574 0xfcfc0000);
575 OUT_RING(ring, 0x0000fcfc); /* VFD_CONTROL_2 */
576 OUT_RING(ring, 0xfcfcfcfc); /* VFD_CONTROL_3 */
577 OUT_RING(ring, 0x000000fc); /* VFD_CONTROL_4 */
578 OUT_RING(ring, 0x0000fcfc); /* VFD_CONTROL_5 */
579 OUT_RING(ring, 0x00000000); /* VFD_CONTROL_6 */
580
581 bool fragz = fs->no_earlyz | fs->writes_pos;
582
583 OUT_PKT4(ring, REG_A6XX_RB_DEPTH_PLANE_CNTL, 1);
584 OUT_RING(ring, COND(fragz, A6XX_RB_DEPTH_PLANE_CNTL_FRAG_WRITES_Z));
585
586 OUT_PKT4(ring, REG_A6XX_GRAS_SU_DEPTH_PLANE_CNTL, 1);
587 OUT_RING(ring, COND(fragz, A6XX_GRAS_SU_DEPTH_PLANE_CNTL_FRAG_WRITES_Z));
588 }
589
590 /* emits the program state which is not part of the stateobj because of
591 * dependency on other gl state (rasterflat or sprite-coord-replacement)
592 */
593 void
594 fd6_program_emit(struct fd_ringbuffer *ring, struct fd6_emit *emit)
595 {
596 const struct fd6_program_state *state = fd6_emit_get_prog(emit);
597
598 if (!unlikely(emit->rasterflat || emit->sprite_coord_enable)) {
599 /* fastpath: */
600 OUT_PKT4(ring, REG_A6XX_VPC_VARYING_INTERP_MODE(0), 8);
601 for (int i = 0; i < 8; i++)
602 OUT_RING(ring, state->vinterp[i]); /* VPC_VARYING_INTERP[i].MODE */
603
604 OUT_PKT4(ring, REG_A6XX_VPC_VARYING_PS_REPL_MODE(0), 8);
605 for (int i = 0; i < 8; i++)
606 OUT_RING(ring, 0x00000000); /* VPC_VARYING_PS_REPL[i] */
607 } else {
608 /* slow-path: */
609 struct ir3_shader_variant *fs = state->fs;
610 uint32_t vinterp[8], vpsrepl[8];
611
612 memset(vinterp, 0, sizeof(vinterp));
613 memset(vpsrepl, 0, sizeof(vpsrepl));
614
615 for (int j = -1; (j = ir3_next_varying(fs, j)) < (int)fs->inputs_count; ) {
616
617 /* NOTE: varyings are packed, so if compmask is 0xb
618 * then first, third, and fourth component occupy
619 * three consecutive varying slots:
620 */
621 unsigned compmask = fs->inputs[j].compmask;
622
623 uint32_t inloc = fs->inputs[j].inloc;
624
625 if ((fs->inputs[j].interpolate == INTERP_MODE_FLAT) ||
626 (fs->inputs[j].rasterflat && emit->rasterflat)) {
627 uint32_t loc = inloc;
628
629 for (int i = 0; i < 4; i++) {
630 if (compmask & (1 << i)) {
631 vinterp[loc / 16] |= 1 << ((loc % 16) * 2);
632 loc++;
633 }
634 }
635 }
636
637 gl_varying_slot slot = fs->inputs[j].slot;
638
639 /* since we don't enable PIPE_CAP_TGSI_TEXCOORD: */
640 if (slot >= VARYING_SLOT_VAR0) {
641 unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
642 /* Replace the .xy coordinates with S/T from the point sprite. Set
643 * interpolation bits for .zw such that they become .01
644 */
645 if (emit->sprite_coord_enable & texmask) {
646 /* mask is two 2-bit fields, where:
647 * '01' -> S
648 * '10' -> T
649 * '11' -> 1 - T (flip mode)
650 */
651 unsigned mask = emit->sprite_coord_mode ? 0b1101 : 0b1001;
652 uint32_t loc = inloc;
653 if (compmask & 0x1) {
654 vpsrepl[loc / 16] |= ((mask >> 0) & 0x3) << ((loc % 16) * 2);
655 loc++;
656 }
657 if (compmask & 0x2) {
658 vpsrepl[loc / 16] |= ((mask >> 2) & 0x3) << ((loc % 16) * 2);
659 loc++;
660 }
661 if (compmask & 0x4) {
662 /* .z <- 0.0f */
663 vinterp[loc / 16] |= 0b10 << ((loc % 16) * 2);
664 loc++;
665 }
666 if (compmask & 0x8) {
667 /* .w <- 1.0f */
668 vinterp[loc / 16] |= 0b11 << ((loc % 16) * 2);
669 loc++;
670 }
671 }
672 }
673 }
674
675 OUT_PKT4(ring, REG_A6XX_VPC_VARYING_INTERP_MODE(0), 8);
676 for (int i = 0; i < 8; i++)
677 OUT_RING(ring, vinterp[i]); /* VPC_VARYING_INTERP[i].MODE */
678
679 OUT_PKT4(ring, REG_A6XX_VPC_VARYING_PS_REPL_MODE(0), 8);
680 for (int i = 0; i < 8; i++)
681 OUT_RING(ring, vpsrepl[i]); /* VPC_VARYING_PS_REPL[i] */
682 }
683 }
684
685 static struct ir3_program_state *
686 fd6_program_create(void *data, struct ir3_shader_variant *bs,
687 struct ir3_shader_variant *vs,
688 struct ir3_shader_variant *fs,
689 const struct ir3_shader_key *key)
690 {
691 struct fd_context *ctx = data;
692 struct fd6_program_state *state = CALLOC_STRUCT(fd6_program_state);
693
694 state->bs = bs;
695 state->vs = vs;
696 state->fs = fs;
697 state->config_stateobj = fd_ringbuffer_new_object(ctx->pipe, 0x1000);
698 state->binning_stateobj = fd_ringbuffer_new_object(ctx->pipe, 0x1000);
699 state->stateobj = fd_ringbuffer_new_object(ctx->pipe, 0x1000);
700
701 setup_config_stateobj(state->config_stateobj, state);
702 setup_stateobj(state->binning_stateobj, state, key, true);
703 setup_stateobj(state->stateobj, state, key, false);
704
705 return &state->base;
706 }
707
708 static void
709 fd6_program_destroy(void *data, struct ir3_program_state *state)
710 {
711 struct fd6_program_state *so = fd6_program_state(state);
712 fd_ringbuffer_del(so->stateobj);
713 fd_ringbuffer_del(so->binning_stateobj);
714 fd_ringbuffer_del(so->config_stateobj);
715 free(so);
716 }
717
718 static const struct ir3_cache_funcs cache_funcs = {
719 .create_state = fd6_program_create,
720 .destroy_state = fd6_program_destroy,
721 };
722
723 void
724 fd6_prog_init(struct pipe_context *pctx)
725 {
726 struct fd_context *ctx = fd_context(pctx);
727
728 fd6_context(ctx)->shader_cache = ir3_cache_create(&cache_funcs, ctx);
729
730 pctx->create_fs_state = fd6_fp_state_create;
731 pctx->delete_fs_state = fd6_fp_state_delete;
732
733 pctx->create_vs_state = fd6_vp_state_create;
734 pctx->delete_vs_state = fd6_vp_state_delete;
735
736 fd_prog_init(pctx);
737 }