radv: ignore BB labels when splitting the disassembly string
[mesa.git] / src / amd / vulkan / radv_nir_lower_ycbcr_textures.c
index 77f2e6ac66e483b257f7a181c24df6a59efa1c3f..190964a4de3b030c8455fb0727d32d834dd44819 100644 (file)
@@ -26,6 +26,7 @@
 #include "vk_format.h"
 #include "nir/nir.h"
 #include "nir/nir_builder.h"
+#include "nir/nir_vulkan.h"
 
 struct ycbcr_state {
        nir_builder *builder;
@@ -36,124 +37,70 @@ struct ycbcr_state {
 };
 
 static nir_ssa_def *
-y_range(nir_builder *b,
-        nir_ssa_def *y_channel,
-        int bpc,
-        VkSamplerYcbcrRange range)
+get_texture_size(struct ycbcr_state *state, nir_deref_instr *texture)
 {
-       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;
-       }
-}
+       nir_builder *b = state->builder;
+       const struct glsl_type *type = texture->type;
+       nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);
 
-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;
-       }
-}
+       tex->op = nir_texop_txs;
+       tex->sampler_dim = glsl_get_sampler_dim(type);
+       tex->is_array = glsl_sampler_type_is_array(type);
+       tex->is_shadow = glsl_sampler_type_is_shadow(type);
+       tex->dest_type = nir_type_int;
+
+       tex->src[0].src_type = nir_tex_src_texture_deref;
+       tex->src[0].src = nir_src_for_ssa(&texture->dest.ssa);
 
-typedef struct nir_const_value_3_4 {
-       nir_const_value v[3][4];
-} nir_const_value_3_4;
+       nir_ssa_dest_init(&tex->instr, &tex->dest,
+                         nir_tex_instr_dest_size(tex), 32, NULL);
+       nir_builder_instr_insert(b, &tex->instr);
 
-static const nir_const_value_3_4 *
-ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversion model)
+       return nir_i2f32(b, &tex->dest.ssa);
+}
+
+static nir_ssa_def *
+implicit_downsampled_coord(nir_builder *b,
+                           nir_ssa_def *value,
+                           nir_ssa_def *max_value,
+                           int div_scale)
 {
-       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;
-       }
+       return nir_fadd(b,
+                       value,
+                       nir_fdiv(b,
+                                nir_imm_float(b, 1.0f),
+                                nir_fmul(b,
+                                         nir_imm_float(b, div_scale),
+                                         max_value)));
 }
 
 static nir_ssa_def *
-convert_ycbcr(struct ycbcr_state *state,
-              nir_ssa_def *raw_channels,
-              uint8_t bits)
+implicit_downsampled_coords(struct ycbcr_state *state,
+                            nir_ssa_def *old_coords)
 {
        nir_builder *b = state->builder;
        const struct radv_sampler_ycbcr_conversion *conversion = state->conversion;
+       nir_ssa_def *image_size = NULL;
+       nir_ssa_def *comp[4] = { NULL, };
+       const struct vk_format_description *fmt_desc = vk_format_description(state->conversion->format);
+       const unsigned divisors[2] = {fmt_desc->width_divisor, fmt_desc->height_divisor};
+
+       for (int c = 0; c < old_coords->num_components; c++) {
+               if (c < ARRAY_SIZE(divisors) && divisors[c] > 1 &&
+                   conversion->chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVEN) {
+                       if (!image_size)
+                               image_size = get_texture_size(state, state->tex_deref);
+
+                       comp[c] = implicit_downsampled_coord(b,
+                                                            nir_channel(b, old_coords, c),
+                                                            nir_channel(b, image_size, c),
+                                                            divisors[c]);
+               } else {
+                       comp[c] = nir_channel(b, old_coords, c);
+               }
+       }
 
-       nir_ssa_def *expanded_channels =
-               nir_vec4(b,
-                        chroma_range(b, nir_channel(b, raw_channels, 0),
-                                     bits, conversion->ycbcr_range),
-                        y_range(b, nir_channel(b, raw_channels, 1),
-                                bits, conversion->ycbcr_range),
-                        chroma_range(b, nir_channel(b, raw_channels, 2),
-                                     bits, conversion->ycbcr_range),
-                        nir_imm_float(b, 1.0f));
-
-       if (conversion->ycbcr_model == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
-               return expanded_channels;
-
-       const nir_const_value_3_4 *conversion_matrix =
-               ycbcr_model_to_rgb_matrix(conversion->ycbcr_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));
+       return nir_vec(b, comp, old_coords->num_components);
 }
 
 static nir_ssa_def *
@@ -163,10 +110,23 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
        nir_builder *b = state->builder;
        nir_tex_instr *old_tex = state->origin_tex;
        nir_tex_instr *tex = nir_tex_instr_create(b->shader, old_tex->num_srcs+ 1);
-
        for (uint32_t i = 0; i < old_tex->num_srcs; i++) {
                tex->src[i].src_type = old_tex->src[i].src_type;
-               nir_src_copy(&tex->src[i].src, &old_tex->src[i].src, tex);
+
+               switch (old_tex->src[i].src_type) {
+               case nir_tex_src_coord:
+                       if (plane && true/*state->conversion->chroma_reconstruction*/) {
+                               assert(old_tex->src[i].src.is_ssa);
+                               tex->src[i].src =
+                                       nir_src_for_ssa(implicit_downsampled_coords(state,
+                                                                                   old_tex->src[i].src.ssa));
+                               break;
+                       }
+               /* fall through */
+               default:
+                       nir_src_copy(&tex->src[i].src, &old_tex->src[i].src, tex);
+                       break;
+               }
        }
 
        tex->src[tex->num_srcs - 1].src = nir_src_for_ssa(nir_imm_int(b, plane));
@@ -182,7 +142,6 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
        tex->component = old_tex->component;
 
        tex->texture_index = old_tex->texture_index;
-       tex->texture_array_size = old_tex->texture_array_size;
        tex->sampler_index = old_tex->sampler_index;
 
        nir_ssa_dest_init(&tex->instr, &tex->dest,
@@ -326,7 +285,14 @@ try_lower_tex_ycbcr(const struct radv_pipeline_layout *layout,
        nir_ssa_def *result = build_swizzled_components(builder, format, ycbcr_sampler->components, plane_values);
        if (state.conversion->ycbcr_model != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) {
                VkFormat first_format = vk_format_get_plane_format(format, 0);
-               result = convert_ycbcr(&state, result, vk_format_get_component_bits(first_format, VK_FORMAT_COLORSPACE_RGB, VK_SWIZZLE_X));
+               uint32_t bits = vk_format_get_component_bits(first_format, VK_FORMAT_COLORSPACE_RGB, VK_SWIZZLE_X);
+               /* TODO: swizzle and bpcs */
+               uint32_t bpcs[3] = {bits, bits, bits};
+               result = nir_convert_ycbcr_to_rgb(builder,
+                                                 state.conversion->ycbcr_model,
+                                                 state.conversion->ycbcr_range,
+                                                 result,
+                                                 bpcs);
        }
 
        nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));