Path: blob/21.2-virgl/src/gallium/frontends/lavapipe/lvp_lower_input_attachments.c
4565 views
/*1* Copyright © 2016 Intel Corporation2*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 "nir.h"24#include "nir_builder.h"25#include "lvp_lower_vulkan_resource.h"2627static nir_ssa_def *28load_frag_coord(nir_builder *b)29{30nir_variable *pos =31nir_find_variable_with_location(b->shader, nir_var_shader_in,32VARYING_SLOT_POS);33if (pos == NULL) {34pos = nir_variable_create(b->shader, nir_var_shader_in,35glsl_vec4_type(), NULL);36pos->data.location = VARYING_SLOT_POS;37}38/**39* From Vulkan spec:40* "The OriginLowerLeft execution mode must not be used; fragment entry41* points must declare OriginUpperLeft."42*43* So at this point origin_upper_left should be true44*/45assert(b->shader->info.fs.origin_upper_left == true);4647return nir_load_var(b, pos);48}4950static bool51try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,52bool use_fragcoord_sysval)53{54nir_deref_instr *deref = nir_src_as_deref(load->src[0]);55assert(glsl_type_is_image(deref->type));5657enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type);58if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&59image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)60return false;6162nir_builder b;63nir_builder_init(&b, impl);64b.cursor = nir_before_instr(&load->instr);6566nir_ssa_def *frag_coord = use_fragcoord_sysval ? nir_load_frag_coord(&b)67: load_frag_coord(&b);68frag_coord = nir_f2i32(&b, frag_coord);69nir_ssa_def *offset = nir_ssa_for_src(&b, load->src[1], 2);70nir_ssa_def *pos = nir_iadd(&b, frag_coord, offset);7172nir_ssa_def *layer = nir_load_view_index(&b);73nir_ssa_def *coord =74nir_vec4(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer, nir_imm_int(&b, 0));7576nir_instr_rewrite_src(&load->instr, &load->src[1], nir_src_for_ssa(coord));7778return true;79}8081bool82lvp_lower_input_attachments(nir_shader *shader, bool use_fragcoord_sysval)83{84assert(shader->info.stage == MESA_SHADER_FRAGMENT);85bool progress = false;8687nir_foreach_function(function, shader) {88if (!function->impl)89continue;9091nir_foreach_block(block, function->impl) {92nir_foreach_instr_safe(instr, block) {93if (instr->type != nir_instr_type_intrinsic)94continue;9596nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);9798if (load->intrinsic != nir_intrinsic_image_deref_load)99continue;100101progress |= try_lower_input_load(function->impl, load,102use_fragcoord_sysval);103}104}105}106107return progress;108}109110111