1 /* 2 * Copyright 2015 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 * Authors: 24 * Jason Ekstrand <jason (at) jlekstrand.net> 25 */ 26 27 #include "nir.h" 28 #include "nir_builder.h" 29 30 /** 31 * This file implements a NIR lowering pass to perform the normalization of 32 * the cubemap coordinates to have the largest magnitude component be -1.0 33 * or 1.0. This is based on the old GLSL IR based pass by Eric. 34 */ 35 36 static bool 37 normalize_cubemap_coords_block(nir_block *block, nir_builder *b) 38 { 39 bool progress = false; 40 41 nir_foreach_instr(instr, block) { 42 if (instr->type != nir_instr_type_tex) 43 continue; 44 45 nir_tex_instr *tex = nir_instr_as_tex(instr); 46 if (tex->sampler_dim != GLSL_SAMPLER_DIM_CUBE) 47 continue; 48 49 b->cursor = nir_before_instr(&tex->instr); 50 51 for (unsigned i = 0; i < tex->num_srcs; i++) { 52 if (tex->src[i].src_type != nir_tex_src_coord) 53 continue; 54 55 nir_ssa_def *orig_coord = 56 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i)); 57 assert(orig_coord->num_components >= 3); 58 59 nir_ssa_def *abs = nir_fabs(b, orig_coord); 60 nir_ssa_def *norm = nir_fmax(b, nir_channel(b, abs, 0), 61 nir_fmax(b, nir_channel(b, abs, 1), 62 nir_channel(b, abs, 2))); 63 64 nir_ssa_def *normalized = nir_fmul(b, orig_coord, nir_frcp(b, norm)); 65 66 /* Array indices don't have to be normalized, so make a new vector 67 * with the coordinate's array index untouched. 68 */ 69 if (tex->coord_components == 4) { 70 normalized = nir_vec4(b, 71 nir_channel(b, normalized, 0), 72 nir_channel(b, normalized, 1), 73 nir_channel(b, normalized, 2), 74 nir_channel(b, orig_coord, 3)); 75 } 76 77 nir_instr_rewrite_src(&tex->instr, 78 &tex->src[i].src, 79 nir_src_for_ssa(normalized)); 80 81 progress = true; 82 } 83 } 84 85 return progress; 86 } 87 88 static bool 89 normalize_cubemap_coords_impl(nir_function_impl *impl) 90 { 91 nir_builder b; 92 nir_builder_init(&b, impl); 93 bool progress = false; 94 95 nir_foreach_block(block, impl) { 96 progress |= normalize_cubemap_coords_block(block, &b); 97 } 98 99 nir_metadata_preserve(impl, nir_metadata_block_index | 100 nir_metadata_dominance); 101 102 return progress; 103 } 104 105 bool 106 nir_normalize_cubemap_coords(nir_shader *shader) 107 { 108 bool progress = false; 109 110 nir_foreach_function(function, shader) { 111 if (function->impl) 112 progress = normalize_cubemap_coords_impl(function->impl) || progress; 113 } 114 115 return progress; 116 } 117