pan/midgard: Add LOD bias/clamp lowering
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Thu, 21 Nov 2019 13:43:53 +0000 (08:43 -0500)
committerTomeu Vizoso <tomeu.vizoso@collabora.co.uk>
Fri, 22 Nov 2019 05:07:19 +0000 (05:07 +0000)
We fetch the info with the new intrinsic and lower with ALU ops for txl
instructions, which seemingly correspond to "TEXGRD" instructions (what
we call textureLod).

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
src/panfrost/Makefile.sources
src/panfrost/midgard/compiler.h
src/panfrost/midgard/meson.build
src/panfrost/midgard/midgard_errata_lod.c [new file with mode: 0644]

index 5b2535bc42aba019cfddadd4c8e210fbeb0a5f40..1dcb0bd2d028846a2a10a9c9eb60bb0119735ca1 100644 (file)
@@ -43,6 +43,7 @@ midgard_FILES := \
         midgard/midgard_ra.c \
         midgard/midgard_ra_pipeline.c \
         midgard/midgard_schedule.c \
+        midgard/midgard_errata_lod.c \
         midgard/mir.c \
         midgard/mir_promote_uniforms.c
 
@@ -56,4 +57,4 @@ pandecode_FILES := \
         pandecode/decode.h \
         pandecode/pan_pretty_print.c \
         pandecode/pan_pretty_print.h \
-        pandecode/public.h
\ No newline at end of file
+        pandecode/public.h
index 7a203da902a18f1d04f120687f47f36a5173cb9b..f2fc5d361466c942982f1c55745cc2d1072bd83d 100644 (file)
@@ -635,6 +635,8 @@ void emit_binary_bundle(
 bool
 nir_undef_to_zero(nir_shader *shader);
 
+void midgard_nir_lod_errata(nir_shader *shader);
+
 /* Optimizations */
 
 bool midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block);
index e6fa2019bb0d7422fcfbfd4976f1437295770d37..fcb3089c6e204797455b4fa6afa3d24da7bff144 100644 (file)
@@ -36,6 +36,7 @@ libpanfrost_midgard_files = files(
   'midgard_opt_invert.c',
   'midgard_opt_float.c',
   'midgard_opt_perspective.c',
+  'midgard_errata_lod.c',
   'disassemble.c',
   'lcra.c'
 )
diff --git a/src/panfrost/midgard/midgard_errata_lod.c b/src/panfrost/midgard/midgard_errata_lod.c
new file mode 100644 (file)
index 0000000..55e2b23
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2019 Collabora, Ltd.
+ *
+ * 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 "compiler/nir/nir.h"
+#include "compiler/nir/nir_builder.h"
+
+void midgard_nir_lod_errata(nir_shader *shader);
+
+/* Workarounds errata pertaining to early Midgard chips where the settings for
+ * min_lod/max_lod/lod_bias are ignored in the sampler descriptor when
+ * texturing with a textureLod instruction. The workaround is to load these
+ * constants in as system values and perform the bias/clamp in the shader.
+ */
+
+static void
+mir_lod_errata_body(nir_builder *b, nir_tex_instr *tex)
+{
+        /* The errata only applies to textureLod ("TEXGRD") */
+        if (tex->op != nir_texop_txl)
+                return;
+
+        /* Let's grab the sampler parameters */
+        nir_intrinsic_instr *l = nir_intrinsic_instr_create(b->shader,
+                        nir_intrinsic_load_sampler_lod_parameters_pan);
+        l->num_components = 3;
+        nir_ssa_dest_init(&l->instr, &l->dest, 3, 32, NULL);
+
+        /* TODO: Indirect samplers, separate sampler objects XXX */
+        nir_src idx = nir_src_for_ssa(nir_imm_int(b, tex->texture_index));
+        nir_src_copy(&l->src[0], &idx, l);
+
+        nir_builder_instr_insert(b, &l->instr);
+        nir_ssa_def *params = &l->dest.ssa;
+
+        /* Extract the individual components */
+        nir_ssa_def *min_lod = nir_channel(b, params, 0);
+        nir_ssa_def *max_lod = nir_channel(b, params, 1);
+        nir_ssa_def *lod_bias = nir_channel(b, params, 2);
+        
+        /* Rewrite the LOD with bias/clamps. Order sensitive. */
+        for (unsigned i = 0; i < tex->num_srcs; i++) {
+                if (tex->src[i].src_type != nir_tex_src_lod)
+                        continue;
+
+                nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[i].src, 1);
+
+                nir_ssa_def *biased = nir_fadd(b, lod, lod_bias);
+                nir_ssa_def *clamped = nir_fmin(b,
+                                nir_fmax(b, biased, min_lod), max_lod);
+
+                nir_instr_rewrite_src(&tex->instr, &tex->src[i].src,
+                                nir_src_for_ssa(clamped));
+        }
+}
+
+void
+midgard_nir_lod_errata(nir_shader *shader)
+{
+        nir_foreach_function(function, shader) {
+                if (!function->impl) continue;
+
+                nir_builder b;
+                nir_builder_init(&b, function->impl);
+
+                nir_foreach_block(block, function->impl) {
+                        nir_foreach_instr_safe(instr, block) {
+                                if (instr->type != nir_instr_type_tex) continue;
+
+                                nir_tex_instr *tex = nir_instr_as_tex(instr);
+                                b.cursor = nir_before_instr(instr);
+                                mir_lod_errata_body(&b, tex);
+                        }
+                }
+
+                nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
+
+        }
+}