[Mesa-dev] [RFCv3 05/11] nir: add lowering pass for y-transform

Marek Olšák maraeo at gmail.com
Tue Feb 2 11:18:54 UTC 2016


On Mon, Feb 1, 2016 at 5:00 PM, Connor Abbott <cwabbott0 at gmail.com> wrote:
> On Sun, Jan 31, 2016 at 3:16 PM, Rob Clark <robdclark at gmail.com> wrote:
>> From: Rob Clark <robclark at freedesktop.org>
>>
>> Signed-off-by: Rob Clark <robclark at freedesktop.org>
>> ---
>>  src/compiler/Makefile.sources                |   1 +
>>  src/compiler/nir/nir.h                       |  12 +
>>  src/compiler/nir/nir_lower_wpos_ytransform.c | 317 +++++++++++++++++++++++++++
>>  3 files changed, 330 insertions(+)
>>  create mode 100644 src/compiler/nir/nir_lower_wpos_ytransform.c
>>
>> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
>> index c9780d6..26c4c65 100644
>> --- a/src/compiler/Makefile.sources
>> +++ b/src/compiler/Makefile.sources
>> @@ -200,6 +200,7 @@ NIR_FILES = \
>>         nir/nir_lower_vars_to_ssa.c \
>>         nir/nir_lower_var_copies.c \
>>         nir/nir_lower_vec_to_movs.c \
>> +       nir/nir_lower_wpos_ytransform.c \
>>         nir/nir_metadata.c \
>>         nir/nir_move_vec_src_uses_to_dest.c \
>>         nir/nir_normalize_cubemap_coords.c \
>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
>> index 67fe079..a973a44 100644
>> --- a/src/compiler/nir/nir.h
>> +++ b/src/compiler/nir/nir.h
>> @@ -2119,6 +2119,18 @@ void nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables);
>>
>>  void nir_lower_two_sided_color(nir_shader *shader);
>>
>> +
>> +typedef struct nir_lower_wpos_ytransform_options {
>> +   int state_tokens[5];
>
> AFAIK the state slot stuff is only used for old assembly shaders. Why
> are we using it here?

The state slot stuff is used everywhere, including GLSL.

>
>> +   bool fs_coord_origin_upper_left :1;
>> +   bool fs_coord_origin_lower_left :1;
>> +   bool fs_coord_pixel_center_integer :1;
>> +   bool fs_coord_pixel_center_half_integer :1;
>> +} nir_lower_wpos_ytransform_options;
>> +
>> +bool nir_lower_wpos_ytransform(nir_shader *shader,
>> +                               const nir_lower_wpos_ytransform_options *options);
>> +
>>  void nir_lower_atomics(nir_shader *shader,
>>                         const struct gl_shader_program *shader_program);
>>  void nir_lower_to_source_mods(nir_shader *shader);
>> diff --git a/src/compiler/nir/nir_lower_wpos_ytransform.c b/src/compiler/nir/nir_lower_wpos_ytransform.c
>> new file mode 100644
>> index 0000000..89d160b
>> --- /dev/null
>> +++ b/src/compiler/nir/nir_lower_wpos_ytransform.c
>> @@ -0,0 +1,317 @@
>> +/*
>> + * 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"
>> +
>> +/* Lower gl_FragCoord (and fddy) to account for driver's requested coordinate-
>> + * origin and pixel-center vs. shader.  If transformation is required, a
>> + * gl_FbWposYTransform uniform is inserted (with the specified state-slots)
>> + * and additional instructions are inserted to transform gl_FragCoord (and
>> + * fddy src arg).
>> + *
>> + * This is based on the logic in emit_wpos()/emit_wpos_adjustment() in TGSI
>> + * compiler.
>> + *
>> + * Run before nir_lower_io.
>> + */
>> +
>> +typedef struct {
>> +   const nir_lower_wpos_ytransform_options *options;
>> +   nir_shader   *shader;
>> +   nir_builder   b;
>> +   nir_variable *transform;
>> +} lower_wpos_ytransform_state;
>> +
>> +static nir_ssa_def *
>> +get_transform(lower_wpos_ytransform_state *state)
>> +{
>> +   if (state->transform == NULL) {
>> +      /* NOTE: name must be prefixed w/ "gl_" to trigger slot based
>> +       * special handling in uniform setup:
>> +       */
>> +      nir_variable *var = nir_variable_create(state->shader,
>> +                                              nir_var_uniform,
>> +                                              glsl_vec4_type(),
>> +                                              "gl_FbWposYTransform");
>> +
>> +      var->num_state_slots = 1;
>> +      var->state_slots = ralloc_array(var, nir_state_slot, 1);
>> +      memcpy(var->state_slots[0].tokens, state->options->state_tokens,
>> +             sizeof(var->state_slots[0].tokens));
>> +
>> +      state->transform = var;
>> +   }
>> +   return nir_load_var(&state->b, state->transform);
>> +}
>> +
>> +/* NIR equiv of TGSI CMP instruction: */
>> +static nir_ssa_def *
>> +nir_cmp(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1, nir_ssa_def *src2)
>> +{
>> +   return nir_bcsel(b, nir_flt(b, src0, nir_imm_float(b, 0.0)), src1, src2);
>
> I'm not that familiar with TGSI, but you're selecting src1 if src0 is
> less than zero, which seems... not quite right. Is that really how
> TGSI CMP works?
>
>> +}
>> +
>> +static nir_ssa_def *
>> +nir_scalar(nir_builder *b, nir_ssa_def *def, int c)
>> +{
>> +   unsigned swizzle[4] = {c, c, c, c};
>> +   return nir_swizzle(b, def, swizzle, 4, false);
>> +}
>> +
>> +/* see emit_wpos_adjustment() in st_mesa_to_tgsi.c */
>> +static void
>> +emit_wpos_adjustment(lower_wpos_ytransform_state *state,
>> +                     nir_intrinsic_instr *intr,
>> +                     bool invert, float adjX, float adjY[2])
>> +{
>> +   nir_builder *b = &state->b;
>> +   nir_variable *fragcoord = intr->variables[0]->var;
>> +   nir_ssa_def *wpostrans, *wpos_temp, *wpos_temp_y, *wpos_input;
>> +
>> +   assert(intr->dest.is_ssa);
>> +
>> +   b->cursor = nir_before_instr(&intr->instr);
>> +
>> +   wpostrans = get_transform(state);
>> +   wpos_input = nir_load_var(b, fragcoord);
>> +
>> +   /* First, apply the coordinate shift: */
>> +   if (adjX || adjY[0] || adjY[1]) {
>> +      if (adjY[0] != adjY[1]) {
>> +         /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
>> +          * depending on whether inversion is actually going to be applied
>> +          * or not, which is determined by testing against the inversion
>> +          * state variable used below, which will be either +1 or -1.
>> +          */
>> +         nir_ssa_def *adj_temp;
>> +
>> +         adj_temp = nir_cmp(b,
>> +                            nir_scalar(b, wpostrans, invert ? 2 : 0),
>> +                            nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f),
>> +                            nir_imm_vec4(b, adjX, adjY[1], 0.0f, 0.0f));
>
> First, the nir builder will automatically splat things for you when
> the sizes are mismatched so you can just do nir_channel() here and
> avoid having to define nir_scalar().
>
> Secondly, why the weird TGSI-style comparison? Why can't you just pass
> in a normal boolean? At least, I'd like to see nir_cmp() inlined here
> -- for someone who doesn't know about TGSI, it would be easy to skip
> over the nir_cmp without realizing that there's some weird
> gallium-specific stuff going on inside here.

The CMP opcode has nothing to do with TGSI. It exists, because it's
the main conditional assignment opcode on float-only architectures.

Marek


More information about the mesa-dev mailing list