freedreno/ir3: remove pipe_stream_output_info dependency
[mesa.git] / src / gallium / drivers / freedreno / a5xx / fd5_emit.c
index 0e2d204484f8143635e53685c029e61c07d481ba..ad942fd7f5ae5e1f817a29fe99b75b2c8da52730 100644 (file)
 
 #include "fd5_emit.h"
 #include "fd5_blend.h"
+#include "fd5_blitter.h"
 #include "fd5_context.h"
+#include "fd5_image.h"
 #include "fd5_program.h"
 #include "fd5_rasterizer.h"
 #include "fd5_texture.h"
+#include "fd5_screen.h"
 #include "fd5_format.h"
 #include "fd5_zsa.h"
 
@@ -48,7 +51,7 @@
  * sizedwords:     size of const value buffer
  */
 static void
-fd5_emit_const(struct fd_ringbuffer *ring, enum shader_t type,
+fd5_emit_const(struct fd_ringbuffer *ring, gl_shader_stage type,
                uint32_t regid, uint32_t offset, uint32_t sizedwords,
                const uint32_t *dwords, struct pipe_resource *prsc)
 {
@@ -87,7 +90,7 @@ fd5_emit_const(struct fd_ringbuffer *ring, enum shader_t type,
 }
 
 static void
-fd5_emit_const_bo(struct fd_ringbuffer *ring, enum shader_t type, boolean write,
+fd5_emit_const_bo(struct fd_ringbuffer *ring, gl_shader_stage type, boolean write,
                uint32_t regid, uint32_t num, struct pipe_resource **prscs, uint32_t *offsets)
 {
        uint32_t anum = align(num, 2);
@@ -127,61 +130,35 @@ fd5_emit_const_bo(struct fd_ringbuffer *ring, enum shader_t type, boolean write,
  * the same as a6xx then move this somewhere common ;-)
  *
  * Entry layout looks like (total size, 0x60 bytes):
- *
- *   offset | description
- *   -------+-------------
- *     0x00 | fp32[0]
- *          | fp32[1]
- *          | fp32[2]
- *          | fp32[3]
- *     0x10 | uint16[0]
- *          | uint16[1]
- *          | uint16[2]
- *          | uint16[3]
- *     0x18 | int16[0]
- *          | int16[1]
- *          | int16[2]
- *          | int16[3]
- *     0x20 | fp16[0]
- *          | fp16[1]
- *          | fp16[2]
- *          | fp16[3]
- *     0x28 | ?? maybe padding ??
- *     0x30 | uint8[0]
- *          | uint8[1]
- *          | uint8[2]
- *          | uint8[3]
- *     0x34 | int8[0]
- *          | int8[1]
- *          | int8[2]
- *          | int8[3]
- *     0x38 | ?? maybe padding ??
- *
- * Some uncertainty, because not clear that this actually works properly
- * with blob, so who knows..
  */
 
 struct PACKED bcolor_entry {
        uint32_t fp32[4];
        uint16_t ui16[4];
        int16_t  si16[4];
+
        uint16_t fp16[4];
-       uint8_t  __pad0[8];
+       uint16_t rgb565;
+       uint16_t rgb5a1;
+       uint16_t rgba4;
+       uint8_t __pad0[2];
        uint8_t  ui8[4];
        int8_t   si8[4];
-       uint8_t  __pad1[40];
+       uint32_t rgb10a2;
+       uint32_t z24; /* also s8? */
+
+       uint16_t srgb[4];      /* appears to duplicate fp16[], but clamped, used for srgb */
+       uint8_t  __pad1[24];
 };
 
 #define FD5_BORDER_COLOR_SIZE        0x60
 #define FD5_BORDER_COLOR_UPLOAD_SIZE (2 * PIPE_MAX_SAMPLERS * FD5_BORDER_COLOR_SIZE)
-#define FD5_BORDER_COLOR_OFFSET      8   /* TODO probably should be dynamic */
 
 static void
 setup_border_colors(struct fd_texture_stateobj *tex, struct bcolor_entry *entries)
 {
        unsigned i, j;
-
-       debug_assert(tex->num_samplers < FD5_BORDER_COLOR_OFFSET);  // TODO
+       STATIC_ASSERT(sizeof(struct bcolor_entry) == FD5_BORDER_COLOR_SIZE);
 
        for (i = 0; i < tex->num_samplers; i++) {
                struct bcolor_entry *e = &entries[i];
@@ -205,33 +182,98 @@ setup_border_colors(struct fd_texture_stateobj *tex, struct bcolor_entry *entrie
                if ((i >= tex->num_textures) || !tex->textures[i])
                        continue;
 
+               enum pipe_format format = tex->textures[i]->format;
                const struct util_format_description *desc =
-                               util_format_description(tex->textures[i]->format);
+                               util_format_description(format);
+
+               e->rgb565 = 0;
+               e->rgb5a1 = 0;
+               e->rgba4 = 0;
+               e->rgb10a2 = 0;
+               e->z24 = 0;
 
                for (j = 0; j < 4; j++) {
                        int c = desc->swizzle[j];
+                       int cd = c;
+
+                       /*
+                        * HACK: for PIPE_FORMAT_X24S8_UINT we end up w/ the
+                        * stencil border color value in bc->ui[0] but according
+                        * to desc->swizzle and desc->channel, the .x component
+                        * is NONE and the stencil value is in the y component.
+                        * Meanwhile the hardware wants this in the .x componetn.
+                        */
+                       if ((format == PIPE_FORMAT_X24S8_UINT) ||
+                                       (format == PIPE_FORMAT_X32_S8X24_UINT)) {
+                               if (j == 0) {
+                                       c = 1;
+                                       cd = 0;
+                               } else {
+                                       continue;
+                               }
+                       }
 
                        if (c >= 4)
                                continue;
 
                        if (desc->channel[c].pure_integer) {
-                               float f = bc->i[c];
-
-                               e->fp32[j] = fui(f);
-                               e->fp16[j] = util_float_to_half(f);
-                               e->ui16[j] = bc->ui[c];
-                               e->si16[j] = bc->i[c];
-                               e->ui8[j]  = bc->ui[c];
-                               e->si8[j]  = bc->i[c];
+                               uint16_t clamped;
+                               switch (desc->channel[c].size) {
+                               case 2:
+                                       assert(desc->channel[c].type == UTIL_FORMAT_TYPE_UNSIGNED);
+                                       clamped = CLAMP(bc->ui[j], 0, 0x3);
+                                       break;
+                               case 8:
+                                       if (desc->channel[c].type == UTIL_FORMAT_TYPE_SIGNED)
+                                               clamped = CLAMP(bc->i[j], -128, 127);
+                                       else
+                                               clamped = CLAMP(bc->ui[j], 0, 255);
+                                       break;
+                               case 10:
+                                       assert(desc->channel[c].type == UTIL_FORMAT_TYPE_UNSIGNED);
+                                       clamped = CLAMP(bc->ui[j], 0, 0x3ff);
+                                       break;
+                               case 16:
+                                       if (desc->channel[c].type == UTIL_FORMAT_TYPE_SIGNED)
+                                               clamped = CLAMP(bc->i[j], -32768, 32767);
+                                       else
+                                               clamped = CLAMP(bc->ui[j], 0, 65535);
+                                       break;
+                               default:
+                                       assert(!"Unexpected bit size");
+                               case 32:
+                                       clamped = 0;
+                                       break;
+                               }
+                               e->fp32[cd] = bc->ui[j];
+                               e->fp16[cd] = clamped;
                        } else {
-                               float f = bc->f[c];
-
-                               e->fp32[j] = fui(f);
-                               e->fp16[j] = util_float_to_half(f);
-                               e->ui16[j] = f * 65535.0;
-                               e->si16[j] = f * 32767.5;
-                               e->ui8[j]  = f * 255.0;
-                               e->si8[j]  = f * 128.0;
+                               float f = bc->f[j];
+                               float f_u = CLAMP(f, 0, 1);
+                               float f_s = CLAMP(f, -1, 1);
+
+                               e->fp32[c] = fui(f);
+                               e->fp16[c] = util_float_to_half(f);
+                               e->srgb[c] = util_float_to_half(f_u);
+                               e->ui16[c] = f_u * 0xffff;
+                               e->si16[c] = f_s * 0x7fff;
+                               e->ui8[c]  = f_u * 0xff;
+                               e->si8[c]  = f_s * 0x7f;
+                               if (c == 1)
+                                       e->rgb565 |= (int)(f_u * 0x3f) << 5;
+                               else if (c < 3)
+                                       e->rgb565 |= (int)(f_u * 0x1f) << (c ? 11 : 0);
+                               if (c == 3)
+                                       e->rgb5a1 |= (f_u > 0.5) ? 0x8000 : 0;
+                               else
+                                       e->rgb5a1 |= (int)(f_u * 0x1f) << (c * 5);
+                               if (c == 3)
+                                       e->rgb10a2 |= (int)(f_u * 0x3) << 30;
+                               else
+                                       e->rgb10a2 |= (int)(f_u * 0x3ff) << (c * 10);
+                               e->rgba4 |= (int)(f_u * 0xf) << (c * 4);
+                               if (c == 0)
+                                       e->z24 = f_u * 0xffffff;
                        }
                }
 
@@ -320,13 +362,20 @@ emit_textures(struct fd_context *ctx, struct fd_ringbuffer *ring,
                        const struct fd5_pipe_sampler_view *view = tex->textures[i] ?
                                        fd5_pipe_sampler_view(tex->textures[i]) :
                                        &dummy_view;
+                       enum a5xx_tile_mode tile_mode = TILE5_LINEAR;
 
-                       OUT_RING(ring, view->texconst0);
+                       if (view->base.texture)
+                               tile_mode = fd_resource(view->base.texture)->tile_mode;
+
+                       OUT_RING(ring, view->texconst0 |
+                                       A5XX_TEX_CONST_0_TILE_MODE(tile_mode));
                        OUT_RING(ring, view->texconst1);
                        OUT_RING(ring, view->texconst2);
                        OUT_RING(ring, view->texconst3);
                        if (view->base.texture) {
                                struct fd_resource *rsc = fd_resource(view->base.texture);
+                               if (view->base.format == PIPE_FORMAT_X32_S8X24_UINT)
+                                       rsc = rsc->stencil;
                                OUT_RELOC(ring, rsc->bo, view->offset,
                                                (uint64_t)view->texconst5 << 32, 0);
                        } else {
@@ -363,14 +412,8 @@ emit_ssbos(struct fd_context *ctx, struct fd_ringbuffer *ring,
                        CP_LOAD_STATE4_1_EXT_SRC_ADDR(0));
        OUT_RING(ring, CP_LOAD_STATE4_2_EXT_SRC_ADDR_HI(0));
        for (unsigned i = 0; i < count; i++) {
-               struct pipe_shader_buffer *buf = &so->sb[i];
-               if (buf->buffer) {
-                       struct fd_resource *rsc = fd_resource(buf->buffer);
-                       OUT_RELOCW(ring, rsc->bo, 0, 0, 0);
-               } else {
-                       OUT_RING(ring, 0x00000000);
-                       OUT_RING(ring, 0x00000000);
-               }
+               OUT_RING(ring, 0x00000000);
+               OUT_RING(ring, 0x00000000);
                OUT_RING(ring, 0x00000000);
                OUT_RING(ring, 0x00000000);
        }
@@ -385,10 +428,13 @@ emit_ssbos(struct fd_context *ctx, struct fd_ringbuffer *ring,
        OUT_RING(ring, CP_LOAD_STATE4_2_EXT_SRC_ADDR_HI(0));
        for (unsigned i = 0; i < count; i++) {
                struct pipe_shader_buffer *buf = &so->sb[i];
+               unsigned sz = buf->buffer_size;
 
-               // TODO maybe offset encoded somewhere here??
-               OUT_RING(ring, (buf->buffer_size << 16));
-               OUT_RING(ring, 0x00000000);
+               /* width is in dwords, overflows into height: */
+               sz /= 4;
+
+               OUT_RING(ring, A5XX_SSBO_1_0_WIDTH(sz));
+               OUT_RING(ring, A5XX_SSBO_1_1_HEIGHT(sz >> 16));
        }
 
        OUT_PKT7(ring, CP_LOAD_STATE4, 3 + (2 * count));
@@ -403,7 +449,7 @@ emit_ssbos(struct fd_context *ctx, struct fd_ringbuffer *ring,
                struct pipe_shader_buffer *buf = &so->sb[i];
                if (buf->buffer) {
                        struct fd_resource *rsc = fd_resource(buf->buffer);
-                       OUT_RELOCW(ring, rsc->bo, 0, 0, 0);
+                       OUT_RELOCW(ring, rsc->bo, buf->buffer_offset, 0, 0);
                } else {
                        OUT_RING(ring, 0x00000000);
                        OUT_RING(ring, 0x00000000);
@@ -433,6 +479,13 @@ fd5_emit_vertex_bufs(struct fd_ringbuffer *ring, struct fd5_emit *emit)
                        uint32_t size = fd_bo_size(rsc->bo) - off;
                        debug_assert(fmt != ~0);
 
+#ifdef DEBUG
+                       /* see dEQP-GLES31.stress.vertex_attribute_binding.buffer_bounds.bind_vertex_buffer_offset_near_wrap_10
+                        */
+                       if (off > fd_bo_size(rsc->bo))
+                               continue;
+#endif
+
                        OUT_PKT4(ring, REG_A5XX_VFD_FETCH(j), 4);
                        OUT_RELOC(ring, rsc->bo, off, 0, 0);
                        OUT_RING(ring, size);           /* VFD_FETCH[j].SIZE */
@@ -442,6 +495,7 @@ fd5_emit_vertex_bufs(struct fd_ringbuffer *ring, struct fd5_emit *emit)
                        OUT_RING(ring, A5XX_VFD_DECODE_INSTR_IDX(j) |
                                        A5XX_VFD_DECODE_INSTR_FORMAT(fmt) |
                                        COND(elem->instance_divisor, A5XX_VFD_DECODE_INSTR_INSTANCED) |
+                                       A5XX_VFD_DECODE_INSTR_SWAP(fd5_pipe2swap(pfmt)) |
                                        A5XX_VFD_DECODE_INSTR_UNK30 |
                                        COND(!isint, A5XX_VFD_DECODE_INSTR_FLOAT));
                        OUT_RING(ring, MAX2(1, elem->instance_divisor)); /* VFD_DECODE[j].STEP_RATE */
@@ -462,6 +516,7 @@ void
 fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
                struct fd5_emit *emit)
 {
+       struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
        const struct ir3_shader_variant *vp = fd5_emit_get_vp(emit);
        const struct ir3_shader_variant *fp = fd5_emit_get_fp(emit);
        const enum fd_dirty_3d_state dirty = emit->dirty;
@@ -469,8 +524,7 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
 
        emit_marker5(ring, 5);
 
-       if ((dirty & FD_DIRTY_FRAMEBUFFER) && !emit->key.binning_pass) {
-               struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
+       if ((dirty & FD_DIRTY_FRAMEBUFFER) && !emit->binning_pass) {
                unsigned char mrt_comp[A5XX_MAX_RENDER_TARGETS] = {0};
 
                for (unsigned i = 0; i < A5XX_MAX_RENDER_TARGETS; i++) {
@@ -490,7 +544,6 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
 
        if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_FRAMEBUFFER)) {
                struct fd5_zsa_stateobj *zsa = fd5_zsa_stateobj(ctx->zsa);
-               struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
                uint32_t rb_alpha_control = zsa->rb_alpha_control;
 
                if (util_format_is_pure_integer(pipe_surface_format(pfb->cbufs[0])))
@@ -503,13 +556,33 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
                OUT_RING(ring, zsa->rb_stencil_control);
        }
 
+       if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_BLEND | FD_DIRTY_PROG)) {
+               struct fd5_blend_stateobj *blend = fd5_blend_stateobj(ctx->blend);
+               struct fd5_zsa_stateobj *zsa = fd5_zsa_stateobj(ctx->zsa);
+
+               if (pfb->zsbuf) {
+                       struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
+                       uint32_t gras_lrz_cntl = zsa->gras_lrz_cntl;
+
+                       if (emit->no_lrz_write || !rsc->lrz || !rsc->lrz_valid)
+                               gras_lrz_cntl = 0;
+                       else if (emit->binning_pass && blend->lrz_write && zsa->lrz_write)
+                               gras_lrz_cntl |= A5XX_GRAS_LRZ_CNTL_LRZ_WRITE;
+
+                       OUT_PKT4(ring, REG_A5XX_GRAS_LRZ_CNTL, 1);
+                       OUT_RING(ring, gras_lrz_cntl);
+               }
+       }
+
        if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_STENCIL_REF)) {
                struct fd5_zsa_stateobj *zsa = fd5_zsa_stateobj(ctx->zsa);
                struct pipe_stencil_ref *sr = &ctx->stencil_ref;
 
-               OUT_PKT4(ring, REG_A5XX_RB_STENCILREFMASK, 1);
+               OUT_PKT4(ring, REG_A5XX_RB_STENCILREFMASK, 2);
                OUT_RING(ring, zsa->rb_stencilrefmask |
                                A5XX_RB_STENCILREFMASK_STENCILREF(sr->ref_value[0]));
+               OUT_RING(ring, zsa->rb_stencilrefmask_bf |
+                               A5XX_RB_STENCILREFMASK_BF_STENCILREF(sr->ref_value[1]));
        }
 
        if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_RASTERIZER | FD_DIRTY_PROG)) {
@@ -528,40 +601,8 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
                                COND(fragz && fp->frag_coord, A5XX_GRAS_SU_DEPTH_PLANE_CNTL_UNK1));
        }
 
-       if (dirty & FD_DIRTY_RASTERIZER) {
-               struct fd5_rasterizer_stateobj *rasterizer =
-                               fd5_rasterizer_stateobj(ctx->rasterizer);
-
-               OUT_PKT4(ring, REG_A5XX_GRAS_SU_CNTL, 1);
-               OUT_RING(ring, rasterizer->gras_su_cntl);
-
-               OUT_PKT4(ring, REG_A5XX_GRAS_SU_POINT_MINMAX, 2);
-               OUT_RING(ring, rasterizer->gras_su_point_minmax);
-               OUT_RING(ring, rasterizer->gras_su_point_size);
-
-               OUT_PKT4(ring, REG_A5XX_GRAS_SU_POLY_OFFSET_SCALE, 3);
-               OUT_RING(ring, rasterizer->gras_su_poly_offset_scale);
-               OUT_RING(ring, rasterizer->gras_su_poly_offset_offset);
-               OUT_RING(ring, rasterizer->gras_su_poly_offset_clamp);
-       }
-
-       /* NOTE: since primitive_restart is not actually part of any
-        * state object, we need to make sure that we always emit
-        * PRIM_VTX_CNTL.. either that or be more clever and detect
-        * when it changes.
-        */
-       if (emit->info) {
-               struct fd5_rasterizer_stateobj *rast =
-                       fd5_rasterizer_stateobj(ctx->rasterizer);
-               uint32_t val = rast->pc_prim_vtx_cntl;
-
-               val |= COND(vp->writes_psize, A5XX_PC_PRIM_VTX_CNTL_PSIZE);
-
-               OUT_PKT4(ring, REG_A5XX_PC_PRIM_VTX_CNTL, 1);
-               OUT_RING(ring, val);
-       }
-
-       if (dirty & FD_DIRTY_SCISSOR) {
+       /* NOTE: scissor enabled bit is part of rasterizer state: */
+       if (dirty & (FD_DIRTY_SCISSOR | FD_DIRTY_RASTERIZER)) {
                struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
 
                OUT_PKT4(ring, REG_A5XX_GRAS_SC_SCREEN_SCISSOR_TL_0, 2);
@@ -594,14 +635,57 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
        }
 
        if (dirty & FD_DIRTY_PROG)
-               fd5_program_emit(ring, emit);
+               fd5_program_emit(ctx, ring, emit);
 
-       if (dirty & (FD_DIRTY_FRAMEBUFFER | FD_DIRTY_RASTERIZER)) {
-               struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
+       if (dirty & FD_DIRTY_RASTERIZER) {
+               struct fd5_rasterizer_stateobj *rasterizer =
+                               fd5_rasterizer_stateobj(ctx->rasterizer);
+
+               OUT_PKT4(ring, REG_A5XX_GRAS_SU_CNTL, 1);
+               OUT_RING(ring, rasterizer->gras_su_cntl |
+                               COND(pfb->samples > 1, A5XX_GRAS_SU_CNTL_MSAA_ENABLE));
+
+               OUT_PKT4(ring, REG_A5XX_GRAS_SU_POINT_MINMAX, 2);
+               OUT_RING(ring, rasterizer->gras_su_point_minmax);
+               OUT_RING(ring, rasterizer->gras_su_point_size);
+
+               OUT_PKT4(ring, REG_A5XX_GRAS_SU_POLY_OFFSET_SCALE, 3);
+               OUT_RING(ring, rasterizer->gras_su_poly_offset_scale);
+               OUT_RING(ring, rasterizer->gras_su_poly_offset_offset);
+               OUT_RING(ring, rasterizer->gras_su_poly_offset_clamp);
+
+               OUT_PKT4(ring, REG_A5XX_PC_RASTER_CNTL, 1);
+               OUT_RING(ring, rasterizer->pc_raster_cntl);
+
+               OUT_PKT4(ring, REG_A5XX_GRAS_CL_CNTL, 1);
+               OUT_RING(ring, rasterizer->gras_cl_clip_cntl);
+       }
+
+       /* note: must come after program emit.. because there is some overlap
+        * in registers, ex. PC_PRIMITIVE_CNTL and we rely on some cached
+        * values from fd5_program_emit() to avoid having to re-emit the prog
+        * every time rast state changes.
+        *
+        * Since the primitive restart state is not part of a tracked object, we
+        * re-emit this register every time.
+        */
+       if (emit->info && ctx->rasterizer) {
+               struct fd5_rasterizer_stateobj *rasterizer =
+                               fd5_rasterizer_stateobj(ctx->rasterizer);
+               unsigned max_loc = fd5_context(ctx)->max_loc;
+
+               OUT_PKT4(ring, REG_A5XX_PC_PRIMITIVE_CNTL, 1);
+               OUT_RING(ring, rasterizer->pc_primitive_cntl |
+                                A5XX_PC_PRIMITIVE_CNTL_STRIDE_IN_VPC(max_loc) |
+                                COND(emit->info->primitive_restart && emit->info->index_size,
+                                         A5XX_PC_PRIMITIVE_CNTL_PRIMITIVE_RESTART));
+       }
+
+       if (dirty & (FD_DIRTY_FRAMEBUFFER | FD_DIRTY_RASTERIZER | FD_DIRTY_PROG)) {
                uint32_t posz_regid = ir3_find_output_regid(fp, FRAG_RESULT_DEPTH);
                unsigned nr = pfb->nr_cbufs;
 
-               if (emit->key.binning_pass)
+               if (emit->binning_pass)
                        nr = 0;
                else if (ctx->rasterizer->rasterizer_discard)
                        nr = 0;
@@ -616,49 +700,46 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
                                A5XX_SP_FS_OUTPUT_CNTL_SAMPLEMASK_REGID(regid(63, 0)));
        }
 
-       if (emit->prog == &ctx->prog) { /* evil hack to deal sanely with clear path */
-               ir3_emit_vs_consts(vp, ring, ctx, emit->info);
-               if (!emit->key.binning_pass)
-                       ir3_emit_fs_consts(fp, ring, ctx);
+       ir3_emit_vs_consts(vp, ring, ctx, emit->info);
+       if (!emit->binning_pass)
+               ir3_emit_fs_consts(fp, ring, ctx);
 
-               struct pipe_stream_output_info *info = &vp->shader->stream_output;
-               if (info->num_outputs) {
-                       struct fd_streamout_stateobj *so = &ctx->streamout;
+       struct ir3_stream_output_info *info = &vp->shader->stream_output;
+       if (info->num_outputs) {
+               struct fd_streamout_stateobj *so = &ctx->streamout;
 
-                       for (unsigned i = 0; i < so->num_targets; i++) {
-                               struct pipe_stream_output_target *target = so->targets[i];
+               for (unsigned i = 0; i < so->num_targets; i++) {
+                       struct pipe_stream_output_target *target = so->targets[i];
 
-                               if (!target)
-                                       continue;
+                       if (!target)
+                               continue;
 
-                               unsigned offset = (so->offsets[i] * info->stride[i] * 4) +
-                                               target->buffer_offset;
+                       unsigned offset = (so->offsets[i] * info->stride[i] * 4) +
+                                       target->buffer_offset;
 
-                               OUT_PKT4(ring, REG_A5XX_VPC_SO_BUFFER_BASE_LO(i), 3);
-                               /* VPC_SO[i].BUFFER_BASE_LO: */
-                               OUT_RELOCW(ring, fd_resource(target->buffer)->bo, 0, 0, 0);
-                               OUT_RING(ring, target->buffer_size + offset);
+                       OUT_PKT4(ring, REG_A5XX_VPC_SO_BUFFER_BASE_LO(i), 3);
+                       /* VPC_SO[i].BUFFER_BASE_LO: */
+                       OUT_RELOCW(ring, fd_resource(target->buffer)->bo, 0, 0, 0);
+                       OUT_RING(ring, target->buffer_size + offset);
 
-                               OUT_PKT4(ring, REG_A5XX_VPC_SO_BUFFER_OFFSET(i), 3);
-                               OUT_RING(ring, offset);
-                               /* VPC_SO[i].FLUSH_BASE_LO/HI: */
-                               // TODO just give hw a dummy addr for now.. we should
-                               // be using this an then CP_MEM_TO_REG to set the
-                               // VPC_SO[i].BUFFER_OFFSET for the next draw..
-                               OUT_RELOCW(ring, fd5_context(ctx)->blit_mem, 0x100, 0, 0);
+                       OUT_PKT4(ring, REG_A5XX_VPC_SO_BUFFER_OFFSET(i), 3);
+                       OUT_RING(ring, offset);
+                       /* VPC_SO[i].FLUSH_BASE_LO/HI: */
+                       // TODO just give hw a dummy addr for now.. we should
+                       // be using this an then CP_MEM_TO_REG to set the
+                       // VPC_SO[i].BUFFER_OFFSET for the next draw..
+                       OUT_RELOCW(ring, fd5_context(ctx)->blit_mem, 0x100, 0, 0);
 
-                               emit->streamout_mask |= (1 << i);
-                       }
+                       emit->streamout_mask |= (1 << i);
                }
        }
 
-       if ((dirty & FD_DIRTY_BLEND)) {
+       if (dirty & FD_DIRTY_BLEND) {
                struct fd5_blend_stateobj *blend = fd5_blend_stateobj(ctx->blend);
                uint32_t i;
 
                for (i = 0; i < A5XX_MAX_RENDER_TARGETS; i++) {
-                       enum pipe_format format = pipe_surface_format(
-                                       ctx->batch->framebuffer.cbufs[i]);
+                       enum pipe_format format = pipe_surface_format(pfb->cbufs[i]);
                        bool is_int = util_format_is_pure_integer(format);
                        bool has_alpha = util_format_has_alpha(format);
                        uint32_t control = blend->rb_mrt[i].control;
@@ -666,7 +747,7 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
 
                        if (is_int) {
                                control &= A5XX_RB_MRT_CONTROL_COMPONENT_ENABLE__MASK;
-//                             control |= A5XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY);
+                               control |= A5XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY);
                        }
 
                        if (has_alpha) {
@@ -683,12 +764,16 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
                        OUT_RING(ring, blend_control);
                }
 
+               OUT_PKT4(ring, REG_A5XX_SP_BLEND_CNTL, 1);
+               OUT_RING(ring, blend->sp_blend_cntl);
+       }
+
+       if (dirty & (FD_DIRTY_BLEND | FD_DIRTY_SAMPLE_MASK)) {
+               struct fd5_blend_stateobj *blend = fd5_blend_stateobj(ctx->blend);
+
                OUT_PKT4(ring, REG_A5XX_RB_BLEND_CNTL, 1);
                OUT_RING(ring, blend->rb_blend_cntl |
-                               A5XX_RB_BLEND_CNTL_SAMPLE_MASK(0xffff));
-
-               OUT_PKT4(ring, REG_A5XX_SP_BLEND_CNTL, 1);
-               OUT_RING(ring, 0x00000100);
+                               A5XX_RB_BLEND_CNTL_SAMPLE_MASK(ctx->sample_mask));
        }
 
        if (dirty & FD_DIRTY_BLEND_COLOR) {
@@ -723,10 +808,12 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
        if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & FD_DIRTY_SHADER_TEX) {
                needs_border |= emit_textures(ctx, ring, SB4_FS_TEX,
                                &ctx->tex[PIPE_SHADER_FRAGMENT]);
-               OUT_PKT4(ring, REG_A5XX_TPL1_FS_TEX_COUNT, 1);
-               OUT_RING(ring, ctx->tex[PIPE_SHADER_FRAGMENT].num_textures);
        }
 
+       OUT_PKT4(ring, REG_A5XX_TPL1_FS_TEX_COUNT, 1);
+       OUT_RING(ring, ctx->shaderimg[PIPE_SHADER_FRAGMENT].enabled_mask ?
+                       ~0 : ctx->tex[PIPE_SHADER_FRAGMENT].num_textures);
+
        OUT_PKT4(ring, REG_A5XX_TPL1_CS_TEX_COUNT, 1);
        OUT_RING(ring, 0);
 
@@ -735,6 +822,9 @@ fd5_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
 
        if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & FD_DIRTY_SHADER_SSBO)
                emit_ssbos(ctx, ring, SB4_SSBO, &ctx->shaderbuf[PIPE_SHADER_FRAGMENT]);
+
+       if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & FD_DIRTY_SHADER_IMAGE)
+               fd5_emit_images(ctx, ring, PIPE_SHADER_FRAGMENT);
 }
 
 void
@@ -765,13 +855,17 @@ fd5_emit_cs_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
 
                OUT_PKT4(ring, REG_A5XX_TPL1_FS_TEX_COUNT, 1);
                OUT_RING(ring, 0);
-
-               OUT_PKT4(ring, REG_A5XX_TPL1_CS_TEX_COUNT, 1);
-               OUT_RING(ring, ctx->tex[PIPE_SHADER_COMPUTE].num_textures);
        }
 
+       OUT_PKT4(ring, REG_A5XX_TPL1_CS_TEX_COUNT, 1);
+       OUT_RING(ring, ctx->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask ?
+                       ~0 : ctx->tex[PIPE_SHADER_COMPUTE].num_textures);
+
        if (dirty & FD_DIRTY_SHADER_SSBO)
                emit_ssbos(ctx, ring, SB4_CS_SSBO, &ctx->shaderbuf[PIPE_SHADER_COMPUTE]);
+
+       if (dirty & FD_DIRTY_SHADER_IMAGE)
+               fd5_emit_images(ctx, ring, PIPE_SHADER_COMPUTE);
 }
 
 /* emit setup at begin of new cmdstream buffer (don't rely on previous
@@ -804,9 +898,6 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_PKT4(ring, REG_A5XX_PC_RASTER_CNTL, 1);
        OUT_RING(ring, 0x00000012);
 
-       OUT_PKT4(ring, REG_A5XX_GRAS_LRZ_CNTL, 1);
-       OUT_RING(ring, 0x00000000);
-
        OUT_PKT4(ring, REG_A5XX_GRAS_SU_POINT_MINMAX, 2);
        OUT_RING(ring, A5XX_GRAS_SU_POINT_MINMAX_MIN(1.0) |
                        A5XX_GRAS_SU_POINT_MINMAX_MAX(4092.0));
@@ -870,10 +961,6 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_RING(ring, CP_SET_DRAW_STATE__1_ADDR_LO(0));
        OUT_RING(ring, CP_SET_DRAW_STATE__2_ADDR_HI(0));
 
-       /* other regs not used (yet?) and always seem to have same value: */
-       OUT_PKT4(ring, REG_A5XX_GRAS_CL_CNTL, 1);
-       OUT_RING(ring, 0x00000080);   /* GRAS_CL_CNTL */
-
        OUT_PKT4(ring, REG_A5XX_GRAS_SU_CONSERVATIVE_RAS_CNTL, 1);
        OUT_RING(ring, 0x00000000);   /* GRAS_SU_CONSERVATIVE_RAS_CNTL */
 
@@ -913,11 +1000,8 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_PKT4(ring, REG_A5XX_UNKNOWN_E004, 1);
        OUT_RING(ring, 0x00000000);   /* UNKNOWN_E004 */
 
-       OUT_PKT4(ring, REG_A5XX_UNKNOWN_E093, 1);
-       OUT_RING(ring, 0x00000000);   /* UNKNOWN_E093 */
-
-       OUT_PKT4(ring, REG_A5XX_UNKNOWN_E1C7, 1);
-       OUT_RING(ring, 0x00000000);   /* UNKNOWN_E1C7 */
+       OUT_PKT4(ring, REG_A5XX_GRAS_SU_LAYERED, 1);
+       OUT_RING(ring, 0x00000000);   /* GRAS_SU_LAYERED */
 
        OUT_PKT4(ring, REG_A5XX_UNKNOWN_E29A, 1);
        OUT_RING(ring, 0x00ffff00);   /* UNKNOWN_E29A */
@@ -931,8 +1015,8 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_PKT4(ring, REG_A5XX_UNKNOWN_E389, 1);
        OUT_RING(ring, 0x00000000);   /* UNKNOWN_E389 */
 
-       OUT_PKT4(ring, REG_A5XX_UNKNOWN_E38D, 1);
-       OUT_RING(ring, 0x00000000);   /* UNKNOWN_E38D */
+       OUT_PKT4(ring, REG_A5XX_PC_GS_LAYERED, 1);
+       OUT_RING(ring, 0x00000000);   /* PC_GS_LAYERED */
 
        OUT_PKT4(ring, REG_A5XX_UNKNOWN_E5AB, 1);
        OUT_RING(ring, 0x00000000);   /* UNKNOWN_E5AB */
@@ -969,10 +1053,10 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_PKT4(ring, REG_A5XX_UNKNOWN_E5DB, 1);
        OUT_RING(ring, 0x00000000);
 
-       OUT_PKT4(ring, REG_A5XX_UNKNOWN_E600, 1);
+       OUT_PKT4(ring, REG_A5XX_SP_HS_CTRL_REG0, 1);
        OUT_RING(ring, 0x00000000);
 
-       OUT_PKT4(ring, REG_A5XX_UNKNOWN_E640, 1);
+       OUT_PKT4(ring, REG_A5XX_SP_GS_CTRL_REG0, 1);
        OUT_RING(ring, 0x00000000);
 
        OUT_PKT4(ring, REG_A5XX_TPL1_VS_TEX_COUNT, 4);
@@ -1014,12 +1098,43 @@ t7              opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
        OUT_RING(ring, 0x00000000);
        OUT_RING(ring, 0x00000000);
        OUT_RING(ring, 0x00000000);
+
+       OUT_PKT4(ring, REG_A5XX_RB_CLEAR_CNTL, 1);
+       OUT_RING(ring, 0x00000000);
 }
 
 static void
 fd5_emit_ib(struct fd_ringbuffer *ring, struct fd_ringbuffer *target)
 {
+       /* for debug after a lock up, write a unique counter value
+        * to scratch6 for each IB, to make it easier to match up
+        * register dumps to cmdstream.  The combination of IB and
+        * DRAW (scratch7) is enough to "triangulate" the particular
+        * draw that caused lockup.
+        */
+       emit_marker5(ring, 6);
        __OUT_IB5(ring, target);
+       emit_marker5(ring, 6);
+}
+
+static void
+fd5_mem_to_mem(struct fd_ringbuffer *ring, struct pipe_resource *dst,
+               unsigned dst_off, struct pipe_resource *src, unsigned src_off,
+               unsigned sizedwords)
+{
+       struct fd_bo *src_bo = fd_resource(src)->bo;
+       struct fd_bo *dst_bo = fd_resource(dst)->bo;
+       unsigned i;
+
+       for (i = 0; i < sizedwords; i++) {
+               OUT_PKT7(ring, CP_MEM_TO_MEM, 5);
+               OUT_RING(ring, 0x00000000);
+               OUT_RELOCW(ring, dst_bo, dst_off, 0, 0);
+               OUT_RELOC (ring, src_bo, src_off, 0, 0);
+
+               dst_off += 4;
+               src_off += 4;
+       }
 }
 
 void
@@ -1029,4 +1144,5 @@ fd5_emit_init(struct pipe_context *pctx)
        ctx->emit_const = fd5_emit_const;
        ctx->emit_const_bo = fd5_emit_const_bo;
        ctx->emit_ib = fd5_emit_ib;
+       ctx->mem_to_mem = fd5_mem_to_mem;
 }