freedreno/a3xx+a4xx: fix for stk binning pass hang
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_program.c
index 64c9668129bfb49513e70c5617459075cf31677c..4ed04b38dea72efcc7cb65893e8755cb991b6574 100644 (file)
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
 #include "util/u_format.h"
-#include "tgsi/tgsi_dump.h"
-#include "tgsi/tgsi_parse.h"
 
-#include "freedreno_lowering.h"
 #include "freedreno_program.h"
 
 #include "fd3_program.h"
 #include "fd3_emit.h"
 #include "fd3_texture.h"
-#include "fd3_util.h"
+#include "fd3_format.h"
 
 static void
 delete_shader_stateobj(struct fd3_shader_stateobj *so)
@@ -54,7 +51,7 @@ create_shader_stateobj(struct pipe_context *pctx, const struct pipe_shader_state
                enum shader_t type)
 {
        struct fd3_shader_stateobj *so = CALLOC_STRUCT(fd3_shader_stateobj);
-       so->shader = ir3_shader_create(pctx, cso->tokens, type);
+       so->shader = ir3_shader_create(pctx, cso, type);
        return so;
 }
 
@@ -127,79 +124,28 @@ emit_shader(struct fd_ringbuffer *ring, const struct ir3_shader_variant *so)
        }
 }
 
-static int
-find_output(const struct ir3_shader_variant *so, ir3_semantic semantic)
-{
-       int j;
-
-       for (j = 0; j < so->outputs_count; j++)
-               if (so->outputs[j].semantic == semantic)
-                       return j;
-
-       /* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
-        * in the vertex shader.. but the fragment shader doesn't know this
-        * so  it will always have both IN.COLOR[n] and IN.BCOLOR[n].  So
-        * at link time if there is no matching OUT.BCOLOR[n], we must map
-        * OUT.COLOR[n] to IN.BCOLOR[n].  And visa versa if there is only
-        * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
-        */
-       if (sem2name(semantic) == TGSI_SEMANTIC_BCOLOR) {
-               unsigned idx = sem2idx(semantic);
-               semantic = ir3_semantic_name(TGSI_SEMANTIC_COLOR, idx);
-       } else if (sem2name(semantic) == TGSI_SEMANTIC_COLOR) {
-               unsigned idx = sem2idx(semantic);
-               semantic = ir3_semantic_name(TGSI_SEMANTIC_BCOLOR, idx);
-       }
-
-       for (j = 0; j < so->outputs_count; j++)
-               if (so->outputs[j].semantic == semantic)
-                       return j;
-
-       debug_assert(0);
-
-       return 0;
-}
-
-static int
-next_varying(const struct ir3_shader_variant *so, int i)
-{
-       while (++i < so->inputs_count)
-               if (so->inputs[i].compmask && so->inputs[i].bary)
-                       break;
-       return i;
-}
-
-static uint32_t
-find_output_regid(const struct ir3_shader_variant *so, ir3_semantic semantic)
-{
-       int j;
-       for (j = 0; j < so->outputs_count; j++)
-               if (so->outputs[j].semantic == semantic)
-                       return so->outputs[j].regid;
-       return regid(63, 0);
-}
-
 void
