etnaviv: prep for UBOs
authorJonathan Marek <jonathan@marek.ca>
Fri, 28 Jun 2019 02:02:45 +0000 (22:02 -0400)
committerJonathan Marek <jonathan@marek.ca>
Tue, 6 Aug 2019 14:33:17 +0000 (10:33 -0400)
Allow UBO relocs and only emitting uniforms that are actually used.

GC7000Lite has no address register, so upload uniforms to a UBO object to
LOAD from.

I removed the code to check for changes to individual uniforms and just
reupload to entire uniform state when the state is dirty. I think there
was very limited benefit to it and it isn't compatible with relocs.

Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>
Reviewed-by: Christian Gmeiner <christian.gmeiner@gmail.com>
src/gallium/drivers/etnaviv/etnaviv_compiler.c
src/gallium/drivers/etnaviv/etnaviv_context.h
src/gallium/drivers/etnaviv/etnaviv_emit.c
src/gallium/drivers/etnaviv/etnaviv_internal.h
src/gallium/drivers/etnaviv/etnaviv_shader.c
src/gallium/drivers/etnaviv/etnaviv_state.c
src/gallium/drivers/etnaviv/etnaviv_uniforms.c
src/gallium/drivers/etnaviv/etnaviv_uniforms.h

index f89c46eff664a11cf126f5a411f8e502765cdfb4..474e3d23405af3afc6eea56839149b7cde52fc0d 100644 (file)
@@ -2265,13 +2265,20 @@ etna_compile_check_limits(struct etna_compile *c)
 static void
 copy_uniform_state_to_shader(struct etna_compile *c, struct etna_shader_variant *sobj)
 {
-   uint32_t count = c->imm_size;
+   uint32_t count = c->imm_base + c->imm_size;
    struct etna_shader_uniform_info *uinfo = &sobj->uniforms;
 
-   uinfo->const_count = c->imm_base;
    uinfo->imm_count = count;
-   uinfo->imm_data = mem_dup(c->imm_data, count * sizeof(*c->imm_data));
-   uinfo->imm_contents = mem_dup(c->imm_contents, count * sizeof(*c->imm_contents));
+
+   uinfo->imm_data = malloc(count * sizeof(*c->imm_data));
+   for (unsigned i = 0; i < c->imm_base; i++)
+      uinfo->imm_data[i] = i;
+   memcpy(&uinfo->imm_data[c->imm_base], c->imm_data, c->imm_size * sizeof(*c->imm_data));
+
+   uinfo->imm_contents = malloc(count * sizeof(*c->imm_contents));
+   for (unsigned i = 0; i < c->imm_base; i++)
+      uinfo->imm_contents[i] = ETNA_IMMEDIATE_UNIFORM;
+   memcpy(&uinfo->imm_contents[c->imm_base], c->imm_contents, c->imm_size * sizeof(*c->imm_contents));
 
    etna_set_shader_uniforms_dirty_flags(sobj);
 }
@@ -2486,14 +2493,14 @@ etna_dump_shader(const struct etna_shader_variant *shader)
 
    printf("num loops: %i\n", shader->num_loops);
    printf("num temps: %i\n", shader->num_temps);
