Path: blob/21.2-virgl/src/compiler/nir/nir_convert_ycbcr.c
4549 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir_vulkan.h"24#include <math.h>2526static nir_ssa_def *27y_range(nir_builder *b,28nir_ssa_def *y_channel,29int bpc,30VkSamplerYcbcrRange range)31{32switch (range) {33case VK_SAMPLER_YCBCR_RANGE_ITU_FULL:34return y_channel;35case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:36return nir_fmul(b,37nir_fadd(b,38nir_fmul(b, y_channel,39nir_imm_float(b, pow(2, bpc) - 1)),40nir_imm_float(b, -16.0f * pow(2, bpc - 8))),41nir_frcp(b, nir_imm_float(b, 219.0f * pow(2, bpc - 8))));42default:43unreachable("missing Ycbcr range");44return NULL;45}46}4748static nir_ssa_def *49chroma_range(nir_builder *b,50nir_ssa_def *chroma_channel,51int bpc,52VkSamplerYcbcrRange range)53{54switch (range) {55case VK_SAMPLER_YCBCR_RANGE_ITU_FULL:56return nir_fadd(b, chroma_channel,57nir_imm_float(b, -pow(2, bpc - 1) / (pow(2, bpc) - 1.0f)));58case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:59return nir_fmul(b,60nir_fadd(b,61nir_fmul(b, chroma_channel,62nir_imm_float(b, pow(2, bpc) - 1)),63nir_imm_float(b, -128.0f * pow(2, bpc - 8))),64nir_frcp(b, nir_imm_float(b, 224.0f * pow(2, bpc - 8))));65default:66unreachable("missing Ycbcr range");67return NULL;68}69}7071typedef struct nir_const_value_3_4 {72nir_const_value v[3][4];73} nir_const_value_3_4;7475static const nir_const_value_3_4 *76ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversion model)77{78switch (model) {79case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601: {80static const nir_const_value_3_4 bt601 = { {81{ { .f32 = 1.402f }, { .f32 = 1.0f }, { .f32 = 0.0f }, { .f32 = 0.0f } },82{ { .f32 = -0.714136286201022f }, { .f32 = 1.0f }, { .f32 = -0.344136286201022f }, { .f32 = 0.0f } },83{ { .f32 = 0.0f }, { .f32 = 1.0f }, { .f32 = 1.772f }, { .f32 = 0.0f } },84} };8586return &bt601;87}88case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709: {89static const nir_const_value_3_4 bt709 = { {90{ { .f32 = 1.5748031496063f }, { .f32 = 1.0f }, { .f32 = 0.0f }, { .f32 = 0.0f } },91{ { .f32 = -0.468125209181067f }, { .f32 = 1.0f }, { .f32 = -0.187327487470334f }, { .f32 = 0.0f } },92{ { .f32 = 0.0f }, { .f32 = 1.0f }, { .f32 = 1.85563184264242f }, { .f32 = 0.0f } },93} };9495return &bt709;96}97case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020: {98static const nir_const_value_3_4 bt2020 = { {99{ { .f32 = 1.4746f }, { .f32 = 1.0f }, { .f32 = 0.0f }, { .f32 = 0.0f } },100{ { .f32 = -0.571353126843658f }, { .f32 = 1.0f }, { .f32 = -0.164553126843658f }, { .f32 = 0.0f } },101{ { .f32 = 0.0f }, { .f32 = 1.0f }, { .f32 = 1.8814f }, { .f32 = 0.0f } },102} };103104return &bt2020;105}106default:107unreachable("missing Ycbcr model");108return NULL;109}110}111112nir_ssa_def *113nir_convert_ycbcr_to_rgb(nir_builder *b,114VkSamplerYcbcrModelConversion model,115VkSamplerYcbcrRange range,116nir_ssa_def *raw_channels,117uint32_t *bpcs)118{119nir_ssa_def *expanded_channels =120nir_vec4(b,121chroma_range(b, nir_channel(b, raw_channels, 0), bpcs[0], range),122y_range(b, nir_channel(b, raw_channels, 1), bpcs[1], range),123chroma_range(b, nir_channel(b, raw_channels, 2), bpcs[2], range),124nir_channel(b, raw_channels, 3));125126if (model == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)127return expanded_channels;128129const nir_const_value_3_4 *conversion_matrix =130ycbcr_model_to_rgb_matrix(model);131132nir_ssa_def *converted_channels[] = {133nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[0])),134nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[1])),135nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix->v[2]))136};137138return nir_vec4(b,139converted_channels[0], converted_channels[1],140converted_channels[2], nir_channel(b, raw_channels, 3));141}142143144