nir_builtin_builder.c revision 01e04c3f
1/* 2 * Copyright © 2018 Red Hat Inc. 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 "nir.h" 25#include "nir_builtin_builder.h" 26 27nir_ssa_def* 28nir_cross(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 29{ 30 unsigned yzx[3] = { 1, 2, 0 }; 31 unsigned zxy[3] = { 2, 0, 1 }; 32 33 return nir_fsub(b, nir_fmul(b, nir_swizzle(b, x, yzx, 3, true), 34 nir_swizzle(b, y, zxy, 3, true)), 35 nir_fmul(b, nir_swizzle(b, x, zxy, 3, true), 36 nir_swizzle(b, y, yzx, 3, true))); 37} 38 39nir_ssa_def* 40nir_fast_length(nir_builder *b, nir_ssa_def *vec) 41{ 42 switch (vec->num_components) { 43 case 1: return nir_fsqrt(b, nir_fmul(b, vec, vec)); 44 case 2: return nir_fsqrt(b, nir_fdot2(b, vec, vec)); 45 case 3: return nir_fsqrt(b, nir_fdot3(b, vec, vec)); 46 case 4: return nir_fsqrt(b, nir_fdot4(b, vec, vec)); 47 default: 48 unreachable("Invalid number of components"); 49 } 50} 51 52nir_ssa_def* 53nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_def *x) 54{ 55 nir_ssa_def *f2 = nir_imm_floatN_t(b, 2.0, x->bit_size); 56 nir_ssa_def *f3 = nir_imm_floatN_t(b, 3.0, x->bit_size); 57 58 /* t = clamp((x - edge0) / (edge1 - edge0), 0, 1) */ 59 nir_ssa_def *t = 60 nir_fsat(b, nir_fdiv(b, nir_fsub(b, x, edge0), 61 nir_fsub(b, edge1, edge0))); 62 63 /* result = t * t * (3 - 2 * t) */ 64 return nir_fmul(b, t, nir_fmul(b, t, nir_fsub(b, f3, nir_fmul(b, f2, t)))); 65} 66