Path: blob/21.2-virgl/src/panfrost/util/pan_lower_sample_position.c
4560 views
/*1* Copyright (C) 2021 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 "pan_ir.h"24#include "compiler/nir/nir_builder.h"2526/* Sample positions are supplied in a packed 8:8 fixed-point vec2 format in GPU27* memory indexed by the sample. We lower in NIR to take advantage of possible28* ALU optimizations at the end. This is convenient for Bifrost, since the29* sample positions are passed in this format and it saves the driver from any30* system value handling. For Midgard, it's a bit suboptimal (fp16 positions31* could be supplied directly), but this lets us unify the implementation, and32* it's a pretty trivial difference */3334static bool35pan_lower_sample_pos_impl(struct nir_builder *b,36nir_instr *instr, UNUSED void *data)37{38if (instr->type != nir_instr_type_intrinsic)39return false;4041nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);42if (intr->intrinsic != nir_intrinsic_load_sample_pos)43return false;4445b->cursor = nir_before_instr(instr);4647/* Elements are 4 bytes */48nir_ssa_def *addr = nir_iadd(b,49nir_load_sample_positions_pan(b),50nir_u2u64(b, nir_imul_imm(b, nir_load_sample_id(b), 4)));5152/* Decode 8:8 fixed-point */53nir_ssa_def *raw = nir_load_global(b, addr, 2, 2, 16);54nir_ssa_def *decoded = nir_fmul_imm(b, nir_i2f16(b, raw), 1.0 / 256.0);5556/* Make NIR validator happy */57if (decoded->bit_size != nir_dest_bit_size(intr->dest))58decoded = nir_f2fN(b, decoded, nir_dest_bit_size(intr->dest));5960nir_ssa_def_rewrite_uses(&intr->dest.ssa, decoded);61return true;62}6364bool65pan_lower_sample_pos(nir_shader *shader)66{67if (shader->info.stage != MESA_SHADER_FRAGMENT)68return false;6970return nir_shader_instructions_pass(shader,71pan_lower_sample_pos_impl,72nir_metadata_block_index | nir_metadata_dominance,73NULL);74}757677