1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "compiler/nir/nir.h" 25bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_cibool midgard_nir_lod_errata(nir_shader *shader); 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci/* Workarounds errata pertaining to early Midgard chips where the settings for 30bf215546Sopenharmony_ci * min_lod/max_lod/lod_bias are ignored in the sampler descriptor when 31bf215546Sopenharmony_ci * texturing with a textureLod instruction. The workaround is to load these 32bf215546Sopenharmony_ci * constants in as system values and perform the bias/clamp in the shader. 33bf215546Sopenharmony_ci */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistatic bool 36bf215546Sopenharmony_cinir_lod_errata_instr(nir_builder *b, nir_instr *instr, void *data) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci if (instr->type != nir_instr_type_tex) 39bf215546Sopenharmony_ci return false; 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci nir_tex_instr *tex = nir_instr_as_tex(instr); 42bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci /* The errata only applies to textureLod ("TEXGRD") */ 45bf215546Sopenharmony_ci if (tex->op != nir_texop_txl) 46bf215546Sopenharmony_ci return false; 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci /* Let's grab the sampler parameters */ 49bf215546Sopenharmony_ci nir_intrinsic_instr *l = nir_intrinsic_instr_create(b->shader, 50bf215546Sopenharmony_ci nir_intrinsic_load_sampler_lod_parameters_pan); 51bf215546Sopenharmony_ci l->num_components = 3; 52bf215546Sopenharmony_ci nir_ssa_dest_init(&l->instr, &l->dest, 3, 32, NULL); 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci /* TODO: Indirect samplers, separate sampler objects XXX */ 55bf215546Sopenharmony_ci nir_src idx = nir_src_for_ssa(nir_imm_int(b, tex->texture_index)); 56bf215546Sopenharmony_ci nir_src_copy(&l->src[0], &idx); 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci nir_builder_instr_insert(b, &l->instr); 59bf215546Sopenharmony_ci nir_ssa_def *params = &l->dest.ssa; 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci /* Extract the individual components */ 62bf215546Sopenharmony_ci nir_ssa_def *min_lod = nir_channel(b, params, 0); 63bf215546Sopenharmony_ci nir_ssa_def *max_lod = nir_channel(b, params, 1); 64bf215546Sopenharmony_ci nir_ssa_def *lod_bias = nir_channel(b, params, 2); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci /* Rewrite the LOD with bias/clamps. Order sensitive. */ 67bf215546Sopenharmony_ci for (unsigned i = 0; i < tex->num_srcs; i++) { 68bf215546Sopenharmony_ci if (tex->src[i].src_type != nir_tex_src_lod) 69bf215546Sopenharmony_ci continue; 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[i].src, 1); 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci nir_ssa_def *biased = nir_fadd(b, lod, lod_bias); 74bf215546Sopenharmony_ci nir_ssa_def *clamped = nir_fmin(b, 75bf215546Sopenharmony_ci nir_fmax(b, biased, min_lod), max_lod); 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci nir_instr_rewrite_src(&tex->instr, &tex->src[i].src, 78bf215546Sopenharmony_ci nir_src_for_ssa(clamped)); 79bf215546Sopenharmony_ci } 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_ci return true; 82bf215546Sopenharmony_ci} 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_cibool 85bf215546Sopenharmony_cimidgard_nir_lod_errata(nir_shader *shader) 86bf215546Sopenharmony_ci{ 87bf215546Sopenharmony_ci return nir_shader_instructions_pass(shader, 88bf215546Sopenharmony_ci nir_lod_errata_instr, 89bf215546Sopenharmony_ci nir_metadata_block_index | nir_metadata_dominance, 90bf215546Sopenharmony_ci NULL); 91bf215546Sopenharmony_ci} 92