Path: blob/21.2-virgl/src/panfrost/midgard/midgard_errata_lod.c
4564 views
/*1* Copyright (C) 2019 Collabora, Ltd.2*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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "compiler/nir/nir.h"24#include "compiler/nir/nir_builder.h"2526bool midgard_nir_lod_errata(nir_shader *shader);2728/* Workarounds errata pertaining to early Midgard chips where the settings for29* min_lod/max_lod/lod_bias are ignored in the sampler descriptor when30* texturing with a textureLod instruction. The workaround is to load these31* constants in as system values and perform the bias/clamp in the shader.32*/3334static bool35nir_lod_errata_instr(nir_builder *b, nir_instr *instr, void *data)36{37if (instr->type != nir_instr_type_tex)38return false;3940nir_tex_instr *tex = nir_instr_as_tex(instr);41b->cursor = nir_before_instr(instr);4243/* The errata only applies to textureLod ("TEXGRD") */44if (tex->op != nir_texop_txl)45return false;4647/* Let's grab the sampler parameters */48nir_intrinsic_instr *l = nir_intrinsic_instr_create(b->shader,49nir_intrinsic_load_sampler_lod_parameters_pan);50l->num_components = 3;51nir_ssa_dest_init(&l->instr, &l->dest, 3, 32, NULL);5253/* TODO: Indirect samplers, separate sampler objects XXX */54nir_src idx = nir_src_for_ssa(nir_imm_int(b, tex->texture_index));55nir_src_copy(&l->src[0], &idx, l);5657nir_builder_instr_insert(b, &l->instr);58nir_ssa_def *params = &l->dest.ssa;5960/* Extract the individual components */61nir_ssa_def *min_lod = nir_channel(b, params, 0);62nir_ssa_def *max_lod = nir_channel(b, params, 1);63nir_ssa_def *lod_bias = nir_channel(b, params, 2);6465/* Rewrite the LOD with bias/clamps. Order sensitive. */66for (unsigned i = 0; i < tex->num_srcs; i++) {67if (tex->src[i].src_type != nir_tex_src_lod)68continue;6970nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[i].src, 1);7172nir_ssa_def *biased = nir_fadd(b, lod, lod_bias);73nir_ssa_def *clamped = nir_fmin(b,74nir_fmax(b, biased, min_lod), max_lod);7576nir_instr_rewrite_src(&tex->instr, &tex->src[i].src,77nir_src_for_ssa(clamped));78}7980return true;81}8283bool84midgard_nir_lod_errata(nir_shader *shader)85{86return nir_shader_instructions_pass(shader,87nir_lod_errata_instr,88nir_metadata_block_index | nir_metadata_dominance,89NULL);90}919293