From dc91a02a72cd677b132c00b1fae2aeffefa1de1c Mon Sep 17 00:00:00 2001 From: Dave Airlie Date: Thu, 24 Jan 2019 13:07:42 +1000 Subject: [PATCH] nir: add a pass to lower flat shading. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This takes any color or backcolor that has unspecified shading and converts it to flat shading. Reviewed-by: Marek Olšák --- src/compiler/Makefile.sources | 1 + src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 2 ++ src/compiler/nir/nir_lower_flatshade.c | 50 ++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 src/compiler/nir/nir_lower_flatshade.c diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index b6429366b05..f54de56e8e6 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -245,6 +245,7 @@ NIR_FILES = \ nir/nir_lower_double_ops.c \ nir/nir_lower_drawpixels.c \ nir/nir_lower_fb_read.c \ + nir/nir_lower_flatshade.c \ nir/nir_lower_flrp.c \ nir/nir_lower_fragcoord_wtrans.c \ nir/nir_lower_frexp.c \ diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index d18c683eb0c..b6e8ea0878e 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -126,6 +126,7 @@ files_libnir = files( 'nir_lower_double_ops.c', 'nir_lower_drawpixels.c', 'nir_lower_fb_read.c', + 'nir_lower_flatshade.c', 'nir_lower_flrp.c', 'nir_lower_fragcoord_wtrans.c', 'nir_lower_frexp.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 960184b7f3c..fd7f3635cbe 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -3916,6 +3916,8 @@ void nir_lower_two_sided_color(nir_shader *shader); bool nir_lower_clamp_color_outputs(nir_shader *shader); +bool nir_lower_flatshade(nir_shader *shader); + void nir_lower_passthrough_edgeflags(nir_shader *shader); bool nir_lower_patch_vertices(nir_shader *nir, unsigned static_count, const gl_state_index16 *uniform_state_tokens); diff --git a/src/compiler/nir/nir_lower_flatshade.c b/src/compiler/nir/nir_lower_flatshade.c new file mode 100644 index 00000000000..81857847398 --- /dev/null +++ b/src/compiler/nir/nir_lower_flatshade.c @@ -0,0 +1,50 @@ +/* + * Copyright © 2015 Red Hat + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + + +#include "nir.h" +#include "nir_builder.h" + +static bool +lower_input(nir_shader *shader, nir_variable *var) +{ + if (var->data.interpolation == INTERP_MODE_NONE && + (var->data.location == VARYING_SLOT_COL0 || + var->data.location == VARYING_SLOT_COL1 || + var->data.location == VARYING_SLOT_BFC0 || + var->data.location == VARYING_SLOT_BFC1)) + var->data.interpolation = INTERP_MODE_FLAT; + return true; +} + +bool +nir_lower_flatshade(nir_shader *shader) +{ + bool progress = false; + + nir_foreach_variable(var, &shader->inputs) { + progress |= lower_input(shader, var); + } + + return progress; +} -- 2.30.2