From f8558fb1ce770a817a16bde439dd8865931f59a2 Mon Sep 17 00:00:00 2001 From: Jonathan Marek Date: Fri, 10 Apr 2020 08:34:53 -0400 Subject: [PATCH] nir: add common convert_ycbcr for vulkan csc Copied from anv, replaced state with passing model/range directly. Signed-off-by: Jonathan Marek Reviewed-by: Bas Nieuwenhuizen Acked-by: Jason Ekstrand Reviewed-by: D Scott Phillips Reviewed-by: Lionel Landwerlin Part-of: --- src/compiler/Makefile.sources | 2 + src/compiler/nir/meson.build | 2 + src/compiler/nir/nir_convert_ycbcr.c | 142 +++++++++++++++++++++++++++ src/compiler/nir/nir_vulkan.h | 12 +++ 4 files changed, 158 insertions(+) create mode 100644 src/compiler/nir/nir_convert_ycbcr.c create mode 100644 src/compiler/nir/nir_vulkan.h diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index bda3e13e91e..317b95648ba 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -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 diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 82e2d17d8cb..7e720e5e925 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -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 index 00000000000..fdd6cf1597d --- /dev/null +++ b/src/compiler/nir/nir_convert_ycbcr.c @@ -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 + +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 index 00000000000..1a765065ff5 --- /dev/null +++ b/src/compiler/nir/nir_vulkan.h @@ -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); -- 2.30.2