17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg/**
257ec681f3Smrg * This lowering pass supports (as configured via nir_lower_image_options)
267ec681f3Smrg * image related conversions:
277ec681f3Smrg *   + cube array size lowering. The size operation is converted from cube
287ec681f3Smrg *     size to a 2d-array with the z component divided by 6.
297ec681f3Smrg */
307ec681f3Smrg
317ec681f3Smrg#include "nir.h"
327ec681f3Smrg#include "nir_builder.h"
337ec681f3Smrg
347ec681f3Smrgstatic void
357ec681f3Smrglower_cube_size(nir_builder *b, nir_intrinsic_instr *intrin)
367ec681f3Smrg{
377ec681f3Smrg   assert(nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE);
387ec681f3Smrg
397ec681f3Smrg   b->cursor = nir_before_instr(&intrin->instr);
407ec681f3Smrg
417ec681f3Smrg   nir_intrinsic_instr *_2darray_size =
427ec681f3Smrg      nir_instr_as_intrinsic(nir_instr_clone(b->shader, &intrin->instr));
437ec681f3Smrg   nir_intrinsic_set_image_dim(_2darray_size, GLSL_SAMPLER_DIM_2D);
447ec681f3Smrg   nir_intrinsic_set_image_array(_2darray_size, true);
457ec681f3Smrg   nir_builder_instr_insert(b, &_2darray_size->instr);
467ec681f3Smrg
477ec681f3Smrg   nir_ssa_def *size = nir_instr_ssa_def(&_2darray_size->instr);
487ec681f3Smrg   nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS] = { NULL, };
497ec681f3Smrg   unsigned coord_comps = intrin->dest.ssa.num_components;
507ec681f3Smrg   for (unsigned c = 0; c < coord_comps; c++) {
517ec681f3Smrg      if (c == 2) {
527ec681f3Smrg         comps[2] = nir_idiv(b, nir_channel(b, size, 2), nir_imm_int(b, 6));
537ec681f3Smrg      } else {
547ec681f3Smrg         comps[c] = nir_channel(b, size, c);
557ec681f3Smrg      }
567ec681f3Smrg   }
577ec681f3Smrg
587ec681f3Smrg   nir_ssa_def *vec = nir_vec(b, comps, intrin->dest.ssa.num_components);
597ec681f3Smrg   nir_ssa_def_rewrite_uses(&intrin->dest.ssa, vec);
607ec681f3Smrg   nir_instr_remove(&intrin->instr);
617ec681f3Smrg   nir_instr_free(&intrin->instr);
627ec681f3Smrg}
637ec681f3Smrg
647ec681f3Smrgstatic bool
657ec681f3Smrglower_image_instr(nir_builder *b, nir_instr *instr, void *state)
667ec681f3Smrg{
677ec681f3Smrg   if (instr->type != nir_instr_type_intrinsic)
687ec681f3Smrg      return false;
697ec681f3Smrg
707ec681f3Smrg   const nir_lower_image_options *options = state;
717ec681f3Smrg   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
727ec681f3Smrg
737ec681f3Smrg   switch (intrin->intrinsic) {
747ec681f3Smrg   case nir_intrinsic_image_size:
757ec681f3Smrg   case nir_intrinsic_image_deref_size:
767ec681f3Smrg   case nir_intrinsic_bindless_image_size:
777ec681f3Smrg      if (options->lower_cube_size &&
787ec681f3Smrg          nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE) {
797ec681f3Smrg         lower_cube_size(b, intrin);
807ec681f3Smrg         return true;
817ec681f3Smrg      }
827ec681f3Smrg      return false;
837ec681f3Smrg
847ec681f3Smrg   default:
857ec681f3Smrg      return false;
867ec681f3Smrg   }
877ec681f3Smrg}
887ec681f3Smrg
897ec681f3Smrgbool
907ec681f3Smrgnir_lower_image(nir_shader *nir, const nir_lower_image_options *options)
917ec681f3Smrg{
927ec681f3Smrg   return nir_shader_instructions_pass(nir, lower_image_instr,
937ec681f3Smrg                                       nir_metadata_block_index |
947ec681f3Smrg                                       nir_metadata_dominance, (void*)options);
957ec681f3Smrg}
96