nir: improve convert_yuv_to_rgb
authorJonathan Marek <jonathan@marek.ca>
Fri, 30 Nov 2018 02:43:12 +0000 (21:43 -0500)
committerIan Romanick <ian.d.romanick@intel.com>
Wed, 1 May 2019 11:13:36 +0000 (04:13 -0700)
Use a different arrangement of constants to allow more ffma.

A vec4 backend will now use 3 fma for yuv_to_rgb. On freedreno/ir3, it is
down from 10 to 7 alu (4 fma, 3 mul, 3 add to 7 fma). Other backends
shouldn't be hurt.

Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Reviewed-by: Eric Anholt <eric@anholt.net>
Tested-by: Ian Romanick <ian.d.romanick@intel.com>
src/compiler/nir/nir_lower_tex.c

index e814e65bf2cd55fcc89ad4d48edf53f7a14698e2..0e1aa26a9045934d4f19980758af9362fd01db57 100644 (file)
@@ -357,24 +357,23 @@ convert_yuv_to_rgb(nir_builder *b, nir_tex_instr *tex,
                    nir_ssa_def *a)
 {
    nir_const_value m[3][4] = {
-      { { .f32 = 1.0f }, { .f32 =  0.0f        }, { .f32 =  1.59602678f }, { .f32 = 0.0f } },
-      { { .f32 = 1.0f }, { .f32 = -0.39176229f }, { .f32 = -0.81296764f }, { .f32 = 0.0f } },
-      { { .f32 = 1.0f }, { .f32 =  2.01723214f }, { .f32 =  0.0f        }, { .f32 = 0.0f } },
+      { { .f32 = 1.16438356f }, { .f32 =  1.16438356f }, { .f32 = 1.16438356f }, { .f32 = 0.0f } },
+      { { .f32 = 0.0f        }, { .f32 = -0.39176229f }, { .f32 = 2.01723214f }, { .f32 = 0.0f } },
+      { { .f32 = 1.59602678f }, { .f32 = -0.81296764f }, { .f32 = 0.0f        }, { .f32 = 0.0f } },
    };
 
-   nir_ssa_def *yuv =
+   nir_ssa_def *offset =
       nir_vec4(b,
-               nir_fmul(b, nir_imm_float(b, 1.16438356f),
-                        nir_fadd(b, y, nir_imm_float(b, -16.0f / 255.0f))),
-               nir_channel(b, nir_fadd(b, u, nir_imm_float(b, -128.0f / 255.0f)), 0),
-               nir_channel(b, nir_fadd(b, v, nir_imm_float(b, -128.0f / 255.0f)), 0),
-               nir_imm_float(b, 0.0));
-
-   nir_ssa_def *red = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[0]));
-   nir_ssa_def *green = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[1]));
-   nir_ssa_def *blue = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[2]));
-
-   nir_ssa_def *result = nir_vec4(b, red, green, blue, a);
+               nir_imm_float(b, -0.874202214f),
+               nir_imm_float(b,  0.531667820f),
+               nir_imm_float(b, -1.085630787f),
+               a);
+
+   nir_ssa_def *result =
+      nir_ffma(b, y, nir_build_imm(b, 4, 32, m[0]),
+               nir_ffma(b, u, nir_build_imm(b, 4, 32, m[1]),
+                        nir_ffma(b, v, nir_build_imm(b, 4, 32, m[2]),
+                                 offset)));
 
    nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));
 }