nir: add common convert_ycbcr for vulkan csc
authorJonathan Marek <jonathan@marek.ca>
Fri, 10 Apr 2020 12:34:53 +0000 (08:34 -0400)
committerMarge Bot <eric+marge@anholt.net>
Mon, 20 Apr 2020 22:01:43 +0000 (22:01 +0000)
Copied from anv, replaced state with passing model/range directly.

Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Acked-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: D Scott Phillips <d.scott.phillips@intel.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4528>

src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir_convert_ycbcr.c [new file with mode: 0644]
src/compiler/nir/nir_vulkan.h [new file with mode: 0644]

index bda3e13e91eaa2d6cb52b10d1168826779a9dba0..317b95648baa771d5e3f06a52a6fc17a7fd93130 100644 (file)
@@ -215,6 +215,7 @@ NIR_FILES = \
        nir/nir_control_flow.c \
        nir/nir_control_flow.h \
        nir/nir_control_flow_private.h \
+       nir/nir_convert_ycbcr.c \
        nir/nir_deref.c \
        nir/nir_deref.h \
        nir/nir_divergence_analysis.c \
@@ -348,6 +349,7 @@ NIR_FILES = \
        nir/nir_to_lcssa.c \
        nir/nir_validate.c \
        nir/nir_vla.h \
+       nir/nir_vulkan.h \
        nir/nir_worklist.c \
        nir/nir_worklist.h \
        nir/nir_xfb_info.h
