Debian Codegraph @

src/imagination/vulkan/pvr_nir_lower_ycbcr.c

raw
1 /*
2 * Copyright © 2026 Imagination Technologies Ltd.
3 * Copyright © 2017 Intel Corporation
4 * SPDX-License-Identifier: MIT
5 *
6 * Based on vk_nir_convert_ycbcr.c.
7 *
8 * We need this to fixup YCBCR_IDENTITY conversion since the TPU clamps the
9 * output from csc into the range 0.0 to 1.0.
10 */
11 #include "pvr_nir_lower_ycbcr.h"
12
13 #include "nir_builder.h"
14 #include "vk_ycbcr_conversion.h"
15
16 struct lower_ycbcr_tex_state {
17 nir_pvr_ycbcr_conversion_lookup_cb cb;
18 const void *cb_data;
19 };
20
21 static bool
22 lower_ycbcr_tex_instr(nir_builder *b, nir_tex_instr *tex, void *_state)
23 {
24 const struct lower_ycbcr_tex_state *state = _state;
25
26 /* For the following instructions, we don't apply any change and let the
27 * instruction apply to the first plane.
28 */
29 if (tex->op == nir_texop_txs || tex->op == nir_texop_query_levels ||
30 tex->op == nir_texop_lod)
31 return false;
32
33 int deref_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
34 assert(deref_src_idx >= 0);
35 nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);
36
37 nir_variable *var = nir_deref_instr_get_variable(deref);
38 uint32_t set = var->data.descriptor_set;
39 uint32_t binding = var->data.binding;
40
41 assert(tex->texture_index == 0);
42 unsigned array_index = 0;
43 if (deref->deref_type != nir_deref_type_var) {
44 assert(deref->deref_type == nir_deref_type_array);
45 if (!nir_src_is_const(deref->arr.index))
46 return false;
47 array_index = nir_src_as_uint(deref->arr.index);
48 }
49
50 const struct vk_ycbcr_conversion_state *conversion =
51 state->cb(state->cb_data, set, binding, array_index);
52 if (conversion == NULL)
53 return false;
54
55 /* Range expansion fix only needed for YCbCr Identity */
56 if (conversion->ycbcr_model !=
57 VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
58 return false;
59
60 b->cursor = nir_after_instr(&tex->instr);
61 nir_def *result =
62 nir_fsub(b, &tex->def, nir_imm_vec4(b, 0.5, 0.0, 0.5, 0.0));
63 nir_def_rewrite_uses_after(&tex->def, result);
64 return true;
65 }
66
67 bool nir_pvr_lower_ycbcr_tex(nir_shader *nir,
68 nir_pvr_ycbcr_conversion_lookup_cb cb,
69 const void *cb_data)
70 {
71 struct lower_ycbcr_tex_state state = {
72 .cb = cb,
73 .cb_data = cb_data,
74 };
75
76 return nir_shader_tex_pass(nir,
77 lower_ycbcr_tex_instr,
78 nir_metadata_control_flow,
79 &state);
80 }