"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/gallium/drivers/etnaviv/etnaviv_shader.c" (16 Sep 2020, 15712 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "etnaviv_shader.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.1.5_vs_20.2.0-rc1.

    1 /*
    2  * Copyright (c) 2012-2015 Etnaviv Project
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a
    5  * copy of this software and associated documentation files (the "Software"),
    6  * to deal in the Software without restriction, including without limitation
    7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
    8  * and/or sell copies of the Software, and to permit persons to whom the
    9  * Software is furnished to do so, subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice (including the
   12  * next paragraph) shall be included in all copies or substantial portions
   13  * of the Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
   18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   21  * DEALINGS IN THE SOFTWARE.
   22  *
   23  * Authors:
   24  *    Wladimir J. van der Laan <laanwj@gmail.com>
   25  */
   26 
   27 #include "etnaviv_shader.h"
   28 
   29 #include "etnaviv_compiler.h"
   30 #include "etnaviv_context.h"
   31 #include "etnaviv_debug.h"
   32 #include "etnaviv_screen.h"
   33 #include "etnaviv_util.h"
   34 
   35 #include "tgsi/tgsi_parse.h"
   36 #include "nir/tgsi_to_nir.h"
   37 #include "util/u_math.h"
   38 #include "util/u_memory.h"
   39 
   40 /* Upload shader code to bo, if not already done */
   41 static bool etna_icache_upload_shader(struct etna_context *ctx, struct etna_shader_variant *v)
   42 {
   43    if (v->bo)
   44       return true;
   45    v->bo = etna_bo_new(ctx->screen->dev, v->code_size*4, DRM_ETNA_GEM_CACHE_WC);
   46    if (!v->bo)
   47       return false;
   48 
   49    void *buf = etna_bo_map(v->bo);
   50    etna_bo_cpu_prep(v->bo, DRM_ETNA_PREP_WRITE);
   51    memcpy(buf, v->code, v->code_size*4);
   52    etna_bo_cpu_fini(v->bo);
   53    DBG("Uploaded %s of %u words to bo %p", v->stage == MESA_SHADER_FRAGMENT ? "fs":"vs", v->code_size, v->bo);
   54    return true;
   55 }
   56 
   57 /* Link vs and fs together: fill in shader_state from vs and fs
   58  * as this function is called every time a new fs or vs is bound, the goal is to
   59  * do little processing as possible here, and to precompute as much as possible in
   60  * the vs/fs shader_object.
   61  *
   62  * XXX we could cache the link result for a certain set of VS/PS; usually a pair
   63  * of VS and PS will be used together anyway.
   64  */
   65 static bool
   66 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
   67                   struct etna_shader_variant *vs, struct etna_shader_variant *fs)
   68 {
   69    struct etna_shader_link_info link = { };
   70    bool failed;
   71 
   72    assert(vs->stage == MESA_SHADER_VERTEX);
   73    assert(fs->stage == MESA_SHADER_FRAGMENT);
   74 
   75 #ifdef DEBUG
   76    if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
   77       if (DBG_ENABLED(ETNA_DBG_NIR)) {
   78          etna_dump_shader_nir(vs);
   79          etna_dump_shader_nir(fs);
   80       } else {
   81          etna_dump_shader(vs);
   82          etna_dump_shader(fs);
   83       }
   84    }
   85 #endif
   86 
   87    if (DBG_ENABLED(ETNA_DBG_NIR))
   88       failed = etna_link_shader_nir(&link, vs, fs);
   89    else
   90       failed = etna_link_shader(&link, vs, fs);
   91 
   92    if (failed) {
   93       /* linking failed: some fs inputs do not have corresponding
   94        * vs outputs */
   95       assert(0);
   96 
   97       return false;
   98    }
   99 
  100    if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
  101       debug_printf("link result:\n");
  102       debug_printf("  vs  -> fs  comps use     pa_attr\n");
  103 
  104       for (int idx = 0; idx < link.num_varyings; ++idx)
  105          debug_printf("  t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
  106                       link.varyings[idx].reg, idx + 1,
  107                       link.varyings[idx].num_components, "xyzw",
  108                       link.varyings[idx].use[0], link.varyings[idx].use[1],
  109                       link.varyings[idx].use[2], link.varyings[idx].use[3],
  110                       link.varyings[idx].pa_attributes);
  111    }
  112 
  113    /* set last_varying_2x flag if the last varying has 1 or 2 components */
  114    bool last_varying_2x = false;
  115    if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
  116       last_varying_2x = true;
  117 
  118    cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
  119                     COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
  120 
  121    cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
  122    for (int idx = 0; idx < link.num_varyings; ++idx)
  123       cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
  124 
  125    cs->VS_END_PC = vs->code_size / 4;
  126    cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
  127 
  128    /* vs outputs (varyings) */
  129    DEFINE_ETNA_BITARRAY(vs_output, 16, 8) = {0};
  130    int varid = 0;
  131    etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
  132    for (int idx = 0; idx < link.num_varyings; ++idx)
  133       etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
  134    if (vs->vs_pointsize_out_reg >= 0)
  135       etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
  136 
  137    for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
  138       cs->VS_OUTPUT[idx] = vs_output[idx];
  139 
  140    if (vs->vs_pointsize_out_reg != -1) {
  141       /* vertex shader outputs point coordinate, provide extra output and make
  142        * sure PA config is
  143        * not masked */
  144       cs->PA_CONFIG = ~0;
  145       cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
  146    } else {
  147       /* vertex shader does not output point coordinate, make sure thate
  148        * POINT_SIZE_ENABLE is masked
  149        * and no extra output is given */
  150       cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
  151       cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
  152    }
  153 
  154    /* if fragment shader doesn't read pointcoord, disable it */
  155    if (link.pcoord_varying_comp_ofs == -1)
  156       cs->PA_CONFIG &= ~VIVS_PA_CONFIG_POINT_SPRITE_ENABLE;
  157 
  158    cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
  159    cs->VS_START_PC = 0;
  160 
  161    cs->PS_END_PC = fs->code_size / 4;
  162    cs->PS_OUTPUT_REG = fs->ps_color_out_reg;
  163    cs->PS_INPUT_COUNT =
  164       VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
  165       VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
  166    cs->PS_TEMP_REGISTER_CONTROL =
  167       VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
  168    cs->PS_START_PC = 0;
  169 
  170    /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
  171     * mode, avoids some fumbling in sync_context. */
  172    cs->PS_INPUT_COUNT_MSAA =
  173       VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) | /* MSAA adds another input */
  174       VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
  175    cs->PS_TEMP_REGISTER_CONTROL_MSAA =
  176       VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 2));
  177 
  178    uint32_t total_components = 0;
  179    DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
  180    DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
  181    for (int idx = 0; idx < link.num_varyings; ++idx) {
  182       const struct etna_varying *varying = &link.varyings[idx];
  183 
  184       etna_bitarray_set(num_components, 4, idx, varying->num_components);
  185       for (int comp = 0; comp < varying->num_components; ++comp) {
  186          etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
  187          total_components += 1;
  188       }
  189    }
  190 
  191    cs->GL_VARYING_TOTAL_COMPONENTS =
  192       VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
  193    cs->GL_VARYING_NUM_COMPONENTS[0] = num_components[0];
  194    cs->GL_VARYING_NUM_COMPONENTS[1] = num_components[1];
  195    cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
  196    cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
  197 
  198    cs->GL_HALTI5_SH_SPECIALS =
  199       0x7f7f0000 | /* unknown bits, probably other PS inputs */
  200       /* pointsize is last (see above) */
  201       VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
  202                                               cs->VS_OUTPUT_COUNT * 4 : 0x00) |
  203       VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
  204                                               link.pcoord_varying_comp_ofs : 0x7f);
  205 
  206    /* mask out early Z bit when frag depth is written */
  207    cs->PE_DEPTH_CONFIG = ~COND(fs->ps_depth_out_reg >= 0, VIVS_PE_DEPTH_CONFIG_EARLY_Z);
  208 
  209    /* reference instruction memory */
  210    cs->vs_inst_mem_size = vs->code_size;
  211    cs->VS_INST_MEM = vs->code;
  212 
  213    cs->ps_inst_mem_size = fs->code_size;
  214    cs->PS_INST_MEM = fs->code;
  215 
  216    if (vs->needs_icache || fs->needs_icache) {
  217       /* If either of the shaders needs ICACHE, we use it for both. It is
  218        * either switched on or off for the entire shader processor.
  219        */
  220       if (!etna_icache_upload_shader(ctx, vs) ||
  221           !etna_icache_upload_shader(ctx, fs)) {
  222          assert(0);
  223          return false;
  224       }
  225 
  226       cs->VS_INST_ADDR.bo = vs->bo;
  227       cs->VS_INST_ADDR.offset = 0;
  228       cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
  229       cs->PS_INST_ADDR.bo = fs->bo;
  230       cs->PS_INST_ADDR.offset = 0;
  231       cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
  232    } else {
  233       /* clear relocs */
  234       memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
  235       memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
  236    }
  237 
  238    return true;
  239 }
  240 
  241 bool
  242 etna_shader_link(struct etna_context *ctx)
  243 {
  244    if (!ctx->shader.vs || !ctx->shader.fs)
  245       return false;
  246 
  247    /* re-link vs and fs if needed */
  248    return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
  249 }
  250 
  251 static bool
  252 etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
  253                              const struct etna_shader_variant *vs,
  254                              const struct compiled_vertex_elements_state *ves)
  255 {
  256    unsigned num_temps, cur_temp, num_vs_inputs;
  257 
  258    if (!vs)
  259       return false;
  260 
  261    /* Number of vertex elements determines number of VS inputs. Otherwise,
  262     * the GPU crashes. Allocate any unused vertex elements to VS temporary
  263     * registers. */
  264    num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
  265    if (num_vs_inputs != ves->num_elements) {
  266       BUG("Number of elements %u does not match the number of VS inputs %zu",
  267           ves->num_elements, vs->infile.num_reg);
  268       return false;
  269    }
  270 
  271    cur_temp = vs->num_temps;
  272    num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
  273 
  274    cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
  275                         VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
  276    cs->VS_TEMP_REGISTER_CONTROL =
  277       VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
  278 
  279    /* vs inputs (attributes) */
  280    DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
  281    for (int idx = 0; idx < num_vs_inputs; ++idx) {
  282       if (idx < vs->infile.num_reg)
  283          etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
  284       else
  285          etna_bitarray_set(vs_input, 8, idx, cur_temp++);
  286    }
  287 
  288    if (vs->vs_id_in_reg >= 0) {
  289       cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs + 1) |
  290                            VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8) |
  291                            VIVS_VS_INPUT_COUNT_ID_ENABLE;
  292 
  293       etna_bitarray_set(vs_input, 8, num_vs_inputs, vs->vs_id_in_reg);
  294 
  295       cs->FE_HALTI5_ID_CONFIG =
  296          VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_ENABLE |
  297          VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_ENABLE |
  298          VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_REG(vs->vs_id_in_reg * 4) |
  299          VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_REG(vs->vs_id_in_reg * 4 + 1);
  300    }
  301 
  302    for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
  303       cs->VS_INPUT[idx] = vs_input[idx];
  304 
  305    return true;
  306 }
  307 
  308 static inline const char *
  309 etna_shader_stage(struct etna_shader_variant *shader)
  310 {
  311    switch (shader->stage) {
  312    case MESA_SHADER_VERTEX:     return "VERT";
  313    case MESA_SHADER_FRAGMENT:   return "FRAG";
  314    case MESA_SHADER_COMPUTE:    return "CL";
  315    default:
  316       unreachable("invalid type");
  317       return NULL;
  318    }
  319 }
  320 
  321 static void
  322 dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debug)
  323 {
  324    if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
  325       return;
  326 
  327    pipe_debug_message(debug, SHADER_INFO,
  328          "%s shader: %u instructions, %u temps, "
  329          "%u immediates, %u loops",
  330          etna_shader_stage(v),
  331          v->code_size,
  332          v->num_temps,
  333          v->uniforms.imm_count,
  334          v->num_loops);
  335 }
  336 
  337 bool
  338 etna_shader_update_vertex(struct etna_context *ctx)
  339 {
  340    return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
  341                                        ctx->vertex_elements);
  342 }
  343 
  344 static struct etna_shader_variant *
  345 create_variant(struct etna_shader *shader, struct etna_shader_key key)
  346 {
  347    struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
  348    int ret;
  349 
  350    if (!v)
  351       return NULL;
  352 
  353    v->shader = shader;
  354    v->key = key;
  355 
  356    ret = etna_compile_shader(v);
  357    if (!ret) {
  358       debug_error("compile failed!");
  359       goto fail;
  360    }
  361 
  362    v->id = ++shader->variant_count;
  363 
  364    return v;
  365 
  366 fail:
  367    FREE(v);
  368    return NULL;
  369 }
  370 
  371 struct etna_shader_variant *
  372 etna_shader_variant(struct etna_shader *shader, struct etna_shader_key key,
  373                    struct pipe_debug_callback *debug)
  374 {
  375    struct etna_shader_variant *v;
  376 
  377    for (v = shader->variants; v; v = v->next)
  378       if (etna_shader_key_equal(&key, &v->key))
  379          return v;
  380 
  381    /* compile new variant if it doesn't exist already */
  382    v = create_variant(shader, key);
  383    if (v) {
  384       v->next = shader->variants;
  385       shader->variants = v;
  386       dump_shader_info(v, debug);
  387    }
  388 
  389    return v;
  390 }
  391 
  392 static void *
  393 etna_create_shader_state(struct pipe_context *pctx,
  394                          const struct pipe_shader_state *pss)
  395 {
  396    struct etna_context *ctx = etna_context(pctx);
  397    struct etna_screen *screen = ctx->screen;
  398    struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
  399 
  400    if (!shader)
  401       return NULL;
  402 
  403    static uint32_t id;
  404    shader->id = id++;
  405    shader->specs = &screen->specs;
  406 
  407    if (DBG_ENABLED(ETNA_DBG_NIR))
  408       shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
  409                      tgsi_to_nir(pss->tokens, pctx->screen);
  410    else
  411       shader->tokens = tgsi_dup_tokens(pss->tokens);
  412 
  413 
  414 
  415    if (etna_mesa_debug & ETNA_DBG_SHADERDB) {
  416       /* if shader-db run, create a standard variant immediately
  417        * (as otherwise nothing will trigger the shader to be
  418        * actually compiled).
  419        */
  420       struct etna_shader_key key = {};
  421       etna_shader_variant(shader, key, &ctx->debug);
  422    }
  423 
  424    return shader;
  425 }
  426 
  427 static void
  428 etna_delete_shader_state(struct pipe_context *pctx, void *ss)
  429 {
  430    struct etna_shader *shader = ss;
  431    struct etna_shader_variant *v, *t;
  432 
  433    v = shader->variants;
  434    while (v) {
  435       t = v;
  436       v = v->next;
  437       if (t->bo)
  438          etna_bo_del(t->bo);
  439 
  440       if (DBG_ENABLED(ETNA_DBG_NIR))
  441          etna_destroy_shader_nir(t);
  442       else
  443          etna_destroy_shader(t);
  444    }
  445 
  446    ralloc_free(shader->nir);
  447    FREE(shader);
  448 }
  449 
  450 static void
  451 etna_bind_fs_state(struct pipe_context *pctx, void *hwcso)
  452 {
  453    struct etna_context *ctx = etna_context(pctx);
  454 
  455    ctx->shader.bind_fs = hwcso;
  456    ctx->dirty |= ETNA_DIRTY_SHADER;
  457 }
  458 
  459 static void
  460 etna_bind_vs_state(struct pipe_context *pctx, void *hwcso)
  461 {
  462    struct etna_context *ctx = etna_context(pctx);
  463 
  464    ctx->shader.bind_vs = hwcso;
  465    ctx->dirty |= ETNA_DIRTY_SHADER;
  466 }
  467 
  468 void
  469 etna_shader_init(struct pipe_context *pctx)
  470 {
  471    pctx->create_fs_state = etna_create_shader_state;
  472    pctx->bind_fs_state = etna_bind_fs_state;
  473    pctx->delete_fs_state = etna_delete_shader_state;
  474    pctx->create_vs_state = etna_create_shader_state;
  475    pctx->bind_vs_state = etna_bind_vs_state;
  476    pctx->delete_vs_state = etna_delete_shader_state;
  477 }