Mesa (main): r300: transform vs sin and cos input to [-PI,PI] range in NIR

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Feb 11 16:18:39 UTC 2022


Module: Mesa
Branch: main
Commit: 3f97306b956f707d637da6b76dd9465fcabfc451
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=3f97306b956f707d637da6b76dd9465fcabfc451

Author: Pavel Ondračka <pavel.ondracka at gmail.com>
Date:   Wed Feb  9 15:48:04 2022 +0100

r300: transform vs sin and cos input to [-PI,PI] range in NIR

The python generator is mostly copy-pasted from lima.

Reviewed-by: Emma Anholt <emma at anholt.net>
Reviewed-by: Dylan Baker <dylan at pnwbakers.com>
Signed-off-by: Pavel Ondračka <pavel.ondracka at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14957>

---

 .../drivers/r300/compiler/r300_nir_algebraic.py    | 54 ++++++++++++++++++++++
 .../drivers/r300/compiler/radeon_compiler.h        |  1 +
 .../drivers/r300/compiler/radeon_program_alu.c     |  3 ++
 src/gallium/drivers/r300/meson.build               | 14 +++++-
 src/gallium/drivers/r300/r300_state.c              |  5 +-
 src/gallium/drivers/r300/r300_vs.c                 |  1 +
 src/gallium/drivers/r300/r300_vs.h                 |  4 ++
 7 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py b/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py