index 82e2d17d8cbb0df82d372e10c2285a2512158872..7e720e5e9251eeb50605bb3473e2bbfab6d58e1d 100644 (file)
@@ -94,6 +94,7 @@ files_libnir = files(
   'nir_control_flow.c',
   'nir_control_flow.h',
   'nir_control_flow_private.h',
+  'nir_convert_ycbcr.c',
   'nir_deref.c',
   'nir_deref.h',
   'nir_divergence_analysis.c',
@@ -227,6 +228,7 @@ files_libnir = files(
   'nir_to_lcssa.c',
   'nir_validate.c',
   'nir_vla.h',
+  'nir_vulkan.h',
   'nir_worklist.c',
   'nir_worklist.h',
   'nir_xfb_info.h',
diff --git a/src/compiler/nir/nir_convert_ycbcr.c b/src/compiler/nir/nir_convert_ycbcr.c
new file mode 100644 (file)
index 0000000..fdd6cf1
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Copyright © 2017 Intel Corporation
+ *
+ * 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 "nir_vulkan.h"
+#include <math.h>
+
+static nir_ssa_def *
+y_range(nir_builder *b,
+        nir_ssa_def *y_channel,
+        int bpc,
+        VkSamplerYcbcrRange range)
+{
+   switch (range) {
+   case VK_SAMPLER_YCBCR_RANGE_ITU_FULL:
+      return y_channel;
+   case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:
+      return nir_fmul(b,
+                      nir_fadd(b,
+                               nir_fmul(b, y_channel,
+                                        nir_imm_float(b, pow(2, bpc) - 1)),
+                               nir_imm_float(b, -16.0f * pow(2, bpc - 8))),
+                      nir_frcp(b, nir_imm_float(b, 219.0f * pow(2, bpc - 8))));
+   default:
+      unreachable("missing Ycbcr range");
+      return NULL;
+   }
+}
+
+static nir_ssa_def *
+chroma_range(nir_builder *b,
+             nir_ssa_def *chroma_channel,
+             int bpc,
+             VkSamplerYcbcrRange range)
+{
+   switch (range) {
+   case VK_SAMPLER_YCBCR_RANGE_ITU_FULL:
+      return nir_fadd(b, chroma_channel,
+                      nir_imm_float(b, -pow(2, bpc - 1) / (pow(2, bpc) - 1.0f)));
+   case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:
+      return nir_fmul(b,
+                      nir_fadd(b,
+                               nir_fmul(b, chroma_channel,
+                                        nir_imm_float(b, pow(2, bpc) - 1)),
+                               nir_imm_float(b, -128.0f * pow(2, bpc - 8))),
+                      nir_frcp(b, nir_imm_float(b, 224.0f * pow(2, bpc - 8))));
+   default:
+      unreachable("missing Ycbcr range");
+      return NULL;
+   }
+}
+
+typedef struct nir_const_value_3_4 {
+   nir_const_value v[3][4];
+} nir_const_value_3_4;
+
+static const nir_const_value_3_4 *
+ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversion model)
+{
+   switch (model) {
+   case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601: {
+      static const nir_const_value_3_4 bt601 = { {
+         { { .f32 =  1.402f             }, { .f32 = 1.0f }, { .f32 =  0.0f               }, { .f32 = 0.0f } },
+         { { .f32 = -0.714136286201022f }, { .f32 = 1.0f }, { .f32 = -0.344136286201022f }, { .f32 = 0.0f } },
+         { { .f32 =  0.0f               }, { .f32 = 1.0f }, { .f32 =  1.772f             }, { .f32 = 0.0f } },
+      } };
+
+      return &bt601;
+   }
+   case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709: {
+      static const nir_const_value_3_4 bt709 = { {
+         { { .f32 =  1.5748031496063f   }, { .f32 = 1.0f }, { .f32 =  0.0f               }, { .f32 = 0.0f } },
+         { { .f32 = -0.468125209181067f }, { .f32 = 1.0f }, { .f32 = -0.187327487470334f }, { .f32 = 0.0f } },
+         { { .f32 =  0.0f               }, { .f32 = 1.0f }, { .f32 =  1.85563184264242f  }, { .f32 = 0.0f } },
+      } };
+
+      return &bt709;
+   }
+   case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020: {
+      static const nir_const_value_3_4 bt2020 = { {
+         { { .f32 =  1.4746f            }, { .f32 = 1.0f }, { .f32 =  0.0f               }, { .f32 = 0.0f } },
+         { { .f32 = -0.571353126843658f }, { .f32 = 1.0f }, { .f32 = -0.164553126843658f }, { .f32 = 0.0f } },
+         { { .f32 =  0.0f               }, { .f32 = 1.0f }, { .f32 =  1.8814f            }, { .f32 = 0.0f } },
+      } };
+
+      return &bt2020;
+   }
+   default:
+      unreachable("missing Ycbcr model");
+      return NULL;
+   }
+}
+
+nir_ssa_def *
+nir_convert_ycbcr_to_rgb(nir_builder *b,
+                         VkSamplerYcbcrModelConversion model,
+                         VkSamplerYcbcrRange range,
+                         nir_ssa_def *raw_channels,
+                         uint32_t *bpcs)
+{
+   nir_ssa_def *expanded_channels =
+      nir_vec4(b,
+               chroma_range(b, nir_channel(b, raw_channels, 0), bpcs[0], range),
+               y_range(b, nir_channel(b, raw_channels, 1), bpcs[1], range),
+               chroma_range(b, nir_channel(b, raw_channels, 2), bpcs[2], range),
+               nir_imm_float(b, 1.0f));
+
+   if (model == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
+      return expanded_channels;
+
+   const nir_const_value_3_4 *conversion_matrix =
+      ycbcr_model_to_rgb_matrix(model);
+
+   nir_ssa_def *converted_channels[] = {
+      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[0])),
+      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[1])),
+      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[2]))
+   };
+
+   return nir_vec4(b,
+                   converted_channels[0], converted_channels[1],
+                   converted_channels[2], nir_imm_float(b, 1.0f));
+}
diff --git a/src/compiler/nir/nir_vulkan.h b/src/compiler/nir/nir_vulkan.h
new file mode 100644 (file)
index 0000000..1a76506
--- /dev/null
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: MIT */
+
+#include "nir.h"
+#include "nir_builder.h"
+#include "vulkan/vulkan_core.h"
+
+nir_ssa_def *
+nir_convert_ycbcr_to_rgb(nir_builder *b,
+                         VkSamplerYcbcrModelConversion model,
+                         VkSamplerYcbcrRange range,
+                         nir_ssa_def *raw_channels,
+                         uint32_t *bpcs);