Mesa (staging/22.0): nir: Add lowering for fround_even on r300.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Apr 21 20:48:23 UTC 2022


Module: Mesa
Branch: staging/22.0
Commit: 5c3920ee6a5e5a3e95734a3f613df89ecd44492b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=5c3920ee6a5e5a3e95734a3f613df89ecd44492b

Author: Emma Anholt <emma at anholt.net>
Date:   Mon Apr 11 16:28:05 2022 -0700

nir: Add lowering for fround_even on r300.

When we put NIR in the compiler stack for r300, indirect addressing broke
for gallium nine.  DX's array indirects round the float value, so the DX
shader gets mapped to a TGSI "ARR ADDR[0] src.x" instruction.  Translating
that to NIR maps to r0[f2i32(fround(src.x))].  While we might hope that in
translation back using nir-to-tgsi after optimization we would recognize
the construct and emit ARR again, that's going to be error prone (think
"what if src.x is in a NIR register?") so we need a fallback plan.  r300
will be able to handle this lowering, so get it in place first to fix the
regression.

Fixes: #6297
Fixes: 7d2ea9b0edef ("r300: Request NIR shaders from mesa/st and use NIR-to-TGSI.")
Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15870>
(cherry picked from commit 6947016b468417f60203db68c34e0961e4ac7845)

---

 .pick_status.json                      | 2 +-
 src/compiler/nir/nir.h                 | 8 ++++++++
 src/compiler/nir/nir_opt_algebraic.py  | 8 ++++++++
 src/gallium/drivers/r300/r300_screen.c | 4 ++++
 4 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/.pick_status.json b/.pick_status.json
index 7d5d9577d6e..acc06e56746 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -1596,7 +1596,7 @@
         "description": "nir: Add lowering for fround_even on r300.",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "because_sha": "7d2ea9b0edef2176140629ac3dee6a6809c4abe2"
     },
     {
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 4ea0be0b666..2822be84b22 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3224,6 +3224,14 @@ typedef struct nir_shader_compiler_options {
 
    bool lower_ftrunc;
 
+   /** Lowers fround_even to ffract+feq+csel.
+    *
+    * Not correct in that it doesn't correctly handle the "_even" part of the
+    * rounding, but good enough for DX9 array indexing handling on DX9-class
+    * hardware.
+    */
+   bool lower_fround_even;
+
    bool lower_ldexp;
 
    bool lower_pack_half_2x16;
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index 5f8757c1651..9b3117af2fe 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -356,6 +356,14 @@ optimizations.extend([
 
    (('~flrp', a, 0.0, c), ('fadd', ('fmul', ('fneg', a), c), a)),
    (('ftrunc', a), ('bcsel', ('flt', a, 0.0), ('fneg', ('ffloor', ('fabs', a))), ('ffloor', ('fabs', a))), 'options->lower_ftrunc'),
+
+   # Approximate handling of fround_even for DX9 addressing from gallium nine on
+   # DX9-class hardware with no proper fround support.
+   (('fround_even', a), ('bcsel',
+                         ('feq', ('ffract', a), 0.5),
+                         ('fadd', ('ffloor', ('fadd', a, 0.5)), 1.0),
+                         ('ffloor', ('fadd', a, 0.5))), 'options->lower_fround_even'),
+
    (('ffloor', a), ('fsub', a, ('ffract', a)), 'options->lower_ffloor'),
    (('fadd', a, ('fneg', ('ffract', a))), ('ffloor', a), '!options->lower_ffloor'),
    (('ffract', a), ('fsub', a, ('ffloor', a)), 'options->lower_ffract'),
diff --git a/src/gallium/drivers/r300/r300_screen.c b/src/gallium/drivers/r300/r300_screen.c
index 06be9f0d78d..17347d35ead 100644
--- a/src/gallium/drivers/r300/r300_screen.c
+++ b/src/gallium/drivers/r300/r300_screen.c
@@ -514,6 +514,7 @@ static const nir_shader_compiler_options r500_vs_compiler_options = {
    .lower_flrp32 = true,
    .lower_flrp64 = true,
    .lower_fmod = true,
+   .lower_fround_even = true,
    .lower_rotate = true,
    .lower_uniforms_to_ubo = true,
    .lower_vector_cmp = true,
@@ -541,6 +542,7 @@ static const nir_shader_compiler_options r500_fs_compiler_options = {
    .lower_flrp32 = true,
    .lower_flrp64 = true,
    .lower_fmod = true,
+   .lower_fround_even = true,
    .lower_rotate = true,
    .lower_uniforms_to_ubo = true,
    .lower_vector_cmp = true,
@@ -568,6 +570,7 @@ static const nir_shader_compiler_options r300_vs_compiler_options = {
    .lower_flrp32 = true,
    .lower_flrp64 = true,
    .lower_fmod = true,
+   .lower_fround_even = true,
    .lower_rotate = true,
    .lower_uniforms_to_ubo = true,
    .lower_vector_cmp = true,
@@ -593,6 +596,7 @@ static const nir_shader_compiler_options r300_fs_compiler_options = {
    .lower_flrp32 = true,
    .lower_flrp64 = true,
    .lower_fmod = true,
+   .lower_fround_even = true,
    .lower_rotate = true,
    .lower_uniforms_to_ubo = true,
    .lower_vector_cmp = true,



More information about the mesa-commit mailing list