<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 17, 2019 at 12:42 PM Boris Brezillon <<a href="mailto:boris.brezillon@collabora.com">boris.brezillon@collabora.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, 17 Jun 2019 10:53:47 -0500<br>
Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>> wrote:<br>
<br>
> On Mon, Jun 17, 2019 at 5:49 AM Boris Brezillon <<br>
> <a href="mailto:boris.brezillon@collabora.com" target="_blank">boris.brezillon@collabora.com</a>> wrote:  <br>
> <br>
> > The V3D driver has an open-coded solution for this, and we need the<br>
> > same thing for Panfrost, so let's add a generic way to lower TXS(LOD)<br>
> > into max(TXS(0) >> LOD, 1).<br>
> ><br>
> > Signed-off-by: Boris Brezillon <<a href="mailto:boris.brezillon@collabora.com" target="_blank">boris.brezillon@collabora.com</a>><br>
> > ---<br>
> >  src/compiler/nir/nir.h           |  6 ++++<br>
> >  src/compiler/nir/nir_lower_tex.c | 49 ++++++++++++++++++++++++++++++++<br>
> >  2 files changed, 55 insertions(+)<br>
> ><br>
> > diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> > index 4270df565111..8972b4af7480 100644<br>
> > --- a/src/compiler/nir/nir.h<br>
> > +++ b/src/compiler/nir/nir.h<br>
> > @@ -3426,6 +3426,12 @@ typedef struct nir_lower_tex_options {<br>
> >      */<br>
> >     bool lower_txd_clamp_if_sampler_index_not_lt_16;<br>
> ><br>
> > +   /**<br>
> > +    * If true, lower nir_texop_txs with a non-0-lod into nir_texop_txs<br>
> > with<br>
> > +    * 0-lod followed by a nir_ishr.<br>
> > +    */<br>
> > +   bool lower_txs_lod;<br>
> > +<br>
> >     /**<br>
> >      * If true, apply a .bagr swizzle on tg4 results to handle Broadcom's<br>
> >      * mixed-up tg4 locations.<br>
> > diff --git a/src/compiler/nir/nir_lower_tex.c<br>
> > b/src/compiler/nir/nir_lower_tex.c<br>
> > index 53719017a87f..c29581d9b048 100644<br>
> > --- a/src/compiler/nir/nir_lower_tex.c<br>
> > +++ b/src/compiler/nir/nir_lower_tex.c<br>
> > @@ -978,6 +978,50 @@ lower_tg4_offsets(nir_builder *b, nir_tex_instr *tex)<br>
> >     return true;<br>
> >  }<br>
> ><br>
> > +static bool<br>
> > +nir_lower_txs_lod(nir_builder *b, nir_tex_instr *tex)<br>
> > +{<br>
> > +   int lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod);<br>
> > +   if (lod_idx < 0 ||<br>
> > +       (nir_src_is_const(tex->src[lod_idx].src) &&<br>
> > +        !nir_src_as_int(tex->src[lod_idx].src)))<br>
> >  <br>
> <br>
> Please use == 0 instead of ! here.  We're checking an integer, not a<br>
> boolean.<br>
<br>
Sure, I'll change that.<br>
<br>
> <br>
> <br>
> > +      return false;<br>
> > +<br>
> > +   nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[lod_idx].src, 1);<br>
> >  <br>
> <br>
> In theory, this can emit instructions.  We should set the cursor before<br>
> calling it.<br>
<br>
Oops, I'll move the cursor initialization before this line.<br>
<br>
> <br>
> <br>
> > +   unsigned dest_size = nir_tex_instr_dest_size(tex);<br>
> > +   nir_ssa_def *shift, *min, *result;<br>
> > +<br>
> > +   b->cursor = nir_after_instr(&tex->instr);<br>
> > +<br>
> > +   switch (dest_size) {<br>
> > +   case 3:<br>
> > +      shift = nir_vec3(b, lod, lod, tex->is_array ? nir_imm_int(b, 0) :<br>
> > lod);<br>
> > +      min = nir_imm_ivec3(b, 1, 1, tex->is_array ? 0 : 1);<br>
> > +      break;<br>
> > +   case 2:<br>
> > +      shift = nir_vec2(b, lod, tex->is_array ? nir_imm_int(b, 0) : lod);<br>
> > +      min = nir_imm_ivec2(b, 1, tex->is_array ? 0 : 1);<br>
> > +      break;<br>
> > +   case 1:<br>
> > +      shift = lod;<br>
> > +      min = nir_imm_int(b, 1);<br>
> > +      break;<br>
> > +   default:<br>
> > +      unreachable("Invalid nir_tex_instr_dest_size()\n");<br>
> > +   }<br>
> > +<br>
> > +   /* TXS(LOD) = max(TXS(0) >> LOD, 1) */<br>
> > +   result = nir_imax(b, nir_ishr(b, &tex->dest.ssa, shift), min);<br>
> >  <br>
> <br>
> I think it will actually emit less code and be slightly simpler if you do<br>
> <br>
> nir_ssa_def *minified =<br>
>    nir_imax(b, nir_ushr(b, &tex->dest.ssa, lod), nir_imm_int(b, 1));<br>
<br>
Are you sure for the ushr()? lod is supposed to be an int, so I'd<br>
expect to have an ishr() here (note that I don't even handle the<br>
lod < 0 case as I'm not sure what's supposed to be done in that case).<br></blockquote><div><br></div><div>I don't think it's going to matter in practice.  Neither the lod nor the size are ever validly going to be negative.  GLSL might require that we clamp the LOD to a valid range before using it in the shift.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> if (tex->is_array) {<br>
>    nir_ssa_def *comp[4];<br>
>    for (unsigned i = 0; i < dest_size - 1; i++)<br>
>       comp[i] = nir_component(b, minified, i);<br>
>    comp[dest_size - 1] = nir_component(b, &tex->dest.ssa, dest_size - 1);<br>
>    minified = nir_vec(b, comp, dest_size);<br>
> }<br>
> <br>
> That way, it generates one vec() operation instead of two.  Note that you<br>
> don't need to explcitly expand lod out to three components as nir_builder<br>
> will do that for you.<br>
<br>
Oh, good to know.<br>
<br>
> <br>
> <br>
> > +   nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),<br>
> > +                                  result->parent_instr);<br>
> > +<br>
> > +   /* Replace the non-0-LOD in the initial TXS operation by a 0-LOD. */<br>
> > +   b->cursor = nir_before_instr(&tex->instr);<br>
> > +   nir_instr_rewrite_src(&tex->instr, &tex->src[lod_idx].src,<br>
> > +                         nir_src_for_ssa(nir_imm_int(b, 0)));<br>
> >  <br>
> <br>
> Personally, I'd put this right after we grab the LOD from the instruction<br>
> but this is fine too.<br>
<br>
Okay, I'll move it there.<br>
<br>
Thanks for the review.<br>
<br>
Boris<br>
</blockquote></div></div>