freedreno: Split ir3_const's user buffer and indirect upload APIs.
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_const.c
index 5b7f7518ab709efd1e3d1b1d2bce10ebd970cf9e..419a22d070805f7fc59a76d03ee131454f4c4637 100644 (file)
 #include "fd6_const.h"
 #include "fd6_pack.h"
 
+#define emit_const_user fd6_emit_const_user
+#define emit_const_bo fd6_emit_const_bo
 #include "ir3_const.h"
 
 /* regid:          base const register
  * prsc or dwords: buffer containing constant values
  * sizedwords:     size of const value buffer
  */
-static void
-fd6_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)
+void
+fd6_emit_const_user(struct fd_ringbuffer *ring,
+               const struct ir3_shader_variant *v, uint32_t regid,
+               uint32_t sizedwords, const uint32_t *dwords)
 {
-       if (prsc) {
-               struct fd_bo *bo = fd_resource(prsc)->bo;
-
-               if (fd6_geom_stage(type)) {
-                       OUT_PKT(ring, CP_LOAD_STATE6_GEOM,
-                                       CP_LOAD_STATE6_0(
-                                                       .dst_off     = regid/4,
-                                                       .state_type  = ST6_CONSTANTS,
-                                                       .state_src   = SS6_INDIRECT,
-                                                       .state_block = fd6_stage2shadersb(type),
-                                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
-                                               ),
-                                       CP_LOAD_STATE6_EXT_SRC_ADDR(
-                                                       .bo          = bo,
-                                                       .bo_offset   = offset
-                                               )
-                               );
-               } else {
-                       OUT_PKT(ring, CP_LOAD_STATE6_FRAG,
-                                       CP_LOAD_STATE6_0(
-                                                       .dst_off     = regid/4,
-                                                       .state_type  = ST6_CONSTANTS,
-                                                       .state_src   = SS6_INDIRECT,
-                                                       .state_block = fd6_stage2shadersb(type),
-                                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
-                                               ),
-                                       CP_LOAD_STATE6_EXT_SRC_ADDR(
-                                                       .bo          = bo,
-                                                       .bo_offset   = offset
-                                               )
-                               );
-               }
+       emit_const_asserts(ring, v, regid, sizedwords);
+
+       /* NOTE we cheat a bit here, since we know mesa is aligning
+        * the size of the user buffer to 16 bytes.  And we want to
+        * cut cycles in a hot path.
+        */
+       uint32_t align_sz = align(sizedwords, 4);
+
+       if (fd6_geom_stage(v->type)) {
+               OUT_PKTBUF(ring, CP_LOAD_STATE6_GEOM, dwords, align_sz,
+                               CP_LOAD_STATE6_0(
+                                       .dst_off     = regid/4,
+                                       .state_type  = ST6_CONSTANTS,
+                                       .state_src   = SS6_DIRECT,
+                                       .state_block = fd6_stage2shadersb(v->type),
+                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
+                                       ),
+                               CP_LOAD_STATE6_1(),
+                               CP_LOAD_STATE6_2()
+                       );
        } else {
-               /* NOTE we cheat a bit here, since we know mesa is aligning
-                * the size of the user buffer to 16 bytes.  And we want to
-                * cut cycles in a hot path.
-                */
-               uint32_t align_sz = align(sizedwords, 4);
-               dwords = (uint32_t *)&((uint8_t *)dwords)[offset];
-
-               if (fd6_geom_stage(type)) {
-                       OUT_PKTBUF(ring, CP_LOAD_STATE6_GEOM, dwords, align_sz,
-                                       CP_LOAD_STATE6_0(
-                                                       .dst_off     = regid/4,
-                                                       .state_type  = ST6_CONSTANTS,
-                                                       .state_src   = SS6_DIRECT,
-                                                       .state_block = fd6_stage2shadersb(type),
-                                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
-                                               ),
-                                       CP_LOAD_STATE6_1(),
-                                       CP_LOAD_STATE6_2()
-                               );
-               } else {
-                       OUT_PKTBUF(ring, CP_LOAD_STATE6_FRAG, dwords, align_sz,
-                                       CP_LOAD_STATE6_0(
-                                                       .dst_off     = regid/4,
-                                                       .state_type  = ST6_CONSTANTS,
-                                                       .state_src   = SS6_DIRECT,
-                                                       .state_block = fd6_stage2shadersb(type),
-                                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
-                                               ),
-                                       CP_LOAD_STATE6_1(),
-                                       CP_LOAD_STATE6_2()
-                               );
-               }
+               OUT_PKTBUF(ring, CP_LOAD_STATE6_FRAG, dwords, align_sz,
+                               CP_LOAD_STATE6_0(
+                                       .dst_off     = regid/4,
+                                       .state_type  = ST6_CONSTANTS,
+                                       .state_src   = SS6_DIRECT,
+                                       .state_block = fd6_stage2shadersb(v->type),
+                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
+                                       ),
+                               CP_LOAD_STATE6_1(),
+                               CP_LOAD_STATE6_2()
+                       );
+       }
+}
+void
+fd6_emit_const_bo(struct fd_ringbuffer *ring,
+               const struct ir3_shader_variant *v, uint32_t regid,
+               uint32_t offset, uint32_t sizedwords, struct fd_bo *bo)
+{
+       emit_const_asserts(ring, v, regid, sizedwords);
+
+       if (fd6_geom_stage(v->type)) {
+               OUT_PKT(ring, CP_LOAD_STATE6_GEOM,
+                               CP_LOAD_STATE6_0(
+                                       .dst_off     = regid/4,
+                                       .state_type  = ST6_CONSTANTS,
+                                       .state_src   = SS6_INDIRECT,
+                                       .state_block = fd6_stage2shadersb(v->type),
+                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
+                                       ),
+                               CP_LOAD_STATE6_EXT_SRC_ADDR(
+                                       .bo          = bo,
+                                       .bo_offset   = offset
+                                       )
+                       );
+       } else {
+               OUT_PKT(ring, CP_LOAD_STATE6_FRAG,
+                               CP_LOAD_STATE6_0(
+                                       .dst_off     = regid/4,
+                                       .state_type  = ST6_CONSTANTS,
+                                       .state_src   = SS6_INDIRECT,
+                                       .state_block = fd6_stage2shadersb(v->type),
+                                       .num_unit    = DIV_ROUND_UP(sizedwords, 4)
+                                       ),
+                               CP_LOAD_STATE6_EXT_SRC_ADDR(
+                                       .bo          = bo,
+                                       .bo_offset   = offset
+                                       )
+                       );
        }
 }
 
@@ -110,20 +116,8 @@ is_stateobj(struct fd_ringbuffer *ring)
        return true;
 }
 
