Path: blob/21.2-virgl/src/compiler/nir/nir_builtin_builder.c
4545 views
/*1* Copyright © 2018 Red Hat Inc.2* Copyright © 2015 Intel Corporation3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324#include <math.h>2526#include "nir.h"27#include "nir_builtin_builder.h"2829nir_ssa_def*30nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)31{32unsigned yzx[3] = { 1, 2, 0 };33unsigned zxy[3] = { 2, 0, 1 };3435return nir_fsub(b, nir_fmul(b, nir_swizzle(b, x, yzx, 3),36nir_swizzle(b, y, zxy, 3)),37nir_fmul(b, nir_swizzle(b, x, zxy, 3),38nir_swizzle(b, y, yzx, 3)));39}4041nir_ssa_def*42nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)43{44nir_ssa_def *cross = nir_cross3(b, x, y);4546return nir_vec4(b,47nir_channel(b, cross, 0),48nir_channel(b, cross, 1),49nir_channel(b, cross, 2),50nir_imm_intN_t(b, 0, cross->bit_size));51}5253nir_ssa_def*54nir_fast_length(nir_builder *b, nir_ssa_def *vec)55{56switch (vec->num_components) {57case 1: return nir_fsqrt(b, nir_fmul(b, vec, vec));58case 2: return nir_fsqrt(b, nir_fdot2(b, vec, vec));59case 3: return nir_fsqrt(b, nir_fdot3(b, vec, vec));60case 4: return nir_fsqrt(b, nir_fdot4(b, vec, vec));61case 8: return nir_fsqrt(b, nir_fdot8(b, vec, vec));62case 16: return nir_fsqrt(b, nir_fdot16(b, vec, vec));63default:64unreachable("Invalid number of components");65}66}6768nir_ssa_def*69nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)70{71nir_ssa_def *zero = nir_imm_intN_t(b, 0, x->bit_size);72nir_ssa_def *one = nir_imm_intN_t(b, 1, x->bit_size);7374nir_ssa_def *condeq = nir_feq(b, x, y);75nir_ssa_def *conddir = nir_flt(b, x, y);76nir_ssa_def *condzero = nir_feq(b, x, zero);7778uint64_t sign_mask = 1ull << (x->bit_size - 1);79uint64_t min_abs = 1;8081if (nir_is_denorm_flush_to_zero(b->shader->info.float_controls_execution_mode, x->bit_size)) {82switch (x->bit_size) {83case 16:84min_abs = 1 << 10;85break;86case 32:87min_abs = 1 << 23;88break;89case 64:90min_abs = 1ULL << 52;91break;92}9394/* Flush denorm to zero to avoid returning a denorm when condeq is true. */95x = nir_fmul(b, x, nir_imm_floatN_t(b, 1.0, x->bit_size));96}9798/* beware of: +/-0.0 - 1 == NaN */99nir_ssa_def *xn =100nir_bcsel(b,101condzero,102nir_imm_intN_t(b, sign_mask | min_abs, x->bit_size),103nir_isub(b, x, one));104105/* beware of -0.0 + 1 == -0x1p-149 */106nir_ssa_def *xp = nir_bcsel(b, condzero,107nir_imm_intN_t(b, min_abs, x->bit_size),108nir_iadd(b, x, one));109110/* nextafter can be implemented by just +/- 1 on the int value */111nir_ssa_def *res =112nir_bcsel(b, nir_ixor(b, conddir, nir_flt(b, x, zero)), xp, xn);113114return nir_nan_check2(b, x, y, nir_bcsel(b, condeq, x, res));115}116117nir_ssa_def*118nir_normalize(nir_builder *b, nir_ssa_def *vec)119{120if (vec->num_components == 1)121return nir_fsign(b, vec);122123nir_ssa_def *f0 = nir_imm_floatN_t(b, 0.0, vec->bit_size);124nir_ssa_def *f1 = nir_imm_floatN_t(b, 1.0, vec->bit_size);125nir_ssa_def *finf = nir_imm_floatN_t(b, INFINITY, vec->bit_size);126127/* scale the input to increase precision */128nir_ssa_def *maxc = nir_fmax_abs_vec_comp(b, vec);129nir_ssa_def *svec = nir_fdiv(b, vec, maxc);130/* for inf */131nir_ssa_def *finfvec = nir_copysign(b, nir_bcsel(b, nir_feq(b, vec, finf), f1, f0), f1);132133nir_ssa_def *temp = nir_bcsel(b, nir_feq(b, maxc, finf), finfvec, svec);134nir_ssa_def *res = nir_fmul(b, temp, nir_frsq(b, nir_fdot(b, temp, temp)));135136return nir_bcsel(b, nir_feq(b, maxc, f0), vec, res);137}138139nir_ssa_def*140nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_def *x)141{142nir_ssa_def *f2 = nir_imm_floatN_t(b, 2.0, x->bit_size);143nir_ssa_def *f3 = nir_imm_floatN_t(b, 3.0, x->bit_size);144145/* t = clamp((x - edge0) / (edge1 - edge0), 0, 1) */146nir_ssa_def *t =147nir_fsat(b, nir_fdiv(b, nir_fsub(b, x, edge0),148nir_fsub(b, edge1, edge0)));149150/* result = t * t * (3 - 2 * t) */151return nir_fmul(b, t, nir_fmul(b, t, nir_fsub(b, f3, nir_fmul(b, f2, t))));152}153154nir_ssa_def*155nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo)156{157assert(lo->num_components == hi->num_components);158assert(lo->bit_size == hi->bit_size);159160nir_ssa_def *res[NIR_MAX_VEC_COMPONENTS];161for (unsigned i = 0; i < lo->num_components; ++i) {162nir_ssa_def *vec = nir_vec2(b, nir_channel(b, lo, i), nir_channel(b, hi, i));163res[i] = nir_pack_bits(b, vec, vec->bit_size * 2);164}165166return nir_vec(b, res, lo->num_components);167}168169/**170* Compute xs[0] + xs[1] + xs[2] + ... using fadd.171*/172static nir_ssa_def *173build_fsum(nir_builder *b, nir_ssa_def **xs, int terms)174{175nir_ssa_def *accum = xs[0];176177for (int i = 1; i < terms; i++)178accum = nir_fadd(b, accum, xs[i]);179180return accum;181}182183nir_ssa_def *184nir_atan(nir_builder *b, nir_ssa_def *y_over_x)185{186const uint32_t bit_size = y_over_x->bit_size;187188nir_ssa_def *abs_y_over_x = nir_fabs(b, y_over_x);189nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, bit_size);190191/*192* range-reduction, first step:193*194* / y_over_x if |y_over_x| <= 1.0;195* x = <196* \ 1.0 / y_over_x otherwise197*/198nir_ssa_def *x = nir_fdiv(b, nir_fmin(b, abs_y_over_x, one),199nir_fmax(b, abs_y_over_x, one));200201/*202* approximate atan by evaluating polynomial:203*204* x * 0.9999793128310355 - x^3 * 0.3326756418091246 +205* x^5 * 0.1938924977115610 - x^7 * 0.1173503194786851 +206* x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444207*/208nir_ssa_def *x_2 = nir_fmul(b, x, x);209nir_ssa_def *x_3 = nir_fmul(b, x_2, x);210nir_ssa_def *x_5 = nir_fmul(b, x_3, x_2);211nir_ssa_def *x_7 = nir_fmul(b, x_5, x_2);212nir_ssa_def *x_9 = nir_fmul(b, x_7, x_2);213nir_ssa_def *x_11 = nir_fmul(b, x_9, x_2);214215nir_ssa_def *polynomial_terms[] = {216nir_fmul_imm(b, x, 0.9999793128310355f),217nir_fmul_imm(b, x_3, -0.3326756418091246f),218nir_fmul_imm(b, x_5, 0.1938924977115610f),219nir_fmul_imm(b, x_7, -0.1173503194786851f),220nir_fmul_imm(b, x_9, 0.0536813784310406f),221nir_fmul_imm(b, x_11, -0.0121323213173444f),222};223224nir_ssa_def *tmp =225build_fsum(b, polynomial_terms, ARRAY_SIZE(polynomial_terms));226227/* range-reduction fixup */228tmp = nir_fadd(b, tmp,229nir_fmul(b, nir_b2f(b, nir_flt(b, one, abs_y_over_x), bit_size),230nir_fadd_imm(b, nir_fmul_imm(b, tmp, -2.0f), M_PI_2)));231232/* sign fixup */233return nir_fmul(b, tmp, nir_fsign(b, y_over_x));234}235236nir_ssa_def *237nir_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)238{239assert(y->bit_size == x->bit_size);240const uint32_t bit_size = x->bit_size;241242nir_ssa_def *zero = nir_imm_floatN_t(b, 0, bit_size);243nir_ssa_def *one = nir_imm_floatN_t(b, 1, bit_size);244245/* If we're on the left half-plane rotate the coordinates π/2 clock-wise246* for the y=0 discontinuity to end up aligned with the vertical247* discontinuity of atan(s/t) along t=0. This also makes sure that we248* don't attempt to divide by zero along the vertical line, which may give249* unspecified results on non-GLSL 4.1-capable hardware.250*/251nir_ssa_def *flip = nir_fge(b, zero, x);252nir_ssa_def *s = nir_bcsel(b, flip, nir_fabs(b, x), y);253nir_ssa_def *t = nir_bcsel(b, flip, y, nir_fabs(b, x));254255/* If the magnitude of the denominator exceeds some huge value, scale down256* the arguments in order to prevent the reciprocal operation from flushing257* its result to zero, which would cause precision problems, and for s258* infinite would cause us to return a NaN instead of the correct finite259* value.260*261* If fmin and fmax are respectively the smallest and largest positive262* normalized floating point values representable by the implementation,263* the constants below should be in agreement with:264*265* huge <= 1 / fmin266* scale <= 1 / fmin / fmax (for |t| >= huge)267*268* In addition scale should be a negative power of two in order to avoid269* loss of precision. The values chosen below should work for most usual270* floating point representations with at least the dynamic range of ATI's271* 24-bit representation.272*/273const double huge_val = bit_size >= 32 ? 1e18 : 16384;274nir_ssa_def *huge = nir_imm_floatN_t(b, huge_val, bit_size);275nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),276nir_imm_floatN_t(b, 0.25, bit_size), one);277nir_ssa_def *rcp_scaled_t = nir_frcp(b, nir_fmul(b, t, scale));278nir_ssa_def *s_over_t = nir_fmul(b, nir_fmul(b, s, scale), rcp_scaled_t);279280/* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily281* that ∞/∞ = 1) in order to comply with the rather artificial rules282* inherited from IEEE 754-2008, namely:283*284* "atan2(±∞, −∞) is ±3π/4285* atan2(±∞, +∞) is ±π/4"286*287* Note that this is inconsistent with the rules for the neighborhood of288* zero that are based on iterated limits:289*290* "atan2(±0, −0) is ±π291* atan2(±0, +0) is ±0"292*293* but GLSL specifically allows implementations to deviate from IEEE rules294* at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as295* well).296*/297nir_ssa_def *tan = nir_bcsel(b, nir_feq(b, nir_fabs(b, x), nir_fabs(b, y)),298one, nir_fabs(b, s_over_t));299300/* Calculate the arctangent and fix up the result if we had flipped the301* coordinate system.302*/303nir_ssa_def *arc =304nir_fadd(b, nir_fmul_imm(b, nir_b2f(b, flip, bit_size), M_PI_2),305nir_atan(b, tan));306307/* Rather convoluted calculation of the sign of the result. When x < 0 we308* cannot use fsign because we need to be able to distinguish between309* negative and positive zero. We don't use bitwise arithmetic tricks for310* consistency with the GLSL front-end. When x >= 0 rcp_scaled_t will311* always be non-negative so this won't be able to distinguish between312* negative and positive zero, but we don't care because atan2 is313* continuous along the whole positive y = 0 half-line, so it won't affect314* the result significantly.315*/316return nir_bcsel(b, nir_flt(b, nir_fmin(b, y, rcp_scaled_t), zero),317nir_fneg(b, arc), arc);318}319320nir_ssa_def *321nir_get_texture_size(nir_builder *b, nir_tex_instr *tex)322{323b->cursor = nir_before_instr(&tex->instr);324325nir_tex_instr *txs;326327unsigned num_srcs = 1; /* One for the LOD */328for (unsigned i = 0; i < tex->num_srcs; i++) {329if (tex->src[i].src_type == nir_tex_src_texture_deref ||330tex->src[i].src_type == nir_tex_src_sampler_deref ||331tex->src[i].src_type == nir_tex_src_texture_offset ||332tex->src[i].src_type == nir_tex_src_sampler_offset ||333tex->src[i].src_type == nir_tex_src_texture_handle ||334tex->src[i].src_type == nir_tex_src_sampler_handle)335num_srcs++;336}337338txs = nir_tex_instr_create(b->shader, num_srcs);339txs->op = nir_texop_txs;340txs->sampler_dim = tex->sampler_dim;341txs->is_array = tex->is_array;342txs->is_shadow = tex->is_shadow;343txs->is_new_style_shadow = tex->is_new_style_shadow;344txs->texture_index = tex->texture_index;345txs->sampler_index = tex->sampler_index;346txs->dest_type = nir_type_int32;347348unsigned idx = 0;349for (unsigned i = 0; i < tex->num_srcs; i++) {350if (tex->src[i].src_type == nir_tex_src_texture_deref ||351tex->src[i].src_type == nir_tex_src_sampler_deref ||352tex->src[i].src_type == nir_tex_src_texture_offset ||353tex->src[i].src_type == nir_tex_src_sampler_offset ||354tex->src[i].src_type == nir_tex_src_texture_handle ||355tex->src[i].src_type == nir_tex_src_sampler_handle) {356nir_src_copy(&txs->src[idx].src, &tex->src[i].src, txs);357txs->src[idx].src_type = tex->src[i].src_type;358idx++;359}360}361/* Add in an LOD because some back-ends require it */362txs->src[idx].src = nir_src_for_ssa(nir_imm_int(b, 0));363txs->src[idx].src_type = nir_tex_src_lod;364365nir_ssa_dest_init(&txs->instr, &txs->dest,366nir_tex_instr_dest_size(txs), 32, NULL);367nir_builder_instr_insert(b, &txs->instr);368369return &txs->dest.ssa;370}371372nir_ssa_def *373nir_get_texture_lod(nir_builder *b, nir_tex_instr *tex)374{375b->cursor = nir_before_instr(&tex->instr);376377nir_tex_instr *tql;378379unsigned num_srcs = 0;380for (unsigned i = 0; i < tex->num_srcs; i++) {381if (tex->src[i].src_type == nir_tex_src_coord ||382tex->src[i].src_type == nir_tex_src_texture_deref ||383tex->src[i].src_type == nir_tex_src_sampler_deref ||384tex->src[i].src_type == nir_tex_src_texture_offset ||385tex->src[i].src_type == nir_tex_src_sampler_offset ||386tex->src[i].src_type == nir_tex_src_texture_handle ||387tex->src[i].src_type == nir_tex_src_sampler_handle)388num_srcs++;389}390391tql = nir_tex_instr_create(b->shader, num_srcs);392tql->op = nir_texop_lod;393tql->coord_components = tex->coord_components;394tql->sampler_dim = tex->sampler_dim;395tql->is_array = tex->is_array;396tql->is_shadow = tex->is_shadow;397tql->is_new_style_shadow = tex->is_new_style_shadow;398tql->texture_index = tex->texture_index;399tql->sampler_index = tex->sampler_index;400tql->dest_type = nir_type_float32;401402unsigned idx = 0;403for (unsigned i = 0; i < tex->num_srcs; i++) {404if (tex->src[i].src_type == nir_tex_src_coord ||405tex->src[i].src_type == nir_tex_src_texture_deref ||406tex->src[i].src_type == nir_tex_src_sampler_deref ||407tex->src[i].src_type == nir_tex_src_texture_offset ||408tex->src[i].src_type == nir_tex_src_sampler_offset ||409tex->src[i].src_type == nir_tex_src_texture_handle ||410tex->src[i].src_type == nir_tex_src_sampler_handle) {411nir_src_copy(&tql->src[idx].src, &tex->src[i].src, tql);412tql->src[idx].src_type = tex->src[i].src_type;413idx++;414}415}416417nir_ssa_dest_init(&tql->instr, &tql->dest, 2, 32, NULL);418nir_builder_instr_insert(b, &tql->instr);419420/* The LOD is the y component of the result */421return nir_channel(b, &tql->dest.ssa, 1);422}423424425