Home | History | Annotate | Download | only in nir
      1 /*
      2  * Copyright  2016 Broadcom
      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 
     24 #include "nir.h"
     25 #include "nir_builder.h"
     26 
     27 /** @file nir_lower_io_to_scalar.c
     28  *
     29  * Replaces nir_load_input/nir_store_output operations with num_components !=
     30  * 1 with individual per-channel operations.
     31  */
     32 
     33 static void
     34 lower_load_input_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
     35 {
     36    b->cursor = nir_before_instr(&intr->instr);
     37 
     38    assert(intr->dest.is_ssa);
     39 
     40    nir_ssa_def *loads[4];
     41 
     42    for (unsigned i = 0; i < intr->num_components; i++) {
     43       nir_intrinsic_instr *chan_intr =
     44          nir_intrinsic_instr_create(b->shader, intr->intrinsic);
     45       nir_ssa_dest_init(&chan_intr->instr, &chan_intr->dest,
     46                         1, intr->dest.ssa.bit_size, NULL);
     47       chan_intr->num_components = 1;
     48 
     49       nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
     50       nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
     51       /* offset */
     52       chan_intr->src[0] = intr->src[0];
     53 
     54       nir_builder_instr_insert(b, &chan_intr->instr);
     55 
     56       loads[i] = &chan_intr->dest.ssa;
     57    }
     58 
     59    nir_ssa_def_rewrite_uses(&intr->dest.ssa,
     60                             nir_src_for_ssa(nir_vec(b, loads,
     61                                                     intr->num_components)));
     62    nir_instr_remove(&intr->instr);
     63 }
     64 
     65 static void
     66 lower_store_output_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
     67 {
     68    b->cursor = nir_before_instr(&intr->instr);
     69 
     70    nir_ssa_def *value = nir_ssa_for_src(b, intr->src[0], intr->num_components);
     71 
     72    for (unsigned i = 0; i < intr->num_components; i++) {
     73       if (!(nir_intrinsic_write_mask(intr) & (1 << i)))
     74          continue;
     75 
     76       nir_intrinsic_instr *chan_intr =
     77          nir_intrinsic_instr_create(b->shader, intr->intrinsic);
     78       chan_intr->num_components = 1;
     79 
     80       nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr));
     81       nir_intrinsic_set_write_mask(chan_intr, 0x1);
     82       nir_intrinsic_set_component(chan_intr, nir_intrinsic_component(intr) + i);
     83 
     84       /* value */
     85       chan_intr->src[0] = nir_src_for_ssa(nir_channel(b, value, i));
     86       /* offset */
     87       chan_intr->src[1] = intr->src[1];
     88 
     89       nir_builder_instr_insert(b, &chan_intr->instr);
     90    }
     91 
     92    nir_instr_remove(&intr->instr);
     93 }
     94 
     95 void
     96 nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask)
     97 {
     98    nir_foreach_function(function, shader) {
     99       if (function->impl) {
    100          nir_builder b;
    101          nir_builder_init(&b, function->impl);
    102 
    103          nir_foreach_block(block, function->impl) {
    104             nir_foreach_instr_safe(instr, block) {
    105                if (instr->type != nir_instr_type_intrinsic)
    106                   continue;
    107 
    108                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
    109 
    110                if (intr->num_components == 1)
    111                   continue;
    112 
    113                switch (intr->intrinsic) {
    114                case nir_intrinsic_load_input:
    115                   if (mask & nir_var_shader_in)
    116                      lower_load_input_to_scalar(&b, intr);
    117                   break;
    118                case nir_intrinsic_store_output:
    119                   if (mask & nir_var_shader_out)
    120                      lower_store_output_to_scalar(&b, intr);
    121                   break;
    122                default:
    123                   break;
    124                }
    125             }
    126          }
    127       }
    128    }
    129 }
    130