-fd3_program_emit(struct fd_ringbuffer *ring,
-               struct fd_program_stateobj *prog,
-               struct ir3_shader_key key,
-               boolean rasterflat)
+fd3_program_emit(struct fd_ringbuffer *ring, struct fd3_emit *emit,
+                                int nr, struct pipe_surface **bufs)
 {
        const struct ir3_shader_variant *vp, *fp;
        const struct ir3_info *vsi, *fsi;
        enum a3xx_instrbuffermode fpbuffer, vpbuffer;
        uint32_t fpbuffersz, vpbuffersz, fsoff;
-       uint32_t pos_regid, posz_regid, psize_regid, color_regid;
+       uint32_t pos_regid, posz_regid, psize_regid, color_regid[4] = {0};
+       int constmode;
        int i, j, k;
 
-       vp = fd3_shader_variant(prog->vp, key);
+       debug_assert(nr <= ARRAY_SIZE(color_regid));
+
+       vp = fd3_emit_get_vp(emit);
 
-       if (key.binning_pass) {
+       if (emit->key.binning_pass) {
                /* use dummy stateobj to simplify binning vs non-binning: */
                static const struct ir3_shader_variant binning_fp = {};
                fp = &binning_fp;
        } else {
-               fp = fd3_shader_variant(prog->fp, key);
+               fp = fd3_emit_get_fp(emit);
        }
 
        vsi = &vp->info;
@@ -245,14 +191,28 @@ fd3_program_emit(struct fd_ringbuffer *ring,
                fsoff = 256 - fpbuffersz;
        }
 
-       pos_regid = find_output_regid(vp,
-               ir3_semantic_name(TGSI_SEMANTIC_POSITION, 0));
-       posz_regid = find_output_regid(fp,
-               ir3_semantic_name(TGSI_SEMANTIC_POSITION, 0));
-       psize_regid = find_output_regid(vp,
-               ir3_semantic_name(TGSI_SEMANTIC_PSIZE, 0));
-       color_regid = find_output_regid(fp,
-               ir3_semantic_name(TGSI_SEMANTIC_COLOR, 0));
+       /* seems like vs->constlen + fs->constlen > 256, then CONSTMODE=1 */
+       constmode = ((vp->constlen + fp->constlen) > 256) ? 1 : 0;
+
+       pos_regid = ir3_find_output_regid(vp, VARYING_SLOT_POS);
+       posz_regid = ir3_find_output_regid(fp, FRAG_RESULT_DEPTH);
+       psize_regid = ir3_find_output_regid(vp, VARYING_SLOT_PSIZ);
+       if (fp->color0_mrt) {
+               color_regid[0] = color_regid[1] = color_regid[2] = color_regid[3] =
+                       ir3_find_output_regid(fp, FRAG_RESULT_COLOR);
+       } else {
+               color_regid[0] = ir3_find_output_regid(fp, FRAG_RESULT_DATA0);
+               color_regid[1] = ir3_find_output_regid(fp, FRAG_RESULT_DATA1);
+               color_regid[2] = ir3_find_output_regid(fp, FRAG_RESULT_DATA2);
+               color_regid[3] = ir3_find_output_regid(fp, FRAG_RESULT_DATA3);
+       }
+
+       /* adjust regids for alpha output formats. there is no alpha render
+        * format, so it's just treated like red
+        */
+       for (i = 0; i < nr; i++)
+               if (util_format_is_alpha(pipe_surface_format(bufs[i])))
+                       color_regid[i] += 3;
 
        /* we could probably divide this up into things that need to be
         * emitted if frag-prog is dirty vs if vert-prog is dirty..
@@ -260,6 +220,7 @@ fd3_program_emit(struct fd_ringbuffer *ring,
 
        OUT_PKT0(ring, REG_A3XX_HLSQ_CONTROL_0_REG, 6);
        OUT_RING(ring, A3XX_HLSQ_CONTROL_0_REG_FSTHREADSIZE(FOUR_QUADS) |
+                       A3XX_HLSQ_CONTROL_0_REG_CONSTMODE(constmode) |
                        /* NOTE:  I guess SHADERRESTART and CONSTFULLUPDATE maybe
                         * flush some caches? I think we only need to set those
                         * bits if we have updated const or shader..
@@ -279,8 +240,8 @@ fd3_program_emit(struct fd_ringbuffer *ring,
                        A3XX_HLSQ_FS_CONTROL_REG_INSTRLENGTH(fpbuffersz));
 
        OUT_PKT0(ring, REG_A3XX_SP_SP_CTRL_REG, 1);
-       OUT_RING(ring, A3XX_SP_SP_CTRL_REG_CONSTMODE(0) |
-                       COND(key.binning_pass, A3XX_SP_SP_CTRL_REG_BINNING) |
+       OUT_RING(ring, A3XX_SP_SP_CTRL_REG_CONSTMODE(constmode) |
+                       COND(emit->key.binning_pass, A3XX_SP_SP_CTRL_REG_BINNING) |
                        A3XX_SP_SP_CTRL_REG_SLEEPMODE(1) |
                        A3XX_SP_SP_CTRL_REG_L0MODE(0));
 
@@ -310,16 +271,16 @@ fd3_program_emit(struct fd_ringbuffer *ring,
 
                OUT_PKT0(ring, REG_A3XX_SP_VS_OUT_REG(i), 1);
 
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count) {
-                       k = find_output(vp, fp->inputs[j].semantic);
+                       k = ir3_find_output(vp, fp->inputs[j].slot);
                        reg |= A3XX_SP_VS_OUT_REG_A_REGID(vp->outputs[k].regid);
                        reg |= A3XX_SP_VS_OUT_REG_A_COMPMASK(fp->inputs[j].compmask);
                }
 
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count) {
-                       k = find_output(vp, fp->inputs[j].semantic);
+                       k = ir3_find_output(vp, fp->inputs[j].slot);
                        reg |= A3XX_SP_VS_OUT_REG_B_REGID(vp->outputs[k].regid);
                        reg |= A3XX_SP_VS_OUT_REG_B_COMPMASK(fp->inputs[j].compmask);
                }
@@ -332,16 +293,16 @@ fd3_program_emit(struct fd_ringbuffer *ring,
 
                OUT_PKT0(ring, REG_A3XX_SP_VS_VPC_DST_REG(i), 1);
 
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count)
                        reg |= A3XX_SP_VS_VPC_DST_REG_OUTLOC0(fp->inputs[j].inloc);
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count)
                        reg |= A3XX_SP_VS_VPC_DST_REG_OUTLOC1(fp->inputs[j].inloc);
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count)
                        reg |= A3XX_SP_VS_VPC_DST_REG_OUTLOC2(fp->inputs[j].inloc);
-               j = next_varying(fp, j);
+               j = ir3_next_varying(fp, j);
                if (j < fp->inputs_count)
                        reg |= A3XX_SP_VS_VPC_DST_REG_OUTLOC3(fp->inputs[j].inloc);
 
@@ -353,7 +314,7 @@ fd3_program_emit(struct fd_ringbuffer *ring,
                        A3XX_SP_VS_OBJ_OFFSET_REG_SHADEROBJOFFSET(0));
        OUT_RELOC(ring, vp->bo, 0, 0, 0);  /* SP_VS_OBJ_START_REG */
 
-       if (key.binning_pass) {
+       if (emit->key.binning_pass) {
                OUT_PKT0(ring, REG_A3XX_SP_FS_LENGTH_REG, 1);
                OUT_RING(ring, 0x00000000);
 
@@ -385,59 +346,82 @@ fd3_program_emit(struct fd_ringbuffer *ring,
                                A3XX_SP_FS_CTRL_REG1_CONSTFOOTPRINT(MAX2(fp->constlen + 1, 0)) |
                                A3XX_SP_FS_CTRL_REG1_HALFPRECVAROFFSET(63));
 
-               /* NOTE: I believe VS.CONSTLEN should be <= FS.CONSTOBJOFFSET*/
-               debug_assert(vp->constlen <= 128);
-
                OUT_PKT0(ring, REG_A3XX_SP_FS_OBJ_OFFSET_REG, 2);
-               OUT_RING(ring, A3XX_SP_FS_OBJ_OFFSET_REG_CONSTOBJECTOFFSET(128) |
+               OUT_RING(ring, A3XX_SP_FS_OBJ_OFFSET_REG_CONSTOBJECTOFFSET(
+                                       MAX2(128, vp->constlen)) |
                                A3XX_SP_FS_OBJ_OFFSET_REG_SHADEROBJOFFSET(fsoff));
                OUT_RELOC(ring, fp->bo, 0, 0, 0);  /* SP_FS_OBJ_START_REG */
        }
 
        OUT_PKT0(ring, REG_A3XX_SP_FS_OUTPUT_REG, 1);
-       if (fp->writes_pos) {
-               OUT_RING(ring, A3XX_SP_FS_OUTPUT_REG_DEPTH_ENABLE |
-                               A3XX_SP_FS_OUTPUT_REG_DEPTH_REGID(posz_regid));
-       } else {
-               OUT_RING(ring, 0x00000000);
-       }
+       OUT_RING(ring,
+                        COND(fp->writes_pos, A3XX_SP_FS_OUTPUT_REG_DEPTH_ENABLE) |
+                        A3XX_SP_FS_OUTPUT_REG_DEPTH_REGID(posz_regid) |
+                        A3XX_SP_FS_OUTPUT_REG_MRT(MAX2(1, nr) - 1));
 
        OUT_PKT0(ring, REG_A3XX_SP_FS_MRT_REG(0), 4);
-       OUT_RING(ring, A3XX_SP_FS_MRT_REG_REGID(color_regid) |
-                       COND(fp->key.half_precision, A3XX_SP_FS_MRT_REG_HALF_PRECISION));
-       OUT_RING(ring, A3XX_SP_FS_MRT_REG_REGID(0));
-       OUT_RING(ring, A3XX_SP_FS_MRT_REG_REGID(0));
-       OUT_RING(ring, A3XX_SP_FS_MRT_REG_REGID(0));
+       for (i = 0; i < 4; i++) {
+               uint32_t mrt_reg = A3XX_SP_FS_MRT_REG_REGID(color_regid[i]) |
+                       COND(fp->key.half_precision, A3XX_SP_FS_MRT_REG_HALF_PRECISION);
+
+               if (i < nr) {
+                       enum pipe_format fmt = pipe_surface_format(bufs[i]);
+                       mrt_reg |= COND(util_format_is_pure_uint(fmt), A3XX_SP_FS_MRT_REG_UINT) |
+                               COND(util_format_is_pure_sint(fmt), A3XX_SP_FS_MRT_REG_SINT);
+               }
+               OUT_RING(ring, mrt_reg);
+       }
 
-       if (key.binning_pass) {
+       if (emit->key.binning_pass) {
                OUT_PKT0(ring, REG_A3XX_VPC_ATTR, 2);
                OUT_RING(ring, A3XX_VPC_ATTR_THRDASSIGN(1) |
                                A3XX_VPC_ATTR_LMSIZE(1) |
                                COND(vp->writes_psize, A3XX_VPC_ATTR_PSIZE));
                OUT_RING(ring, 0x00000000);
        } else {
-               uint32_t vinterp[4] = {0}, flatshade[2] = {0};
+               uint32_t vinterp[4], flatshade[2], vpsrepl[4];
+
+               memset(vinterp, 0, sizeof(vinterp));
+               memset(flatshade, 0, sizeof(flatshade));
+               memset(vpsrepl, 0, sizeof(vpsrepl));
 
                /* figure out VARYING_INTERP / FLAT_SHAD register values: */
-               for (j = -1; (j = next_varying(fp, j)) < (int)fp->inputs_count; ) {
-                       uint32_t interp = fp->inputs[j].interpolate;
-                       if ((interp == TGSI_INTERPOLATE_CONSTANT) ||
-                                       ((interp == TGSI_INTERPOLATE_COLOR) && rasterflat)) {
-                               /* TODO might be cleaner to just +8 in SP_VS_VPC_DST_REG
-                                * instead.. rather than -8 everywhere else..
-                                */
-                               uint32_t loc = fp->inputs[j].inloc - 8;
+               for (j = -1; (j = ir3_next_varying(fp, j)) < (int)fp->inputs_count; ) {
 
-                               /* currently assuming varyings aligned to 4 (not
-                                * packed):
-                                */
-                               debug_assert((loc % 4) == 0);
+                       /* TODO might be cleaner to just +8 in SP_VS_VPC_DST_REG
+                        * instead.. rather than -8 everywhere else..
+                        */
+                       uint32_t inloc = fp->inputs[j].inloc - 8;
 
+                       /* currently assuming varyings aligned to 4 (not
+                        * packed):
+                        */
+                       debug_assert((inloc % 4) == 0);
+
+                       if ((fp->inputs[j].interpolate == INTERP_QUALIFIER_FLAT) ||
+                                       (fp->inputs[j].rasterflat && emit->rasterflat)) {
+                               uint32_t loc = inloc;
                                for (i = 0; i < 4; i++, loc++) {
                                        vinterp[loc / 16] |= FLAT << ((loc % 16) * 2);
                                        flatshade[loc / 32] |= 1 << (loc % 32);
                                }
                        }
+
+                       gl_varying_slot slot = fp->inputs[j].slot;
+
+                       /* since we don't enable PIPE_CAP_TGSI_TEXCOORD: */
+                       if (slot >= VARYING_SLOT_VAR0) {
+                               unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
+                               /* Replace the .xy coordinates with S/T from the point sprite. Set
+                                * interpolation bits for .zw such that they become .01
+                                */
+                               if (emit->sprite_coord_enable & texmask) {
+                                       vpsrepl[inloc / 16] |= (emit->sprite_coord_mode ? 0x0d : 0x09)
+                                                       << ((inloc % 16) * 2);
+                                       vinterp[(inloc + 2) / 16] |= 2 << (((inloc + 2) % 16) * 2);
+                                       vinterp[(inloc + 3) / 16] |= 3 << (((inloc + 3) % 16) * 2);
+                               }
+                       }
                }
 
                OUT_PKT0(ring, REG_A3XX_VPC_ATTR, 2);
@@ -455,27 +439,23 @@ fd3_program_emit(struct fd_ringbuffer *ring,
                OUT_RING(ring, vinterp[3]);    /* VPC_VARYING_INTERP[3].MODE */
 
                OUT_PKT0(ring, REG_A3XX_VPC_VARYING_PS_REPL_MODE(0), 4);
-               OUT_RING(ring, fp->shader->vpsrepl[0]);    /* VPC_VARYING_PS_REPL[0].MODE */
-               OUT_RING(ring, fp->shader->vpsrepl[1]);    /* VPC_VARYING_PS_REPL[1].MODE */
-               OUT_RING(ring, fp->shader->vpsrepl[2]);    /* VPC_VARYING_PS_REPL[2].MODE */
-               OUT_RING(ring, fp->shader->vpsrepl[3]);    /* VPC_VARYING_PS_REPL[3].MODE */
+               OUT_RING(ring, vpsrepl[0]);    /* VPC_VARYING_PS_REPL[0].MODE */
+               OUT_RING(ring, vpsrepl[1]);    /* VPC_VARYING_PS_REPL[1].MODE */
+               OUT_RING(ring, vpsrepl[2]);    /* VPC_VARYING_PS_REPL[2].MODE */
+               OUT_RING(ring, vpsrepl[3]);    /* VPC_VARYING_PS_REPL[3].MODE */
 
                OUT_PKT0(ring, REG_A3XX_SP_FS_FLAT_SHAD_MODE_REG_0, 2);
                OUT_RING(ring, flatshade[0]);        /* SP_FS_FLAT_SHAD_MODE_REG_0 */
                OUT_RING(ring, flatshade[1]);        /* SP_FS_FLAT_SHAD_MODE_REG_1 */
        }
 
-       OUT_PKT0(ring, REG_A3XX_VFD_VS_THREADING_THRESHOLD, 1);
-       OUT_RING(ring, A3XX_VFD_VS_THREADING_THRESHOLD_REGID_THRESHOLD(15) |
-                       A3XX_VFD_VS_THREADING_THRESHOLD_REGID_VTXCNT(252));
-
        if (vpbuffer == BUFFER)
                emit_shader(ring, vp);
 
        OUT_PKT0(ring, REG_A3XX_VFD_PERFCOUNTER0_SELECT, 1);
        OUT_RING(ring, 0x00000000);        /* VFD_PERFCOUNTER0_SELECT */
 
-       if (!key.binning_pass) {
+       if (!emit->key.binning_pass) {
                if (fpbuffer == BUFFER)
                        emit_shader(ring, fp);
 
@@ -484,19 +464,6 @@ fd3_program_emit(struct fd_ringbuffer *ring,
        }
 }
 
-/* hack.. until we figure out how to deal w/ vpsrepl properly.. */
-static void
-fix_blit_fp(struct pipe_context *pctx)
-{
-       struct fd_context *ctx = fd_context(pctx);
-       struct fd3_shader_stateobj *so = ctx->blit_prog.fp;
-
-       so->shader->vpsrepl[0] = 0x99999999;
-       so->shader->vpsrepl[1] = 0x99999999;
-       so->shader->vpsrepl[2] = 0x99999999;
-       so->shader->vpsrepl[3] = 0x99999999;
-}
-
 void
 fd3_prog_init(struct pipe_context *pctx)
 {
@@ -507,6 +474,4 @@ fd3_prog_init(struct pipe_context *pctx)
        pctx->delete_vs_state = fd3_vp_state_delete;
 
        fd_prog_init(pctx);
-
-       fix_blit_fp(pctx);
 }