101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2013 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include "brw_compiler.h"
2501e04c3fSmrg#include "compiler/nir/nir.h"
2601e04c3fSmrg
2701e04c3fSmrgstatic char const *get_qual_name(int mode)
2801e04c3fSmrg{
2901e04c3fSmrg   switch (mode) {
3001e04c3fSmrg      case INTERP_MODE_NONE:          return "none";
3101e04c3fSmrg      case INTERP_MODE_FLAT:          return "flat";
3201e04c3fSmrg      case INTERP_MODE_SMOOTH:        return "smooth";
3301e04c3fSmrg      case INTERP_MODE_NOPERSPECTIVE: return "nopersp";
3401e04c3fSmrg      default:                             return "???";
3501e04c3fSmrg   }
3601e04c3fSmrg}
3701e04c3fSmrg
3801e04c3fSmrgstatic void
397ec681f3Smrggfx4_frag_prog_set_interp_modes(struct brw_wm_prog_data *prog_data,
407ec681f3Smrg                                const struct brw_vue_map *vue_map,
4101e04c3fSmrg                                unsigned location, unsigned slot_count,
4201e04c3fSmrg                                enum glsl_interp_mode interp)
4301e04c3fSmrg{
4401e04c3fSmrg   for (unsigned k = 0; k < slot_count; k++) {
4501e04c3fSmrg      unsigned slot = vue_map->varying_to_slot[location + k];
4601e04c3fSmrg      if (slot != -1 && prog_data->interp_mode[slot] == INTERP_MODE_NONE) {
4701e04c3fSmrg         prog_data->interp_mode[slot] = interp;
4801e04c3fSmrg
4901e04c3fSmrg         if (prog_data->interp_mode[slot] == INTERP_MODE_FLAT) {
5001e04c3fSmrg            prog_data->contains_flat_varying = true;
5101e04c3fSmrg         } else if (prog_data->interp_mode[slot] == INTERP_MODE_NOPERSPECTIVE) {
5201e04c3fSmrg            prog_data->contains_noperspective_varying = true;
5301e04c3fSmrg         }
5401e04c3fSmrg      }
5501e04c3fSmrg   }
5601e04c3fSmrg}
5701e04c3fSmrg
5801e04c3fSmrg/* Set up interpolation modes for every element in the VUE */
5901e04c3fSmrgvoid
607ec681f3Smrgbrw_setup_vue_interpolation(const struct brw_vue_map *vue_map, nir_shader *nir,
619f464c52Smaya                            struct brw_wm_prog_data *prog_data)
6201e04c3fSmrg{
6301e04c3fSmrg   /* Initialise interp_mode. INTERP_MODE_NONE == 0 */
6401e04c3fSmrg   memset(prog_data->interp_mode, 0, sizeof(prog_data->interp_mode));
6501e04c3fSmrg
6601e04c3fSmrg   if (!vue_map)
6701e04c3fSmrg      return;
6801e04c3fSmrg
6901e04c3fSmrg   /* HPOS always wants noperspective. setting it up here allows
7001e04c3fSmrg    * us to not need special handling in the SF program.
7101e04c3fSmrg    */
7201e04c3fSmrg   unsigned pos_slot = vue_map->varying_to_slot[VARYING_SLOT_POS];
7301e04c3fSmrg   if (pos_slot != -1) {;
7401e04c3fSmrg      prog_data->interp_mode[pos_slot] = INTERP_MODE_NOPERSPECTIVE;
7501e04c3fSmrg      prog_data->contains_noperspective_varying = true;
7601e04c3fSmrg   }
7701e04c3fSmrg
787ec681f3Smrg   nir_foreach_shader_in_variable(var, nir) {
7901e04c3fSmrg      unsigned location = var->data.location;
8001e04c3fSmrg      unsigned slot_count = glsl_count_attribute_slots(var->type, false);
8101e04c3fSmrg
827ec681f3Smrg      gfx4_frag_prog_set_interp_modes(prog_data, vue_map, location, slot_count,
8301e04c3fSmrg                                      var->data.interpolation);
8401e04c3fSmrg
8501e04c3fSmrg      if (location == VARYING_SLOT_COL0 || location == VARYING_SLOT_COL1) {
8601e04c3fSmrg         location = location + VARYING_SLOT_BFC0 - VARYING_SLOT_COL0;
877ec681f3Smrg         gfx4_frag_prog_set_interp_modes(prog_data, vue_map, location,
8801e04c3fSmrg                                         slot_count, var->data.interpolation);
8901e04c3fSmrg      }
9001e04c3fSmrg   }
9101e04c3fSmrg
927ec681f3Smrg   const bool debug = false;
9301e04c3fSmrg   if (debug) {
9401e04c3fSmrg      fprintf(stderr, "VUE map:\n");
9501e04c3fSmrg      for (int i = 0; i < vue_map->num_slots; i++) {
9601e04c3fSmrg         int varying = vue_map->slot_to_varying[i];
9701e04c3fSmrg         if (varying == -1) {
9801e04c3fSmrg            fprintf(stderr, "%d: --\n", i);
9901e04c3fSmrg            continue;
10001e04c3fSmrg         }
10101e04c3fSmrg
10201e04c3fSmrg         fprintf(stderr, "%d: %d %s ofs %d\n",
10301e04c3fSmrg                 i, varying,
10401e04c3fSmrg                 get_qual_name(prog_data->interp_mode[i]),
10501e04c3fSmrg                 brw_vue_slot_to_offset(i));
10601e04c3fSmrg      }
10701e04c3fSmrg   }
10801e04c3fSmrg}
109