-void
-emit_const(struct fd_ringbuffer *ring,
-               const struct ir3_shader_variant *v, uint32_t dst_offset,
-               uint32_t offset, uint32_t size, const void *user_buffer,
-               struct pipe_resource *buffer)
-{
-       /* TODO inline this */
-       assert(dst_offset + size <= v->constlen * 4);
-       fd6_emit_const(ring, v->type, dst_offset,
-                       offset, size, user_buffer, buffer);
-}
-
 static void
-emit_const_bo(struct fd_ringbuffer *ring,
+emit_const_ptrs(struct fd_ringbuffer *ring,
                const struct ir3_shader_variant *v, uint32_t dst_offset,
                uint32_t num, struct pipe_resource **prscs, uint32_t *offsets)
 {
@@ -134,7 +128,8 @@ static void
 emit_tess_bos(struct fd_ringbuffer *ring, struct fd6_emit *emit, struct ir3_shader_variant *s)
 {
        struct fd_context *ctx = emit->ctx;
-       const unsigned regid = s->shader->const_state.offsets.primitive_param * 4 + 4;
+       const struct ir3_const_state *const_state = ir3_const_state(s);
+       const unsigned regid = const_state->offsets.primitive_param * 4 + 4;
        uint32_t dwords = 16;
 
        OUT_PKT7(ring, fd6_stage2opcode(s->type), 3);
@@ -150,10 +145,11 @@ static void
 emit_stage_tess_consts(struct fd_ringbuffer *ring, struct ir3_shader_variant *v,
                uint32_t *params, int num_params)
 {
-       const unsigned regid = v->shader->const_state.offsets.primitive_param;
+       const struct ir3_const_state *const_state = ir3_const_state(v);
+       const unsigned regid = const_state->offsets.primitive_param;
        int size = MIN2(1 + regid, v->constlen) - regid;
        if (size > 0)
-               fd6_emit_const(ring, v->type, regid * 4, 0, num_params, params, NULL);
+               fd6_emit_const_user(ring, v, regid * 4, num_params, params);
 }
 
 static void
@@ -173,8 +169,8 @@ emit_tess_consts(struct fd6_emit *emit)
                emit->gs->shader->nir->info.gs.vertices_in;
 
        uint32_t vs_params[4] = {
-               emit->vs->shader->output_size * num_vertices * 4,       /* vs primitive stride */
-               emit->vs->shader->output_size * 4,                                      /* vs vertex stride */
+               emit->vs->output_size * num_vertices * 4,       /* vs primitive stride */
+               emit->vs->output_size * 4,                                      /* vs vertex stride */
                0,
                0
        };
@@ -183,9 +179,9 @@ emit_tess_consts(struct fd6_emit *emit)
 
        if (emit->hs) {
                uint32_t hs_params[4] = {
-                       emit->vs->shader->output_size * num_vertices * 4,       /* vs primitive stride */
-                       emit->vs->shader->output_size * 4,                                      /* vs vertex stride */
-                       emit->hs->shader->output_size,
+                       emit->vs->output_size * num_vertices * 4,       /* vs primitive stride */
+                       emit->vs->output_size * 4,                                      /* vs vertex stride */
+                       emit->hs->output_size,
                        emit->info->vertices_per_patch
                };
 
@@ -196,9 +192,9 @@ emit_tess_consts(struct fd6_emit *emit)
                        num_vertices = emit->gs->shader->nir->info.gs.vertices_in;
 
                uint32_t ds_params[4] = {
-                       emit->ds->shader->output_size * num_vertices * 4,       /* ds primitive stride */
-                       emit->ds->shader->output_size * 4,                                      /* ds vertex stride */
-                       emit->hs->shader->output_size,                      /* hs vertex stride (dwords) */
+                       emit->ds->output_size * num_vertices * 4,       /* ds primitive stride */
+                       emit->ds->output_size * 4,                                      /* ds vertex stride */
+                       emit->hs->output_size,                      /* hs vertex stride (dwords) */
                        emit->hs->shader->nir->info.tess.tcs_vertices_out
                };
 
@@ -214,8 +210,8 @@ emit_tess_consts(struct fd6_emit *emit)
                        prev = emit->vs;
 
                uint32_t gs_params[4] = {
-                       prev->shader->output_size * num_vertices * 4,   /* ds primitive stride */
-                       prev->shader->output_size * 4,                                  /* ds vertex stride */
+                       prev->output_size * num_vertices * 4,   /* ds primitive stride */
+                       prev->output_size * 4,                                  /* ds vertex stride */
                        0,
                        0,
                };
@@ -231,10 +227,11 @@ static void
 fd6_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
                struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
 {
-       if (!v->shader->num_ubos)
-               return;
+       const struct ir3_const_state *const_state = ir3_const_state(v);
+       int num_ubos = const_state->num_ubos;
 
-       int num_ubos = v->shader->num_ubos;
+       if (!num_ubos)
+               return;
 
        OUT_PKT7(ring, fd6_stage2opcode(v->type), 3 + (2 * num_ubos));
        OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
@@ -246,10 +243,7 @@ fd6_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
        OUT_RING(ring, CP_LOAD_STATE6_2_EXT_SRC_ADDR_HI(0));
 
        for (int i = 0; i < num_ubos; i++) {
-               /* Note: gallium constbuf 0 was always lowered to hardware constbuf,
-                * and UBO load indices decremented by one.
-                */
-               struct pipe_constant_buffer *cb = &constbuf->cb[i + 1];
+               struct pipe_constant_buffer *cb = &constbuf->cb[i];
 
                /* If we have user pointers (constbuf 0, aka GL uniforms), upload them
                 * to a buffer now, and save it in the constbuf so that we don't have
@@ -273,11 +267,34 @@ fd6_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
                                        0);
                } else {
                        OUT_RING(ring, 0xbad00000 | (i << 16));
-                       OUT_RING(ring, 0xbad00000 | (i << 16));
+                       OUT_RING(ring, A6XX_UBO_1_SIZE(0));
                }
        }
 }
 
+static unsigned
+user_consts_cmdstream_size(struct ir3_shader_variant *v)
+{
+       struct ir3_const_state *const_state = ir3_const_state(v);
+       struct ir3_ubo_analysis_state *ubo_state = &const_state->ubo_state;
+
+       if (unlikely(!ubo_state->cmdstream_size)) {
+               unsigned packets, size;
+
+               /* pre-calculate size required for userconst stateobj: */
+               ir3_user_consts_size(ubo_state, &packets, &size);
+
+               /* also account for UBO addresses: */
+               packets += 1;
+               size += 2 * const_state->num_ubos;
+
+               unsigned sizedwords = (4 * packets) + size;
+               ubo_state->cmdstream_size = sizedwords * 4;
+       }
+
+       return ubo_state->cmdstream_size;
+}
+
 static void
 emit_user_consts(struct fd6_emit *emit)
 {
@@ -285,7 +302,7 @@ emit_user_consts(struct fd6_emit *emit)
                        PIPE_SHADER_VERTEX, PIPE_SHADER_TESS_CTRL, PIPE_SHADER_TESS_EVAL,
                        PIPE_SHADER_GEOMETRY, PIPE_SHADER_FRAGMENT,
        };
-       const struct ir3_shader_variant *variants[] = {
+       struct ir3_shader_variant *variants[] = {
                        emit->vs, emit->hs, emit->ds, emit->gs, emit->fs,
        };
        struct fd_context *ctx = emit->ctx;
@@ -294,7 +311,7 @@ emit_user_consts(struct fd6_emit *emit)
        for (unsigned i = 0; i < ARRAY_SIZE(types); i++) {
                if (!variants[i])
                        continue;
-               sz += variants[i]->shader->ubo_state.cmdstream_size;
+               sz += user_consts_cmdstream_size(variants[i]);
        }
 
        struct fd_ringbuffer *constobj = fd_submit_new_ringbuffer(
@@ -361,13 +378,6 @@ fd6_emit_immediates(struct fd_screen *screen, const struct ir3_shader_variant *v
        ir3_emit_immediates(screen, v, ring);
 }
 
-void
-fd6_user_consts_size(struct ir3_ubo_analysis_state *state,
-               unsigned *packets, unsigned *size)
-{
-       ir3_user_consts_size(state, packets, size);
-}
-
 void
 fd6_emit_link_map(struct fd_screen *screen,
                const struct ir3_shader_variant *producer,