2 * Copyright © 2017 Intel Corporation
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:
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
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
24 #include "radv_private.h"
25 #include "radv_shader.h"
26 #include "vk_format.h"
28 #include "nir/nir_builder.h"
32 nir_ssa_def
*image_size
;
33 nir_tex_instr
*origin_tex
;
34 nir_deref_instr
*tex_deref
;
35 const struct radv_sampler_ycbcr_conversion
*conversion
;
39 y_range(nir_builder
*b
,
40 nir_ssa_def
*y_channel
,
42 VkSamplerYcbcrRange range
)
45 case VK_SAMPLER_YCBCR_RANGE_ITU_FULL
:
47 case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW
:
50 nir_fmul(b
, y_channel
,
51 nir_imm_float(b
, pow(2, bpc
) - 1)),
52 nir_imm_float(b
, -16.0f
* pow(2, bpc
- 8))),
53 nir_frcp(b
, nir_imm_float(b
, 219.0f
* pow(2, bpc
- 8))));
55 unreachable("missing Ycbcr range");
61 chroma_range(nir_builder
*b
,
62 nir_ssa_def
*chroma_channel
,
64 VkSamplerYcbcrRange range
)
67 case VK_SAMPLER_YCBCR_RANGE_ITU_FULL
:
68 return nir_fadd(b
, chroma_channel
,
69 nir_imm_float(b
, -pow(2, bpc
- 1) / (pow(2, bpc
) - 1.0f
)));
70 case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW
:
73 nir_fmul(b
, chroma_channel
,
74 nir_imm_float(b
, pow(2, bpc
) - 1)),
75 nir_imm_float(b
, -128.0f
* pow(2, bpc
- 8))),
76 nir_frcp(b
, nir_imm_float(b
, 224.0f
* pow(2, bpc
- 8))));
78 unreachable("missing Ycbcr range");
83 typedef struct nir_const_value_3_4
{
84 nir_const_value v
[3][4];
85 } nir_const_value_3_4
;
87 static const nir_const_value_3_4
*
88 ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversion model
)
91 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601
: {
92 static const nir_const_value_3_4 bt601
= { {
93 { { .f32
= 1.402f
}, { .f32
= 1.0f
}, { .f32
= 0.0f
}, { .f32
= 0.0f
} },
94 { { .f32
= -0.714136286201022f
}, { .f32
= 1.0f
}, { .f32
= -0.344136286201022f
}, { .f32
= 0.0f
} },
95 { { .f32
= 0.0f
}, { .f32
= 1.0f
}, { .f32
= 1.772f
}, { .f32
= 0.0f
} },
100 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709
: {
101 static const nir_const_value_3_4 bt709
= { {
102 { { .f32
= 1.5748031496063f
}, { .f32
= 1.0f
}, { .f32
= 0.0f
}, { .f32
= 0.0f
} },
103 { { .f32
= -0.468125209181067f
}, { .f32
= 1.0f
}, { .f32
= -0.187327487470334f
}, { .f32
= 0.0f
} },
104 { { .f32
= 0.0f
}, { .f32
= 1.0f
}, { .f32
= 1.85563184264242f
}, { .f32
= 0.0f
} },
109 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020
: {
110 static const nir_const_value_3_4 bt2020
= { {
111 { { .f32
= 1.4746f
}, { .f32
= 1.0f
}, { .f32
= 0.0f
}, { .f32
= 0.0f
} },
112 { { .f32
= -0.571353126843658f
}, { .f32
= 1.0f
}, { .f32
= -0.164553126843658f
}, { .f32
= 0.0f
} },
113 { { .f32
= 0.0f
}, { .f32
= 1.0f
}, { .f32
= 1.8814f
}, { .f32
= 0.0f
} },
119 unreachable("missing Ycbcr model");
125 convert_ycbcr(struct ycbcr_state
*state
,
126 nir_ssa_def
*raw_channels
,
129 nir_builder
*b
= state
->builder
;
130 const struct radv_sampler_ycbcr_conversion
*conversion
= state
->conversion
;
132 nir_ssa_def
*expanded_channels
=
134 chroma_range(b
, nir_channel(b
, raw_channels
, 0),
135 bits
, conversion
->ycbcr_range
),
136 y_range(b
, nir_channel(b
, raw_channels
, 1),
137 bits
, conversion
->ycbcr_range
),
138 chroma_range(b
, nir_channel(b
, raw_channels
, 2),
139 bits
, conversion
->ycbcr_range
),
140 nir_imm_float(b
, 1.0f
));
142 if (conversion
->ycbcr_model
== VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY
)
143 return expanded_channels
;
145 const nir_const_value_3_4
*conversion_matrix
=
146 ycbcr_model_to_rgb_matrix(conversion
->ycbcr_model
);
148 nir_ssa_def
*converted_channels
[] = {
149 nir_fdot4(b
, expanded_channels
, nir_build_imm(b
, 4, 32, conversion_matrix
->v
[0])),
150 nir_fdot4(b
, expanded_channels
, nir_build_imm(b
, 4, 32, conversion_matrix
->v
[1])),
151 nir_fdot4(b
, expanded_channels
, nir_build_imm(b
, 4, 32, conversion_matrix
->v
[2]))
155 converted_channels
[0], converted_channels
[1],
156 converted_channels
[2], nir_imm_float(b
, 1.0f
));
160 get_texture_size(struct ycbcr_state
*state
, nir_deref_instr
*texture
)
162 nir_builder
*b
= state
->builder
;
163 const struct glsl_type
*type
= texture
->type
;
164 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, 1);
166 tex
->op
= nir_texop_txs
;
167 tex
->sampler_dim
= glsl_get_sampler_dim(type
);
168 tex
->is_array
= glsl_sampler_type_is_array(type
);
169 tex
->is_shadow
= glsl_sampler_type_is_shadow(type
);
170 tex
->dest_type
= nir_type_int
;
172 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
173 tex
->src
[0].src
= nir_src_for_ssa(&texture
->dest
.ssa
);
175 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
,
176 nir_tex_instr_dest_size(tex
), 32, NULL
);
177 nir_builder_instr_insert(b
, &tex
->instr
);
179 return nir_i2f32(b
, &tex
->dest
.ssa
);
183 implicit_downsampled_coord(nir_builder
*b
,
185 nir_ssa_def
*max_value
,
191 nir_imm_float(b
, 1.0f
),
193 nir_imm_float(b
, div_scale
),
198 implicit_downsampled_coords(struct ycbcr_state
*state
,
199 nir_ssa_def
*old_coords
)
201 nir_builder
*b
= state
->builder
;
202 const struct radv_sampler_ycbcr_conversion
*conversion
= state
->conversion
;
203 nir_ssa_def
*image_size
= NULL
;
204 nir_ssa_def
*comp
[4] = { NULL
, };
205 const struct vk_format_description
*fmt_desc
= vk_format_description(state
->conversion
->format
);
206 const unsigned divisors
[2] = {fmt_desc
->width_divisor
, fmt_desc
->height_divisor
};
208 for (int c
= 0; c
< old_coords
->num_components
; c
++) {
209 if (c
< ARRAY_SIZE(divisors
) && divisors
[c
] > 1 &&
210 conversion
->chroma_offsets
[c
] == VK_CHROMA_LOCATION_COSITED_EVEN
) {
212 image_size
= get_texture_size(state
, state
->tex_deref
);
214 comp
[c
] = implicit_downsampled_coord(b
,
215 nir_channel(b
, old_coords
, c
),
216 nir_channel(b
, image_size
, c
),
219 comp
[c
] = nir_channel(b
, old_coords
, c
);
223 return nir_vec(b
, comp
, old_coords
->num_components
);
227 create_plane_tex_instr_implicit(struct ycbcr_state
*state
,
230 nir_builder
*b
= state
->builder
;
231 nir_tex_instr
*old_tex
= state
->origin_tex
;
232 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, old_tex
->num_srcs
+ 1);
233 for (uint32_t i
= 0; i
< old_tex
->num_srcs
; i
++) {
234 tex
->src
[i
].src_type
= old_tex
->src
[i
].src_type
;
236 switch (old_tex
->src
[i
].src_type
) {
237 case nir_tex_src_coord
:
238 if (plane
&& true/*state->conversion->chroma_reconstruction*/) {
239 assert(old_tex
->src
[i
].src
.is_ssa
);
241 nir_src_for_ssa(implicit_downsampled_coords(state
,
242 old_tex
->src
[i
].src
.ssa
));
247 nir_src_copy(&tex
->src
[i
].src
, &old_tex
->src
[i
].src
, tex
);
252 tex
->src
[tex
->num_srcs
- 1].src
= nir_src_for_ssa(nir_imm_int(b
, plane
));
253 tex
->src
[tex
->num_srcs
- 1].src_type
= nir_tex_src_plane
;
255 tex
->sampler_dim
= old_tex
->sampler_dim
;
256 tex
->dest_type
= old_tex
->dest_type
;
257 tex
->is_array
= old_tex
->is_array
;
259 tex
->op
= old_tex
->op
;
260 tex
->coord_components
= old_tex
->coord_components
;
261 tex
->is_new_style_shadow
= old_tex
->is_new_style_shadow
;
262 tex
->component
= old_tex
->component
;
264 tex
->texture_index
= old_tex
->texture_index
;
265 tex
->texture_array_size
= old_tex
->texture_array_size
;
266 tex
->sampler_index
= old_tex
->sampler_index
;
268 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
,
269 old_tex
->dest
.ssa
.num_components
,
270 nir_dest_bit_size(old_tex
->dest
), NULL
);
271 nir_builder_instr_insert(b
, &tex
->instr
);
273 return &tex
->dest
.ssa
;
276 struct swizzle_info
{
281 static struct swizzle_info
282 get_plane_swizzles(VkFormat format
)
284 int planes
= vk_format_get_plane_count(format
);
287 return (struct swizzle_info
) {
292 return (struct swizzle_info
) {
297 return (struct swizzle_info
) {
302 unreachable("unhandled plane count for ycbcr swizzling");
308 build_swizzled_components(nir_builder
*builder
,
310 VkComponentMapping mapping
,
311 nir_ssa_def
**plane_values
)
313 struct swizzle_info plane_swizzle
= get_plane_swizzles(format
);
314 enum vk_swizzle swizzles
[4];
315 nir_ssa_def
*values
[4];
317 vk_format_compose_swizzles(&mapping
, (const unsigned char[4]){0,1,2,3}, swizzles
);
319 nir_ssa_def
*zero
= nir_imm_float(builder
, 0.0f
);
320 nir_ssa_def
*one
= nir_imm_float(builder
, 1.0f
);
322 for (unsigned i
= 0; i
< 4; ++i
) {
323 switch(swizzles
[i
]) {
328 unsigned channel
= swizzles
[i
] - VK_SWIZZLE_X
;
329 values
[i
] = nir_channel(builder
,
330 plane_values
[plane_swizzle
.plane
[channel
]],
331 plane_swizzle
.swizzle
[channel
]);
341 unreachable("unhandled swizzle");
344 return nir_vec(builder
, values
, 4);
348 try_lower_tex_ycbcr(const struct radv_pipeline_layout
*layout
,
349 nir_builder
*builder
,
352 int deref_src_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_texture_deref
);
353 assert(deref_src_idx
>= 0);
354 nir_deref_instr
*deref
= nir_src_as_deref(tex
->src
[deref_src_idx
].src
);
356 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
357 const struct radv_descriptor_set_layout
*set_layout
=
358 layout
->set
[var
->data
.descriptor_set
].layout
;
359 const struct radv_descriptor_set_binding_layout
*binding
=
360 &set_layout
->binding
[var
->data
.binding
];
361 const struct radv_sampler_ycbcr_conversion
*ycbcr_samplers
=
362 radv_immutable_ycbcr_samplers(set_layout
, var
->data
.binding
);
367 /* For the following instructions, we don't apply any change and let the
368 * instruction apply to the first plane.
370 if (tex
->op
== nir_texop_txs
||
371 tex
->op
== nir_texop_query_levels
||
372 tex
->op
== nir_texop_lod
)
375 assert(tex
->texture_index
== 0);
376 unsigned array_index
= 0;
377 if (deref
->deref_type
!= nir_deref_type_var
) {
378 assert(deref
->deref_type
== nir_deref_type_array
);
379 if (!nir_src_is_const(deref
->arr
.index
))
381 array_index
= nir_src_as_uint(deref
->arr
.index
);
382 array_index
= MIN2(array_index
, binding
->array_size
- 1);
384 const struct radv_sampler_ycbcr_conversion
*ycbcr_sampler
= ycbcr_samplers
+ array_index
;
386 if (ycbcr_sampler
->format
== VK_FORMAT_UNDEFINED
)
389 struct ycbcr_state state
= {
393 .conversion
= ycbcr_sampler
,
396 builder
->cursor
= nir_before_instr(&tex
->instr
);
398 VkFormat format
= state
.conversion
->format
;
399 const int plane_count
= vk_format_get_plane_count(format
);
400 nir_ssa_def
*plane_values
[3];
402 for (int p
= 0; p
< plane_count
; ++p
) {
403 plane_values
[p
] = create_plane_tex_instr_implicit(&state
, p
);
406 nir_ssa_def
*result
= build_swizzled_components(builder
, format
, ycbcr_sampler
->components
, plane_values
);
407 if (state
.conversion
->ycbcr_model
!= VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY
) {
408 VkFormat first_format
= vk_format_get_plane_format(format
, 0);
409 result
= convert_ycbcr(&state
, result
, vk_format_get_component_bits(first_format
, VK_FORMAT_COLORSPACE_RGB
, VK_SWIZZLE_X
));
412 nir_ssa_def_rewrite_uses(&tex
->dest
.ssa
, nir_src_for_ssa(result
));
413 nir_instr_remove(&tex
->instr
);
419 radv_nir_lower_ycbcr_textures(nir_shader
*shader
,
420 const struct radv_pipeline_layout
*layout
)
422 bool progress
= false;
424 nir_foreach_function(function
, shader
) {
428 bool function_progress
= false;
430 nir_builder_init(&builder
, function
->impl
);
432 nir_foreach_block(block
, function
->impl
) {
433 nir_foreach_instr_safe(instr
, block
) {
434 if (instr
->type
!= nir_instr_type_tex
)
437 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
438 function_progress
|= try_lower_tex_ycbcr(layout
, &builder
, tex
);
442 if (function_progress
) {
443 nir_metadata_preserve(function
->impl
,
444 nir_metadata_block_index
|
445 nir_metadata_dominance
);
448 progress
|= function_progress
;