1/* 2 * Copyright © 2021 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/** 25 * This lowering pass supports (as configured via nir_lower_image_options) 26 * image related conversions: 27 * + cube array size lowering. The size operation is converted from cube 28 * size to a 2d-array with the z component divided by 6. 29 */ 30 31#include "nir.h" 32#include "nir_builder.h" 33 34static void 35lower_cube_size(nir_builder *b, nir_intrinsic_instr *intrin) 36{ 37 assert(nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE); 38 39 b->cursor = nir_before_instr(&intrin->instr); 40 41 nir_intrinsic_instr *_2darray_size = 42 nir_instr_as_intrinsic(nir_instr_clone(b->shader, &intrin->instr)); 43 nir_intrinsic_set_image_dim(_2darray_size, GLSL_SAMPLER_DIM_2D); 44 nir_intrinsic_set_image_array(_2darray_size, true); 45 nir_builder_instr_insert(b, &_2darray_size->instr); 46 47 nir_ssa_def *size = nir_instr_ssa_def(&_2darray_size->instr); 48 nir_ssa_scalar comps[NIR_MAX_VEC_COMPONENTS] = { 0 }; 49 unsigned coord_comps = intrin->dest.ssa.num_components; 50 for (unsigned c = 0; c < coord_comps; c++) { 51 if (c == 2) { 52 comps[2] = nir_get_ssa_scalar(nir_idiv(b, nir_channel(b, size, 2), nir_imm_int(b, 6)), 0); 53 } else { 54 comps[c] = nir_get_ssa_scalar(size, c); 55 } 56 } 57 58 nir_ssa_def *vec = nir_vec_scalars(b, comps, intrin->dest.ssa.num_components); 59 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, vec); 60 nir_instr_remove(&intrin->instr); 61 nir_instr_free(&intrin->instr); 62} 63 64static bool 65lower_image_instr(nir_builder *b, nir_instr *instr, void *state) 66{ 67 if (instr->type != nir_instr_type_intrinsic) 68 return false; 69 70 const nir_lower_image_options *options = state; 71 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 72 73 switch (intrin->intrinsic) { 74 case nir_intrinsic_image_size: 75 case nir_intrinsic_image_deref_size: 76 case nir_intrinsic_bindless_image_size: 77 if (options->lower_cube_size && 78 nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE) { 79 lower_cube_size(b, intrin); 80 return true; 81 } 82 return false; 83 84 default: 85 return false; 86 } 87} 88 89bool 90nir_lower_image(nir_shader *nir, const nir_lower_image_options *options) 91{ 92 return nir_shader_instructions_pass(nir, lower_image_instr, 93 nir_metadata_block_index | 94 nir_metadata_dominance, (void*)options); 95} 96