etnaviv: compiled_framebuffer_state: get rid of SE_SCISSOR_*
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_shader.c
index 0ef611fdff95728b13c9da20125775d62178e47b..3ac619ac9d743d5c762e9ad66ab17012bdde27ff 100644 (file)
 #include "etnaviv_compiler.h"
 #include "etnaviv_context.h"
 #include "etnaviv_debug.h"
+#include "etnaviv_screen.h"
 #include "etnaviv_util.h"
 
 #include "tgsi/tgsi_parse.h"
+#include "nir/tgsi_to_nir.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 
+/* Upload shader code to bo, if not already done */
+static bool etna_icache_upload_shader(struct etna_context *ctx, struct etna_shader_variant *v)
+{
+   if (v->bo)
+      return true;
+   v->bo = etna_bo_new(ctx->screen->dev, v->code_size*4, DRM_ETNA_GEM_CACHE_WC);
+   if (!v->bo)
+      return false;
+
+   void *buf = etna_bo_map(v->bo);
+   etna_bo_cpu_prep(v->bo, DRM_ETNA_PREP_WRITE);
+   memcpy(buf, v->code, v->code_size*4);
+   etna_bo_cpu_fini(v->bo);
+   DBG("Uploaded %s of %u words to bo %p", v->stage == MESA_SHADER_FRAGMENT ? "fs":"vs", v->code_size, v->bo);
+   return true;
+}
+
 /* Link vs and fs together: fill in shader_state from vs and fs
  * as this function is called every time a new fs or vs is bound, the goal is to
  * do little processing as possible here, and to precompute as much as possible in
  */
 static bool
 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
-                  const struct etna_shader_variant *vs, const struct etna_shader_variant *fs)
+                  struct etna_shader_variant *vs, struct etna_shader_variant *fs)
 {
    struct etna_shader_link_info link = { };
+   bool failed;
 
-   assert(vs->processor == PIPE_SHADER_VERTEX);
-   assert(fs->processor == PIPE_SHADER_FRAGMENT);
+   assert(vs->stage == MESA_SHADER_VERTEX);
+   assert(fs->stage == MESA_SHADER_FRAGMENT);
 
 #ifdef DEBUG
    if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
-      etna_dump_shader(vs);
-      etna_dump_shader(fs);
+      if (DBG_ENABLED(ETNA_DBG_NIR)) {
+         etna_dump_shader_nir(vs);
+         etna_dump_shader_nir(fs);
+      } else {
+         etna_dump_shader(vs);
+         etna_dump_shader(fs);
+      }
    }
 #endif
 
-   if (etna_link_shader(&link, vs, fs)) {
+   if (DBG_ENABLED(ETNA_DBG_NIR))
+      failed = etna_link_shader_nir(&link, vs, fs);
+   else
+      failed = etna_link_shader(&link, vs, fs);
+
+   if (failed) {
       /* linking failed: some fs inputs do not have corresponding
        * vs outputs */
       assert(0);
@@ -121,6 +151,10 @@ etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
       cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
    }
 
+   /* if fragment shader doesn't read pointcoord, disable it */
+   if (link.pcoord_varying_comp_ofs == -1)
+      cs->PA_CONFIG &= ~VIVS_PA_CONFIG_POINT_SPRITE_ENABLE;
+
    cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
    cs->VS_START_PC = 0;
 
@@ -131,7 +165,6 @@ etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
       VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
    cs->PS_TEMP_REGISTER_CONTROL =
       VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
-   cs->PS_CONTROL = VIVS_PS_CONTROL_UNK1; /* XXX when can we set BYPASS? */
    cs->PS_START_PC = 0;
 
    /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
@@ -157,32 +190,66 @@ etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
 
    cs->GL_VARYING_TOTAL_COMPONENTS =
       VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
-   cs->GL_VARYING_NUM_COMPONENTS = num_components[0];
+   cs->GL_VARYING_NUM_COMPONENTS[0] = num_components[0];
+   cs->GL_VARYING_NUM_COMPONENTS[1] = num_components[1];
    cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
    cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
 
+   cs->GL_HALTI5_SH_SPECIALS =
+      0x7f7f0000 | /* unknown bits, probably other PS inputs */
+      /* pointsize is last (see above) */
+      VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
+                                              cs->VS_OUTPUT_COUNT * 4 : 0x00) |
+      VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
+                                              link.pcoord_varying_comp_ofs : 0x7f);
+
+   /* mask out early Z bit when frag depth is written */
+   cs->PE_DEPTH_CONFIG = ~COND(fs->ps_depth_out_reg >= 0, VIVS_PE_DEPTH_CONFIG_EARLY_Z);
+
    /* reference instruction memory */
    cs->vs_inst_mem_size = vs->code_size;
    cs->VS_INST_MEM = vs->code;
