xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/lavapipe/lvp_lower_input_attachments.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "lvp_lower_vulkan_resource.h"
27 
28 static nir_def *
load_frag_coord(nir_builder * b)29 load_frag_coord(nir_builder *b)
30 {
31    nir_variable *pos =
32       nir_find_variable_with_location(b->shader, nir_var_shader_in,
33                                       VARYING_SLOT_POS);
34    if (pos == NULL) {
35       pos = nir_variable_create(b->shader, nir_var_shader_in,
36                                            glsl_vec4_type(), NULL);
37       pos->data.location = VARYING_SLOT_POS;
38    }
39    /**
40     * From Vulkan spec:
41     *   "The OriginLowerLeft execution mode must not be used; fragment entry
42     *    points must declare OriginUpperLeft."
43     *
44     * So at this point origin_upper_left should be true
45     */
46    assert(b->shader->info.fs.origin_upper_left == true);
47 
48    return nir_load_var(b, pos);
49 }
50 
51 static bool
try_lower_input_load(nir_intrinsic_instr * load,bool use_fragcoord_sysval)52 try_lower_input_load(nir_intrinsic_instr *load, bool use_fragcoord_sysval)
53 {
54    nir_deref_instr *deref = nir_src_as_deref(load->src[0]);
55    assert(glsl_type_is_image(deref->type));
56 
57    enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type);
58    if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&
59        image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
60       return false;
61 
62    nir_builder b = nir_builder_at(nir_before_instr(&load->instr));
63 
64    nir_def *frag_coord = use_fragcoord_sysval ? nir_load_frag_coord(&b)
65                                                   : load_frag_coord(&b);
66    frag_coord = nir_f2i32(&b, frag_coord);
67    nir_def *offset = nir_trim_vector(&b, load->src[1].ssa, 2);
68    nir_def *pos = nir_iadd(&b, frag_coord, offset);
69 
70    nir_def *layer = nir_load_view_index(&b);
71    nir_def *coord =
72       nir_vec4(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer, nir_imm_int(&b, 0));
73 
74    nir_src_rewrite(&load->src[1], coord);
75 
76    return true;
77 }
78 
79 bool
lvp_lower_input_attachments(nir_shader * shader,bool use_fragcoord_sysval)80 lvp_lower_input_attachments(nir_shader *shader, bool use_fragcoord_sysval)
81 {
82    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
83    bool progress = false;
84 
85    nir_foreach_function_impl(impl, shader) {
86       nir_foreach_block(block, impl) {
87          nir_foreach_instr_safe(instr, block) {
88             if (instr->type != nir_instr_type_intrinsic)
89                continue;
90 
91             nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
92 
93             if (load->intrinsic != nir_intrinsic_image_deref_load)
94                continue;
95 
96             progress |= try_lower_input_load(load, use_fragcoord_sysval);
97          }
98       }
99    }
100 
101    return progress;
102 }
103