1 /* 2 * Copyright 2017 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #include "anv_nir.h" 25 #include "anv_private.h" 26 #include "nir/nir.h" 27 #include "nir/nir_builder.h" 28 29 struct ycbcr_state { 30 nir_builder *builder; 31 nir_ssa_def *image_size; 32 nir_tex_instr *origin_tex; 33 struct anv_ycbcr_conversion *conversion; 34 }; 35 36 static nir_ssa_def * 37 y_range(nir_builder *b, 38 nir_ssa_def *y_channel, 39 int bpc, 40 VkSamplerYcbcrRangeKHR range) 41 { 42 switch (range) { 43 case VK_SAMPLER_YCBCR_RANGE_ITU_FULL_KHR: 44 return y_channel; 45 case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW_KHR: 46 return nir_fmul(b, 47 nir_fadd(b, 48 nir_fmul(b, y_channel, 49 nir_imm_float(b, pow(2, bpc) - 1)), 50 nir_imm_float(b, -16.0f * pow(2, bpc - 8))), 51 nir_frcp(b, nir_imm_float(b, 219.0f * pow(2, bpc - 8)))); 52 default: 53 unreachable("missing Ycbcr range"); 54 return NULL; 55 } 56 } 57 58 static nir_ssa_def * 59 chroma_range(nir_builder *b, 60 nir_ssa_def *chroma_channel, 61 int bpc, 62 VkSamplerYcbcrRangeKHR range) 63 { 64 switch (range) { 65 case VK_SAMPLER_YCBCR_RANGE_ITU_FULL_KHR: 66 return nir_fadd(b, chroma_channel, 67 nir_imm_float(b, -pow(2, bpc - 1) / (pow(2, bpc) - 1.0f))); 68 case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW_KHR: 69 return nir_fmul(b, 70 nir_fadd(b, 71 nir_fmul(b, chroma_channel, 72 nir_imm_float(b, pow(2, bpc) - 1)), 73 nir_imm_float(b, -128.0f * pow(2, bpc - 8))), 74 nir_frcp(b, nir_imm_float(b, 224.0f * pow(2, bpc - 8)))); 75 default: 76 unreachable("missing Ycbcr range"); 77 return NULL; 78 } 79 } 80 81 static const nir_const_value * 82 ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversionKHR model) 83 { 84 switch (model) { 85 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601_KHR: { 86 static const nir_const_value bt601[3] = { 87 { .f32 = { 1.402f, 1.0f, 0.0f, 0.0f } }, 88 { .f32 = { -0.714136286201022f, 1.0f, -0.344136286201022f, 0.0f } }, 89 { .f32 = { 0.0f, 1.0f, 1.772f, 0.0f } } 90 }; 91 92 return bt601; 93 } 94 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709_KHR: { 95 static const nir_const_value bt709[3] = { 96 { .f32 = { 1.5748031496063f, 1.0f, 0.0, 0.0f } }, 97 { .f32 = { -0.468125209181067f, 1.0f, -0.187327487470334f, 0.0f } }, 98 { .f32 = { 0.0f, 1.0f, 1.85563184264242f, 0.0f } } 99 }; 100 101 return bt709; 102 } 103 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020_KHR: { 104 static const nir_const_value bt2020[3] = { 105 { .f32 = { 1.4746f, 1.0f, 0.0f, 0.0f } }, 106 { .f32 = { -0.571353126843658f, 1.0f, -0.164553126843658f, 0.0f } }, 107 { .f32 = { 0.0f, 1.0f, 1.8814f, 0.0f } } 108 }; 109 110 return bt2020; 111 } 112 default: 113 unreachable("missing Ycbcr model"); 114 return NULL; 115 } 116 } 117 118 static nir_ssa_def * 119 convert_ycbcr(struct ycbcr_state *state, 120 nir_ssa_def *raw_channels, 121 uint32_t *bpcs) 122 { 123 nir_builder *b = state->builder; 124 struct anv_ycbcr_conversion *conversion = state->conversion; 125 126 nir_ssa_def *expanded_channels = 127 nir_vec4(b, 128 chroma_range(b, nir_channel(b, raw_channels, 0), 129 bpcs[0], conversion->ycbcr_range), 130 y_range(b, nir_channel(b, raw_channels, 1), 131 bpcs[1], conversion->ycbcr_range), 132 chroma_range(b, nir_channel(b, raw_channels, 2), 133 bpcs[2], conversion->ycbcr_range), 134 nir_imm_float(b, 1.0f)); 135 136 if (conversion->ycbcr_model == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY_KHR) 137 return expanded_channels; 138 139 const nir_const_value *conversion_matrix = 140 ycbcr_model_to_rgb_matrix(conversion->ycbcr_model); 141 142 nir_ssa_def *converted_channels[] = { 143 nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix[0])), 144 nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix[1])), 145 nir_fdot4(b, expanded_channels, nir_build_imm(b, 4, 32, conversion_matrix[2])) 146 }; 147 148 return nir_vec4(b, 149 converted_channels[0], converted_channels[1], 150 converted_channels[2], nir_imm_float(b, 1.0f)); 151 } 152 153 /* TODO: we should probably replace this with a push constant/uniform. */ 154 static nir_ssa_def * 155 get_texture_size(struct ycbcr_state *state, nir_deref_var *texture) 156 { 157 if (state->image_size) 158 return state->image_size; 159 160 nir_builder *b = state->builder; 161 const struct glsl_type *type = nir_deref_tail(&texture->deref)->type; 162 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 0); 163 164 tex->op = nir_texop_txs; 165 tex->sampler_dim = glsl_get_sampler_dim(type); 166 tex->is_array = glsl_sampler_type_is_array(type); 167 tex->is_shadow = glsl_sampler_type_is_shadow(type); 168 tex->texture = nir_deref_var_clone(texture, tex); 169 tex->dest_type = nir_type_int; 170 171 nir_ssa_dest_init(&tex->instr, &tex->dest, 172 nir_tex_instr_dest_size(tex), 32, NULL); 173 nir_builder_instr_insert(b, &tex->instr); 174 175 state->image_size = nir_i2f32(b, &tex->dest.ssa); 176 177 return state->image_size; 178 } 179 180 static nir_ssa_def * 181 implicit_downsampled_coord(nir_builder *b, 182 nir_ssa_def *value, 183 nir_ssa_def *max_value, 184 int div_scale) 185 { 186 return nir_fadd(b, 187 value, 188 nir_fdiv(b, 189 nir_imm_float(b, 1.0f), 190 nir_fmul(b, 191 nir_imm_float(b, div_scale), 192 max_value))); 193 } 194 195 static nir_ssa_def * 196 implicit_downsampled_coords(struct ycbcr_state *state, 197 nir_ssa_def *old_coords, 198 const struct anv_format_plane *plane_format) 199 { 200 nir_builder *b = state->builder; 201 struct anv_ycbcr_conversion *conversion = state->conversion; 202 nir_ssa_def *image_size = get_texture_size(state, 203 state->origin_tex->texture); 204 nir_ssa_def *comp[4] = { NULL, }; 205 int c; 206 207 for (c = 0; c < ARRAY_SIZE(conversion->chroma_offsets); c++) { 208 if (plane_format->denominator_scales[c] > 1 && 209 conversion->chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVEN_KHR) { 210 comp[c] = implicit_downsampled_coord(b, 211 nir_channel(b, old_coords, c), 212 nir_channel(b, image_size, c), 213 plane_format->denominator_scales[c]); 214 } else { 215 comp[c] = nir_channel(b, old_coords, c); 216 } 217 } 218 219 /* Leave other coordinates untouched */ 220 for (; c < old_coords->num_components; c++) 221 comp[c] = nir_channel(b, old_coords, c); 222 223 return nir_vec(b, comp, old_coords->num_components); 224 } 225 226 static nir_ssa_def * 227 create_plane_tex_instr_implicit(struct ycbcr_state *state, 228 uint32_t plane) 229 { 230 nir_builder *b = state->builder; 231 struct anv_ycbcr_conversion *conversion = state->conversion; 232 const struct anv_format_plane *plane_format = 233 &conversion->format->planes[plane]; 234 nir_tex_instr *old_tex = state->origin_tex; 235 nir_tex_instr *tex = nir_tex_instr_create(b->shader, old_tex->num_srcs + 1); 236 237 for (uint32_t i = 0; i < old_tex->num_srcs; i++) { 238 tex->src[i].src_type = old_tex->src[i].src_type; 239 240 switch (old_tex->src[i].src_type) { 241 case nir_tex_src_coord: 242 if (plane_format->has_chroma && conversion->chroma_reconstruction) { 243 assert(old_tex->src[i].src.is_ssa); 244 tex->src[i].src = 245 nir_src_for_ssa(implicit_downsampled_coords(state, 246 old_tex->src[i].src.ssa, 247 plane_format)); 248 break; 249 } 250 /* fall through */ 251 default: 252 nir_src_copy(&tex->src[i].src, &old_tex->src[i].src, tex); 253 break; 254 } 255 } 256 tex->src[tex->num_srcs - 1].src = nir_src_for_ssa(nir_imm_int(b, plane)); 257 tex->src[tex->num_srcs - 1].src_type = nir_tex_src_plane; 258 259 tex->sampler_dim = old_tex->sampler_dim; 260 tex->dest_type = old_tex->dest_type; 261 262 tex->op = old_tex->op; 263 tex->coord_components = old_tex->coord_components; 264 tex->is_new_style_shadow = old_tex->is_new_style_shadow; 265 tex->component = old_tex->component; 266 267 tex->texture_index = old_tex->texture_index; 268 tex->texture_array_size = old_tex->texture_array_size; 269 tex->texture = nir_deref_var_clone(old_tex->texture, tex); 270 271 tex->sampler_index = old_tex->sampler_index; 272 tex->sampler = nir_deref_var_clone(old_tex->sampler, tex); 273 274 nir_ssa_dest_init(&tex->instr, &tex->dest, 275 old_tex->dest.ssa.num_components, 276 nir_dest_bit_size(old_tex->dest), NULL); 277 nir_builder_instr_insert(b, &tex->instr); 278 279 return &tex->dest.ssa; 280 } 281 282 static unsigned 283 channel_to_component(enum isl_channel_select channel) 284 { 285 switch (channel) { 286 case ISL_CHANNEL_SELECT_RED: 287 return 0; 288 case ISL_CHANNEL_SELECT_GREEN: 289 return 1; 290 case ISL_CHANNEL_SELECT_BLUE: 291 return 2; 292 case ISL_CHANNEL_SELECT_ALPHA: 293 return 3; 294 default: 295 unreachable("invalid channel"); 296 return 0; 297 } 298 } 299 300 static enum isl_channel_select 301 swizzle_channel(struct isl_swizzle swizzle, unsigned channel) 302 { 303 switch (channel) { 304 case 0: 305 return swizzle.r; 306 case 1: 307 return swizzle.g; 308 case 2: 309 return swizzle.b; 310 case 3: 311 return swizzle.a; 312 default: 313 unreachable("invalid channel"); 314 return 0; 315 } 316 } 317 318 static bool 319 try_lower_tex_ycbcr(struct anv_pipeline *pipeline, 320 nir_builder *builder, 321 nir_tex_instr *tex) 322 { 323 nir_variable *var = tex->texture->var; 324 const struct anv_descriptor_set_layout *set_layout = 325 pipeline->layout->set[var->data.descriptor_set].layout; 326 const struct anv_descriptor_set_binding_layout *binding = 327 &set_layout->binding[var->data.binding]; 328 329 /* For the following instructions, we don't apply any change and let the 330 * instruction apply to the first plane. 331 */ 332 if (tex->op == nir_texop_txs || 333 tex->op == nir_texop_query_levels || 334 tex->op == nir_texop_lod) 335 return false; 336 337 if (binding->immutable_samplers == NULL) 338 return false; 339 340 unsigned texture_index = tex->texture_index; 341 if (tex->texture->deref.child) { 342 assert(tex->texture->deref.child->deref_type == nir_deref_type_array); 343 nir_deref_array *deref_array = nir_deref_as_array(tex->texture->deref.child); 344 if (deref_array->deref_array_type != nir_deref_array_type_direct) 345 return false; 346 size_t hw_binding_size = 347 anv_descriptor_set_binding_layout_get_hw_size(binding); 348 texture_index += MIN2(deref_array->base_offset, hw_binding_size - 1); 349 } 350 const struct anv_sampler *sampler = 351 binding->immutable_samplers[texture_index]; 352 353 if (sampler->conversion == NULL) 354 return false; 355 356 struct ycbcr_state state = { 357 .builder = builder, 358 .origin_tex = tex, 359 .conversion = sampler->conversion, 360 }; 361 362 builder->cursor = nir_before_instr(&tex->instr); 363 364 const struct anv_format *format = state.conversion->format; 365 const struct isl_format_layout *y_isl_layout = NULL; 366 for (uint32_t p = 0; p < format->n_planes; p++) { 367 if (!format->planes[p].has_chroma) 368 y_isl_layout = isl_format_get_layout(format->planes[p].isl_format); 369 } 370 assert(y_isl_layout != NULL); 371 uint8_t y_bpc = y_isl_layout->channels_array[0].bits; 372 373 /* |ycbcr_comp| holds components in the order : Cr-Y-Cb */ 374 nir_ssa_def *ycbcr_comp[5] = { NULL, NULL, NULL, 375 /* Use extra 2 channels for following swizzle */ 376 nir_imm_float(builder, 1.0f), 377 nir_imm_float(builder, 0.0f), 378 }; 379 uint8_t ycbcr_bpcs[5]; 380 memset(ycbcr_bpcs, y_bpc, sizeof(ycbcr_bpcs)); 381 382 /* Go through all the planes and gather the samples into a |ycbcr_comp| 383 * while applying a swizzle required by the spec: 384 * 385 * R, G, B should respectively map to Cr, Y, Cb 386 */ 387 for (uint32_t p = 0; p < format->n_planes; p++) { 388 const struct anv_format_plane *plane_format = &format->planes[p]; 389 nir_ssa_def *plane_sample = create_plane_tex_instr_implicit(&state, p); 390 391 for (uint32_t pc = 0; pc < 4; pc++) { 392 enum isl_channel_select ycbcr_swizzle = 393 swizzle_channel(plane_format->ycbcr_swizzle, pc); 394 if (ycbcr_swizzle == ISL_CHANNEL_SELECT_ZERO) 395 continue; 396 397 unsigned ycbcr_component = channel_to_component(ycbcr_swizzle); 398 ycbcr_comp[ycbcr_component] = nir_channel(builder, plane_sample, pc); 399 400 /* Also compute the number of bits for each component. */ 401 const struct isl_format_layout *isl_layout = 402 isl_format_get_layout(plane_format->isl_format); 403 ycbcr_bpcs[ycbcr_component] = isl_layout->channels_array[pc].bits; 404 } 405 } 406 407 /* Now remaps components to the order specified by the conversion. */ 408 nir_ssa_def *swizzled_comp[4] = { NULL, }; 409 uint32_t swizzled_bpcs[4] = { 0, }; 410 411 for (uint32_t i = 0; i < ARRAY_SIZE(state.conversion->mapping); i++) { 412 /* Maps to components in |ycbcr_comp| */ 413 static const uint32_t swizzle_mapping[] = { 414 [VK_COMPONENT_SWIZZLE_ZERO] = 4, 415 [VK_COMPONENT_SWIZZLE_ONE] = 3, 416 [VK_COMPONENT_SWIZZLE_R] = 0, 417 [VK_COMPONENT_SWIZZLE_G] = 1, 418 [VK_COMPONENT_SWIZZLE_B] = 2, 419 [VK_COMPONENT_SWIZZLE_A] = 3, 420 }; 421 const VkComponentSwizzle m = state.conversion->mapping[i]; 422 423 if (m == VK_COMPONENT_SWIZZLE_IDENTITY) { 424 swizzled_comp[i] = ycbcr_comp[i]; 425 swizzled_bpcs[i] = ycbcr_bpcs[i]; 426 } else { 427 swizzled_comp[i] = ycbcr_comp[swizzle_mapping[m]]; 428 swizzled_bpcs[i] = ycbcr_bpcs[swizzle_mapping[m]]; 429 } 430 } 431 432 nir_ssa_def *result = nir_vec(builder, swizzled_comp, 4); 433 if (state.conversion->ycbcr_model != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY_KHR) 434 result = convert_ycbcr(&state, result, swizzled_bpcs); 435 436 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result)); 437 nir_instr_remove(&tex->instr); 438 439 return true; 440 } 441 442 bool 443 anv_nir_lower_ycbcr_textures(nir_shader *shader, struct anv_pipeline *pipeline) 444 { 445 bool progress = false; 446 447 nir_foreach_function(function, shader) { 448 if (!function->impl) 449 continue; 450 451 bool function_progress = false; 452 nir_builder builder; 453 nir_builder_init(&builder, function->impl); 454 455 nir_foreach_block(block, function->impl) { 456 nir_foreach_instr_safe(instr, block) { 457 if (instr->type != nir_instr_type_tex) 458 continue; 459 460 nir_tex_instr *tex = nir_instr_as_tex(instr); 461 function_progress |= try_lower_tex_ycbcr(pipeline, &builder, tex); 462 } 463 } 464 465 if (function_progress) { 466 nir_metadata_preserve(function->impl, 467 nir_metadata_block_index | 468 nir_metadata_dominance); 469 } 470 471 progress |= function_progress; 472 } 473 474 return progress; 475 } 476