1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** 25bf215546Sopenharmony_ci * This lowering pass supports (as configured via nir_lower_image_options) 26bf215546Sopenharmony_ci * image related conversions: 27bf215546Sopenharmony_ci * + cube array size lowering. The size operation is converted from cube 28bf215546Sopenharmony_ci * size to a 2d-array with the z component divided by 6. 29bf215546Sopenharmony_ci */ 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci#include "nir.h" 32bf215546Sopenharmony_ci#include "nir_builder.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cistatic void 35bf215546Sopenharmony_cilower_cube_size(nir_builder *b, nir_intrinsic_instr *intrin) 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci assert(nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE); 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci b->cursor = nir_before_instr(&intrin->instr); 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci nir_intrinsic_instr *_2darray_size = 42bf215546Sopenharmony_ci nir_instr_as_intrinsic(nir_instr_clone(b->shader, &intrin->instr)); 43bf215546Sopenharmony_ci nir_intrinsic_set_image_dim(_2darray_size, GLSL_SAMPLER_DIM_2D); 44bf215546Sopenharmony_ci nir_intrinsic_set_image_array(_2darray_size, true); 45bf215546Sopenharmony_ci nir_builder_instr_insert(b, &_2darray_size->instr); 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci nir_ssa_def *size = nir_instr_ssa_def(&_2darray_size->instr); 48bf215546Sopenharmony_ci nir_ssa_scalar comps[NIR_MAX_VEC_COMPONENTS] = { 0 }; 49bf215546Sopenharmony_ci unsigned coord_comps = intrin->dest.ssa.num_components; 50bf215546Sopenharmony_ci for (unsigned c = 0; c < coord_comps; c++) { 51bf215546Sopenharmony_ci if (c == 2) { 52bf215546Sopenharmony_ci comps[2] = nir_get_ssa_scalar(nir_idiv(b, nir_channel(b, size, 2), nir_imm_int(b, 6)), 0); 53bf215546Sopenharmony_ci } else { 54bf215546Sopenharmony_ci comps[c] = nir_get_ssa_scalar(size, c); 55bf215546Sopenharmony_ci } 56bf215546Sopenharmony_ci } 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci nir_ssa_def *vec = nir_vec_scalars(b, comps, intrin->dest.ssa.num_components); 59bf215546Sopenharmony_ci nir_ssa_def_rewrite_uses(&intrin->dest.ssa, vec); 60bf215546Sopenharmony_ci nir_instr_remove(&intrin->instr); 61bf215546Sopenharmony_ci nir_instr_free(&intrin->instr); 62bf215546Sopenharmony_ci} 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cistatic bool 65bf215546Sopenharmony_cilower_image_instr(nir_builder *b, nir_instr *instr, void *state) 66bf215546Sopenharmony_ci{ 67bf215546Sopenharmony_ci if (instr->type != nir_instr_type_intrinsic) 68bf215546Sopenharmony_ci return false; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci const nir_lower_image_options *options = state; 71bf215546Sopenharmony_ci nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci switch (intrin->intrinsic) { 74bf215546Sopenharmony_ci case nir_intrinsic_image_size: 75bf215546Sopenharmony_ci case nir_intrinsic_image_deref_size: 76bf215546Sopenharmony_ci case nir_intrinsic_bindless_image_size: 77bf215546Sopenharmony_ci if (options->lower_cube_size && 78bf215546Sopenharmony_ci nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_CUBE) { 79bf215546Sopenharmony_ci lower_cube_size(b, intrin); 80bf215546Sopenharmony_ci return true; 81bf215546Sopenharmony_ci } 82bf215546Sopenharmony_ci return false; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci default: 85bf215546Sopenharmony_ci return false; 86bf215546Sopenharmony_ci } 87bf215546Sopenharmony_ci} 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_cibool 90bf215546Sopenharmony_cinir_lower_image(nir_shader *nir, const nir_lower_image_options *options) 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci return nir_shader_instructions_pass(nir, lower_image_instr, 93bf215546Sopenharmony_ci nir_metadata_block_index | 94bf215546Sopenharmony_ci nir_metadata_dominance, (void*)options); 95bf215546Sopenharmony_ci} 96