Home | History | Annotate | Download | only in nir
      1 /*
      2  * Copyright  2015 Red Hat
      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, sublicense,
      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 next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * 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 NONINFRINGEMENT.  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 DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Rob Clark <robclark (at) freedesktop.org>
     25  */
     26 
     27 #include "nir.h"
     28 #include "nir_builder.h"
     29 
     30 #define MAX_COLORS 2  /* VARYING_SLOT_COL0/COL1 */
     31 
     32 typedef struct {
     33    nir_builder   b;
     34    nir_shader   *shader;
     35    struct {
     36       nir_variable *front;        /* COLn */
     37       nir_variable *back;         /* BFCn */
     38    } colors[MAX_COLORS];
     39    int colors_count;
     40 } lower_2side_state;
     41 
     42 
     43 /* Lowering pass for fragment shaders to emulated two-sided-color.  For
     44  * each COLOR input, a corresponding BCOLOR input is created, and bcsel
     45  * instruction used to select front or back color based on FACE.
     46  */
     47 
     48 static nir_variable *
     49 create_input(nir_shader *shader, unsigned drvloc, gl_varying_slot slot)
     50 {
     51    nir_variable *var = rzalloc(shader, nir_variable);
     52 
     53    var->data.driver_location = drvloc;
     54    var->type = glsl_vec4_type();
     55    var->data.mode = nir_var_shader_in;
     56    var->name = ralloc_asprintf(var, "in_%d", drvloc);
     57    var->data.index = 0;
     58    var->data.location = slot;
     59 
     60    exec_list_push_tail(&shader->inputs, &var->node);
     61 
     62    shader->num_inputs++;     /* TODO use type_size() */
     63 
     64    return var;
     65 }
     66 
     67 static nir_ssa_def *
     68 load_input(nir_builder *b, nir_variable *in)
     69 {
     70    nir_intrinsic_instr *load;
     71 
     72    load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
     73    load->num_components = 4;
     74    nir_intrinsic_set_base(load, in->data.driver_location);
     75    load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
     76    nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
     77    nir_builder_instr_insert(b, &load->instr);
     78 
     79    return &load->dest.ssa;
     80 }
     81 
     82 static int
     83 setup_inputs(lower_2side_state *state)
     84 {
     85    int maxloc = -1;
     86 
     87    /* find color inputs: */
     88    nir_foreach_variable(var, &state->shader->inputs) {
     89       int loc = var->data.driver_location;
     90 
     91       /* keep track of last used driver-location.. we'll be
     92        * appending BCLr after last existing input:
     93        */
     94       maxloc = MAX2(maxloc, loc);
     95 
     96       switch (var->data.location) {
     97       case VARYING_SLOT_COL0:
     98       case VARYING_SLOT_COL1:
     99          assert(state->colors_count < ARRAY_SIZE(state->colors));
    100          state->colors[state->colors_count].front = var;
    101          state->colors_count++;
    102          break;
    103       }
    104    }
    105 
    106    /* if we don't have any color inputs, nothing to do: */
    107    if (state->colors_count == 0)
    108       return -1;
    109 
    110    /* add required back-face color inputs: */
    111    for (int i = 0; i < state->colors_count; i++) {
    112       gl_varying_slot slot;
    113 
    114       if (state->colors[i].front->data.location == VARYING_SLOT_COL0)
    115          slot = VARYING_SLOT_BFC0;
    116       else
    117          slot = VARYING_SLOT_BFC1;
    118 
    119       state->colors[i].back = create_input(state->shader, ++maxloc, slot);
    120    }
    121 
    122    return 0;
    123 }
    124 
    125 static bool
    126 nir_lower_two_sided_color_block(nir_block *block,
    127                                 lower_2side_state *state)
    128 {
    129    nir_builder *b = &state->b;
    130 
    131    nir_foreach_instr_safe(instr, block) {
    132       if (instr->type != nir_instr_type_intrinsic)
    133          continue;
    134 
    135       nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
    136 
    137       if (intr->intrinsic != nir_intrinsic_load_input)
    138          continue;
    139 
    140       int idx;
    141       for (idx = 0; idx < state->colors_count; idx++) {
    142          unsigned drvloc =
    143             state->colors[idx].front->data.driver_location;
    144          if (nir_intrinsic_base(intr) == drvloc) {
    145             assert(nir_src_as_const_value(intr->src[0]));
    146             break;
    147          }
    148       }
    149 
    150       if (idx == state->colors_count)
    151          continue;
    152 
    153       /* replace load_input(COLn) with
    154        * bcsel(load_system_value(FACE), load_input(COLn), load_input(BFCn))
    155        */
    156       b->cursor = nir_before_instr(&intr->instr);
    157       nir_ssa_def *face  = nir_load_front_face(b);
    158       nir_ssa_def *front = load_input(b, state->colors[idx].front);
    159       nir_ssa_def *back  = load_input(b, state->colors[idx].back);
    160       nir_ssa_def *color = nir_bcsel(b, face, front, back);
    161 
    162       assert(intr->dest.is_ssa);
    163       nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(color));
    164    }
    165 
    166    return true;
    167 }
    168 
    169 static void
    170 nir_lower_two_sided_color_impl(nir_function_impl *impl,
    171                                lower_2side_state *state)
    172 {
    173    nir_builder *b = &state->b;
    174 
    175    nir_builder_init(b, impl);
    176 
    177    nir_foreach_block(block, impl) {
    178       nir_lower_two_sided_color_block(block, state);
    179    }
    180 
    181    nir_metadata_preserve(impl, nir_metadata_block_index |
    182                                nir_metadata_dominance);
    183 }
    184 
    185 void
    186 nir_lower_two_sided_color(nir_shader *shader)
    187 {
    188    lower_2side_state state = {
    189       .shader = shader,
    190    };
    191 
    192    if (shader->stage != MESA_SHADER_FRAGMENT)
    193       return;
    194 
    195    if (setup_inputs(&state) != 0)
    196       return;
    197 
    198    nir_foreach_function(function, shader) {
    199       if (function->impl)
    200          nir_lower_two_sided_color_impl(function->impl, &state);
    201    }
    202 
    203 }
    204