Path: blob/21.2-virgl/src/amd/vulkan/radv_nir_lower_ycbcr_textures.c
7243 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/nir.h"24#include "nir/nir_builder.h"25#include "nir/nir_vulkan.h"26#include "radv_private.h"27#include "radv_shader.h"28#include "vk_format.h"2930struct ycbcr_state {31nir_builder *builder;32nir_ssa_def *image_size;33nir_tex_instr *origin_tex;34nir_deref_instr *tex_deref;35const struct radv_sampler_ycbcr_conversion *conversion;36bool unnormalized_coordinates;37};3839static nir_ssa_def *40get_texture_size(struct ycbcr_state *state, nir_deref_instr *texture)41{42nir_builder *b = state->builder;43const struct glsl_type *type = texture->type;44nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);4546tex->op = nir_texop_txs;47tex->sampler_dim = glsl_get_sampler_dim(type);48tex->is_array = glsl_sampler_type_is_array(type);49tex->is_shadow = glsl_sampler_type_is_shadow(type);50tex->dest_type = nir_type_int32;5152tex->src[0].src_type = nir_tex_src_texture_deref;53tex->src[0].src = nir_src_for_ssa(&texture->dest.ssa);5455nir_ssa_dest_init(&tex->instr, &tex->dest, nir_tex_instr_dest_size(tex), 32, NULL);56nir_builder_instr_insert(b, &tex->instr);5758return nir_i2f32(b, &tex->dest.ssa);59}6061static nir_ssa_def *62implicit_downsampled_coord(nir_builder *b, nir_ssa_def *value, nir_ssa_def *max_value,63int div_scale)64{65return nir_fadd(66b, value,67nir_fdiv(b, nir_imm_float(b, 1.0f), nir_fmul(b, nir_imm_float(b, div_scale), max_value)));68}6970static nir_ssa_def *71implicit_downsampled_coord_unnormalized(nir_builder *b, nir_ssa_def *value, int div_scale)72{73return nir_fadd(74b, value,75nir_imm_float(b, 1.0f / (float)div_scale));76}7778static nir_ssa_def *79implicit_downsampled_coords(struct ycbcr_state *state, nir_ssa_def *old_coords)80{81nir_builder *b = state->builder;82const struct radv_sampler_ycbcr_conversion *conversion = state->conversion;83nir_ssa_def *image_size = NULL;84nir_ssa_def *comp[4] = {85NULL,86};87enum pipe_video_chroma_format chroma_format =88pipe_format_to_chroma_format(vk_format_to_pipe_format(state->conversion->format));89const unsigned divisors[2] = {chroma_format <= PIPE_VIDEO_CHROMA_FORMAT_422 ? 2 : 1,90chroma_format <= PIPE_VIDEO_CHROMA_FORMAT_420 ? 2 : 1};9192for (int c = 0; c < old_coords->num_components; c++) {93comp[c] = nir_channel(b, old_coords, c);9495if (c < ARRAY_SIZE(divisors) && divisors[c] > 1) {96if (state->unnormalized_coordinates)97comp[c] = nir_fdiv(b, comp[c], nir_imm_float(b, divisors[c]));9899if (conversion->chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVEN) {100if (state->unnormalized_coordinates) {101comp[c] = implicit_downsampled_coord_unnormalized(b, comp[c], divisors[c]);102} else {103if (!image_size)104image_size = get_texture_size(state, state->tex_deref);105106comp[c] = implicit_downsampled_coord(b, comp[c], nir_channel(b, image_size, c), divisors[c]);107}108}109}110}111112return nir_vec(b, comp, old_coords->num_components);113}114115static nir_ssa_def *116create_plane_tex_instr_implicit(struct ycbcr_state *state, uint32_t plane)117{118nir_builder *b = state->builder;119nir_tex_instr *old_tex = state->origin_tex;120nir_tex_instr *tex = nir_tex_instr_create(b->shader, old_tex->num_srcs + 1);121for (uint32_t i = 0; i < old_tex->num_srcs; i++) {122tex->src[i].src_type = old_tex->src[i].src_type;123124switch (old_tex->src[i].src_type) {125case nir_tex_src_coord:126if (plane && true /*state->conversion->chroma_reconstruction*/) {127assert(old_tex->src[i].src.is_ssa);128tex->src[i].src =129nir_src_for_ssa(implicit_downsampled_coords(state, old_tex->src[i].src.ssa));130break;131}132FALLTHROUGH;133default:134nir_src_copy(&tex->src[i].src, &old_tex->src[i].src, tex);135break;136}137}138139tex->src[tex->num_srcs - 1].src = nir_src_for_ssa(nir_imm_int(b, plane));140tex->src[tex->num_srcs - 1].src_type = nir_tex_src_plane;141142tex->sampler_dim = old_tex->sampler_dim;143tex->dest_type = old_tex->dest_type;144tex->is_array = old_tex->is_array;145146tex->op = old_tex->op;147tex->coord_components = old_tex->coord_components;148tex->is_new_style_shadow = old_tex->is_new_style_shadow;149tex->component = old_tex->component;150151tex->texture_index = old_tex->texture_index;152tex->sampler_index = old_tex->sampler_index;153154nir_ssa_dest_init(&tex->instr, &tex->dest, old_tex->dest.ssa.num_components,155nir_dest_bit_size(old_tex->dest), NULL);156nir_builder_instr_insert(b, &tex->instr);157158return &tex->dest.ssa;159}160161struct swizzle_info {162unsigned plane[4];163unsigned swizzle[4];164};165166static struct swizzle_info167get_plane_swizzles(VkFormat format)168{169int planes = vk_format_get_plane_count(format);170switch (planes) {171case 3:172return (struct swizzle_info){{2, 0, 1, 0}, {0, 0, 0, 3}};173case 2:174return (struct swizzle_info){{1, 0, 1, 0}, {1, 0, 0, 3}};175case 1:176return (struct swizzle_info){{0, 0, 0, 0}, {0, 1, 2, 3}};177default:178unreachable("unhandled plane count for ycbcr swizzling");179}180}181182static nir_ssa_def *183build_swizzled_components(nir_builder *builder, VkFormat format, VkComponentMapping mapping,184nir_ssa_def **plane_values)185{186struct swizzle_info plane_swizzle = get_plane_swizzles(format);187enum pipe_swizzle swizzles[4];188nir_ssa_def *values[4];189190vk_format_compose_swizzles(&mapping, (const unsigned char[4]){0, 1, 2, 3}, swizzles);191192nir_ssa_def *zero = nir_imm_float(builder, 0.0f);193nir_ssa_def *one = nir_imm_float(builder, 1.0f);194195for (unsigned i = 0; i < 4; ++i) {196switch (swizzles[i]) {197case PIPE_SWIZZLE_X:198case PIPE_SWIZZLE_Y:199case PIPE_SWIZZLE_Z:200case PIPE_SWIZZLE_W: {201unsigned channel = swizzles[i] - PIPE_SWIZZLE_X;202values[i] = nir_channel(builder, plane_values[plane_swizzle.plane[channel]],203plane_swizzle.swizzle[channel]);204break;205}206case PIPE_SWIZZLE_0:207values[i] = zero;208break;209case PIPE_SWIZZLE_1:210values[i] = one;211break;212default:213unreachable("unhandled swizzle");214}215}216return nir_vec(builder, values, 4);217}218219static bool220try_lower_tex_ycbcr(const struct radv_pipeline_layout *layout, nir_builder *builder,221nir_tex_instr *tex)222{223int deref_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);224assert(deref_src_idx >= 0);225nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);226227nir_variable *var = nir_deref_instr_get_variable(deref);228const struct radv_descriptor_set_layout *set_layout =229layout->set[var->data.descriptor_set].layout;230const struct radv_descriptor_set_binding_layout *binding =231&set_layout->binding[var->data.binding];232const struct radv_sampler_ycbcr_conversion *ycbcr_samplers =233radv_immutable_ycbcr_samplers(set_layout, var->data.binding);234235if (!ycbcr_samplers)236return false;237238assert(binding->immutable_samplers_offset);239const uint32_t *immutable_samplers =240radv_immutable_samplers(set_layout, binding);241242/* For the following instructions, we don't apply any change and let the243* instruction apply to the first plane.244*/245if (tex->op == nir_texop_txs || tex->op == nir_texop_query_levels || tex->op == nir_texop_lod)246return false;247248assert(tex->texture_index == 0);249unsigned array_index = 0;250if (deref->deref_type != nir_deref_type_var) {251assert(deref->deref_type == nir_deref_type_array);252if (!nir_src_is_const(deref->arr.index))253return false;254array_index = nir_src_as_uint(deref->arr.index);255array_index = MIN2(array_index, binding->array_size - 1);256}257const struct radv_sampler_ycbcr_conversion *ycbcr_sampler = ycbcr_samplers + array_index;258259if (ycbcr_sampler->format == VK_FORMAT_UNDEFINED)260return false;261262bool unnormalized_coordinates = immutable_samplers[4 * array_index + 0] & S_008F30_FORCE_UNNORMALIZED(1);263264struct ycbcr_state state = {265.builder = builder,266.origin_tex = tex,267.tex_deref = deref,268.conversion = ycbcr_sampler,269.unnormalized_coordinates = unnormalized_coordinates,270};271272builder->cursor = nir_before_instr(&tex->instr);273274VkFormat format = state.conversion->format;275const int plane_count = vk_format_get_plane_count(format);276nir_ssa_def *plane_values[3];277278for (int p = 0; p < plane_count; ++p) {279plane_values[p] = create_plane_tex_instr_implicit(&state, p);280}281282nir_ssa_def *result =283build_swizzled_components(builder, format, ycbcr_sampler->components, plane_values);284if (state.conversion->ycbcr_model != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) {285VkFormat first_format = vk_format_get_plane_format(format, 0);286uint32_t bits =287vk_format_get_component_bits(first_format, UTIL_FORMAT_COLORSPACE_RGB, PIPE_SWIZZLE_X);288/* TODO: swizzle and bpcs */289uint32_t bpcs[3] = {bits, bits, bits};290result = nir_convert_ycbcr_to_rgb(builder, state.conversion->ycbcr_model,291state.conversion->ycbcr_range, result, bpcs);292}293294nir_ssa_def_rewrite_uses(&tex->dest.ssa, result);295nir_instr_remove(&tex->instr);296297return true;298}299300bool301radv_nir_lower_ycbcr_textures(nir_shader *shader, const struct radv_pipeline_layout *layout)302{303bool progress = false;304305nir_foreach_function (function, shader) {306if (!function->impl)307continue;308309bool function_progress = false;310nir_builder builder;311nir_builder_init(&builder, function->impl);312313nir_foreach_block (block, function->impl) {314nir_foreach_instr_safe (instr, block) {315if (instr->type != nir_instr_type_tex)316continue;317318nir_tex_instr *tex = nir_instr_as_tex(instr);319function_progress |= try_lower_tex_ycbcr(layout, &builder, tex);320}321}322323if (function_progress) {324nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);325}326327progress |= function_progress;328}329330return progress;331}332333334