Path: blob/21.2-virgl/src/compiler/glsl/gl_nir_lower_images.c
4545 views
/*1* Copyright © 2019 Red Hat Inc.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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223/**24* \file25*26* Lower image operations by turning the image_deref_* into a image_* on an27* index number or bindless_image_* intrinsic on a load_deref of the previous28* deref source. All applicable indicies are also set so that fetching the29* variable in the backend wouldn't be needed anymore.30*/3132#include "compiler/nir/nir.h"33#include "compiler/nir/nir_builder.h"34#include "compiler/nir/nir_deref.h"3536#include "compiler/glsl/gl_nir.h"3738static void39type_size_align_1(const struct glsl_type *type, unsigned *size, unsigned *align)40{41unsigned s;4243if (glsl_type_is_array(type))44s = glsl_get_aoa_size(type);45else46s = 1;4748*size = s;49*align = s;50}5152static bool53lower_impl(nir_builder *b, nir_instr *instr, bool bindless_only)54{55if (instr->type != nir_instr_type_intrinsic)56return false;5758nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);5960nir_deref_instr *deref;61nir_variable *var;6263switch (intrinsic->intrinsic) {64case nir_intrinsic_image_deref_atomic_add:65case nir_intrinsic_image_deref_atomic_imin:66case nir_intrinsic_image_deref_atomic_umin:67case nir_intrinsic_image_deref_atomic_imax:68case nir_intrinsic_image_deref_atomic_umax:69case nir_intrinsic_image_deref_atomic_and:70case nir_intrinsic_image_deref_atomic_or:71case nir_intrinsic_image_deref_atomic_xor:72case nir_intrinsic_image_deref_atomic_exchange:73case nir_intrinsic_image_deref_atomic_comp_swap:74case nir_intrinsic_image_deref_atomic_fadd:75case nir_intrinsic_image_deref_atomic_inc_wrap:76case nir_intrinsic_image_deref_atomic_dec_wrap:77case nir_intrinsic_image_deref_load:78case nir_intrinsic_image_deref_samples:79case nir_intrinsic_image_deref_size:80case nir_intrinsic_image_deref_store: {81deref = nir_src_as_deref(intrinsic->src[0]);82var = nir_deref_instr_get_variable(deref);83break;84}85default:86return false;87}8889bool bindless = var->data.mode != nir_var_uniform || var->data.bindless;90if (bindless_only && !bindless)91return false;9293b->cursor = nir_before_instr(instr);9495nir_ssa_def *src;96if (bindless) {97src = nir_load_deref(b, deref);98} else {99src = nir_iadd_imm(b,100nir_build_deref_offset(b, deref, type_size_align_1),101var->data.driver_location);102}103nir_rewrite_image_intrinsic(intrinsic, src, bindless);104105return true;106}107108bool109gl_nir_lower_images(nir_shader *shader, bool bindless_only)110{111bool progress = false;112113nir_foreach_function(function, shader) {114if (function->impl) {115nir_builder b;116nir_builder_init(&b, function->impl);117118bool impl_progress = false;119nir_foreach_block(block, function->impl)120nir_foreach_instr(instr, block)121impl_progress |= lower_impl(&b, instr, bindless_only);122123if (impl_progress) {124nir_metadata_preserve(function->impl,125nir_metadata_block_index |126nir_metadata_dominance);127progress = true;128} else {129nir_metadata_preserve(function->impl, nir_metadata_all);130}131}132}133134return progress;135}136137138