new file mode 100644
index 00000000000..b05f994750e
--- /dev/null
+++ b/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py
@@ -0,0 +1,54 @@
+#
+# Copyright (C) 2019 Vasily Khoruzhick <anarsoul at gmail.com>
+# Copyright (C) 2021 Pavel Ondračka
+#
+# 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.
+
+import argparse
+import sys
+from math import pi
+
+# Transform input to range [-PI, PI]:
+#
+# y = frac(x / 2PI + 0.5) * 2PI - PI
+#
+transform_trig_input_vs_r500 = [
+        (('fsin', 'a'), ('fsin', ('fadd', ('fmul', ('ffract', ('fadd', ('fmul', 'a', 1 / (2 * pi)) , 0.5)), 2 * pi), -pi))),
+        (('fcos', 'a'), ('fcos', ('fadd', ('fmul', ('ffract', ('fadd', ('fmul', 'a', 1 / (2 * pi)) , 0.5)), 2 * pi), -pi))),
+]
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-p', '--import-path', required=True)
+    parser.add_argument('output')
+    args = parser.parse_args()
+    sys.path.insert(0, args.import_path)
+
+    import nir_algebraic  # pylint: disable=import-error
+
+    with open(args.output, 'w') as f:
+        f.write('#include "r300_vs.h"')
+
+        f.write(nir_algebraic.AlgebraicPass("r300_transform_vs_trig_input",
+                                            transform_trig_input_vs_r500).render())
+
+
+if __name__ == '__main__':
+    main()
diff --git a/src/gallium/drivers/r300/compiler/radeon_compiler.h b/src/gallium/drivers/r300/compiler/radeon_compiler.h
index 99ded74e487..ceb8857e26e 100644
--- a/src/gallium/drivers/r300/compiler/radeon_compiler.h
+++ b/src/gallium/drivers/r300/compiler/radeon_compiler.h
@@ -55,6 +55,7 @@ struct radeon_compiler {
 	unsigned has_presub:1;
 	unsigned has_omod:1;
 	unsigned disable_optimizations:1;
+	unsigned needs_trig_input_transform:1;
 	unsigned max_temp_regs;
 	unsigned max_constants;
 	int max_alu_insts;
diff --git a/src/gallium/drivers/r300/compiler/radeon_program_alu.c b/src/gallium/drivers/r300/compiler/radeon_program_alu.c
index 20de8a51692..c58cf72bdf7 100644
--- a/src/gallium/drivers/r300/compiler/radeon_program_alu.c
+++ b/src/gallium/drivers/r300/compiler/radeon_program_alu.c
@@ -1055,6 +1055,9 @@ int r300_transform_trig_scale_vertex(struct radeon_compiler *c,
 	    inst->U.I.Opcode != RC_OPCODE_SIN)
 		return 0;
 
+	if (!c->needs_trig_input_transform)
+		return 1;
+
 	/* Repeat x in the range [-PI, PI]:
 	 *
 	 *   repeat(x) = frac(x / 2PI + 0.5) * 2PI - PI
diff --git a/src/gallium/drivers/r300/meson.build b/src/gallium/drivers/r300/meson.build
index 226c374e477..14dbd90403e 100644
--- a/src/gallium/drivers/r300/meson.build
+++ b/src/gallium/drivers/r300/meson.build
@@ -118,9 +118,21 @@ files_r300 = files(
   'compiler/radeon_vert_fc.c',
 )
 
+r300_nir_algebraic_c = custom_target(
+  'r300_nir_algebraic.c',
+  input : 'compiler/r300_nir_algebraic.py',
+  output : 'r300_nir_algebraic.c',
+  command : [
+    prog_python, '@INPUT@',
+    '-p', join_paths(meson.source_root(), 'src/compiler/nir/'),
+    '@OUTPUT@',
+  ],
+  depend_files : nir_algebraic_py,
+)
+
 libr300 = static_library(
   'r300',
-  files_r300,
+  files_r300, r300_nir_algebraic_c,
   include_directories : [
     inc_src, inc_include, inc_gallium, inc_gallium_aux, inc_gallium_drivers,
     inc_mesa,
diff --git a/src/gallium/drivers/r300/r300_state.c b/src/gallium/drivers/r300/r300_state.c
index d1c3cdd83ea..64d7a4e8f68 100644
--- a/src/gallium/drivers/r300/r300_state.c
+++ b/src/gallium/drivers/r300/r300_state.c
@@ -46,6 +46,7 @@
 #include "r300_fs.h"
 #include "r300_texture.h"
 #include "r300_vs.h"
+#include "nir.h"
 #include "nir/nir_to_tgsi.h"
 
 /* r300_state: Functions used to initialize state context by translating
@@ -1953,8 +1954,10 @@ static void* r300_create_vs_state(struct pipe_context* pipe,
        };
        const struct nir_to_tgsi_options *ntt_options;
        if (r300->screen->caps.has_tcl) {
-           if (r300->screen->caps.is_r500)
+           if (r300->screen->caps.is_r500) {
                ntt_options = &hwtcl_r500_options;
+               NIR_PASS_V(shader->ir.nir, r300_transform_vs_trig_input);
+           }
             else
                ntt_options = &hwtcl_r300_options;
        } else {
diff --git a/src/gallium/drivers/r300/r300_vs.c b/src/gallium/drivers/r300/r300_vs.c
index fb225296592..0ea2d4631ee 100644
--- a/src/gallium/drivers/r300/r300_vs.c
+++ b/src/gallium/drivers/r300/r300_vs.c
@@ -223,6 +223,7 @@ void r300_translate_vertex_shader(struct r300_context *r300,
     compiler.Base.has_half_swizzles = FALSE;
     compiler.Base.has_presub = FALSE;
     compiler.Base.has_omod = FALSE;
+    compiler.Base.needs_trig_input_transform = DBG_ON(r300, DBG_USE_TGSI);
     compiler.Base.max_temp_regs = 32;
     compiler.Base.max_constants = 256;
     compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;
diff --git a/src/gallium/drivers/r300/r300_vs.h b/src/gallium/drivers/r300/r300_vs.h
index b02d5d79731..4ca5fba255f 100644
--- a/src/gallium/drivers/r300/r300_vs.h
+++ b/src/gallium/drivers/r300/r300_vs.h
@@ -56,6 +56,8 @@ struct r300_vertex_shader {
     void *draw_vs;
 };
 
+struct nir_shader;
+
 void r300_init_vs_outputs(struct r300_context *r300,
                           struct r300_vertex_shader *vs);
 
@@ -65,4 +67,6 @@ void r300_translate_vertex_shader(struct r300_context *r300,
 void r300_draw_init_vertex_shader(struct r300_context *r300,
                                   struct r300_vertex_shader *vs);
 
+extern bool r300_transform_trig_input(struct nir_shader *shader);
+
 #endif /* R300_VS_H */



More information about the mesa-commit mailing list