xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_samplers.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
3  * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
4  * Copyright © 2014 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "nir/nir.h"
27 #include "nir_builder.h"
28 
29 static void
lower_tex_src_to_offset(nir_builder * b,nir_tex_instr * instr,unsigned src_idx)30 lower_tex_src_to_offset(nir_builder *b,
31                         nir_tex_instr *instr, unsigned src_idx)
32 {
33    nir_def *index = NULL;
34    unsigned base_index = 0;
35    unsigned array_elements = 1;
36    nir_tex_src *src = &instr->src[src_idx];
37    bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
38 
39    /* We compute first the offsets */
40    nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
41    while (deref->deref_type != nir_deref_type_var) {
42       nir_deref_instr *parent =
43          nir_instr_as_deref(deref->parent.ssa->parent_instr);
44 
45       assert(deref->deref_type == nir_deref_type_array);
46 
47       if (nir_src_is_const(deref->arr.index) && index == NULL) {
48          /* We're still building a direct index */
49          unsigned index_in_array = nir_src_as_uint(deref->arr.index);
50 
51          /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
52           *
53           *    In the subsections described above for array, vector, matrix and
54           *    structure accesses, any out-of-bounds access produced undefined
55           *    behavior.... Out-of-bounds reads return undefined values, which
56           *    include values from other variables of the active program or zero.
57           *
58           * Robustness extensions suggest to return zero on out-of-bounds
59           * accesses, however it's not applicable to the arrays of samplers,
60           * so just clamp the index.
61           *
62           * Otherwise instr->sampler_index or instr->texture_index would be out
63           * of bounds, and they are used as an index to arrays of driver state.
64           */
65          if (index_in_array < glsl_array_size(parent->type)) {
66             base_index += index_in_array * array_elements;
67          } else {
68             base_index = glsl_array_size(parent->type) - 1;
69          }
70       } else {
71          if (index == NULL) {
72             /* We used to be direct but not anymore */
73             index = nir_imm_int(b, base_index);
74             base_index = 0;
75          }
76 
77          index = nir_iadd(b, index,
78                           nir_imul_imm(b,
79                                        deref->arr.index.ssa,
80                                        array_elements));
81       }
82 
83       array_elements *= glsl_get_length(parent->type);
84 
85       deref = parent;
86    }
87 
88    if (index)
89       index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
90 
91    /* We hit the deref_var.  This is the end of the line */
92    assert(deref->deref_type == nir_deref_type_var);
93 
94    base_index += deref->var->data.binding;
95 
96    /* We have the offsets, we apply them, rewriting the source or removing
97     * instr if needed
98     */
99    if (index) {
100       nir_src_rewrite(&src->src, index);
101 
102       src->src_type = is_sampler ? nir_tex_src_sampler_offset : nir_tex_src_texture_offset;
103    } else {
104       nir_tex_instr_remove_src(instr, src_idx);
105    }
106 
107    if (is_sampler) {
108       instr->sampler_index = base_index;
109    } else {
110       instr->texture_index = base_index;
111    }
112 }
113 
114 static bool
lower_sampler(nir_builder * b,nir_instr * instr_,UNUSED void * cb_data)115 lower_sampler(nir_builder *b, nir_instr *instr_, UNUSED void *cb_data)
116 {
117    if (instr_->type != nir_instr_type_tex)
118       return false;
119 
120    nir_tex_instr *instr = nir_instr_as_tex(instr_);
121 
122    int texture_idx =
123       nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
124 
125    if (texture_idx >= 0) {
126       b->cursor = nir_before_instr(&instr->instr);
127 
128       lower_tex_src_to_offset(b, instr, texture_idx);
129    }
130 
131    int sampler_idx =
132       nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
133 
134    if (sampler_idx >= 0) {
135       lower_tex_src_to_offset(b, instr, sampler_idx);
136    }
137 
138    if (texture_idx < 0 && sampler_idx < 0)
139       return false;
140 
141    return true;
142 }
143 
144 bool
nir_lower_samplers(nir_shader * shader)145 nir_lower_samplers(nir_shader *shader)
146 {
147    return nir_shader_instructions_pass(shader, lower_sampler,
148                                        nir_metadata_control_flow,
149                                        NULL);
150 }
151