radv: gather info about PS inputs in the shader info pass
[mesa.git] / src / amd / vulkan / radv_nir_lower_ycbcr_textures.c
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 "radv_private.h"
25 #include "radv_shader.h"
26 #include "vk_format.h"
27 #include "nir/nir.h"
28 #include "nir/nir_builder.h"
29
30 struct ycbcr_state {
31 nir_builder *builder;
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;
36 };
37
38 static nir_ssa_def *
39 y_range(nir_builder *b,
40 nir_ssa_def *y_channel,
41 int bpc,
42 VkSamplerYcbcrRange range)
43 {
44 switch (range) {
45 case VK_SAMPLER_YCBCR_RANGE_ITU_FULL:
46 return y_channel;
47 case VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:
48 return nir_fmul(b,
49 nir_fadd(b,
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))));
54 default:
55 unreachable("missing Ycbcr range");
56 return NULL;
57 }
58 }
59
60 static nir_ssa_def *
61 chroma_range(nir_builder *b,
62 nir_ssa_def *chroma_channel,
63 int bpc,
64 VkSamplerYcbcrRange range)
65 {
66 switch (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:
71 return nir_fmul(b,
72 nir_fadd(b,
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))));
77 default:
78 unreachable("missing Ycbcr range");
79 return NULL;
80 }
81 }
82
83 typedef struct nir_const_value_3_4 {
84 nir_const_value v[3][4];
85 } nir_const_value_3_4;
86
87 static const nir_const_value_3_4 *
88 ycbcr_model_to_rgb_matrix(VkSamplerYcbcrModelConversion model)
89 {
90 switch (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 } },
96 } };
97
98 return &bt601;
99 }
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 } },
105 } };
106
107 return &bt709;
108 }
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 } },
114 } };
115
116 return &bt2020;
117 }
118 default:
119 unreachable("missing Ycbcr model");
120 return NULL;
121 }
122 }
123
124 static nir_ssa_def *
125 convert_ycbcr(struct ycbcr_state *state,
126 nir_ssa_def *raw_channels,
127 uint8_t bits)
128 {
129 nir_builder *b = state->builder;
130 const struct radv_sampler_ycbcr_conversion *conversion = state->conversion;
131
132 nir_ssa_def *expanded_channels =
133 nir_vec4(b,
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));
141
142 if (conversion->ycbcr_model == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
143 return expanded_channels;
144
145 const nir_const_value_3_4 *conversion_matrix =
146 ycbcr_model_to_rgb_matrix(conversion->ycbcr_model);
147
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]))
152 };
153
154 return nir_vec4(b,
155 converted_channels[0], converted_channels[1],
156 converted_channels[2], nir_imm_float(b, 1.0f));
157 }
158
159 static nir_ssa_def *
160 get_texture_size(struct ycbcr_state *state, nir_deref_instr *texture)
161 {
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);
165
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;
171
172 tex->src[0].src_type = nir_tex_src_texture_deref;
173 tex->src[0].src = nir_src_for_ssa(&texture->dest.ssa);
174
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);
178
179 return nir_i2f32(b, &tex->dest.ssa);
180 }
181
182 static nir_ssa_def *
183 implicit_downsampled_coord(nir_builder *b,
184 nir_ssa_def *value,
185 nir_ssa_def *max_value,
186 int div_scale)
187 {
188 return nir_fadd(b,
189 value,
190 nir_fdiv(b,
191 nir_imm_float(b, 1.0f),
192 nir_fmul(b,
193 nir_imm_float(b, div_scale),
194 max_value)));
195 }
196
197 static nir_ssa_def *
198 implicit_downsampled_coords(struct ycbcr_state *state,
199 nir_ssa_def *old_coords)
200 {
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};
207
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) {
211 if (!image_size)
212 image_size = get_texture_size(state, state->tex_deref);
213
214 comp[c] = implicit_downsampled_coord(b,
215 nir_channel(b, old_coords, c),
216 nir_channel(b, image_size, c),
217 divisors[c]);
218 } else {
219 comp[c] = nir_channel(b, old_coords, c);
220 }
221 }
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 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;
235
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);
240 tex->src[i].src =
241 nir_src_for_ssa(implicit_downsampled_coords(state,
242 old_tex->src[i].src.ssa));
243 break;
244 }
245 /* fall through */
246 default:
247 nir_src_copy(&tex->src[i].src, &old_tex->src[i].src, tex);
248 break;
249 }
250 }
251
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;
254
255 tex->sampler_dim = old_tex->sampler_dim;
256 tex->dest_type = old_tex->dest_type;
257 tex->is_array = old_tex->is_array;
258
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;
263
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;
267
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);
272
273 return &tex->dest.ssa;
274 }
275
276 struct swizzle_info {
277 unsigned plane[4];
278 unsigned swizzle[4];
279 };
280
281 static struct swizzle_info
282 get_plane_swizzles(VkFormat format)
283 {
284 int planes = vk_format_get_plane_count(format);
285 switch (planes) {
286 case 3:
287 return (struct swizzle_info) {
288 {2, 0, 1, 0},
289 {0, 0, 0, 3}
290 };
291 case 2:
292 return (struct swizzle_info) {
293 {1, 0, 1, 0},
294 {1, 0, 0, 3}
295 };
296 case 1:
297 return (struct swizzle_info) {
298 {0, 0, 0, 0},
299 {0, 1, 2, 3}
300 };
301 default:
302 unreachable("unhandled plane count for ycbcr swizzling");
303 }
304 }
305
306
307 static nir_ssa_def *
308 build_swizzled_components(nir_builder *builder,
309 VkFormat format,
310 VkComponentMapping mapping,
311 nir_ssa_def **plane_values)
312 {
313 struct swizzle_info plane_swizzle = get_plane_swizzles(format);
314 enum vk_swizzle swizzles[4];
315 nir_ssa_def *values[4];
316
317 vk_format_compose_swizzles(&mapping, (const unsigned char[4]){0,1,2,3}, swizzles);
318
319 nir_ssa_def *zero = nir_imm_float(builder, 0.0f);
320 nir_ssa_def *one = nir_imm_float(builder, 1.0f);
321
322 for (unsigned i = 0; i < 4; ++i) {
323 switch(swizzles[i]) {
324 case VK_SWIZZLE_X:
325 case VK_SWIZZLE_Y:
326 case VK_SWIZZLE_Z:
327 case VK_SWIZZLE_W: {
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]);
332 break;
333 }
334 case VK_SWIZZLE_0:
335 values[i] = zero;
336 break;
337 case VK_SWIZZLE_1:
338 values[i] = one;
339 break;
340 default:
341 unreachable("unhandled swizzle");
342 }
343 }
344 return nir_vec(builder, values, 4);
345 }
346
347 static bool
348 try_lower_tex_ycbcr(const struct radv_pipeline_layout *layout,
349 nir_builder *builder,
350 nir_tex_instr *tex)
351 {
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);
355
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);
363
364 if (!ycbcr_samplers)
365 return false;
366
367 /* For the following instructions, we don't apply any change and let the
368 * instruction apply to the first plane.
369 */
370 if (tex->op == nir_texop_txs ||
371 tex->op == nir_texop_query_levels ||
372 tex->op == nir_texop_lod)
373 return false;
374
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))
380 return false;
381 array_index = nir_src_as_uint(deref->arr.index);
382 array_index = MIN2(array_index, binding->array_size - 1);
383 }
384 const struct radv_sampler_ycbcr_conversion *ycbcr_sampler = ycbcr_samplers + array_index;
385
386 if (ycbcr_sampler->format == VK_FORMAT_UNDEFINED)
387 return false;
388
389 struct ycbcr_state state = {
390 .builder = builder,
391 .origin_tex = tex,
392 .tex_deref = deref,
393 .conversion = ycbcr_sampler,
394 };
395
396 builder->cursor = nir_before_instr(&tex->instr);
397
398 VkFormat format = state.conversion->format;
399 const int plane_count = vk_format_get_plane_count(format);
400 nir_ssa_def *plane_values[3];
401
402 for (int p = 0; p < plane_count; ++p) {
403 plane_values[p] = create_plane_tex_instr_implicit(&state, p);
404 }
405
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));
410 }
411
412 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));
413 nir_instr_remove(&tex->instr);
414
415 return true;
416 }
417
418 bool
419 radv_nir_lower_ycbcr_textures(nir_shader *shader,
420 const struct radv_pipeline_layout *layout)
421 {
422 bool progress = false;
423
424 nir_foreach_function(function, shader) {
425 if (!function->impl)
426 continue;
427
428 bool function_progress = false;
429 nir_builder builder;
430 nir_builder_init(&builder, function->impl);
431
432 nir_foreach_block(block, function->impl) {
433 nir_foreach_instr_safe(instr, block) {
434 if (instr->type != nir_instr_type_tex)
435 continue;
436
437 nir_tex_instr *tex = nir_instr_as_tex(instr);
438 function_progress |= try_lower_tex_ycbcr(layout, &builder, tex);
439 }
440 }
441
442 if (function_progress) {
443 nir_metadata_preserve(function->impl,
444 nir_metadata_block_index |
445 nir_metadata_dominance);
446 }
447
448 progress |= function_progress;
449 }
450
451 return progress;
452 }