panfrost: Un/pack sRGB via NIR
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Thu, 14 May 2020 22:54:38 +0000 (18:54 -0400)
committerMarge Bot <eric+marge@anholt.net>
Mon, 1 Jun 2020 15:46:23 +0000 (15:46 +0000)
Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5265>

src/panfrost/util/pan_lower_framebuffer.c

index c21d42ba4ea3bf83c7475beffbf6939f250b0214..fe20be1c3f97c779b3f021a86cb5265e4d7adbd0 100644 (file)
@@ -442,6 +442,48 @@ pan_unpack_r11g11b10(nir_builder *b, nir_ssa_def *v)
         return nir_vec(b, components, 4);
 }
 
+/* Wrapper around sRGB conversion */
+
+static nir_ssa_def *
+pan_linear_to_srgb(nir_builder *b, nir_ssa_def *linear)
+{
+        nir_ssa_def *rgb = nir_channels(b, linear, 0x7);
+
+        /* TODO: fp16 native conversion */
+        nir_ssa_def *srgb = nir_f2f16(b,
+                        nir_format_linear_to_srgb(b, nir_f2f32(b, rgb)));
+
+        nir_ssa_def *comp[4] = {
+                nir_channel(b, srgb, 0),
+                nir_channel(b, srgb, 1),
+                nir_channel(b, srgb, 2),
+                nir_channel(b, linear, 3),
+        };
+
+        return nir_vec(b, comp, 4);
+}
+
+static nir_ssa_def *
+pan_srgb_to_linear(nir_builder *b, nir_ssa_def *srgb)
+{
+        nir_ssa_def *rgb = nir_channels(b, srgb, 0x7);
+
+        /* TODO: fp16 native conversion */
+        nir_ssa_def *linear = nir_f2f16(b,
+                        nir_format_srgb_to_linear(b, nir_f2f32(b, rgb)));
+
+        nir_ssa_def *comp[4] = {
+                nir_channel(b, linear, 0),
+                nir_channel(b, linear, 1),
+                nir_channel(b, linear, 2),
+                nir_channel(b, srgb, 3),
+        };
+
+        return nir_vec(b, comp, 4);
+}
+
+
+
 /* Generic dispatches for un/pack regardless of format */
 
 static bool
@@ -514,6 +556,9 @@ pan_pack(nir_builder *b,
                 const struct util_format_description *desc,
                 nir_ssa_def *unpacked)
 {
+        if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
+                unpacked = pan_linear_to_srgb(b, unpacked);
+
         if (util_format_is_unorm8(desc))
                 return pan_pack_unorm_8(b, unpacked);
 
@@ -594,6 +639,9 @@ pan_lower_fb_load(nir_shader *shader,
         nir_ssa_def *packed = &new->dest.ssa;
         nir_ssa_def *unpacked = pan_unpack(b, desc, packed);
 
+        if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
+                unpacked = pan_srgb_to_linear(b, unpacked);
+
         nir_src rewritten = nir_src_for_ssa(unpacked);
         nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, rewritten, &intr->instr);
 }