Path: blob/21.2-virgl/src/freedreno/ir3/ir3_nir_lower_tg4_to_tex.c
4565 views
/*1* Copyright © 2017 Ilia Mirkin2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "compiler/nir/nir_builder.h"24#include "ir3_nir.h"2526/* A4XX has a broken GATHER4 operation. It performs the texture swizzle on the27* gather results, rather than before. As a result, it must be emulated with28* direct texture calls.29*/3031static nir_ssa_def *32ir3_nir_lower_tg4_to_tex_instr(nir_builder *b, nir_instr *instr, void *data)33{34nir_tex_instr *tg4 = nir_instr_as_tex(instr);35static const int offsets[3][2] = {{0, 1}, {1, 1}, {1, 0}};3637nir_ssa_def *results[4];38int offset_index = nir_tex_instr_src_index(tg4, nir_tex_src_offset);39for (int i = 0; i < 4; i++) {40int num_srcs = tg4->num_srcs + 1 /* lod */;41if (offset_index < 0 && i < 3)42num_srcs++;4344nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);45tex->op = nir_texop_txl;46tex->sampler_dim = tg4->sampler_dim;47tex->coord_components = tg4->coord_components;48tex->is_array = tg4->is_array;49tex->is_shadow = tg4->is_shadow;50tex->is_new_style_shadow = tg4->is_new_style_shadow;51tex->texture_index = tg4->texture_index;52tex->sampler_index = tg4->sampler_index;53tex->dest_type = tg4->dest_type;5455for (int j = 0; j < tg4->num_srcs; j++) {56nir_src_copy(&tex->src[j].src, &tg4->src[j].src, tex);57tex->src[j].src_type = tg4->src[j].src_type;58}59if (i != 3) {60nir_ssa_def *offset = nir_vec2(b, nir_imm_int(b, offsets[i][0]),61nir_imm_int(b, offsets[i][1]));62if (offset_index < 0) {63tex->src[tg4->num_srcs].src = nir_src_for_ssa(offset);64tex->src[tg4->num_srcs].src_type = nir_tex_src_offset;65} else {66assert(nir_tex_instr_src_size(tex, offset_index) == 2);67nir_ssa_def *orig =68nir_ssa_for_src(b, tex->src[offset_index].src, 2);69tex->src[offset_index].src =70nir_src_for_ssa(nir_iadd(b, orig, offset));71}72}73tex->src[num_srcs - 1].src = nir_src_for_ssa(nir_imm_float(b, 0));74tex->src[num_srcs - 1].src_type = nir_tex_src_lod;7576nir_ssa_dest_init(&tex->instr, &tex->dest, nir_tex_instr_dest_size(tex),7732, NULL);78nir_builder_instr_insert(b, &tex->instr);7980results[i] = nir_channel(b, &tex->dest.ssa, tg4->component);81}8283return nir_vec(b, results, 4);84}8586static bool87ir3_nir_lower_tg4_to_tex_filter(const nir_instr *instr, const void *data)88{89return (instr->type == nir_instr_type_tex &&90nir_instr_as_tex(instr)->op == nir_texop_tg4);91}9293bool94ir3_nir_lower_tg4_to_tex(nir_shader *shader)95{96return nir_shader_lower_instructions(shader, ir3_nir_lower_tg4_to_tex_filter,97ir3_nir_lower_tg4_to_tex_instr, NULL);98}99100101