-   printf("num const: %i\n", shader->uniforms.const_count);
    printf("immediates:\n");
    for (int idx = 0; idx < shader->uniforms.imm_count; ++idx) {
-      printf(" [%i].%s = %f (0x%08x)\n",
-             (idx + shader->uniforms.const_count) / 4,
+      printf(" [%i].%s = %f (0x%08x) (%d)\n",
+             idx / 4,
              tgsi_swizzle_names[idx % 4],
              *((float *)&shader->uniforms.imm_data[idx]),
-             shader->uniforms.imm_data[idx]);
+             shader->uniforms.imm_data[idx],
+             shader->uniforms.imm_contents[idx]);
    }
    printf("inputs:\n");
    for (int idx = 0; idx < shader->infile.num_reg; ++idx) {
index a79d739100d97a489138f3693b7bcfd376a9324b..e1ce66b5dffc282f4fa03178e929f32f1c563bde 100644 (file)
@@ -88,15 +88,17 @@ struct etna_shader_state {
 enum etna_immediate_contents {
    ETNA_IMMEDIATE_UNUSED = 0,
    ETNA_IMMEDIATE_CONSTANT,
+   ETNA_IMMEDIATE_UNIFORM,
    ETNA_IMMEDIATE_TEXRECT_SCALE_X,
    ETNA_IMMEDIATE_TEXRECT_SCALE_Y,
+   ETNA_IMMEDIATE_UBO0_ADDR,
+   ETNA_IMMEDIATE_UBOMAX_ADDR = ETNA_IMMEDIATE_UBO0_ADDR + 255,
 };
 
 struct etna_shader_uniform_info {
    enum etna_immediate_contents *imm_contents;
    uint32_t *imm_data;
    uint32_t imm_count;
-   uint32_t const_count;
 };
 
 struct etna_context {
index 05223ff2062bcc4add9f385d4c2164058f7b4966..93f4b369a3031fbbd67f916851fde17e7d49c2ee 100644 (file)
@@ -105,8 +105,8 @@ required_stream_size(struct etna_context *ctx)
    size += ctx->vertex_elements->num_elements + 1;
 
    /* uniforms - worst case (2 words per uniform load) */
-   size += ctx->shader.vs->uniforms.const_count * 2;
-   size += ctx->shader.fs->uniforms.const_count * 2;
+   size += ctx->shader.vs->uniforms.imm_count * 2;
+   size += ctx->shader.fs->uniforms.imm_count * 2;
 
    /* shader */
    size += ctx->shader_state.vs_inst_mem_size + 1;
@@ -583,16 +583,6 @@ etna_emit_state(struct etna_context *ctx)
    static const uint32_t uniform_dirty_bits =
       ETNA_DIRTY_SHADER | ETNA_DIRTY_CONSTBUF;
 
-   if (dirty & (uniform_dirty_bits | ctx->shader.vs->uniforms_dirty_bits))
-      etna_uniforms_write(
-         ctx, ctx->shader.vs, &ctx->constant_buffer[PIPE_SHADER_VERTEX],
-         ctx->shader_state.VS_UNIFORMS, &ctx->shader_state.vs_uniforms_size);
-
-   if (dirty & (uniform_dirty_bits | ctx->shader.fs->uniforms_dirty_bits))
-      etna_uniforms_write(
-         ctx, ctx->shader.fs, &ctx->constant_buffer[PIPE_SHADER_FRAGMENT],
-         ctx->shader_state.PS_UNIFORMS, &ctx->shader_state.ps_uniforms_size);
-
    /**** Large dynamically-sized state ****/
    bool do_uniform_flush = ctx->specs.halti < 5;
    if (dirty & (ETNA_DIRTY_SHADER)) {
@@ -672,22 +662,13 @@ etna_emit_state(struct etna_context *ctx)
 
       if (do_uniform_flush)
          etna_set_state(stream, VIVS_VS_UNIFORM_CACHE, VIVS_VS_UNIFORM_CACHE_FLUSH);
-      etna_set_state_multi(stream, ctx->specs.vs_uniforms_offset,
-                                     ctx->shader_state.vs_uniforms_size,
-                                     ctx->shader_state.VS_UNIFORMS);
+
+      etna_uniforms_write(ctx, ctx->shader.vs, &ctx->constant_buffer[PIPE_SHADER_VERTEX]);
+
       if (do_uniform_flush)
          etna_set_state(stream, VIVS_VS_UNIFORM_CACHE, VIVS_VS_UNIFORM_CACHE_FLUSH | VIVS_VS_UNIFORM_CACHE_PS);
-      etna_set_state_multi(stream, ctx->specs.ps_uniforms_offset,
-                                     ctx->shader_state.ps_uniforms_size,
-                                     ctx->shader_state.PS_UNIFORMS);
-
-      /* Copy uniforms to gpu3d, so that incremental updates to uniforms are
-       * possible as long as the
-       * same shader remains bound */
-      memcpy(ctx->gpu3d.VS_UNIFORMS, ctx->shader_state.VS_UNIFORMS,
-             ctx->shader_state.vs_uniforms_size * 4);
-      memcpy(ctx->gpu3d.PS_UNIFORMS, ctx->shader_state.PS_UNIFORMS,
-             ctx->shader_state.ps_uniforms_size * 4);
+
+      etna_uniforms_write(ctx, ctx->shader.fs, &ctx->constant_buffer[PIPE_SHADER_FRAGMENT]);
 
       if (ctx->specs.halti >= 5) {
          /* HALTI5 needs to be prompted to pre-fetch shaders */
@@ -699,26 +680,16 @@ etna_emit_state(struct etna_context *ctx)
       /* ideally this cache would only be flushed if there are VS uniform changes */
       if (do_uniform_flush)
          etna_set_state(stream, VIVS_VS_UNIFORM_CACHE, VIVS_VS_UNIFORM_CACHE_FLUSH);
-      etna_coalesce_start(stream, &coalesce);
-      for (int x = 0; x < ctx->shader.vs->uniforms.const_count; ++x) {
-         if (ctx->gpu3d.VS_UNIFORMS[x] != ctx->shader_state.VS_UNIFORMS[x]) {
-            etna_coalsence_emit(stream, &coalesce, ctx->specs.vs_uniforms_offset + x*4, ctx->shader_state.VS_UNIFORMS[x]);
-            ctx->gpu3d.VS_UNIFORMS[x] = ctx->shader_state.VS_UNIFORMS[x];
-         }
-      }
-      etna_coalesce_end(stream, &coalesce);
+
+      if (dirty & (uniform_dirty_bits | ctx->shader.vs->uniforms_dirty_bits))
+         etna_uniforms_write(ctx, ctx->shader.vs, &ctx->constant_buffer[PIPE_SHADER_VERTEX]);
 
       /* ideally this cache would only be flushed if there are PS uniform changes */
       if (do_uniform_flush)
          etna_set_state(stream, VIVS_VS_UNIFORM_CACHE, VIVS_VS_UNIFORM_CACHE_FLUSH | VIVS_VS_UNIFORM_CACHE_PS);
-      etna_coalesce_start(stream, &coalesce);
-      for (int x = 0; x < ctx->shader.fs->uniforms.const_count; ++x) {
-         if (ctx->gpu3d.PS_UNIFORMS[x] != ctx->shader_state.PS_UNIFORMS[x]) {
-            etna_coalsence_emit(stream, &coalesce, ctx->specs.ps_uniforms_offset + x*4, ctx->shader_state.PS_UNIFORMS[x]);
-            ctx->gpu3d.PS_UNIFORMS[x] = ctx->shader_state.PS_UNIFORMS[x];
-         }
-      }
-      etna_coalesce_end(stream, &coalesce);
+
+      if (dirty & (uniform_dirty_bits | ctx->shader.fs->uniforms_dirty_bits))
+         etna_uniforms_write(ctx, ctx->shader.fs, &ctx->constant_buffer[PIPE_SHADER_FRAGMENT]);
    }
 /**** End of state update ****/
 #undef EMIT_STATE
index c5b2dc14b1a04f996a7af9861bb4632b7f859a75..c8897e4ad92191806d87f0827b6942fbc1721322 100644 (file)
@@ -262,13 +262,9 @@ struct compiled_shader_state {
    uint32_t GL_VARYING_COMPONENT_USE[2];
    uint32_t GL_HALTI5_SH_SPECIALS;
    unsigned vs_inst_mem_size;
-   unsigned vs_uniforms_size;
    unsigned ps_inst_mem_size;
-   unsigned ps_uniforms_size;
    uint32_t *VS_INST_MEM;
-   uint32_t VS_UNIFORMS[ETNA_MAX_UNIFORMS * 4];
    uint32_t *PS_INST_MEM;
-   uint32_t PS_UNIFORMS[ETNA_MAX_UNIFORMS * 4];
    struct etna_reloc PS_INST_ADDR;
    struct etna_reloc VS_INST_ADDR;
 };
index d2d736bdee5d9d058b582d2c99359d2a5db82e99..479c88bb44d1b1cdbd1276367d5c52ea0951e81f 100644 (file)
@@ -293,8 +293,7 @@ dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debu
 
    pipe_debug_message(debug, SHADER_INFO, "\n"
          "SHADER-DB: %s prog %d/%d: %u instructions %u temps\n"
-         "SHADER-DB: %s prog %d/%d: %u immediates %u consts\n"
-         "SHADER-DB: %s prog %d/%d: %u loops\n",
+         "SHADER-DB: %s prog %d/%d: %u immediates %u loops\n",
          etna_shader_stage(v),
          v->shader->id, v->id,
          v->code_size,
@@ -302,9 +301,6 @@ dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debu
          etna_shader_stage(v),
          v->shader->id, v->id,
          v->uniforms.imm_count,
-         v->uniforms.const_count,
-         etna_shader_stage(v),
-         v->shader->id, v->id,
          v->num_loops);
 }
 
index 8df00ab1db2a81bc2ecbd022b9f15d19c69a9ae2..4884865e78d83d5304a7fead521ce2cc7a552759 100644 (file)
@@ -42,6 +42,7 @@
 #include "util/u_inlines.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
+#include "util/u_upload_mgr.h"
 
 static void
 etna_set_stencil_ref(struct pipe_context *pctx, const struct pipe_stencil_ref *sr)
@@ -96,6 +97,11 @@ etna_set_constant_buffer(struct pipe_context *pctx,
    /* there is no support for ARB_uniform_buffer_object */
    assert(cb->buffer == NULL && cb->user_buffer != NULL);
 
+   if (!cb->buffer) {
+      struct pipe_constant_buffer *cb = &ctx->constant_buffer[shader];
+      u_upload_data(pctx->const_uploader, 0, cb->buffer_size, 16, cb->user_buffer, &cb->buffer_offset, &cb->buffer);
+   }
+
    ctx->dirty |= ETNA_DIRTY_CONSTBUF;
 }
 
index a8d970d185a52e7c2d07a0187f0223dd4939f37a..22dbd6dbae1abb690f6016c49f2971261ea18057 100644 (file)
@@ -29,6 +29,7 @@
 #include "etnaviv_compiler.h"
 #include "etnaviv_context.h"
 #include "etnaviv_util.h"
+#include "etnaviv_emit.h"
 #include "pipe/p_defines.h"
 #include "util/u_math.h"
 
@@ -60,40 +61,55 @@ get_texrect_scale(const struct etna_context *ctx, bool frag,
 void
 etna_uniforms_write(const struct etna_context *ctx,
                     const struct etna_shader_variant *sobj,
-                    struct pipe_constant_buffer *cb, uint32_t *uniforms,
-                    unsigned *size)
+                    struct pipe_constant_buffer *cb)
 {
+   struct etna_cmd_stream *stream = ctx->stream;
    const struct etna_shader_uniform_info *uinfo = &sobj->uniforms;
-   bool frag = false;
+   bool frag = (sobj == ctx->shader.fs);
+   uint32_t base = frag ? ctx->specs.ps_uniforms_offset : ctx->specs.vs_uniforms_offset;
 
-   if (cb->user_buffer) {
-      unsigned size = MIN2(cb->buffer_size, uinfo->const_count * 4);
+   if (!uinfo->imm_count)
+      return;
 
-      memcpy(uniforms, cb->user_buffer, size);
-   }
-
-   if (sobj == ctx->shader.fs)
-      frag = true;
+   etna_cmd_stream_reserve(stream, align(uinfo->imm_count + 1, 2));
+   etna_emit_load_state(stream, base >> 2, uinfo->imm_count, 0);
 
    for (uint32_t i = 0; i < uinfo->imm_count; i++) {
+      uint32_t val = uinfo->imm_data[i];
+
       switch (uinfo->imm_contents[i]) {
       case ETNA_IMMEDIATE_CONSTANT:
-         uniforms[i + uinfo->const_count] = uinfo->imm_data[i];
+         etna_cmd_stream_emit(stream, val);
+         break;
+
+      case ETNA_IMMEDIATE_UNIFORM:
+         assert(cb->user_buffer && val * 4 < cb->buffer_size);
+         etna_cmd_stream_emit(stream, ((uint32_t*) cb->user_buffer)[val]);
          break;
 
       case ETNA_IMMEDIATE_TEXRECT_SCALE_X:
       case ETNA_IMMEDIATE_TEXRECT_SCALE_Y:
-         uniforms[i + uinfo->const_count] =
-               get_texrect_scale(ctx, frag, uinfo->imm_contents[i], uinfo->imm_data[i]);
+         etna_cmd_stream_emit(stream,
+            get_texrect_scale(ctx, frag, uinfo->imm_contents[i], val));
+         break;
+
+      case ETNA_IMMEDIATE_UBO0_ADDR ... ETNA_IMMEDIATE_UBOMAX_ADDR:
+         assert(uinfo->imm_contents[i] == ETNA_IMMEDIATE_UBO0_ADDR);
+         etna_cmd_stream_reloc(stream, &(struct etna_reloc) {
+            .bo = etna_resource(cb->buffer)->bo,
+            .flags = ETNA_RELOC_READ,
+            .offset = cb->buffer_offset + val,
+         });
          break;
 
       case ETNA_IMMEDIATE_UNUSED:
-         /* nothing to do */
+         etna_cmd_stream_emit(stream, 0);
          break;
       }
    }
 
-   *size = uinfo->const_count + uinfo->imm_count;
+   if ((uinfo->imm_count % 2) == 0)
+      etna_cmd_stream_emit(stream, 0);
 }
 
 void
@@ -103,8 +119,7 @@ etna_set_shader_uniforms_dirty_flags(struct etna_shader_variant *sobj)
 
    for (uint32_t i = 0; i < sobj->uniforms.imm_count; i++) {
       switch (sobj->uniforms.imm_contents[i]) {
-      case ETNA_IMMEDIATE_UNUSED:
-      case ETNA_IMMEDIATE_CONSTANT:
+      default:
          break;
 
       case ETNA_IMMEDIATE_TEXRECT_SCALE_X:
index 1dacd2a85d8a32ef78b7ef0227a843e207dafcd4..119d96bc1d6f541d1c5343cfb08ae2b8e785872f 100644 (file)
@@ -36,8 +36,7 @@ struct pipe_constant_buffer;
 void
 etna_uniforms_write(const struct etna_context *ctx,
                     const struct etna_shader_variant *sobj,
-                    struct pipe_constant_buffer *cb, uint32_t *uniforms,
-                    unsigned *size);
+                    struct pipe_constant_buffer *cb);
 
 void
 etna_set_shader_uniforms_dirty_flags(struct etna_shader_variant *sobj);