+
    cs->ps_inst_mem_size = fs->code_size;
    cs->PS_INST_MEM = fs->code;
 
+   if (vs->needs_icache || fs->needs_icache) {
+      /* If either of the shaders needs ICACHE, we use it for both. It is
+       * either switched on or off for the entire shader processor.
+       */
+      if (!etna_icache_upload_shader(ctx, vs) ||
+          !etna_icache_upload_shader(ctx, fs)) {
+         assert(0);
+         return false;
+      }
+
+      cs->VS_INST_ADDR.bo = vs->bo;
+      cs->VS_INST_ADDR.offset = 0;
+      cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
+      cs->PS_INST_ADDR.bo = fs->bo;
+      cs->PS_INST_ADDR.offset = 0;
+      cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
+   } else {
+      /* clear relocs */
+      memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
+      memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
+   }
+
    return true;
 }
 
 bool
 etna_shader_link(struct etna_context *ctx)
 {
-   if (!ctx->vs || !ctx->fs)
+   if (!ctx->shader.vs || !ctx->shader.fs)
       return false;
 
    /* re-link vs and fs if needed */
-   return etna_link_shaders(ctx, &ctx->shader_state, ctx->vs, ctx->fs);
+   return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
 }
 
 static bool
-etna_shader_update_vs_inputs(struct etna_context *ctx,
-                             struct compiled_shader_state *cs,
+etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
                              const struct etna_shader_variant *vs,
                              const struct compiled_vertex_elements_state *ves)
 {
@@ -197,7 +264,7 @@ etna_shader_update_vs_inputs(struct etna_context *ctx,
    num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
    if (num_vs_inputs != ves->num_elements) {
       BUG("Number of elements %u does not match the number of VS inputs %zu",
-          ctx->vertex_elements->num_elements, ctx->vs->infile.num_reg);
+          ves->num_elements, vs->infile.num_reg);
       return false;
    }
 
@@ -218,6 +285,20 @@ etna_shader_update_vs_inputs(struct etna_context *ctx,
          etna_bitarray_set(vs_input, 8, idx, cur_temp++);
    }
 
+   if (vs->vs_id_in_reg >= 0) {
+      cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs + 1) |
+                           VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8) |
+                           VIVS_VS_INPUT_COUNT_ID_ENABLE;
+
+      etna_bitarray_set(vs_input, 8, num_vs_inputs, vs->vs_id_in_reg);
+
+      cs->FE_HALTI5_ID_CONFIG =
+         VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_ENABLE |
+         VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_ENABLE |
+         VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_REG(vs->vs_id_in_reg * 4) |
+         VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_REG(vs->vs_id_in_reg * 4 + 1);
+   }
+
    for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
       cs->VS_INPUT[idx] = vs_input[idx];
 
