• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "vk_nir_convert_ycbcr.h"
29 
30 struct ycbcr_state {
31    nir_builder *builder;
32    nir_def *image_size;
33    nir_tex_instr *origin_tex;
34    nir_deref_instr *tex_deref;
35    const struct vk_ycbcr_conversion *conversion;
36 };
37 
38 /* TODO: we should probably replace this with a push constant/uniform. */
39 static nir_def *
get_texture_size(struct ycbcr_state * state,nir_deref_instr * texture)40 get_texture_size(struct ycbcr_state *state, nir_deref_instr *texture)
41 {
42    if (state->image_size)
43       return state->image_size;
44 
45    nir_builder *b = state->builder;
46    const struct glsl_type *type = texture->type;
47    nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);
48 
49    tex->op = nir_texop_txs;
50    tex->sampler_dim = glsl_get_sampler_dim(type);
51    tex->is_array = glsl_sampler_type_is_array(type);
52    tex->is_shadow = glsl_sampler_type_is_shadow(type);
53    tex->dest_type = nir_type_int32;
54 
55    tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
56                                      &texture->def);
57 
58    nir_def_init(&tex->instr, &tex->def, nir_tex_instr_dest_size(tex), 32);
59    nir_builder_instr_insert(b, &tex->instr);
60 
61    state->image_size = nir_i2f32(b, &tex->def);
62 
63    return state->image_size;
64 }
65 
66 static nir_def *
implicit_downsampled_coord(nir_builder * b,nir_def * value,nir_def * max_value,int div_scale)67 implicit_downsampled_coord(nir_builder *b,
68                            nir_def *value,
69                            nir_def *max_value,
70                            int div_scale)
71 {
72    return nir_fadd(b,
73                    value,
74                    nir_frcp(b,
75                             nir_fmul(b,
76                                      nir_imm_float(b, div_scale),
77                                      max_value)));
78 }
79 
80 static nir_def *
implicit_downsampled_coords(struct ycbcr_state * state,nir_def * old_coords,const struct anv_format_plane * plane_format)81 implicit_downsampled_coords(struct ycbcr_state *state,
82                             nir_def *old_coords,
83                             const struct anv_format_plane *plane_format)
84 {
85    nir_builder *b = state->builder;
86    const struct vk_ycbcr_conversion *conversion = state->conversion;
87    nir_def *image_size = get_texture_size(state, state->tex_deref);
88    nir_def *comp[4] = { NULL, };
89    int c;
90 
91    for (c = 0; c < ARRAY_SIZE(conversion->state.chroma_offsets); c++) {
92       if (plane_format->denominator_scales[c] > 1 &&
93           conversion->state.chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVEN) {
94          comp[c] = implicit_downsampled_coord(b,
95                                               nir_channel(b, old_coords, c),
96                                               nir_channel(b, image_size, c),
97                                               plane_format->denominator_scales[c]);
98       } else {
99          comp[c] = nir_channel(b, old_coords, c);
100       }
101    }
102 
103    /* Leave other coordinates untouched */
104    for (; c < old_coords->num_components; c++)
105       comp[c] = nir_channel(b, old_coords, c);
106 
107    return nir_vec(b, comp, old_coords->num_components);
108 }
109 
110 static nir_def *
create_plane_tex_instr_implicit(struct ycbcr_state * state,uint32_t plane)111 create_plane_tex_instr_implicit(struct ycbcr_state *state,
112                                 uint32_t plane)
113 {
114    nir_builder *b = state->builder;
115    const struct vk_ycbcr_conversion *conversion = state->conversion;
116    const struct anv_format_plane *plane_format =
117       &anv_get_format(conversion->state.format)->planes[plane];
118    nir_tex_instr *old_tex = state->origin_tex;
119    nir_tex_instr *tex = nir_tex_instr_create(b->shader, old_tex->num_srcs + 1);
120 
121    for (uint32_t i = 0; i < old_tex->num_srcs; i++) {
122       tex->src[i].src_type = old_tex->src[i].src_type;
123 
124       switch (old_tex->src[i].src_type) {
125       case nir_tex_src_coord:
126          if (plane_format->has_chroma && conversion->state.chroma_reconstruction) {
127             tex->src[i].src =
128                nir_src_for_ssa(implicit_downsampled_coords(state,
129                                                            old_tex->src[i].src.ssa,
130                                                            plane_format));
131             break;
132          }
133          FALLTHROUGH;
134       default:
135          tex->src[i].src = nir_src_for_ssa(old_tex->src[i].src.ssa);
136          break;
137       }
138    }
139    tex->src[tex->num_srcs - 1] = nir_tex_src_for_ssa(nir_tex_src_plane,
140                                                      nir_imm_int(b, plane));
141 
142    tex->sampler_dim = old_tex->sampler_dim;
143    tex->dest_type = old_tex->dest_type;
144 
145    tex->op = old_tex->op;
146    tex->coord_components = old_tex->coord_components;
147    tex->is_new_style_shadow = old_tex->is_new_style_shadow;
148    tex->component = old_tex->component;
149 
150    tex->texture_index = old_tex->texture_index;
151    tex->sampler_index = old_tex->sampler_index;
152    tex->is_array = old_tex->is_array;
153 
154    nir_def_init(&tex->instr, &tex->def, old_tex->def.num_components,
155                 old_tex->def.bit_size);
156    nir_builder_instr_insert(b, &tex->instr);
157 
158    return &tex->def;
159 }
160 
161 static unsigned
channel_to_component(enum isl_channel_select channel)162 channel_to_component(enum isl_channel_select channel)
163 {
164    switch (channel) {
165    case ISL_CHANNEL_SELECT_RED:
166       return 0;
167    case ISL_CHANNEL_SELECT_GREEN:
168       return 1;
169    case ISL_CHANNEL_SELECT_BLUE:
170       return 2;
171    case ISL_CHANNEL_SELECT_ALPHA:
172       return 3;
173    default:
174       unreachable("invalid channel");
175       return 0;
176    }
177 }
178 
179 static enum isl_channel_select
swizzle_channel(struct isl_swizzle swizzle,unsigned channel)180 swizzle_channel(struct isl_swizzle swizzle, unsigned channel)
181 {
182    switch (channel) {
183    case 0:
184       return swizzle.r;
185    case 1:
186       return swizzle.g;
187    case 2:
188       return swizzle.b;
189    case 3:
190       return swizzle.a;
191    default:
192       unreachable("invalid channel");
193       return 0;
194    }
195 }
196 
197 static bool
anv_nir_lower_ycbcr_textures_instr(nir_builder * builder,nir_instr * instr,void * cb_data)198 anv_nir_lower_ycbcr_textures_instr(nir_builder *builder,
199                                    nir_instr *instr,
200                                    void *cb_data)
201 {
202    const struct anv_pipeline_layout *layout = cb_data;
203 
204    if (instr->type != nir_instr_type_tex)
205       return false;
206 
207    nir_tex_instr *tex = nir_instr_as_tex(instr);
208 
209    int deref_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
210    assert(deref_src_idx >= 0);
211    nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);
212 
213    nir_variable *var = nir_deref_instr_get_variable(deref);
214    const struct anv_descriptor_set_layout *set_layout =
215       layout->set[var->data.descriptor_set].layout;
216    const struct anv_descriptor_set_binding_layout *binding =
217       &set_layout->binding[var->data.binding];
218 
219    /* For the following instructions, we don't apply any change and let the
220     * instruction apply to the first plane.
221     */
222    if (tex->op == nir_texop_txs ||
223        tex->op == nir_texop_query_levels ||
224        tex->op == nir_texop_lod)
225       return false;
226 
227    if (binding->immutable_samplers == NULL)
228       return false;
229 
230    assert(tex->texture_index == 0);
231    unsigned array_index = 0;
232    if (deref->deref_type != nir_deref_type_var) {
233       assert(deref->deref_type == nir_deref_type_array);
234       if (!nir_src_is_const(deref->arr.index))
235          return false;
236       array_index = nir_src_as_uint(deref->arr.index);
237       array_index = MIN2(array_index, binding->array_size - 1);
238    }
239    const struct anv_sampler *sampler = binding->immutable_samplers[array_index];
240 
241    if (sampler->conversion == NULL)
242       return false;
243 
244    struct ycbcr_state state = {
245       .builder = builder,
246       .origin_tex = tex,
247       .tex_deref = deref,
248       .conversion = sampler->conversion,
249    };
250 
251    builder->cursor = nir_before_instr(&tex->instr);
252 
253    const struct anv_format *format = anv_get_format(state.conversion->state.format);
254    const struct isl_format_layout *y_isl_layout = NULL;
255    for (uint32_t p = 0; p < format->n_planes; p++) {
256       if (!format->planes[p].has_chroma)
257          y_isl_layout = isl_format_get_layout(format->planes[p].isl_format);
258    }
259    assert(y_isl_layout != NULL);
260    uint8_t y_bpc = y_isl_layout->channels_array[0].bits;
261 
262    /* |ycbcr_comp| holds components in the order : Cr-Y-Cb */
263    nir_def *zero = nir_imm_float(builder, 0.0f);
264    nir_def *one = nir_imm_float(builder, 1.0f);
265    /* Use extra 2 channels for following swizzle */
266    nir_def *ycbcr_comp[5] = { zero, zero, zero, one, zero };
267 
268    uint8_t ycbcr_bpcs[5];
269    memset(ycbcr_bpcs, y_bpc, sizeof(ycbcr_bpcs));
270 
271    /* Go through all the planes and gather the samples into a |ycbcr_comp|
272     * while applying a swizzle required by the spec:
273     *
274     *    R, G, B should respectively map to Cr, Y, Cb
275     */
276    for (uint32_t p = 0; p < format->n_planes; p++) {
277       const struct anv_format_plane *plane_format = &format->planes[p];
278       nir_def *plane_sample = create_plane_tex_instr_implicit(&state, p);
279 
280       for (uint32_t pc = 0; pc < 4; pc++) {
281          enum isl_channel_select ycbcr_swizzle =
282             swizzle_channel(plane_format->ycbcr_swizzle, pc);
283          if (ycbcr_swizzle == ISL_CHANNEL_SELECT_ZERO)
284             continue;
285 
286          unsigned ycbcr_component = channel_to_component(ycbcr_swizzle);
287          ycbcr_comp[ycbcr_component] = nir_channel(builder, plane_sample, pc);
288 
289          /* Also compute the number of bits for each component. */
290          const struct isl_format_layout *isl_layout =
291             isl_format_get_layout(plane_format->isl_format);
292          ycbcr_bpcs[ycbcr_component] = isl_layout->channels_array[pc].bits;
293       }
294    }
295 
296    /* Now remaps components to the order specified by the conversion. */
297    nir_def *swizzled_comp[4] = { NULL, };
298    uint32_t swizzled_bpcs[4] = { 0, };
299 
300    for (uint32_t i = 0; i < ARRAY_SIZE(state.conversion->state.mapping); i++) {
301       /* Maps to components in |ycbcr_comp| */
302       static const uint32_t swizzle_mapping[] = {
303          [VK_COMPONENT_SWIZZLE_ZERO] = 4,
304          [VK_COMPONENT_SWIZZLE_ONE]  = 3,
305          [VK_COMPONENT_SWIZZLE_R]    = 0,
306          [VK_COMPONENT_SWIZZLE_G]    = 1,
307          [VK_COMPONENT_SWIZZLE_B]    = 2,
308          [VK_COMPONENT_SWIZZLE_A]    = 3,
309       };
310       const VkComponentSwizzle m = state.conversion->state.mapping[i];
311 
312       if (m == VK_COMPONENT_SWIZZLE_IDENTITY) {
313          swizzled_comp[i] = ycbcr_comp[i];
314          swizzled_bpcs[i] = ycbcr_bpcs[i];
315       } else {
316          swizzled_comp[i] = ycbcr_comp[swizzle_mapping[m]];
317          swizzled_bpcs[i] = ycbcr_bpcs[swizzle_mapping[m]];
318       }
319    }
320 
321    nir_def *result = nir_vec(builder, swizzled_comp, 4);
322    if (state.conversion->state.ycbcr_model != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY) {
323       result = nir_convert_ycbcr_to_rgb(builder,
324                                         state.conversion->state.ycbcr_model,
325                                         state.conversion->state.ycbcr_range,
326                                         result,
327                                         swizzled_bpcs);
328    }
329 
330    nir_def_replace(&tex->def, result);
331 
332    return true;
333 }
334 
335 bool
anv_nir_lower_ycbcr_textures(nir_shader * shader,const struct anv_pipeline_layout * layout)336 anv_nir_lower_ycbcr_textures(nir_shader *shader,
337                              const struct anv_pipeline_layout *layout)
338 {
339    return nir_shader_instructions_pass(shader,
340                                        anv_nir_lower_ycbcr_textures_instr,
341                                        nir_metadata_control_flow,
342                                        (void *)layout);
343 }
344