<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 17, 2019 at 5:49 AM 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">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 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 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></blockquote><div><br></div><div>Please use == 0 instead of ! here. We're checking an integer, not a boolean.<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">
+ return false;<br>
+<br>
+ nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[lod_idx].src, 1);<br></blockquote><div><br></div><div>In theory, this can emit instructions. We should set the cursor before calling it.<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">
+ 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) : 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></blockquote><div><br></div><div>I think it will actually emit less code and be slightly simpler if you do</div><div><br></div><div>nir_ssa_def *minified =</div><div> nir_imax(b, nir_ushr(b, &tex->dest.ssa, lod), nir_imm_int(b, 1));</div><div>if (tex->is_array) {</div><div> nir_ssa_def *comp[4];</div><div> for (unsigned i = 0; i < dest_size - 1; i++)<br></div><div> comp[i] = nir_component(b, minified, i);</div><div> comp[dest_size - 1] = nir_component(b, &tex->dest.ssa, dest_size - 1);</div><div> minified = nir_vec(b, comp, dest_size);<br></div><div>}</div><div><br></div><div>That way, it generates one vec() operation instead of two. Note that you don't need to explcitly expand lod out to three components as nir_builder will do that for you.<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">
+ 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></blockquote><div><br></div><div>Personally, I'd put this right after we grab the LOD from the instruction but this is fine too.<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">
+ return true;<br>
+}<br>
+<br>
static bool<br>
nir_lower_tex_block(nir_block *block, nir_builder *b,<br>
const nir_lower_tex_options *options)<br>
@@ -1132,6 +1176,11 @@ nir_lower_tex_block(nir_block *block, nir_builder *b,<br>
continue;<br>
}<br>
<br>
+ if (options->lower_txs_lod && tex->op == nir_texop_txs) {<br>
+ progress |= nir_lower_txs_lod(b, tex);<br>
+ continue;<br>
+ }<br>
+<br>
/* has to happen after all the other lowerings as the original tg4 gets<br>
* replaced by 4 tg4 instructions.<br>
*/<br>
-- <br>
2.20.1<br>
<br>
</blockquote></div></div>