@@ -227,10 +308,10 @@ etna_shader_update_vs_inputs(struct etna_context *ctx,
 static inline const char *
 etna_shader_stage(struct etna_shader_variant *shader)
 {
-   switch (shader->processor) {
-   case PIPE_SHADER_VERTEX:     return "VERT";
-   case PIPE_SHADER_FRAGMENT:   return "FRAG";
-   case PIPE_SHADER_COMPUTE:    return "CL";
+   switch (shader->stage) {
+   case MESA_SHADER_VERTEX:     return "VERT";
+   case MESA_SHADER_FRAGMENT:   return "FRAG";
+   case MESA_SHADER_COMPUTE:    return "CL";
    default:
       unreachable("invalid type");
       return NULL;
@@ -238,48 +319,73 @@ etna_shader_stage(struct etna_shader_variant *shader)
 }
 
 static void
-dump_shader_info(struct etna_shader_variant *shader, struct pipe_debug_callback *debug)
+dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debug)
 {
    if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
       return;
 
-   pipe_debug_message(debug, SHADER_INFO, "\n"
-         "SHADER-DB: %s prog %d: %u instructions %u temps\n"
-         "SHADER-DB: %s prog %d: %u immediates %u consts\n"
-         "SHADER-DB: %s prog %d: %u loops\n",
-         etna_shader_stage(shader),
-         shader->id,
-         shader->code_size,
-         shader->num_temps,
-         etna_shader_stage(shader),
-         shader->id,
-         shader->uniforms.imm_count,
-         shader->uniforms.const_count,
-         etna_shader_stage(shader),
-         shader->id,
-         shader->num_loops);
+   pipe_debug_message(debug, SHADER_INFO,
+         "%s shader: %u instructions, %u temps, "
+         "%u immediates, %u loops",
+         etna_shader_stage(v),
+         v->code_size,
+         v->num_temps,
+         v->uniforms.imm_count,
+         v->num_loops);
 }
 
 bool
 etna_shader_update_vertex(struct etna_context *ctx)
 {
-   return etna_shader_update_vs_inputs(ctx, &ctx->shader_state, ctx->vs,
+   return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
                                        ctx->vertex_elements);
 }
 
 static struct etna_shader_variant *
-create_variant(struct etna_shader *shader)
+create_variant(struct etna_shader *shader, struct etna_shader_key key)
 {
-   struct etna_shader_variant *v;
+   struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
+   int ret;
 
-   v = etna_compile_shader(shader->specs, shader->tokens);
-   if (!v) {
-      debug_error("compile failed!");
+   if (!v)
       return NULL;
+
+   v->shader = shader;
+   v->key = key;
+
+   ret = etna_compile_shader(v);
+   if (!ret) {
+      debug_error("compile failed!");
+      goto fail;
    }
 
    v->id = ++shader->variant_count;
 
+   return v;
+
+fail:
+   FREE(v);
+   return NULL;
+}
+
+struct etna_shader_variant *
+etna_shader_variant(struct etna_shader *shader, struct etna_shader_key key,
+                   struct pipe_debug_callback *debug)
+{
+   struct etna_shader_variant *v;
+
+   for (v = shader->variants; v; v = v->next)
+      if (etna_shader_key_equal(&key, &v->key))
+         return v;
+
+   /* compile new variant if it doesn't exist already */
+   v = create_variant(shader, key);
+   if (v) {
+      v->next = shader->variants;
+      shader->variants = v;
+      dump_shader_info(v, debug);
+   }
+
    return v;
 }
 
@@ -288,6 +394,7 @@ etna_create_shader_state(struct pipe_context *pctx,
                          const struct pipe_shader_state *pss)
 {
    struct etna_context *ctx = etna_context(pctx);
+   struct etna_screen *screen = ctx->screen;
    struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
 
    if (!shader)
@@ -295,20 +402,26 @@ etna_create_shader_state(struct pipe_context *pctx,
 
    static uint32_t id;
    shader->id = id++;
-   shader->specs = &ctx->specs;
-   shader->tokens = tgsi_dup_tokens(pss->tokens);
+   shader->specs = &screen->specs;
 
-   /* compile new variant */
-   struct etna_shader_variant *v;
+   if (DBG_ENABLED(ETNA_DBG_NIR))
+      shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
+                     tgsi_to_nir(pss->tokens, pctx->screen);
+   else
+      shader->tokens = tgsi_dup_tokens(pss->tokens);
 
-   v = create_variant(shader);
-   if (v) {
-      v->next = shader->variants;
-      shader->variants = v;
-      dump_shader_info(v, &ctx->debug);
+
+
+   if (etna_mesa_debug & ETNA_DBG_SHADERDB) {
+      /* if shader-db run, create a standard variant immediately
+       * (as otherwise nothing will trigger the shader to be
+       * actually compiled).
+       */
+      struct etna_shader_key key = {};
+      etna_shader_variant(shader, key, &ctx->debug);
    }
 
-   return v;
+   return shader;
 }
 
 static void
@@ -321,38 +434,34 @@ etna_delete_shader_state(struct pipe_context *pctx, void *ss)
    while (v) {
       t = v;
       v = v->next;
-      etna_destroy_shader(t);
+      if (t->bo)
+         etna_bo_del(t->bo);
+
+      if (DBG_ENABLED(ETNA_DBG_NIR))
+         etna_destroy_shader_nir(t);
+      else
+         etna_destroy_shader(t);
    }
 
-   FREE(shader->tokens);
+   ralloc_free(shader->nir);
    FREE(shader);
 }
 
 static void
-etna_bind_fs_state(struct pipe_context *pctx, void *fss_)
+etna_bind_fs_state(struct pipe_context *pctx, void *hwcso)
 {
    struct etna_context *ctx = etna_context(pctx);
-   struct etna_shader_variant *fss = fss_;
-
-   if (ctx->fs == fss) /* skip if already bound */
-      return;
 
-   assert(fss == NULL || fss->processor == PIPE_SHADER_FRAGMENT);
-   ctx->fs = fss;
+   ctx->shader.bind_fs = hwcso;
    ctx->dirty |= ETNA_DIRTY_SHADER;
 }
 
 static void
-etna_bind_vs_state(struct pipe_context *pctx, void *vss_)
+etna_bind_vs_state(struct pipe_context *pctx, void *hwcso)
 {
    struct etna_context *ctx = etna_context(pctx);
-   struct etna_shader_variant *vss = vss_;
-
-   if (ctx->vs == vss) /* skip if already bound */
-      return;
 
-   assert(vss == NULL || vss->processor == PIPE_SHADER_VERTEX);
-   ctx->vs = vss;
+   ctx->shader.bind_vs = hwcso;
    ctx->dirty |= ETNA_DIRTY_SHADER;
 }